ghash-ce-glue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Accelerated GHASH implementation with ARMv8 PMULL instructions.
  3. *
  4. * Copyright (C) 2014 - 2017 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 a;
  33. u64 b;
  34. be128 k;
  35. };
  36. struct ghash_desc_ctx {
  37. u64 digest[GHASH_DIGEST_SIZE/sizeof(u64)];
  38. u8 buf[GHASH_BLOCK_SIZE];
  39. u32 count;
  40. };
  41. struct gcm_aes_ctx {
  42. struct crypto_aes_ctx aes_key;
  43. struct ghash_key ghash_key;
  44. };
  45. asmlinkage void pmull_ghash_update_p64(int blocks, u64 dg[], const char *src,
  46. struct ghash_key const *k,
  47. const char *head);
  48. asmlinkage void pmull_ghash_update_p8(int blocks, u64 dg[], const char *src,
  49. struct ghash_key const *k,
  50. const char *head);
  51. static void (*pmull_ghash_update)(int blocks, u64 dg[], const char *src,
  52. struct ghash_key const *k,
  53. const char *head);
  54. asmlinkage void pmull_gcm_encrypt(int blocks, u64 dg[], u8 dst[],
  55. const u8 src[], struct ghash_key const *k,
  56. u8 ctr[], int rounds, u8 ks[]);
  57. asmlinkage void pmull_gcm_decrypt(int blocks, u64 dg[], u8 dst[],
  58. const u8 src[], struct ghash_key const *k,
  59. u8 ctr[], int rounds);
  60. asmlinkage void pmull_gcm_encrypt_block(u8 dst[], u8 const src[],
  61. u32 const rk[], int rounds);
  62. asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
  63. static int ghash_init(struct shash_desc *desc)
  64. {
  65. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  66. *ctx = (struct ghash_desc_ctx){};
  67. return 0;
  68. }
  69. static void ghash_do_update(int blocks, u64 dg[], const char *src,
  70. struct ghash_key *key, const char *head)
  71. {
  72. if (likely(may_use_simd())) {
  73. kernel_neon_begin();
  74. pmull_ghash_update(blocks, dg, src, key, head);
  75. kernel_neon_end();
  76. } else {
  77. be128 dst = { cpu_to_be64(dg[1]), cpu_to_be64(dg[0]) };
  78. do {
  79. const u8 *in = src;
  80. if (head) {
  81. in = head;
  82. blocks++;
  83. head = NULL;
  84. } else {
  85. src += GHASH_BLOCK_SIZE;
  86. }
  87. crypto_xor((u8 *)&dst, in, GHASH_BLOCK_SIZE);
  88. gf128mul_lle(&dst, &key->k);
  89. } while (--blocks);
  90. dg[0] = be64_to_cpu(dst.b);
  91. dg[1] = be64_to_cpu(dst.a);
  92. }
  93. }
  94. static int ghash_update(struct shash_desc *desc, const u8 *src,
  95. unsigned int len)
  96. {
  97. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  98. unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
  99. ctx->count += len;
  100. if ((partial + len) >= GHASH_BLOCK_SIZE) {
  101. struct ghash_key *key = crypto_shash_ctx(desc->tfm);
  102. int blocks;
  103. if (partial) {
  104. int p = GHASH_BLOCK_SIZE - partial;
  105. memcpy(ctx->buf + partial, src, p);
  106. src += p;
  107. len -= p;
  108. }
  109. blocks = len / GHASH_BLOCK_SIZE;
  110. len %= GHASH_BLOCK_SIZE;
  111. ghash_do_update(blocks, ctx->digest, src, key,
  112. partial ? ctx->buf : NULL);
  113. src += blocks * GHASH_BLOCK_SIZE;
  114. partial = 0;
  115. }
  116. if (len)
  117. memcpy(ctx->buf + partial, src, len);
  118. return 0;
  119. }
  120. static int ghash_final(struct shash_desc *desc, u8 *dst)
  121. {
  122. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  123. unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
  124. if (partial) {
  125. struct ghash_key *key = crypto_shash_ctx(desc->tfm);
  126. memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
  127. ghash_do_update(1, ctx->digest, ctx->buf, key, NULL);
  128. }
  129. put_unaligned_be64(ctx->digest[1], dst);
  130. put_unaligned_be64(ctx->digest[0], dst + 8);
  131. *ctx = (struct ghash_desc_ctx){};
  132. return 0;
  133. }
  134. static int __ghash_setkey(struct ghash_key *key,
  135. const u8 *inkey, unsigned int keylen)
  136. {
  137. u64 a, b;
  138. /* needed for the fallback */
  139. memcpy(&key->k, inkey, GHASH_BLOCK_SIZE);
  140. /* perform multiplication by 'x' in GF(2^128) */
  141. b = get_unaligned_be64(inkey);
  142. a = get_unaligned_be64(inkey + 8);
  143. key->a = (a << 1) | (b >> 63);
  144. key->b = (b << 1) | (a >> 63);
  145. if (b >> 63)
  146. key->b ^= 0xc200000000000000UL;
  147. return 0;
  148. }
  149. static int ghash_setkey(struct crypto_shash *tfm,
  150. const u8 *inkey, unsigned int keylen)
  151. {
  152. struct ghash_key *key = crypto_shash_ctx(tfm);
  153. if (keylen != GHASH_BLOCK_SIZE) {
  154. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  155. return -EINVAL;
  156. }
  157. return __ghash_setkey(key, inkey, keylen);
  158. }
  159. static struct shash_alg ghash_alg = {
  160. .base.cra_name = "ghash",
  161. .base.cra_driver_name = "ghash-ce",
  162. .base.cra_priority = 200,
  163. .base.cra_flags = CRYPTO_ALG_TYPE_SHASH,
  164. .base.cra_blocksize = GHASH_BLOCK_SIZE,
  165. .base.cra_ctxsize = sizeof(struct ghash_key),
  166. .base.cra_module = THIS_MODULE,
  167. .digestsize = GHASH_DIGEST_SIZE,
  168. .init = ghash_init,
  169. .update = ghash_update,
  170. .final = ghash_final,
  171. .setkey = ghash_setkey,
  172. .descsize = sizeof(struct ghash_desc_ctx),
  173. };
  174. static int num_rounds(struct crypto_aes_ctx *ctx)
  175. {
  176. /*
  177. * # of rounds specified by AES:
  178. * 128 bit key 10 rounds
  179. * 192 bit key 12 rounds
  180. * 256 bit key 14 rounds
  181. * => n byte key => 6 + (n/4) rounds
  182. */
  183. return 6 + ctx->key_length / 4;
  184. }
  185. static int gcm_setkey(struct crypto_aead *tfm, const u8 *inkey,
  186. unsigned int keylen)
  187. {
  188. struct gcm_aes_ctx *ctx = crypto_aead_ctx(tfm);
  189. u8 key[GHASH_BLOCK_SIZE];
  190. int ret;
  191. ret = crypto_aes_expand_key(&ctx->aes_key, inkey, keylen);
  192. if (ret) {
  193. tfm->base.crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  194. return -EINVAL;
  195. }
  196. __aes_arm64_encrypt(ctx->aes_key.key_enc, key, (u8[AES_BLOCK_SIZE]){},
  197. num_rounds(&ctx->aes_key));
  198. return __ghash_setkey(&ctx->ghash_key, key, sizeof(key));
  199. }
  200. static int gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  201. {
  202. switch (authsize) {
  203. case 4:
  204. case 8:
  205. case 12 ... 16:
  206. break;
  207. default:
  208. return -EINVAL;
  209. }
  210. return 0;
  211. }
  212. static void gcm_update_mac(u64 dg[], const u8 *src, int count, u8 buf[],
  213. int *buf_count, struct gcm_aes_ctx *ctx)
  214. {
  215. if (*buf_count > 0) {
  216. int buf_added = min(count, GHASH_BLOCK_SIZE - *buf_count);
  217. memcpy(&buf[*buf_count], src, buf_added);
  218. *buf_count += buf_added;
  219. src += buf_added;
  220. count -= buf_added;
  221. }
  222. if (count >= GHASH_BLOCK_SIZE || *buf_count == GHASH_BLOCK_SIZE) {
  223. int blocks = count / GHASH_BLOCK_SIZE;
  224. ghash_do_update(blocks, dg, src, &ctx->ghash_key,
  225. *buf_count ? buf : NULL);
  226. src += blocks * GHASH_BLOCK_SIZE;
  227. count %= GHASH_BLOCK_SIZE;
  228. *buf_count = 0;
  229. }
  230. if (count > 0) {
  231. memcpy(buf, src, count);
  232. *buf_count = count;
  233. }
  234. }
  235. static void gcm_calculate_auth_mac(struct aead_request *req, u64 dg[])
  236. {
  237. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  238. struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
  239. u8 buf[GHASH_BLOCK_SIZE];
  240. struct scatter_walk walk;
  241. u32 len = req->assoclen;
  242. int buf_count = 0;
  243. scatterwalk_start(&walk, req->src);
  244. do {
  245. u32 n = scatterwalk_clamp(&walk, len);
  246. u8 *p;
  247. if (!n) {
  248. scatterwalk_start(&walk, sg_next(walk.sg));
  249. n = scatterwalk_clamp(&walk, len);
  250. }
  251. p = scatterwalk_map(&walk);
  252. gcm_update_mac(dg, p, n, buf, &buf_count, ctx);
  253. len -= n;
  254. scatterwalk_unmap(p);
  255. scatterwalk_advance(&walk, n);
  256. scatterwalk_done(&walk, 0, len);
  257. } while (len);
  258. if (buf_count) {
  259. memset(&buf[buf_count], 0, GHASH_BLOCK_SIZE - buf_count);
  260. ghash_do_update(1, dg, buf, &ctx->ghash_key, NULL);
  261. }
  262. }
  263. static void gcm_final(struct aead_request *req, struct gcm_aes_ctx *ctx,
  264. u64 dg[], u8 tag[], int cryptlen)
  265. {
  266. u8 mac[AES_BLOCK_SIZE];
  267. u128 lengths;
  268. lengths.a = cpu_to_be64(req->assoclen * 8);
  269. lengths.b = cpu_to_be64(cryptlen * 8);
  270. ghash_do_update(1, dg, (void *)&lengths, &ctx->ghash_key, NULL);
  271. put_unaligned_be64(dg[1], mac);
  272. put_unaligned_be64(dg[0], mac + 8);
  273. crypto_xor(tag, mac, AES_BLOCK_SIZE);
  274. }
  275. static int gcm_encrypt(struct aead_request *req)
  276. {
  277. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  278. struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
  279. struct skcipher_walk walk;
  280. u8 iv[AES_BLOCK_SIZE];
  281. u8 ks[AES_BLOCK_SIZE];
  282. u8 tag[AES_BLOCK_SIZE];
  283. u64 dg[2] = {};
  284. int err;
  285. if (req->assoclen)
  286. gcm_calculate_auth_mac(req, dg);
  287. memcpy(iv, req->iv, GCM_IV_SIZE);
  288. put_unaligned_be32(1, iv + GCM_IV_SIZE);
  289. if (likely(may_use_simd())) {
  290. kernel_neon_begin();
  291. pmull_gcm_encrypt_block(tag, iv, ctx->aes_key.key_enc,
  292. num_rounds(&ctx->aes_key));
  293. put_unaligned_be32(2, iv + GCM_IV_SIZE);
  294. pmull_gcm_encrypt_block(ks, iv, NULL,
  295. num_rounds(&ctx->aes_key));
  296. put_unaligned_be32(3, iv + GCM_IV_SIZE);
  297. err = skcipher_walk_aead_encrypt(&walk, req, true);
  298. while (walk.nbytes >= AES_BLOCK_SIZE) {
  299. int blocks = walk.nbytes / AES_BLOCK_SIZE;
  300. pmull_gcm_encrypt(blocks, dg, walk.dst.virt.addr,
  301. walk.src.virt.addr, &ctx->ghash_key,
  302. iv, num_rounds(&ctx->aes_key), ks);
  303. err = skcipher_walk_done(&walk,
  304. walk.nbytes % AES_BLOCK_SIZE);
  305. }
  306. kernel_neon_end();
  307. } else {
  308. __aes_arm64_encrypt(ctx->aes_key.key_enc, tag, iv,
  309. num_rounds(&ctx->aes_key));
  310. put_unaligned_be32(2, iv + GCM_IV_SIZE);
  311. err = skcipher_walk_aead_encrypt(&walk, req, true);
  312. while (walk.nbytes >= AES_BLOCK_SIZE) {
  313. int blocks = walk.nbytes / AES_BLOCK_SIZE;
  314. u8 *dst = walk.dst.virt.addr;
  315. u8 *src = walk.src.virt.addr;
  316. do {
  317. __aes_arm64_encrypt(ctx->aes_key.key_enc,
  318. ks, iv,
  319. num_rounds(&ctx->aes_key));
  320. crypto_xor_cpy(dst, src, ks, AES_BLOCK_SIZE);
  321. crypto_inc(iv, AES_BLOCK_SIZE);
  322. dst += AES_BLOCK_SIZE;
  323. src += AES_BLOCK_SIZE;
  324. } while (--blocks > 0);
  325. ghash_do_update(walk.nbytes / AES_BLOCK_SIZE, dg,
  326. walk.dst.virt.addr, &ctx->ghash_key,
  327. NULL);
  328. err = skcipher_walk_done(&walk,
  329. walk.nbytes % AES_BLOCK_SIZE);
  330. }
  331. if (walk.nbytes)
  332. __aes_arm64_encrypt(ctx->aes_key.key_enc, ks, iv,
  333. num_rounds(&ctx->aes_key));
  334. }
  335. /* handle the tail */
  336. if (walk.nbytes) {
  337. u8 buf[GHASH_BLOCK_SIZE];
  338. crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, ks,
  339. walk.nbytes);
  340. memcpy(buf, walk.dst.virt.addr, walk.nbytes);
  341. memset(buf + walk.nbytes, 0, GHASH_BLOCK_SIZE - walk.nbytes);
  342. ghash_do_update(1, dg, buf, &ctx->ghash_key, NULL);
  343. err = skcipher_walk_done(&walk, 0);
  344. }
  345. if (err)
  346. return err;
  347. gcm_final(req, ctx, dg, tag, req->cryptlen);
  348. /* copy authtag to end of dst */
  349. scatterwalk_map_and_copy(tag, req->dst, req->assoclen + req->cryptlen,
  350. crypto_aead_authsize(aead), 1);
  351. return 0;
  352. }
  353. static int gcm_decrypt(struct aead_request *req)
  354. {
  355. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  356. struct gcm_aes_ctx *ctx = crypto_aead_ctx(aead);
  357. unsigned int authsize = crypto_aead_authsize(aead);
  358. struct skcipher_walk walk;
  359. u8 iv[AES_BLOCK_SIZE];
  360. u8 tag[AES_BLOCK_SIZE];
  361. u8 buf[GHASH_BLOCK_SIZE];
  362. u64 dg[2] = {};
  363. int err;
  364. if (req->assoclen)
  365. gcm_calculate_auth_mac(req, dg);
  366. memcpy(iv, req->iv, GCM_IV_SIZE);
  367. put_unaligned_be32(1, iv + GCM_IV_SIZE);
  368. if (likely(may_use_simd())) {
  369. kernel_neon_begin();
  370. pmull_gcm_encrypt_block(tag, iv, ctx->aes_key.key_enc,
  371. num_rounds(&ctx->aes_key));
  372. put_unaligned_be32(2, iv + GCM_IV_SIZE);
  373. err = skcipher_walk_aead_decrypt(&walk, req, true);
  374. while (walk.nbytes >= AES_BLOCK_SIZE) {
  375. int blocks = walk.nbytes / AES_BLOCK_SIZE;
  376. pmull_gcm_decrypt(blocks, dg, walk.dst.virt.addr,
  377. walk.src.virt.addr, &ctx->ghash_key,
  378. iv, num_rounds(&ctx->aes_key));
  379. err = skcipher_walk_done(&walk,
  380. walk.nbytes % AES_BLOCK_SIZE);
  381. }
  382. if (walk.nbytes)
  383. pmull_gcm_encrypt_block(iv, iv, NULL,
  384. num_rounds(&ctx->aes_key));
  385. kernel_neon_end();
  386. } else {
  387. __aes_arm64_encrypt(ctx->aes_key.key_enc, tag, iv,
  388. num_rounds(&ctx->aes_key));
  389. put_unaligned_be32(2, iv + GCM_IV_SIZE);
  390. err = skcipher_walk_aead_decrypt(&walk, req, true);
  391. while (walk.nbytes >= AES_BLOCK_SIZE) {
  392. int blocks = walk.nbytes / AES_BLOCK_SIZE;
  393. u8 *dst = walk.dst.virt.addr;
  394. u8 *src = walk.src.virt.addr;
  395. ghash_do_update(blocks, dg, walk.src.virt.addr,
  396. &ctx->ghash_key, NULL);
  397. do {
  398. __aes_arm64_encrypt(ctx->aes_key.key_enc,
  399. buf, iv,
  400. num_rounds(&ctx->aes_key));
  401. crypto_xor_cpy(dst, src, buf, AES_BLOCK_SIZE);
  402. crypto_inc(iv, AES_BLOCK_SIZE);
  403. dst += AES_BLOCK_SIZE;
  404. src += AES_BLOCK_SIZE;
  405. } while (--blocks > 0);
  406. err = skcipher_walk_done(&walk,
  407. walk.nbytes % AES_BLOCK_SIZE);
  408. }
  409. if (walk.nbytes)
  410. __aes_arm64_encrypt(ctx->aes_key.key_enc, iv, iv,
  411. num_rounds(&ctx->aes_key));
  412. }
  413. /* handle the tail */
  414. if (walk.nbytes) {
  415. memcpy(buf, walk.src.virt.addr, walk.nbytes);
  416. memset(buf + walk.nbytes, 0, GHASH_BLOCK_SIZE - walk.nbytes);
  417. ghash_do_update(1, dg, buf, &ctx->ghash_key, NULL);
  418. crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, iv,
  419. walk.nbytes);
  420. err = skcipher_walk_done(&walk, 0);
  421. }
  422. if (err)
  423. return err;
  424. gcm_final(req, ctx, dg, tag, req->cryptlen - authsize);
  425. /* compare calculated auth tag with the stored one */
  426. scatterwalk_map_and_copy(buf, req->src,
  427. req->assoclen + req->cryptlen - authsize,
  428. authsize, 0);
  429. if (crypto_memneq(tag, buf, authsize))
  430. return -EBADMSG;
  431. return 0;
  432. }
  433. static struct aead_alg gcm_aes_alg = {
  434. .ivsize = GCM_IV_SIZE,
  435. .chunksize = AES_BLOCK_SIZE,
  436. .maxauthsize = AES_BLOCK_SIZE,
  437. .setkey = gcm_setkey,
  438. .setauthsize = gcm_setauthsize,
  439. .encrypt = gcm_encrypt,
  440. .decrypt = gcm_decrypt,
  441. .base.cra_name = "gcm(aes)",
  442. .base.cra_driver_name = "gcm-aes-ce",
  443. .base.cra_priority = 300,
  444. .base.cra_blocksize = 1,
  445. .base.cra_ctxsize = sizeof(struct gcm_aes_ctx),
  446. .base.cra_module = THIS_MODULE,
  447. };
  448. static int __init ghash_ce_mod_init(void)
  449. {
  450. int ret;
  451. if (!(elf_hwcap & HWCAP_ASIMD))
  452. return -ENODEV;
  453. if (elf_hwcap & HWCAP_PMULL)
  454. pmull_ghash_update = pmull_ghash_update_p64;
  455. else
  456. pmull_ghash_update = pmull_ghash_update_p8;
  457. ret = crypto_register_shash(&ghash_alg);
  458. if (ret)
  459. return ret;
  460. if (elf_hwcap & HWCAP_PMULL) {
  461. ret = crypto_register_aead(&gcm_aes_alg);
  462. if (ret)
  463. crypto_unregister_shash(&ghash_alg);
  464. }
  465. return ret;
  466. }
  467. static void __exit ghash_ce_mod_exit(void)
  468. {
  469. crypto_unregister_shash(&ghash_alg);
  470. crypto_unregister_aead(&gcm_aes_alg);
  471. }
  472. static const struct cpu_feature ghash_cpu_feature[] = {
  473. { cpu_feature(PMULL) }, { }
  474. };
  475. MODULE_DEVICE_TABLE(cpu, ghash_cpu_feature);
  476. module_init(ghash_ce_mod_init);
  477. module_exit(ghash_ce_mod_exit);