sha256.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * SHA-256 hash implementation and interface functions
  3. * Copyright (c) 2003-2007, 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 "sha256.h"
  17. #include "crypto.h"
  18. /**
  19. * hmac_sha256_vector - HMAC-SHA256 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 (32 bytes)
  26. */
  27. void hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
  28. const u8 *addr[], const size_t *len, u8 *mac)
  29. {
  30. unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
  31. unsigned char tk[32];
  32. const u8 *_addr[6];
  33. size_t _len[6], i;
  34. if (num_elem > 5) {
  35. /*
  36. * Fixed limit on the number of fragments to avoid having to
  37. * allocate memory (which could fail).
  38. */
  39. return;
  40. }
  41. /* if key is longer than 64 bytes reset it to key = SHA256(key) */
  42. if (key_len > 64) {
  43. sha256_vector(1, &key, &key_len, tk);
  44. key = tk;
  45. key_len = 32;
  46. }
  47. /* the HMAC_SHA256 transform looks like:
  48. *
  49. * SHA256(K XOR opad, SHA256(K XOR ipad, text))
  50. *
  51. * where K is an n byte key
  52. * ipad is the byte 0x36 repeated 64 times
  53. * opad is the byte 0x5c repeated 64 times
  54. * and text is the data being protected */
  55. /* start out by storing key in ipad */
  56. os_memset(k_pad, 0, sizeof(k_pad));
  57. os_memcpy(k_pad, key, key_len);
  58. /* XOR key with ipad values */
  59. for (i = 0; i < 64; i++)
  60. k_pad[i] ^= 0x36;
  61. /* perform inner SHA256 */
  62. _addr[0] = k_pad;
  63. _len[0] = 64;
  64. for (i = 0; i < num_elem; i++) {
  65. _addr[i + 1] = addr[i];
  66. _len[i + 1] = len[i];
  67. }
  68. sha256_vector(1 + num_elem, _addr, _len, mac);
  69. os_memset(k_pad, 0, sizeof(k_pad));
  70. os_memcpy(k_pad, key, key_len);
  71. /* XOR key with opad values */
  72. for (i = 0; i < 64; i++)
  73. k_pad[i] ^= 0x5c;
  74. /* perform outer SHA256 */
  75. _addr[0] = k_pad;
  76. _len[0] = 64;
  77. _addr[1] = mac;
  78. _len[1] = SHA256_MAC_LEN;
  79. sha256_vector(2, _addr, _len, mac);
  80. }
  81. /**
  82. * hmac_sha256 - HMAC-SHA256 over data buffer (RFC 2104)
  83. * @key: Key for HMAC operations
  84. * @key_len: Length of the key in bytes
  85. * @data: Pointers to the data area
  86. * @data_len: Length of the data area
  87. * @mac: Buffer for the hash (20 bytes)
  88. */
  89. void hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
  90. size_t data_len, u8 *mac)
  91. {
  92. hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
  93. }
  94. /**
  95. * sha256_prf - SHA256-based Pseudo-Random Function (IEEE 802.11r, 8.5.1.5.2)
  96. * @key: Key for PRF
  97. * @key_len: Length of the key in bytes
  98. * @label: A unique label for each purpose of the PRF
  99. * @data: Extra data to bind into the key
  100. * @data_len: Length of the data
  101. * @buf: Buffer for the generated pseudo-random key
  102. * @buf_len: Number of bytes of key to generate
  103. *
  104. * This function is used to derive new, cryptographically separate keys from a
  105. * given key.
  106. */
  107. void sha256_prf(const u8 *key, size_t key_len, const char *label,
  108. const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
  109. {
  110. u16 counter = 1;
  111. size_t pos, plen;
  112. u8 hash[SHA256_MAC_LEN];
  113. const u8 *addr[4];
  114. size_t len[4];
  115. u8 counter_le[2], length_le[2];
  116. addr[0] = counter_le;
  117. len[0] = 2;
  118. addr[1] = (u8 *) label;
  119. len[1] = os_strlen(label);
  120. addr[2] = data;
  121. len[2] = data_len;
  122. addr[3] = length_le;
  123. len[3] = sizeof(length_le);
  124. WPA_PUT_LE16(length_le, buf_len * 8);
  125. pos = 0;
  126. while (pos < buf_len) {
  127. plen = buf_len - pos;
  128. WPA_PUT_LE16(counter_le, counter);
  129. if (plen >= SHA256_MAC_LEN) {
  130. hmac_sha256_vector(key, key_len, 4, addr, len,
  131. &buf[pos]);
  132. pos += SHA256_MAC_LEN;
  133. } else {
  134. hmac_sha256_vector(key, key_len, 4, addr, len, hash);
  135. os_memcpy(&buf[pos], hash, plen);
  136. break;
  137. }
  138. counter++;
  139. }
  140. }