ghash-clmulni-intel_glue.c 8.2 KB

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