hmac.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. *
  8. * The HMAC implementation is derived from USAGI.
  9. * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. *
  16. */
  17. #include <linux/crypto.h>
  18. #include <linux/mm.h>
  19. #include <linux/highmem.h>
  20. #include <linux/slab.h>
  21. #include <linux/scatterlist.h>
  22. #include "internal.h"
  23. static void hash_key(struct crypto_tfm *tfm, u8 *key, unsigned int keylen)
  24. {
  25. struct scatterlist tmp;
  26. sg_set_buf(&tmp, key, keylen);
  27. crypto_digest_digest(tfm, &tmp, 1, key);
  28. }
  29. int crypto_alloc_hmac_block(struct crypto_tfm *tfm)
  30. {
  31. int ret = 0;
  32. BUG_ON(!crypto_tfm_alg_blocksize(tfm));
  33. tfm->crt_digest.dit_hmac_block = kmalloc(crypto_tfm_alg_blocksize(tfm),
  34. GFP_KERNEL);
  35. if (tfm->crt_digest.dit_hmac_block == NULL)
  36. ret = -ENOMEM;
  37. return ret;
  38. }
  39. void crypto_free_hmac_block(struct crypto_tfm *tfm)
  40. {
  41. kfree(tfm->crt_digest.dit_hmac_block);
  42. }
  43. void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen)
  44. {
  45. unsigned int i;
  46. struct scatterlist tmp;
  47. char *ipad = tfm->crt_digest.dit_hmac_block;
  48. if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
  49. hash_key(tfm, key, *keylen);
  50. *keylen = crypto_tfm_alg_digestsize(tfm);
  51. }
  52. memset(ipad, 0, crypto_tfm_alg_blocksize(tfm));
  53. memcpy(ipad, key, *keylen);
  54. for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
  55. ipad[i] ^= 0x36;
  56. sg_set_buf(&tmp, ipad, crypto_tfm_alg_blocksize(tfm));
  57. crypto_digest_init(tfm);
  58. crypto_digest_update(tfm, &tmp, 1);
  59. }
  60. void crypto_hmac_update(struct crypto_tfm *tfm,
  61. struct scatterlist *sg, unsigned int nsg)
  62. {
  63. crypto_digest_update(tfm, sg, nsg);
  64. }
  65. void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
  66. unsigned int *keylen, u8 *out)
  67. {
  68. unsigned int i;
  69. struct scatterlist tmp;
  70. char *opad = tfm->crt_digest.dit_hmac_block;
  71. if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
  72. hash_key(tfm, key, *keylen);
  73. *keylen = crypto_tfm_alg_digestsize(tfm);
  74. }
  75. crypto_digest_final(tfm, out);
  76. memset(opad, 0, crypto_tfm_alg_blocksize(tfm));
  77. memcpy(opad, key, *keylen);
  78. for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
  79. opad[i] ^= 0x5c;
  80. sg_set_buf(&tmp, opad, crypto_tfm_alg_blocksize(tfm));
  81. crypto_digest_init(tfm);
  82. crypto_digest_update(tfm, &tmp, 1);
  83. sg_set_buf(&tmp, out, crypto_tfm_alg_digestsize(tfm));
  84. crypto_digest_update(tfm, &tmp, 1);
  85. crypto_digest_final(tfm, out);
  86. }
  87. void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
  88. struct scatterlist *sg, unsigned int nsg, u8 *out)
  89. {
  90. crypto_hmac_init(tfm, key, keylen);
  91. crypto_hmac_update(tfm, sg, nsg);
  92. crypto_hmac_final(tfm, key, keylen, out);
  93. }
  94. EXPORT_SYMBOL_GPL(crypto_hmac_init);
  95. EXPORT_SYMBOL_GPL(crypto_hmac_update);
  96. EXPORT_SYMBOL_GPL(crypto_hmac_final);
  97. EXPORT_SYMBOL_GPL(crypto_hmac);