[PATCH 03/04] Add tweakable cipher interface

From: Fruhwirth Clemens
Date: Mon Jan 24 2005 - 07:05:09 EST


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 <clemens@xxxxxxxxxxxxx>

--- 2/crypto/cipher.c 2005-01-22 16:53:33.000000000 +0100
+++ 3/crypto/cipher.c 2005-01-24 11:35:58.994317520 +0100
@@ -4,6 +4,7 @@
* Cipher operations.
*
* Copyright (c) 2002 James Morris <jmorris@xxxxxxxxxxxxxxxx>
+ * Copyright (c) 2005 Clemens Fruhwirth <clemens@xxxxxxxxxxxxx>
*
* 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:
--- 2/include/linux/crypto.h 2005-01-20 10:16:06.000000000 +0100
+++ 3/include/linux/crypto.h 2005-01-24 11:33:34.498284256 +0100
@@ -3,6 +3,7 @@
*
* Copyright (c) 2002 James Morris <jmorris@xxxxxxxxxxxxxxxx>
* Copyright (c) 2002 David S. Miller (davem@xxxxxxxxxx)
+ * Copyright (C) 2004 Clemens Fruhwirth <clemens@xxxxxxxxxxxxx>
*
* Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@xxxxxxx>
* 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)
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/