cast6_avx_glue.c 17 KB

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