twofish_avx_glue.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
  46. const u8 *src)
  47. {
  48. __twofish_enc_blk_3way(ctx, dst, src, false);
  49. }
  50. /* 8-way parallel cipher functions */
  51. asmlinkage void __twofish_enc_blk_8way(struct twofish_ctx *ctx, u8 *dst,
  52. const u8 *src, bool xor);
  53. asmlinkage void twofish_dec_blk_8way(struct twofish_ctx *ctx, u8 *dst,
  54. const u8 *src);
  55. static inline void twofish_enc_blk_xway(struct twofish_ctx *ctx, u8 *dst,
  56. const u8 *src)
  57. {
  58. __twofish_enc_blk_8way(ctx, dst, src, false);
  59. }
  60. static inline void twofish_enc_blk_xway_xor(struct twofish_ctx *ctx, u8 *dst,
  61. const u8 *src)
  62. {
  63. __twofish_enc_blk_8way(ctx, dst, src, true);
  64. }
  65. static inline void twofish_dec_blk_xway(struct twofish_ctx *ctx, u8 *dst,
  66. const u8 *src)
  67. {
  68. twofish_dec_blk_8way(ctx, dst, src);
  69. }
  70. static void twofish_dec_blk_cbc_xway(void *ctx, u128 *dst, const u128 *src)
  71. {
  72. u128 ivs[TWOFISH_PARALLEL_BLOCKS - 1];
  73. unsigned int j;
  74. for (j = 0; j < TWOFISH_PARALLEL_BLOCKS - 1; j++)
  75. ivs[j] = src[j];
  76. twofish_dec_blk_xway(ctx, (u8 *)dst, (u8 *)src);
  77. for (j = 0; j < TWOFISH_PARALLEL_BLOCKS - 1; j++)
  78. u128_xor(dst + (j + 1), dst + (j + 1), ivs + j);
  79. }
  80. static void twofish_enc_blk_ctr_xway(void *ctx, u128 *dst, const u128 *src,
  81. u128 *iv)
  82. {
  83. be128 ctrblks[TWOFISH_PARALLEL_BLOCKS];
  84. unsigned int i;
  85. for (i = 0; i < TWOFISH_PARALLEL_BLOCKS; i++) {
  86. if (dst != src)
  87. dst[i] = src[i];
  88. u128_to_be128(&ctrblks[i], iv);
  89. u128_inc(iv);
  90. }
  91. twofish_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks);
  92. }
  93. static const struct common_glue_ctx twofish_enc = {
  94. .num_funcs = 3,
  95. .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
  96. .funcs = { {
  97. .num_blocks = TWOFISH_PARALLEL_BLOCKS,
  98. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_xway) }
  99. }, {
  100. .num_blocks = 3,
  101. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_3way) }
  102. }, {
  103. .num_blocks = 1,
  104. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk) }
  105. } }
  106. };
  107. static const struct common_glue_ctx twofish_ctr = {
  108. .num_funcs = 3,
  109. .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
  110. .funcs = { {
  111. .num_blocks = TWOFISH_PARALLEL_BLOCKS,
  112. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr_xway) }
  113. }, {
  114. .num_blocks = 3,
  115. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr_3way) }
  116. }, {
  117. .num_blocks = 1,
  118. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr) }
  119. } }
  120. };
  121. static const struct common_glue_ctx twofish_dec = {
  122. .num_funcs = 3,
  123. .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
  124. .funcs = { {
  125. .num_blocks = TWOFISH_PARALLEL_BLOCKS,
  126. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk_xway) }
  127. }, {
  128. .num_blocks = 3,
  129. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk_3way) }
  130. }, {
  131. .num_blocks = 1,
  132. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk) }
  133. } }
  134. };
  135. static const struct common_glue_ctx twofish_dec_cbc = {
  136. .num_funcs = 3,
  137. .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
  138. .funcs = { {
  139. .num_blocks = TWOFISH_PARALLEL_BLOCKS,
  140. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk_cbc_xway) }
  141. }, {
  142. .num_blocks = 3,
  143. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk_cbc_3way) }
  144. }, {
  145. .num_blocks = 1,
  146. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk) }
  147. } }
  148. };
  149. static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  150. struct scatterlist *src, unsigned int nbytes)
  151. {
  152. return glue_ecb_crypt_128bit(&twofish_enc, desc, dst, src, nbytes);
  153. }
  154. static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  155. struct scatterlist *src, unsigned int nbytes)
  156. {
  157. return glue_ecb_crypt_128bit(&twofish_dec, desc, dst, src, nbytes);
  158. }
  159. static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  160. struct scatterlist *src, unsigned int nbytes)
  161. {
  162. return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(twofish_enc_blk), desc,
  163. dst, src, nbytes);
  164. }
  165. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  166. struct scatterlist *src, unsigned int nbytes)
  167. {
  168. return glue_cbc_decrypt_128bit(&twofish_dec_cbc, desc, dst, src,
  169. nbytes);
  170. }
  171. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  172. struct scatterlist *src, unsigned int nbytes)
  173. {
  174. return glue_ctr_crypt_128bit(&twofish_ctr, desc, dst, src, nbytes);
  175. }
  176. static inline bool twofish_fpu_begin(bool fpu_enabled, unsigned int nbytes)
  177. {
  178. return glue_fpu_begin(TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS, NULL,
  179. fpu_enabled, nbytes);
  180. }
  181. static inline void twofish_fpu_end(bool fpu_enabled)
  182. {
  183. glue_fpu_end(fpu_enabled);
  184. }
  185. struct crypt_priv {
  186. struct twofish_ctx *ctx;
  187. bool fpu_enabled;
  188. };
  189. static void encrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
  190. {
  191. const unsigned int bsize = TF_BLOCK_SIZE;
  192. struct crypt_priv *ctx = priv;
  193. int i;
  194. ctx->fpu_enabled = twofish_fpu_begin(ctx->fpu_enabled, nbytes);
  195. if (nbytes == bsize * TWOFISH_PARALLEL_BLOCKS) {
  196. twofish_enc_blk_xway(ctx->ctx, srcdst, srcdst);
  197. return;
  198. }
  199. for (i = 0; i < nbytes / (bsize * 3); i++, srcdst += bsize * 3)
  200. twofish_enc_blk_3way(ctx->ctx, srcdst, srcdst);
  201. nbytes %= bsize * 3;
  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. if (nbytes == bsize * TWOFISH_PARALLEL_BLOCKS) {
  212. twofish_dec_blk_xway(ctx->ctx, srcdst, srcdst);
  213. return;
  214. }
  215. for (i = 0; i < nbytes / (bsize * 3); i++, srcdst += bsize * 3)
  216. twofish_dec_blk_3way(ctx->ctx, srcdst, srcdst);
  217. nbytes %= bsize * 3;
  218. for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
  219. twofish_dec_blk(ctx->ctx, srcdst, srcdst);
  220. }
  221. static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  222. struct scatterlist *src, unsigned int nbytes)
  223. {
  224. struct twofish_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  225. be128 buf[TWOFISH_PARALLEL_BLOCKS];
  226. struct crypt_priv crypt_ctx = {
  227. .ctx = &ctx->twofish_ctx,
  228. .fpu_enabled = false,
  229. };
  230. struct lrw_crypt_req req = {
  231. .tbuf = buf,
  232. .tbuflen = sizeof(buf),
  233. .table_ctx = &ctx->lrw_table,
  234. .crypt_ctx = &crypt_ctx,
  235. .crypt_fn = encrypt_callback,
  236. };
  237. int ret;
  238. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  239. ret = lrw_crypt(desc, dst, src, nbytes, &req);
  240. twofish_fpu_end(crypt_ctx.fpu_enabled);
  241. return ret;
  242. }
  243. static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  244. struct scatterlist *src, unsigned int nbytes)
  245. {
  246. struct twofish_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  247. be128 buf[TWOFISH_PARALLEL_BLOCKS];
  248. struct crypt_priv crypt_ctx = {
  249. .ctx = &ctx->twofish_ctx,
  250. .fpu_enabled = false,
  251. };
  252. struct lrw_crypt_req req = {
  253. .tbuf = buf,
  254. .tbuflen = sizeof(buf),
  255. .table_ctx = &ctx->lrw_table,
  256. .crypt_ctx = &crypt_ctx,
  257. .crypt_fn = decrypt_callback,
  258. };
  259. int ret;
  260. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  261. ret = lrw_crypt(desc, dst, src, nbytes, &req);
  262. twofish_fpu_end(crypt_ctx.fpu_enabled);
  263. return ret;
  264. }
  265. static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  266. struct scatterlist *src, unsigned int nbytes)
  267. {
  268. struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  269. be128 buf[TWOFISH_PARALLEL_BLOCKS];
  270. struct crypt_priv crypt_ctx = {
  271. .ctx = &ctx->crypt_ctx,
  272. .fpu_enabled = false,
  273. };
  274. struct xts_crypt_req req = {
  275. .tbuf = buf,
  276. .tbuflen = sizeof(buf),
  277. .tweak_ctx = &ctx->tweak_ctx,
  278. .tweak_fn = XTS_TWEAK_CAST(twofish_enc_blk),
  279. .crypt_ctx = &crypt_ctx,
  280. .crypt_fn = encrypt_callback,
  281. };
  282. int ret;
  283. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  284. ret = xts_crypt(desc, dst, src, nbytes, &req);
  285. twofish_fpu_end(crypt_ctx.fpu_enabled);
  286. return ret;
  287. }
  288. static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  289. struct scatterlist *src, unsigned int nbytes)
  290. {
  291. struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  292. be128 buf[TWOFISH_PARALLEL_BLOCKS];
  293. struct crypt_priv crypt_ctx = {
  294. .ctx = &ctx->crypt_ctx,
  295. .fpu_enabled = false,
  296. };
  297. struct xts_crypt_req req = {
  298. .tbuf = buf,
  299. .tbuflen = sizeof(buf),
  300. .tweak_ctx = &ctx->tweak_ctx,
  301. .tweak_fn = XTS_TWEAK_CAST(twofish_enc_blk),
  302. .crypt_ctx = &crypt_ctx,
  303. .crypt_fn = decrypt_callback,
  304. };
  305. int ret;
  306. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  307. ret = xts_crypt(desc, dst, src, nbytes, &req);
  308. twofish_fpu_end(crypt_ctx.fpu_enabled);
  309. return ret;
  310. }
  311. static struct crypto_alg twofish_algs[10] = { {
  312. .cra_name = "__ecb-twofish-avx",
  313. .cra_driver_name = "__driver-ecb-twofish-avx",
  314. .cra_priority = 0,
  315. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  316. .cra_blocksize = TF_BLOCK_SIZE,
  317. .cra_ctxsize = sizeof(struct twofish_ctx),
  318. .cra_alignmask = 0,
  319. .cra_type = &crypto_blkcipher_type,
  320. .cra_module = THIS_MODULE,
  321. .cra_u = {
  322. .blkcipher = {
  323. .min_keysize = TF_MIN_KEY_SIZE,
  324. .max_keysize = TF_MAX_KEY_SIZE,
  325. .setkey = twofish_setkey,
  326. .encrypt = ecb_encrypt,
  327. .decrypt = ecb_decrypt,
  328. },
  329. },
  330. }, {
  331. .cra_name = "__cbc-twofish-avx",
  332. .cra_driver_name = "__driver-cbc-twofish-avx",
  333. .cra_priority = 0,
  334. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  335. .cra_blocksize = TF_BLOCK_SIZE,
  336. .cra_ctxsize = sizeof(struct twofish_ctx),
  337. .cra_alignmask = 0,
  338. .cra_type = &crypto_blkcipher_type,
  339. .cra_module = THIS_MODULE,
  340. .cra_u = {
  341. .blkcipher = {
  342. .min_keysize = TF_MIN_KEY_SIZE,
  343. .max_keysize = TF_MAX_KEY_SIZE,
  344. .setkey = twofish_setkey,
  345. .encrypt = cbc_encrypt,
  346. .decrypt = cbc_decrypt,
  347. },
  348. },
  349. }, {
  350. .cra_name = "__ctr-twofish-avx",
  351. .cra_driver_name = "__driver-ctr-twofish-avx",
  352. .cra_priority = 0,
  353. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  354. .cra_blocksize = 1,
  355. .cra_ctxsize = sizeof(struct twofish_ctx),
  356. .cra_alignmask = 0,
  357. .cra_type = &crypto_blkcipher_type,
  358. .cra_module = THIS_MODULE,
  359. .cra_u = {
  360. .blkcipher = {
  361. .min_keysize = TF_MIN_KEY_SIZE,
  362. .max_keysize = TF_MAX_KEY_SIZE,
  363. .ivsize = TF_BLOCK_SIZE,
  364. .setkey = twofish_setkey,
  365. .encrypt = ctr_crypt,
  366. .decrypt = ctr_crypt,
  367. },
  368. },
  369. }, {
  370. .cra_name = "__lrw-twofish-avx",
  371. .cra_driver_name = "__driver-lrw-twofish-avx",
  372. .cra_priority = 0,
  373. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  374. .cra_blocksize = TF_BLOCK_SIZE,
  375. .cra_ctxsize = sizeof(struct twofish_lrw_ctx),
  376. .cra_alignmask = 0,
  377. .cra_type = &crypto_blkcipher_type,
  378. .cra_module = THIS_MODULE,
  379. .cra_exit = lrw_twofish_exit_tfm,
  380. .cra_u = {
  381. .blkcipher = {
  382. .min_keysize = TF_MIN_KEY_SIZE +
  383. TF_BLOCK_SIZE,
  384. .max_keysize = TF_MAX_KEY_SIZE +
  385. TF_BLOCK_SIZE,
  386. .ivsize = TF_BLOCK_SIZE,
  387. .setkey = lrw_twofish_setkey,
  388. .encrypt = lrw_encrypt,
  389. .decrypt = lrw_decrypt,
  390. },
  391. },
  392. }, {
  393. .cra_name = "__xts-twofish-avx",
  394. .cra_driver_name = "__driver-xts-twofish-avx",
  395. .cra_priority = 0,
  396. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  397. .cra_blocksize = TF_BLOCK_SIZE,
  398. .cra_ctxsize = sizeof(struct twofish_xts_ctx),
  399. .cra_alignmask = 0,
  400. .cra_type = &crypto_blkcipher_type,
  401. .cra_module = THIS_MODULE,
  402. .cra_u = {
  403. .blkcipher = {
  404. .min_keysize = TF_MIN_KEY_SIZE * 2,
  405. .max_keysize = TF_MAX_KEY_SIZE * 2,
  406. .ivsize = TF_BLOCK_SIZE,
  407. .setkey = xts_twofish_setkey,
  408. .encrypt = xts_encrypt,
  409. .decrypt = xts_decrypt,
  410. },
  411. },
  412. }, {
  413. .cra_name = "ecb(twofish)",
  414. .cra_driver_name = "ecb-twofish-avx",
  415. .cra_priority = 400,
  416. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  417. .cra_blocksize = TF_BLOCK_SIZE,
  418. .cra_ctxsize = sizeof(struct async_helper_ctx),
  419. .cra_alignmask = 0,
  420. .cra_type = &crypto_ablkcipher_type,
  421. .cra_module = THIS_MODULE,
  422. .cra_init = ablk_init,
  423. .cra_exit = ablk_exit,
  424. .cra_u = {
  425. .ablkcipher = {
  426. .min_keysize = TF_MIN_KEY_SIZE,
  427. .max_keysize = TF_MAX_KEY_SIZE,
  428. .setkey = ablk_set_key,
  429. .encrypt = ablk_encrypt,
  430. .decrypt = ablk_decrypt,
  431. },
  432. },
  433. }, {
  434. .cra_name = "cbc(twofish)",
  435. .cra_driver_name = "cbc-twofish-avx",
  436. .cra_priority = 400,
  437. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  438. .cra_blocksize = TF_BLOCK_SIZE,
  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_decrypt,
  453. },
  454. },
  455. }, {
  456. .cra_name = "ctr(twofish)",
  457. .cra_driver_name = "ctr-twofish-avx",
  458. .cra_priority = 400,
  459. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  460. .cra_blocksize = 1,
  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. .max_keysize = TF_MAX_KEY_SIZE,
  471. .ivsize = TF_BLOCK_SIZE,
  472. .setkey = ablk_set_key,
  473. .encrypt = ablk_encrypt,
  474. .decrypt = ablk_encrypt,
  475. .geniv = "chainiv",
  476. },
  477. },
  478. }, {
  479. .cra_name = "lrw(twofish)",
  480. .cra_driver_name = "lrw-twofish-avx",
  481. .cra_priority = 400,
  482. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  483. .cra_blocksize = TF_BLOCK_SIZE,
  484. .cra_ctxsize = sizeof(struct async_helper_ctx),
  485. .cra_alignmask = 0,
  486. .cra_type = &crypto_ablkcipher_type,
  487. .cra_module = THIS_MODULE,
  488. .cra_init = ablk_init,
  489. .cra_exit = ablk_exit,
  490. .cra_u = {
  491. .ablkcipher = {
  492. .min_keysize = TF_MIN_KEY_SIZE +
  493. TF_BLOCK_SIZE,
  494. .max_keysize = TF_MAX_KEY_SIZE +
  495. TF_BLOCK_SIZE,
  496. .ivsize = TF_BLOCK_SIZE,
  497. .setkey = ablk_set_key,
  498. .encrypt = ablk_encrypt,
  499. .decrypt = ablk_decrypt,
  500. },
  501. },
  502. }, {
  503. .cra_name = "xts(twofish)",
  504. .cra_driver_name = "xts-twofish-avx",
  505. .cra_priority = 400,
  506. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  507. .cra_blocksize = TF_BLOCK_SIZE,
  508. .cra_ctxsize = sizeof(struct async_helper_ctx),
  509. .cra_alignmask = 0,
  510. .cra_type = &crypto_ablkcipher_type,
  511. .cra_module = THIS_MODULE,
  512. .cra_init = ablk_init,
  513. .cra_exit = ablk_exit,
  514. .cra_u = {
  515. .ablkcipher = {
  516. .min_keysize = TF_MIN_KEY_SIZE * 2,
  517. .max_keysize = TF_MAX_KEY_SIZE * 2,
  518. .ivsize = TF_BLOCK_SIZE,
  519. .setkey = ablk_set_key,
  520. .encrypt = ablk_encrypt,
  521. .decrypt = ablk_decrypt,
  522. },
  523. },
  524. } };
  525. static int __init twofish_init(void)
  526. {
  527. u64 xcr0;
  528. if (!cpu_has_avx || !cpu_has_osxsave) {
  529. printk(KERN_INFO "AVX instructions are not detected.\n");
  530. return -ENODEV;
  531. }
  532. xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  533. if ((xcr0 & (XSTATE_SSE | XSTATE_YMM)) != (XSTATE_SSE | XSTATE_YMM)) {
  534. printk(KERN_INFO "AVX detected but unusable.\n");
  535. return -ENODEV;
  536. }
  537. return crypto_register_algs(twofish_algs, ARRAY_SIZE(twofish_algs));
  538. }
  539. static void __exit twofish_exit(void)
  540. {
  541. crypto_unregister_algs(twofish_algs, ARRAY_SIZE(twofish_algs));
  542. }
  543. module_init(twofish_init);
  544. module_exit(twofish_exit);
  545. MODULE_DESCRIPTION("Twofish Cipher Algorithm, AVX optimized");
  546. MODULE_LICENSE("GPL");
  547. MODULE_ALIAS("twofish");