hmac.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/string.h>
  22. struct hmac_ctx {
  23. struct crypto_shash *hash;
  24. };
  25. static inline void *align_ptr(void *p, unsigned int align)
  26. {
  27. return (void *)ALIGN((unsigned long)p, align);
  28. }
  29. static inline struct hmac_ctx *hmac_ctx(struct crypto_shash *tfm)
  30. {
  31. return align_ptr(crypto_shash_ctx_aligned(tfm) +
  32. crypto_shash_statesize(tfm) * 2,
  33. crypto_tfm_ctx_alignment());
  34. }
  35. static int hmac_setkey(struct crypto_shash *parent,
  36. const u8 *inkey, unsigned int keylen)
  37. {
  38. int bs = crypto_shash_blocksize(parent);
  39. int ds = crypto_shash_digestsize(parent);
  40. int ss = crypto_shash_statesize(parent);
  41. char *ipad = crypto_shash_ctx_aligned(parent);
  42. char *opad = ipad + ss;
  43. struct hmac_ctx *ctx = align_ptr(opad + ss,
  44. crypto_tfm_ctx_alignment());
  45. struct crypto_shash *hash = ctx->hash;
  46. SHASH_DESC_ON_STACK(shash, hash);
  47. unsigned int i;
  48. shash->tfm = hash;
  49. if (keylen > bs) {
  50. int err;
  51. err = crypto_shash_digest(shash, inkey, keylen, ipad);
  52. if (err)
  53. return err;
  54. keylen = ds;
  55. } else
  56. memcpy(ipad, inkey, keylen);
  57. memset(ipad + keylen, 0, bs - keylen);
  58. memcpy(opad, ipad, bs);
  59. for (i = 0; i < bs; i++) {
  60. ipad[i] ^= HMAC_IPAD_VALUE;
  61. opad[i] ^= HMAC_OPAD_VALUE;
  62. }
  63. return crypto_shash_init(shash) ?:
  64. crypto_shash_update(shash, ipad, bs) ?:
  65. crypto_shash_export(shash, ipad) ?:
  66. crypto_shash_init(shash) ?:
  67. crypto_shash_update(shash, opad, bs) ?:
  68. crypto_shash_export(shash, opad);
  69. }
  70. static int hmac_export(struct shash_desc *pdesc, void *out)
  71. {
  72. struct shash_desc *desc = shash_desc_ctx(pdesc);
  73. return crypto_shash_export(desc, out);
  74. }
  75. static int hmac_import(struct shash_desc *pdesc, const void *in)
  76. {
  77. struct shash_desc *desc = shash_desc_ctx(pdesc);
  78. struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
  79. desc->tfm = ctx->hash;
  80. return crypto_shash_import(desc, in);
  81. }
  82. static int hmac_init(struct shash_desc *pdesc)
  83. {
  84. return hmac_import(pdesc, crypto_shash_ctx_aligned(pdesc->tfm));
  85. }
  86. static int hmac_update(struct shash_desc *pdesc,
  87. const u8 *data, unsigned int nbytes)
  88. {
  89. struct shash_desc *desc = shash_desc_ctx(pdesc);
  90. return crypto_shash_update(desc, data, nbytes);
  91. }
  92. static int hmac_final(struct shash_desc *pdesc, u8 *out)
  93. {
  94. struct crypto_shash *parent = pdesc->tfm;
  95. int ds = crypto_shash_digestsize(parent);
  96. int ss = crypto_shash_statesize(parent);
  97. char *opad = crypto_shash_ctx_aligned(parent) + ss;
  98. struct shash_desc *desc = shash_desc_ctx(pdesc);
  99. return crypto_shash_final(desc, out) ?:
  100. crypto_shash_import(desc, opad) ?:
  101. crypto_shash_finup(desc, out, ds, out);
  102. }
  103. static int hmac_finup(struct shash_desc *pdesc, const u8 *data,
  104. unsigned int nbytes, u8 *out)
  105. {
  106. struct crypto_shash *parent = pdesc->tfm;
  107. int ds = crypto_shash_digestsize(parent);
  108. int ss = crypto_shash_statesize(parent);
  109. char *opad = crypto_shash_ctx_aligned(parent) + ss;
  110. struct shash_desc *desc = shash_desc_ctx(pdesc);
  111. return crypto_shash_finup(desc, data, nbytes, out) ?:
  112. crypto_shash_import(desc, opad) ?:
  113. crypto_shash_finup(desc, out, ds, out);
  114. }
  115. static int hmac_init_tfm(struct crypto_shash *parent)
  116. {
  117. struct crypto_shash *hash;
  118. struct shash_instance *inst = shash_alg_instance(parent);
  119. struct crypto_shash_spawn *spawn = shash_instance_ctx(inst);
  120. struct hmac_ctx *ctx = hmac_ctx(parent);
  121. hash = crypto_spawn_shash(spawn);
  122. if (IS_ERR(hash))
  123. return PTR_ERR(hash);
  124. parent->descsize = sizeof(struct shash_desc) +
  125. crypto_shash_descsize(hash);
  126. ctx->hash = hash;
  127. return 0;
  128. }
  129. static void hmac_exit_tfm(struct crypto_shash *parent)
  130. {
  131. struct hmac_ctx *ctx = hmac_ctx(parent);
  132. crypto_free_shash(ctx->hash);
  133. }
  134. static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
  135. {
  136. struct shash_instance *inst;
  137. struct crypto_shash_spawn *spawn;
  138. struct crypto_alg *alg;
  139. struct shash_alg *salg;
  140. u32 mask;
  141. int err;
  142. int ds;
  143. int ss;
  144. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
  145. if (err)
  146. return err;
  147. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  148. if (!inst)
  149. return -ENOMEM;
  150. spawn = shash_instance_ctx(inst);
  151. err = crypto_grab_shash(spawn, shash_crypto_instance(inst),
  152. crypto_attr_alg_name(tb[1]), 0, mask);
  153. if (err)
  154. goto err_free_inst;
  155. salg = crypto_spawn_shash_alg(spawn);
  156. alg = &salg->base;
  157. /* The underlying hash algorithm must not require a key */
  158. err = -EINVAL;
  159. if (crypto_shash_alg_needs_key(salg))
  160. goto err_free_inst;
  161. ds = salg->digestsize;
  162. ss = salg->statesize;
  163. if (ds > alg->cra_blocksize ||
  164. ss < alg->cra_blocksize)
  165. goto err_free_inst;
  166. err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
  167. if (err)
  168. goto err_free_inst;
  169. inst->alg.base.cra_priority = alg->cra_priority;
  170. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  171. inst->alg.base.cra_alignmask = alg->cra_alignmask;
  172. ss = ALIGN(ss, alg->cra_alignmask + 1);
  173. inst->alg.digestsize = ds;
  174. inst->alg.statesize = ss;
  175. inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
  176. ALIGN(ss * 2, crypto_tfm_ctx_alignment());
  177. inst->alg.init = hmac_init;
  178. inst->alg.update = hmac_update;
  179. inst->alg.final = hmac_final;
  180. inst->alg.finup = hmac_finup;
  181. inst->alg.export = hmac_export;
  182. inst->alg.import = hmac_import;
  183. inst->alg.setkey = hmac_setkey;
  184. inst->alg.init_tfm = hmac_init_tfm;
  185. inst->alg.exit_tfm = hmac_exit_tfm;
  186. inst->free = shash_free_singlespawn_instance;
  187. err = shash_register_instance(tmpl, inst);
  188. if (err) {
  189. err_free_inst:
  190. shash_free_singlespawn_instance(inst);
  191. }
  192. return err;
  193. }
  194. static struct crypto_template hmac_tmpl = {
  195. .name = "hmac",
  196. .create = hmac_create,
  197. .module = THIS_MODULE,
  198. };
  199. static int __init hmac_module_init(void)
  200. {
  201. return crypto_register_template(&hmac_tmpl);
  202. }
  203. static void __exit hmac_module_exit(void)
  204. {
  205. crypto_unregister_template(&hmac_tmpl);
  206. }
  207. subsys_initcall(hmac_module_init);
  208. module_exit(hmac_module_exit);
  209. MODULE_LICENSE("GPL");
  210. MODULE_DESCRIPTION("HMAC hash algorithm");
  211. MODULE_ALIAS_CRYPTO("hmac");