ghash-ce-glue.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. <[email protected]>
  6. */
  7. #include <asm/hwcap.h>
  8. #include <asm/neon.h>
  9. #include <asm/simd.h>
  10. #include <asm/unaligned.h>
  11. #include <crypto/b128ops.h>
  12. #include <crypto/cryptd.h>
  13. #include <crypto/internal/hash.h>
  14. #include <crypto/internal/simd.h>
  15. #include <crypto/gf128mul.h>
  16. #include <linux/cpufeature.h>
  17. #include <linux/crypto.h>
  18. #include <linux/jump_label.h>
  19. #include <linux/module.h>
  20. MODULE_DESCRIPTION("GHASH hash function using ARMv8 Crypto Extensions");
  21. MODULE_AUTHOR("Ard Biesheuvel <[email protected]>");
  22. MODULE_LICENSE("GPL v2");
  23. MODULE_ALIAS_CRYPTO("ghash");
  24. #define GHASH_BLOCK_SIZE 16
  25. #define GHASH_DIGEST_SIZE 16
  26. struct ghash_key {
  27. be128 k;
  28. u64 h[][2];
  29. };
  30. struct ghash_desc_ctx {
  31. u64 digest[GHASH_DIGEST_SIZE/sizeof(u64)];
  32. u8 buf[GHASH_BLOCK_SIZE];
  33. u32 count;
  34. };
  35. struct ghash_async_ctx {
  36. struct cryptd_ahash *cryptd_tfm;
  37. };
  38. asmlinkage void pmull_ghash_update_p64(int blocks, u64 dg[], const char *src,
  39. u64 const h[][2], const char *head);
  40. asmlinkage void pmull_ghash_update_p8(int blocks, u64 dg[], const char *src,
  41. u64 const h[][2], const char *head);
  42. static __ro_after_init DEFINE_STATIC_KEY_FALSE(use_p64);
  43. static int ghash_init(struct shash_desc *desc)
  44. {
  45. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  46. *ctx = (struct ghash_desc_ctx){};
  47. return 0;
  48. }
  49. static void ghash_do_update(int blocks, u64 dg[], const char *src,
  50. struct ghash_key *key, const char *head)
  51. {
  52. if (likely(crypto_simd_usable())) {
  53. kernel_neon_begin();
  54. if (static_branch_likely(&use_p64))
  55. pmull_ghash_update_p64(blocks, dg, src, key->h, head);
  56. else
  57. pmull_ghash_update_p8(blocks, dg, src, key->h, head);
  58. kernel_neon_end();
  59. } else {
  60. be128 dst = { cpu_to_be64(dg[1]), cpu_to_be64(dg[0]) };
  61. do {
  62. const u8 *in = src;
  63. if (head) {
  64. in = head;
  65. blocks++;
  66. head = NULL;
  67. } else {
  68. src += GHASH_BLOCK_SIZE;
  69. }
  70. crypto_xor((u8 *)&dst, in, GHASH_BLOCK_SIZE);
  71. gf128mul_lle(&dst, &key->k);
  72. } while (--blocks);
  73. dg[0] = be64_to_cpu(dst.b);
  74. dg[1] = be64_to_cpu(dst.a);
  75. }
  76. }
  77. static int ghash_update(struct shash_desc *desc, const u8 *src,
  78. unsigned int len)
  79. {
  80. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  81. unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
  82. ctx->count += len;
  83. if ((partial + len) >= GHASH_BLOCK_SIZE) {
  84. struct ghash_key *key = crypto_shash_ctx(desc->tfm);
  85. int blocks;
  86. if (partial) {
  87. int p = GHASH_BLOCK_SIZE - partial;
  88. memcpy(ctx->buf + partial, src, p);
  89. src += p;
  90. len -= p;
  91. }
  92. blocks = len / GHASH_BLOCK_SIZE;
  93. len %= GHASH_BLOCK_SIZE;
  94. ghash_do_update(blocks, ctx->digest, src, key,
  95. partial ? ctx->buf : NULL);
  96. src += blocks * GHASH_BLOCK_SIZE;
  97. partial = 0;
  98. }
  99. if (len)
  100. memcpy(ctx->buf + partial, src, len);
  101. return 0;
  102. }
  103. static int ghash_final(struct shash_desc *desc, u8 *dst)
  104. {
  105. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  106. unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
  107. if (partial) {
  108. struct ghash_key *key = crypto_shash_ctx(desc->tfm);
  109. memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
  110. ghash_do_update(1, ctx->digest, ctx->buf, key, NULL);
  111. }
  112. put_unaligned_be64(ctx->digest[1], dst);
  113. put_unaligned_be64(ctx->digest[0], dst + 8);
  114. *ctx = (struct ghash_desc_ctx){};
  115. return 0;
  116. }
  117. static void ghash_reflect(u64 h[], const be128 *k)
  118. {
  119. u64 carry = be64_to_cpu(k->a) >> 63;
  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. if (static_branch_likely(&use_p64)) {
  135. be128 h = key->k;
  136. gf128mul_lle(&h, &key->k);
  137. ghash_reflect(key->h[1], &h);
  138. gf128mul_lle(&h, &key->k);
  139. ghash_reflect(key->h[2], &h);
  140. gf128mul_lle(&h, &key->k);
  141. ghash_reflect(key->h[3], &h);
  142. }
  143. return 0;
  144. }
  145. static struct shash_alg ghash_alg = {
  146. .digestsize = GHASH_DIGEST_SIZE,
  147. .init = ghash_init,
  148. .update = ghash_update,
  149. .final = ghash_final,
  150. .setkey = ghash_setkey,
  151. .descsize = sizeof(struct ghash_desc_ctx),
  152. .base.cra_name = "ghash",
  153. .base.cra_driver_name = "ghash-ce-sync",
  154. .base.cra_priority = 300 - 1,
  155. .base.cra_blocksize = GHASH_BLOCK_SIZE,
  156. .base.cra_ctxsize = sizeof(struct ghash_key) + sizeof(u64[2]),
  157. .base.cra_module = THIS_MODULE,
  158. };
  159. static int ghash_async_init(struct ahash_request *req)
  160. {
  161. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  162. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  163. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  164. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  165. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  166. struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
  167. desc->tfm = child;
  168. return crypto_shash_init(desc);
  169. }
  170. static int ghash_async_update(struct ahash_request *req)
  171. {
  172. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  173. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  174. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  175. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  176. if (!crypto_simd_usable() ||
  177. (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
  178. memcpy(cryptd_req, req, sizeof(*req));
  179. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  180. return crypto_ahash_update(cryptd_req);
  181. } else {
  182. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  183. return shash_ahash_update(req, desc);
  184. }
  185. }
  186. static int ghash_async_final(struct ahash_request *req)
  187. {
  188. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  189. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  190. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  191. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  192. if (!crypto_simd_usable() ||
  193. (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
  194. memcpy(cryptd_req, req, sizeof(*req));
  195. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  196. return crypto_ahash_final(cryptd_req);
  197. } else {
  198. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  199. return crypto_shash_final(desc, req->result);
  200. }
  201. }
  202. static int ghash_async_digest(struct ahash_request *req)
  203. {
  204. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  205. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  206. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  207. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  208. if (!crypto_simd_usable() ||
  209. (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
  210. memcpy(cryptd_req, req, sizeof(*req));
  211. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  212. return crypto_ahash_digest(cryptd_req);
  213. } else {
  214. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  215. struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
  216. desc->tfm = child;
  217. return shash_ahash_digest(req, desc);
  218. }
  219. }
  220. static int ghash_async_import(struct ahash_request *req, const void *in)
  221. {
  222. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  223. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  224. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  225. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  226. desc->tfm = cryptd_ahash_child(ctx->cryptd_tfm);
  227. return crypto_shash_import(desc, in);
  228. }
  229. static int ghash_async_export(struct ahash_request *req, void *out)
  230. {
  231. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  232. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  233. return crypto_shash_export(desc, out);
  234. }
  235. static int ghash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
  236. unsigned int keylen)
  237. {
  238. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  239. struct crypto_ahash *child = &ctx->cryptd_tfm->base;
  240. crypto_ahash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  241. crypto_ahash_set_flags(child, crypto_ahash_get_flags(tfm)
  242. & CRYPTO_TFM_REQ_MASK);
  243. return crypto_ahash_setkey(child, key, keylen);
  244. }
  245. static int ghash_async_init_tfm(struct crypto_tfm *tfm)
  246. {
  247. struct cryptd_ahash *cryptd_tfm;
  248. struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
  249. cryptd_tfm = cryptd_alloc_ahash("ghash-ce-sync", 0, 0);
  250. if (IS_ERR(cryptd_tfm))
  251. return PTR_ERR(cryptd_tfm);
  252. ctx->cryptd_tfm = cryptd_tfm;
  253. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  254. sizeof(struct ahash_request) +
  255. crypto_ahash_reqsize(&cryptd_tfm->base));
  256. return 0;
  257. }
  258. static void ghash_async_exit_tfm(struct crypto_tfm *tfm)
  259. {
  260. struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
  261. cryptd_free_ahash(ctx->cryptd_tfm);
  262. }
  263. static struct ahash_alg ghash_async_alg = {
  264. .init = ghash_async_init,
  265. .update = ghash_async_update,
  266. .final = ghash_async_final,
  267. .setkey = ghash_async_setkey,
  268. .digest = ghash_async_digest,
  269. .import = ghash_async_import,
  270. .export = ghash_async_export,
  271. .halg.digestsize = GHASH_DIGEST_SIZE,
  272. .halg.statesize = sizeof(struct ghash_desc_ctx),
  273. .halg.base = {
  274. .cra_name = "ghash",
  275. .cra_driver_name = "ghash-ce",
  276. .cra_priority = 300,
  277. .cra_flags = CRYPTO_ALG_ASYNC,
  278. .cra_blocksize = GHASH_BLOCK_SIZE,
  279. .cra_ctxsize = sizeof(struct ghash_async_ctx),
  280. .cra_module = THIS_MODULE,
  281. .cra_init = ghash_async_init_tfm,
  282. .cra_exit = ghash_async_exit_tfm,
  283. },
  284. };
  285. static int __init ghash_ce_mod_init(void)
  286. {
  287. int err;
  288. if (!(elf_hwcap & HWCAP_NEON))
  289. return -ENODEV;
  290. if (elf_hwcap2 & HWCAP2_PMULL) {
  291. ghash_alg.base.cra_ctxsize += 3 * sizeof(u64[2]);
  292. static_branch_enable(&use_p64);
  293. }
  294. err = crypto_register_shash(&ghash_alg);
  295. if (err)
  296. return err;
  297. err = crypto_register_ahash(&ghash_async_alg);
  298. if (err)
  299. goto err_shash;
  300. return 0;
  301. err_shash:
  302. crypto_unregister_shash(&ghash_alg);
  303. return err;
  304. }
  305. static void __exit ghash_ce_mod_exit(void)
  306. {
  307. crypto_unregister_ahash(&ghash_async_alg);
  308. crypto_unregister_shash(&ghash_alg);
  309. }
  310. module_init(ghash_ce_mod_init);
  311. module_exit(ghash_ce_mod_exit);