2 * Glue Code for 3-way parallel assembler optimized version of Twofish
4 * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
6 * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
7 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
8 * CTR part based on code (crypto/ctr.c) by:
9 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <linux/crypto.h>
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/types.h>
32 #include <crypto/algapi.h>
33 #include <crypto/twofish.h>
34 #include <crypto/b128ops.h>
35 #include <crypto/lrw.h>
36 #include <crypto/xts.h>
38 #if defined(CONFIG_CRYPTO_LRW) || defined(CONFIG_CRYPTO_LRW_MODULE)
42 #if defined(CONFIG_CRYPTO_XTS) || defined(CONFIG_CRYPTO_XTS_MODULE)
46 /* regular block cipher functions from twofish_x86_64 module */
47 asmlinkage void twofish_enc_blk(struct twofish_ctx *ctx, u8 *dst,
49 asmlinkage void twofish_dec_blk(struct twofish_ctx *ctx, u8 *dst,
52 /* 3-way parallel cipher functions */
53 asmlinkage void __twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
54 const u8 *src, bool xor);
55 asmlinkage void twofish_dec_blk_3way(struct twofish_ctx *ctx, u8 *dst,
58 static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
61 __twofish_enc_blk_3way(ctx, dst, src, false);
64 static inline void twofish_enc_blk_xor_3way(struct twofish_ctx *ctx, u8 *dst,
67 __twofish_enc_blk_3way(ctx, dst, src, true);
70 static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
71 void (*fn)(struct twofish_ctx *, u8 *, const u8 *),
72 void (*fn_3way)(struct twofish_ctx *, u8 *, const u8 *))
74 struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
75 unsigned int bsize = TF_BLOCK_SIZE;
79 err = blkcipher_walk_virt(desc, walk);
81 while ((nbytes = walk->nbytes)) {
82 u8 *wsrc = walk->src.virt.addr;
83 u8 *wdst = walk->dst.virt.addr;
85 /* Process three block batch */
86 if (nbytes >= bsize * 3) {
88 fn_3way(ctx, wdst, wsrc);
93 } while (nbytes >= bsize * 3);
99 /* Handle leftovers */
106 } while (nbytes >= bsize);
109 err = blkcipher_walk_done(desc, walk, nbytes);
115 static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
116 struct scatterlist *src, unsigned int nbytes)
118 struct blkcipher_walk walk;
120 blkcipher_walk_init(&walk, dst, src, nbytes);
121 return ecb_crypt(desc, &walk, twofish_enc_blk, twofish_enc_blk_3way);
124 static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
125 struct scatterlist *src, unsigned int nbytes)
127 struct blkcipher_walk walk;
129 blkcipher_walk_init(&walk, dst, src, nbytes);
130 return ecb_crypt(desc, &walk, twofish_dec_blk, twofish_dec_blk_3way);
133 static struct crypto_alg blk_ecb_alg = {
134 .cra_name = "ecb(twofish)",
135 .cra_driver_name = "ecb-twofish-3way",
137 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
138 .cra_blocksize = TF_BLOCK_SIZE,
139 .cra_ctxsize = sizeof(struct twofish_ctx),
141 .cra_type = &crypto_blkcipher_type,
142 .cra_module = THIS_MODULE,
143 .cra_list = LIST_HEAD_INIT(blk_ecb_alg.cra_list),
146 .min_keysize = TF_MIN_KEY_SIZE,
147 .max_keysize = TF_MAX_KEY_SIZE,
148 .setkey = twofish_setkey,
149 .encrypt = ecb_encrypt,
150 .decrypt = ecb_decrypt,
155 static unsigned int __cbc_encrypt(struct blkcipher_desc *desc,
156 struct blkcipher_walk *walk)
158 struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
159 unsigned int bsize = TF_BLOCK_SIZE;
160 unsigned int nbytes = walk->nbytes;
161 u128 *src = (u128 *)walk->src.virt.addr;
162 u128 *dst = (u128 *)walk->dst.virt.addr;
163 u128 *iv = (u128 *)walk->iv;
166 u128_xor(dst, src, iv);
167 twofish_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
173 } while (nbytes >= bsize);
175 u128_xor((u128 *)walk->iv, (u128 *)walk->iv, iv);
179 static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
180 struct scatterlist *src, unsigned int nbytes)
182 struct blkcipher_walk walk;
185 blkcipher_walk_init(&walk, dst, src, nbytes);
186 err = blkcipher_walk_virt(desc, &walk);
188 while ((nbytes = walk.nbytes)) {
189 nbytes = __cbc_encrypt(desc, &walk);
190 err = blkcipher_walk_done(desc, &walk, nbytes);
196 static unsigned int __cbc_decrypt(struct blkcipher_desc *desc,
197 struct blkcipher_walk *walk)
199 struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
200 unsigned int bsize = TF_BLOCK_SIZE;
201 unsigned int nbytes = walk->nbytes;
202 u128 *src = (u128 *)walk->src.virt.addr;
203 u128 *dst = (u128 *)walk->dst.virt.addr;
207 /* Start of the last block. */
208 src += nbytes / bsize - 1;
209 dst += nbytes / bsize - 1;
213 /* Process three block batch */
214 if (nbytes >= bsize * 3) {
216 nbytes -= bsize * (3 - 1);
223 twofish_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
225 u128_xor(dst + 1, dst + 1, ivs + 0);
226 u128_xor(dst + 2, dst + 2, ivs + 1);
232 u128_xor(dst, dst, src - 1);
235 } while (nbytes >= bsize * 3);
241 /* Handle leftovers */
243 twofish_dec_blk(ctx, (u8 *)dst, (u8 *)src);
249 u128_xor(dst, dst, src - 1);
255 u128_xor(dst, dst, (u128 *)walk->iv);
256 *(u128 *)walk->iv = last_iv;
261 static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
262 struct scatterlist *src, unsigned int nbytes)
264 struct blkcipher_walk walk;
267 blkcipher_walk_init(&walk, dst, src, nbytes);
268 err = blkcipher_walk_virt(desc, &walk);
270 while ((nbytes = walk.nbytes)) {
271 nbytes = __cbc_decrypt(desc, &walk);
272 err = blkcipher_walk_done(desc, &walk, nbytes);
278 static struct crypto_alg blk_cbc_alg = {
279 .cra_name = "cbc(twofish)",
280 .cra_driver_name = "cbc-twofish-3way",
282 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
283 .cra_blocksize = TF_BLOCK_SIZE,
284 .cra_ctxsize = sizeof(struct twofish_ctx),
286 .cra_type = &crypto_blkcipher_type,
287 .cra_module = THIS_MODULE,
288 .cra_list = LIST_HEAD_INIT(blk_cbc_alg.cra_list),
291 .min_keysize = TF_MIN_KEY_SIZE,
292 .max_keysize = TF_MAX_KEY_SIZE,
293 .ivsize = TF_BLOCK_SIZE,
294 .setkey = twofish_setkey,
295 .encrypt = cbc_encrypt,
296 .decrypt = cbc_decrypt,
301 static inline void u128_to_be128(be128 *dst, const u128 *src)
303 dst->a = cpu_to_be64(src->a);
304 dst->b = cpu_to_be64(src->b);
307 static inline void be128_to_u128(u128 *dst, const be128 *src)
309 dst->a = be64_to_cpu(src->a);
310 dst->b = be64_to_cpu(src->b);
313 static inline void u128_inc(u128 *i)
320 static void ctr_crypt_final(struct blkcipher_desc *desc,
321 struct blkcipher_walk *walk)
323 struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
324 u8 *ctrblk = walk->iv;
325 u8 keystream[TF_BLOCK_SIZE];
326 u8 *src = walk->src.virt.addr;
327 u8 *dst = walk->dst.virt.addr;
328 unsigned int nbytes = walk->nbytes;
330 twofish_enc_blk(ctx, keystream, ctrblk);
331 crypto_xor(keystream, src, nbytes);
332 memcpy(dst, keystream, nbytes);
334 crypto_inc(ctrblk, TF_BLOCK_SIZE);
337 static unsigned int __ctr_crypt(struct blkcipher_desc *desc,
338 struct blkcipher_walk *walk)
340 struct twofish_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
341 unsigned int bsize = TF_BLOCK_SIZE;
342 unsigned int nbytes = walk->nbytes;
343 u128 *src = (u128 *)walk->src.virt.addr;
344 u128 *dst = (u128 *)walk->dst.virt.addr;
348 be128_to_u128(&ctrblk, (be128 *)walk->iv);
350 /* Process three block batch */
351 if (nbytes >= bsize * 3) {
359 /* create ctrblks for parallel encrypt */
360 u128_to_be128(&ctrblocks[0], &ctrblk);
362 u128_to_be128(&ctrblocks[1], &ctrblk);
364 u128_to_be128(&ctrblocks[2], &ctrblk);
367 twofish_enc_blk_xor_3way(ctx, (u8 *)dst,
373 } while (nbytes >= bsize * 3);
379 /* Handle leftovers */
384 u128_to_be128(&ctrblocks[0], &ctrblk);
387 twofish_enc_blk(ctx, (u8 *)ctrblocks, (u8 *)ctrblocks);
388 u128_xor(dst, dst, (u128 *)ctrblocks);
393 } while (nbytes >= bsize);
396 u128_to_be128((be128 *)walk->iv, &ctrblk);
400 static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
401 struct scatterlist *src, unsigned int nbytes)
403 struct blkcipher_walk walk;
406 blkcipher_walk_init(&walk, dst, src, nbytes);
407 err = blkcipher_walk_virt_block(desc, &walk, TF_BLOCK_SIZE);
409 while ((nbytes = walk.nbytes) >= TF_BLOCK_SIZE) {
410 nbytes = __ctr_crypt(desc, &walk);
411 err = blkcipher_walk_done(desc, &walk, nbytes);
415 ctr_crypt_final(desc, &walk);
416 err = blkcipher_walk_done(desc, &walk, 0);
422 static struct crypto_alg blk_ctr_alg = {
423 .cra_name = "ctr(twofish)",
424 .cra_driver_name = "ctr-twofish-3way",
426 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
428 .cra_ctxsize = sizeof(struct twofish_ctx),
430 .cra_type = &crypto_blkcipher_type,
431 .cra_module = THIS_MODULE,
432 .cra_list = LIST_HEAD_INIT(blk_ctr_alg.cra_list),
435 .min_keysize = TF_MIN_KEY_SIZE,
436 .max_keysize = TF_MAX_KEY_SIZE,
437 .ivsize = TF_BLOCK_SIZE,
438 .setkey = twofish_setkey,
439 .encrypt = ctr_crypt,
440 .decrypt = ctr_crypt,
445 #if defined(HAS_LRW) || defined(HAS_XTS)
447 static void encrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
449 const unsigned int bsize = TF_BLOCK_SIZE;
450 struct twofish_ctx *ctx = priv;
453 if (nbytes == 3 * bsize) {
454 twofish_enc_blk_3way(ctx, srcdst, srcdst);
458 for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
459 twofish_enc_blk(ctx, srcdst, srcdst);
462 static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
464 const unsigned int bsize = TF_BLOCK_SIZE;
465 struct twofish_ctx *ctx = priv;
468 if (nbytes == 3 * bsize) {
469 twofish_dec_blk_3way(ctx, srcdst, srcdst);
473 for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
474 twofish_dec_blk(ctx, srcdst, srcdst);
481 struct twofish_lrw_ctx {
482 struct lrw_table_ctx lrw_table;
483 struct twofish_ctx twofish_ctx;
486 static int lrw_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
489 struct twofish_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
492 err = __twofish_setkey(&ctx->twofish_ctx, key, keylen - TF_BLOCK_SIZE,
497 return lrw_init_table(&ctx->lrw_table, key + keylen - TF_BLOCK_SIZE);
500 static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
501 struct scatterlist *src, unsigned int nbytes)
503 struct twofish_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
505 struct lrw_crypt_req req = {
507 .tbuflen = sizeof(buf),
509 .table_ctx = &ctx->lrw_table,
510 .crypt_ctx = &ctx->twofish_ctx,
511 .crypt_fn = encrypt_callback,
514 return lrw_crypt(desc, dst, src, nbytes, &req);
517 static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
518 struct scatterlist *src, unsigned int nbytes)
520 struct twofish_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
522 struct lrw_crypt_req req = {
524 .tbuflen = sizeof(buf),
526 .table_ctx = &ctx->lrw_table,
527 .crypt_ctx = &ctx->twofish_ctx,
528 .crypt_fn = decrypt_callback,
531 return lrw_crypt(desc, dst, src, nbytes, &req);
534 static void lrw_exit_tfm(struct crypto_tfm *tfm)
536 struct twofish_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
538 lrw_free_table(&ctx->lrw_table);
541 static struct crypto_alg blk_lrw_alg = {
542 .cra_name = "lrw(twofish)",
543 .cra_driver_name = "lrw-twofish-3way",
545 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
546 .cra_blocksize = TF_BLOCK_SIZE,
547 .cra_ctxsize = sizeof(struct twofish_lrw_ctx),
549 .cra_type = &crypto_blkcipher_type,
550 .cra_module = THIS_MODULE,
551 .cra_list = LIST_HEAD_INIT(blk_lrw_alg.cra_list),
552 .cra_exit = lrw_exit_tfm,
555 .min_keysize = TF_MIN_KEY_SIZE + TF_BLOCK_SIZE,
556 .max_keysize = TF_MAX_KEY_SIZE + TF_BLOCK_SIZE,
557 .ivsize = TF_BLOCK_SIZE,
558 .setkey = lrw_twofish_setkey,
559 .encrypt = lrw_encrypt,
560 .decrypt = lrw_decrypt,
569 struct twofish_xts_ctx {
570 struct twofish_ctx tweak_ctx;
571 struct twofish_ctx crypt_ctx;
574 static int xts_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
577 struct twofish_xts_ctx *ctx = crypto_tfm_ctx(tfm);
578 u32 *flags = &tfm->crt_flags;
581 /* key consists of keys of equal size concatenated, therefore
582 * the length must be even
585 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
589 /* first half of xts-key is for crypt */
590 err = __twofish_setkey(&ctx->crypt_ctx, key, keylen / 2, flags);
594 /* second half of xts-key is for tweak */
595 return __twofish_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2,
599 static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
600 struct scatterlist *src, unsigned int nbytes)
602 struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
604 struct xts_crypt_req req = {
606 .tbuflen = sizeof(buf),
608 .tweak_ctx = &ctx->tweak_ctx,
609 .tweak_fn = XTS_TWEAK_CAST(twofish_enc_blk),
610 .crypt_ctx = &ctx->crypt_ctx,
611 .crypt_fn = encrypt_callback,
614 return xts_crypt(desc, dst, src, nbytes, &req);
617 static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
618 struct scatterlist *src, unsigned int nbytes)
620 struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
622 struct xts_crypt_req req = {
624 .tbuflen = sizeof(buf),
626 .tweak_ctx = &ctx->tweak_ctx,
627 .tweak_fn = XTS_TWEAK_CAST(twofish_enc_blk),
628 .crypt_ctx = &ctx->crypt_ctx,
629 .crypt_fn = decrypt_callback,
632 return xts_crypt(desc, dst, src, nbytes, &req);
635 static struct crypto_alg blk_xts_alg = {
636 .cra_name = "xts(twofish)",
637 .cra_driver_name = "xts-twofish-3way",
639 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
640 .cra_blocksize = TF_BLOCK_SIZE,
641 .cra_ctxsize = sizeof(struct twofish_xts_ctx),
643 .cra_type = &crypto_blkcipher_type,
644 .cra_module = THIS_MODULE,
645 .cra_list = LIST_HEAD_INIT(blk_xts_alg.cra_list),
648 .min_keysize = TF_MIN_KEY_SIZE * 2,
649 .max_keysize = TF_MAX_KEY_SIZE * 2,
650 .ivsize = TF_BLOCK_SIZE,
651 .setkey = xts_twofish_setkey,
652 .encrypt = xts_encrypt,
653 .decrypt = xts_decrypt,
660 int __init init(void)
664 err = crypto_register_alg(&blk_ecb_alg);
667 err = crypto_register_alg(&blk_cbc_alg);
670 err = crypto_register_alg(&blk_ctr_alg);
674 err = crypto_register_alg(&blk_lrw_alg);
679 err = crypto_register_alg(&blk_xts_alg);
687 crypto_unregister_alg(&blk_xts_alg);
691 crypto_unregister_alg(&blk_lrw_alg);
694 crypto_unregister_alg(&blk_ctr_alg);
696 crypto_unregister_alg(&blk_cbc_alg);
698 crypto_unregister_alg(&blk_ecb_alg);
703 void __exit fini(void)
706 crypto_unregister_alg(&blk_xts_alg);
709 crypto_unregister_alg(&blk_lrw_alg);
711 crypto_unregister_alg(&blk_ctr_alg);
712 crypto_unregister_alg(&blk_cbc_alg);
713 crypto_unregister_alg(&blk_ecb_alg);
719 MODULE_LICENSE("GPL");
720 MODULE_DESCRIPTION("Twofish Cipher Algorithm, 3-way parallel asm optimized");
721 MODULE_ALIAS("twofish");
722 MODULE_ALIAS("twofish-asm");