hmac.c 6.6 KB

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