ghash-ce-glue.c 9.6 KB

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