camellia_aesni_avx_glue.c 16 KB

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