hmac.c 6.9 KB

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