camellia_aesni_avx_glue.c 9.3 KB

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