blob: 2ab168b8a957a7858f04eb012a88dda69f7c2184 [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"
Hai Shalom021b0b52019-04-10 11:17:58 -070012#include "utils/const_time.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080013#include "crypto/crypto.h"
14#include "crypto/sha256.h"
Hai Shalomc3565922019-10-28 11:58:20 -070015#include "crypto/sha384.h"
16#include "crypto/sha512.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080017#include "crypto/random.h"
18#include "crypto/dh_groups.h"
19#include "ieee802_11_defs.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070020#include "dragonfly.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080021#include "sae.h"
22
23
24int sae_set_group(struct sae_data *sae, int group)
25{
26 struct sae_temporary_data *tmp;
27
Hai Shalom81f62d82019-07-22 12:10:00 -070028#ifdef CONFIG_TESTING_OPTIONS
29 /* Allow all groups for testing purposes in non-production builds. */
30#else /* CONFIG_TESTING_OPTIONS */
31 if (!dragonfly_suitable_group(group, 0)) {
Hai Shalom021b0b52019-04-10 11:17:58 -070032 wpa_printf(MSG_DEBUG, "SAE: Reject unsuitable group %d", group);
33 return -1;
34 }
Hai Shalom81f62d82019-07-22 12:10:00 -070035#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom021b0b52019-04-10 11:17:58 -070036
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080037 sae_clear_data(sae);
38 tmp = sae->tmp = os_zalloc(sizeof(*tmp));
39 if (tmp == NULL)
40 return -1;
41
42 /* First, check if this is an ECC group */
43 tmp->ec = crypto_ec_init(group);
44 if (tmp->ec) {
Roshan Pius3a1667e2018-07-03 15:17:14 -070045 wpa_printf(MSG_DEBUG, "SAE: Selecting supported ECC group %d",
46 group);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080047 sae->group = group;
48 tmp->prime_len = crypto_ec_prime_len(tmp->ec);
49 tmp->prime = crypto_ec_get_prime(tmp->ec);
Hai Shalomc3565922019-10-28 11:58:20 -070050 tmp->order_len = crypto_ec_order_len(tmp->ec);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080051 tmp->order = crypto_ec_get_order(tmp->ec);
52 return 0;
53 }
54
55 /* Not an ECC group, check FFC */
56 tmp->dh = dh_groups_get(group);
57 if (tmp->dh) {
Roshan Pius3a1667e2018-07-03 15:17:14 -070058 wpa_printf(MSG_DEBUG, "SAE: Selecting supported FFC group %d",
59 group);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080060 sae->group = group;
61 tmp->prime_len = tmp->dh->prime_len;
62 if (tmp->prime_len > SAE_MAX_PRIME_LEN) {
63 sae_clear_data(sae);
64 return -1;
65 }
66
67 tmp->prime_buf = crypto_bignum_init_set(tmp->dh->prime,
68 tmp->prime_len);
69 if (tmp->prime_buf == NULL) {
70 sae_clear_data(sae);
71 return -1;
72 }
73 tmp->prime = tmp->prime_buf;
74
Hai Shalomc3565922019-10-28 11:58:20 -070075 tmp->order_len = tmp->dh->order_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080076 tmp->order_buf = crypto_bignum_init_set(tmp->dh->order,
77 tmp->dh->order_len);
78 if (tmp->order_buf == NULL) {
79 sae_clear_data(sae);
80 return -1;
81 }
82 tmp->order = tmp->order_buf;
83
84 return 0;
85 }
86
87 /* Unsupported group */
Roshan Pius3a1667e2018-07-03 15:17:14 -070088 wpa_printf(MSG_DEBUG,
89 "SAE: Group %d not supported by the crypto library", group);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080090 return -1;
91}
92
93
94void sae_clear_temp_data(struct sae_data *sae)
95{
96 struct sae_temporary_data *tmp;
97 if (sae == NULL || sae->tmp == NULL)
98 return;
99 tmp = sae->tmp;
100 crypto_ec_deinit(tmp->ec);
101 crypto_bignum_deinit(tmp->prime_buf, 0);
102 crypto_bignum_deinit(tmp->order_buf, 0);
103 crypto_bignum_deinit(tmp->sae_rand, 1);
104 crypto_bignum_deinit(tmp->pwe_ffc, 1);
105 crypto_bignum_deinit(tmp->own_commit_scalar, 0);
106 crypto_bignum_deinit(tmp->own_commit_element_ffc, 0);
107 crypto_bignum_deinit(tmp->peer_commit_element_ffc, 0);
108 crypto_ec_point_deinit(tmp->pwe_ecc, 1);
109 crypto_ec_point_deinit(tmp->own_commit_element_ecc, 0);
110 crypto_ec_point_deinit(tmp->peer_commit_element_ecc, 0);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800111 wpabuf_free(tmp->anti_clogging_token);
Hai Shalomc3565922019-10-28 11:58:20 -0700112 wpabuf_free(tmp->own_rejected_groups);
113 wpabuf_free(tmp->peer_rejected_groups);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700114 os_free(tmp->pw_id);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800115 bin_clear_free(tmp, sizeof(*tmp));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800116 sae->tmp = NULL;
117}
118
119
120void sae_clear_data(struct sae_data *sae)
121{
122 if (sae == NULL)
123 return;
124 sae_clear_temp_data(sae);
125 crypto_bignum_deinit(sae->peer_commit_scalar, 0);
126 os_memset(sae, 0, sizeof(*sae));
127}
128
129
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800130static void sae_pwd_seed_key(const u8 *addr1, const u8 *addr2, u8 *key)
131{
132 wpa_printf(MSG_DEBUG, "SAE: PWE derivation - addr1=" MACSTR
133 " addr2=" MACSTR, MAC2STR(addr1), MAC2STR(addr2));
134 if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) {
135 os_memcpy(key, addr1, ETH_ALEN);
136 os_memcpy(key + ETH_ALEN, addr2, ETH_ALEN);
137 } else {
138 os_memcpy(key, addr2, ETH_ALEN);
139 os_memcpy(key + ETH_ALEN, addr1, ETH_ALEN);
140 }
141}
142
143
Dmitry Shmidt41712582015-06-29 11:02:15 -0700144static int sae_test_pwd_seed_ecc(struct sae_data *sae, const u8 *pwd_seed,
Hai Shalom021b0b52019-04-10 11:17:58 -0700145 const u8 *prime, const u8 *qr, const u8 *qnr,
146 u8 *pwd_value)
Dmitry Shmidt41712582015-06-29 11:02:15 -0700147{
Dmitry Shmidt41712582015-06-29 11:02:15 -0700148 struct crypto_bignum *y_sqr, *x_cand;
149 int res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800150 size_t bits;
Hai Shalom81f62d82019-07-22 12:10:00 -0700151 int cmp_prime;
152 unsigned int in_range;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800153
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800154 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
155
156 /* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
157 bits = crypto_ec_prime_len_bits(sae->tmp->ec);
Dmitry Shmidte4663042016-04-04 10:07:49 -0700158 if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking",
159 prime, sae->tmp->prime_len, pwd_value, bits) < 0)
160 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800161 if (bits % 8)
Hai Shalom021b0b52019-04-10 11:17:58 -0700162 buf_shift_right(pwd_value, sae->tmp->prime_len, 8 - bits % 8);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800163 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value",
164 pwd_value, sae->tmp->prime_len);
165
Hai Shalom81f62d82019-07-22 12:10:00 -0700166 cmp_prime = const_time_memcmp(pwd_value, prime, sae->tmp->prime_len);
167 /* Create a const_time mask for selection based on prf result
168 * being smaller than prime. */
169 in_range = const_time_fill_msb((unsigned int) cmp_prime);
170 /* The algorithm description would skip the next steps if
171 * cmp_prime >= 0 (reutnr 0 here), but go through them regardless to
172 * minimize externally observable differences in behavior. */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800173
Dmitry Shmidt41712582015-06-29 11:02:15 -0700174 x_cand = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
175 if (!x_cand)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800176 return -1;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700177 y_sqr = crypto_ec_point_compute_y_sqr(sae->tmp->ec, x_cand);
Hai Shalom021b0b52019-04-10 11:17:58 -0700178 crypto_bignum_deinit(x_cand, 1);
179 if (!y_sqr)
Dmitry Shmidt41712582015-06-29 11:02:15 -0700180 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800181
Hai Shalom81f62d82019-07-22 12:10:00 -0700182 res = dragonfly_is_quadratic_residue_blind(sae->tmp->ec, qr, qnr,
183 y_sqr);
Dmitry Shmidt41712582015-06-29 11:02:15 -0700184 crypto_bignum_deinit(y_sqr, 1);
Hai Shalom81f62d82019-07-22 12:10:00 -0700185 if (res < 0)
186 return res;
187 return const_time_select_int(in_range, res, 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800188}
189
190
Hai Shalom021b0b52019-04-10 11:17:58 -0700191/* Returns -1 on fatal failure, 0 if PWE cannot be derived from the provided
192 * pwd-seed, or 1 if a valid PWE was derived from pwd-seed. */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800193static int sae_test_pwd_seed_ffc(struct sae_data *sae, const u8 *pwd_seed,
194 struct crypto_bignum *pwe)
195{
196 u8 pwd_value[SAE_MAX_PRIME_LEN];
197 size_t bits = sae->tmp->prime_len * 8;
198 u8 exp[1];
Hai Shalom021b0b52019-04-10 11:17:58 -0700199 struct crypto_bignum *a, *b = NULL;
200 int res, is_val;
201 u8 pwd_value_valid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800202
203 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
204
205 /* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
Dmitry Shmidte4663042016-04-04 10:07:49 -0700206 if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking",
207 sae->tmp->dh->prime, sae->tmp->prime_len, pwd_value,
208 bits) < 0)
209 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800210 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value", pwd_value,
211 sae->tmp->prime_len);
212
Hai Shalom021b0b52019-04-10 11:17:58 -0700213 /* Check whether pwd-value < p */
214 res = const_time_memcmp(pwd_value, sae->tmp->dh->prime,
215 sae->tmp->prime_len);
216 /* pwd-value >= p is invalid, so res is < 0 for the valid cases and
217 * the negative sign can be used to fill the mask for constant time
218 * selection */
219 pwd_value_valid = const_time_fill_msb(res);
220
221 /* If pwd-value >= p, force pwd-value to be < p and perform the
222 * calculations anyway to hide timing difference. The derived PWE will
223 * be ignored in that case. */
224 pwd_value[0] = const_time_select_u8(pwd_value_valid, pwd_value[0], 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800225
226 /* PWE = pwd-value^((p-1)/r) modulo p */
227
Hai Shalom021b0b52019-04-10 11:17:58 -0700228 res = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800229 a = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
Hai Shalom021b0b52019-04-10 11:17:58 -0700230 if (!a)
231 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800232
Hai Shalom021b0b52019-04-10 11:17:58 -0700233 /* This is an optimization based on the used group that does not depend
234 * on the password in any way, so it is fine to use separate branches
235 * for this step without constant time operations. */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800236 if (sae->tmp->dh->safe_prime) {
237 /*
238 * r = (p-1)/2 for the group used here, so this becomes:
239 * PWE = pwd-value^2 modulo p
240 */
241 exp[0] = 2;
242 b = crypto_bignum_init_set(exp, sizeof(exp));
243 } else {
244 /* Calculate exponent: (p-1)/r */
245 exp[0] = 1;
246 b = crypto_bignum_init_set(exp, sizeof(exp));
247 if (b == NULL ||
248 crypto_bignum_sub(sae->tmp->prime, b, b) < 0 ||
Hai Shalom021b0b52019-04-10 11:17:58 -0700249 crypto_bignum_div(b, sae->tmp->order, b) < 0)
250 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800251 }
252
Hai Shalom021b0b52019-04-10 11:17:58 -0700253 if (!b)
254 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800255
Hai Shalom021b0b52019-04-10 11:17:58 -0700256 res = crypto_bignum_exptmod(a, b, sae->tmp->prime, pwe);
257 if (res < 0)
258 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800259
Hai Shalom021b0b52019-04-10 11:17:58 -0700260 /* There were no fatal errors in calculations, so determine the return
261 * value using constant time operations. We get here for number of
262 * invalid cases which are cleared here after having performed all the
263 * computation. PWE is valid if pwd-value was less than prime and
264 * PWE > 1. Start with pwd-value check first and then use constant time
265 * operations to clear res to 0 if PWE is 0 or 1.
266 */
267 res = const_time_select_u8(pwd_value_valid, 1, 0);
268 is_val = crypto_bignum_is_zero(pwe);
269 res = const_time_select_u8(const_time_is_zero(is_val), res, 0);
270 is_val = crypto_bignum_is_one(pwe);
271 res = const_time_select_u8(const_time_is_zero(is_val), res, 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800272
Hai Shalom021b0b52019-04-10 11:17:58 -0700273fail:
274 crypto_bignum_deinit(a, 1);
275 crypto_bignum_deinit(b, 1);
276 return res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800277}
278
279
280static int sae_derive_pwe_ecc(struct sae_data *sae, const u8 *addr1,
281 const u8 *addr2, const u8 *password,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700282 size_t password_len, const char *identifier)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800283{
Hai Shalomc3565922019-10-28 11:58:20 -0700284 u8 counter, k;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800285 u8 addrs[2 * ETH_ALEN];
Roshan Pius3a1667e2018-07-03 15:17:14 -0700286 const u8 *addr[3];
287 size_t len[3];
288 size_t num_elem;
Hai Shalom021b0b52019-04-10 11:17:58 -0700289 u8 *dummy_password, *tmp_password;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700290 int pwd_seed_odd = 0;
291 u8 prime[SAE_MAX_ECC_PRIME_LEN];
292 size_t prime_len;
Hai Shalom021b0b52019-04-10 11:17:58 -0700293 struct crypto_bignum *x = NULL, *qr = NULL, *qnr = NULL;
294 u8 x_bin[SAE_MAX_ECC_PRIME_LEN];
295 u8 x_cand_bin[SAE_MAX_ECC_PRIME_LEN];
296 u8 qr_bin[SAE_MAX_ECC_PRIME_LEN];
297 u8 qnr_bin[SAE_MAX_ECC_PRIME_LEN];
Hai Shalom021b0b52019-04-10 11:17:58 -0700298 int res = -1;
299 u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
300 * mask */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800301
Hai Shalom021b0b52019-04-10 11:17:58 -0700302 os_memset(x_bin, 0, sizeof(x_bin));
303
304 dummy_password = os_malloc(password_len);
305 tmp_password = os_malloc(password_len);
306 if (!dummy_password || !tmp_password ||
307 random_get_bytes(dummy_password, password_len) < 0)
308 goto fail;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700309
310 prime_len = sae->tmp->prime_len;
311 if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
312 prime_len) < 0)
Hai Shalom021b0b52019-04-10 11:17:58 -0700313 goto fail;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700314
315 /*
316 * Create a random quadratic residue (qr) and quadratic non-residue
317 * (qnr) modulo p for blinding purposes during the loop.
318 */
Hai Shalom81f62d82019-07-22 12:10:00 -0700319 if (dragonfly_get_random_qr_qnr(sae->tmp->prime, &qr, &qnr) < 0 ||
Hai Shalom021b0b52019-04-10 11:17:58 -0700320 crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin), prime_len) < 0 ||
321 crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin), prime_len) < 0)
322 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800323
324 wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
325 password, password_len);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700326 if (identifier)
327 wpa_printf(MSG_DEBUG, "SAE: password identifier: %s",
328 identifier);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800329
330 /*
331 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
Roshan Pius3a1667e2018-07-03 15:17:14 -0700332 * base = password [|| identifier]
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800333 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
Dmitry Shmidt41712582015-06-29 11:02:15 -0700334 * base || counter)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800335 */
336 sae_pwd_seed_key(addr1, addr2, addrs);
337
Hai Shalom021b0b52019-04-10 11:17:58 -0700338 addr[0] = tmp_password;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800339 len[0] = password_len;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700340 num_elem = 1;
341 if (identifier) {
342 addr[num_elem] = (const u8 *) identifier;
343 len[num_elem] = os_strlen(identifier);
344 num_elem++;
345 }
346 addr[num_elem] = &counter;
347 len[num_elem] = sizeof(counter);
348 num_elem++;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800349
350 /*
351 * Continue for at least k iterations to protect against side-channel
352 * attacks that attempt to determine the number of iterations required
353 * in the loop.
354 */
Hai Shalomc3565922019-10-28 11:58:20 -0700355 k = dragonfly_min_pwe_loop_iter(sae->group);
356
Hai Shalom021b0b52019-04-10 11:17:58 -0700357 for (counter = 1; counter <= k || !found; counter++) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800358 u8 pwd_seed[SHA256_MAC_LEN];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800359
360 if (counter > 200) {
361 /* This should not happen in practice */
362 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
363 break;
364 }
365
Hai Shalom021b0b52019-04-10 11:17:58 -0700366 wpa_printf(MSG_DEBUG, "SAE: counter = %03u", counter);
367 const_time_select_bin(found, dummy_password, password,
368 password_len, tmp_password);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700369 if (hmac_sha256_vector(addrs, sizeof(addrs), num_elem,
370 addr, len, pwd_seed) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800371 break;
Dmitry Shmidt41712582015-06-29 11:02:15 -0700372
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800373 res = sae_test_pwd_seed_ecc(sae, pwd_seed,
Hai Shalom021b0b52019-04-10 11:17:58 -0700374 prime, qr_bin, qnr_bin, x_cand_bin);
375 const_time_select_bin(found, x_bin, x_cand_bin, prime_len,
376 x_bin);
377 pwd_seed_odd = const_time_select_u8(
378 found, pwd_seed_odd,
379 pwd_seed[SHA256_MAC_LEN - 1] & 0x01);
380 os_memset(pwd_seed, 0, sizeof(pwd_seed));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800381 if (res < 0)
Dmitry Shmidt41712582015-06-29 11:02:15 -0700382 goto fail;
Hai Shalom021b0b52019-04-10 11:17:58 -0700383 /* Need to minimize differences in handling res == 0 and 1 here
384 * to avoid differences in timing and instruction cache access,
385 * so use const_time_select_*() to make local copies of the
386 * values based on whether this loop iteration was the one that
387 * found the pwd-seed/x. */
Dmitry Shmidt41712582015-06-29 11:02:15 -0700388
Hai Shalom021b0b52019-04-10 11:17:58 -0700389 /* found is 0 or 0xff here and res is 0 or 1. Bitwise OR of them
390 * (with res converted to 0/0xff) handles this in constant time.
391 */
392 found |= res * 0xff;
393 wpa_printf(MSG_DEBUG, "SAE: pwd-seed result %d found=0x%02x",
394 res, found);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800395 }
396
Hai Shalom021b0b52019-04-10 11:17:58 -0700397 if (!found) {
Dmitry Shmidt41712582015-06-29 11:02:15 -0700398 wpa_printf(MSG_DEBUG, "SAE: Could not generate PWE");
399 res = -1;
400 goto fail;
401 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800402
Hai Shalom021b0b52019-04-10 11:17:58 -0700403 x = crypto_bignum_init_set(x_bin, prime_len);
404 if (!x) {
405 res = -1;
406 goto fail;
407 }
408
Dmitry Shmidt41712582015-06-29 11:02:15 -0700409 if (!sae->tmp->pwe_ecc)
410 sae->tmp->pwe_ecc = crypto_ec_point_init(sae->tmp->ec);
411 if (!sae->tmp->pwe_ecc)
412 res = -1;
413 else
414 res = crypto_ec_point_solve_y_coord(sae->tmp->ec,
415 sae->tmp->pwe_ecc, x,
416 pwd_seed_odd);
Dmitry Shmidt41712582015-06-29 11:02:15 -0700417 if (res < 0) {
418 /*
419 * This should not happen since we already checked that there
420 * is a result.
421 */
422 wpa_printf(MSG_DEBUG, "SAE: Could not solve y");
423 }
424
425fail:
426 crypto_bignum_deinit(qr, 0);
427 crypto_bignum_deinit(qnr, 0);
Hai Shalom021b0b52019-04-10 11:17:58 -0700428 os_free(dummy_password);
429 bin_clear_free(tmp_password, password_len);
430 crypto_bignum_deinit(x, 1);
431 os_memset(x_bin, 0, sizeof(x_bin));
432 os_memset(x_cand_bin, 0, sizeof(x_cand_bin));
Dmitry Shmidt41712582015-06-29 11:02:15 -0700433
434 return res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800435}
436
437
438static int sae_derive_pwe_ffc(struct sae_data *sae, const u8 *addr1,
439 const u8 *addr2, const u8 *password,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700440 size_t password_len, const char *identifier)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800441{
Hai Shalom021b0b52019-04-10 11:17:58 -0700442 u8 counter, k, sel_counter = 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800443 u8 addrs[2 * ETH_ALEN];
Roshan Pius3a1667e2018-07-03 15:17:14 -0700444 const u8 *addr[3];
445 size_t len[3];
446 size_t num_elem;
Hai Shalom021b0b52019-04-10 11:17:58 -0700447 u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
448 * mask */
449 u8 mask;
450 struct crypto_bignum *pwe;
451 size_t prime_len = sae->tmp->prime_len * 8;
452 u8 *pwe_buf;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800453
Hai Shalom021b0b52019-04-10 11:17:58 -0700454 crypto_bignum_deinit(sae->tmp->pwe_ffc, 1);
455 sae->tmp->pwe_ffc = NULL;
456
457 /* Allocate a buffer to maintain selected and candidate PWE for constant
458 * time selection. */
459 pwe_buf = os_zalloc(prime_len * 2);
460 pwe = crypto_bignum_init();
461 if (!pwe_buf || !pwe)
462 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800463
464 wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
465 password, password_len);
466
467 /*
468 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
469 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
Roshan Pius3a1667e2018-07-03 15:17:14 -0700470 * password [|| identifier] || counter)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800471 */
472 sae_pwd_seed_key(addr1, addr2, addrs);
473
474 addr[0] = password;
475 len[0] = password_len;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700476 num_elem = 1;
477 if (identifier) {
478 addr[num_elem] = (const u8 *) identifier;
479 len[num_elem] = os_strlen(identifier);
480 num_elem++;
481 }
482 addr[num_elem] = &counter;
483 len[num_elem] = sizeof(counter);
484 num_elem++;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800485
Hai Shalomc3565922019-10-28 11:58:20 -0700486 k = dragonfly_min_pwe_loop_iter(sae->group);
Hai Shalom021b0b52019-04-10 11:17:58 -0700487
488 for (counter = 1; counter <= k || !found; counter++) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800489 u8 pwd_seed[SHA256_MAC_LEN];
490 int res;
491
492 if (counter > 200) {
493 /* This should not happen in practice */
494 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
495 break;
496 }
497
Hai Shalom021b0b52019-04-10 11:17:58 -0700498 wpa_printf(MSG_DEBUG, "SAE: counter = %02u", counter);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700499 if (hmac_sha256_vector(addrs, sizeof(addrs), num_elem,
500 addr, len, pwd_seed) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800501 break;
Hai Shalom021b0b52019-04-10 11:17:58 -0700502 res = sae_test_pwd_seed_ffc(sae, pwd_seed, pwe);
503 /* res is -1 for fatal failure, 0 if a valid PWE was not found,
504 * or 1 if a valid PWE was found. */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800505 if (res < 0)
506 break;
Hai Shalom021b0b52019-04-10 11:17:58 -0700507 /* Store the candidate PWE into the second half of pwe_buf and
508 * the selected PWE in the beginning of pwe_buf using constant
509 * time selection. */
510 if (crypto_bignum_to_bin(pwe, pwe_buf + prime_len, prime_len,
511 prime_len) < 0)
512 break;
513 const_time_select_bin(found, pwe_buf, pwe_buf + prime_len,
514 prime_len, pwe_buf);
515 sel_counter = const_time_select_u8(found, sel_counter, counter);
516 mask = const_time_eq_u8(res, 1);
517 found = const_time_select_u8(found, found, mask);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800518 }
519
Hai Shalom021b0b52019-04-10 11:17:58 -0700520 if (!found)
521 goto fail;
522
523 wpa_printf(MSG_DEBUG, "SAE: Use PWE from counter = %02u", sel_counter);
524 sae->tmp->pwe_ffc = crypto_bignum_init_set(pwe_buf, prime_len);
525fail:
526 crypto_bignum_deinit(pwe, 1);
527 bin_clear_free(pwe_buf, prime_len * 2);
528 return sae->tmp->pwe_ffc ? 0 : -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800529}
530
531
Hai Shalomc3565922019-10-28 11:58:20 -0700532static int hkdf_extract(size_t hash_len, const u8 *salt, size_t salt_len,
533 size_t num_elem, const u8 *addr[], const size_t len[],
534 u8 *prk)
535{
536 if (hash_len == 32)
537 return hmac_sha256_vector(salt, salt_len, num_elem, addr, len,
538 prk);
539#ifdef CONFIG_SHA384
540 if (hash_len == 48)
541 return hmac_sha384_vector(salt, salt_len, num_elem, addr, len,
542 prk);
543#endif /* CONFIG_SHA384 */
544#ifdef CONFIG_SHA512
545 if (hash_len == 64)
546 return hmac_sha512_vector(salt, salt_len, num_elem, addr, len,
547 prk);
548#endif /* CONFIG_SHA512 */
549 return -1;
550}
551
552
553static int hkdf_expand(size_t hash_len, const u8 *prk, size_t prk_len,
554 const char *info, u8 *okm, size_t okm_len)
555{
556 size_t info_len = os_strlen(info);
557
558 if (hash_len == 32)
559 return hmac_sha256_kdf(prk, prk_len, NULL,
560 (const u8 *) info, info_len,
561 okm, okm_len);
562#ifdef CONFIG_SHA384
563 if (hash_len == 48)
564 return hmac_sha384_kdf(prk, prk_len, NULL,
565 (const u8 *) info, info_len,
566 okm, okm_len);
567#endif /* CONFIG_SHA384 */
568#ifdef CONFIG_SHA512
569 if (hash_len == 64)
570 return hmac_sha512_kdf(prk, prk_len, NULL,
571 (const u8 *) info, info_len,
572 okm, okm_len);
573#endif /* CONFIG_SHA512 */
574 return -1;
575}
576
577
578static int sswu_curve_param(int group, int *z)
579{
580 switch (group) {
581 case 19:
582 case 20:
583 case 21:
584 case 28:
585 *z = -2;
586 return 0;
587 case 25:
588 case 29:
589 *z = -5;
590 return 0;
591 case 26:
592 *z = -11;
593 return 0;
594 case 30:
595 *z = 2;
596 return 0;
597 }
598
599 return -1;
600}
601
602
603static void debug_print_bignum(const char *title, const struct crypto_bignum *a,
604 size_t prime_len)
605{
606 u8 *bin;
607
608 bin = os_malloc(prime_len);
609 if (bin && crypto_bignum_to_bin(a, bin, prime_len, prime_len) >= 0)
610 wpa_hexdump_key(MSG_DEBUG, title, bin, prime_len);
611 else
612 wpa_printf(MSG_DEBUG, "Could not print bignum (%s)", title);
613 bin_clear_free(bin, prime_len);
614}
615
616
617static struct crypto_ec_point * sswu(struct crypto_ec *ec, int group,
618 const struct crypto_bignum *u)
619{
620 int z_int;
621 const struct crypto_bignum *a, *b, *prime;
622 struct crypto_bignum *u2, *t1, *t2, *z, *t, *zero, *one, *two, *three,
623 *x1a, *x1b, *y = NULL;
624 struct crypto_bignum *x1 = NULL, *x2, *gx1, *gx2, *v = NULL;
625 unsigned int m_is_zero, is_qr, is_eq;
626 size_t prime_len;
627 u8 bin[SAE_MAX_ECC_PRIME_LEN];
628 u8 bin1[SAE_MAX_ECC_PRIME_LEN];
629 u8 bin2[SAE_MAX_ECC_PRIME_LEN];
630 u8 x_y[2 * SAE_MAX_ECC_PRIME_LEN];
631 struct crypto_ec_point *p = NULL;
632
633 if (sswu_curve_param(group, &z_int) < 0)
634 return NULL;
635
636 prime = crypto_ec_get_prime(ec);
637 prime_len = crypto_ec_prime_len(ec);
638 a = crypto_ec_get_a(ec);
639 b = crypto_ec_get_b(ec);
640
641 u2 = crypto_bignum_init();
642 t1 = crypto_bignum_init();
643 t2 = crypto_bignum_init();
644 z = crypto_bignum_init_uint(abs(z_int));
645 t = crypto_bignum_init();
646 zero = crypto_bignum_init_uint(0);
647 one = crypto_bignum_init_uint(1);
648 two = crypto_bignum_init_uint(2);
649 three = crypto_bignum_init_uint(3);
650 x1a = crypto_bignum_init();
651 x1b = crypto_bignum_init();
652 x2 = crypto_bignum_init();
653 gx1 = crypto_bignum_init();
654 gx2 = crypto_bignum_init();
655 if (!u2 || !t1 || !t2 || !z || !t || !zero || !one || !two || !three ||
656 !x1a || !x1b || !x2 || !gx1 || !gx2)
657 goto fail;
658
659 if (z_int < 0 && crypto_bignum_sub(prime, z, z) < 0)
660 goto fail;
661
662 /* m = z^2 * u^4 + z * u^2 */
663 /* --> tmp = z * u^2, m = tmp^2 + tmp */
664
665 /* u2 = u^2
666 * t1 = z * u2
667 * t2 = t1^2
668 * m = t1 = t1 + t2 */
669 if (crypto_bignum_sqrmod(u, prime, u2) < 0 ||
670 crypto_bignum_mulmod(z, u2, prime, t1) < 0 ||
671 crypto_bignum_sqrmod(t1, prime, t2) < 0 ||
672 crypto_bignum_addmod(t1, t2, prime, t1) < 0)
673 goto fail;
674 debug_print_bignum("SSWU: m", t1, prime_len);
675
676 /* l = CEQ(m, 0)
677 * t = CSEL(l, 0, inverse(m); where inverse(x) is calculated as
678 * x^(p-2) modulo p which will handle m == 0 case correctly */
679 /* TODO: Make sure crypto_bignum_is_zero() is constant time */
680 m_is_zero = const_time_eq(crypto_bignum_is_zero(t1), 1);
681 /* t = m^(p-2) modulo p */
682 if (crypto_bignum_sub(prime, two, t2) < 0 ||
683 crypto_bignum_exptmod(t1, t2, prime, t) < 0)
684 goto fail;
685 debug_print_bignum("SSWU: t", t, prime_len);
686
687 /* b / (z * a) */
688 if (crypto_bignum_mulmod(z, a, prime, t1) < 0 ||
689 crypto_bignum_inverse(t1, prime, t1) < 0 ||
690 crypto_bignum_mulmod(b, t1, prime, x1a) < 0)
691 goto fail;
692 debug_print_bignum("SSWU: x1a = b / (z * a)", x1a, prime_len);
693
694 /* (-b/a) * (1 + t) */
695 if (crypto_bignum_sub(prime, b, t1) < 0 ||
696 crypto_bignum_inverse(a, prime, t2) < 0 ||
697 crypto_bignum_mulmod(t1, t2, prime, t1) < 0 ||
698 crypto_bignum_addmod(one, t, prime, t2) < 0 ||
699 crypto_bignum_mulmod(t1, t2, prime, x1b) < 0)
700 goto fail;
701 debug_print_bignum("SSWU: x1b = (-b/a) * (1 + t)", x1b, prime_len);
702
703 /* x1 = CSEL(CEQ(m, 0), x1a, x1b) */
704 if (crypto_bignum_to_bin(x1a, bin1, sizeof(bin1), prime_len) < 0 ||
705 crypto_bignum_to_bin(x1b, bin2, sizeof(bin2), prime_len) < 0)
706 goto fail;
707 const_time_select_bin(m_is_zero, bin1, bin2, prime_len, bin);
708 x1 = crypto_bignum_init_set(bin, prime_len);
709 debug_print_bignum("SSWU: x1 = CSEL(l, x1a, x1b)", x1, prime_len);
710
711 /* gx1 = x1^3 + a * x1 + b */
712 if (crypto_bignum_exptmod(x1, three, prime, t1) < 0 ||
713 crypto_bignum_mulmod(a, x1, prime, t2) < 0 ||
714 crypto_bignum_addmod(t1, t2, prime, t1) < 0 ||
715 crypto_bignum_addmod(t1, b, prime, gx1) < 0)
716 goto fail;
717 debug_print_bignum("SSWU: gx1 = x1^3 + a * x1 + b", gx1, prime_len);
718
719 /* x2 = z * u^2 * x1 */
720 if (crypto_bignum_mulmod(z, u2, prime, t1) < 0 ||
721 crypto_bignum_mulmod(t1, x1, prime, x2) < 0)
722 goto fail;
723 debug_print_bignum("SSWU: x2 = z * u^2 * x1", x2, prime_len);
724
725 /* gx2 = x2^3 + a * x2 + b */
726 if (crypto_bignum_exptmod(x2, three, prime, t1) < 0 ||
727 crypto_bignum_mulmod(a, x2, prime, t2) < 0 ||
728 crypto_bignum_addmod(t1, t2, prime, t1) < 0 ||
729 crypto_bignum_addmod(t1, b, prime, gx2) < 0)
730 goto fail;
731 debug_print_bignum("SSWU: gx2 = x2^3 + a * x2 + b", gx2, prime_len);
732
733 /* l = gx1 is a quadratic residue modulo p
734 * --> gx1^((p-1)/2) modulo p is zero or one */
735 if (crypto_bignum_sub(prime, one, t1) < 0 ||
736 crypto_bignum_rshift(t1, 1, t1) < 0 ||
737 crypto_bignum_exptmod(gx1, t1, prime, t1) < 0)
738 goto fail;
739 debug_print_bignum("SSWU: gx1^((p-1)/2) modulo p", t1, prime_len);
740 is_qr = const_time_eq(crypto_bignum_is_zero(t1) |
741 crypto_bignum_is_one(t1), 1);
742
743 /* v = CSEL(l, gx1, gx2) */
744 if (crypto_bignum_to_bin(gx1, bin1, sizeof(bin1), prime_len) < 0 ||
745 crypto_bignum_to_bin(gx2, bin2, sizeof(bin2), prime_len) < 0)
746 goto fail;
747 const_time_select_bin(is_qr, bin1, bin2, prime_len, bin);
748 v = crypto_bignum_init_set(bin, prime_len);
749 debug_print_bignum("SSWU: v = CSEL(l, gx1, gx2)", v, prime_len);
750
751 /* x = CSEL(l, x1, x2) */
752 if (crypto_bignum_to_bin(x1, bin1, sizeof(bin1), prime_len) < 0 ||
753 crypto_bignum_to_bin(x2, bin2, sizeof(bin2), prime_len) < 0)
754 goto fail;
755 const_time_select_bin(is_qr, bin1, bin2, prime_len, x_y);
756 wpa_hexdump_key(MSG_DEBUG, "SSWU: x = CSEL(l, x1, x2)", x_y, prime_len);
757
758 /* y = sqrt(v)
759 * For prime p such that p = 3 mod 4 --> v^((p+1)/4) */
760 if (crypto_bignum_to_bin(prime, bin1, sizeof(bin1), prime_len) < 0)
761 goto fail;
762 if ((bin1[prime_len - 1] & 0x03) != 3) {
763 wpa_printf(MSG_DEBUG, "SSWU: prime does not have p = 3 mod 4");
764 goto fail;
765 }
766 y = crypto_bignum_init();
767 if (!y ||
768 crypto_bignum_add(prime, one, t1) < 0 ||
769 crypto_bignum_rshift(t1, 2, t1) < 0 ||
770 crypto_bignum_exptmod(v, t1, prime, y) < 0)
771 goto fail;
772 debug_print_bignum("SSWU: y = sqrt(v)", y, prime_len);
773
774 /* l = CEQ(LSB(u), LSB(y)) */
775 if (crypto_bignum_to_bin(u, bin1, sizeof(bin1), prime_len) < 0 ||
776 crypto_bignum_to_bin(y, bin2, sizeof(bin2), prime_len) < 0)
777 goto fail;
778 is_eq = const_time_eq(bin1[prime_len - 1] & 0x01,
779 bin2[prime_len - 1] & 0x01);
780
781 /* P = CSEL(l, (x,y), (x, p-y)) */
782 if (crypto_bignum_sub(prime, y, t1) < 0)
783 goto fail;
784 debug_print_bignum("SSWU: p - y", t1, prime_len);
785 if (crypto_bignum_to_bin(y, bin1, sizeof(bin1), prime_len) < 0 ||
786 crypto_bignum_to_bin(t1, bin2, sizeof(bin2), prime_len) < 0)
787 goto fail;
788 const_time_select_bin(is_eq, bin1, bin2, prime_len, &x_y[prime_len]);
789
790 /* output P */
791 wpa_hexdump_key(MSG_DEBUG, "SSWU: P.x", x_y, prime_len);
792 wpa_hexdump_key(MSG_DEBUG, "SSWU: P.y", &x_y[prime_len], prime_len);
793 p = crypto_ec_point_from_bin(ec, x_y);
794
795fail:
796 crypto_bignum_deinit(u2, 1);
797 crypto_bignum_deinit(t1, 1);
798 crypto_bignum_deinit(t2, 1);
799 crypto_bignum_deinit(z, 0);
800 crypto_bignum_deinit(t, 1);
801 crypto_bignum_deinit(x1a, 1);
802 crypto_bignum_deinit(x1b, 1);
803 crypto_bignum_deinit(x1, 1);
804 crypto_bignum_deinit(x2, 1);
805 crypto_bignum_deinit(gx1, 1);
806 crypto_bignum_deinit(gx2, 1);
807 crypto_bignum_deinit(y, 1);
808 crypto_bignum_deinit(v, 1);
809 crypto_bignum_deinit(zero, 0);
810 crypto_bignum_deinit(one, 0);
811 crypto_bignum_deinit(two, 0);
812 crypto_bignum_deinit(three, 0);
813 forced_memzero(bin, sizeof(bin));
814 forced_memzero(bin1, sizeof(bin1));
815 forced_memzero(bin2, sizeof(bin2));
816 forced_memzero(x_y, sizeof(x_y));
817 return p;
818}
819
820
821static int sae_pwd_seed(size_t hash_len, const u8 *ssid, size_t ssid_len,
822 const u8 *password, size_t password_len,
823 const char *identifier, u8 *pwd_seed)
824{
825 const u8 *addr[2];
826 size_t len[2];
827 size_t num_elem;
828
829 /* pwd-seed = HKDF-Extract(ssid, password [ || identifier ]) */
830 addr[0] = password;
831 len[0] = password_len;
832 num_elem = 1;
833 wpa_hexdump_ascii(MSG_DEBUG, "SAE: SSID", ssid, ssid_len);
834 wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
835 password, password_len);
836 if (identifier) {
837 wpa_printf(MSG_DEBUG, "SAE: password identifier: %s",
838 identifier);
839 addr[num_elem] = (const u8 *) identifier;
840 len[num_elem] = os_strlen(identifier);
841 num_elem++;
842 }
843 if (hkdf_extract(hash_len, ssid, ssid_len, num_elem, addr, len,
844 pwd_seed) < 0)
845 return -1;
846 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, hash_len);
847 return 0;
848}
849
850
851size_t sae_ecc_prime_len_2_hash_len(size_t prime_len)
852{
853 if (prime_len <= 256 / 8)
854 return 32;
855 if (prime_len <= 384 / 8)
856 return 48;
857 return 64;
858}
859
860
861struct crypto_ec_point *
862sae_derive_pt_ecc(struct crypto_ec *ec, int group,
863 const u8 *ssid, size_t ssid_len,
864 const u8 *password, size_t password_len,
865 const char *identifier)
866{
867 u8 pwd_seed[64];
868 u8 pwd_value[SAE_MAX_ECC_PRIME_LEN * 2];
869 size_t pwd_value_len, hash_len, prime_len;
870 const struct crypto_bignum *prime;
871 struct crypto_bignum *bn = NULL;
872 struct crypto_ec_point *p1 = NULL, *p2 = NULL, *pt = NULL;
873
874 prime = crypto_ec_get_prime(ec);
875 prime_len = crypto_ec_prime_len(ec);
876 if (prime_len > SAE_MAX_ECC_PRIME_LEN)
877 goto fail;
878 hash_len = sae_ecc_prime_len_2_hash_len(prime_len);
879
880 /* len = olen(p) + ceil(olen(p)/2) */
881 pwd_value_len = prime_len + (prime_len + 1) / 2;
882
883 if (sae_pwd_seed(hash_len, ssid, ssid_len, password, password_len,
884 identifier, pwd_seed) < 0)
885 goto fail;
886
887 /* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u1 P1", len)
888 */
889 if (hkdf_expand(hash_len, pwd_seed, hash_len,
890 "SAE Hash to Element u1 P1", pwd_value, pwd_value_len) <
891 0)
892 goto fail;
893 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value (u1 P1)",
894 pwd_value, pwd_value_len);
895
896 /* u1 = pwd-value modulo p */
897 bn = crypto_bignum_init_set(pwd_value, pwd_value_len);
898 if (!bn || crypto_bignum_mod(bn, prime, bn) < 0 ||
899 crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value),
900 prime_len) < 0)
901 goto fail;
902 wpa_hexdump_key(MSG_DEBUG, "SAE: u1", pwd_value, prime_len);
903
904 /* P1 = SSWU(u1) */
905 p1 = sswu(ec, group, bn);
906 if (!p1)
907 goto fail;
908
909 /* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u2 P2", len)
910 */
911 if (hkdf_expand(hash_len, pwd_seed, hash_len,
912 "SAE Hash to Element u2 P2", pwd_value,
913 pwd_value_len) < 0)
914 goto fail;
915 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value (u2 P2)",
916 pwd_value, pwd_value_len);
917
918 /* u2 = pwd-value modulo p */
919 crypto_bignum_deinit(bn, 1);
920 bn = crypto_bignum_init_set(pwd_value, pwd_value_len);
921 if (!bn || crypto_bignum_mod(bn, prime, bn) < 0 ||
922 crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value),
923 prime_len) < 0)
924 goto fail;
925 wpa_hexdump_key(MSG_DEBUG, "SAE: u2", pwd_value, prime_len);
926
927 /* P2 = SSWU(u2) */
928 p2 = sswu(ec, group, bn);
929 if (!p2)
930 goto fail;
931
932 /* PT = elem-op(P1, P2) */
933 pt = crypto_ec_point_init(ec);
934 if (!pt)
935 goto fail;
936 if (crypto_ec_point_add(ec, p1, p2, pt) < 0) {
937 crypto_ec_point_deinit(pt, 1);
938 pt = NULL;
939 }
940
941fail:
942 forced_memzero(pwd_seed, sizeof(pwd_seed));
943 forced_memzero(pwd_value, sizeof(pwd_value));
944 crypto_bignum_deinit(bn, 1);
945 crypto_ec_point_deinit(p1, 1);
946 crypto_ec_point_deinit(p2, 1);
947 return pt;
948}
949
950
951size_t sae_ffc_prime_len_2_hash_len(size_t prime_len)
952{
953 if (prime_len <= 2048 / 8)
954 return 32;
955 if (prime_len <= 3072 / 8)
956 return 48;
957 return 64;
958}
959
960
961static struct crypto_bignum *
962sae_derive_pt_ffc(const struct dh_group *dh, int group,
963 const u8 *ssid, size_t ssid_len,
964 const u8 *password, size_t password_len,
965 const char *identifier)
966{
967 size_t hash_len, prime_len, pwd_value_len;
968 struct crypto_bignum *prime, *order;
969 struct crypto_bignum *one = NULL, *two = NULL, *bn = NULL, *tmp = NULL,
970 *pt = NULL;
971 u8 pwd_seed[64];
972 u8 pwd_value[SAE_MAX_PRIME_LEN + SAE_MAX_PRIME_LEN / 2];
973
974 prime = crypto_bignum_init_set(dh->prime, dh->prime_len);
975 order = crypto_bignum_init_set(dh->order, dh->order_len);
976 if (!prime || !order)
977 goto fail;
978 prime_len = dh->prime_len;
979 if (prime_len > SAE_MAX_PRIME_LEN)
980 goto fail;
981 hash_len = sae_ffc_prime_len_2_hash_len(prime_len);
982
983 /* len = olen(p) + ceil(olen(p)/2) */
984 pwd_value_len = prime_len + (prime_len + 1) / 2;
985 if (pwd_value_len > sizeof(pwd_value))
986 goto fail;
987
988 if (sae_pwd_seed(hash_len, ssid, ssid_len, password, password_len,
989 identifier, pwd_seed) < 0)
990 goto fail;
991
992 /* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element", len) */
993 if (hkdf_expand(hash_len, pwd_seed, hash_len,
994 "SAE Hash to Element", pwd_value, pwd_value_len) < 0)
995 goto fail;
996 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value",
997 pwd_value, pwd_value_len);
998
999 /* pwd-value = (pwd-value modulo (p-2)) + 2 */
1000 bn = crypto_bignum_init_set(pwd_value, pwd_value_len);
1001 one = crypto_bignum_init_uint(1);
1002 two = crypto_bignum_init_uint(2);
1003 tmp = crypto_bignum_init();
1004 if (!bn || !one || !two || !tmp ||
1005 crypto_bignum_sub(prime, two, tmp) < 0 ||
1006 crypto_bignum_mod(bn, tmp, bn) < 0 ||
1007 crypto_bignum_add(bn, two, bn) < 0 ||
1008 crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value),
1009 prime_len) < 0)
1010 goto fail;
1011 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value(reduced)",
1012 pwd_value, prime_len);
1013
1014 /* PT = pwd-value^((p-1)/q) modulo p */
1015 pt = crypto_bignum_init();
1016 if (!pt ||
1017 crypto_bignum_sub(prime, one, tmp) < 0 ||
1018 crypto_bignum_div(tmp, order, tmp) < 0 ||
1019 crypto_bignum_exptmod(bn, tmp, prime, pt) < 0) {
1020 crypto_bignum_deinit(pt, 1);
1021 pt = NULL;
1022 goto fail;
1023 }
1024 debug_print_bignum("SAE: PT", pt, prime_len);
1025
1026fail:
1027 forced_memzero(pwd_seed, sizeof(pwd_seed));
1028 forced_memzero(pwd_value, sizeof(pwd_value));
1029 crypto_bignum_deinit(bn, 1);
1030 crypto_bignum_deinit(tmp, 1);
1031 crypto_bignum_deinit(one, 0);
1032 crypto_bignum_deinit(two, 0);
1033 crypto_bignum_deinit(prime, 0);
1034 crypto_bignum_deinit(order, 0);
1035 return pt;
1036}
1037
1038
1039static struct sae_pt *
1040sae_derive_pt_group(int group, const u8 *ssid, size_t ssid_len,
1041 const u8 *password, size_t password_len,
1042 const char *identifier)
1043{
1044 struct sae_pt *pt;
1045
1046 wpa_printf(MSG_DEBUG, "SAE: Derive PT - group %d", group);
1047
1048 pt = os_zalloc(sizeof(*pt));
1049 if (!pt)
1050 return NULL;
1051
1052 pt->group = group;
1053 pt->ec = crypto_ec_init(group);
1054 if (pt->ec) {
1055 pt->ecc_pt = sae_derive_pt_ecc(pt->ec, group, ssid, ssid_len,
1056 password, password_len,
1057 identifier);
1058 if (!pt->ecc_pt) {
1059 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PT");
1060 goto fail;
1061 }
1062
1063 return pt;
1064 }
1065
1066 pt->dh = dh_groups_get(group);
1067 if (!pt->dh) {
1068 wpa_printf(MSG_DEBUG, "SAE: Unsupported group %d", group);
1069 goto fail;
1070 }
1071
1072 pt->ffc_pt = sae_derive_pt_ffc(pt->dh, group, ssid, ssid_len,
1073 password, password_len, identifier);
1074 if (!pt->ffc_pt) {
1075 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PT");
1076 goto fail;
1077 }
1078
1079 return pt;
1080fail:
1081 sae_deinit_pt(pt);
1082 return NULL;
1083}
1084
1085
1086struct sae_pt * sae_derive_pt(int *groups, const u8 *ssid, size_t ssid_len,
1087 const u8 *password, size_t password_len,
1088 const char *identifier)
1089{
1090 struct sae_pt *pt = NULL, *last = NULL, *tmp;
1091 int default_groups[] = { 19, 0 };
1092 int i;
1093
1094 if (!groups)
1095 groups = default_groups;
1096 for (i = 0; groups[i] > 0; i++) {
1097 tmp = sae_derive_pt_group(groups[i], ssid, ssid_len, password,
1098 password_len, identifier);
1099 if (!tmp)
1100 continue;
1101
1102 if (last)
1103 last->next = tmp;
1104 else
1105 pt = tmp;
1106 last = tmp;
1107 }
1108
1109 return pt;
1110}
1111
1112
1113static void sae_max_min_addr(const u8 *addr[], size_t len[],
1114 const u8 *addr1, const u8 *addr2)
1115{
1116 len[0] = ETH_ALEN;
1117 len[1] = ETH_ALEN;
1118 if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) {
1119 addr[0] = addr1;
1120 addr[1] = addr2;
1121 } else {
1122 addr[0] = addr2;
1123 addr[1] = addr1;
1124 }
1125}
1126
1127
1128struct crypto_ec_point *
1129sae_derive_pwe_from_pt_ecc(const struct sae_pt *pt,
1130 const u8 *addr1, const u8 *addr2)
1131{
1132 u8 bin[SAE_MAX_ECC_PRIME_LEN * 2];
1133 size_t prime_len;
1134 const u8 *addr[2];
1135 size_t len[2];
1136 u8 salt[64], hash[64];
1137 size_t hash_len;
1138 const struct crypto_bignum *order;
1139 struct crypto_bignum *tmp = NULL, *val = NULL, *one = NULL;
1140 struct crypto_ec_point *pwe = NULL;
1141
1142 wpa_printf(MSG_DEBUG, "SAE: Derive PWE from PT");
1143 prime_len = crypto_ec_prime_len(pt->ec);
1144 if (crypto_ec_point_to_bin(pt->ec, pt->ecc_pt,
1145 bin, bin + prime_len) < 0)
1146 return NULL;
1147 wpa_hexdump_key(MSG_DEBUG, "SAE: PT.x", bin, prime_len);
1148 wpa_hexdump_key(MSG_DEBUG, "SAE: PT.y", bin + prime_len, prime_len);
1149
1150 sae_max_min_addr(addr, len, addr1, addr2);
1151
1152 /* val = H(0^n,
1153 * MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC)) */
1154 wpa_printf(MSG_DEBUG, "SAE: val = H(0^n, MAX(addrs) || MIN(addrs))");
1155 hash_len = sae_ecc_prime_len_2_hash_len(prime_len);
1156 os_memset(salt, 0, hash_len);
1157 if (hkdf_extract(hash_len, salt, hash_len, 2, addr, len, hash) < 0)
1158 goto fail;
1159 wpa_hexdump(MSG_DEBUG, "SAE: val", hash, hash_len);
1160
1161 /* val = val modulo (q - 1) + 1 */
1162 order = crypto_ec_get_order(pt->ec);
1163 tmp = crypto_bignum_init();
1164 val = crypto_bignum_init_set(hash, hash_len);
1165 one = crypto_bignum_init_uint(1);
1166 if (!tmp || !val || !one ||
1167 crypto_bignum_sub(order, one, tmp) < 0 ||
1168 crypto_bignum_mod(val, tmp, val) < 0 ||
1169 crypto_bignum_add(val, one, val) < 0)
1170 goto fail;
1171 debug_print_bignum("SAE: val(reduced to 1..q-1)", val, prime_len);
1172
1173 /* PWE = scalar-op(val, PT) */
1174 pwe = crypto_ec_point_init(pt->ec);
1175 if (!pwe ||
1176 crypto_ec_point_mul(pt->ec, pt->ecc_pt, val, pwe) < 0 ||
1177 crypto_ec_point_to_bin(pt->ec, pwe, bin, bin + prime_len) < 0) {
1178 crypto_ec_point_deinit(pwe, 1);
1179 pwe = NULL;
1180 goto fail;
1181 }
1182 wpa_hexdump_key(MSG_DEBUG, "SAE: PWE.x", bin, prime_len);
1183 wpa_hexdump_key(MSG_DEBUG, "SAE: PWE.y", bin + prime_len, prime_len);
1184
1185fail:
1186 crypto_bignum_deinit(tmp, 1);
1187 crypto_bignum_deinit(val, 1);
1188 crypto_bignum_deinit(one, 0);
1189 return pwe;
1190}
1191
1192
1193struct crypto_bignum *
1194sae_derive_pwe_from_pt_ffc(const struct sae_pt *pt,
1195 const u8 *addr1, const u8 *addr2)
1196{
1197 size_t prime_len;
1198 const u8 *addr[2];
1199 size_t len[2];
1200 u8 salt[64], hash[64];
1201 size_t hash_len;
1202 struct crypto_bignum *tmp = NULL, *val = NULL, *one = NULL;
1203 struct crypto_bignum *pwe = NULL, *order = NULL, *prime = NULL;
1204
1205 wpa_printf(MSG_DEBUG, "SAE: Derive PWE from PT");
1206 prime = crypto_bignum_init_set(pt->dh->prime, pt->dh->prime_len);
1207 order = crypto_bignum_init_set(pt->dh->order, pt->dh->order_len);
1208 if (!prime || !order)
1209 goto fail;
1210 prime_len = pt->dh->prime_len;
1211
1212 sae_max_min_addr(addr, len, addr1, addr2);
1213
1214 /* val = H(0^n,
1215 * MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC)) */
1216 wpa_printf(MSG_DEBUG, "SAE: val = H(0^n, MAX(addrs) || MIN(addrs))");
1217 hash_len = sae_ffc_prime_len_2_hash_len(prime_len);
1218 os_memset(salt, 0, hash_len);
1219 if (hkdf_extract(hash_len, salt, hash_len, 2, addr, len, hash) < 0)
1220 goto fail;
1221 wpa_hexdump(MSG_DEBUG, "SAE: val", hash, hash_len);
1222
1223 /* val = val modulo (q - 1) + 1 */
1224 tmp = crypto_bignum_init();
1225 val = crypto_bignum_init_set(hash, hash_len);
1226 one = crypto_bignum_init_uint(1);
1227 if (!tmp || !val || !one ||
1228 crypto_bignum_sub(order, one, tmp) < 0 ||
1229 crypto_bignum_mod(val, tmp, val) < 0 ||
1230 crypto_bignum_add(val, one, val) < 0)
1231 goto fail;
1232 debug_print_bignum("SAE: val(reduced to 1..q-1)", val, prime_len);
1233
1234 /* PWE = scalar-op(val, PT) */
1235 pwe = crypto_bignum_init();
1236 if (!pwe || crypto_bignum_exptmod(pt->ffc_pt, val, prime, pwe) < 0) {
1237 crypto_bignum_deinit(pwe, 1);
1238 pwe = NULL;
1239 goto fail;
1240 }
1241 debug_print_bignum("SAE: PWE", pwe, prime_len);
1242
1243fail:
1244 crypto_bignum_deinit(tmp, 1);
1245 crypto_bignum_deinit(val, 1);
1246 crypto_bignum_deinit(one, 0);
1247 crypto_bignum_deinit(prime, 0);
1248 crypto_bignum_deinit(order, 0);
1249 return pwe;
1250}
1251
1252
1253void sae_deinit_pt(struct sae_pt *pt)
1254{
1255 struct sae_pt *prev;
1256
1257 while (pt) {
1258 crypto_ec_point_deinit(pt->ecc_pt, 1);
1259 crypto_bignum_deinit(pt->ffc_pt, 1);
1260 crypto_ec_deinit(pt->ec);
1261 prev = pt;
1262 pt = pt->next;
1263 os_free(prev);
1264 }
1265}
1266
1267
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001268static int sae_derive_commit_element_ecc(struct sae_data *sae,
1269 struct crypto_bignum *mask)
1270{
1271 /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
1272 if (!sae->tmp->own_commit_element_ecc) {
1273 sae->tmp->own_commit_element_ecc =
1274 crypto_ec_point_init(sae->tmp->ec);
1275 if (!sae->tmp->own_commit_element_ecc)
1276 return -1;
1277 }
1278
1279 if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc, mask,
1280 sae->tmp->own_commit_element_ecc) < 0 ||
1281 crypto_ec_point_invert(sae->tmp->ec,
1282 sae->tmp->own_commit_element_ecc) < 0) {
1283 wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
1284 return -1;
1285 }
1286
1287 return 0;
1288}
1289
1290
1291static int sae_derive_commit_element_ffc(struct sae_data *sae,
1292 struct crypto_bignum *mask)
1293{
1294 /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
1295 if (!sae->tmp->own_commit_element_ffc) {
1296 sae->tmp->own_commit_element_ffc = crypto_bignum_init();
1297 if (!sae->tmp->own_commit_element_ffc)
1298 return -1;
1299 }
1300
1301 if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, mask, sae->tmp->prime,
1302 sae->tmp->own_commit_element_ffc) < 0 ||
1303 crypto_bignum_inverse(sae->tmp->own_commit_element_ffc,
1304 sae->tmp->prime,
1305 sae->tmp->own_commit_element_ffc) < 0) {
1306 wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
1307 return -1;
1308 }
1309
1310 return 0;
1311}
1312
1313
1314static int sae_derive_commit(struct sae_data *sae)
1315{
1316 struct crypto_bignum *mask;
Hai Shalom81f62d82019-07-22 12:10:00 -07001317 int ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001318
Hai Shalom81f62d82019-07-22 12:10:00 -07001319 mask = crypto_bignum_init();
1320 if (!sae->tmp->sae_rand)
1321 sae->tmp->sae_rand = crypto_bignum_init();
1322 if (!sae->tmp->own_commit_scalar)
1323 sae->tmp->own_commit_scalar = crypto_bignum_init();
1324 ret = !mask || !sae->tmp->sae_rand || !sae->tmp->own_commit_scalar ||
1325 dragonfly_generate_scalar(sae->tmp->order, sae->tmp->sae_rand,
1326 mask,
1327 sae->tmp->own_commit_scalar) < 0 ||
1328 (sae->tmp->ec &&
1329 sae_derive_commit_element_ecc(sae, mask) < 0) ||
1330 (sae->tmp->dh &&
1331 sae_derive_commit_element_ffc(sae, mask) < 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001332 crypto_bignum_deinit(mask, 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07001333 return ret ? -1 : 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001334}
1335
1336
1337int sae_prepare_commit(const u8 *addr1, const u8 *addr2,
1338 const u8 *password, size_t password_len,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001339 const char *identifier, struct sae_data *sae)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001340{
Dmitry Shmidt41712582015-06-29 11:02:15 -07001341 if (sae->tmp == NULL ||
1342 (sae->tmp->ec && sae_derive_pwe_ecc(sae, addr1, addr2, password,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001343 password_len,
1344 identifier) < 0) ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07001345 (sae->tmp->dh && sae_derive_pwe_ffc(sae, addr1, addr2, password,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001346 password_len,
Hai Shalomc3565922019-10-28 11:58:20 -07001347 identifier) < 0))
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001348 return -1;
Hai Shalomc3565922019-10-28 11:58:20 -07001349
1350 sae->tmp->h2e = 0;
1351 return sae_derive_commit(sae);
1352}
1353
1354
1355int sae_prepare_commit_pt(struct sae_data *sae, const struct sae_pt *pt,
1356 const u8 *addr1, const u8 *addr2,
1357 int *rejected_groups)
1358{
1359 if (!sae->tmp)
1360 return -1;
1361
1362 while (pt) {
1363 if (pt->group == sae->group)
1364 break;
1365 pt = pt->next;
1366 }
1367 if (!pt) {
1368 wpa_printf(MSG_INFO, "SAE: Could not find PT for group %u",
1369 sae->group);
1370 return -1;
1371 }
1372
1373 sae->tmp->own_addr_higher = os_memcmp(addr1, addr2, ETH_ALEN) > 0;
1374 wpabuf_free(sae->tmp->own_rejected_groups);
1375 sae->tmp->own_rejected_groups = NULL;
1376 if (rejected_groups) {
1377 int count, i;
1378 struct wpabuf *groups;
1379
1380 count = int_array_len(rejected_groups);
1381 groups = wpabuf_alloc(count * 2);
1382 if (!groups)
1383 return -1;
1384 for (i = 0; i < count; i++)
1385 wpabuf_put_le16(groups, rejected_groups[i]);
1386 sae->tmp->own_rejected_groups = groups;
1387 }
1388
1389 if (pt->ec) {
1390 crypto_ec_point_deinit(sae->tmp->pwe_ecc, 1);
1391 sae->tmp->pwe_ecc = sae_derive_pwe_from_pt_ecc(pt, addr1,
1392 addr2);
1393 if (!sae->tmp->pwe_ecc)
1394 return -1;
1395 }
1396
1397 if (pt->dh) {
1398 crypto_bignum_deinit(sae->tmp->pwe_ffc, 1);
1399 sae->tmp->pwe_ffc = sae_derive_pwe_from_pt_ffc(pt, addr1,
1400 addr2);
1401 if (!sae->tmp->pwe_ffc)
1402 return -1;
1403 }
1404
1405 sae->tmp->h2e = 1;
1406 return sae_derive_commit(sae);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001407}
1408
1409
1410static int sae_derive_k_ecc(struct sae_data *sae, u8 *k)
1411{
1412 struct crypto_ec_point *K;
1413 int ret = -1;
1414
1415 K = crypto_ec_point_init(sae->tmp->ec);
1416 if (K == NULL)
1417 goto fail;
1418
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001419 /*
1420 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
1421 * PEER-COMMIT-ELEMENT)))
1422 * If K is identity element (point-at-infinity), reject
1423 * k = F(K) (= x coordinate)
1424 */
1425
1426 if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc,
1427 sae->peer_commit_scalar, K) < 0 ||
1428 crypto_ec_point_add(sae->tmp->ec, K,
1429 sae->tmp->peer_commit_element_ecc, K) < 0 ||
1430 crypto_ec_point_mul(sae->tmp->ec, K, sae->tmp->sae_rand, K) < 0 ||
1431 crypto_ec_point_is_at_infinity(sae->tmp->ec, K) ||
1432 crypto_ec_point_to_bin(sae->tmp->ec, K, k, NULL) < 0) {
1433 wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
1434 goto fail;
1435 }
1436
1437 wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
1438
1439 ret = 0;
1440fail:
1441 crypto_ec_point_deinit(K, 1);
1442 return ret;
1443}
1444
1445
1446static int sae_derive_k_ffc(struct sae_data *sae, u8 *k)
1447{
1448 struct crypto_bignum *K;
1449 int ret = -1;
1450
1451 K = crypto_bignum_init();
1452 if (K == NULL)
1453 goto fail;
1454
1455 /*
1456 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
1457 * PEER-COMMIT-ELEMENT)))
1458 * If K is identity element (one), reject.
1459 * k = F(K) (= x coordinate)
1460 */
1461
1462 if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, sae->peer_commit_scalar,
1463 sae->tmp->prime, K) < 0 ||
1464 crypto_bignum_mulmod(K, sae->tmp->peer_commit_element_ffc,
1465 sae->tmp->prime, K) < 0 ||
1466 crypto_bignum_exptmod(K, sae->tmp->sae_rand, sae->tmp->prime, K) < 0
1467 ||
1468 crypto_bignum_is_one(K) ||
1469 crypto_bignum_to_bin(K, k, SAE_MAX_PRIME_LEN, sae->tmp->prime_len) <
1470 0) {
1471 wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
1472 goto fail;
1473 }
1474
1475 wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
1476
1477 ret = 0;
1478fail:
1479 crypto_bignum_deinit(K, 1);
1480 return ret;
1481}
1482
1483
Hai Shalomc3565922019-10-28 11:58:20 -07001484static int sae_kdf_hash(size_t hash_len, const u8 *k, const char *label,
1485 const u8 *context, size_t context_len,
1486 u8 *out, size_t out_len)
1487{
1488 if (hash_len == 32)
1489 return sha256_prf(k, hash_len, label,
1490 context, context_len, out, out_len);
1491#ifdef CONFIG_SHA384
1492 if (hash_len == 48)
1493 return sha384_prf(k, hash_len, label,
1494 context, context_len, out, out_len);
1495#endif /* CONFIG_SHA384 */
1496#ifdef CONFIG_SHA512
1497 if (hash_len == 64)
1498 return sha512_prf(k, hash_len, label,
1499 context, context_len, out, out_len);
1500#endif /* CONFIG_SHA512 */
1501 return -1;
1502}
1503
1504
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001505static int sae_derive_keys(struct sae_data *sae, const u8 *k)
1506{
Hai Shalomc3565922019-10-28 11:58:20 -07001507 u8 zero[SAE_MAX_HASH_LEN], val[SAE_MAX_PRIME_LEN];
1508 const u8 *salt;
1509 struct wpabuf *rejected_groups = NULL;
1510 u8 keyseed[SAE_MAX_HASH_LEN];
1511 u8 keys[SAE_MAX_HASH_LEN + SAE_PMK_LEN];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001512 struct crypto_bignum *tmp;
1513 int ret = -1;
Hai Shalomc3565922019-10-28 11:58:20 -07001514 size_t hash_len, salt_len, prime_len = sae->tmp->prime_len;
1515 const u8 *addr[1];
1516 size_t len[1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001517
1518 tmp = crypto_bignum_init();
1519 if (tmp == NULL)
1520 goto fail;
1521
Hai Shalomc3565922019-10-28 11:58:20 -07001522 /* keyseed = H(salt, k)
1523 * KCK || PMK = KDF-Hash-Length(keyseed, "SAE KCK and PMK",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001524 * (commit-scalar + peer-commit-scalar) modulo r)
1525 * PMKID = L((commit-scalar + peer-commit-scalar) modulo r, 0, 128)
1526 */
Hai Shalomc3565922019-10-28 11:58:20 -07001527 if (!sae->tmp->h2e)
1528 hash_len = SHA256_MAC_LEN;
1529 else if (sae->tmp->dh)
1530 hash_len = sae_ffc_prime_len_2_hash_len(prime_len);
1531 else
1532 hash_len = sae_ecc_prime_len_2_hash_len(prime_len);
1533 if (sae->tmp->h2e && (sae->tmp->own_rejected_groups ||
1534 sae->tmp->peer_rejected_groups)) {
1535 struct wpabuf *own, *peer;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001536
Hai Shalomc3565922019-10-28 11:58:20 -07001537 own = sae->tmp->own_rejected_groups;
1538 peer = sae->tmp->peer_rejected_groups;
1539 salt_len = 0;
1540 if (own)
1541 salt_len += wpabuf_len(own);
1542 if (peer)
1543 salt_len += wpabuf_len(peer);
1544 rejected_groups = wpabuf_alloc(salt_len);
1545 if (!rejected_groups)
1546 goto fail;
1547 if (sae->tmp->own_addr_higher) {
1548 if (own)
1549 wpabuf_put_buf(rejected_groups, own);
1550 if (peer)
1551 wpabuf_put_buf(rejected_groups, peer);
1552 } else {
1553 if (peer)
1554 wpabuf_put_buf(rejected_groups, peer);
1555 if (own)
1556 wpabuf_put_buf(rejected_groups, own);
1557 }
1558 salt = wpabuf_head(rejected_groups);
1559 salt_len = wpabuf_len(rejected_groups);
1560 } else {
1561 os_memset(zero, 0, hash_len);
1562 salt = zero;
1563 salt_len = hash_len;
1564 }
1565 wpa_hexdump(MSG_DEBUG, "SAE: salt for keyseed derivation",
1566 salt, salt_len);
1567 addr[0] = k;
1568 len[0] = prime_len;
1569 if (hkdf_extract(hash_len, salt, salt_len, 1, addr, len, keyseed) < 0)
Dmitry Shmidte4663042016-04-04 10:07:49 -07001570 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07001571 wpa_hexdump_key(MSG_DEBUG, "SAE: keyseed", keyseed, hash_len);
1572
1573 if (crypto_bignum_add(sae->tmp->own_commit_scalar,
1574 sae->peer_commit_scalar, tmp) < 0 ||
1575 crypto_bignum_mod(tmp, sae->tmp->order, tmp) < 0)
1576 goto fail;
1577 /* IEEE Std 802.11-2016 is not exactly clear on the encoding of the bit
1578 * string that is needed for KCK, PMK, and PMKID derivation, but it
1579 * seems to make most sense to encode the
1580 * (commit-scalar + peer-commit-scalar) mod r part as a bit string by
1581 * zero padding it from left to the length of the order (in full
1582 * octets). */
1583 crypto_bignum_to_bin(tmp, val, sizeof(val), sae->tmp->order_len);
1584 wpa_hexdump(MSG_DEBUG, "SAE: PMKID", val, SAE_PMKID_LEN);
1585 if (sae_kdf_hash(hash_len, keyseed, "SAE KCK and PMK",
1586 val, sae->tmp->order_len,
1587 keys, hash_len + SAE_PMK_LEN) < 0)
1588 goto fail;
1589 forced_memzero(keyseed, sizeof(keyseed));
1590 os_memcpy(sae->tmp->kck, keys, hash_len);
1591 sae->tmp->kck_len = hash_len;
1592 os_memcpy(sae->pmk, keys + hash_len, SAE_PMK_LEN);
Dmitry Shmidtd97138d2015-12-28 13:27:49 -08001593 os_memcpy(sae->pmkid, val, SAE_PMKID_LEN);
Hai Shalomc3565922019-10-28 11:58:20 -07001594 forced_memzero(keys, sizeof(keys));
1595 wpa_hexdump_key(MSG_DEBUG, "SAE: KCK",
1596 sae->tmp->kck, sae->tmp->kck_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001597 wpa_hexdump_key(MSG_DEBUG, "SAE: PMK", sae->pmk, SAE_PMK_LEN);
1598
1599 ret = 0;
1600fail:
Hai Shalomc3565922019-10-28 11:58:20 -07001601 wpabuf_free(rejected_groups);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001602 crypto_bignum_deinit(tmp, 0);
1603 return ret;
1604}
1605
1606
1607int sae_process_commit(struct sae_data *sae)
1608{
1609 u8 k[SAE_MAX_PRIME_LEN];
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001610 if (sae->tmp == NULL ||
1611 (sae->tmp->ec && sae_derive_k_ecc(sae, k) < 0) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001612 (sae->tmp->dh && sae_derive_k_ffc(sae, k) < 0) ||
1613 sae_derive_keys(sae, k) < 0)
1614 return -1;
1615 return 0;
1616}
1617
1618
1619void sae_write_commit(struct sae_data *sae, struct wpabuf *buf,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001620 const struct wpabuf *token, const char *identifier)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001621{
1622 u8 *pos;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001623
1624 if (sae->tmp == NULL)
1625 return;
1626
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001627 wpabuf_put_le16(buf, sae->group); /* Finite Cyclic Group */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001628 if (token) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001629 wpabuf_put_buf(buf, token);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001630 wpa_hexdump(MSG_DEBUG, "SAE: Anti-clogging token",
1631 wpabuf_head(token), wpabuf_len(token));
1632 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001633 pos = wpabuf_put(buf, sae->tmp->prime_len);
1634 crypto_bignum_to_bin(sae->tmp->own_commit_scalar, pos,
1635 sae->tmp->prime_len, sae->tmp->prime_len);
1636 wpa_hexdump(MSG_DEBUG, "SAE: own commit-scalar",
1637 pos, sae->tmp->prime_len);
1638 if (sae->tmp->ec) {
1639 pos = wpabuf_put(buf, 2 * sae->tmp->prime_len);
1640 crypto_ec_point_to_bin(sae->tmp->ec,
1641 sae->tmp->own_commit_element_ecc,
1642 pos, pos + sae->tmp->prime_len);
1643 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(x)",
1644 pos, sae->tmp->prime_len);
1645 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(y)",
1646 pos + sae->tmp->prime_len, sae->tmp->prime_len);
1647 } else {
1648 pos = wpabuf_put(buf, sae->tmp->prime_len);
1649 crypto_bignum_to_bin(sae->tmp->own_commit_element_ffc, pos,
1650 sae->tmp->prime_len, sae->tmp->prime_len);
1651 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element",
1652 pos, sae->tmp->prime_len);
1653 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001654
1655 if (identifier) {
1656 /* Password Identifier element */
1657 wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
1658 wpabuf_put_u8(buf, 1 + os_strlen(identifier));
1659 wpabuf_put_u8(buf, WLAN_EID_EXT_PASSWORD_IDENTIFIER);
1660 wpabuf_put_str(buf, identifier);
1661 wpa_printf(MSG_DEBUG, "SAE: own Password Identifier: %s",
1662 identifier);
1663 }
Hai Shalomc3565922019-10-28 11:58:20 -07001664
1665 if (sae->tmp->h2e && sae->tmp->own_rejected_groups) {
1666 wpa_hexdump_buf(MSG_DEBUG, "SAE: own Rejected Groups",
1667 sae->tmp->own_rejected_groups);
1668 wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
1669 wpabuf_put_u8(buf,
1670 1 + wpabuf_len(sae->tmp->own_rejected_groups));
1671 wpabuf_put_u8(buf, WLAN_EID_EXT_REJECTED_GROUPS);
1672 wpabuf_put_buf(buf, sae->tmp->own_rejected_groups);
1673 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001674}
1675
1676
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001677u16 sae_group_allowed(struct sae_data *sae, int *allowed_groups, u16 group)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001678{
1679 if (allowed_groups) {
1680 int i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001681 for (i = 0; allowed_groups[i] > 0; i++) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001682 if (allowed_groups[i] == group)
1683 break;
1684 }
1685 if (allowed_groups[i] != group) {
1686 wpa_printf(MSG_DEBUG, "SAE: Proposed group %u not "
1687 "enabled in the current configuration",
1688 group);
1689 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1690 }
1691 }
1692
1693 if (sae->state == SAE_COMMITTED && group != sae->group) {
1694 wpa_printf(MSG_DEBUG, "SAE: Do not allow group to be changed");
1695 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1696 }
1697
1698 if (group != sae->group && sae_set_group(sae, group) < 0) {
1699 wpa_printf(MSG_DEBUG, "SAE: Unsupported Finite Cyclic Group %u",
1700 group);
1701 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1702 }
1703
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001704 if (sae->tmp == NULL) {
1705 wpa_printf(MSG_DEBUG, "SAE: Group information not yet initialized");
1706 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1707 }
1708
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001709 if (sae->tmp->dh && !allowed_groups) {
1710 wpa_printf(MSG_DEBUG, "SAE: Do not allow FFC group %u without "
1711 "explicit configuration enabling it", group);
1712 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1713 }
1714
1715 return WLAN_STATUS_SUCCESS;
1716}
1717
1718
Roshan Pius3a1667e2018-07-03 15:17:14 -07001719static int sae_is_password_id_elem(const u8 *pos, const u8 *end)
1720{
1721 return end - pos >= 3 &&
1722 pos[0] == WLAN_EID_EXTENSION &&
1723 pos[1] >= 1 &&
1724 end - pos - 2 >= pos[1] &&
1725 pos[2] == WLAN_EID_EXT_PASSWORD_IDENTIFIER;
1726}
1727
1728
Hai Shalomc3565922019-10-28 11:58:20 -07001729static int sae_is_rejected_groups_elem(const u8 *pos, const u8 *end)
1730{
1731 return end - pos >= 3 &&
1732 pos[0] == WLAN_EID_EXTENSION &&
1733 pos[1] >= 2 &&
1734 end - pos - 2 >= pos[1] &&
1735 pos[2] == WLAN_EID_EXT_REJECTED_GROUPS;
1736}
1737
1738
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001739static void sae_parse_commit_token(struct sae_data *sae, const u8 **pos,
1740 const u8 *end, const u8 **token,
Hai Shalomc3565922019-10-28 11:58:20 -07001741 size_t *token_len, int h2e)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001742{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001743 size_t scalar_elem_len, tlen;
1744 const u8 *elem;
1745
1746 if (token)
1747 *token = NULL;
1748 if (token_len)
1749 *token_len = 0;
1750
1751 scalar_elem_len = (sae->tmp->ec ? 3 : 2) * sae->tmp->prime_len;
1752 if (scalar_elem_len >= (size_t) (end - *pos))
1753 return; /* No extra data beyond peer scalar and element */
1754
1755 /* It is a bit difficult to parse this now that there is an
1756 * optional variable length Anti-Clogging Token field and
1757 * optional variable length Password Identifier element in the
1758 * frame. We are sending out fixed length Anti-Clogging Token
1759 * fields, so use that length as a requirement for the received
1760 * token and check for the presence of possible Password
1761 * Identifier element based on the element header information.
Hai Shalomc3565922019-10-28 11:58:20 -07001762 * When parsing H2E case, also consider the Rejected Groupd element
1763 * similarly.
Roshan Pius3a1667e2018-07-03 15:17:14 -07001764 */
1765 tlen = end - (*pos + scalar_elem_len);
1766
1767 if (tlen < SHA256_MAC_LEN) {
1768 wpa_printf(MSG_DEBUG,
1769 "SAE: Too short optional data (%u octets) to include our Anti-Clogging Token",
1770 (unsigned int) tlen);
1771 return;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001772 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001773
1774 elem = *pos + scalar_elem_len;
1775 if (sae_is_password_id_elem(elem, end)) {
1776 /* Password Identifier element takes out all available
1777 * extra octets, so there can be no Anti-Clogging token in
1778 * this frame. */
1779 return;
1780 }
Hai Shalomc3565922019-10-28 11:58:20 -07001781 if (h2e && sae_is_rejected_groups_elem(elem, end)) {
1782 /* Rejected Groups takes out all available extra octets, so
1783 * there can be no Anti-Clogging token in this frame. */
1784 return;
1785 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001786
1787 elem += SHA256_MAC_LEN;
1788 if (sae_is_password_id_elem(elem, end)) {
1789 /* Password Identifier element is included in the end, so
1790 * remove its length from the Anti-Clogging token field. */
1791 tlen -= 2 + elem[1];
Hai Shalomc3565922019-10-28 11:58:20 -07001792 elem += 2 + elem[1];
1793 if (h2e && sae_is_rejected_groups_elem(elem, end)) {
1794 /* Also remove Rejected Groups element from the
1795 * Anti-Clogging token field length */
1796 tlen -= 2 + elem[1];
1797 }
1798 } else if (h2e && sae_is_rejected_groups_elem(elem, end)) {
1799 /* Rejected Groups element is included in the end, so
1800 * remove its length from the Anti-Clogging token field. */
1801 tlen -= 2 + elem[1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07001802 }
1803
1804 wpa_hexdump(MSG_DEBUG, "SAE: Anti-Clogging Token", *pos, tlen);
1805 if (token)
1806 *token = *pos;
1807 if (token_len)
1808 *token_len = tlen;
1809 *pos += tlen;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001810}
1811
1812
1813static u16 sae_parse_commit_scalar(struct sae_data *sae, const u8 **pos,
1814 const u8 *end)
1815{
1816 struct crypto_bignum *peer_scalar;
1817
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001818 if (sae->tmp->prime_len > end - *pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001819 wpa_printf(MSG_DEBUG, "SAE: Not enough data for scalar");
1820 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1821 }
1822
1823 peer_scalar = crypto_bignum_init_set(*pos, sae->tmp->prime_len);
1824 if (peer_scalar == NULL)
1825 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1826
1827 /*
1828 * IEEE Std 802.11-2012, 11.3.8.6.1: If there is a protocol instance for
1829 * the peer and it is in Authenticated state, the new Commit Message
1830 * shall be dropped if the peer-scalar is identical to the one used in
1831 * the existing protocol instance.
1832 */
1833 if (sae->state == SAE_ACCEPTED && sae->peer_commit_scalar &&
1834 crypto_bignum_cmp(sae->peer_commit_scalar, peer_scalar) == 0) {
1835 wpa_printf(MSG_DEBUG, "SAE: Do not accept re-use of previous "
1836 "peer-commit-scalar");
1837 crypto_bignum_deinit(peer_scalar, 0);
1838 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1839 }
1840
Dmitry Shmidt41712582015-06-29 11:02:15 -07001841 /* 1 < scalar < r */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001842 if (crypto_bignum_is_zero(peer_scalar) ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07001843 crypto_bignum_is_one(peer_scalar) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001844 crypto_bignum_cmp(peer_scalar, sae->tmp->order) >= 0) {
1845 wpa_printf(MSG_DEBUG, "SAE: Invalid peer scalar");
1846 crypto_bignum_deinit(peer_scalar, 0);
1847 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1848 }
1849
1850
1851 crypto_bignum_deinit(sae->peer_commit_scalar, 0);
1852 sae->peer_commit_scalar = peer_scalar;
1853 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-scalar",
1854 *pos, sae->tmp->prime_len);
1855 *pos += sae->tmp->prime_len;
1856
1857 return WLAN_STATUS_SUCCESS;
1858}
1859
1860
Roshan Pius3a1667e2018-07-03 15:17:14 -07001861static u16 sae_parse_commit_element_ecc(struct sae_data *sae, const u8 **pos,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001862 const u8 *end)
1863{
1864 u8 prime[SAE_MAX_ECC_PRIME_LEN];
1865
Roshan Pius3a1667e2018-07-03 15:17:14 -07001866 if (2 * sae->tmp->prime_len > end - *pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001867 wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
1868 "commit-element");
1869 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1870 }
1871
1872 if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
1873 sae->tmp->prime_len) < 0)
1874 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1875
1876 /* element x and y coordinates < p */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001877 if (os_memcmp(*pos, prime, sae->tmp->prime_len) >= 0 ||
1878 os_memcmp(*pos + sae->tmp->prime_len, prime,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001879 sae->tmp->prime_len) >= 0) {
1880 wpa_printf(MSG_DEBUG, "SAE: Invalid coordinates in peer "
1881 "element");
1882 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1883 }
1884
1885 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(x)",
Roshan Pius3a1667e2018-07-03 15:17:14 -07001886 *pos, sae->tmp->prime_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001887 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(y)",
Roshan Pius3a1667e2018-07-03 15:17:14 -07001888 *pos + sae->tmp->prime_len, sae->tmp->prime_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001889
1890 crypto_ec_point_deinit(sae->tmp->peer_commit_element_ecc, 0);
1891 sae->tmp->peer_commit_element_ecc =
Roshan Pius3a1667e2018-07-03 15:17:14 -07001892 crypto_ec_point_from_bin(sae->tmp->ec, *pos);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001893 if (sae->tmp->peer_commit_element_ecc == NULL)
1894 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1895
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001896 if (!crypto_ec_point_is_on_curve(sae->tmp->ec,
1897 sae->tmp->peer_commit_element_ecc)) {
1898 wpa_printf(MSG_DEBUG, "SAE: Peer element is not on curve");
1899 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1900 }
1901
Roshan Pius3a1667e2018-07-03 15:17:14 -07001902 *pos += 2 * sae->tmp->prime_len;
1903
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001904 return WLAN_STATUS_SUCCESS;
1905}
1906
1907
Roshan Pius3a1667e2018-07-03 15:17:14 -07001908static u16 sae_parse_commit_element_ffc(struct sae_data *sae, const u8 **pos,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001909 const u8 *end)
1910{
Dmitry Shmidt41712582015-06-29 11:02:15 -07001911 struct crypto_bignum *res, *one;
1912 const u8 one_bin[1] = { 0x01 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001913
Roshan Pius3a1667e2018-07-03 15:17:14 -07001914 if (sae->tmp->prime_len > end - *pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001915 wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
1916 "commit-element");
1917 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1918 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001919 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element", *pos,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001920 sae->tmp->prime_len);
1921
1922 crypto_bignum_deinit(sae->tmp->peer_commit_element_ffc, 0);
1923 sae->tmp->peer_commit_element_ffc =
Roshan Pius3a1667e2018-07-03 15:17:14 -07001924 crypto_bignum_init_set(*pos, sae->tmp->prime_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001925 if (sae->tmp->peer_commit_element_ffc == NULL)
1926 return WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001927 /* 1 < element < p - 1 */
1928 res = crypto_bignum_init();
1929 one = crypto_bignum_init_set(one_bin, sizeof(one_bin));
1930 if (!res || !one ||
1931 crypto_bignum_sub(sae->tmp->prime, one, res) ||
1932 crypto_bignum_is_zero(sae->tmp->peer_commit_element_ffc) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001933 crypto_bignum_is_one(sae->tmp->peer_commit_element_ffc) ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07001934 crypto_bignum_cmp(sae->tmp->peer_commit_element_ffc, res) >= 0) {
1935 crypto_bignum_deinit(res, 0);
1936 crypto_bignum_deinit(one, 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001937 wpa_printf(MSG_DEBUG, "SAE: Invalid peer element");
1938 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1939 }
Dmitry Shmidt41712582015-06-29 11:02:15 -07001940 crypto_bignum_deinit(one, 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001941
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001942 /* scalar-op(r, ELEMENT) = 1 modulo p */
Dmitry Shmidt41712582015-06-29 11:02:15 -07001943 if (crypto_bignum_exptmod(sae->tmp->peer_commit_element_ffc,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001944 sae->tmp->order, sae->tmp->prime, res) < 0 ||
1945 !crypto_bignum_is_one(res)) {
1946 wpa_printf(MSG_DEBUG, "SAE: Invalid peer element (scalar-op)");
1947 crypto_bignum_deinit(res, 0);
1948 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1949 }
1950 crypto_bignum_deinit(res, 0);
1951
Roshan Pius3a1667e2018-07-03 15:17:14 -07001952 *pos += sae->tmp->prime_len;
1953
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001954 return WLAN_STATUS_SUCCESS;
1955}
1956
1957
Roshan Pius3a1667e2018-07-03 15:17:14 -07001958static u16 sae_parse_commit_element(struct sae_data *sae, const u8 **pos,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001959 const u8 *end)
1960{
1961 if (sae->tmp->dh)
1962 return sae_parse_commit_element_ffc(sae, pos, end);
1963 return sae_parse_commit_element_ecc(sae, pos, end);
1964}
1965
1966
Roshan Pius3a1667e2018-07-03 15:17:14 -07001967static int sae_parse_password_identifier(struct sae_data *sae,
Hai Shalomc3565922019-10-28 11:58:20 -07001968 const u8 **pos, const u8 *end)
Roshan Pius3a1667e2018-07-03 15:17:14 -07001969{
1970 wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
Hai Shalomc3565922019-10-28 11:58:20 -07001971 *pos, end - *pos);
1972 if (!sae_is_password_id_elem(*pos, end)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001973 if (sae->tmp->pw_id) {
1974 wpa_printf(MSG_DEBUG,
1975 "SAE: No Password Identifier included, but expected one (%s)",
1976 sae->tmp->pw_id);
1977 return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
1978 }
1979 os_free(sae->tmp->pw_id);
1980 sae->tmp->pw_id = NULL;
1981 return WLAN_STATUS_SUCCESS; /* No Password Identifier */
1982 }
1983
1984 if (sae->tmp->pw_id &&
Hai Shalomc3565922019-10-28 11:58:20 -07001985 ((*pos)[1] - 1 != (int) os_strlen(sae->tmp->pw_id) ||
1986 os_memcmp(sae->tmp->pw_id, (*pos) + 3, (*pos)[1] - 1) != 0)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001987 wpa_printf(MSG_DEBUG,
1988 "SAE: The included Password Identifier does not match the expected one (%s)",
1989 sae->tmp->pw_id);
1990 return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
1991 }
1992
1993 os_free(sae->tmp->pw_id);
Hai Shalomc3565922019-10-28 11:58:20 -07001994 sae->tmp->pw_id = os_malloc((*pos)[1]);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001995 if (!sae->tmp->pw_id)
1996 return WLAN_STATUS_UNSPECIFIED_FAILURE;
Hai Shalomc3565922019-10-28 11:58:20 -07001997 os_memcpy(sae->tmp->pw_id, (*pos) + 3, (*pos)[1] - 1);
1998 sae->tmp->pw_id[(*pos)[1] - 1] = '\0';
Roshan Pius3a1667e2018-07-03 15:17:14 -07001999 wpa_hexdump_ascii(MSG_DEBUG, "SAE: Received Password Identifier",
Hai Shalomc3565922019-10-28 11:58:20 -07002000 sae->tmp->pw_id, (*pos)[1] - 1);
2001 *pos = *pos + 2 + (*pos)[1];
2002 return WLAN_STATUS_SUCCESS;
2003}
2004
2005
2006static int sae_parse_rejected_groups(struct sae_data *sae,
2007 const u8 *pos, const u8 *end)
2008{
2009 wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
2010 pos, end - pos);
2011 if (!sae_is_rejected_groups_elem(pos, end))
2012 return WLAN_STATUS_SUCCESS;
2013 wpabuf_free(sae->tmp->peer_rejected_groups);
2014 sae->tmp->peer_rejected_groups = wpabuf_alloc(pos[1] - 1);
2015 if (!sae->tmp->peer_rejected_groups)
2016 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2017 wpabuf_put_data(sae->tmp->peer_rejected_groups, pos + 3, pos[1] - 1);
2018 wpa_hexdump_buf(MSG_DEBUG, "SAE: Received Rejected Groups list",
2019 sae->tmp->peer_rejected_groups);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002020 return WLAN_STATUS_SUCCESS;
2021}
2022
2023
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002024u16 sae_parse_commit(struct sae_data *sae, const u8 *data, size_t len,
Hai Shalomc3565922019-10-28 11:58:20 -07002025 const u8 **token, size_t *token_len, int *allowed_groups,
2026 int h2e)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002027{
2028 const u8 *pos = data, *end = data + len;
2029 u16 res;
2030
2031 /* Check Finite Cyclic Group */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002032 if (end - pos < 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002033 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2034 res = sae_group_allowed(sae, allowed_groups, WPA_GET_LE16(pos));
2035 if (res != WLAN_STATUS_SUCCESS)
2036 return res;
2037 pos += 2;
2038
2039 /* Optional Anti-Clogging Token */
Hai Shalomc3565922019-10-28 11:58:20 -07002040 sae_parse_commit_token(sae, &pos, end, token, token_len, h2e);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002041
2042 /* commit-scalar */
2043 res = sae_parse_commit_scalar(sae, &pos, end);
2044 if (res != WLAN_STATUS_SUCCESS)
2045 return res;
2046
2047 /* commit-element */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002048 res = sae_parse_commit_element(sae, &pos, end);
2049 if (res != WLAN_STATUS_SUCCESS)
2050 return res;
2051
2052 /* Optional Password Identifier element */
Hai Shalomc3565922019-10-28 11:58:20 -07002053 res = sae_parse_password_identifier(sae, &pos, end);
Dmitry Shmidt41712582015-06-29 11:02:15 -07002054 if (res != WLAN_STATUS_SUCCESS)
2055 return res;
2056
Hai Shalomc3565922019-10-28 11:58:20 -07002057 /* Conditional Rejected Groups element */
2058 if (h2e) {
2059 res = sae_parse_rejected_groups(sae, pos, end);
2060 if (res != WLAN_STATUS_SUCCESS)
2061 return res;
2062 }
2063
Dmitry Shmidt41712582015-06-29 11:02:15 -07002064 /*
2065 * Check whether peer-commit-scalar and PEER-COMMIT-ELEMENT are same as
2066 * the values we sent which would be evidence of a reflection attack.
2067 */
2068 if (!sae->tmp->own_commit_scalar ||
2069 crypto_bignum_cmp(sae->tmp->own_commit_scalar,
2070 sae->peer_commit_scalar) != 0 ||
2071 (sae->tmp->dh &&
2072 (!sae->tmp->own_commit_element_ffc ||
2073 crypto_bignum_cmp(sae->tmp->own_commit_element_ffc,
2074 sae->tmp->peer_commit_element_ffc) != 0)) ||
2075 (sae->tmp->ec &&
2076 (!sae->tmp->own_commit_element_ecc ||
2077 crypto_ec_point_cmp(sae->tmp->ec,
2078 sae->tmp->own_commit_element_ecc,
2079 sae->tmp->peer_commit_element_ecc) != 0)))
2080 return WLAN_STATUS_SUCCESS; /* scalars/elements are different */
2081
2082 /*
2083 * This is a reflection attack - return special value to trigger caller
2084 * to silently discard the frame instead of replying with a specific
2085 * status code.
2086 */
2087 return SAE_SILENTLY_DISCARD;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002088}
2089
2090
Hai Shalomc3565922019-10-28 11:58:20 -07002091static int sae_cn_confirm(struct sae_data *sae, const u8 *sc,
2092 const struct crypto_bignum *scalar1,
2093 const u8 *element1, size_t element1_len,
2094 const struct crypto_bignum *scalar2,
2095 const u8 *element2, size_t element2_len,
2096 u8 *confirm)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002097{
2098 const u8 *addr[5];
2099 size_t len[5];
2100 u8 scalar_b1[SAE_MAX_PRIME_LEN], scalar_b2[SAE_MAX_PRIME_LEN];
2101
2102 /* Confirm
2103 * CN(key, X, Y, Z, ...) =
2104 * HMAC-SHA256(key, D2OS(X) || D2OS(Y) || D2OS(Z) | ...)
2105 * confirm = CN(KCK, send-confirm, commit-scalar, COMMIT-ELEMENT,
2106 * peer-commit-scalar, PEER-COMMIT-ELEMENT)
2107 * verifier = CN(KCK, peer-send-confirm, peer-commit-scalar,
2108 * PEER-COMMIT-ELEMENT, commit-scalar, COMMIT-ELEMENT)
2109 */
Hai Shalomc3565922019-10-28 11:58:20 -07002110 if (crypto_bignum_to_bin(scalar1, scalar_b1, sizeof(scalar_b1),
2111 sae->tmp->prime_len) < 0 ||
2112 crypto_bignum_to_bin(scalar2, scalar_b2, sizeof(scalar_b2),
2113 sae->tmp->prime_len) < 0)
2114 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002115 addr[0] = sc;
2116 len[0] = 2;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002117 addr[1] = scalar_b1;
2118 len[1] = sae->tmp->prime_len;
2119 addr[2] = element1;
2120 len[2] = element1_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002121 addr[3] = scalar_b2;
2122 len[3] = sae->tmp->prime_len;
2123 addr[4] = element2;
2124 len[4] = element2_len;
Hai Shalomc3565922019-10-28 11:58:20 -07002125 return hkdf_extract(sae->tmp->kck_len, sae->tmp->kck, sae->tmp->kck_len,
2126 5, addr, len, confirm);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002127}
2128
2129
Hai Shalomc3565922019-10-28 11:58:20 -07002130static int sae_cn_confirm_ecc(struct sae_data *sae, const u8 *sc,
2131 const struct crypto_bignum *scalar1,
2132 const struct crypto_ec_point *element1,
2133 const struct crypto_bignum *scalar2,
2134 const struct crypto_ec_point *element2,
2135 u8 *confirm)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002136{
2137 u8 element_b1[2 * SAE_MAX_ECC_PRIME_LEN];
2138 u8 element_b2[2 * SAE_MAX_ECC_PRIME_LEN];
2139
Hai Shalomc3565922019-10-28 11:58:20 -07002140 if (crypto_ec_point_to_bin(sae->tmp->ec, element1, element_b1,
2141 element_b1 + sae->tmp->prime_len) < 0 ||
2142 crypto_ec_point_to_bin(sae->tmp->ec, element2, element_b2,
2143 element_b2 + sae->tmp->prime_len) < 0 ||
2144 sae_cn_confirm(sae, sc, scalar1, element_b1,
2145 2 * sae->tmp->prime_len,
2146 scalar2, element_b2, 2 * sae->tmp->prime_len,
2147 confirm) < 0)
2148 return -1;
2149 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002150}
2151
2152
Hai Shalomc3565922019-10-28 11:58:20 -07002153static int sae_cn_confirm_ffc(struct sae_data *sae, const u8 *sc,
2154 const struct crypto_bignum *scalar1,
2155 const struct crypto_bignum *element1,
2156 const struct crypto_bignum *scalar2,
2157 const struct crypto_bignum *element2,
2158 u8 *confirm)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002159{
2160 u8 element_b1[SAE_MAX_PRIME_LEN];
2161 u8 element_b2[SAE_MAX_PRIME_LEN];
2162
Hai Shalomc3565922019-10-28 11:58:20 -07002163 if (crypto_bignum_to_bin(element1, element_b1, sizeof(element_b1),
2164 sae->tmp->prime_len) < 0 ||
2165 crypto_bignum_to_bin(element2, element_b2, sizeof(element_b2),
2166 sae->tmp->prime_len) < 0 ||
2167 sae_cn_confirm(sae, sc, scalar1, element_b1, sae->tmp->prime_len,
2168 scalar2, element_b2, sae->tmp->prime_len,
2169 confirm) < 0)
2170 return -1;
2171 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002172}
2173
2174
2175void sae_write_confirm(struct sae_data *sae, struct wpabuf *buf)
2176{
2177 const u8 *sc;
Hai Shalomc3565922019-10-28 11:58:20 -07002178 size_t hash_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002179
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002180 if (sae->tmp == NULL)
2181 return;
2182
Hai Shalomc3565922019-10-28 11:58:20 -07002183 hash_len = sae->tmp->kck_len;
2184
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002185 /* Send-Confirm */
2186 sc = wpabuf_put(buf, 0);
2187 wpabuf_put_le16(buf, sae->send_confirm);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002188 if (sae->send_confirm < 0xffff)
2189 sae->send_confirm++;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002190
2191 if (sae->tmp->ec)
2192 sae_cn_confirm_ecc(sae, sc, sae->tmp->own_commit_scalar,
2193 sae->tmp->own_commit_element_ecc,
2194 sae->peer_commit_scalar,
2195 sae->tmp->peer_commit_element_ecc,
Hai Shalomc3565922019-10-28 11:58:20 -07002196 wpabuf_put(buf, hash_len));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002197 else
2198 sae_cn_confirm_ffc(sae, sc, sae->tmp->own_commit_scalar,
2199 sae->tmp->own_commit_element_ffc,
2200 sae->peer_commit_scalar,
2201 sae->tmp->peer_commit_element_ffc,
Hai Shalomc3565922019-10-28 11:58:20 -07002202 wpabuf_put(buf, hash_len));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002203}
2204
2205
2206int sae_check_confirm(struct sae_data *sae, const u8 *data, size_t len)
2207{
Hai Shalomc3565922019-10-28 11:58:20 -07002208 u8 verifier[SAE_MAX_HASH_LEN];
2209 size_t hash_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002210
Hai Shalomc3565922019-10-28 11:58:20 -07002211 if (!sae->tmp)
2212 return -1;
2213
2214 hash_len = sae->tmp->kck_len;
2215 if (len < 2 + hash_len) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002216 wpa_printf(MSG_DEBUG, "SAE: Too short confirm message");
2217 return -1;
2218 }
2219
2220 wpa_printf(MSG_DEBUG, "SAE: peer-send-confirm %u", WPA_GET_LE16(data));
2221
Hai Shalomc3565922019-10-28 11:58:20 -07002222 if (!sae->peer_commit_scalar || !sae->tmp->own_commit_scalar) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002223 wpa_printf(MSG_DEBUG, "SAE: Temporary data not yet available");
2224 return -1;
2225 }
2226
Hai Shalom021b0b52019-04-10 11:17:58 -07002227 if (sae->tmp->ec) {
2228 if (!sae->tmp->peer_commit_element_ecc ||
Hai Shalomc3565922019-10-28 11:58:20 -07002229 !sae->tmp->own_commit_element_ecc ||
2230 sae_cn_confirm_ecc(sae, data, sae->peer_commit_scalar,
2231 sae->tmp->peer_commit_element_ecc,
2232 sae->tmp->own_commit_scalar,
2233 sae->tmp->own_commit_element_ecc,
2234 verifier) < 0)
Hai Shalom021b0b52019-04-10 11:17:58 -07002235 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -07002236 } else {
2237 if (!sae->tmp->peer_commit_element_ffc ||
Hai Shalomc3565922019-10-28 11:58:20 -07002238 !sae->tmp->own_commit_element_ffc ||
2239 sae_cn_confirm_ffc(sae, data, sae->peer_commit_scalar,
2240 sae->tmp->peer_commit_element_ffc,
2241 sae->tmp->own_commit_scalar,
2242 sae->tmp->own_commit_element_ffc,
2243 verifier) < 0)
Hai Shalom021b0b52019-04-10 11:17:58 -07002244 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -07002245 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002246
Hai Shalomc3565922019-10-28 11:58:20 -07002247 if (os_memcmp_const(verifier, data + 2, hash_len) != 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002248 wpa_printf(MSG_DEBUG, "SAE: Confirm mismatch");
2249 wpa_hexdump(MSG_DEBUG, "SAE: Received confirm",
Hai Shalomc3565922019-10-28 11:58:20 -07002250 data + 2, hash_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002251 wpa_hexdump(MSG_DEBUG, "SAE: Calculated verifier",
Hai Shalomc3565922019-10-28 11:58:20 -07002252 verifier, hash_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002253 return -1;
2254 }
2255
2256 return 0;
2257}
Roshan Pius3a1667e2018-07-03 15:17:14 -07002258
2259
2260const char * sae_state_txt(enum sae_state state)
2261{
2262 switch (state) {
2263 case SAE_NOTHING:
2264 return "Nothing";
2265 case SAE_COMMITTED:
2266 return "Committed";
2267 case SAE_CONFIRMED:
2268 return "Confirmed";
2269 case SAE_ACCEPTED:
2270 return "Accepted";
2271 }
2272 return "?";
2273}