Add tweakable cipher interface
PATCH 3/4 Add tweakable cipher interface
This patch adds a new cipher interface "tweakable". This interface will be used for tweakable cipher modes such as LRW (or EME, CMC .. if I every going to port my old code).
Signed-off-by Fruhwirth Clemens <[email protected]>
crypto/cipher.c +19 -4
@@ -4,6 +4,7 @@* Cipher operations. * * Copyright (c) 2002 James Morris <[email protected]> +* Copyright (c) 2005 Clemens Fruhwirth <[email protected]> * * 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
@@ -233,6 +234,14 @@return -ENOSYS ; } +static int nocrypt_tweaks (struct crypto_tfm *tfm, +struct scatterlist *dst, +struct scatterlist *src, +unsigned int nbytes,struct scatterlist *tweaksg) +{ +return -ENOSYS ; +} +int crypto_init_cipher_flags (struct crypto_tfm *tfm,u32 flags) {u32 mode = flags &CRYPTO_TFM_MODE_MASK ;
@@ -262,6 +271,12 @@ ops->cit_decrypt = cbc_decrypt; ops->cit_encrypt_iv = cbc_encrypt_iv; ops->cit_decrypt_iv = cbc_decrypt_iv; + ops->cit_encrypt_tweaks = nocrypt_tweaks; + ops->cit_decrypt_tweaks = nocrypt_tweaks; + ops->cit_ivsize =crypto_tfm_alg_blocksize (tfm); + ops->cit_iv =kmalloc (ops->cit_ivsize,GFP_KERNEL ); +if (ops->cit_iv ==NULL ) + ret = -ENOMEM ;break ;case CRYPTO_TFM_MODE_CFB :
@@ -269,6 +284,8 @@ ops->cit_decrypt = nocrypt; ops->cit_encrypt_iv = nocrypt_iv; ops->cit_decrypt_iv = nocrypt_iv; + ops->cit_encrypt_tweaks = nocrypt_tweaks; + ops->cit_decrypt_tweaks = nocrypt_tweaks;break ;case CRYPTO_TFM_MODE_CTR :
@@ -276,6 +293,8 @@ ops->cit_decrypt = nocrypt; ops->cit_encrypt_iv = nocrypt_iv; ops->cit_decrypt_iv = nocrypt_iv; + ops->cit_encrypt_tweaks = nocrypt_tweaks; + ops->cit_decrypt_tweaks = nocrypt_tweaks;break ;default :
@@ -301,10 +320,6 @@ 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:
include/linux/crypto.h +35 -0
@@ -3,6 +3,7 @@* * Copyright (c) 2002 James Morris <[email protected]> * Copyright (c) 2002 David S. Miller ([email protected]) +* Copyright (C) 2004 Clemens Fruhwirth <[email protected]> * * Portions derived from Cryptoapi, by Alexander Kjeldaas <[email protected]> * and Nettle, by Niels M�ller.
@@ -38,6 +39,11 @@#define CRYPTO_TFM_REQ_MASK 0x000fff00#define CRYPTO_TFM_RES_MASK 0xfff00000 +/* +* Available cipher modes +* Also modify api.c:crypto_tfm_cmctx_size, when adding new modes +*/ +#define CRYPTO_TFM_MODE_ECB 0x00000001#define CRYPTO_TFM_MODE_CBC 0x00000002#define CRYPTO_TFM_MODE_CFB 0x00000004
@@ -133,6 +139,8 @@struct cipher_tfm {void *cit_iv;unsigned int cit_ivsize; +unsigned int cit_tweaksize; +unsigned int cit_bytes_per_tweak;u32 cit_mode;int (*cit_setkey)(struct crypto_tfm *tfm,const u8 *key,unsigned int keylen);
@@ -144,6 +152,10 @@struct scatterlist *dst, struct scatterlist *src, unsigned int nbytes, u8 *iv); +int (*cit_encrypt_tweaks)(struct crypto_tfm *tfm, +struct scatterlist *dst, +struct scatterlist *src, +unsigned int nbytes,struct scatterlist *tweaks); int (*cit_decrypt)(struct crypto_tfm *tfm, struct scatterlist *dst, struct scatterlist *src,
@@ -152,6 +164,10 @@struct scatterlist *dst, struct scatterlist *src, unsigned int nbytes, u8 *iv); +int (*cit_decrypt_tweaks)(struct crypto_tfm *tfm, +struct scatterlist *dst, +struct scatterlist *src, +unsigned int nbytes,struct scatterlist *tweaks);void (*cit_xor_block)(u8 *dst,const u8 *src); };
@@ -357,6 +373,25 @@memcpy (dst, tfm->crt_cipher.cit_iv, len); } +static inline int crypto_cipher_encrypt_tweaks (struct crypto_tfm *tfm, +struct scatterlist *dst, +struct scatterlist *src, +unsigned int nbytes,struct scatterlist *tweaksg) +{ +BUG_ON (crypto_tfm_alg_type (tfm) !=CRYPTO_ALG_TYPE_CIPHER ); +return tfm->crt_cipher.cit_encrypt_tweaks (tfm, dst, src, nbytes, tweaksg); +} + +static inline int crypto_cipher_decrypt_tweaks (struct crypto_tfm *tfm, +struct scatterlist *dst, +struct scatterlist *src, +unsigned int nbytes,struct scatterlist *tweaksg) +{ +BUG_ON (crypto_tfm_alg_type (tfm) !=CRYPTO_ALG_TYPE_CIPHER ); +return tfm->crt_cipher.cit_decrypt_tweaks (tfm, dst, src, nbytes, tweaksg); +} + +static inline int crypto_comp_compress (struct crypto_tfm *tfm,const u8 *src,unsigned int slen,u8 *dst,unsigned int *dlen)