blob: 33c7e5fe177f236ec19cb977c632b807da4f1d14 [file] [log] [blame]
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001/*
2 * Simultaneous authentication of equals
Dmitry Shmidte4663042016-04-04 10:07:49 -07003 * Copyright (c) 2012-2016, Jouni Malinen <j@w1.fi>
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10
11#include "common.h"
Sunil Ravi89eba102022-09-13 21:04:37 -070012#include "common/defs.h"
13#include "common/wpa_common.h"
Hai Shalom021b0b52019-04-10 11:17:58 -070014#include "utils/const_time.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080015#include "crypto/crypto.h"
16#include "crypto/sha256.h"
Hai Shalomc3565922019-10-28 11:58:20 -070017#include "crypto/sha384.h"
18#include "crypto/sha512.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080019#include "crypto/random.h"
20#include "crypto/dh_groups.h"
21#include "ieee802_11_defs.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070022#include "dragonfly.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080023#include "sae.h"
24
25
26int sae_set_group(struct sae_data *sae, int group)
27{
28 struct sae_temporary_data *tmp;
29
Hai Shalom81f62d82019-07-22 12:10:00 -070030#ifdef CONFIG_TESTING_OPTIONS
31 /* Allow all groups for testing purposes in non-production builds. */
32#else /* CONFIG_TESTING_OPTIONS */
33 if (!dragonfly_suitable_group(group, 0)) {
Hai Shalom021b0b52019-04-10 11:17:58 -070034 wpa_printf(MSG_DEBUG, "SAE: Reject unsuitable group %d", group);
35 return -1;
36 }
Hai Shalom81f62d82019-07-22 12:10:00 -070037#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom021b0b52019-04-10 11:17:58 -070038
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080039 sae_clear_data(sae);
40 tmp = sae->tmp = os_zalloc(sizeof(*tmp));
41 if (tmp == NULL)
42 return -1;
43
44 /* First, check if this is an ECC group */
45 tmp->ec = crypto_ec_init(group);
46 if (tmp->ec) {
Roshan Pius3a1667e2018-07-03 15:17:14 -070047 wpa_printf(MSG_DEBUG, "SAE: Selecting supported ECC group %d",
48 group);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080049 sae->group = group;
50 tmp->prime_len = crypto_ec_prime_len(tmp->ec);
51 tmp->prime = crypto_ec_get_prime(tmp->ec);
Hai Shalomc3565922019-10-28 11:58:20 -070052 tmp->order_len = crypto_ec_order_len(tmp->ec);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080053 tmp->order = crypto_ec_get_order(tmp->ec);
54 return 0;
55 }
56
57 /* Not an ECC group, check FFC */
58 tmp->dh = dh_groups_get(group);
59 if (tmp->dh) {
Roshan Pius3a1667e2018-07-03 15:17:14 -070060 wpa_printf(MSG_DEBUG, "SAE: Selecting supported FFC group %d",
61 group);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080062 sae->group = group;
63 tmp->prime_len = tmp->dh->prime_len;
64 if (tmp->prime_len > SAE_MAX_PRIME_LEN) {
65 sae_clear_data(sae);
66 return -1;
67 }
68
69 tmp->prime_buf = crypto_bignum_init_set(tmp->dh->prime,
70 tmp->prime_len);
71 if (tmp->prime_buf == NULL) {
72 sae_clear_data(sae);
73 return -1;
74 }
75 tmp->prime = tmp->prime_buf;
76
Hai Shalomc3565922019-10-28 11:58:20 -070077 tmp->order_len = tmp->dh->order_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080078 tmp->order_buf = crypto_bignum_init_set(tmp->dh->order,
79 tmp->dh->order_len);
80 if (tmp->order_buf == NULL) {
81 sae_clear_data(sae);
82 return -1;
83 }
84 tmp->order = tmp->order_buf;
85
86 return 0;
87 }
88
89 /* Unsupported group */
Roshan Pius3a1667e2018-07-03 15:17:14 -070090 wpa_printf(MSG_DEBUG,
91 "SAE: Group %d not supported by the crypto library", group);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080092 return -1;
93}
94
95
96void sae_clear_temp_data(struct sae_data *sae)
97{
98 struct sae_temporary_data *tmp;
99 if (sae == NULL || sae->tmp == NULL)
100 return;
101 tmp = sae->tmp;
102 crypto_ec_deinit(tmp->ec);
103 crypto_bignum_deinit(tmp->prime_buf, 0);
104 crypto_bignum_deinit(tmp->order_buf, 0);
105 crypto_bignum_deinit(tmp->sae_rand, 1);
106 crypto_bignum_deinit(tmp->pwe_ffc, 1);
107 crypto_bignum_deinit(tmp->own_commit_scalar, 0);
108 crypto_bignum_deinit(tmp->own_commit_element_ffc, 0);
109 crypto_bignum_deinit(tmp->peer_commit_element_ffc, 0);
110 crypto_ec_point_deinit(tmp->pwe_ecc, 1);
111 crypto_ec_point_deinit(tmp->own_commit_element_ecc, 0);
112 crypto_ec_point_deinit(tmp->peer_commit_element_ecc, 0);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800113 wpabuf_free(tmp->anti_clogging_token);
Hai Shalomc3565922019-10-28 11:58:20 -0700114 wpabuf_free(tmp->own_rejected_groups);
115 wpabuf_free(tmp->peer_rejected_groups);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700116 os_free(tmp->pw_id);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800117 bin_clear_free(tmp, sizeof(*tmp));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800118 sae->tmp = NULL;
119}
120
121
122void sae_clear_data(struct sae_data *sae)
123{
124 if (sae == NULL)
125 return;
126 sae_clear_temp_data(sae);
127 crypto_bignum_deinit(sae->peer_commit_scalar, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700128 crypto_bignum_deinit(sae->peer_commit_scalar_accepted, 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800129 os_memset(sae, 0, sizeof(*sae));
130}
131
132
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800133static void sae_pwd_seed_key(const u8 *addr1, const u8 *addr2, u8 *key)
134{
135 wpa_printf(MSG_DEBUG, "SAE: PWE derivation - addr1=" MACSTR
136 " addr2=" MACSTR, MAC2STR(addr1), MAC2STR(addr2));
137 if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) {
138 os_memcpy(key, addr1, ETH_ALEN);
139 os_memcpy(key + ETH_ALEN, addr2, ETH_ALEN);
140 } else {
141 os_memcpy(key, addr2, ETH_ALEN);
142 os_memcpy(key + ETH_ALEN, addr1, ETH_ALEN);
143 }
144}
145
146
Dmitry Shmidt41712582015-06-29 11:02:15 -0700147static int sae_test_pwd_seed_ecc(struct sae_data *sae, const u8 *pwd_seed,
Hai Shalom021b0b52019-04-10 11:17:58 -0700148 const u8 *prime, const u8 *qr, const u8 *qnr,
149 u8 *pwd_value)
Dmitry Shmidt41712582015-06-29 11:02:15 -0700150{
Dmitry Shmidt41712582015-06-29 11:02:15 -0700151 struct crypto_bignum *y_sqr, *x_cand;
152 int res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800153 size_t bits;
Hai Shalom81f62d82019-07-22 12:10:00 -0700154 int cmp_prime;
155 unsigned int in_range;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800156
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800157 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
158
159 /* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
160 bits = crypto_ec_prime_len_bits(sae->tmp->ec);
Dmitry Shmidte4663042016-04-04 10:07:49 -0700161 if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking",
162 prime, sae->tmp->prime_len, pwd_value, bits) < 0)
163 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800164 if (bits % 8)
Hai Shalom021b0b52019-04-10 11:17:58 -0700165 buf_shift_right(pwd_value, sae->tmp->prime_len, 8 - bits % 8);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800166 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value",
167 pwd_value, sae->tmp->prime_len);
168
Hai Shalom81f62d82019-07-22 12:10:00 -0700169 cmp_prime = const_time_memcmp(pwd_value, prime, sae->tmp->prime_len);
170 /* Create a const_time mask for selection based on prf result
171 * being smaller than prime. */
172 in_range = const_time_fill_msb((unsigned int) cmp_prime);
173 /* The algorithm description would skip the next steps if
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700174 * cmp_prime >= 0 (return 0 here), but go through them regardless to
Hai Shalom81f62d82019-07-22 12:10:00 -0700175 * minimize externally observable differences in behavior. */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800176
Dmitry Shmidt41712582015-06-29 11:02:15 -0700177 x_cand = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
178 if (!x_cand)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800179 return -1;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700180 y_sqr = crypto_ec_point_compute_y_sqr(sae->tmp->ec, x_cand);
Hai Shalom021b0b52019-04-10 11:17:58 -0700181 crypto_bignum_deinit(x_cand, 1);
182 if (!y_sqr)
Dmitry Shmidt41712582015-06-29 11:02:15 -0700183 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800184
Hai Shalom81f62d82019-07-22 12:10:00 -0700185 res = dragonfly_is_quadratic_residue_blind(sae->tmp->ec, qr, qnr,
186 y_sqr);
Dmitry Shmidt41712582015-06-29 11:02:15 -0700187 crypto_bignum_deinit(y_sqr, 1);
Hai Shalom81f62d82019-07-22 12:10:00 -0700188 if (res < 0)
189 return res;
190 return const_time_select_int(in_range, res, 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800191}
192
193
Hai Shalom021b0b52019-04-10 11:17:58 -0700194/* Returns -1 on fatal failure, 0 if PWE cannot be derived from the provided
195 * pwd-seed, or 1 if a valid PWE was derived from pwd-seed. */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800196static int sae_test_pwd_seed_ffc(struct sae_data *sae, const u8 *pwd_seed,
197 struct crypto_bignum *pwe)
198{
199 u8 pwd_value[SAE_MAX_PRIME_LEN];
200 size_t bits = sae->tmp->prime_len * 8;
201 u8 exp[1];
Hai Shalom021b0b52019-04-10 11:17:58 -0700202 struct crypto_bignum *a, *b = NULL;
203 int res, is_val;
204 u8 pwd_value_valid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800205
206 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
207
208 /* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
Dmitry Shmidte4663042016-04-04 10:07:49 -0700209 if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking",
210 sae->tmp->dh->prime, sae->tmp->prime_len, pwd_value,
211 bits) < 0)
212 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800213 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value", pwd_value,
214 sae->tmp->prime_len);
215
Hai Shalom021b0b52019-04-10 11:17:58 -0700216 /* Check whether pwd-value < p */
217 res = const_time_memcmp(pwd_value, sae->tmp->dh->prime,
218 sae->tmp->prime_len);
219 /* pwd-value >= p is invalid, so res is < 0 for the valid cases and
220 * the negative sign can be used to fill the mask for constant time
221 * selection */
222 pwd_value_valid = const_time_fill_msb(res);
223
224 /* If pwd-value >= p, force pwd-value to be < p and perform the
225 * calculations anyway to hide timing difference. The derived PWE will
226 * be ignored in that case. */
227 pwd_value[0] = const_time_select_u8(pwd_value_valid, pwd_value[0], 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800228
229 /* PWE = pwd-value^((p-1)/r) modulo p */
230
Hai Shalom021b0b52019-04-10 11:17:58 -0700231 res = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800232 a = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
Hai Shalom021b0b52019-04-10 11:17:58 -0700233 if (!a)
234 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800235
Hai Shalom021b0b52019-04-10 11:17:58 -0700236 /* This is an optimization based on the used group that does not depend
237 * on the password in any way, so it is fine to use separate branches
238 * for this step without constant time operations. */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800239 if (sae->tmp->dh->safe_prime) {
240 /*
241 * r = (p-1)/2 for the group used here, so this becomes:
242 * PWE = pwd-value^2 modulo p
243 */
244 exp[0] = 2;
245 b = crypto_bignum_init_set(exp, sizeof(exp));
246 } else {
247 /* Calculate exponent: (p-1)/r */
248 exp[0] = 1;
249 b = crypto_bignum_init_set(exp, sizeof(exp));
250 if (b == NULL ||
251 crypto_bignum_sub(sae->tmp->prime, b, b) < 0 ||
Hai Shalom021b0b52019-04-10 11:17:58 -0700252 crypto_bignum_div(b, sae->tmp->order, b) < 0)
253 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800254 }
255
Hai Shalom021b0b52019-04-10 11:17:58 -0700256 if (!b)
257 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800258
Hai Shalom021b0b52019-04-10 11:17:58 -0700259 res = crypto_bignum_exptmod(a, b, sae->tmp->prime, pwe);
260 if (res < 0)
261 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800262
Hai Shalom021b0b52019-04-10 11:17:58 -0700263 /* There were no fatal errors in calculations, so determine the return
264 * value using constant time operations. We get here for number of
265 * invalid cases which are cleared here after having performed all the
266 * computation. PWE is valid if pwd-value was less than prime and
267 * PWE > 1. Start with pwd-value check first and then use constant time
268 * operations to clear res to 0 if PWE is 0 or 1.
269 */
270 res = const_time_select_u8(pwd_value_valid, 1, 0);
271 is_val = crypto_bignum_is_zero(pwe);
272 res = const_time_select_u8(const_time_is_zero(is_val), res, 0);
273 is_val = crypto_bignum_is_one(pwe);
274 res = const_time_select_u8(const_time_is_zero(is_val), res, 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800275
Hai Shalom021b0b52019-04-10 11:17:58 -0700276fail:
277 crypto_bignum_deinit(a, 1);
278 crypto_bignum_deinit(b, 1);
279 return res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800280}
281
282
283static int sae_derive_pwe_ecc(struct sae_data *sae, const u8 *addr1,
284 const u8 *addr2, const u8 *password,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800285 size_t password_len)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800286{
Hai Shalomc3565922019-10-28 11:58:20 -0700287 u8 counter, k;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800288 u8 addrs[2 * ETH_ALEN];
Hai Shaloma20dcd72022-02-04 13:43:00 -0800289 const u8 *addr[2];
290 size_t len[2];
291 u8 *stub_password, *tmp_password;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700292 int pwd_seed_odd = 0;
293 u8 prime[SAE_MAX_ECC_PRIME_LEN];
294 size_t prime_len;
Hai Shaloma20dcd72022-02-04 13:43:00 -0800295 struct crypto_bignum *x = NULL, *y = NULL, *qr = NULL, *qnr = NULL;
Hai Shalom021b0b52019-04-10 11:17:58 -0700296 u8 x_bin[SAE_MAX_ECC_PRIME_LEN];
297 u8 x_cand_bin[SAE_MAX_ECC_PRIME_LEN];
298 u8 qr_bin[SAE_MAX_ECC_PRIME_LEN];
299 u8 qnr_bin[SAE_MAX_ECC_PRIME_LEN];
Hai Shaloma20dcd72022-02-04 13:43:00 -0800300 u8 x_y[2 * SAE_MAX_ECC_PRIME_LEN];
Hai Shalom021b0b52019-04-10 11:17:58 -0700301 int res = -1;
302 u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
303 * mask */
Hai Shaloma20dcd72022-02-04 13:43:00 -0800304 unsigned int is_eq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800305
Hai Shalom021b0b52019-04-10 11:17:58 -0700306 os_memset(x_bin, 0, sizeof(x_bin));
307
Hai Shaloma20dcd72022-02-04 13:43:00 -0800308 stub_password = os_malloc(password_len);
Hai Shalom021b0b52019-04-10 11:17:58 -0700309 tmp_password = os_malloc(password_len);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800310 if (!stub_password || !tmp_password ||
311 random_get_bytes(stub_password, password_len) < 0)
Hai Shalom021b0b52019-04-10 11:17:58 -0700312 goto fail;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700313
314 prime_len = sae->tmp->prime_len;
315 if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
316 prime_len) < 0)
Hai Shalom021b0b52019-04-10 11:17:58 -0700317 goto fail;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700318
319 /*
320 * Create a random quadratic residue (qr) and quadratic non-residue
321 * (qnr) modulo p for blinding purposes during the loop.
322 */
Hai Shalom81f62d82019-07-22 12:10:00 -0700323 if (dragonfly_get_random_qr_qnr(sae->tmp->prime, &qr, &qnr) < 0 ||
Hai Shalom021b0b52019-04-10 11:17:58 -0700324 crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin), prime_len) < 0 ||
325 crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin), prime_len) < 0)
326 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800327
328 wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
329 password, password_len);
330
331 /*
332 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
Hai Shaloma20dcd72022-02-04 13:43:00 -0800333 * base = password
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800334 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
Dmitry Shmidt41712582015-06-29 11:02:15 -0700335 * base || counter)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800336 */
337 sae_pwd_seed_key(addr1, addr2, addrs);
338
Hai Shalom021b0b52019-04-10 11:17:58 -0700339 addr[0] = tmp_password;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800340 len[0] = password_len;
Hai Shaloma20dcd72022-02-04 13:43:00 -0800341 addr[1] = &counter;
342 len[1] = sizeof(counter);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800343
344 /*
345 * Continue for at least k iterations to protect against side-channel
346 * attacks that attempt to determine the number of iterations required
347 * in the loop.
348 */
Hai Shalomc3565922019-10-28 11:58:20 -0700349 k = dragonfly_min_pwe_loop_iter(sae->group);
350
Hai Shalom021b0b52019-04-10 11:17:58 -0700351 for (counter = 1; counter <= k || !found; counter++) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800352 u8 pwd_seed[SHA256_MAC_LEN];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800353
354 if (counter > 200) {
355 /* This should not happen in practice */
356 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
357 break;
358 }
359
Hai Shalom021b0b52019-04-10 11:17:58 -0700360 wpa_printf(MSG_DEBUG, "SAE: counter = %03u", counter);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800361 const_time_select_bin(found, stub_password, password,
Hai Shalom021b0b52019-04-10 11:17:58 -0700362 password_len, tmp_password);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800363 if (hmac_sha256_vector(addrs, sizeof(addrs), 2,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700364 addr, len, pwd_seed) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800365 break;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700366
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800367 res = sae_test_pwd_seed_ecc(sae, pwd_seed,
Hai Shalom021b0b52019-04-10 11:17:58 -0700368 prime, qr_bin, qnr_bin, x_cand_bin);
369 const_time_select_bin(found, x_bin, x_cand_bin, prime_len,
370 x_bin);
371 pwd_seed_odd = const_time_select_u8(
372 found, pwd_seed_odd,
373 pwd_seed[SHA256_MAC_LEN - 1] & 0x01);
374 os_memset(pwd_seed, 0, sizeof(pwd_seed));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800375 if (res < 0)
Dmitry Shmidt41712582015-06-29 11:02:15 -0700376 goto fail;
Hai Shalom021b0b52019-04-10 11:17:58 -0700377 /* Need to minimize differences in handling res == 0 and 1 here
378 * to avoid differences in timing and instruction cache access,
379 * so use const_time_select_*() to make local copies of the
380 * values based on whether this loop iteration was the one that
381 * found the pwd-seed/x. */
Dmitry Shmidt41712582015-06-29 11:02:15 -0700382
Hai Shalom021b0b52019-04-10 11:17:58 -0700383 /* found is 0 or 0xff here and res is 0 or 1. Bitwise OR of them
384 * (with res converted to 0/0xff) handles this in constant time.
385 */
386 found |= res * 0xff;
387 wpa_printf(MSG_DEBUG, "SAE: pwd-seed result %d found=0x%02x",
388 res, found);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800389 }
390
Hai Shalom021b0b52019-04-10 11:17:58 -0700391 if (!found) {
Dmitry Shmidt41712582015-06-29 11:02:15 -0700392 wpa_printf(MSG_DEBUG, "SAE: Could not generate PWE");
393 res = -1;
394 goto fail;
395 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800396
Hai Shalom021b0b52019-04-10 11:17:58 -0700397 x = crypto_bignum_init_set(x_bin, prime_len);
398 if (!x) {
399 res = -1;
400 goto fail;
401 }
402
Hai Shaloma20dcd72022-02-04 13:43:00 -0800403 /* y = sqrt(x^3 + ax + b) mod p
404 * if LSB(save) == LSB(y): PWE = (x, y)
405 * else: PWE = (x, p - y)
406 *
407 * Calculate y and the two possible values for PWE and after that,
408 * use constant time selection to copy the correct alternative.
409 */
410 y = crypto_ec_point_compute_y_sqr(sae->tmp->ec, x);
411 if (!y ||
412 dragonfly_sqrt(sae->tmp->ec, y, y) < 0 ||
413 crypto_bignum_to_bin(y, x_y, SAE_MAX_ECC_PRIME_LEN,
414 prime_len) < 0 ||
415 crypto_bignum_sub(sae->tmp->prime, y, y) < 0 ||
416 crypto_bignum_to_bin(y, x_y + SAE_MAX_ECC_PRIME_LEN,
417 SAE_MAX_ECC_PRIME_LEN, prime_len) < 0) {
Dmitry Shmidt41712582015-06-29 11:02:15 -0700418 wpa_printf(MSG_DEBUG, "SAE: Could not solve y");
Hai Shaloma20dcd72022-02-04 13:43:00 -0800419 goto fail;
420 }
421
422 is_eq = const_time_eq(pwd_seed_odd, x_y[prime_len - 1] & 0x01);
423 const_time_select_bin(is_eq, x_y, x_y + SAE_MAX_ECC_PRIME_LEN,
424 prime_len, x_y + prime_len);
425 os_memcpy(x_y, x_bin, prime_len);
426 wpa_hexdump_key(MSG_DEBUG, "SAE: PWE", x_y, 2 * prime_len);
427 crypto_ec_point_deinit(sae->tmp->pwe_ecc, 1);
428 sae->tmp->pwe_ecc = crypto_ec_point_from_bin(sae->tmp->ec, x_y);
429 if (!sae->tmp->pwe_ecc) {
430 wpa_printf(MSG_DEBUG, "SAE: Could not generate PWE");
431 res = -1;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700432 }
433
434fail:
Hai Shaloma20dcd72022-02-04 13:43:00 -0800435 forced_memzero(x_y, sizeof(x_y));
Dmitry Shmidt41712582015-06-29 11:02:15 -0700436 crypto_bignum_deinit(qr, 0);
437 crypto_bignum_deinit(qnr, 0);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800438 crypto_bignum_deinit(y, 1);
439 os_free(stub_password);
Hai Shalom021b0b52019-04-10 11:17:58 -0700440 bin_clear_free(tmp_password, password_len);
441 crypto_bignum_deinit(x, 1);
442 os_memset(x_bin, 0, sizeof(x_bin));
443 os_memset(x_cand_bin, 0, sizeof(x_cand_bin));
Dmitry Shmidt41712582015-06-29 11:02:15 -0700444
445 return res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800446}
447
448
449static int sae_derive_pwe_ffc(struct sae_data *sae, const u8 *addr1,
450 const u8 *addr2, const u8 *password,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800451 size_t password_len)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800452{
Hai Shalom021b0b52019-04-10 11:17:58 -0700453 u8 counter, k, sel_counter = 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800454 u8 addrs[2 * ETH_ALEN];
Hai Shaloma20dcd72022-02-04 13:43:00 -0800455 const u8 *addr[2];
456 size_t len[2];
Hai Shalom021b0b52019-04-10 11:17:58 -0700457 u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
458 * mask */
459 u8 mask;
460 struct crypto_bignum *pwe;
461 size_t prime_len = sae->tmp->prime_len * 8;
462 u8 *pwe_buf;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800463
Hai Shalom021b0b52019-04-10 11:17:58 -0700464 crypto_bignum_deinit(sae->tmp->pwe_ffc, 1);
465 sae->tmp->pwe_ffc = NULL;
466
467 /* Allocate a buffer to maintain selected and candidate PWE for constant
468 * time selection. */
469 pwe_buf = os_zalloc(prime_len * 2);
470 pwe = crypto_bignum_init();
471 if (!pwe_buf || !pwe)
472 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800473
474 wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
475 password, password_len);
476
477 /*
478 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
479 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
Hai Shaloma20dcd72022-02-04 13:43:00 -0800480 * password || counter)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800481 */
482 sae_pwd_seed_key(addr1, addr2, addrs);
483
484 addr[0] = password;
485 len[0] = password_len;
Hai Shaloma20dcd72022-02-04 13:43:00 -0800486 addr[1] = &counter;
487 len[1] = sizeof(counter);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800488
Hai Shalomc3565922019-10-28 11:58:20 -0700489 k = dragonfly_min_pwe_loop_iter(sae->group);
Hai Shalom021b0b52019-04-10 11:17:58 -0700490
491 for (counter = 1; counter <= k || !found; counter++) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800492 u8 pwd_seed[SHA256_MAC_LEN];
493 int res;
494
495 if (counter > 200) {
496 /* This should not happen in practice */
497 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
498 break;
499 }
500
Hai Shalom021b0b52019-04-10 11:17:58 -0700501 wpa_printf(MSG_DEBUG, "SAE: counter = %02u", counter);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800502 if (hmac_sha256_vector(addrs, sizeof(addrs), 2,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700503 addr, len, pwd_seed) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800504 break;
Hai Shalom021b0b52019-04-10 11:17:58 -0700505 res = sae_test_pwd_seed_ffc(sae, pwd_seed, pwe);
506 /* res is -1 for fatal failure, 0 if a valid PWE was not found,
507 * or 1 if a valid PWE was found. */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800508 if (res < 0)
509 break;
Hai Shalom021b0b52019-04-10 11:17:58 -0700510 /* Store the candidate PWE into the second half of pwe_buf and
511 * the selected PWE in the beginning of pwe_buf using constant
512 * time selection. */
513 if (crypto_bignum_to_bin(pwe, pwe_buf + prime_len, prime_len,
514 prime_len) < 0)
515 break;
516 const_time_select_bin(found, pwe_buf, pwe_buf + prime_len,
517 prime_len, pwe_buf);
518 sel_counter = const_time_select_u8(found, sel_counter, counter);
519 mask = const_time_eq_u8(res, 1);
520 found = const_time_select_u8(found, found, mask);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800521 }
522
Hai Shalom021b0b52019-04-10 11:17:58 -0700523 if (!found)
524 goto fail;
525
526 wpa_printf(MSG_DEBUG, "SAE: Use PWE from counter = %02u", sel_counter);
527 sae->tmp->pwe_ffc = crypto_bignum_init_set(pwe_buf, prime_len);
528fail:
529 crypto_bignum_deinit(pwe, 1);
530 bin_clear_free(pwe_buf, prime_len * 2);
531 return sae->tmp->pwe_ffc ? 0 : -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800532}
533
534
Hai Shalomc3565922019-10-28 11:58:20 -0700535static int hkdf_extract(size_t hash_len, const u8 *salt, size_t salt_len,
536 size_t num_elem, const u8 *addr[], const size_t len[],
537 u8 *prk)
538{
539 if (hash_len == 32)
540 return hmac_sha256_vector(salt, salt_len, num_elem, addr, len,
541 prk);
542#ifdef CONFIG_SHA384
543 if (hash_len == 48)
544 return hmac_sha384_vector(salt, salt_len, num_elem, addr, len,
545 prk);
546#endif /* CONFIG_SHA384 */
547#ifdef CONFIG_SHA512
548 if (hash_len == 64)
549 return hmac_sha512_vector(salt, salt_len, num_elem, addr, len,
550 prk);
551#endif /* CONFIG_SHA512 */
552 return -1;
553}
554
555
556static int hkdf_expand(size_t hash_len, const u8 *prk, size_t prk_len,
557 const char *info, u8 *okm, size_t okm_len)
558{
559 size_t info_len = os_strlen(info);
560
561 if (hash_len == 32)
562 return hmac_sha256_kdf(prk, prk_len, NULL,
563 (const u8 *) info, info_len,
564 okm, okm_len);
565#ifdef CONFIG_SHA384
566 if (hash_len == 48)
567 return hmac_sha384_kdf(prk, prk_len, NULL,
568 (const u8 *) info, info_len,
569 okm, okm_len);
570#endif /* CONFIG_SHA384 */
571#ifdef CONFIG_SHA512
572 if (hash_len == 64)
573 return hmac_sha512_kdf(prk, prk_len, NULL,
574 (const u8 *) info, info_len,
575 okm, okm_len);
576#endif /* CONFIG_SHA512 */
577 return -1;
578}
579
580
581static int sswu_curve_param(int group, int *z)
582{
583 switch (group) {
584 case 19:
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800585 *z = -10;
586 return 0;
Hai Shalomc3565922019-10-28 11:58:20 -0700587 case 20:
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800588 *z = -12;
589 return 0;
Hai Shalomc3565922019-10-28 11:58:20 -0700590 case 21:
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800591 *z = -4;
Hai Shalomc3565922019-10-28 11:58:20 -0700592 return 0;
593 case 25:
594 case 29:
595 *z = -5;
596 return 0;
597 case 26:
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800598 *z = 31;
599 return 0;
600 case 28:
601 *z = -2;
Hai Shalomc3565922019-10-28 11:58:20 -0700602 return 0;
603 case 30:
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800604 *z = 7;
Hai Shalomc3565922019-10-28 11:58:20 -0700605 return 0;
606 }
607
608 return -1;
609}
610
611
612static void debug_print_bignum(const char *title, const struct crypto_bignum *a,
613 size_t prime_len)
614{
615 u8 *bin;
616
617 bin = os_malloc(prime_len);
618 if (bin && crypto_bignum_to_bin(a, bin, prime_len, prime_len) >= 0)
619 wpa_hexdump_key(MSG_DEBUG, title, bin, prime_len);
620 else
621 wpa_printf(MSG_DEBUG, "Could not print bignum (%s)", title);
622 bin_clear_free(bin, prime_len);
623}
624
625
626static struct crypto_ec_point * sswu(struct crypto_ec *ec, int group,
627 const struct crypto_bignum *u)
628{
629 int z_int;
630 const struct crypto_bignum *a, *b, *prime;
631 struct crypto_bignum *u2, *t1, *t2, *z, *t, *zero, *one, *two, *three,
632 *x1a, *x1b, *y = NULL;
633 struct crypto_bignum *x1 = NULL, *x2, *gx1, *gx2, *v = NULL;
634 unsigned int m_is_zero, is_qr, is_eq;
635 size_t prime_len;
636 u8 bin[SAE_MAX_ECC_PRIME_LEN];
637 u8 bin1[SAE_MAX_ECC_PRIME_LEN];
638 u8 bin2[SAE_MAX_ECC_PRIME_LEN];
639 u8 x_y[2 * SAE_MAX_ECC_PRIME_LEN];
640 struct crypto_ec_point *p = NULL;
641
642 if (sswu_curve_param(group, &z_int) < 0)
643 return NULL;
644
645 prime = crypto_ec_get_prime(ec);
646 prime_len = crypto_ec_prime_len(ec);
647 a = crypto_ec_get_a(ec);
648 b = crypto_ec_get_b(ec);
649
650 u2 = crypto_bignum_init();
651 t1 = crypto_bignum_init();
652 t2 = crypto_bignum_init();
653 z = crypto_bignum_init_uint(abs(z_int));
654 t = crypto_bignum_init();
655 zero = crypto_bignum_init_uint(0);
656 one = crypto_bignum_init_uint(1);
657 two = crypto_bignum_init_uint(2);
658 three = crypto_bignum_init_uint(3);
659 x1a = crypto_bignum_init();
660 x1b = crypto_bignum_init();
661 x2 = crypto_bignum_init();
662 gx1 = crypto_bignum_init();
663 gx2 = crypto_bignum_init();
664 if (!u2 || !t1 || !t2 || !z || !t || !zero || !one || !two || !three ||
665 !x1a || !x1b || !x2 || !gx1 || !gx2)
666 goto fail;
667
668 if (z_int < 0 && crypto_bignum_sub(prime, z, z) < 0)
669 goto fail;
670
671 /* m = z^2 * u^4 + z * u^2 */
672 /* --> tmp = z * u^2, m = tmp^2 + tmp */
673
674 /* u2 = u^2
675 * t1 = z * u2
676 * t2 = t1^2
677 * m = t1 = t1 + t2 */
678 if (crypto_bignum_sqrmod(u, prime, u2) < 0 ||
679 crypto_bignum_mulmod(z, u2, prime, t1) < 0 ||
680 crypto_bignum_sqrmod(t1, prime, t2) < 0 ||
681 crypto_bignum_addmod(t1, t2, prime, t1) < 0)
682 goto fail;
683 debug_print_bignum("SSWU: m", t1, prime_len);
684
685 /* l = CEQ(m, 0)
686 * t = CSEL(l, 0, inverse(m); where inverse(x) is calculated as
687 * x^(p-2) modulo p which will handle m == 0 case correctly */
688 /* TODO: Make sure crypto_bignum_is_zero() is constant time */
689 m_is_zero = const_time_eq(crypto_bignum_is_zero(t1), 1);
690 /* t = m^(p-2) modulo p */
691 if (crypto_bignum_sub(prime, two, t2) < 0 ||
692 crypto_bignum_exptmod(t1, t2, prime, t) < 0)
693 goto fail;
694 debug_print_bignum("SSWU: t", t, prime_len);
695
696 /* b / (z * a) */
697 if (crypto_bignum_mulmod(z, a, prime, t1) < 0 ||
698 crypto_bignum_inverse(t1, prime, t1) < 0 ||
699 crypto_bignum_mulmod(b, t1, prime, x1a) < 0)
700 goto fail;
701 debug_print_bignum("SSWU: x1a = b / (z * a)", x1a, prime_len);
702
703 /* (-b/a) * (1 + t) */
704 if (crypto_bignum_sub(prime, b, t1) < 0 ||
705 crypto_bignum_inverse(a, prime, t2) < 0 ||
706 crypto_bignum_mulmod(t1, t2, prime, t1) < 0 ||
707 crypto_bignum_addmod(one, t, prime, t2) < 0 ||
708 crypto_bignum_mulmod(t1, t2, prime, x1b) < 0)
709 goto fail;
710 debug_print_bignum("SSWU: x1b = (-b/a) * (1 + t)", x1b, prime_len);
711
712 /* x1 = CSEL(CEQ(m, 0), x1a, x1b) */
713 if (crypto_bignum_to_bin(x1a, bin1, sizeof(bin1), prime_len) < 0 ||
714 crypto_bignum_to_bin(x1b, bin2, sizeof(bin2), prime_len) < 0)
715 goto fail;
716 const_time_select_bin(m_is_zero, bin1, bin2, prime_len, bin);
717 x1 = crypto_bignum_init_set(bin, prime_len);
Hai Shalom899fcc72020-10-19 14:38:18 -0700718 if (!x1)
719 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -0700720 debug_print_bignum("SSWU: x1 = CSEL(l, x1a, x1b)", x1, prime_len);
721
722 /* gx1 = x1^3 + a * x1 + b */
723 if (crypto_bignum_exptmod(x1, three, prime, t1) < 0 ||
724 crypto_bignum_mulmod(a, x1, prime, t2) < 0 ||
725 crypto_bignum_addmod(t1, t2, prime, t1) < 0 ||
726 crypto_bignum_addmod(t1, b, prime, gx1) < 0)
727 goto fail;
728 debug_print_bignum("SSWU: gx1 = x1^3 + a * x1 + b", gx1, prime_len);
729
730 /* x2 = z * u^2 * x1 */
731 if (crypto_bignum_mulmod(z, u2, prime, t1) < 0 ||
732 crypto_bignum_mulmod(t1, x1, prime, x2) < 0)
733 goto fail;
734 debug_print_bignum("SSWU: x2 = z * u^2 * x1", x2, prime_len);
735
736 /* gx2 = x2^3 + a * x2 + b */
737 if (crypto_bignum_exptmod(x2, three, prime, t1) < 0 ||
738 crypto_bignum_mulmod(a, x2, prime, t2) < 0 ||
739 crypto_bignum_addmod(t1, t2, prime, t1) < 0 ||
740 crypto_bignum_addmod(t1, b, prime, gx2) < 0)
741 goto fail;
742 debug_print_bignum("SSWU: gx2 = x2^3 + a * x2 + b", gx2, prime_len);
743
744 /* l = gx1 is a quadratic residue modulo p
745 * --> gx1^((p-1)/2) modulo p is zero or one */
746 if (crypto_bignum_sub(prime, one, t1) < 0 ||
747 crypto_bignum_rshift(t1, 1, t1) < 0 ||
748 crypto_bignum_exptmod(gx1, t1, prime, t1) < 0)
749 goto fail;
750 debug_print_bignum("SSWU: gx1^((p-1)/2) modulo p", t1, prime_len);
751 is_qr = const_time_eq(crypto_bignum_is_zero(t1) |
752 crypto_bignum_is_one(t1), 1);
753
754 /* v = CSEL(l, gx1, gx2) */
755 if (crypto_bignum_to_bin(gx1, bin1, sizeof(bin1), prime_len) < 0 ||
756 crypto_bignum_to_bin(gx2, bin2, sizeof(bin2), prime_len) < 0)
757 goto fail;
758 const_time_select_bin(is_qr, bin1, bin2, prime_len, bin);
759 v = crypto_bignum_init_set(bin, prime_len);
Hai Shalom899fcc72020-10-19 14:38:18 -0700760 if (!v)
761 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -0700762 debug_print_bignum("SSWU: v = CSEL(l, gx1, gx2)", v, prime_len);
763
764 /* x = CSEL(l, x1, x2) */
765 if (crypto_bignum_to_bin(x1, bin1, sizeof(bin1), prime_len) < 0 ||
766 crypto_bignum_to_bin(x2, bin2, sizeof(bin2), prime_len) < 0)
767 goto fail;
768 const_time_select_bin(is_qr, bin1, bin2, prime_len, x_y);
769 wpa_hexdump_key(MSG_DEBUG, "SSWU: x = CSEL(l, x1, x2)", x_y, prime_len);
770
Hai Shaloma20dcd72022-02-04 13:43:00 -0800771 /* y = sqrt(v) */
Hai Shalomc3565922019-10-28 11:58:20 -0700772 y = crypto_bignum_init();
Hai Shaloma20dcd72022-02-04 13:43:00 -0800773 if (!y || dragonfly_sqrt(ec, v, y) < 0)
Hai Shalomc3565922019-10-28 11:58:20 -0700774 goto fail;
775 debug_print_bignum("SSWU: y = sqrt(v)", y, prime_len);
776
777 /* l = CEQ(LSB(u), LSB(y)) */
778 if (crypto_bignum_to_bin(u, bin1, sizeof(bin1), prime_len) < 0 ||
779 crypto_bignum_to_bin(y, bin2, sizeof(bin2), prime_len) < 0)
780 goto fail;
781 is_eq = const_time_eq(bin1[prime_len - 1] & 0x01,
782 bin2[prime_len - 1] & 0x01);
783
784 /* P = CSEL(l, (x,y), (x, p-y)) */
785 if (crypto_bignum_sub(prime, y, t1) < 0)
786 goto fail;
787 debug_print_bignum("SSWU: p - y", t1, prime_len);
788 if (crypto_bignum_to_bin(y, bin1, sizeof(bin1), prime_len) < 0 ||
789 crypto_bignum_to_bin(t1, bin2, sizeof(bin2), prime_len) < 0)
790 goto fail;
791 const_time_select_bin(is_eq, bin1, bin2, prime_len, &x_y[prime_len]);
792
793 /* output P */
794 wpa_hexdump_key(MSG_DEBUG, "SSWU: P.x", x_y, prime_len);
795 wpa_hexdump_key(MSG_DEBUG, "SSWU: P.y", &x_y[prime_len], prime_len);
796 p = crypto_ec_point_from_bin(ec, x_y);
797
798fail:
799 crypto_bignum_deinit(u2, 1);
800 crypto_bignum_deinit(t1, 1);
801 crypto_bignum_deinit(t2, 1);
802 crypto_bignum_deinit(z, 0);
803 crypto_bignum_deinit(t, 1);
804 crypto_bignum_deinit(x1a, 1);
805 crypto_bignum_deinit(x1b, 1);
806 crypto_bignum_deinit(x1, 1);
807 crypto_bignum_deinit(x2, 1);
808 crypto_bignum_deinit(gx1, 1);
809 crypto_bignum_deinit(gx2, 1);
810 crypto_bignum_deinit(y, 1);
811 crypto_bignum_deinit(v, 1);
812 crypto_bignum_deinit(zero, 0);
813 crypto_bignum_deinit(one, 0);
814 crypto_bignum_deinit(two, 0);
815 crypto_bignum_deinit(three, 0);
816 forced_memzero(bin, sizeof(bin));
817 forced_memzero(bin1, sizeof(bin1));
818 forced_memzero(bin2, sizeof(bin2));
819 forced_memzero(x_y, sizeof(x_y));
820 return p;
821}
822
823
824static int sae_pwd_seed(size_t hash_len, const u8 *ssid, size_t ssid_len,
825 const u8 *password, size_t password_len,
826 const char *identifier, u8 *pwd_seed)
827{
828 const u8 *addr[2];
829 size_t len[2];
830 size_t num_elem;
831
832 /* pwd-seed = HKDF-Extract(ssid, password [ || identifier ]) */
833 addr[0] = password;
834 len[0] = password_len;
835 num_elem = 1;
836 wpa_hexdump_ascii(MSG_DEBUG, "SAE: SSID", ssid, ssid_len);
837 wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
838 password, password_len);
839 if (identifier) {
840 wpa_printf(MSG_DEBUG, "SAE: password identifier: %s",
841 identifier);
842 addr[num_elem] = (const u8 *) identifier;
843 len[num_elem] = os_strlen(identifier);
844 num_elem++;
845 }
846 if (hkdf_extract(hash_len, ssid, ssid_len, num_elem, addr, len,
847 pwd_seed) < 0)
848 return -1;
849 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, hash_len);
850 return 0;
851}
852
853
854size_t sae_ecc_prime_len_2_hash_len(size_t prime_len)
855{
856 if (prime_len <= 256 / 8)
857 return 32;
858 if (prime_len <= 384 / 8)
859 return 48;
860 return 64;
861}
862
863
Hai Shalomfdcde762020-04-02 11:19:20 -0700864static struct crypto_ec_point *
Hai Shalomc3565922019-10-28 11:58:20 -0700865sae_derive_pt_ecc(struct crypto_ec *ec, int group,
866 const u8 *ssid, size_t ssid_len,
867 const u8 *password, size_t password_len,
868 const char *identifier)
869{
870 u8 pwd_seed[64];
871 u8 pwd_value[SAE_MAX_ECC_PRIME_LEN * 2];
872 size_t pwd_value_len, hash_len, prime_len;
873 const struct crypto_bignum *prime;
874 struct crypto_bignum *bn = NULL;
875 struct crypto_ec_point *p1 = NULL, *p2 = NULL, *pt = NULL;
876
877 prime = crypto_ec_get_prime(ec);
878 prime_len = crypto_ec_prime_len(ec);
879 if (prime_len > SAE_MAX_ECC_PRIME_LEN)
880 goto fail;
881 hash_len = sae_ecc_prime_len_2_hash_len(prime_len);
882
883 /* len = olen(p) + ceil(olen(p)/2) */
884 pwd_value_len = prime_len + (prime_len + 1) / 2;
885
886 if (sae_pwd_seed(hash_len, ssid, ssid_len, password, password_len,
887 identifier, pwd_seed) < 0)
888 goto fail;
889
890 /* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u1 P1", len)
891 */
892 if (hkdf_expand(hash_len, pwd_seed, hash_len,
893 "SAE Hash to Element u1 P1", pwd_value, pwd_value_len) <
894 0)
895 goto fail;
896 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value (u1 P1)",
897 pwd_value, pwd_value_len);
898
899 /* u1 = pwd-value modulo p */
900 bn = crypto_bignum_init_set(pwd_value, pwd_value_len);
901 if (!bn || crypto_bignum_mod(bn, prime, bn) < 0 ||
902 crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value),
903 prime_len) < 0)
904 goto fail;
905 wpa_hexdump_key(MSG_DEBUG, "SAE: u1", pwd_value, prime_len);
906
907 /* P1 = SSWU(u1) */
908 p1 = sswu(ec, group, bn);
909 if (!p1)
910 goto fail;
911
912 /* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u2 P2", len)
913 */
914 if (hkdf_expand(hash_len, pwd_seed, hash_len,
915 "SAE Hash to Element u2 P2", pwd_value,
916 pwd_value_len) < 0)
917 goto fail;
918 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value (u2 P2)",
919 pwd_value, pwd_value_len);
920
921 /* u2 = pwd-value modulo p */
922 crypto_bignum_deinit(bn, 1);
923 bn = crypto_bignum_init_set(pwd_value, pwd_value_len);
924 if (!bn || crypto_bignum_mod(bn, prime, bn) < 0 ||
925 crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value),
926 prime_len) < 0)
927 goto fail;
928 wpa_hexdump_key(MSG_DEBUG, "SAE: u2", pwd_value, prime_len);
929
930 /* P2 = SSWU(u2) */
931 p2 = sswu(ec, group, bn);
932 if (!p2)
933 goto fail;
934
935 /* PT = elem-op(P1, P2) */
936 pt = crypto_ec_point_init(ec);
937 if (!pt)
938 goto fail;
939 if (crypto_ec_point_add(ec, p1, p2, pt) < 0) {
940 crypto_ec_point_deinit(pt, 1);
941 pt = NULL;
942 }
943
944fail:
945 forced_memzero(pwd_seed, sizeof(pwd_seed));
946 forced_memzero(pwd_value, sizeof(pwd_value));
947 crypto_bignum_deinit(bn, 1);
948 crypto_ec_point_deinit(p1, 1);
949 crypto_ec_point_deinit(p2, 1);
950 return pt;
951}
952
953
954size_t sae_ffc_prime_len_2_hash_len(size_t prime_len)
955{
956 if (prime_len <= 2048 / 8)
957 return 32;
958 if (prime_len <= 3072 / 8)
959 return 48;
960 return 64;
961}
962
963
964static struct crypto_bignum *
965sae_derive_pt_ffc(const struct dh_group *dh, int group,
966 const u8 *ssid, size_t ssid_len,
967 const u8 *password, size_t password_len,
968 const char *identifier)
969{
970 size_t hash_len, prime_len, pwd_value_len;
971 struct crypto_bignum *prime, *order;
972 struct crypto_bignum *one = NULL, *two = NULL, *bn = NULL, *tmp = NULL,
973 *pt = NULL;
974 u8 pwd_seed[64];
975 u8 pwd_value[SAE_MAX_PRIME_LEN + SAE_MAX_PRIME_LEN / 2];
976
977 prime = crypto_bignum_init_set(dh->prime, dh->prime_len);
978 order = crypto_bignum_init_set(dh->order, dh->order_len);
979 if (!prime || !order)
980 goto fail;
981 prime_len = dh->prime_len;
982 if (prime_len > SAE_MAX_PRIME_LEN)
983 goto fail;
984 hash_len = sae_ffc_prime_len_2_hash_len(prime_len);
985
986 /* len = olen(p) + ceil(olen(p)/2) */
987 pwd_value_len = prime_len + (prime_len + 1) / 2;
988 if (pwd_value_len > sizeof(pwd_value))
989 goto fail;
990
991 if (sae_pwd_seed(hash_len, ssid, ssid_len, password, password_len,
992 identifier, pwd_seed) < 0)
993 goto fail;
994
995 /* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element", len) */
996 if (hkdf_expand(hash_len, pwd_seed, hash_len,
997 "SAE Hash to Element", pwd_value, pwd_value_len) < 0)
998 goto fail;
999 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value",
1000 pwd_value, pwd_value_len);
1001
1002 /* pwd-value = (pwd-value modulo (p-2)) + 2 */
1003 bn = crypto_bignum_init_set(pwd_value, pwd_value_len);
1004 one = crypto_bignum_init_uint(1);
1005 two = crypto_bignum_init_uint(2);
1006 tmp = crypto_bignum_init();
1007 if (!bn || !one || !two || !tmp ||
1008 crypto_bignum_sub(prime, two, tmp) < 0 ||
1009 crypto_bignum_mod(bn, tmp, bn) < 0 ||
1010 crypto_bignum_add(bn, two, bn) < 0 ||
1011 crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value),
1012 prime_len) < 0)
1013 goto fail;
1014 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value(reduced)",
1015 pwd_value, prime_len);
1016
1017 /* PT = pwd-value^((p-1)/q) modulo p */
1018 pt = crypto_bignum_init();
1019 if (!pt ||
1020 crypto_bignum_sub(prime, one, tmp) < 0 ||
1021 crypto_bignum_div(tmp, order, tmp) < 0 ||
1022 crypto_bignum_exptmod(bn, tmp, prime, pt) < 0) {
1023 crypto_bignum_deinit(pt, 1);
1024 pt = NULL;
1025 goto fail;
1026 }
1027 debug_print_bignum("SAE: PT", pt, prime_len);
1028
1029fail:
1030 forced_memzero(pwd_seed, sizeof(pwd_seed));
1031 forced_memzero(pwd_value, sizeof(pwd_value));
1032 crypto_bignum_deinit(bn, 1);
1033 crypto_bignum_deinit(tmp, 1);
1034 crypto_bignum_deinit(one, 0);
1035 crypto_bignum_deinit(two, 0);
1036 crypto_bignum_deinit(prime, 0);
1037 crypto_bignum_deinit(order, 0);
1038 return pt;
1039}
1040
1041
1042static struct sae_pt *
1043sae_derive_pt_group(int group, const u8 *ssid, size_t ssid_len,
1044 const u8 *password, size_t password_len,
1045 const char *identifier)
1046{
1047 struct sae_pt *pt;
1048
1049 wpa_printf(MSG_DEBUG, "SAE: Derive PT - group %d", group);
1050
Hai Shalom899fcc72020-10-19 14:38:18 -07001051 if (ssid_len > 32)
1052 return NULL;
1053
Hai Shalomc3565922019-10-28 11:58:20 -07001054 pt = os_zalloc(sizeof(*pt));
1055 if (!pt)
1056 return NULL;
1057
Hai Shalom899fcc72020-10-19 14:38:18 -07001058#ifdef CONFIG_SAE_PK
1059 os_memcpy(pt->ssid, ssid, ssid_len);
1060 pt->ssid_len = ssid_len;
1061#endif /* CONFIG_SAE_PK */
Hai Shalomc3565922019-10-28 11:58:20 -07001062 pt->group = group;
1063 pt->ec = crypto_ec_init(group);
1064 if (pt->ec) {
1065 pt->ecc_pt = sae_derive_pt_ecc(pt->ec, group, ssid, ssid_len,
1066 password, password_len,
1067 identifier);
1068 if (!pt->ecc_pt) {
1069 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PT");
1070 goto fail;
1071 }
1072
1073 return pt;
1074 }
1075
1076 pt->dh = dh_groups_get(group);
1077 if (!pt->dh) {
1078 wpa_printf(MSG_DEBUG, "SAE: Unsupported group %d", group);
1079 goto fail;
1080 }
1081
1082 pt->ffc_pt = sae_derive_pt_ffc(pt->dh, group, ssid, ssid_len,
1083 password, password_len, identifier);
1084 if (!pt->ffc_pt) {
1085 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PT");
1086 goto fail;
1087 }
1088
1089 return pt;
1090fail:
1091 sae_deinit_pt(pt);
1092 return NULL;
1093}
1094
1095
1096struct sae_pt * sae_derive_pt(int *groups, const u8 *ssid, size_t ssid_len,
1097 const u8 *password, size_t password_len,
1098 const char *identifier)
1099{
1100 struct sae_pt *pt = NULL, *last = NULL, *tmp;
1101 int default_groups[] = { 19, 0 };
1102 int i;
1103
1104 if (!groups)
1105 groups = default_groups;
1106 for (i = 0; groups[i] > 0; i++) {
1107 tmp = sae_derive_pt_group(groups[i], ssid, ssid_len, password,
1108 password_len, identifier);
1109 if (!tmp)
1110 continue;
1111
1112 if (last)
1113 last->next = tmp;
1114 else
1115 pt = tmp;
1116 last = tmp;
1117 }
1118
1119 return pt;
1120}
1121
1122
1123static void sae_max_min_addr(const u8 *addr[], size_t len[],
1124 const u8 *addr1, const u8 *addr2)
1125{
1126 len[0] = ETH_ALEN;
1127 len[1] = ETH_ALEN;
1128 if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) {
1129 addr[0] = addr1;
1130 addr[1] = addr2;
1131 } else {
1132 addr[0] = addr2;
1133 addr[1] = addr1;
1134 }
1135}
1136
1137
1138struct crypto_ec_point *
1139sae_derive_pwe_from_pt_ecc(const struct sae_pt *pt,
1140 const u8 *addr1, const u8 *addr2)
1141{
1142 u8 bin[SAE_MAX_ECC_PRIME_LEN * 2];
1143 size_t prime_len;
1144 const u8 *addr[2];
1145 size_t len[2];
1146 u8 salt[64], hash[64];
1147 size_t hash_len;
1148 const struct crypto_bignum *order;
1149 struct crypto_bignum *tmp = NULL, *val = NULL, *one = NULL;
1150 struct crypto_ec_point *pwe = NULL;
1151
1152 wpa_printf(MSG_DEBUG, "SAE: Derive PWE from PT");
1153 prime_len = crypto_ec_prime_len(pt->ec);
1154 if (crypto_ec_point_to_bin(pt->ec, pt->ecc_pt,
1155 bin, bin + prime_len) < 0)
1156 return NULL;
1157 wpa_hexdump_key(MSG_DEBUG, "SAE: PT.x", bin, prime_len);
1158 wpa_hexdump_key(MSG_DEBUG, "SAE: PT.y", bin + prime_len, prime_len);
1159
1160 sae_max_min_addr(addr, len, addr1, addr2);
1161
1162 /* val = H(0^n,
1163 * MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC)) */
1164 wpa_printf(MSG_DEBUG, "SAE: val = H(0^n, MAX(addrs) || MIN(addrs))");
1165 hash_len = sae_ecc_prime_len_2_hash_len(prime_len);
1166 os_memset(salt, 0, hash_len);
1167 if (hkdf_extract(hash_len, salt, hash_len, 2, addr, len, hash) < 0)
1168 goto fail;
1169 wpa_hexdump(MSG_DEBUG, "SAE: val", hash, hash_len);
1170
1171 /* val = val modulo (q - 1) + 1 */
1172 order = crypto_ec_get_order(pt->ec);
1173 tmp = crypto_bignum_init();
1174 val = crypto_bignum_init_set(hash, hash_len);
1175 one = crypto_bignum_init_uint(1);
1176 if (!tmp || !val || !one ||
1177 crypto_bignum_sub(order, one, tmp) < 0 ||
1178 crypto_bignum_mod(val, tmp, val) < 0 ||
1179 crypto_bignum_add(val, one, val) < 0)
1180 goto fail;
1181 debug_print_bignum("SAE: val(reduced to 1..q-1)", val, prime_len);
1182
1183 /* PWE = scalar-op(val, PT) */
1184 pwe = crypto_ec_point_init(pt->ec);
1185 if (!pwe ||
1186 crypto_ec_point_mul(pt->ec, pt->ecc_pt, val, pwe) < 0 ||
1187 crypto_ec_point_to_bin(pt->ec, pwe, bin, bin + prime_len) < 0) {
1188 crypto_ec_point_deinit(pwe, 1);
1189 pwe = NULL;
1190 goto fail;
1191 }
1192 wpa_hexdump_key(MSG_DEBUG, "SAE: PWE.x", bin, prime_len);
1193 wpa_hexdump_key(MSG_DEBUG, "SAE: PWE.y", bin + prime_len, prime_len);
1194
1195fail:
1196 crypto_bignum_deinit(tmp, 1);
1197 crypto_bignum_deinit(val, 1);
1198 crypto_bignum_deinit(one, 0);
1199 return pwe;
1200}
1201
1202
1203struct crypto_bignum *
1204sae_derive_pwe_from_pt_ffc(const struct sae_pt *pt,
1205 const u8 *addr1, const u8 *addr2)
1206{
1207 size_t prime_len;
1208 const u8 *addr[2];
1209 size_t len[2];
1210 u8 salt[64], hash[64];
1211 size_t hash_len;
1212 struct crypto_bignum *tmp = NULL, *val = NULL, *one = NULL;
1213 struct crypto_bignum *pwe = NULL, *order = NULL, *prime = NULL;
1214
1215 wpa_printf(MSG_DEBUG, "SAE: Derive PWE from PT");
1216 prime = crypto_bignum_init_set(pt->dh->prime, pt->dh->prime_len);
1217 order = crypto_bignum_init_set(pt->dh->order, pt->dh->order_len);
1218 if (!prime || !order)
1219 goto fail;
1220 prime_len = pt->dh->prime_len;
1221
1222 sae_max_min_addr(addr, len, addr1, addr2);
1223
1224 /* val = H(0^n,
1225 * MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC)) */
1226 wpa_printf(MSG_DEBUG, "SAE: val = H(0^n, MAX(addrs) || MIN(addrs))");
1227 hash_len = sae_ffc_prime_len_2_hash_len(prime_len);
1228 os_memset(salt, 0, hash_len);
1229 if (hkdf_extract(hash_len, salt, hash_len, 2, addr, len, hash) < 0)
1230 goto fail;
1231 wpa_hexdump(MSG_DEBUG, "SAE: val", hash, hash_len);
1232
1233 /* val = val modulo (q - 1) + 1 */
1234 tmp = crypto_bignum_init();
1235 val = crypto_bignum_init_set(hash, hash_len);
1236 one = crypto_bignum_init_uint(1);
1237 if (!tmp || !val || !one ||
1238 crypto_bignum_sub(order, one, tmp) < 0 ||
1239 crypto_bignum_mod(val, tmp, val) < 0 ||
1240 crypto_bignum_add(val, one, val) < 0)
1241 goto fail;
1242 debug_print_bignum("SAE: val(reduced to 1..q-1)", val, prime_len);
1243
1244 /* PWE = scalar-op(val, PT) */
1245 pwe = crypto_bignum_init();
1246 if (!pwe || crypto_bignum_exptmod(pt->ffc_pt, val, prime, pwe) < 0) {
1247 crypto_bignum_deinit(pwe, 1);
1248 pwe = NULL;
1249 goto fail;
1250 }
1251 debug_print_bignum("SAE: PWE", pwe, prime_len);
1252
1253fail:
1254 crypto_bignum_deinit(tmp, 1);
1255 crypto_bignum_deinit(val, 1);
1256 crypto_bignum_deinit(one, 0);
1257 crypto_bignum_deinit(prime, 0);
1258 crypto_bignum_deinit(order, 0);
1259 return pwe;
1260}
1261
1262
1263void sae_deinit_pt(struct sae_pt *pt)
1264{
1265 struct sae_pt *prev;
1266
1267 while (pt) {
1268 crypto_ec_point_deinit(pt->ecc_pt, 1);
1269 crypto_bignum_deinit(pt->ffc_pt, 1);
1270 crypto_ec_deinit(pt->ec);
1271 prev = pt;
1272 pt = pt->next;
1273 os_free(prev);
1274 }
1275}
1276
1277
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001278static int sae_derive_commit_element_ecc(struct sae_data *sae,
1279 struct crypto_bignum *mask)
1280{
1281 /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
1282 if (!sae->tmp->own_commit_element_ecc) {
1283 sae->tmp->own_commit_element_ecc =
1284 crypto_ec_point_init(sae->tmp->ec);
1285 if (!sae->tmp->own_commit_element_ecc)
1286 return -1;
1287 }
1288
1289 if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc, mask,
1290 sae->tmp->own_commit_element_ecc) < 0 ||
1291 crypto_ec_point_invert(sae->tmp->ec,
1292 sae->tmp->own_commit_element_ecc) < 0) {
1293 wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
1294 return -1;
1295 }
1296
1297 return 0;
1298}
1299
1300
1301static int sae_derive_commit_element_ffc(struct sae_data *sae,
1302 struct crypto_bignum *mask)
1303{
1304 /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
1305 if (!sae->tmp->own_commit_element_ffc) {
1306 sae->tmp->own_commit_element_ffc = crypto_bignum_init();
1307 if (!sae->tmp->own_commit_element_ffc)
1308 return -1;
1309 }
1310
1311 if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, mask, sae->tmp->prime,
1312 sae->tmp->own_commit_element_ffc) < 0 ||
1313 crypto_bignum_inverse(sae->tmp->own_commit_element_ffc,
1314 sae->tmp->prime,
1315 sae->tmp->own_commit_element_ffc) < 0) {
1316 wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
1317 return -1;
1318 }
1319
1320 return 0;
1321}
1322
1323
1324static int sae_derive_commit(struct sae_data *sae)
1325{
1326 struct crypto_bignum *mask;
Hai Shalom81f62d82019-07-22 12:10:00 -07001327 int ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001328
Hai Shalom81f62d82019-07-22 12:10:00 -07001329 mask = crypto_bignum_init();
1330 if (!sae->tmp->sae_rand)
1331 sae->tmp->sae_rand = crypto_bignum_init();
1332 if (!sae->tmp->own_commit_scalar)
1333 sae->tmp->own_commit_scalar = crypto_bignum_init();
1334 ret = !mask || !sae->tmp->sae_rand || !sae->tmp->own_commit_scalar ||
1335 dragonfly_generate_scalar(sae->tmp->order, sae->tmp->sae_rand,
1336 mask,
1337 sae->tmp->own_commit_scalar) < 0 ||
1338 (sae->tmp->ec &&
1339 sae_derive_commit_element_ecc(sae, mask) < 0) ||
1340 (sae->tmp->dh &&
1341 sae_derive_commit_element_ffc(sae, mask) < 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001342 crypto_bignum_deinit(mask, 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07001343 return ret ? -1 : 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001344}
1345
1346
1347int sae_prepare_commit(const u8 *addr1, const u8 *addr2,
1348 const u8 *password, size_t password_len,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001349 struct sae_data *sae)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001350{
Dmitry Shmidt41712582015-06-29 11:02:15 -07001351 if (sae->tmp == NULL ||
1352 (sae->tmp->ec && sae_derive_pwe_ecc(sae, addr1, addr2, password,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001353 password_len) < 0) ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07001354 (sae->tmp->dh && sae_derive_pwe_ffc(sae, addr1, addr2, password,
Hai Shaloma20dcd72022-02-04 13:43:00 -08001355 password_len) < 0))
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001356 return -1;
Hai Shalomc3565922019-10-28 11:58:20 -07001357
Hai Shalom899fcc72020-10-19 14:38:18 -07001358 sae->h2e = 0;
1359 sae->pk = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07001360 return sae_derive_commit(sae);
1361}
1362
1363
1364int sae_prepare_commit_pt(struct sae_data *sae, const struct sae_pt *pt,
1365 const u8 *addr1, const u8 *addr2,
Hai Shalom899fcc72020-10-19 14:38:18 -07001366 int *rejected_groups, const struct sae_pk *pk)
Hai Shalomc3565922019-10-28 11:58:20 -07001367{
1368 if (!sae->tmp)
1369 return -1;
1370
1371 while (pt) {
1372 if (pt->group == sae->group)
1373 break;
1374 pt = pt->next;
1375 }
1376 if (!pt) {
1377 wpa_printf(MSG_INFO, "SAE: Could not find PT for group %u",
1378 sae->group);
1379 return -1;
1380 }
1381
Hai Shalom899fcc72020-10-19 14:38:18 -07001382#ifdef CONFIG_SAE_PK
1383 os_memcpy(sae->tmp->ssid, pt->ssid, pt->ssid_len);
1384 sae->tmp->ssid_len = pt->ssid_len;
1385 sae->tmp->ap_pk = pk;
1386#endif /* CONFIG_SAE_PK */
Hai Shalomc3565922019-10-28 11:58:20 -07001387 sae->tmp->own_addr_higher = os_memcmp(addr1, addr2, ETH_ALEN) > 0;
1388 wpabuf_free(sae->tmp->own_rejected_groups);
1389 sae->tmp->own_rejected_groups = NULL;
1390 if (rejected_groups) {
1391 int count, i;
1392 struct wpabuf *groups;
1393
1394 count = int_array_len(rejected_groups);
1395 groups = wpabuf_alloc(count * 2);
1396 if (!groups)
1397 return -1;
1398 for (i = 0; i < count; i++)
1399 wpabuf_put_le16(groups, rejected_groups[i]);
1400 sae->tmp->own_rejected_groups = groups;
1401 }
1402
1403 if (pt->ec) {
1404 crypto_ec_point_deinit(sae->tmp->pwe_ecc, 1);
1405 sae->tmp->pwe_ecc = sae_derive_pwe_from_pt_ecc(pt, addr1,
1406 addr2);
1407 if (!sae->tmp->pwe_ecc)
1408 return -1;
1409 }
1410
1411 if (pt->dh) {
1412 crypto_bignum_deinit(sae->tmp->pwe_ffc, 1);
1413 sae->tmp->pwe_ffc = sae_derive_pwe_from_pt_ffc(pt, addr1,
1414 addr2);
1415 if (!sae->tmp->pwe_ffc)
1416 return -1;
1417 }
1418
Hai Shalom899fcc72020-10-19 14:38:18 -07001419 sae->h2e = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07001420 return sae_derive_commit(sae);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001421}
1422
1423
1424static int sae_derive_k_ecc(struct sae_data *sae, u8 *k)
1425{
1426 struct crypto_ec_point *K;
1427 int ret = -1;
1428
1429 K = crypto_ec_point_init(sae->tmp->ec);
1430 if (K == NULL)
1431 goto fail;
1432
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001433 /*
1434 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
1435 * PEER-COMMIT-ELEMENT)))
1436 * If K is identity element (point-at-infinity), reject
1437 * k = F(K) (= x coordinate)
1438 */
1439
1440 if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc,
1441 sae->peer_commit_scalar, K) < 0 ||
1442 crypto_ec_point_add(sae->tmp->ec, K,
1443 sae->tmp->peer_commit_element_ecc, K) < 0 ||
1444 crypto_ec_point_mul(sae->tmp->ec, K, sae->tmp->sae_rand, K) < 0 ||
1445 crypto_ec_point_is_at_infinity(sae->tmp->ec, K) ||
1446 crypto_ec_point_to_bin(sae->tmp->ec, K, k, NULL) < 0) {
1447 wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
1448 goto fail;
1449 }
1450
1451 wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
1452
1453 ret = 0;
1454fail:
1455 crypto_ec_point_deinit(K, 1);
1456 return ret;
1457}
1458
1459
1460static int sae_derive_k_ffc(struct sae_data *sae, u8 *k)
1461{
1462 struct crypto_bignum *K;
1463 int ret = -1;
1464
1465 K = crypto_bignum_init();
1466 if (K == NULL)
1467 goto fail;
1468
1469 /*
1470 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
1471 * PEER-COMMIT-ELEMENT)))
1472 * If K is identity element (one), reject.
1473 * k = F(K) (= x coordinate)
1474 */
1475
1476 if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, sae->peer_commit_scalar,
1477 sae->tmp->prime, K) < 0 ||
1478 crypto_bignum_mulmod(K, sae->tmp->peer_commit_element_ffc,
1479 sae->tmp->prime, K) < 0 ||
1480 crypto_bignum_exptmod(K, sae->tmp->sae_rand, sae->tmp->prime, K) < 0
1481 ||
1482 crypto_bignum_is_one(K) ||
1483 crypto_bignum_to_bin(K, k, SAE_MAX_PRIME_LEN, sae->tmp->prime_len) <
1484 0) {
1485 wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
1486 goto fail;
1487 }
1488
1489 wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
1490
1491 ret = 0;
1492fail:
1493 crypto_bignum_deinit(K, 1);
1494 return ret;
1495}
1496
1497
Hai Shalomc3565922019-10-28 11:58:20 -07001498static int sae_kdf_hash(size_t hash_len, const u8 *k, const char *label,
1499 const u8 *context, size_t context_len,
1500 u8 *out, size_t out_len)
1501{
1502 if (hash_len == 32)
1503 return sha256_prf(k, hash_len, label,
1504 context, context_len, out, out_len);
1505#ifdef CONFIG_SHA384
1506 if (hash_len == 48)
1507 return sha384_prf(k, hash_len, label,
1508 context, context_len, out, out_len);
1509#endif /* CONFIG_SHA384 */
1510#ifdef CONFIG_SHA512
1511 if (hash_len == 64)
1512 return sha512_prf(k, hash_len, label,
1513 context, context_len, out, out_len);
1514#endif /* CONFIG_SHA512 */
1515 return -1;
1516}
1517
1518
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001519static int sae_derive_keys(struct sae_data *sae, const u8 *k)
1520{
Hai Shalomc3565922019-10-28 11:58:20 -07001521 u8 zero[SAE_MAX_HASH_LEN], val[SAE_MAX_PRIME_LEN];
1522 const u8 *salt;
1523 struct wpabuf *rejected_groups = NULL;
1524 u8 keyseed[SAE_MAX_HASH_LEN];
Sunil Ravi89eba102022-09-13 21:04:37 -07001525 u8 keys[2 * SAE_MAX_HASH_LEN + SAE_PMK_LEN_MAX];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001526 struct crypto_bignum *tmp;
1527 int ret = -1;
Hai Shalomc3565922019-10-28 11:58:20 -07001528 size_t hash_len, salt_len, prime_len = sae->tmp->prime_len;
Sunil Ravi89eba102022-09-13 21:04:37 -07001529 size_t pmk_len;
Hai Shalomc3565922019-10-28 11:58:20 -07001530 const u8 *addr[1];
1531 size_t len[1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001532
1533 tmp = crypto_bignum_init();
1534 if (tmp == NULL)
1535 goto fail;
1536
Hai Shalomc3565922019-10-28 11:58:20 -07001537 /* keyseed = H(salt, k)
1538 * KCK || PMK = KDF-Hash-Length(keyseed, "SAE KCK and PMK",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001539 * (commit-scalar + peer-commit-scalar) modulo r)
1540 * PMKID = L((commit-scalar + peer-commit-scalar) modulo r, 0, 128)
Hai Shalom899fcc72020-10-19 14:38:18 -07001541 *
1542 * When SAE-PK is used,
1543 * KCK || PMK || KEK = KDF-Hash-Length(keyseed, "SAE-PK keys", context)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001544 */
Hai Shalom899fcc72020-10-19 14:38:18 -07001545 if (!sae->h2e)
Hai Shalomc3565922019-10-28 11:58:20 -07001546 hash_len = SHA256_MAC_LEN;
1547 else if (sae->tmp->dh)
1548 hash_len = sae_ffc_prime_len_2_hash_len(prime_len);
1549 else
1550 hash_len = sae_ecc_prime_len_2_hash_len(prime_len);
Sunil Ravi89eba102022-09-13 21:04:37 -07001551 if (wpa_key_mgmt_sae_ext_key(sae->akmp))
1552 pmk_len = hash_len;
1553 else
1554 pmk_len = SAE_PMK_LEN;
1555 wpa_printf(MSG_DEBUG, "SAE: Derive keys - H2E=%d AKMP=0x%x = %08x (%s)",
1556 sae->h2e, sae->akmp,
1557 wpa_akm_to_suite(sae->akmp),
1558 wpa_key_mgmt_txt(sae->akmp, WPA_PROTO_RSN));
Hai Shalom899fcc72020-10-19 14:38:18 -07001559 if (sae->h2e && (sae->tmp->own_rejected_groups ||
1560 sae->tmp->peer_rejected_groups)) {
Hai Shalomc3565922019-10-28 11:58:20 -07001561 struct wpabuf *own, *peer;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001562
Hai Shalomc3565922019-10-28 11:58:20 -07001563 own = sae->tmp->own_rejected_groups;
1564 peer = sae->tmp->peer_rejected_groups;
1565 salt_len = 0;
1566 if (own)
1567 salt_len += wpabuf_len(own);
1568 if (peer)
1569 salt_len += wpabuf_len(peer);
1570 rejected_groups = wpabuf_alloc(salt_len);
1571 if (!rejected_groups)
1572 goto fail;
1573 if (sae->tmp->own_addr_higher) {
1574 if (own)
1575 wpabuf_put_buf(rejected_groups, own);
1576 if (peer)
1577 wpabuf_put_buf(rejected_groups, peer);
1578 } else {
1579 if (peer)
1580 wpabuf_put_buf(rejected_groups, peer);
1581 if (own)
1582 wpabuf_put_buf(rejected_groups, own);
1583 }
1584 salt = wpabuf_head(rejected_groups);
1585 salt_len = wpabuf_len(rejected_groups);
1586 } else {
1587 os_memset(zero, 0, hash_len);
1588 salt = zero;
1589 salt_len = hash_len;
1590 }
1591 wpa_hexdump(MSG_DEBUG, "SAE: salt for keyseed derivation",
1592 salt, salt_len);
1593 addr[0] = k;
1594 len[0] = prime_len;
1595 if (hkdf_extract(hash_len, salt, salt_len, 1, addr, len, keyseed) < 0)
Dmitry Shmidte4663042016-04-04 10:07:49 -07001596 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07001597 wpa_hexdump_key(MSG_DEBUG, "SAE: keyseed", keyseed, hash_len);
1598
1599 if (crypto_bignum_add(sae->tmp->own_commit_scalar,
1600 sae->peer_commit_scalar, tmp) < 0 ||
1601 crypto_bignum_mod(tmp, sae->tmp->order, tmp) < 0)
1602 goto fail;
1603 /* IEEE Std 802.11-2016 is not exactly clear on the encoding of the bit
1604 * string that is needed for KCK, PMK, and PMKID derivation, but it
1605 * seems to make most sense to encode the
1606 * (commit-scalar + peer-commit-scalar) mod r part as a bit string by
1607 * zero padding it from left to the length of the order (in full
1608 * octets). */
1609 crypto_bignum_to_bin(tmp, val, sizeof(val), sae->tmp->order_len);
1610 wpa_hexdump(MSG_DEBUG, "SAE: PMKID", val, SAE_PMKID_LEN);
Hai Shalom60840252021-02-19 19:02:11 -08001611
1612#ifdef CONFIG_SAE_PK
1613 if (sae->pk) {
1614 if (sae_kdf_hash(hash_len, keyseed, "SAE-PK keys",
1615 val, sae->tmp->order_len,
Sunil Ravi89eba102022-09-13 21:04:37 -07001616 keys, 2 * hash_len + pmk_len) < 0)
Hai Shalom60840252021-02-19 19:02:11 -08001617 goto fail;
1618 } else {
1619 if (sae_kdf_hash(hash_len, keyseed, "SAE KCK and PMK",
1620 val, sae->tmp->order_len,
Sunil Ravi89eba102022-09-13 21:04:37 -07001621 keys, hash_len + pmk_len) < 0)
Hai Shalom60840252021-02-19 19:02:11 -08001622 goto fail;
1623 }
1624#else /* CONFIG_SAE_PK */
1625 if (sae_kdf_hash(hash_len, keyseed, "SAE KCK and PMK",
Hai Shalomc3565922019-10-28 11:58:20 -07001626 val, sae->tmp->order_len,
Sunil Ravi89eba102022-09-13 21:04:37 -07001627 keys, hash_len + pmk_len) < 0)
Hai Shalomc3565922019-10-28 11:58:20 -07001628 goto fail;
Hai Shalom60840252021-02-19 19:02:11 -08001629#endif /* !CONFIG_SAE_PK */
1630
Hai Shalomc3565922019-10-28 11:58:20 -07001631 forced_memzero(keyseed, sizeof(keyseed));
1632 os_memcpy(sae->tmp->kck, keys, hash_len);
1633 sae->tmp->kck_len = hash_len;
Sunil Ravi89eba102022-09-13 21:04:37 -07001634 os_memcpy(sae->pmk, keys + hash_len, pmk_len);
1635 sae->pmk_len = pmk_len;
Dmitry Shmidtd97138d2015-12-28 13:27:49 -08001636 os_memcpy(sae->pmkid, val, SAE_PMKID_LEN);
Hai Shalom899fcc72020-10-19 14:38:18 -07001637#ifdef CONFIG_SAE_PK
1638 if (sae->pk) {
1639 os_memcpy(sae->tmp->kek, keys + hash_len + SAE_PMK_LEN,
1640 hash_len);
1641 sae->tmp->kek_len = hash_len;
1642 wpa_hexdump_key(MSG_DEBUG, "SAE: KEK for SAE-PK",
1643 sae->tmp->kek, sae->tmp->kek_len);
1644 }
1645#endif /* CONFIG_SAE_PK */
Hai Shalomc3565922019-10-28 11:58:20 -07001646 forced_memzero(keys, sizeof(keys));
1647 wpa_hexdump_key(MSG_DEBUG, "SAE: KCK",
1648 sae->tmp->kck, sae->tmp->kck_len);
Sunil Ravi89eba102022-09-13 21:04:37 -07001649 wpa_hexdump_key(MSG_DEBUG, "SAE: PMK", sae->pmk, sae->pmk_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001650
1651 ret = 0;
1652fail:
Hai Shalomc3565922019-10-28 11:58:20 -07001653 wpabuf_free(rejected_groups);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001654 crypto_bignum_deinit(tmp, 0);
1655 return ret;
1656}
1657
1658
1659int sae_process_commit(struct sae_data *sae)
1660{
1661 u8 k[SAE_MAX_PRIME_LEN];
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001662 if (sae->tmp == NULL ||
1663 (sae->tmp->ec && sae_derive_k_ecc(sae, k) < 0) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001664 (sae->tmp->dh && sae_derive_k_ffc(sae, k) < 0) ||
1665 sae_derive_keys(sae, k) < 0)
1666 return -1;
1667 return 0;
1668}
1669
1670
Hai Shalomfdcde762020-04-02 11:19:20 -07001671int sae_write_commit(struct sae_data *sae, struct wpabuf *buf,
1672 const struct wpabuf *token, const char *identifier)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001673{
1674 u8 *pos;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001675
1676 if (sae->tmp == NULL)
Hai Shalomfdcde762020-04-02 11:19:20 -07001677 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001678
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001679 wpabuf_put_le16(buf, sae->group); /* Finite Cyclic Group */
Hai Shalom899fcc72020-10-19 14:38:18 -07001680 if (!sae->h2e && token) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001681 wpabuf_put_buf(buf, token);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001682 wpa_hexdump(MSG_DEBUG, "SAE: Anti-clogging token",
1683 wpabuf_head(token), wpabuf_len(token));
1684 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001685 pos = wpabuf_put(buf, sae->tmp->prime_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07001686 if (crypto_bignum_to_bin(sae->tmp->own_commit_scalar, pos,
1687 sae->tmp->prime_len, sae->tmp->prime_len) < 0)
1688 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001689 wpa_hexdump(MSG_DEBUG, "SAE: own commit-scalar",
1690 pos, sae->tmp->prime_len);
1691 if (sae->tmp->ec) {
1692 pos = wpabuf_put(buf, 2 * sae->tmp->prime_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07001693 if (crypto_ec_point_to_bin(sae->tmp->ec,
1694 sae->tmp->own_commit_element_ecc,
1695 pos, pos + sae->tmp->prime_len) < 0)
1696 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001697 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(x)",
1698 pos, sae->tmp->prime_len);
1699 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(y)",
1700 pos + sae->tmp->prime_len, sae->tmp->prime_len);
1701 } else {
1702 pos = wpabuf_put(buf, sae->tmp->prime_len);
Hai Shalomfdcde762020-04-02 11:19:20 -07001703 if (crypto_bignum_to_bin(sae->tmp->own_commit_element_ffc, pos,
1704 sae->tmp->prime_len,
1705 sae->tmp->prime_len) < 0)
1706 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001707 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element",
1708 pos, sae->tmp->prime_len);
1709 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001710
1711 if (identifier) {
1712 /* Password Identifier element */
1713 wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
1714 wpabuf_put_u8(buf, 1 + os_strlen(identifier));
1715 wpabuf_put_u8(buf, WLAN_EID_EXT_PASSWORD_IDENTIFIER);
1716 wpabuf_put_str(buf, identifier);
1717 wpa_printf(MSG_DEBUG, "SAE: own Password Identifier: %s",
1718 identifier);
1719 }
Hai Shalomc3565922019-10-28 11:58:20 -07001720
Hai Shalom899fcc72020-10-19 14:38:18 -07001721 if (sae->h2e && sae->tmp->own_rejected_groups) {
Hai Shalomc3565922019-10-28 11:58:20 -07001722 wpa_hexdump_buf(MSG_DEBUG, "SAE: own Rejected Groups",
1723 sae->tmp->own_rejected_groups);
1724 wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
1725 wpabuf_put_u8(buf,
1726 1 + wpabuf_len(sae->tmp->own_rejected_groups));
1727 wpabuf_put_u8(buf, WLAN_EID_EXT_REJECTED_GROUPS);
1728 wpabuf_put_buf(buf, sae->tmp->own_rejected_groups);
1729 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001730
Hai Shalom899fcc72020-10-19 14:38:18 -07001731 if (sae->h2e && token) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001732 wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
1733 wpabuf_put_u8(buf, 1 + wpabuf_len(token));
1734 wpabuf_put_u8(buf, WLAN_EID_EXT_ANTI_CLOGGING_TOKEN);
1735 wpabuf_put_buf(buf, token);
1736 wpa_hexdump_buf(MSG_DEBUG,
1737 "SAE: Anti-clogging token (in container)",
1738 token);
1739 }
1740
Sunil Ravi89eba102022-09-13 21:04:37 -07001741 if (wpa_key_mgmt_sae_ext_key(sae->akmp)) {
1742 u32 suite = wpa_akm_to_suite(sae->akmp);
1743
1744 wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
1745 wpabuf_put_u8(buf, 1 + RSN_SELECTOR_LEN);
1746 wpabuf_put_u8(buf, WLAN_EID_EXT_AKM_SUITE_SELECTOR);
1747 RSN_SELECTOR_PUT(wpabuf_put(buf, RSN_SELECTOR_LEN), suite);
1748 wpa_printf(MSG_DEBUG, "SAE: AKM Suite Selector: %08x", suite);
1749 sae->own_akm_suite_selector = suite;
1750 }
1751
Hai Shalomfdcde762020-04-02 11:19:20 -07001752 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001753}
1754
1755
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001756u16 sae_group_allowed(struct sae_data *sae, int *allowed_groups, u16 group)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001757{
1758 if (allowed_groups) {
1759 int i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001760 for (i = 0; allowed_groups[i] > 0; i++) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001761 if (allowed_groups[i] == group)
1762 break;
1763 }
1764 if (allowed_groups[i] != group) {
1765 wpa_printf(MSG_DEBUG, "SAE: Proposed group %u not "
1766 "enabled in the current configuration",
1767 group);
1768 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1769 }
1770 }
1771
1772 if (sae->state == SAE_COMMITTED && group != sae->group) {
1773 wpa_printf(MSG_DEBUG, "SAE: Do not allow group to be changed");
1774 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1775 }
1776
1777 if (group != sae->group && sae_set_group(sae, group) < 0) {
1778 wpa_printf(MSG_DEBUG, "SAE: Unsupported Finite Cyclic Group %u",
1779 group);
1780 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1781 }
1782
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001783 if (sae->tmp == NULL) {
1784 wpa_printf(MSG_DEBUG, "SAE: Group information not yet initialized");
1785 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1786 }
1787
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001788 if (sae->tmp->dh && !allowed_groups) {
1789 wpa_printf(MSG_DEBUG, "SAE: Do not allow FFC group %u without "
1790 "explicit configuration enabling it", group);
1791 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1792 }
1793
1794 return WLAN_STATUS_SUCCESS;
1795}
1796
1797
Roshan Pius3a1667e2018-07-03 15:17:14 -07001798static int sae_is_password_id_elem(const u8 *pos, const u8 *end)
1799{
1800 return end - pos >= 3 &&
1801 pos[0] == WLAN_EID_EXTENSION &&
1802 pos[1] >= 1 &&
1803 end - pos - 2 >= pos[1] &&
1804 pos[2] == WLAN_EID_EXT_PASSWORD_IDENTIFIER;
1805}
1806
1807
Hai Shalomc3565922019-10-28 11:58:20 -07001808static int sae_is_rejected_groups_elem(const u8 *pos, const u8 *end)
1809{
1810 return end - pos >= 3 &&
1811 pos[0] == WLAN_EID_EXTENSION &&
1812 pos[1] >= 2 &&
1813 end - pos - 2 >= pos[1] &&
1814 pos[2] == WLAN_EID_EXT_REJECTED_GROUPS;
1815}
1816
1817
Hai Shalomfdcde762020-04-02 11:19:20 -07001818static int sae_is_token_container_elem(const u8 *pos, const u8 *end)
1819{
1820 return end - pos >= 3 &&
1821 pos[0] == WLAN_EID_EXTENSION &&
1822 pos[1] >= 1 &&
1823 end - pos - 2 >= pos[1] &&
1824 pos[2] == WLAN_EID_EXT_ANTI_CLOGGING_TOKEN;
1825}
1826
1827
Sunil Ravi89eba102022-09-13 21:04:37 -07001828static int sae_is_akm_suite_selector_elem(const u8 *pos, const u8 *end)
1829{
1830 return end - pos >= 2 + 1 + RSN_SELECTOR_LEN &&
1831 pos[0] == WLAN_EID_EXTENSION &&
1832 pos[1] >= 1 + RSN_SELECTOR_LEN &&
1833 end - pos - 2 >= pos[1] &&
1834 pos[2] == WLAN_EID_EXT_AKM_SUITE_SELECTOR;
1835}
1836
1837
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001838static void sae_parse_commit_token(struct sae_data *sae, const u8 **pos,
1839 const u8 *end, const u8 **token,
Hai Shalomc3565922019-10-28 11:58:20 -07001840 size_t *token_len, int h2e)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001841{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001842 size_t scalar_elem_len, tlen;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001843
1844 if (token)
1845 *token = NULL;
1846 if (token_len)
1847 *token_len = 0;
1848
Hai Shalomfdcde762020-04-02 11:19:20 -07001849 if (h2e)
1850 return; /* No Anti-Clogging Token field outside container IE */
1851
Roshan Pius3a1667e2018-07-03 15:17:14 -07001852 scalar_elem_len = (sae->tmp->ec ? 3 : 2) * sae->tmp->prime_len;
1853 if (scalar_elem_len >= (size_t) (end - *pos))
1854 return; /* No extra data beyond peer scalar and element */
1855
Roshan Pius3a1667e2018-07-03 15:17:14 -07001856 tlen = end - (*pos + scalar_elem_len);
1857
1858 if (tlen < SHA256_MAC_LEN) {
1859 wpa_printf(MSG_DEBUG,
1860 "SAE: Too short optional data (%u octets) to include our Anti-Clogging Token",
1861 (unsigned int) tlen);
1862 return;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001863 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001864
Roshan Pius3a1667e2018-07-03 15:17:14 -07001865 wpa_hexdump(MSG_DEBUG, "SAE: Anti-Clogging Token", *pos, tlen);
1866 if (token)
1867 *token = *pos;
1868 if (token_len)
1869 *token_len = tlen;
1870 *pos += tlen;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001871}
1872
1873
Hai Shalomfdcde762020-04-02 11:19:20 -07001874static void sae_parse_token_container(struct sae_data *sae,
1875 const u8 *pos, const u8 *end,
1876 const u8 **token, size_t *token_len)
1877{
1878 wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
1879 pos, end - pos);
1880 if (!sae_is_token_container_elem(pos, end))
1881 return;
1882 *token = pos + 3;
1883 *token_len = pos[1] - 1;
1884 wpa_hexdump(MSG_DEBUG, "SAE: Anti-Clogging Token (in container)",
1885 *token, *token_len);
1886}
1887
1888
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001889static u16 sae_parse_commit_scalar(struct sae_data *sae, const u8 **pos,
1890 const u8 *end)
1891{
1892 struct crypto_bignum *peer_scalar;
1893
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001894 if (sae->tmp->prime_len > end - *pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001895 wpa_printf(MSG_DEBUG, "SAE: Not enough data for scalar");
1896 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1897 }
1898
1899 peer_scalar = crypto_bignum_init_set(*pos, sae->tmp->prime_len);
1900 if (peer_scalar == NULL)
1901 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1902
1903 /*
1904 * IEEE Std 802.11-2012, 11.3.8.6.1: If there is a protocol instance for
1905 * the peer and it is in Authenticated state, the new Commit Message
1906 * shall be dropped if the peer-scalar is identical to the one used in
1907 * the existing protocol instance.
1908 */
Hai Shalomfdcde762020-04-02 11:19:20 -07001909 if (sae->state == SAE_ACCEPTED && sae->peer_commit_scalar_accepted &&
1910 crypto_bignum_cmp(sae->peer_commit_scalar_accepted,
1911 peer_scalar) == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001912 wpa_printf(MSG_DEBUG, "SAE: Do not accept re-use of previous "
1913 "peer-commit-scalar");
1914 crypto_bignum_deinit(peer_scalar, 0);
1915 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1916 }
1917
Dmitry Shmidt41712582015-06-29 11:02:15 -07001918 /* 1 < scalar < r */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001919 if (crypto_bignum_is_zero(peer_scalar) ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07001920 crypto_bignum_is_one(peer_scalar) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001921 crypto_bignum_cmp(peer_scalar, sae->tmp->order) >= 0) {
1922 wpa_printf(MSG_DEBUG, "SAE: Invalid peer scalar");
1923 crypto_bignum_deinit(peer_scalar, 0);
1924 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1925 }
1926
1927
1928 crypto_bignum_deinit(sae->peer_commit_scalar, 0);
1929 sae->peer_commit_scalar = peer_scalar;
1930 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-scalar",
1931 *pos, sae->tmp->prime_len);
1932 *pos += sae->tmp->prime_len;
1933
1934 return WLAN_STATUS_SUCCESS;
1935}
1936
1937
Roshan Pius3a1667e2018-07-03 15:17:14 -07001938static u16 sae_parse_commit_element_ecc(struct sae_data *sae, const u8 **pos,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001939 const u8 *end)
1940{
1941 u8 prime[SAE_MAX_ECC_PRIME_LEN];
1942
Roshan Pius3a1667e2018-07-03 15:17:14 -07001943 if (2 * sae->tmp->prime_len > end - *pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001944 wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
1945 "commit-element");
1946 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1947 }
1948
1949 if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
1950 sae->tmp->prime_len) < 0)
1951 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1952
1953 /* element x and y coordinates < p */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001954 if (os_memcmp(*pos, prime, sae->tmp->prime_len) >= 0 ||
1955 os_memcmp(*pos + sae->tmp->prime_len, prime,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001956 sae->tmp->prime_len) >= 0) {
1957 wpa_printf(MSG_DEBUG, "SAE: Invalid coordinates in peer "
1958 "element");
1959 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1960 }
1961
1962 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(x)",
Roshan Pius3a1667e2018-07-03 15:17:14 -07001963 *pos, sae->tmp->prime_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001964 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(y)",
Roshan Pius3a1667e2018-07-03 15:17:14 -07001965 *pos + sae->tmp->prime_len, sae->tmp->prime_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001966
1967 crypto_ec_point_deinit(sae->tmp->peer_commit_element_ecc, 0);
1968 sae->tmp->peer_commit_element_ecc =
Roshan Pius3a1667e2018-07-03 15:17:14 -07001969 crypto_ec_point_from_bin(sae->tmp->ec, *pos);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001970 if (sae->tmp->peer_commit_element_ecc == NULL)
1971 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1972
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001973 if (!crypto_ec_point_is_on_curve(sae->tmp->ec,
1974 sae->tmp->peer_commit_element_ecc)) {
1975 wpa_printf(MSG_DEBUG, "SAE: Peer element is not on curve");
1976 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1977 }
1978
Roshan Pius3a1667e2018-07-03 15:17:14 -07001979 *pos += 2 * sae->tmp->prime_len;
1980
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001981 return WLAN_STATUS_SUCCESS;
1982}
1983
1984
Roshan Pius3a1667e2018-07-03 15:17:14 -07001985static u16 sae_parse_commit_element_ffc(struct sae_data *sae, const u8 **pos,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001986 const u8 *end)
1987{
Dmitry Shmidt41712582015-06-29 11:02:15 -07001988 struct crypto_bignum *res, *one;
1989 const u8 one_bin[1] = { 0x01 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001990
Roshan Pius3a1667e2018-07-03 15:17:14 -07001991 if (sae->tmp->prime_len > end - *pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001992 wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
1993 "commit-element");
1994 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1995 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001996 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element", *pos,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001997 sae->tmp->prime_len);
1998
1999 crypto_bignum_deinit(sae->tmp->peer_commit_element_ffc, 0);
2000 sae->tmp->peer_commit_element_ffc =
Roshan Pius3a1667e2018-07-03 15:17:14 -07002001 crypto_bignum_init_set(*pos, sae->tmp->prime_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002002 if (sae->tmp->peer_commit_element_ffc == NULL)
2003 return WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002004 /* 1 < element < p - 1 */
2005 res = crypto_bignum_init();
2006 one = crypto_bignum_init_set(one_bin, sizeof(one_bin));
2007 if (!res || !one ||
2008 crypto_bignum_sub(sae->tmp->prime, one, res) ||
2009 crypto_bignum_is_zero(sae->tmp->peer_commit_element_ffc) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002010 crypto_bignum_is_one(sae->tmp->peer_commit_element_ffc) ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07002011 crypto_bignum_cmp(sae->tmp->peer_commit_element_ffc, res) >= 0) {
2012 crypto_bignum_deinit(res, 0);
2013 crypto_bignum_deinit(one, 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002014 wpa_printf(MSG_DEBUG, "SAE: Invalid peer element");
2015 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2016 }
Dmitry Shmidt41712582015-06-29 11:02:15 -07002017 crypto_bignum_deinit(one, 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002018
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002019 /* scalar-op(r, ELEMENT) = 1 modulo p */
Dmitry Shmidt41712582015-06-29 11:02:15 -07002020 if (crypto_bignum_exptmod(sae->tmp->peer_commit_element_ffc,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002021 sae->tmp->order, sae->tmp->prime, res) < 0 ||
2022 !crypto_bignum_is_one(res)) {
2023 wpa_printf(MSG_DEBUG, "SAE: Invalid peer element (scalar-op)");
2024 crypto_bignum_deinit(res, 0);
2025 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2026 }
2027 crypto_bignum_deinit(res, 0);
2028
Roshan Pius3a1667e2018-07-03 15:17:14 -07002029 *pos += sae->tmp->prime_len;
2030
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002031 return WLAN_STATUS_SUCCESS;
2032}
2033
2034
Roshan Pius3a1667e2018-07-03 15:17:14 -07002035static u16 sae_parse_commit_element(struct sae_data *sae, const u8 **pos,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002036 const u8 *end)
2037{
2038 if (sae->tmp->dh)
2039 return sae_parse_commit_element_ffc(sae, pos, end);
2040 return sae_parse_commit_element_ecc(sae, pos, end);
2041}
2042
2043
Roshan Pius3a1667e2018-07-03 15:17:14 -07002044static int sae_parse_password_identifier(struct sae_data *sae,
Hai Shalomc3565922019-10-28 11:58:20 -07002045 const u8 **pos, const u8 *end)
Roshan Pius3a1667e2018-07-03 15:17:14 -07002046{
Hai Shaloma20dcd72022-02-04 13:43:00 -08002047 const u8 *epos;
2048 u8 len;
2049
Roshan Pius3a1667e2018-07-03 15:17:14 -07002050 wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
Hai Shalomc3565922019-10-28 11:58:20 -07002051 *pos, end - *pos);
2052 if (!sae_is_password_id_elem(*pos, end)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002053 if (sae->tmp->pw_id) {
2054 wpa_printf(MSG_DEBUG,
2055 "SAE: No Password Identifier included, but expected one (%s)",
2056 sae->tmp->pw_id);
2057 return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
2058 }
2059 os_free(sae->tmp->pw_id);
2060 sae->tmp->pw_id = NULL;
2061 return WLAN_STATUS_SUCCESS; /* No Password Identifier */
2062 }
2063
Hai Shaloma20dcd72022-02-04 13:43:00 -08002064 epos = *pos;
2065 epos++; /* skip IE type */
2066 len = *epos++; /* IE length */
2067 if (len > end - epos || len < 1)
2068 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2069 epos++; /* skip ext ID */
2070 len--;
2071
Roshan Pius3a1667e2018-07-03 15:17:14 -07002072 if (sae->tmp->pw_id &&
Hai Shaloma20dcd72022-02-04 13:43:00 -08002073 (len != os_strlen(sae->tmp->pw_id) ||
2074 os_memcmp(sae->tmp->pw_id, epos, len) != 0)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002075 wpa_printf(MSG_DEBUG,
2076 "SAE: The included Password Identifier does not match the expected one (%s)",
2077 sae->tmp->pw_id);
2078 return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
2079 }
2080
2081 os_free(sae->tmp->pw_id);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002082 sae->tmp->pw_id = os_malloc(len + 1);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002083 if (!sae->tmp->pw_id)
2084 return WLAN_STATUS_UNSPECIFIED_FAILURE;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002085 os_memcpy(sae->tmp->pw_id, epos, len);
2086 sae->tmp->pw_id[len] = '\0';
Roshan Pius3a1667e2018-07-03 15:17:14 -07002087 wpa_hexdump_ascii(MSG_DEBUG, "SAE: Received Password Identifier",
Hai Shaloma20dcd72022-02-04 13:43:00 -08002088 sae->tmp->pw_id, len);
2089 *pos = epos + len;
Hai Shalomc3565922019-10-28 11:58:20 -07002090 return WLAN_STATUS_SUCCESS;
2091}
2092
2093
2094static int sae_parse_rejected_groups(struct sae_data *sae,
Hai Shalomfdcde762020-04-02 11:19:20 -07002095 const u8 **pos, const u8 *end)
Hai Shalomc3565922019-10-28 11:58:20 -07002096{
Hai Shaloma20dcd72022-02-04 13:43:00 -08002097 const u8 *epos;
2098 u8 len;
2099
Hai Shalomc3565922019-10-28 11:58:20 -07002100 wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
Hai Shalomfdcde762020-04-02 11:19:20 -07002101 *pos, end - *pos);
2102 if (!sae_is_rejected_groups_elem(*pos, end))
Hai Shalomc3565922019-10-28 11:58:20 -07002103 return WLAN_STATUS_SUCCESS;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002104
2105 epos = *pos;
2106 epos++; /* skip IE type */
2107 len = *epos++; /* IE length */
2108 if (len > end - epos || len < 1)
2109 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2110 epos++; /* skip ext ID */
2111 len--;
2112
Hai Shalomc3565922019-10-28 11:58:20 -07002113 wpabuf_free(sae->tmp->peer_rejected_groups);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002114 sae->tmp->peer_rejected_groups = wpabuf_alloc(len);
Hai Shalomc3565922019-10-28 11:58:20 -07002115 if (!sae->tmp->peer_rejected_groups)
2116 return WLAN_STATUS_UNSPECIFIED_FAILURE;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002117 wpabuf_put_data(sae->tmp->peer_rejected_groups, epos, len);
Hai Shalomc3565922019-10-28 11:58:20 -07002118 wpa_hexdump_buf(MSG_DEBUG, "SAE: Received Rejected Groups list",
2119 sae->tmp->peer_rejected_groups);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002120 *pos = epos + len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002121 return WLAN_STATUS_SUCCESS;
2122}
2123
2124
Sunil Ravi89eba102022-09-13 21:04:37 -07002125static int sae_parse_akm_suite_selector(struct sae_data *sae,
2126 const u8 **pos, const u8 *end)
2127{
2128 const u8 *epos;
2129 u8 len;
2130
2131 wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
2132 *pos, end - *pos);
2133 if (!sae_is_akm_suite_selector_elem(*pos, end))
2134 return WLAN_STATUS_SUCCESS;
2135
2136 epos = *pos;
2137 epos++; /* skip IE type */
2138 len = *epos++; /* IE length */
2139 if (len > end - epos || len < 1)
2140 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2141 epos++; /* skip ext ID */
2142 len--;
2143
2144 if (len < RSN_SELECTOR_LEN)
2145 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2146 sae->peer_akm_suite_selector = RSN_SELECTOR_GET(epos);
2147 wpa_printf(MSG_DEBUG, "SAE: Received AKM Suite Selector: %08x",
2148 sae->peer_akm_suite_selector);
2149 *pos = epos + len;
2150 return WLAN_STATUS_SUCCESS;
2151}
2152
2153
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002154u16 sae_parse_commit(struct sae_data *sae, const u8 *data, size_t len,
Hai Shalomc3565922019-10-28 11:58:20 -07002155 const u8 **token, size_t *token_len, int *allowed_groups,
2156 int h2e)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002157{
2158 const u8 *pos = data, *end = data + len;
2159 u16 res;
2160
2161 /* Check Finite Cyclic Group */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002162 if (end - pos < 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002163 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2164 res = sae_group_allowed(sae, allowed_groups, WPA_GET_LE16(pos));
2165 if (res != WLAN_STATUS_SUCCESS)
2166 return res;
2167 pos += 2;
2168
2169 /* Optional Anti-Clogging Token */
Hai Shalomc3565922019-10-28 11:58:20 -07002170 sae_parse_commit_token(sae, &pos, end, token, token_len, h2e);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002171
2172 /* commit-scalar */
2173 res = sae_parse_commit_scalar(sae, &pos, end);
2174 if (res != WLAN_STATUS_SUCCESS)
2175 return res;
2176
2177 /* commit-element */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002178 res = sae_parse_commit_element(sae, &pos, end);
2179 if (res != WLAN_STATUS_SUCCESS)
2180 return res;
2181
2182 /* Optional Password Identifier element */
Hai Shalomc3565922019-10-28 11:58:20 -07002183 res = sae_parse_password_identifier(sae, &pos, end);
Dmitry Shmidt41712582015-06-29 11:02:15 -07002184 if (res != WLAN_STATUS_SUCCESS)
2185 return res;
2186
Hai Shalomc3565922019-10-28 11:58:20 -07002187 /* Conditional Rejected Groups element */
2188 if (h2e) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002189 res = sae_parse_rejected_groups(sae, &pos, end);
Hai Shalomc3565922019-10-28 11:58:20 -07002190 if (res != WLAN_STATUS_SUCCESS)
2191 return res;
2192 }
2193
Hai Shalomfdcde762020-04-02 11:19:20 -07002194 /* Optional Anti-Clogging Token Container element */
2195 if (h2e)
2196 sae_parse_token_container(sae, pos, end, token, token_len);
2197
Sunil Ravi89eba102022-09-13 21:04:37 -07002198 /* Conditional AKM Suite Selector element */
2199 if (h2e) {
2200 res = sae_parse_akm_suite_selector(sae, &pos, end);
2201 if (res != WLAN_STATUS_SUCCESS)
2202 return res;
2203 }
2204
2205 if (sae->own_akm_suite_selector &&
2206 sae->own_akm_suite_selector != sae->peer_akm_suite_selector) {
2207 wpa_printf(MSG_DEBUG,
2208 "SAE: AKM suite selector mismatch: own=%08x peer=%08x",
2209 sae->own_akm_suite_selector,
2210 sae->peer_akm_suite_selector);
2211 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2212 }
2213
2214 if (!sae->akmp) {
2215 if (sae->peer_akm_suite_selector ==
2216 RSN_AUTH_KEY_MGMT_SAE_EXT_KEY)
2217 sae->akmp = WPA_KEY_MGMT_SAE_EXT_KEY;
2218 else if (sae->peer_akm_suite_selector ==
2219 RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY)
2220 sae->akmp = WPA_KEY_MGMT_FT_SAE_EXT_KEY;
2221 }
2222
Dmitry Shmidt41712582015-06-29 11:02:15 -07002223 /*
2224 * Check whether peer-commit-scalar and PEER-COMMIT-ELEMENT are same as
2225 * the values we sent which would be evidence of a reflection attack.
2226 */
2227 if (!sae->tmp->own_commit_scalar ||
2228 crypto_bignum_cmp(sae->tmp->own_commit_scalar,
2229 sae->peer_commit_scalar) != 0 ||
2230 (sae->tmp->dh &&
2231 (!sae->tmp->own_commit_element_ffc ||
2232 crypto_bignum_cmp(sae->tmp->own_commit_element_ffc,
2233 sae->tmp->peer_commit_element_ffc) != 0)) ||
2234 (sae->tmp->ec &&
2235 (!sae->tmp->own_commit_element_ecc ||
2236 crypto_ec_point_cmp(sae->tmp->ec,
2237 sae->tmp->own_commit_element_ecc,
2238 sae->tmp->peer_commit_element_ecc) != 0)))
2239 return WLAN_STATUS_SUCCESS; /* scalars/elements are different */
2240
2241 /*
2242 * This is a reflection attack - return special value to trigger caller
2243 * to silently discard the frame instead of replying with a specific
2244 * status code.
2245 */
2246 return SAE_SILENTLY_DISCARD;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002247}
2248
2249
Hai Shalomc3565922019-10-28 11:58:20 -07002250static int sae_cn_confirm(struct sae_data *sae, const u8 *sc,
2251 const struct crypto_bignum *scalar1,
2252 const u8 *element1, size_t element1_len,
2253 const struct crypto_bignum *scalar2,
2254 const u8 *element2, size_t element2_len,
2255 u8 *confirm)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002256{
2257 const u8 *addr[5];
2258 size_t len[5];
2259 u8 scalar_b1[SAE_MAX_PRIME_LEN], scalar_b2[SAE_MAX_PRIME_LEN];
2260
2261 /* Confirm
2262 * CN(key, X, Y, Z, ...) =
2263 * HMAC-SHA256(key, D2OS(X) || D2OS(Y) || D2OS(Z) | ...)
2264 * confirm = CN(KCK, send-confirm, commit-scalar, COMMIT-ELEMENT,
2265 * peer-commit-scalar, PEER-COMMIT-ELEMENT)
2266 * verifier = CN(KCK, peer-send-confirm, peer-commit-scalar,
2267 * PEER-COMMIT-ELEMENT, commit-scalar, COMMIT-ELEMENT)
2268 */
Hai Shalomc3565922019-10-28 11:58:20 -07002269 if (crypto_bignum_to_bin(scalar1, scalar_b1, sizeof(scalar_b1),
2270 sae->tmp->prime_len) < 0 ||
2271 crypto_bignum_to_bin(scalar2, scalar_b2, sizeof(scalar_b2),
2272 sae->tmp->prime_len) < 0)
2273 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002274 addr[0] = sc;
2275 len[0] = 2;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002276 addr[1] = scalar_b1;
2277 len[1] = sae->tmp->prime_len;
2278 addr[2] = element1;
2279 len[2] = element1_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002280 addr[3] = scalar_b2;
2281 len[3] = sae->tmp->prime_len;
2282 addr[4] = element2;
2283 len[4] = element2_len;
Hai Shalomc3565922019-10-28 11:58:20 -07002284 return hkdf_extract(sae->tmp->kck_len, sae->tmp->kck, sae->tmp->kck_len,
2285 5, addr, len, confirm);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002286}
2287
2288
Hai Shalomc3565922019-10-28 11:58:20 -07002289static int sae_cn_confirm_ecc(struct sae_data *sae, const u8 *sc,
2290 const struct crypto_bignum *scalar1,
2291 const struct crypto_ec_point *element1,
2292 const struct crypto_bignum *scalar2,
2293 const struct crypto_ec_point *element2,
2294 u8 *confirm)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002295{
2296 u8 element_b1[2 * SAE_MAX_ECC_PRIME_LEN];
2297 u8 element_b2[2 * SAE_MAX_ECC_PRIME_LEN];
2298
Hai Shalomc3565922019-10-28 11:58:20 -07002299 if (crypto_ec_point_to_bin(sae->tmp->ec, element1, element_b1,
2300 element_b1 + sae->tmp->prime_len) < 0 ||
2301 crypto_ec_point_to_bin(sae->tmp->ec, element2, element_b2,
2302 element_b2 + sae->tmp->prime_len) < 0 ||
2303 sae_cn_confirm(sae, sc, scalar1, element_b1,
2304 2 * sae->tmp->prime_len,
2305 scalar2, element_b2, 2 * sae->tmp->prime_len,
2306 confirm) < 0)
2307 return -1;
2308 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002309}
2310
2311
Hai Shalomc3565922019-10-28 11:58:20 -07002312static int sae_cn_confirm_ffc(struct sae_data *sae, const u8 *sc,
2313 const struct crypto_bignum *scalar1,
2314 const struct crypto_bignum *element1,
2315 const struct crypto_bignum *scalar2,
2316 const struct crypto_bignum *element2,
2317 u8 *confirm)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002318{
2319 u8 element_b1[SAE_MAX_PRIME_LEN];
2320 u8 element_b2[SAE_MAX_PRIME_LEN];
2321
Hai Shalomc3565922019-10-28 11:58:20 -07002322 if (crypto_bignum_to_bin(element1, element_b1, sizeof(element_b1),
2323 sae->tmp->prime_len) < 0 ||
2324 crypto_bignum_to_bin(element2, element_b2, sizeof(element_b2),
2325 sae->tmp->prime_len) < 0 ||
2326 sae_cn_confirm(sae, sc, scalar1, element_b1, sae->tmp->prime_len,
2327 scalar2, element_b2, sae->tmp->prime_len,
2328 confirm) < 0)
2329 return -1;
2330 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002331}
2332
2333
Hai Shalom899fcc72020-10-19 14:38:18 -07002334int sae_write_confirm(struct sae_data *sae, struct wpabuf *buf)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002335{
2336 const u8 *sc;
Hai Shalomc3565922019-10-28 11:58:20 -07002337 size_t hash_len;
Hai Shalom899fcc72020-10-19 14:38:18 -07002338 int res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002339
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002340 if (sae->tmp == NULL)
Hai Shalom899fcc72020-10-19 14:38:18 -07002341 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002342
Hai Shalomc3565922019-10-28 11:58:20 -07002343 hash_len = sae->tmp->kck_len;
2344
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002345 /* Send-Confirm */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002346 if (sae->send_confirm < 0xffff)
2347 sae->send_confirm++;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002348 sc = wpabuf_put(buf, 0);
2349 wpabuf_put_le16(buf, sae->send_confirm);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002350
2351 if (sae->tmp->ec)
Hai Shalom899fcc72020-10-19 14:38:18 -07002352 res = sae_cn_confirm_ecc(sae, sc, sae->tmp->own_commit_scalar,
2353 sae->tmp->own_commit_element_ecc,
2354 sae->peer_commit_scalar,
2355 sae->tmp->peer_commit_element_ecc,
2356 wpabuf_put(buf, hash_len));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002357 else
Hai Shalom899fcc72020-10-19 14:38:18 -07002358 res = sae_cn_confirm_ffc(sae, sc, sae->tmp->own_commit_scalar,
2359 sae->tmp->own_commit_element_ffc,
2360 sae->peer_commit_scalar,
2361 sae->tmp->peer_commit_element_ffc,
2362 wpabuf_put(buf, hash_len));
2363 if (res)
2364 return res;
2365
2366#ifdef CONFIG_SAE_PK
2367 if (sae_write_confirm_pk(sae, buf) < 0)
2368 return -1;
2369#endif /* CONFIG_SAE_PK */
2370
2371 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002372}
2373
2374
2375int sae_check_confirm(struct sae_data *sae, const u8 *data, size_t len)
2376{
Hai Shalomc3565922019-10-28 11:58:20 -07002377 u8 verifier[SAE_MAX_HASH_LEN];
2378 size_t hash_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002379
Hai Shalomc3565922019-10-28 11:58:20 -07002380 if (!sae->tmp)
2381 return -1;
2382
2383 hash_len = sae->tmp->kck_len;
2384 if (len < 2 + hash_len) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002385 wpa_printf(MSG_DEBUG, "SAE: Too short confirm message");
2386 return -1;
2387 }
2388
2389 wpa_printf(MSG_DEBUG, "SAE: peer-send-confirm %u", WPA_GET_LE16(data));
2390
Hai Shalomc3565922019-10-28 11:58:20 -07002391 if (!sae->peer_commit_scalar || !sae->tmp->own_commit_scalar) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002392 wpa_printf(MSG_DEBUG, "SAE: Temporary data not yet available");
2393 return -1;
2394 }
2395
Hai Shalom021b0b52019-04-10 11:17:58 -07002396 if (sae->tmp->ec) {
2397 if (!sae->tmp->peer_commit_element_ecc ||
Hai Shalomc3565922019-10-28 11:58:20 -07002398 !sae->tmp->own_commit_element_ecc ||
2399 sae_cn_confirm_ecc(sae, data, sae->peer_commit_scalar,
2400 sae->tmp->peer_commit_element_ecc,
2401 sae->tmp->own_commit_scalar,
2402 sae->tmp->own_commit_element_ecc,
2403 verifier) < 0)
Hai Shalom021b0b52019-04-10 11:17:58 -07002404 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -07002405 } else {
2406 if (!sae->tmp->peer_commit_element_ffc ||
Hai Shalomc3565922019-10-28 11:58:20 -07002407 !sae->tmp->own_commit_element_ffc ||
2408 sae_cn_confirm_ffc(sae, data, sae->peer_commit_scalar,
2409 sae->tmp->peer_commit_element_ffc,
2410 sae->tmp->own_commit_scalar,
2411 sae->tmp->own_commit_element_ffc,
2412 verifier) < 0)
Hai Shalom021b0b52019-04-10 11:17:58 -07002413 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -07002414 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002415
Hai Shalomc3565922019-10-28 11:58:20 -07002416 if (os_memcmp_const(verifier, data + 2, hash_len) != 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002417 wpa_printf(MSG_DEBUG, "SAE: Confirm mismatch");
2418 wpa_hexdump(MSG_DEBUG, "SAE: Received confirm",
Hai Shalomc3565922019-10-28 11:58:20 -07002419 data + 2, hash_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002420 wpa_hexdump(MSG_DEBUG, "SAE: Calculated verifier",
Hai Shalomc3565922019-10-28 11:58:20 -07002421 verifier, hash_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002422 return -1;
2423 }
2424
Hai Shalom899fcc72020-10-19 14:38:18 -07002425#ifdef CONFIG_SAE_PK
2426 if (sae_check_confirm_pk(sae, data + 2 + hash_len,
2427 len - 2 - hash_len) < 0)
2428 return -1;
2429#endif /* CONFIG_SAE_PK */
2430
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002431 return 0;
2432}
Roshan Pius3a1667e2018-07-03 15:17:14 -07002433
2434
2435const char * sae_state_txt(enum sae_state state)
2436{
2437 switch (state) {
2438 case SAE_NOTHING:
2439 return "Nothing";
2440 case SAE_COMMITTED:
2441 return "Committed";
2442 case SAE_CONFIRMED:
2443 return "Confirmed";
2444 case SAE_ACCEPTED:
2445 return "Accepted";
2446 }
2447 return "?";
2448}