aes-neonbs-glue.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Bit sliced AES using NEON instructions
  3. *
  4. * Copyright (C) 2016 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <asm/neon.h>
  11. #include <asm/simd.h>
  12. #include <crypto/aes.h>
  13. #include <crypto/internal/simd.h>
  14. #include <crypto/internal/skcipher.h>
  15. #include <crypto/xts.h>
  16. #include <linux/module.h>
  17. #include "aes-ctr-fallback.h"
  18. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  19. MODULE_LICENSE("GPL v2");
  20. MODULE_ALIAS_CRYPTO("ecb(aes)");
  21. MODULE_ALIAS_CRYPTO("cbc(aes)");
  22. MODULE_ALIAS_CRYPTO("ctr(aes)");
  23. MODULE_ALIAS_CRYPTO("xts(aes)");
  24. asmlinkage void aesbs_convert_key(u8 out[], u32 const rk[], int rounds);
  25. asmlinkage void aesbs_ecb_encrypt(u8 out[], u8 const in[], u8 const rk[],
  26. int rounds, int blocks);
  27. asmlinkage void aesbs_ecb_decrypt(u8 out[], u8 const in[], u8 const rk[],
  28. int rounds, int blocks);
  29. asmlinkage void aesbs_cbc_decrypt(u8 out[], u8 const in[], u8 const rk[],
  30. int rounds, int blocks, u8 iv[]);
  31. asmlinkage void aesbs_ctr_encrypt(u8 out[], u8 const in[], u8 const rk[],
  32. int rounds, int blocks, u8 iv[], u8 final[]);
  33. asmlinkage void aesbs_xts_encrypt(u8 out[], u8 const in[], u8 const rk[],
  34. int rounds, int blocks, u8 iv[]);
  35. asmlinkage void aesbs_xts_decrypt(u8 out[], u8 const in[], u8 const rk[],
  36. int rounds, int blocks, u8 iv[]);
  37. /* borrowed from aes-neon-blk.ko */
  38. asmlinkage void neon_aes_ecb_encrypt(u8 out[], u8 const in[], u32 const rk[],
  39. int rounds, int blocks);
  40. asmlinkage void neon_aes_cbc_encrypt(u8 out[], u8 const in[], u32 const rk[],
  41. int rounds, int blocks, u8 iv[]);
  42. struct aesbs_ctx {
  43. u8 rk[13 * (8 * AES_BLOCK_SIZE) + 32];
  44. int rounds;
  45. } __aligned(AES_BLOCK_SIZE);
  46. struct aesbs_cbc_ctx {
  47. struct aesbs_ctx key;
  48. u32 enc[AES_MAX_KEYLENGTH_U32];
  49. };
  50. struct aesbs_ctr_ctx {
  51. struct aesbs_ctx key; /* must be first member */
  52. struct crypto_aes_ctx fallback;
  53. };
  54. struct aesbs_xts_ctx {
  55. struct aesbs_ctx key;
  56. u32 twkey[AES_MAX_KEYLENGTH_U32];
  57. };
  58. static int aesbs_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
  59. unsigned int key_len)
  60. {
  61. struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm);
  62. struct crypto_aes_ctx rk;
  63. int err;
  64. err = crypto_aes_expand_key(&rk, in_key, key_len);
  65. if (err)
  66. return err;
  67. ctx->rounds = 6 + key_len / 4;
  68. kernel_neon_begin();
  69. aesbs_convert_key(ctx->rk, rk.key_enc, ctx->rounds);
  70. kernel_neon_end();
  71. return 0;
  72. }
  73. static int __ecb_crypt(struct skcipher_request *req,
  74. void (*fn)(u8 out[], u8 const in[], u8 const rk[],
  75. int rounds, int blocks))
  76. {
  77. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  78. struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm);
  79. struct skcipher_walk walk;
  80. int err;
  81. err = skcipher_walk_virt(&walk, req, false);
  82. while (walk.nbytes >= AES_BLOCK_SIZE) {
  83. unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE;
  84. if (walk.nbytes < walk.total)
  85. blocks = round_down(blocks,
  86. walk.stride / AES_BLOCK_SIZE);
  87. kernel_neon_begin();
  88. fn(walk.dst.virt.addr, walk.src.virt.addr, ctx->rk,
  89. ctx->rounds, blocks);
  90. kernel_neon_end();
  91. err = skcipher_walk_done(&walk,
  92. walk.nbytes - blocks * AES_BLOCK_SIZE);
  93. }
  94. return err;
  95. }
  96. static int ecb_encrypt(struct skcipher_request *req)
  97. {
  98. return __ecb_crypt(req, aesbs_ecb_encrypt);
  99. }
  100. static int ecb_decrypt(struct skcipher_request *req)
  101. {
  102. return __ecb_crypt(req, aesbs_ecb_decrypt);
  103. }
  104. static int aesbs_cbc_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
  105. unsigned int key_len)
  106. {
  107. struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  108. struct crypto_aes_ctx rk;
  109. int err;
  110. err = crypto_aes_expand_key(&rk, in_key, key_len);
  111. if (err)
  112. return err;
  113. ctx->key.rounds = 6 + key_len / 4;
  114. memcpy(ctx->enc, rk.key_enc, sizeof(ctx->enc));
  115. kernel_neon_begin();
  116. aesbs_convert_key(ctx->key.rk, rk.key_enc, ctx->key.rounds);
  117. kernel_neon_end();
  118. return 0;
  119. }
  120. static int cbc_encrypt(struct skcipher_request *req)
  121. {
  122. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  123. struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  124. struct skcipher_walk walk;
  125. int err;
  126. err = skcipher_walk_virt(&walk, req, false);
  127. while (walk.nbytes >= AES_BLOCK_SIZE) {
  128. unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE;
  129. /* fall back to the non-bitsliced NEON implementation */
  130. kernel_neon_begin();
  131. neon_aes_cbc_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
  132. ctx->enc, ctx->key.rounds, blocks,
  133. walk.iv);
  134. kernel_neon_end();
  135. err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
  136. }
  137. return err;
  138. }
  139. static int cbc_decrypt(struct skcipher_request *req)
  140. {
  141. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  142. struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  143. struct skcipher_walk walk;
  144. int err;
  145. err = skcipher_walk_virt(&walk, req, false);
  146. while (walk.nbytes >= AES_BLOCK_SIZE) {
  147. unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE;
  148. if (walk.nbytes < walk.total)
  149. blocks = round_down(blocks,
  150. walk.stride / AES_BLOCK_SIZE);
  151. kernel_neon_begin();
  152. aesbs_cbc_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
  153. ctx->key.rk, ctx->key.rounds, blocks,
  154. walk.iv);
  155. kernel_neon_end();
  156. err = skcipher_walk_done(&walk,
  157. walk.nbytes - blocks * AES_BLOCK_SIZE);
  158. }
  159. return err;
  160. }
  161. static int aesbs_ctr_setkey_sync(struct crypto_skcipher *tfm, const u8 *in_key,
  162. unsigned int key_len)
  163. {
  164. struct aesbs_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
  165. int err;
  166. err = crypto_aes_expand_key(&ctx->fallback, in_key, key_len);
  167. if (err)
  168. return err;
  169. ctx->key.rounds = 6 + key_len / 4;
  170. kernel_neon_begin();
  171. aesbs_convert_key(ctx->key.rk, ctx->fallback.key_enc, ctx->key.rounds);
  172. kernel_neon_end();
  173. return 0;
  174. }
  175. static int ctr_encrypt(struct skcipher_request *req)
  176. {
  177. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  178. struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm);
  179. struct skcipher_walk walk;
  180. u8 buf[AES_BLOCK_SIZE];
  181. int err;
  182. err = skcipher_walk_virt(&walk, req, false);
  183. while (walk.nbytes > 0) {
  184. unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE;
  185. u8 *final = (walk.total % AES_BLOCK_SIZE) ? buf : NULL;
  186. if (walk.nbytes < walk.total) {
  187. blocks = round_down(blocks,
  188. walk.stride / AES_BLOCK_SIZE);
  189. final = NULL;
  190. }
  191. kernel_neon_begin();
  192. aesbs_ctr_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
  193. ctx->rk, ctx->rounds, blocks, walk.iv, final);
  194. kernel_neon_end();
  195. if (final) {
  196. u8 *dst = walk.dst.virt.addr + blocks * AES_BLOCK_SIZE;
  197. u8 *src = walk.src.virt.addr + blocks * AES_BLOCK_SIZE;
  198. crypto_xor_cpy(dst, src, final,
  199. walk.total % AES_BLOCK_SIZE);
  200. err = skcipher_walk_done(&walk, 0);
  201. break;
  202. }
  203. err = skcipher_walk_done(&walk,
  204. walk.nbytes - blocks * AES_BLOCK_SIZE);
  205. }
  206. return err;
  207. }
  208. static int aesbs_xts_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
  209. unsigned int key_len)
  210. {
  211. struct aesbs_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
  212. struct crypto_aes_ctx rk;
  213. int err;
  214. err = xts_verify_key(tfm, in_key, key_len);
  215. if (err)
  216. return err;
  217. key_len /= 2;
  218. err = crypto_aes_expand_key(&rk, in_key + key_len, key_len);
  219. if (err)
  220. return err;
  221. memcpy(ctx->twkey, rk.key_enc, sizeof(ctx->twkey));
  222. return aesbs_setkey(tfm, in_key, key_len);
  223. }
  224. static int ctr_encrypt_sync(struct skcipher_request *req)
  225. {
  226. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  227. struct aesbs_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
  228. if (!may_use_simd())
  229. return aes_ctr_encrypt_fallback(&ctx->fallback, req);
  230. return ctr_encrypt(req);
  231. }
  232. static int __xts_crypt(struct skcipher_request *req,
  233. void (*fn)(u8 out[], u8 const in[], u8 const rk[],
  234. int rounds, int blocks, u8 iv[]))
  235. {
  236. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  237. struct aesbs_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
  238. struct skcipher_walk walk;
  239. int err;
  240. err = skcipher_walk_virt(&walk, req, false);
  241. if (err)
  242. return err;
  243. kernel_neon_begin();
  244. neon_aes_ecb_encrypt(walk.iv, walk.iv, ctx->twkey, ctx->key.rounds, 1);
  245. kernel_neon_end();
  246. while (walk.nbytes >= AES_BLOCK_SIZE) {
  247. unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE;
  248. if (walk.nbytes < walk.total)
  249. blocks = round_down(blocks,
  250. walk.stride / AES_BLOCK_SIZE);
  251. kernel_neon_begin();
  252. fn(walk.dst.virt.addr, walk.src.virt.addr, ctx->key.rk,
  253. ctx->key.rounds, blocks, walk.iv);
  254. kernel_neon_end();
  255. err = skcipher_walk_done(&walk,
  256. walk.nbytes - blocks * AES_BLOCK_SIZE);
  257. }
  258. return err;
  259. }
  260. static int xts_encrypt(struct skcipher_request *req)
  261. {
  262. return __xts_crypt(req, aesbs_xts_encrypt);
  263. }
  264. static int xts_decrypt(struct skcipher_request *req)
  265. {
  266. return __xts_crypt(req, aesbs_xts_decrypt);
  267. }
  268. static struct skcipher_alg aes_algs[] = { {
  269. .base.cra_name = "__ecb(aes)",
  270. .base.cra_driver_name = "__ecb-aes-neonbs",
  271. .base.cra_priority = 250,
  272. .base.cra_blocksize = AES_BLOCK_SIZE,
  273. .base.cra_ctxsize = sizeof(struct aesbs_ctx),
  274. .base.cra_module = THIS_MODULE,
  275. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  276. .min_keysize = AES_MIN_KEY_SIZE,
  277. .max_keysize = AES_MAX_KEY_SIZE,
  278. .walksize = 8 * AES_BLOCK_SIZE,
  279. .setkey = aesbs_setkey,
  280. .encrypt = ecb_encrypt,
  281. .decrypt = ecb_decrypt,
  282. }, {
  283. .base.cra_name = "__cbc(aes)",
  284. .base.cra_driver_name = "__cbc-aes-neonbs",
  285. .base.cra_priority = 250,
  286. .base.cra_blocksize = AES_BLOCK_SIZE,
  287. .base.cra_ctxsize = sizeof(struct aesbs_cbc_ctx),
  288. .base.cra_module = THIS_MODULE,
  289. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  290. .min_keysize = AES_MIN_KEY_SIZE,
  291. .max_keysize = AES_MAX_KEY_SIZE,
  292. .walksize = 8 * AES_BLOCK_SIZE,
  293. .ivsize = AES_BLOCK_SIZE,
  294. .setkey = aesbs_cbc_setkey,
  295. .encrypt = cbc_encrypt,
  296. .decrypt = cbc_decrypt,
  297. }, {
  298. .base.cra_name = "__ctr(aes)",
  299. .base.cra_driver_name = "__ctr-aes-neonbs",
  300. .base.cra_priority = 250,
  301. .base.cra_blocksize = 1,
  302. .base.cra_ctxsize = sizeof(struct aesbs_ctx),
  303. .base.cra_module = THIS_MODULE,
  304. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  305. .min_keysize = AES_MIN_KEY_SIZE,
  306. .max_keysize = AES_MAX_KEY_SIZE,
  307. .chunksize = AES_BLOCK_SIZE,
  308. .walksize = 8 * AES_BLOCK_SIZE,
  309. .ivsize = AES_BLOCK_SIZE,
  310. .setkey = aesbs_setkey,
  311. .encrypt = ctr_encrypt,
  312. .decrypt = ctr_encrypt,
  313. }, {
  314. .base.cra_name = "ctr(aes)",
  315. .base.cra_driver_name = "ctr-aes-neonbs",
  316. .base.cra_priority = 250 - 1,
  317. .base.cra_blocksize = 1,
  318. .base.cra_ctxsize = sizeof(struct aesbs_ctr_ctx),
  319. .base.cra_module = THIS_MODULE,
  320. .min_keysize = AES_MIN_KEY_SIZE,
  321. .max_keysize = AES_MAX_KEY_SIZE,
  322. .chunksize = AES_BLOCK_SIZE,
  323. .walksize = 8 * AES_BLOCK_SIZE,
  324. .ivsize = AES_BLOCK_SIZE,
  325. .setkey = aesbs_ctr_setkey_sync,
  326. .encrypt = ctr_encrypt_sync,
  327. .decrypt = ctr_encrypt_sync,
  328. }, {
  329. .base.cra_name = "__xts(aes)",
  330. .base.cra_driver_name = "__xts-aes-neonbs",
  331. .base.cra_priority = 250,
  332. .base.cra_blocksize = AES_BLOCK_SIZE,
  333. .base.cra_ctxsize = sizeof(struct aesbs_xts_ctx),
  334. .base.cra_module = THIS_MODULE,
  335. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  336. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  337. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  338. .walksize = 8 * AES_BLOCK_SIZE,
  339. .ivsize = AES_BLOCK_SIZE,
  340. .setkey = aesbs_xts_setkey,
  341. .encrypt = xts_encrypt,
  342. .decrypt = xts_decrypt,
  343. } };
  344. static struct simd_skcipher_alg *aes_simd_algs[ARRAY_SIZE(aes_algs)];
  345. static void aes_exit(void)
  346. {
  347. int i;
  348. for (i = 0; i < ARRAY_SIZE(aes_simd_algs); i++)
  349. if (aes_simd_algs[i])
  350. simd_skcipher_free(aes_simd_algs[i]);
  351. crypto_unregister_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
  352. }
  353. static int __init aes_init(void)
  354. {
  355. struct simd_skcipher_alg *simd;
  356. const char *basename;
  357. const char *algname;
  358. const char *drvname;
  359. int err;
  360. int i;
  361. if (!(elf_hwcap & HWCAP_ASIMD))
  362. return -ENODEV;
  363. err = crypto_register_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
  364. if (err)
  365. return err;
  366. for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
  367. if (!(aes_algs[i].base.cra_flags & CRYPTO_ALG_INTERNAL))
  368. continue;
  369. algname = aes_algs[i].base.cra_name + 2;
  370. drvname = aes_algs[i].base.cra_driver_name + 2;
  371. basename = aes_algs[i].base.cra_driver_name;
  372. simd = simd_skcipher_create_compat(algname, drvname, basename);
  373. err = PTR_ERR(simd);
  374. if (IS_ERR(simd))
  375. goto unregister_simds;
  376. aes_simd_algs[i] = simd;
  377. }
  378. return 0;
  379. unregister_simds:
  380. aes_exit();
  381. return err;
  382. }
  383. module_init(aes_init);
  384. module_exit(aes_exit);