cast6_avx_glue.c 16 KB

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