ghash-ce-glue.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. if (!may_use_simd()) {
  130. memcpy(cryptd_req, req, sizeof(*req));
  131. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  132. return crypto_ahash_init(cryptd_req);
  133. } else {
  134. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  135. struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
  136. desc->tfm = child;
  137. desc->flags = req->base.flags;
  138. return crypto_shash_init(desc);
  139. }
  140. }
  141. static int ghash_async_update(struct ahash_request *req)
  142. {
  143. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  144. if (!may_use_simd()) {
  145. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  146. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  147. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  148. memcpy(cryptd_req, req, sizeof(*req));
  149. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  150. return crypto_ahash_update(cryptd_req);
  151. } else {
  152. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  153. return shash_ahash_update(req, desc);
  154. }
  155. }
  156. static int ghash_async_final(struct ahash_request *req)
  157. {
  158. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  159. if (!may_use_simd()) {
  160. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  161. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  162. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  163. memcpy(cryptd_req, req, sizeof(*req));
  164. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  165. return crypto_ahash_final(cryptd_req);
  166. } else {
  167. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  168. return crypto_shash_final(desc, req->result);
  169. }
  170. }
  171. static int ghash_async_digest(struct ahash_request *req)
  172. {
  173. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  174. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  175. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  176. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  177. if (!may_use_simd()) {
  178. memcpy(cryptd_req, req, sizeof(*req));
  179. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  180. return crypto_ahash_digest(cryptd_req);
  181. } else {
  182. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  183. struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
  184. desc->tfm = child;
  185. desc->flags = req->base.flags;
  186. return shash_ahash_digest(req, desc);
  187. }
  188. }
  189. static int ghash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
  190. unsigned int keylen)
  191. {
  192. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  193. struct crypto_ahash *child = &ctx->cryptd_tfm->base;
  194. int err;
  195. crypto_ahash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  196. crypto_ahash_set_flags(child, crypto_ahash_get_flags(tfm)
  197. & CRYPTO_TFM_REQ_MASK);
  198. err = crypto_ahash_setkey(child, key, keylen);
  199. crypto_ahash_set_flags(tfm, crypto_ahash_get_flags(child)
  200. & CRYPTO_TFM_RES_MASK);
  201. return err;
  202. }
  203. static int ghash_async_init_tfm(struct crypto_tfm *tfm)
  204. {
  205. struct cryptd_ahash *cryptd_tfm;
  206. struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
  207. cryptd_tfm = cryptd_alloc_ahash("__driver-ghash-ce",
  208. CRYPTO_ALG_INTERNAL,
  209. CRYPTO_ALG_INTERNAL);
  210. if (IS_ERR(cryptd_tfm))
  211. return PTR_ERR(cryptd_tfm);
  212. ctx->cryptd_tfm = cryptd_tfm;
  213. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  214. sizeof(struct ahash_request) +
  215. crypto_ahash_reqsize(&cryptd_tfm->base));
  216. return 0;
  217. }
  218. static void ghash_async_exit_tfm(struct crypto_tfm *tfm)
  219. {
  220. struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
  221. cryptd_free_ahash(ctx->cryptd_tfm);
  222. }
  223. static struct ahash_alg ghash_async_alg = {
  224. .init = ghash_async_init,
  225. .update = ghash_async_update,
  226. .final = ghash_async_final,
  227. .setkey = ghash_async_setkey,
  228. .digest = ghash_async_digest,
  229. .halg.digestsize = GHASH_DIGEST_SIZE,
  230. .halg.base = {
  231. .cra_name = "ghash",
  232. .cra_driver_name = "ghash-ce",
  233. .cra_priority = 300,
  234. .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
  235. .cra_blocksize = GHASH_BLOCK_SIZE,
  236. .cra_type = &crypto_ahash_type,
  237. .cra_ctxsize = sizeof(struct ghash_async_ctx),
  238. .cra_module = THIS_MODULE,
  239. .cra_init = ghash_async_init_tfm,
  240. .cra_exit = ghash_async_exit_tfm,
  241. },
  242. };
  243. static int __init ghash_ce_mod_init(void)
  244. {
  245. int err;
  246. if (!(elf_hwcap2 & HWCAP2_PMULL))
  247. return -ENODEV;
  248. err = crypto_register_shash(&ghash_alg);
  249. if (err)
  250. return err;
  251. err = crypto_register_ahash(&ghash_async_alg);
  252. if (err)
  253. goto err_shash;
  254. return 0;
  255. err_shash:
  256. crypto_unregister_shash(&ghash_alg);
  257. return err;
  258. }
  259. static void __exit ghash_ce_mod_exit(void)
  260. {
  261. crypto_unregister_ahash(&ghash_async_alg);
  262. crypto_unregister_shash(&ghash_alg);
  263. }
  264. module_init(ghash_ce_mod_init);
  265. module_exit(ghash_ce_mod_exit);