e05210fc36c78a6885814b31a59be024851ca32e
[libeap.git] / src / crypto / sha1.c
1 /*
2  * SHA1 hash implementation and interface functions
3  * Copyright (c) 2003-2005, 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 #include "includes.h"
16
17 #include "common.h"
18 #include "sha1.h"
19 #include "md5.h"
20 #include "crypto.h"
21
22
23 /**
24  * hmac_sha1_vector - HMAC-SHA1 over data vector (RFC 2104)
25  * @key: Key for HMAC operations
26  * @key_len: Length of the key in bytes
27  * @num_elem: Number of elements in the data vector
28  * @addr: Pointers to the data areas
29  * @len: Lengths of the data blocks
30  * @mac: Buffer for the hash (20 bytes)
31  */
32 void hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
33                       const u8 *addr[], const size_t *len, u8 *mac)
34 {
35         unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
36         unsigned char tk[20];
37         const u8 *_addr[6];
38         size_t _len[6], i;
39
40         if (num_elem > 5) {
41                 /*
42                  * Fixed limit on the number of fragments to avoid having to
43                  * allocate memory (which could fail).
44                  */
45                 return;
46         }
47
48         /* if key is longer than 64 bytes reset it to key = SHA1(key) */
49         if (key_len > 64) {
50                 sha1_vector(1, &key, &key_len, tk);
51                 key = tk;
52                 key_len = 20;
53         }
54
55         /* the HMAC_SHA1 transform looks like:
56          *
57          * SHA1(K XOR opad, SHA1(K XOR ipad, text))
58          *
59          * where K is an n byte key
60          * ipad is the byte 0x36 repeated 64 times
61          * opad is the byte 0x5c repeated 64 times
62          * and text is the data being protected */
63
64         /* start out by storing key in ipad */
65         os_memset(k_pad, 0, sizeof(k_pad));
66         os_memcpy(k_pad, key, key_len);
67         /* XOR key with ipad values */
68         for (i = 0; i < 64; i++)
69                 k_pad[i] ^= 0x36;
70
71         /* perform inner SHA1 */
72         _addr[0] = k_pad;
73         _len[0] = 64;
74         for (i = 0; i < num_elem; i++) {
75                 _addr[i + 1] = addr[i];
76                 _len[i + 1] = len[i];
77         }
78         sha1_vector(1 + num_elem, _addr, _len, mac);
79
80         os_memset(k_pad, 0, sizeof(k_pad));
81         os_memcpy(k_pad, key, key_len);
82         /* XOR key with opad values */
83         for (i = 0; i < 64; i++)
84                 k_pad[i] ^= 0x5c;
85
86         /* perform outer SHA1 */
87         _addr[0] = k_pad;
88         _len[0] = 64;
89         _addr[1] = mac;
90         _len[1] = SHA1_MAC_LEN;
91         sha1_vector(2, _addr, _len, mac);
92 }
93
94
95 /**
96  * hmac_sha1 - HMAC-SHA1 over data buffer (RFC 2104)
97  * @key: Key for HMAC operations
98  * @key_len: Length of the key in bytes
99  * @data: Pointers to the data area
100  * @data_len: Length of the data area
101  * @mac: Buffer for the hash (20 bytes)
102  */
103 void hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
104                u8 *mac)
105 {
106         hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
107 }
108
109
110 /**
111  * sha1_prf - SHA1-based Pseudo-Random Function (PRF) (IEEE 802.11i, 8.5.1.1)
112  * @key: Key for PRF
113  * @key_len: Length of the key in bytes
114  * @label: A unique label for each purpose of the PRF
115  * @data: Extra data to bind into the key
116  * @data_len: Length of the data
117  * @buf: Buffer for the generated pseudo-random key
118  * @buf_len: Number of bytes of key to generate
119  *
120  * This function is used to derive new, cryptographically separate keys from a
121  * given key (e.g., PMK in IEEE 802.11i).
122  */
123 void sha1_prf(const u8 *key, size_t key_len, const char *label,
124               const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
125 {
126         u8 counter = 0;
127         size_t pos, plen;
128         u8 hash[SHA1_MAC_LEN];
129         size_t label_len = os_strlen(label) + 1;
130         const unsigned char *addr[3];
131         size_t len[3];
132
133         addr[0] = (u8 *) label;
134         len[0] = label_len;
135         addr[1] = data;
136         len[1] = data_len;
137         addr[2] = &counter;
138         len[2] = 1;
139
140         pos = 0;
141         while (pos < buf_len) {
142                 plen = buf_len - pos;
143                 if (plen >= SHA1_MAC_LEN) {
144                         hmac_sha1_vector(key, key_len, 3, addr, len,
145                                          &buf[pos]);
146                         pos += SHA1_MAC_LEN;
147                 } else {
148                         hmac_sha1_vector(key, key_len, 3, addr, len,
149                                          hash);
150                         os_memcpy(&buf[pos], hash, plen);
151                         break;
152                 }
153                 counter++;
154         }
155 }
156
157
158 #ifndef CONFIG_NO_T_PRF
159 /**
160  * sha1_t_prf - EAP-FAST Pseudo-Random Function (T-PRF)
161  * @key: Key for PRF
162  * @key_len: Length of the key in bytes
163  * @label: A unique label for each purpose of the PRF
164  * @seed: Seed value to bind into the key
165  * @seed_len: Length of the seed
166  * @buf: Buffer for the generated pseudo-random key
167  * @buf_len: Number of bytes of key to generate
168  *
169  * This function is used to derive new, cryptographically separate keys from a
170  * given key for EAP-FAST. T-PRF is defined in RFC 4851, Section 5.5.
171  */
172 void sha1_t_prf(const u8 *key, size_t key_len, const char *label,
173                 const u8 *seed, size_t seed_len, u8 *buf, size_t buf_len)
174 {
175         unsigned char counter = 0;
176         size_t pos, plen;
177         u8 hash[SHA1_MAC_LEN];
178         size_t label_len = os_strlen(label);
179         u8 output_len[2];
180         const unsigned char *addr[5];
181         size_t len[5];
182
183         addr[0] = hash;
184         len[0] = 0;
185         addr[1] = (unsigned char *) label;
186         len[1] = label_len + 1;
187         addr[2] = seed;
188         len[2] = seed_len;
189         addr[3] = output_len;
190         len[3] = 2;
191         addr[4] = &counter;
192         len[4] = 1;
193
194         output_len[0] = (buf_len >> 8) & 0xff;
195         output_len[1] = buf_len & 0xff;
196         pos = 0;
197         while (pos < buf_len) {
198                 counter++;
199                 plen = buf_len - pos;
200                 hmac_sha1_vector(key, key_len, 5, addr, len, hash);
201                 if (plen >= SHA1_MAC_LEN) {
202                         os_memcpy(&buf[pos], hash, SHA1_MAC_LEN);
203                         pos += SHA1_MAC_LEN;
204                 } else {
205                         os_memcpy(&buf[pos], hash, plen);
206                         break;
207                 }
208                 len[0] = SHA1_MAC_LEN;
209         }
210 }
211 #endif /* CONFIG_NO_T_PRF */
212
213
214 #ifndef CONFIG_NO_TLS_PRF
215 /**
216  * tls_prf - Pseudo-Random Function for TLS (TLS-PRF, RFC 2246)
217  * @secret: Key for PRF
218  * @secret_len: Length of the key in bytes
219  * @label: A unique label for each purpose of the PRF
220  * @seed: Seed value to bind into the key
221  * @seed_len: Length of the seed
222  * @out: Buffer for the generated pseudo-random key
223  * @outlen: Number of bytes of key to generate
224  * Returns: 0 on success, -1 on failure.
225  *
226  * This function is used to derive new, cryptographically separate keys from a
227  * given key in TLS. This PRF is defined in RFC 2246, Chapter 5.
228  */
229 int tls_prf(const u8 *secret, size_t secret_len, const char *label,
230             const u8 *seed, size_t seed_len, u8 *out, size_t outlen)
231 {
232         size_t L_S1, L_S2, i;
233         const u8 *S1, *S2;
234         u8 A_MD5[MD5_MAC_LEN], A_SHA1[SHA1_MAC_LEN];
235         u8 P_MD5[MD5_MAC_LEN], P_SHA1[SHA1_MAC_LEN];
236         int MD5_pos, SHA1_pos;
237         const u8 *MD5_addr[3];
238         size_t MD5_len[3];
239         const unsigned char *SHA1_addr[3];
240         size_t SHA1_len[3];
241
242         if (secret_len & 1)
243                 return -1;
244
245         MD5_addr[0] = A_MD5;
246         MD5_len[0] = MD5_MAC_LEN;
247         MD5_addr[1] = (unsigned char *) label;
248         MD5_len[1] = os_strlen(label);
249         MD5_addr[2] = seed;
250         MD5_len[2] = seed_len;
251
252         SHA1_addr[0] = A_SHA1;
253         SHA1_len[0] = SHA1_MAC_LEN;
254         SHA1_addr[1] = (unsigned char *) label;
255         SHA1_len[1] = os_strlen(label);
256         SHA1_addr[2] = seed;
257         SHA1_len[2] = seed_len;
258
259         /* RFC 2246, Chapter 5
260          * A(0) = seed, A(i) = HMAC(secret, A(i-1))
261          * P_hash = HMAC(secret, A(1) + seed) + HMAC(secret, A(2) + seed) + ..
262          * PRF = P_MD5(S1, label + seed) XOR P_SHA-1(S2, label + seed)
263          */
264
265         L_S1 = L_S2 = (secret_len + 1) / 2;
266         S1 = secret;
267         S2 = secret + L_S1;
268         if (secret_len & 1) {
269                 /* The last byte of S1 will be shared with S2 */
270                 S2--;
271         }
272
273         hmac_md5_vector(S1, L_S1, 2, &MD5_addr[1], &MD5_len[1], A_MD5);
274         hmac_sha1_vector(S2, L_S2, 2, &SHA1_addr[1], &SHA1_len[1], A_SHA1);
275
276         MD5_pos = MD5_MAC_LEN;
277         SHA1_pos = SHA1_MAC_LEN;
278         for (i = 0; i < outlen; i++) {
279                 if (MD5_pos == MD5_MAC_LEN) {
280                         hmac_md5_vector(S1, L_S1, 3, MD5_addr, MD5_len, P_MD5);
281                         MD5_pos = 0;
282                         hmac_md5(S1, L_S1, A_MD5, MD5_MAC_LEN, A_MD5);
283                 }
284                 if (SHA1_pos == SHA1_MAC_LEN) {
285                         hmac_sha1_vector(S2, L_S2, 3, SHA1_addr, SHA1_len,
286                                          P_SHA1);
287                         SHA1_pos = 0;
288                         hmac_sha1(S2, L_S2, A_SHA1, SHA1_MAC_LEN, A_SHA1);
289                 }
290
291                 out[i] = P_MD5[MD5_pos] ^ P_SHA1[SHA1_pos];
292
293                 MD5_pos++;
294                 SHA1_pos++;
295         }
296
297         return 0;
298 }
299 #endif /* CONFIG_NO_TLS_PRF */
300
301
302 #ifndef CONFIG_NO_PBKDF2
303
304 static void pbkdf2_sha1_f(const char *passphrase, const char *ssid,
305                           size_t ssid_len, int iterations, unsigned int count,
306                           u8 *digest)
307 {
308         unsigned char tmp[SHA1_MAC_LEN], tmp2[SHA1_MAC_LEN];
309         int i, j;
310         unsigned char count_buf[4];
311         const u8 *addr[2];
312         size_t len[2];
313         size_t passphrase_len = os_strlen(passphrase);
314
315         addr[0] = (u8 *) ssid;
316         len[0] = ssid_len;
317         addr[1] = count_buf;
318         len[1] = 4;
319
320         /* F(P, S, c, i) = U1 xor U2 xor ... Uc
321          * U1 = PRF(P, S || i)
322          * U2 = PRF(P, U1)
323          * Uc = PRF(P, Uc-1)
324          */
325
326         count_buf[0] = (count >> 24) & 0xff;
327         count_buf[1] = (count >> 16) & 0xff;
328         count_buf[2] = (count >> 8) & 0xff;
329         count_buf[3] = count & 0xff;
330         hmac_sha1_vector((u8 *) passphrase, passphrase_len, 2, addr, len, tmp);
331         os_memcpy(digest, tmp, SHA1_MAC_LEN);
332
333         for (i = 1; i < iterations; i++) {
334                 hmac_sha1((u8 *) passphrase, passphrase_len, tmp, SHA1_MAC_LEN,
335                           tmp2);
336                 os_memcpy(tmp, tmp2, SHA1_MAC_LEN);
337                 for (j = 0; j < SHA1_MAC_LEN; j++)
338                         digest[j] ^= tmp2[j];
339         }
340 }
341
342
343 /**
344  * pbkdf2_sha1 - SHA1-based key derivation function (PBKDF2) for IEEE 802.11i
345  * @passphrase: ASCII passphrase
346  * @ssid: SSID
347  * @ssid_len: SSID length in bytes
348  * @iterations: Number of iterations to run
349  * @buf: Buffer for the generated key
350  * @buflen: Length of the buffer in bytes
351  *
352  * This function is used to derive PSK for WPA-PSK. For this protocol,
353  * iterations is set to 4096 and buflen to 32. This function is described in
354  * IEEE Std 802.11-2004, Clause H.4. The main construction is from PKCS#5 v2.0.
355  */
356 void pbkdf2_sha1(const char *passphrase, const char *ssid, size_t ssid_len,
357                  int iterations, u8 *buf, size_t buflen)
358 {
359         unsigned int count = 0;
360         unsigned char *pos = buf;
361         size_t left = buflen, plen;
362         unsigned char digest[SHA1_MAC_LEN];
363
364         while (left > 0) {
365                 count++;
366                 pbkdf2_sha1_f(passphrase, ssid, ssid_len, iterations, count,
367                               digest);
368                 plen = left > SHA1_MAC_LEN ? SHA1_MAC_LEN : left;
369                 os_memcpy(pos, digest, plen);
370                 pos += plen;
371                 left -= plen;
372         }
373 }
374
375 #endif /* CONFIG_NO_PBKDF2 */