twofish_avx2_glue.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * Glue Code for x86_64/AVX2 assembler optimized version of Twofish
  3. *
  4. * Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/crypto.h>
  15. #include <linux/err.h>
  16. #include <crypto/algapi.h>
  17. #include <crypto/ctr.h>
  18. #include <crypto/twofish.h>
  19. #include <crypto/lrw.h>
  20. #include <crypto/xts.h>
  21. #include <asm/xcr.h>
  22. #include <asm/xsave.h>
  23. #include <asm/crypto/twofish.h>
  24. #include <asm/crypto/ablk_helper.h>
  25. #include <asm/crypto/glue_helper.h>
  26. #include <crypto/scatterwalk.h>
  27. #define TF_AVX2_PARALLEL_BLOCKS 16
  28. /* 16-way AVX2 parallel cipher functions */
  29. asmlinkage void twofish_ecb_enc_16way(struct twofish_ctx *ctx, u8 *dst,
  30. const u8 *src);
  31. asmlinkage void twofish_ecb_dec_16way(struct twofish_ctx *ctx, u8 *dst,
  32. const u8 *src);
  33. asmlinkage void twofish_cbc_dec_16way(void *ctx, u128 *dst, const u128 *src);
  34. asmlinkage void twofish_ctr_16way(void *ctx, u128 *dst, const u128 *src,
  35. le128 *iv);
  36. asmlinkage void twofish_xts_enc_16way(struct twofish_ctx *ctx, u8 *dst,
  37. const u8 *src, le128 *iv);
  38. asmlinkage void twofish_xts_dec_16way(struct twofish_ctx *ctx, u8 *dst,
  39. const u8 *src, le128 *iv);
  40. static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
  41. const u8 *src)
  42. {
  43. __twofish_enc_blk_3way(ctx, dst, src, false);
  44. }
  45. static const struct common_glue_ctx twofish_enc = {
  46. .num_funcs = 4,
  47. .fpu_blocks_limit = 8,
  48. .funcs = { {
  49. .num_blocks = 16,
  50. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_ecb_enc_16way) }
  51. }, {
  52. .num_blocks = 8,
  53. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_ecb_enc_8way) }
  54. }, {
  55. .num_blocks = 3,
  56. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_3way) }
  57. }, {
  58. .num_blocks = 1,
  59. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk) }
  60. } }
  61. };
  62. static const struct common_glue_ctx twofish_ctr = {
  63. .num_funcs = 4,
  64. .fpu_blocks_limit = 8,
  65. .funcs = { {
  66. .num_blocks = 16,
  67. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_ctr_16way) }
  68. }, {
  69. .num_blocks = 8,
  70. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_ctr_8way) }
  71. }, {
  72. .num_blocks = 3,
  73. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr_3way) }
  74. }, {
  75. .num_blocks = 1,
  76. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr) }
  77. } }
  78. };
  79. static const struct common_glue_ctx twofish_enc_xts = {
  80. .num_funcs = 3,
  81. .fpu_blocks_limit = 8,
  82. .funcs = { {
  83. .num_blocks = 16,
  84. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_enc_16way) }
  85. }, {
  86. .num_blocks = 8,
  87. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_enc_8way) }
  88. }, {
  89. .num_blocks = 1,
  90. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_enc) }
  91. } }
  92. };
  93. static const struct common_glue_ctx twofish_dec = {
  94. .num_funcs = 4,
  95. .fpu_blocks_limit = 8,
  96. .funcs = { {
  97. .num_blocks = 16,
  98. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_ecb_dec_16way) }
  99. }, {
  100. .num_blocks = 8,
  101. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_ecb_dec_8way) }
  102. }, {
  103. .num_blocks = 3,
  104. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk_3way) }
  105. }, {
  106. .num_blocks = 1,
  107. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk) }
  108. } }
  109. };
  110. static const struct common_glue_ctx twofish_dec_cbc = {
  111. .num_funcs = 4,
  112. .fpu_blocks_limit = 8,
  113. .funcs = { {
  114. .num_blocks = 16,
  115. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_cbc_dec_16way) }
  116. }, {
  117. .num_blocks = 8,
  118. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_cbc_dec_8way) }
  119. }, {
  120. .num_blocks = 3,
  121. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk_cbc_3way) }
  122. }, {
  123. .num_blocks = 1,
  124. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk) }
  125. } }
  126. };
  127. static const struct common_glue_ctx twofish_dec_xts = {
  128. .num_funcs = 3,
  129. .fpu_blocks_limit = 8,
  130. .funcs = { {
  131. .num_blocks = 16,
  132. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_dec_16way) }
  133. }, {
  134. .num_blocks = 8,
  135. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_dec_8way) }
  136. }, {
  137. .num_blocks = 1,
  138. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_dec) }
  139. } }
  140. };
  141. static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  142. struct scatterlist *src, unsigned int nbytes)
  143. {
  144. return glue_ecb_crypt_128bit(&twofish_enc, desc, dst, src, nbytes);
  145. }
  146. static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  147. struct scatterlist *src, unsigned int nbytes)
  148. {
  149. return glue_ecb_crypt_128bit(&twofish_dec, desc, dst, src, nbytes);
  150. }
  151. static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  152. struct scatterlist *src, unsigned int nbytes)
  153. {
  154. return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(twofish_enc_blk), desc,
  155. dst, src, nbytes);
  156. }
  157. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  158. struct scatterlist *src, unsigned int nbytes)
  159. {
  160. return glue_cbc_decrypt_128bit(&twofish_dec_cbc, desc, dst, src,
  161. nbytes);
  162. }
  163. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  164. struct scatterlist *src, unsigned int nbytes)
  165. {
  166. return glue_ctr_crypt_128bit(&twofish_ctr, desc, dst, src, nbytes);
  167. }
  168. static inline bool twofish_fpu_begin(bool fpu_enabled, unsigned int nbytes)
  169. {
  170. /* since reusing AVX functions, starts using FPU at 8 parallel blocks */
  171. return glue_fpu_begin(TF_BLOCK_SIZE, 8, NULL, fpu_enabled, nbytes);
  172. }
  173. static inline void twofish_fpu_end(bool fpu_enabled)
  174. {
  175. glue_fpu_end(fpu_enabled);
  176. }
  177. struct crypt_priv {
  178. struct twofish_ctx *ctx;
  179. bool fpu_enabled;
  180. };
  181. static void encrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
  182. {
  183. const unsigned int bsize = TF_BLOCK_SIZE;
  184. struct crypt_priv *ctx = priv;
  185. int i;
  186. ctx->fpu_enabled = twofish_fpu_begin(ctx->fpu_enabled, nbytes);
  187. while (nbytes >= TF_AVX2_PARALLEL_BLOCKS * bsize) {
  188. twofish_ecb_enc_16way(ctx->ctx, srcdst, srcdst);
  189. srcdst += bsize * TF_AVX2_PARALLEL_BLOCKS;
  190. nbytes -= bsize * TF_AVX2_PARALLEL_BLOCKS;
  191. }
  192. while (nbytes >= 8 * bsize) {
  193. twofish_ecb_enc_8way(ctx->ctx, srcdst, srcdst);
  194. srcdst += bsize * 8;
  195. nbytes -= bsize * 8;
  196. }
  197. while (nbytes >= 3 * bsize) {
  198. twofish_enc_blk_3way(ctx->ctx, srcdst, srcdst);
  199. srcdst += bsize * 3;
  200. nbytes -= bsize * 3;
  201. }
  202. for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
  203. twofish_enc_blk(ctx->ctx, srcdst, srcdst);
  204. }
  205. static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
  206. {
  207. const unsigned int bsize = TF_BLOCK_SIZE;
  208. struct crypt_priv *ctx = priv;
  209. int i;
  210. ctx->fpu_enabled = twofish_fpu_begin(ctx->fpu_enabled, nbytes);
  211. while (nbytes >= TF_AVX2_PARALLEL_BLOCKS * bsize) {
  212. twofish_ecb_dec_16way(ctx->ctx, srcdst, srcdst);
  213. srcdst += bsize * TF_AVX2_PARALLEL_BLOCKS;
  214. nbytes -= bsize * TF_AVX2_PARALLEL_BLOCKS;
  215. }
  216. while (nbytes >= 8 * bsize) {
  217. twofish_ecb_dec_8way(ctx->ctx, srcdst, srcdst);
  218. srcdst += bsize * 8;
  219. nbytes -= bsize * 8;
  220. }
  221. while (nbytes >= 3 * bsize) {
  222. twofish_dec_blk_3way(ctx->ctx, srcdst, srcdst);
  223. srcdst += bsize * 3;
  224. nbytes -= bsize * 3;
  225. }
  226. for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
  227. twofish_dec_blk(ctx->ctx, srcdst, srcdst);
  228. }
  229. static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  230. struct scatterlist *src, unsigned int nbytes)
  231. {
  232. struct twofish_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  233. be128 buf[TF_AVX2_PARALLEL_BLOCKS];
  234. struct crypt_priv crypt_ctx = {
  235. .ctx = &ctx->twofish_ctx,
  236. .fpu_enabled = false,
  237. };
  238. struct lrw_crypt_req req = {
  239. .tbuf = buf,
  240. .tbuflen = sizeof(buf),
  241. .table_ctx = &ctx->lrw_table,
  242. .crypt_ctx = &crypt_ctx,
  243. .crypt_fn = encrypt_callback,
  244. };
  245. int ret;
  246. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  247. ret = lrw_crypt(desc, dst, src, nbytes, &req);
  248. twofish_fpu_end(crypt_ctx.fpu_enabled);
  249. return ret;
  250. }
  251. static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  252. struct scatterlist *src, unsigned int nbytes)
  253. {
  254. struct twofish_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  255. be128 buf[TF_AVX2_PARALLEL_BLOCKS];
  256. struct crypt_priv crypt_ctx = {
  257. .ctx = &ctx->twofish_ctx,
  258. .fpu_enabled = false,
  259. };
  260. struct lrw_crypt_req req = {
  261. .tbuf = buf,
  262. .tbuflen = sizeof(buf),
  263. .table_ctx = &ctx->lrw_table,
  264. .crypt_ctx = &crypt_ctx,
  265. .crypt_fn = decrypt_callback,
  266. };
  267. int ret;
  268. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  269. ret = lrw_crypt(desc, dst, src, nbytes, &req);
  270. twofish_fpu_end(crypt_ctx.fpu_enabled);
  271. return ret;
  272. }
  273. static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  274. struct scatterlist *src, unsigned int nbytes)
  275. {
  276. struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  277. return glue_xts_crypt_128bit(&twofish_enc_xts, desc, dst, src, nbytes,
  278. XTS_TWEAK_CAST(twofish_enc_blk),
  279. &ctx->tweak_ctx, &ctx->crypt_ctx);
  280. }
  281. static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  282. struct scatterlist *src, unsigned int nbytes)
  283. {
  284. struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  285. return glue_xts_crypt_128bit(&twofish_dec_xts, desc, dst, src, nbytes,
  286. XTS_TWEAK_CAST(twofish_enc_blk),
  287. &ctx->tweak_ctx, &ctx->crypt_ctx);
  288. }
  289. static struct crypto_alg tf_algs[10] = { {
  290. .cra_name = "__ecb-twofish-avx2",
  291. .cra_driver_name = "__driver-ecb-twofish-avx2",
  292. .cra_priority = 0,
  293. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  294. .cra_blocksize = TF_BLOCK_SIZE,
  295. .cra_ctxsize = sizeof(struct twofish_ctx),
  296. .cra_alignmask = 0,
  297. .cra_type = &crypto_blkcipher_type,
  298. .cra_module = THIS_MODULE,
  299. .cra_u = {
  300. .blkcipher = {
  301. .min_keysize = TF_MIN_KEY_SIZE,
  302. .max_keysize = TF_MAX_KEY_SIZE,
  303. .setkey = twofish_setkey,
  304. .encrypt = ecb_encrypt,
  305. .decrypt = ecb_decrypt,
  306. },
  307. },
  308. }, {
  309. .cra_name = "__cbc-twofish-avx2",
  310. .cra_driver_name = "__driver-cbc-twofish-avx2",
  311. .cra_priority = 0,
  312. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  313. .cra_blocksize = TF_BLOCK_SIZE,
  314. .cra_ctxsize = sizeof(struct twofish_ctx),
  315. .cra_alignmask = 0,
  316. .cra_type = &crypto_blkcipher_type,
  317. .cra_module = THIS_MODULE,
  318. .cra_u = {
  319. .blkcipher = {
  320. .min_keysize = TF_MIN_KEY_SIZE,
  321. .max_keysize = TF_MAX_KEY_SIZE,
  322. .setkey = twofish_setkey,
  323. .encrypt = cbc_encrypt,
  324. .decrypt = cbc_decrypt,
  325. },
  326. },
  327. }, {
  328. .cra_name = "__ctr-twofish-avx2",
  329. .cra_driver_name = "__driver-ctr-twofish-avx2",
  330. .cra_priority = 0,
  331. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  332. .cra_blocksize = 1,
  333. .cra_ctxsize = sizeof(struct twofish_ctx),
  334. .cra_alignmask = 0,
  335. .cra_type = &crypto_blkcipher_type,
  336. .cra_module = THIS_MODULE,
  337. .cra_u = {
  338. .blkcipher = {
  339. .min_keysize = TF_MIN_KEY_SIZE,
  340. .max_keysize = TF_MAX_KEY_SIZE,
  341. .ivsize = TF_BLOCK_SIZE,
  342. .setkey = twofish_setkey,
  343. .encrypt = ctr_crypt,
  344. .decrypt = ctr_crypt,
  345. },
  346. },
  347. }, {
  348. .cra_name = "__lrw-twofish-avx2",
  349. .cra_driver_name = "__driver-lrw-twofish-avx2",
  350. .cra_priority = 0,
  351. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  352. .cra_blocksize = TF_BLOCK_SIZE,
  353. .cra_ctxsize = sizeof(struct twofish_lrw_ctx),
  354. .cra_alignmask = 0,
  355. .cra_type = &crypto_blkcipher_type,
  356. .cra_module = THIS_MODULE,
  357. .cra_exit = lrw_twofish_exit_tfm,
  358. .cra_u = {
  359. .blkcipher = {
  360. .min_keysize = TF_MIN_KEY_SIZE +
  361. TF_BLOCK_SIZE,
  362. .max_keysize = TF_MAX_KEY_SIZE +
  363. TF_BLOCK_SIZE,
  364. .ivsize = TF_BLOCK_SIZE,
  365. .setkey = lrw_twofish_setkey,
  366. .encrypt = lrw_encrypt,
  367. .decrypt = lrw_decrypt,
  368. },
  369. },
  370. }, {
  371. .cra_name = "__xts-twofish-avx2",
  372. .cra_driver_name = "__driver-xts-twofish-avx2",
  373. .cra_priority = 0,
  374. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  375. .cra_blocksize = TF_BLOCK_SIZE,
  376. .cra_ctxsize = sizeof(struct twofish_xts_ctx),
  377. .cra_alignmask = 0,
  378. .cra_type = &crypto_blkcipher_type,
  379. .cra_module = THIS_MODULE,
  380. .cra_u = {
  381. .blkcipher = {
  382. .min_keysize = TF_MIN_KEY_SIZE * 2,
  383. .max_keysize = TF_MAX_KEY_SIZE * 2,
  384. .ivsize = TF_BLOCK_SIZE,
  385. .setkey = xts_twofish_setkey,
  386. .encrypt = xts_encrypt,
  387. .decrypt = xts_decrypt,
  388. },
  389. },
  390. }, {
  391. .cra_name = "ecb(twofish)",
  392. .cra_driver_name = "ecb-twofish-avx2",
  393. .cra_priority = 500,
  394. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  395. .cra_blocksize = TF_BLOCK_SIZE,
  396. .cra_ctxsize = sizeof(struct async_helper_ctx),
  397. .cra_alignmask = 0,
  398. .cra_type = &crypto_ablkcipher_type,
  399. .cra_module = THIS_MODULE,
  400. .cra_init = ablk_init,
  401. .cra_exit = ablk_exit,
  402. .cra_u = {
  403. .ablkcipher = {
  404. .min_keysize = TF_MIN_KEY_SIZE,
  405. .max_keysize = TF_MAX_KEY_SIZE,
  406. .setkey = ablk_set_key,
  407. .encrypt = ablk_encrypt,
  408. .decrypt = ablk_decrypt,
  409. },
  410. },
  411. }, {
  412. .cra_name = "cbc(twofish)",
  413. .cra_driver_name = "cbc-twofish-avx2",
  414. .cra_priority = 500,
  415. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  416. .cra_blocksize = TF_BLOCK_SIZE,
  417. .cra_ctxsize = sizeof(struct async_helper_ctx),
  418. .cra_alignmask = 0,
  419. .cra_type = &crypto_ablkcipher_type,
  420. .cra_module = THIS_MODULE,
  421. .cra_init = ablk_init,
  422. .cra_exit = ablk_exit,
  423. .cra_u = {
  424. .ablkcipher = {
  425. .min_keysize = TF_MIN_KEY_SIZE,
  426. .max_keysize = TF_MAX_KEY_SIZE,
  427. .ivsize = TF_BLOCK_SIZE,
  428. .setkey = ablk_set_key,
  429. .encrypt = __ablk_encrypt,
  430. .decrypt = ablk_decrypt,
  431. },
  432. },
  433. }, {
  434. .cra_name = "ctr(twofish)",
  435. .cra_driver_name = "ctr-twofish-avx2",
  436. .cra_priority = 500,
  437. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  438. .cra_blocksize = 1,
  439. .cra_ctxsize = sizeof(struct async_helper_ctx),
  440. .cra_alignmask = 0,
  441. .cra_type = &crypto_ablkcipher_type,
  442. .cra_module = THIS_MODULE,
  443. .cra_init = ablk_init,
  444. .cra_exit = ablk_exit,
  445. .cra_u = {
  446. .ablkcipher = {
  447. .min_keysize = TF_MIN_KEY_SIZE,
  448. .max_keysize = TF_MAX_KEY_SIZE,
  449. .ivsize = TF_BLOCK_SIZE,
  450. .setkey = ablk_set_key,
  451. .encrypt = ablk_encrypt,
  452. .decrypt = ablk_encrypt,
  453. .geniv = "chainiv",
  454. },
  455. },
  456. }, {
  457. .cra_name = "lrw(twofish)",
  458. .cra_driver_name = "lrw-twofish-avx2",
  459. .cra_priority = 500,
  460. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  461. .cra_blocksize = TF_BLOCK_SIZE,
  462. .cra_ctxsize = sizeof(struct async_helper_ctx),
  463. .cra_alignmask = 0,
  464. .cra_type = &crypto_ablkcipher_type,
  465. .cra_module = THIS_MODULE,
  466. .cra_init = ablk_init,
  467. .cra_exit = ablk_exit,
  468. .cra_u = {
  469. .ablkcipher = {
  470. .min_keysize = TF_MIN_KEY_SIZE +
  471. TF_BLOCK_SIZE,
  472. .max_keysize = TF_MAX_KEY_SIZE +
  473. TF_BLOCK_SIZE,
  474. .ivsize = TF_BLOCK_SIZE,
  475. .setkey = ablk_set_key,
  476. .encrypt = ablk_encrypt,
  477. .decrypt = ablk_decrypt,
  478. },
  479. },
  480. }, {
  481. .cra_name = "xts(twofish)",
  482. .cra_driver_name = "xts-twofish-avx2",
  483. .cra_priority = 500,
  484. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  485. .cra_blocksize = TF_BLOCK_SIZE,
  486. .cra_ctxsize = sizeof(struct async_helper_ctx),
  487. .cra_alignmask = 0,
  488. .cra_type = &crypto_ablkcipher_type,
  489. .cra_module = THIS_MODULE,
  490. .cra_init = ablk_init,
  491. .cra_exit = ablk_exit,
  492. .cra_u = {
  493. .ablkcipher = {
  494. .min_keysize = TF_MIN_KEY_SIZE * 2,
  495. .max_keysize = TF_MAX_KEY_SIZE * 2,
  496. .ivsize = TF_BLOCK_SIZE,
  497. .setkey = ablk_set_key,
  498. .encrypt = ablk_encrypt,
  499. .decrypt = ablk_decrypt,
  500. },
  501. },
  502. } };
  503. static int __init init(void)
  504. {
  505. u64 xcr0;
  506. if (!cpu_has_avx2 || !cpu_has_osxsave) {
  507. pr_info("AVX2 instructions are not detected.\n");
  508. return -ENODEV;
  509. }
  510. xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  511. if ((xcr0 & (XSTATE_SSE | XSTATE_YMM)) != (XSTATE_SSE | XSTATE_YMM)) {
  512. pr_info("AVX2 detected but unusable.\n");
  513. return -ENODEV;
  514. }
  515. return crypto_register_algs(tf_algs, ARRAY_SIZE(tf_algs));
  516. }
  517. static void __exit fini(void)
  518. {
  519. crypto_unregister_algs(tf_algs, ARRAY_SIZE(tf_algs));
  520. }
  521. module_init(init);
  522. module_exit(fini);
  523. MODULE_LICENSE("GPL");
  524. MODULE_DESCRIPTION("Twofish Cipher Algorithm, AVX2 optimized");
  525. MODULE_ALIAS("twofish");
  526. MODULE_ALIAS("twofish-asm");