hmac.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/slab.h>
  26. #include <linux/string.h>
  27. struct hmac_ctx {
  28. struct shash_desc *desc;
  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_blocksize(tfm) * 2 +
  38. crypto_shash_digestsize(tfm),
  39. crypto_tfm_ctx_alignment());
  40. }
  41. static int hmac_setkey(struct crypto_shash *parent,
  42. const u8 *inkey, unsigned int keylen)
  43. {
  44. int bs = crypto_shash_blocksize(parent);
  45. int ds = crypto_shash_digestsize(parent);
  46. char *ipad = crypto_shash_ctx_aligned(parent);
  47. char *opad = ipad + bs;
  48. char *digest = opad + bs;
  49. struct hmac_ctx *ctx = align_ptr(digest + ds,
  50. crypto_tfm_ctx_alignment());
  51. unsigned int i;
  52. if (keylen > bs) {
  53. int err;
  54. ctx->desc->flags = crypto_shash_get_flags(parent) &
  55. CRYPTO_TFM_REQ_MAY_SLEEP;
  56. err = crypto_shash_digest(ctx->desc, inkey, keylen, digest);
  57. if (err)
  58. return err;
  59. inkey = digest;
  60. keylen = ds;
  61. }
  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 0;
  70. }
  71. static int hmac_init(struct shash_desc *pdesc)
  72. {
  73. struct crypto_shash *parent = pdesc->tfm;
  74. int bs = crypto_shash_blocksize(parent);
  75. int ds = crypto_shash_digestsize(parent);
  76. char *ipad = crypto_shash_ctx_aligned(parent);
  77. struct hmac_ctx *ctx = align_ptr(ipad + bs * 2 + ds,
  78. crypto_tfm_ctx_alignment());
  79. struct shash_desc *desc = shash_desc_ctx(pdesc);
  80. desc->tfm = ctx->desc->tfm;
  81. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  82. return crypto_shash_init(desc) ?:
  83. crypto_shash_update(desc, ipad, bs);
  84. }
  85. static int hmac_update(struct shash_desc *pdesc,
  86. const u8 *data, unsigned int nbytes)
  87. {
  88. struct shash_desc *desc = shash_desc_ctx(pdesc);
  89. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  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 bs = crypto_shash_blocksize(parent);
  96. int ds = crypto_shash_digestsize(parent);
  97. char *opad = crypto_shash_ctx_aligned(parent) + bs;
  98. char *digest = opad + bs;
  99. struct shash_desc *desc = shash_desc_ctx(pdesc);
  100. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  101. return crypto_shash_final(desc, digest) ?:
  102. crypto_shash_digest(desc, opad, bs + ds, out);
  103. }
  104. static int hmac_finup(struct shash_desc *pdesc, const u8 *data,
  105. unsigned int nbytes, u8 *out)
  106. {
  107. struct crypto_shash *parent = pdesc->tfm;
  108. int bs = crypto_shash_blocksize(parent);
  109. int ds = crypto_shash_digestsize(parent);
  110. char *opad = crypto_shash_ctx_aligned(parent) + bs;
  111. char *digest = opad + bs;
  112. struct shash_desc *desc = shash_desc_ctx(pdesc);
  113. desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  114. return crypto_shash_finup(desc, data, nbytes, digest) ?:
  115. crypto_shash_digest(desc, opad, bs + ds, out);
  116. }
  117. static int hmac_init_tfm(struct crypto_tfm *tfm)
  118. {
  119. struct crypto_shash *parent = __crypto_shash_cast(tfm);
  120. struct crypto_shash *hash;
  121. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  122. struct crypto_shash_spawn *spawn = crypto_instance_ctx(inst);
  123. struct hmac_ctx *ctx = hmac_ctx(parent);
  124. hash = crypto_spawn_shash(spawn);
  125. if (IS_ERR(hash))
  126. return PTR_ERR(hash);
  127. parent->descsize = sizeof(struct shash_desc) +
  128. crypto_shash_descsize(hash);
  129. ctx->desc = kmalloc(parent->descsize, GFP_KERNEL);
  130. if (!ctx->desc) {
  131. crypto_free_shash(hash);
  132. return -ENOMEM;
  133. }
  134. ctx->desc->tfm = hash;
  135. return 0;
  136. }
  137. static void hmac_exit_tfm(struct crypto_tfm *tfm)
  138. {
  139. struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm));
  140. crypto_free_shash(ctx->desc->tfm);
  141. kzfree(ctx->desc);
  142. }
  143. static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
  144. {
  145. struct shash_instance *inst;
  146. struct crypto_alg *alg;
  147. struct shash_alg *salg;
  148. int err;
  149. int ds;
  150. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
  151. if (err)
  152. return err;
  153. salg = shash_attr_alg(tb[1], 0, 0);
  154. if (IS_ERR(salg))
  155. return PTR_ERR(salg);
  156. err = -EINVAL;
  157. ds = salg->digestsize;
  158. alg = &salg->base;
  159. if (ds > alg->cra_blocksize)
  160. goto out_put_alg;
  161. inst = shash_alloc_instance("hmac", alg);
  162. err = PTR_ERR(inst);
  163. if (IS_ERR(inst))
  164. goto out_put_alg;
  165. err = crypto_init_shash_spawn(shash_instance_ctx(inst), salg,
  166. shash_crypto_instance(inst));
  167. if (err)
  168. goto out_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. inst->alg.digestsize = ds;
  173. inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
  174. ALIGN(alg->cra_blocksize * 2 + ds,
  175. crypto_tfm_ctx_alignment());
  176. inst->alg.base.cra_init = hmac_init_tfm;
  177. inst->alg.base.cra_exit = hmac_exit_tfm;
  178. inst->alg.init = hmac_init;
  179. inst->alg.update = hmac_update;
  180. inst->alg.final = hmac_final;
  181. inst->alg.finup = hmac_finup;
  182. inst->alg.setkey = hmac_setkey;
  183. err = shash_register_instance(tmpl, inst);
  184. if (err) {
  185. out_free_inst:
  186. shash_free_instance(shash_crypto_instance(inst));
  187. }
  188. out_put_alg:
  189. crypto_mod_put(alg);
  190. return err;
  191. }
  192. static struct crypto_template hmac_tmpl = {
  193. .name = "hmac",
  194. .create = hmac_create,
  195. .free = shash_free_instance,
  196. .module = THIS_MODULE,
  197. };
  198. static int __init hmac_module_init(void)
  199. {
  200. return crypto_register_template(&hmac_tmpl);
  201. }
  202. static void __exit hmac_module_exit(void)
  203. {
  204. crypto_unregister_template(&hmac_tmpl);
  205. }
  206. module_init(hmac_module_init);
  207. module_exit(hmac_module_exit);
  208. MODULE_LICENSE("GPL");
  209. MODULE_DESCRIPTION("HMAC hash algorithm");