1/*
2 * EAP server/peer: EAP-pwd shared routines
3 * Copyright (c) 2010, Dan Harkins <dharkins@lounge.org>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10#include "common.h"
11#include "utils/const_time.h"
12#include "common/dragonfly.h"
13#include "crypto/sha256.h"
14#include "crypto/crypto.h"
15#include "eap_defs.h"
16#include "eap_pwd_common.h"
17
18#define MAX_ECC_PRIME_LEN 66
19
20
21/* The random function H(x) = HMAC-SHA256(0^32, x) */
22struct crypto_hash * eap_pwd_h_init(void)
23{
24	u8 allzero[SHA256_MAC_LEN];
25	os_memset(allzero, 0, SHA256_MAC_LEN);
26	return crypto_hash_init(CRYPTO_HASH_ALG_HMAC_SHA256, allzero,
27				SHA256_MAC_LEN);
28}
29
30
31void eap_pwd_h_update(struct crypto_hash *hash, const u8 *data, size_t len)
32{
33	crypto_hash_update(hash, data, len);
34}
35
36
37void eap_pwd_h_final(struct crypto_hash *hash, u8 *digest)
38{
39	size_t len = SHA256_MAC_LEN;
40	crypto_hash_finish(hash, digest, &len);
41}
42
43
44/* a counter-based KDF based on NIST SP800-108 */
45static int eap_pwd_kdf(const u8 *key, size_t keylen, const u8 *label,
46		       size_t labellen, u8 *result, size_t resultbitlen)
47{
48	struct crypto_hash *hash;
49	u8 digest[SHA256_MAC_LEN];
50	u16 i, ctr, L;
51	size_t resultbytelen, len = 0, mdlen;
52
53	resultbytelen = (resultbitlen + 7) / 8;
54	ctr = 0;
55	L = htons(resultbitlen);
56	while (len < resultbytelen) {
57		ctr++;
58		i = htons(ctr);
59		hash = crypto_hash_init(CRYPTO_HASH_ALG_HMAC_SHA256,
60					key, keylen);
61		if (hash == NULL)
62			return -1;
63		if (ctr > 1)
64			crypto_hash_update(hash, digest, SHA256_MAC_LEN);
65		crypto_hash_update(hash, (u8 *) &i, sizeof(u16));
66		crypto_hash_update(hash, label, labellen);
67		crypto_hash_update(hash, (u8 *) &L, sizeof(u16));
68		mdlen = SHA256_MAC_LEN;
69		if (crypto_hash_finish(hash, digest, &mdlen) < 0)
70			return -1;
71		if ((len + mdlen) > resultbytelen)
72			os_memcpy(result + len, digest, resultbytelen - len);
73		else
74			os_memcpy(result + len, digest, mdlen);
75		len += mdlen;
76	}
77
78	/* since we're expanding to a bit length, mask off the excess */
79	if (resultbitlen % 8) {
80		u8 mask = 0xff;
81		mask <<= (8 - (resultbitlen % 8));
82		result[resultbytelen - 1] &= mask;
83	}
84
85	return 0;
86}
87
88
89EAP_PWD_group * get_eap_pwd_group(u16 num)
90{
91	EAP_PWD_group *grp;
92
93	if (!dragonfly_suitable_group(num, 1)) {
94		wpa_printf(MSG_INFO, "EAP-pwd: unsuitable group %u", num);
95		return NULL;
96	}
97	grp = os_zalloc(sizeof(EAP_PWD_group));
98	if (!grp)
99		return NULL;
100	grp->group = crypto_ec_init(num);
101	if (!grp->group) {
102		wpa_printf(MSG_INFO, "EAP-pwd: unable to create EC group");
103		os_free(grp);
104		return NULL;
105	}
106
107	grp->group_num = num;
108	wpa_printf(MSG_INFO, "EAP-pwd: provisioned group %d", num);
109
110	return grp;
111}
112
113
114static void buf_shift_right(u8 *buf, size_t len, size_t bits)
115{
116	size_t i;
117	for (i = len - 1; i > 0; i--)
118		buf[i] = (buf[i - 1] << (8 - bits)) | (buf[i] >> bits);
119	buf[0] >>= bits;
120}
121
122
123/*
124 * compute a "random" secret point on an elliptic curve based
125 * on the password and identities.
126 */
127int compute_password_element(EAP_PWD_group *grp, u16 num,
128			     const u8 *password, size_t password_len,
129			     const u8 *id_server, size_t id_server_len,
130			     const u8 *id_peer, size_t id_peer_len,
131			     const u8 *token)
132{
133	struct crypto_bignum *qr = NULL, *qnr = NULL;
134	u8 qr_bin[MAX_ECC_PRIME_LEN];
135	u8 qnr_bin[MAX_ECC_PRIME_LEN];
136	u8 qr_or_qnr_bin[MAX_ECC_PRIME_LEN];
137	u8 x_bin[MAX_ECC_PRIME_LEN];
138	u8 prime_bin[MAX_ECC_PRIME_LEN];
139	struct crypto_bignum *tmp2 = NULL;
140	struct crypto_hash *hash;
141	unsigned char pwe_digest[SHA256_MAC_LEN], *prfbuf = NULL, ctr;
142	int ret = 0, res;
143	u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
144		       * mask */
145	size_t primebytelen = 0, primebitlen;
146	struct crypto_bignum *x_candidate = NULL;
147	const struct crypto_bignum *prime;
148	u8 found_ctr = 0, is_odd = 0;
149	int cmp_prime;
150	unsigned int in_range;
151
152	if (grp->pwe)
153		return -1;
154
155	os_memset(x_bin, 0, sizeof(x_bin));
156
157	prime = crypto_ec_get_prime(grp->group);
158	primebitlen = crypto_ec_prime_len_bits(grp->group);
159	primebytelen = crypto_ec_prime_len(grp->group);
160	if (crypto_bignum_to_bin(prime, prime_bin, sizeof(prime_bin),
161				 primebytelen) < 0)
162		return -1;
163	grp->pwe = crypto_ec_point_init(grp->group);
164	if (!grp->pwe) {
165		wpa_printf(MSG_INFO, "EAP-pwd: unable to create bignums");
166		goto fail;
167	}
168
169	if ((prfbuf = os_malloc(primebytelen)) == NULL) {
170		wpa_printf(MSG_INFO, "EAP-pwd: unable to malloc space for prf "
171			   "buffer");
172		goto fail;
173	}
174
175	/* get a random quadratic residue and nonresidue */
176	if (dragonfly_get_random_qr_qnr(prime, &qr, &qnr) < 0 ||
177	    crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin),
178				 primebytelen) < 0 ||
179	    crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin),
180				 primebytelen) < 0)
181		goto fail;
182
183	os_memset(prfbuf, 0, primebytelen);
184	ctr = 0;
185
186	/*
187	 * Run through the hunting-and-pecking loop 40 times to mask the time
188	 * necessary to find PWE. The odds of PWE not being found in 40 loops is
189	 * roughly 1 in 1 trillion.
190	 */
191	while (ctr < 40) {
192		ctr++;
193
194		/*
195		 * compute counter-mode password value and stretch to prime
196		 *    pwd-seed = H(token | peer-id | server-id | password |
197		 *		   counter)
198		 */
199		hash = eap_pwd_h_init();
200		if (hash == NULL)
201			goto fail;
202		eap_pwd_h_update(hash, token, sizeof(u32));
203		eap_pwd_h_update(hash, id_peer, id_peer_len);
204		eap_pwd_h_update(hash, id_server, id_server_len);
205		eap_pwd_h_update(hash, password, password_len);
206		eap_pwd_h_update(hash, &ctr, sizeof(ctr));
207		eap_pwd_h_final(hash, pwe_digest);
208
209		is_odd = const_time_select_u8(
210			found, is_odd, pwe_digest[SHA256_MAC_LEN - 1] & 0x01);
211		if (eap_pwd_kdf(pwe_digest, SHA256_MAC_LEN,
212				(u8 *) "EAP-pwd Hunting And Pecking",
213				os_strlen("EAP-pwd Hunting And Pecking"),
214				prfbuf, primebitlen) < 0)
215			goto fail;
216		if (primebitlen % 8)
217			buf_shift_right(prfbuf, primebytelen,
218					8 - primebitlen % 8);
219		cmp_prime = const_time_memcmp(prfbuf, prime_bin, primebytelen);
220		/* Create a const_time mask for selection based on prf result
221		 * being smaller than prime. */
222		in_range = const_time_fill_msb((unsigned int) cmp_prime);
223		/* The algorithm description would skip the next steps if
224		 * cmp_prime >= 0, but go through them regardless to minimize
225		 * externally observable differences in behavior. */
226
227		crypto_bignum_deinit(x_candidate, 1);
228		x_candidate = crypto_bignum_init_set(prfbuf, primebytelen);
229		if (!x_candidate) {
230			wpa_printf(MSG_INFO,
231				   "EAP-pwd: unable to create x_candidate");
232			goto fail;
233		}
234
235		wpa_hexdump_key(MSG_DEBUG, "EAP-pwd: x_candidate",
236				prfbuf, primebytelen);
237		const_time_select_bin(found, x_bin, prfbuf, primebytelen,
238				      x_bin);
239
240		/*
241		 * compute y^2 using the equation of the curve
242		 *
243		 *      y^2 = x^3 + ax + b
244		 */
245		crypto_bignum_deinit(tmp2, 1);
246		tmp2 = crypto_ec_point_compute_y_sqr(grp->group, x_candidate);
247		if (!tmp2)
248			goto fail;
249
250		res = dragonfly_is_quadratic_residue_blind(grp->group, qr_bin,
251							   qnr_bin, tmp2);
252		if (res < 0)
253			goto fail;
254		found_ctr = const_time_select_u8(found, found_ctr, ctr);
255		/* found is 0 or 0xff here and res is 0 or 1. Bitwise OR of them
256		 * (with res converted to 0/0xff and masked with prf being below
257		 * prime) handles this in constant time.
258		 */
259		found |= (res & in_range) * 0xff;
260	}
261	if (found == 0) {
262		wpa_printf(MSG_INFO,
263			   "EAP-pwd: unable to find random point on curve for group %d, something's fishy",
264			   num);
265		goto fail;
266	}
267
268	/*
269	 * We know x_candidate is a quadratic residue so set it here.
270	 */
271	crypto_bignum_deinit(x_candidate, 1);
272	x_candidate = crypto_bignum_init_set(x_bin, primebytelen);
273	if (!x_candidate ||
274	    crypto_ec_point_solve_y_coord(grp->group, grp->pwe, x_candidate,
275					  is_odd) != 0) {
276		wpa_printf(MSG_INFO, "EAP-pwd: Could not solve for y");
277		goto fail;
278	}
279
280	/*
281	 * If there's a solution to the equation then the point must be on the
282	 * curve so why check again explicitly? OpenSSL code says this is
283	 * required by X9.62. We're not X9.62 but it can't hurt just to be sure.
284	 */
285	if (!crypto_ec_point_is_on_curve(grp->group, grp->pwe)) {
286		wpa_printf(MSG_INFO, "EAP-pwd: point is not on curve");
287		goto fail;
288	}
289
290	wpa_printf(MSG_DEBUG, "EAP-pwd: found a PWE in %02d tries", found_ctr);
291
292	if (0) {
293 fail:
294		crypto_ec_point_deinit(grp->pwe, 1);
295		grp->pwe = NULL;
296		ret = 1;
297	}
298	/* cleanliness and order.... */
299	crypto_bignum_deinit(x_candidate, 1);
300	crypto_bignum_deinit(tmp2, 1);
301	crypto_bignum_deinit(qr, 1);
302	crypto_bignum_deinit(qnr, 1);
303	bin_clear_free(prfbuf, primebytelen);
304	os_memset(qr_bin, 0, sizeof(qr_bin));
305	os_memset(qnr_bin, 0, sizeof(qnr_bin));
306	os_memset(qr_or_qnr_bin, 0, sizeof(qr_or_qnr_bin));
307	os_memset(pwe_digest, 0, sizeof(pwe_digest));
308
309	return ret;
310}
311
312
313int compute_keys(EAP_PWD_group *grp, const struct crypto_bignum *k,
314		 const struct crypto_bignum *peer_scalar,
315		 const struct crypto_bignum *server_scalar,
316		 const u8 *confirm_peer, const u8 *confirm_server,
317		 const u32 *ciphersuite, u8 *msk, u8 *emsk, u8 *session_id)
318{
319	struct crypto_hash *hash;
320	u8 mk[SHA256_MAC_LEN], *cruft;
321	u8 msk_emsk[EAP_MSK_LEN + EAP_EMSK_LEN];
322	size_t prime_len, order_len;
323
324	prime_len = crypto_ec_prime_len(grp->group);
325	order_len = crypto_ec_order_len(grp->group);
326
327	cruft = os_malloc(prime_len);
328	if (!cruft)
329		return -1;
330
331	/*
332	 * first compute the session-id = TypeCode | H(ciphersuite | scal_p |
333	 *	scal_s)
334	 */
335	session_id[0] = EAP_TYPE_PWD;
336	hash = eap_pwd_h_init();
337	if (hash == NULL) {
338		os_free(cruft);
339		return -1;
340	}
341	eap_pwd_h_update(hash, (const u8 *) ciphersuite, sizeof(u32));
342	crypto_bignum_to_bin(peer_scalar, cruft, order_len, order_len);
343	eap_pwd_h_update(hash, cruft, order_len);
344	crypto_bignum_to_bin(server_scalar, cruft, order_len, order_len);
345	eap_pwd_h_update(hash, cruft, order_len);
346	eap_pwd_h_final(hash, &session_id[1]);
347
348	/* then compute MK = H(k | confirm-peer | confirm-server) */
349	hash = eap_pwd_h_init();
350	if (hash == NULL) {
351		os_free(cruft);
352		return -1;
353	}
354	crypto_bignum_to_bin(k, cruft, prime_len, prime_len);
355	eap_pwd_h_update(hash, cruft, prime_len);
356	os_free(cruft);
357	eap_pwd_h_update(hash, confirm_peer, SHA256_MAC_LEN);
358	eap_pwd_h_update(hash, confirm_server, SHA256_MAC_LEN);
359	eap_pwd_h_final(hash, mk);
360
361	/* stretch the mk with the session-id to get MSK | EMSK */
362	if (eap_pwd_kdf(mk, SHA256_MAC_LEN,
363			session_id, SHA256_MAC_LEN + 1,
364			msk_emsk, (EAP_MSK_LEN + EAP_EMSK_LEN) * 8) < 0) {
365		return -1;
366	}
367
368	os_memcpy(msk, msk_emsk, EAP_MSK_LEN);
369	os_memcpy(emsk, msk_emsk + EAP_MSK_LEN, EAP_EMSK_LEN);
370
371	return 1;
372}
373
374
375static int eap_pwd_element_coord_ok(const struct crypto_bignum *prime,
376				    const u8 *buf, size_t len)
377{
378	struct crypto_bignum *val;
379	int ok = 1;
380
381	val = crypto_bignum_init_set(buf, len);
382	if (!val || crypto_bignum_is_zero(val) ||
383	    crypto_bignum_cmp(val, prime) >= 0)
384		ok = 0;
385	crypto_bignum_deinit(val, 0);
386	return ok;
387}
388
389
390struct crypto_ec_point * eap_pwd_get_element(EAP_PWD_group *group,
391					     const u8 *buf)
392{
393	struct crypto_ec_point *element;
394	const struct crypto_bignum *prime;
395	size_t prime_len;
396
397	prime = crypto_ec_get_prime(group->group);
398	prime_len = crypto_ec_prime_len(group->group);
399
400	/* RFC 5931, 2.8.5.2.2: 0 < x,y < p */
401	if (!eap_pwd_element_coord_ok(prime, buf, prime_len) ||
402	    !eap_pwd_element_coord_ok(prime, buf + prime_len, prime_len)) {
403		wpa_printf(MSG_INFO, "EAP-pwd: Invalid coordinate in element");
404		return NULL;
405	}
406
407	element = crypto_ec_point_from_bin(group->group, buf);
408	if (!element) {
409		wpa_printf(MSG_INFO, "EAP-pwd: EC point from element failed");
410		return NULL;
411	}
412
413	/* RFC 5931, 2.8.5.2.2: on curve and not the point at infinity */
414	if (!crypto_ec_point_is_on_curve(group->group, element) ||
415	    crypto_ec_point_is_at_infinity(group->group, element)) {
416		wpa_printf(MSG_INFO, "EAP-pwd: Invalid element");
417		goto fail;
418	}
419
420out:
421	return element;
422fail:
423	crypto_ec_point_deinit(element, 0);
424	element = NULL;
425	goto out;
426}
427
428
429struct crypto_bignum * eap_pwd_get_scalar(EAP_PWD_group *group, const u8 *buf)
430{
431	struct crypto_bignum *scalar;
432	const struct crypto_bignum *order;
433	size_t order_len;
434
435	order = crypto_ec_get_order(group->group);
436	order_len = crypto_ec_order_len(group->group);
437
438	/* RFC 5931, 2.8.5.2: 1 < scalar < r */
439	scalar = crypto_bignum_init_set(buf, order_len);
440	if (!scalar || crypto_bignum_is_zero(scalar) ||
441	    crypto_bignum_is_one(scalar) ||
442	    crypto_bignum_cmp(scalar, order) >= 0) {
443		wpa_printf(MSG_INFO, "EAP-pwd: received scalar is invalid");
444		crypto_bignum_deinit(scalar, 0);
445		scalar = NULL;
446	}
447
448	return scalar;
449}
450
451
452int eap_pwd_get_rand_mask(EAP_PWD_group *group, struct crypto_bignum *_rand,
453			  struct crypto_bignum *_mask,
454			  struct crypto_bignum *scalar)
455{
456	return dragonfly_generate_scalar(crypto_ec_get_order(group->group),
457					 _rand, _mask, scalar);
458}
459