ghash-ce-glue.c 13 KB

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