hmac.c 7.2 KB

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