Add LRW
PATCH 4/4 Add LRW
This is the core of my LRW patch. Added test vectors.
http://grouper.ieee.org/groups/1619/email/pdf00017.pdf
Please notice, that this is not a patch for dm-crypt. I will post a nice splitted patch set for dm later that day.
crypto/cipher.c +31 -9
@@ -20,10 +20,7 @@#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 ); +#include "lrw.h" static inline void xor_64 (u8 *a,const u8 *b) {
@@ -39,7 +36,6 @@ ((u32 *)a)[3 ] ^= ((u32 *)b)[3 ]; } -struct cbc_process_priv {struct crypto_tfm *tfm;int enc;
@@ -85,7 +81,7 @@ priv->crfn (crypto_tfm_ctx (priv->tfm), buf[0 ], buf[1 ]); } -static int setkey (struct crypto_tfm *tfm,const u8 *key,unsigned int keylen) +static int setkey_generic (struct crypto_tfm *tfm,const u8 *key,unsigned int keylen) {struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
@@ -258,15 +254,15 @@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_setkey = setkey_generic; ops->cit_encrypt = ecb_encrypt; ops->cit_decrypt = ecb_decrypt;break ;case CRYPTO_TFM_MODE_CBC : + ops->cit_setkey = setkey_generic; ops->cit_encrypt = cbc_encrypt; ops->cit_decrypt = cbc_decrypt; ops->cit_encrypt_iv = cbc_encrypt_iv;
@@ -280,6 +276,7 @@break ;case CRYPTO_TFM_MODE_CFB : + ops->cit_setkey = setkey_generic; ops->cit_encrypt = nocrypt; ops->cit_decrypt = nocrypt; ops->cit_encrypt_iv = nocrypt_iv;
@@ -289,6 +286,7 @@break ;case CRYPTO_TFM_MODE_CTR : + ops->cit_setkey = setkey_generic; ops->cit_encrypt = nocrypt; ops->cit_decrypt = nocrypt; ops->cit_encrypt_iv = nocrypt_iv;
@@ -297,11 +295,35 @@ ops->cit_decrypt_tweaks = nocrypt_tweaks;break ; +case CRYPTO_TFM_MODE_LRW : +case CRYPTO_TFM_MODE_LRW_RAW : +if (crypto_tfm_alg_blocksize (tfm) !=16 ) { +printk (KERN_WARNING "LRW can't be used with non-128-bit ciphers\n" ); +return -EINVAL ; + } + ops->cit_tweaksize =crypto_tfm_alg_blocksize (tfm); + ops->cit_bytes_per_tweak =crypto_tfm_alg_blocksize (tfm); + ops->cit_setkey = setkey_lrw; + ops->cit_encrypt = nocrypt; + ops->cit_decrypt = nocrypt; + ops->cit_encrypt_iv = nocrypt_iv; + ops->cit_decrypt_iv = nocrypt_iv; +if (tfm->crt_cipher.cit_mode ==CRYPTO_TFM_MODE_LRW ) { + ops->cit_encrypt_tweaks = lrw_encrypt; + ops->cit_decrypt_tweaks = lrw_decrypt; + }else if (tfm->crt_cipher.cit_mode ==CRYPTO_TFM_MODE_LRW_RAW ) { + ops->cit_encrypt_tweaks = lrw_raw_encrypt; + ops->cit_decrypt_tweaks = lrw_raw_decrypt; + }else { +BUG (); + } +break ;default :BUG (); } -if (ops->cit_mode ==CRYPTO_TFM_MODE_CBC ) { +if (ops->cit_mode ==CRYPTO_TFM_MODE_CBC || ops->cit_mode ==CRYPTO_TFM_MODE_LRW + || ops->cit_mode ==CRYPTO_TFM_MODE_LRW_RAW ) {switch (crypto_tfm_alg_blocksize (tfm)) {case 8 :
crypto/api.c +3 -0
@@ -27,6 +27,9 @@static inline int crypto_cmctx_size (u32 flags) {switch (flags &CRYPTO_TFM_MODE_MASK ) { +case CRYPTO_TFM_MODE_LRW : +case CRYPTO_TFM_MODE_LRW_RAW : +return 2048 ;default :return 0 ; }
crypto/internal.h +4 -0
@@ -19,6 +19,10 @@#include <linux/kmod.h> #include <asm/kmap_types.h> +typedef void (cryptfn_t )(void *,u8 *,const u8 *); +typedef void (procfn_t )(struct crypto_tfm *,u8 *, +u8 *,cryptfn_t ,int enc,void *,int ); +extern enum km_type crypto_km_types[];static inline enum km_type crypto_kmap_type (int out)
include/linux/crypto.h +2 -0
@@ -48,6 +48,8 @@#define CRYPTO_TFM_MODE_CBC 0x00000002#define CRYPTO_TFM_MODE_CFB 0x00000004#define CRYPTO_TFM_MODE_CTR 0x00000008 +#define CRYPTO_TFM_MODE_LRW 0x00000020 +#define CRYPTO_TFM_MODE_LRW_RAW 0x00000040#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
crypto/lrw.c new file +262 -0
262 lines not on this page · raw message
crypto/lrw.h new file +45 -0
45 lines not on this page · raw message
crypto/gfmulseq.c new file +219 -0
219 lines not on this page · raw message
crypto/gfmulseq.h new file +74 -0
74 lines not on this page · raw message
crypto/Makefile +1 -1
@@ -4,7 +4,7 @@ proc-crypto-$(CONFIG_PROC_FS) = proc.o -obj-$(CONFIG_CRYPTO) += api.o scatterwalk.o cipher.o digest.o compress.o \ +obj-$(CONFIG_CRYPTO) += api.o scatterwalk.o cipher.o digest.o compress.o lrw.o gfmulseq.o \ $(proc-crypto-y) obj-$(CONFIG_CRYPTO_HMAC) += hmac.o
crypto/tcrypt.c +76 -36
162 lines not on this page · raw message
crypto/tcrypt.h +79 -0
94 lines not on this page · raw message
Quoted 2 lines · Fruhwirth Clemens
This is the core of my LRW patch. Added test vectors.
http://grouper.ieee.org/groups/1619/email/pdf00017.pdf
Please include a URL for the standard at the top of the LRW code and next to the test vectors. I had to search around a fair bit for decent background material, would be helpful to a couple other references as well.
crypto/gfmulseq.c line 71
Quoted 2 lines
crypto/gfmulseq.c new file +2
-0+static inline void findAlignment (u128 callersN,int value,int *align) { +int i;
Your gfmulseq code has lots of StudlyCaps and strange whitespace, eg this '{' should be on the next line.
crypto/gfmulseq.c line 74
Quoted 3 lines
crypto/gfmulseq.c new file +3
-0+/* Copy N, so lsr does not destroy caller's copy */ +u128_alloc (N ); +copy128 (N ,callersN);
The usage of your u128 type is really confusing, so 'u128' is an especially bad name. I expect u128 to work like u64 and u32. I propose gf128_t.
crypto/gfmulseq.c line 93
Quoted 1 line
crypto/gfmulseq.c new file +1
-0+int i;// Outer control loop counter
C++ comments.
Quoted 1 line
+#define
min(a,b)(a)<(b)?(a):(b)
Have a very nice one of those already.
crypto/gfmulseq.c line 148
Quoted 4 lines
crypto/gfmulseq.c new file +4
-0+#ifdef DEBUG +printf ("negative step at:" ); +print128 (currentN); +#endif
Better to use printk and put #define printk printf in your userspace test harness.
crypto/gfmulseq.h line 31
Quoted 2 lines
crypto/gfmulseq.h new file +2
-0+typedef u64 *u128 ; +typedef u64 *u128_t ;
Did I mention confusing?
Quoted 1 line
+#define
u128_alloc(VAR)u64 _ ## VAR ## _[2]; u128 VAR = _ ## VAR ## _
Wrap this in a struct, please. That's disgusting.
crypto/Makefile line 7
Quoted 2 lines
crypto/Makefile +1
-1-obj-$(CONFIG_CRYPTO) += api.o scatterwalk.o cipher.o digest.o compress.o \ +obj-$(CONFIG_CRYPTO) += api.o scatterwalk.o cipher.o digest.o compress.o lrw.o gfmulseq.o \
LRW and the GF(2**128) code is not configurable?
Signature · 1 line
Mathematics is the supreme nostalgia of our time.
Quoted 8 lines · Matt Mackall
Quoted 2 lines · Fruhwirth Clemens
This is the core of my LRW patch. Added test vectors.
http://grouper.ieee.org/groups/1619/email/pdf00017.pdfPlease include a URL for the standard at the top of the LRW code and next to the test vectors. I had to search around a fair bit for decent background material, would be helpful to a couple other references as well.
Ack.
Quoted 5 lines
crypto/gfmulseq.c line 71
Quoted 2 lines
crypto/gfmulseq.c new file +2
-0+static inline void findAlignment (u128 callersN,int value,int *align) { +int i;Your gfmulseq code has lots of StudlyCaps and strange whitespace, eg this '{' should be on the next line.
In fact, it's lowerCamelCase, that's intentional. The whitespace and the left '{' is an error.
Quoted 7 lines
crypto/gfmulseq.c line 74
Quoted 3 lines
crypto/gfmulseq.c new file +3
-0+/* Copy N, so lsr does not destroy caller's copy */ +u128_alloc (N ); +copy128 (N ,callersN);The usage of your u128 type is really confusing, so 'u128' is an especially bad name. I expect u128 to work like u64 and u32. I propose
gf128_t.
That's ok.
Quoted 3 lines
Quoted 1 line
+#define
u128_alloc(VAR)u64 _ ## VAR ## _[2]; u128 VAR = _ ## VAR ## _Wrap this in a struct, please. That's disgusting.
No need to be disgusted, I've seen much worse things in the kernel. I will change it to C99 compound literals.
Quoted 4 lines
crypto/Makefile line 7
Quoted 2 lines
crypto/Makefile +1
-1-obj-$(CONFIG_CRYPTO) += api.o scatterwalk.o cipher.o digest.o compress.o \ +obj-$(CONFIG_CRYPTO) += api.o scatterwalk.o cipher.o digest.o compress.o lrw.o gfmulseq.o \LRW and the
GF(2**128)code is not configurable?
No, it's a cipher mode. None of the modes is configurable.
Signature · 1 line
Fruhwirth Clemens <[email protected]> http://clemens.endorphin.org
Quoted 2 lines · Fruhwirth Clemens
In fact, it's lowerCamelCase, that's intentional.
The problem with mixing of naming styles is that it becomes difficult to remember what style is used where. Is it foo_bar_baz() or foobarbaz() or fooBarBaz() or FooBarBaz()?
Quoted 3 lines
Quoted 1 line
LRW and the
GF(2**128)code is not configurable?No, it's a cipher mode. None of the modes is configurable.
Is LRW an appropriate mode for anything but block storage?
Signature · 1 line
Mathematics is the supreme nostalgia of our time.
Quoted 5 lines · Matt Mackall
Quoted 3 lines
Quoted 1 line
LRW and the
GF(2**128)code is not configurable?No, it's a cipher mode. None of the modes is configurable.
Is LRW an appropriate mode for anything but block storage?
Looks like it is only specified for this case.
"The scope of this document is limited to direct application of the
LRW-AES transform to encrypt or decrypt data at rest, when this data consists of an integral number of wide blocks."
Where "wide blocks" means "512 consecutive bytes".
In any case, LRW should be made configurable and marked experimental.
- James
Signature · 2 lines
James Morris <[email protected]>
Quoted 8 lines · Matt Mackall
Quoted 2 lines · Fruhwirth Clemens
This is the core of my LRW patch. Added test vectors.
http://grouper.ieee.org/groups/1619/email/pdf00017.pdfPlease include a URL for the standard at the top of the LRW code and next to the test vectors. I had to search around a fair bit for decent background material, would be helpful to a couple other references as well.
Something useful if you haven't seen it yet is the tweakable block cipher paper http://www.cs.berkeley.edu/~daw/papers/tweak-crypto02.pdf
(Pointers to any critique of the system would be appreciated).
- James
Signature · 2 lines
James Morris <[email protected]>