twofish_avx_glue.c 16 KB

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