hmac.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API.
  4. *
  5. * HMAC: Keyed-Hashing for Message Authentication (RFC2104).
  6. *
  7. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  8. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  9. *
  10. * The HMAC implementation is derived from USAGI.
  11. * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
  12. */
  13. #include <crypto/hmac.h>
  14. #include <crypto/internal/hash.h>
  15. #include <crypto/scatterwalk.h>
  16. #include <linux/err.h>
  17. #include <linux/fips.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/string.h>
  23. struct hmac_ctx {
  24. struct crypto_shash *hash;
  25. /* Contains 'u8 ipad[statesize];', then 'u8 opad[statesize];' */
  26. u8 pads[];
  27. };
  28. static int hmac_setkey(struct crypto_shash *parent,
  29. const u8 *inkey, unsigned int keylen)
  30. {
  31. int bs = crypto_shash_blocksize(parent);
  32. int ds = crypto_shash_digestsize(parent);
  33. int ss = crypto_shash_statesize(parent);
  34. struct hmac_ctx *tctx = crypto_shash_ctx(parent);
  35. struct crypto_shash *hash = tctx->hash;
  36. u8 *ipad = &tctx->pads[0];
  37. u8 *opad = &tctx->pads[ss];
  38. SHASH_DESC_ON_STACK(shash, hash);
  39. unsigned int i;
  40. if (fips_enabled && (keylen < 112 / 8))
  41. return -EINVAL;
  42. shash->tfm = hash;
  43. if (keylen > bs) {
  44. int err;
  45. err = crypto_shash_digest(shash, inkey, keylen, ipad);
  46. if (err)
  47. return err;
  48. keylen = ds;
  49. } else
  50. memcpy(ipad, inkey, keylen);
  51. memset(ipad + keylen, 0, bs - keylen);
  52. memcpy(opad, ipad, bs);
  53. for (i = 0; i < bs; i++) {
  54. ipad[i] ^= HMAC_IPAD_VALUE;
  55. opad[i] ^= HMAC_OPAD_VALUE;
  56. }
  57. return crypto_shash_init(shash) ?:
  58. crypto_shash_update(shash, ipad, bs) ?:
  59. crypto_shash_export(shash, ipad) ?:
  60. crypto_shash_init(shash) ?:
  61. crypto_shash_update(shash, opad, bs) ?:
  62. crypto_shash_export(shash, opad);
  63. }
  64. static int hmac_export(struct shash_desc *pdesc, void *out)
  65. {
  66. struct shash_desc *desc = shash_desc_ctx(pdesc);
  67. return crypto_shash_export(desc, out);
  68. }
  69. static int hmac_import(struct shash_desc *pdesc, const void *in)
  70. {
  71. struct shash_desc *desc = shash_desc_ctx(pdesc);
  72. const struct hmac_ctx *tctx = crypto_shash_ctx(pdesc->tfm);
  73. desc->tfm = tctx->hash;
  74. return crypto_shash_import(desc, in);
  75. }
  76. static int hmac_init(struct shash_desc *pdesc)
  77. {
  78. const struct hmac_ctx *tctx = crypto_shash_ctx(pdesc->tfm);
  79. return hmac_import(pdesc, &tctx->pads[0]);
  80. }
  81. static int hmac_update(struct shash_desc *pdesc,
  82. const u8 *data, unsigned int nbytes)
  83. {
  84. struct shash_desc *desc = shash_desc_ctx(pdesc);
  85. return crypto_shash_update(desc, data, nbytes);
  86. }
  87. static int hmac_final(struct shash_desc *pdesc, u8 *out)
  88. {
  89. struct crypto_shash *parent = pdesc->tfm;
  90. int ds = crypto_shash_digestsize(parent);
  91. int ss = crypto_shash_statesize(parent);
  92. const struct hmac_ctx *tctx = crypto_shash_ctx(parent);
  93. const u8 *opad = &tctx->pads[ss];
  94. struct shash_desc *desc = shash_desc_ctx(pdesc);
  95. return crypto_shash_final(desc, out) ?:
  96. crypto_shash_import(desc, opad) ?:
  97. crypto_shash_finup(desc, out, ds, out);
  98. }
  99. static int hmac_finup(struct shash_desc *pdesc, const u8 *data,
  100. unsigned int nbytes, u8 *out)
  101. {
  102. struct crypto_shash *parent = pdesc->tfm;
  103. int ds = crypto_shash_digestsize(parent);
  104. int ss = crypto_shash_statesize(parent);
  105. const struct hmac_ctx *tctx = crypto_shash_ctx(parent);
  106. const u8 *opad = &tctx->pads[ss];
  107. struct shash_desc *desc = shash_desc_ctx(pdesc);
  108. return crypto_shash_finup(desc, data, nbytes, out) ?:
  109. crypto_shash_import(desc, opad) ?:
  110. crypto_shash_finup(desc, out, ds, out);
  111. }
  112. static int hmac_init_tfm(struct crypto_shash *parent)
  113. {
  114. struct crypto_shash *hash;
  115. struct shash_instance *inst = shash_alg_instance(parent);
  116. struct crypto_shash_spawn *spawn = shash_instance_ctx(inst);
  117. struct hmac_ctx *tctx = crypto_shash_ctx(parent);
  118. hash = crypto_spawn_shash(spawn);
  119. if (IS_ERR(hash))
  120. return PTR_ERR(hash);
  121. parent->descsize = sizeof(struct shash_desc) +
  122. crypto_shash_descsize(hash);
  123. tctx->hash = hash;
  124. return 0;
  125. }
  126. static int hmac_clone_tfm(struct crypto_shash *dst, struct crypto_shash *src)
  127. {
  128. struct hmac_ctx *sctx = crypto_shash_ctx(src);
  129. struct hmac_ctx *dctx = crypto_shash_ctx(dst);
  130. struct crypto_shash *hash;
  131. hash = crypto_clone_shash(sctx->hash);
  132. if (IS_ERR(hash))
  133. return PTR_ERR(hash);
  134. dctx->hash = hash;
  135. return 0;
  136. }
  137. static void hmac_exit_tfm(struct crypto_shash *parent)
  138. {
  139. struct hmac_ctx *tctx = crypto_shash_ctx(parent);
  140. crypto_free_shash(tctx->hash);
  141. }
  142. static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
  143. {
  144. struct shash_instance *inst;
  145. struct crypto_shash_spawn *spawn;
  146. struct crypto_alg *alg;
  147. struct shash_alg *salg;
  148. u32 mask;
  149. int err;
  150. int ds;
  151. int ss;
  152. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
  153. if (err)
  154. return err;
  155. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  156. if (!inst)
  157. return -ENOMEM;
  158. spawn = shash_instance_ctx(inst);
  159. err = crypto_grab_shash(spawn, shash_crypto_instance(inst),
  160. crypto_attr_alg_name(tb[1]), 0, mask);
  161. if (err)
  162. goto err_free_inst;
  163. salg = crypto_spawn_shash_alg(spawn);
  164. alg = &salg->base;
  165. /* The underlying hash algorithm must not require a key */
  166. err = -EINVAL;
  167. if (crypto_shash_alg_needs_key(salg))
  168. goto err_free_inst;
  169. ds = salg->digestsize;
  170. ss = salg->statesize;
  171. if (ds > alg->cra_blocksize ||
  172. ss < alg->cra_blocksize)
  173. goto err_free_inst;
  174. err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
  175. if (err)
  176. goto err_free_inst;
  177. inst->alg.base.cra_priority = alg->cra_priority;
  178. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  179. inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) + (ss * 2);
  180. inst->alg.digestsize = ds;
  181. inst->alg.statesize = ss;
  182. inst->alg.init = hmac_init;
  183. inst->alg.update = hmac_update;
  184. inst->alg.final = hmac_final;
  185. inst->alg.finup = hmac_finup;
  186. inst->alg.export = hmac_export;
  187. inst->alg.import = hmac_import;
  188. inst->alg.setkey = hmac_setkey;
  189. inst->alg.init_tfm = hmac_init_tfm;
  190. inst->alg.clone_tfm = hmac_clone_tfm;
  191. inst->alg.exit_tfm = hmac_exit_tfm;
  192. inst->free = shash_free_singlespawn_instance;
  193. err = shash_register_instance(tmpl, inst);
  194. if (err) {
  195. err_free_inst:
  196. shash_free_singlespawn_instance(inst);
  197. }
  198. return err;
  199. }
  200. static struct crypto_template hmac_tmpl = {
  201. .name = "hmac",
  202. .create = hmac_create,
  203. .module = THIS_MODULE,
  204. };
  205. static int __init hmac_module_init(void)
  206. {
  207. return crypto_register_template(&hmac_tmpl);
  208. }
  209. static void __exit hmac_module_exit(void)
  210. {
  211. crypto_unregister_template(&hmac_tmpl);
  212. }
  213. subsys_initcall(hmac_module_init);
  214. module_exit(hmac_module_exit);
  215. MODULE_LICENSE("GPL");
  216. MODULE_DESCRIPTION("HMAC hash algorithm");
  217. MODULE_ALIAS_CRYPTO("hmac");