hmac.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. struct {
  51. struct shash_desc shash;
  52. char ctx[crypto_shash_descsize(hash)];
  53. } desc;
  54. unsigned int i;
  55. desc.shash.tfm = hash;
  56. desc.shash.flags = crypto_shash_get_flags(parent) &
  57. CRYPTO_TFM_REQ_MAY_SLEEP;
  58. if (keylen > bs) {
  59. int err;
  60. err = crypto_shash_digest(&desc.shash, inkey, keylen, ipad);
  61. if (err)
  62. return err;
  63. keylen = ds;
  64. } else
  65. memcpy(ipad, inkey, keylen);
  66. memset(ipad + keylen, 0, bs - keylen);
  67. memcpy(opad, ipad, bs);
  68. for (i = 0; i < bs; i++) {
  69. ipad[i] ^= 0x36;
  70. opad[i] ^= 0x5c;
  71. }
  72. return crypto_shash_init(&desc.shash) ?:
  73. crypto_shash_update(&desc.shash, ipad, bs) ?:
  74. crypto_shash_export(&desc.shash, ipad) ?:
  75. crypto_shash_init(&desc.shash) ?:
  76. crypto_shash_update(&desc.shash, opad, bs) ?:
  77. crypto_shash_export(&desc.shash, opad);
  78. }
  79. static int hmac_export(struct shash_desc *pdesc, void *out)
  80. {
  81. struct shash_desc *desc = shash_desc_ctx(pdesc);
  82. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  83. return crypto_shash_export(desc, out);
  84. }
  85. static int hmac_import(struct shash_desc *pdesc, const void *in)
  86. {
  87. struct shash_desc *desc = shash_desc_ctx(pdesc);
  88. struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
  89. desc->tfm = ctx->hash;
  90. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  91. return crypto_shash_import(desc, in);
  92. }
  93. static int hmac_init(struct shash_desc *pdesc)
  94. {
  95. return hmac_import(pdesc, crypto_shash_ctx_aligned(pdesc->tfm));
  96. }
  97. static int hmac_update(struct shash_desc *pdesc,
  98. const u8 *data, unsigned int nbytes)
  99. {
  100. struct shash_desc *desc = shash_desc_ctx(pdesc);
  101. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  102. return crypto_shash_update(desc, data, nbytes);
  103. }
  104. static int hmac_final(struct shash_desc *pdesc, 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. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  112. return crypto_shash_final(desc, out) ?:
  113. crypto_shash_import(desc, opad) ?:
  114. crypto_shash_finup(desc, out, ds, out);
  115. }
  116. static int hmac_finup(struct shash_desc *pdesc, const u8 *data,
  117. unsigned int nbytes, u8 *out)
  118. {
  119. struct crypto_shash *parent = pdesc->tfm;
  120. int ds = crypto_shash_digestsize(parent);
  121. int ss = crypto_shash_statesize(parent);
  122. char *opad = crypto_shash_ctx_aligned(parent) + ss;
  123. struct shash_desc *desc = shash_desc_ctx(pdesc);
  124. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  125. return crypto_shash_finup(desc, data, nbytes, out) ?:
  126. crypto_shash_import(desc, opad) ?:
  127. crypto_shash_finup(desc, out, ds, out);
  128. }
  129. static int hmac_init_tfm(struct crypto_tfm *tfm)
  130. {
  131. struct crypto_shash *parent = __crypto_shash_cast(tfm);
  132. struct crypto_shash *hash;
  133. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  134. struct crypto_shash_spawn *spawn = crypto_instance_ctx(inst);
  135. struct hmac_ctx *ctx = hmac_ctx(parent);
  136. hash = crypto_spawn_shash(spawn);
  137. if (IS_ERR(hash))
  138. return PTR_ERR(hash);
  139. parent->descsize = sizeof(struct shash_desc) +
  140. crypto_shash_descsize(hash);
  141. ctx->hash = hash;
  142. return 0;
  143. }
  144. static void hmac_exit_tfm(struct crypto_tfm *tfm)
  145. {
  146. struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm));
  147. crypto_free_shash(ctx->hash);
  148. }
  149. static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
  150. {
  151. struct shash_instance *inst;
  152. struct crypto_alg *alg;
  153. struct shash_alg *salg;
  154. int err;
  155. int ds;
  156. int ss;
  157. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
  158. if (err)
  159. return err;
  160. salg = shash_attr_alg(tb[1], 0, 0);
  161. if (IS_ERR(salg))
  162. return PTR_ERR(salg);
  163. err = -EINVAL;
  164. ds = salg->digestsize;
  165. ss = salg->statesize;
  166. alg = &salg->base;
  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");