sha256.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "common.h"
00019 #include "sha256.h"
00020 #include "crypto.h"
00021 
00022 
00033 void hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
00034                         const u8 *addr[], const size_t *len, u8 *mac)
00035 {
00036         unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
00037         unsigned char tk[32];
00038         const u8 *_addr[6];
00039         size_t _len[6], i;
00040 
00041         if (num_elem > 5) {
00042                 /*
00043                  * Fixed limit on the number of fragments to avoid having to
00044                  * allocate memory (which could fail).
00045                  */
00046                 return;
00047         }
00048 
00049         /* if key is longer than 64 bytes reset it to key = SHA256(key) */
00050         if (key_len > 64) {
00051                 sha256_vector(1, &key, &key_len, tk);
00052                 key = tk;
00053                 key_len = 32;
00054         }
00055 
00056         /* the HMAC_SHA256 transform looks like:
00057          *
00058          * SHA256(K XOR opad, SHA256(K XOR ipad, text))
00059          *
00060          * where K is an n byte key
00061          * ipad is the byte 0x36 repeated 64 times
00062          * opad is the byte 0x5c repeated 64 times
00063          * and text is the data being protected */
00064 
00065         /* start out by storing key in ipad */
00066         os_memset(k_pad, 0, sizeof(k_pad));
00067         os_memcpy(k_pad, key, key_len);
00068         /* XOR key with ipad values */
00069         for (i = 0; i < 64; i++)
00070                 k_pad[i] ^= 0x36;
00071 
00072         /* perform inner SHA256 */
00073         _addr[0] = k_pad;
00074         _len[0] = 64;
00075         for (i = 0; i < num_elem; i++) {
00076                 _addr[i + 1] = addr[i];
00077                 _len[i + 1] = len[i];
00078         }
00079         sha256_vector(1 + num_elem, _addr, _len, mac);
00080 
00081         os_memset(k_pad, 0, sizeof(k_pad));
00082         os_memcpy(k_pad, key, key_len);
00083         /* XOR key with opad values */
00084         for (i = 0; i < 64; i++)
00085                 k_pad[i] ^= 0x5c;
00086 
00087         /* perform outer SHA256 */
00088         _addr[0] = k_pad;
00089         _len[0] = 64;
00090         _addr[1] = mac;
00091         _len[1] = SHA256_MAC_LEN;
00092         sha256_vector(2, _addr, _len, mac);
00093 }
00094 
00095 
00105 void hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
00106                  size_t data_len, u8 *mac)
00107 {
00108         hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
00109 }
00110 
00111 
00126 void sha256_prf(const u8 *key, size_t key_len, const char *label,
00127                 const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
00128 {
00129         u16 counter = 1;
00130         size_t pos, plen;
00131         u8 hash[SHA256_MAC_LEN];
00132         const u8 *addr[4];
00133         size_t len[4];
00134         u8 counter_le[2], length_le[2];
00135 
00136         addr[0] = counter_le;
00137         len[0] = 2;
00138         addr[1] = (u8 *) label;
00139         len[1] = os_strlen(label);
00140         addr[2] = data;
00141         len[2] = data_len;
00142         addr[3] = length_le;
00143         len[3] = sizeof(length_le);
00144 
00145         WPA_PUT_LE16(length_le, buf_len * 8);
00146         pos = 0;
00147         while (pos < buf_len) {
00148                 plen = buf_len - pos;
00149                 WPA_PUT_LE16(counter_le, counter);
00150                 if (plen >= SHA256_MAC_LEN) {
00151                         hmac_sha256_vector(key, key_len, 4, addr, len,
00152                                            &buf[pos]);
00153                         pos += SHA256_MAC_LEN;
00154                 } else {
00155                         hmac_sha256_vector(key, key_len, 4, addr, len, hash);
00156                         os_memcpy(&buf[pos], hash, plen);
00157                         break;
00158                 }
00159                 counter++;
00160         }
00161 }
00162 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:50 2009 for hostapd by  doxygen 1.6.1