ghash-clmulni-intel_glue.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #define GHASH_BLOCK_SIZE 16
  23. #define GHASH_DIGEST_SIZE 16
  24. void clmul_ghash_mul(char *dst, const be128 *shash);
  25. void clmul_ghash_update(char *dst, const char *src, unsigned int srclen,
  26. const be128 *shash);
  27. void clmul_ghash_setkey(be128 *shash, const u8 *key);
  28. struct ghash_async_ctx {
  29. struct cryptd_ahash *cryptd_tfm;
  30. };
  31. struct ghash_ctx {
  32. be128 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. if (keylen != GHASH_BLOCK_SIZE) {
  49. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  50. return -EINVAL;
  51. }
  52. clmul_ghash_setkey(&ctx->shash, key);
  53. return 0;
  54. }
  55. static int ghash_update(struct shash_desc *desc,
  56. const u8 *src, unsigned int srclen)
  57. {
  58. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  59. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  60. u8 *dst = dctx->buffer;
  61. kernel_fpu_begin();
  62. if (dctx->bytes) {
  63. int n = min(srclen, dctx->bytes);
  64. u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  65. dctx->bytes -= n;
  66. srclen -= n;
  67. while (n--)
  68. *pos++ ^= *src++;
  69. if (!dctx->bytes)
  70. clmul_ghash_mul(dst, &ctx->shash);
  71. }
  72. clmul_ghash_update(dst, src, srclen, &ctx->shash);
  73. kernel_fpu_end();
  74. if (srclen & 0xf) {
  75. src += srclen - (srclen & 0xf);
  76. srclen &= 0xf;
  77. dctx->bytes = GHASH_BLOCK_SIZE - srclen;
  78. while (srclen--)
  79. *dst++ ^= *src++;
  80. }
  81. return 0;
  82. }
  83. static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
  84. {
  85. u8 *dst = dctx->buffer;
  86. if (dctx->bytes) {
  87. u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  88. while (dctx->bytes--)
  89. *tmp++ ^= 0;
  90. kernel_fpu_begin();
  91. clmul_ghash_mul(dst, &ctx->shash);
  92. kernel_fpu_end();
  93. }
  94. dctx->bytes = 0;
  95. }
  96. static int ghash_final(struct shash_desc *desc, u8 *dst)
  97. {
  98. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  99. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  100. u8 *buf = dctx->buffer;
  101. ghash_flush(ctx, dctx);
  102. memcpy(dst, buf, GHASH_BLOCK_SIZE);
  103. return 0;
  104. }
  105. static struct shash_alg ghash_alg = {
  106. .digestsize = GHASH_DIGEST_SIZE,
  107. .init = ghash_init,
  108. .update = ghash_update,
  109. .final = ghash_final,
  110. .setkey = ghash_setkey,
  111. .descsize = sizeof(struct ghash_desc_ctx),
  112. .base = {
  113. .cra_name = "__ghash",
  114. .cra_driver_name = "__ghash-pclmulqdqni",
  115. .cra_priority = 0,
  116. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  117. .cra_blocksize = GHASH_BLOCK_SIZE,
  118. .cra_ctxsize = sizeof(struct ghash_ctx),
  119. .cra_module = THIS_MODULE,
  120. .cra_list = LIST_HEAD_INIT(ghash_alg.base.cra_list),
  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 (!irq_fpu_usable()) {
  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 (!irq_fpu_usable()) {
  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 (!irq_fpu_usable()) {
  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 (!irq_fpu_usable()) {
  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 0;
  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("__ghash-pclmulqdqni", 0, 0);
  208. if (IS_ERR(cryptd_tfm))
  209. return PTR_ERR(cryptd_tfm);
  210. ctx->cryptd_tfm = cryptd_tfm;
  211. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  212. sizeof(struct ahash_request) +
  213. crypto_ahash_reqsize(&cryptd_tfm->base));
  214. return 0;
  215. }
  216. static void ghash_async_exit_tfm(struct crypto_tfm *tfm)
  217. {
  218. struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
  219. cryptd_free_ahash(ctx->cryptd_tfm);
  220. }
  221. static struct ahash_alg ghash_async_alg = {
  222. .init = ghash_async_init,
  223. .update = ghash_async_update,
  224. .final = ghash_async_final,
  225. .setkey = ghash_async_setkey,
  226. .digest = ghash_async_digest,
  227. .halg = {
  228. .digestsize = GHASH_DIGEST_SIZE,
  229. .base = {
  230. .cra_name = "ghash",
  231. .cra_driver_name = "ghash-clmulni",
  232. .cra_priority = 400,
  233. .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
  234. .cra_blocksize = GHASH_BLOCK_SIZE,
  235. .cra_type = &crypto_ahash_type,
  236. .cra_module = THIS_MODULE,
  237. .cra_list = LIST_HEAD_INIT(ghash_async_alg.halg.base.cra_list),
  238. .cra_init = ghash_async_init_tfm,
  239. .cra_exit = ghash_async_exit_tfm,
  240. },
  241. },
  242. };
  243. static int __init ghash_pclmulqdqni_mod_init(void)
  244. {
  245. int err;
  246. if (!cpu_has_pclmulqdq) {
  247. printk(KERN_INFO "Intel PCLMULQDQ-NI instructions are not"
  248. " detected.\n");
  249. return -ENODEV;
  250. }
  251. err = crypto_register_shash(&ghash_alg);
  252. if (err)
  253. goto err_out;
  254. err = crypto_register_ahash(&ghash_async_alg);
  255. if (err)
  256. goto err_shash;
  257. return 0;
  258. err_shash:
  259. crypto_unregister_shash(&ghash_alg);
  260. err_out:
  261. return err;
  262. }
  263. static void __exit ghash_pclmulqdqni_mod_exit(void)
  264. {
  265. crypto_unregister_ahash(&ghash_async_alg);
  266. crypto_unregister_shash(&ghash_alg);
  267. }
  268. module_init(ghash_pclmulqdqni_mod_init);
  269. module_exit(ghash_pclmulqdqni_mod_exit);
  270. MODULE_LICENSE("GPL");
  271. MODULE_DESCRIPTION("GHASH Message Digest Algorithm, "
  272. "acclerated by PCLMULQDQ-NI");
  273. MODULE_ALIAS("ghash");