ghash-clmulni-intel_glue.c 9.3 KB

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