hmac.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Cryptographic API.
  3. *
  4. * HMAC: Keyed-Hashing for Message Authentication (RFC2104).
  5. *
  6. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  7. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  8. *
  9. * The HMAC implementation is derived from USAGI.
  10. * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the Free
  14. * Software Foundation; either version 2 of the License, or (at your option)
  15. * any later version.
  16. *
  17. */
  18. #include <crypto/hmac.h>
  19. #include <crypto/internal/hash.h>
  20. #include <crypto/scatterwalk.h>
  21. #include <linux/err.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/scatterlist.h>
  26. #include <linux/string.h>
  27. struct hmac_ctx {
  28. struct crypto_shash *hash;
  29. };
  30. static inline void *align_ptr(void *p, unsigned int align)
  31. {
  32. return (void *)ALIGN((unsigned long)p, align);
  33. }
  34. static inline struct hmac_ctx *hmac_ctx(struct crypto_shash *tfm)
  35. {
  36. return align_ptr(crypto_shash_ctx_aligned(tfm) +
  37. crypto_shash_statesize(tfm) * 2,
  38. crypto_tfm_ctx_alignment());
  39. }
  40. static int hmac_setkey(struct crypto_shash *parent,
  41. const u8 *inkey, unsigned int keylen)
  42. {
  43. int bs = crypto_shash_blocksize(parent);
  44. int ds = crypto_shash_digestsize(parent);
  45. int ss = crypto_shash_statesize(parent);
  46. char *ipad = crypto_shash_ctx_aligned(parent);
  47. char *opad = ipad + ss;
  48. struct hmac_ctx *ctx = align_ptr(opad + ss,
  49. crypto_tfm_ctx_alignment());
  50. struct crypto_shash *hash = ctx->hash;
  51. SHASH_DESC_ON_STACK(shash, hash);
  52. unsigned int i;
  53. shash->tfm = hash;
  54. shash->flags = crypto_shash_get_flags(parent)
  55. & CRYPTO_TFM_REQ_MAY_SLEEP;
  56. if (keylen > bs) {
  57. int err;
  58. err = crypto_shash_digest(shash, inkey, keylen, ipad);
  59. if (err)
  60. return err;
  61. keylen = ds;
  62. } else
  63. memcpy(ipad, inkey, keylen);
  64. memset(ipad + keylen, 0, bs - keylen);
  65. memcpy(opad, ipad, bs);
  66. for (i = 0; i < bs; i++) {
  67. ipad[i] ^= HMAC_IPAD_VALUE;
  68. opad[i] ^= HMAC_OPAD_VALUE;
  69. }
  70. return crypto_shash_init(shash) ?:
  71. crypto_shash_update(shash, ipad, bs) ?:
  72. crypto_shash_export(shash, ipad) ?:
  73. crypto_shash_init(shash) ?:
  74. crypto_shash_update(shash, opad, bs) ?:
  75. crypto_shash_export(shash, opad);
  76. }
  77. static int hmac_export(struct shash_desc *pdesc, void *out)
  78. {
  79. struct shash_desc *desc = shash_desc_ctx(pdesc);
  80. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  81. return crypto_shash_export(desc, out);
  82. }
  83. static int hmac_import(struct shash_desc *pdesc, const void *in)
  84. {
  85. struct shash_desc *desc = shash_desc_ctx(pdesc);
  86. struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
  87. desc->tfm = ctx->hash;
  88. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  89. return crypto_shash_import(desc, in);
  90. }
  91. static int hmac_init(struct shash_desc *pdesc)
  92. {
  93. return hmac_import(pdesc, crypto_shash_ctx_aligned(pdesc->tfm));
  94. }
  95. static int hmac_update(struct shash_desc *pdesc,
  96. const u8 *data, unsigned int nbytes)
  97. {
  98. struct shash_desc *desc = shash_desc_ctx(pdesc);
  99. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  100. return crypto_shash_update(desc, data, nbytes);
  101. }
  102. static int hmac_final(struct shash_desc *pdesc, u8 *out)
  103. {
  104. struct crypto_shash *parent = pdesc->tfm;
  105. int ds = crypto_shash_digestsize(parent);
  106. int ss = crypto_shash_statesize(parent);
  107. char *opad = crypto_shash_ctx_aligned(parent) + ss;
  108. struct shash_desc *desc = shash_desc_ctx(pdesc);
  109. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  110. return crypto_shash_final(desc, out) ?:
  111. crypto_shash_import(desc, opad) ?:
  112. crypto_shash_finup(desc, out, ds, out);
  113. }
  114. static int hmac_finup(struct shash_desc *pdesc, const u8 *data,
  115. unsigned int nbytes, u8 *out)
  116. {
  117. struct crypto_shash *parent = pdesc->tfm;
  118. int ds = crypto_shash_digestsize(parent);
  119. int ss = crypto_shash_statesize(parent);
  120. char *opad = crypto_shash_ctx_aligned(parent) + ss;
  121. struct shash_desc *desc = shash_desc_ctx(pdesc);
  122. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  123. return crypto_shash_finup(desc, data, nbytes, out) ?:
  124. crypto_shash_import(desc, opad) ?:
  125. crypto_shash_finup(desc, out, ds, out);
  126. }
  127. static int hmac_init_tfm(struct crypto_tfm *tfm)
  128. {
  129. struct crypto_shash *parent = __crypto_shash_cast(tfm);
  130. struct crypto_shash *hash;
  131. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  132. struct crypto_shash_spawn *spawn = crypto_instance_ctx(inst);
  133. struct hmac_ctx *ctx = hmac_ctx(parent);
  134. hash = crypto_spawn_shash(spawn);
  135. if (IS_ERR(hash))
  136. return PTR_ERR(hash);
  137. parent->descsize = sizeof(struct shash_desc) +
  138. crypto_shash_descsize(hash);
  139. ctx->hash = hash;
  140. return 0;
  141. }
  142. static void hmac_exit_tfm(struct crypto_tfm *tfm)
  143. {
  144. struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm));
  145. crypto_free_shash(ctx->hash);
  146. }
  147. static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
  148. {
  149. struct shash_instance *inst;
  150. struct crypto_alg *alg;
  151. struct shash_alg *salg;
  152. int err;
  153. int ds;
  154. int ss;
  155. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
  156. if (err)
  157. return err;
  158. salg = shash_attr_alg(tb[1], 0, 0);
  159. if (IS_ERR(salg))
  160. return PTR_ERR(salg);
  161. alg = &salg->base;
  162. /* The underlying hash algorithm must be unkeyed */
  163. err = -EINVAL;
  164. if (crypto_shash_alg_has_setkey(salg))
  165. goto out_put_alg;
  166. ds = salg->digestsize;
  167. ss = salg->statesize;
  168. if (ds > alg->cra_blocksize ||
  169. ss < alg->cra_blocksize)
  170. goto out_put_alg;
  171. inst = shash_alloc_instance("hmac", alg);
  172. err = PTR_ERR(inst);
  173. if (IS_ERR(inst))
  174. goto out_put_alg;
  175. err = crypto_init_shash_spawn(shash_instance_ctx(inst), salg,
  176. shash_crypto_instance(inst));
  177. if (err)
  178. goto out_free_inst;
  179. inst->alg.base.cra_priority = alg->cra_priority;
  180. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  181. inst->alg.base.cra_alignmask = alg->cra_alignmask;
  182. ss = ALIGN(ss, alg->cra_alignmask + 1);
  183. inst->alg.digestsize = ds;
  184. inst->alg.statesize = ss;
  185. inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
  186. ALIGN(ss * 2, crypto_tfm_ctx_alignment());
  187. inst->alg.base.cra_init = hmac_init_tfm;
  188. inst->alg.base.cra_exit = hmac_exit_tfm;
  189. inst->alg.init = hmac_init;
  190. inst->alg.update = hmac_update;
  191. inst->alg.final = hmac_final;
  192. inst->alg.finup = hmac_finup;
  193. inst->alg.export = hmac_export;
  194. inst->alg.import = hmac_import;
  195. inst->alg.setkey = hmac_setkey;
  196. err = shash_register_instance(tmpl, inst);
  197. if (err) {
  198. out_free_inst:
  199. shash_free_instance(shash_crypto_instance(inst));
  200. }
  201. out_put_alg:
  202. crypto_mod_put(alg);
  203. return err;
  204. }
  205. static struct crypto_template hmac_tmpl = {
  206. .name = "hmac",
  207. .create = hmac_create,
  208. .free = shash_free_instance,
  209. .module = THIS_MODULE,
  210. };
  211. static int __init hmac_module_init(void)
  212. {
  213. return crypto_register_template(&hmac_tmpl);
  214. }
  215. static void __exit hmac_module_exit(void)
  216. {
  217. crypto_unregister_template(&hmac_tmpl);
  218. }
  219. module_init(hmac_module_init);
  220. module_exit(hmac_module_exit);
  221. MODULE_LICENSE("GPL");
  222. MODULE_DESCRIPTION("HMAC hash algorithm");
  223. MODULE_ALIAS_CRYPTO("hmac");