ghash-clmulni-intel_glue.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Accelerated GHASH implementation with Intel PCLMULQDQ-NI
  4. * instructions. This file contains glue code.
  5. *
  6. * Copyright (c) 2009 Intel Corp.
  7. * Author: Huang Ying <[email protected]>
  8. */
  9. #include <linux/err.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/crypto.h>
  14. #include <crypto/algapi.h>
  15. #include <crypto/cryptd.h>
  16. #include <crypto/gf128mul.h>
  17. #include <crypto/internal/hash.h>
  18. #include <crypto/internal/simd.h>
  19. #include <asm/cpu_device_id.h>
  20. #include <asm/simd.h>
  21. #include <asm/unaligned.h>
  22. #define GHASH_BLOCK_SIZE 16
  23. #define GHASH_DIGEST_SIZE 16
  24. void clmul_ghash_mul(char *dst, const u128 *shash);
  25. void clmul_ghash_update(char *dst, const char *src, unsigned int srclen,
  26. const u128 *shash);
  27. struct ghash_async_ctx {
  28. struct cryptd_ahash *cryptd_tfm;
  29. };
  30. struct ghash_ctx {
  31. u128 shash;
  32. };
  33. struct ghash_desc_ctx {
  34. u8 buffer[GHASH_BLOCK_SIZE];
  35. u32 bytes;
  36. };
  37. static int ghash_init(struct shash_desc *desc)
  38. {
  39. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  40. memset(dctx, 0, sizeof(*dctx));
  41. return 0;
  42. }
  43. static int ghash_setkey(struct crypto_shash *tfm,
  44. const u8 *key, unsigned int keylen)
  45. {
  46. struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
  47. u64 a, b;
  48. if (keylen != GHASH_BLOCK_SIZE)
  49. return -EINVAL;
  50. /* perform multiplication by 'x' in GF(2^128) */
  51. a = get_unaligned_be64(key);
  52. b = get_unaligned_be64(key + 8);
  53. ctx->shash.a = (b << 1) | (a >> 63);
  54. ctx->shash.b = (a << 1) | (b >> 63);
  55. if (a >> 63)
  56. ctx->shash.b ^= ((u64)0xc2) << 56;
  57. return 0;
  58. }
  59. static int ghash_update(struct shash_desc *desc,
  60. const u8 *src, unsigned int srclen)
  61. {
  62. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  63. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  64. u8 *dst = dctx->buffer;
  65. kernel_fpu_begin();
  66. if (dctx->bytes) {
  67. int n = min(srclen, dctx->bytes);
  68. u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  69. dctx->bytes -= n;
  70. srclen -= n;
  71. while (n--)
  72. *pos++ ^= *src++;
  73. if (!dctx->bytes)
  74. clmul_ghash_mul(dst, &ctx->shash);
  75. }
  76. clmul_ghash_update(dst, src, srclen, &ctx->shash);
  77. kernel_fpu_end();
  78. if (srclen & 0xf) {
  79. src += srclen - (srclen & 0xf);
  80. srclen &= 0xf;
  81. dctx->bytes = GHASH_BLOCK_SIZE - srclen;
  82. while (srclen--)
  83. *dst++ ^= *src++;
  84. }
  85. return 0;
  86. }
  87. static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
  88. {
  89. u8 *dst = dctx->buffer;
  90. if (dctx->bytes) {
  91. u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  92. while (dctx->bytes--)
  93. *tmp++ ^= 0;
  94. kernel_fpu_begin();
  95. clmul_ghash_mul(dst, &ctx->shash);
  96. kernel_fpu_end();
  97. }
  98. dctx->bytes = 0;
  99. }
  100. static int ghash_final(struct shash_desc *desc, u8 *dst)
  101. {
  102. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  103. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  104. u8 *buf = dctx->buffer;
  105. ghash_flush(ctx, dctx);
  106. memcpy(dst, buf, GHASH_BLOCK_SIZE);
  107. return 0;
  108. }
  109. static struct shash_alg ghash_alg = {
  110. .digestsize = GHASH_DIGEST_SIZE,
  111. .init = ghash_init,
  112. .update = ghash_update,
  113. .final = ghash_final,
  114. .setkey = ghash_setkey,
  115. .descsize = sizeof(struct ghash_desc_ctx),
  116. .base = {
  117. .cra_name = "__ghash",
  118. .cra_driver_name = "__ghash-pclmulqdqni",
  119. .cra_priority = 0,
  120. .cra_flags = CRYPTO_ALG_INTERNAL,
  121. .cra_blocksize = GHASH_BLOCK_SIZE,
  122. .cra_ctxsize = sizeof(struct ghash_ctx),
  123. .cra_module = THIS_MODULE,
  124. },
  125. };
  126. static int ghash_async_init(struct ahash_request *req)
  127. {
  128. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  129. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  130. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  131. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  132. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  133. struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
  134. desc->tfm = child;
  135. return crypto_shash_init(desc);
  136. }
  137. static int ghash_async_update(struct ahash_request *req)
  138. {
  139. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  140. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  141. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  142. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  143. if (!crypto_simd_usable() ||
  144. (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
  145. memcpy(cryptd_req, req, sizeof(*req));
  146. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  147. return crypto_ahash_update(cryptd_req);
  148. } else {
  149. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  150. return shash_ahash_update(req, desc);
  151. }
  152. }
  153. static int ghash_async_final(struct ahash_request *req)
  154. {
  155. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  156. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  157. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  158. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  159. if (!crypto_simd_usable() ||
  160. (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
  161. memcpy(cryptd_req, req, sizeof(*req));
  162. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  163. return crypto_ahash_final(cryptd_req);
  164. } else {
  165. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  166. return crypto_shash_final(desc, req->result);
  167. }
  168. }
  169. static int ghash_async_import(struct ahash_request *req, const void *in)
  170. {
  171. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  172. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  173. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  174. ghash_async_init(req);
  175. memcpy(dctx, in, sizeof(*dctx));
  176. return 0;
  177. }
  178. static int ghash_async_export(struct ahash_request *req, void *out)
  179. {
  180. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  181. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  182. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  183. memcpy(out, dctx, sizeof(*dctx));
  184. return 0;
  185. }
  186. static int ghash_async_digest(struct ahash_request *req)
  187. {
  188. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  189. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  190. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  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_digest(cryptd_req);
  197. } else {
  198. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  199. struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
  200. desc->tfm = child;
  201. return shash_ahash_digest(req, desc);
  202. }
  203. }
  204. static int ghash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
  205. unsigned int keylen)
  206. {
  207. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  208. struct crypto_ahash *child = &ctx->cryptd_tfm->base;
  209. crypto_ahash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  210. crypto_ahash_set_flags(child, crypto_ahash_get_flags(tfm)
  211. & CRYPTO_TFM_REQ_MASK);
  212. return crypto_ahash_setkey(child, key, keylen);
  213. }
  214. static int ghash_async_init_tfm(struct crypto_tfm *tfm)
  215. {
  216. struct cryptd_ahash *cryptd_tfm;
  217. struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
  218. cryptd_tfm = cryptd_alloc_ahash("__ghash-pclmulqdqni",
  219. CRYPTO_ALG_INTERNAL,
  220. CRYPTO_ALG_INTERNAL);
  221. if (IS_ERR(cryptd_tfm))
  222. return PTR_ERR(cryptd_tfm);
  223. ctx->cryptd_tfm = cryptd_tfm;
  224. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  225. sizeof(struct ahash_request) +
  226. crypto_ahash_reqsize(&cryptd_tfm->base));
  227. return 0;
  228. }
  229. static void ghash_async_exit_tfm(struct crypto_tfm *tfm)
  230. {
  231. struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
  232. cryptd_free_ahash(ctx->cryptd_tfm);
  233. }
  234. static struct ahash_alg ghash_async_alg = {
  235. .init = ghash_async_init,
  236. .update = ghash_async_update,
  237. .final = ghash_async_final,
  238. .setkey = ghash_async_setkey,
  239. .digest = ghash_async_digest,
  240. .export = ghash_async_export,
  241. .import = ghash_async_import,
  242. .halg = {
  243. .digestsize = GHASH_DIGEST_SIZE,
  244. .statesize = sizeof(struct ghash_desc_ctx),
  245. .base = {
  246. .cra_name = "ghash",
  247. .cra_driver_name = "ghash-clmulni",
  248. .cra_priority = 400,
  249. .cra_ctxsize = sizeof(struct ghash_async_ctx),
  250. .cra_flags = CRYPTO_ALG_ASYNC,
  251. .cra_blocksize = GHASH_BLOCK_SIZE,
  252. .cra_module = THIS_MODULE,
  253. .cra_init = ghash_async_init_tfm,
  254. .cra_exit = ghash_async_exit_tfm,
  255. },
  256. },
  257. };
  258. static const struct x86_cpu_id pcmul_cpu_id[] = {
  259. X86_MATCH_FEATURE(X86_FEATURE_PCLMULQDQ, NULL), /* Pickle-Mickle-Duck */
  260. {}
  261. };
  262. MODULE_DEVICE_TABLE(x86cpu, pcmul_cpu_id);
  263. static int __init ghash_pclmulqdqni_mod_init(void)
  264. {
  265. int err;
  266. if (!x86_match_cpu(pcmul_cpu_id))
  267. return -ENODEV;
  268. err = crypto_register_shash(&ghash_alg);
  269. if (err)
  270. goto err_out;
  271. err = crypto_register_ahash(&ghash_async_alg);
  272. if (err)
  273. goto err_shash;
  274. return 0;
  275. err_shash:
  276. crypto_unregister_shash(&ghash_alg);
  277. err_out:
  278. return err;
  279. }
  280. static void __exit ghash_pclmulqdqni_mod_exit(void)
  281. {
  282. crypto_unregister_ahash(&ghash_async_alg);
  283. crypto_unregister_shash(&ghash_alg);
  284. }
  285. module_init(ghash_pclmulqdqni_mod_init);
  286. module_exit(ghash_pclmulqdqni_mod_exit);
  287. MODULE_LICENSE("GPL");
  288. MODULE_DESCRIPTION("GHASH hash function, accelerated by PCLMULQDQ-NI");
  289. MODULE_ALIAS_CRYPTO("ghash");