twofish_avx_glue.c 16 KB

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