md5.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * MD5 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 "md5.h"
  11. #include "crypto.h"
  12. /**
  13. * hmac_md5_vector - HMAC-MD5 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 (16 bytes)
  20. * Returns: 0 on success, -1 on failure
  21. */
  22. int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
  23. const u8 *addr[], const size_t *len, u8 *mac)
  24. {
  25. u8 k_pad[64]; /* padding - key XORd with ipad/opad */
  26. u8 tk[16];
  27. const u8 *_addr[6];
  28. size_t i, _len[6];
  29. int res;
  30. if (num_elem > 5) {
  31. /*
  32. * Fixed limit on the number of fragments to avoid having to
  33. * allocate memory (which could fail).
  34. */
  35. return -1;
  36. }
  37. /* if key is longer than 64 bytes reset it to key = MD5(key) */
  38. if (key_len > 64) {
  39. if (md5_vector(1, &key, &key_len, tk))
  40. return -1;
  41. key = tk;
  42. key_len = 16;
  43. }
  44. /* the HMAC_MD5 transform looks like:
  45. *
  46. * MD5(K XOR opad, MD5(K XOR ipad, text))
  47. *
  48. * where K is an n byte key
  49. * ipad is the byte 0x36 repeated 64 times
  50. * opad is the byte 0x5c repeated 64 times
  51. * and text is the data being protected */
  52. /* start out by storing key in ipad */
  53. os_memset(k_pad, 0, sizeof(k_pad));
  54. os_memcpy(k_pad, key, key_len);
  55. /* XOR key with ipad values */
  56. for (i = 0; i < 64; i++)
  57. k_pad[i] ^= 0x36;
  58. /* perform inner MD5 */
  59. _addr[0] = k_pad;
  60. _len[0] = 64;
  61. for (i = 0; i < num_elem; i++) {
  62. _addr[i + 1] = addr[i];
  63. _len[i + 1] = len[i];
  64. }
  65. if (md5_vector(1 + num_elem, _addr, _len, mac))
  66. return -1;
  67. os_memset(k_pad, 0, sizeof(k_pad));
  68. os_memcpy(k_pad, key, key_len);
  69. /* XOR key with opad values */
  70. for (i = 0; i < 64; i++)
  71. k_pad[i] ^= 0x5c;
  72. /* perform outer MD5 */
  73. _addr[0] = k_pad;
  74. _len[0] = 64;
  75. _addr[1] = mac;
  76. _len[1] = MD5_MAC_LEN;
  77. res = md5_vector(2, _addr, _len, mac);
  78. os_memset(k_pad, 0, sizeof(k_pad));
  79. os_memset(tk, 0, sizeof(tk));
  80. return res;
  81. }
  82. /**
  83. * hmac_md5 - HMAC-MD5 over data buffer (RFC 2104)
  84. * @key: Key for HMAC operations
  85. * @key_len: Length of the key in bytes
  86. * @data: Pointers to the data area
  87. * @data_len: Length of the data area
  88. * @mac: Buffer for the hash (16 bytes)
  89. * Returns: 0 on success, -1 on failure
  90. */
  91. int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  92. u8 *mac)
  93. {
  94. return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
  95. }