camellia_aesni_avx2_glue.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <asm/crypto/camellia.h>
  13. #include <asm/crypto/glue_helper.h>
  14. #include <crypto/algapi.h>
  15. #include <crypto/internal/simd.h>
  16. #include <crypto/xts.h>
  17. #include <linux/crypto.h>
  18. #include <linux/err.h>
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #define CAMELLIA_AESNI_PARALLEL_BLOCKS 16
  22. #define CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS 32
  23. /* 32-way AVX2/AES-NI parallel cipher functions */
  24. asmlinkage void camellia_ecb_enc_32way(struct camellia_ctx *ctx, u8 *dst,
  25. const u8 *src);
  26. asmlinkage void camellia_ecb_dec_32way(struct camellia_ctx *ctx, u8 *dst,
  27. const u8 *src);
  28. asmlinkage void camellia_cbc_dec_32way(struct camellia_ctx *ctx, u8 *dst,
  29. const u8 *src);
  30. asmlinkage void camellia_ctr_32way(struct camellia_ctx *ctx, u8 *dst,
  31. const u8 *src, le128 *iv);
  32. asmlinkage void camellia_xts_enc_32way(struct camellia_ctx *ctx, u8 *dst,
  33. const u8 *src, le128 *iv);
  34. asmlinkage void camellia_xts_dec_32way(struct camellia_ctx *ctx, u8 *dst,
  35. const u8 *src, le128 *iv);
  36. static const struct common_glue_ctx camellia_enc = {
  37. .num_funcs = 4,
  38. .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  39. .funcs = { {
  40. .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
  41. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_ecb_enc_32way) }
  42. }, {
  43. .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  44. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_ecb_enc_16way) }
  45. }, {
  46. .num_blocks = 2,
  47. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_enc_blk_2way) }
  48. }, {
  49. .num_blocks = 1,
  50. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_enc_blk) }
  51. } }
  52. };
  53. static const struct common_glue_ctx camellia_ctr = {
  54. .num_funcs = 4,
  55. .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  56. .funcs = { {
  57. .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
  58. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(camellia_ctr_32way) }
  59. }, {
  60. .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  61. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(camellia_ctr_16way) }
  62. }, {
  63. .num_blocks = 2,
  64. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(camellia_crypt_ctr_2way) }
  65. }, {
  66. .num_blocks = 1,
  67. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(camellia_crypt_ctr) }
  68. } }
  69. };
  70. static const struct common_glue_ctx camellia_enc_xts = {
  71. .num_funcs = 3,
  72. .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  73. .funcs = { {
  74. .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
  75. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(camellia_xts_enc_32way) }
  76. }, {
  77. .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  78. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(camellia_xts_enc_16way) }
  79. }, {
  80. .num_blocks = 1,
  81. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(camellia_xts_enc) }
  82. } }
  83. };
  84. static const struct common_glue_ctx camellia_dec = {
  85. .num_funcs = 4,
  86. .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  87. .funcs = { {
  88. .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
  89. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_ecb_dec_32way) }
  90. }, {
  91. .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  92. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_ecb_dec_16way) }
  93. }, {
  94. .num_blocks = 2,
  95. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_dec_blk_2way) }
  96. }, {
  97. .num_blocks = 1,
  98. .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_dec_blk) }
  99. } }
  100. };
  101. static const struct common_glue_ctx camellia_dec_cbc = {
  102. .num_funcs = 4,
  103. .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  104. .funcs = { {
  105. .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
  106. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(camellia_cbc_dec_32way) }
  107. }, {
  108. .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  109. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(camellia_cbc_dec_16way) }
  110. }, {
  111. .num_blocks = 2,
  112. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(camellia_decrypt_cbc_2way) }
  113. }, {
  114. .num_blocks = 1,
  115. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(camellia_dec_blk) }
  116. } }
  117. };
  118. static const struct common_glue_ctx camellia_dec_xts = {
  119. .num_funcs = 3,
  120. .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  121. .funcs = { {
  122. .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
  123. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(camellia_xts_dec_32way) }
  124. }, {
  125. .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
  126. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(camellia_xts_dec_16way) }
  127. }, {
  128. .num_blocks = 1,
  129. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(camellia_xts_dec) }
  130. } }
  131. };
  132. static int camellia_setkey(struct crypto_skcipher *tfm, const u8 *key,
  133. unsigned int keylen)
  134. {
  135. return __camellia_setkey(crypto_skcipher_ctx(tfm), key, keylen,
  136. &tfm->base.crt_flags);
  137. }
  138. static int ecb_encrypt(struct skcipher_request *req)
  139. {
  140. return glue_ecb_req_128bit(&camellia_enc, req);
  141. }
  142. static int ecb_decrypt(struct skcipher_request *req)
  143. {
  144. return glue_ecb_req_128bit(&camellia_dec, req);
  145. }
  146. static int cbc_encrypt(struct skcipher_request *req)
  147. {
  148. return glue_cbc_encrypt_req_128bit(GLUE_FUNC_CAST(camellia_enc_blk),
  149. req);
  150. }
  151. static int cbc_decrypt(struct skcipher_request *req)
  152. {
  153. return glue_cbc_decrypt_req_128bit(&camellia_dec_cbc, req);
  154. }
  155. static int ctr_crypt(struct skcipher_request *req)
  156. {
  157. return glue_ctr_req_128bit(&camellia_ctr, req);
  158. }
  159. static int xts_encrypt(struct skcipher_request *req)
  160. {
  161. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  162. struct camellia_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
  163. return glue_xts_req_128bit(&camellia_enc_xts, req,
  164. XTS_TWEAK_CAST(camellia_enc_blk),
  165. &ctx->tweak_ctx, &ctx->crypt_ctx);
  166. }
  167. static int xts_decrypt(struct skcipher_request *req)
  168. {
  169. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  170. struct camellia_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
  171. return glue_xts_req_128bit(&camellia_dec_xts, req,
  172. XTS_TWEAK_CAST(camellia_enc_blk),
  173. &ctx->tweak_ctx, &ctx->crypt_ctx);
  174. }
  175. static struct skcipher_alg camellia_algs[] = {
  176. {
  177. .base.cra_name = "__ecb(camellia)",
  178. .base.cra_driver_name = "__ecb-camellia-aesni-avx2",
  179. .base.cra_priority = 500,
  180. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  181. .base.cra_blocksize = CAMELLIA_BLOCK_SIZE,
  182. .base.cra_ctxsize = sizeof(struct camellia_ctx),
  183. .base.cra_module = THIS_MODULE,
  184. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  185. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  186. .setkey = camellia_setkey,
  187. .encrypt = ecb_encrypt,
  188. .decrypt = ecb_decrypt,
  189. }, {
  190. .base.cra_name = "__cbc(camellia)",
  191. .base.cra_driver_name = "__cbc-camellia-aesni-avx2",
  192. .base.cra_priority = 500,
  193. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  194. .base.cra_blocksize = CAMELLIA_BLOCK_SIZE,
  195. .base.cra_ctxsize = sizeof(struct camellia_ctx),
  196. .base.cra_module = THIS_MODULE,
  197. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  198. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  199. .ivsize = CAMELLIA_BLOCK_SIZE,
  200. .setkey = camellia_setkey,
  201. .encrypt = cbc_encrypt,
  202. .decrypt = cbc_decrypt,
  203. }, {
  204. .base.cra_name = "__ctr(camellia)",
  205. .base.cra_driver_name = "__ctr-camellia-aesni-avx2",
  206. .base.cra_priority = 500,
  207. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  208. .base.cra_blocksize = 1,
  209. .base.cra_ctxsize = sizeof(struct camellia_ctx),
  210. .base.cra_module = THIS_MODULE,
  211. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  212. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  213. .ivsize = CAMELLIA_BLOCK_SIZE,
  214. .chunksize = CAMELLIA_BLOCK_SIZE,
  215. .setkey = camellia_setkey,
  216. .encrypt = ctr_crypt,
  217. .decrypt = ctr_crypt,
  218. }, {
  219. .base.cra_name = "__xts(camellia)",
  220. .base.cra_driver_name = "__xts-camellia-aesni-avx2",
  221. .base.cra_priority = 500,
  222. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  223. .base.cra_blocksize = CAMELLIA_BLOCK_SIZE,
  224. .base.cra_ctxsize = sizeof(struct camellia_xts_ctx),
  225. .base.cra_module = THIS_MODULE,
  226. .min_keysize = 2 * CAMELLIA_MIN_KEY_SIZE,
  227. .max_keysize = 2 * CAMELLIA_MAX_KEY_SIZE,
  228. .ivsize = CAMELLIA_BLOCK_SIZE,
  229. .setkey = xts_camellia_setkey,
  230. .encrypt = xts_encrypt,
  231. .decrypt = xts_decrypt,
  232. },
  233. };
  234. static struct simd_skcipher_alg *camellia_simd_algs[ARRAY_SIZE(camellia_algs)];
  235. static int __init camellia_aesni_init(void)
  236. {
  237. const char *feature_name;
  238. if (!boot_cpu_has(X86_FEATURE_AVX) ||
  239. !boot_cpu_has(X86_FEATURE_AVX2) ||
  240. !boot_cpu_has(X86_FEATURE_AES) ||
  241. !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
  242. pr_info("AVX2 or AES-NI instructions are not detected.\n");
  243. return -ENODEV;
  244. }
  245. if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
  246. &feature_name)) {
  247. pr_info("CPU feature '%s' is not supported.\n", feature_name);
  248. return -ENODEV;
  249. }
  250. return simd_register_skciphers_compat(camellia_algs,
  251. ARRAY_SIZE(camellia_algs),
  252. camellia_simd_algs);
  253. }
  254. static void __exit camellia_aesni_fini(void)
  255. {
  256. simd_unregister_skciphers(camellia_algs, ARRAY_SIZE(camellia_algs),
  257. camellia_simd_algs);
  258. }
  259. module_init(camellia_aesni_init);
  260. module_exit(camellia_aesni_fini);
  261. MODULE_LICENSE("GPL");
  262. MODULE_DESCRIPTION("Camellia Cipher Algorithm, AES-NI/AVX2 optimized");
  263. MODULE_ALIAS_CRYPTO("camellia");
  264. MODULE_ALIAS_CRYPTO("camellia-asm");