ghash-ce-glue.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Accelerated GHASH implementation with ARMv8 PMULL instructions.
  4. *
  5. * Copyright (C) 2014 - 2018 Linaro Ltd. <[email protected]>
  6. */
  7. #include <asm/neon.h>
  8. #include <asm/simd.h>
  9. #include <asm/unaligned.h>
  10. #include <crypto/aes.h>
  11. #include <crypto/algapi.h>
  12. #include <crypto/b128ops.h>
  13. #include <crypto/gf128mul.h>
  14. #include <crypto/internal/aead.h>
  15. #include <crypto/internal/hash.h>
  16. #include <crypto/internal/simd.h>
  17. #include <crypto/internal/skcipher.h>
  18. #include <crypto/scatterwalk.h>
  19. #include <linux/cpufeature.h>
  20. #include <linux/crypto.h>
  21. #include <linux/module.h>
  22. MODULE_DESCRIPTION("GHASH and AES-GCM using ARMv8 Crypto Extensions");
  23. MODULE_AUTHOR("Ard Biesheuvel <[email protected]>");
  24. MODULE_LICENSE("GPL v2");
  25. MODULE_ALIAS_CRYPTO("ghash");
  26. #define GHASH_BLOCK_SIZE 16
  27. #define GHASH_DIGEST_SIZE 16
  28. #define GCM_IV_SIZE 12
  29. struct ghash_key {
  30. be128 k;
  31. u64 h[][2];
  32. };
  33. struct ghash_desc_ctx {
  34. u64 digest[GHASH_DIGEST_SIZE/sizeof(u64)];
  35. u8 buf[GHASH_BLOCK_SIZE];
  36. u32 count;
  37. };
  38. struct gcm_aes_ctx {
  39. struct crypto_aes_ctx aes_key;
  40. struct ghash_key ghash_key;
  41. };
  42. asmlinkage void pmull_ghash_update_p64(int blocks, u64 dg[], const char *src,
  43. u64 const h[][2], const char *head);
  44. asmlinkage void pmull_ghash_update_p8(int blocks, u64 dg[], const char *src,
  45. u64 const h[][2], const char *head);
  46. asmlinkage void pmull_gcm_encrypt(int bytes, u8 dst[], const u8 src[],
  47. u64 const h[][2], u64 dg[], u8 ctr[],
  48. u32 const rk[], int rounds, u8 tag[]);
  49. asmlinkage int pmull_gcm_decrypt(int bytes, u8 dst[], const u8 src[],
  50. u64 const h[][2], u64 dg[], u8 ctr[],
  51. u32 const rk[], int rounds, const u8 l[],
  52. const u8 tag[], u64 authsize);
  53. static int ghash_init(struct shash_desc *desc)
  54. {
  55. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  56. *ctx = (struct ghash_desc_ctx){};
  57. return 0;
  58. }
  59. static void ghash_do_update(int blocks, u64 dg[], const char *src,
  60. struct ghash_key *key, const char *head)
  61. {
  62. be128 dst = { cpu_to_be64(dg[1]), cpu_to_be64(dg[0]) };
  63. do {
  64. const u8 *in = src;
  65. if (head) {
  66. in = head;
  67. blocks++;
  68. head = NULL;
  69. } else {
  70. src += GHASH_BLOCK_SIZE;
  71. }
  72. crypto_xor((u8 *)&dst, in, GHASH_BLOCK_SIZE);
  73. gf128mul_lle(&dst, &key->k);
  74. } while (--blocks);
  75. dg[0] = be64_to_cpu(dst.b);
  76. dg[1] = be64_to_cpu(dst.a);
  77. }
  78. static __always_inline
  79. void ghash_do_simd_update(int blocks, u64 dg[], const char *src,
  80. struct ghash_key *key, const char *head,
  81. void (*simd_update)(int blocks, u64 dg[],
  82. const char *src,
  83. u64 const h[][2],
  84. const char *head))
  85. {
  86. if (likely(crypto_simd_usable())) {
  87. kernel_neon_begin();
  88. simd_update(blocks, dg, src, key->h, head);
  89. kernel_neon_end();
  90. } else {
  91. ghash_do_update(blocks, dg, src, key, head);
  92. }
  93. }
  94. /* avoid hogging the CPU for too long */
  95. #define MAX_BLOCKS (SZ_64K / GHASH_BLOCK_SIZE)
  96. static int ghash_update(struct shash_desc *desc, const u8 *src,
  97. unsigned int len)
  98. {
  99. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  100. unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
  101. ctx->count += len;
  102. if ((partial + len) >= GHASH_BLOCK_SIZE) {
  103. struct ghash_key *key = crypto_shash_ctx(desc->tfm);
  104. int blocks;
  105. if (partial) {
  106. int p = GHASH_BLOCK_SIZE - partial;
  107. memcpy(ctx->buf + partial, src, p);
  108. src += p;
  109. len -= p;
  110. }
  111. blocks = len / GHASH_BLOCK_SIZE;
  112. len %= GHASH_BLOCK_SIZE;
  113. do {
  114. int chunk = min(blocks, MAX_BLOCKS);
  115. ghash_do_simd_update(chunk, ctx->digest, src, key,
  116. partial ? ctx->buf : NULL,
  117. pmull_ghash_update_p8);
  118. blocks -= chunk;
  119. src += chunk * GHASH_BLOCK_SIZE;
  120. partial = 0;
  121. } while (unlikely(blocks > 0));
  122. }
  123. if (len)
  124. memcpy(ctx->buf + partial, src, len);
  125. return 0;
  126. }
  127. static int ghash_final(struct shash_desc *desc, u8 *dst)
  128. {
  129. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  130. unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
  131. if (partial) {
  132. struct ghash_key *key = crypto_shash_ctx(desc->tfm);
  133. memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
  134. ghash_do_simd_update(1, ctx->digest, ctx->buf, key, NULL,
  135. pmull_ghash_update_p8);
  136. }
  137. put_unaligned_be64(ctx->digest[1], dst);
  138. put_unaligned_be64(ctx->digest[0], dst + 8);
  139. memzero_explicit(ctx, sizeof(*ctx));
  140. return 0;
  141. }
  142. static void ghash_reflect(u64 h[], const be128 *k)
  143. {
  144. u64 carry = be64_to_cpu(k->a) & BIT(63) ? 1 : 0;
  145. h[0] = (be64_to_cpu(k->b) << 1) | carry;
  146. h[1] = (be64_to_cpu(k->a) << 1) | (be64_to_cpu(k->b) >> 63);
  147. if (carry)
  148. h[1] ^= 0xc200000000000000UL;
  149. }
  150. static int ghash_setkey(struct crypto_shash *tfm,
  151. const u8 *inkey, unsigned int keylen)
  152. {
  153. struct ghash_key *key = crypto_shash_ctx(tfm);
  154. if (keylen != GHASH_BLOCK_SIZE)
  155. return -EINVAL;
  156. /* needed for the fallback */
  157. memcpy(&key->k, inkey, GHASH_BLOCK_SIZE);
  158. ghash_reflect(key->h[0], &key->k);
  159. return 0;
  160. }
  161. static struct shash_alg ghash_alg = {
  162. .base.cra_name = "ghash",
  163. .base.cra_driver_name = "ghash-neon",
  164. .base.cra_priority = 150,
  165. .base.cra_blocksize = GHASH_BLOCK_SIZE,
  166. .base.cra_ctxsize = sizeof(struct ghash_key) + sizeof(u64[2]),
  167. .base.cra_module = THIS_MODULE,
  168. .digestsize = GHASH_DIGEST_SIZE,
  169. .init = ghash_init,
  170. .update = ghash_update,
  171. .final = ghash_final,
  172. .setkey = ghash_setkey,
  173. .descsize = sizeof(struct ghash_desc_ctx),
  174. };
  175. static int num_rounds(struct crypto_aes_ctx *ctx)
  176. {
  177. /*
  178. * # of rounds specified by AES:
  179. * 128 bit key 10 rounds
  180. * 192 bit key 12 rounds
  181. * 256 bit key 14 rounds
  182. * => n byte key => 6 + (n/4) rounds
  183. */
  184. return 6 + ctx->key_length / 4;
  185. }
  186. static int gcm_setkey(struct crypto_aead *tfm, const u8 *inkey,
  187. unsigned int keylen)
  188. {
  189. struct gcm_aes_ctx *ctx = crypto_aead_ctx(tfm);
  190. u8 key[GHASH_BLOCK_SIZE];
  191. be128 h;
  192. int ret;
  193. ret = aes_expandkey(&ctx->aes_key, inkey, keylen);
  194. if (ret)
  195. return -EINVAL;
  196. aes_encrypt(&ctx->aes_key, key, (u8[AES_BLOCK_SIZE]){});
  197. /* needed for the fallback */
  198. memcpy(&ctx->ghash_key.k, key, GHASH_BLOCK_SIZE);
  199. ghash_reflect(ctx->ghash_key.h[0], &ctx->ghash_key.k);
  200. h = ctx->ghash_key.k;
  201. gf128mul_lle(&h, &ctx->ghash_key.k);
  202. ghash_reflect(ctx->ghash_key.h[1], &h);
  203. gf128mul_lle(&h, &ctx->ghash_key.k);
  204. ghash_reflect(ctx->ghash_key.h[2], &h);
  205. gf128mul_lle(&h, &ctx->ghash_key.k);
  206. ghash_reflect(ctx->ghash_key.h[3], &h);
  207. return 0;
  208. }
  209. static int gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  210. {
  211. switch (authsize) {
  212. case 4:
  213. case 8:
  214. case 12 ... 16:
  215. break;
  216. default:
  217. return -EINVAL;
  218. }
  219. return 0;
  220. }
  221. static void gcm_update_mac(u64 dg[], const u8 *src, int count, u8 buf[],
  222. int *buf_count, struct gcm_aes_ctx *ctx)
  223. {
  224. if (*buf_count > 0) {
  225. int buf_added = min(count, GHASH_BLOCK_SIZE - *buf_count);
  226. memcpy(&buf[*buf_count], src, buf_added);
  227. *buf_count += buf_added;
  228. src += buf_added;
  229. count -= buf_added;
  230. }
  231. if (count >= GHASH_BLOCK_SIZE || *buf_count == GHASH_BLOCK_SIZE) {
  232. int blocks = count / GHASH_BLOCK_SIZE;
  233. ghash_do_simd_update(blocks, dg, src, &ctx->ghash_key,
  234. *buf_count ? buf : NULL,
  235. pmull_ghash_update_p64);
  236. src += blocks * GHASH_BLOCK_SIZE;
  237. count %= GHASH_BLOCK_SIZE;
  238. *buf_count = 0;
  239. }
  240. if (count > 0) {
  241. memcpy(buf, src, count);
  242. *buf_count = count;
  243. }
  244. }
  245. static void gcm_calculate_auth_mac(struct aead_request *req, u64 dg[])
  246. {
  247. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  248. struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
  249. u8 buf[GHASH_BLOCK_SIZE];
  250. struct scatter_walk walk;
  251. u32 len = req->assoclen;
  252. int buf_count = 0;
  253. scatterwalk_start(&walk, req->src);
  254. do {
  255. u32 n = scatterwalk_clamp(&walk, len);
  256. u8 *p;
  257. if (!n) {
  258. scatterwalk_start(&walk, sg_next(walk.sg));
  259. n = scatterwalk_clamp(&walk, len);
  260. }
  261. p = scatterwalk_map(&walk);
  262. gcm_update_mac(dg, p, n, buf, &buf_count, ctx);
  263. len -= n;
  264. scatterwalk_unmap(p);
  265. scatterwalk_advance(&walk, n);
  266. scatterwalk_done(&walk, 0, len);
  267. } while (len);
  268. if (buf_count) {
  269. memset(&buf[buf_count], 0, GHASH_BLOCK_SIZE - buf_count);
  270. ghash_do_simd_update(1, dg, buf, &ctx->ghash_key, NULL,
  271. pmull_ghash_update_p64);
  272. }
  273. }
  274. static int gcm_encrypt(struct aead_request *req)
  275. {
  276. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  277. struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
  278. int nrounds = num_rounds(&ctx->aes_key);
  279. struct skcipher_walk walk;
  280. u8 buf[AES_BLOCK_SIZE];
  281. u8 iv[AES_BLOCK_SIZE];
  282. u64 dg[2] = {};
  283. be128 lengths;
  284. u8 *tag;
  285. int err;
  286. lengths.a = cpu_to_be64(req->assoclen * 8);
  287. lengths.b = cpu_to_be64(req->cryptlen * 8);
  288. if (req->assoclen)
  289. gcm_calculate_auth_mac(req, dg);
  290. memcpy(iv, req->iv, GCM_IV_SIZE);
  291. put_unaligned_be32(2, iv + GCM_IV_SIZE);
  292. err = skcipher_walk_aead_encrypt(&walk, req, false);
  293. do {
  294. const u8 *src = walk.src.virt.addr;
  295. u8 *dst = walk.dst.virt.addr;
  296. int nbytes = walk.nbytes;
  297. tag = (u8 *)&lengths;
  298. if (unlikely(nbytes > 0 && nbytes < AES_BLOCK_SIZE)) {
  299. src = dst = memcpy(buf + sizeof(buf) - nbytes,
  300. src, nbytes);
  301. } else if (nbytes < walk.total) {
  302. nbytes &= ~(AES_BLOCK_SIZE - 1);
  303. tag = NULL;
  304. }
  305. kernel_neon_begin();
  306. pmull_gcm_encrypt(nbytes, dst, src, ctx->ghash_key.h,
  307. dg, iv, ctx->aes_key.key_enc, nrounds,
  308. tag);
  309. kernel_neon_end();
  310. if (unlikely(!nbytes))
  311. break;
  312. if (unlikely(nbytes > 0 && nbytes < AES_BLOCK_SIZE))
  313. memcpy(walk.dst.virt.addr,
  314. buf + sizeof(buf) - nbytes, nbytes);
  315. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  316. } while (walk.nbytes);
  317. if (err)
  318. return err;
  319. /* copy authtag to end of dst */
  320. scatterwalk_map_and_copy(tag, req->dst, req->assoclen + req->cryptlen,
  321. crypto_aead_authsize(aead), 1);
  322. return 0;
  323. }
  324. static int gcm_decrypt(struct aead_request *req)
  325. {
  326. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  327. struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
  328. unsigned int authsize = crypto_aead_authsize(aead);
  329. int nrounds = num_rounds(&ctx->aes_key);
  330. struct skcipher_walk walk;
  331. u8 otag[AES_BLOCK_SIZE];
  332. u8 buf[AES_BLOCK_SIZE];
  333. u8 iv[AES_BLOCK_SIZE];
  334. u64 dg[2] = {};
  335. be128 lengths;
  336. u8 *tag;
  337. int ret;
  338. int err;
  339. lengths.a = cpu_to_be64(req->assoclen * 8);
  340. lengths.b = cpu_to_be64((req->cryptlen - authsize) * 8);
  341. if (req->assoclen)
  342. gcm_calculate_auth_mac(req, dg);
  343. memcpy(iv, req->iv, GCM_IV_SIZE);
  344. put_unaligned_be32(2, iv + GCM_IV_SIZE);
  345. scatterwalk_map_and_copy(otag, req->src,
  346. req->assoclen + req->cryptlen - authsize,
  347. authsize, 0);
  348. err = skcipher_walk_aead_decrypt(&walk, req, false);
  349. do {
  350. const u8 *src = walk.src.virt.addr;
  351. u8 *dst = walk.dst.virt.addr;
  352. int nbytes = walk.nbytes;
  353. tag = (u8 *)&lengths;
  354. if (unlikely(nbytes > 0 && nbytes < AES_BLOCK_SIZE)) {
  355. src = dst = memcpy(buf + sizeof(buf) - nbytes,
  356. src, nbytes);
  357. } else if (nbytes < walk.total) {
  358. nbytes &= ~(AES_BLOCK_SIZE - 1);
  359. tag = NULL;
  360. }
  361. kernel_neon_begin();
  362. ret = pmull_gcm_decrypt(nbytes, dst, src, ctx->ghash_key.h,
  363. dg, iv, ctx->aes_key.key_enc,
  364. nrounds, tag, otag, authsize);
  365. kernel_neon_end();
  366. if (unlikely(!nbytes))
  367. break;
  368. if (unlikely(nbytes > 0 && nbytes < AES_BLOCK_SIZE))
  369. memcpy(walk.dst.virt.addr,
  370. buf + sizeof(buf) - nbytes, nbytes);
  371. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  372. } while (walk.nbytes);
  373. if (err)
  374. return err;
  375. return ret ? -EBADMSG : 0;
  376. }
  377. static struct aead_alg gcm_aes_alg = {
  378. .ivsize = GCM_IV_SIZE,
  379. .chunksize = AES_BLOCK_SIZE,
  380. .maxauthsize = AES_BLOCK_SIZE,
  381. .setkey = gcm_setkey,
  382. .setauthsize = gcm_setauthsize,
  383. .encrypt = gcm_encrypt,
  384. .decrypt = gcm_decrypt,
  385. .base.cra_name = "gcm(aes)",
  386. .base.cra_driver_name = "gcm-aes-ce",
  387. .base.cra_priority = 300,
  388. .base.cra_blocksize = 1,
  389. .base.cra_ctxsize = sizeof(struct gcm_aes_ctx) +
  390. 4 * sizeof(u64[2]),
  391. .base.cra_module = THIS_MODULE,
  392. };
  393. static int __init ghash_ce_mod_init(void)
  394. {
  395. if (!cpu_have_named_feature(ASIMD))
  396. return -ENODEV;
  397. if (cpu_have_named_feature(PMULL))
  398. return crypto_register_aead(&gcm_aes_alg);
  399. return crypto_register_shash(&ghash_alg);
  400. }
  401. static void __exit ghash_ce_mod_exit(void)
  402. {
  403. if (cpu_have_named_feature(PMULL))
  404. crypto_unregister_aead(&gcm_aes_alg);
  405. else
  406. crypto_unregister_shash(&ghash_alg);
  407. }
  408. static const struct cpu_feature ghash_cpu_feature[] = {
  409. { cpu_feature(PMULL) }, { }
  410. };
  411. MODULE_DEVICE_TABLE(cpu, ghash_cpu_feature);
  412. module_init(ghash_ce_mod_init);
  413. module_exit(ghash_ce_mod_exit);