hmac.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. desc.tfm = ctx->child;
  83. desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  84. sg_set_buf(&tmp, ipad, bs);
  85. return unlikely(crypto_hash_init(&desc)) ?:
  86. crypto_hash_update(&desc, &tmp, 1);
  87. }
  88. static int hmac_update(struct hash_desc *pdesc,
  89. struct scatterlist *sg, unsigned int nbytes)
  90. {
  91. struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
  92. struct hash_desc desc;
  93. desc.tfm = ctx->child;
  94. desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  95. return crypto_hash_update(&desc, sg, nbytes);
  96. }
  97. static int hmac_final(struct hash_desc *pdesc, u8 *out)
  98. {
  99. struct crypto_hash *parent = pdesc->tfm;
  100. int bs = crypto_hash_blocksize(parent);
  101. int ds = crypto_hash_digestsize(parent);
  102. char *opad = crypto_hash_ctx_aligned(parent) + bs;
  103. char *digest = opad + bs;
  104. struct hmac_ctx *ctx = align_ptr(digest + ds, sizeof(void *));
  105. struct hash_desc desc;
  106. struct scatterlist tmp;
  107. desc.tfm = ctx->child;
  108. desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  109. sg_set_buf(&tmp, opad, bs + ds);
  110. return unlikely(crypto_hash_final(&desc, digest)) ?:
  111. crypto_hash_digest(&desc, &tmp, bs + ds, out);
  112. }
  113. static int hmac_digest(struct hash_desc *pdesc, struct scatterlist *sg,
  114. unsigned int nbytes, u8 *out)
  115. {
  116. struct crypto_hash *parent = pdesc->tfm;
  117. int bs = crypto_hash_blocksize(parent);
  118. int ds = crypto_hash_digestsize(parent);
  119. char *ipad = crypto_hash_ctx_aligned(parent);
  120. char *opad = ipad + bs;
  121. char *digest = opad + bs;
  122. struct hmac_ctx *ctx = align_ptr(digest + ds, sizeof(void *));
  123. struct hash_desc desc;
  124. struct scatterlist sg1[2];
  125. struct scatterlist sg2[1];
  126. desc.tfm = ctx->child;
  127. desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  128. sg_set_buf(sg1, ipad, bs);
  129. sg1[1].page = (void *)sg;
  130. sg1[1].length = 0;
  131. sg_set_buf(sg2, opad, bs + ds);
  132. return unlikely(crypto_hash_digest(&desc, sg1, nbytes + bs, digest)) ?:
  133. crypto_hash_digest(&desc, sg2, bs + ds, out);
  134. }
  135. static int hmac_init_tfm(struct crypto_tfm *tfm)
  136. {
  137. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  138. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  139. struct hmac_ctx *ctx = hmac_ctx(__crypto_hash_cast(tfm));
  140. tfm = crypto_spawn_tfm(spawn);
  141. if (IS_ERR(tfm))
  142. return PTR_ERR(tfm);
  143. ctx->child = crypto_hash_cast(tfm);
  144. return 0;
  145. }
  146. static void hmac_exit_tfm(struct crypto_tfm *tfm)
  147. {
  148. struct hmac_ctx *ctx = hmac_ctx(__crypto_hash_cast(tfm));
  149. crypto_free_hash(ctx->child);
  150. }
  151. static void hmac_free(struct crypto_instance *inst)
  152. {
  153. crypto_drop_spawn(crypto_instance_ctx(inst));
  154. kfree(inst);
  155. }
  156. static struct crypto_instance *hmac_alloc(void *param, unsigned int len)
  157. {
  158. struct crypto_instance *inst;
  159. struct crypto_alg *alg;
  160. alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_HASH,
  161. CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC);
  162. if (IS_ERR(alg))
  163. return ERR_PTR(PTR_ERR(alg));
  164. inst = crypto_alloc_instance("hmac", alg);
  165. if (IS_ERR(inst))
  166. goto out_put_alg;
  167. inst->alg.cra_flags = CRYPTO_ALG_TYPE_HASH;
  168. inst->alg.cra_priority = alg->cra_priority;
  169. inst->alg.cra_blocksize = alg->cra_blocksize;
  170. inst->alg.cra_alignmask = alg->cra_alignmask;
  171. inst->alg.cra_type = &crypto_hash_type;
  172. inst->alg.cra_hash.digestsize =
  173. (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  174. CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
  175. alg->cra_digest.dia_digestsize;
  176. inst->alg.cra_ctxsize = sizeof(struct hmac_ctx) +
  177. ALIGN(inst->alg.cra_blocksize * 2 +
  178. inst->alg.cra_hash.digestsize,
  179. sizeof(void *));
  180. inst->alg.cra_init = hmac_init_tfm;
  181. inst->alg.cra_exit = hmac_exit_tfm;
  182. inst->alg.cra_hash.init = hmac_init;
  183. inst->alg.cra_hash.update = hmac_update;
  184. inst->alg.cra_hash.final = hmac_final;
  185. inst->alg.cra_hash.digest = hmac_digest;
  186. inst->alg.cra_hash.setkey = hmac_setkey;
  187. out_put_alg:
  188. crypto_mod_put(alg);
  189. return inst;
  190. }
  191. static struct crypto_template hmac_tmpl = {
  192. .name = "hmac",
  193. .alloc = hmac_alloc,
  194. .free = hmac_free,
  195. .module = THIS_MODULE,
  196. };
  197. static int __init hmac_module_init(void)
  198. {
  199. return crypto_register_template(&hmac_tmpl);
  200. }
  201. static void __exit hmac_module_exit(void)
  202. {
  203. crypto_unregister_template(&hmac_tmpl);
  204. }
  205. module_init(hmac_module_init);
  206. module_exit(hmac_module_exit);
  207. MODULE_LICENSE("GPL");
  208. MODULE_DESCRIPTION("HMAC hash algorithm");