CFB Mode

Jean-Luc Cooke jlcooke at certainkey.com
Fri Sep 24 05:35:22 CEST 2004


Here it is.

Thoughts?

JLC


On Wed, Sep 15, 2004 at 11:42:49AM -0400, Jean-Luc Cooke wrote:
> Care to write up an OFB just to be complete?  :)
> 
> JLC
> 
> On Wed, Sep 15, 2004 at 09:08:12AM -0500, Michael Halcrow wrote:
> > On Tue, Sep 14, 2004 at 04:06:30PM -0400, Jean-Luc Cooke wrote:
> > > my laptop died a few days ago and I've been busy.  I will re-install
> > > tonight and get this going.  Can you send me your CFB code please?
> > > I'll slip it into the same patch once I get testvect's working.
> > 
> > Well, I have not tested it yet, but it compiles.  If it does not work
> > for you, please don't waste too much time on it - I will be testing it
> > later on today.
> > 
> > Mike
> > .___________________________________________________________________.
> >                          Michael A. Halcrow                          
> >        Security Software Engineer, IBM Linux Technology Center       
> > GnuPG Fingerprint: 05B5 08A8 713A 64C1 D35D  2371 2D3C FDDA 3EB6 601D
> > 
> > "The whole problem with the world is that fools and fanatics are     
> > always so certain of themselves, and wiser people so full of         
> > doubts."                                                             
> >  - Bertrand Russell 
> 
> > /*
> >  * Cryptographic API.
> >  *
> >  * Cipher operations.
> >  *
> >  * Copyright (c) 2002 James Morris <jmorris at intercode.com.au>
> >  * Copyright (c) 2004 International Business Machines
> >  * <mahalcro at us.ibm.com>
> >  *
> >  * This program is free software; you can redistribute it and/or modify it
> >  * under the terms of the GNU General Public License as published by the Free
> >  * Software Foundation; either version 2 of the License, or (at your option) 
> >  * any later version.
> >  *
> >  */
> > #include <linux/kernel.h>
> > #include <linux/crypto.h>
> > #include <linux/errno.h>
> > #include <linux/mm.h>
> > #include <linux/slab.h>
> > #include <asm/scatterlist.h>
> > #include "internal.h"
> > #include "scatterwalk.h"
> > 
> > typedef void (cryptfn_t)(void *, u8 *, const u8 *);
> > typedef void (procfn_t)(struct crypto_tfm *, u8 *,
> >                         u8*, cryptfn_t, int enc, void *, int);
> > 
> > static inline void xor_64(u8 *a, const u8 *b)
> > {
> > 	((u32 *)a)[0] ^= ((u32 *)b)[0];
> > 	((u32 *)a)[1] ^= ((u32 *)b)[1];
> > }
> > 
> > static inline void xor_128(u8 *a, const u8 *b)
> > {
> > 	((u32 *)a)[0] ^= ((u32 *)b)[0];
> > 	((u32 *)a)[1] ^= ((u32 *)b)[1];
> > 	((u32 *)a)[2] ^= ((u32 *)b)[2];
> > 	((u32 *)a)[3] ^= ((u32 *)b)[3];
> > }
> > 
> > 
> > /* 
> >  * Generic encrypt/decrypt wrapper for ciphers, handles operations across
> >  * multiple page boundaries by using temporary blocks.  In user context,
> >  * the kernel is given a chance to schedule us once per block.
> >  */
> > static int crypt(struct crypto_tfm *tfm,
> > 		 struct scatterlist *dst,
> > 		 struct scatterlist *src,
> >                  unsigned int nbytes, cryptfn_t crfn,
> >                  procfn_t prfn, int enc, void *info)
> > {
> > 	struct scatter_walk walk_in, walk_out;
> > 	const unsigned int bsize = crypto_tfm_alg_blocksize(tfm);
> > 	u8 tmp_src[bsize];
> > 	u8 tmp_dst[bsize];
> > 
> > 	if (!nbytes)
> > 		return 0;
> > 
> > 	if (nbytes % bsize) {
> > 		tfm->crt_flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
> > 		return -EINVAL;
> > 	}
> > 
> > 	scatterwalk_start(&walk_in, src);
> > 	scatterwalk_start(&walk_out, dst);
> > 
> > 	for(;;) {
> > 		u8 *src_p, *dst_p;
> > 		int in_place;
> > 
> > 		scatterwalk_map(&walk_in, 0);
> > 		scatterwalk_map(&walk_out, 1);
> > 		src_p = scatterwalk_whichbuf(&walk_in, bsize, tmp_src);
> > 		dst_p = scatterwalk_whichbuf(&walk_out, bsize, tmp_dst);
> > 		in_place = scatterwalk_samebuf(&walk_in, &walk_out,
> > 					       src_p, dst_p);
> > 
> > 		nbytes -= bsize;
> > 
> > 		scatterwalk_copychunks(src_p, &walk_in, bsize, 0);
> > 
> > 		prfn(tfm, dst_p, src_p, crfn, enc, info, in_place);
> > 
> > 		scatterwalk_done(&walk_in, 0, nbytes);
> > 
> > 		scatterwalk_copychunks(dst_p, &walk_out, bsize, 1);
> > 		scatterwalk_done(&walk_out, 1, nbytes);
> > 
> > 		if (!nbytes)
> > 			return 0;
> > 
> > 		crypto_yield(tfm);
> > 	}
> > }
> > 
> > /* n-bit CFB mode with n-bit block cipher */
> > static void cfb_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
> > 			cryptfn_t fn, int enc, void *info, int in_place)
> > {
> > 	u8 *iv = info;
> > 	/* Null encryption */
> > 	if (!iv)
> > 		return;
> > 	if (enc) {
> > 		if (in_place) {
> > 			u8 buf[crypto_tfm_alg_blocksize(tfm)];
> > 			fn(crypto_tfm_ctx(tfm), buf, iv);
> > 			tfm->crt_u.cipher.cit_xor_block(buf, src);
> > 			memcpy(dst, buf, crypto_tfm_alg_blocksize(tfm));
> > 			memcpy(iv, buf, crypto_tfm_alg_blocksize(tfm));
> > 		} else {
> > 			fn(crypto_tfm_ctx(tfm), dst, iv);
> > 			tfm->crt_u.cipher.cit_xor_block(dst, src);
> > 			memcpy(iv, dst, crypto_tfm_alg_blocksize(tfm));
> > 		}
> > 	} else {
> > 		if (in_place) {
> > 			u8 buf[crypto_tfm_alg_blocksize(tfm)];
> > 			fn(crypto_tfm_ctx(tfm), buf, iv);
> > 			tfm->crt_u.cipher.cit_xor_block(buf, src);
> > 			memcpy(dst, buf, crypto_tfm_alg_blocksize(tfm));
> > 		} else {
> > 			fn(crypto_tfm_ctx(tfm), dst, iv);
> > 			tfm->crt_u.cipher.cit_xor_block(dst, src);
> > 		}
> > 		memcpy(iv, src, crypto_tfm_alg_blocksize(tfm));
> > 	}
> > }
> > 
> > static void cbc_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
> > 			cryptfn_t fn, int enc, void *info, int in_place)
> > {
> > 	u8 *iv = info;
> > 	
> > 	/* Null encryption */
> > 	if (!iv)
> > 		return;
> > 		
> > 	if (enc) {
> > 		tfm->crt_u.cipher.cit_xor_block(iv, src);
> > 		fn(crypto_tfm_ctx(tfm), dst, iv);
> > 		memcpy(iv, dst, crypto_tfm_alg_blocksize(tfm));
> > 	} else {
> > 		u8 stack[in_place ? crypto_tfm_alg_blocksize(tfm) : 0];
> > 		u8 *buf = in_place ? stack : dst;
> > 
> > 		fn(crypto_tfm_ctx(tfm), buf, src);
> > 		tfm->crt_u.cipher.cit_xor_block(buf, iv);
> > 		memcpy(iv, src, crypto_tfm_alg_blocksize(tfm));
> > 		if (buf != dst)
> > 			memcpy(dst, buf, crypto_tfm_alg_blocksize(tfm));
> > 	}
> > }
> > 
> > static void ecb_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
> > 			cryptfn_t fn, int enc, void *info, int in_place)
> > {
> > 	fn(crypto_tfm_ctx(tfm), dst, src);
> > }
> > 
> > static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
> > {
> > 	struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
> > 	
> > 	if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
> > 		tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
> > 		return -EINVAL;
> > 	} else
> > 		return cia->cia_setkey(crypto_tfm_ctx(tfm), key, keylen,
> > 		                       &tfm->crt_flags);
> > }
> > 
> > static int ecb_encrypt(struct crypto_tfm *tfm,
> > 		       struct scatterlist *dst,
> >                        struct scatterlist *src, unsigned int nbytes)
> > {
> > 	return crypt(tfm, dst, src, nbytes,
> > 	             tfm->__crt_alg->cra_cipher.cia_encrypt,
> > 	             ecb_process, 1, NULL);
> > }
> > 
> > static int ecb_decrypt(struct crypto_tfm *tfm,
> >                        struct scatterlist *dst,
> >                        struct scatterlist *src,
> > 		       unsigned int nbytes)
> > {
> > 	return crypt(tfm, dst, src, nbytes,
> > 	             tfm->__crt_alg->cra_cipher.cia_decrypt,
> > 	             ecb_process, 1, NULL);
> > }
> > 
> > static int cbc_encrypt(struct crypto_tfm *tfm,
> >                        struct scatterlist *dst,
> >                        struct scatterlist *src,
> > 		       unsigned int nbytes)
> > {
> > 	return crypt(tfm, dst, src, nbytes,
> > 	             tfm->__crt_alg->cra_cipher.cia_encrypt,
> > 	             cbc_process, 1, tfm->crt_cipher.cit_iv);
> > }
> > 
> > static int cbc_encrypt_iv(struct crypto_tfm *tfm,
> >                           struct scatterlist *dst,
> >                           struct scatterlist *src,
> >                           unsigned int nbytes, u8 *iv)
> > {
> > 	return crypt(tfm, dst, src, nbytes,
> > 	             tfm->__crt_alg->cra_cipher.cia_encrypt,
> > 	             cbc_process, 1, iv);
> > }
> > 
> > static int cbc_decrypt(struct crypto_tfm *tfm,
> >                        struct scatterlist *dst,
> >                        struct scatterlist *src,
> > 		       unsigned int nbytes)
> > {
> > 	return crypt(tfm, dst, src, nbytes,
> > 	             tfm->__crt_alg->cra_cipher.cia_decrypt,
> > 	             cbc_process, 0, tfm->crt_cipher.cit_iv);
> > }
> > 
> > static int cbc_decrypt_iv(struct crypto_tfm *tfm,
> >                           struct scatterlist *dst,
> >                           struct scatterlist *src,
> >                           unsigned int nbytes, u8 *iv)
> > {
> > 	return crypt(tfm, dst, src, nbytes,
> > 	             tfm->__crt_alg->cra_cipher.cia_decrypt,
> > 	             cbc_process, 0, iv);
> > }
> > 
> > static int cfb_encrypt(struct crypto_tfm *tfm,
> >                        struct scatterlist *dst,
> >                        struct scatterlist *src,
> > 		       unsigned int nbytes)
> > {
> > 	return crypt(tfm, dst, src, nbytes,
> > 	             tfm->__crt_alg->cra_cipher.cia_encrypt,
> > 	             cfb_process, 1, tfm->crt_cipher.cit_iv);
> > }
> > 
> > static int cfb_encrypt_iv(struct crypto_tfm *tfm,
> >                           struct scatterlist *dst,
> >                           struct scatterlist *src,
> >                           unsigned int nbytes, u8 *iv)
> > {
> > 	return crypt(tfm, dst, src, nbytes,
> > 	             tfm->__crt_alg->cra_cipher.cia_encrypt,
> > 	             cfb_process, 1, iv);
> > }
> > 
> > static int cfb_decrypt(struct crypto_tfm *tfm,
> >                        struct scatterlist *dst,
> >                        struct scatterlist *src,
> > 		       unsigned int nbytes)
> > {
> > 	return crypt(tfm, dst, src, nbytes,
> > 	             tfm->__crt_alg->cra_cipher.cia_decrypt,
> > 	             cfb_process, 0, tfm->crt_cipher.cit_iv);
> > }
> > 
> > static int cfb_decrypt_iv(struct crypto_tfm *tfm,
> >                           struct scatterlist *dst,
> >                           struct scatterlist *src,
> >                           unsigned int nbytes, u8 *iv)
> > {
> > 	return crypt(tfm, dst, src, nbytes,
> > 	             tfm->__crt_alg->cra_cipher.cia_decrypt,
> > 	             cfb_process, 0, iv);
> > }
> > 
> > static int nocrypt(struct crypto_tfm *tfm,
> >                    struct scatterlist *dst,
> >                    struct scatterlist *src,
> > 		   unsigned int nbytes)
> > {
> > 	return -ENOSYS;
> > }
> > 
> > static int nocrypt_iv(struct crypto_tfm *tfm,
> >                       struct scatterlist *dst,
> >                       struct scatterlist *src,
> >                       unsigned int nbytes, u8 *iv)
> > {
> > 	return -ENOSYS;
> > }
> > 
> > int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags)
> > {
> > 	u32 mode = flags & CRYPTO_TFM_MODE_MASK;
> > 	
> > 	tfm->crt_cipher.cit_mode = mode ? mode : CRYPTO_TFM_MODE_ECB;
> > 	if (flags & CRYPTO_TFM_REQ_WEAK_KEY)
> > 		tfm->crt_flags = CRYPTO_TFM_REQ_WEAK_KEY;
> > 	
> > 	return 0;
> > }
> > 
> > int crypto_init_cipher_ops(struct crypto_tfm *tfm)
> > {
> > 	int ret = 0;
> > 	struct cipher_tfm *ops = &tfm->crt_cipher;
> > 
> > 	ops->cit_setkey = setkey;
> > 
> > 	switch (tfm->crt_cipher.cit_mode) {
> > 	case CRYPTO_TFM_MODE_ECB:
> > 		ops->cit_encrypt = ecb_encrypt;
> > 		ops->cit_decrypt = ecb_decrypt;
> > 		break;
> > 		
> > 	case CRYPTO_TFM_MODE_CBC:
> > 		ops->cit_encrypt = cbc_encrypt;
> > 		ops->cit_decrypt = cbc_decrypt;
> > 		ops->cit_encrypt_iv = cbc_encrypt_iv;
> > 		ops->cit_decrypt_iv = cbc_decrypt_iv;
> > 		break;
> > 		
> > 	case CRYPTO_TFM_MODE_CFB:
> > 		ops->cit_encrypt = cfb_encrypt;
> > 		ops->cit_decrypt = cfb_decrypt;
> > 		ops->cit_encrypt_iv = cfb_encrypt_iv;
> > 		ops->cit_decrypt_iv = cfb_decrypt_iv;
> > 		break;
> > 	
> > 	case CRYPTO_TFM_MODE_CTR:
> > 		ops->cit_encrypt = nocrypt;
> > 		ops->cit_decrypt = nocrypt;
> > 		ops->cit_encrypt_iv = nocrypt_iv;
> > 		ops->cit_decrypt_iv = nocrypt_iv;
> > 		break;
> > 
> > 	default:
> > 		BUG();
> > 	}
> > 	
> > 	if (ops->cit_mode == CRYPTO_TFM_MODE_CBC
> > 	    || ops->cit_mode == CRYPTO_TFM_MODE_CFB) {
> > 	    	
> > 	    	switch (crypto_tfm_alg_blocksize(tfm)) {
> > 	    	case 8:
> > 	    		ops->cit_xor_block = xor_64;
> > 	    		break;
> > 	    		
> > 	    	case 16:
> > 	    		ops->cit_xor_block = xor_128;
> > 	    		break;
> > 	    		
> > 	    	default:
> > 	    		printk(KERN_WARNING "%s: block size %u not supported\n",
> > 	    		       crypto_tfm_alg_name(tfm),
> > 	    		       crypto_tfm_alg_blocksize(tfm));
> > 	    		ret = -EINVAL;
> > 	    		goto out;
> > 	    	}
> > 	    	
> > 		ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm);
> > 	    	ops->cit_iv = kmalloc(ops->cit_ivsize, GFP_KERNEL);
> > 		if (ops->cit_iv == NULL)
> > 			ret = -ENOMEM;
> > 	}
> > 
> > out:	
> > 	return ret;
> > }
> > 
> > void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
> > {
> > 	if (tfm->crt_cipher.cit_iv)
> > 		kfree(tfm->crt_cipher.cit_iv);
> > }
> 
> 
> 
> 
> > _______________________________________________
> > 
> > Subscription: http://lists.logix.cz/mailman/listinfo/cryptoapi
> > List archive: http://lists.logix.cz/pipermail/cryptoapi
> _______________________________________________
> 
> Subscription: http://lists.logix.cz/mailman/listinfo/cryptoapi
> List archive: http://lists.logix.cz/pipermail/cryptoapi
-------------- next part --------------
diff -X exclude -Nur linux-2.6.8.1/crypto/cipher.c linux-2.6.8.1-rand2/crypto/cipher.c
--- linux-2.6.8.1/crypto/cipher.c	2004-09-23 23:14:26.740570080 -0400
+++ linux-2.6.8.1-rand2/crypto/cipher.c	2004-08-14 06:55:32.000000000 -0400
@@ -95,80 +95,6 @@
 	}
 }
 
-inline void ctr_increment_iv(u8 *iv, int blocksize) {
-  	int i;
-	switch (blocksize) {
-		case 8:
-			if (++((u32*)iv)[0])
-				++((u32*)iv)[1];
-		break;
-
-		case 16:
-			if (++((u32*)iv)[0])
-				if (++((u32*)iv)[1])
-					if (++((u32*)iv)[2])
-						++((u32*)iv)[3];
-		break;
-
-		default:
-			for (i=0; i<blocksize; i++)
-				if (++iv[i])
-					break;
-		break;
-	}
-}
-static void ctr_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
-			cryptfn_t fn, int enc, void *info, int in_place)
-{
-	u8 *iv = info;
-  	u8 stack[in_place ? crypto_tfm_alg_blocksize(tfm) : 0];
-	u8 *buf = in_place ? stack : dst;
-	
-	/* Null encryption */
-	if (!iv)
-		return;
-		
-	/* jlcooke: encrypt is the same as decrypt */
-
-	fn(crypto_tfm_ctx(tfm), buf, iv);
-	tfm->crt_u.cipher.cit_xor_block(buf, src);
-	memcpy(dst, buf, crypto_tfm_alg_blocksize(tfm));
-	ctr_increment_iv(iv, crypto_tfm_alg_blocksize(tfm));
-}
-
-static void cfb_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
-			cryptfn_t fn, int enc, void *info, int in_place)
-{
-	u8 *iv = info;
-	/* Null encryption */
-	if (!iv)
-		return;
-	if (enc) {
-		if (in_place) {
-			u8 buf[crypto_tfm_alg_blocksize(tfm)];
-			fn(crypto_tfm_ctx(tfm), buf, iv);
-			tfm->crt_u.cipher.cit_xor_block(buf, src);
-			memcpy(dst, buf, crypto_tfm_alg_blocksize(tfm));
-			memcpy(iv, buf, crypto_tfm_alg_blocksize(tfm));
-		} else {
-			fn(crypto_tfm_ctx(tfm), dst, iv);
-			tfm->crt_u.cipher.cit_xor_block(dst, src);
-			memcpy(iv, dst, crypto_tfm_alg_blocksize(tfm));
-		}
-	} else {
-		if (in_place) {
-			u8 buf[crypto_tfm_alg_blocksize(tfm)];
-			fn(crypto_tfm_ctx(tfm), buf, iv);
-			tfm->crt_u.cipher.cit_xor_block(buf, src);
-			memcpy(dst, buf, crypto_tfm_alg_blocksize(tfm));
-		} else {
-			fn(crypto_tfm_ctx(tfm), dst, iv);
-			tfm->crt_u.cipher.cit_xor_block(dst, src);
-		}
-		memcpy(iv, src, crypto_tfm_alg_blocksize(tfm));
-	}
-}
-
 static void cbc_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
 			cryptfn_t fn, int enc, void *info, int in_place)
 {
@@ -271,67 +197,6 @@
 	             cbc_process, 0, iv);
 }
 
-
-static int ctr_encrypt(struct crypto_tfm *tfm,
-                       struct scatterlist *dst,
-                       struct scatterlist *src,
-		       unsigned int nbytes)
-{
-	return crypt(tfm, dst, src, nbytes,
-	             tfm->__crt_alg->cra_cipher.cia_encrypt,
-	             ctr_process, 1, tfm->crt_cipher.cit_iv);
-}
-
-static int ctr_encrypt_iv(struct crypto_tfm *tfm,
-                          struct scatterlist *dst,
-                          struct scatterlist *src,
-                          unsigned int nbytes, u8 *iv)
-{
-	return crypt(tfm, dst, src, nbytes,
-	             tfm->__crt_alg->cra_cipher.cia_encrypt,
-	             ctr_process, 1, iv);
-}
-
-static int cfb_encrypt(struct crypto_tfm *tfm,
-			struct scatterlist *dst,
-			struct scatterlist *src,
-			unsigned int nbytes)
-{
-	return crypt(tfm, dst, src, nbytes,
-		tfm->__crt_alg->cra_cipher.cia_encrypt,
-		cfb_process, 1, tfm->crt_cipher.cit_iv);
-}
-
-static int cfb_encrypt_iv(struct crypto_tfm *tfm,
-			struct scatterlist *dst,
-			struct scatterlist *src,
-			unsigned int nbytes, u8 *iv)
-{
-	return crypt(tfm, dst, src, nbytes,
-		tfm->__crt_alg->cra_cipher.cia_encrypt,
-		cfb_process, 1, iv);
-}
-
-static int cfb_decrypt(struct crypto_tfm *tfm,
-			struct scatterlist *dst,
-			struct scatterlist *src,
-			unsigned int nbytes)
-{
-	return crypt(tfm, dst, src, nbytes,
-		tfm->__crt_alg->cra_cipher.cia_decrypt,
-		cfb_process, 0, tfm->crt_cipher.cit_iv);
-}
-
-static int cfb_decrypt_iv(struct crypto_tfm *tfm,
-			struct scatterlist *dst,
-			struct scatterlist *src,
-			unsigned int nbytes, u8 *iv)
-{
-	return crypt(tfm, dst, src, nbytes,
-		tfm->__crt_alg->cra_cipher.cia_decrypt,
-		cfb_process, 0, iv);
-}
-
 static int nocrypt(struct crypto_tfm *tfm,
                    struct scatterlist *dst,
                    struct scatterlist *src,
@@ -380,13 +245,6 @@
 		break;
 		
 	case CRYPTO_TFM_MODE_CFB:
-		ops->cit_encrypt = cfb_encrypt;
-		ops->cit_decrypt = cfb_decrypt;
-		ops->cit_encrypt_iv = cfb_encrypt_iv;
-		ops->cit_decrypt_iv = cfb_decrypt_iv;
-		break;
-
-	case CRYPTO_TFM_MODE_OFB:
 		ops->cit_encrypt = nocrypt;
 		ops->cit_decrypt = nocrypt;
 		ops->cit_encrypt_iv = nocrypt_iv;
@@ -394,17 +252,17 @@
 		break;
 	
 	case CRYPTO_TFM_MODE_CTR:
-		ops->cit_encrypt = ctr_encrypt;
-		ops->cit_decrypt = ctr_encrypt;
-		ops->cit_encrypt_iv = ctr_encrypt_iv;
-		ops->cit_decrypt_iv = ctr_encrypt_iv;
+		ops->cit_encrypt = nocrypt;
+		ops->cit_decrypt = nocrypt;
+		ops->cit_encrypt_iv = nocrypt_iv;
+		ops->cit_decrypt_iv = nocrypt_iv;
 		break;
 
 	default:
 		BUG();
 	}
 	
-	if (ops->cit_mode != CRYPTO_TFM_MODE_ECB) {
+	if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
 	    	
 	    	switch (crypto_tfm_alg_blocksize(tfm)) {
 	    	case 8:
diff -X exclude -Nur linux-2.6.8.1/crypto/tcrypt.c linux-2.6.8.1-rand2/crypto/tcrypt.c
--- linux-2.6.8.1/crypto/tcrypt.c	2004-09-23 23:14:26.532601696 -0400
+++ linux-2.6.8.1-rand2/crypto/tcrypt.c	2004-08-14 06:55:59.000000000 -0400
@@ -49,11 +49,8 @@
 */
 #define ENCRYPT 1
 #define DECRYPT 0
-#define MODE_ECB 0
-#define MODE_CBC 1
-#define MODE_CFB 2
-#define MODE_OFB 3
-#define MODE_CTR 4
+#define MODE_ECB 1
+#define MODE_CBC 0
 
 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
 
@@ -272,16 +269,10 @@
 	        strncpy(e, "encryption", 11);
 	else
         	strncpy(e, "decryption", 11);
-	if (mode == MODE_CBC)
-        	strncpy(m, "CBC", 4);
-	else if (mode == MODE_CFB)
-        	strncpy(m, "CFB", 4);
-	else if (mode == MODE_OFB)
-        	strncpy(m, "OFB", 4);
-	else if (mode == MODE_CTR)
-        	strncpy(m, "CTR", 4);
-	else
+	if (mode == MODE_ECB)
         	strncpy(m, "ECB", 4);
+	else
+        	strncpy(m, "CBC", 4);
 
 	printk("\ntesting %s %s %s \n", algo, m, e);
 
@@ -297,16 +288,10 @@
 	memcpy(tvmem, template, tsize);
 	cipher_tv = (void *) tvmem;
 
-	if (mode == MODE_CBC) 
-		tfm = crypto_alloc_tfm (algo, CRYPTO_TFM_MODE_CBC);
-	else if (mode == MODE_CFB) 
-		tfm = crypto_alloc_tfm (algo, CRYPTO_TFM_MODE_CFB);
-	else if (mode == MODE_OFB) 
-		tfm = crypto_alloc_tfm (algo, CRYPTO_TFM_MODE_OFB);
-	else if (mode == MODE_CTR) 
-		tfm = crypto_alloc_tfm (algo, CRYPTO_TFM_MODE_CTR);
-	else
+	if (mode) 
 		tfm = crypto_alloc_tfm (algo, 0);
+	else 
+		tfm = crypto_alloc_tfm (algo, CRYPTO_TFM_MODE_CBC);
 	
 	if (tfm == NULL) {
 		printk("failed to load transform for %s %s\n", algo, m);
@@ -338,7 +323,7 @@
 			sg[0].offset = offset_in_page(p);
 			sg[0].length = cipher_tv[i].ilen;
 	
-			if (mode != MODE_ECB) {
+			if (!mode) {
 				crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
 					crypto_tfm_alg_ivsize (tfm));
 			}
@@ -397,7 +382,7 @@
 				sg[k].length = cipher_tv[i].tap[k];
 			}
 			
-			if (mode != MODE_ECB) {
+			if (!mode) {
 				crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
 						crypto_tfm_alg_ivsize (tfm));
 			}
@@ -667,14 +652,6 @@
 		//AES
 		test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
 		test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);
-		test_cipher ("aes", MODE_CBC, ENCRYPT, aes_cbc_enc_tv_template, AES_CBC_ENC_TEST_VECTORS);
-		test_cipher ("aes", MODE_CBC, DECRYPT, aes_cbc_dec_tv_template, AES_CBC_DEC_TEST_VECTORS);
-		test_cipher ("aes", MODE_CFB, ENCRYPT, aes_cfb_enc_tv_template, AES_CFB_ENC_TEST_VECTORS);
-		test_cipher ("aes", MODE_CFB, DECRYPT, aes_cfb_dec_tv_template, AES_CFB_DEC_TEST_VECTORS);
-		test_cipher ("aes", MODE_OFB, ENCRYPT, aes_ofb_enc_tv_template, AES_OFB_ENC_TEST_VECTORS);
-		test_cipher ("aes", MODE_OFB, DECRYPT, aes_ofb_dec_tv_template, AES_OFB_DEC_TEST_VECTORS);
-		test_cipher ("aes", MODE_CTR, ENCRYPT, aes_ctr_enc_tv_template, AES_CTR_ENC_TEST_VECTORS);
-		test_cipher ("aes", MODE_CTR, DECRYPT, aes_ctr_dec_tv_template, AES_CTR_DEC_TEST_VECTORS);
 
 		//CAST5
 		test_cipher ("cast5", MODE_ECB, ENCRYPT, cast5_enc_tv_template, CAST5_ENC_TEST_VECTORS);
@@ -703,7 +680,6 @@
 
 		test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
 		test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
-
 		test_deflate();
 		test_crc32c();
 #ifdef CONFIG_CRYPTO_HMAC
@@ -763,14 +739,6 @@
 	case 10:
 		test_cipher ("aes", MODE_ECB, ENCRYPT, aes_enc_tv_template, AES_ENC_TEST_VECTORS);
 		test_cipher ("aes", MODE_ECB, DECRYPT, aes_dec_tv_template, AES_DEC_TEST_VECTORS);	
-		test_cipher ("aes", MODE_CBC, ENCRYPT, aes_cbc_enc_tv_template, AES_CBC_ENC_TEST_VECTORS);
-		test_cipher ("aes", MODE_CBC, DECRYPT, aes_cbc_dec_tv_template, AES_CBC_DEC_TEST_VECTORS);	
-		test_cipher ("aes", MODE_CFB, ENCRYPT, aes_cfb_enc_tv_template, AES_CFB_ENC_TEST_VECTORS);
-		test_cipher ("aes", MODE_CFB, DECRYPT, aes_cfb_dec_tv_template, AES_CFB_DEC_TEST_VECTORS);	
-		test_cipher ("aes", MODE_OFB, ENCRYPT, aes_ofb_enc_tv_template, AES_OFB_ENC_TEST_VECTORS);
-		test_cipher ("aes", MODE_OFB, DECRYPT, aes_ofb_dec_tv_template, AES_OFB_DEC_TEST_VECTORS);	
-		test_cipher ("aes", MODE_CTR, ENCRYPT, aes_ctr_enc_tv_template, AES_CTR_ENC_TEST_VECTORS);
-		test_cipher ("aes", MODE_CTR, DECRYPT, aes_ctr_dec_tv_template, AES_CTR_DEC_TEST_VECTORS);	
 		break;
 
 	case 11:
diff -X exclude -Nur linux-2.6.8.1/crypto/tcrypt.h linux-2.6.8.1-rand2/crypto/tcrypt.h
--- linux-2.6.8.1/crypto/tcrypt.h	2004-09-23 23:14:26.686578288 -0400
+++ linux-2.6.8.1-rand2/crypto/tcrypt.h	2004-09-14 10:03:35.000000000 -0400
@@ -1547,130 +1547,90 @@
 /*
  * AES test vectors.
  */
-#define AES_CTR_ENC_TEST_VECTORS 1
-#define AES_CTR_DEC_TEST_VECTORS 1
+#define AES_CTR_ENC_TEST_VECTORS 3
+#define AES_CTR_DEC_TEST_VECTORS 3
 
-struct cipher_testvec aes_ctr_enc_tv_template[] = { 
-	{
-		.key	= { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
-			    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
-		.klen	= 16,
-		.input	= { 0x61, 0x62, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
-			    0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70 },
-		.iv	= { 0x20, 0x21, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
-			    0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f },
-		.ilen	= 16,
-		.result	= { 0x46, 0xc7, 0x9e, 0x47, 0x6d, 0xf0, 0xfe, 0x48,
-			    0x77, 0xb3, 0xc9, 0xd9, 0x88, 0xbf, 0x75, 0x37 },
-		.rlen	= 16,
-	},
-};
+/*
+key= 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
+iv = 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
+in = 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70
+out= 54 ef 38 3d c8 d0 3a 6c 79 1c 1d 34 02 29 5b 36
 
-struct cipher_testvec aes_ctr_dec_tv_template[] = { 
-	{
+ */
+struct cipher_testvec aes_ctr_enc_tv_template[] = { 
+	{ /* From FIPS-197 */
 		.key	= { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
 			    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
 		.klen	= 16,
 		.input	= { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
 			    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
-		.iv	= { 0x10, 0x11, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
-			    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
 		.ilen	= 16,
-		.result	= { 0x77, 0xdd, 0x58, 0x2a, 0xe8, 0x53, 0x8d, 0xba,
-			    0x9f, 0x11, 0x6b, 0x93, 0xd6, 0x57, 0x2e, 0xab },
+		.result	= { 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
+			    0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a },
 		.rlen	= 16,
-	},
-};
-
-#define AES_CBC_ENC_TEST_VECTORS 1
-#define AES_CBC_DEC_TEST_VECTORS 0
-struct cipher_testvec aes_cbc_enc_tv_template[] = { 
-	{
-		.key	= { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
-			    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
-		.klen	= 16,
-		.input	= { 0x61, 0x62, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
-			    0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70 },
-		.iv	= { 0x10, 0x11, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
-			    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
+	}, {
+		.key	= { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+			    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+			    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 },
+		.klen	= 24,
+		.input	= { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 
+			    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
 		.ilen	= 16,
-		.result	= { 0x0b, 0xc1, 0x9f, 0xdc, 0xdd, 0x59, 0x62, 0x23,
-			    0x49, 0x62, 0x28, 0xe3, 0x9c, 0x19, 0xc1, 0xdd },
+		.result	= { 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0,
+			    0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 },
 		.rlen	= 16,
-	},
-};
-
-struct cipher_testvec aes_cbc_dec_tv_template[] = { 
-	{
-		.key	= { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
-			    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
-		.klen	= 16,
-		.input	= { 0x61, 0x62, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
-			    0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70 },
-		.iv	= { 0x10, 0x11, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
+	}, {
+		.key	= { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+			    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+			    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 			    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
+		.klen	= 32,
+		.input	= { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 
+			    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
 		.ilen	= 16,
-		.result	= { 0x54, 0xef, 0x38, 0x3d, 0xc8, 0xd0, 0x3a, 0x6c,
-			    0x79, 0x1c, 0x1d, 0x34, 0x02, 0x29, 0x5b, 0x36 },
+		.result	= { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
+			    0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 },
 		.rlen	= 16,
 	},
 };
 
-#define AES_CFB_ENC_TEST_VECTORS 1
-#define AES_CFB_DEC_TEST_VECTORS 1
-struct cipher_testvec aes_cfb_enc_tv_template[] = { 
-	{
-		.key	= { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
-			    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
+struct cipher_testvec aes_ctr_dec_tv_template[] = { 
+	{ /* From FIPS-197 */
+		.key	= { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
+			    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
 		.klen	= 16,
-		.input	= { 0x61, 0x62, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
-			    0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70 },
-		.iv	= { 0x10, 0x11, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
-			    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
+		.input	= { 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
+			    0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a },
 		.ilen	= 16,
-		.result	= { 0xbb, 0x42, 0x5b, 0x8f, 0x12, 0xd8, 0x85, 0xa9,
-			    0x8c, 0xb0, 0xa2, 0x7c, 0x6a, 0x98, 0x05, 0xe0 },
+		.result	= { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+			    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
 		.rlen	= 16,
-	},
-};
-
-struct cipher_testvec aes_cfb_dec_tv_template[] = { 
-	{
-		.key	= { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
-			    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
-		.klen	= 16,
-		.input	= { 0x61, 0x62, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
-			    0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70 },
-		.iv	= { 0x10, 0x11, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
-			    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
+	}, {
+		.key	= { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+			    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+			    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 },
+		.klen	= 24,
+		.input	= { 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0,
+			    0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 },
 		.ilen	= 16,
-		.result	= { 0x81, 0xfa, 0x56, 0x98, 0x18, 0xfc, 0xe4, 0x3a,
-			    0x96, 0x5b, 0xeb, 0x72, 0x0f, 0xad, 0x42, 0xa2 },
+		.result	= { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 
+			    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },  
 		.rlen	= 16,
-	},
-};
-
-#define AES_OFB_ENC_TEST_VECTORS 1
-#define AES_OFB_DEC_TEST_VECTORS 0
-struct cipher_testvec aes_ofb_enc_tv_template[] = { 
-	{
-		.key	= { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
-			    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
-		.klen	= 16,
-		.input	= { 0x61, 0x62, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
-			    0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70 },
-		.iv	= { 0x10, 0x11, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
+	}, {
+		.key	= { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+			    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+			    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 			    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
+		.klen	= 32,
+		.input	= { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
+			    0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 },
 		.ilen	= 16,
-		.result	= { 0x54, 0xef, 0x38, 0x3d, 0xc8, 0xd0, 0x3a, 0x6c,
-			    0x79, 0x1c, 0x1d, 0x34, 0x02, 0x29, 0x5b, 0x36 },
+		.result	= { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 
+			    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
 		.rlen	= 16,
 	},
 };
 
-struct cipher_testvec aes_ofb_dec_tv_template[] = { 
-};
-
 /* Cast5 test vectors from RFC 2144 */
 #define CAST5_ENC_TEST_VECTORS	3
 #define CAST5_DEC_TEST_VECTORS	3


More information about the CryptoAPI mailing list