1 /*
2  * EAP-PEAP common routines
3  * Copyright (c) 2008-2011, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #ifdef EAP_PEAP
12 
13 #include "utils/common.h"
14 #include "crypto/sha1.h"
15 #include "eap_peer/eap_peap_common.h"
16 
17 int
peap_prfplus(int version,const u8 * key,size_t key_len,const char * label,const u8 * seed,size_t seed_len,u8 * buf,size_t buf_len)18 peap_prfplus(int version, const u8 *key, size_t key_len,
19 	     const char *label, const u8 *seed, size_t seed_len,
20 	     u8 *buf, size_t buf_len)
21 {
22 	unsigned char counter = 0;
23 	size_t pos, plen;
24 	u8 hash[SHA1_MAC_LEN];
25 	size_t label_len = os_strlen(label);
26 	u8 extra[2];
27 	const unsigned char *addr[5];
28 	size_t len[5];
29 
30 	addr[0] = hash;
31 	len[0] = 0;
32 	addr[1] = (unsigned char *) label;
33 	len[1] = label_len;
34 	addr[2] = seed;
35 	len[2] = seed_len;
36 
37 	if (version == 0) {
38 		/*
39 		 * PRF+(K, S, LEN) = T1 | T2 | ... | Tn
40 		 * T1 = HMAC-SHA1(K, S | 0x01 | 0x00 | 0x00)
41 		 * T2 = HMAC-SHA1(K, T1 | S | 0x02 | 0x00 | 0x00)
42 		 * ...
43 		 * Tn = HMAC-SHA1(K, Tn-1 | S | n | 0x00 | 0x00)
44 		 */
45 
46 		extra[0] = 0;
47 		extra[1] = 0;
48 
49 		addr[3] = &counter;
50 		len[3] = 1;
51 		addr[4] = extra;
52 		len[4] = 2;
53 	} else {
54 		/*
55 		 * PRF (K,S,LEN) = T1 | T2 | T3 | T4 | ... where:
56 		 * T1 = HMAC-SHA1(K, S | LEN | 0x01)
57 		 * T2 = HMAC-SHA1 (K, T1 | S | LEN | 0x02)
58 		 * T3 = HMAC-SHA1 (K, T2 | S | LEN | 0x03)
59 		 * T4 = HMAC-SHA1 (K, T3 | S | LEN | 0x04)
60 		 *   ...
61 		 */
62 
63 		extra[0] = buf_len & 0xff;
64 
65 		addr[3] = extra;
66 		len[3] = 1;
67 		addr[4] = &counter;
68 		len[4] = 1;
69 	}
70 
71 	pos = 0;
72 	while (pos < buf_len) {
73 		counter++;
74 		plen = buf_len - pos;
75 		if (hmac_sha1_vector(key, key_len, 5, addr, len, hash) < 0)
76 			return -1;
77 		if (plen >= SHA1_MAC_LEN) {
78 			os_memcpy(&buf[pos], hash, SHA1_MAC_LEN);
79 			pos += SHA1_MAC_LEN;
80 		} else {
81 			os_memcpy(&buf[pos], hash, plen);
82 			break;
83 		}
84 		len[0] = SHA1_MAC_LEN;
85 	}
86 
87 	return 0;
88 }
89 
90 #endif /* EAP_PEAP */
91