sha1.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * SHA1 hash implementation and interface functions
  3. * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "sha1.h"
  11. #include "crypto.h"
  12. /**
  13. * hmac_sha1_vector - HMAC-SHA1 over data vector (RFC 2104)
  14. * @key: Key for HMAC operations
  15. * @key_len: Length of the key in bytes
  16. * @num_elem: Number of elements in the data vector
  17. * @addr: Pointers to the data areas
  18. * @len: Lengths of the data blocks
  19. * @mac: Buffer for the hash (20 bytes)
  20. * Returns: 0 on success, -1 on failure
  21. */
  22. int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
  23. const u8 *addr[], const size_t *len, u8 *mac)
  24. {
  25. unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
  26. unsigned char tk[20];
  27. const u8 *_addr[6];
  28. size_t _len[6], i;
  29. if (num_elem > 5) {
  30. /*
  31. * Fixed limit on the number of fragments to avoid having to
  32. * allocate memory (which could fail).
  33. */
  34. return -1;
  35. }
  36. /* if key is longer than 64 bytes reset it to key = SHA1(key) */
  37. if (key_len > 64) {
  38. if (sha1_vector(1, &key, &key_len, tk))
  39. return -1;
  40. key = tk;
  41. key_len = 20;
  42. }
  43. /* the HMAC_SHA1 transform looks like:
  44. *
  45. * SHA1(K XOR opad, SHA1(K XOR ipad, text))
  46. *
  47. * where K is an n byte key
  48. * ipad is the byte 0x36 repeated 64 times
  49. * opad is the byte 0x5c repeated 64 times
  50. * and text is the data being protected */
  51. /* start out by storing key in ipad */
  52. os_memset(k_pad, 0, sizeof(k_pad));
  53. os_memcpy(k_pad, key, key_len);
  54. /* XOR key with ipad values */
  55. for (i = 0; i < 64; i++)
  56. k_pad[i] ^= 0x36;
  57. /* perform inner SHA1 */
  58. _addr[0] = k_pad;
  59. _len[0] = 64;
  60. for (i = 0; i < num_elem; i++) {
  61. _addr[i + 1] = addr[i];
  62. _len[i + 1] = len[i];
  63. }
  64. if (sha1_vector(1 + num_elem, _addr, _len, mac))
  65. return -1;
  66. os_memset(k_pad, 0, sizeof(k_pad));
  67. os_memcpy(k_pad, key, key_len);
  68. /* XOR key with opad values */
  69. for (i = 0; i < 64; i++)
  70. k_pad[i] ^= 0x5c;
  71. /* perform outer SHA1 */
  72. _addr[0] = k_pad;
  73. _len[0] = 64;
  74. _addr[1] = mac;
  75. _len[1] = SHA1_MAC_LEN;
  76. return sha1_vector(2, _addr, _len, mac);
  77. }
  78. /**
  79. * hmac_sha1 - HMAC-SHA1 over data buffer (RFC 2104)
  80. * @key: Key for HMAC operations
  81. * @key_len: Length of the key in bytes
  82. * @data: Pointers to the data area
  83. * @data_len: Length of the data area
  84. * @mac: Buffer for the hash (20 bytes)
  85. * Returns: 0 on success, -1 of failure
  86. */
  87. int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  88. u8 *mac)
  89. {
  90. return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
  91. }