hmac.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. kfree(tfm->crt_digest.dit_hmac_block);
  44. }
  45. void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen)
  46. {
  47. unsigned int i;
  48. struct scatterlist tmp;
  49. char *ipad = tfm->crt_digest.dit_hmac_block;
  50. if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
  51. hash_key(tfm, key, *keylen);
  52. *keylen = crypto_tfm_alg_digestsize(tfm);
  53. }
  54. memset(ipad, 0, crypto_tfm_alg_blocksize(tfm));
  55. memcpy(ipad, key, *keylen);
  56. for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
  57. ipad[i] ^= 0x36;
  58. tmp.page = virt_to_page(ipad);
  59. tmp.offset = offset_in_page(ipad);
  60. tmp.length = crypto_tfm_alg_blocksize(tfm);
  61. crypto_digest_init(tfm);
  62. crypto_digest_update(tfm, &tmp, 1);
  63. }
  64. void crypto_hmac_update(struct crypto_tfm *tfm,
  65. struct scatterlist *sg, unsigned int nsg)
  66. {
  67. crypto_digest_update(tfm, sg, nsg);
  68. }
  69. void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
  70. unsigned int *keylen, u8 *out)
  71. {
  72. unsigned int i;
  73. struct scatterlist tmp;
  74. char *opad = tfm->crt_digest.dit_hmac_block;
  75. if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
  76. hash_key(tfm, key, *keylen);
  77. *keylen = crypto_tfm_alg_digestsize(tfm);
  78. }
  79. crypto_digest_final(tfm, out);
  80. memset(opad, 0, crypto_tfm_alg_blocksize(tfm));
  81. memcpy(opad, key, *keylen);
  82. for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
  83. opad[i] ^= 0x5c;
  84. tmp.page = virt_to_page(opad);
  85. tmp.offset = offset_in_page(opad);
  86. tmp.length = crypto_tfm_alg_blocksize(tfm);
  87. crypto_digest_init(tfm);
  88. crypto_digest_update(tfm, &tmp, 1);
  89. tmp.page = virt_to_page(out);
  90. tmp.offset = offset_in_page(out);
  91. tmp.length = crypto_tfm_alg_digestsize(tfm);
  92. crypto_digest_update(tfm, &tmp, 1);
  93. crypto_digest_final(tfm, out);
  94. }
  95. void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
  96. struct scatterlist *sg, unsigned int nsg, u8 *out)
  97. {
  98. crypto_hmac_init(tfm, key, keylen);
  99. crypto_hmac_update(tfm, sg, nsg);
  100. crypto_hmac_final(tfm, key, keylen, out);
  101. }
  102. EXPORT_SYMBOL_GPL(crypto_hmac_init);
  103. EXPORT_SYMBOL_GPL(crypto_hmac_update);
  104. EXPORT_SYMBOL_GPL(crypto_hmac_final);
  105. EXPORT_SYMBOL_GPL(crypto_hmac);