twofish_avx_glue.c 15 KB

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