md5.c 2.8 KB

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