24 #include <linux/module.h>
26 #include <linux/types.h>
30 #include <crypto/twofish.h>
46 #define TWOFISH_PARALLEL_BLOCKS 8
86 twofish_dec_blk_xway(ctx, (
u8 *)dst, (
u8 *)src);
88 for (j = 0; j < TWOFISH_PARALLEL_BLOCKS - 1; j++)
89 u128_xor(dst + (j + 1), dst + (j + 1), ivs + j);
92 static void twofish_enc_blk_ctr_xway(
void *ctx,
u128 *dst,
const u128 *src,
102 u128_to_be128(&ctrblks[i], iv);
106 twofish_enc_blk_xway_xor(ctx, (
u8 *)dst, (
u8 *)ctrblks);
205 static inline bool twofish_fpu_begin(
bool fpu_enabled,
unsigned int nbytes)
208 fpu_enabled, nbytes);
211 static inline void twofish_fpu_end(
bool fpu_enabled)
213 glue_fpu_end(fpu_enabled);
221 static void encrypt_callback(
void *
priv,
u8 *srcdst,
unsigned int nbytes)
229 if (nbytes == bsize * TWOFISH_PARALLEL_BLOCKS) {
230 twofish_enc_blk_xway(ctx->
ctx, srcdst, srcdst);
234 for (i = 0; i < nbytes / (bsize * 3); i++, srcdst += bsize * 3)
235 twofish_enc_blk_3way(ctx->
ctx, srcdst, srcdst);
239 for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
243 static void decrypt_callback(
void *priv,
u8 *srcdst,
unsigned int nbytes)
251 if (nbytes == bsize * TWOFISH_PARALLEL_BLOCKS) {
252 twofish_dec_blk_xway(ctx->
ctx, srcdst, srcdst);
256 for (i = 0; i < nbytes / (bsize * 3); i++, srcdst += bsize * 3)
261 for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
272 .fpu_enabled =
false,
276 .tbuflen =
sizeof(
buf),
279 .crypt_ctx = &crypt_ctx,
280 .crypt_fn = encrypt_callback,
285 ret =
lrw_crypt(desc, dst, src, nbytes, &req);
298 .fpu_enabled =
false,
302 .tbuflen =
sizeof(
buf),
305 .crypt_ctx = &crypt_ctx,
306 .crypt_fn = decrypt_callback,
311 ret =
lrw_crypt(desc, dst, src, nbytes, &req);
324 .fpu_enabled =
false,
328 .tbuflen =
sizeof(
buf),
333 .crypt_fn = encrypt_callback,
338 ret =
xts_crypt(desc, dst, src, nbytes, &req);
351 .fpu_enabled =
false,
355 .tbuflen =
sizeof(
buf),
360 .crypt_fn = decrypt_callback,
365 ret =
xts_crypt(desc, dst, src, nbytes, &req);
371 static struct crypto_alg twofish_algs[10] = { {
372 .cra_name =
"__ecb-twofish-avx",
373 .cra_driver_name =
"__driver-ecb-twofish-avx",
386 .encrypt = ecb_encrypt,
387 .decrypt = ecb_decrypt,
391 .cra_name =
"__cbc-twofish-avx",
392 .cra_driver_name =
"__driver-cbc-twofish-avx",
405 .encrypt = cbc_encrypt,
406 .decrypt = cbc_decrypt,
410 .cra_name =
"__ctr-twofish-avx",
411 .cra_driver_name =
"__driver-ctr-twofish-avx",
425 .encrypt = ctr_crypt,
426 .decrypt = ctr_crypt,
430 .cra_name =
"__lrw-twofish-avx",
431 .cra_driver_name =
"__driver-lrw-twofish-avx",
448 .encrypt = lrw_encrypt,
449 .decrypt = lrw_decrypt,
453 .cra_name =
"__xts-twofish-avx",
454 .cra_driver_name =
"__driver-xts-twofish-avx",
468 .encrypt = xts_encrypt,
469 .decrypt = xts_decrypt,
473 .cra_name =
"ecb(twofish)",
474 .cra_driver_name =
"ecb-twofish-avx",
494 .cra_name =
"cbc(twofish)",
495 .cra_driver_name =
"cbc-twofish-avx",
516 .cra_name =
"ctr(twofish)",
517 .cra_driver_name =
"ctr-twofish-avx",
539 .cra_name =
"lrw(twofish)",
540 .cra_driver_name =
"lrw-twofish-avx",
563 .cra_name =
"xts(twofish)",
564 .cra_driver_name =
"xts-twofish-avx",
586 static int __init twofish_init(
void)
590 if (!cpu_has_avx || !cpu_has_osxsave) {
604 static void __exit twofish_exit(
void)