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