camellia_aesni_avx_glue.c 16 KB

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