hmac.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/algapi.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 crypto_hash *child;
  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_hash *tfm)
  35. {
  36. return align_ptr(crypto_hash_ctx_aligned(tfm) +
  37. crypto_hash_blocksize(tfm) * 2 +
  38. crypto_hash_digestsize(tfm), sizeof(void *));
  39. }
  40. static int hmac_setkey(struct crypto_hash *parent,
  41. const u8 *inkey, unsigned int keylen)
  42. {
  43. int bs = crypto_hash_blocksize(parent);
  44. int ds = crypto_hash_digestsize(parent);
  45. char *ipad = crypto_hash_ctx_aligned(parent);
  46. char *opad = ipad + bs;
  47. char *digest = opad + bs;
  48. struct hmac_ctx *ctx = align_ptr(digest + ds, sizeof(void *));
  49. struct crypto_hash *tfm = ctx->child;
  50. unsigned int i;
  51. if (keylen > bs) {
  52. struct hash_desc desc;
  53. struct scatterlist tmp;
  54. int err;
  55. desc.tfm = tfm;
  56. desc.flags = crypto_hash_get_flags(parent);
  57. desc.flags &= CRYPTO_TFM_REQ_MAY_SLEEP;
  58. sg_init_one(&tmp, inkey, keylen);
  59. err = crypto_hash_digest(&desc, &tmp, keylen, digest);
  60. if (err)
  61. return err;
  62. inkey = digest;
  63. keylen = ds;
  64. }
  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 0;
  73. }
  74. static int hmac_init(struct hash_desc *pdesc)
  75. {
  76. struct crypto_hash *parent = pdesc->tfm;
  77. int bs = crypto_hash_blocksize(parent);
  78. int ds = crypto_hash_digestsize(parent);
  79. char *ipad = crypto_hash_ctx_aligned(parent);
  80. struct hmac_ctx *ctx = align_ptr(ipad + bs * 2 + ds, sizeof(void *));
  81. struct hash_desc desc;
  82. struct scatterlist tmp;
  83. int err;
  84. desc.tfm = ctx->child;
  85. desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  86. sg_init_one(&tmp, ipad, bs);
  87. err = crypto_hash_init(&desc);
  88. if (unlikely(err))
  89. return err;
  90. return crypto_hash_update(&desc, &tmp, bs);
  91. }
  92. static int hmac_update(struct hash_desc *pdesc,
  93. struct scatterlist *sg, unsigned int nbytes)
  94. {
  95. struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
  96. struct hash_desc desc;
  97. desc.tfm = ctx->child;
  98. desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  99. return crypto_hash_update(&desc, sg, nbytes);
  100. }
  101. static int hmac_final(struct hash_desc *pdesc, u8 *out)
  102. {
  103. struct crypto_hash *parent = pdesc->tfm;
  104. int bs = crypto_hash_blocksize(parent);
  105. int ds = crypto_hash_digestsize(parent);
  106. char *opad = crypto_hash_ctx_aligned(parent) + bs;
  107. char *digest = opad + bs;
  108. struct hmac_ctx *ctx = align_ptr(digest + ds, sizeof(void *));
  109. struct hash_desc desc;
  110. struct scatterlist tmp;
  111. int err;
  112. desc.tfm = ctx->child;
  113. desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  114. sg_init_one(&tmp, opad, bs + ds);
  115. err = crypto_hash_final(&desc, digest);
  116. if (unlikely(err))
  117. return err;
  118. return crypto_hash_digest(&desc, &tmp, bs + ds, out);
  119. }
  120. static int hmac_digest(struct hash_desc *pdesc, struct scatterlist *sg,
  121. unsigned int nbytes, u8 *out)
  122. {
  123. struct crypto_hash *parent = pdesc->tfm;
  124. int bs = crypto_hash_blocksize(parent);
  125. int ds = crypto_hash_digestsize(parent);
  126. char *ipad = crypto_hash_ctx_aligned(parent);
  127. char *opad = ipad + bs;
  128. char *digest = opad + bs;
  129. struct hmac_ctx *ctx = align_ptr(digest + ds, sizeof(void *));
  130. struct hash_desc desc;
  131. struct scatterlist sg1[2];
  132. struct scatterlist sg2[1];
  133. int err;
  134. desc.tfm = ctx->child;
  135. desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  136. sg_init_table(sg1, 2);
  137. sg_set_buf(sg1, ipad, bs);
  138. scatterwalk_sg_chain(sg1, 2, sg);
  139. sg_init_table(sg2, 1);
  140. sg_set_buf(sg2, opad, bs + ds);
  141. err = crypto_hash_digest(&desc, sg1, nbytes + bs, digest);
  142. if (unlikely(err))
  143. return err;
  144. return crypto_hash_digest(&desc, sg2, bs + ds, out);
  145. }
  146. static int hmac_init_tfm(struct crypto_tfm *tfm)
  147. {
  148. struct crypto_hash *hash;
  149. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  150. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  151. struct hmac_ctx *ctx = hmac_ctx(__crypto_hash_cast(tfm));
  152. hash = crypto_spawn_hash(spawn);
  153. if (IS_ERR(hash))
  154. return PTR_ERR(hash);
  155. ctx->child = hash;
  156. return 0;
  157. }
  158. static void hmac_exit_tfm(struct crypto_tfm *tfm)
  159. {
  160. struct hmac_ctx *ctx = hmac_ctx(__crypto_hash_cast(tfm));
  161. crypto_free_hash(ctx->child);
  162. }
  163. static void hmac_free(struct crypto_instance *inst)
  164. {
  165. crypto_drop_spawn(crypto_instance_ctx(inst));
  166. kfree(inst);
  167. }
  168. static struct crypto_instance *hmac_alloc(struct rtattr **tb)
  169. {
  170. struct crypto_instance *inst;
  171. struct crypto_alg *alg;
  172. int err;
  173. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_HASH);
  174. if (err)
  175. return ERR_PTR(err);
  176. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_HASH,
  177. CRYPTO_ALG_TYPE_HASH_MASK);
  178. if (IS_ERR(alg))
  179. return ERR_PTR(PTR_ERR(alg));
  180. inst = crypto_alloc_instance("hmac", alg);
  181. if (IS_ERR(inst))
  182. goto out_put_alg;
  183. inst->alg.cra_flags = CRYPTO_ALG_TYPE_HASH;
  184. inst->alg.cra_priority = alg->cra_priority;
  185. inst->alg.cra_blocksize = alg->cra_blocksize;
  186. inst->alg.cra_alignmask = alg->cra_alignmask;
  187. inst->alg.cra_type = &crypto_hash_type;
  188. inst->alg.cra_hash.digestsize =
  189. (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  190. CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
  191. alg->cra_digest.dia_digestsize;
  192. inst->alg.cra_ctxsize = sizeof(struct hmac_ctx) +
  193. ALIGN(inst->alg.cra_blocksize * 2 +
  194. inst->alg.cra_hash.digestsize,
  195. sizeof(void *));
  196. inst->alg.cra_init = hmac_init_tfm;
  197. inst->alg.cra_exit = hmac_exit_tfm;
  198. inst->alg.cra_hash.init = hmac_init;
  199. inst->alg.cra_hash.update = hmac_update;
  200. inst->alg.cra_hash.final = hmac_final;
  201. inst->alg.cra_hash.digest = hmac_digest;
  202. inst->alg.cra_hash.setkey = hmac_setkey;
  203. out_put_alg:
  204. crypto_mod_put(alg);
  205. return inst;
  206. }
  207. static struct crypto_template hmac_tmpl = {
  208. .name = "hmac",
  209. .alloc = hmac_alloc,
  210. .free = hmac_free,
  211. .module = THIS_MODULE,
  212. };
  213. static int __init hmac_module_init(void)
  214. {
  215. return crypto_register_template(&hmac_tmpl);
  216. }
  217. static void __exit hmac_module_exit(void)
  218. {
  219. crypto_unregister_template(&hmac_tmpl);
  220. }
  221. module_init(hmac_module_init);
  222. module_exit(hmac_module_exit);
  223. MODULE_LICENSE("GPL");
  224. MODULE_DESCRIPTION("HMAC hash algorithm");