ghash-ce-glue.c 8.9 KB

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