xref: /freebsd/contrib/wpa/src/common/sae.c (revision ec080394)
15b9c547cSRui Paulo /*
25b9c547cSRui Paulo  * Simultaneous authentication of equals
3780fb4a2SCy Schubert  * Copyright (c) 2012-2016, Jouni Malinen <j@w1.fi>
45b9c547cSRui Paulo  *
55b9c547cSRui Paulo  * This software may be distributed under the terms of the BSD license.
65b9c547cSRui Paulo  * See README for more details.
75b9c547cSRui Paulo  */
85b9c547cSRui Paulo 
95b9c547cSRui Paulo #include "includes.h"
105b9c547cSRui Paulo 
115b9c547cSRui Paulo #include "common.h"
124bc52338SCy Schubert #include "utils/const_time.h"
135b9c547cSRui Paulo #include "crypto/crypto.h"
145b9c547cSRui Paulo #include "crypto/sha256.h"
15c1d255d3SCy Schubert #include "crypto/sha384.h"
16c1d255d3SCy Schubert #include "crypto/sha512.h"
175b9c547cSRui Paulo #include "crypto/random.h"
185b9c547cSRui Paulo #include "crypto/dh_groups.h"
195b9c547cSRui Paulo #include "ieee802_11_defs.h"
20206b73d0SCy Schubert #include "dragonfly.h"
215b9c547cSRui Paulo #include "sae.h"
225b9c547cSRui Paulo 
235b9c547cSRui Paulo 
sae_set_group(struct sae_data * sae,int group)245b9c547cSRui Paulo int sae_set_group(struct sae_data *sae, int group)
255b9c547cSRui Paulo {
265b9c547cSRui Paulo 	struct sae_temporary_data *tmp;
275b9c547cSRui Paulo 
28206b73d0SCy Schubert #ifdef CONFIG_TESTING_OPTIONS
29206b73d0SCy Schubert 	/* Allow all groups for testing purposes in non-production builds. */
30206b73d0SCy Schubert #else /* CONFIG_TESTING_OPTIONS */
31206b73d0SCy Schubert 	if (!dragonfly_suitable_group(group, 0)) {
324bc52338SCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: Reject unsuitable group %d", group);
334bc52338SCy Schubert 		return -1;
344bc52338SCy Schubert 	}
35206b73d0SCy Schubert #endif /* CONFIG_TESTING_OPTIONS */
364bc52338SCy Schubert 
375b9c547cSRui Paulo 	sae_clear_data(sae);
385b9c547cSRui Paulo 	tmp = sae->tmp = os_zalloc(sizeof(*tmp));
395b9c547cSRui Paulo 	if (tmp == NULL)
405b9c547cSRui Paulo 		return -1;
415b9c547cSRui Paulo 
425b9c547cSRui Paulo 	/* First, check if this is an ECC group */
435b9c547cSRui Paulo 	tmp->ec = crypto_ec_init(group);
445b9c547cSRui Paulo 	if (tmp->ec) {
4585732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: Selecting supported ECC group %d",
4685732ac8SCy Schubert 			   group);
475b9c547cSRui Paulo 		sae->group = group;
485b9c547cSRui Paulo 		tmp->prime_len = crypto_ec_prime_len(tmp->ec);
495b9c547cSRui Paulo 		tmp->prime = crypto_ec_get_prime(tmp->ec);
50206b73d0SCy Schubert 		tmp->order_len = crypto_ec_order_len(tmp->ec);
515b9c547cSRui Paulo 		tmp->order = crypto_ec_get_order(tmp->ec);
525b9c547cSRui Paulo 		return 0;
535b9c547cSRui Paulo 	}
545b9c547cSRui Paulo 
555b9c547cSRui Paulo 	/* Not an ECC group, check FFC */
565b9c547cSRui Paulo 	tmp->dh = dh_groups_get(group);
575b9c547cSRui Paulo 	if (tmp->dh) {
5885732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: Selecting supported FFC group %d",
5985732ac8SCy Schubert 			   group);
605b9c547cSRui Paulo 		sae->group = group;
615b9c547cSRui Paulo 		tmp->prime_len = tmp->dh->prime_len;
625b9c547cSRui Paulo 		if (tmp->prime_len > SAE_MAX_PRIME_LEN) {
635b9c547cSRui Paulo 			sae_clear_data(sae);
645b9c547cSRui Paulo 			return -1;
655b9c547cSRui Paulo 		}
665b9c547cSRui Paulo 
675b9c547cSRui Paulo 		tmp->prime_buf = crypto_bignum_init_set(tmp->dh->prime,
685b9c547cSRui Paulo 							tmp->prime_len);
695b9c547cSRui Paulo 		if (tmp->prime_buf == NULL) {
705b9c547cSRui Paulo 			sae_clear_data(sae);
715b9c547cSRui Paulo 			return -1;
725b9c547cSRui Paulo 		}
735b9c547cSRui Paulo 		tmp->prime = tmp->prime_buf;
745b9c547cSRui Paulo 
75206b73d0SCy Schubert 		tmp->order_len = tmp->dh->order_len;
765b9c547cSRui Paulo 		tmp->order_buf = crypto_bignum_init_set(tmp->dh->order,
775b9c547cSRui Paulo 							tmp->dh->order_len);
785b9c547cSRui Paulo 		if (tmp->order_buf == NULL) {
795b9c547cSRui Paulo 			sae_clear_data(sae);
805b9c547cSRui Paulo 			return -1;
815b9c547cSRui Paulo 		}
825b9c547cSRui Paulo 		tmp->order = tmp->order_buf;
835b9c547cSRui Paulo 
845b9c547cSRui Paulo 		return 0;
855b9c547cSRui Paulo 	}
865b9c547cSRui Paulo 
875b9c547cSRui Paulo 	/* Unsupported group */
8885732ac8SCy Schubert 	wpa_printf(MSG_DEBUG,
8985732ac8SCy Schubert 		   "SAE: Group %d not supported by the crypto library", group);
905b9c547cSRui Paulo 	return -1;
915b9c547cSRui Paulo }
925b9c547cSRui Paulo 
935b9c547cSRui Paulo 
sae_clear_temp_data(struct sae_data * sae)945b9c547cSRui Paulo void sae_clear_temp_data(struct sae_data *sae)
955b9c547cSRui Paulo {
965b9c547cSRui Paulo 	struct sae_temporary_data *tmp;
975b9c547cSRui Paulo 	if (sae == NULL || sae->tmp == NULL)
985b9c547cSRui Paulo 		return;
995b9c547cSRui Paulo 	tmp = sae->tmp;
1005b9c547cSRui Paulo 	crypto_ec_deinit(tmp->ec);
1015b9c547cSRui Paulo 	crypto_bignum_deinit(tmp->prime_buf, 0);
1025b9c547cSRui Paulo 	crypto_bignum_deinit(tmp->order_buf, 0);
1035b9c547cSRui Paulo 	crypto_bignum_deinit(tmp->sae_rand, 1);
1045b9c547cSRui Paulo 	crypto_bignum_deinit(tmp->pwe_ffc, 1);
1055b9c547cSRui Paulo 	crypto_bignum_deinit(tmp->own_commit_scalar, 0);
1065b9c547cSRui Paulo 	crypto_bignum_deinit(tmp->own_commit_element_ffc, 0);
1075b9c547cSRui Paulo 	crypto_bignum_deinit(tmp->peer_commit_element_ffc, 0);
1085b9c547cSRui Paulo 	crypto_ec_point_deinit(tmp->pwe_ecc, 1);
1095b9c547cSRui Paulo 	crypto_ec_point_deinit(tmp->own_commit_element_ecc, 0);
1105b9c547cSRui Paulo 	crypto_ec_point_deinit(tmp->peer_commit_element_ecc, 0);
1115b9c547cSRui Paulo 	wpabuf_free(tmp->anti_clogging_token);
112c1d255d3SCy Schubert 	wpabuf_free(tmp->own_rejected_groups);
113c1d255d3SCy Schubert 	wpabuf_free(tmp->peer_rejected_groups);
11485732ac8SCy Schubert 	os_free(tmp->pw_id);
1155b9c547cSRui Paulo 	bin_clear_free(tmp, sizeof(*tmp));
1165b9c547cSRui Paulo 	sae->tmp = NULL;
1175b9c547cSRui Paulo }
1185b9c547cSRui Paulo 
1195b9c547cSRui Paulo 
sae_clear_data(struct sae_data * sae)1205b9c547cSRui Paulo void sae_clear_data(struct sae_data *sae)
1215b9c547cSRui Paulo {
1225b9c547cSRui Paulo 	if (sae == NULL)
1235b9c547cSRui Paulo 		return;
1245b9c547cSRui Paulo 	sae_clear_temp_data(sae);
1255b9c547cSRui Paulo 	crypto_bignum_deinit(sae->peer_commit_scalar, 0);
126c1d255d3SCy Schubert 	crypto_bignum_deinit(sae->peer_commit_scalar_accepted, 0);
1275b9c547cSRui Paulo 	os_memset(sae, 0, sizeof(*sae));
1285b9c547cSRui Paulo }
1295b9c547cSRui Paulo 
1305b9c547cSRui Paulo 
sae_pwd_seed_key(const u8 * addr1,const u8 * addr2,u8 * key)1315b9c547cSRui Paulo static void sae_pwd_seed_key(const u8 *addr1, const u8 *addr2, u8 *key)
1325b9c547cSRui Paulo {
1335b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "SAE: PWE derivation - addr1=" MACSTR
1345b9c547cSRui Paulo 		   " addr2=" MACSTR, MAC2STR(addr1), MAC2STR(addr2));
1355b9c547cSRui Paulo 	if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) {
1365b9c547cSRui Paulo 		os_memcpy(key, addr1, ETH_ALEN);
1375b9c547cSRui Paulo 		os_memcpy(key + ETH_ALEN, addr2, ETH_ALEN);
1385b9c547cSRui Paulo 	} else {
1395b9c547cSRui Paulo 		os_memcpy(key, addr2, ETH_ALEN);
1405b9c547cSRui Paulo 		os_memcpy(key + ETH_ALEN, addr1, ETH_ALEN);
1415b9c547cSRui Paulo 	}
1425b9c547cSRui Paulo }
1435b9c547cSRui Paulo 
1445b9c547cSRui Paulo 
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)145325151a3SRui Paulo static int sae_test_pwd_seed_ecc(struct sae_data *sae, const u8 *pwd_seed,
1464bc52338SCy Schubert 				 const u8 *prime, const u8 *qr, const u8 *qnr,
1474bc52338SCy Schubert 				 u8 *pwd_value)
148325151a3SRui Paulo {
149325151a3SRui Paulo 	struct crypto_bignum *y_sqr, *x_cand;
150325151a3SRui Paulo 	int res;
1515b9c547cSRui Paulo 	size_t bits;
152206b73d0SCy Schubert 	int cmp_prime;
153206b73d0SCy Schubert 	unsigned int in_range;
1545b9c547cSRui Paulo 
1555b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
1565b9c547cSRui Paulo 
1575b9c547cSRui Paulo 	/* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
1585b9c547cSRui Paulo 	bits = crypto_ec_prime_len_bits(sae->tmp->ec);
159780fb4a2SCy Schubert 	if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking",
160780fb4a2SCy Schubert 			    prime, sae->tmp->prime_len, pwd_value, bits) < 0)
161780fb4a2SCy Schubert 		return -1;
1625b9c547cSRui Paulo 	if (bits % 8)
1634bc52338SCy Schubert 		buf_shift_right(pwd_value, sae->tmp->prime_len, 8 - bits % 8);
1645b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value",
1655b9c547cSRui Paulo 			pwd_value, sae->tmp->prime_len);
1665b9c547cSRui Paulo 
167206b73d0SCy Schubert 	cmp_prime = const_time_memcmp(pwd_value, prime, sae->tmp->prime_len);
168206b73d0SCy Schubert 	/* Create a const_time mask for selection based on prf result
169206b73d0SCy Schubert 	 * being smaller than prime. */
170206b73d0SCy Schubert 	in_range = const_time_fill_msb((unsigned int) cmp_prime);
171206b73d0SCy Schubert 	/* The algorithm description would skip the next steps if
172c1d255d3SCy Schubert 	 * cmp_prime >= 0 (return 0 here), but go through them regardless to
173206b73d0SCy Schubert 	 * minimize externally observable differences in behavior. */
1745b9c547cSRui Paulo 
175325151a3SRui Paulo 	x_cand = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
176325151a3SRui Paulo 	if (!x_cand)
1775b9c547cSRui Paulo 		return -1;
178325151a3SRui Paulo 	y_sqr = crypto_ec_point_compute_y_sqr(sae->tmp->ec, x_cand);
179325151a3SRui Paulo 	crypto_bignum_deinit(x_cand, 1);
1804bc52338SCy Schubert 	if (!y_sqr)
181325151a3SRui Paulo 		return -1;
1825b9c547cSRui Paulo 
183206b73d0SCy Schubert 	res = dragonfly_is_quadratic_residue_blind(sae->tmp->ec, qr, qnr,
184206b73d0SCy Schubert 						   y_sqr);
185325151a3SRui Paulo 	crypto_bignum_deinit(y_sqr, 1);
186206b73d0SCy Schubert 	if (res < 0)
187325151a3SRui Paulo 		return res;
188206b73d0SCy Schubert 	return const_time_select_int(in_range, res, 0);
189325151a3SRui Paulo }
1905b9c547cSRui Paulo 
1915b9c547cSRui Paulo 
1924bc52338SCy Schubert /* Returns -1 on fatal failure, 0 if PWE cannot be derived from the provided
1934bc52338SCy Schubert  * 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)1945b9c547cSRui Paulo static int sae_test_pwd_seed_ffc(struct sae_data *sae, const u8 *pwd_seed,
1955b9c547cSRui Paulo 				 struct crypto_bignum *pwe)
1965b9c547cSRui Paulo {
1975b9c547cSRui Paulo 	u8 pwd_value[SAE_MAX_PRIME_LEN];
1985b9c547cSRui Paulo 	size_t bits = sae->tmp->prime_len * 8;
1995b9c547cSRui Paulo 	u8 exp[1];
2004bc52338SCy Schubert 	struct crypto_bignum *a, *b = NULL;
2014bc52338SCy Schubert 	int res, is_val;
2024bc52338SCy Schubert 	u8 pwd_value_valid;
2035b9c547cSRui Paulo 
2045b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
2055b9c547cSRui Paulo 
2065b9c547cSRui Paulo 	/* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
207780fb4a2SCy Schubert 	if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking",
2085b9c547cSRui Paulo 			    sae->tmp->dh->prime, sae->tmp->prime_len, pwd_value,
209780fb4a2SCy Schubert 			    bits) < 0)
210780fb4a2SCy Schubert 		return -1;
2115b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value", pwd_value,
2125b9c547cSRui Paulo 			sae->tmp->prime_len);
2135b9c547cSRui Paulo 
2144bc52338SCy Schubert 	/* Check whether pwd-value < p */
2154bc52338SCy Schubert 	res = const_time_memcmp(pwd_value, sae->tmp->dh->prime,
2164bc52338SCy Schubert 				sae->tmp->prime_len);
2174bc52338SCy Schubert 	/* pwd-value >= p is invalid, so res is < 0 for the valid cases and
2184bc52338SCy Schubert 	 * the negative sign can be used to fill the mask for constant time
2194bc52338SCy Schubert 	 * selection */
2204bc52338SCy Schubert 	pwd_value_valid = const_time_fill_msb(res);
2214bc52338SCy Schubert 
2224bc52338SCy Schubert 	/* If pwd-value >= p, force pwd-value to be < p and perform the
2234bc52338SCy Schubert 	 * calculations anyway to hide timing difference. The derived PWE will
2244bc52338SCy Schubert 	 * be ignored in that case. */
2254bc52338SCy Schubert 	pwd_value[0] = const_time_select_u8(pwd_value_valid, pwd_value[0], 0);
2265b9c547cSRui Paulo 
2275b9c547cSRui Paulo 	/* PWE = pwd-value^((p-1)/r) modulo p */
2285b9c547cSRui Paulo 
2294bc52338SCy Schubert 	res = -1;
2305b9c547cSRui Paulo 	a = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
2314bc52338SCy Schubert 	if (!a)
2324bc52338SCy Schubert 		goto fail;
2335b9c547cSRui Paulo 
2344bc52338SCy Schubert 	/* This is an optimization based on the used group that does not depend
2354bc52338SCy Schubert 	 * on the password in any way, so it is fine to use separate branches
2364bc52338SCy Schubert 	 * for this step without constant time operations. */
2375b9c547cSRui Paulo 	if (sae->tmp->dh->safe_prime) {
2385b9c547cSRui Paulo 		/*
2395b9c547cSRui Paulo 		 * r = (p-1)/2 for the group used here, so this becomes:
2405b9c547cSRui Paulo 		 * PWE = pwd-value^2 modulo p
2415b9c547cSRui Paulo 		 */
2425b9c547cSRui Paulo 		exp[0] = 2;
2435b9c547cSRui Paulo 		b = crypto_bignum_init_set(exp, sizeof(exp));
2445b9c547cSRui Paulo 	} else {
2455b9c547cSRui Paulo 		/* Calculate exponent: (p-1)/r */
2465b9c547cSRui Paulo 		exp[0] = 1;
2475b9c547cSRui Paulo 		b = crypto_bignum_init_set(exp, sizeof(exp));
2485b9c547cSRui Paulo 		if (b == NULL ||
2495b9c547cSRui Paulo 		    crypto_bignum_sub(sae->tmp->prime, b, b) < 0 ||
2504bc52338SCy Schubert 		    crypto_bignum_div(b, sae->tmp->order, b) < 0)
2514bc52338SCy Schubert 			goto fail;
2525b9c547cSRui Paulo 	}
2535b9c547cSRui Paulo 
2544bc52338SCy Schubert 	if (!b)
2554bc52338SCy Schubert 		goto fail;
2564bc52338SCy Schubert 
2575b9c547cSRui Paulo 	res = crypto_bignum_exptmod(a, b, sae->tmp->prime, pwe);
2584bc52338SCy Schubert 	if (res < 0)
2594bc52338SCy Schubert 		goto fail;
2605b9c547cSRui Paulo 
2614bc52338SCy Schubert 	/* There were no fatal errors in calculations, so determine the return
2624bc52338SCy Schubert 	 * value using constant time operations. We get here for number of
2634bc52338SCy Schubert 	 * invalid cases which are cleared here after having performed all the
2644bc52338SCy Schubert 	 * computation. PWE is valid if pwd-value was less than prime and
2654bc52338SCy Schubert 	 * PWE > 1. Start with pwd-value check first and then use constant time
2664bc52338SCy Schubert 	 * operations to clear res to 0 if PWE is 0 or 1.
2674bc52338SCy Schubert 	 */
2684bc52338SCy Schubert 	res = const_time_select_u8(pwd_value_valid, 1, 0);
2694bc52338SCy Schubert 	is_val = crypto_bignum_is_zero(pwe);
2704bc52338SCy Schubert 	res = const_time_select_u8(const_time_is_zero(is_val), res, 0);
2714bc52338SCy Schubert 	is_val = crypto_bignum_is_one(pwe);
2724bc52338SCy Schubert 	res = const_time_select_u8(const_time_is_zero(is_val), res, 0);
2735b9c547cSRui Paulo 
2744bc52338SCy Schubert fail:
2754bc52338SCy Schubert 	crypto_bignum_deinit(a, 1);
2764bc52338SCy Schubert 	crypto_bignum_deinit(b, 1);
2774bc52338SCy Schubert 	return res;
2785b9c547cSRui Paulo }
2795b9c547cSRui Paulo 
2805b9c547cSRui Paulo 
sae_derive_pwe_ecc(struct sae_data * sae,const u8 * addr1,const u8 * addr2,const u8 * password,size_t password_len)2815b9c547cSRui Paulo static int sae_derive_pwe_ecc(struct sae_data *sae, const u8 *addr1,
2825b9c547cSRui Paulo 			      const u8 *addr2, const u8 *password,
283c1d255d3SCy Schubert 			      size_t password_len)
2845b9c547cSRui Paulo {
285206b73d0SCy Schubert 	u8 counter, k;
2865b9c547cSRui Paulo 	u8 addrs[2 * ETH_ALEN];
287c1d255d3SCy Schubert 	const u8 *addr[2];
288c1d255d3SCy Schubert 	size_t len[2];
2894b72b91aSCy Schubert 	u8 *stub_password, *tmp_password;
290325151a3SRui Paulo 	int pwd_seed_odd = 0;
291325151a3SRui Paulo 	u8 prime[SAE_MAX_ECC_PRIME_LEN];
292325151a3SRui Paulo 	size_t prime_len;
293ec080394SCy Schubert 	struct crypto_bignum *x = NULL, *y = NULL, *qr = NULL, *qnr = NULL;
2944bc52338SCy Schubert 	u8 x_bin[SAE_MAX_ECC_PRIME_LEN];
2954bc52338SCy Schubert 	u8 x_cand_bin[SAE_MAX_ECC_PRIME_LEN];
2964bc52338SCy Schubert 	u8 qr_bin[SAE_MAX_ECC_PRIME_LEN];
2974bc52338SCy Schubert 	u8 qnr_bin[SAE_MAX_ECC_PRIME_LEN];
298ec080394SCy Schubert 	u8 x_y[2 * SAE_MAX_ECC_PRIME_LEN];
2994bc52338SCy Schubert 	int res = -1;
3004bc52338SCy Schubert 	u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
3014bc52338SCy Schubert 		       * mask */
302ec080394SCy Schubert 	unsigned int is_eq;
3035b9c547cSRui Paulo 
3044bc52338SCy Schubert 	os_memset(x_bin, 0, sizeof(x_bin));
3054bc52338SCy Schubert 
3064b72b91aSCy Schubert 	stub_password = os_malloc(password_len);
3074bc52338SCy Schubert 	tmp_password = os_malloc(password_len);
3084b72b91aSCy Schubert 	if (!stub_password || !tmp_password ||
3094b72b91aSCy Schubert 	    random_get_bytes(stub_password, password_len) < 0)
3104bc52338SCy Schubert 		goto fail;
311325151a3SRui Paulo 
312325151a3SRui Paulo 	prime_len = sae->tmp->prime_len;
313325151a3SRui Paulo 	if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
314325151a3SRui Paulo 				 prime_len) < 0)
3154bc52338SCy Schubert 		goto fail;
316325151a3SRui Paulo 
317325151a3SRui Paulo 	/*
318325151a3SRui Paulo 	 * Create a random quadratic residue (qr) and quadratic non-residue
319325151a3SRui Paulo 	 * (qnr) modulo p for blinding purposes during the loop.
320325151a3SRui Paulo 	 */
321206b73d0SCy Schubert 	if (dragonfly_get_random_qr_qnr(sae->tmp->prime, &qr, &qnr) < 0 ||
3224bc52338SCy Schubert 	    crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin), prime_len) < 0 ||
3234bc52338SCy Schubert 	    crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin), prime_len) < 0)
3244bc52338SCy Schubert 		goto fail;
3255b9c547cSRui Paulo 
3265b9c547cSRui Paulo 	wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
3275b9c547cSRui Paulo 			      password, password_len);
3285b9c547cSRui Paulo 
3295b9c547cSRui Paulo 	/*
3305b9c547cSRui Paulo 	 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
331c1d255d3SCy Schubert 	 * base = password
3325b9c547cSRui Paulo 	 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
333325151a3SRui Paulo 	 *              base || counter)
3345b9c547cSRui Paulo 	 */
3355b9c547cSRui Paulo 	sae_pwd_seed_key(addr1, addr2, addrs);
3365b9c547cSRui Paulo 
3374bc52338SCy Schubert 	addr[0] = tmp_password;
3385b9c547cSRui Paulo 	len[0] = password_len;
339c1d255d3SCy Schubert 	addr[1] = &counter;
340c1d255d3SCy Schubert 	len[1] = sizeof(counter);
3415b9c547cSRui Paulo 
3425b9c547cSRui Paulo 	/*
3435b9c547cSRui Paulo 	 * Continue for at least k iterations to protect against side-channel
3445b9c547cSRui Paulo 	 * attacks that attempt to determine the number of iterations required
3455b9c547cSRui Paulo 	 * in the loop.
3465b9c547cSRui Paulo 	 */
347206b73d0SCy Schubert 	k = dragonfly_min_pwe_loop_iter(sae->group);
348206b73d0SCy Schubert 
3494bc52338SCy Schubert 	for (counter = 1; counter <= k || !found; counter++) {
3505b9c547cSRui Paulo 		u8 pwd_seed[SHA256_MAC_LEN];
3515b9c547cSRui Paulo 
3525b9c547cSRui Paulo 		if (counter > 200) {
3535b9c547cSRui Paulo 			/* This should not happen in practice */
3545b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
3555b9c547cSRui Paulo 			break;
3565b9c547cSRui Paulo 		}
3575b9c547cSRui Paulo 
3584bc52338SCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: counter = %03u", counter);
3594b72b91aSCy Schubert 		const_time_select_bin(found, stub_password, password,
3604bc52338SCy Schubert 				      password_len, tmp_password);
361c1d255d3SCy Schubert 		if (hmac_sha256_vector(addrs, sizeof(addrs), 2,
36285732ac8SCy Schubert 				       addr, len, pwd_seed) < 0)
3635b9c547cSRui Paulo 			break;
364325151a3SRui Paulo 
3655b9c547cSRui Paulo 		res = sae_test_pwd_seed_ecc(sae, pwd_seed,
3664bc52338SCy Schubert 					    prime, qr_bin, qnr_bin, x_cand_bin);
3674bc52338SCy Schubert 		const_time_select_bin(found, x_bin, x_cand_bin, prime_len,
3684bc52338SCy Schubert 				      x_bin);
3694bc52338SCy Schubert 		pwd_seed_odd = const_time_select_u8(
3704bc52338SCy Schubert 			found, pwd_seed_odd,
3714bc52338SCy Schubert 			pwd_seed[SHA256_MAC_LEN - 1] & 0x01);
3724bc52338SCy Schubert 		os_memset(pwd_seed, 0, sizeof(pwd_seed));
3735b9c547cSRui Paulo 		if (res < 0)
374325151a3SRui Paulo 			goto fail;
3754bc52338SCy Schubert 		/* Need to minimize differences in handling res == 0 and 1 here
3764bc52338SCy Schubert 		 * to avoid differences in timing and instruction cache access,
3774bc52338SCy Schubert 		 * so use const_time_select_*() to make local copies of the
3784bc52338SCy Schubert 		 * values based on whether this loop iteration was the one that
3794bc52338SCy Schubert 		 * found the pwd-seed/x. */
380325151a3SRui Paulo 
3814bc52338SCy Schubert 		/* found is 0 or 0xff here and res is 0 or 1. Bitwise OR of them
3824bc52338SCy Schubert 		 * (with res converted to 0/0xff) handles this in constant time.
383325151a3SRui Paulo 		 */
3844bc52338SCy Schubert 		found |= res * 0xff;
3854bc52338SCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: pwd-seed result %d found=0x%02x",
3864bc52338SCy Schubert 			   res, found);
3875b9c547cSRui Paulo 	}
3885b9c547cSRui Paulo 
3894bc52338SCy Schubert 	if (!found) {
390325151a3SRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Could not generate PWE");
391325151a3SRui Paulo 		res = -1;
392325151a3SRui Paulo 		goto fail;
393325151a3SRui Paulo 	}
3945b9c547cSRui Paulo 
3954bc52338SCy Schubert 	x = crypto_bignum_init_set(x_bin, prime_len);
3964bc52338SCy Schubert 	if (!x) {
3974bc52338SCy Schubert 		res = -1;
3984bc52338SCy Schubert 		goto fail;
3994bc52338SCy Schubert 	}
4004bc52338SCy Schubert 
401ec080394SCy Schubert 	/* y = sqrt(x^3 + ax + b) mod p
402ec080394SCy Schubert 	 * if LSB(save) == LSB(y): PWE = (x, y)
403ec080394SCy Schubert 	 * else: PWE = (x, p - y)
404ec080394SCy Schubert 	 *
405ec080394SCy Schubert 	 * Calculate y and the two possible values for PWE and after that,
406ec080394SCy Schubert 	 * use constant time selection to copy the correct alternative.
40764e33c5cSCy Schubert 	 */
408ec080394SCy Schubert 	y = crypto_ec_point_compute_y_sqr(sae->tmp->ec, x);
409ec080394SCy Schubert 	if (!y ||
410ec080394SCy Schubert 	    dragonfly_sqrt(sae->tmp->ec, y, y) < 0 ||
411ec080394SCy Schubert 	    crypto_bignum_to_bin(y, x_y, SAE_MAX_ECC_PRIME_LEN,
412ec080394SCy Schubert 				 prime_len) < 0 ||
413ec080394SCy Schubert 	    crypto_bignum_sub(sae->tmp->prime, y, y) < 0 ||
414ec080394SCy Schubert 	    crypto_bignum_to_bin(y, x_y + SAE_MAX_ECC_PRIME_LEN,
415ec080394SCy Schubert 				 SAE_MAX_ECC_PRIME_LEN, prime_len) < 0) {
41664e33c5cSCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: Could not solve y");
417ec080394SCy Schubert 		goto fail;
418ec080394SCy Schubert 	}
419ec080394SCy Schubert 
420ec080394SCy Schubert 	is_eq = const_time_eq(pwd_seed_odd, x_y[prime_len - 1] & 0x01);
421ec080394SCy Schubert 	const_time_select_bin(is_eq, x_y, x_y + SAE_MAX_ECC_PRIME_LEN,
422ec080394SCy Schubert 			      prime_len, x_y + prime_len);
423ec080394SCy Schubert 	os_memcpy(x_y, x_bin, prime_len);
424ec080394SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: PWE", x_y, 2 * prime_len);
425ec080394SCy Schubert 	crypto_ec_point_deinit(sae->tmp->pwe_ecc, 1);
426ec080394SCy Schubert 	sae->tmp->pwe_ecc = crypto_ec_point_from_bin(sae->tmp->ec, x_y);
427ec080394SCy Schubert 	if (!sae->tmp->pwe_ecc) {
428ec080394SCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: Could not generate PWE");
429ec080394SCy Schubert 		res = -1;
430325151a3SRui Paulo 	}
431325151a3SRui Paulo 
432325151a3SRui Paulo fail:
433ec080394SCy Schubert 	forced_memzero(x_y, sizeof(x_y));
434325151a3SRui Paulo 	crypto_bignum_deinit(qr, 0);
435325151a3SRui Paulo 	crypto_bignum_deinit(qnr, 0);
436ec080394SCy Schubert 	crypto_bignum_deinit(y, 1);
4374b72b91aSCy Schubert 	os_free(stub_password);
4384bc52338SCy Schubert 	bin_clear_free(tmp_password, password_len);
4394bc52338SCy Schubert 	crypto_bignum_deinit(x, 1);
4404bc52338SCy Schubert 	os_memset(x_bin, 0, sizeof(x_bin));
4414bc52338SCy Schubert 	os_memset(x_cand_bin, 0, sizeof(x_cand_bin));
442325151a3SRui Paulo 
443325151a3SRui Paulo 	return res;
4445b9c547cSRui Paulo }
4455b9c547cSRui Paulo 
4465b9c547cSRui Paulo 
sae_derive_pwe_ffc(struct sae_data * sae,const u8 * addr1,const u8 * addr2,const u8 * password,size_t password_len)4475b9c547cSRui Paulo static int sae_derive_pwe_ffc(struct sae_data *sae, const u8 *addr1,
4485b9c547cSRui Paulo 			      const u8 *addr2, const u8 *password,
449c1d255d3SCy Schubert 			      size_t password_len)
4505b9c547cSRui Paulo {
4514bc52338SCy Schubert 	u8 counter, k, sel_counter = 0;
4525b9c547cSRui Paulo 	u8 addrs[2 * ETH_ALEN];
453c1d255d3SCy Schubert 	const u8 *addr[2];
454c1d255d3SCy Schubert 	size_t len[2];
4554bc52338SCy Schubert 	u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
4564bc52338SCy Schubert 		       * mask */
4574bc52338SCy Schubert 	u8 mask;
4584bc52338SCy Schubert 	struct crypto_bignum *pwe;
4594bc52338SCy Schubert 	size_t prime_len = sae->tmp->prime_len * 8;
4604bc52338SCy Schubert 	u8 *pwe_buf;
4615b9c547cSRui Paulo 
4624bc52338SCy Schubert 	crypto_bignum_deinit(sae->tmp->pwe_ffc, 1);
4634bc52338SCy Schubert 	sae->tmp->pwe_ffc = NULL;
4644bc52338SCy Schubert 
4654bc52338SCy Schubert 	/* Allocate a buffer to maintain selected and candidate PWE for constant
4664bc52338SCy Schubert 	 * time selection. */
4674bc52338SCy Schubert 	pwe_buf = os_zalloc(prime_len * 2);
4684bc52338SCy Schubert 	pwe = crypto_bignum_init();
4694bc52338SCy Schubert 	if (!pwe_buf || !pwe)
4704bc52338SCy Schubert 		goto fail;
4715b9c547cSRui Paulo 
4725b9c547cSRui Paulo 	wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
4735b9c547cSRui Paulo 			      password, password_len);
4745b9c547cSRui Paulo 
4755b9c547cSRui Paulo 	/*
4765b9c547cSRui Paulo 	 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
4775b9c547cSRui Paulo 	 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
478c1d255d3SCy Schubert 	 *              password || counter)
4795b9c547cSRui Paulo 	 */
4805b9c547cSRui Paulo 	sae_pwd_seed_key(addr1, addr2, addrs);
4815b9c547cSRui Paulo 
4825b9c547cSRui Paulo 	addr[0] = password;
4835b9c547cSRui Paulo 	len[0] = password_len;
484c1d255d3SCy Schubert 	addr[1] = &counter;
485c1d255d3SCy Schubert 	len[1] = sizeof(counter);
4865b9c547cSRui Paulo 
487206b73d0SCy Schubert 	k = dragonfly_min_pwe_loop_iter(sae->group);
4884bc52338SCy Schubert 
4894bc52338SCy Schubert 	for (counter = 1; counter <= k || !found; counter++) {
4905b9c547cSRui Paulo 		u8 pwd_seed[SHA256_MAC_LEN];
4915b9c547cSRui Paulo 		int res;
4925b9c547cSRui Paulo 
4935b9c547cSRui Paulo 		if (counter > 200) {
4945b9c547cSRui Paulo 			/* This should not happen in practice */
4955b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
4965b9c547cSRui Paulo 			break;
4975b9c547cSRui Paulo 		}
4985b9c547cSRui Paulo 
4994bc52338SCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: counter = %02u", counter);
500c1d255d3SCy Schubert 		if (hmac_sha256_vector(addrs, sizeof(addrs), 2,
50185732ac8SCy Schubert 				       addr, len, pwd_seed) < 0)
5025b9c547cSRui Paulo 			break;
5034bc52338SCy Schubert 		res = sae_test_pwd_seed_ffc(sae, pwd_seed, pwe);
5044bc52338SCy Schubert 		/* res is -1 for fatal failure, 0 if a valid PWE was not found,
5054bc52338SCy Schubert 		 * or 1 if a valid PWE was found. */
5065b9c547cSRui Paulo 		if (res < 0)
5075b9c547cSRui Paulo 			break;
5084bc52338SCy Schubert 		/* Store the candidate PWE into the second half of pwe_buf and
5094bc52338SCy Schubert 		 * the selected PWE in the beginning of pwe_buf using constant
5104bc52338SCy Schubert 		 * time selection. */
5114bc52338SCy Schubert 		if (crypto_bignum_to_bin(pwe, pwe_buf + prime_len, prime_len,
5124bc52338SCy Schubert 					 prime_len) < 0)
5134bc52338SCy Schubert 			break;
5144bc52338SCy Schubert 		const_time_select_bin(found, pwe_buf, pwe_buf + prime_len,
5154bc52338SCy Schubert 				      prime_len, pwe_buf);
5164bc52338SCy Schubert 		sel_counter = const_time_select_u8(found, sel_counter, counter);
5174bc52338SCy Schubert 		mask = const_time_eq_u8(res, 1);
5184bc52338SCy Schubert 		found = const_time_select_u8(found, found, mask);
5195b9c547cSRui Paulo 	}
5205b9c547cSRui Paulo 
5214bc52338SCy Schubert 	if (!found)
5224bc52338SCy Schubert 		goto fail;
5234bc52338SCy Schubert 
5244bc52338SCy Schubert 	wpa_printf(MSG_DEBUG, "SAE: Use PWE from counter = %02u", sel_counter);
5254bc52338SCy Schubert 	sae->tmp->pwe_ffc = crypto_bignum_init_set(pwe_buf, prime_len);
5264bc52338SCy Schubert fail:
5274bc52338SCy Schubert 	crypto_bignum_deinit(pwe, 1);
5284bc52338SCy Schubert 	bin_clear_free(pwe_buf, prime_len * 2);
5294bc52338SCy Schubert 	return sae->tmp->pwe_ffc ? 0 : -1;
5305b9c547cSRui Paulo }
5315b9c547cSRui Paulo 
5325b9c547cSRui Paulo 
hkdf_extract(size_t hash_len,const u8 * salt,size_t salt_len,size_t num_elem,const u8 * addr[],const size_t len[],u8 * prk)533c1d255d3SCy Schubert static int hkdf_extract(size_t hash_len, const u8 *salt, size_t salt_len,
534c1d255d3SCy Schubert 			size_t num_elem, const u8 *addr[], const size_t len[],
535c1d255d3SCy Schubert 			u8 *prk)
536c1d255d3SCy Schubert {
537c1d255d3SCy Schubert 	if (hash_len == 32)
538c1d255d3SCy Schubert 		return hmac_sha256_vector(salt, salt_len, num_elem, addr, len,
539c1d255d3SCy Schubert 					  prk);
540c1d255d3SCy Schubert #ifdef CONFIG_SHA384
541c1d255d3SCy Schubert 	if (hash_len == 48)
542c1d255d3SCy Schubert 		return hmac_sha384_vector(salt, salt_len, num_elem, addr, len,
543c1d255d3SCy Schubert 					  prk);
544c1d255d3SCy Schubert #endif /* CONFIG_SHA384 */
545c1d255d3SCy Schubert #ifdef CONFIG_SHA512
546c1d255d3SCy Schubert 	if (hash_len == 64)
547c1d255d3SCy Schubert 		return hmac_sha512_vector(salt, salt_len, num_elem, addr, len,
548c1d255d3SCy Schubert 					  prk);
549c1d255d3SCy Schubert #endif /* CONFIG_SHA512 */
550c1d255d3SCy Schubert 	return -1;
551c1d255d3SCy Schubert }
552c1d255d3SCy Schubert 
553c1d255d3SCy Schubert 
hkdf_expand(size_t hash_len,const u8 * prk,size_t prk_len,const char * info,u8 * okm,size_t okm_len)554c1d255d3SCy Schubert static int hkdf_expand(size_t hash_len, const u8 *prk, size_t prk_len,
555c1d255d3SCy Schubert 		       const char *info, u8 *okm, size_t okm_len)
556c1d255d3SCy Schubert {
557c1d255d3SCy Schubert 	size_t info_len = os_strlen(info);
558c1d255d3SCy Schubert 
559c1d255d3SCy Schubert 	if (hash_len == 32)
560c1d255d3SCy Schubert 		return hmac_sha256_kdf(prk, prk_len, NULL,
561c1d255d3SCy Schubert 				       (const u8 *) info, info_len,
562c1d255d3SCy Schubert 				       okm, okm_len);
563c1d255d3SCy Schubert #ifdef CONFIG_SHA384
564c1d255d3SCy Schubert 	if (hash_len == 48)
565c1d255d3SCy Schubert 		return hmac_sha384_kdf(prk, prk_len, NULL,
566c1d255d3SCy Schubert 				       (const u8 *) info, info_len,
567c1d255d3SCy Schubert 				       okm, okm_len);
568c1d255d3SCy Schubert #endif /* CONFIG_SHA384 */
569c1d255d3SCy Schubert #ifdef CONFIG_SHA512
570c1d255d3SCy Schubert 	if (hash_len == 64)
571c1d255d3SCy Schubert 		return hmac_sha512_kdf(prk, prk_len, NULL,
572c1d255d3SCy Schubert 				       (const u8 *) info, info_len,
573c1d255d3SCy Schubert 				       okm, okm_len);
574c1d255d3SCy Schubert #endif /* CONFIG_SHA512 */
575c1d255d3SCy Schubert 	return -1;
576c1d255d3SCy Schubert }
577c1d255d3SCy Schubert 
578c1d255d3SCy Schubert 
sswu_curve_param(int group,int * z)579c1d255d3SCy Schubert static int sswu_curve_param(int group, int *z)
580c1d255d3SCy Schubert {
581c1d255d3SCy Schubert 	switch (group) {
582c1d255d3SCy Schubert 	case 19:
583c1d255d3SCy Schubert 		*z = -10;
584c1d255d3SCy Schubert 		return 0;
585c1d255d3SCy Schubert 	case 20:
586c1d255d3SCy Schubert 		*z = -12;
587c1d255d3SCy Schubert 		return 0;
588c1d255d3SCy Schubert 	case 21:
589c1d255d3SCy Schubert 		*z = -4;
590c1d255d3SCy Schubert 		return 0;
591c1d255d3SCy Schubert 	case 25:
592c1d255d3SCy Schubert 	case 29:
593c1d255d3SCy Schubert 		*z = -5;
594c1d255d3SCy Schubert 		return 0;
595c1d255d3SCy Schubert 	case 26:
596c1d255d3SCy Schubert 		*z = 31;
597c1d255d3SCy Schubert 		return 0;
598c1d255d3SCy Schubert 	case 28:
599c1d255d3SCy Schubert 		*z = -2;
600c1d255d3SCy Schubert 		return 0;
601c1d255d3SCy Schubert 	case 30:
602c1d255d3SCy Schubert 		*z = 7;
603c1d255d3SCy Schubert 		return 0;
604c1d255d3SCy Schubert 	}
605c1d255d3SCy Schubert 
606c1d255d3SCy Schubert 	return -1;
607c1d255d3SCy Schubert }
608c1d255d3SCy Schubert 
609c1d255d3SCy Schubert 
debug_print_bignum(const char * title,const struct crypto_bignum * a,size_t prime_len)610c1d255d3SCy Schubert static void debug_print_bignum(const char *title, const struct crypto_bignum *a,
611c1d255d3SCy Schubert 			       size_t prime_len)
612c1d255d3SCy Schubert {
613c1d255d3SCy Schubert 	u8 *bin;
614c1d255d3SCy Schubert 
615c1d255d3SCy Schubert 	bin = os_malloc(prime_len);
616c1d255d3SCy Schubert 	if (bin && crypto_bignum_to_bin(a, bin, prime_len, prime_len) >= 0)
617c1d255d3SCy Schubert 		wpa_hexdump_key(MSG_DEBUG, title, bin, prime_len);
618c1d255d3SCy Schubert 	else
619c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG, "Could not print bignum (%s)", title);
620c1d255d3SCy Schubert 	bin_clear_free(bin, prime_len);
621c1d255d3SCy Schubert }
622c1d255d3SCy Schubert 
623c1d255d3SCy Schubert 
sswu(struct crypto_ec * ec,int group,const struct crypto_bignum * u)624c1d255d3SCy Schubert static struct crypto_ec_point * sswu(struct crypto_ec *ec, int group,
625c1d255d3SCy Schubert 				     const struct crypto_bignum *u)
626c1d255d3SCy Schubert {
627c1d255d3SCy Schubert 	int z_int;
628c1d255d3SCy Schubert 	const struct crypto_bignum *a, *b, *prime;
629c1d255d3SCy Schubert 	struct crypto_bignum *u2, *t1, *t2, *z, *t, *zero, *one, *two, *three,
630c1d255d3SCy Schubert 		*x1a, *x1b, *y = NULL;
631c1d255d3SCy Schubert 	struct crypto_bignum *x1 = NULL, *x2, *gx1, *gx2, *v = NULL;
632c1d255d3SCy Schubert 	unsigned int m_is_zero, is_qr, is_eq;
633c1d255d3SCy Schubert 	size_t prime_len;
634c1d255d3SCy Schubert 	u8 bin[SAE_MAX_ECC_PRIME_LEN];
635c1d255d3SCy Schubert 	u8 bin1[SAE_MAX_ECC_PRIME_LEN];
636c1d255d3SCy Schubert 	u8 bin2[SAE_MAX_ECC_PRIME_LEN];
637c1d255d3SCy Schubert 	u8 x_y[2 * SAE_MAX_ECC_PRIME_LEN];
638c1d255d3SCy Schubert 	struct crypto_ec_point *p = NULL;
639c1d255d3SCy Schubert 
640c1d255d3SCy Schubert 	if (sswu_curve_param(group, &z_int) < 0)
641c1d255d3SCy Schubert 		return NULL;
642c1d255d3SCy Schubert 
643c1d255d3SCy Schubert 	prime = crypto_ec_get_prime(ec);
644c1d255d3SCy Schubert 	prime_len = crypto_ec_prime_len(ec);
645c1d255d3SCy Schubert 	a = crypto_ec_get_a(ec);
646c1d255d3SCy Schubert 	b = crypto_ec_get_b(ec);
647c1d255d3SCy Schubert 
648c1d255d3SCy Schubert 	u2 = crypto_bignum_init();
649c1d255d3SCy Schubert 	t1 = crypto_bignum_init();
650c1d255d3SCy Schubert 	t2 = crypto_bignum_init();
651c1d255d3SCy Schubert 	z = crypto_bignum_init_uint(abs(z_int));
652c1d255d3SCy Schubert 	t = crypto_bignum_init();
653c1d255d3SCy Schubert 	zero = crypto_bignum_init_uint(0);
654c1d255d3SCy Schubert 	one = crypto_bignum_init_uint(1);
655c1d255d3SCy Schubert 	two = crypto_bignum_init_uint(2);
656c1d255d3SCy Schubert 	three = crypto_bignum_init_uint(3);
657c1d255d3SCy Schubert 	x1a = crypto_bignum_init();
658c1d255d3SCy Schubert 	x1b = crypto_bignum_init();
659c1d255d3SCy Schubert 	x2 = crypto_bignum_init();
660c1d255d3SCy Schubert 	gx1 = crypto_bignum_init();
661c1d255d3SCy Schubert 	gx2 = crypto_bignum_init();
662c1d255d3SCy Schubert 	if (!u2 || !t1 || !t2 || !z || !t || !zero || !one || !two || !three ||
663c1d255d3SCy Schubert 	    !x1a || !x1b || !x2 || !gx1 || !gx2)
664c1d255d3SCy Schubert 		goto fail;
665c1d255d3SCy Schubert 
666c1d255d3SCy Schubert 	if (z_int < 0 && crypto_bignum_sub(prime, z, z) < 0)
667c1d255d3SCy Schubert 		goto fail;
668c1d255d3SCy Schubert 
669c1d255d3SCy Schubert 	/* m = z^2 * u^4 + z * u^2 */
670c1d255d3SCy Schubert 	/* --> tmp = z * u^2, m = tmp^2 + tmp */
671c1d255d3SCy Schubert 
672c1d255d3SCy Schubert 	/* u2 = u^2
673c1d255d3SCy Schubert 	 * t1 = z * u2
674c1d255d3SCy Schubert 	 * t2 = t1^2
675c1d255d3SCy Schubert 	 * m = t1 = t1 + t2 */
676c1d255d3SCy Schubert 	if (crypto_bignum_sqrmod(u, prime, u2) < 0 ||
677c1d255d3SCy Schubert 	    crypto_bignum_mulmod(z, u2, prime, t1) < 0 ||
678c1d255d3SCy Schubert 	    crypto_bignum_sqrmod(t1, prime, t2) < 0 ||
679c1d255d3SCy Schubert 	    crypto_bignum_addmod(t1, t2, prime, t1) < 0)
680c1d255d3SCy Schubert 		goto fail;
681c1d255d3SCy Schubert 	debug_print_bignum("SSWU: m", t1, prime_len);
682c1d255d3SCy Schubert 
683c1d255d3SCy Schubert 	/* l = CEQ(m, 0)
684c1d255d3SCy Schubert 	 * t = CSEL(l, 0, inverse(m); where inverse(x) is calculated as
685c1d255d3SCy Schubert 	 * x^(p-2) modulo p which will handle m == 0 case correctly */
686c1d255d3SCy Schubert 	/* TODO: Make sure crypto_bignum_is_zero() is constant time */
687c1d255d3SCy Schubert 	m_is_zero = const_time_eq(crypto_bignum_is_zero(t1), 1);
688c1d255d3SCy Schubert 	/* t = m^(p-2) modulo p */
689c1d255d3SCy Schubert 	if (crypto_bignum_sub(prime, two, t2) < 0 ||
690c1d255d3SCy Schubert 	    crypto_bignum_exptmod(t1, t2, prime, t) < 0)
691c1d255d3SCy Schubert 		goto fail;
692c1d255d3SCy Schubert 	debug_print_bignum("SSWU: t", t, prime_len);
693c1d255d3SCy Schubert 
694c1d255d3SCy Schubert 	/* b / (z * a) */
695c1d255d3SCy Schubert 	if (crypto_bignum_mulmod(z, a, prime, t1) < 0 ||
696c1d255d3SCy Schubert 	    crypto_bignum_inverse(t1, prime, t1) < 0 ||
697c1d255d3SCy Schubert 	    crypto_bignum_mulmod(b, t1, prime, x1a) < 0)
698c1d255d3SCy Schubert 		goto fail;
699c1d255d3SCy Schubert 	debug_print_bignum("SSWU: x1a = b / (z * a)", x1a, prime_len);
700c1d255d3SCy Schubert 
701c1d255d3SCy Schubert 	/* (-b/a) * (1 + t) */
702c1d255d3SCy Schubert 	if (crypto_bignum_sub(prime, b, t1) < 0 ||
703c1d255d3SCy Schubert 	    crypto_bignum_inverse(a, prime, t2) < 0 ||
704c1d255d3SCy Schubert 	    crypto_bignum_mulmod(t1, t2, prime, t1) < 0 ||
705c1d255d3SCy Schubert 	    crypto_bignum_addmod(one, t, prime, t2) < 0 ||
706c1d255d3SCy Schubert 	    crypto_bignum_mulmod(t1, t2, prime, x1b) < 0)
707c1d255d3SCy Schubert 		goto fail;
708c1d255d3SCy Schubert 	debug_print_bignum("SSWU: x1b = (-b/a) * (1 + t)", x1b, prime_len);
709c1d255d3SCy Schubert 
710c1d255d3SCy Schubert 	/* x1 = CSEL(CEQ(m, 0), x1a, x1b) */
711c1d255d3SCy Schubert 	if (crypto_bignum_to_bin(x1a, bin1, sizeof(bin1), prime_len) < 0 ||
712c1d255d3SCy Schubert 	    crypto_bignum_to_bin(x1b, bin2, sizeof(bin2), prime_len) < 0)
713c1d255d3SCy Schubert 		goto fail;
714c1d255d3SCy Schubert 	const_time_select_bin(m_is_zero, bin1, bin2, prime_len, bin);
715c1d255d3SCy Schubert 	x1 = crypto_bignum_init_set(bin, prime_len);
716c1d255d3SCy Schubert 	if (!x1)
717c1d255d3SCy Schubert 		goto fail;
718c1d255d3SCy Schubert 	debug_print_bignum("SSWU: x1 = CSEL(l, x1a, x1b)", x1, prime_len);
719c1d255d3SCy Schubert 
720c1d255d3SCy Schubert 	/* gx1 = x1^3 + a * x1 + b */
721c1d255d3SCy Schubert 	if (crypto_bignum_exptmod(x1, three, prime, t1) < 0 ||
722c1d255d3SCy Schubert 	    crypto_bignum_mulmod(a, x1, prime, t2) < 0 ||
723c1d255d3SCy Schubert 	    crypto_bignum_addmod(t1, t2, prime, t1) < 0 ||
724c1d255d3SCy Schubert 	    crypto_bignum_addmod(t1, b, prime, gx1) < 0)
725c1d255d3SCy Schubert 		goto fail;
726c1d255d3SCy Schubert 	debug_print_bignum("SSWU: gx1 = x1^3 + a * x1 + b", gx1, prime_len);
727c1d255d3SCy Schubert 
728c1d255d3SCy Schubert 	/* x2 = z * u^2 * x1 */
729c1d255d3SCy Schubert 	if (crypto_bignum_mulmod(z, u2, prime, t1) < 0 ||
730c1d255d3SCy Schubert 	    crypto_bignum_mulmod(t1, x1, prime, x2) < 0)
731c1d255d3SCy Schubert 		goto fail;
732c1d255d3SCy Schubert 	debug_print_bignum("SSWU: x2 = z * u^2 * x1", x2, prime_len);
733c1d255d3SCy Schubert 
734c1d255d3SCy Schubert 	/* gx2 = x2^3 + a * x2 + b */
735c1d255d3SCy Schubert 	if (crypto_bignum_exptmod(x2, three, prime, t1) < 0 ||
736c1d255d3SCy Schubert 	    crypto_bignum_mulmod(a, x2, prime, t2) < 0 ||
737c1d255d3SCy Schubert 	    crypto_bignum_addmod(t1, t2, prime, t1) < 0 ||
738c1d255d3SCy Schubert 	    crypto_bignum_addmod(t1, b, prime, gx2) < 0)
739c1d255d3SCy Schubert 		goto fail;
740c1d255d3SCy Schubert 	debug_print_bignum("SSWU: gx2 = x2^3 + a * x2 + b", gx2, prime_len);
741c1d255d3SCy Schubert 
742c1d255d3SCy Schubert 	/* l = gx1 is a quadratic residue modulo p
743c1d255d3SCy Schubert 	 * --> gx1^((p-1)/2) modulo p is zero or one */
744c1d255d3SCy Schubert 	if (crypto_bignum_sub(prime, one, t1) < 0 ||
745c1d255d3SCy Schubert 	    crypto_bignum_rshift(t1, 1, t1) < 0 ||
746c1d255d3SCy Schubert 	    crypto_bignum_exptmod(gx1, t1, prime, t1) < 0)
747c1d255d3SCy Schubert 		goto fail;
748c1d255d3SCy Schubert 	debug_print_bignum("SSWU: gx1^((p-1)/2) modulo p", t1, prime_len);
749c1d255d3SCy Schubert 	is_qr = const_time_eq(crypto_bignum_is_zero(t1) |
750c1d255d3SCy Schubert 			      crypto_bignum_is_one(t1), 1);
751c1d255d3SCy Schubert 
752c1d255d3SCy Schubert 	/* v = CSEL(l, gx1, gx2) */
753c1d255d3SCy Schubert 	if (crypto_bignum_to_bin(gx1, bin1, sizeof(bin1), prime_len) < 0 ||
754c1d255d3SCy Schubert 	    crypto_bignum_to_bin(gx2, bin2, sizeof(bin2), prime_len) < 0)
755c1d255d3SCy Schubert 		goto fail;
756c1d255d3SCy Schubert 	const_time_select_bin(is_qr, bin1, bin2, prime_len, bin);
757c1d255d3SCy Schubert 	v = crypto_bignum_init_set(bin, prime_len);
758c1d255d3SCy Schubert 	if (!v)
759c1d255d3SCy Schubert 		goto fail;
760c1d255d3SCy Schubert 	debug_print_bignum("SSWU: v = CSEL(l, gx1, gx2)", v, prime_len);
761c1d255d3SCy Schubert 
762c1d255d3SCy Schubert 	/* x = CSEL(l, x1, x2) */
763c1d255d3SCy Schubert 	if (crypto_bignum_to_bin(x1, bin1, sizeof(bin1), prime_len) < 0 ||
764c1d255d3SCy Schubert 	    crypto_bignum_to_bin(x2, bin2, sizeof(bin2), prime_len) < 0)
765c1d255d3SCy Schubert 		goto fail;
766c1d255d3SCy Schubert 	const_time_select_bin(is_qr, bin1, bin2, prime_len, x_y);
767c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SSWU: x = CSEL(l, x1, x2)", x_y, prime_len);
768c1d255d3SCy Schubert 
769ec080394SCy Schubert 	/* y = sqrt(v) */
770c1d255d3SCy Schubert 	y = crypto_bignum_init();
771ec080394SCy Schubert 	if (!y || dragonfly_sqrt(ec, v, y) < 0)
772c1d255d3SCy Schubert 		goto fail;
773c1d255d3SCy Schubert 	debug_print_bignum("SSWU: y = sqrt(v)", y, prime_len);
774c1d255d3SCy Schubert 
775c1d255d3SCy Schubert 	/* l = CEQ(LSB(u), LSB(y)) */
776c1d255d3SCy Schubert 	if (crypto_bignum_to_bin(u, bin1, sizeof(bin1), prime_len) < 0 ||
777c1d255d3SCy Schubert 	    crypto_bignum_to_bin(y, bin2, sizeof(bin2), prime_len) < 0)
778c1d255d3SCy Schubert 		goto fail;
779c1d255d3SCy Schubert 	is_eq = const_time_eq(bin1[prime_len - 1] & 0x01,
780c1d255d3SCy Schubert 			      bin2[prime_len - 1] & 0x01);
781c1d255d3SCy Schubert 
782c1d255d3SCy Schubert 	/* P = CSEL(l, (x,y), (x, p-y)) */
783c1d255d3SCy Schubert 	if (crypto_bignum_sub(prime, y, t1) < 0)
784c1d255d3SCy Schubert 		goto fail;
785c1d255d3SCy Schubert 	debug_print_bignum("SSWU: p - y", t1, prime_len);
786c1d255d3SCy Schubert 	if (crypto_bignum_to_bin(y, bin1, sizeof(bin1), prime_len) < 0 ||
787c1d255d3SCy Schubert 	    crypto_bignum_to_bin(t1, bin2, sizeof(bin2), prime_len) < 0)
788c1d255d3SCy Schubert 		goto fail;
789c1d255d3SCy Schubert 	const_time_select_bin(is_eq, bin1, bin2, prime_len, &x_y[prime_len]);
790c1d255d3SCy Schubert 
791c1d255d3SCy Schubert 	/* output P */
792c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SSWU: P.x", x_y, prime_len);
793c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SSWU: P.y", &x_y[prime_len], prime_len);
794c1d255d3SCy Schubert 	p = crypto_ec_point_from_bin(ec, x_y);
795c1d255d3SCy Schubert 
796c1d255d3SCy Schubert fail:
797c1d255d3SCy Schubert 	crypto_bignum_deinit(u2, 1);
798c1d255d3SCy Schubert 	crypto_bignum_deinit(t1, 1);
799c1d255d3SCy Schubert 	crypto_bignum_deinit(t2, 1);
800c1d255d3SCy Schubert 	crypto_bignum_deinit(z, 0);
801c1d255d3SCy Schubert 	crypto_bignum_deinit(t, 1);
802c1d255d3SCy Schubert 	crypto_bignum_deinit(x1a, 1);
803c1d255d3SCy Schubert 	crypto_bignum_deinit(x1b, 1);
804c1d255d3SCy Schubert 	crypto_bignum_deinit(x1, 1);
805c1d255d3SCy Schubert 	crypto_bignum_deinit(x2, 1);
806c1d255d3SCy Schubert 	crypto_bignum_deinit(gx1, 1);
807c1d255d3SCy Schubert 	crypto_bignum_deinit(gx2, 1);
808c1d255d3SCy Schubert 	crypto_bignum_deinit(y, 1);
809c1d255d3SCy Schubert 	crypto_bignum_deinit(v, 1);
810c1d255d3SCy Schubert 	crypto_bignum_deinit(zero, 0);
811c1d255d3SCy Schubert 	crypto_bignum_deinit(one, 0);
812c1d255d3SCy Schubert 	crypto_bignum_deinit(two, 0);
813c1d255d3SCy Schubert 	crypto_bignum_deinit(three, 0);
814c1d255d3SCy Schubert 	forced_memzero(bin, sizeof(bin));
815c1d255d3SCy Schubert 	forced_memzero(bin1, sizeof(bin1));
816c1d255d3SCy Schubert 	forced_memzero(bin2, sizeof(bin2));
817c1d255d3SCy Schubert 	forced_memzero(x_y, sizeof(x_y));
818c1d255d3SCy Schubert 	return p;
819c1d255d3SCy Schubert }
820c1d255d3SCy Schubert 
821c1d255d3SCy Schubert 
sae_pwd_seed(size_t hash_len,const u8 * ssid,size_t ssid_len,const u8 * password,size_t password_len,const char * identifier,u8 * pwd_seed)822c1d255d3SCy Schubert static int sae_pwd_seed(size_t hash_len, const u8 *ssid, size_t ssid_len,
823c1d255d3SCy Schubert 			const u8 *password, size_t password_len,
824c1d255d3SCy Schubert 			const char *identifier, u8 *pwd_seed)
825c1d255d3SCy Schubert {
826c1d255d3SCy Schubert 	const u8 *addr[2];
827c1d255d3SCy Schubert 	size_t len[2];
828c1d255d3SCy Schubert 	size_t num_elem;
829c1d255d3SCy Schubert 
830c1d255d3SCy Schubert 	/* pwd-seed = HKDF-Extract(ssid, password [ || identifier ]) */
831c1d255d3SCy Schubert 	addr[0] = password;
832c1d255d3SCy Schubert 	len[0] = password_len;
833c1d255d3SCy Schubert 	num_elem = 1;
834c1d255d3SCy Schubert 	wpa_hexdump_ascii(MSG_DEBUG, "SAE: SSID", ssid, ssid_len);
835c1d255d3SCy Schubert 	wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
836c1d255d3SCy Schubert 			      password, password_len);
837c1d255d3SCy Schubert 	if (identifier) {
838c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: password identifier: %s",
839c1d255d3SCy Schubert 			   identifier);
840c1d255d3SCy Schubert 		addr[num_elem] = (const u8 *) identifier;
841c1d255d3SCy Schubert 		len[num_elem] = os_strlen(identifier);
842c1d255d3SCy Schubert 		num_elem++;
843c1d255d3SCy Schubert 	}
844c1d255d3SCy Schubert 	if (hkdf_extract(hash_len, ssid, ssid_len, num_elem, addr, len,
845c1d255d3SCy Schubert 			 pwd_seed) < 0)
846c1d255d3SCy Schubert 		return -1;
847c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, hash_len);
848c1d255d3SCy Schubert 	return 0;
849c1d255d3SCy Schubert }
850c1d255d3SCy Schubert 
851c1d255d3SCy Schubert 
sae_ecc_prime_len_2_hash_len(size_t prime_len)852c1d255d3SCy Schubert size_t sae_ecc_prime_len_2_hash_len(size_t prime_len)
853c1d255d3SCy Schubert {
854c1d255d3SCy Schubert 	if (prime_len <= 256 / 8)
855c1d255d3SCy Schubert 		return 32;
856c1d255d3SCy Schubert 	if (prime_len <= 384 / 8)
857c1d255d3SCy Schubert 		return 48;
858c1d255d3SCy Schubert 	return 64;
859c1d255d3SCy Schubert }
860c1d255d3SCy Schubert 
861c1d255d3SCy Schubert 
862c1d255d3SCy Schubert static struct crypto_ec_point *
sae_derive_pt_ecc(struct crypto_ec * ec,int group,const u8 * ssid,size_t ssid_len,const u8 * password,size_t password_len,const char * identifier)863c1d255d3SCy Schubert sae_derive_pt_ecc(struct crypto_ec *ec, int group,
864c1d255d3SCy Schubert 		  const u8 *ssid, size_t ssid_len,
865c1d255d3SCy Schubert 		  const u8 *password, size_t password_len,
866c1d255d3SCy Schubert 		  const char *identifier)
867c1d255d3SCy Schubert {
868c1d255d3SCy Schubert 	u8 pwd_seed[64];
869c1d255d3SCy Schubert 	u8 pwd_value[SAE_MAX_ECC_PRIME_LEN * 2];
870c1d255d3SCy Schubert 	size_t pwd_value_len, hash_len, prime_len;
871c1d255d3SCy Schubert 	const struct crypto_bignum *prime;
872c1d255d3SCy Schubert 	struct crypto_bignum *bn = NULL;
873c1d255d3SCy Schubert 	struct crypto_ec_point *p1 = NULL, *p2 = NULL, *pt = NULL;
874c1d255d3SCy Schubert 
875c1d255d3SCy Schubert 	prime = crypto_ec_get_prime(ec);
876c1d255d3SCy Schubert 	prime_len = crypto_ec_prime_len(ec);
877c1d255d3SCy Schubert 	if (prime_len > SAE_MAX_ECC_PRIME_LEN)
878c1d255d3SCy Schubert 		goto fail;
879c1d255d3SCy Schubert 	hash_len = sae_ecc_prime_len_2_hash_len(prime_len);
880c1d255d3SCy Schubert 
881c1d255d3SCy Schubert 	/* len = olen(p) + ceil(olen(p)/2) */
882c1d255d3SCy Schubert 	pwd_value_len = prime_len + (prime_len + 1) / 2;
883c1d255d3SCy Schubert 
884c1d255d3SCy Schubert 	if (sae_pwd_seed(hash_len, ssid, ssid_len, password, password_len,
885c1d255d3SCy Schubert 			 identifier, pwd_seed) < 0)
886c1d255d3SCy Schubert 		goto fail;
887c1d255d3SCy Schubert 
888c1d255d3SCy Schubert 	/* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u1 P1", len)
889c1d255d3SCy Schubert 	 */
890c1d255d3SCy Schubert 	if (hkdf_expand(hash_len, pwd_seed, hash_len,
891c1d255d3SCy Schubert 			"SAE Hash to Element u1 P1", pwd_value, pwd_value_len) <
892c1d255d3SCy Schubert 	    0)
893c1d255d3SCy Schubert 		goto fail;
894c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value (u1 P1)",
895c1d255d3SCy Schubert 			pwd_value, pwd_value_len);
896c1d255d3SCy Schubert 
897c1d255d3SCy Schubert 	/* u1 = pwd-value modulo p */
898c1d255d3SCy Schubert 	bn = crypto_bignum_init_set(pwd_value, pwd_value_len);
899c1d255d3SCy Schubert 	if (!bn || crypto_bignum_mod(bn, prime, bn) < 0 ||
900c1d255d3SCy Schubert 	    crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value),
901c1d255d3SCy Schubert 				 prime_len) < 0)
902c1d255d3SCy Schubert 		goto fail;
903c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: u1", pwd_value, prime_len);
904c1d255d3SCy Schubert 
905c1d255d3SCy Schubert 	/* P1 = SSWU(u1) */
906c1d255d3SCy Schubert 	p1 = sswu(ec, group, bn);
907c1d255d3SCy Schubert 	if (!p1)
908c1d255d3SCy Schubert 		goto fail;
909c1d255d3SCy Schubert 
910c1d255d3SCy Schubert 	/* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u2 P2", len)
911c1d255d3SCy Schubert 	 */
912c1d255d3SCy Schubert 	if (hkdf_expand(hash_len, pwd_seed, hash_len,
913c1d255d3SCy Schubert 			"SAE Hash to Element u2 P2", pwd_value,
914c1d255d3SCy Schubert 			pwd_value_len) < 0)
915c1d255d3SCy Schubert 		goto fail;
916c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value (u2 P2)",
917c1d255d3SCy Schubert 			pwd_value, pwd_value_len);
918c1d255d3SCy Schubert 
919c1d255d3SCy Schubert 	/* u2 = pwd-value modulo p */
920c1d255d3SCy Schubert 	crypto_bignum_deinit(bn, 1);
921c1d255d3SCy Schubert 	bn = crypto_bignum_init_set(pwd_value, pwd_value_len);
922c1d255d3SCy Schubert 	if (!bn || crypto_bignum_mod(bn, prime, bn) < 0 ||
923c1d255d3SCy Schubert 	    crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value),
924c1d255d3SCy Schubert 				 prime_len) < 0)
925c1d255d3SCy Schubert 		goto fail;
926c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: u2", pwd_value, prime_len);
927c1d255d3SCy Schubert 
928c1d255d3SCy Schubert 	/* P2 = SSWU(u2) */
929c1d255d3SCy Schubert 	p2 = sswu(ec, group, bn);
930c1d255d3SCy Schubert 	if (!p2)
931c1d255d3SCy Schubert 		goto fail;
932c1d255d3SCy Schubert 
933c1d255d3SCy Schubert 	/* PT = elem-op(P1, P2) */
934c1d255d3SCy Schubert 	pt = crypto_ec_point_init(ec);
935c1d255d3SCy Schubert 	if (!pt)
936c1d255d3SCy Schubert 		goto fail;
937c1d255d3SCy Schubert 	if (crypto_ec_point_add(ec, p1, p2, pt) < 0) {
938c1d255d3SCy Schubert 		crypto_ec_point_deinit(pt, 1);
939c1d255d3SCy Schubert 		pt = NULL;
940c1d255d3SCy Schubert 	}
941c1d255d3SCy Schubert 
942c1d255d3SCy Schubert fail:
943c1d255d3SCy Schubert 	forced_memzero(pwd_seed, sizeof(pwd_seed));
944c1d255d3SCy Schubert 	forced_memzero(pwd_value, sizeof(pwd_value));
945c1d255d3SCy Schubert 	crypto_bignum_deinit(bn, 1);
946c1d255d3SCy Schubert 	crypto_ec_point_deinit(p1, 1);
947c1d255d3SCy Schubert 	crypto_ec_point_deinit(p2, 1);
948c1d255d3SCy Schubert 	return pt;
949c1d255d3SCy Schubert }
950c1d255d3SCy Schubert 
951c1d255d3SCy Schubert 
sae_ffc_prime_len_2_hash_len(size_t prime_len)952c1d255d3SCy Schubert size_t sae_ffc_prime_len_2_hash_len(size_t prime_len)
953c1d255d3SCy Schubert {
954c1d255d3SCy Schubert 	if (prime_len <= 2048 / 8)
955c1d255d3SCy Schubert 		return 32;
956c1d255d3SCy Schubert 	if (prime_len <= 3072 / 8)
957c1d255d3SCy Schubert 		return 48;
958c1d255d3SCy Schubert 	return 64;
959c1d255d3SCy Schubert }
960c1d255d3SCy Schubert 
961c1d255d3SCy Schubert 
962c1d255d3SCy Schubert static struct crypto_bignum *
sae_derive_pt_ffc(const struct dh_group * dh,int group,const u8 * ssid,size_t ssid_len,const u8 * password,size_t password_len,const char * identifier)963c1d255d3SCy Schubert sae_derive_pt_ffc(const struct dh_group *dh, int group,
964c1d255d3SCy Schubert 		  const u8 *ssid, size_t ssid_len,
965c1d255d3SCy Schubert 		  const u8 *password, size_t password_len,
966c1d255d3SCy Schubert 		  const char *identifier)
967c1d255d3SCy Schubert {
968c1d255d3SCy Schubert 	size_t hash_len, prime_len, pwd_value_len;
969c1d255d3SCy Schubert 	struct crypto_bignum *prime, *order;
970c1d255d3SCy Schubert 	struct crypto_bignum *one = NULL, *two = NULL, *bn = NULL, *tmp = NULL,
971c1d255d3SCy Schubert 		*pt = NULL;
972c1d255d3SCy Schubert 	u8 pwd_seed[64];
973c1d255d3SCy Schubert 	u8 pwd_value[SAE_MAX_PRIME_LEN + SAE_MAX_PRIME_LEN / 2];
974c1d255d3SCy Schubert 
975c1d255d3SCy Schubert 	prime = crypto_bignum_init_set(dh->prime, dh->prime_len);
976c1d255d3SCy Schubert 	order = crypto_bignum_init_set(dh->order, dh->order_len);
977c1d255d3SCy Schubert 	if (!prime || !order)
978c1d255d3SCy Schubert 		goto fail;
979c1d255d3SCy Schubert 	prime_len = dh->prime_len;
980c1d255d3SCy Schubert 	if (prime_len > SAE_MAX_PRIME_LEN)
981c1d255d3SCy Schubert 		goto fail;
982c1d255d3SCy Schubert 	hash_len = sae_ffc_prime_len_2_hash_len(prime_len);
983c1d255d3SCy Schubert 
984c1d255d3SCy Schubert 	/* len = olen(p) + ceil(olen(p)/2) */
985c1d255d3SCy Schubert 	pwd_value_len = prime_len + (prime_len + 1) / 2;
986c1d255d3SCy Schubert 	if (pwd_value_len > sizeof(pwd_value))
987c1d255d3SCy Schubert 		goto fail;
988c1d255d3SCy Schubert 
989c1d255d3SCy Schubert 	if (sae_pwd_seed(hash_len, ssid, ssid_len, password, password_len,
990c1d255d3SCy Schubert 			 identifier, pwd_seed) < 0)
991c1d255d3SCy Schubert 		goto fail;
992c1d255d3SCy Schubert 
993c1d255d3SCy Schubert 	/* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element", len) */
994c1d255d3SCy Schubert 	if (hkdf_expand(hash_len, pwd_seed, hash_len,
995c1d255d3SCy Schubert 			"SAE Hash to Element", pwd_value, pwd_value_len) < 0)
996c1d255d3SCy Schubert 		goto fail;
997c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value",
998c1d255d3SCy Schubert 			pwd_value, pwd_value_len);
999c1d255d3SCy Schubert 
1000c1d255d3SCy Schubert 	/* pwd-value = (pwd-value modulo (p-2)) + 2 */
1001c1d255d3SCy Schubert 	bn = crypto_bignum_init_set(pwd_value, pwd_value_len);
1002c1d255d3SCy Schubert 	one = crypto_bignum_init_uint(1);
1003c1d255d3SCy Schubert 	two = crypto_bignum_init_uint(2);
1004c1d255d3SCy Schubert 	tmp = crypto_bignum_init();
1005c1d255d3SCy Schubert 	if (!bn || !one || !two || !tmp ||
1006c1d255d3SCy Schubert 	    crypto_bignum_sub(prime, two, tmp) < 0 ||
1007c1d255d3SCy Schubert 	    crypto_bignum_mod(bn, tmp, bn) < 0 ||
1008c1d255d3SCy Schubert 	    crypto_bignum_add(bn, two, bn) < 0 ||
1009c1d255d3SCy Schubert 	    crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value),
1010c1d255d3SCy Schubert 				 prime_len) < 0)
1011c1d255d3SCy Schubert 		goto fail;
1012c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value(reduced)",
1013c1d255d3SCy Schubert 			pwd_value, prime_len);
1014c1d255d3SCy Schubert 
1015c1d255d3SCy Schubert 	/* PT = pwd-value^((p-1)/q) modulo p */
1016c1d255d3SCy Schubert 	pt = crypto_bignum_init();
1017c1d255d3SCy Schubert 	if (!pt ||
1018c1d255d3SCy Schubert 	    crypto_bignum_sub(prime, one, tmp) < 0 ||
1019c1d255d3SCy Schubert 	    crypto_bignum_div(tmp, order, tmp) < 0 ||
1020c1d255d3SCy Schubert 	    crypto_bignum_exptmod(bn, tmp, prime, pt) < 0) {
1021c1d255d3SCy Schubert 		crypto_bignum_deinit(pt, 1);
1022c1d255d3SCy Schubert 		pt = NULL;
1023c1d255d3SCy Schubert 		goto fail;
1024c1d255d3SCy Schubert 	}
1025c1d255d3SCy Schubert 	debug_print_bignum("SAE: PT", pt, prime_len);
1026c1d255d3SCy Schubert 
1027c1d255d3SCy Schubert fail:
1028c1d255d3SCy Schubert 	forced_memzero(pwd_seed, sizeof(pwd_seed));
1029c1d255d3SCy Schubert 	forced_memzero(pwd_value, sizeof(pwd_value));
1030c1d255d3SCy Schubert 	crypto_bignum_deinit(bn, 1);
1031c1d255d3SCy Schubert 	crypto_bignum_deinit(tmp, 1);
1032c1d255d3SCy Schubert 	crypto_bignum_deinit(one, 0);
1033c1d255d3SCy Schubert 	crypto_bignum_deinit(two, 0);
1034c1d255d3SCy Schubert 	crypto_bignum_deinit(prime, 0);
1035c1d255d3SCy Schubert 	crypto_bignum_deinit(order, 0);
1036c1d255d3SCy Schubert 	return pt;
1037c1d255d3SCy Schubert }
1038c1d255d3SCy Schubert 
1039c1d255d3SCy Schubert 
1040c1d255d3SCy Schubert static struct sae_pt *
sae_derive_pt_group(int group,const u8 * ssid,size_t ssid_len,const u8 * password,size_t password_len,const char * identifier)1041c1d255d3SCy Schubert sae_derive_pt_group(int group, const u8 *ssid, size_t ssid_len,
1042c1d255d3SCy Schubert 		    const u8 *password, size_t password_len,
1043c1d255d3SCy Schubert 		    const char *identifier)
1044c1d255d3SCy Schubert {
1045c1d255d3SCy Schubert 	struct sae_pt *pt;
1046c1d255d3SCy Schubert 
1047c1d255d3SCy Schubert 	wpa_printf(MSG_DEBUG, "SAE: Derive PT - group %d", group);
1048c1d255d3SCy Schubert 
1049c1d255d3SCy Schubert 	if (ssid_len > 32)
1050c1d255d3SCy Schubert 		return NULL;
1051c1d255d3SCy Schubert 
1052c1d255d3SCy Schubert 	pt = os_zalloc(sizeof(*pt));
1053c1d255d3SCy Schubert 	if (!pt)
1054c1d255d3SCy Schubert 		return NULL;
1055c1d255d3SCy Schubert 
1056c1d255d3SCy Schubert #ifdef CONFIG_SAE_PK
1057c1d255d3SCy Schubert 	os_memcpy(pt->ssid, ssid, ssid_len);
1058c1d255d3SCy Schubert 	pt->ssid_len = ssid_len;
1059c1d255d3SCy Schubert #endif /* CONFIG_SAE_PK */
1060c1d255d3SCy Schubert 	pt->group = group;
1061c1d255d3SCy Schubert 	pt->ec = crypto_ec_init(group);
1062c1d255d3SCy Schubert 	if (pt->ec) {
1063c1d255d3SCy Schubert 		pt->ecc_pt = sae_derive_pt_ecc(pt->ec, group, ssid, ssid_len,
1064c1d255d3SCy Schubert 					       password, password_len,
1065c1d255d3SCy Schubert 					       identifier);
1066c1d255d3SCy Schubert 		if (!pt->ecc_pt) {
1067c1d255d3SCy Schubert 			wpa_printf(MSG_DEBUG, "SAE: Failed to derive PT");
1068c1d255d3SCy Schubert 			goto fail;
1069c1d255d3SCy Schubert 		}
1070c1d255d3SCy Schubert 
1071c1d255d3SCy Schubert 		return pt;
1072c1d255d3SCy Schubert 	}
1073c1d255d3SCy Schubert 
1074c1d255d3SCy Schubert 	pt->dh = dh_groups_get(group);
1075c1d255d3SCy Schubert 	if (!pt->dh) {
1076c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: Unsupported group %d", group);
1077c1d255d3SCy Schubert 		goto fail;
1078c1d255d3SCy Schubert 	}
1079c1d255d3SCy Schubert 
1080c1d255d3SCy Schubert 	pt->ffc_pt = sae_derive_pt_ffc(pt->dh, group, ssid, ssid_len,
1081c1d255d3SCy Schubert 				       password, password_len, identifier);
1082c1d255d3SCy Schubert 	if (!pt->ffc_pt) {
1083c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: Failed to derive PT");
1084c1d255d3SCy Schubert 		goto fail;
1085c1d255d3SCy Schubert 	}
1086c1d255d3SCy Schubert 
1087c1d255d3SCy Schubert 	return pt;
1088c1d255d3SCy Schubert fail:
1089c1d255d3SCy Schubert 	sae_deinit_pt(pt);
1090c1d255d3SCy Schubert 	return NULL;
1091c1d255d3SCy Schubert }
1092c1d255d3SCy Schubert 
1093c1d255d3SCy Schubert 
sae_derive_pt(int * groups,const u8 * ssid,size_t ssid_len,const u8 * password,size_t password_len,const char * identifier)1094c1d255d3SCy Schubert struct sae_pt * sae_derive_pt(int *groups, const u8 *ssid, size_t ssid_len,
1095c1d255d3SCy Schubert 			      const u8 *password, size_t password_len,
1096c1d255d3SCy Schubert 			      const char *identifier)
1097c1d255d3SCy Schubert {
1098c1d255d3SCy Schubert 	struct sae_pt *pt = NULL, *last = NULL, *tmp;
1099c1d255d3SCy Schubert 	int default_groups[] = { 19, 0 };
1100c1d255d3SCy Schubert 	int i;
1101c1d255d3SCy Schubert 
1102c1d255d3SCy Schubert 	if (!groups)
1103c1d255d3SCy Schubert 		groups = default_groups;
1104c1d255d3SCy Schubert 	for (i = 0; groups[i] > 0; i++) {
1105c1d255d3SCy Schubert 		tmp = sae_derive_pt_group(groups[i], ssid, ssid_len, password,
1106c1d255d3SCy Schubert 					  password_len, identifier);
1107c1d255d3SCy Schubert 		if (!tmp)
1108c1d255d3SCy Schubert 			continue;
1109c1d255d3SCy Schubert 
1110c1d255d3SCy Schubert 		if (last)
1111c1d255d3SCy Schubert 			last->next = tmp;
1112c1d255d3SCy Schubert 		else
1113c1d255d3SCy Schubert 			pt = tmp;
1114c1d255d3SCy Schubert 		last = tmp;
1115c1d255d3SCy Schubert 	}
1116c1d255d3SCy Schubert 
1117c1d255d3SCy Schubert 	return pt;
1118c1d255d3SCy Schubert }
1119c1d255d3SCy Schubert 
1120c1d255d3SCy Schubert 
sae_max_min_addr(const u8 * addr[],size_t len[],const u8 * addr1,const u8 * addr2)1121c1d255d3SCy Schubert static void sae_max_min_addr(const u8 *addr[], size_t len[],
1122c1d255d3SCy Schubert 			     const u8 *addr1, const u8 *addr2)
1123c1d255d3SCy Schubert {
1124c1d255d3SCy Schubert 	len[0] = ETH_ALEN;
1125c1d255d3SCy Schubert 	len[1] = ETH_ALEN;
1126c1d255d3SCy Schubert 	if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) {
1127c1d255d3SCy Schubert 		addr[0] = addr1;
1128c1d255d3SCy Schubert 		addr[1] = addr2;
1129c1d255d3SCy Schubert 	} else {
1130c1d255d3SCy Schubert 		addr[0] = addr2;
1131c1d255d3SCy Schubert 		addr[1] = addr1;
1132c1d255d3SCy Schubert 	}
1133c1d255d3SCy Schubert }
1134c1d255d3SCy Schubert 
1135c1d255d3SCy Schubert 
1136c1d255d3SCy Schubert struct crypto_ec_point *
sae_derive_pwe_from_pt_ecc(const struct sae_pt * pt,const u8 * addr1,const u8 * addr2)1137c1d255d3SCy Schubert sae_derive_pwe_from_pt_ecc(const struct sae_pt *pt,
1138c1d255d3SCy Schubert 			   const u8 *addr1, const u8 *addr2)
1139c1d255d3SCy Schubert {
1140c1d255d3SCy Schubert 	u8 bin[SAE_MAX_ECC_PRIME_LEN * 2];
1141c1d255d3SCy Schubert 	size_t prime_len;
1142c1d255d3SCy Schubert 	const u8 *addr[2];
1143c1d255d3SCy Schubert 	size_t len[2];
1144c1d255d3SCy Schubert 	u8 salt[64], hash[64];
1145c1d255d3SCy Schubert 	size_t hash_len;
1146c1d255d3SCy Schubert 	const struct crypto_bignum *order;
1147c1d255d3SCy Schubert 	struct crypto_bignum *tmp = NULL, *val = NULL, *one = NULL;
1148c1d255d3SCy Schubert 	struct crypto_ec_point *pwe = NULL;
1149c1d255d3SCy Schubert 
1150c1d255d3SCy Schubert 	wpa_printf(MSG_DEBUG, "SAE: Derive PWE from PT");
1151c1d255d3SCy Schubert 	prime_len = crypto_ec_prime_len(pt->ec);
1152c1d255d3SCy Schubert 	if (crypto_ec_point_to_bin(pt->ec, pt->ecc_pt,
1153c1d255d3SCy Schubert 				   bin, bin + prime_len) < 0)
1154c1d255d3SCy Schubert 		return NULL;
1155c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: PT.x", bin, prime_len);
1156c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: PT.y", bin + prime_len, prime_len);
1157c1d255d3SCy Schubert 
1158c1d255d3SCy Schubert 	sae_max_min_addr(addr, len, addr1, addr2);
1159c1d255d3SCy Schubert 
1160c1d255d3SCy Schubert 	/* val = H(0^n,
1161c1d255d3SCy Schubert 	 *         MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC)) */
1162c1d255d3SCy Schubert 	wpa_printf(MSG_DEBUG, "SAE: val = H(0^n, MAX(addrs) || MIN(addrs))");
1163c1d255d3SCy Schubert 	hash_len = sae_ecc_prime_len_2_hash_len(prime_len);
1164c1d255d3SCy Schubert 	os_memset(salt, 0, hash_len);
1165c1d255d3SCy Schubert 	if (hkdf_extract(hash_len, salt, hash_len, 2, addr, len, hash) < 0)
1166c1d255d3SCy Schubert 		goto fail;
1167c1d255d3SCy Schubert 	wpa_hexdump(MSG_DEBUG, "SAE: val", hash, hash_len);
1168c1d255d3SCy Schubert 
1169c1d255d3SCy Schubert 	/* val = val modulo (q - 1) + 1 */
1170c1d255d3SCy Schubert 	order = crypto_ec_get_order(pt->ec);
1171c1d255d3SCy Schubert 	tmp = crypto_bignum_init();
1172c1d255d3SCy Schubert 	val = crypto_bignum_init_set(hash, hash_len);
1173c1d255d3SCy Schubert 	one = crypto_bignum_init_uint(1);
1174c1d255d3SCy Schubert 	if (!tmp || !val || !one ||
1175c1d255d3SCy Schubert 	    crypto_bignum_sub(order, one, tmp) < 0 ||
1176c1d255d3SCy Schubert 	    crypto_bignum_mod(val, tmp, val) < 0 ||
1177c1d255d3SCy Schubert 	    crypto_bignum_add(val, one, val) < 0)
1178c1d255d3SCy Schubert 		goto fail;
1179c1d255d3SCy Schubert 	debug_print_bignum("SAE: val(reduced to 1..q-1)", val, prime_len);
1180c1d255d3SCy Schubert 
1181c1d255d3SCy Schubert 	/* PWE = scalar-op(val, PT) */
1182c1d255d3SCy Schubert 	pwe = crypto_ec_point_init(pt->ec);
1183c1d255d3SCy Schubert 	if (!pwe ||
1184c1d255d3SCy Schubert 	    crypto_ec_point_mul(pt->ec, pt->ecc_pt, val, pwe) < 0 ||
1185c1d255d3SCy Schubert 	    crypto_ec_point_to_bin(pt->ec, pwe, bin, bin + prime_len) < 0) {
1186c1d255d3SCy Schubert 		crypto_ec_point_deinit(pwe, 1);
1187c1d255d3SCy Schubert 		pwe = NULL;
1188c1d255d3SCy Schubert 		goto fail;
1189c1d255d3SCy Schubert 	}
1190c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: PWE.x", bin, prime_len);
1191c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: PWE.y", bin + prime_len, prime_len);
1192c1d255d3SCy Schubert 
1193c1d255d3SCy Schubert fail:
1194c1d255d3SCy Schubert 	crypto_bignum_deinit(tmp, 1);
1195c1d255d3SCy Schubert 	crypto_bignum_deinit(val, 1);
1196c1d255d3SCy Schubert 	crypto_bignum_deinit(one, 0);
1197c1d255d3SCy Schubert 	return pwe;
1198c1d255d3SCy Schubert }
1199c1d255d3SCy Schubert 
1200c1d255d3SCy Schubert 
1201c1d255d3SCy Schubert struct crypto_bignum *
sae_derive_pwe_from_pt_ffc(const struct sae_pt * pt,const u8 * addr1,const u8 * addr2)1202c1d255d3SCy Schubert sae_derive_pwe_from_pt_ffc(const struct sae_pt *pt,
1203c1d255d3SCy Schubert 			   const u8 *addr1, const u8 *addr2)
1204c1d255d3SCy Schubert {
1205c1d255d3SCy Schubert 	size_t prime_len;
1206c1d255d3SCy Schubert 	const u8 *addr[2];
1207c1d255d3SCy Schubert 	size_t len[2];
1208c1d255d3SCy Schubert 	u8 salt[64], hash[64];
1209c1d255d3SCy Schubert 	size_t hash_len;
1210c1d255d3SCy Schubert 	struct crypto_bignum *tmp = NULL, *val = NULL, *one = NULL;
1211c1d255d3SCy Schubert 	struct crypto_bignum *pwe = NULL, *order = NULL, *prime = NULL;
1212c1d255d3SCy Schubert 
1213c1d255d3SCy Schubert 	wpa_printf(MSG_DEBUG, "SAE: Derive PWE from PT");
1214c1d255d3SCy Schubert 	prime = crypto_bignum_init_set(pt->dh->prime, pt->dh->prime_len);
1215c1d255d3SCy Schubert 	order = crypto_bignum_init_set(pt->dh->order, pt->dh->order_len);
1216c1d255d3SCy Schubert 	if (!prime || !order)
1217c1d255d3SCy Schubert 		goto fail;
1218c1d255d3SCy Schubert 	prime_len = pt->dh->prime_len;
1219c1d255d3SCy Schubert 
1220c1d255d3SCy Schubert 	sae_max_min_addr(addr, len, addr1, addr2);
1221c1d255d3SCy Schubert 
1222c1d255d3SCy Schubert 	/* val = H(0^n,
1223c1d255d3SCy Schubert 	 *         MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC)) */
1224c1d255d3SCy Schubert 	wpa_printf(MSG_DEBUG, "SAE: val = H(0^n, MAX(addrs) || MIN(addrs))");
1225c1d255d3SCy Schubert 	hash_len = sae_ffc_prime_len_2_hash_len(prime_len);
1226c1d255d3SCy Schubert 	os_memset(salt, 0, hash_len);
1227c1d255d3SCy Schubert 	if (hkdf_extract(hash_len, salt, hash_len, 2, addr, len, hash) < 0)
1228c1d255d3SCy Schubert 		goto fail;
1229c1d255d3SCy Schubert 	wpa_hexdump(MSG_DEBUG, "SAE: val", hash, hash_len);
1230c1d255d3SCy Schubert 
1231c1d255d3SCy Schubert 	/* val = val modulo (q - 1) + 1 */
1232c1d255d3SCy Schubert 	tmp = crypto_bignum_init();
1233c1d255d3SCy Schubert 	val = crypto_bignum_init_set(hash, hash_len);
1234c1d255d3SCy Schubert 	one = crypto_bignum_init_uint(1);
1235c1d255d3SCy Schubert 	if (!tmp || !val || !one ||
1236c1d255d3SCy Schubert 	    crypto_bignum_sub(order, one, tmp) < 0 ||
1237c1d255d3SCy Schubert 	    crypto_bignum_mod(val, tmp, val) < 0 ||
1238c1d255d3SCy Schubert 	    crypto_bignum_add(val, one, val) < 0)
1239c1d255d3SCy Schubert 		goto fail;
1240c1d255d3SCy Schubert 	debug_print_bignum("SAE: val(reduced to 1..q-1)", val, prime_len);
1241c1d255d3SCy Schubert 
1242c1d255d3SCy Schubert 	/* PWE = scalar-op(val, PT) */
1243c1d255d3SCy Schubert 	pwe = crypto_bignum_init();
1244c1d255d3SCy Schubert 	if (!pwe || crypto_bignum_exptmod(pt->ffc_pt, val, prime, pwe) < 0) {
1245c1d255d3SCy Schubert 		crypto_bignum_deinit(pwe, 1);
1246c1d255d3SCy Schubert 		pwe = NULL;
1247c1d255d3SCy Schubert 		goto fail;
1248c1d255d3SCy Schubert 	}
1249c1d255d3SCy Schubert 	debug_print_bignum("SAE: PWE", pwe, prime_len);
1250c1d255d3SCy Schubert 
1251c1d255d3SCy Schubert fail:
1252c1d255d3SCy Schubert 	crypto_bignum_deinit(tmp, 1);
1253c1d255d3SCy Schubert 	crypto_bignum_deinit(val, 1);
1254c1d255d3SCy Schubert 	crypto_bignum_deinit(one, 0);
1255c1d255d3SCy Schubert 	crypto_bignum_deinit(prime, 0);
1256c1d255d3SCy Schubert 	crypto_bignum_deinit(order, 0);
1257c1d255d3SCy Schubert 	return pwe;
1258c1d255d3SCy Schubert }
1259c1d255d3SCy Schubert 
1260c1d255d3SCy Schubert 
sae_deinit_pt(struct sae_pt * pt)1261c1d255d3SCy Schubert void sae_deinit_pt(struct sae_pt *pt)
1262c1d255d3SCy Schubert {
1263c1d255d3SCy Schubert 	struct sae_pt *prev;
1264c1d255d3SCy Schubert 
1265c1d255d3SCy Schubert 	while (pt) {
1266c1d255d3SCy Schubert 		crypto_ec_point_deinit(pt->ecc_pt, 1);
1267c1d255d3SCy Schubert 		crypto_bignum_deinit(pt->ffc_pt, 1);
1268c1d255d3SCy Schubert 		crypto_ec_deinit(pt->ec);
1269c1d255d3SCy Schubert 		prev = pt;
1270c1d255d3SCy Schubert 		pt = pt->next;
1271c1d255d3SCy Schubert 		os_free(prev);
1272c1d255d3SCy Schubert 	}
1273c1d255d3SCy Schubert }
1274c1d255d3SCy Schubert 
1275c1d255d3SCy Schubert 
sae_derive_commit_element_ecc(struct sae_data * sae,struct crypto_bignum * mask)12765b9c547cSRui Paulo static int sae_derive_commit_element_ecc(struct sae_data *sae,
12775b9c547cSRui Paulo 					 struct crypto_bignum *mask)
12785b9c547cSRui Paulo {
12795b9c547cSRui Paulo 	/* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
12805b9c547cSRui Paulo 	if (!sae->tmp->own_commit_element_ecc) {
12815b9c547cSRui Paulo 		sae->tmp->own_commit_element_ecc =
12825b9c547cSRui Paulo 			crypto_ec_point_init(sae->tmp->ec);
12835b9c547cSRui Paulo 		if (!sae->tmp->own_commit_element_ecc)
12845b9c547cSRui Paulo 			return -1;
12855b9c547cSRui Paulo 	}
12865b9c547cSRui Paulo 
12875b9c547cSRui Paulo 	if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc, mask,
12885b9c547cSRui Paulo 				sae->tmp->own_commit_element_ecc) < 0 ||
12895b9c547cSRui Paulo 	    crypto_ec_point_invert(sae->tmp->ec,
12905b9c547cSRui Paulo 				   sae->tmp->own_commit_element_ecc) < 0) {
12915b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
12925b9c547cSRui Paulo 		return -1;
12935b9c547cSRui Paulo 	}
12945b9c547cSRui Paulo 
12955b9c547cSRui Paulo 	return 0;
12965b9c547cSRui Paulo }
12975b9c547cSRui Paulo 
12985b9c547cSRui Paulo 
sae_derive_commit_element_ffc(struct sae_data * sae,struct crypto_bignum * mask)12995b9c547cSRui Paulo static int sae_derive_commit_element_ffc(struct sae_data *sae,
13005b9c547cSRui Paulo 					 struct crypto_bignum *mask)
13015b9c547cSRui Paulo {
13025b9c547cSRui Paulo 	/* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
13035b9c547cSRui Paulo 	if (!sae->tmp->own_commit_element_ffc) {
13045b9c547cSRui Paulo 		sae->tmp->own_commit_element_ffc = crypto_bignum_init();
13055b9c547cSRui Paulo 		if (!sae->tmp->own_commit_element_ffc)
13065b9c547cSRui Paulo 			return -1;
13075b9c547cSRui Paulo 	}
13085b9c547cSRui Paulo 
13095b9c547cSRui Paulo 	if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, mask, sae->tmp->prime,
13105b9c547cSRui Paulo 				  sae->tmp->own_commit_element_ffc) < 0 ||
13115b9c547cSRui Paulo 	    crypto_bignum_inverse(sae->tmp->own_commit_element_ffc,
13125b9c547cSRui Paulo 				  sae->tmp->prime,
13135b9c547cSRui Paulo 				  sae->tmp->own_commit_element_ffc) < 0) {
13145b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
13155b9c547cSRui Paulo 		return -1;
13165b9c547cSRui Paulo 	}
13175b9c547cSRui Paulo 
13185b9c547cSRui Paulo 	return 0;
13195b9c547cSRui Paulo }
13205b9c547cSRui Paulo 
13215b9c547cSRui Paulo 
sae_derive_commit(struct sae_data * sae)13225b9c547cSRui Paulo static int sae_derive_commit(struct sae_data *sae)
13235b9c547cSRui Paulo {
13245b9c547cSRui Paulo 	struct crypto_bignum *mask;
1325206b73d0SCy Schubert 	int ret;
1326325151a3SRui Paulo 
1327206b73d0SCy Schubert 	mask = crypto_bignum_init();
1328206b73d0SCy Schubert 	if (!sae->tmp->sae_rand)
1329206b73d0SCy Schubert 		sae->tmp->sae_rand = crypto_bignum_init();
13305b9c547cSRui Paulo 	if (!sae->tmp->own_commit_scalar)
1331206b73d0SCy Schubert 		sae->tmp->own_commit_scalar = crypto_bignum_init();
1332206b73d0SCy Schubert 	ret = !mask || !sae->tmp->sae_rand || !sae->tmp->own_commit_scalar ||
1333206b73d0SCy Schubert 		dragonfly_generate_scalar(sae->tmp->order, sae->tmp->sae_rand,
1334206b73d0SCy Schubert 					  mask,
1335206b73d0SCy Schubert 					  sae->tmp->own_commit_scalar) < 0 ||
1336206b73d0SCy Schubert 		(sae->tmp->ec &&
1337206b73d0SCy Schubert 		 sae_derive_commit_element_ecc(sae, mask) < 0) ||
1338206b73d0SCy Schubert 		(sae->tmp->dh &&
1339206b73d0SCy Schubert 		 sae_derive_commit_element_ffc(sae, mask) < 0);
13405b9c547cSRui Paulo 	crypto_bignum_deinit(mask, 1);
1341206b73d0SCy Schubert 	return ret ? -1 : 0;
13425b9c547cSRui Paulo }
13435b9c547cSRui Paulo 
13445b9c547cSRui Paulo 
sae_prepare_commit(const u8 * addr1,const u8 * addr2,const u8 * password,size_t password_len,struct sae_data * sae)13455b9c547cSRui Paulo int sae_prepare_commit(const u8 *addr1, const u8 *addr2,
13465b9c547cSRui Paulo 		       const u8 *password, size_t password_len,
1347c1d255d3SCy Schubert 		       struct sae_data *sae)
13485b9c547cSRui Paulo {
1349325151a3SRui Paulo 	if (sae->tmp == NULL ||
1350325151a3SRui Paulo 	    (sae->tmp->ec && sae_derive_pwe_ecc(sae, addr1, addr2, password,
1351c1d255d3SCy Schubert 						password_len) < 0) ||
1352325151a3SRui Paulo 	    (sae->tmp->dh && sae_derive_pwe_ffc(sae, addr1, addr2, password,
1353c1d255d3SCy Schubert 						password_len) < 0))
13545b9c547cSRui Paulo 		return -1;
1355c1d255d3SCy Schubert 
1356c1d255d3SCy Schubert 	sae->h2e = 0;
1357c1d255d3SCy Schubert 	sae->pk = 0;
1358c1d255d3SCy Schubert 	return sae_derive_commit(sae);
1359c1d255d3SCy Schubert }
1360c1d255d3SCy Schubert 
1361c1d255d3SCy Schubert 
sae_prepare_commit_pt(struct sae_data * sae,const struct sae_pt * pt,const u8 * addr1,const u8 * addr2,int * rejected_groups,const struct sae_pk * pk)1362c1d255d3SCy Schubert int sae_prepare_commit_pt(struct sae_data *sae, const struct sae_pt *pt,
1363c1d255d3SCy Schubert 			  const u8 *addr1, const u8 *addr2,
1364c1d255d3SCy Schubert 			  int *rejected_groups, const struct sae_pk *pk)
1365c1d255d3SCy Schubert {
1366c1d255d3SCy Schubert 	if (!sae->tmp)
1367c1d255d3SCy Schubert 		return -1;
1368c1d255d3SCy Schubert 
1369c1d255d3SCy Schubert 	while (pt) {
1370c1d255d3SCy Schubert 		if (pt->group == sae->group)
1371c1d255d3SCy Schubert 			break;
1372c1d255d3SCy Schubert 		pt = pt->next;
1373c1d255d3SCy Schubert 	}
1374c1d255d3SCy Schubert 	if (!pt) {
1375c1d255d3SCy Schubert 		wpa_printf(MSG_INFO, "SAE: Could not find PT for group %u",
1376c1d255d3SCy Schubert 			   sae->group);
1377c1d255d3SCy Schubert 		return -1;
1378c1d255d3SCy Schubert 	}
1379c1d255d3SCy Schubert 
1380c1d255d3SCy Schubert #ifdef CONFIG_SAE_PK
1381c1d255d3SCy Schubert 	os_memcpy(sae->tmp->ssid, pt->ssid, pt->ssid_len);
1382c1d255d3SCy Schubert 	sae->tmp->ssid_len = pt->ssid_len;
1383c1d255d3SCy Schubert 	sae->tmp->ap_pk = pk;
1384c1d255d3SCy Schubert #endif /* CONFIG_SAE_PK */
1385c1d255d3SCy Schubert 	sae->tmp->own_addr_higher = os_memcmp(addr1, addr2, ETH_ALEN) > 0;
1386c1d255d3SCy Schubert 	wpabuf_free(sae->tmp->own_rejected_groups);
1387c1d255d3SCy Schubert 	sae->tmp->own_rejected_groups = NULL;
1388c1d255d3SCy Schubert 	if (rejected_groups) {
1389c1d255d3SCy Schubert 		int count, i;
1390c1d255d3SCy Schubert 		struct wpabuf *groups;
1391c1d255d3SCy Schubert 
1392c1d255d3SCy Schubert 		count = int_array_len(rejected_groups);
1393c1d255d3SCy Schubert 		groups = wpabuf_alloc(count * 2);
1394c1d255d3SCy Schubert 		if (!groups)
1395c1d255d3SCy Schubert 			return -1;
1396c1d255d3SCy Schubert 		for (i = 0; i < count; i++)
1397c1d255d3SCy Schubert 			wpabuf_put_le16(groups, rejected_groups[i]);
1398c1d255d3SCy Schubert 		sae->tmp->own_rejected_groups = groups;
1399c1d255d3SCy Schubert 	}
1400c1d255d3SCy Schubert 
1401c1d255d3SCy Schubert 	if (pt->ec) {
1402c1d255d3SCy Schubert 		crypto_ec_point_deinit(sae->tmp->pwe_ecc, 1);
1403c1d255d3SCy Schubert 		sae->tmp->pwe_ecc = sae_derive_pwe_from_pt_ecc(pt, addr1,
1404c1d255d3SCy Schubert 							       addr2);
1405c1d255d3SCy Schubert 		if (!sae->tmp->pwe_ecc)
1406c1d255d3SCy Schubert 			return -1;
1407c1d255d3SCy Schubert 	}
1408c1d255d3SCy Schubert 
1409c1d255d3SCy Schubert 	if (pt->dh) {
1410c1d255d3SCy Schubert 		crypto_bignum_deinit(sae->tmp->pwe_ffc, 1);
1411c1d255d3SCy Schubert 		sae->tmp->pwe_ffc = sae_derive_pwe_from_pt_ffc(pt, addr1,
1412c1d255d3SCy Schubert 							       addr2);
1413c1d255d3SCy Schubert 		if (!sae->tmp->pwe_ffc)
1414c1d255d3SCy Schubert 			return -1;
1415c1d255d3SCy Schubert 	}
1416c1d255d3SCy Schubert 
1417c1d255d3SCy Schubert 	sae->h2e = 1;
1418c1d255d3SCy Schubert 	return sae_derive_commit(sae);
14195b9c547cSRui Paulo }
14205b9c547cSRui Paulo 
14215b9c547cSRui Paulo 
sae_derive_k_ecc(struct sae_data * sae,u8 * k)14225b9c547cSRui Paulo static int sae_derive_k_ecc(struct sae_data *sae, u8 *k)
14235b9c547cSRui Paulo {
14245b9c547cSRui Paulo 	struct crypto_ec_point *K;
14255b9c547cSRui Paulo 	int ret = -1;
14265b9c547cSRui Paulo 
14275b9c547cSRui Paulo 	K = crypto_ec_point_init(sae->tmp->ec);
14285b9c547cSRui Paulo 	if (K == NULL)
14295b9c547cSRui Paulo 		goto fail;
14305b9c547cSRui Paulo 
14315b9c547cSRui Paulo 	/*
14325b9c547cSRui Paulo 	 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
14335b9c547cSRui Paulo 	 *                                        PEER-COMMIT-ELEMENT)))
14345b9c547cSRui Paulo 	 * If K is identity element (point-at-infinity), reject
14355b9c547cSRui Paulo 	 * k = F(K) (= x coordinate)
14365b9c547cSRui Paulo 	 */
14375b9c547cSRui Paulo 
14385b9c547cSRui Paulo 	if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc,
14395b9c547cSRui Paulo 				sae->peer_commit_scalar, K) < 0 ||
14405b9c547cSRui Paulo 	    crypto_ec_point_add(sae->tmp->ec, K,
14415b9c547cSRui Paulo 				sae->tmp->peer_commit_element_ecc, K) < 0 ||
14425b9c547cSRui Paulo 	    crypto_ec_point_mul(sae->tmp->ec, K, sae->tmp->sae_rand, K) < 0 ||
14435b9c547cSRui Paulo 	    crypto_ec_point_is_at_infinity(sae->tmp->ec, K) ||
14445b9c547cSRui Paulo 	    crypto_ec_point_to_bin(sae->tmp->ec, K, k, NULL) < 0) {
14455b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
14465b9c547cSRui Paulo 		goto fail;
14475b9c547cSRui Paulo 	}
14485b9c547cSRui Paulo 
14495b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
14505b9c547cSRui Paulo 
14515b9c547cSRui Paulo 	ret = 0;
14525b9c547cSRui Paulo fail:
14535b9c547cSRui Paulo 	crypto_ec_point_deinit(K, 1);
14545b9c547cSRui Paulo 	return ret;
14555b9c547cSRui Paulo }
14565b9c547cSRui Paulo 
14575b9c547cSRui Paulo 
sae_derive_k_ffc(struct sae_data * sae,u8 * k)14585b9c547cSRui Paulo static int sae_derive_k_ffc(struct sae_data *sae, u8 *k)
14595b9c547cSRui Paulo {
14605b9c547cSRui Paulo 	struct crypto_bignum *K;
14615b9c547cSRui Paulo 	int ret = -1;
14625b9c547cSRui Paulo 
14635b9c547cSRui Paulo 	K = crypto_bignum_init();
14645b9c547cSRui Paulo 	if (K == NULL)
14655b9c547cSRui Paulo 		goto fail;
14665b9c547cSRui Paulo 
14675b9c547cSRui Paulo 	/*
14685b9c547cSRui Paulo 	 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
14695b9c547cSRui Paulo 	 *                                        PEER-COMMIT-ELEMENT)))
14705b9c547cSRui Paulo 	 * If K is identity element (one), reject.
14715b9c547cSRui Paulo 	 * k = F(K) (= x coordinate)
14725b9c547cSRui Paulo 	 */
14735b9c547cSRui Paulo 
14745b9c547cSRui Paulo 	if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, sae->peer_commit_scalar,
14755b9c547cSRui Paulo 				  sae->tmp->prime, K) < 0 ||
14765b9c547cSRui Paulo 	    crypto_bignum_mulmod(K, sae->tmp->peer_commit_element_ffc,
14775b9c547cSRui Paulo 				 sae->tmp->prime, K) < 0 ||
14785b9c547cSRui Paulo 	    crypto_bignum_exptmod(K, sae->tmp->sae_rand, sae->tmp->prime, K) < 0
14795b9c547cSRui Paulo 	    ||
14805b9c547cSRui Paulo 	    crypto_bignum_is_one(K) ||
14815b9c547cSRui Paulo 	    crypto_bignum_to_bin(K, k, SAE_MAX_PRIME_LEN, sae->tmp->prime_len) <
14825b9c547cSRui Paulo 	    0) {
14835b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
14845b9c547cSRui Paulo 		goto fail;
14855b9c547cSRui Paulo 	}
14865b9c547cSRui Paulo 
14875b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
14885b9c547cSRui Paulo 
14895b9c547cSRui Paulo 	ret = 0;
14905b9c547cSRui Paulo fail:
14915b9c547cSRui Paulo 	crypto_bignum_deinit(K, 1);
14925b9c547cSRui Paulo 	return ret;
14935b9c547cSRui Paulo }
14945b9c547cSRui Paulo 
14955b9c547cSRui Paulo 
sae_kdf_hash(size_t hash_len,const u8 * k,const char * label,const u8 * context,size_t context_len,u8 * out,size_t out_len)1496c1d255d3SCy Schubert static int sae_kdf_hash(size_t hash_len, const u8 *k, const char *label,
1497c1d255d3SCy Schubert 			const u8 *context, size_t context_len,
1498c1d255d3SCy Schubert 			u8 *out, size_t out_len)
1499c1d255d3SCy Schubert {
1500c1d255d3SCy Schubert 	if (hash_len == 32)
1501c1d255d3SCy Schubert 		return sha256_prf(k, hash_len, label,
1502c1d255d3SCy Schubert 				  context, context_len, out, out_len);
1503c1d255d3SCy Schubert #ifdef CONFIG_SHA384
1504c1d255d3SCy Schubert 	if (hash_len == 48)
1505c1d255d3SCy Schubert 		return sha384_prf(k, hash_len, label,
1506c1d255d3SCy Schubert 				  context, context_len, out, out_len);
1507c1d255d3SCy Schubert #endif /* CONFIG_SHA384 */
1508c1d255d3SCy Schubert #ifdef CONFIG_SHA512
1509c1d255d3SCy Schubert 	if (hash_len == 64)
1510c1d255d3SCy Schubert 		return sha512_prf(k, hash_len, label,
1511c1d255d3SCy Schubert 				  context, context_len, out, out_len);
1512c1d255d3SCy Schubert #endif /* CONFIG_SHA512 */
1513c1d255d3SCy Schubert 	return -1;
1514c1d255d3SCy Schubert }
1515c1d255d3SCy Schubert 
1516c1d255d3SCy Schubert 
sae_derive_keys(struct sae_data * sae,const u8 * k)15175b9c547cSRui Paulo static int sae_derive_keys(struct sae_data *sae, const u8 *k)
15185b9c547cSRui Paulo {
1519c1d255d3SCy Schubert 	u8 zero[SAE_MAX_HASH_LEN], val[SAE_MAX_PRIME_LEN];
1520c1d255d3SCy Schubert 	const u8 *salt;
1521c1d255d3SCy Schubert 	struct wpabuf *rejected_groups = NULL;
1522c1d255d3SCy Schubert 	u8 keyseed[SAE_MAX_HASH_LEN];
1523c1d255d3SCy Schubert 	u8 keys[2 * SAE_MAX_HASH_LEN + SAE_PMK_LEN];
15245b9c547cSRui Paulo 	struct crypto_bignum *tmp;
15255b9c547cSRui Paulo 	int ret = -1;
1526c1d255d3SCy Schubert 	size_t hash_len, salt_len, prime_len = sae->tmp->prime_len;
1527c1d255d3SCy Schubert 	const u8 *addr[1];
1528c1d255d3SCy Schubert 	size_t len[1];
15295b9c547cSRui Paulo 
15305b9c547cSRui Paulo 	tmp = crypto_bignum_init();
15315b9c547cSRui Paulo 	if (tmp == NULL)
15325b9c547cSRui Paulo 		goto fail;
15335b9c547cSRui Paulo 
1534c1d255d3SCy Schubert 	/* keyseed = H(salt, k)
1535c1d255d3SCy Schubert 	 * KCK || PMK = KDF-Hash-Length(keyseed, "SAE KCK and PMK",
15365b9c547cSRui Paulo 	 *                      (commit-scalar + peer-commit-scalar) modulo r)
15375b9c547cSRui Paulo 	 * PMKID = L((commit-scalar + peer-commit-scalar) modulo r, 0, 128)
1538c1d255d3SCy Schubert 	 *
1539c1d255d3SCy Schubert 	 * When SAE-PK is used,
1540c1d255d3SCy Schubert 	 * KCK || PMK || KEK = KDF-Hash-Length(keyseed, "SAE-PK keys", context)
15415b9c547cSRui Paulo 	 */
1542c1d255d3SCy Schubert 	if (!sae->h2e)
1543c1d255d3SCy Schubert 		hash_len = SHA256_MAC_LEN;
1544c1d255d3SCy Schubert 	else if (sae->tmp->dh)
1545c1d255d3SCy Schubert 		hash_len = sae_ffc_prime_len_2_hash_len(prime_len);
1546c1d255d3SCy Schubert 	else
1547c1d255d3SCy Schubert 		hash_len = sae_ecc_prime_len_2_hash_len(prime_len);
1548c1d255d3SCy Schubert 	if (sae->h2e && (sae->tmp->own_rejected_groups ||
1549c1d255d3SCy Schubert 			 sae->tmp->peer_rejected_groups)) {
1550c1d255d3SCy Schubert 		struct wpabuf *own, *peer;
15515b9c547cSRui Paulo 
1552c1d255d3SCy Schubert 		own = sae->tmp->own_rejected_groups;
1553c1d255d3SCy Schubert 		peer = sae->tmp->peer_rejected_groups;
1554c1d255d3SCy Schubert 		salt_len = 0;
1555c1d255d3SCy Schubert 		if (own)
1556c1d255d3SCy Schubert 			salt_len += wpabuf_len(own);
1557c1d255d3SCy Schubert 		if (peer)
1558c1d255d3SCy Schubert 			salt_len += wpabuf_len(peer);
1559c1d255d3SCy Schubert 		rejected_groups = wpabuf_alloc(salt_len);
1560c1d255d3SCy Schubert 		if (!rejected_groups)
1561c1d255d3SCy Schubert 			goto fail;
1562c1d255d3SCy Schubert 		if (sae->tmp->own_addr_higher) {
1563c1d255d3SCy Schubert 			if (own)
1564c1d255d3SCy Schubert 				wpabuf_put_buf(rejected_groups, own);
1565c1d255d3SCy Schubert 			if (peer)
1566c1d255d3SCy Schubert 				wpabuf_put_buf(rejected_groups, peer);
1567c1d255d3SCy Schubert 		} else {
1568c1d255d3SCy Schubert 			if (peer)
1569c1d255d3SCy Schubert 				wpabuf_put_buf(rejected_groups, peer);
1570c1d255d3SCy Schubert 			if (own)
1571c1d255d3SCy Schubert 				wpabuf_put_buf(rejected_groups, own);
1572c1d255d3SCy Schubert 		}
1573c1d255d3SCy Schubert 		salt = wpabuf_head(rejected_groups);
1574c1d255d3SCy Schubert 		salt_len = wpabuf_len(rejected_groups);
1575c1d255d3SCy Schubert 	} else {
1576c1d255d3SCy Schubert 		os_memset(zero, 0, hash_len);
1577c1d255d3SCy Schubert 		salt = zero;
1578c1d255d3SCy Schubert 		salt_len = hash_len;
1579c1d255d3SCy Schubert 	}
1580c1d255d3SCy Schubert 	wpa_hexdump(MSG_DEBUG, "SAE: salt for keyseed derivation",
1581c1d255d3SCy Schubert 		    salt, salt_len);
1582c1d255d3SCy Schubert 	addr[0] = k;
1583c1d255d3SCy Schubert 	len[0] = prime_len;
1584c1d255d3SCy Schubert 	if (hkdf_extract(hash_len, salt, salt_len, 1, addr, len, keyseed) < 0)
1585c1d255d3SCy Schubert 		goto fail;
1586c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: keyseed", keyseed, hash_len);
15875b9c547cSRui Paulo 
1588c1d255d3SCy Schubert 	if (crypto_bignum_add(sae->tmp->own_commit_scalar,
1589c1d255d3SCy Schubert 			      sae->peer_commit_scalar, tmp) < 0 ||
1590c1d255d3SCy Schubert 	    crypto_bignum_mod(tmp, sae->tmp->order, tmp) < 0)
1591c1d255d3SCy Schubert 		goto fail;
1592206b73d0SCy Schubert 	/* IEEE Std 802.11-2016 is not exactly clear on the encoding of the bit
1593206b73d0SCy Schubert 	 * string that is needed for KCK, PMK, and PMKID derivation, but it
1594206b73d0SCy Schubert 	 * seems to make most sense to encode the
1595206b73d0SCy Schubert 	 * (commit-scalar + peer-commit-scalar) mod r part as a bit string by
1596206b73d0SCy Schubert 	 * zero padding it from left to the length of the order (in full
1597206b73d0SCy Schubert 	 * octets). */
1598206b73d0SCy Schubert 	crypto_bignum_to_bin(tmp, val, sizeof(val), sae->tmp->order_len);
15995b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "SAE: PMKID", val, SAE_PMKID_LEN);
1600c1d255d3SCy Schubert 
1601c1d255d3SCy Schubert #ifdef CONFIG_SAE_PK
1602c1d255d3SCy Schubert 	if (sae->pk) {
1603c1d255d3SCy Schubert 		if (sae_kdf_hash(hash_len, keyseed, "SAE-PK keys",
1604c1d255d3SCy Schubert 				 val, sae->tmp->order_len,
1605c1d255d3SCy Schubert 				 keys, 2 * hash_len + SAE_PMK_LEN) < 0)
1606780fb4a2SCy Schubert 			goto fail;
1607c1d255d3SCy Schubert 	} else {
1608c1d255d3SCy Schubert 		if (sae_kdf_hash(hash_len, keyseed, "SAE KCK and PMK",
1609c1d255d3SCy Schubert 				 val, sae->tmp->order_len,
1610c1d255d3SCy Schubert 				 keys, hash_len + SAE_PMK_LEN) < 0)
1611c1d255d3SCy Schubert 			goto fail;
1612c1d255d3SCy Schubert 	}
1613c1d255d3SCy Schubert #else /* CONFIG_SAE_PK */
1614c1d255d3SCy Schubert 	if (sae_kdf_hash(hash_len, keyseed, "SAE KCK and PMK",
1615c1d255d3SCy Schubert 			 val, sae->tmp->order_len,
1616c1d255d3SCy Schubert 			 keys, hash_len + SAE_PMK_LEN) < 0)
1617c1d255d3SCy Schubert 		goto fail;
1618c1d255d3SCy Schubert #endif /* !CONFIG_SAE_PK */
1619c1d255d3SCy Schubert 
1620c1d255d3SCy Schubert 	forced_memzero(keyseed, sizeof(keyseed));
1621c1d255d3SCy Schubert 	os_memcpy(sae->tmp->kck, keys, hash_len);
1622c1d255d3SCy Schubert 	sae->tmp->kck_len = hash_len;
1623c1d255d3SCy Schubert 	os_memcpy(sae->pmk, keys + hash_len, SAE_PMK_LEN);
1624780fb4a2SCy Schubert 	os_memcpy(sae->pmkid, val, SAE_PMKID_LEN);
1625c1d255d3SCy Schubert #ifdef CONFIG_SAE_PK
1626c1d255d3SCy Schubert 	if (sae->pk) {
1627c1d255d3SCy Schubert 		os_memcpy(sae->tmp->kek, keys + hash_len + SAE_PMK_LEN,
1628c1d255d3SCy Schubert 			  hash_len);
1629c1d255d3SCy Schubert 		sae->tmp->kek_len = hash_len;
1630c1d255d3SCy Schubert 		wpa_hexdump_key(MSG_DEBUG, "SAE: KEK for SAE-PK",
1631c1d255d3SCy Schubert 				sae->tmp->kek, sae->tmp->kek_len);
1632c1d255d3SCy Schubert 	}
1633c1d255d3SCy Schubert #endif /* CONFIG_SAE_PK */
1634c1d255d3SCy Schubert 	forced_memzero(keys, sizeof(keys));
1635c1d255d3SCy Schubert 	wpa_hexdump_key(MSG_DEBUG, "SAE: KCK",
1636c1d255d3SCy Schubert 			sae->tmp->kck, sae->tmp->kck_len);
16375b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "SAE: PMK", sae->pmk, SAE_PMK_LEN);
16385b9c547cSRui Paulo 
16395b9c547cSRui Paulo 	ret = 0;
16405b9c547cSRui Paulo fail:
1641c1d255d3SCy Schubert 	wpabuf_free(rejected_groups);
16425b9c547cSRui Paulo 	crypto_bignum_deinit(tmp, 0);
16435b9c547cSRui Paulo 	return ret;
16445b9c547cSRui Paulo }
16455b9c547cSRui Paulo 
16465b9c547cSRui Paulo 
sae_process_commit(struct sae_data * sae)16475b9c547cSRui Paulo int sae_process_commit(struct sae_data *sae)
16485b9c547cSRui Paulo {
16495b9c547cSRui Paulo 	u8 k[SAE_MAX_PRIME_LEN];
16505b9c547cSRui Paulo 	if (sae->tmp == NULL ||
16515b9c547cSRui Paulo 	    (sae->tmp->ec && sae_derive_k_ecc(sae, k) < 0) ||
16525b9c547cSRui Paulo 	    (sae->tmp->dh && sae_derive_k_ffc(sae, k) < 0) ||
16535b9c547cSRui Paulo 	    sae_derive_keys(sae, k) < 0)
16545b9c547cSRui Paulo 		return -1;
16555b9c547cSRui Paulo 	return 0;
16565b9c547cSRui Paulo }
16575b9c547cSRui Paulo 
16585b9c547cSRui Paulo 
sae_write_commit(struct sae_data * sae,struct wpabuf * buf,const struct wpabuf * token,const char * identifier)1659c1d255d3SCy Schubert int sae_write_commit(struct sae_data *sae, struct wpabuf *buf,
166085732ac8SCy Schubert 		     const struct wpabuf *token, const char *identifier)
16615b9c547cSRui Paulo {
16625b9c547cSRui Paulo 	u8 *pos;
16635b9c547cSRui Paulo 
16645b9c547cSRui Paulo 	if (sae->tmp == NULL)
1665c1d255d3SCy Schubert 		return -1;
16665b9c547cSRui Paulo 
16675b9c547cSRui Paulo 	wpabuf_put_le16(buf, sae->group); /* Finite Cyclic Group */
1668c1d255d3SCy Schubert 	if (!sae->h2e && token) {
16695b9c547cSRui Paulo 		wpabuf_put_buf(buf, token);
16705b9c547cSRui Paulo 		wpa_hexdump(MSG_DEBUG, "SAE: Anti-clogging token",
16715b9c547cSRui Paulo 			    wpabuf_head(token), wpabuf_len(token));
16725b9c547cSRui Paulo 	}
16735b9c547cSRui Paulo 	pos = wpabuf_put(buf, sae->tmp->prime_len);
1674c1d255d3SCy Schubert 	if (crypto_bignum_to_bin(sae->tmp->own_commit_scalar, pos,
1675c1d255d3SCy Schubert 				 sae->tmp->prime_len, sae->tmp->prime_len) < 0)
1676c1d255d3SCy Schubert 		return -1;
16775b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "SAE: own commit-scalar",
16785b9c547cSRui Paulo 		    pos, sae->tmp->prime_len);
16795b9c547cSRui Paulo 	if (sae->tmp->ec) {
16805b9c547cSRui Paulo 		pos = wpabuf_put(buf, 2 * sae->tmp->prime_len);
1681c1d255d3SCy Schubert 		if (crypto_ec_point_to_bin(sae->tmp->ec,
16825b9c547cSRui Paulo 					   sae->tmp->own_commit_element_ecc,
1683c1d255d3SCy Schubert 					   pos, pos + sae->tmp->prime_len) < 0)
1684c1d255d3SCy Schubert 			return -1;
16855b9c547cSRui Paulo 		wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(x)",
16865b9c547cSRui Paulo 			    pos, sae->tmp->prime_len);
16875b9c547cSRui Paulo 		wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(y)",
16885b9c547cSRui Paulo 			    pos + sae->tmp->prime_len, sae->tmp->prime_len);
16895b9c547cSRui Paulo 	} else {
16905b9c547cSRui Paulo 		pos = wpabuf_put(buf, sae->tmp->prime_len);
1691c1d255d3SCy Schubert 		if (crypto_bignum_to_bin(sae->tmp->own_commit_element_ffc, pos,
1692c1d255d3SCy Schubert 					 sae->tmp->prime_len,
1693c1d255d3SCy Schubert 					 sae->tmp->prime_len) < 0)
1694c1d255d3SCy Schubert 			return -1;
16955b9c547cSRui Paulo 		wpa_hexdump(MSG_DEBUG, "SAE: own commit-element",
16965b9c547cSRui Paulo 			    pos, sae->tmp->prime_len);
16975b9c547cSRui Paulo 	}
169885732ac8SCy Schubert 
169985732ac8SCy Schubert 	if (identifier) {
170085732ac8SCy Schubert 		/* Password Identifier element */
170185732ac8SCy Schubert 		wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
170285732ac8SCy Schubert 		wpabuf_put_u8(buf, 1 + os_strlen(identifier));
170385732ac8SCy Schubert 		wpabuf_put_u8(buf, WLAN_EID_EXT_PASSWORD_IDENTIFIER);
170485732ac8SCy Schubert 		wpabuf_put_str(buf, identifier);
170585732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: own Password Identifier: %s",
170685732ac8SCy Schubert 			   identifier);
170785732ac8SCy Schubert 	}
1708c1d255d3SCy Schubert 
1709c1d255d3SCy Schubert 	if (sae->h2e && sae->tmp->own_rejected_groups) {
1710c1d255d3SCy Schubert 		wpa_hexdump_buf(MSG_DEBUG, "SAE: own Rejected Groups",
1711c1d255d3SCy Schubert 				sae->tmp->own_rejected_groups);
1712c1d255d3SCy Schubert 		wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
1713c1d255d3SCy Schubert 		wpabuf_put_u8(buf,
1714c1d255d3SCy Schubert 			      1 + wpabuf_len(sae->tmp->own_rejected_groups));
1715c1d255d3SCy Schubert 		wpabuf_put_u8(buf, WLAN_EID_EXT_REJECTED_GROUPS);
1716c1d255d3SCy Schubert 		wpabuf_put_buf(buf, sae->tmp->own_rejected_groups);
1717c1d255d3SCy Schubert 	}
1718c1d255d3SCy Schubert 
1719c1d255d3SCy Schubert 	if (sae->h2e && token) {
1720c1d255d3SCy Schubert 		wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
1721c1d255d3SCy Schubert 		wpabuf_put_u8(buf, 1 + wpabuf_len(token));
1722c1d255d3SCy Schubert 		wpabuf_put_u8(buf, WLAN_EID_EXT_ANTI_CLOGGING_TOKEN);
1723c1d255d3SCy Schubert 		wpabuf_put_buf(buf, token);
1724c1d255d3SCy Schubert 		wpa_hexdump_buf(MSG_DEBUG,
1725c1d255d3SCy Schubert 				"SAE: Anti-clogging token (in container)",
1726c1d255d3SCy Schubert 				token);
1727c1d255d3SCy Schubert 	}
1728c1d255d3SCy Schubert 
1729c1d255d3SCy Schubert 	return 0;
17305b9c547cSRui Paulo }
17315b9c547cSRui Paulo 
17325b9c547cSRui Paulo 
sae_group_allowed(struct sae_data * sae,int * allowed_groups,u16 group)17335b9c547cSRui Paulo u16 sae_group_allowed(struct sae_data *sae, int *allowed_groups, u16 group)
17345b9c547cSRui Paulo {
17355b9c547cSRui Paulo 	if (allowed_groups) {
17365b9c547cSRui Paulo 		int i;
17375b9c547cSRui Paulo 		for (i = 0; allowed_groups[i] > 0; i++) {
17385b9c547cSRui Paulo 			if (allowed_groups[i] == group)
17395b9c547cSRui Paulo 				break;
17405b9c547cSRui Paulo 		}
17415b9c547cSRui Paulo 		if (allowed_groups[i] != group) {
17425b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "SAE: Proposed group %u not "
17435b9c547cSRui Paulo 				   "enabled in the current configuration",
17445b9c547cSRui Paulo 				   group);
17455b9c547cSRui Paulo 			return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
17465b9c547cSRui Paulo 		}
17475b9c547cSRui Paulo 	}
17485b9c547cSRui Paulo 
17495b9c547cSRui Paulo 	if (sae->state == SAE_COMMITTED && group != sae->group) {
17505b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Do not allow group to be changed");
17515b9c547cSRui Paulo 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
17525b9c547cSRui Paulo 	}
17535b9c547cSRui Paulo 
17545b9c547cSRui Paulo 	if (group != sae->group && sae_set_group(sae, group) < 0) {
17555b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Unsupported Finite Cyclic Group %u",
17565b9c547cSRui Paulo 			   group);
17575b9c547cSRui Paulo 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
17585b9c547cSRui Paulo 	}
17595b9c547cSRui Paulo 
17605b9c547cSRui Paulo 	if (sae->tmp == NULL) {
17615b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Group information not yet initialized");
17625b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
17635b9c547cSRui Paulo 	}
17645b9c547cSRui Paulo 
17655b9c547cSRui Paulo 	if (sae->tmp->dh && !allowed_groups) {
17665b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Do not allow FFC group %u without "
17675b9c547cSRui Paulo 			   "explicit configuration enabling it", group);
17685b9c547cSRui Paulo 		return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
17695b9c547cSRui Paulo 	}
17705b9c547cSRui Paulo 
17715b9c547cSRui Paulo 	return WLAN_STATUS_SUCCESS;
17725b9c547cSRui Paulo }
17735b9c547cSRui Paulo 
17745b9c547cSRui Paulo 
sae_is_password_id_elem(const u8 * pos,const u8 * end)177585732ac8SCy Schubert static int sae_is_password_id_elem(const u8 *pos, const u8 *end)
177685732ac8SCy Schubert {
177785732ac8SCy Schubert 	return end - pos >= 3 &&
177885732ac8SCy Schubert 		pos[0] == WLAN_EID_EXTENSION &&
177985732ac8SCy Schubert 		pos[1] >= 1 &&
178085732ac8SCy Schubert 		end - pos - 2 >= pos[1] &&
178185732ac8SCy Schubert 		pos[2] == WLAN_EID_EXT_PASSWORD_IDENTIFIER;
178285732ac8SCy Schubert }
178385732ac8SCy Schubert 
178485732ac8SCy Schubert 
sae_is_rejected_groups_elem(const u8 * pos,const u8 * end)1785c1d255d3SCy Schubert static int sae_is_rejected_groups_elem(const u8 *pos, const u8 *end)
1786c1d255d3SCy Schubert {
1787c1d255d3SCy Schubert 	return end - pos >= 3 &&
1788c1d255d3SCy Schubert 		pos[0] == WLAN_EID_EXTENSION &&
1789c1d255d3SCy Schubert 		pos[1] >= 2 &&
1790c1d255d3SCy Schubert 		end - pos - 2 >= pos[1] &&
1791c1d255d3SCy Schubert 		pos[2] == WLAN_EID_EXT_REJECTED_GROUPS;
1792c1d255d3SCy Schubert }
1793c1d255d3SCy Schubert 
1794c1d255d3SCy Schubert 
sae_is_token_container_elem(const u8 * pos,const u8 * end)1795c1d255d3SCy Schubert static int sae_is_token_container_elem(const u8 *pos, const u8 *end)
1796c1d255d3SCy Schubert {
1797c1d255d3SCy Schubert 	return end - pos >= 3 &&
1798c1d255d3SCy Schubert 		pos[0] == WLAN_EID_EXTENSION &&
1799c1d255d3SCy Schubert 		pos[1] >= 1 &&
1800c1d255d3SCy Schubert 		end - pos - 2 >= pos[1] &&
1801c1d255d3SCy Schubert 		pos[2] == WLAN_EID_EXT_ANTI_CLOGGING_TOKEN;
1802c1d255d3SCy Schubert }
1803c1d255d3SCy Schubert 
1804c1d255d3SCy Schubert 
sae_parse_commit_token(struct sae_data * sae,const u8 ** pos,const u8 * end,const u8 ** token,size_t * token_len,int h2e)18055b9c547cSRui Paulo static void sae_parse_commit_token(struct sae_data *sae, const u8 **pos,
18065b9c547cSRui Paulo 				   const u8 *end, const u8 **token,
1807c1d255d3SCy Schubert 				   size_t *token_len, int h2e)
18085b9c547cSRui Paulo {
180985732ac8SCy Schubert 	size_t scalar_elem_len, tlen;
181085732ac8SCy Schubert 
181185732ac8SCy Schubert 	if (token)
181285732ac8SCy Schubert 		*token = NULL;
181385732ac8SCy Schubert 	if (token_len)
181485732ac8SCy Schubert 		*token_len = 0;
181585732ac8SCy Schubert 
1816c1d255d3SCy Schubert 	if (h2e)
1817c1d255d3SCy Schubert 		return; /* No Anti-Clogging Token field outside container IE */
1818c1d255d3SCy Schubert 
181985732ac8SCy Schubert 	scalar_elem_len = (sae->tmp->ec ? 3 : 2) * sae->tmp->prime_len;
182085732ac8SCy Schubert 	if (scalar_elem_len >= (size_t) (end - *pos))
182185732ac8SCy Schubert 		return; /* No extra data beyond peer scalar and element */
182285732ac8SCy Schubert 
182385732ac8SCy Schubert 	tlen = end - (*pos + scalar_elem_len);
182485732ac8SCy Schubert 
182585732ac8SCy Schubert 	if (tlen < SHA256_MAC_LEN) {
182685732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
182785732ac8SCy Schubert 			   "SAE: Too short optional data (%u octets) to include our Anti-Clogging Token",
182885732ac8SCy Schubert 			   (unsigned int) tlen);
182985732ac8SCy Schubert 		return;
183085732ac8SCy Schubert 	}
183185732ac8SCy Schubert 
18325b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "SAE: Anti-Clogging Token", *pos, tlen);
18335b9c547cSRui Paulo 	if (token)
18345b9c547cSRui Paulo 		*token = *pos;
18355b9c547cSRui Paulo 	if (token_len)
18365b9c547cSRui Paulo 		*token_len = tlen;
18375b9c547cSRui Paulo 	*pos += tlen;
18385b9c547cSRui Paulo }
18395b9c547cSRui Paulo 
18405b9c547cSRui Paulo 
sae_parse_token_container(struct sae_data * sae,const u8 * pos,const u8 * end,const u8 ** token,size_t * token_len)1841c1d255d3SCy Schubert static void sae_parse_token_container(struct sae_data *sae,
1842c1d255d3SCy Schubert 				      const u8 *pos, const u8 *end,
1843c1d255d3SCy Schubert 				      const u8 **token, size_t *token_len)
1844c1d255d3SCy Schubert {
1845c1d255d3SCy Schubert 	wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
1846c1d255d3SCy Schubert 		    pos, end - pos);
1847c1d255d3SCy Schubert 	if (!sae_is_token_container_elem(pos, end))
1848c1d255d3SCy Schubert 		return;
1849c1d255d3SCy Schubert 	*token = pos + 3;
1850c1d255d3SCy Schubert 	*token_len = pos[1] - 1;
1851c1d255d3SCy Schubert 	wpa_hexdump(MSG_DEBUG, "SAE: Anti-Clogging Token (in container)",
1852c1d255d3SCy Schubert 		    *token, *token_len);
1853c1d255d3SCy Schubert }
1854c1d255d3SCy Schubert 
1855c1d255d3SCy Schubert 
sae_parse_commit_scalar(struct sae_data * sae,const u8 ** pos,const u8 * end)18565b9c547cSRui Paulo static u16 sae_parse_commit_scalar(struct sae_data *sae, const u8 **pos,
18575b9c547cSRui Paulo 				   const u8 *end)
18585b9c547cSRui Paulo {
18595b9c547cSRui Paulo 	struct crypto_bignum *peer_scalar;
18605b9c547cSRui Paulo 
1861780fb4a2SCy Schubert 	if (sae->tmp->prime_len > end - *pos) {
18625b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Not enough data for scalar");
18635b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
18645b9c547cSRui Paulo 	}
18655b9c547cSRui Paulo 
18665b9c547cSRui Paulo 	peer_scalar = crypto_bignum_init_set(*pos, sae->tmp->prime_len);
18675b9c547cSRui Paulo 	if (peer_scalar == NULL)
18685b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
18695b9c547cSRui Paulo 
18705b9c547cSRui Paulo 	/*
18715b9c547cSRui Paulo 	 * IEEE Std 802.11-2012, 11.3.8.6.1: If there is a protocol instance for
18725b9c547cSRui Paulo 	 * the peer and it is in Authenticated state, the new Commit Message
18735b9c547cSRui Paulo 	 * shall be dropped if the peer-scalar is identical to the one used in
18745b9c547cSRui Paulo 	 * the existing protocol instance.
18755b9c547cSRui Paulo 	 */
1876c1d255d3SCy Schubert 	if (sae->state == SAE_ACCEPTED && sae->peer_commit_scalar_accepted &&
1877c1d255d3SCy Schubert 	    crypto_bignum_cmp(sae->peer_commit_scalar_accepted,
1878c1d255d3SCy Schubert 			      peer_scalar) == 0) {
18795b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Do not accept re-use of previous "
18805b9c547cSRui Paulo 			   "peer-commit-scalar");
18815b9c547cSRui Paulo 		crypto_bignum_deinit(peer_scalar, 0);
18825b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
18835b9c547cSRui Paulo 	}
18845b9c547cSRui Paulo 
1885325151a3SRui Paulo 	/* 1 < scalar < r */
18865b9c547cSRui Paulo 	if (crypto_bignum_is_zero(peer_scalar) ||
1887325151a3SRui Paulo 	    crypto_bignum_is_one(peer_scalar) ||
18885b9c547cSRui Paulo 	    crypto_bignum_cmp(peer_scalar, sae->tmp->order) >= 0) {
18895b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Invalid peer scalar");
18905b9c547cSRui Paulo 		crypto_bignum_deinit(peer_scalar, 0);
18915b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
18925b9c547cSRui Paulo 	}
18935b9c547cSRui Paulo 
18945b9c547cSRui Paulo 
18955b9c547cSRui Paulo 	crypto_bignum_deinit(sae->peer_commit_scalar, 0);
18965b9c547cSRui Paulo 	sae->peer_commit_scalar = peer_scalar;
18975b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-scalar",
18985b9c547cSRui Paulo 		    *pos, sae->tmp->prime_len);
18995b9c547cSRui Paulo 	*pos += sae->tmp->prime_len;
19005b9c547cSRui Paulo 
19015b9c547cSRui Paulo 	return WLAN_STATUS_SUCCESS;
19025b9c547cSRui Paulo }
19035b9c547cSRui Paulo 
19045b9c547cSRui Paulo 
sae_parse_commit_element_ecc(struct sae_data * sae,const u8 ** pos,const u8 * end)190585732ac8SCy Schubert static u16 sae_parse_commit_element_ecc(struct sae_data *sae, const u8 **pos,
19065b9c547cSRui Paulo 					const u8 *end)
19075b9c547cSRui Paulo {
19085b9c547cSRui Paulo 	u8 prime[SAE_MAX_ECC_PRIME_LEN];
19095b9c547cSRui Paulo 
191085732ac8SCy Schubert 	if (2 * sae->tmp->prime_len > end - *pos) {
19115b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
19125b9c547cSRui Paulo 			   "commit-element");
19135b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
19145b9c547cSRui Paulo 	}
19155b9c547cSRui Paulo 
19165b9c547cSRui Paulo 	if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
19175b9c547cSRui Paulo 				 sae->tmp->prime_len) < 0)
19185b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
19195b9c547cSRui Paulo 
19205b9c547cSRui Paulo 	/* element x and y coordinates < p */
192185732ac8SCy Schubert 	if (os_memcmp(*pos, prime, sae->tmp->prime_len) >= 0 ||
192285732ac8SCy Schubert 	    os_memcmp(*pos + sae->tmp->prime_len, prime,
19235b9c547cSRui Paulo 		      sae->tmp->prime_len) >= 0) {
19245b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Invalid coordinates in peer "
19255b9c547cSRui Paulo 			   "element");
19265b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
19275b9c547cSRui Paulo 	}
19285b9c547cSRui Paulo 
19295b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(x)",
193085732ac8SCy Schubert 		    *pos, sae->tmp->prime_len);
19315b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(y)",
193285732ac8SCy Schubert 		    *pos + sae->tmp->prime_len, sae->tmp->prime_len);
19335b9c547cSRui Paulo 
19345b9c547cSRui Paulo 	crypto_ec_point_deinit(sae->tmp->peer_commit_element_ecc, 0);
19355b9c547cSRui Paulo 	sae->tmp->peer_commit_element_ecc =
193685732ac8SCy Schubert 		crypto_ec_point_from_bin(sae->tmp->ec, *pos);
19375b9c547cSRui Paulo 	if (sae->tmp->peer_commit_element_ecc == NULL)
19385b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
19395b9c547cSRui Paulo 
19405b9c547cSRui Paulo 	if (!crypto_ec_point_is_on_curve(sae->tmp->ec,
19415b9c547cSRui Paulo 					 sae->tmp->peer_commit_element_ecc)) {
19425b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Peer element is not on curve");
19435b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
19445b9c547cSRui Paulo 	}
19455b9c547cSRui Paulo 
194685732ac8SCy Schubert 	*pos += 2 * sae->tmp->prime_len;
194785732ac8SCy Schubert 
19485b9c547cSRui Paulo 	return WLAN_STATUS_SUCCESS;
19495b9c547cSRui Paulo }
19505b9c547cSRui Paulo 
19515b9c547cSRui Paulo 
sae_parse_commit_element_ffc(struct sae_data * sae,const u8 ** pos,const u8 * end)195285732ac8SCy Schubert static u16 sae_parse_commit_element_ffc(struct sae_data *sae, const u8 **pos,
19535b9c547cSRui Paulo 					const u8 *end)
19545b9c547cSRui Paulo {
1955325151a3SRui Paulo 	struct crypto_bignum *res, *one;
1956325151a3SRui Paulo 	const u8 one_bin[1] = { 0x01 };
19575b9c547cSRui Paulo 
195885732ac8SCy Schubert 	if (sae->tmp->prime_len > end - *pos) {
19595b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
19605b9c547cSRui Paulo 			   "commit-element");
19615b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
19625b9c547cSRui Paulo 	}
196385732ac8SCy Schubert 	wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element", *pos,
19645b9c547cSRui Paulo 		    sae->tmp->prime_len);
19655b9c547cSRui Paulo 
19665b9c547cSRui Paulo 	crypto_bignum_deinit(sae->tmp->peer_commit_element_ffc, 0);
19675b9c547cSRui Paulo 	sae->tmp->peer_commit_element_ffc =
196885732ac8SCy Schubert 		crypto_bignum_init_set(*pos, sae->tmp->prime_len);
19695b9c547cSRui Paulo 	if (sae->tmp->peer_commit_element_ffc == NULL)
19705b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
1971325151a3SRui Paulo 	/* 1 < element < p - 1 */
1972325151a3SRui Paulo 	res = crypto_bignum_init();
1973325151a3SRui Paulo 	one = crypto_bignum_init_set(one_bin, sizeof(one_bin));
1974325151a3SRui Paulo 	if (!res || !one ||
1975325151a3SRui Paulo 	    crypto_bignum_sub(sae->tmp->prime, one, res) ||
1976325151a3SRui Paulo 	    crypto_bignum_is_zero(sae->tmp->peer_commit_element_ffc) ||
19775b9c547cSRui Paulo 	    crypto_bignum_is_one(sae->tmp->peer_commit_element_ffc) ||
1978325151a3SRui Paulo 	    crypto_bignum_cmp(sae->tmp->peer_commit_element_ffc, res) >= 0) {
1979325151a3SRui Paulo 		crypto_bignum_deinit(res, 0);
1980325151a3SRui Paulo 		crypto_bignum_deinit(one, 0);
19815b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Invalid peer element");
19825b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
19835b9c547cSRui Paulo 	}
1984325151a3SRui Paulo 	crypto_bignum_deinit(one, 0);
19855b9c547cSRui Paulo 
19865b9c547cSRui Paulo 	/* scalar-op(r, ELEMENT) = 1 modulo p */
1987325151a3SRui Paulo 	if (crypto_bignum_exptmod(sae->tmp->peer_commit_element_ffc,
19885b9c547cSRui Paulo 				  sae->tmp->order, sae->tmp->prime, res) < 0 ||
19895b9c547cSRui Paulo 	    !crypto_bignum_is_one(res)) {
19905b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Invalid peer element (scalar-op)");
19915b9c547cSRui Paulo 		crypto_bignum_deinit(res, 0);
19925b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
19935b9c547cSRui Paulo 	}
19945b9c547cSRui Paulo 	crypto_bignum_deinit(res, 0);
19955b9c547cSRui Paulo 
199685732ac8SCy Schubert 	*pos += sae->tmp->prime_len;
199785732ac8SCy Schubert 
19985b9c547cSRui Paulo 	return WLAN_STATUS_SUCCESS;
19995b9c547cSRui Paulo }
20005b9c547cSRui Paulo 
20015b9c547cSRui Paulo 
sae_parse_commit_element(struct sae_data * sae,const u8 ** pos,const u8 * end)200285732ac8SCy Schubert static u16 sae_parse_commit_element(struct sae_data *sae, const u8 **pos,
20035b9c547cSRui Paulo 				    const u8 *end)
20045b9c547cSRui Paulo {
20055b9c547cSRui Paulo 	if (sae->tmp->dh)
20065b9c547cSRui Paulo 		return sae_parse_commit_element_ffc(sae, pos, end);
20075b9c547cSRui Paulo 	return sae_parse_commit_element_ecc(sae, pos, end);
20085b9c547cSRui Paulo }
20095b9c547cSRui Paulo 
20105b9c547cSRui Paulo 
sae_parse_password_identifier(struct sae_data * sae,const u8 ** pos,const u8 * end)201185732ac8SCy Schubert static int sae_parse_password_identifier(struct sae_data *sae,
2012c1d255d3SCy Schubert 					 const u8 **pos, const u8 *end)
201385732ac8SCy Schubert {
2014c1d255d3SCy Schubert 	const u8 *epos;
2015c1d255d3SCy Schubert 	u8 len;
2016c1d255d3SCy Schubert 
201785732ac8SCy Schubert 	wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
2018c1d255d3SCy Schubert 		    *pos, end - *pos);
2019c1d255d3SCy Schubert 	if (!sae_is_password_id_elem(*pos, end)) {
202085732ac8SCy Schubert 		if (sae->tmp->pw_id) {
202185732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
202285732ac8SCy Schubert 				   "SAE: No Password Identifier included, but expected one (%s)",
202385732ac8SCy Schubert 				   sae->tmp->pw_id);
202485732ac8SCy Schubert 			return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
202585732ac8SCy Schubert 		}
202685732ac8SCy Schubert 		os_free(sae->tmp->pw_id);
202785732ac8SCy Schubert 		sae->tmp->pw_id = NULL;
202885732ac8SCy Schubert 		return WLAN_STATUS_SUCCESS; /* No Password Identifier */
202985732ac8SCy Schubert 	}
203085732ac8SCy Schubert 
2031c1d255d3SCy Schubert 	epos = *pos;
2032c1d255d3SCy Schubert 	epos++; /* skip IE type */
2033c1d255d3SCy Schubert 	len = *epos++; /* IE length */
2034c1d255d3SCy Schubert 	if (len > end - epos || len < 1)
2035c1d255d3SCy Schubert 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2036c1d255d3SCy Schubert 	epos++; /* skip ext ID */
2037c1d255d3SCy Schubert 	len--;
2038c1d255d3SCy Schubert 
203985732ac8SCy Schubert 	if (sae->tmp->pw_id &&
2040c1d255d3SCy Schubert 	    (len != os_strlen(sae->tmp->pw_id) ||
2041c1d255d3SCy Schubert 	     os_memcmp(sae->tmp->pw_id, epos, len) != 0)) {
204285732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
204385732ac8SCy Schubert 			   "SAE: The included Password Identifier does not match the expected one (%s)",
204485732ac8SCy Schubert 			   sae->tmp->pw_id);
204585732ac8SCy Schubert 		return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
204685732ac8SCy Schubert 	}
204785732ac8SCy Schubert 
204885732ac8SCy Schubert 	os_free(sae->tmp->pw_id);
2049c1d255d3SCy Schubert 	sae->tmp->pw_id = os_malloc(len + 1);
205085732ac8SCy Schubert 	if (!sae->tmp->pw_id)
205185732ac8SCy Schubert 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2052c1d255d3SCy Schubert 	os_memcpy(sae->tmp->pw_id, epos, len);
2053c1d255d3SCy Schubert 	sae->tmp->pw_id[len] = '\0';
205485732ac8SCy Schubert 	wpa_hexdump_ascii(MSG_DEBUG, "SAE: Received Password Identifier",
2055c1d255d3SCy Schubert 			  sae->tmp->pw_id, len);
2056c1d255d3SCy Schubert 	*pos = epos + len;
2057c1d255d3SCy Schubert 	return WLAN_STATUS_SUCCESS;
2058c1d255d3SCy Schubert }
2059c1d255d3SCy Schubert 
2060c1d255d3SCy Schubert 
sae_parse_rejected_groups(struct sae_data * sae,const u8 ** pos,const u8 * end)2061c1d255d3SCy Schubert static int sae_parse_rejected_groups(struct sae_data *sae,
2062c1d255d3SCy Schubert 				     const u8 **pos, const u8 *end)
2063c1d255d3SCy Schubert {
2064c1d255d3SCy Schubert 	const u8 *epos;
2065c1d255d3SCy Schubert 	u8 len;
2066c1d255d3SCy Schubert 
2067c1d255d3SCy Schubert 	wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
2068c1d255d3SCy Schubert 		    *pos, end - *pos);
2069c1d255d3SCy Schubert 	if (!sae_is_rejected_groups_elem(*pos, end))
2070c1d255d3SCy Schubert 		return WLAN_STATUS_SUCCESS;
2071c1d255d3SCy Schubert 
2072c1d255d3SCy Schubert 	epos = *pos;
2073c1d255d3SCy Schubert 	epos++; /* skip IE type */
2074c1d255d3SCy Schubert 	len = *epos++; /* IE length */
2075c1d255d3SCy Schubert 	if (len > end - epos || len < 1)
2076c1d255d3SCy Schubert 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2077c1d255d3SCy Schubert 	epos++; /* skip ext ID */
2078c1d255d3SCy Schubert 	len--;
2079c1d255d3SCy Schubert 
2080c1d255d3SCy Schubert 	wpabuf_free(sae->tmp->peer_rejected_groups);
2081c1d255d3SCy Schubert 	sae->tmp->peer_rejected_groups = wpabuf_alloc(len);
2082c1d255d3SCy Schubert 	if (!sae->tmp->peer_rejected_groups)
2083c1d255d3SCy Schubert 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
2084c1d255d3SCy Schubert 	wpabuf_put_data(sae->tmp->peer_rejected_groups, epos, len);
2085c1d255d3SCy Schubert 	wpa_hexdump_buf(MSG_DEBUG, "SAE: Received Rejected Groups list",
2086c1d255d3SCy Schubert 			sae->tmp->peer_rejected_groups);
2087c1d255d3SCy Schubert 	*pos = epos + len;
208885732ac8SCy Schubert 	return WLAN_STATUS_SUCCESS;
208985732ac8SCy Schubert }
209085732ac8SCy Schubert 
209185732ac8SCy Schubert 
sae_parse_commit(struct sae_data * sae,const u8 * data,size_t len,const u8 ** token,size_t * token_len,int * allowed_groups,int h2e)20925b9c547cSRui Paulo u16 sae_parse_commit(struct sae_data *sae, const u8 *data, size_t len,
2093c1d255d3SCy Schubert 		     const u8 **token, size_t *token_len, int *allowed_groups,
2094c1d255d3SCy Schubert 		     int h2e)
20955b9c547cSRui Paulo {
20965b9c547cSRui Paulo 	const u8 *pos = data, *end = data + len;
20975b9c547cSRui Paulo 	u16 res;
20985b9c547cSRui Paulo 
20995b9c547cSRui Paulo 	/* Check Finite Cyclic Group */
2100780fb4a2SCy Schubert 	if (end - pos < 2)
21015b9c547cSRui Paulo 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
21025b9c547cSRui Paulo 	res = sae_group_allowed(sae, allowed_groups, WPA_GET_LE16(pos));
21035b9c547cSRui Paulo 	if (res != WLAN_STATUS_SUCCESS)
21045b9c547cSRui Paulo 		return res;
21055b9c547cSRui Paulo 	pos += 2;
21065b9c547cSRui Paulo 
21075b9c547cSRui Paulo 	/* Optional Anti-Clogging Token */
2108c1d255d3SCy Schubert 	sae_parse_commit_token(sae, &pos, end, token, token_len, h2e);
21095b9c547cSRui Paulo 
21105b9c547cSRui Paulo 	/* commit-scalar */
21115b9c547cSRui Paulo 	res = sae_parse_commit_scalar(sae, &pos, end);
21125b9c547cSRui Paulo 	if (res != WLAN_STATUS_SUCCESS)
21135b9c547cSRui Paulo 		return res;
21145b9c547cSRui Paulo 
21155b9c547cSRui Paulo 	/* commit-element */
211685732ac8SCy Schubert 	res = sae_parse_commit_element(sae, &pos, end);
211785732ac8SCy Schubert 	if (res != WLAN_STATUS_SUCCESS)
211885732ac8SCy Schubert 		return res;
211985732ac8SCy Schubert 
212085732ac8SCy Schubert 	/* Optional Password Identifier element */
2121c1d255d3SCy Schubert 	res = sae_parse_password_identifier(sae, &pos, end);
2122325151a3SRui Paulo 	if (res != WLAN_STATUS_SUCCESS)
2123325151a3SRui Paulo 		return res;
2124325151a3SRui Paulo 
2125c1d255d3SCy Schubert 	/* Conditional Rejected Groups element */
2126c1d255d3SCy Schubert 	if (h2e) {
2127c1d255d3SCy Schubert 		res = sae_parse_rejected_groups(sae, &pos, end);
2128c1d255d3SCy Schubert 		if (res != WLAN_STATUS_SUCCESS)
2129c1d255d3SCy Schubert 			return res;
2130c1d255d3SCy Schubert 	}
2131c1d255d3SCy Schubert 
2132c1d255d3SCy Schubert 	/* Optional Anti-Clogging Token Container element */
2133c1d255d3SCy Schubert 	if (h2e)
2134c1d255d3SCy Schubert 		sae_parse_token_container(sae, pos, end, token, token_len);
2135c1d255d3SCy Schubert 
2136325151a3SRui Paulo 	/*
2137325151a3SRui Paulo 	 * Check whether peer-commit-scalar and PEER-COMMIT-ELEMENT are same as
2138325151a3SRui Paulo 	 * the values we sent which would be evidence of a reflection attack.
2139325151a3SRui Paulo 	 */
2140325151a3SRui Paulo 	if (!sae->tmp->own_commit_scalar ||
2141325151a3SRui Paulo 	    crypto_bignum_cmp(sae->tmp->own_commit_scalar,
2142325151a3SRui Paulo 			      sae->peer_commit_scalar) != 0 ||
2143325151a3SRui Paulo 	    (sae->tmp->dh &&
2144325151a3SRui Paulo 	     (!sae->tmp->own_commit_element_ffc ||
2145325151a3SRui Paulo 	      crypto_bignum_cmp(sae->tmp->own_commit_element_ffc,
2146325151a3SRui Paulo 				sae->tmp->peer_commit_element_ffc) != 0)) ||
2147325151a3SRui Paulo 	    (sae->tmp->ec &&
2148325151a3SRui Paulo 	     (!sae->tmp->own_commit_element_ecc ||
2149325151a3SRui Paulo 	      crypto_ec_point_cmp(sae->tmp->ec,
2150325151a3SRui Paulo 				  sae->tmp->own_commit_element_ecc,
2151325151a3SRui Paulo 				  sae->tmp->peer_commit_element_ecc) != 0)))
2152325151a3SRui Paulo 		return WLAN_STATUS_SUCCESS; /* scalars/elements are different */
2153325151a3SRui Paulo 
2154325151a3SRui Paulo 	/*
2155325151a3SRui Paulo 	 * This is a reflection attack - return special value to trigger caller
2156325151a3SRui Paulo 	 * to silently discard the frame instead of replying with a specific
2157325151a3SRui Paulo 	 * status code.
2158325151a3SRui Paulo 	 */
2159325151a3SRui Paulo 	return SAE_SILENTLY_DISCARD;
21605b9c547cSRui Paulo }
21615b9c547cSRui Paulo 
21625b9c547cSRui Paulo 
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)2163c1d255d3SCy Schubert static int sae_cn_confirm(struct sae_data *sae, const u8 *sc,
21645b9c547cSRui Paulo 			  const struct crypto_bignum *scalar1,
21655b9c547cSRui Paulo 			  const u8 *element1, size_t element1_len,
21665b9c547cSRui Paulo 			  const struct crypto_bignum *scalar2,
21675b9c547cSRui Paulo 			  const u8 *element2, size_t element2_len,
21685b9c547cSRui Paulo 			  u8 *confirm)
21695b9c547cSRui Paulo {
21705b9c547cSRui Paulo 	const u8 *addr[5];
21715b9c547cSRui Paulo 	size_t len[5];
21725b9c547cSRui Paulo 	u8 scalar_b1[SAE_MAX_PRIME_LEN], scalar_b2[SAE_MAX_PRIME_LEN];
21735b9c547cSRui Paulo 
21745b9c547cSRui Paulo 	/* Confirm
21755b9c547cSRui Paulo 	 * CN(key, X, Y, Z, ...) =
21765b9c547cSRui Paulo 	 *    HMAC-SHA256(key, D2OS(X) || D2OS(Y) || D2OS(Z) | ...)
21775b9c547cSRui Paulo 	 * confirm = CN(KCK, send-confirm, commit-scalar, COMMIT-ELEMENT,
21785b9c547cSRui Paulo 	 *              peer-commit-scalar, PEER-COMMIT-ELEMENT)
21795b9c547cSRui Paulo 	 * verifier = CN(KCK, peer-send-confirm, peer-commit-scalar,
21805b9c547cSRui Paulo 	 *               PEER-COMMIT-ELEMENT, commit-scalar, COMMIT-ELEMENT)
21815b9c547cSRui Paulo 	 */
2182c1d255d3SCy Schubert 	if (crypto_bignum_to_bin(scalar1, scalar_b1, sizeof(scalar_b1),
2183c1d255d3SCy Schubert 				 sae->tmp->prime_len) < 0 ||
2184c1d255d3SCy Schubert 	    crypto_bignum_to_bin(scalar2, scalar_b2, sizeof(scalar_b2),
2185c1d255d3SCy Schubert 				 sae->tmp->prime_len) < 0)
2186c1d255d3SCy Schubert 		return -1;
21875b9c547cSRui Paulo 	addr[0] = sc;
21885b9c547cSRui Paulo 	len[0] = 2;
21895b9c547cSRui Paulo 	addr[1] = scalar_b1;
21905b9c547cSRui Paulo 	len[1] = sae->tmp->prime_len;
21915b9c547cSRui Paulo 	addr[2] = element1;
21925b9c547cSRui Paulo 	len[2] = element1_len;
21935b9c547cSRui Paulo 	addr[3] = scalar_b2;
21945b9c547cSRui Paulo 	len[3] = sae->tmp->prime_len;
21955b9c547cSRui Paulo 	addr[4] = element2;
21965b9c547cSRui Paulo 	len[4] = element2_len;
2197c1d255d3SCy Schubert 	return hkdf_extract(sae->tmp->kck_len, sae->tmp->kck, sae->tmp->kck_len,
2198c1d255d3SCy Schubert 			    5, addr, len, confirm);
21995b9c547cSRui Paulo }
22005b9c547cSRui Paulo 
22015b9c547cSRui Paulo 
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)2202c1d255d3SCy Schubert static int sae_cn_confirm_ecc(struct sae_data *sae, const u8 *sc,
22035b9c547cSRui Paulo 			      const struct crypto_bignum *scalar1,
22045b9c547cSRui Paulo 			      const struct crypto_ec_point *element1,
22055b9c547cSRui Paulo 			      const struct crypto_bignum *scalar2,
22065b9c547cSRui Paulo 			      const struct crypto_ec_point *element2,
22075b9c547cSRui Paulo 			      u8 *confirm)
22085b9c547cSRui Paulo {
22095b9c547cSRui Paulo 	u8 element_b1[2 * SAE_MAX_ECC_PRIME_LEN];
22105b9c547cSRui Paulo 	u8 element_b2[2 * SAE_MAX_ECC_PRIME_LEN];
22115b9c547cSRui Paulo 
2212c1d255d3SCy Schubert 	if (crypto_ec_point_to_bin(sae->tmp->ec, element1, element_b1,
2213c1d255d3SCy Schubert 				   element_b1 + sae->tmp->prime_len) < 0 ||
22145b9c547cSRui Paulo 	    crypto_ec_point_to_bin(sae->tmp->ec, element2, element_b2,
2215c1d255d3SCy Schubert 				   element_b2 + sae->tmp->prime_len) < 0 ||
2216c1d255d3SCy Schubert 	    sae_cn_confirm(sae, sc, scalar1, element_b1,
2217c1d255d3SCy Schubert 			   2 * sae->tmp->prime_len,
2218c1d255d3SCy Schubert 			   scalar2, element_b2, 2 * sae->tmp->prime_len,
2219c1d255d3SCy Schubert 			   confirm) < 0)
2220c1d255d3SCy Schubert 		return -1;
2221c1d255d3SCy Schubert 	return 0;
22225b9c547cSRui Paulo }
22235b9c547cSRui Paulo 
22245b9c547cSRui Paulo 
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)2225c1d255d3SCy Schubert static int sae_cn_confirm_ffc(struct sae_data *sae, const u8 *sc,
22265b9c547cSRui Paulo 			      const struct crypto_bignum *scalar1,
22275b9c547cSRui Paulo 			      const struct crypto_bignum *element1,
22285b9c547cSRui Paulo 			      const struct crypto_bignum *scalar2,
22295b9c547cSRui Paulo 			      const struct crypto_bignum *element2,
22305b9c547cSRui Paulo 			      u8 *confirm)
22315b9c547cSRui Paulo {
22325b9c547cSRui Paulo 	u8 element_b1[SAE_MAX_PRIME_LEN];
22335b9c547cSRui Paulo 	u8 element_b2[SAE_MAX_PRIME_LEN];
22345b9c547cSRui Paulo 
2235c1d255d3SCy Schubert 	if (crypto_bignum_to_bin(element1, element_b1, sizeof(element_b1),
2236c1d255d3SCy Schubert 				 sae->tmp->prime_len) < 0 ||
22375b9c547cSRui Paulo 	    crypto_bignum_to_bin(element2, element_b2, sizeof(element_b2),
2238c1d255d3SCy Schubert 				 sae->tmp->prime_len) < 0 ||
22395b9c547cSRui Paulo 	    sae_cn_confirm(sae, sc, scalar1, element_b1, sae->tmp->prime_len,
2240c1d255d3SCy Schubert 			   scalar2, element_b2, sae->tmp->prime_len,
2241c1d255d3SCy Schubert 			   confirm) < 0)
2242c1d255d3SCy Schubert 		return -1;
2243c1d255d3SCy Schubert 	return 0;
22445b9c547cSRui Paulo }
22455b9c547cSRui Paulo 
22465b9c547cSRui Paulo 
sae_write_confirm(struct sae_data * sae,struct wpabuf * buf)2247c1d255d3SCy Schubert int sae_write_confirm(struct sae_data *sae, struct wpabuf *buf)
22485b9c547cSRui Paulo {
22495b9c547cSRui Paulo 	const u8 *sc;
2250c1d255d3SCy Schubert 	size_t hash_len;
2251c1d255d3SCy Schubert 	int res;
22525b9c547cSRui Paulo 
22535b9c547cSRui Paulo 	if (sae->tmp == NULL)
2254c1d255d3SCy Schubert 		return -1;
2255c1d255d3SCy Schubert 
2256c1d255d3SCy Schubert 	hash_len = sae->tmp->kck_len;
22575b9c547cSRui Paulo 
22585b9c547cSRui Paulo 	/* Send-Confirm */
225985732ac8SCy Schubert 	if (sae->send_confirm < 0xffff)
22605b9c547cSRui Paulo 		sae->send_confirm++;
2261c1d255d3SCy Schubert 	sc = wpabuf_put(buf, 0);
2262c1d255d3SCy Schubert 	wpabuf_put_le16(buf, sae->send_confirm);
22635b9c547cSRui Paulo 
22645b9c547cSRui Paulo 	if (sae->tmp->ec)
2265c1d255d3SCy Schubert 		res = sae_cn_confirm_ecc(sae, sc, sae->tmp->own_commit_scalar,
22665b9c547cSRui Paulo 					 sae->tmp->own_commit_element_ecc,
22675b9c547cSRui Paulo 					 sae->peer_commit_scalar,
22685b9c547cSRui Paulo 					 sae->tmp->peer_commit_element_ecc,
2269c1d255d3SCy Schubert 					 wpabuf_put(buf, hash_len));
22705b9c547cSRui Paulo 	else
2271c1d255d3SCy Schubert 		res = sae_cn_confirm_ffc(sae, sc, sae->tmp->own_commit_scalar,
22725b9c547cSRui Paulo 					 sae->tmp->own_commit_element_ffc,
22735b9c547cSRui Paulo 					 sae->peer_commit_scalar,
22745b9c547cSRui Paulo 					 sae->tmp->peer_commit_element_ffc,
2275c1d255d3SCy Schubert 					 wpabuf_put(buf, hash_len));
2276c1d255d3SCy Schubert 	if (res)
2277c1d255d3SCy Schubert 		return res;
2278c1d255d3SCy Schubert 
2279c1d255d3SCy Schubert #ifdef CONFIG_SAE_PK
2280c1d255d3SCy Schubert 	if (sae_write_confirm_pk(sae, buf) < 0)
2281c1d255d3SCy Schubert 		return -1;
2282c1d255d3SCy Schubert #endif /* CONFIG_SAE_PK */
2283c1d255d3SCy Schubert 
2284c1d255d3SCy Schubert 	return 0;
22855b9c547cSRui Paulo }
22865b9c547cSRui Paulo 
22875b9c547cSRui Paulo 
sae_check_confirm(struct sae_data * sae,const u8 * data,size_t len)22885b9c547cSRui Paulo int sae_check_confirm(struct sae_data *sae, const u8 *data, size_t len)
22895b9c547cSRui Paulo {
2290c1d255d3SCy Schubert 	u8 verifier[SAE_MAX_HASH_LEN];
2291c1d255d3SCy Schubert 	size_t hash_len;
22925b9c547cSRui Paulo 
2293c1d255d3SCy Schubert 	if (!sae->tmp)
2294c1d255d3SCy Schubert 		return -1;
2295c1d255d3SCy Schubert 
2296c1d255d3SCy Schubert 	hash_len = sae->tmp->kck_len;
2297c1d255d3SCy Schubert 	if (len < 2 + hash_len) {
22985b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Too short confirm message");
22995b9c547cSRui Paulo 		return -1;
23005b9c547cSRui Paulo 	}
23015b9c547cSRui Paulo 
23025b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "SAE: peer-send-confirm %u", WPA_GET_LE16(data));
23035b9c547cSRui Paulo 
2304c1d255d3SCy Schubert 	if (!sae->peer_commit_scalar || !sae->tmp->own_commit_scalar) {
23055b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "SAE: Temporary data not yet available");
23065b9c547cSRui Paulo 		return -1;
23075b9c547cSRui Paulo 	}
23085b9c547cSRui Paulo 
23094bc52338SCy Schubert 	if (sae->tmp->ec) {
23104bc52338SCy Schubert 		if (!sae->tmp->peer_commit_element_ecc ||
2311c1d255d3SCy Schubert 		    !sae->tmp->own_commit_element_ecc ||
23125b9c547cSRui Paulo 		    sae_cn_confirm_ecc(sae, data, sae->peer_commit_scalar,
23135b9c547cSRui Paulo 				       sae->tmp->peer_commit_element_ecc,
23145b9c547cSRui Paulo 				       sae->tmp->own_commit_scalar,
23155b9c547cSRui Paulo 				       sae->tmp->own_commit_element_ecc,
2316c1d255d3SCy Schubert 				       verifier) < 0)
2317c1d255d3SCy Schubert 			return -1;
23184bc52338SCy Schubert 	} else {
23194bc52338SCy Schubert 		if (!sae->tmp->peer_commit_element_ffc ||
2320c1d255d3SCy Schubert 		    !sae->tmp->own_commit_element_ffc ||
23215b9c547cSRui Paulo 		    sae_cn_confirm_ffc(sae, data, sae->peer_commit_scalar,
23225b9c547cSRui Paulo 				       sae->tmp->peer_commit_element_ffc,
23235b9c547cSRui Paulo 				       sae->tmp->own_commit_scalar,
23245b9c547cSRui Paulo 				       sae->tmp->own_commit_element_ffc,
2325c1d255d3SCy Schubert 				       verifier) < 0)
23265b9c547cSRui Paulo 			return -1;
23275b9c547cSRui Paulo 	}
23285b9c547cSRui Paulo 
2329c1d255d3SCy Schubert 	if (os_memcmp_const(verifier, data + 2, hash_len) != 0) {
2330c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG, "SAE: Confirm mismatch");
2331c1d255d3SCy Schubert 		wpa_hexdump(MSG_DEBUG, "SAE: Received confirm",
2332c1d255d3SCy Schubert 			    data + 2, hash_len);
2333c1d255d3SCy Schubert 		wpa_hexdump(MSG_DEBUG, "SAE: Calculated verifier",
2334c1d255d3SCy Schubert 			    verifier, hash_len);
2335c1d255d3SCy Schubert 		return -1;
2336c1d255d3SCy Schubert 	}
2337c1d255d3SCy Schubert 
2338c1d255d3SCy Schubert #ifdef CONFIG_SAE_PK
2339c1d255d3SCy Schubert 	if (sae_check_confirm_pk(sae, data + 2 + hash_len,
2340c1d255d3SCy Schubert 				 len - 2 - hash_len) < 0)
2341c1d255d3SCy Schubert 		return -1;
2342c1d255d3SCy Schubert #endif /* CONFIG_SAE_PK */
2343c1d255d3SCy Schubert 
23445b9c547cSRui Paulo 	return 0;
23455b9c547cSRui Paulo }
234685732ac8SCy Schubert 
234785732ac8SCy Schubert 
sae_state_txt(enum sae_state state)234885732ac8SCy Schubert const char * sae_state_txt(enum sae_state state)
234985732ac8SCy Schubert {
235085732ac8SCy Schubert 	switch (state) {
235185732ac8SCy Schubert 	case SAE_NOTHING:
235285732ac8SCy Schubert 		return "Nothing";
235385732ac8SCy Schubert 	case SAE_COMMITTED:
235485732ac8SCy Schubert 		return "Committed";
235585732ac8SCy Schubert 	case SAE_CONFIRMED:
235685732ac8SCy Schubert 		return "Confirmed";
235785732ac8SCy Schubert 	case SAE_ACCEPTED:
235885732ac8SCy Schubert 		return "Accepted";
235985732ac8SCy Schubert 	}
236085732ac8SCy Schubert 	return "?";
236185732ac8SCy Schubert }
2362