xref: /dflybsd-src/contrib/wpa_supplicant/src/common/sae.c (revision 3a84a4273475ed07d0ab1c2dfeffdfedef35d9cd)
13ff40c12SJohn Marino /*
23ff40c12SJohn Marino  * Simultaneous authentication of equals
3*a1157835SDaniel Fojt  * Copyright (c) 2012-2016, Jouni Malinen <j@w1.fi>
43ff40c12SJohn Marino  *
53ff40c12SJohn Marino  * This software may be distributed under the terms of the BSD license.
63ff40c12SJohn Marino  * See README for more details.
73ff40c12SJohn Marino  */
83ff40c12SJohn Marino 
93ff40c12SJohn Marino #include "includes.h"
103ff40c12SJohn Marino 
113ff40c12SJohn Marino #include "common.h"
12*a1157835SDaniel Fojt #include "utils/const_time.h"
133ff40c12SJohn Marino #include "crypto/crypto.h"
143ff40c12SJohn Marino #include "crypto/sha256.h"
153ff40c12SJohn Marino #include "crypto/random.h"
163ff40c12SJohn Marino #include "crypto/dh_groups.h"
173ff40c12SJohn Marino #include "ieee802_11_defs.h"
18*a1157835SDaniel Fojt #include "dragonfly.h"
193ff40c12SJohn Marino #include "sae.h"
203ff40c12SJohn Marino 
213ff40c12SJohn Marino 
sae_set_group(struct sae_data * sae,int group)223ff40c12SJohn Marino int sae_set_group(struct sae_data *sae, int group)
233ff40c12SJohn Marino {
243ff40c12SJohn Marino 	struct sae_temporary_data *tmp;
253ff40c12SJohn Marino 
26*a1157835SDaniel Fojt #ifdef CONFIG_TESTING_OPTIONS
27*a1157835SDaniel Fojt 	/* Allow all groups for testing purposes in non-production builds. */
28*a1157835SDaniel Fojt #else /* CONFIG_TESTING_OPTIONS */
29*a1157835SDaniel Fojt 	if (!dragonfly_suitable_group(group, 0)) {
30*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: Reject unsuitable group %d", group);
31*a1157835SDaniel Fojt 		return -1;
32*a1157835SDaniel Fojt 	}
33*a1157835SDaniel Fojt #endif /* CONFIG_TESTING_OPTIONS */
34*a1157835SDaniel Fojt 
353ff40c12SJohn Marino 	sae_clear_data(sae);
363ff40c12SJohn Marino 	tmp = sae->tmp = os_zalloc(sizeof(*tmp));
373ff40c12SJohn Marino 	if (tmp == NULL)
383ff40c12SJohn Marino 		return -1;
393ff40c12SJohn Marino 
403ff40c12SJohn Marino 	/* First, check if this is an ECC group */
413ff40c12SJohn Marino 	tmp->ec = crypto_ec_init(group);
423ff40c12SJohn Marino 	if (tmp->ec) {
43*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: Selecting supported ECC group %d",
44*a1157835SDaniel Fojt 			   group);
453ff40c12SJohn Marino 		sae->group = group;
463ff40c12SJohn Marino 		tmp->prime_len = crypto_ec_prime_len(tmp->ec);
473ff40c12SJohn Marino 		tmp->prime = crypto_ec_get_prime(tmp->ec);
48*a1157835SDaniel Fojt 		tmp->order_len = crypto_ec_order_len(tmp->ec);
493ff40c12SJohn Marino 		tmp->order = crypto_ec_get_order(tmp->ec);
503ff40c12SJohn Marino 		return 0;
513ff40c12SJohn Marino 	}
523ff40c12SJohn Marino 
533ff40c12SJohn Marino 	/* Not an ECC group, check FFC */
543ff40c12SJohn Marino 	tmp->dh = dh_groups_get(group);
553ff40c12SJohn Marino 	if (tmp->dh) {
56*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: Selecting supported FFC group %d",
57*a1157835SDaniel Fojt 			   group);
583ff40c12SJohn Marino 		sae->group = group;
593ff40c12SJohn Marino 		tmp->prime_len = tmp->dh->prime_len;
603ff40c12SJohn Marino 		if (tmp->prime_len > SAE_MAX_PRIME_LEN) {
613ff40c12SJohn Marino 			sae_clear_data(sae);
623ff40c12SJohn Marino 			return -1;
633ff40c12SJohn Marino 		}
643ff40c12SJohn Marino 
653ff40c12SJohn Marino 		tmp->prime_buf = crypto_bignum_init_set(tmp->dh->prime,
663ff40c12SJohn Marino 							tmp->prime_len);
673ff40c12SJohn Marino 		if (tmp->prime_buf == NULL) {
683ff40c12SJohn Marino 			sae_clear_data(sae);
693ff40c12SJohn Marino 			return -1;
703ff40c12SJohn Marino 		}
713ff40c12SJohn Marino 		tmp->prime = tmp->prime_buf;
723ff40c12SJohn Marino 
73*a1157835SDaniel Fojt 		tmp->order_len = tmp->dh->order_len;
743ff40c12SJohn Marino 		tmp->order_buf = crypto_bignum_init_set(tmp->dh->order,
753ff40c12SJohn Marino 							tmp->dh->order_len);
763ff40c12SJohn Marino 		if (tmp->order_buf == NULL) {
773ff40c12SJohn Marino 			sae_clear_data(sae);
783ff40c12SJohn Marino 			return -1;
793ff40c12SJohn Marino 		}
803ff40c12SJohn Marino 		tmp->order = tmp->order_buf;
813ff40c12SJohn Marino 
823ff40c12SJohn Marino 		return 0;
833ff40c12SJohn Marino 	}
843ff40c12SJohn Marino 
853ff40c12SJohn Marino 	/* Unsupported group */
86*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
87*a1157835SDaniel Fojt 		   "SAE: Group %d not supported by the crypto library", group);
883ff40c12SJohn Marino 	return -1;
893ff40c12SJohn Marino }
903ff40c12SJohn Marino 
913ff40c12SJohn Marino 
sae_clear_temp_data(struct sae_data * sae)923ff40c12SJohn Marino void sae_clear_temp_data(struct sae_data *sae)
933ff40c12SJohn Marino {
943ff40c12SJohn Marino 	struct sae_temporary_data *tmp;
953ff40c12SJohn Marino 	if (sae == NULL || sae->tmp == NULL)
963ff40c12SJohn Marino 		return;
973ff40c12SJohn Marino 	tmp = sae->tmp;
983ff40c12SJohn Marino 	crypto_ec_deinit(tmp->ec);
993ff40c12SJohn Marino 	crypto_bignum_deinit(tmp->prime_buf, 0);
1003ff40c12SJohn Marino 	crypto_bignum_deinit(tmp->order_buf, 0);
1013ff40c12SJohn Marino 	crypto_bignum_deinit(tmp->sae_rand, 1);
1023ff40c12SJohn Marino 	crypto_bignum_deinit(tmp->pwe_ffc, 1);
1033ff40c12SJohn Marino 	crypto_bignum_deinit(tmp->own_commit_scalar, 0);
1043ff40c12SJohn Marino 	crypto_bignum_deinit(tmp->own_commit_element_ffc, 0);
1053ff40c12SJohn Marino 	crypto_bignum_deinit(tmp->peer_commit_element_ffc, 0);
1063ff40c12SJohn Marino 	crypto_ec_point_deinit(tmp->pwe_ecc, 1);
1073ff40c12SJohn Marino 	crypto_ec_point_deinit(tmp->own_commit_element_ecc, 0);
1083ff40c12SJohn Marino 	crypto_ec_point_deinit(tmp->peer_commit_element_ecc, 0);
109*a1157835SDaniel Fojt 	wpabuf_free(tmp->anti_clogging_token);
110*a1157835SDaniel Fojt 	os_free(tmp->pw_id);
111*a1157835SDaniel Fojt 	bin_clear_free(tmp, sizeof(*tmp));
1123ff40c12SJohn Marino 	sae->tmp = NULL;
1133ff40c12SJohn Marino }
1143ff40c12SJohn Marino 
1153ff40c12SJohn Marino 
sae_clear_data(struct sae_data * sae)1163ff40c12SJohn Marino void sae_clear_data(struct sae_data *sae)
1173ff40c12SJohn Marino {
1183ff40c12SJohn Marino 	if (sae == NULL)
1193ff40c12SJohn Marino 		return;
1203ff40c12SJohn Marino 	sae_clear_temp_data(sae);
1213ff40c12SJohn Marino 	crypto_bignum_deinit(sae->peer_commit_scalar, 0);
1223ff40c12SJohn Marino 	os_memset(sae, 0, sizeof(*sae));
1233ff40c12SJohn Marino }
1243ff40c12SJohn Marino 
1253ff40c12SJohn Marino 
sae_pwd_seed_key(const u8 * addr1,const u8 * addr2,u8 * key)1263ff40c12SJohn Marino static void sae_pwd_seed_key(const u8 *addr1, const u8 *addr2, u8 *key)
1273ff40c12SJohn Marino {
1283ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "SAE: PWE derivation - addr1=" MACSTR
1293ff40c12SJohn Marino 		   " addr2=" MACSTR, MAC2STR(addr1), MAC2STR(addr2));
1303ff40c12SJohn Marino 	if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) {
1313ff40c12SJohn Marino 		os_memcpy(key, addr1, ETH_ALEN);
1323ff40c12SJohn Marino 		os_memcpy(key + ETH_ALEN, addr2, ETH_ALEN);
1333ff40c12SJohn Marino 	} else {
1343ff40c12SJohn Marino 		os_memcpy(key, addr2, ETH_ALEN);
1353ff40c12SJohn Marino 		os_memcpy(key + ETH_ALEN, addr1, ETH_ALEN);
1363ff40c12SJohn Marino 	}
1373ff40c12SJohn Marino }
1383ff40c12SJohn Marino 
1393ff40c12SJohn Marino 
sae_test_pwd_seed_ecc(struct sae_data * sae,const u8 * pwd_seed,const u8 * prime,const u8 * qr,const u8 * qnr,u8 * pwd_value)1403ff40c12SJohn Marino static int sae_test_pwd_seed_ecc(struct sae_data *sae, const u8 *pwd_seed,
141*a1157835SDaniel Fojt 				 const u8 *prime, const u8 *qr, const u8 *qnr,
142*a1157835SDaniel Fojt 				 u8 *pwd_value)
1433ff40c12SJohn Marino {
144*a1157835SDaniel Fojt 	struct crypto_bignum *y_sqr, *x_cand;
145*a1157835SDaniel Fojt 	int res;
1463ff40c12SJohn Marino 	size_t bits;
147*a1157835SDaniel Fojt 	int cmp_prime;
148*a1157835SDaniel Fojt 	unsigned int in_range;
1493ff40c12SJohn Marino 
1503ff40c12SJohn Marino 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
1513ff40c12SJohn Marino 
1523ff40c12SJohn Marino 	/* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
1533ff40c12SJohn Marino 	bits = crypto_ec_prime_len_bits(sae->tmp->ec);
154*a1157835SDaniel Fojt 	if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking",
155*a1157835SDaniel Fojt 			    prime, sae->tmp->prime_len, pwd_value, bits) < 0)
156*a1157835SDaniel Fojt 		return -1;
1573ff40c12SJohn Marino 	if (bits % 8)
158*a1157835SDaniel Fojt 		buf_shift_right(pwd_value, sae->tmp->prime_len, 8 - bits % 8);
1593ff40c12SJohn Marino 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value",
1603ff40c12SJohn Marino 			pwd_value, sae->tmp->prime_len);
1613ff40c12SJohn Marino 
162*a1157835SDaniel Fojt 	cmp_prime = const_time_memcmp(pwd_value, prime, sae->tmp->prime_len);
163*a1157835SDaniel Fojt 	/* Create a const_time mask for selection based on prf result
164*a1157835SDaniel Fojt 	 * being smaller than prime. */
165*a1157835SDaniel Fojt 	in_range = const_time_fill_msb((unsigned int) cmp_prime);
166*a1157835SDaniel Fojt 	/* The algorithm description would skip the next steps if
167*a1157835SDaniel Fojt 	 * cmp_prime >= 0 (reutnr 0 here), but go through them regardless to
168*a1157835SDaniel Fojt 	 * minimize externally observable differences in behavior. */
1693ff40c12SJohn Marino 
170*a1157835SDaniel Fojt 	x_cand = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
171*a1157835SDaniel Fojt 	if (!x_cand)
1723ff40c12SJohn Marino 		return -1;
173*a1157835SDaniel Fojt 	y_sqr = crypto_ec_point_compute_y_sqr(sae->tmp->ec, x_cand);
174*a1157835SDaniel Fojt 	crypto_bignum_deinit(x_cand, 1);
175*a1157835SDaniel Fojt 	if (!y_sqr)
176*a1157835SDaniel Fojt 		return -1;
1773ff40c12SJohn Marino 
178*a1157835SDaniel Fojt 	res = dragonfly_is_quadratic_residue_blind(sae->tmp->ec, qr, qnr,
179*a1157835SDaniel Fojt 						   y_sqr);
180*a1157835SDaniel Fojt 	crypto_bignum_deinit(y_sqr, 1);
181*a1157835SDaniel Fojt 	if (res < 0)
182*a1157835SDaniel Fojt 		return res;
183*a1157835SDaniel Fojt 	return const_time_select_int(in_range, res, 0);
1843ff40c12SJohn Marino }
1853ff40c12SJohn Marino 
1863ff40c12SJohn Marino 
187*a1157835SDaniel Fojt /* Returns -1 on fatal failure, 0 if PWE cannot be derived from the provided
188*a1157835SDaniel Fojt  * pwd-seed, or 1 if a valid PWE was derived from pwd-seed. */
sae_test_pwd_seed_ffc(struct sae_data * sae,const u8 * pwd_seed,struct crypto_bignum * pwe)1893ff40c12SJohn Marino static int sae_test_pwd_seed_ffc(struct sae_data *sae, const u8 *pwd_seed,
1903ff40c12SJohn Marino 				 struct crypto_bignum *pwe)
1913ff40c12SJohn Marino {
1923ff40c12SJohn Marino 	u8 pwd_value[SAE_MAX_PRIME_LEN];
1933ff40c12SJohn Marino 	size_t bits = sae->tmp->prime_len * 8;
1943ff40c12SJohn Marino 	u8 exp[1];
195*a1157835SDaniel Fojt 	struct crypto_bignum *a, *b = NULL;
196*a1157835SDaniel Fojt 	int res, is_val;
197*a1157835SDaniel Fojt 	u8 pwd_value_valid;
1983ff40c12SJohn Marino 
1993ff40c12SJohn Marino 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
2003ff40c12SJohn Marino 
2013ff40c12SJohn Marino 	/* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
202*a1157835SDaniel Fojt 	if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking",
2033ff40c12SJohn Marino 			    sae->tmp->dh->prime, sae->tmp->prime_len, pwd_value,
204*a1157835SDaniel Fojt 			    bits) < 0)
205*a1157835SDaniel Fojt 		return -1;
2063ff40c12SJohn Marino 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value", pwd_value,
2073ff40c12SJohn Marino 			sae->tmp->prime_len);
2083ff40c12SJohn Marino 
209*a1157835SDaniel Fojt 	/* Check whether pwd-value < p */
210*a1157835SDaniel Fojt 	res = const_time_memcmp(pwd_value, sae->tmp->dh->prime,
211*a1157835SDaniel Fojt 				sae->tmp->prime_len);
212*a1157835SDaniel Fojt 	/* pwd-value >= p is invalid, so res is < 0 for the valid cases and
213*a1157835SDaniel Fojt 	 * the negative sign can be used to fill the mask for constant time
214*a1157835SDaniel Fojt 	 * selection */
215*a1157835SDaniel Fojt 	pwd_value_valid = const_time_fill_msb(res);
216*a1157835SDaniel Fojt 
217*a1157835SDaniel Fojt 	/* If pwd-value >= p, force pwd-value to be < p and perform the
218*a1157835SDaniel Fojt 	 * calculations anyway to hide timing difference. The derived PWE will
219*a1157835SDaniel Fojt 	 * be ignored in that case. */
220*a1157835SDaniel Fojt 	pwd_value[0] = const_time_select_u8(pwd_value_valid, pwd_value[0], 0);
2213ff40c12SJohn Marino 
2223ff40c12SJohn Marino 	/* PWE = pwd-value^((p-1)/r) modulo p */
2233ff40c12SJohn Marino 
224*a1157835SDaniel Fojt 	res = -1;
2253ff40c12SJohn Marino 	a = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
226*a1157835SDaniel Fojt 	if (!a)
227*a1157835SDaniel Fojt 		goto fail;
2283ff40c12SJohn Marino 
229*a1157835SDaniel Fojt 	/* This is an optimization based on the used group that does not depend
230*a1157835SDaniel Fojt 	 * on the password in any way, so it is fine to use separate branches
231*a1157835SDaniel Fojt 	 * for this step without constant time operations. */
2323ff40c12SJohn Marino 	if (sae->tmp->dh->safe_prime) {
2333ff40c12SJohn Marino 		/*
2343ff40c12SJohn Marino 		 * r = (p-1)/2 for the group used here, so this becomes:
2353ff40c12SJohn Marino 		 * PWE = pwd-value^2 modulo p
2363ff40c12SJohn Marino 		 */
2373ff40c12SJohn Marino 		exp[0] = 2;
2383ff40c12SJohn Marino 		b = crypto_bignum_init_set(exp, sizeof(exp));
2393ff40c12SJohn Marino 	} else {
2403ff40c12SJohn Marino 		/* Calculate exponent: (p-1)/r */
2413ff40c12SJohn Marino 		exp[0] = 1;
2423ff40c12SJohn Marino 		b = crypto_bignum_init_set(exp, sizeof(exp));
2433ff40c12SJohn Marino 		if (b == NULL ||
2443ff40c12SJohn Marino 		    crypto_bignum_sub(sae->tmp->prime, b, b) < 0 ||
245*a1157835SDaniel Fojt 		    crypto_bignum_div(b, sae->tmp->order, b) < 0)
246*a1157835SDaniel Fojt 			goto fail;
2473ff40c12SJohn Marino 	}
2483ff40c12SJohn Marino 
249*a1157835SDaniel Fojt 	if (!b)
250*a1157835SDaniel Fojt 		goto fail;
251*a1157835SDaniel Fojt 
2523ff40c12SJohn Marino 	res = crypto_bignum_exptmod(a, b, sae->tmp->prime, pwe);
253*a1157835SDaniel Fojt 	if (res < 0)
254*a1157835SDaniel Fojt 		goto fail;
2553ff40c12SJohn Marino 
256*a1157835SDaniel Fojt 	/* There were no fatal errors in calculations, so determine the return
257*a1157835SDaniel Fojt 	 * value using constant time operations. We get here for number of
258*a1157835SDaniel Fojt 	 * invalid cases which are cleared here after having performed all the
259*a1157835SDaniel Fojt 	 * computation. PWE is valid if pwd-value was less than prime and
260*a1157835SDaniel Fojt 	 * PWE > 1. Start with pwd-value check first and then use constant time
261*a1157835SDaniel Fojt 	 * operations to clear res to 0 if PWE is 0 or 1.
262*a1157835SDaniel Fojt 	 */
263*a1157835SDaniel Fojt 	res = const_time_select_u8(pwd_value_valid, 1, 0);
264*a1157835SDaniel Fojt 	is_val = crypto_bignum_is_zero(pwe);
265*a1157835SDaniel Fojt 	res = const_time_select_u8(const_time_is_zero(is_val), res, 0);
266*a1157835SDaniel Fojt 	is_val = crypto_bignum_is_one(pwe);
267*a1157835SDaniel Fojt 	res = const_time_select_u8(const_time_is_zero(is_val), res, 0);
2683ff40c12SJohn Marino 
269*a1157835SDaniel Fojt fail:
270*a1157835SDaniel Fojt 	crypto_bignum_deinit(a, 1);
271*a1157835SDaniel Fojt 	crypto_bignum_deinit(b, 1);
272*a1157835SDaniel Fojt 	return res;
2733ff40c12SJohn Marino }
2743ff40c12SJohn Marino 
2753ff40c12SJohn Marino 
sae_derive_pwe_ecc(struct sae_data * sae,const u8 * addr1,const u8 * addr2,const u8 * password,size_t password_len,const char * identifier)2763ff40c12SJohn Marino static int sae_derive_pwe_ecc(struct sae_data *sae, const u8 *addr1,
2773ff40c12SJohn Marino 			      const u8 *addr2, const u8 *password,
278*a1157835SDaniel Fojt 			      size_t password_len, const char *identifier)
2793ff40c12SJohn Marino {
280*a1157835SDaniel Fojt 	u8 counter, k;
2813ff40c12SJohn Marino 	u8 addrs[2 * ETH_ALEN];
282*a1157835SDaniel Fojt 	const u8 *addr[3];
283*a1157835SDaniel Fojt 	size_t len[3];
284*a1157835SDaniel Fojt 	size_t num_elem;
285*a1157835SDaniel Fojt 	u8 *dummy_password, *tmp_password;
286*a1157835SDaniel Fojt 	int pwd_seed_odd = 0;
287*a1157835SDaniel Fojt 	u8 prime[SAE_MAX_ECC_PRIME_LEN];
288*a1157835SDaniel Fojt 	size_t prime_len;
289*a1157835SDaniel Fojt 	struct crypto_bignum *x = NULL, *qr = NULL, *qnr = NULL;
290*a1157835SDaniel Fojt 	u8 x_bin[SAE_MAX_ECC_PRIME_LEN];
291*a1157835SDaniel Fojt 	u8 x_cand_bin[SAE_MAX_ECC_PRIME_LEN];
292*a1157835SDaniel Fojt 	u8 qr_bin[SAE_MAX_ECC_PRIME_LEN];
293*a1157835SDaniel Fojt 	u8 qnr_bin[SAE_MAX_ECC_PRIME_LEN];
294*a1157835SDaniel Fojt 	int res = -1;
295*a1157835SDaniel Fojt 	u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
296*a1157835SDaniel Fojt 		       * mask */
2973ff40c12SJohn Marino 
298*a1157835SDaniel Fojt 	os_memset(x_bin, 0, sizeof(x_bin));
299*a1157835SDaniel Fojt 
300*a1157835SDaniel Fojt 	dummy_password = os_malloc(password_len);
301*a1157835SDaniel Fojt 	tmp_password = os_malloc(password_len);
302*a1157835SDaniel Fojt 	if (!dummy_password || !tmp_password ||
303*a1157835SDaniel Fojt 	    random_get_bytes(dummy_password, password_len) < 0)
304*a1157835SDaniel Fojt 		goto fail;
305*a1157835SDaniel Fojt 
306*a1157835SDaniel Fojt 	prime_len = sae->tmp->prime_len;
307*a1157835SDaniel Fojt 	if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
308*a1157835SDaniel Fojt 				 prime_len) < 0)
309*a1157835SDaniel Fojt 		goto fail;
310*a1157835SDaniel Fojt 
311*a1157835SDaniel Fojt 	/*
312*a1157835SDaniel Fojt 	 * Create a random quadratic residue (qr) and quadratic non-residue
313*a1157835SDaniel Fojt 	 * (qnr) modulo p for blinding purposes during the loop.
314*a1157835SDaniel Fojt 	 */
315*a1157835SDaniel Fojt 	if (dragonfly_get_random_qr_qnr(sae->tmp->prime, &qr, &qnr) < 0 ||
316*a1157835SDaniel Fojt 	    crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin), prime_len) < 0 ||
317*a1157835SDaniel Fojt 	    crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin), prime_len) < 0)
318*a1157835SDaniel Fojt 		goto fail;
3193ff40c12SJohn Marino 
3203ff40c12SJohn Marino 	wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
3213ff40c12SJohn Marino 			      password, password_len);
322*a1157835SDaniel Fojt 	if (identifier)
323*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: password identifier: %s",
324*a1157835SDaniel Fojt 			   identifier);
3253ff40c12SJohn Marino 
3263ff40c12SJohn Marino 	/*
3273ff40c12SJohn Marino 	 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
328*a1157835SDaniel Fojt 	 * base = password [|| identifier]
3293ff40c12SJohn Marino 	 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
330*a1157835SDaniel Fojt 	 *              base || counter)
3313ff40c12SJohn Marino 	 */
3323ff40c12SJohn Marino 	sae_pwd_seed_key(addr1, addr2, addrs);
3333ff40c12SJohn Marino 
334*a1157835SDaniel Fojt 	addr[0] = tmp_password;
3353ff40c12SJohn Marino 	len[0] = password_len;
336*a1157835SDaniel Fojt 	num_elem = 1;
337*a1157835SDaniel Fojt 	if (identifier) {
338*a1157835SDaniel Fojt 		addr[num_elem] = (const u8 *) identifier;
339*a1157835SDaniel Fojt 		len[num_elem] = os_strlen(identifier);
340*a1157835SDaniel Fojt 		num_elem++;
341*a1157835SDaniel Fojt 	}
342*a1157835SDaniel Fojt 	addr[num_elem] = &counter;
343*a1157835SDaniel Fojt 	len[num_elem] = sizeof(counter);
344*a1157835SDaniel Fojt 	num_elem++;
3453ff40c12SJohn Marino 
3463ff40c12SJohn Marino 	/*
3473ff40c12SJohn Marino 	 * Continue for at least k iterations to protect against side-channel
3483ff40c12SJohn Marino 	 * attacks that attempt to determine the number of iterations required
3493ff40c12SJohn Marino 	 * in the loop.
3503ff40c12SJohn Marino 	 */
351*a1157835SDaniel Fojt 	k = dragonfly_min_pwe_loop_iter(sae->group);
352*a1157835SDaniel Fojt 
353*a1157835SDaniel Fojt 	for (counter = 1; counter <= k || !found; counter++) {
3543ff40c12SJohn Marino 		u8 pwd_seed[SHA256_MAC_LEN];
3553ff40c12SJohn Marino 
3563ff40c12SJohn Marino 		if (counter > 200) {
3573ff40c12SJohn Marino 			/* This should not happen in practice */
3583ff40c12SJohn Marino 			wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
3593ff40c12SJohn Marino 			break;
3603ff40c12SJohn Marino 		}
3613ff40c12SJohn Marino 
362*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: counter = %03u", counter);
363*a1157835SDaniel Fojt 		const_time_select_bin(found, dummy_password, password,
364*a1157835SDaniel Fojt 				      password_len, tmp_password);
365*a1157835SDaniel Fojt 		if (hmac_sha256_vector(addrs, sizeof(addrs), num_elem,
366*a1157835SDaniel Fojt 				       addr, len, pwd_seed) < 0)
3673ff40c12SJohn Marino 			break;
368*a1157835SDaniel Fojt 
3693ff40c12SJohn Marino 		res = sae_test_pwd_seed_ecc(sae, pwd_seed,
370*a1157835SDaniel Fojt 					    prime, qr_bin, qnr_bin, x_cand_bin);
371*a1157835SDaniel Fojt 		const_time_select_bin(found, x_bin, x_cand_bin, prime_len,
372*a1157835SDaniel Fojt 				      x_bin);
373*a1157835SDaniel Fojt 		pwd_seed_odd = const_time_select_u8(
374*a1157835SDaniel Fojt 			found, pwd_seed_odd,
375*a1157835SDaniel Fojt 			pwd_seed[SHA256_MAC_LEN - 1] & 0x01);
376*a1157835SDaniel Fojt 		os_memset(pwd_seed, 0, sizeof(pwd_seed));
3773ff40c12SJohn Marino 		if (res < 0)
378*a1157835SDaniel Fojt 			goto fail;
379*a1157835SDaniel Fojt 		/* Need to minimize differences in handling res == 0 and 1 here
380*a1157835SDaniel Fojt 		 * to avoid differences in timing and instruction cache access,
381*a1157835SDaniel Fojt 		 * so use const_time_select_*() to make local copies of the
382*a1157835SDaniel Fojt 		 * values based on whether this loop iteration was the one that
383*a1157835SDaniel Fojt 		 * found the pwd-seed/x. */
384*a1157835SDaniel Fojt 
385*a1157835SDaniel Fojt 		/* found is 0 or 0xff here and res is 0 or 1. Bitwise OR of them
386*a1157835SDaniel Fojt 		 * (with res converted to 0/0xff) handles this in constant time.
387*a1157835SDaniel Fojt 		 */
388*a1157835SDaniel Fojt 		found |= res * 0xff;
389*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: pwd-seed result %d found=0x%02x",
390*a1157835SDaniel Fojt 			   res, found);
3913ff40c12SJohn Marino 	}
3923ff40c12SJohn Marino 
393*a1157835SDaniel Fojt 	if (!found) {
394*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: Could not generate PWE");
395*a1157835SDaniel Fojt 		res = -1;
396*a1157835SDaniel Fojt 		goto fail;
397*a1157835SDaniel Fojt 	}
3983ff40c12SJohn Marino 
399*a1157835SDaniel Fojt 	x = crypto_bignum_init_set(x_bin, prime_len);
400*a1157835SDaniel Fojt 	if (!x) {
401*a1157835SDaniel Fojt 		res = -1;
402*a1157835SDaniel Fojt 		goto fail;
403*a1157835SDaniel Fojt 	}
404*a1157835SDaniel Fojt 
405*a1157835SDaniel Fojt 	if (!sae->tmp->pwe_ecc)
406*a1157835SDaniel Fojt 		sae->tmp->pwe_ecc = crypto_ec_point_init(sae->tmp->ec);
407*a1157835SDaniel Fojt 	if (!sae->tmp->pwe_ecc)
408*a1157835SDaniel Fojt 		res = -1;
409*a1157835SDaniel Fojt 	else
410*a1157835SDaniel Fojt 		res = crypto_ec_point_solve_y_coord(sae->tmp->ec,
411*a1157835SDaniel Fojt 						    sae->tmp->pwe_ecc, x,
412*a1157835SDaniel Fojt 						    pwd_seed_odd);
413*a1157835SDaniel Fojt 	if (res < 0) {
414*a1157835SDaniel Fojt 		/*
415*a1157835SDaniel Fojt 		 * This should not happen since we already checked that there
416*a1157835SDaniel Fojt 		 * is a result.
417*a1157835SDaniel Fojt 		 */
418*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: Could not solve y");
419*a1157835SDaniel Fojt 	}
420*a1157835SDaniel Fojt 
421*a1157835SDaniel Fojt fail:
422*a1157835SDaniel Fojt 	crypto_bignum_deinit(qr, 0);
423*a1157835SDaniel Fojt 	crypto_bignum_deinit(qnr, 0);
424*a1157835SDaniel Fojt 	os_free(dummy_password);
425*a1157835SDaniel Fojt 	bin_clear_free(tmp_password, password_len);
426*a1157835SDaniel Fojt 	crypto_bignum_deinit(x, 1);
427*a1157835SDaniel Fojt 	os_memset(x_bin, 0, sizeof(x_bin));
428*a1157835SDaniel Fojt 	os_memset(x_cand_bin, 0, sizeof(x_cand_bin));
429*a1157835SDaniel Fojt 
430*a1157835SDaniel Fojt 	return res;
4313ff40c12SJohn Marino }
4323ff40c12SJohn Marino 
4333ff40c12SJohn Marino 
sae_derive_pwe_ffc(struct sae_data * sae,const u8 * addr1,const u8 * addr2,const u8 * password,size_t password_len,const char * identifier)4343ff40c12SJohn Marino static int sae_derive_pwe_ffc(struct sae_data *sae, const u8 *addr1,
4353ff40c12SJohn Marino 			      const u8 *addr2, const u8 *password,
436*a1157835SDaniel Fojt 			      size_t password_len, const char *identifier)
4373ff40c12SJohn Marino {
438*a1157835SDaniel Fojt 	u8 counter, k, sel_counter = 0;
4393ff40c12SJohn Marino 	u8 addrs[2 * ETH_ALEN];
440*a1157835SDaniel Fojt 	const u8 *addr[3];
441*a1157835SDaniel Fojt 	size_t len[3];
442*a1157835SDaniel Fojt 	size_t num_elem;
443*a1157835SDaniel Fojt 	u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
444*a1157835SDaniel Fojt 		       * mask */
445*a1157835SDaniel Fojt 	u8 mask;
446*a1157835SDaniel Fojt 	struct crypto_bignum *pwe;
447*a1157835SDaniel Fojt 	size_t prime_len = sae->tmp->prime_len * 8;
448*a1157835SDaniel Fojt 	u8 *pwe_buf;
4493ff40c12SJohn Marino 
450*a1157835SDaniel Fojt 	crypto_bignum_deinit(sae->tmp->pwe_ffc, 1);
451*a1157835SDaniel Fojt 	sae->tmp->pwe_ffc = NULL;
452*a1157835SDaniel Fojt 
453*a1157835SDaniel Fojt 	/* Allocate a buffer to maintain selected and candidate PWE for constant
454*a1157835SDaniel Fojt 	 * time selection. */
455*a1157835SDaniel Fojt 	pwe_buf = os_zalloc(prime_len * 2);
456*a1157835SDaniel Fojt 	pwe = crypto_bignum_init();
457*a1157835SDaniel Fojt 	if (!pwe_buf || !pwe)
458*a1157835SDaniel Fojt 		goto fail;
4593ff40c12SJohn Marino 
4603ff40c12SJohn Marino 	wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
4613ff40c12SJohn Marino 			      password, password_len);
4623ff40c12SJohn Marino 
4633ff40c12SJohn Marino 	/*
4643ff40c12SJohn Marino 	 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
4653ff40c12SJohn Marino 	 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
466*a1157835SDaniel Fojt 	 *              password [|| identifier] || counter)
4673ff40c12SJohn Marino 	 */
4683ff40c12SJohn Marino 	sae_pwd_seed_key(addr1, addr2, addrs);
4693ff40c12SJohn Marino 
4703ff40c12SJohn Marino 	addr[0] = password;
4713ff40c12SJohn Marino 	len[0] = password_len;
472*a1157835SDaniel Fojt 	num_elem = 1;
473*a1157835SDaniel Fojt 	if (identifier) {
474*a1157835SDaniel Fojt 		addr[num_elem] = (const u8 *) identifier;
475*a1157835SDaniel Fojt 		len[num_elem] = os_strlen(identifier);
476*a1157835SDaniel Fojt 		num_elem++;
477*a1157835SDaniel Fojt 	}
478*a1157835SDaniel Fojt 	addr[num_elem] = &counter;
479*a1157835SDaniel Fojt 	len[num_elem] = sizeof(counter);
480*a1157835SDaniel Fojt 	num_elem++;
4813ff40c12SJohn Marino 
482*a1157835SDaniel Fojt 	k = dragonfly_min_pwe_loop_iter(sae->group);
483*a1157835SDaniel Fojt 
484*a1157835SDaniel Fojt 	for (counter = 1; counter <= k || !found; counter++) {
4853ff40c12SJohn Marino 		u8 pwd_seed[SHA256_MAC_LEN];
4863ff40c12SJohn Marino 		int res;
4873ff40c12SJohn Marino 
4883ff40c12SJohn Marino 		if (counter > 200) {
4893ff40c12SJohn Marino 			/* This should not happen in practice */
4903ff40c12SJohn Marino 			wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
4913ff40c12SJohn Marino 			break;
4923ff40c12SJohn Marino 		}
4933ff40c12SJohn Marino 
494*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: counter = %02u", counter);
495*a1157835SDaniel Fojt 		if (hmac_sha256_vector(addrs, sizeof(addrs), num_elem,
496*a1157835SDaniel Fojt 				       addr, len, pwd_seed) < 0)
4973ff40c12SJohn Marino 			break;
498*a1157835SDaniel Fojt 		res = sae_test_pwd_seed_ffc(sae, pwd_seed, pwe);
499*a1157835SDaniel Fojt 		/* res is -1 for fatal failure, 0 if a valid PWE was not found,
500*a1157835SDaniel Fojt 		 * or 1 if a valid PWE was found. */
5013ff40c12SJohn Marino 		if (res < 0)
5023ff40c12SJohn Marino 			break;
503*a1157835SDaniel Fojt 		/* Store the candidate PWE into the second half of pwe_buf and
504*a1157835SDaniel Fojt 		 * the selected PWE in the beginning of pwe_buf using constant
505*a1157835SDaniel Fojt 		 * time selection. */
506*a1157835SDaniel Fojt 		if (crypto_bignum_to_bin(pwe, pwe_buf + prime_len, prime_len,
507*a1157835SDaniel Fojt 					 prime_len) < 0)
508*a1157835SDaniel Fojt 			break;
509*a1157835SDaniel Fojt 		const_time_select_bin(found, pwe_buf, pwe_buf + prime_len,
510*a1157835SDaniel Fojt 				      prime_len, pwe_buf);
511*a1157835SDaniel Fojt 		sel_counter = const_time_select_u8(found, sel_counter, counter);
512*a1157835SDaniel Fojt 		mask = const_time_eq_u8(res, 1);
513*a1157835SDaniel Fojt 		found = const_time_select_u8(found, found, mask);
5143ff40c12SJohn Marino 	}
5153ff40c12SJohn Marino 
516*a1157835SDaniel Fojt 	if (!found)
517*a1157835SDaniel Fojt 		goto fail;
518*a1157835SDaniel Fojt 
519*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SAE: Use PWE from counter = %02u", sel_counter);
520*a1157835SDaniel Fojt 	sae->tmp->pwe_ffc = crypto_bignum_init_set(pwe_buf, prime_len);
521*a1157835SDaniel Fojt fail:
522*a1157835SDaniel Fojt 	crypto_bignum_deinit(pwe, 1);
523*a1157835SDaniel Fojt 	bin_clear_free(pwe_buf, prime_len * 2);
524*a1157835SDaniel Fojt 	return sae->tmp->pwe_ffc ? 0 : -1;
5253ff40c12SJohn Marino }
5263ff40c12SJohn Marino 
5273ff40c12SJohn Marino 
sae_derive_commit_element_ecc(struct sae_data * sae,struct crypto_bignum * mask)5283ff40c12SJohn Marino static int sae_derive_commit_element_ecc(struct sae_data *sae,
5293ff40c12SJohn Marino 					 struct crypto_bignum *mask)
5303ff40c12SJohn Marino {
5313ff40c12SJohn Marino 	/* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
5323ff40c12SJohn Marino 	if (!sae->tmp->own_commit_element_ecc) {
5333ff40c12SJohn Marino 		sae->tmp->own_commit_element_ecc =
5343ff40c12SJohn Marino 			crypto_ec_point_init(sae->tmp->ec);
5353ff40c12SJohn Marino 		if (!sae->tmp->own_commit_element_ecc)
5363ff40c12SJohn Marino 			return -1;
5373ff40c12SJohn Marino 	}
5383ff40c12SJohn Marino 
5393ff40c12SJohn Marino 	if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc, mask,
5403ff40c12SJohn Marino 				sae->tmp->own_commit_element_ecc) < 0 ||
5413ff40c12SJohn Marino 	    crypto_ec_point_invert(sae->tmp->ec,
5423ff40c12SJohn Marino 				   sae->tmp->own_commit_element_ecc) < 0) {
5433ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
5443ff40c12SJohn Marino 		return -1;
5453ff40c12SJohn Marino 	}
5463ff40c12SJohn Marino 
5473ff40c12SJohn Marino 	return 0;
5483ff40c12SJohn Marino }
5493ff40c12SJohn Marino 
5503ff40c12SJohn Marino 
sae_derive_commit_element_ffc(struct sae_data * sae,struct crypto_bignum * mask)5513ff40c12SJohn Marino static int sae_derive_commit_element_ffc(struct sae_data *sae,
5523ff40c12SJohn Marino 					 struct crypto_bignum *mask)
5533ff40c12SJohn Marino {
5543ff40c12SJohn Marino 	/* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
5553ff40c12SJohn Marino 	if (!sae->tmp->own_commit_element_ffc) {
5563ff40c12SJohn Marino 		sae->tmp->own_commit_element_ffc = crypto_bignum_init();
5573ff40c12SJohn Marino 		if (!sae->tmp->own_commit_element_ffc)
5583ff40c12SJohn Marino 			return -1;
5593ff40c12SJohn Marino 	}
5603ff40c12SJohn Marino 
5613ff40c12SJohn Marino 	if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, mask, sae->tmp->prime,
5623ff40c12SJohn Marino 				  sae->tmp->own_commit_element_ffc) < 0 ||
5633ff40c12SJohn Marino 	    crypto_bignum_inverse(sae->tmp->own_commit_element_ffc,
5643ff40c12SJohn Marino 				  sae->tmp->prime,
5653ff40c12SJohn Marino 				  sae->tmp->own_commit_element_ffc) < 0) {
5663ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
5673ff40c12SJohn Marino 		return -1;
5683ff40c12SJohn Marino 	}
5693ff40c12SJohn Marino 
5703ff40c12SJohn Marino 	return 0;
5713ff40c12SJohn Marino }
5723ff40c12SJohn Marino 
5733ff40c12SJohn Marino 
sae_derive_commit(struct sae_data * sae)5743ff40c12SJohn Marino static int sae_derive_commit(struct sae_data *sae)
5753ff40c12SJohn Marino {
5763ff40c12SJohn Marino 	struct crypto_bignum *mask;
577*a1157835SDaniel Fojt 	int ret;
5783ff40c12SJohn Marino 
579*a1157835SDaniel Fojt 	mask = crypto_bignum_init();
580*a1157835SDaniel Fojt 	if (!sae->tmp->sae_rand)
581*a1157835SDaniel Fojt 		sae->tmp->sae_rand = crypto_bignum_init();
5823ff40c12SJohn Marino 	if (!sae->tmp->own_commit_scalar)
583*a1157835SDaniel Fojt 		sae->tmp->own_commit_scalar = crypto_bignum_init();
584*a1157835SDaniel Fojt 	ret = !mask || !sae->tmp->sae_rand || !sae->tmp->own_commit_scalar ||
585*a1157835SDaniel Fojt 		dragonfly_generate_scalar(sae->tmp->order, sae->tmp->sae_rand,
586*a1157835SDaniel Fojt 					  mask,
587*a1157835SDaniel Fojt 					  sae->tmp->own_commit_scalar) < 0 ||
588*a1157835SDaniel Fojt 		(sae->tmp->ec &&
589*a1157835SDaniel Fojt 		 sae_derive_commit_element_ecc(sae, mask) < 0) ||
590*a1157835SDaniel Fojt 		(sae->tmp->dh &&
591*a1157835SDaniel Fojt 		 sae_derive_commit_element_ffc(sae, mask) < 0);
5923ff40c12SJohn Marino 	crypto_bignum_deinit(mask, 1);
593*a1157835SDaniel Fojt 	return ret ? -1 : 0;
5943ff40c12SJohn Marino }
5953ff40c12SJohn Marino 
5963ff40c12SJohn Marino 
sae_prepare_commit(const u8 * addr1,const u8 * addr2,const u8 * password,size_t password_len,const char * identifier,struct sae_data * sae)5973ff40c12SJohn Marino int sae_prepare_commit(const u8 *addr1, const u8 *addr2,
5983ff40c12SJohn Marino 		       const u8 *password, size_t password_len,
599*a1157835SDaniel Fojt 		       const char *identifier, struct sae_data *sae)
6003ff40c12SJohn Marino {
601*a1157835SDaniel Fojt 	if (sae->tmp == NULL ||
602*a1157835SDaniel Fojt 	    (sae->tmp->ec && sae_derive_pwe_ecc(sae, addr1, addr2, password,
603*a1157835SDaniel Fojt 						password_len,
604*a1157835SDaniel Fojt 						identifier) < 0) ||
605*a1157835SDaniel Fojt 	    (sae->tmp->dh && sae_derive_pwe_ffc(sae, addr1, addr2, password,
606*a1157835SDaniel Fojt 						password_len,
607*a1157835SDaniel Fojt 						identifier) < 0) ||
608*a1157835SDaniel Fojt 	    sae_derive_commit(sae) < 0)
6093ff40c12SJohn Marino 		return -1;
6103ff40c12SJohn Marino 	return 0;
6113ff40c12SJohn Marino }
6123ff40c12SJohn Marino 
6133ff40c12SJohn Marino 
sae_derive_k_ecc(struct sae_data * sae,u8 * k)6143ff40c12SJohn Marino static int sae_derive_k_ecc(struct sae_data *sae, u8 *k)
6153ff40c12SJohn Marino {
6163ff40c12SJohn Marino 	struct crypto_ec_point *K;
6173ff40c12SJohn Marino 	int ret = -1;
6183ff40c12SJohn Marino 
6193ff40c12SJohn Marino 	K = crypto_ec_point_init(sae->tmp->ec);
6203ff40c12SJohn Marino 	if (K == NULL)
6213ff40c12SJohn Marino 		goto fail;
6223ff40c12SJohn Marino 
6233ff40c12SJohn Marino 	/*
6243ff40c12SJohn Marino 	 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
6253ff40c12SJohn Marino 	 *                                        PEER-COMMIT-ELEMENT)))
6263ff40c12SJohn Marino 	 * If K is identity element (point-at-infinity), reject
6273ff40c12SJohn Marino 	 * k = F(K) (= x coordinate)
6283ff40c12SJohn Marino 	 */
6293ff40c12SJohn Marino 
6303ff40c12SJohn Marino 	if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc,
6313ff40c12SJohn Marino 				sae->peer_commit_scalar, K) < 0 ||
6323ff40c12SJohn Marino 	    crypto_ec_point_add(sae->tmp->ec, K,
6333ff40c12SJohn Marino 				sae->tmp->peer_commit_element_ecc, K) < 0 ||
6343ff40c12SJohn Marino 	    crypto_ec_point_mul(sae->tmp->ec, K, sae->tmp->sae_rand, K) < 0 ||
6353ff40c12SJohn Marino 	    crypto_ec_point_is_at_infinity(sae->tmp->ec, K) ||
6363ff40c12SJohn Marino 	    crypto_ec_point_to_bin(sae->tmp->ec, K, k, NULL) < 0) {
6373ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
6383ff40c12SJohn Marino 		goto fail;
6393ff40c12SJohn Marino 	}
6403ff40c12SJohn Marino 
6413ff40c12SJohn Marino 	wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
6423ff40c12SJohn Marino 
6433ff40c12SJohn Marino 	ret = 0;
6443ff40c12SJohn Marino fail:
6453ff40c12SJohn Marino 	crypto_ec_point_deinit(K, 1);
6463ff40c12SJohn Marino 	return ret;
6473ff40c12SJohn Marino }
6483ff40c12SJohn Marino 
6493ff40c12SJohn Marino 
sae_derive_k_ffc(struct sae_data * sae,u8 * k)6503ff40c12SJohn Marino static int sae_derive_k_ffc(struct sae_data *sae, u8 *k)
6513ff40c12SJohn Marino {
6523ff40c12SJohn Marino 	struct crypto_bignum *K;
6533ff40c12SJohn Marino 	int ret = -1;
6543ff40c12SJohn Marino 
6553ff40c12SJohn Marino 	K = crypto_bignum_init();
6563ff40c12SJohn Marino 	if (K == NULL)
6573ff40c12SJohn Marino 		goto fail;
6583ff40c12SJohn Marino 
6593ff40c12SJohn Marino 	/*
6603ff40c12SJohn Marino 	 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
6613ff40c12SJohn Marino 	 *                                        PEER-COMMIT-ELEMENT)))
6623ff40c12SJohn Marino 	 * If K is identity element (one), reject.
6633ff40c12SJohn Marino 	 * k = F(K) (= x coordinate)
6643ff40c12SJohn Marino 	 */
6653ff40c12SJohn Marino 
6663ff40c12SJohn Marino 	if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, sae->peer_commit_scalar,
6673ff40c12SJohn Marino 				  sae->tmp->prime, K) < 0 ||
6683ff40c12SJohn Marino 	    crypto_bignum_mulmod(K, sae->tmp->peer_commit_element_ffc,
6693ff40c12SJohn Marino 				 sae->tmp->prime, K) < 0 ||
6703ff40c12SJohn Marino 	    crypto_bignum_exptmod(K, sae->tmp->sae_rand, sae->tmp->prime, K) < 0
6713ff40c12SJohn Marino 	    ||
6723ff40c12SJohn Marino 	    crypto_bignum_is_one(K) ||
6733ff40c12SJohn Marino 	    crypto_bignum_to_bin(K, k, SAE_MAX_PRIME_LEN, sae->tmp->prime_len) <
6743ff40c12SJohn Marino 	    0) {
6753ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
6763ff40c12SJohn Marino 		goto fail;
6773ff40c12SJohn Marino 	}
6783ff40c12SJohn Marino 
6793ff40c12SJohn Marino 	wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
6803ff40c12SJohn Marino 
6813ff40c12SJohn Marino 	ret = 0;
6823ff40c12SJohn Marino fail:
6833ff40c12SJohn Marino 	crypto_bignum_deinit(K, 1);
6843ff40c12SJohn Marino 	return ret;
6853ff40c12SJohn Marino }
6863ff40c12SJohn Marino 
6873ff40c12SJohn Marino 
sae_derive_keys(struct sae_data * sae,const u8 * k)6883ff40c12SJohn Marino static int sae_derive_keys(struct sae_data *sae, const u8 *k)
6893ff40c12SJohn Marino {
6903ff40c12SJohn Marino 	u8 null_key[SAE_KEYSEED_KEY_LEN], val[SAE_MAX_PRIME_LEN];
6913ff40c12SJohn Marino 	u8 keyseed[SHA256_MAC_LEN];
6923ff40c12SJohn Marino 	u8 keys[SAE_KCK_LEN + SAE_PMK_LEN];
6933ff40c12SJohn Marino 	struct crypto_bignum *tmp;
6943ff40c12SJohn Marino 	int ret = -1;
6953ff40c12SJohn Marino 
6963ff40c12SJohn Marino 	tmp = crypto_bignum_init();
6973ff40c12SJohn Marino 	if (tmp == NULL)
6983ff40c12SJohn Marino 		goto fail;
6993ff40c12SJohn Marino 
7003ff40c12SJohn Marino 	/* keyseed = H(<0>32, k)
7013ff40c12SJohn Marino 	 * KCK || PMK = KDF-512(keyseed, "SAE KCK and PMK",
7023ff40c12SJohn Marino 	 *                      (commit-scalar + peer-commit-scalar) modulo r)
7033ff40c12SJohn Marino 	 * PMKID = L((commit-scalar + peer-commit-scalar) modulo r, 0, 128)
7043ff40c12SJohn Marino 	 */
7053ff40c12SJohn Marino 
7063ff40c12SJohn Marino 	os_memset(null_key, 0, sizeof(null_key));
7073ff40c12SJohn Marino 	hmac_sha256(null_key, sizeof(null_key), k, sae->tmp->prime_len,
7083ff40c12SJohn Marino 		    keyseed);
7093ff40c12SJohn Marino 	wpa_hexdump_key(MSG_DEBUG, "SAE: keyseed", keyseed, sizeof(keyseed));
7103ff40c12SJohn Marino 
7113ff40c12SJohn Marino 	crypto_bignum_add(sae->tmp->own_commit_scalar, sae->peer_commit_scalar,
7123ff40c12SJohn Marino 			  tmp);
7133ff40c12SJohn Marino 	crypto_bignum_mod(tmp, sae->tmp->order, tmp);
714*a1157835SDaniel Fojt 	/* IEEE Std 802.11-2016 is not exactly clear on the encoding of the bit
715*a1157835SDaniel Fojt 	 * string that is needed for KCK, PMK, and PMKID derivation, but it
716*a1157835SDaniel Fojt 	 * seems to make most sense to encode the
717*a1157835SDaniel Fojt 	 * (commit-scalar + peer-commit-scalar) mod r part as a bit string by
718*a1157835SDaniel Fojt 	 * zero padding it from left to the length of the order (in full
719*a1157835SDaniel Fojt 	 * octets). */
720*a1157835SDaniel Fojt 	crypto_bignum_to_bin(tmp, val, sizeof(val), sae->tmp->order_len);
7213ff40c12SJohn Marino 	wpa_hexdump(MSG_DEBUG, "SAE: PMKID", val, SAE_PMKID_LEN);
722*a1157835SDaniel Fojt 	if (sha256_prf(keyseed, sizeof(keyseed), "SAE KCK and PMK",
723*a1157835SDaniel Fojt 		       val, sae->tmp->order_len, keys, sizeof(keys)) < 0)
724*a1157835SDaniel Fojt 		goto fail;
725*a1157835SDaniel Fojt 	os_memset(keyseed, 0, sizeof(keyseed));
7263ff40c12SJohn Marino 	os_memcpy(sae->tmp->kck, keys, SAE_KCK_LEN);
7273ff40c12SJohn Marino 	os_memcpy(sae->pmk, keys + SAE_KCK_LEN, SAE_PMK_LEN);
728*a1157835SDaniel Fojt 	os_memcpy(sae->pmkid, val, SAE_PMKID_LEN);
729*a1157835SDaniel Fojt 	os_memset(keys, 0, sizeof(keys));
7303ff40c12SJohn Marino 	wpa_hexdump_key(MSG_DEBUG, "SAE: KCK", sae->tmp->kck, SAE_KCK_LEN);
7313ff40c12SJohn Marino 	wpa_hexdump_key(MSG_DEBUG, "SAE: PMK", sae->pmk, SAE_PMK_LEN);
7323ff40c12SJohn Marino 
7333ff40c12SJohn Marino 	ret = 0;
7343ff40c12SJohn Marino fail:
7353ff40c12SJohn Marino 	crypto_bignum_deinit(tmp, 0);
7363ff40c12SJohn Marino 	return ret;
7373ff40c12SJohn Marino }
7383ff40c12SJohn Marino 
7393ff40c12SJohn Marino 
sae_process_commit(struct sae_data * sae)7403ff40c12SJohn Marino int sae_process_commit(struct sae_data *sae)
7413ff40c12SJohn Marino {
7423ff40c12SJohn Marino 	u8 k[SAE_MAX_PRIME_LEN];
743*a1157835SDaniel Fojt 	if (sae->tmp == NULL ||
744*a1157835SDaniel Fojt 	    (sae->tmp->ec && sae_derive_k_ecc(sae, k) < 0) ||
7453ff40c12SJohn Marino 	    (sae->tmp->dh && sae_derive_k_ffc(sae, k) < 0) ||
7463ff40c12SJohn Marino 	    sae_derive_keys(sae, k) < 0)
7473ff40c12SJohn Marino 		return -1;
7483ff40c12SJohn Marino 	return 0;
7493ff40c12SJohn Marino }
7503ff40c12SJohn Marino 
7513ff40c12SJohn Marino 
sae_write_commit(struct sae_data * sae,struct wpabuf * buf,const struct wpabuf * token,const char * identifier)7523ff40c12SJohn Marino void sae_write_commit(struct sae_data *sae, struct wpabuf *buf,
753*a1157835SDaniel Fojt 		      const struct wpabuf *token, const char *identifier)
7543ff40c12SJohn Marino {
7553ff40c12SJohn Marino 	u8 *pos;
756*a1157835SDaniel Fojt 
757*a1157835SDaniel Fojt 	if (sae->tmp == NULL)
758*a1157835SDaniel Fojt 		return;
759*a1157835SDaniel Fojt 
7603ff40c12SJohn Marino 	wpabuf_put_le16(buf, sae->group); /* Finite Cyclic Group */
761*a1157835SDaniel Fojt 	if (token) {
7623ff40c12SJohn Marino 		wpabuf_put_buf(buf, token);
763*a1157835SDaniel Fojt 		wpa_hexdump(MSG_DEBUG, "SAE: Anti-clogging token",
764*a1157835SDaniel Fojt 			    wpabuf_head(token), wpabuf_len(token));
765*a1157835SDaniel Fojt 	}
7663ff40c12SJohn Marino 	pos = wpabuf_put(buf, sae->tmp->prime_len);
7673ff40c12SJohn Marino 	crypto_bignum_to_bin(sae->tmp->own_commit_scalar, pos,
7683ff40c12SJohn Marino 			     sae->tmp->prime_len, sae->tmp->prime_len);
7693ff40c12SJohn Marino 	wpa_hexdump(MSG_DEBUG, "SAE: own commit-scalar",
7703ff40c12SJohn Marino 		    pos, sae->tmp->prime_len);
7713ff40c12SJohn Marino 	if (sae->tmp->ec) {
7723ff40c12SJohn Marino 		pos = wpabuf_put(buf, 2 * sae->tmp->prime_len);
7733ff40c12SJohn Marino 		crypto_ec_point_to_bin(sae->tmp->ec,
7743ff40c12SJohn Marino 				       sae->tmp->own_commit_element_ecc,
7753ff40c12SJohn Marino 				       pos, pos + sae->tmp->prime_len);
7763ff40c12SJohn Marino 		wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(x)",
7773ff40c12SJohn Marino 			    pos, sae->tmp->prime_len);
7783ff40c12SJohn Marino 		wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(y)",
7793ff40c12SJohn Marino 			    pos + sae->tmp->prime_len, sae->tmp->prime_len);
7803ff40c12SJohn Marino 	} else {
7813ff40c12SJohn Marino 		pos = wpabuf_put(buf, sae->tmp->prime_len);
7823ff40c12SJohn Marino 		crypto_bignum_to_bin(sae->tmp->own_commit_element_ffc, pos,
7833ff40c12SJohn Marino 				     sae->tmp->prime_len, sae->tmp->prime_len);
7843ff40c12SJohn Marino 		wpa_hexdump(MSG_DEBUG, "SAE: own commit-element",
7853ff40c12SJohn Marino 			    pos, sae->tmp->prime_len);
7863ff40c12SJohn Marino 	}
787*a1157835SDaniel Fojt 
788*a1157835SDaniel Fojt 	if (identifier) {
789*a1157835SDaniel Fojt 		/* Password Identifier element */
790*a1157835SDaniel Fojt 		wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
791*a1157835SDaniel Fojt 		wpabuf_put_u8(buf, 1 + os_strlen(identifier));
792*a1157835SDaniel Fojt 		wpabuf_put_u8(buf, WLAN_EID_EXT_PASSWORD_IDENTIFIER);
793*a1157835SDaniel Fojt 		wpabuf_put_str(buf, identifier);
794*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: own Password Identifier: %s",
795*a1157835SDaniel Fojt 			   identifier);
796*a1157835SDaniel Fojt 	}
7973ff40c12SJohn Marino }
7983ff40c12SJohn Marino 
7993ff40c12SJohn Marino 
sae_group_allowed(struct sae_data * sae,int * allowed_groups,u16 group)800*a1157835SDaniel Fojt u16 sae_group_allowed(struct sae_data *sae, int *allowed_groups, u16 group)
8013ff40c12SJohn Marino {
8023ff40c12SJohn Marino 	if (allowed_groups) {
8033ff40c12SJohn Marino 		int i;
8043ff40c12SJohn Marino 		for (i = 0; allowed_groups[i] > 0; i++) {
8053ff40c12SJohn Marino 			if (allowed_groups[i] == group)
8063ff40c12SJohn Marino 				break;
8073ff40c12SJohn Marino 		}
8083ff40c12SJohn Marino 		if (allowed_groups[i] != group) {
8093ff40c12SJohn Marino 			wpa_printf(MSG_DEBUG, "SAE: Proposed group %u not "
8103ff40c12SJohn Marino 				   "enabled in the current configuration",
8113ff40c12SJohn Marino 				   group);
8123ff40c12SJohn Marino 			return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
8133ff40c12SJohn Marino 		}
8143ff40c12SJohn Marino 	}
8153ff40c12SJohn Marino 
8163ff40c12SJohn Marino 	if (sae->state == SAE_COMMITTED && group != sae->group) {
8173ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Do not allow group to be changed");
8183ff40c12SJohn Marino 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
8193ff40c12SJohn Marino 	}
8203ff40c12SJohn Marino 
8213ff40c12SJohn Marino 	if (group != sae->group && sae_set_group(sae, group) < 0) {
8223ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Unsupported Finite Cyclic Group %u",
8233ff40c12SJohn Marino 			   group);
8243ff40c12SJohn Marino 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
8253ff40c12SJohn Marino 	}
8263ff40c12SJohn Marino 
8273ff40c12SJohn Marino 	if (sae->tmp == NULL) {
8283ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Group information not yet initialized");
8293ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
8303ff40c12SJohn Marino 	}
8313ff40c12SJohn Marino 
8323ff40c12SJohn Marino 	if (sae->tmp->dh && !allowed_groups) {
8333ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Do not allow FFC group %u without "
8343ff40c12SJohn Marino 			   "explicit configuration enabling it", group);
8353ff40c12SJohn Marino 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
8363ff40c12SJohn Marino 	}
8373ff40c12SJohn Marino 
8383ff40c12SJohn Marino 	return WLAN_STATUS_SUCCESS;
8393ff40c12SJohn Marino }
8403ff40c12SJohn Marino 
8413ff40c12SJohn Marino 
sae_is_password_id_elem(const u8 * pos,const u8 * end)842*a1157835SDaniel Fojt static int sae_is_password_id_elem(const u8 *pos, const u8 *end)
843*a1157835SDaniel Fojt {
844*a1157835SDaniel Fojt 	return end - pos >= 3 &&
845*a1157835SDaniel Fojt 		pos[0] == WLAN_EID_EXTENSION &&
846*a1157835SDaniel Fojt 		pos[1] >= 1 &&
847*a1157835SDaniel Fojt 		end - pos - 2 >= pos[1] &&
848*a1157835SDaniel Fojt 		pos[2] == WLAN_EID_EXT_PASSWORD_IDENTIFIER;
849*a1157835SDaniel Fojt }
850*a1157835SDaniel Fojt 
851*a1157835SDaniel Fojt 
sae_parse_commit_token(struct sae_data * sae,const u8 ** pos,const u8 * end,const u8 ** token,size_t * token_len)8523ff40c12SJohn Marino static void sae_parse_commit_token(struct sae_data *sae, const u8 **pos,
8533ff40c12SJohn Marino 				   const u8 *end, const u8 **token,
8543ff40c12SJohn Marino 				   size_t *token_len)
8553ff40c12SJohn Marino {
856*a1157835SDaniel Fojt 	size_t scalar_elem_len, tlen;
857*a1157835SDaniel Fojt 	const u8 *elem;
858*a1157835SDaniel Fojt 
859*a1157835SDaniel Fojt 	if (token)
860*a1157835SDaniel Fojt 		*token = NULL;
861*a1157835SDaniel Fojt 	if (token_len)
862*a1157835SDaniel Fojt 		*token_len = 0;
863*a1157835SDaniel Fojt 
864*a1157835SDaniel Fojt 	scalar_elem_len = (sae->tmp->ec ? 3 : 2) * sae->tmp->prime_len;
865*a1157835SDaniel Fojt 	if (scalar_elem_len >= (size_t) (end - *pos))
866*a1157835SDaniel Fojt 		return; /* No extra data beyond peer scalar and element */
867*a1157835SDaniel Fojt 
868*a1157835SDaniel Fojt 	/* It is a bit difficult to parse this now that there is an
869*a1157835SDaniel Fojt 	 * optional variable length Anti-Clogging Token field and
870*a1157835SDaniel Fojt 	 * optional variable length Password Identifier element in the
871*a1157835SDaniel Fojt 	 * frame. We are sending out fixed length Anti-Clogging Token
872*a1157835SDaniel Fojt 	 * fields, so use that length as a requirement for the received
873*a1157835SDaniel Fojt 	 * token and check for the presence of possible Password
874*a1157835SDaniel Fojt 	 * Identifier element based on the element header information.
875*a1157835SDaniel Fojt 	 */
876*a1157835SDaniel Fojt 	tlen = end - (*pos + scalar_elem_len);
877*a1157835SDaniel Fojt 
878*a1157835SDaniel Fojt 	if (tlen < SHA256_MAC_LEN) {
879*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
880*a1157835SDaniel Fojt 			   "SAE: Too short optional data (%u octets) to include our Anti-Clogging Token",
881*a1157835SDaniel Fojt 			   (unsigned int) tlen);
882*a1157835SDaniel Fojt 		return;
883*a1157835SDaniel Fojt 	}
884*a1157835SDaniel Fojt 
885*a1157835SDaniel Fojt 	elem = *pos + scalar_elem_len;
886*a1157835SDaniel Fojt 	if (sae_is_password_id_elem(elem, end)) {
887*a1157835SDaniel Fojt 		 /* Password Identifier element takes out all available
888*a1157835SDaniel Fojt 		  * extra octets, so there can be no Anti-Clogging token in
889*a1157835SDaniel Fojt 		  * this frame. */
890*a1157835SDaniel Fojt 		return;
891*a1157835SDaniel Fojt 	}
892*a1157835SDaniel Fojt 
893*a1157835SDaniel Fojt 	elem += SHA256_MAC_LEN;
894*a1157835SDaniel Fojt 	if (sae_is_password_id_elem(elem, end)) {
895*a1157835SDaniel Fojt 		 /* Password Identifier element is included in the end, so
896*a1157835SDaniel Fojt 		  * remove its length from the Anti-Clogging token field. */
897*a1157835SDaniel Fojt 		tlen -= 2 + elem[1];
898*a1157835SDaniel Fojt 	}
899*a1157835SDaniel Fojt 
9003ff40c12SJohn Marino 	wpa_hexdump(MSG_DEBUG, "SAE: Anti-Clogging Token", *pos, tlen);
9013ff40c12SJohn Marino 	if (token)
9023ff40c12SJohn Marino 		*token = *pos;
9033ff40c12SJohn Marino 	if (token_len)
9043ff40c12SJohn Marino 		*token_len = tlen;
9053ff40c12SJohn Marino 	*pos += tlen;
9063ff40c12SJohn Marino }
9073ff40c12SJohn Marino 
9083ff40c12SJohn Marino 
sae_parse_commit_scalar(struct sae_data * sae,const u8 ** pos,const u8 * end)9093ff40c12SJohn Marino static u16 sae_parse_commit_scalar(struct sae_data *sae, const u8 **pos,
9103ff40c12SJohn Marino 				   const u8 *end)
9113ff40c12SJohn Marino {
9123ff40c12SJohn Marino 	struct crypto_bignum *peer_scalar;
9133ff40c12SJohn Marino 
914*a1157835SDaniel Fojt 	if (sae->tmp->prime_len > end - *pos) {
9153ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Not enough data for scalar");
9163ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
9173ff40c12SJohn Marino 	}
9183ff40c12SJohn Marino 
9193ff40c12SJohn Marino 	peer_scalar = crypto_bignum_init_set(*pos, sae->tmp->prime_len);
9203ff40c12SJohn Marino 	if (peer_scalar == NULL)
9213ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
9223ff40c12SJohn Marino 
9233ff40c12SJohn Marino 	/*
9243ff40c12SJohn Marino 	 * IEEE Std 802.11-2012, 11.3.8.6.1: If there is a protocol instance for
9253ff40c12SJohn Marino 	 * the peer and it is in Authenticated state, the new Commit Message
9263ff40c12SJohn Marino 	 * shall be dropped if the peer-scalar is identical to the one used in
9273ff40c12SJohn Marino 	 * the existing protocol instance.
9283ff40c12SJohn Marino 	 */
9293ff40c12SJohn Marino 	if (sae->state == SAE_ACCEPTED && sae->peer_commit_scalar &&
9303ff40c12SJohn Marino 	    crypto_bignum_cmp(sae->peer_commit_scalar, peer_scalar) == 0) {
9313ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Do not accept re-use of previous "
9323ff40c12SJohn Marino 			   "peer-commit-scalar");
9333ff40c12SJohn Marino 		crypto_bignum_deinit(peer_scalar, 0);
9343ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
9353ff40c12SJohn Marino 	}
9363ff40c12SJohn Marino 
937*a1157835SDaniel Fojt 	/* 1 < scalar < r */
9383ff40c12SJohn Marino 	if (crypto_bignum_is_zero(peer_scalar) ||
939*a1157835SDaniel Fojt 	    crypto_bignum_is_one(peer_scalar) ||
9403ff40c12SJohn Marino 	    crypto_bignum_cmp(peer_scalar, sae->tmp->order) >= 0) {
9413ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Invalid peer scalar");
9423ff40c12SJohn Marino 		crypto_bignum_deinit(peer_scalar, 0);
9433ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
9443ff40c12SJohn Marino 	}
9453ff40c12SJohn Marino 
9463ff40c12SJohn Marino 
9473ff40c12SJohn Marino 	crypto_bignum_deinit(sae->peer_commit_scalar, 0);
9483ff40c12SJohn Marino 	sae->peer_commit_scalar = peer_scalar;
9493ff40c12SJohn Marino 	wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-scalar",
9503ff40c12SJohn Marino 		    *pos, sae->tmp->prime_len);
9513ff40c12SJohn Marino 	*pos += sae->tmp->prime_len;
9523ff40c12SJohn Marino 
9533ff40c12SJohn Marino 	return WLAN_STATUS_SUCCESS;
9543ff40c12SJohn Marino }
9553ff40c12SJohn Marino 
9563ff40c12SJohn Marino 
sae_parse_commit_element_ecc(struct sae_data * sae,const u8 ** pos,const u8 * end)957*a1157835SDaniel Fojt static u16 sae_parse_commit_element_ecc(struct sae_data *sae, const u8 **pos,
9583ff40c12SJohn Marino 					const u8 *end)
9593ff40c12SJohn Marino {
9603ff40c12SJohn Marino 	u8 prime[SAE_MAX_ECC_PRIME_LEN];
9613ff40c12SJohn Marino 
962*a1157835SDaniel Fojt 	if (2 * sae->tmp->prime_len > end - *pos) {
9633ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
9643ff40c12SJohn Marino 			   "commit-element");
9653ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
9663ff40c12SJohn Marino 	}
9673ff40c12SJohn Marino 
9683ff40c12SJohn Marino 	if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
9693ff40c12SJohn Marino 				 sae->tmp->prime_len) < 0)
9703ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
9713ff40c12SJohn Marino 
9723ff40c12SJohn Marino 	/* element x and y coordinates < p */
973*a1157835SDaniel Fojt 	if (os_memcmp(*pos, prime, sae->tmp->prime_len) >= 0 ||
974*a1157835SDaniel Fojt 	    os_memcmp(*pos + sae->tmp->prime_len, prime,
9753ff40c12SJohn Marino 		      sae->tmp->prime_len) >= 0) {
9763ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Invalid coordinates in peer "
9773ff40c12SJohn Marino 			   "element");
9783ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
9793ff40c12SJohn Marino 	}
9803ff40c12SJohn Marino 
9813ff40c12SJohn Marino 	wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(x)",
982*a1157835SDaniel Fojt 		    *pos, sae->tmp->prime_len);
9833ff40c12SJohn Marino 	wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(y)",
984*a1157835SDaniel Fojt 		    *pos + sae->tmp->prime_len, sae->tmp->prime_len);
9853ff40c12SJohn Marino 
9863ff40c12SJohn Marino 	crypto_ec_point_deinit(sae->tmp->peer_commit_element_ecc, 0);
9873ff40c12SJohn Marino 	sae->tmp->peer_commit_element_ecc =
988*a1157835SDaniel Fojt 		crypto_ec_point_from_bin(sae->tmp->ec, *pos);
9893ff40c12SJohn Marino 	if (sae->tmp->peer_commit_element_ecc == NULL)
9903ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
9913ff40c12SJohn Marino 
9923ff40c12SJohn Marino 	if (!crypto_ec_point_is_on_curve(sae->tmp->ec,
9933ff40c12SJohn Marino 					 sae->tmp->peer_commit_element_ecc)) {
9943ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Peer element is not on curve");
9953ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
9963ff40c12SJohn Marino 	}
9973ff40c12SJohn Marino 
998*a1157835SDaniel Fojt 	*pos += 2 * sae->tmp->prime_len;
999*a1157835SDaniel Fojt 
10003ff40c12SJohn Marino 	return WLAN_STATUS_SUCCESS;
10013ff40c12SJohn Marino }
10023ff40c12SJohn Marino 
10033ff40c12SJohn Marino 
sae_parse_commit_element_ffc(struct sae_data * sae,const u8 ** pos,const u8 * end)1004*a1157835SDaniel Fojt static u16 sae_parse_commit_element_ffc(struct sae_data *sae, const u8 **pos,
10053ff40c12SJohn Marino 					const u8 *end)
10063ff40c12SJohn Marino {
1007*a1157835SDaniel Fojt 	struct crypto_bignum *res, *one;
1008*a1157835SDaniel Fojt 	const u8 one_bin[1] = { 0x01 };
10093ff40c12SJohn Marino 
1010*a1157835SDaniel Fojt 	if (sae->tmp->prime_len > end - *pos) {
10113ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
10123ff40c12SJohn Marino 			   "commit-element");
10133ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
10143ff40c12SJohn Marino 	}
1015*a1157835SDaniel Fojt 	wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element", *pos,
10163ff40c12SJohn Marino 		    sae->tmp->prime_len);
10173ff40c12SJohn Marino 
10183ff40c12SJohn Marino 	crypto_bignum_deinit(sae->tmp->peer_commit_element_ffc, 0);
10193ff40c12SJohn Marino 	sae->tmp->peer_commit_element_ffc =
1020*a1157835SDaniel Fojt 		crypto_bignum_init_set(*pos, sae->tmp->prime_len);
10213ff40c12SJohn Marino 	if (sae->tmp->peer_commit_element_ffc == NULL)
10223ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
1023*a1157835SDaniel Fojt 	/* 1 < element < p - 1 */
1024*a1157835SDaniel Fojt 	res = crypto_bignum_init();
1025*a1157835SDaniel Fojt 	one = crypto_bignum_init_set(one_bin, sizeof(one_bin));
1026*a1157835SDaniel Fojt 	if (!res || !one ||
1027*a1157835SDaniel Fojt 	    crypto_bignum_sub(sae->tmp->prime, one, res) ||
1028*a1157835SDaniel Fojt 	    crypto_bignum_is_zero(sae->tmp->peer_commit_element_ffc) ||
10293ff40c12SJohn Marino 	    crypto_bignum_is_one(sae->tmp->peer_commit_element_ffc) ||
1030*a1157835SDaniel Fojt 	    crypto_bignum_cmp(sae->tmp->peer_commit_element_ffc, res) >= 0) {
1031*a1157835SDaniel Fojt 		crypto_bignum_deinit(res, 0);
1032*a1157835SDaniel Fojt 		crypto_bignum_deinit(one, 0);
10333ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Invalid peer element");
10343ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
10353ff40c12SJohn Marino 	}
1036*a1157835SDaniel Fojt 	crypto_bignum_deinit(one, 0);
10373ff40c12SJohn Marino 
10383ff40c12SJohn Marino 	/* scalar-op(r, ELEMENT) = 1 modulo p */
1039*a1157835SDaniel Fojt 	if (crypto_bignum_exptmod(sae->tmp->peer_commit_element_ffc,
10403ff40c12SJohn Marino 				  sae->tmp->order, sae->tmp->prime, res) < 0 ||
10413ff40c12SJohn Marino 	    !crypto_bignum_is_one(res)) {
10423ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Invalid peer element (scalar-op)");
10433ff40c12SJohn Marino 		crypto_bignum_deinit(res, 0);
10443ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
10453ff40c12SJohn Marino 	}
10463ff40c12SJohn Marino 	crypto_bignum_deinit(res, 0);
10473ff40c12SJohn Marino 
1048*a1157835SDaniel Fojt 	*pos += sae->tmp->prime_len;
1049*a1157835SDaniel Fojt 
10503ff40c12SJohn Marino 	return WLAN_STATUS_SUCCESS;
10513ff40c12SJohn Marino }
10523ff40c12SJohn Marino 
10533ff40c12SJohn Marino 
sae_parse_commit_element(struct sae_data * sae,const u8 ** pos,const u8 * end)1054*a1157835SDaniel Fojt static u16 sae_parse_commit_element(struct sae_data *sae, const u8 **pos,
10553ff40c12SJohn Marino 				    const u8 *end)
10563ff40c12SJohn Marino {
10573ff40c12SJohn Marino 	if (sae->tmp->dh)
10583ff40c12SJohn Marino 		return sae_parse_commit_element_ffc(sae, pos, end);
10593ff40c12SJohn Marino 	return sae_parse_commit_element_ecc(sae, pos, end);
10603ff40c12SJohn Marino }
10613ff40c12SJohn Marino 
10623ff40c12SJohn Marino 
sae_parse_password_identifier(struct sae_data * sae,const u8 * pos,const u8 * end)1063*a1157835SDaniel Fojt static int sae_parse_password_identifier(struct sae_data *sae,
1064*a1157835SDaniel Fojt 					 const u8 *pos, const u8 *end)
1065*a1157835SDaniel Fojt {
1066*a1157835SDaniel Fojt 	wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
1067*a1157835SDaniel Fojt 		    pos, end - pos);
1068*a1157835SDaniel Fojt 	if (!sae_is_password_id_elem(pos, end)) {
1069*a1157835SDaniel Fojt 		if (sae->tmp->pw_id) {
1070*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1071*a1157835SDaniel Fojt 				   "SAE: No Password Identifier included, but expected one (%s)",
1072*a1157835SDaniel Fojt 				   sae->tmp->pw_id);
1073*a1157835SDaniel Fojt 			return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
1074*a1157835SDaniel Fojt 		}
1075*a1157835SDaniel Fojt 		os_free(sae->tmp->pw_id);
1076*a1157835SDaniel Fojt 		sae->tmp->pw_id = NULL;
1077*a1157835SDaniel Fojt 		return WLAN_STATUS_SUCCESS; /* No Password Identifier */
1078*a1157835SDaniel Fojt 	}
1079*a1157835SDaniel Fojt 
1080*a1157835SDaniel Fojt 	if (sae->tmp->pw_id &&
1081*a1157835SDaniel Fojt 	    (pos[1] - 1 != (int) os_strlen(sae->tmp->pw_id) ||
1082*a1157835SDaniel Fojt 	     os_memcmp(sae->tmp->pw_id, pos + 3, pos[1] - 1) != 0)) {
1083*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1084*a1157835SDaniel Fojt 			   "SAE: The included Password Identifier does not match the expected one (%s)",
1085*a1157835SDaniel Fojt 			   sae->tmp->pw_id);
1086*a1157835SDaniel Fojt 		return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
1087*a1157835SDaniel Fojt 	}
1088*a1157835SDaniel Fojt 
1089*a1157835SDaniel Fojt 	os_free(sae->tmp->pw_id);
1090*a1157835SDaniel Fojt 	sae->tmp->pw_id = os_malloc(pos[1]);
1091*a1157835SDaniel Fojt 	if (!sae->tmp->pw_id)
1092*a1157835SDaniel Fojt 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
1093*a1157835SDaniel Fojt 	os_memcpy(sae->tmp->pw_id, pos + 3, pos[1] - 1);
1094*a1157835SDaniel Fojt 	sae->tmp->pw_id[pos[1] - 1] = '\0';
1095*a1157835SDaniel Fojt 	wpa_hexdump_ascii(MSG_DEBUG, "SAE: Received Password Identifier",
1096*a1157835SDaniel Fojt 			  sae->tmp->pw_id, pos[1] -  1);
1097*a1157835SDaniel Fojt 	return WLAN_STATUS_SUCCESS;
1098*a1157835SDaniel Fojt }
1099*a1157835SDaniel Fojt 
1100*a1157835SDaniel Fojt 
sae_parse_commit(struct sae_data * sae,const u8 * data,size_t len,const u8 ** token,size_t * token_len,int * allowed_groups)11013ff40c12SJohn Marino u16 sae_parse_commit(struct sae_data *sae, const u8 *data, size_t len,
11023ff40c12SJohn Marino 		     const u8 **token, size_t *token_len, int *allowed_groups)
11033ff40c12SJohn Marino {
11043ff40c12SJohn Marino 	const u8 *pos = data, *end = data + len;
11053ff40c12SJohn Marino 	u16 res;
11063ff40c12SJohn Marino 
11073ff40c12SJohn Marino 	/* Check Finite Cyclic Group */
1108*a1157835SDaniel Fojt 	if (end - pos < 2)
11093ff40c12SJohn Marino 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
11103ff40c12SJohn Marino 	res = sae_group_allowed(sae, allowed_groups, WPA_GET_LE16(pos));
11113ff40c12SJohn Marino 	if (res != WLAN_STATUS_SUCCESS)
11123ff40c12SJohn Marino 		return res;
11133ff40c12SJohn Marino 	pos += 2;
11143ff40c12SJohn Marino 
11153ff40c12SJohn Marino 	/* Optional Anti-Clogging Token */
11163ff40c12SJohn Marino 	sae_parse_commit_token(sae, &pos, end, token, token_len);
11173ff40c12SJohn Marino 
11183ff40c12SJohn Marino 	/* commit-scalar */
11193ff40c12SJohn Marino 	res = sae_parse_commit_scalar(sae, &pos, end);
11203ff40c12SJohn Marino 	if (res != WLAN_STATUS_SUCCESS)
11213ff40c12SJohn Marino 		return res;
11223ff40c12SJohn Marino 
11233ff40c12SJohn Marino 	/* commit-element */
1124*a1157835SDaniel Fojt 	res = sae_parse_commit_element(sae, &pos, end);
1125*a1157835SDaniel Fojt 	if (res != WLAN_STATUS_SUCCESS)
1126*a1157835SDaniel Fojt 		return res;
1127*a1157835SDaniel Fojt 
1128*a1157835SDaniel Fojt 	/* Optional Password Identifier element */
1129*a1157835SDaniel Fojt 	res = sae_parse_password_identifier(sae, pos, end);
1130*a1157835SDaniel Fojt 	if (res != WLAN_STATUS_SUCCESS)
1131*a1157835SDaniel Fojt 		return res;
1132*a1157835SDaniel Fojt 
1133*a1157835SDaniel Fojt 	/*
1134*a1157835SDaniel Fojt 	 * Check whether peer-commit-scalar and PEER-COMMIT-ELEMENT are same as
1135*a1157835SDaniel Fojt 	 * the values we sent which would be evidence of a reflection attack.
1136*a1157835SDaniel Fojt 	 */
1137*a1157835SDaniel Fojt 	if (!sae->tmp->own_commit_scalar ||
1138*a1157835SDaniel Fojt 	    crypto_bignum_cmp(sae->tmp->own_commit_scalar,
1139*a1157835SDaniel Fojt 			      sae->peer_commit_scalar) != 0 ||
1140*a1157835SDaniel Fojt 	    (sae->tmp->dh &&
1141*a1157835SDaniel Fojt 	     (!sae->tmp->own_commit_element_ffc ||
1142*a1157835SDaniel Fojt 	      crypto_bignum_cmp(sae->tmp->own_commit_element_ffc,
1143*a1157835SDaniel Fojt 				sae->tmp->peer_commit_element_ffc) != 0)) ||
1144*a1157835SDaniel Fojt 	    (sae->tmp->ec &&
1145*a1157835SDaniel Fojt 	     (!sae->tmp->own_commit_element_ecc ||
1146*a1157835SDaniel Fojt 	      crypto_ec_point_cmp(sae->tmp->ec,
1147*a1157835SDaniel Fojt 				  sae->tmp->own_commit_element_ecc,
1148*a1157835SDaniel Fojt 				  sae->tmp->peer_commit_element_ecc) != 0)))
1149*a1157835SDaniel Fojt 		return WLAN_STATUS_SUCCESS; /* scalars/elements are different */
1150*a1157835SDaniel Fojt 
1151*a1157835SDaniel Fojt 	/*
1152*a1157835SDaniel Fojt 	 * This is a reflection attack - return special value to trigger caller
1153*a1157835SDaniel Fojt 	 * to silently discard the frame instead of replying with a specific
1154*a1157835SDaniel Fojt 	 * status code.
1155*a1157835SDaniel Fojt 	 */
1156*a1157835SDaniel Fojt 	return SAE_SILENTLY_DISCARD;
11573ff40c12SJohn Marino }
11583ff40c12SJohn Marino 
11593ff40c12SJohn Marino 
sae_cn_confirm(struct sae_data * sae,const u8 * sc,const struct crypto_bignum * scalar1,const u8 * element1,size_t element1_len,const struct crypto_bignum * scalar2,const u8 * element2,size_t element2_len,u8 * confirm)11603ff40c12SJohn Marino static void sae_cn_confirm(struct sae_data *sae, const u8 *sc,
11613ff40c12SJohn Marino 			   const struct crypto_bignum *scalar1,
11623ff40c12SJohn Marino 			   const u8 *element1, size_t element1_len,
11633ff40c12SJohn Marino 			   const struct crypto_bignum *scalar2,
11643ff40c12SJohn Marino 			   const u8 *element2, size_t element2_len,
11653ff40c12SJohn Marino 			   u8 *confirm)
11663ff40c12SJohn Marino {
11673ff40c12SJohn Marino 	const u8 *addr[5];
11683ff40c12SJohn Marino 	size_t len[5];
11693ff40c12SJohn Marino 	u8 scalar_b1[SAE_MAX_PRIME_LEN], scalar_b2[SAE_MAX_PRIME_LEN];
11703ff40c12SJohn Marino 
11713ff40c12SJohn Marino 	/* Confirm
11723ff40c12SJohn Marino 	 * CN(key, X, Y, Z, ...) =
11733ff40c12SJohn Marino 	 *    HMAC-SHA256(key, D2OS(X) || D2OS(Y) || D2OS(Z) | ...)
11743ff40c12SJohn Marino 	 * confirm = CN(KCK, send-confirm, commit-scalar, COMMIT-ELEMENT,
11753ff40c12SJohn Marino 	 *              peer-commit-scalar, PEER-COMMIT-ELEMENT)
11763ff40c12SJohn Marino 	 * verifier = CN(KCK, peer-send-confirm, peer-commit-scalar,
11773ff40c12SJohn Marino 	 *               PEER-COMMIT-ELEMENT, commit-scalar, COMMIT-ELEMENT)
11783ff40c12SJohn Marino 	 */
11793ff40c12SJohn Marino 	addr[0] = sc;
11803ff40c12SJohn Marino 	len[0] = 2;
11813ff40c12SJohn Marino 	crypto_bignum_to_bin(scalar1, scalar_b1, sizeof(scalar_b1),
11823ff40c12SJohn Marino 			     sae->tmp->prime_len);
11833ff40c12SJohn Marino 	addr[1] = scalar_b1;
11843ff40c12SJohn Marino 	len[1] = sae->tmp->prime_len;
11853ff40c12SJohn Marino 	addr[2] = element1;
11863ff40c12SJohn Marino 	len[2] = element1_len;
11873ff40c12SJohn Marino 	crypto_bignum_to_bin(scalar2, scalar_b2, sizeof(scalar_b2),
11883ff40c12SJohn Marino 			     sae->tmp->prime_len);
11893ff40c12SJohn Marino 	addr[3] = scalar_b2;
11903ff40c12SJohn Marino 	len[3] = sae->tmp->prime_len;
11913ff40c12SJohn Marino 	addr[4] = element2;
11923ff40c12SJohn Marino 	len[4] = element2_len;
11933ff40c12SJohn Marino 	hmac_sha256_vector(sae->tmp->kck, sizeof(sae->tmp->kck), 5, addr, len,
11943ff40c12SJohn Marino 			   confirm);
11953ff40c12SJohn Marino }
11963ff40c12SJohn Marino 
11973ff40c12SJohn Marino 
sae_cn_confirm_ecc(struct sae_data * sae,const u8 * sc,const struct crypto_bignum * scalar1,const struct crypto_ec_point * element1,const struct crypto_bignum * scalar2,const struct crypto_ec_point * element2,u8 * confirm)11983ff40c12SJohn Marino static void sae_cn_confirm_ecc(struct sae_data *sae, const u8 *sc,
11993ff40c12SJohn Marino 			       const struct crypto_bignum *scalar1,
12003ff40c12SJohn Marino 			       const struct crypto_ec_point *element1,
12013ff40c12SJohn Marino 			       const struct crypto_bignum *scalar2,
12023ff40c12SJohn Marino 			       const struct crypto_ec_point *element2,
12033ff40c12SJohn Marino 			       u8 *confirm)
12043ff40c12SJohn Marino {
12053ff40c12SJohn Marino 	u8 element_b1[2 * SAE_MAX_ECC_PRIME_LEN];
12063ff40c12SJohn Marino 	u8 element_b2[2 * SAE_MAX_ECC_PRIME_LEN];
12073ff40c12SJohn Marino 
12083ff40c12SJohn Marino 	crypto_ec_point_to_bin(sae->tmp->ec, element1, element_b1,
12093ff40c12SJohn Marino 			       element_b1 + sae->tmp->prime_len);
12103ff40c12SJohn Marino 	crypto_ec_point_to_bin(sae->tmp->ec, element2, element_b2,
12113ff40c12SJohn Marino 			       element_b2 + sae->tmp->prime_len);
12123ff40c12SJohn Marino 
12133ff40c12SJohn Marino 	sae_cn_confirm(sae, sc, scalar1, element_b1, 2 * sae->tmp->prime_len,
12143ff40c12SJohn Marino 		       scalar2, element_b2, 2 * sae->tmp->prime_len, confirm);
12153ff40c12SJohn Marino }
12163ff40c12SJohn Marino 
12173ff40c12SJohn Marino 
sae_cn_confirm_ffc(struct sae_data * sae,const u8 * sc,const struct crypto_bignum * scalar1,const struct crypto_bignum * element1,const struct crypto_bignum * scalar2,const struct crypto_bignum * element2,u8 * confirm)12183ff40c12SJohn Marino static void sae_cn_confirm_ffc(struct sae_data *sae, const u8 *sc,
12193ff40c12SJohn Marino 			       const struct crypto_bignum *scalar1,
12203ff40c12SJohn Marino 			       const struct crypto_bignum *element1,
12213ff40c12SJohn Marino 			       const struct crypto_bignum *scalar2,
12223ff40c12SJohn Marino 			       const struct crypto_bignum *element2,
12233ff40c12SJohn Marino 			       u8 *confirm)
12243ff40c12SJohn Marino {
12253ff40c12SJohn Marino 	u8 element_b1[SAE_MAX_PRIME_LEN];
12263ff40c12SJohn Marino 	u8 element_b2[SAE_MAX_PRIME_LEN];
12273ff40c12SJohn Marino 
12283ff40c12SJohn Marino 	crypto_bignum_to_bin(element1, element_b1, sizeof(element_b1),
12293ff40c12SJohn Marino 			     sae->tmp->prime_len);
12303ff40c12SJohn Marino 	crypto_bignum_to_bin(element2, element_b2, sizeof(element_b2),
12313ff40c12SJohn Marino 			     sae->tmp->prime_len);
12323ff40c12SJohn Marino 
12333ff40c12SJohn Marino 	sae_cn_confirm(sae, sc, scalar1, element_b1, sae->tmp->prime_len,
12343ff40c12SJohn Marino 		       scalar2, element_b2, sae->tmp->prime_len, confirm);
12353ff40c12SJohn Marino }
12363ff40c12SJohn Marino 
12373ff40c12SJohn Marino 
sae_write_confirm(struct sae_data * sae,struct wpabuf * buf)12383ff40c12SJohn Marino void sae_write_confirm(struct sae_data *sae, struct wpabuf *buf)
12393ff40c12SJohn Marino {
12403ff40c12SJohn Marino 	const u8 *sc;
12413ff40c12SJohn Marino 
1242*a1157835SDaniel Fojt 	if (sae->tmp == NULL)
1243*a1157835SDaniel Fojt 		return;
1244*a1157835SDaniel Fojt 
12453ff40c12SJohn Marino 	/* Send-Confirm */
12463ff40c12SJohn Marino 	sc = wpabuf_put(buf, 0);
12473ff40c12SJohn Marino 	wpabuf_put_le16(buf, sae->send_confirm);
1248*a1157835SDaniel Fojt 	if (sae->send_confirm < 0xffff)
12493ff40c12SJohn Marino 		sae->send_confirm++;
12503ff40c12SJohn Marino 
12513ff40c12SJohn Marino 	if (sae->tmp->ec)
12523ff40c12SJohn Marino 		sae_cn_confirm_ecc(sae, sc, sae->tmp->own_commit_scalar,
12533ff40c12SJohn Marino 				   sae->tmp->own_commit_element_ecc,
12543ff40c12SJohn Marino 				   sae->peer_commit_scalar,
12553ff40c12SJohn Marino 				   sae->tmp->peer_commit_element_ecc,
12563ff40c12SJohn Marino 				   wpabuf_put(buf, SHA256_MAC_LEN));
12573ff40c12SJohn Marino 	else
12583ff40c12SJohn Marino 		sae_cn_confirm_ffc(sae, sc, sae->tmp->own_commit_scalar,
12593ff40c12SJohn Marino 				   sae->tmp->own_commit_element_ffc,
12603ff40c12SJohn Marino 				   sae->peer_commit_scalar,
12613ff40c12SJohn Marino 				   sae->tmp->peer_commit_element_ffc,
12623ff40c12SJohn Marino 				   wpabuf_put(buf, SHA256_MAC_LEN));
12633ff40c12SJohn Marino }
12643ff40c12SJohn Marino 
12653ff40c12SJohn Marino 
sae_check_confirm(struct sae_data * sae,const u8 * data,size_t len)12663ff40c12SJohn Marino int sae_check_confirm(struct sae_data *sae, const u8 *data, size_t len)
12673ff40c12SJohn Marino {
12683ff40c12SJohn Marino 	u8 verifier[SHA256_MAC_LEN];
12693ff40c12SJohn Marino 
12703ff40c12SJohn Marino 	if (len < 2 + SHA256_MAC_LEN) {
12713ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Too short confirm message");
12723ff40c12SJohn Marino 		return -1;
12733ff40c12SJohn Marino 	}
12743ff40c12SJohn Marino 
12753ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "SAE: peer-send-confirm %u", WPA_GET_LE16(data));
12763ff40c12SJohn Marino 
1277*a1157835SDaniel Fojt 	if (!sae->tmp || !sae->peer_commit_scalar ||
1278*a1157835SDaniel Fojt 	    !sae->tmp->own_commit_scalar) {
1279*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SAE: Temporary data not yet available");
1280*a1157835SDaniel Fojt 		return -1;
1281*a1157835SDaniel Fojt 	}
1282*a1157835SDaniel Fojt 
1283*a1157835SDaniel Fojt 	if (sae->tmp->ec) {
1284*a1157835SDaniel Fojt 		if (!sae->tmp->peer_commit_element_ecc ||
1285*a1157835SDaniel Fojt 		    !sae->tmp->own_commit_element_ecc)
1286*a1157835SDaniel Fojt 			return -1;
12873ff40c12SJohn Marino 		sae_cn_confirm_ecc(sae, data, sae->peer_commit_scalar,
12883ff40c12SJohn Marino 				   sae->tmp->peer_commit_element_ecc,
12893ff40c12SJohn Marino 				   sae->tmp->own_commit_scalar,
12903ff40c12SJohn Marino 				   sae->tmp->own_commit_element_ecc,
12913ff40c12SJohn Marino 				   verifier);
1292*a1157835SDaniel Fojt 	} else {
1293*a1157835SDaniel Fojt 		if (!sae->tmp->peer_commit_element_ffc ||
1294*a1157835SDaniel Fojt 		    !sae->tmp->own_commit_element_ffc)
1295*a1157835SDaniel Fojt 			return -1;
12963ff40c12SJohn Marino 		sae_cn_confirm_ffc(sae, data, sae->peer_commit_scalar,
12973ff40c12SJohn Marino 				   sae->tmp->peer_commit_element_ffc,
12983ff40c12SJohn Marino 				   sae->tmp->own_commit_scalar,
12993ff40c12SJohn Marino 				   sae->tmp->own_commit_element_ffc,
13003ff40c12SJohn Marino 				   verifier);
1301*a1157835SDaniel Fojt 	}
13023ff40c12SJohn Marino 
1303*a1157835SDaniel Fojt 	if (os_memcmp_const(verifier, data + 2, SHA256_MAC_LEN) != 0) {
13043ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "SAE: Confirm mismatch");
13053ff40c12SJohn Marino 		wpa_hexdump(MSG_DEBUG, "SAE: Received confirm",
13063ff40c12SJohn Marino 			    data + 2, SHA256_MAC_LEN);
13073ff40c12SJohn Marino 		wpa_hexdump(MSG_DEBUG, "SAE: Calculated verifier",
13083ff40c12SJohn Marino 			    verifier, SHA256_MAC_LEN);
13093ff40c12SJohn Marino 		return -1;
13103ff40c12SJohn Marino 	}
13113ff40c12SJohn Marino 
13123ff40c12SJohn Marino 	return 0;
13133ff40c12SJohn Marino }
1314*a1157835SDaniel Fojt 
1315*a1157835SDaniel Fojt 
sae_state_txt(enum sae_state state)1316*a1157835SDaniel Fojt const char * sae_state_txt(enum sae_state state)
1317*a1157835SDaniel Fojt {
1318*a1157835SDaniel Fojt 	switch (state) {
1319*a1157835SDaniel Fojt 	case SAE_NOTHING:
1320*a1157835SDaniel Fojt 		return "Nothing";
1321*a1157835SDaniel Fojt 	case SAE_COMMITTED:
1322*a1157835SDaniel Fojt 		return "Committed";
1323*a1157835SDaniel Fojt 	case SAE_CONFIRMED:
1324*a1157835SDaniel Fojt 		return "Confirmed";
1325*a1157835SDaniel Fojt 	case SAE_ACCEPTED:
1326*a1157835SDaniel Fojt 		return "Accepted";
1327*a1157835SDaniel Fojt 	}
1328*a1157835SDaniel Fojt 	return "?";
1329*a1157835SDaniel Fojt }
1330