camellia_aesni_avx2_glue.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * Glue Code for x86_64/AVX2/AES-NI assembler optimized version of Camellia
  3. *
  4. * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/crypto.h>
  15. #include <linux/err.h>
  16. #include <crypto/ablk_helper.h>
  17. #include <crypto/algapi.h>
  18. #include <crypto/ctr.h>
  19. #include <crypto/lrw.h>
  20. #include <crypto/xts.h>
  21. #include <asm/fpu/api.h>
  22. #include <asm/crypto/camellia.h>
  23. #include <asm/crypto/glue_helper.h>
  24. #define CAMELLIA_AESNI_PARALLEL_BLOCKS 16
  25. #define CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS 32
  26. /* 32-way AVX2/AES-NI parallel cipher functions */
  27. asmlinkage void camellia_ecb_enc_32way(struct camellia_ctx *ctx, u8 *dst,
  28. const u8 *src);
  29. asmlinkage void camellia_ecb_dec_32way(struct camellia_ctx *ctx, u8 *dst,
  30. const u8 *src);
  31. asmlinkage void camellia_cbc_dec_32way(struct camellia_ctx *ctx, u8 *dst,
  32. const u8 *src);
  33. asmlinkage void camellia_ctr_32way(struct camellia_ctx *ctx, u8 *dst,
  34. const u8 *src, le128 *iv);
  35. asmlinkage void camellia_xts_enc_32way(struct camellia_ctx *ctx, u8 *dst,
  36. const u8 *src, le128 *iv);
  37. asmlinkage void camellia_xts_dec_32way(struct camellia_ctx *ctx, u8 *dst,
  38. const u8 *src, le128 *iv);
  39. static const struct common_glue_ctx camellia_enc = {
  40. .num_funcs = 4,
  41. .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  42. .funcs = { {
  43. .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
  44. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_ecb_enc_32way) }
  45. }, {
  46. .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  47. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_ecb_enc_16way) }
  48. }, {
  49. .num_blocks = 2,
  50. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_enc_blk_2way) }
  51. }, {
  52. .num_blocks = 1,
  53. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_enc_blk) }
  54. } }
  55. };
  56. static const struct common_glue_ctx camellia_ctr = {
  57. .num_funcs = 4,
  58. .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  59. .funcs = { {
  60. .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
  61. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(camellia_ctr_32way) }
  62. }, {
  63. .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  64. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(camellia_ctr_16way) }
  65. }, {
  66. .num_blocks = 2,
  67. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(camellia_crypt_ctr_2way) }
  68. }, {
  69. .num_blocks = 1,
  70. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(camellia_crypt_ctr) }
  71. } }
  72. };
  73. static const struct common_glue_ctx camellia_enc_xts = {
  74. .num_funcs = 3,
  75. .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  76. .funcs = { {
  77. .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
  78. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(camellia_xts_enc_32way) }
  79. }, {
  80. .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  81. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(camellia_xts_enc_16way) }
  82. }, {
  83. .num_blocks = 1,
  84. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(camellia_xts_enc) }
  85. } }
  86. };
  87. static const struct common_glue_ctx camellia_dec = {
  88. .num_funcs = 4,
  89. .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  90. .funcs = { {
  91. .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
  92. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_ecb_dec_32way) }
  93. }, {
  94. .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  95. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_ecb_dec_16way) }
  96. }, {
  97. .num_blocks = 2,
  98. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_dec_blk_2way) }
  99. }, {
  100. .num_blocks = 1,
  101. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_dec_blk) }
  102. } }
  103. };
  104. static const struct common_glue_ctx camellia_dec_cbc = {
  105. .num_funcs = 4,
  106. .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  107. .funcs = { {
  108. .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
  109. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(camellia_cbc_dec_32way) }
  110. }, {
  111. .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  112. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(camellia_cbc_dec_16way) }
  113. }, {
  114. .num_blocks = 2,
  115. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(camellia_decrypt_cbc_2way) }
  116. }, {
  117. .num_blocks = 1,
  118. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(camellia_dec_blk) }
  119. } }
  120. };
  121. static const struct common_glue_ctx camellia_dec_xts = {
  122. .num_funcs = 3,
  123. .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  124. .funcs = { {
  125. .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
  126. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(camellia_xts_dec_32way) }
  127. }, {
  128. .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  129. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(camellia_xts_dec_16way) }
  130. }, {
  131. .num_blocks = 1,
  132. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(camellia_xts_dec) }
  133. } }
  134. };
  135. static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  136. struct scatterlist *src, unsigned int nbytes)
  137. {
  138. return glue_ecb_crypt_128bit(&camellia_enc, desc, dst, src, nbytes);
  139. }
  140. static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  141. struct scatterlist *src, unsigned int nbytes)
  142. {
  143. return glue_ecb_crypt_128bit(&camellia_dec, desc, dst, src, nbytes);
  144. }
  145. static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  146. struct scatterlist *src, unsigned int nbytes)
  147. {
  148. return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(camellia_enc_blk), desc,
  149. dst, src, nbytes);
  150. }
  151. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  152. struct scatterlist *src, unsigned int nbytes)
  153. {
  154. return glue_cbc_decrypt_128bit(&camellia_dec_cbc, desc, dst, src,
  155. nbytes);
  156. }
  157. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  158. struct scatterlist *src, unsigned int nbytes)
  159. {
  160. return glue_ctr_crypt_128bit(&camellia_ctr, desc, dst, src, nbytes);
  161. }
  162. static inline bool camellia_fpu_begin(bool fpu_enabled, unsigned int nbytes)
  163. {
  164. return glue_fpu_begin(CAMELLIA_BLOCK_SIZE,
  165. CAMELLIA_AESNI_PARALLEL_BLOCKS, NULL, fpu_enabled,
  166. nbytes);
  167. }
  168. static inline void camellia_fpu_end(bool fpu_enabled)
  169. {
  170. glue_fpu_end(fpu_enabled);
  171. }
  172. static int camellia_setkey(struct crypto_tfm *tfm, const u8 *in_key,
  173. unsigned int key_len)
  174. {
  175. return __camellia_setkey(crypto_tfm_ctx(tfm), in_key, key_len,
  176. &tfm->crt_flags);
  177. }
  178. struct crypt_priv {
  179. struct camellia_ctx *ctx;
  180. bool fpu_enabled;
  181. };
  182. static void encrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
  183. {
  184. const unsigned int bsize = CAMELLIA_BLOCK_SIZE;
  185. struct crypt_priv *ctx = priv;
  186. int i;
  187. ctx->fpu_enabled = camellia_fpu_begin(ctx->fpu_enabled, nbytes);
  188. if (nbytes >= CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS * bsize) {
  189. camellia_ecb_enc_32way(ctx->ctx, srcdst, srcdst);
  190. srcdst += bsize * CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS;
  191. nbytes -= bsize * CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS;
  192. }
  193. if (nbytes >= CAMELLIA_AESNI_PARALLEL_BLOCKS * bsize) {
  194. camellia_ecb_enc_16way(ctx->ctx, srcdst, srcdst);
  195. srcdst += bsize * CAMELLIA_AESNI_PARALLEL_BLOCKS;
  196. nbytes -= bsize * CAMELLIA_AESNI_PARALLEL_BLOCKS;
  197. }
  198. while (nbytes >= CAMELLIA_PARALLEL_BLOCKS * bsize) {
  199. camellia_enc_blk_2way(ctx->ctx, srcdst, srcdst);
  200. srcdst += bsize * CAMELLIA_PARALLEL_BLOCKS;
  201. nbytes -= bsize * CAMELLIA_PARALLEL_BLOCKS;
  202. }
  203. for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
  204. camellia_enc_blk(ctx->ctx, srcdst, srcdst);
  205. }
  206. static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
  207. {
  208. const unsigned int bsize = CAMELLIA_BLOCK_SIZE;
  209. struct crypt_priv *ctx = priv;
  210. int i;
  211. ctx->fpu_enabled = camellia_fpu_begin(ctx->fpu_enabled, nbytes);
  212. if (nbytes >= CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS * bsize) {
  213. camellia_ecb_dec_32way(ctx->ctx, srcdst, srcdst);
  214. srcdst += bsize * CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS;
  215. nbytes -= bsize * CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS;
  216. }
  217. if (nbytes >= CAMELLIA_AESNI_PARALLEL_BLOCKS * bsize) {
  218. camellia_ecb_dec_16way(ctx->ctx, srcdst, srcdst);
  219. srcdst += bsize * CAMELLIA_AESNI_PARALLEL_BLOCKS;
  220. nbytes -= bsize * CAMELLIA_AESNI_PARALLEL_BLOCKS;
  221. }
  222. while (nbytes >= CAMELLIA_PARALLEL_BLOCKS * bsize) {
  223. camellia_dec_blk_2way(ctx->ctx, srcdst, srcdst);
  224. srcdst += bsize * CAMELLIA_PARALLEL_BLOCKS;
  225. nbytes -= bsize * CAMELLIA_PARALLEL_BLOCKS;
  226. }
  227. for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
  228. camellia_dec_blk(ctx->ctx, srcdst, srcdst);
  229. }
  230. static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  231. struct scatterlist *src, unsigned int nbytes)
  232. {
  233. struct camellia_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  234. be128 buf[CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS];
  235. struct crypt_priv crypt_ctx = {
  236. .ctx = &ctx->camellia_ctx,
  237. .fpu_enabled = false,
  238. };
  239. struct lrw_crypt_req req = {
  240. .tbuf = buf,
  241. .tbuflen = sizeof(buf),
  242. .table_ctx = &ctx->lrw_table,
  243. .crypt_ctx = &crypt_ctx,
  244. .crypt_fn = encrypt_callback,
  245. };
  246. int ret;
  247. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  248. ret = lrw_crypt(desc, dst, src, nbytes, &req);
  249. camellia_fpu_end(crypt_ctx.fpu_enabled);
  250. return ret;
  251. }
  252. static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  253. struct scatterlist *src, unsigned int nbytes)
  254. {
  255. struct camellia_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  256. be128 buf[CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS];
  257. struct crypt_priv crypt_ctx = {
  258. .ctx = &ctx->camellia_ctx,
  259. .fpu_enabled = false,
  260. };
  261. struct lrw_crypt_req req = {
  262. .tbuf = buf,
  263. .tbuflen = sizeof(buf),
  264. .table_ctx = &ctx->lrw_table,
  265. .crypt_ctx = &crypt_ctx,
  266. .crypt_fn = decrypt_callback,
  267. };
  268. int ret;
  269. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  270. ret = lrw_crypt(desc, dst, src, nbytes, &req);
  271. camellia_fpu_end(crypt_ctx.fpu_enabled);
  272. return ret;
  273. }
  274. static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  275. struct scatterlist *src, unsigned int nbytes)
  276. {
  277. struct camellia_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  278. return glue_xts_crypt_128bit(&camellia_enc_xts, desc, dst, src, nbytes,
  279. XTS_TWEAK_CAST(camellia_enc_blk),
  280. &ctx->tweak_ctx, &ctx->crypt_ctx);
  281. }
  282. static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  283. struct scatterlist *src, unsigned int nbytes)
  284. {
  285. struct camellia_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  286. return glue_xts_crypt_128bit(&camellia_dec_xts, desc, dst, src, nbytes,
  287. XTS_TWEAK_CAST(camellia_enc_blk),
  288. &ctx->tweak_ctx, &ctx->crypt_ctx);
  289. }
  290. static struct crypto_alg cmll_algs[10] = { {
  291. .cra_name = "__ecb-camellia-aesni-avx2",
  292. .cra_driver_name = "__driver-ecb-camellia-aesni-avx2",
  293. .cra_priority = 0,
  294. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  295. CRYPTO_ALG_INTERNAL,
  296. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  297. .cra_ctxsize = sizeof(struct camellia_ctx),
  298. .cra_alignmask = 0,
  299. .cra_type = &crypto_blkcipher_type,
  300. .cra_module = THIS_MODULE,
  301. .cra_u = {
  302. .blkcipher = {
  303. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  304. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  305. .setkey = camellia_setkey,
  306. .encrypt = ecb_encrypt,
  307. .decrypt = ecb_decrypt,
  308. },
  309. },
  310. }, {
  311. .cra_name = "__cbc-camellia-aesni-avx2",
  312. .cra_driver_name = "__driver-cbc-camellia-aesni-avx2",
  313. .cra_priority = 0,
  314. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  315. CRYPTO_ALG_INTERNAL,
  316. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  317. .cra_ctxsize = sizeof(struct camellia_ctx),
  318. .cra_alignmask = 0,
  319. .cra_type = &crypto_blkcipher_type,
  320. .cra_module = THIS_MODULE,
  321. .cra_u = {
  322. .blkcipher = {
  323. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  324. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  325. .setkey = camellia_setkey,
  326. .encrypt = cbc_encrypt,
  327. .decrypt = cbc_decrypt,
  328. },
  329. },
  330. }, {
  331. .cra_name = "__ctr-camellia-aesni-avx2",
  332. .cra_driver_name = "__driver-ctr-camellia-aesni-avx2",
  333. .cra_priority = 0,
  334. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  335. CRYPTO_ALG_INTERNAL,
  336. .cra_blocksize = 1,
  337. .cra_ctxsize = sizeof(struct camellia_ctx),
  338. .cra_alignmask = 0,
  339. .cra_type = &crypto_blkcipher_type,
  340. .cra_module = THIS_MODULE,
  341. .cra_u = {
  342. .blkcipher = {
  343. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  344. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  345. .ivsize = CAMELLIA_BLOCK_SIZE,
  346. .setkey = camellia_setkey,
  347. .encrypt = ctr_crypt,
  348. .decrypt = ctr_crypt,
  349. },
  350. },
  351. }, {
  352. .cra_name = "__lrw-camellia-aesni-avx2",
  353. .cra_driver_name = "__driver-lrw-camellia-aesni-avx2",
  354. .cra_priority = 0,
  355. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  356. CRYPTO_ALG_INTERNAL,
  357. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  358. .cra_ctxsize = sizeof(struct camellia_lrw_ctx),
  359. .cra_alignmask = 0,
  360. .cra_type = &crypto_blkcipher_type,
  361. .cra_module = THIS_MODULE,
  362. .cra_exit = lrw_camellia_exit_tfm,
  363. .cra_u = {
  364. .blkcipher = {
  365. .min_keysize = CAMELLIA_MIN_KEY_SIZE +
  366. CAMELLIA_BLOCK_SIZE,
  367. .max_keysize = CAMELLIA_MAX_KEY_SIZE +
  368. CAMELLIA_BLOCK_SIZE,
  369. .ivsize = CAMELLIA_BLOCK_SIZE,
  370. .setkey = lrw_camellia_setkey,
  371. .encrypt = lrw_encrypt,
  372. .decrypt = lrw_decrypt,
  373. },
  374. },
  375. }, {
  376. .cra_name = "__xts-camellia-aesni-avx2",
  377. .cra_driver_name = "__driver-xts-camellia-aesni-avx2",
  378. .cra_priority = 0,
  379. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  380. CRYPTO_ALG_INTERNAL,
  381. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  382. .cra_ctxsize = sizeof(struct camellia_xts_ctx),
  383. .cra_alignmask = 0,
  384. .cra_type = &crypto_blkcipher_type,
  385. .cra_module = THIS_MODULE,
  386. .cra_u = {
  387. .blkcipher = {
  388. .min_keysize = CAMELLIA_MIN_KEY_SIZE * 2,
  389. .max_keysize = CAMELLIA_MAX_KEY_SIZE * 2,
  390. .ivsize = CAMELLIA_BLOCK_SIZE,
  391. .setkey = xts_camellia_setkey,
  392. .encrypt = xts_encrypt,
  393. .decrypt = xts_decrypt,
  394. },
  395. },
  396. }, {
  397. .cra_name = "ecb(camellia)",
  398. .cra_driver_name = "ecb-camellia-aesni-avx2",
  399. .cra_priority = 500,
  400. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  401. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  402. .cra_ctxsize = sizeof(struct async_helper_ctx),
  403. .cra_alignmask = 0,
  404. .cra_type = &crypto_ablkcipher_type,
  405. .cra_module = THIS_MODULE,
  406. .cra_init = ablk_init,
  407. .cra_exit = ablk_exit,
  408. .cra_u = {
  409. .ablkcipher = {
  410. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  411. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  412. .setkey = ablk_set_key,
  413. .encrypt = ablk_encrypt,
  414. .decrypt = ablk_decrypt,
  415. },
  416. },
  417. }, {
  418. .cra_name = "cbc(camellia)",
  419. .cra_driver_name = "cbc-camellia-aesni-avx2",
  420. .cra_priority = 500,
  421. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  422. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  423. .cra_ctxsize = sizeof(struct async_helper_ctx),
  424. .cra_alignmask = 0,
  425. .cra_type = &crypto_ablkcipher_type,
  426. .cra_module = THIS_MODULE,
  427. .cra_init = ablk_init,
  428. .cra_exit = ablk_exit,
  429. .cra_u = {
  430. .ablkcipher = {
  431. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  432. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  433. .ivsize = CAMELLIA_BLOCK_SIZE,
  434. .setkey = ablk_set_key,
  435. .encrypt = __ablk_encrypt,
  436. .decrypt = ablk_decrypt,
  437. },
  438. },
  439. }, {
  440. .cra_name = "ctr(camellia)",
  441. .cra_driver_name = "ctr-camellia-aesni-avx2",
  442. .cra_priority = 500,
  443. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  444. .cra_blocksize = 1,
  445. .cra_ctxsize = sizeof(struct async_helper_ctx),
  446. .cra_alignmask = 0,
  447. .cra_type = &crypto_ablkcipher_type,
  448. .cra_module = THIS_MODULE,
  449. .cra_init = ablk_init,
  450. .cra_exit = ablk_exit,
  451. .cra_u = {
  452. .ablkcipher = {
  453. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  454. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  455. .ivsize = CAMELLIA_BLOCK_SIZE,
  456. .setkey = ablk_set_key,
  457. .encrypt = ablk_encrypt,
  458. .decrypt = ablk_encrypt,
  459. .geniv = "chainiv",
  460. },
  461. },
  462. }, {
  463. .cra_name = "lrw(camellia)",
  464. .cra_driver_name = "lrw-camellia-aesni-avx2",
  465. .cra_priority = 500,
  466. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  467. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  468. .cra_ctxsize = sizeof(struct async_helper_ctx),
  469. .cra_alignmask = 0,
  470. .cra_type = &crypto_ablkcipher_type,
  471. .cra_module = THIS_MODULE,
  472. .cra_init = ablk_init,
  473. .cra_exit = ablk_exit,
  474. .cra_u = {
  475. .ablkcipher = {
  476. .min_keysize = CAMELLIA_MIN_KEY_SIZE +
  477. CAMELLIA_BLOCK_SIZE,
  478. .max_keysize = CAMELLIA_MAX_KEY_SIZE +
  479. CAMELLIA_BLOCK_SIZE,
  480. .ivsize = CAMELLIA_BLOCK_SIZE,
  481. .setkey = ablk_set_key,
  482. .encrypt = ablk_encrypt,
  483. .decrypt = ablk_decrypt,
  484. },
  485. },
  486. }, {
  487. .cra_name = "xts(camellia)",
  488. .cra_driver_name = "xts-camellia-aesni-avx2",
  489. .cra_priority = 500,
  490. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  491. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  492. .cra_ctxsize = sizeof(struct async_helper_ctx),
  493. .cra_alignmask = 0,
  494. .cra_type = &crypto_ablkcipher_type,
  495. .cra_module = THIS_MODULE,
  496. .cra_init = ablk_init,
  497. .cra_exit = ablk_exit,
  498. .cra_u = {
  499. .ablkcipher = {
  500. .min_keysize = CAMELLIA_MIN_KEY_SIZE * 2,
  501. .max_keysize = CAMELLIA_MAX_KEY_SIZE * 2,
  502. .ivsize = CAMELLIA_BLOCK_SIZE,
  503. .setkey = ablk_set_key,
  504. .encrypt = ablk_encrypt,
  505. .decrypt = ablk_decrypt,
  506. },
  507. },
  508. } };
  509. static int __init camellia_aesni_init(void)
  510. {
  511. const char *feature_name;
  512. if (!cpu_has_avx2 || !cpu_has_avx || !cpu_has_aes || !cpu_has_osxsave) {
  513. pr_info("AVX2 or AES-NI instructions are not detected.\n");
  514. return -ENODEV;
  515. }
  516. if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
  517. &feature_name)) {
  518. pr_info("CPU feature '%s' is not supported.\n", feature_name);
  519. return -ENODEV;
  520. }
  521. return crypto_register_algs(cmll_algs, ARRAY_SIZE(cmll_algs));
  522. }
  523. static void __exit camellia_aesni_fini(void)
  524. {
  525. crypto_unregister_algs(cmll_algs, ARRAY_SIZE(cmll_algs));
  526. }
  527. module_init(camellia_aesni_init);
  528. module_exit(camellia_aesni_fini);
  529. MODULE_LICENSE("GPL");
  530. MODULE_DESCRIPTION("Camellia Cipher Algorithm, AES-NI/AVX2 optimized");
  531. MODULE_ALIAS_CRYPTO("camellia");
  532. MODULE_ALIAS_CRYPTO("camellia-asm");