ghash-ce-glue.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * Accelerated GHASH implementation with ARMv8 PMULL instructions.
  3. *
  4. * Copyright (C) 2014 - 2018 Linaro Ltd. <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <asm/neon.h>
  11. #include <asm/simd.h>
  12. #include <asm/unaligned.h>
  13. #include <crypto/aes.h>
  14. #include <crypto/algapi.h>
  15. #include <crypto/b128ops.h>
  16. #include <crypto/gf128mul.h>
  17. #include <crypto/internal/aead.h>
  18. #include <crypto/internal/hash.h>
  19. #include <crypto/internal/skcipher.h>
  20. #include <crypto/scatterwalk.h>
  21. #include <linux/cpufeature.h>
  22. #include <linux/crypto.h>
  23. #include <linux/module.h>
  24. MODULE_DESCRIPTION("GHASH and AES-GCM using ARMv8 Crypto Extensions");
  25. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  26. MODULE_LICENSE("GPL v2");
  27. MODULE_ALIAS_CRYPTO("ghash");
  28. #define GHASH_BLOCK_SIZE 16
  29. #define GHASH_DIGEST_SIZE 16
  30. #define GCM_IV_SIZE 12
  31. struct ghash_key {
  32. u64 h[2];
  33. u64 h2[2];
  34. u64 h3[2];
  35. u64 h4[2];
  36. be128 k;
  37. };
  38. struct ghash_desc_ctx {
  39. u64 digest[GHASH_DIGEST_SIZE/sizeof(u64)];
  40. u8 buf[GHASH_BLOCK_SIZE];
  41. u32 count;
  42. };
  43. struct gcm_aes_ctx {
  44. struct crypto_aes_ctx aes_key;
  45. struct ghash_key ghash_key;
  46. };
  47. asmlinkage void pmull_ghash_update_p64(int blocks, u64 dg[], const char *src,
  48. struct ghash_key const *k,
  49. const char *head);
  50. asmlinkage void pmull_ghash_update_p8(int blocks, u64 dg[], const char *src,
  51. struct ghash_key const *k,
  52. const char *head);
  53. static void (*pmull_ghash_update)(int blocks, u64 dg[], const char *src,
  54. struct ghash_key const *k,
  55. const char *head);
  56. asmlinkage void pmull_gcm_encrypt(int blocks, u64 dg[], u8 dst[],
  57. const u8 src[], struct ghash_key const *k,
  58. u8 ctr[], u32 const rk[], int rounds,
  59. u8 ks[]);
  60. asmlinkage void pmull_gcm_decrypt(int blocks, u64 dg[], u8 dst[],
  61. const u8 src[], struct ghash_key const *k,
  62. u8 ctr[], u32 const rk[], int rounds);
  63. asmlinkage void pmull_gcm_encrypt_block(u8 dst[], u8 const src[],
  64. u32 const rk[], int rounds);
  65. asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
  66. static int ghash_init(struct shash_desc *desc)
  67. {
  68. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  69. *ctx = (struct ghash_desc_ctx){};
  70. return 0;
  71. }
  72. static void ghash_do_update(int blocks, u64 dg[], const char *src,
  73. struct ghash_key *key, const char *head)
  74. {
  75. if (likely(may_use_simd())) {
  76. kernel_neon_begin();
  77. pmull_ghash_update(blocks, dg, src, key, head);
  78. kernel_neon_end();
  79. } else {
  80. be128 dst = { cpu_to_be64(dg[1]), cpu_to_be64(dg[0]) };
  81. do {
  82. const u8 *in = src;
  83. if (head) {
  84. in = head;
  85. blocks++;
  86. head = NULL;
  87. } else {
  88. src += GHASH_BLOCK_SIZE;
  89. }
  90. crypto_xor((u8 *)&dst, in, GHASH_BLOCK_SIZE);
  91. gf128mul_lle(&dst, &key->k);
  92. } while (--blocks);
  93. dg[0] = be64_to_cpu(dst.b);
  94. dg[1] = be64_to_cpu(dst.a);
  95. }
  96. }
  97. /* avoid hogging the CPU for too long */
  98. #define MAX_BLOCKS (SZ_64K / GHASH_BLOCK_SIZE)
  99. static int ghash_update(struct shash_desc *desc, const u8 *src,
  100. unsigned int len)
  101. {
  102. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  103. unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
  104. ctx->count += len;
  105. if ((partial + len) >= GHASH_BLOCK_SIZE) {
  106. struct ghash_key *key = crypto_shash_ctx(desc->tfm);
  107. int blocks;
  108. if (partial) {
  109. int p = GHASH_BLOCK_SIZE - partial;
  110. memcpy(ctx->buf + partial, src, p);
  111. src += p;
  112. len -= p;
  113. }
  114. blocks = len / GHASH_BLOCK_SIZE;
  115. len %= GHASH_BLOCK_SIZE;
  116. do {
  117. int chunk = min(blocks, MAX_BLOCKS);
  118. ghash_do_update(chunk, ctx->digest, src, key,
  119. partial ? ctx->buf : NULL);
  120. blocks -= chunk;
  121. src += chunk * GHASH_BLOCK_SIZE;
  122. partial = 0;
  123. } while (unlikely(blocks > 0));
  124. }
  125. if (len)
  126. memcpy(ctx->buf + partial, src, len);
  127. return 0;
  128. }
  129. static int ghash_final(struct shash_desc *desc, u8 *dst)
  130. {
  131. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  132. unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
  133. if (partial) {
  134. struct ghash_key *key = crypto_shash_ctx(desc->tfm);
  135. memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
  136. ghash_do_update(1, ctx->digest, ctx->buf, key, NULL);
  137. }
  138. put_unaligned_be64(ctx->digest[1], dst);
  139. put_unaligned_be64(ctx->digest[0], dst + 8);
  140. *ctx = (struct ghash_desc_ctx){};
  141. return 0;
  142. }
  143. static void ghash_reflect(u64 h[], const be128 *k)
  144. {
  145. u64 carry = be64_to_cpu(k->a) & BIT(63) ? 1 : 0;
  146. h[0] = (be64_to_cpu(k->b) << 1) | carry;
  147. h[1] = (be64_to_cpu(k->a) << 1) | (be64_to_cpu(k->b) >> 63);
  148. if (carry)
  149. h[1] ^= 0xc200000000000000UL;
  150. }
  151. static int __ghash_setkey(struct ghash_key *key,
  152. const u8 *inkey, unsigned int keylen)
  153. {
  154. be128 h;
  155. /* needed for the fallback */
  156. memcpy(&key->k, inkey, GHASH_BLOCK_SIZE);
  157. ghash_reflect(key->h, &key->k);
  158. h = key->k;
  159. gf128mul_lle(&h, &key->k);
  160. ghash_reflect(key->h2, &h);
  161. gf128mul_lle(&h, &key->k);
  162. ghash_reflect(key->h3, &h);
  163. gf128mul_lle(&h, &key->k);
  164. ghash_reflect(key->h4, &h);
  165. return 0;
  166. }
  167. static int ghash_setkey(struct crypto_shash *tfm,
  168. const u8 *inkey, unsigned int keylen)
  169. {
  170. struct ghash_key *key = crypto_shash_ctx(tfm);
  171. if (keylen != GHASH_BLOCK_SIZE) {
  172. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  173. return -EINVAL;
  174. }
  175. return __ghash_setkey(key, inkey, keylen);
  176. }
  177. static struct shash_alg ghash_alg = {
  178. .base.cra_name = "ghash",
  179. .base.cra_driver_name = "ghash-ce",
  180. .base.cra_priority = 200,
  181. .base.cra_blocksize = GHASH_BLOCK_SIZE,
  182. .base.cra_ctxsize = sizeof(struct ghash_key),
  183. .base.cra_module = THIS_MODULE,
  184. .digestsize = GHASH_DIGEST_SIZE,
  185. .init = ghash_init,
  186. .update = ghash_update,
  187. .final = ghash_final,
  188. .setkey = ghash_setkey,
  189. .descsize = sizeof(struct ghash_desc_ctx),
  190. };
  191. static int num_rounds(struct crypto_aes_ctx *ctx)
  192. {
  193. /*
  194. * # of rounds specified by AES:
  195. * 128 bit key 10 rounds
  196. * 192 bit key 12 rounds
  197. * 256 bit key 14 rounds
  198. * => n byte key => 6 + (n/4) rounds
  199. */
  200. return 6 + ctx->key_length / 4;
  201. }
  202. static int gcm_setkey(struct crypto_aead *tfm, const u8 *inkey,
  203. unsigned int keylen)
  204. {
  205. struct gcm_aes_ctx *ctx = crypto_aead_ctx(tfm);
  206. u8 key[GHASH_BLOCK_SIZE];
  207. int ret;
  208. ret = crypto_aes_expand_key(&ctx->aes_key, inkey, keylen);
  209. if (ret) {
  210. tfm->base.crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  211. return -EINVAL;
  212. }
  213. __aes_arm64_encrypt(ctx->aes_key.key_enc, key, (u8[AES_BLOCK_SIZE]){},
  214. num_rounds(&ctx->aes_key));
  215. return __ghash_setkey(&ctx->ghash_key, key, sizeof(be128));
  216. }
  217. static int gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  218. {
  219. switch (authsize) {
  220. case 4:
  221. case 8:
  222. case 12 ... 16:
  223. break;
  224. default:
  225. return -EINVAL;
  226. }
  227. return 0;
  228. }
  229. static void gcm_update_mac(u64 dg[], const u8 *src, int count, u8 buf[],
  230. int *buf_count, struct gcm_aes_ctx *ctx)
  231. {
  232. if (*buf_count > 0) {
  233. int buf_added = min(count, GHASH_BLOCK_SIZE - *buf_count);
  234. memcpy(&buf[*buf_count], src, buf_added);
  235. *buf_count += buf_added;
  236. src += buf_added;
  237. count -= buf_added;
  238. }
  239. if (count >= GHASH_BLOCK_SIZE || *buf_count == GHASH_BLOCK_SIZE) {
  240. int blocks = count / GHASH_BLOCK_SIZE;
  241. ghash_do_update(blocks, dg, src, &ctx->ghash_key,
  242. *buf_count ? buf : NULL);
  243. src += blocks * GHASH_BLOCK_SIZE;
  244. count %= GHASH_BLOCK_SIZE;
  245. *buf_count = 0;
  246. }
  247. if (count > 0) {
  248. memcpy(buf, src, count);
  249. *buf_count = count;
  250. }
  251. }
  252. static void gcm_calculate_auth_mac(struct aead_request *req, u64 dg[])
  253. {
  254. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  255. struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
  256. u8 buf[GHASH_BLOCK_SIZE];
  257. struct scatter_walk walk;
  258. u32 len = req->assoclen;
  259. int buf_count = 0;
  260. scatterwalk_start(&walk, req->src);
  261. do {
  262. u32 n = scatterwalk_clamp(&walk, len);
  263. u8 *p;
  264. if (!n) {
  265. scatterwalk_start(&walk, sg_next(walk.sg));
  266. n = scatterwalk_clamp(&walk, len);
  267. }
  268. p = scatterwalk_map(&walk);
  269. gcm_update_mac(dg, p, n, buf, &buf_count, ctx);
  270. len -= n;
  271. scatterwalk_unmap(p);
  272. scatterwalk_advance(&walk, n);
  273. scatterwalk_done(&walk, 0, len);
  274. } while (len);
  275. if (buf_count) {
  276. memset(&buf[buf_count], 0, GHASH_BLOCK_SIZE - buf_count);
  277. ghash_do_update(1, dg, buf, &ctx->ghash_key, NULL);
  278. }
  279. }
  280. static void gcm_final(struct aead_request *req, struct gcm_aes_ctx *ctx,
  281. u64 dg[], u8 tag[], int cryptlen)
  282. {
  283. u8 mac[AES_BLOCK_SIZE];
  284. u128 lengths;
  285. lengths.a = cpu_to_be64(req->assoclen * 8);
  286. lengths.b = cpu_to_be64(cryptlen * 8);
  287. ghash_do_update(1, dg, (void *)&lengths, &ctx->ghash_key, NULL);
  288. put_unaligned_be64(dg[1], mac);
  289. put_unaligned_be64(dg[0], mac + 8);
  290. crypto_xor(tag, mac, AES_BLOCK_SIZE);
  291. }
  292. static int gcm_encrypt(struct aead_request *req)
  293. {
  294. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  295. struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
  296. struct skcipher_walk walk;
  297. u8 iv[AES_BLOCK_SIZE];
  298. u8 ks[2 * AES_BLOCK_SIZE];
  299. u8 tag[AES_BLOCK_SIZE];
  300. u64 dg[2] = {};
  301. int nrounds = num_rounds(&ctx->aes_key);
  302. int err;
  303. if (req->assoclen)
  304. gcm_calculate_auth_mac(req, dg);
  305. memcpy(iv, req->iv, GCM_IV_SIZE);
  306. put_unaligned_be32(1, iv + GCM_IV_SIZE);
  307. err = skcipher_walk_aead_encrypt(&walk, req, false);
  308. if (likely(may_use_simd() && walk.total >= 2 * AES_BLOCK_SIZE)) {
  309. u32 const *rk = NULL;
  310. kernel_neon_begin();
  311. pmull_gcm_encrypt_block(tag, iv, ctx->aes_key.key_enc, nrounds);
  312. put_unaligned_be32(2, iv + GCM_IV_SIZE);
  313. pmull_gcm_encrypt_block(ks, iv, NULL, nrounds);
  314. put_unaligned_be32(3, iv + GCM_IV_SIZE);
  315. pmull_gcm_encrypt_block(ks + AES_BLOCK_SIZE, iv, NULL, nrounds);
  316. put_unaligned_be32(4, iv + GCM_IV_SIZE);
  317. do {
  318. int blocks = walk.nbytes / (2 * AES_BLOCK_SIZE) * 2;
  319. if (rk)
  320. kernel_neon_begin();
  321. pmull_gcm_encrypt(blocks, dg, walk.dst.virt.addr,
  322. walk.src.virt.addr, &ctx->ghash_key,
  323. iv, rk, nrounds, ks);
  324. kernel_neon_end();
  325. err = skcipher_walk_done(&walk,
  326. walk.nbytes % (2 * AES_BLOCK_SIZE));
  327. rk = ctx->aes_key.key_enc;
  328. } while (walk.nbytes >= 2 * AES_BLOCK_SIZE);
  329. } else {
  330. __aes_arm64_encrypt(ctx->aes_key.key_enc, tag, iv, nrounds);
  331. put_unaligned_be32(2, iv + GCM_IV_SIZE);
  332. while (walk.nbytes >= (2 * AES_BLOCK_SIZE)) {
  333. const int blocks =
  334. walk.nbytes / (2 * AES_BLOCK_SIZE) * 2;
  335. u8 *dst = walk.dst.virt.addr;
  336. u8 *src = walk.src.virt.addr;
  337. int remaining = blocks;
  338. do {
  339. __aes_arm64_encrypt(ctx->aes_key.key_enc,
  340. ks, iv, nrounds);
  341. crypto_xor_cpy(dst, src, ks, AES_BLOCK_SIZE);
  342. crypto_inc(iv, AES_BLOCK_SIZE);
  343. dst += AES_BLOCK_SIZE;
  344. src += AES_BLOCK_SIZE;
  345. } while (--remaining > 0);
  346. ghash_do_update(blocks, dg,
  347. walk.dst.virt.addr, &ctx->ghash_key,
  348. NULL);
  349. err = skcipher_walk_done(&walk,
  350. walk.nbytes % (2 * AES_BLOCK_SIZE));
  351. }
  352. if (walk.nbytes) {
  353. __aes_arm64_encrypt(ctx->aes_key.key_enc, ks, iv,
  354. nrounds);
  355. if (walk.nbytes > AES_BLOCK_SIZE) {
  356. crypto_inc(iv, AES_BLOCK_SIZE);
  357. __aes_arm64_encrypt(ctx->aes_key.key_enc,
  358. ks + AES_BLOCK_SIZE, iv,
  359. nrounds);
  360. }
  361. }
  362. }
  363. /* handle the tail */
  364. if (walk.nbytes) {
  365. u8 buf[GHASH_BLOCK_SIZE];
  366. unsigned int nbytes = walk.nbytes;
  367. u8 *dst = walk.dst.virt.addr;
  368. u8 *head = NULL;
  369. crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, ks,
  370. walk.nbytes);
  371. if (walk.nbytes > GHASH_BLOCK_SIZE) {
  372. head = dst;
  373. dst += GHASH_BLOCK_SIZE;
  374. nbytes %= GHASH_BLOCK_SIZE;
  375. }
  376. memcpy(buf, dst, nbytes);
  377. memset(buf + nbytes, 0, GHASH_BLOCK_SIZE - nbytes);
  378. ghash_do_update(!!nbytes, dg, buf, &ctx->ghash_key, head);
  379. err = skcipher_walk_done(&walk, 0);
  380. }
  381. if (err)
  382. return err;
  383. gcm_final(req, ctx, dg, tag, req->cryptlen);
  384. /* copy authtag to end of dst */
  385. scatterwalk_map_and_copy(tag, req->dst, req->assoclen + req->cryptlen,
  386. crypto_aead_authsize(aead), 1);
  387. return 0;
  388. }
  389. static int gcm_decrypt(struct aead_request *req)
  390. {
  391. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  392. struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
  393. unsigned int authsize = crypto_aead_authsize(aead);
  394. struct skcipher_walk walk;
  395. u8 iv[2 * AES_BLOCK_SIZE];
  396. u8 tag[AES_BLOCK_SIZE];
  397. u8 buf[2 * GHASH_BLOCK_SIZE];
  398. u64 dg[2] = {};
  399. int nrounds = num_rounds(&ctx->aes_key);
  400. int err;
  401. if (req->assoclen)
  402. gcm_calculate_auth_mac(req, dg);
  403. memcpy(iv, req->iv, GCM_IV_SIZE);
  404. put_unaligned_be32(1, iv + GCM_IV_SIZE);
  405. err = skcipher_walk_aead_decrypt(&walk, req, false);
  406. if (likely(may_use_simd() && walk.total >= 2 * AES_BLOCK_SIZE)) {
  407. u32 const *rk = NULL;
  408. kernel_neon_begin();
  409. pmull_gcm_encrypt_block(tag, iv, ctx->aes_key.key_enc, nrounds);
  410. put_unaligned_be32(2, iv + GCM_IV_SIZE);
  411. do {
  412. int blocks = walk.nbytes / (2 * AES_BLOCK_SIZE) * 2;
  413. int rem = walk.total - blocks * AES_BLOCK_SIZE;
  414. if (rk)
  415. kernel_neon_begin();
  416. pmull_gcm_decrypt(blocks, dg, walk.dst.virt.addr,
  417. walk.src.virt.addr, &ctx->ghash_key,
  418. iv, rk, nrounds);
  419. /* check if this is the final iteration of the loop */
  420. if (rem < (2 * AES_BLOCK_SIZE)) {
  421. u8 *iv2 = iv + AES_BLOCK_SIZE;
  422. if (rem > AES_BLOCK_SIZE) {
  423. memcpy(iv2, iv, AES_BLOCK_SIZE);
  424. crypto_inc(iv2, AES_BLOCK_SIZE);
  425. }
  426. pmull_gcm_encrypt_block(iv, iv, NULL, nrounds);
  427. if (rem > AES_BLOCK_SIZE)
  428. pmull_gcm_encrypt_block(iv2, iv2, NULL,
  429. nrounds);
  430. }
  431. kernel_neon_end();
  432. err = skcipher_walk_done(&walk,
  433. walk.nbytes % (2 * AES_BLOCK_SIZE));
  434. rk = ctx->aes_key.key_enc;
  435. } while (walk.nbytes >= 2 * AES_BLOCK_SIZE);
  436. } else {
  437. __aes_arm64_encrypt(ctx->aes_key.key_enc, tag, iv, nrounds);
  438. put_unaligned_be32(2, iv + GCM_IV_SIZE);
  439. while (walk.nbytes >= (2 * AES_BLOCK_SIZE)) {
  440. int blocks = walk.nbytes / (2 * AES_BLOCK_SIZE) * 2;
  441. u8 *dst = walk.dst.virt.addr;
  442. u8 *src = walk.src.virt.addr;
  443. ghash_do_update(blocks, dg, walk.src.virt.addr,
  444. &ctx->ghash_key, NULL);
  445. do {
  446. __aes_arm64_encrypt(ctx->aes_key.key_enc,
  447. buf, iv, nrounds);
  448. crypto_xor_cpy(dst, src, buf, AES_BLOCK_SIZE);
  449. crypto_inc(iv, AES_BLOCK_SIZE);
  450. dst += AES_BLOCK_SIZE;
  451. src += AES_BLOCK_SIZE;
  452. } while (--blocks > 0);
  453. err = skcipher_walk_done(&walk,
  454. walk.nbytes % (2 * AES_BLOCK_SIZE));
  455. }
  456. if (walk.nbytes) {
  457. if (walk.nbytes > AES_BLOCK_SIZE) {
  458. u8 *iv2 = iv + AES_BLOCK_SIZE;
  459. memcpy(iv2, iv, AES_BLOCK_SIZE);
  460. crypto_inc(iv2, AES_BLOCK_SIZE);
  461. __aes_arm64_encrypt(ctx->aes_key.key_enc, iv2,
  462. iv2, nrounds);
  463. }
  464. __aes_arm64_encrypt(ctx->aes_key.key_enc, iv, iv,
  465. nrounds);
  466. }
  467. }
  468. /* handle the tail */
  469. if (walk.nbytes) {
  470. const u8 *src = walk.src.virt.addr;
  471. const u8 *head = NULL;
  472. unsigned int nbytes = walk.nbytes;
  473. if (walk.nbytes > GHASH_BLOCK_SIZE) {
  474. head = src;
  475. src += GHASH_BLOCK_SIZE;
  476. nbytes %= GHASH_BLOCK_SIZE;
  477. }
  478. memcpy(buf, src, nbytes);
  479. memset(buf + nbytes, 0, GHASH_BLOCK_SIZE - nbytes);
  480. ghash_do_update(!!nbytes, dg, buf, &ctx->ghash_key, head);
  481. crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, iv,
  482. walk.nbytes);
  483. err = skcipher_walk_done(&walk, 0);
  484. }
  485. if (err)
  486. return err;
  487. gcm_final(req, ctx, dg, tag, req->cryptlen - authsize);
  488. /* compare calculated auth tag with the stored one */
  489. scatterwalk_map_and_copy(buf, req->src,
  490. req->assoclen + req->cryptlen - authsize,
  491. authsize, 0);
  492. if (crypto_memneq(tag, buf, authsize))
  493. return -EBADMSG;
  494. return 0;
  495. }
  496. static struct aead_alg gcm_aes_alg = {
  497. .ivsize = GCM_IV_SIZE,
  498. .chunksize = 2 * AES_BLOCK_SIZE,
  499. .maxauthsize = AES_BLOCK_SIZE,
  500. .setkey = gcm_setkey,
  501. .setauthsize = gcm_setauthsize,
  502. .encrypt = gcm_encrypt,
  503. .decrypt = gcm_decrypt,
  504. .base.cra_name = "gcm(aes)",
  505. .base.cra_driver_name = "gcm-aes-ce",
  506. .base.cra_priority = 300,
  507. .base.cra_blocksize = 1,
  508. .base.cra_ctxsize = sizeof(struct gcm_aes_ctx),
  509. .base.cra_module = THIS_MODULE,
  510. };
  511. static int __init ghash_ce_mod_init(void)
  512. {
  513. int ret;
  514. if (!(elf_hwcap & HWCAP_ASIMD))
  515. return -ENODEV;
  516. if (elf_hwcap & HWCAP_PMULL)
  517. pmull_ghash_update = pmull_ghash_update_p64;
  518. else
  519. pmull_ghash_update = pmull_ghash_update_p8;
  520. ret = crypto_register_shash(&ghash_alg);
  521. if (ret)
  522. return ret;
  523. if (elf_hwcap & HWCAP_PMULL) {
  524. ret = crypto_register_aead(&gcm_aes_alg);
  525. if (ret)
  526. crypto_unregister_shash(&ghash_alg);
  527. }
  528. return ret;
  529. }
  530. static void __exit ghash_ce_mod_exit(void)
  531. {
  532. crypto_unregister_shash(&ghash_alg);
  533. crypto_unregister_aead(&gcm_aes_alg);
  534. }
  535. static const struct cpu_feature ghash_cpu_feature[] = {
  536. { cpu_feature(PMULL) }, { }
  537. };
  538. MODULE_DEVICE_TABLE(cpu, ghash_cpu_feature);
  539. module_init(ghash_ce_mod_init);
  540. module_exit(ghash_ce_mod_exit);