aes-glue.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * linux/arch/arm64/crypto/aes-glue.c - wrapper code for ARMv8 AES
  3. *
  4. * Copyright (C) 2013 - 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/hwcap.h>
  12. #include <asm/simd.h>
  13. #include <crypto/aes.h>
  14. #include <crypto/internal/hash.h>
  15. #include <crypto/internal/simd.h>
  16. #include <crypto/internal/skcipher.h>
  17. #include <linux/module.h>
  18. #include <linux/cpufeature.h>
  19. #include <crypto/xts.h>
  20. #include "aes-ce-setkey.h"
  21. #include "aes-ctr-fallback.h"
  22. #ifdef USE_V8_CRYPTO_EXTENSIONS
  23. #define MODE "ce"
  24. #define PRIO 300
  25. #define aes_setkey ce_aes_setkey
  26. #define aes_expandkey ce_aes_expandkey
  27. #define aes_ecb_encrypt ce_aes_ecb_encrypt
  28. #define aes_ecb_decrypt ce_aes_ecb_decrypt
  29. #define aes_cbc_encrypt ce_aes_cbc_encrypt
  30. #define aes_cbc_decrypt ce_aes_cbc_decrypt
  31. #define aes_ctr_encrypt ce_aes_ctr_encrypt
  32. #define aes_xts_encrypt ce_aes_xts_encrypt
  33. #define aes_xts_decrypt ce_aes_xts_decrypt
  34. #define aes_mac_update ce_aes_mac_update
  35. MODULE_DESCRIPTION("AES-ECB/CBC/CTR/XTS using ARMv8 Crypto Extensions");
  36. #else
  37. #define MODE "neon"
  38. #define PRIO 200
  39. #define aes_setkey crypto_aes_set_key
  40. #define aes_expandkey crypto_aes_expand_key
  41. #define aes_ecb_encrypt neon_aes_ecb_encrypt
  42. #define aes_ecb_decrypt neon_aes_ecb_decrypt
  43. #define aes_cbc_encrypt neon_aes_cbc_encrypt
  44. #define aes_cbc_decrypt neon_aes_cbc_decrypt
  45. #define aes_ctr_encrypt neon_aes_ctr_encrypt
  46. #define aes_xts_encrypt neon_aes_xts_encrypt
  47. #define aes_xts_decrypt neon_aes_xts_decrypt
  48. #define aes_mac_update neon_aes_mac_update
  49. MODULE_DESCRIPTION("AES-ECB/CBC/CTR/XTS using ARMv8 NEON");
  50. MODULE_ALIAS_CRYPTO("ecb(aes)");
  51. MODULE_ALIAS_CRYPTO("cbc(aes)");
  52. MODULE_ALIAS_CRYPTO("ctr(aes)");
  53. MODULE_ALIAS_CRYPTO("xts(aes)");
  54. MODULE_ALIAS_CRYPTO("cmac(aes)");
  55. MODULE_ALIAS_CRYPTO("xcbc(aes)");
  56. MODULE_ALIAS_CRYPTO("cbcmac(aes)");
  57. #endif
  58. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  59. MODULE_LICENSE("GPL v2");
  60. /* defined in aes-modes.S */
  61. asmlinkage void aes_ecb_encrypt(u8 out[], u8 const in[], u8 const rk[],
  62. int rounds, int blocks);
  63. asmlinkage void aes_ecb_decrypt(u8 out[], u8 const in[], u8 const rk[],
  64. int rounds, int blocks);
  65. asmlinkage void aes_cbc_encrypt(u8 out[], u8 const in[], u8 const rk[],
  66. int rounds, int blocks, u8 iv[]);
  67. asmlinkage void aes_cbc_decrypt(u8 out[], u8 const in[], u8 const rk[],
  68. int rounds, int blocks, u8 iv[]);
  69. asmlinkage void aes_ctr_encrypt(u8 out[], u8 const in[], u8 const rk[],
  70. int rounds, int blocks, u8 ctr[]);
  71. asmlinkage void aes_xts_encrypt(u8 out[], u8 const in[], u8 const rk1[],
  72. int rounds, int blocks, u8 const rk2[], u8 iv[],
  73. int first);
  74. asmlinkage void aes_xts_decrypt(u8 out[], u8 const in[], u8 const rk1[],
  75. int rounds, int blocks, u8 const rk2[], u8 iv[],
  76. int first);
  77. asmlinkage void aes_mac_update(u8 const in[], u32 const rk[], int rounds,
  78. int blocks, u8 dg[], int enc_before,
  79. int enc_after);
  80. struct crypto_aes_xts_ctx {
  81. struct crypto_aes_ctx key1;
  82. struct crypto_aes_ctx __aligned(8) key2;
  83. };
  84. struct mac_tfm_ctx {
  85. struct crypto_aes_ctx key;
  86. u8 __aligned(8) consts[];
  87. };
  88. struct mac_desc_ctx {
  89. unsigned int len;
  90. u8 dg[AES_BLOCK_SIZE];
  91. };
  92. static int skcipher_aes_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
  93. unsigned int key_len)
  94. {
  95. return aes_setkey(crypto_skcipher_tfm(tfm), in_key, key_len);
  96. }
  97. static int xts_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
  98. unsigned int key_len)
  99. {
  100. struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
  101. int ret;
  102. ret = xts_verify_key(tfm, in_key, key_len);
  103. if (ret)
  104. return ret;
  105. ret = aes_expandkey(&ctx->key1, in_key, key_len / 2);
  106. if (!ret)
  107. ret = aes_expandkey(&ctx->key2, &in_key[key_len / 2],
  108. key_len / 2);
  109. if (!ret)
  110. return 0;
  111. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  112. return -EINVAL;
  113. }
  114. static int ecb_encrypt(struct skcipher_request *req)
  115. {
  116. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  117. struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
  118. int err, rounds = 6 + ctx->key_length / 4;
  119. struct skcipher_walk walk;
  120. unsigned int blocks;
  121. err = skcipher_walk_virt(&walk, req, false);
  122. while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) {
  123. kernel_neon_begin();
  124. aes_ecb_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
  125. (u8 *)ctx->key_enc, rounds, blocks);
  126. kernel_neon_end();
  127. err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
  128. }
  129. return err;
  130. }
  131. static int ecb_decrypt(struct skcipher_request *req)
  132. {
  133. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  134. struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
  135. int err, rounds = 6 + ctx->key_length / 4;
  136. struct skcipher_walk walk;
  137. unsigned int blocks;
  138. err = skcipher_walk_virt(&walk, req, false);
  139. while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) {
  140. kernel_neon_begin();
  141. aes_ecb_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
  142. (u8 *)ctx->key_dec, rounds, blocks);
  143. kernel_neon_end();
  144. err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
  145. }
  146. return err;
  147. }
  148. static int cbc_encrypt(struct skcipher_request *req)
  149. {
  150. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  151. struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
  152. int err, rounds = 6 + ctx->key_length / 4;
  153. struct skcipher_walk walk;
  154. unsigned int blocks;
  155. err = skcipher_walk_virt(&walk, req, false);
  156. while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) {
  157. kernel_neon_begin();
  158. aes_cbc_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
  159. (u8 *)ctx->key_enc, rounds, blocks, walk.iv);
  160. kernel_neon_end();
  161. err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
  162. }
  163. return err;
  164. }
  165. static int cbc_decrypt(struct skcipher_request *req)
  166. {
  167. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  168. struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
  169. int err, rounds = 6 + ctx->key_length / 4;
  170. struct skcipher_walk walk;
  171. unsigned int blocks;
  172. err = skcipher_walk_virt(&walk, req, false);
  173. while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) {
  174. kernel_neon_begin();
  175. aes_cbc_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
  176. (u8 *)ctx->key_dec, rounds, blocks, walk.iv);
  177. kernel_neon_end();
  178. err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
  179. }
  180. return err;
  181. }
  182. static int ctr_encrypt(struct skcipher_request *req)
  183. {
  184. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  185. struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
  186. int err, rounds = 6 + ctx->key_length / 4;
  187. struct skcipher_walk walk;
  188. int blocks;
  189. err = skcipher_walk_virt(&walk, req, false);
  190. while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) {
  191. kernel_neon_begin();
  192. aes_ctr_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
  193. (u8 *)ctx->key_enc, rounds, blocks, walk.iv);
  194. kernel_neon_end();
  195. err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
  196. }
  197. if (walk.nbytes) {
  198. u8 __aligned(8) tail[AES_BLOCK_SIZE];
  199. unsigned int nbytes = walk.nbytes;
  200. u8 *tdst = walk.dst.virt.addr;
  201. u8 *tsrc = walk.src.virt.addr;
  202. /*
  203. * Tell aes_ctr_encrypt() to process a tail block.
  204. */
  205. blocks = -1;
  206. kernel_neon_begin();
  207. aes_ctr_encrypt(tail, NULL, (u8 *)ctx->key_enc, rounds,
  208. blocks, walk.iv);
  209. kernel_neon_end();
  210. crypto_xor_cpy(tdst, tsrc, tail, nbytes);
  211. err = skcipher_walk_done(&walk, 0);
  212. }
  213. return err;
  214. }
  215. static int ctr_encrypt_sync(struct skcipher_request *req)
  216. {
  217. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  218. struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
  219. if (!may_use_simd())
  220. return aes_ctr_encrypt_fallback(ctx, req);
  221. return ctr_encrypt(req);
  222. }
  223. static int xts_encrypt(struct skcipher_request *req)
  224. {
  225. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  226. struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
  227. int err, first, rounds = 6 + ctx->key1.key_length / 4;
  228. struct skcipher_walk walk;
  229. unsigned int blocks;
  230. err = skcipher_walk_virt(&walk, req, false);
  231. for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) {
  232. kernel_neon_begin();
  233. aes_xts_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
  234. (u8 *)ctx->key1.key_enc, rounds, blocks,
  235. (u8 *)ctx->key2.key_enc, walk.iv, first);
  236. kernel_neon_end();
  237. err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
  238. }
  239. return err;
  240. }
  241. static int xts_decrypt(struct skcipher_request *req)
  242. {
  243. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  244. struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
  245. int err, first, rounds = 6 + ctx->key1.key_length / 4;
  246. struct skcipher_walk walk;
  247. unsigned int blocks;
  248. err = skcipher_walk_virt(&walk, req, false);
  249. for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) {
  250. kernel_neon_begin();
  251. aes_xts_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
  252. (u8 *)ctx->key1.key_dec, rounds, blocks,
  253. (u8 *)ctx->key2.key_enc, walk.iv, first);
  254. kernel_neon_end();
  255. err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE);
  256. }
  257. return err;
  258. }
  259. static struct skcipher_alg aes_algs[] = { {
  260. .base = {
  261. .cra_name = "__ecb(aes)",
  262. .cra_driver_name = "__ecb-aes-" MODE,
  263. .cra_priority = PRIO,
  264. .cra_flags = CRYPTO_ALG_INTERNAL,
  265. .cra_blocksize = AES_BLOCK_SIZE,
  266. .cra_ctxsize = sizeof(struct crypto_aes_ctx),
  267. .cra_module = THIS_MODULE,
  268. },
  269. .min_keysize = AES_MIN_KEY_SIZE,
  270. .max_keysize = AES_MAX_KEY_SIZE,
  271. .setkey = skcipher_aes_setkey,
  272. .encrypt = ecb_encrypt,
  273. .decrypt = ecb_decrypt,
  274. }, {
  275. .base = {
  276. .cra_name = "__cbc(aes)",
  277. .cra_driver_name = "__cbc-aes-" MODE,
  278. .cra_priority = PRIO,
  279. .cra_flags = CRYPTO_ALG_INTERNAL,
  280. .cra_blocksize = AES_BLOCK_SIZE,
  281. .cra_ctxsize = sizeof(struct crypto_aes_ctx),
  282. .cra_module = THIS_MODULE,
  283. },
  284. .min_keysize = AES_MIN_KEY_SIZE,
  285. .max_keysize = AES_MAX_KEY_SIZE,
  286. .ivsize = AES_BLOCK_SIZE,
  287. .setkey = skcipher_aes_setkey,
  288. .encrypt = cbc_encrypt,
  289. .decrypt = cbc_decrypt,
  290. }, {
  291. .base = {
  292. .cra_name = "__ctr(aes)",
  293. .cra_driver_name = "__ctr-aes-" MODE,
  294. .cra_priority = PRIO,
  295. .cra_flags = CRYPTO_ALG_INTERNAL,
  296. .cra_blocksize = 1,
  297. .cra_ctxsize = sizeof(struct crypto_aes_ctx),
  298. .cra_module = THIS_MODULE,
  299. },
  300. .min_keysize = AES_MIN_KEY_SIZE,
  301. .max_keysize = AES_MAX_KEY_SIZE,
  302. .ivsize = AES_BLOCK_SIZE,
  303. .chunksize = AES_BLOCK_SIZE,
  304. .setkey = skcipher_aes_setkey,
  305. .encrypt = ctr_encrypt,
  306. .decrypt = ctr_encrypt,
  307. }, {
  308. .base = {
  309. .cra_name = "ctr(aes)",
  310. .cra_driver_name = "ctr-aes-" MODE,
  311. .cra_priority = PRIO - 1,
  312. .cra_blocksize = 1,
  313. .cra_ctxsize = sizeof(struct crypto_aes_ctx),
  314. .cra_module = THIS_MODULE,
  315. },
  316. .min_keysize = AES_MIN_KEY_SIZE,
  317. .max_keysize = AES_MAX_KEY_SIZE,
  318. .ivsize = AES_BLOCK_SIZE,
  319. .chunksize = AES_BLOCK_SIZE,
  320. .setkey = skcipher_aes_setkey,
  321. .encrypt = ctr_encrypt_sync,
  322. .decrypt = ctr_encrypt_sync,
  323. }, {
  324. .base = {
  325. .cra_name = "__xts(aes)",
  326. .cra_driver_name = "__xts-aes-" MODE,
  327. .cra_priority = PRIO,
  328. .cra_flags = CRYPTO_ALG_INTERNAL,
  329. .cra_blocksize = AES_BLOCK_SIZE,
  330. .cra_ctxsize = sizeof(struct crypto_aes_xts_ctx),
  331. .cra_module = THIS_MODULE,
  332. },
  333. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  334. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  335. .ivsize = AES_BLOCK_SIZE,
  336. .setkey = xts_set_key,
  337. .encrypt = xts_encrypt,
  338. .decrypt = xts_decrypt,
  339. } };
  340. static int cbcmac_setkey(struct crypto_shash *tfm, const u8 *in_key,
  341. unsigned int key_len)
  342. {
  343. struct mac_tfm_ctx *ctx = crypto_shash_ctx(tfm);
  344. int err;
  345. err = aes_expandkey(&ctx->key, in_key, key_len);
  346. if (err)
  347. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  348. return err;
  349. }
  350. static void cmac_gf128_mul_by_x(be128 *y, const be128 *x)
  351. {
  352. u64 a = be64_to_cpu(x->a);
  353. u64 b = be64_to_cpu(x->b);
  354. y->a = cpu_to_be64((a << 1) | (b >> 63));
  355. y->b = cpu_to_be64((b << 1) ^ ((a >> 63) ? 0x87 : 0));
  356. }
  357. static int cmac_setkey(struct crypto_shash *tfm, const u8 *in_key,
  358. unsigned int key_len)
  359. {
  360. struct mac_tfm_ctx *ctx = crypto_shash_ctx(tfm);
  361. be128 *consts = (be128 *)ctx->consts;
  362. u8 *rk = (u8 *)ctx->key.key_enc;
  363. int rounds = 6 + key_len / 4;
  364. int err;
  365. err = cbcmac_setkey(tfm, in_key, key_len);
  366. if (err)
  367. return err;
  368. /* encrypt the zero vector */
  369. kernel_neon_begin();
  370. aes_ecb_encrypt(ctx->consts, (u8[AES_BLOCK_SIZE]){}, rk, rounds, 1);
  371. kernel_neon_end();
  372. cmac_gf128_mul_by_x(consts, consts);
  373. cmac_gf128_mul_by_x(consts + 1, consts);
  374. return 0;
  375. }
  376. static int xcbc_setkey(struct crypto_shash *tfm, const u8 *in_key,
  377. unsigned int key_len)
  378. {
  379. static u8 const ks[3][AES_BLOCK_SIZE] = {
  380. { [0 ... AES_BLOCK_SIZE - 1] = 0x1 },
  381. { [0 ... AES_BLOCK_SIZE - 1] = 0x2 },
  382. { [0 ... AES_BLOCK_SIZE - 1] = 0x3 },
  383. };
  384. struct mac_tfm_ctx *ctx = crypto_shash_ctx(tfm);
  385. u8 *rk = (u8 *)ctx->key.key_enc;
  386. int rounds = 6 + key_len / 4;
  387. u8 key[AES_BLOCK_SIZE];
  388. int err;
  389. err = cbcmac_setkey(tfm, in_key, key_len);
  390. if (err)
  391. return err;
  392. kernel_neon_begin();
  393. aes_ecb_encrypt(key, ks[0], rk, rounds, 1);
  394. aes_ecb_encrypt(ctx->consts, ks[1], rk, rounds, 2);
  395. kernel_neon_end();
  396. return cbcmac_setkey(tfm, key, sizeof(key));
  397. }
  398. static int mac_init(struct shash_desc *desc)
  399. {
  400. struct mac_desc_ctx *ctx = shash_desc_ctx(desc);
  401. memset(ctx->dg, 0, AES_BLOCK_SIZE);
  402. ctx->len = 0;
  403. return 0;
  404. }
  405. static void mac_do_update(struct crypto_aes_ctx *ctx, u8 const in[], int blocks,
  406. u8 dg[], int enc_before, int enc_after)
  407. {
  408. int rounds = 6 + ctx->key_length / 4;
  409. if (may_use_simd()) {
  410. kernel_neon_begin();
  411. aes_mac_update(in, ctx->key_enc, rounds, blocks, dg, enc_before,
  412. enc_after);
  413. kernel_neon_end();
  414. } else {
  415. if (enc_before)
  416. __aes_arm64_encrypt(ctx->key_enc, dg, dg, rounds);
  417. while (blocks--) {
  418. crypto_xor(dg, in, AES_BLOCK_SIZE);
  419. in += AES_BLOCK_SIZE;
  420. if (blocks || enc_after)
  421. __aes_arm64_encrypt(ctx->key_enc, dg, dg,
  422. rounds);
  423. }
  424. }
  425. }
  426. static int mac_update(struct shash_desc *desc, const u8 *p, unsigned int len)
  427. {
  428. struct mac_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
  429. struct mac_desc_ctx *ctx = shash_desc_ctx(desc);
  430. while (len > 0) {
  431. unsigned int l;
  432. if ((ctx->len % AES_BLOCK_SIZE) == 0 &&
  433. (ctx->len + len) > AES_BLOCK_SIZE) {
  434. int blocks = len / AES_BLOCK_SIZE;
  435. len %= AES_BLOCK_SIZE;
  436. mac_do_update(&tctx->key, p, blocks, ctx->dg,
  437. (ctx->len != 0), (len != 0));
  438. p += blocks * AES_BLOCK_SIZE;
  439. if (!len) {
  440. ctx->len = AES_BLOCK_SIZE;
  441. break;
  442. }
  443. ctx->len = 0;
  444. }
  445. l = min(len, AES_BLOCK_SIZE - ctx->len);
  446. if (l <= AES_BLOCK_SIZE) {
  447. crypto_xor(ctx->dg + ctx->len, p, l);
  448. ctx->len += l;
  449. len -= l;
  450. p += l;
  451. }
  452. }
  453. return 0;
  454. }
  455. static int cbcmac_final(struct shash_desc *desc, u8 *out)
  456. {
  457. struct mac_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
  458. struct mac_desc_ctx *ctx = shash_desc_ctx(desc);
  459. mac_do_update(&tctx->key, NULL, 0, ctx->dg, 1, 0);
  460. memcpy(out, ctx->dg, AES_BLOCK_SIZE);
  461. return 0;
  462. }
  463. static int cmac_final(struct shash_desc *desc, u8 *out)
  464. {
  465. struct mac_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
  466. struct mac_desc_ctx *ctx = shash_desc_ctx(desc);
  467. u8 *consts = tctx->consts;
  468. if (ctx->len != AES_BLOCK_SIZE) {
  469. ctx->dg[ctx->len] ^= 0x80;
  470. consts += AES_BLOCK_SIZE;
  471. }
  472. mac_do_update(&tctx->key, consts, 1, ctx->dg, 0, 1);
  473. memcpy(out, ctx->dg, AES_BLOCK_SIZE);
  474. return 0;
  475. }
  476. static struct shash_alg mac_algs[] = { {
  477. .base.cra_name = "cmac(aes)",
  478. .base.cra_driver_name = "cmac-aes-" MODE,
  479. .base.cra_priority = PRIO,
  480. .base.cra_blocksize = AES_BLOCK_SIZE,
  481. .base.cra_ctxsize = sizeof(struct mac_tfm_ctx) +
  482. 2 * AES_BLOCK_SIZE,
  483. .base.cra_module = THIS_MODULE,
  484. .digestsize = AES_BLOCK_SIZE,
  485. .init = mac_init,
  486. .update = mac_update,
  487. .final = cmac_final,
  488. .setkey = cmac_setkey,
  489. .descsize = sizeof(struct mac_desc_ctx),
  490. }, {
  491. .base.cra_name = "xcbc(aes)",
  492. .base.cra_driver_name = "xcbc-aes-" MODE,
  493. .base.cra_priority = PRIO,
  494. .base.cra_blocksize = AES_BLOCK_SIZE,
  495. .base.cra_ctxsize = sizeof(struct mac_tfm_ctx) +
  496. 2 * AES_BLOCK_SIZE,
  497. .base.cra_module = THIS_MODULE,
  498. .digestsize = AES_BLOCK_SIZE,
  499. .init = mac_init,
  500. .update = mac_update,
  501. .final = cmac_final,
  502. .setkey = xcbc_setkey,
  503. .descsize = sizeof(struct mac_desc_ctx),
  504. }, {
  505. .base.cra_name = "cbcmac(aes)",
  506. .base.cra_driver_name = "cbcmac-aes-" MODE,
  507. .base.cra_priority = PRIO,
  508. .base.cra_blocksize = 1,
  509. .base.cra_ctxsize = sizeof(struct mac_tfm_ctx),
  510. .base.cra_module = THIS_MODULE,
  511. .digestsize = AES_BLOCK_SIZE,
  512. .init = mac_init,
  513. .update = mac_update,
  514. .final = cbcmac_final,
  515. .setkey = cbcmac_setkey,
  516. .descsize = sizeof(struct mac_desc_ctx),
  517. } };
  518. static struct simd_skcipher_alg *aes_simd_algs[ARRAY_SIZE(aes_algs)];
  519. static void aes_exit(void)
  520. {
  521. int i;
  522. for (i = 0; i < ARRAY_SIZE(aes_simd_algs); i++)
  523. if (aes_simd_algs[i])
  524. simd_skcipher_free(aes_simd_algs[i]);
  525. crypto_unregister_shashes(mac_algs, ARRAY_SIZE(mac_algs));
  526. crypto_unregister_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
  527. }
  528. static int __init aes_init(void)
  529. {
  530. struct simd_skcipher_alg *simd;
  531. const char *basename;
  532. const char *algname;
  533. const char *drvname;
  534. int err;
  535. int i;
  536. err = crypto_register_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
  537. if (err)
  538. return err;
  539. err = crypto_register_shashes(mac_algs, ARRAY_SIZE(mac_algs));
  540. if (err)
  541. goto unregister_ciphers;
  542. for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
  543. if (!(aes_algs[i].base.cra_flags & CRYPTO_ALG_INTERNAL))
  544. continue;
  545. algname = aes_algs[i].base.cra_name + 2;
  546. drvname = aes_algs[i].base.cra_driver_name + 2;
  547. basename = aes_algs[i].base.cra_driver_name;
  548. simd = simd_skcipher_create_compat(algname, drvname, basename);
  549. err = PTR_ERR(simd);
  550. if (IS_ERR(simd))
  551. goto unregister_simds;
  552. aes_simd_algs[i] = simd;
  553. }
  554. return 0;
  555. unregister_simds:
  556. aes_exit();
  557. return err;
  558. unregister_ciphers:
  559. crypto_unregister_skciphers(aes_algs, ARRAY_SIZE(aes_algs));
  560. return err;
  561. }
  562. #ifdef USE_V8_CRYPTO_EXTENSIONS
  563. module_cpu_feature_match(AES, aes_init);
  564. #else
  565. module_init(aes_init);
  566. EXPORT_SYMBOL(neon_aes_ecb_encrypt);
  567. EXPORT_SYMBOL(neon_aes_cbc_encrypt);
  568. #endif
  569. module_exit(aes_exit);