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