md5-non-fips.c 2.9 KB

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