hmac.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <asm/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. tmp.page = virt_to_page(key);
  27. tmp.offset = offset_in_page(key);
  28. tmp.length = keylen;
  29. crypto_digest_digest(tfm, &tmp, 1, key);
  30. }
  31. int crypto_alloc_hmac_block(struct crypto_tfm *tfm)
  32. {
  33. int ret = 0;
  34. BUG_ON(!crypto_tfm_alg_blocksize(tfm));
  35. tfm->crt_digest.dit_hmac_block = kmalloc(crypto_tfm_alg_blocksize(tfm),
  36. GFP_KERNEL);
  37. if (tfm->crt_digest.dit_hmac_block == NULL)
  38. ret = -ENOMEM;
  39. return ret;
  40. }
  41. void crypto_free_hmac_block(struct crypto_tfm *tfm)
  42. {
  43. if (tfm->crt_digest.dit_hmac_block)
  44. kfree(tfm->crt_digest.dit_hmac_block);
  45. }
  46. void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen)
  47. {
  48. unsigned int i;
  49. struct scatterlist tmp;
  50. char *ipad = tfm->crt_digest.dit_hmac_block;
  51. if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
  52. hash_key(tfm, key, *keylen);
  53. *keylen = crypto_tfm_alg_digestsize(tfm);
  54. }
  55. memset(ipad, 0, crypto_tfm_alg_blocksize(tfm));
  56. memcpy(ipad, key, *keylen);
  57. for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
  58. ipad[i] ^= 0x36;
  59. tmp.page = virt_to_page(ipad);
  60. tmp.offset = offset_in_page(ipad);
  61. tmp.length = crypto_tfm_alg_blocksize(tfm);
  62. crypto_digest_init(tfm);
  63. crypto_digest_update(tfm, &tmp, 1);
  64. }
  65. void crypto_hmac_update(struct crypto_tfm *tfm,
  66. struct scatterlist *sg, unsigned int nsg)
  67. {
  68. crypto_digest_update(tfm, sg, nsg);
  69. }
  70. void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
  71. unsigned int *keylen, u8 *out)
  72. {
  73. unsigned int i;
  74. struct scatterlist tmp;
  75. char *opad = tfm->crt_digest.dit_hmac_block;
  76. if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
  77. hash_key(tfm, key, *keylen);
  78. *keylen = crypto_tfm_alg_digestsize(tfm);
  79. }
  80. crypto_digest_final(tfm, out);
  81. memset(opad, 0, crypto_tfm_alg_blocksize(tfm));
  82. memcpy(opad, key, *keylen);
  83. for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
  84. opad[i] ^= 0x5c;
  85. tmp.page = virt_to_page(opad);
  86. tmp.offset = offset_in_page(opad);
  87. tmp.length = crypto_tfm_alg_blocksize(tfm);
  88. crypto_digest_init(tfm);
  89. crypto_digest_update(tfm, &tmp, 1);
  90. tmp.page = virt_to_page(out);
  91. tmp.offset = offset_in_page(out);
  92. tmp.length = crypto_tfm_alg_digestsize(tfm);
  93. crypto_digest_update(tfm, &tmp, 1);
  94. crypto_digest_final(tfm, out);
  95. }
  96. void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
  97. struct scatterlist *sg, unsigned int nsg, u8 *out)
  98. {
  99. crypto_hmac_init(tfm, key, keylen);
  100. crypto_hmac_update(tfm, sg, nsg);
  101. crypto_hmac_final(tfm, key, keylen, out);
  102. }
  103. EXPORT_SYMBOL_GPL(crypto_hmac_init);
  104. EXPORT_SYMBOL_GPL(crypto_hmac_update);
  105. EXPORT_SYMBOL_GPL(crypto_hmac_final);
  106. EXPORT_SYMBOL_GPL(crypto_hmac);