crypto: Add helper functions for parallelized LRW and XTS modes
Subsystems (3)
cover crypto: Add helper functions for parallelized LRW and XTS modes
This series adds lrw_crypt() and xts_crypt() functions for cipher implementations that can benefit from parallel cipher block operations. To make interface flexible, caller is reponsible of allocating buffer large enough to store temporary cipher blocks. This buffer size should be as large as size of parallel blocks processed at once. Cryption callback is called with buffer size at maximum of parallel blocks size and at minimum size of one block.
Series adds LRW/XTS support to serpent-sse2 and twofish-x86_64-3way, and therefore depends on those series.
Patches 1-4: include LRW fixes/cleanups, export gf128mul table and add lrw_crypt(). Patches 5-7: add LRW support to serpent-sse2, with tcrypt tests and test vectors. Patches 8-10: add LRW support to twofish-x86_64-3way, with tcrypt tests and test vectors. Patches 11-12: include XTS cleanup for blocksize usage and add xts_crypt(). Patches 13-15: add XTS support to serpent-sse2, with tcrypt tests and test vectors. Patches 16-18: add XTS support to twofish-x86_64-3way, with tcrypt tests and test vectors.
---
Jussi Kivilinna (18):
crypto: lrw: fix memleak
crypto: lrw: use blocksize constant
crypto: lrw: split gf128mul table initialization from setkey
crypto: lrw: add interface for parallelized cipher implementions
crypto: testmgr: add lrw(serpent) test vectors
crypto: tcrypt: add lrw(serpent) tests
crypto: serpent-sse2: add lrw support
crypto: testmgr: add lrw(twofish) test vectors
crypto: tcrypt: add lrw(twofish) tests
crypto: twofish-x86_64-3way: add lrw support
crypto: xts: use blocksize constant
crypto: xts: add interface for parallelized cipher implementations
crypto: testmgr: add xts(serpent) test vectors
crypto: tcrypt: add xts(serpent) tests
crypto: serpent-sse2: add xts support
crypto: testmgr: add xts(twofish) test vectors
crypto: tcrypt: add xts(twofish) tests
crypto: twofish-x86_64-3way: add xts support
arch/x86/crypto/serpent_sse2_glue.c | 387 ++++++ arch/x86/crypto/twofish_glue_3way.c | 250 ++++ crypto/lrw.c | 155 ++ crypto/serpent.c | 10 crypto/tcrypt.c | 28 crypto/tcrypt.h | 2 crypto/testmgr.c | 60 + crypto/testmgr.h | 2366 +++++++++++++++++++++++++++++++++++ crypto/twofish_common.c | 13 crypto/xts.c | 78 + include/crypto/lrw.h | 43 + include/crypto/serpent.h | 2 include/crypto/twofish.h | 2 include/crypto/xts.h | 27 14 files changed, 3379 insertions(+), 44 deletions(-) create mode 100644 include/crypto/lrw.h create mode 100644 include/crypto/xts.h
PATCH 1/18 crypto: lrw: fix memleak
LRW module leaks child cipher memory when init_tfm() fails because of child block size not being 16.
--- crypto/lrw.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
crypto/lrw.c +1 -0
@@ -220,6 +220,7 @@ static int init_tfm(struct crypto_tfm *tfm)if (crypto_cipher_blocksize (cipher) !=16 ) { *flags |=CRYPTO_TFM_RES_BAD_BLOCK_LEN ; +crypto_free_cipher (cipher);return -EINVAL ; }
PATCH 2/18 crypto: lrw: use blocksize constant
In reply to Jussi KivilinnaLRW has fixed blocksize of 16. Define LRW_BLOCK_SIZE and use in place of crypto_cipher_blocksize().
--- crypto/lrw.c | 8 +++++--- 1 files changed, 5 insertions(+), 3 deletions(-)
crypto/lrw.c +5 -3
@@ -27,6 +27,8 @@#include <crypto/b128ops.h> #include <crypto/gf128mul.h> +#define LRW_BLOCK_SIZE 16 +struct priv {struct crypto_cipher *child;/* optimizes multiplying a random (non incrementing, as at the
@@ -61,7 +63,7 @@ static int setkey(struct crypto_tfm *parent, const u8 *key,struct crypto_cipher *child = ctx->child;int err, i;be128 tmp = {0 }; -int bsize =crypto_cipher_blocksize (child); +int bsize =LRW_BLOCK_SIZE ;crypto_cipher_clear_flags (child,CRYPTO_TFM_REQ_MASK ); crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
@@ -134,7 +136,7 @@ static int crypt(struct blkcipher_desc *d, {int err;unsigned int avail; -const int bs =crypto_cipher_blocksize (ctx->child); +const int bs =LRW_BLOCK_SIZE ;struct sinfo s = { .tfm =crypto_cipher_tfm (ctx->child), .fn = fn
@@ -218,7 +220,7 @@ static int init_tfm(struct crypto_tfm *tfm)if (IS_ERR (cipher))return PTR_ERR (cipher); -if (crypto_cipher_blocksize (cipher) !=16 ) { +if (crypto_cipher_blocksize (cipher) !=LRW_BLOCK_SIZE ) { *flags |=CRYPTO_TFM_RES_BAD_BLOCK_LEN ;crypto_free_cipher (cipher);return -EINVAL ;
PATCH 3/18 crypto: lrw: split gf128mul table initialization from setkey
In reply to Jussi KivilinnaSplit gf128mul initialization from setkey so that it can be used outside lrw-module.
--- crypto/lrw.c | 61 ++++++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 40 insertions(+), 21 deletions(-)
crypto/lrw.c +40 -21
@@ -29,8 +29,7 @@#define LRW_BLOCK_SIZE 16 -struct priv { -struct crypto_cipher *child; +struct lrw_table_ctx {/* optimizes multiplying a random (non incrementing, as at the * start of a new sector) value with key2, we could also have * used 4k optimization tables or no optimization at all. In the
@@ -45,6 +44,11 @@ struct priv {be128 mulinc[128 ]; }; +struct priv { +struct crypto_cipher *child; +struct lrw_table_ctx table; +}; +static inline void setbit128_bbe (void *b,int bit) { __set_bit(bit ^ (0x80 -
@@ -56,28 +60,16 @@ static inline void setbit128_bbe(void *b, int bit) ), b); } -static int setkey (struct crypto_tfm *parent,const u8 *key, -unsigned int keylen) +static int lrw_init_table (struct lrw_table_ctx *ctx,const u8 *tweak) { -struct priv *ctx =crypto_tfm_ctx (parent); -struct crypto_cipher *child = ctx->child; -int err, i;be128 tmp = {0 }; -int bsize =LRW_BLOCK_SIZE ; - -crypto_cipher_clear_flags (child,CRYPTO_TFM_REQ_MASK ); -crypto_cipher_set_flags (child,crypto_tfm_get_flags (parent) & -CRYPTO_TFM_REQ_MASK ); -if ((err =crypto_cipher_setkey (child, key, keylen - bsize))) -return err; -crypto_tfm_set_flags (parent,crypto_cipher_get_flags (child) & -CRYPTO_TFM_RES_MASK ); +int i;if (ctx->table)gf128mul_free_64k (ctx->table);/* initialize multiplication table for Key2 */ - ctx->table =gf128mul_init_64k_bbe ((be128 *)(key + keylen - bsize)); + ctx->table =gf128mul_init_64k_bbe ((be128 *)tweak);if (!ctx->table)return -ENOMEM ;
@@ -91,6 +83,32 @@ static int setkey(struct crypto_tfm *parent, const u8 *key,return 0 ; } +static void lrw_free_table (struct lrw_table_ctx *ctx) +{ +if (ctx->table) +gf128mul_free_64k (ctx->table); +} + +static int setkey (struct crypto_tfm *parent,const u8 *key, +unsigned int keylen) +{ +struct priv *ctx =crypto_tfm_ctx (parent); +struct crypto_cipher *child = ctx->child; +int err, bsize =LRW_BLOCK_SIZE ; +const u8 *tweak = key + keylen - bsize; + +crypto_cipher_clear_flags (child,CRYPTO_TFM_REQ_MASK ); +crypto_cipher_set_flags (child,crypto_tfm_get_flags (parent) & +CRYPTO_TFM_REQ_MASK ); + err =crypto_cipher_setkey (child, key, keylen - bsize); +if (err) +return err; +crypto_tfm_set_flags (parent,crypto_cipher_get_flags (child) & +CRYPTO_TFM_RES_MASK ); + +return lrw_init_table (&ctx->table, tweak); +} +struct sinfo {be128 t;struct crypto_tfm *tfm;
@@ -157,7 +175,7 @@ static int crypt(struct blkcipher_desc *d, s.t = *iv;/* T <- I*Key2 */ -gf128mul_64k_bbe (&s.t, ctx->table); +gf128mul_64k_bbe (&s.t, ctx->table.table); goto first;
@@ -165,7 +183,8 @@ static int crypt(struct blkcipher_desc *d, do { /* T <- I*Key2, using the optimization * discussed in the specification */ - be128_xor(&s.t, &s.t, &ctx->mulinc[get_index128(iv)]); + be128_xor(&s.t, &s.t, + &ctx->table.mulinc[get_index128(iv)]); inc(iv); first:
@@ -233,8 +252,8 @@ static int init_tfm(struct crypto_tfm *tfm)static void exit_tfm (struct crypto_tfm *tfm) {struct priv *ctx =crypto_tfm_ctx (tfm); -if (ctx->table) -gf128mul_free_64k (ctx->table); + +lrw_free_table (&ctx->table);crypto_free_cipher (ctx->child); }
PATCH 4/18 crypto: lrw: add interface for parallelized cipher implementions
In reply to Jussi KivilinnaExport gf128mul table initialization routines and add lrw_crypt() function that can be used by cipher implementations that can benefit from parallelized cipher operations.
--- crypto/lrw.c | 105 ++++++++++++++++++++++++++++++++++++++++---------- include/crypto/lrw.h | 43 ++++++++++++++++++++ 2 files changed, 128 insertions(+), 20 deletions(-) create mode 100644 include/crypto/lrw.h
crypto/lrw.c +85 -20
@@ -3,7 +3,7 @@* * Copyright (c) 2006 Rik Snel <[email protected]> * -* Based om ecb.c +* Based on ecb.c * Copyright (c) 2006 Herbert Xu <[email protected]> * * This program is free software; you can redistribute it and/or modify it
@@ -16,6 +16,7 @@* http://www.mail-archive.com/[email protected]/msg00173.html * * The test vectors are included in the testing module tcrypt.[ch] */ +#include <crypto/algapi.h> #include <linux/err.h> #include <linux/init.h>
@@ -26,23 +27,7 @@#include <crypto/b128ops.h> #include <crypto/gf128mul.h> - -#define LRW_BLOCK_SIZE 16 - -struct lrw_table_ctx { -/* optimizes multiplying a random (non incrementing, as at the -* start of a new sector) value with key2, we could also have -* used 4k optimization tables or no optimization at all. In the -* latter case we would have to store key2 here */ -struct gf128mul_64k *table; -/* stores: -* key2*{ 0,0,...0,0,0,0,1 }, key2*{ 0,0,...0,0,0,1,1 }, -* key2*{ 0,0,...0,0,1,1,1 }, key2*{ 0,0,...0,1,1,1,1 } -* key2*{ 0,0,...1,1,1,1,1 }, etc -* needed for optimized multiplication of incrementing values -* with key2 */ -be128 mulinc[128 ]; -}; +#include <crypto/lrw.h> struct priv {struct crypto_cipher *child;
@@ -60,7 +45,7 @@ static inline void setbit128_bbe(void *b, int bit) ), b); } -static int lrw_init_table (struct lrw_table_ctx *ctx,const u8 *tweak) +int lrw_init_table (struct lrw_table_ctx *ctx,const u8 *tweak) {be128 tmp = {0 };int i;
@@ -82,12 +67,14 @@ static int lrw_init_table(struct lrw_table_ctx *ctx, const u8 *tweak)return 0 ; } +EXPORT_SYMBOL_GPL (lrw_init_table); -static void lrw_free_table (struct lrw_table_ctx *ctx) +void lrw_free_table (struct lrw_table_ctx *ctx) {if (ctx->table)gf128mul_free_64k (ctx->table); } +EXPORT_SYMBOL_GPL (lrw_free_table);static int setkey (struct crypto_tfm *parent,const u8 *key,unsigned int keylen)
@@ -227,6 +214,84 @@ static int decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, crypto_cipher_alg(ctx->child)->cia_decrypt); } +int lrw_crypt (struct blkcipher_desc *desc,struct scatterlist *sdst, +struct scatterlist *ssrc,unsigned int nbytes, +struct lrw_crypt_req *req) +{ +const unsigned int bsize =LRW_BLOCK_SIZE ; +const unsigned int max_blks = req->tbuflen / bsize; +struct lrw_table_ctx *ctx = req->table_ctx; +struct blkcipher_walk walk; +unsigned int nblocks; +be128 *iv, *src, *dst, *t; +be128 *t_buf = req->tbuf; +int err, i; + +BUG_ON (max_blks <1 ); + +blkcipher_walk_init (&walk, sdst, ssrc, nbytes); + + err =blkcipher_walk_virt (desc, &walk); + nbytes = walk.nbytes; +if (!nbytes) +return err; + + nblocks =min (walk.nbytes / bsize, max_blks); + src = (be128 *)walk.src.virt.addr; + dst = (be128 *)walk.dst.virt.addr; + +/* calculate first value of T */ + iv = (be128 *)walk.iv; + t_buf[0 ] = *iv; + +/* T <- I*Key2 */ +gf128mul_64k_bbe (&t_buf[0 ], ctx->table); + + i =0 ; + goto first; + +for (;;) { +do { +for (i =0 ; i < nblocks; i++) { +/* T <- I*Key2, using the optimization +* discussed in the specification */ +be128_xor (&t_buf[i], t, + &ctx->mulinc[get_index128 (iv)]); +inc (iv); +first: + t = &t_buf[i]; + +/* PP <- T xor P */ +be128_xor (dst + i, t, src + i); + } + +/* CC <- E(Key2,PP) */ + req->crypt_fn (req->crypt_ctx, (u8 *)dst, + nblocks * bsize); + +/* C <- T xor CC */ +for (i =0 ; i < nblocks; i++) +be128_xor (dst + i, dst + i, &t_buf[i]); + + src += nblocks; + dst += nblocks; + nbytes -= nblocks * bsize; + nblocks =min (nbytes / bsize, max_blks); + }while (nblocks >0 ); + + err =blkcipher_walk_done (desc, &walk, nbytes); + nbytes = walk.nbytes; +if (!nbytes) +break ; + + src = (be128 *)walk.src.virt.addr; + dst = (be128 *)walk.dst.virt.addr; + } + +return err; +} +EXPORT_SYMBOL_GPL (lrw_crypt); +static int init_tfm (struct crypto_tfm *tfm) {struct crypto_cipher *cipher;
include/crypto/lrw.h new file +43 -0
43 lines not on this page · raw message
PATCH 5/18 crypto: testmgr: add lrw(serpent) test vectors
In reply to Jussi KivilinnaAdd test vectors for lrw(serpent). These are generated from lrw(aes) test vectors.
--- crypto/testmgr.c | 15 ++ crypto/testmgr.h | 502 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 517 insertions(+), 0 deletions(-)
crypto/testmgr.c +15 -0
@@ -2297,6 +2297,21 @@ static const struct alg_test_desc alg_test_descs[] = { } } }, { + .alg = "lrw(serpent)", + .test = alg_test_skcipher, + .suite = { + .cipher = { + .enc = { + .vecs = serpent_lrw_enc_tv_template, + .count = SERPENT_LRW_ENC_TEST_VECTORS + }, + .dec = { + .vecs = serpent_lrw_dec_tv_template, + .count = SERPENT_LRW_DEC_TEST_VECTORS + } + } + } + }, { .alg = "lzo", .test = alg_test_comp, .suite = {
crypto/testmgr.h +502 -0
514 lines not on this page · raw message
PATCH 6/18 crypto: tcrypt: add lrw(serpent) tests
In reply to Jussi Kivilinna--- crypto/tcrypt.c | 9 +++++++++ crypto/tcrypt.h | 1 + 2 files changed, 10 insertions(+), 0 deletions(-)
crypto/tcrypt.c +9 -0
@@ -996,6 +996,7 @@ static int do_test(int m) ret +=tcrypt_test ("ecb(serpent)" ); ret +=tcrypt_test ("cbc(serpent)" ); ret +=tcrypt_test ("ctr(serpent)" ); + ret +=tcrypt_test ("lrw(serpent)" );break ;case 10 :
@@ -1305,6 +1306,10 @@ static int do_test(int m) speed_template_16_32);test_cipher_speed ("ctr(serpent)" ,DECRYPT , sec,NULL ,0 , speed_template_16_32); +test_cipher_speed ("lrw(serpent)" ,ENCRYPT , sec,NULL ,0 , + speed_template_32_48); +test_cipher_speed ("lrw(serpent)" ,DECRYPT , sec,NULL ,0 , + speed_template_32_48);break ;case 300 :
@@ -1521,6 +1526,10 @@ static int do_test(int m) speed_template_16_32);test_acipher_speed ("ctr(serpent)" ,DECRYPT , sec,NULL ,0 , speed_template_16_32); +test_acipher_speed ("lrw(serpent)" ,ENCRYPT , sec,NULL ,0 , + speed_template_32_48); +test_acipher_speed ("lrw(serpent)" ,DECRYPT , sec,NULL ,0 , + speed_template_32_48);break ;case 1000 :
crypto/tcrypt.h +1 -0
@@ -51,6 +51,7 @@ static u8 speed_template_8_32[] = {8, 32, 0};static u8 speed_template_16_32[] = {16 ,32 ,0 };static u8 speed_template_16_24_32[] = {16 ,24 ,32 ,0 };static u8 speed_template_32_40_48[] = {32 ,40 ,48 ,0 }; +static u8 speed_template_32_48[] = {32 ,48 ,0 };static u8 speed_template_32_48_64[] = {32 ,48 ,64 ,0 };/*
PATCH 7/18 crypto: serpent-sse2: add lrw support
In reply to Jussi KivilinnaPatch adds LRW support for serpent-sse2 by using lrw_crypt(). Patch has been tested with tcrypt and automated filesystem tests.
Tcrypt benchmarks results (serpent-sse2/serpent_generic speed ratios):
Benchmark results with tcrypt:
Intel Celeron T1600 (x86_64) (fam:6, model:15, step:13):
size lrw-enc lrw-dec 16B 1.00x 0.96x 64B 1.01x 1.01x 256B 3.01x 2.97x 1024B 3.39x 3.33x 8192B 3.35x 3.33x
AMD Phenom II 1055T (x86_64) (fam:16, model:10):
size lrw-enc lrw-dec 16B 0.98x 1.03x 64B 1.01x 1.04x 256B 2.10x 2.14x 1024B 2.28x 2.33x 8192B 2.30x 2.33x
Intel Atom N270 (i586):
size lrw-enc lrw-dec 16B 0.97x 0.97x 64B 1.47x 1.50x 256B 1.72x 1.69x 1024B 1.88x 1.81x 8192B 1.84x 1.79x
--- arch/x86/crypto/serpent_sse2_glue.c | 211 +++++++++++++++++++++++++++++++++++ crypto/serpent.c | 10 +- include/crypto/serpent.h | 2 3 files changed, 221 insertions(+), 2 deletions(-)
arch/x86/crypto/serpent_sse2_glue.c +211 -0
@@ -38,12 +38,17 @@#include <crypto/cryptd.h> #include <crypto/b128ops.h> #include <crypto/ctr.h> +#include <crypto/lrw.h> #include <asm/i387.h> #include <asm/serpent.h> #include <crypto/scatterwalk.h> #include <linux/workqueue.h> #include <linux/spinlock.h> +#if defined(CONFIG_CRYPTO_LRW ) || defined(CONFIG_CRYPTO_LRW_MODULE ) +#define HAS_LRW +#endif +struct async_serpent_ctx {struct cryptd_ablkcipher *cryptd_tfm; };
@@ -459,6 +464,152 @@ static struct crypto_alg blk_ctr_alg = { }, }; +#ifdef HAS_LRW + +struct crypt_priv { +struct serpent_ctx *ctx; +bool fpu_enabled; +}; + +static void encrypt_callback (void *priv,u8 *srcdst,unsigned int nbytes) +{ +const unsigned int bsize =SERPENT_BLOCK_SIZE ; +struct crypt_priv *ctx = priv; +int i; + + ctx->fpu_enabled =serpent_fpu_begin (ctx->fpu_enabled, nbytes); + +if (nbytes == bsize *SERPENT_PARALLEL_BLOCKS ) { +serpent_enc_blk_xway (ctx->ctx, srcdst, srcdst); +return ; + } + +for (i =0 ; i < nbytes / bsize; i++, srcdst += bsize) +__serpent_encrypt (ctx->ctx, srcdst, srcdst); +} + +static void decrypt_callback (void *priv,u8 *srcdst,unsigned int nbytes) +{ +const unsigned int bsize =SERPENT_BLOCK_SIZE ; +struct crypt_priv *ctx = priv; +int i; + + ctx->fpu_enabled =serpent_fpu_begin (ctx->fpu_enabled, nbytes); + +if (nbytes == bsize *SERPENT_PARALLEL_BLOCKS ) { +serpent_dec_blk_xway (ctx->ctx, srcdst, srcdst); +return ; + } + +for (i =0 ; i < nbytes / bsize; i++, srcdst += bsize) +__serpent_decrypt (ctx->ctx, srcdst, srcdst); +} + +struct serpent_lrw_ctx { +struct lrw_table_ctx lrw_table; +struct serpent_ctx serpent_ctx; +}; + +static int lrw_serpent_setkey (struct crypto_tfm *tfm,const u8 *key, +unsigned int keylen) +{ +struct serpent_lrw_ctx *ctx =crypto_tfm_ctx (tfm); +int err; + + err =__serpent_setkey (&ctx->serpent_ctx, key, keylen - +SERPENT_BLOCK_SIZE ); +if (err) +return err; + +return lrw_init_table (&ctx->lrw_table, key + keylen - +SERPENT_BLOCK_SIZE ); +} + +static int lrw_encrypt (struct blkcipher_desc *desc,struct scatterlist *dst, +struct scatterlist *src,unsigned int nbytes) +{ +struct serpent_lrw_ctx *ctx =crypto_blkcipher_ctx (desc->tfm); +be128 buf[SERPENT_PARALLEL_BLOCKS ]; +struct crypt_priv crypt_ctx = { + .ctx = &ctx->serpent_ctx, + .fpu_enabled = false, + }; +struct lrw_crypt_req req = { + .tbuf = buf, + .tbuflen =sizeof (buf), + + .table_ctx = &ctx->lrw_table, + .crypt_ctx = &crypt_ctx, + .crypt_fn = encrypt_callback, + }; +int ret; + + ret =lrw_crypt (desc, dst, src, nbytes, &req); +serpent_fpu_end (crypt_ctx.fpu_enabled); + +return ret; +} + +static int lrw_decrypt (struct blkcipher_desc *desc,struct scatterlist *dst, +struct scatterlist *src,unsigned int nbytes) +{ +struct serpent_lrw_ctx *ctx =crypto_blkcipher_ctx (desc->tfm); +be128 buf[SERPENT_PARALLEL_BLOCKS ]; +struct crypt_priv crypt_ctx = { + .ctx = &ctx->serpent_ctx, + .fpu_enabled = false, + }; +struct lrw_crypt_req req = { + .tbuf = buf, + .tbuflen =sizeof (buf), + + .table_ctx = &ctx->lrw_table, + .crypt_ctx = &crypt_ctx, + .crypt_fn = decrypt_callback, + }; +int ret; + + ret =lrw_crypt (desc, dst, src, nbytes, &req); +serpent_fpu_end (crypt_ctx.fpu_enabled); + +return ret; +} + +static void lrw_exit_tfm (struct crypto_tfm *tfm) +{ +struct serpent_lrw_ctx *ctx =crypto_tfm_ctx (tfm); + +lrw_free_table (&ctx->lrw_table); +} + +static struct crypto_alg blk_lrw_alg = { + .cra_name ="__lrw-serpent-sse2" , + .cra_driver_name ="__driver-lrw-serpent-sse2" , + .cra_priority =0 , + .cra_flags =CRYPTO_ALG_TYPE_BLKCIPHER , + .cra_blocksize =SERPENT_BLOCK_SIZE , + .cra_ctxsize =sizeof (struct serpent_lrw_ctx ), + .cra_alignmask =0 , + .cra_type = &crypto_blkcipher_type, + .cra_module =THIS_MODULE , + .cra_list =LIST_HEAD_INIT (blk_lrw_alg.cra_list), + .cra_exit = lrw_exit_tfm, + .cra_u = { + .blkcipher = { + .min_keysize =SERPENT_MIN_KEY_SIZE + +SERPENT_BLOCK_SIZE , + .max_keysize =SERPENT_MAX_KEY_SIZE + +SERPENT_BLOCK_SIZE , + .ivsize =SERPENT_BLOCK_SIZE , + .setkey = lrw_serpent_setkey, + .encrypt = lrw_encrypt, + .decrypt = lrw_decrypt, + }, + }, +}; + +#endif +static int ablk_set_key (struct crypto_ablkcipher *tfm,const u8 *key,unsigned int key_len) {
@@ -657,6 +808,48 @@ static struct crypto_alg ablk_ctr_alg = { }, }; +#ifdef HAS_LRW + +static int ablk_lrw_init (struct crypto_tfm *tfm) +{ +struct cryptd_ablkcipher *cryptd_tfm; + + cryptd_tfm =cryptd_alloc_ablkcipher ("__driver-lrw-serpent-sse2" ,0 ,0 ); +if (IS_ERR (cryptd_tfm)) +return PTR_ERR (cryptd_tfm); +ablk_init_common (tfm, cryptd_tfm); +return 0 ; +} + +static struct crypto_alg ablk_lrw_alg = { + .cra_name ="lrw(serpent)" , + .cra_driver_name ="lrw-serpent-sse2" , + .cra_priority =400 , + .cra_flags =CRYPTO_ALG_TYPE_ABLKCIPHER |CRYPTO_ALG_ASYNC , + .cra_blocksize =SERPENT_BLOCK_SIZE , + .cra_ctxsize =sizeof (struct async_serpent_ctx ), + .cra_alignmask =0 , + .cra_type = &crypto_ablkcipher_type, + .cra_module =THIS_MODULE , + .cra_list =LIST_HEAD_INIT (ablk_lrw_alg.cra_list), + .cra_init = ablk_lrw_init, + .cra_exit = ablk_exit, + .cra_u = { + .ablkcipher = { + .min_keysize =SERPENT_MIN_KEY_SIZE + +SERPENT_BLOCK_SIZE , + .max_keysize =SERPENT_MAX_KEY_SIZE + +SERPENT_BLOCK_SIZE , + .ivsize =SERPENT_BLOCK_SIZE , + .setkey = ablk_set_key, + .encrypt = ablk_encrypt, + .decrypt = ablk_decrypt, + }, + }, +}; + +#endif +static int __init serpent_sse2_init (void ) {int err;
@@ -684,8 +877,22 @@ static int __init serpent_sse2_init(void) err =crypto_register_alg (&ablk_ctr_alg);if (err) goto ablk_ctr_err; +#ifdef HAS_LRW + err =crypto_register_alg (&blk_lrw_alg); +if (err) + goto blk_lrw_err; + err =crypto_register_alg (&ablk_lrw_alg); +if (err) + goto ablk_lrw_err; +#endif return err; +#ifdef HAS_LRW +ablk_lrw_err: +crypto_unregister_alg (&blk_lrw_alg); +blk_lrw_err: +crypto_unregister_alg (&ablk_ctr_alg); +#endif ablk_ctr_err:crypto_unregister_alg (&ablk_cbc_alg); ablk_cbc_err:
@@ -702,6 +909,10 @@ blk_ecb_err: static void __exit serpent_sse2_exit(void) { +#ifdef HAS_LRW +crypto_unregister_alg (&ablk_lrw_alg); +crypto_unregister_alg (&blk_lrw_alg); +#endif crypto_unregister_alg (&ablk_ctr_alg);crypto_unregister_alg (&ablk_cbc_alg);crypto_unregister_alg (&ablk_ecb_alg);
crypto/serpent.c +8 -2
@@ -206,9 +206,9 @@ x1 ^= x4; x3 ^= x4; x4 &= x0; \ x4 ^= x2; -int serpent_setkey (struct crypto_tfm *tfm,const u8 *key,unsigned int keylen) +int __serpent_setkey (struct serpent_ctx *ctx,const u8 *key, +unsigned int keylen) { -struct serpent_ctx *ctx =crypto_tfm_ctx (tfm);u32 *k = ctx->expkey;u8 *k8 = (u8 *)k;u32 r0,r1,r2,r3,r4;
@@ -349,6 +349,12 @@ int serpent_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)return 0 ; } +EXPORT_SYMBOL_GPL (__serpent_setkey); + +int serpent_setkey (struct crypto_tfm *tfm,const u8 *key,unsigned int keylen) +{ +return __serpent_setkey (crypto_tfm_ctx (tfm), key, keylen); +}EXPORT_SYMBOL_GPL (serpent_setkey);void __serpent_encrypt (struct serpent_ctx *ctx,u8 *dst,const u8 *src)
include/crypto/serpent.h +2 -0
@@ -17,6 +17,8 @@ struct serpent_ctx {u32 expkey[SERPENT_EXPKEY_WORDS ]; }; +int __serpent_setkey (struct serpent_ctx *ctx,const u8 *key, +unsigned int keylen);int serpent_setkey (struct crypto_tfm *tfm,const u8 *key,unsigned int keylen);void __serpent_encrypt (struct serpent_ctx *ctx,u8 *dst,const u8 *src);
PATCH 8/18 crypto: testmgr: add lrw(twofish) test vectors
In reply to Jussi KivilinnaAdd test vectors for lrw(twofish). These are generated from lrw(aes) test vectors.
--- crypto/testmgr.c | 15 ++ crypto/testmgr.h | 501 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 516 insertions(+), 0 deletions(-)
crypto/testmgr.c +15 -0
@@ -2312,6 +2312,21 @@ static const struct alg_test_desc alg_test_descs[] = { } } }, { + .alg = "lrw(twofish)", + .test = alg_test_skcipher, + .suite = { + .cipher = { + .enc = { + .vecs = tf_lrw_enc_tv_template, + .count = TF_LRW_ENC_TEST_VECTORS + }, + .dec = { + .vecs = tf_lrw_dec_tv_template, + .count = TF_LRW_DEC_TEST_VECTORS + } + } + } + }, { .alg = "lzo", .test = alg_test_comp, .suite = {
crypto/testmgr.h +501 -0
513 lines not on this page · raw message
PATCH 9/18 crypto: tcrypt: add lrw(twofish) tests
In reply to Jussi Kivilinna--- crypto/tcrypt.c | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-)
crypto/tcrypt.c +5 -0
@@ -990,6 +990,7 @@ static int do_test(int m) ret +=tcrypt_test ("ecb(twofish)" ); ret +=tcrypt_test ("cbc(twofish)" ); ret +=tcrypt_test ("ctr(twofish)" ); + ret +=tcrypt_test ("lrw(twofish)" );break ;case 9 :
@@ -1249,6 +1250,10 @@ static int do_test(int m) speed_template_16_24_32);test_cipher_speed ("ctr(twofish)" ,DECRYPT , sec,NULL ,0 , speed_template_16_24_32); +test_cipher_speed ("lrw(twofish)" ,ENCRYPT , sec,NULL ,0 , + speed_template_32_40_48); +test_cipher_speed ("lrw(twofish)" ,DECRYPT , sec,NULL ,0 , + speed_template_32_40_48);break ;case 203 :
PATCH 10/18 crypto: twofish-x86_64-3way: add lrw support
In reply to Jussi KivilinnaPatch adds LRW support for twofish-x86_64-3way by using lrw_crypt(). Patch has been tested with tcrypt and automated filesystem tests.
Tcrypt benchmarks results (twofish-3way/twofish-asm speed ratios):
Intel Celeron T1600 (fam:6, model:15, step:13):
size lrw-enc lrw-dec
16B 0.99x 1.00x
64B 1.17x 1.17x
256B 1.26x 1.27x
1024B 1.30x 1.31x
8192B 1.31x 1.32x
AMD Phenom II 1055T (fam:16, model:10):
size lrw-enc lrw-dec
16B 1.06x 1.01x
64B 1.08x 1.14x
256B 1.19x 1.20x
1024B 1.21x 1.22x
8192B 1.23x 1.24x
--- arch/x86/crypto/twofish_glue_3way.c | 135 +++++++++++++++++++++++++++++++++++ crypto/twofish_common.c | 13 ++- include/crypto/twofish.h | 2 + 3 files changed, 145 insertions(+), 5 deletions(-)
arch/x86/crypto/twofish_glue_3way.c +135 -0
@@ -32,6 +32,11 @@#include <crypto/algapi.h> #include <crypto/twofish.h> #include <crypto/b128ops.h> +#include <crypto/lrw.h> + +#if defined(CONFIG_CRYPTO_LRW ) || defined(CONFIG_CRYPTO_LRW_MODULE ) +#define HAS_LRW +#endif /* regular block cipher functions from twofish_x86_64 module */ asmlinkage void twofish_enc_blk(struct twofish_ctx *ctx, u8 *dst,
@@ -432,6 +437,124 @@ static struct crypto_alg blk_ctr_alg = { }, }; +#ifdef HAS_LRW + +static void encrypt_callback (void *priv,u8 *srcdst,unsigned int nbytes) +{ +const unsigned int bsize =TF_BLOCK_SIZE ; +struct twofish_ctx *ctx = priv; +int i; + +if (nbytes ==3 * bsize) { +twofish_enc_blk_3way (ctx, srcdst, srcdst); +return ; + } + +for (i =0 ; i < nbytes / bsize; i++, srcdst += bsize) +twofish_enc_blk (ctx, srcdst, srcdst); +} + +static void decrypt_callback (void *priv,u8 *srcdst,unsigned int nbytes) +{ +const unsigned int bsize =TF_BLOCK_SIZE ; +struct twofish_ctx *ctx = priv; +int i; + +if (nbytes ==3 * bsize) { +twofish_dec_blk_3way (ctx, srcdst, srcdst); +return ; + } + +for (i =0 ; i < nbytes / bsize; i++, srcdst += bsize) +twofish_dec_blk (ctx, srcdst, srcdst); +} + +struct twofish_lrw_ctx { +struct lrw_table_ctx lrw_table; +struct twofish_ctx twofish_ctx; +}; + +static int lrw_twofish_setkey (struct crypto_tfm *tfm,const u8 *key, +unsigned int keylen) +{ +struct twofish_lrw_ctx *ctx =crypto_tfm_ctx (tfm); +int err; + + err =__twofish_setkey (&ctx->twofish_ctx, key, keylen -TF_BLOCK_SIZE , + &tfm->crt_flags); +if (err) +return err; + +return lrw_init_table (&ctx->lrw_table, key + keylen -TF_BLOCK_SIZE ); +} + +static int lrw_encrypt (struct blkcipher_desc *desc,struct scatterlist *dst, +struct scatterlist *src,unsigned int nbytes) +{ +struct twofish_lrw_ctx *ctx =crypto_blkcipher_ctx (desc->tfm); +be128 buf[3 ]; +struct lrw_crypt_req req = { + .tbuf = buf, + .tbuflen =sizeof (buf), + + .table_ctx = &ctx->lrw_table, + .crypt_ctx = &ctx->twofish_ctx, + .crypt_fn = encrypt_callback, + }; + +return lrw_crypt (desc, dst, src, nbytes, &req); +} + +static int lrw_decrypt (struct blkcipher_desc *desc,struct scatterlist *dst, +struct scatterlist *src,unsigned int nbytes) +{ +struct twofish_lrw_ctx *ctx =crypto_blkcipher_ctx (desc->tfm); +be128 buf[3 ]; +struct lrw_crypt_req req = { + .tbuf = buf, + .tbuflen =sizeof (buf), + + .table_ctx = &ctx->lrw_table, + .crypt_ctx = &ctx->twofish_ctx, + .crypt_fn = decrypt_callback, + }; + +return lrw_crypt (desc, dst, src, nbytes, &req); +} + +static void lrw_exit_tfm (struct crypto_tfm *tfm) +{ +struct twofish_lrw_ctx *ctx =crypto_tfm_ctx (tfm); + +lrw_free_table (&ctx->lrw_table); +} + +static struct crypto_alg blk_lrw_alg = { + .cra_name ="lrw(twofish)" , + .cra_driver_name ="lrw-twofish-3way" , + .cra_priority =300 , + .cra_flags =CRYPTO_ALG_TYPE_BLKCIPHER , + .cra_blocksize =TF_BLOCK_SIZE , + .cra_ctxsize =sizeof (struct twofish_lrw_ctx ), + .cra_alignmask =0 , + .cra_type = &crypto_blkcipher_type, + .cra_module =THIS_MODULE , + .cra_list =LIST_HEAD_INIT (blk_lrw_alg.cra_list), + .cra_exit = lrw_exit_tfm, + .cra_u = { + .blkcipher = { + .min_keysize =TF_MIN_KEY_SIZE +TF_BLOCK_SIZE , + .max_keysize =TF_MAX_KEY_SIZE +TF_BLOCK_SIZE , + .ivsize =TF_BLOCK_SIZE , + .setkey = lrw_twofish_setkey, + .encrypt = lrw_encrypt, + .decrypt = lrw_decrypt, + }, + }, +}; + +#endif +int __init init (void ) {int err;
@@ -445,9 +568,18 @@ int __init init(void) err =crypto_register_alg (&blk_ctr_alg);if (err) goto ctr_err; +#ifdef HAS_LRW + err =crypto_register_alg (&blk_lrw_alg); +if (err) + goto blk_lrw_err; +#endif return 0 ; +#ifdef HAS_LRW +blk_lrw_err: +crypto_unregister_alg (&blk_ctr_alg); +#endif ctr_err:crypto_unregister_alg (&blk_cbc_alg); cbc_err:
@@ -458,6 +590,9 @@ ecb_err: void __exit fini(void) { +#ifdef HAS_LRW +crypto_unregister_alg (&blk_lrw_alg); +#endif crypto_unregister_alg (&blk_ctr_alg);crypto_unregister_alg (&blk_cbc_alg);crypto_unregister_alg (&blk_ecb_alg);
crypto/twofish_common.c +8 -5
@@ -580,12 +580,9 @@ static const u8 calc_sb_tbl[512] = { ctx->a[(j) +1 ] =rol32 (y,9 ) /* Perform the key setup. */ -int twofish_setkey (struct crypto_tfm *tfm,const u8 *key,unsigned int key_len) +int __twofish_setkey (struct twofish_ctx *ctx,const u8 *key, +unsigned int key_len,u32 *flags) { - -struct twofish_ctx *ctx =crypto_tfm_ctx (tfm); -u32 *flags = &tfm->crt_flags; -int i, j, k;/* Temporaries for CALC_K. */
@@ -701,7 +698,13 @@ int twofish_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int key_len)return 0 ; } +EXPORT_SYMBOL_GPL (__twofish_setkey); +int twofish_setkey (struct crypto_tfm *tfm,const u8 *key,unsigned int key_len) +{ +return __twofish_setkey (crypto_tfm_ctx (tfm), key, key_len, + &tfm->crt_flags); +}EXPORT_SYMBOL_GPL (twofish_setkey);MODULE_LICENSE ("GPL" );
include/crypto/twofish.h +2 -0
@@ -17,6 +17,8 @@ struct twofish_ctx { u32 s[4][256], w[8], k[32]; }; +int __twofish_setkey (struct twofish_ctx *ctx,const u8 *key, +unsigned int key_len,u32 *flags);int twofish_setkey (struct crypto_tfm *tfm,const u8 *key,unsigned int key_len);#endif
PATCH 11/18 crypto: xts: use blocksize constant
In reply to Jussi KivilinnaXTS has fixed blocksize of 16. Define XTS_BLOCK_SIZE and use in place of crypto_cipher_blocksize().
--- crypto/xts.c | 8 +++++--- 1 files changed, 5 insertions(+), 3 deletions(-)
crypto/xts.c +5 -3
@@ -24,6 +24,8 @@#include <crypto/b128ops.h> #include <crypto/gf128mul.h> +#define XTS_BLOCK_SIZE 16 +struct priv {struct crypto_cipher *child;struct crypto_cipher *tweak;
@@ -96,7 +98,7 @@ static int crypt(struct blkcipher_desc *d, {int err;unsigned int avail; -const int bs =crypto_cipher_blocksize (ctx->child); +const int bs =XTS_BLOCK_SIZE ;struct sinfo s = { .tfm =crypto_cipher_tfm (ctx->child), .fn = fn
@@ -177,7 +179,7 @@ static int init_tfm(struct crypto_tfm *tfm)if (IS_ERR (cipher))return PTR_ERR (cipher); -if (crypto_cipher_blocksize (cipher) !=16 ) { +if (crypto_cipher_blocksize (cipher) !=XTS_BLOCK_SIZE ) { *flags |=CRYPTO_TFM_RES_BAD_BLOCK_LEN ;crypto_free_cipher (cipher);return -EINVAL ;
@@ -192,7 +194,7 @@ static int init_tfm(struct crypto_tfm *tfm) }/* this check isn't really needed, leave it here just in case */ -if (crypto_cipher_blocksize (cipher) !=16 ) { +if (crypto_cipher_blocksize (cipher) !=XTS_BLOCK_SIZE ) {crypto_free_cipher (cipher);crypto_free_cipher (ctx->child); *flags |=CRYPTO_TFM_RES_BAD_BLOCK_LEN ;
PATCH 13/18 crypto: testmgr: add xts(serpent) test vectors
In reply to Jussi KivilinnaAdd test vectors for xts(serpent). These are generated from xts(aes) test vectors.
--- crypto/testmgr.c | 15 + crypto/testmgr.h | 682 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 697 insertions(+), 0 deletions(-)
crypto/testmgr.c +15 -0
@@ -2634,6 +2634,21 @@ static const struct alg_test_desc alg_test_descs[] = { } } }, { + .alg = "xts(serpent)", + .test = alg_test_skcipher, + .suite = { + .cipher = { + .enc = { + .vecs = serpent_xts_enc_tv_template, + .count = SERPENT_XTS_ENC_TEST_VECTORS + }, + .dec = { + .vecs = serpent_xts_dec_tv_template, + .count = SERPENT_XTS_DEC_TEST_VECTORS + } + } + } + }, { .alg = "zlib", .test = alg_test_pcomp, .suite = {
crypto/testmgr.h +682 -0
694 lines not on this page · raw message
PATCH 12/18 crypto: xts: add interface for parallelized cipher implementations
In reply to Jussi KivilinnaAdd xts_crypt() function that can be used by cipher implementations that can benefit from parallelized cipher operations.
--- crypto/xts.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++- include/crypto/xts.h | 27 ++++++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 include/crypto/xts.h
crypto/xts.c +72 -2
@@ -21,11 +21,10 @@#include <linux/scatterlist.h> #include <linux/slab.h> +#include <crypto/xts.h> #include <crypto/b128ops.h> #include <crypto/gf128mul.h> -#define XTS_BLOCK_SIZE 16 -struct priv {struct crypto_cipher *child;struct crypto_cipher *tweak;
@@ -167,6 +166,77 @@ static int decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, crypto_cipher_alg(ctx->child)->cia_decrypt); } +int xts_crypt (struct blkcipher_desc *desc,struct scatterlist *sdst, +struct scatterlist *ssrc,unsigned int nbytes, +struct xts_crypt_req *req) +{ +const unsigned int bsize =XTS_BLOCK_SIZE ; +const unsigned int max_blks = req->tbuflen / bsize; +struct blkcipher_walk walk; +unsigned int nblocks; +be128 *src, *dst, *t; +be128 *t_buf = req->tbuf; +int err, i; + +BUG_ON (max_blks <1 ); + +blkcipher_walk_init (&walk, sdst, ssrc, nbytes); + + err =blkcipher_walk_virt (desc, &walk); + nbytes = walk.nbytes; +if (!nbytes) +return err; + + nblocks =min (nbytes / bsize, max_blks); + src = (be128 *)walk.src.virt.addr; + dst = (be128 *)walk.dst.virt.addr; + +/* calculate first value of T */ + req->tweak_fn (req->tweak_ctx, (u8 *)&t_buf[0 ], walk.iv); + + i =0 ; + goto first; + +for (;;) { +do { +for (i =0 ; i < nblocks; i++) { +gf128mul_x_ble (&t_buf[i], t); +first: + t = &t_buf[i]; + +/* PP <- T xor P */ +be128_xor (dst + i, t, src + i); + } + +/* CC <- E(Key2,PP) */ + req->crypt_fn (req->crypt_ctx, (u8 *)dst, + nblocks * bsize); + +/* C <- T xor CC */ +for (i =0 ; i < nblocks; i++) +be128_xor (dst + i, dst + i, &t_buf[i]); + + src += nblocks; + dst += nblocks; + nbytes -= nblocks * bsize; + nblocks =min (nbytes / bsize, max_blks); + }while (nblocks >0 ); + + *(be128 *)walk.iv = *t; + + err =blkcipher_walk_done (desc, &walk, nbytes); + nbytes = walk.nbytes; +if (!nbytes) +break ; + + src = (be128 *)walk.src.virt.addr; + dst = (be128 *)walk.dst.virt.addr; + } + +return err; +} +EXPORT_SYMBOL_GPL (xts_crypt); +static int init_tfm (struct crypto_tfm *tfm) {struct crypto_cipher *cipher;
include/crypto/xts.h new file +27 -0
@@ -0,0 +1,27 @@ +#ifndef _CRYPTO_XTS_H +#define _CRYPTO_XTS_H + +#include <crypto/b128ops.h> + +struct scatterlist ; +struct blkcipher_desc ; + +#define XTS_BLOCK_SIZE 16 + +struct xts_crypt_req { +be128 *tbuf; +unsigned int tbuflen; + +void *tweak_ctx; +void (*tweak_fn)(void *ctx,u8 * dst,const u8 * src); +void *crypt_ctx; +void (*crypt_fn)(void *ctx,u8 *blks,unsigned int nbytes); +}; + +#define XTS_TWEAK_CAST (x) ((void (*)(void *, u8*, const u8*))(x)) + +int xts_crypt (struct blkcipher_desc *desc,struct scatterlist *dst, +struct scatterlist *src,unsigned int nbytes, +struct xts_crypt_req *req); + +#endif /* _CRYPTO_XTS_H */
PATCH 15/18 crypto: serpent-sse2: add xts support
In reply to Jussi KivilinnaPatch adds XTS support for serpent-sse2 by using xts_crypt(). Patch has been tested with tcrypt and automated filesystem tests.
Tcrypt benchmarks results (serpent-sse2/serpent_generic speed ratios):
Intel Celeron T1600 (x86_64) (fam:6, model:15, step:13):
size xts-enc xts-dec 16B 0.98x 1.00x 64B 1.00x 1.01x 256B 2.78x 2.75x 1024B 3.30x 3.26x 8192B 3.39x 3.30x
AMD Phenom II 1055T (x86_64) (fam:16, model:10):
size xts-enc xts-dec 16B 1.05x 1.02x 64B 1.04x 1.03x 256B 2.10x 2.05x 1024B 2.34x 2.35x 8192B 2.34x 2.40x
Intel Atom N270 (i586):
size xts-enc xts-dec 16B 0.95x 0.96x 64B 1.53x 1.50x 256B 1.72x 1.75x 1024B 1.88x 1.87x 8192B 1.86x 1.83x
--- arch/x86/crypto/serpent_sse2_glue.c | 180 +++++++++++++++++++++++++++++++++++ 1 files changed, 178 insertions(+), 2 deletions(-)
arch/x86/crypto/serpent_sse2_glue.c +178 -2
@@ -39,6 +39,7 @@#include <crypto/b128ops.h> #include <crypto/ctr.h> #include <crypto/lrw.h> +#include <crypto/xts.h> #include <asm/i387.h> #include <asm/serpent.h> #include <crypto/scatterwalk.h>
@@ -49,6 +50,10 @@#define HAS_LRW #endif +#if defined(CONFIG_CRYPTO_XTS ) || defined(CONFIG_CRYPTO_XTS_MODULE ) +#define HAS_XTS +#endif +struct async_serpent_ctx {struct cryptd_ablkcipher *cryptd_tfm; };
@@ -464,7 +469,7 @@ static struct crypto_alg blk_ctr_alg = { }, }; -#ifdef HAS_LRW +#if defined(HAS_LRW) || defined(HAS_XTS) struct crypt_priv { struct serpent_ctx *ctx;
@@ -505,6 +510,10 @@ static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)__serpent_decrypt (ctx->ctx, srcdst, srcdst); } +#endif + +#ifdef HAS_LRW + struct serpent_lrw_ctx { struct lrw_table_ctx lrw_table; struct serpent_ctx serpent_ctx;
@@ -610,6 +619,114 @@ static struct crypto_alg blk_lrw_alg = {#endif +#ifdef HAS_XTS + +struct serpent_xts_ctx { +struct serpent_ctx tweak_ctx; +struct serpent_ctx crypt_ctx; +}; + +static int xts_serpent_setkey (struct crypto_tfm *tfm,const u8 *key, +unsigned int keylen) +{ +struct serpent_xts_ctx *ctx =crypto_tfm_ctx (tfm); +u32 *flags = &tfm->crt_flags; +int err; + +/* key consists of keys of equal size concatenated, therefore +* the length must be even +*/ +if (keylen %2 ) { + *flags |=CRYPTO_TFM_RES_BAD_KEY_LEN ; +return -EINVAL ; + } + +/* first half of xts-key is for crypt */ + err =__serpent_setkey (&ctx->crypt_ctx, key, keylen /2 ); +if (err) +return err; + +/* second half of xts-key is for tweak */ +return __serpent_setkey (&ctx->tweak_ctx, key + keylen /2 , keylen /2 ); +} + +static int xts_encrypt (struct blkcipher_desc *desc,struct scatterlist *dst, +struct scatterlist *src,unsigned int nbytes) +{ +struct serpent_xts_ctx *ctx =crypto_blkcipher_ctx (desc->tfm); +be128 buf[SERPENT_PARALLEL_BLOCKS ]; +struct crypt_priv crypt_ctx = { + .ctx = &ctx->crypt_ctx, + .fpu_enabled = false, + }; +struct xts_crypt_req req = { + .tbuf = buf, + .tbuflen =sizeof (buf), + + .tweak_ctx = &ctx->tweak_ctx, + .tweak_fn =XTS_TWEAK_CAST (__serpent_encrypt), + .crypt_ctx = &crypt_ctx, + .crypt_fn = encrypt_callback, + }; +int ret; + + ret =xts_crypt (desc, dst, src, nbytes, &req); +serpent_fpu_end (crypt_ctx.fpu_enabled); + +return ret; +} + +static int xts_decrypt (struct blkcipher_desc *desc,struct scatterlist *dst, +struct scatterlist *src,unsigned int nbytes) +{ +struct serpent_xts_ctx *ctx =crypto_blkcipher_ctx (desc->tfm); +be128 buf[SERPENT_PARALLEL_BLOCKS ]; +struct crypt_priv crypt_ctx = { + .ctx = &ctx->crypt_ctx, + .fpu_enabled = false, + }; +struct xts_crypt_req req = { + .tbuf = buf, + .tbuflen =sizeof (buf), + + .tweak_ctx = &ctx->tweak_ctx, + .tweak_fn =XTS_TWEAK_CAST (__serpent_encrypt), + .crypt_ctx = &crypt_ctx, + .crypt_fn = decrypt_callback, + }; +int ret; + + ret =xts_crypt (desc, dst, src, nbytes, &req); +serpent_fpu_end (crypt_ctx.fpu_enabled); + +return ret; +} + +static struct crypto_alg blk_xts_alg = { + .cra_name ="__xts-serpent-sse2" , + .cra_driver_name ="__driver-xts-serpent-sse2" , + .cra_priority =0 , + .cra_flags =CRYPTO_ALG_TYPE_BLKCIPHER , + .cra_blocksize =SERPENT_BLOCK_SIZE , + .cra_ctxsize =sizeof (struct serpent_xts_ctx ), + .cra_alignmask =0 , + .cra_type = &crypto_blkcipher_type, + .cra_module =THIS_MODULE , + .cra_list =LIST_HEAD_INIT (blk_xts_alg.cra_list), + .cra_u = { + .blkcipher = { + .min_keysize =SERPENT_MIN_KEY_SIZE *2 , + .max_keysize =SERPENT_MAX_KEY_SIZE *2 , + .ivsize =SERPENT_BLOCK_SIZE , + .setkey = xts_serpent_setkey, + .encrypt = xts_encrypt, + .decrypt = xts_decrypt, + }, + }, +}; + +#endif +static int ablk_set_key (struct crypto_ablkcipher *tfm,const u8 *key,unsigned int key_len) {
@@ -850,6 +967,46 @@ static struct crypto_alg ablk_lrw_alg = {#endif +#ifdef HAS_XTS + +static int ablk_xts_init (struct crypto_tfm *tfm) +{ +struct cryptd_ablkcipher *cryptd_tfm; + + cryptd_tfm =cryptd_alloc_ablkcipher ("__driver-xts-serpent-sse2" ,0 ,0 ); +if (IS_ERR (cryptd_tfm)) +return PTR_ERR (cryptd_tfm); +ablk_init_common (tfm, cryptd_tfm); +return 0 ; +} + +static struct crypto_alg ablk_xts_alg = { + .cra_name ="xts(serpent)" , + .cra_driver_name ="xts-serpent-sse2" , + .cra_priority =400 , + .cra_flags =CRYPTO_ALG_TYPE_ABLKCIPHER |CRYPTO_ALG_ASYNC , + .cra_blocksize =SERPENT_BLOCK_SIZE , + .cra_ctxsize =sizeof (struct async_serpent_ctx ), + .cra_alignmask =0 , + .cra_type = &crypto_ablkcipher_type, + .cra_module =THIS_MODULE , + .cra_list =LIST_HEAD_INIT (ablk_xts_alg.cra_list), + .cra_init = ablk_xts_init, + .cra_exit = ablk_exit, + .cra_u = { + .ablkcipher = { + .min_keysize =SERPENT_MIN_KEY_SIZE *2 , + .max_keysize =SERPENT_MAX_KEY_SIZE *2 , + .ivsize =SERPENT_BLOCK_SIZE , + .setkey = ablk_set_key, + .encrypt = ablk_encrypt, + .decrypt = ablk_decrypt, + }, + }, +}; + +#endif +static int __init serpent_sse2_init (void ) {int err;
@@ -885,14 +1042,29 @@ static int __init serpent_sse2_init(void)if (err) goto ablk_lrw_err;#endif +#ifdef HAS_XTS + err =crypto_register_alg (&blk_xts_alg); +if (err) + goto blk_xts_err; + err =crypto_register_alg (&ablk_xts_alg); +if (err) + goto ablk_xts_err; +#endif return err; +#ifdef HAS_XTS +crypto_unregister_alg (&ablk_xts_alg); +ablk_xts_err: +crypto_unregister_alg (&blk_xts_alg); +blk_xts_err: +#endif #ifdef HAS_LRW +crypto_unregister_alg (&ablk_lrw_alg); ablk_lrw_err:crypto_unregister_alg (&blk_lrw_alg); blk_lrw_err: -crypto_unregister_alg (&ablk_ctr_alg);#endif +crypto_unregister_alg (&ablk_ctr_alg); ablk_ctr_err:crypto_unregister_alg (&ablk_cbc_alg); ablk_cbc_err:
@@ -909,6 +1081,10 @@ blk_ecb_err: static void __exit serpent_sse2_exit(void) { +#ifdef HAS_XTS +crypto_unregister_alg (&ablk_xts_alg); +crypto_unregister_alg (&blk_xts_alg); +#endif #ifdef HAS_LRW crypto_unregister_alg (&ablk_lrw_alg);crypto_unregister_alg (&blk_lrw_alg);
PATCH 14/18 crypto: tcrypt: add xts(serpent) tests
In reply to Jussi Kivilinna--- crypto/tcrypt.c | 9 +++++++++ crypto/tcrypt.h | 1 + 2 files changed, 10 insertions(+), 0 deletions(-)
crypto/tcrypt.c +9 -0
@@ -998,6 +998,7 @@ static int do_test(int m) ret +=tcrypt_test ("cbc(serpent)" ); ret +=tcrypt_test ("ctr(serpent)" ); ret +=tcrypt_test ("lrw(serpent)" ); + ret +=tcrypt_test ("xts(serpent)" );break ;case 10 :
@@ -1315,6 +1316,10 @@ static int do_test(int m) speed_template_32_48);test_cipher_speed ("lrw(serpent)" ,DECRYPT , sec,NULL ,0 , speed_template_32_48); +test_cipher_speed ("xts(serpent)" ,ENCRYPT , sec,NULL ,0 , + speed_template_32_64); +test_cipher_speed ("xts(serpent)" ,DECRYPT , sec,NULL ,0 , + speed_template_32_64);break ;case 300 :
@@ -1535,6 +1540,10 @@ static int do_test(int m) speed_template_32_48);test_acipher_speed ("lrw(serpent)" ,DECRYPT , sec,NULL ,0 , speed_template_32_48); +test_acipher_speed ("xts(serpent)" ,ENCRYPT , sec,NULL ,0 , + speed_template_32_64); +test_acipher_speed ("xts(serpent)" ,DECRYPT , sec,NULL ,0 , + speed_template_32_64);break ;case 1000 :
crypto/tcrypt.h +1 -0
@@ -53,6 +53,7 @@ static u8 speed_template_16_24_32[] = {16, 24, 32, 0};static u8 speed_template_32_40_48[] = {32 ,40 ,48 ,0 };static u8 speed_template_32_48[] = {32 ,48 ,0 };static u8 speed_template_32_48_64[] = {32 ,48 ,64 ,0 }; +static u8 speed_template_32_64[] = {32 ,64 ,0 };/* * Digest speed tests
PATCH 17/18 crypto: tcrypt: add xts(twofish) tests
In reply to Jussi Kivilinna--- crypto/tcrypt.c | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-)
crypto/tcrypt.c +5 -0
@@ -991,6 +991,7 @@ static int do_test(int m) ret +=tcrypt_test ("cbc(twofish)" ); ret +=tcrypt_test ("ctr(twofish)" ); ret +=tcrypt_test ("lrw(twofish)" ); + ret +=tcrypt_test ("xts(twofish)" );break ;case 9 :
@@ -1255,6 +1256,10 @@ static int do_test(int m) speed_template_32_40_48);test_cipher_speed ("lrw(twofish)" ,DECRYPT , sec,NULL ,0 , speed_template_32_40_48); +test_cipher_speed ("xts(twofish)" ,ENCRYPT , sec,NULL ,0 , + speed_template_32_48_64); +test_cipher_speed ("xts(twofish)" ,DECRYPT , sec,NULL ,0 , + speed_template_32_48_64);break ;case 203 :
PATCH 16/18 crypto: testmgr: add xts(twofish) test vectors
In reply to Jussi KivilinnaAdd test vectors for xts(twofish). These are generated from xts(twofish) test vectors.
--- crypto/testmgr.c | 15 + crypto/testmgr.h | 681 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 696 insertions(+), 0 deletions(-)
crypto/testmgr.c +15 -0
@@ -2649,6 +2649,21 @@ static const struct alg_test_desc alg_test_descs[] = { } } }, { + .alg = "xts(twofish)", + .test = alg_test_skcipher, + .suite = { + .cipher = { + .enc = { + .vecs = tf_xts_enc_tv_template, + .count = TF_XTS_ENC_TEST_VECTORS + }, + .dec = { + .vecs = tf_xts_dec_tv_template, + .count = TF_XTS_DEC_TEST_VECTORS + } + } + } + }, { .alg = "zlib", .test = alg_test_pcomp, .suite = {
crypto/testmgr.h +681 -0
693 lines not on this page · raw message
PATCH 18/18 crypto: twofish-x86_64-3way: add xts support
In reply to Jussi KivilinnaPatch adds XTS support for twofish-x86_64-3way by using xts_crypt(). Patch has been tested with tcrypt and automated filesystem tests.
Tcrypt benchmarks results (twofish-3way/twofish-asm speed ratios):
Intel Celeron T1600 (fam:6, model:15, step:13):
size xts-enc xts-dec 16B 0.98x 1.00x 64B 1.14x 1.15x 256B 1.23x 1.25x 1024B 1.26x 1.29x 8192B 1.28x 1.30x
AMD Phenom II 1055T (fam:16, model:10):
size xts-enc xts-dec 16B 1.03x 1.03x 64B 1.13x 1.16x 256B 1.20x 1.20x 1024B 1.22x 1.22x 8192B 1.22x 1.21x
--- arch/x86/crypto/twofish_glue_3way.c | 119 ++++++++++++++++++++++++++++++++++- 1 files changed, 117 insertions(+), 2 deletions(-)
arch/x86/crypto/twofish_glue_3way.c +117 -2
@@ -33,11 +33,16 @@#include <crypto/twofish.h> #include <crypto/b128ops.h> #include <crypto/lrw.h> +#include <crypto/xts.h> #if defined(CONFIG_CRYPTO_LRW ) || defined(CONFIG_CRYPTO_LRW_MODULE )#define HAS_LRW #endif +#if defined(CONFIG_CRYPTO_XTS ) || defined(CONFIG_CRYPTO_XTS_MODULE ) +#define HAS_XTS +#endif +/* regular block cipher functions from twofish_x86_64 module */ asmlinkage void twofish_enc_blk(struct twofish_ctx *ctx, u8 *dst,const u8 *src);
@@ -437,7 +442,7 @@ static struct crypto_alg blk_ctr_alg = { }, }; -#ifdef HAS_LRW +#if defined(HAS_LRW ) || defined(HAS_XTS )static void encrypt_callback (void *priv,u8 *srcdst,unsigned int nbytes) {
@@ -469,6 +474,10 @@ static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)twofish_dec_blk (ctx, srcdst, srcdst); } +#endif + +#ifdef HAS_LRW + struct twofish_lrw_ctx { struct lrw_table_ctx lrw_table; struct twofish_ctx twofish_ctx;
@@ -555,6 +564,99 @@ static struct crypto_alg blk_lrw_alg = {#endif +#ifdef HAS_XTS + +struct twofish_xts_ctx { +struct twofish_ctx tweak_ctx; +struct twofish_ctx crypt_ctx; +}; + +static int xts_twofish_setkey (struct crypto_tfm *tfm,const u8 *key, +unsigned int keylen) +{ +struct twofish_xts_ctx *ctx =crypto_tfm_ctx (tfm); +u32 *flags = &tfm->crt_flags; +int err; + +/* key consists of keys of equal size concatenated, therefore +* the length must be even +*/ +if (keylen %2 ) { + *flags |=CRYPTO_TFM_RES_BAD_KEY_LEN ; +return -EINVAL ; + } + +/* first half of xts-key is for crypt */ + err =__twofish_setkey (&ctx->crypt_ctx, key, keylen /2 , flags); +if (err) +return err; + +/* second half of xts-key is for tweak */ +return __twofish_setkey (&ctx->tweak_ctx, key + keylen /2 , keylen /2 , + flags); +} + +static int xts_encrypt (struct blkcipher_desc *desc,struct scatterlist *dst, +struct scatterlist *src,unsigned int nbytes) +{ +struct twofish_xts_ctx *ctx =crypto_blkcipher_ctx (desc->tfm); +be128 buf[3 ]; +struct xts_crypt_req req = { + .tbuf = buf, + .tbuflen =sizeof (buf), + + .tweak_ctx = &ctx->tweak_ctx, + .tweak_fn =XTS_TWEAK_CAST (twofish_enc_blk), + .crypt_ctx = &ctx->crypt_ctx, + .crypt_fn = encrypt_callback, + }; + +return xts_crypt (desc, dst, src, nbytes, &req); +} + +static int xts_decrypt (struct blkcipher_desc *desc,struct scatterlist *dst, +struct scatterlist *src,unsigned int nbytes) +{ +struct twofish_xts_ctx *ctx =crypto_blkcipher_ctx (desc->tfm); +be128 buf[3 ]; +struct xts_crypt_req req = { + .tbuf = buf, + .tbuflen =sizeof (buf), + + .tweak_ctx = &ctx->tweak_ctx, + .tweak_fn =XTS_TWEAK_CAST (twofish_enc_blk), + .crypt_ctx = &ctx->crypt_ctx, + .crypt_fn = decrypt_callback, + }; + +return xts_crypt (desc, dst, src, nbytes, &req); +} + +static struct crypto_alg blk_xts_alg = { + .cra_name ="xts(twofish)" , + .cra_driver_name ="xts-twofish-3way" , + .cra_priority =300 , + .cra_flags =CRYPTO_ALG_TYPE_BLKCIPHER , + .cra_blocksize =TF_BLOCK_SIZE , + .cra_ctxsize =sizeof (struct twofish_xts_ctx ), + .cra_alignmask =0 , + .cra_type = &crypto_blkcipher_type, + .cra_module =THIS_MODULE , + .cra_list =LIST_HEAD_INIT (blk_xts_alg.cra_list), + .cra_u = { + .blkcipher = { + .min_keysize =TF_MIN_KEY_SIZE *2 , + .max_keysize =TF_MAX_KEY_SIZE *2 , + .ivsize =TF_BLOCK_SIZE , + .setkey = xts_twofish_setkey, + .encrypt = xts_encrypt, + .decrypt = xts_decrypt, + }, + }, +}; + +#endif +int __init init (void ) {int err;
@@ -573,13 +675,23 @@ int __init init(void)if (err) goto blk_lrw_err;#endif +#ifdef HAS_XTS + err =crypto_register_alg (&blk_xts_alg); +if (err) + goto blk_xts_err; +#endif return 0 ; +#ifdef HAS_XTS +crypto_unregister_alg (&blk_xts_alg); +blk_xts_err: +#endif #ifdef HAS_LRW +crypto_unregister_alg (&blk_lrw_alg); blk_lrw_err: -crypto_unregister_alg (&blk_ctr_alg);#endif +crypto_unregister_alg (&blk_ctr_alg); ctr_err:crypto_unregister_alg (&blk_cbc_alg); cbc_err:
@@ -590,6 +702,9 @@ ecb_err: void __exit fini(void) { +#ifdef HAS_XTS +crypto_unregister_alg (&blk_xts_alg); +#endif #ifdef HAS_LRW crypto_unregister_alg (&blk_lrw_alg);#endif
Herbert Xu
maintainerQuoted 10 lines · Jussi Kivilinna
This series adds
lrw_crypt()andxts_crypt()functions for cipher implementations that can benefit from parallel cipher block operations. To make interface flexible, caller is reponsible of allocating buffer large enough to store temporary cipher blocks. This buffer size should be as large as size of parallel blocks processed at once. Cryption callback is called with buffer size at maximum of parallel blocks size and at minimum size of one block.Series adds LRW/XTS support to serpent-sse2 and twofish-x86_64-3way, and therefore depends on those series.
Quoted 6 lines
Patches 1-4: include LRW fixes/cleanups, export gf128mul table and add
lrw_crypt(). Patches 5-7: add LRW support to serpent-sse2, with tcrypt tests and test vectors. Patches 8-10: add LRW support to twofish-x86_64-3way, with tcrypt tests and test vectors. Patches 11-12: include XTS cleanup for blocksize usage and addxts_crypt(). Patches 13-15: add XTS support to serpent-sse2, with tcrypt tests and test vectors. Patches 16-18: add XTS support to twofish-x86_64-3way, with tcrypt tests and test vectors.
All applied except for the serpent patches. Please resend those after you've fixed up the previous serpent patches.
I've also merged your later fixes into these.
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt