ghash-clmulni-intel_glue.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/i387.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 be128 *shash);
  26. void clmul_ghash_update(char *dst, const char *src, unsigned int srclen,
  27. const be128 *shash);
  28. void clmul_ghash_setkey(be128 *shash, const u8 *key);
  29. struct ghash_async_ctx {
  30. struct cryptd_ahash *cryptd_tfm;
  31. };
  32. struct ghash_ctx {
  33. be128 shash;
  34. };
  35. struct ghash_desc_ctx {
  36. u8 buffer[GHASH_BLOCK_SIZE];
  37. u32 bytes;
  38. };
  39. static int ghash_init(struct shash_desc *desc)
  40. {
  41. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  42. memset(dctx, 0, sizeof(*dctx));
  43. return 0;
  44. }
  45. static int ghash_setkey(struct crypto_shash *tfm,
  46. const u8 *key, unsigned int keylen)
  47. {
  48. struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
  49. if (keylen != GHASH_BLOCK_SIZE) {
  50. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  51. return -EINVAL;
  52. }
  53. clmul_ghash_setkey(&ctx->shash, key);
  54. return 0;
  55. }
  56. static int ghash_update(struct shash_desc *desc,
  57. const u8 *src, unsigned int srclen)
  58. {
  59. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  60. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  61. u8 *dst = dctx->buffer;
  62. kernel_fpu_begin();
  63. if (dctx->bytes) {
  64. int n = min(srclen, dctx->bytes);
  65. u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  66. dctx->bytes -= n;
  67. srclen -= n;
  68. while (n--)
  69. *pos++ ^= *src++;
  70. if (!dctx->bytes)
  71. clmul_ghash_mul(dst, &ctx->shash);
  72. }
  73. clmul_ghash_update(dst, src, srclen, &ctx->shash);
  74. kernel_fpu_end();
  75. if (srclen & 0xf) {
  76. src += srclen - (srclen & 0xf);
  77. srclen &= 0xf;
  78. dctx->bytes = GHASH_BLOCK_SIZE - srclen;
  79. while (srclen--)
  80. *dst++ ^= *src++;
  81. }
  82. return 0;
  83. }
  84. static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
  85. {
  86. u8 *dst = dctx->buffer;
  87. if (dctx->bytes) {
  88. u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  89. while (dctx->bytes--)
  90. *tmp++ ^= 0;
  91. kernel_fpu_begin();
  92. clmul_ghash_mul(dst, &ctx->shash);
  93. kernel_fpu_end();
  94. }
  95. dctx->bytes = 0;
  96. }
  97. static int ghash_final(struct shash_desc *desc, u8 *dst)
  98. {
  99. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  100. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  101. u8 *buf = dctx->buffer;
  102. ghash_flush(ctx, dctx);
  103. memcpy(dst, buf, GHASH_BLOCK_SIZE);
  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 = "__ghash-pclmulqdqni",
  116. .cra_priority = 0,
  117. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  118. .cra_blocksize = GHASH_BLOCK_SIZE,
  119. .cra_ctxsize = sizeof(struct ghash_ctx),
  120. .cra_module = THIS_MODULE,
  121. .cra_list = LIST_HEAD_INIT(ghash_alg.base.cra_list),
  122. },
  123. };
  124. static int ghash_async_init(struct ahash_request *req)
  125. {
  126. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  127. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  128. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  129. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  130. if (!irq_fpu_usable()) {
  131. memcpy(cryptd_req, req, sizeof(*req));
  132. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  133. return crypto_ahash_init(cryptd_req);
  134. } else {
  135. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  136. struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
  137. desc->tfm = child;
  138. desc->flags = req->base.flags;
  139. return crypto_shash_init(desc);
  140. }
  141. }
  142. static int ghash_async_update(struct ahash_request *req)
  143. {
  144. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  145. if (!irq_fpu_usable()) {
  146. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  147. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  148. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  149. memcpy(cryptd_req, req, sizeof(*req));
  150. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  151. return crypto_ahash_update(cryptd_req);
  152. } else {
  153. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  154. return shash_ahash_update(req, desc);
  155. }
  156. }
  157. static int ghash_async_final(struct ahash_request *req)
  158. {
  159. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  160. if (!irq_fpu_usable()) {
  161. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  162. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  163. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  164. memcpy(cryptd_req, req, sizeof(*req));
  165. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  166. return crypto_ahash_final(cryptd_req);
  167. } else {
  168. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  169. return crypto_shash_final(desc, req->result);
  170. }
  171. }
  172. static int ghash_async_digest(struct ahash_request *req)
  173. {
  174. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  175. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  176. struct ahash_request *cryptd_req = ahash_request_ctx(req);
  177. struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
  178. if (!irq_fpu_usable()) {
  179. memcpy(cryptd_req, req, sizeof(*req));
  180. ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
  181. return crypto_ahash_digest(cryptd_req);
  182. } else {
  183. struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
  184. struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
  185. desc->tfm = child;
  186. desc->flags = req->base.flags;
  187. return shash_ahash_digest(req, desc);
  188. }
  189. }
  190. static int ghash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
  191. unsigned int keylen)
  192. {
  193. struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
  194. struct crypto_ahash *child = &ctx->cryptd_tfm->base;
  195. int err;
  196. crypto_ahash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  197. crypto_ahash_set_flags(child, crypto_ahash_get_flags(tfm)
  198. & CRYPTO_TFM_REQ_MASK);
  199. err = crypto_ahash_setkey(child, key, keylen);
  200. crypto_ahash_set_flags(tfm, crypto_ahash_get_flags(child)
  201. & CRYPTO_TFM_RES_MASK);
  202. return err;
  203. }
  204. static int ghash_async_init_tfm(struct crypto_tfm *tfm)
  205. {
  206. struct cryptd_ahash *cryptd_tfm;
  207. struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
  208. cryptd_tfm = cryptd_alloc_ahash("__ghash-pclmulqdqni", 0, 0);
  209. if (IS_ERR(cryptd_tfm))
  210. return PTR_ERR(cryptd_tfm);
  211. ctx->cryptd_tfm = cryptd_tfm;
  212. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  213. sizeof(struct ahash_request) +
  214. crypto_ahash_reqsize(&cryptd_tfm->base));
  215. return 0;
  216. }
  217. static void ghash_async_exit_tfm(struct crypto_tfm *tfm)
  218. {
  219. struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
  220. cryptd_free_ahash(ctx->cryptd_tfm);
  221. }
  222. static struct ahash_alg ghash_async_alg = {
  223. .init = ghash_async_init,
  224. .update = ghash_async_update,
  225. .final = ghash_async_final,
  226. .setkey = ghash_async_setkey,
  227. .digest = ghash_async_digest,
  228. .halg = {
  229. .digestsize = GHASH_DIGEST_SIZE,
  230. .base = {
  231. .cra_name = "ghash",
  232. .cra_driver_name = "ghash-clmulni",
  233. .cra_priority = 400,
  234. .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
  235. .cra_blocksize = GHASH_BLOCK_SIZE,
  236. .cra_type = &crypto_ahash_type,
  237. .cra_module = THIS_MODULE,
  238. .cra_list = LIST_HEAD_INIT(ghash_async_alg.halg.base.cra_list),
  239. .cra_init = ghash_async_init_tfm,
  240. .cra_exit = ghash_async_exit_tfm,
  241. },
  242. },
  243. };
  244. static const struct x86_cpu_id pcmul_cpu_id[] = {
  245. X86_FEATURE_MATCH(X86_FEATURE_PCLMULQDQ), /* Pickle-Mickle-Duck */
  246. {}
  247. };
  248. MODULE_DEVICE_TABLE(x86cpu, pcmul_cpu_id);
  249. static int __init ghash_pclmulqdqni_mod_init(void)
  250. {
  251. int err;
  252. if (!x86_match_cpu(pcmul_cpu_id))
  253. return -ENODEV;
  254. err = crypto_register_shash(&ghash_alg);
  255. if (err)
  256. goto err_out;
  257. err = crypto_register_ahash(&ghash_async_alg);
  258. if (err)
  259. goto err_shash;
  260. return 0;
  261. err_shash:
  262. crypto_unregister_shash(&ghash_alg);
  263. err_out:
  264. return err;
  265. }
  266. static void __exit ghash_pclmulqdqni_mod_exit(void)
  267. {
  268. crypto_unregister_ahash(&ghash_async_alg);
  269. crypto_unregister_shash(&ghash_alg);
  270. }
  271. module_init(ghash_pclmulqdqni_mod_init);
  272. module_exit(ghash_pclmulqdqni_mod_exit);
  273. MODULE_LICENSE("GPL");
  274. MODULE_DESCRIPTION("GHASH Message Digest Algorithm, "
  275. "acclerated by PCLMULQDQ-NI");
  276. MODULE_ALIAS("ghash");