Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SHA-256 hash implementation and interface functions |
| 3 | * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi> |
| 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame^] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
| 10 | |
| 11 | #include "common.h" |
| 12 | #include "sha256.h" |
| 13 | #include "crypto.h" |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * hmac_sha256_vector - HMAC-SHA256 over data vector (RFC 2104) |
| 18 | * @key: Key for HMAC operations |
| 19 | * @key_len: Length of the key in bytes |
| 20 | * @num_elem: Number of elements in the data vector |
| 21 | * @addr: Pointers to the data areas |
| 22 | * @len: Lengths of the data blocks |
| 23 | * @mac: Buffer for the hash (32 bytes) |
| 24 | */ |
| 25 | void hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 26 | const u8 *addr[], const size_t *len, u8 *mac) |
| 27 | { |
| 28 | unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */ |
| 29 | unsigned char tk[32]; |
| 30 | const u8 *_addr[6]; |
| 31 | size_t _len[6], i; |
| 32 | |
| 33 | if (num_elem > 5) { |
| 34 | /* |
| 35 | * Fixed limit on the number of fragments to avoid having to |
| 36 | * allocate memory (which could fail). |
| 37 | */ |
| 38 | return; |
| 39 | } |
| 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 | |
| 48 | /* the HMAC_SHA256 transform looks like: |
| 49 | * |
| 50 | * SHA256(K XOR opad, SHA256(K XOR ipad, text)) |
| 51 | * |
| 52 | * where K is an n byte key |
| 53 | * ipad is the byte 0x36 repeated 64 times |
| 54 | * opad is the byte 0x5c repeated 64 times |
| 55 | * and text is the data being protected */ |
| 56 | |
| 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 | |
| 64 | /* perform inner SHA256 */ |
| 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 | sha256_vector(1 + num_elem, _addr, _len, mac); |
| 72 | |
| 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 | |
| 79 | /* perform outer SHA256 */ |
| 80 | _addr[0] = k_pad; |
| 81 | _len[0] = 64; |
| 82 | _addr[1] = mac; |
| 83 | _len[1] = SHA256_MAC_LEN; |
| 84 | sha256_vector(2, _addr, _len, mac); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * hmac_sha256 - HMAC-SHA256 over data buffer (RFC 2104) |
| 90 | * @key: Key for HMAC operations |
| 91 | * @key_len: Length of the key in bytes |
| 92 | * @data: Pointers to the data area |
| 93 | * @data_len: Length of the data area |
| 94 | * @mac: Buffer for the hash (20 bytes) |
| 95 | */ |
| 96 | void hmac_sha256(const u8 *key, size_t key_len, const u8 *data, |
| 97 | size_t data_len, u8 *mac) |
| 98 | { |
| 99 | hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * sha256_prf - SHA256-based Pseudo-Random Function (IEEE 802.11r, 8.5.1.5.2) |
| 105 | * @key: Key for PRF |
| 106 | * @key_len: Length of the key in bytes |
| 107 | * @label: A unique label for each purpose of the PRF |
| 108 | * @data: Extra data to bind into the key |
| 109 | * @data_len: Length of the data |
| 110 | * @buf: Buffer for the generated pseudo-random key |
| 111 | * @buf_len: Number of bytes of key to generate |
| 112 | * |
| 113 | * This function is used to derive new, cryptographically separate keys from a |
| 114 | * given key. |
| 115 | */ |
| 116 | void sha256_prf(const u8 *key, size_t key_len, const char *label, |
| 117 | const u8 *data, size_t data_len, u8 *buf, size_t buf_len) |
| 118 | { |
| 119 | u16 counter = 1; |
| 120 | size_t pos, plen; |
| 121 | u8 hash[SHA256_MAC_LEN]; |
| 122 | const u8 *addr[4]; |
| 123 | size_t len[4]; |
| 124 | u8 counter_le[2], length_le[2]; |
| 125 | |
| 126 | addr[0] = counter_le; |
| 127 | len[0] = 2; |
| 128 | addr[1] = (u8 *) label; |
| 129 | len[1] = os_strlen(label); |
| 130 | addr[2] = data; |
| 131 | len[2] = data_len; |
| 132 | addr[3] = length_le; |
| 133 | len[3] = sizeof(length_le); |
| 134 | |
| 135 | WPA_PUT_LE16(length_le, buf_len * 8); |
| 136 | pos = 0; |
| 137 | while (pos < buf_len) { |
| 138 | plen = buf_len - pos; |
| 139 | WPA_PUT_LE16(counter_le, counter); |
| 140 | if (plen >= SHA256_MAC_LEN) { |
| 141 | hmac_sha256_vector(key, key_len, 4, addr, len, |
| 142 | &buf[pos]); |
| 143 | pos += SHA256_MAC_LEN; |
| 144 | } else { |
| 145 | hmac_sha256_vector(key, key_len, 4, addr, len, hash); |
| 146 | os_memcpy(&buf[pos], hash, plen); |
| 147 | break; |
| 148 | } |
| 149 | counter++; |
| 150 | } |
| 151 | } |