2 * Glue Code for AVX assembler version of Twofish Cipher
4 * Copyright (C) 2012 Johannes Goetzfried
5 * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
7 * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <linux/module.h>
27 #include <linux/hardirq.h>
28 #include <linux/types.h>
29 #include <linux/crypto.h>
30 #include <linux/err.h>
31 #include <crypto/algapi.h>
32 #include <crypto/twofish.h>
33 #include <crypto/cryptd.h>
34 #include <crypto/b128ops.h>
35 #include <crypto/ctr.h>
36 #include <crypto/lrw.h>
37 #include <crypto/xts.h>
40 #include <asm/xsave.h>
41 #include <asm/crypto/twofish.h>
42 #include <asm/crypto/ablk_helper.h>
43 #include <asm/crypto/glue_helper.h>
44 #include <crypto/scatterwalk.h>
45 #include <linux/workqueue.h>
46 #include <linux/spinlock.h>
48 #define TWOFISH_PARALLEL_BLOCKS 8
50 /* 8-way parallel cipher functions */
51 asmlinkage
void twofish_ecb_enc_8way(struct twofish_ctx
*ctx
, u8
*dst
,
53 EXPORT_SYMBOL_GPL(twofish_ecb_enc_8way
);
55 asmlinkage
void twofish_ecb_dec_8way(struct twofish_ctx
*ctx
, u8
*dst
,
57 EXPORT_SYMBOL_GPL(twofish_ecb_dec_8way
);
59 asmlinkage
void twofish_cbc_dec_8way(struct twofish_ctx
*ctx
, u8
*dst
,
61 EXPORT_SYMBOL_GPL(twofish_cbc_dec_8way
);
63 asmlinkage
void twofish_ctr_8way(struct twofish_ctx
*ctx
, u8
*dst
,
64 const u8
*src
, le128
*iv
);
65 EXPORT_SYMBOL_GPL(twofish_ctr_8way
);
67 asmlinkage
void twofish_xts_enc_8way(struct twofish_ctx
*ctx
, u8
*dst
,
68 const u8
*src
, le128
*iv
);
69 EXPORT_SYMBOL_GPL(twofish_xts_enc_8way
);
70 asmlinkage
void twofish_xts_dec_8way(struct twofish_ctx
*ctx
, u8
*dst
,
71 const u8
*src
, le128
*iv
);
72 EXPORT_SYMBOL_GPL(twofish_xts_dec_8way
);
74 static inline void twofish_enc_blk_3way(struct twofish_ctx
*ctx
, u8
*dst
,
77 __twofish_enc_blk_3way(ctx
, dst
, src
, false);
80 void twofish_xts_enc(void *ctx
, u128
*dst
, const u128
*src
, le128
*iv
)
82 glue_xts_crypt_128bit_one(ctx
, dst
, src
, iv
,
83 GLUE_FUNC_CAST(twofish_enc_blk
));
85 EXPORT_SYMBOL_GPL(twofish_xts_enc
);
87 void twofish_xts_dec(void *ctx
, u128
*dst
, const u128
*src
, le128
*iv
)
89 glue_xts_crypt_128bit_one(ctx
, dst
, src
, iv
,
90 GLUE_FUNC_CAST(twofish_dec_blk
));
92 EXPORT_SYMBOL_GPL(twofish_xts_dec
);
95 static const struct common_glue_ctx twofish_enc
= {
97 .fpu_blocks_limit
= TWOFISH_PARALLEL_BLOCKS
,
100 .num_blocks
= TWOFISH_PARALLEL_BLOCKS
,
101 .fn_u
= { .ecb
= GLUE_FUNC_CAST(twofish_ecb_enc_8way
) }
104 .fn_u
= { .ecb
= GLUE_FUNC_CAST(twofish_enc_blk_3way
) }
107 .fn_u
= { .ecb
= GLUE_FUNC_CAST(twofish_enc_blk
) }
111 static const struct common_glue_ctx twofish_ctr
= {
113 .fpu_blocks_limit
= TWOFISH_PARALLEL_BLOCKS
,
116 .num_blocks
= TWOFISH_PARALLEL_BLOCKS
,
117 .fn_u
= { .ctr
= GLUE_CTR_FUNC_CAST(twofish_ctr_8way
) }
120 .fn_u
= { .ctr
= GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr_3way
) }
123 .fn_u
= { .ctr
= GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr
) }
127 static const struct common_glue_ctx twofish_enc_xts
= {
129 .fpu_blocks_limit
= TWOFISH_PARALLEL_BLOCKS
,
132 .num_blocks
= TWOFISH_PARALLEL_BLOCKS
,
133 .fn_u
= { .xts
= GLUE_XTS_FUNC_CAST(twofish_xts_enc_8way
) }
136 .fn_u
= { .xts
= GLUE_XTS_FUNC_CAST(twofish_xts_enc
) }
140 static const struct common_glue_ctx twofish_dec
= {
142 .fpu_blocks_limit
= TWOFISH_PARALLEL_BLOCKS
,
145 .num_blocks
= TWOFISH_PARALLEL_BLOCKS
,
146 .fn_u
= { .ecb
= GLUE_FUNC_CAST(twofish_ecb_dec_8way
) }
149 .fn_u
= { .ecb
= GLUE_FUNC_CAST(twofish_dec_blk_3way
) }
152 .fn_u
= { .ecb
= GLUE_FUNC_CAST(twofish_dec_blk
) }
156 static const struct common_glue_ctx twofish_dec_cbc
= {
158 .fpu_blocks_limit
= TWOFISH_PARALLEL_BLOCKS
,
161 .num_blocks
= TWOFISH_PARALLEL_BLOCKS
,
162 .fn_u
= { .cbc
= GLUE_CBC_FUNC_CAST(twofish_cbc_dec_8way
) }
165 .fn_u
= { .cbc
= GLUE_CBC_FUNC_CAST(twofish_dec_blk_cbc_3way
) }
168 .fn_u
= { .cbc
= GLUE_CBC_FUNC_CAST(twofish_dec_blk
) }
172 static const struct common_glue_ctx twofish_dec_xts
= {
174 .fpu_blocks_limit
= TWOFISH_PARALLEL_BLOCKS
,
177 .num_blocks
= TWOFISH_PARALLEL_BLOCKS
,
178 .fn_u
= { .xts
= GLUE_XTS_FUNC_CAST(twofish_xts_dec_8way
) }
181 .fn_u
= { .xts
= GLUE_XTS_FUNC_CAST(twofish_xts_dec
) }
185 static int ecb_encrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
186 struct scatterlist
*src
, unsigned int nbytes
)
188 return glue_ecb_crypt_128bit(&twofish_enc
, desc
, dst
, src
, nbytes
);
191 static int ecb_decrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
192 struct scatterlist
*src
, unsigned int nbytes
)
194 return glue_ecb_crypt_128bit(&twofish_dec
, desc
, dst
, src
, nbytes
);
197 static int cbc_encrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
198 struct scatterlist
*src
, unsigned int nbytes
)
200 return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(twofish_enc_blk
), desc
,
204 static int cbc_decrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
205 struct scatterlist
*src
, unsigned int nbytes
)
207 return glue_cbc_decrypt_128bit(&twofish_dec_cbc
, desc
, dst
, src
,
211 static int ctr_crypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
212 struct scatterlist
*src
, unsigned int nbytes
)
214 return glue_ctr_crypt_128bit(&twofish_ctr
, desc
, dst
, src
, nbytes
);
217 static inline bool twofish_fpu_begin(bool fpu_enabled
, unsigned int nbytes
)
219 return glue_fpu_begin(TF_BLOCK_SIZE
, TWOFISH_PARALLEL_BLOCKS
, NULL
,
220 fpu_enabled
, nbytes
);
223 static inline void twofish_fpu_end(bool fpu_enabled
)
225 glue_fpu_end(fpu_enabled
);
229 struct twofish_ctx
*ctx
;
233 static void encrypt_callback(void *priv
, u8
*srcdst
, unsigned int nbytes
)
235 const unsigned int bsize
= TF_BLOCK_SIZE
;
236 struct crypt_priv
*ctx
= priv
;
239 ctx
->fpu_enabled
= twofish_fpu_begin(ctx
->fpu_enabled
, nbytes
);
241 if (nbytes
== bsize
* TWOFISH_PARALLEL_BLOCKS
) {
242 twofish_ecb_enc_8way(ctx
->ctx
, srcdst
, srcdst
);
246 for (i
= 0; i
< nbytes
/ (bsize
* 3); i
++, srcdst
+= bsize
* 3)
247 twofish_enc_blk_3way(ctx
->ctx
, srcdst
, srcdst
);
251 for (i
= 0; i
< nbytes
/ bsize
; i
++, srcdst
+= bsize
)
252 twofish_enc_blk(ctx
->ctx
, srcdst
, srcdst
);
255 static void decrypt_callback(void *priv
, u8
*srcdst
, unsigned int nbytes
)
257 const unsigned int bsize
= TF_BLOCK_SIZE
;
258 struct crypt_priv
*ctx
= priv
;
261 ctx
->fpu_enabled
= twofish_fpu_begin(ctx
->fpu_enabled
, nbytes
);
263 if (nbytes
== bsize
* TWOFISH_PARALLEL_BLOCKS
) {
264 twofish_ecb_dec_8way(ctx
->ctx
, srcdst
, srcdst
);
268 for (i
= 0; i
< nbytes
/ (bsize
* 3); i
++, srcdst
+= bsize
* 3)
269 twofish_dec_blk_3way(ctx
->ctx
, srcdst
, srcdst
);
273 for (i
= 0; i
< nbytes
/ bsize
; i
++, srcdst
+= bsize
)
274 twofish_dec_blk(ctx
->ctx
, srcdst
, srcdst
);
277 static int lrw_encrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
278 struct scatterlist
*src
, unsigned int nbytes
)
280 struct twofish_lrw_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
281 be128 buf
[TWOFISH_PARALLEL_BLOCKS
];
282 struct crypt_priv crypt_ctx
= {
283 .ctx
= &ctx
->twofish_ctx
,
284 .fpu_enabled
= false,
286 struct lrw_crypt_req req
= {
288 .tbuflen
= sizeof(buf
),
290 .table_ctx
= &ctx
->lrw_table
,
291 .crypt_ctx
= &crypt_ctx
,
292 .crypt_fn
= encrypt_callback
,
296 desc
->flags
&= ~CRYPTO_TFM_REQ_MAY_SLEEP
;
297 ret
= lrw_crypt(desc
, dst
, src
, nbytes
, &req
);
298 twofish_fpu_end(crypt_ctx
.fpu_enabled
);
303 static int lrw_decrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
304 struct scatterlist
*src
, unsigned int nbytes
)
306 struct twofish_lrw_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
307 be128 buf
[TWOFISH_PARALLEL_BLOCKS
];
308 struct crypt_priv crypt_ctx
= {
309 .ctx
= &ctx
->twofish_ctx
,
310 .fpu_enabled
= false,
312 struct lrw_crypt_req req
= {
314 .tbuflen
= sizeof(buf
),
316 .table_ctx
= &ctx
->lrw_table
,
317 .crypt_ctx
= &crypt_ctx
,
318 .crypt_fn
= decrypt_callback
,
322 desc
->flags
&= ~CRYPTO_TFM_REQ_MAY_SLEEP
;
323 ret
= lrw_crypt(desc
, dst
, src
, nbytes
, &req
);
324 twofish_fpu_end(crypt_ctx
.fpu_enabled
);
329 static int xts_encrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
330 struct scatterlist
*src
, unsigned int nbytes
)
332 struct twofish_xts_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
334 return glue_xts_crypt_128bit(&twofish_enc_xts
, desc
, dst
, src
, nbytes
,
335 XTS_TWEAK_CAST(twofish_enc_blk
),
336 &ctx
->tweak_ctx
, &ctx
->crypt_ctx
);
339 static int xts_decrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
340 struct scatterlist
*src
, unsigned int nbytes
)
342 struct twofish_xts_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
344 return glue_xts_crypt_128bit(&twofish_dec_xts
, desc
, dst
, src
, nbytes
,
345 XTS_TWEAK_CAST(twofish_enc_blk
),
346 &ctx
->tweak_ctx
, &ctx
->crypt_ctx
);
349 static struct crypto_alg twofish_algs
[10] = { {
350 .cra_name
= "__ecb-twofish-avx",
351 .cra_driver_name
= "__driver-ecb-twofish-avx",
353 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
354 .cra_blocksize
= TF_BLOCK_SIZE
,
355 .cra_ctxsize
= sizeof(struct twofish_ctx
),
357 .cra_type
= &crypto_blkcipher_type
,
358 .cra_module
= THIS_MODULE
,
361 .min_keysize
= TF_MIN_KEY_SIZE
,
362 .max_keysize
= TF_MAX_KEY_SIZE
,
363 .setkey
= twofish_setkey
,
364 .encrypt
= ecb_encrypt
,
365 .decrypt
= ecb_decrypt
,
369 .cra_name
= "__cbc-twofish-avx",
370 .cra_driver_name
= "__driver-cbc-twofish-avx",
372 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
373 .cra_blocksize
= TF_BLOCK_SIZE
,
374 .cra_ctxsize
= sizeof(struct twofish_ctx
),
376 .cra_type
= &crypto_blkcipher_type
,
377 .cra_module
= THIS_MODULE
,
380 .min_keysize
= TF_MIN_KEY_SIZE
,
381 .max_keysize
= TF_MAX_KEY_SIZE
,
382 .setkey
= twofish_setkey
,
383 .encrypt
= cbc_encrypt
,
384 .decrypt
= cbc_decrypt
,
388 .cra_name
= "__ctr-twofish-avx",
389 .cra_driver_name
= "__driver-ctr-twofish-avx",
391 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
393 .cra_ctxsize
= sizeof(struct twofish_ctx
),
395 .cra_type
= &crypto_blkcipher_type
,
396 .cra_module
= THIS_MODULE
,
399 .min_keysize
= TF_MIN_KEY_SIZE
,
400 .max_keysize
= TF_MAX_KEY_SIZE
,
401 .ivsize
= TF_BLOCK_SIZE
,
402 .setkey
= twofish_setkey
,
403 .encrypt
= ctr_crypt
,
404 .decrypt
= ctr_crypt
,
408 .cra_name
= "__lrw-twofish-avx",
409 .cra_driver_name
= "__driver-lrw-twofish-avx",
411 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
412 .cra_blocksize
= TF_BLOCK_SIZE
,
413 .cra_ctxsize
= sizeof(struct twofish_lrw_ctx
),
415 .cra_type
= &crypto_blkcipher_type
,
416 .cra_module
= THIS_MODULE
,
417 .cra_exit
= lrw_twofish_exit_tfm
,
420 .min_keysize
= TF_MIN_KEY_SIZE
+
422 .max_keysize
= TF_MAX_KEY_SIZE
+
424 .ivsize
= TF_BLOCK_SIZE
,
425 .setkey
= lrw_twofish_setkey
,
426 .encrypt
= lrw_encrypt
,
427 .decrypt
= lrw_decrypt
,
431 .cra_name
= "__xts-twofish-avx",
432 .cra_driver_name
= "__driver-xts-twofish-avx",
434 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
435 .cra_blocksize
= TF_BLOCK_SIZE
,
436 .cra_ctxsize
= sizeof(struct twofish_xts_ctx
),
438 .cra_type
= &crypto_blkcipher_type
,
439 .cra_module
= THIS_MODULE
,
442 .min_keysize
= TF_MIN_KEY_SIZE
* 2,
443 .max_keysize
= TF_MAX_KEY_SIZE
* 2,
444 .ivsize
= TF_BLOCK_SIZE
,
445 .setkey
= xts_twofish_setkey
,
446 .encrypt
= xts_encrypt
,
447 .decrypt
= xts_decrypt
,
451 .cra_name
= "ecb(twofish)",
452 .cra_driver_name
= "ecb-twofish-avx",
454 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
455 .cra_blocksize
= TF_BLOCK_SIZE
,
456 .cra_ctxsize
= sizeof(struct async_helper_ctx
),
458 .cra_type
= &crypto_ablkcipher_type
,
459 .cra_module
= THIS_MODULE
,
460 .cra_init
= ablk_init
,
461 .cra_exit
= ablk_exit
,
464 .min_keysize
= TF_MIN_KEY_SIZE
,
465 .max_keysize
= TF_MAX_KEY_SIZE
,
466 .setkey
= ablk_set_key
,
467 .encrypt
= ablk_encrypt
,
468 .decrypt
= ablk_decrypt
,
472 .cra_name
= "cbc(twofish)",
473 .cra_driver_name
= "cbc-twofish-avx",
475 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
476 .cra_blocksize
= TF_BLOCK_SIZE
,
477 .cra_ctxsize
= sizeof(struct async_helper_ctx
),
479 .cra_type
= &crypto_ablkcipher_type
,
480 .cra_module
= THIS_MODULE
,
481 .cra_init
= ablk_init
,
482 .cra_exit
= ablk_exit
,
485 .min_keysize
= TF_MIN_KEY_SIZE
,
486 .max_keysize
= TF_MAX_KEY_SIZE
,
487 .ivsize
= TF_BLOCK_SIZE
,
488 .setkey
= ablk_set_key
,
489 .encrypt
= __ablk_encrypt
,
490 .decrypt
= ablk_decrypt
,
494 .cra_name
= "ctr(twofish)",
495 .cra_driver_name
= "ctr-twofish-avx",
497 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
499 .cra_ctxsize
= sizeof(struct async_helper_ctx
),
501 .cra_type
= &crypto_ablkcipher_type
,
502 .cra_module
= THIS_MODULE
,
503 .cra_init
= ablk_init
,
504 .cra_exit
= ablk_exit
,
507 .min_keysize
= TF_MIN_KEY_SIZE
,
508 .max_keysize
= TF_MAX_KEY_SIZE
,
509 .ivsize
= TF_BLOCK_SIZE
,
510 .setkey
= ablk_set_key
,
511 .encrypt
= ablk_encrypt
,
512 .decrypt
= ablk_encrypt
,
517 .cra_name
= "lrw(twofish)",
518 .cra_driver_name
= "lrw-twofish-avx",
520 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
521 .cra_blocksize
= TF_BLOCK_SIZE
,
522 .cra_ctxsize
= sizeof(struct async_helper_ctx
),
524 .cra_type
= &crypto_ablkcipher_type
,
525 .cra_module
= THIS_MODULE
,
526 .cra_init
= ablk_init
,
527 .cra_exit
= ablk_exit
,
530 .min_keysize
= TF_MIN_KEY_SIZE
+
532 .max_keysize
= TF_MAX_KEY_SIZE
+
534 .ivsize
= TF_BLOCK_SIZE
,
535 .setkey
= ablk_set_key
,
536 .encrypt
= ablk_encrypt
,
537 .decrypt
= ablk_decrypt
,
541 .cra_name
= "xts(twofish)",
542 .cra_driver_name
= "xts-twofish-avx",
544 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
545 .cra_blocksize
= TF_BLOCK_SIZE
,
546 .cra_ctxsize
= sizeof(struct async_helper_ctx
),
548 .cra_type
= &crypto_ablkcipher_type
,
549 .cra_module
= THIS_MODULE
,
550 .cra_init
= ablk_init
,
551 .cra_exit
= ablk_exit
,
554 .min_keysize
= TF_MIN_KEY_SIZE
* 2,
555 .max_keysize
= TF_MAX_KEY_SIZE
* 2,
556 .ivsize
= TF_BLOCK_SIZE
,
557 .setkey
= ablk_set_key
,
558 .encrypt
= ablk_encrypt
,
559 .decrypt
= ablk_decrypt
,
564 static int __init
twofish_init(void)
568 if (!cpu_has_avx
|| !cpu_has_osxsave
) {
569 printk(KERN_INFO
"AVX instructions are not detected.\n");
573 xcr0
= xgetbv(XCR_XFEATURE_ENABLED_MASK
);
574 if ((xcr0
& (XSTATE_SSE
| XSTATE_YMM
)) != (XSTATE_SSE
| XSTATE_YMM
)) {
575 printk(KERN_INFO
"AVX detected but unusable.\n");
579 return crypto_register_algs(twofish_algs
, ARRAY_SIZE(twofish_algs
));
582 static void __exit
twofish_exit(void)
584 crypto_unregister_algs(twofish_algs
, ARRAY_SIZE(twofish_algs
));
587 module_init(twofish_init
);
588 module_exit(twofish_exit
);
590 MODULE_DESCRIPTION("Twofish Cipher Algorithm, AVX optimized");
591 MODULE_LICENSE("GPL");
592 MODULE_ALIAS("twofish");