ghash-ce-glue.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Accelerated GHASH implementation with ARMv8 vmull.p64 instructions.
  4. *
  5. * Copyright (C) 2015 - 2018 Linaro Ltd.
  6. * Copyright (C) 2023 Google LLC.
  7. */
  8. #include <asm/hwcap.h>
  9. #include <asm/neon.h>
  10. #include <asm/simd.h>
  11. #include <linux/unaligned.h>
  12. #include <crypto/aes.h>
  13. #include <crypto/gcm.h>
  14. #include <crypto/b128ops.h>
  15. #include <crypto/cryptd.h>
  16. #include <crypto/internal/aead.h>
  17. #include <crypto/internal/hash.h>
  18. #include <crypto/internal/simd.h>
  19. #include <crypto/internal/skcipher.h>
  20. #include <crypto/gf128mul.h>
  21. #include <crypto/scatterwalk.h>
  22. #include <linux/cpufeature.h>
  23. #include <linux/crypto.h>
  24. #include <linux/jump_label.h>
  25. #include <linux/module.h>
  26. MODULE_DESCRIPTION("GHASH hash function using ARMv8 Crypto Extensions");
  27. MODULE_AUTHOR("Ard Biesheuvel <ardb@kernel.org>");
  28. MODULE_LICENSE("GPL");
  29. MODULE_ALIAS_CRYPTO("ghash");
  30. MODULE_ALIAS_CRYPTO("gcm(aes)");
  31. MODULE_ALIAS_CRYPTO("rfc4106(gcm(aes))");
  32. #define GHASH_BLOCK_SIZE 16
  33. #define GHASH_DIGEST_SIZE 16
  34. #define RFC4106_NONCE_SIZE 4
  35. struct ghash_key {
  36. be128 k;
  37. u64 h[][2];
  38. };
  39. struct gcm_key {
  40. u64 h[4][2];
  41. u32 rk[AES_MAX_KEYLENGTH_U32];
  42. int rounds;
  43. u8 nonce[]; // for RFC4106 nonce
  44. };
  45. struct ghash_desc_ctx {
  46. u64 digest[GHASH_DIGEST_SIZE/sizeof(u64)];
  47. u8 buf[GHASH_BLOCK_SIZE];
  48. u32 count;
  49. };
  50. struct ghash_async_ctx {
  51. struct cryptd_ahash *cryptd_tfm;
  52. };
  53. asmlinkage void pmull_ghash_update_p64(int blocks, u64 dg[], const char *src,
  54. u64 const h[][2], const char *head);
  55. asmlinkage void pmull_ghash_update_p8(int blocks, u64 dg[], const char *src,
  56. u64 const h[][2], const char *head);
  57. static __ro_after_init DEFINE_STATIC_KEY_FALSE(use_p64);
  58. static int ghash_init(struct shash_desc *desc)
  59. {
  60. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  61. *ctx = (struct ghash_desc_ctx){};
  62. return 0;
  63. }
  64. static void ghash_do_update(int blocks, u64 dg[], const char *src,
  65. struct ghash_key *key, const char *head)
  66. {
  67. if (likely(crypto_simd_usable())) {
  68. kernel_neon_begin();
  69. if (static_branch_likely(&use_p64))
  70. pmull_ghash_update_p64(blocks, dg, src, key->h, head);
  71. else
  72. pmull_ghash_update_p8(blocks, dg, src, key->h, head);
  73. kernel_neon_end();
  74. } else {
  75. be128 dst = { cpu_to_be64(dg[1]), cpu_to_be64(dg[0]) };
  76. do {
  77. const u8 *in = src;
  78. if (head) {
  79. in = head;
  80. blocks++;
  81. head = NULL;
  82. } else {
  83. src += GHASH_BLOCK_SIZE;
  84. }
  85. crypto_xor((u8 *)&dst, in, GHASH_BLOCK_SIZE);
  86. gf128mul_lle(&dst, &key->k);
  87. } while (--blocks);
  88. dg[0] = be64_to_cpu(dst.b);
  89. dg[1] = be64_to_cpu(dst.a);
  90. }
  91. }
  92. static int ghash_update(struct shash_desc *desc, const u8 *src,
  93. unsigned int len)
  94. {
  95. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  96. unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
  97. ctx->count += len;
  98. if ((partial + len) >= GHASH_BLOCK_SIZE) {
  99. struct ghash_key *key = crypto_shash_ctx(desc->tfm);
  100. int blocks;
  101. if (partial) {
  102. int p = GHASH_BLOCK_SIZE - partial;
  103. memcpy(ctx->buf + partial, src, p);
  104. src += p;
  105. len -= p;
  106. }
  107. blocks = len / GHASH_BLOCK_SIZE;
  108. len %= GHASH_BLOCK_SIZE;
  109. ghash_do_update(blocks, ctx->digest, src, key,
  110. partial ? ctx->buf : NULL);
  111. src += blocks * GHASH_BLOCK_SIZE;
  112. partial = 0;
  113. }
  114. if (len)
  115. memcpy(ctx->buf + partial, src, len);
  116. return 0;
  117. }
  118. static int ghash_final(struct shash_desc *desc, u8 *dst)
  119. {
  120. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  121. unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
  122. if (partial) {
  123. struct ghash_key *key = crypto_shash_ctx(desc->tfm);
  124. memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
  125. ghash_do_update(1, ctx->digest, ctx->buf, key, NULL);
  126. }
  127. put_unaligned_be64(ctx->digest[1], dst);
  128. put_unaligned_be64(ctx->digest[0], dst + 8);
  129. *ctx = (struct ghash_desc_ctx){};
  130. return 0;
  131. }
  132. static void ghash_reflect(u64 h[], const be128 *k)
  133. {
  134. u64 carry = be64_to_cpu(k->a) >> 63;
  135. h[0] = (be64_to_cpu(k->b) << 1) | carry;
  136. h[1] = (be64_to_cpu(k->a) << 1) | (be64_to_cpu(k->b) >> 63);
  137. if (carry)
  138. h[1] ^= 0xc200000000000000UL;
  139. }
  140. static int ghash_setkey(struct crypto_shash *tfm,
  141. const u8 *inkey, unsigned int keylen)
  142. {
  143. struct ghash_key *key = crypto_shash_ctx(tfm);
  144. if (keylen != GHASH_BLOCK_SIZE)
  145. return -EINVAL;
  146. /* needed for the fallback */
  147. memcpy(&key->k, inkey, GHASH_BLOCK_SIZE);
  148. ghash_reflect(key->h[0], &key->k);
  149. if (static_branch_likely(&use_p64)) {
  150. be128 h = key->k;
  151. gf128mul_lle(&h, &key->k);
  152. ghash_reflect(key->h[1], &h);
  153. gf128mul_lle(&h, &key->k);
  154. ghash_reflect(key->h[2], &h);
  155. gf128mul_lle(&h, &key->k);
  156. ghash_reflect(key->h[3], &h);
  157. }
  158. return 0;
  159. }
  160. static struct shash_alg ghash_alg = {
  161. .digestsize = GHASH_DIGEST_SIZE,
  162. .init = ghash_init,
  163. .update = ghash_update,
  164. .final = ghash_final,
  165. .setkey = ghash_setkey,
  166. .descsize = sizeof(struct ghash_desc_ctx),
  167. .base.cra_name = "ghash",
  168. .base.cra_driver_name = "ghash-ce-sync",
  169. .base.cra_priority = 300 - 1,
  170. .base.cra_blocksize = GHASH_BLOCK_SIZE,
  171. .base.cra_ctxsize = sizeof(struct ghash_key) + sizeof(u64[2]),
  172. .base.cra_module = THIS_MODULE,
  173. };
  174. static int ghash_async_init(struct ahash_request *req)
  175. {
  176. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  177. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  178. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  179. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  180. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  181. struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
  182. desc->tfm = child;
  183. return crypto_shash_init(desc);
  184. }
  185. static int ghash_async_update(struct ahash_request *req)
  186. {
  187. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  188. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  189. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  190. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  191. if (!crypto_simd_usable() ||
  192. (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
  193. memcpy(cryptd_req, req, sizeof(*req));
  194. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  195. return crypto_ahash_update(cryptd_req);
  196. } else {
  197. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  198. return shash_ahash_update(req, desc);
  199. }
  200. }
  201. static int ghash_async_final(struct ahash_request *req)
  202. {
  203. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  204. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  205. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  206. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  207. if (!crypto_simd_usable() ||
  208. (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
  209. memcpy(cryptd_req, req, sizeof(*req));
  210. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  211. return crypto_ahash_final(cryptd_req);
  212. } else {
  213. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  214. return crypto_shash_final(desc, req->result);
  215. }
  216. }
  217. static int ghash_async_digest(struct ahash_request *req)
  218. {
  219. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  220. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  221. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  222. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  223. if (!crypto_simd_usable() ||
  224. (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
  225. memcpy(cryptd_req, req, sizeof(*req));
  226. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  227. return crypto_ahash_digest(cryptd_req);
  228. } else {
  229. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  230. struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
  231. desc->tfm = child;
  232. return shash_ahash_digest(req, desc);
  233. }
  234. }
  235. static int ghash_async_import(struct ahash_request *req, const void *in)
  236. {
  237. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  238. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  239. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  240. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  241. desc->tfm = cryptd_ahash_child(ctx->cryptd_tfm);
  242. return crypto_shash_import(desc, in);
  243. }
  244. static int ghash_async_export(struct ahash_request *req, void *out)
  245. {
  246. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  247. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  248. return crypto_shash_export(desc, out);
  249. }
  250. static int ghash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
  251. unsigned int keylen)
  252. {
  253. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  254. struct crypto_ahash *child = &ctx->cryptd_tfm->base;
  255. crypto_ahash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  256. crypto_ahash_set_flags(child, crypto_ahash_get_flags(tfm)
  257. & CRYPTO_TFM_REQ_MASK);
  258. return crypto_ahash_setkey(child, key, keylen);
  259. }
  260. static int ghash_async_init_tfm(struct crypto_tfm *tfm)
  261. {
  262. struct cryptd_ahash *cryptd_tfm;
  263. struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
  264. cryptd_tfm = cryptd_alloc_ahash("ghash-ce-sync", 0, 0);
  265. if (IS_ERR(cryptd_tfm))
  266. return PTR_ERR(cryptd_tfm);
  267. ctx->cryptd_tfm = cryptd_tfm;
  268. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  269. sizeof(struct ahash_request) +
  270. crypto_ahash_reqsize(&cryptd_tfm->base));
  271. return 0;
  272. }
  273. static void ghash_async_exit_tfm(struct crypto_tfm *tfm)
  274. {
  275. struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
  276. cryptd_free_ahash(ctx->cryptd_tfm);
  277. }
  278. static struct ahash_alg ghash_async_alg = {
  279. .init = ghash_async_init,
  280. .update = ghash_async_update,
  281. .final = ghash_async_final,
  282. .setkey = ghash_async_setkey,
  283. .digest = ghash_async_digest,
  284. .import = ghash_async_import,
  285. .export = ghash_async_export,
  286. .halg.digestsize = GHASH_DIGEST_SIZE,
  287. .halg.statesize = sizeof(struct ghash_desc_ctx),
  288. .halg.base = {
  289. .cra_name = "ghash",
  290. .cra_driver_name = "ghash-ce",
  291. .cra_priority = 300,
  292. .cra_flags = CRYPTO_ALG_ASYNC,
  293. .cra_blocksize = GHASH_BLOCK_SIZE,
  294. .cra_ctxsize = sizeof(struct ghash_async_ctx),
  295. .cra_module = THIS_MODULE,
  296. .cra_init = ghash_async_init_tfm,
  297. .cra_exit = ghash_async_exit_tfm,
  298. },
  299. };
  300. void pmull_gcm_encrypt(int blocks, u64 dg[], const char *src,
  301. struct gcm_key const *k, char *dst,
  302. const char *iv, int rounds, u32 counter);
  303. void pmull_gcm_enc_final(int blocks, u64 dg[], char *tag,
  304. struct gcm_key const *k, char *head,
  305. const char *iv, int rounds, u32 counter);
  306. void pmull_gcm_decrypt(int bytes, u64 dg[], const char *src,
  307. struct gcm_key const *k, char *dst,
  308. const char *iv, int rounds, u32 counter);
  309. int pmull_gcm_dec_final(int bytes, u64 dg[], char *tag,
  310. struct gcm_key const *k, char *head,
  311. const char *iv, int rounds, u32 counter,
  312. const char *otag, int authsize);
  313. static int gcm_aes_setkey(struct crypto_aead *tfm, const u8 *inkey,
  314. unsigned int keylen)
  315. {
  316. struct gcm_key *ctx = crypto_aead_ctx(tfm);
  317. struct crypto_aes_ctx aes_ctx;
  318. be128 h, k;
  319. int ret;
  320. ret = aes_expandkey(&aes_ctx, inkey, keylen);
  321. if (ret)
  322. return -EINVAL;
  323. aes_encrypt(&aes_ctx, (u8 *)&k, (u8[AES_BLOCK_SIZE]){});
  324. memcpy(ctx->rk, aes_ctx.key_enc, sizeof(ctx->rk));
  325. ctx->rounds = 6 + keylen / 4;
  326. memzero_explicit(&aes_ctx, sizeof(aes_ctx));
  327. ghash_reflect(ctx->h[0], &k);
  328. h = k;
  329. gf128mul_lle(&h, &k);
  330. ghash_reflect(ctx->h[1], &h);
  331. gf128mul_lle(&h, &k);
  332. ghash_reflect(ctx->h[2], &h);
  333. gf128mul_lle(&h, &k);
  334. ghash_reflect(ctx->h[3], &h);
  335. return 0;
  336. }
  337. static int gcm_aes_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  338. {
  339. return crypto_gcm_check_authsize(authsize);
  340. }
  341. static void gcm_update_mac(u64 dg[], const u8 *src, int count, u8 buf[],
  342. int *buf_count, struct gcm_key *ctx)
  343. {
  344. if (*buf_count > 0) {
  345. int buf_added = min(count, GHASH_BLOCK_SIZE - *buf_count);
  346. memcpy(&buf[*buf_count], src, buf_added);
  347. *buf_count += buf_added;
  348. src += buf_added;
  349. count -= buf_added;
  350. }
  351. if (count >= GHASH_BLOCK_SIZE || *buf_count == GHASH_BLOCK_SIZE) {
  352. int blocks = count / GHASH_BLOCK_SIZE;
  353. pmull_ghash_update_p64(blocks, dg, src, ctx->h,
  354. *buf_count ? buf : NULL);
  355. src += blocks * GHASH_BLOCK_SIZE;
  356. count %= GHASH_BLOCK_SIZE;
  357. *buf_count = 0;
  358. }
  359. if (count > 0) {
  360. memcpy(buf, src, count);
  361. *buf_count = count;
  362. }
  363. }
  364. static void gcm_calculate_auth_mac(struct aead_request *req, u64 dg[], u32 len)
  365. {
  366. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  367. struct gcm_key *ctx = crypto_aead_ctx(aead);
  368. u8 buf[GHASH_BLOCK_SIZE];
  369. struct scatter_walk walk;
  370. int buf_count = 0;
  371. scatterwalk_start(&walk, req->src);
  372. do {
  373. u32 n = scatterwalk_clamp(&walk, len);
  374. u8 *p;
  375. if (!n) {
  376. scatterwalk_start(&walk, sg_next(walk.sg));
  377. n = scatterwalk_clamp(&walk, len);
  378. }
  379. p = scatterwalk_map(&walk);
  380. gcm_update_mac(dg, p, n, buf, &buf_count, ctx);
  381. scatterwalk_unmap(p);
  382. if (unlikely(len / SZ_4K > (len - n) / SZ_4K)) {
  383. kernel_neon_end();
  384. kernel_neon_begin();
  385. }
  386. len -= n;
  387. scatterwalk_advance(&walk, n);
  388. scatterwalk_done(&walk, 0, len);
  389. } while (len);
  390. if (buf_count) {
  391. memset(&buf[buf_count], 0, GHASH_BLOCK_SIZE - buf_count);
  392. pmull_ghash_update_p64(1, dg, buf, ctx->h, NULL);
  393. }
  394. }
  395. static int gcm_encrypt(struct aead_request *req, const u8 *iv, u32 assoclen)
  396. {
  397. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  398. struct gcm_key *ctx = crypto_aead_ctx(aead);
  399. struct skcipher_walk walk;
  400. u8 buf[AES_BLOCK_SIZE];
  401. u32 counter = 2;
  402. u64 dg[2] = {};
  403. be128 lengths;
  404. const u8 *src;
  405. u8 *tag, *dst;
  406. int tail, err;
  407. if (WARN_ON_ONCE(!may_use_simd()))
  408. return -EBUSY;
  409. err = skcipher_walk_aead_encrypt(&walk, req, false);
  410. kernel_neon_begin();
  411. if (assoclen)
  412. gcm_calculate_auth_mac(req, dg, assoclen);
  413. src = walk.src.virt.addr;
  414. dst = walk.dst.virt.addr;
  415. while (walk.nbytes >= AES_BLOCK_SIZE) {
  416. int nblocks = walk.nbytes / AES_BLOCK_SIZE;
  417. pmull_gcm_encrypt(nblocks, dg, src, ctx, dst, iv,
  418. ctx->rounds, counter);
  419. counter += nblocks;
  420. if (walk.nbytes == walk.total) {
  421. src += nblocks * AES_BLOCK_SIZE;
  422. dst += nblocks * AES_BLOCK_SIZE;
  423. break;
  424. }
  425. kernel_neon_end();
  426. err = skcipher_walk_done(&walk,
  427. walk.nbytes % AES_BLOCK_SIZE);
  428. if (err)
  429. return err;
  430. src = walk.src.virt.addr;
  431. dst = walk.dst.virt.addr;
  432. kernel_neon_begin();
  433. }
  434. lengths.a = cpu_to_be64(assoclen * 8);
  435. lengths.b = cpu_to_be64(req->cryptlen * 8);
  436. tag = (u8 *)&lengths;
  437. tail = walk.nbytes % AES_BLOCK_SIZE;
  438. /*
  439. * Bounce via a buffer unless we are encrypting in place and src/dst
  440. * are not pointing to the start of the walk buffer. In that case, we
  441. * can do a NEON load/xor/store sequence in place as long as we move
  442. * the plain/ciphertext and keystream to the start of the register. If
  443. * not, do a memcpy() to the end of the buffer so we can reuse the same
  444. * logic.
  445. */
  446. if (unlikely(tail && (tail == walk.nbytes || src != dst)))
  447. src = memcpy(buf + sizeof(buf) - tail, src, tail);
  448. pmull_gcm_enc_final(tail, dg, tag, ctx, (u8 *)src, iv,
  449. ctx->rounds, counter);
  450. kernel_neon_end();
  451. if (unlikely(tail && src != dst))
  452. memcpy(dst, src, tail);
  453. if (walk.nbytes) {
  454. err = skcipher_walk_done(&walk, 0);
  455. if (err)
  456. return err;
  457. }
  458. /* copy authtag to end of dst */
  459. scatterwalk_map_and_copy(tag, req->dst, req->assoclen + req->cryptlen,
  460. crypto_aead_authsize(aead), 1);
  461. return 0;
  462. }
  463. static int gcm_decrypt(struct aead_request *req, const u8 *iv, u32 assoclen)
  464. {
  465. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  466. struct gcm_key *ctx = crypto_aead_ctx(aead);
  467. int authsize = crypto_aead_authsize(aead);
  468. struct skcipher_walk walk;
  469. u8 otag[AES_BLOCK_SIZE];
  470. u8 buf[AES_BLOCK_SIZE];
  471. u32 counter = 2;
  472. u64 dg[2] = {};
  473. be128 lengths;
  474. const u8 *src;
  475. u8 *tag, *dst;
  476. int tail, err, ret;
  477. if (WARN_ON_ONCE(!may_use_simd()))
  478. return -EBUSY;
  479. scatterwalk_map_and_copy(otag, req->src,
  480. req->assoclen + req->cryptlen - authsize,
  481. authsize, 0);
  482. err = skcipher_walk_aead_decrypt(&walk, req, false);
  483. kernel_neon_begin();
  484. if (assoclen)
  485. gcm_calculate_auth_mac(req, dg, assoclen);
  486. src = walk.src.virt.addr;
  487. dst = walk.dst.virt.addr;
  488. while (walk.nbytes >= AES_BLOCK_SIZE) {
  489. int nblocks = walk.nbytes / AES_BLOCK_SIZE;
  490. pmull_gcm_decrypt(nblocks, dg, src, ctx, dst, iv,
  491. ctx->rounds, counter);
  492. counter += nblocks;
  493. if (walk.nbytes == walk.total) {
  494. src += nblocks * AES_BLOCK_SIZE;
  495. dst += nblocks * AES_BLOCK_SIZE;
  496. break;
  497. }
  498. kernel_neon_end();
  499. err = skcipher_walk_done(&walk,
  500. walk.nbytes % AES_BLOCK_SIZE);
  501. if (err)
  502. return err;
  503. src = walk.src.virt.addr;
  504. dst = walk.dst.virt.addr;
  505. kernel_neon_begin();
  506. }
  507. lengths.a = cpu_to_be64(assoclen * 8);
  508. lengths.b = cpu_to_be64((req->cryptlen - authsize) * 8);
  509. tag = (u8 *)&lengths;
  510. tail = walk.nbytes % AES_BLOCK_SIZE;
  511. if (unlikely(tail && (tail == walk.nbytes || src != dst)))
  512. src = memcpy(buf + sizeof(buf) - tail, src, tail);
  513. ret = pmull_gcm_dec_final(tail, dg, tag, ctx, (u8 *)src, iv,
  514. ctx->rounds, counter, otag, authsize);
  515. kernel_neon_end();
  516. if (unlikely(tail && src != dst))
  517. memcpy(dst, src, tail);
  518. if (walk.nbytes) {
  519. err = skcipher_walk_done(&walk, 0);
  520. if (err)
  521. return err;
  522. }
  523. return ret ? -EBADMSG : 0;
  524. }
  525. static int gcm_aes_encrypt(struct aead_request *req)
  526. {
  527. return gcm_encrypt(req, req->iv, req->assoclen);
  528. }
  529. static int gcm_aes_decrypt(struct aead_request *req)
  530. {
  531. return gcm_decrypt(req, req->iv, req->assoclen);
  532. }
  533. static int rfc4106_setkey(struct crypto_aead *tfm, const u8 *inkey,
  534. unsigned int keylen)
  535. {
  536. struct gcm_key *ctx = crypto_aead_ctx(tfm);
  537. int err;
  538. keylen -= RFC4106_NONCE_SIZE;
  539. err = gcm_aes_setkey(tfm, inkey, keylen);
  540. if (err)
  541. return err;
  542. memcpy(ctx->nonce, inkey + keylen, RFC4106_NONCE_SIZE);
  543. return 0;
  544. }
  545. static int rfc4106_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  546. {
  547. return crypto_rfc4106_check_authsize(authsize);
  548. }
  549. static int rfc4106_encrypt(struct aead_request *req)
  550. {
  551. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  552. struct gcm_key *ctx = crypto_aead_ctx(aead);
  553. u8 iv[GCM_AES_IV_SIZE];
  554. memcpy(iv, ctx->nonce, RFC4106_NONCE_SIZE);
  555. memcpy(iv + RFC4106_NONCE_SIZE, req->iv, GCM_RFC4106_IV_SIZE);
  556. return crypto_ipsec_check_assoclen(req->assoclen) ?:
  557. gcm_encrypt(req, iv, req->assoclen - GCM_RFC4106_IV_SIZE);
  558. }
  559. static int rfc4106_decrypt(struct aead_request *req)
  560. {
  561. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  562. struct gcm_key *ctx = crypto_aead_ctx(aead);
  563. u8 iv[GCM_AES_IV_SIZE];
  564. memcpy(iv, ctx->nonce, RFC4106_NONCE_SIZE);
  565. memcpy(iv + RFC4106_NONCE_SIZE, req->iv, GCM_RFC4106_IV_SIZE);
  566. return crypto_ipsec_check_assoclen(req->assoclen) ?:
  567. gcm_decrypt(req, iv, req->assoclen - GCM_RFC4106_IV_SIZE);
  568. }
  569. static struct aead_alg gcm_aes_algs[] = {{
  570. .ivsize = GCM_AES_IV_SIZE,
  571. .chunksize = AES_BLOCK_SIZE,
  572. .maxauthsize = AES_BLOCK_SIZE,
  573. .setkey = gcm_aes_setkey,
  574. .setauthsize = gcm_aes_setauthsize,
  575. .encrypt = gcm_aes_encrypt,
  576. .decrypt = gcm_aes_decrypt,
  577. .base.cra_name = "gcm(aes)",
  578. .base.cra_driver_name = "gcm-aes-ce",
  579. .base.cra_priority = 400,
  580. .base.cra_blocksize = 1,
  581. .base.cra_ctxsize = sizeof(struct gcm_key),
  582. .base.cra_module = THIS_MODULE,
  583. }, {
  584. .ivsize = GCM_RFC4106_IV_SIZE,
  585. .chunksize = AES_BLOCK_SIZE,
  586. .maxauthsize = AES_BLOCK_SIZE,
  587. .setkey = rfc4106_setkey,
  588. .setauthsize = rfc4106_setauthsize,
  589. .encrypt = rfc4106_encrypt,
  590. .decrypt = rfc4106_decrypt,
  591. .base.cra_name = "rfc4106(gcm(aes))",
  592. .base.cra_driver_name = "rfc4106-gcm-aes-ce",
  593. .base.cra_priority = 400,
  594. .base.cra_blocksize = 1,
  595. .base.cra_ctxsize = sizeof(struct gcm_key) + RFC4106_NONCE_SIZE,
  596. .base.cra_module = THIS_MODULE,
  597. }};
  598. static int __init ghash_ce_mod_init(void)
  599. {
  600. int err;
  601. if (!(elf_hwcap & HWCAP_NEON))
  602. return -ENODEV;
  603. if (elf_hwcap2 & HWCAP2_PMULL) {
  604. err = crypto_register_aeads(gcm_aes_algs,
  605. ARRAY_SIZE(gcm_aes_algs));
  606. if (err)
  607. return err;
  608. ghash_alg.base.cra_ctxsize += 3 * sizeof(u64[2]);
  609. static_branch_enable(&use_p64);
  610. }
  611. err = crypto_register_shash(&ghash_alg);
  612. if (err)
  613. goto err_aead;
  614. err = crypto_register_ahash(&ghash_async_alg);
  615. if (err)
  616. goto err_shash;
  617. return 0;
  618. err_shash:
  619. crypto_unregister_shash(&ghash_alg);
  620. err_aead:
  621. if (elf_hwcap2 & HWCAP2_PMULL)
  622. crypto_unregister_aeads(gcm_aes_algs,
  623. ARRAY_SIZE(gcm_aes_algs));
  624. return err;
  625. }
  626. static void __exit ghash_ce_mod_exit(void)
  627. {
  628. crypto_unregister_ahash(&ghash_async_alg);
  629. crypto_unregister_shash(&ghash_alg);
  630. if (elf_hwcap2 & HWCAP2_PMULL)
  631. crypto_unregister_aeads(gcm_aes_algs,
  632. ARRAY_SIZE(gcm_aes_algs));
  633. }
  634. module_init(ghash_ce_mod_init);
  635. module_exit(ghash_ce_mod_exit);