blob: bf8cc9deaa54047248d0acbb6b49d03fd6d28232 [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:
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800582 *z = -10;
583 return 0;
Hai Shalomc3565922019-10-28 11:58:20 -0700584 case 20:
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800585 *z = -12;
586 return 0;
Hai Shalomc3565922019-10-28 11:58:20 -0700587 case 21:
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800588 *z = -4;
Hai Shalomc3565922019-10-28 11:58:20 -0700589 return 0;
590 case 25:
591 case 29:
592 *z = -5;
593 return 0;
594 case 26:
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800595 *z = 31;
596 return 0;
597 case 28:
598 *z = -2;
Hai Shalomc3565922019-10-28 11:58:20 -0700599 return 0;
600 case 30:
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800601 *z = 7;
Hai Shalomc3565922019-10-28 11:58:20 -0700602 return 0;
603 }
604
605 return -1;
606}
607
608
609static void debug_print_bignum(const char *title, const struct crypto_bignum *a,
610 size_t prime_len)
611{
612 u8 *bin;
613
614 bin = os_malloc(prime_len);
615 if (bin && crypto_bignum_to_bin(a, bin, prime_len, prime_len) >= 0)
616 wpa_hexdump_key(MSG_DEBUG, title, bin, prime_len);
617 else
618 wpa_printf(MSG_DEBUG, "Could not print bignum (%s)", title);
619 bin_clear_free(bin, prime_len);
620}
621
622
623static struct crypto_ec_point * sswu(struct crypto_ec *ec, int group,
624 const struct crypto_bignum *u)
625{
626 int z_int;
627 const struct crypto_bignum *a, *b, *prime;
628 struct crypto_bignum *u2, *t1, *t2, *z, *t, *zero, *one, *two, *three,
629 *x1a, *x1b, *y = NULL;
630 struct crypto_bignum *x1 = NULL, *x2, *gx1, *gx2, *v = NULL;
631 unsigned int m_is_zero, is_qr, is_eq;
632 size_t prime_len;
633 u8 bin[SAE_MAX_ECC_PRIME_LEN];
634 u8 bin1[SAE_MAX_ECC_PRIME_LEN];
635 u8 bin2[SAE_MAX_ECC_PRIME_LEN];
636 u8 x_y[2 * SAE_MAX_ECC_PRIME_LEN];
637 struct crypto_ec_point *p = NULL;
638
639 if (sswu_curve_param(group, &z_int) < 0)
640 return NULL;
641
642 prime = crypto_ec_get_prime(ec);
643 prime_len = crypto_ec_prime_len(ec);
644 a = crypto_ec_get_a(ec);
645 b = crypto_ec_get_b(ec);
646
647 u2 = crypto_bignum_init();
648 t1 = crypto_bignum_init();
649 t2 = crypto_bignum_init();
650 z = crypto_bignum_init_uint(abs(z_int));
651 t = crypto_bignum_init();
652 zero = crypto_bignum_init_uint(0);
653 one = crypto_bignum_init_uint(1);
654 two = crypto_bignum_init_uint(2);
655 three = crypto_bignum_init_uint(3);
656 x1a = crypto_bignum_init();
657 x1b = crypto_bignum_init();
658 x2 = crypto_bignum_init();
659 gx1 = crypto_bignum_init();
660 gx2 = crypto_bignum_init();
661 if (!u2 || !t1 || !t2 || !z || !t || !zero || !one || !two || !three ||
662 !x1a || !x1b || !x2 || !gx1 || !gx2)
663 goto fail;
664
665 if (z_int < 0 && crypto_bignum_sub(prime, z, z) < 0)
666 goto fail;
667
668 /* m = z^2 * u^4 + z * u^2 */
669 /* --> tmp = z * u^2, m = tmp^2 + tmp */
670
671 /* u2 = u^2
672 * t1 = z * u2
673 * t2 = t1^2
674 * m = t1 = t1 + t2 */
675 if (crypto_bignum_sqrmod(u, prime, u2) < 0 ||
676 crypto_bignum_mulmod(z, u2, prime, t1) < 0 ||
677 crypto_bignum_sqrmod(t1, prime, t2) < 0 ||
678 crypto_bignum_addmod(t1, t2, prime, t1) < 0)
679 goto fail;
680 debug_print_bignum("SSWU: m", t1, prime_len);
681
682 /* l = CEQ(m, 0)
683 * t = CSEL(l, 0, inverse(m); where inverse(x) is calculated as
684 * x^(p-2) modulo p which will handle m == 0 case correctly */
685 /* TODO: Make sure crypto_bignum_is_zero() is constant time */
686 m_is_zero = const_time_eq(crypto_bignum_is_zero(t1), 1);
687 /* t = m^(p-2) modulo p */
688 if (crypto_bignum_sub(prime, two, t2) < 0 ||
689 crypto_bignum_exptmod(t1, t2, prime, t) < 0)
690 goto fail;
691 debug_print_bignum("SSWU: t", t, prime_len);
692
693 /* b / (z * a) */
694 if (crypto_bignum_mulmod(z, a, prime, t1) < 0 ||
695 crypto_bignum_inverse(t1, prime, t1) < 0 ||
696 crypto_bignum_mulmod(b, t1, prime, x1a) < 0)
697 goto fail;
698 debug_print_bignum("SSWU: x1a = b / (z * a)", x1a, prime_len);
699
700 /* (-b/a) * (1 + t) */
701 if (crypto_bignum_sub(prime, b, t1) < 0 ||
702 crypto_bignum_inverse(a, prime, t2) < 0 ||
703 crypto_bignum_mulmod(t1, t2, prime, t1) < 0 ||
704 crypto_bignum_addmod(one, t, prime, t2) < 0 ||
705 crypto_bignum_mulmod(t1, t2, prime, x1b) < 0)
706 goto fail;
707 debug_print_bignum("SSWU: x1b = (-b/a) * (1 + t)", x1b, prime_len);
708
709 /* x1 = CSEL(CEQ(m, 0), x1a, x1b) */
710 if (crypto_bignum_to_bin(x1a, bin1, sizeof(bin1), prime_len) < 0 ||
711 crypto_bignum_to_bin(x1b, bin2, sizeof(bin2), prime_len) < 0)
712 goto fail;
713 const_time_select_bin(m_is_zero, bin1, bin2, prime_len, bin);
714 x1 = crypto_bignum_init_set(bin, prime_len);
715 debug_print_bignum("SSWU: x1 = CSEL(l, x1a, x1b)", x1, prime_len);
716
717 /* gx1 = x1^3 + a * x1 + b */
718 if (crypto_bignum_exptmod(x1, three, prime, t1) < 0 ||
719 crypto_bignum_mulmod(a, x1, prime, t2) < 0 ||
720 crypto_bignum_addmod(t1, t2, prime, t1) < 0 ||
721 crypto_bignum_addmod(t1, b, prime, gx1) < 0)
722 goto fail;
723 debug_print_bignum("SSWU: gx1 = x1^3 + a * x1 + b", gx1, prime_len);
724
725 /* x2 = z * u^2 * x1 */
726 if (crypto_bignum_mulmod(z, u2, prime, t1) < 0 ||
727 crypto_bignum_mulmod(t1, x1, prime, x2) < 0)
728 goto fail;
729 debug_print_bignum("SSWU: x2 = z * u^2 * x1", x2, prime_len);
730
731 /* gx2 = x2^3 + a * x2 + b */
732 if (crypto_bignum_exptmod(x2, three, prime, t1) < 0 ||
733 crypto_bignum_mulmod(a, x2, prime, t2) < 0 ||
734 crypto_bignum_addmod(t1, t2, prime, t1) < 0 ||
735 crypto_bignum_addmod(t1, b, prime, gx2) < 0)
736 goto fail;
737 debug_print_bignum("SSWU: gx2 = x2^3 + a * x2 + b", gx2, prime_len);
738
739 /* l = gx1 is a quadratic residue modulo p
740 * --> gx1^((p-1)/2) modulo p is zero or one */
741 if (crypto_bignum_sub(prime, one, t1) < 0 ||
742 crypto_bignum_rshift(t1, 1, t1) < 0 ||
743 crypto_bignum_exptmod(gx1, t1, prime, t1) < 0)
744 goto fail;
745 debug_print_bignum("SSWU: gx1^((p-1)/2) modulo p", t1, prime_len);
746 is_qr = const_time_eq(crypto_bignum_is_zero(t1) |
747 crypto_bignum_is_one(t1), 1);
748
749 /* v = CSEL(l, gx1, gx2) */
750 if (crypto_bignum_to_bin(gx1, bin1, sizeof(bin1), prime_len) < 0 ||
751 crypto_bignum_to_bin(gx2, bin2, sizeof(bin2), prime_len) < 0)
752 goto fail;
753 const_time_select_bin(is_qr, bin1, bin2, prime_len, bin);
754 v = crypto_bignum_init_set(bin, prime_len);
755 debug_print_bignum("SSWU: v = CSEL(l, gx1, gx2)", v, prime_len);
756
757 /* x = CSEL(l, x1, x2) */
758 if (crypto_bignum_to_bin(x1, bin1, sizeof(bin1), prime_len) < 0 ||
759 crypto_bignum_to_bin(x2, bin2, sizeof(bin2), prime_len) < 0)
760 goto fail;
761 const_time_select_bin(is_qr, bin1, bin2, prime_len, x_y);
762 wpa_hexdump_key(MSG_DEBUG, "SSWU: x = CSEL(l, x1, x2)", x_y, prime_len);
763
764 /* y = sqrt(v)
765 * For prime p such that p = 3 mod 4 --> v^((p+1)/4) */
766 if (crypto_bignum_to_bin(prime, bin1, sizeof(bin1), prime_len) < 0)
767 goto fail;
768 if ((bin1[prime_len - 1] & 0x03) != 3) {
769 wpa_printf(MSG_DEBUG, "SSWU: prime does not have p = 3 mod 4");
770 goto fail;
771 }
772 y = crypto_bignum_init();
773 if (!y ||
774 crypto_bignum_add(prime, one, t1) < 0 ||
775 crypto_bignum_rshift(t1, 2, t1) < 0 ||
776 crypto_bignum_exptmod(v, t1, prime, y) < 0)
777 goto fail;
778 debug_print_bignum("SSWU: y = sqrt(v)", y, prime_len);
779
780 /* l = CEQ(LSB(u), LSB(y)) */
781 if (crypto_bignum_to_bin(u, bin1, sizeof(bin1), prime_len) < 0 ||
782 crypto_bignum_to_bin(y, bin2, sizeof(bin2), prime_len) < 0)
783 goto fail;
784 is_eq = const_time_eq(bin1[prime_len - 1] & 0x01,
785 bin2[prime_len - 1] & 0x01);
786
787 /* P = CSEL(l, (x,y), (x, p-y)) */
788 if (crypto_bignum_sub(prime, y, t1) < 0)
789 goto fail;
790 debug_print_bignum("SSWU: p - y", t1, prime_len);
791 if (crypto_bignum_to_bin(y, bin1, sizeof(bin1), prime_len) < 0 ||
792 crypto_bignum_to_bin(t1, bin2, sizeof(bin2), prime_len) < 0)
793 goto fail;
794 const_time_select_bin(is_eq, bin1, bin2, prime_len, &x_y[prime_len]);
795
796 /* output P */
797 wpa_hexdump_key(MSG_DEBUG, "SSWU: P.x", x_y, prime_len);
798 wpa_hexdump_key(MSG_DEBUG, "SSWU: P.y", &x_y[prime_len], prime_len);
799 p = crypto_ec_point_from_bin(ec, x_y);
800
801fail:
802 crypto_bignum_deinit(u2, 1);
803 crypto_bignum_deinit(t1, 1);
804 crypto_bignum_deinit(t2, 1);
805 crypto_bignum_deinit(z, 0);
806 crypto_bignum_deinit(t, 1);
807 crypto_bignum_deinit(x1a, 1);
808 crypto_bignum_deinit(x1b, 1);
809 crypto_bignum_deinit(x1, 1);
810 crypto_bignum_deinit(x2, 1);
811 crypto_bignum_deinit(gx1, 1);
812 crypto_bignum_deinit(gx2, 1);
813 crypto_bignum_deinit(y, 1);
814 crypto_bignum_deinit(v, 1);
815 crypto_bignum_deinit(zero, 0);
816 crypto_bignum_deinit(one, 0);
817 crypto_bignum_deinit(two, 0);
818 crypto_bignum_deinit(three, 0);
819 forced_memzero(bin, sizeof(bin));
820 forced_memzero(bin1, sizeof(bin1));
821 forced_memzero(bin2, sizeof(bin2));
822 forced_memzero(x_y, sizeof(x_y));
823 return p;
824}
825
826
827static int sae_pwd_seed(size_t hash_len, const u8 *ssid, size_t ssid_len,
828 const u8 *password, size_t password_len,
829 const char *identifier, u8 *pwd_seed)
830{
831 const u8 *addr[2];
832 size_t len[2];
833 size_t num_elem;
834
835 /* pwd-seed = HKDF-Extract(ssid, password [ || identifier ]) */
836 addr[0] = password;
837 len[0] = password_len;
838 num_elem = 1;
839 wpa_hexdump_ascii(MSG_DEBUG, "SAE: SSID", ssid, ssid_len);
840 wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
841 password, password_len);
842 if (identifier) {
843 wpa_printf(MSG_DEBUG, "SAE: password identifier: %s",
844 identifier);
845 addr[num_elem] = (const u8 *) identifier;
846 len[num_elem] = os_strlen(identifier);
847 num_elem++;
848 }
849 if (hkdf_extract(hash_len, ssid, ssid_len, num_elem, addr, len,
850 pwd_seed) < 0)
851 return -1;
852 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, hash_len);
853 return 0;
854}
855
856
857size_t sae_ecc_prime_len_2_hash_len(size_t prime_len)
858{
859 if (prime_len <= 256 / 8)
860 return 32;
861 if (prime_len <= 384 / 8)
862 return 48;
863 return 64;
864}
865
866
867struct crypto_ec_point *
868sae_derive_pt_ecc(struct crypto_ec *ec, int group,
869 const u8 *ssid, size_t ssid_len,
870 const u8 *password, size_t password_len,
871 const char *identifier)
872{
873 u8 pwd_seed[64];
874 u8 pwd_value[SAE_MAX_ECC_PRIME_LEN * 2];
875 size_t pwd_value_len, hash_len, prime_len;
876 const struct crypto_bignum *prime;
877 struct crypto_bignum *bn = NULL;
878 struct crypto_ec_point *p1 = NULL, *p2 = NULL, *pt = NULL;
879
880 prime = crypto_ec_get_prime(ec);
881 prime_len = crypto_ec_prime_len(ec);
882 if (prime_len > SAE_MAX_ECC_PRIME_LEN)
883 goto fail;
884 hash_len = sae_ecc_prime_len_2_hash_len(prime_len);
885
886 /* len = olen(p) + ceil(olen(p)/2) */
887 pwd_value_len = prime_len + (prime_len + 1) / 2;
888
889 if (sae_pwd_seed(hash_len, ssid, ssid_len, password, password_len,
890 identifier, pwd_seed) < 0)
891 goto fail;
892
893 /* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u1 P1", len)
894 */
895 if (hkdf_expand(hash_len, pwd_seed, hash_len,
896 "SAE Hash to Element u1 P1", pwd_value, pwd_value_len) <
897 0)
898 goto fail;
899 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value (u1 P1)",
900 pwd_value, pwd_value_len);
901
902 /* u1 = pwd-value modulo p */
903 bn = crypto_bignum_init_set(pwd_value, pwd_value_len);
904 if (!bn || crypto_bignum_mod(bn, prime, bn) < 0 ||
905 crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value),
906 prime_len) < 0)
907 goto fail;
908 wpa_hexdump_key(MSG_DEBUG, "SAE: u1", pwd_value, prime_len);
909
910 /* P1 = SSWU(u1) */
911 p1 = sswu(ec, group, bn);
912 if (!p1)
913 goto fail;
914
915 /* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u2 P2", len)
916 */
917 if (hkdf_expand(hash_len, pwd_seed, hash_len,
918 "SAE Hash to Element u2 P2", pwd_value,
919 pwd_value_len) < 0)
920 goto fail;
921 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value (u2 P2)",
922 pwd_value, pwd_value_len);
923
924 /* u2 = pwd-value modulo p */
925 crypto_bignum_deinit(bn, 1);
926 bn = crypto_bignum_init_set(pwd_value, pwd_value_len);
927 if (!bn || crypto_bignum_mod(bn, prime, bn) < 0 ||
928 crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value),
929 prime_len) < 0)
930 goto fail;
931 wpa_hexdump_key(MSG_DEBUG, "SAE: u2", pwd_value, prime_len);
932
933 /* P2 = SSWU(u2) */
934 p2 = sswu(ec, group, bn);
935 if (!p2)
936 goto fail;
937
938 /* PT = elem-op(P1, P2) */
939 pt = crypto_ec_point_init(ec);
940 if (!pt)
941 goto fail;
942 if (crypto_ec_point_add(ec, p1, p2, pt) < 0) {
943 crypto_ec_point_deinit(pt, 1);
944 pt = NULL;
945 }
946
947fail:
948 forced_memzero(pwd_seed, sizeof(pwd_seed));
949 forced_memzero(pwd_value, sizeof(pwd_value));
950 crypto_bignum_deinit(bn, 1);
951 crypto_ec_point_deinit(p1, 1);
952 crypto_ec_point_deinit(p2, 1);
953 return pt;
954}
955
956
957size_t sae_ffc_prime_len_2_hash_len(size_t prime_len)
958{
959 if (prime_len <= 2048 / 8)
960 return 32;
961 if (prime_len <= 3072 / 8)
962 return 48;
963 return 64;
964}
965
966
967static struct crypto_bignum *
968sae_derive_pt_ffc(const struct dh_group *dh, int group,
969 const u8 *ssid, size_t ssid_len,
970 const u8 *password, size_t password_len,
971 const char *identifier)
972{
973 size_t hash_len, prime_len, pwd_value_len;
974 struct crypto_bignum *prime, *order;
975 struct crypto_bignum *one = NULL, *two = NULL, *bn = NULL, *tmp = NULL,
976 *pt = NULL;
977 u8 pwd_seed[64];
978 u8 pwd_value[SAE_MAX_PRIME_LEN + SAE_MAX_PRIME_LEN / 2];
979
980 prime = crypto_bignum_init_set(dh->prime, dh->prime_len);
981 order = crypto_bignum_init_set(dh->order, dh->order_len);
982 if (!prime || !order)
983 goto fail;
984 prime_len = dh->prime_len;
985 if (prime_len > SAE_MAX_PRIME_LEN)
986 goto fail;
987 hash_len = sae_ffc_prime_len_2_hash_len(prime_len);
988
989 /* len = olen(p) + ceil(olen(p)/2) */
990 pwd_value_len = prime_len + (prime_len + 1) / 2;
991 if (pwd_value_len > sizeof(pwd_value))
992 goto fail;
993
994 if (sae_pwd_seed(hash_len, ssid, ssid_len, password, password_len,
995 identifier, pwd_seed) < 0)
996 goto fail;
997
998 /* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element", len) */
999 if (hkdf_expand(hash_len, pwd_seed, hash_len,
1000 "SAE Hash to Element", pwd_value, pwd_value_len) < 0)
1001 goto fail;
1002 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value",
1003 pwd_value, pwd_value_len);
1004
1005 /* pwd-value = (pwd-value modulo (p-2)) + 2 */
1006 bn = crypto_bignum_init_set(pwd_value, pwd_value_len);
1007 one = crypto_bignum_init_uint(1);
1008 two = crypto_bignum_init_uint(2);
1009 tmp = crypto_bignum_init();
1010 if (!bn || !one || !two || !tmp ||
1011 crypto_bignum_sub(prime, two, tmp) < 0 ||
1012 crypto_bignum_mod(bn, tmp, bn) < 0 ||
1013 crypto_bignum_add(bn, two, bn) < 0 ||
1014 crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value),
1015 prime_len) < 0)
1016 goto fail;
1017 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value(reduced)",
1018 pwd_value, prime_len);
1019
1020 /* PT = pwd-value^((p-1)/q) modulo p */
1021 pt = crypto_bignum_init();
1022 if (!pt ||
1023 crypto_bignum_sub(prime, one, tmp) < 0 ||
1024 crypto_bignum_div(tmp, order, tmp) < 0 ||
1025 crypto_bignum_exptmod(bn, tmp, prime, pt) < 0) {
1026 crypto_bignum_deinit(pt, 1);
1027 pt = NULL;
1028 goto fail;
1029 }
1030 debug_print_bignum("SAE: PT", pt, prime_len);
1031
1032fail:
1033 forced_memzero(pwd_seed, sizeof(pwd_seed));
1034 forced_memzero(pwd_value, sizeof(pwd_value));
1035 crypto_bignum_deinit(bn, 1);
1036 crypto_bignum_deinit(tmp, 1);
1037 crypto_bignum_deinit(one, 0);
1038 crypto_bignum_deinit(two, 0);
1039 crypto_bignum_deinit(prime, 0);
1040 crypto_bignum_deinit(order, 0);
1041 return pt;
1042}
1043
1044
1045static struct sae_pt *
1046sae_derive_pt_group(int group, const u8 *ssid, size_t ssid_len,
1047 const u8 *password, size_t password_len,
1048 const char *identifier)
1049{
1050 struct sae_pt *pt;
1051
1052 wpa_printf(MSG_DEBUG, "SAE: Derive PT - group %d", group);
1053
1054 pt = os_zalloc(sizeof(*pt));
1055 if (!pt)
1056 return NULL;
1057
1058 pt->group = group;
1059 pt->ec = crypto_ec_init(group);
1060 if (pt->ec) {
1061 pt->ecc_pt = sae_derive_pt_ecc(pt->ec, group, ssid, ssid_len,
1062 password, password_len,
1063 identifier);
1064 if (!pt->ecc_pt) {
1065 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PT");
1066 goto fail;
1067 }
1068
1069 return pt;
1070 }
1071
1072 pt->dh = dh_groups_get(group);
1073 if (!pt->dh) {
1074 wpa_printf(MSG_DEBUG, "SAE: Unsupported group %d", group);
1075 goto fail;
1076 }
1077
1078 pt->ffc_pt = sae_derive_pt_ffc(pt->dh, group, ssid, ssid_len,
1079 password, password_len, identifier);
1080 if (!pt->ffc_pt) {
1081 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PT");
1082 goto fail;
1083 }
1084
1085 return pt;
1086fail:
1087 sae_deinit_pt(pt);
1088 return NULL;
1089}
1090
1091
1092struct sae_pt * sae_derive_pt(int *groups, const u8 *ssid, size_t ssid_len,
1093 const u8 *password, size_t password_len,
1094 const char *identifier)
1095{
1096 struct sae_pt *pt = NULL, *last = NULL, *tmp;
1097 int default_groups[] = { 19, 0 };
1098 int i;
1099
1100 if (!groups)
1101 groups = default_groups;
1102 for (i = 0; groups[i] > 0; i++) {
1103 tmp = sae_derive_pt_group(groups[i], ssid, ssid_len, password,
1104 password_len, identifier);
1105 if (!tmp)
1106 continue;
1107
1108 if (last)
1109 last->next = tmp;
1110 else
1111 pt = tmp;
1112 last = tmp;
1113 }
1114
1115 return pt;
1116}
1117
1118
1119static void sae_max_min_addr(const u8 *addr[], size_t len[],
1120 const u8 *addr1, const u8 *addr2)
1121{
1122 len[0] = ETH_ALEN;
1123 len[1] = ETH_ALEN;
1124 if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) {
1125 addr[0] = addr1;
1126 addr[1] = addr2;
1127 } else {
1128 addr[0] = addr2;
1129 addr[1] = addr1;
1130 }
1131}
1132
1133
1134struct crypto_ec_point *
1135sae_derive_pwe_from_pt_ecc(const struct sae_pt *pt,
1136 const u8 *addr1, const u8 *addr2)
1137{
1138 u8 bin[SAE_MAX_ECC_PRIME_LEN * 2];
1139 size_t prime_len;
1140 const u8 *addr[2];
1141 size_t len[2];
1142 u8 salt[64], hash[64];
1143 size_t hash_len;
1144 const struct crypto_bignum *order;
1145 struct crypto_bignum *tmp = NULL, *val = NULL, *one = NULL;
1146 struct crypto_ec_point *pwe = NULL;
1147
1148 wpa_printf(MSG_DEBUG, "SAE: Derive PWE from PT");
1149 prime_len = crypto_ec_prime_len(pt->ec);
1150 if (crypto_ec_point_to_bin(pt->ec, pt->ecc_pt,
1151 bin, bin + prime_len) < 0)
1152 return NULL;
1153 wpa_hexdump_key(MSG_DEBUG, "SAE: PT.x", bin, prime_len);
1154 wpa_hexdump_key(MSG_DEBUG, "SAE: PT.y", bin + prime_len, prime_len);
1155
1156 sae_max_min_addr(addr, len, addr1, addr2);
1157
1158 /* val = H(0^n,
1159 * MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC)) */
1160 wpa_printf(MSG_DEBUG, "SAE: val = H(0^n, MAX(addrs) || MIN(addrs))");
1161 hash_len = sae_ecc_prime_len_2_hash_len(prime_len);
1162 os_memset(salt, 0, hash_len);
1163 if (hkdf_extract(hash_len, salt, hash_len, 2, addr, len, hash) < 0)
1164 goto fail;
1165 wpa_hexdump(MSG_DEBUG, "SAE: val", hash, hash_len);
1166
1167 /* val = val modulo (q - 1) + 1 */
1168 order = crypto_ec_get_order(pt->ec);
1169 tmp = crypto_bignum_init();
1170 val = crypto_bignum_init_set(hash, hash_len);
1171 one = crypto_bignum_init_uint(1);
1172 if (!tmp || !val || !one ||
1173 crypto_bignum_sub(order, one, tmp) < 0 ||
1174 crypto_bignum_mod(val, tmp, val) < 0 ||
1175 crypto_bignum_add(val, one, val) < 0)
1176 goto fail;
1177 debug_print_bignum("SAE: val(reduced to 1..q-1)", val, prime_len);
1178
1179 /* PWE = scalar-op(val, PT) */
1180 pwe = crypto_ec_point_init(pt->ec);
1181 if (!pwe ||
1182 crypto_ec_point_mul(pt->ec, pt->ecc_pt, val, pwe) < 0 ||
1183 crypto_ec_point_to_bin(pt->ec, pwe, bin, bin + prime_len) < 0) {
1184 crypto_ec_point_deinit(pwe, 1);
1185 pwe = NULL;
1186 goto fail;
1187 }
1188 wpa_hexdump_key(MSG_DEBUG, "SAE: PWE.x", bin, prime_len);
1189 wpa_hexdump_key(MSG_DEBUG, "SAE: PWE.y", bin + prime_len, prime_len);
1190
1191fail:
1192 crypto_bignum_deinit(tmp, 1);
1193 crypto_bignum_deinit(val, 1);
1194 crypto_bignum_deinit(one, 0);
1195 return pwe;
1196}
1197
1198
1199struct crypto_bignum *
1200sae_derive_pwe_from_pt_ffc(const struct sae_pt *pt,
1201 const u8 *addr1, const u8 *addr2)
1202{
1203 size_t prime_len;
1204 const u8 *addr[2];
1205 size_t len[2];
1206 u8 salt[64], hash[64];
1207 size_t hash_len;
1208 struct crypto_bignum *tmp = NULL, *val = NULL, *one = NULL;
1209 struct crypto_bignum *pwe = NULL, *order = NULL, *prime = NULL;
1210
1211 wpa_printf(MSG_DEBUG, "SAE: Derive PWE from PT");
1212 prime = crypto_bignum_init_set(pt->dh->prime, pt->dh->prime_len);
1213 order = crypto_bignum_init_set(pt->dh->order, pt->dh->order_len);
1214 if (!prime || !order)
1215 goto fail;
1216 prime_len = pt->dh->prime_len;
1217
1218 sae_max_min_addr(addr, len, addr1, addr2);
1219
1220 /* val = H(0^n,
1221 * MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC)) */
1222 wpa_printf(MSG_DEBUG, "SAE: val = H(0^n, MAX(addrs) || MIN(addrs))");
1223 hash_len = sae_ffc_prime_len_2_hash_len(prime_len);
1224 os_memset(salt, 0, hash_len);
1225 if (hkdf_extract(hash_len, salt, hash_len, 2, addr, len, hash) < 0)
1226 goto fail;
1227 wpa_hexdump(MSG_DEBUG, "SAE: val", hash, hash_len);
1228
1229 /* val = val modulo (q - 1) + 1 */
1230 tmp = crypto_bignum_init();
1231 val = crypto_bignum_init_set(hash, hash_len);
1232 one = crypto_bignum_init_uint(1);
1233 if (!tmp || !val || !one ||
1234 crypto_bignum_sub(order, one, tmp) < 0 ||
1235 crypto_bignum_mod(val, tmp, val) < 0 ||
1236 crypto_bignum_add(val, one, val) < 0)
1237 goto fail;
1238 debug_print_bignum("SAE: val(reduced to 1..q-1)", val, prime_len);
1239
1240 /* PWE = scalar-op(val, PT) */
1241 pwe = crypto_bignum_init();
1242 if (!pwe || crypto_bignum_exptmod(pt->ffc_pt, val, prime, pwe) < 0) {
1243 crypto_bignum_deinit(pwe, 1);
1244 pwe = NULL;
1245 goto fail;
1246 }
1247 debug_print_bignum("SAE: PWE", pwe, prime_len);
1248
1249fail:
1250 crypto_bignum_deinit(tmp, 1);
1251 crypto_bignum_deinit(val, 1);
1252 crypto_bignum_deinit(one, 0);
1253 crypto_bignum_deinit(prime, 0);
1254 crypto_bignum_deinit(order, 0);
1255 return pwe;
1256}
1257
1258
1259void sae_deinit_pt(struct sae_pt *pt)
1260{
1261 struct sae_pt *prev;
1262
1263 while (pt) {
1264 crypto_ec_point_deinit(pt->ecc_pt, 1);
1265 crypto_bignum_deinit(pt->ffc_pt, 1);
1266 crypto_ec_deinit(pt->ec);
1267 prev = pt;
1268 pt = pt->next;
1269 os_free(prev);
1270 }
1271}
1272
1273
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001274static int sae_derive_commit_element_ecc(struct sae_data *sae,
1275 struct crypto_bignum *mask)
1276{
1277 /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
1278 if (!sae->tmp->own_commit_element_ecc) {
1279 sae->tmp->own_commit_element_ecc =
1280 crypto_ec_point_init(sae->tmp->ec);
1281 if (!sae->tmp->own_commit_element_ecc)
1282 return -1;
1283 }
1284
1285 if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc, mask,
1286 sae->tmp->own_commit_element_ecc) < 0 ||
1287 crypto_ec_point_invert(sae->tmp->ec,
1288 sae->tmp->own_commit_element_ecc) < 0) {
1289 wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
1290 return -1;
1291 }
1292
1293 return 0;
1294}
1295
1296
1297static int sae_derive_commit_element_ffc(struct sae_data *sae,
1298 struct crypto_bignum *mask)
1299{
1300 /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
1301 if (!sae->tmp->own_commit_element_ffc) {
1302 sae->tmp->own_commit_element_ffc = crypto_bignum_init();
1303 if (!sae->tmp->own_commit_element_ffc)
1304 return -1;
1305 }
1306
1307 if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, mask, sae->tmp->prime,
1308 sae->tmp->own_commit_element_ffc) < 0 ||
1309 crypto_bignum_inverse(sae->tmp->own_commit_element_ffc,
1310 sae->tmp->prime,
1311 sae->tmp->own_commit_element_ffc) < 0) {
1312 wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
1313 return -1;
1314 }
1315
1316 return 0;
1317}
1318
1319
1320static int sae_derive_commit(struct sae_data *sae)
1321{
1322 struct crypto_bignum *mask;
Hai Shalom81f62d82019-07-22 12:10:00 -07001323 int ret;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001324
Hai Shalom81f62d82019-07-22 12:10:00 -07001325 mask = crypto_bignum_init();
1326 if (!sae->tmp->sae_rand)
1327 sae->tmp->sae_rand = crypto_bignum_init();
1328 if (!sae->tmp->own_commit_scalar)
1329 sae->tmp->own_commit_scalar = crypto_bignum_init();
1330 ret = !mask || !sae->tmp->sae_rand || !sae->tmp->own_commit_scalar ||
1331 dragonfly_generate_scalar(sae->tmp->order, sae->tmp->sae_rand,
1332 mask,
1333 sae->tmp->own_commit_scalar) < 0 ||
1334 (sae->tmp->ec &&
1335 sae_derive_commit_element_ecc(sae, mask) < 0) ||
1336 (sae->tmp->dh &&
1337 sae_derive_commit_element_ffc(sae, mask) < 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001338 crypto_bignum_deinit(mask, 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07001339 return ret ? -1 : 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001340}
1341
1342
1343int sae_prepare_commit(const u8 *addr1, const u8 *addr2,
1344 const u8 *password, size_t password_len,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001345 const char *identifier, struct sae_data *sae)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001346{
Dmitry Shmidt41712582015-06-29 11:02:15 -07001347 if (sae->tmp == NULL ||
1348 (sae->tmp->ec && sae_derive_pwe_ecc(sae, addr1, addr2, password,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001349 password_len,
1350 identifier) < 0) ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07001351 (sae->tmp->dh && sae_derive_pwe_ffc(sae, addr1, addr2, password,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001352 password_len,
Hai Shalomc3565922019-10-28 11:58:20 -07001353 identifier) < 0))
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001354 return -1;
Hai Shalomc3565922019-10-28 11:58:20 -07001355
1356 sae->tmp->h2e = 0;
1357 return sae_derive_commit(sae);
1358}
1359
1360
1361int sae_prepare_commit_pt(struct sae_data *sae, const struct sae_pt *pt,
1362 const u8 *addr1, const u8 *addr2,
1363 int *rejected_groups)
1364{
1365 if (!sae->tmp)
1366 return -1;
1367
1368 while (pt) {
1369 if (pt->group == sae->group)
1370 break;
1371 pt = pt->next;
1372 }
1373 if (!pt) {
1374 wpa_printf(MSG_INFO, "SAE: Could not find PT for group %u",
1375 sae->group);
1376 return -1;
1377 }
1378
1379 sae->tmp->own_addr_higher = os_memcmp(addr1, addr2, ETH_ALEN) > 0;
1380 wpabuf_free(sae->tmp->own_rejected_groups);
1381 sae->tmp->own_rejected_groups = NULL;
1382 if (rejected_groups) {
1383 int count, i;
1384 struct wpabuf *groups;
1385
1386 count = int_array_len(rejected_groups);
1387 groups = wpabuf_alloc(count * 2);
1388 if (!groups)
1389 return -1;
1390 for (i = 0; i < count; i++)
1391 wpabuf_put_le16(groups, rejected_groups[i]);
1392 sae->tmp->own_rejected_groups = groups;
1393 }
1394
1395 if (pt->ec) {
1396 crypto_ec_point_deinit(sae->tmp->pwe_ecc, 1);
1397 sae->tmp->pwe_ecc = sae_derive_pwe_from_pt_ecc(pt, addr1,
1398 addr2);
1399 if (!sae->tmp->pwe_ecc)
1400 return -1;
1401 }
1402
1403 if (pt->dh) {
1404 crypto_bignum_deinit(sae->tmp->pwe_ffc, 1);
1405 sae->tmp->pwe_ffc = sae_derive_pwe_from_pt_ffc(pt, addr1,
1406 addr2);
1407 if (!sae->tmp->pwe_ffc)
1408 return -1;
1409 }
1410
1411 sae->tmp->h2e = 1;
1412 return sae_derive_commit(sae);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001413}
1414
1415
1416static int sae_derive_k_ecc(struct sae_data *sae, u8 *k)
1417{
1418 struct crypto_ec_point *K;
1419 int ret = -1;
1420
1421 K = crypto_ec_point_init(sae->tmp->ec);
1422 if (K == NULL)
1423 goto fail;
1424
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001425 /*
1426 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
1427 * PEER-COMMIT-ELEMENT)))
1428 * If K is identity element (point-at-infinity), reject
1429 * k = F(K) (= x coordinate)
1430 */
1431
1432 if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc,
1433 sae->peer_commit_scalar, K) < 0 ||
1434 crypto_ec_point_add(sae->tmp->ec, K,
1435 sae->tmp->peer_commit_element_ecc, K) < 0 ||
1436 crypto_ec_point_mul(sae->tmp->ec, K, sae->tmp->sae_rand, K) < 0 ||
1437 crypto_ec_point_is_at_infinity(sae->tmp->ec, K) ||
1438 crypto_ec_point_to_bin(sae->tmp->ec, K, k, NULL) < 0) {
1439 wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
1440 goto fail;
1441 }
1442
1443 wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
1444
1445 ret = 0;
1446fail:
1447 crypto_ec_point_deinit(K, 1);
1448 return ret;
1449}
1450
1451
1452static int sae_derive_k_ffc(struct sae_data *sae, u8 *k)
1453{
1454 struct crypto_bignum *K;
1455 int ret = -1;
1456
1457 K = crypto_bignum_init();
1458 if (K == NULL)
1459 goto fail;
1460
1461 /*
1462 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
1463 * PEER-COMMIT-ELEMENT)))
1464 * If K is identity element (one), reject.
1465 * k = F(K) (= x coordinate)
1466 */
1467
1468 if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, sae->peer_commit_scalar,
1469 sae->tmp->prime, K) < 0 ||
1470 crypto_bignum_mulmod(K, sae->tmp->peer_commit_element_ffc,
1471 sae->tmp->prime, K) < 0 ||
1472 crypto_bignum_exptmod(K, sae->tmp->sae_rand, sae->tmp->prime, K) < 0
1473 ||
1474 crypto_bignum_is_one(K) ||
1475 crypto_bignum_to_bin(K, k, SAE_MAX_PRIME_LEN, sae->tmp->prime_len) <
1476 0) {
1477 wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
1478 goto fail;
1479 }
1480
1481 wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
1482
1483 ret = 0;
1484fail:
1485 crypto_bignum_deinit(K, 1);
1486 return ret;
1487}
1488
1489
Hai Shalomc3565922019-10-28 11:58:20 -07001490static int sae_kdf_hash(size_t hash_len, const u8 *k, const char *label,
1491 const u8 *context, size_t context_len,
1492 u8 *out, size_t out_len)
1493{
1494 if (hash_len == 32)
1495 return sha256_prf(k, hash_len, label,
1496 context, context_len, out, out_len);
1497#ifdef CONFIG_SHA384
1498 if (hash_len == 48)
1499 return sha384_prf(k, hash_len, label,
1500 context, context_len, out, out_len);
1501#endif /* CONFIG_SHA384 */
1502#ifdef CONFIG_SHA512
1503 if (hash_len == 64)
1504 return sha512_prf(k, hash_len, label,
1505 context, context_len, out, out_len);
1506#endif /* CONFIG_SHA512 */
1507 return -1;
1508}
1509
1510
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001511static int sae_derive_keys(struct sae_data *sae, const u8 *k)
1512{
Hai Shalomc3565922019-10-28 11:58:20 -07001513 u8 zero[SAE_MAX_HASH_LEN], val[SAE_MAX_PRIME_LEN];
1514 const u8 *salt;
1515 struct wpabuf *rejected_groups = NULL;
1516 u8 keyseed[SAE_MAX_HASH_LEN];
1517 u8 keys[SAE_MAX_HASH_LEN + SAE_PMK_LEN];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001518 struct crypto_bignum *tmp;
1519 int ret = -1;
Hai Shalomc3565922019-10-28 11:58:20 -07001520 size_t hash_len, salt_len, prime_len = sae->tmp->prime_len;
1521 const u8 *addr[1];
1522 size_t len[1];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001523
1524 tmp = crypto_bignum_init();
1525 if (tmp == NULL)
1526 goto fail;
1527
Hai Shalomc3565922019-10-28 11:58:20 -07001528 /* keyseed = H(salt, k)
1529 * KCK || PMK = KDF-Hash-Length(keyseed, "SAE KCK and PMK",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001530 * (commit-scalar + peer-commit-scalar) modulo r)
1531 * PMKID = L((commit-scalar + peer-commit-scalar) modulo r, 0, 128)
1532 */
Hai Shalomc3565922019-10-28 11:58:20 -07001533 if (!sae->tmp->h2e)
1534 hash_len = SHA256_MAC_LEN;
1535 else if (sae->tmp->dh)
1536 hash_len = sae_ffc_prime_len_2_hash_len(prime_len);
1537 else
1538 hash_len = sae_ecc_prime_len_2_hash_len(prime_len);
1539 if (sae->tmp->h2e && (sae->tmp->own_rejected_groups ||
1540 sae->tmp->peer_rejected_groups)) {
1541 struct wpabuf *own, *peer;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001542
Hai Shalomc3565922019-10-28 11:58:20 -07001543 own = sae->tmp->own_rejected_groups;
1544 peer = sae->tmp->peer_rejected_groups;
1545 salt_len = 0;
1546 if (own)
1547 salt_len += wpabuf_len(own);
1548 if (peer)
1549 salt_len += wpabuf_len(peer);
1550 rejected_groups = wpabuf_alloc(salt_len);
1551 if (!rejected_groups)
1552 goto fail;
1553 if (sae->tmp->own_addr_higher) {
1554 if (own)
1555 wpabuf_put_buf(rejected_groups, own);
1556 if (peer)
1557 wpabuf_put_buf(rejected_groups, peer);
1558 } else {
1559 if (peer)
1560 wpabuf_put_buf(rejected_groups, peer);
1561 if (own)
1562 wpabuf_put_buf(rejected_groups, own);
1563 }
1564 salt = wpabuf_head(rejected_groups);
1565 salt_len = wpabuf_len(rejected_groups);
1566 } else {
1567 os_memset(zero, 0, hash_len);
1568 salt = zero;
1569 salt_len = hash_len;
1570 }
1571 wpa_hexdump(MSG_DEBUG, "SAE: salt for keyseed derivation",
1572 salt, salt_len);
1573 addr[0] = k;
1574 len[0] = prime_len;
1575 if (hkdf_extract(hash_len, salt, salt_len, 1, addr, len, keyseed) < 0)
Dmitry Shmidte4663042016-04-04 10:07:49 -07001576 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07001577 wpa_hexdump_key(MSG_DEBUG, "SAE: keyseed", keyseed, hash_len);
1578
1579 if (crypto_bignum_add(sae->tmp->own_commit_scalar,
1580 sae->peer_commit_scalar, tmp) < 0 ||
1581 crypto_bignum_mod(tmp, sae->tmp->order, tmp) < 0)
1582 goto fail;
1583 /* IEEE Std 802.11-2016 is not exactly clear on the encoding of the bit
1584 * string that is needed for KCK, PMK, and PMKID derivation, but it
1585 * seems to make most sense to encode the
1586 * (commit-scalar + peer-commit-scalar) mod r part as a bit string by
1587 * zero padding it from left to the length of the order (in full
1588 * octets). */
1589 crypto_bignum_to_bin(tmp, val, sizeof(val), sae->tmp->order_len);
1590 wpa_hexdump(MSG_DEBUG, "SAE: PMKID", val, SAE_PMKID_LEN);
1591 if (sae_kdf_hash(hash_len, keyseed, "SAE KCK and PMK",
1592 val, sae->tmp->order_len,
1593 keys, hash_len + SAE_PMK_LEN) < 0)
1594 goto fail;
1595 forced_memzero(keyseed, sizeof(keyseed));
1596 os_memcpy(sae->tmp->kck, keys, hash_len);
1597 sae->tmp->kck_len = hash_len;
1598 os_memcpy(sae->pmk, keys + hash_len, SAE_PMK_LEN);
Dmitry Shmidtd97138d2015-12-28 13:27:49 -08001599 os_memcpy(sae->pmkid, val, SAE_PMKID_LEN);
Hai Shalomc3565922019-10-28 11:58:20 -07001600 forced_memzero(keys, sizeof(keys));
1601 wpa_hexdump_key(MSG_DEBUG, "SAE: KCK",
1602 sae->tmp->kck, sae->tmp->kck_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001603 wpa_hexdump_key(MSG_DEBUG, "SAE: PMK", sae->pmk, SAE_PMK_LEN);
1604
1605 ret = 0;
1606fail:
Hai Shalomc3565922019-10-28 11:58:20 -07001607 wpabuf_free(rejected_groups);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001608 crypto_bignum_deinit(tmp, 0);
1609 return ret;
1610}
1611
1612
1613int sae_process_commit(struct sae_data *sae)
1614{
1615 u8 k[SAE_MAX_PRIME_LEN];
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001616 if (sae->tmp == NULL ||
1617 (sae->tmp->ec && sae_derive_k_ecc(sae, k) < 0) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001618 (sae->tmp->dh && sae_derive_k_ffc(sae, k) < 0) ||
1619 sae_derive_keys(sae, k) < 0)
1620 return -1;
1621 return 0;
1622}
1623
1624
1625void sae_write_commit(struct sae_data *sae, struct wpabuf *buf,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001626 const struct wpabuf *token, const char *identifier)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001627{
1628 u8 *pos;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001629
1630 if (sae->tmp == NULL)
1631 return;
1632
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001633 wpabuf_put_le16(buf, sae->group); /* Finite Cyclic Group */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001634 if (token) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001635 wpabuf_put_buf(buf, token);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001636 wpa_hexdump(MSG_DEBUG, "SAE: Anti-clogging token",
1637 wpabuf_head(token), wpabuf_len(token));
1638 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001639 pos = wpabuf_put(buf, sae->tmp->prime_len);
1640 crypto_bignum_to_bin(sae->tmp->own_commit_scalar, pos,
1641 sae->tmp->prime_len, sae->tmp->prime_len);
1642 wpa_hexdump(MSG_DEBUG, "SAE: own commit-scalar",
1643 pos, sae->tmp->prime_len);
1644 if (sae->tmp->ec) {
1645 pos = wpabuf_put(buf, 2 * sae->tmp->prime_len);
1646 crypto_ec_point_to_bin(sae->tmp->ec,
1647 sae->tmp->own_commit_element_ecc,
1648 pos, pos + sae->tmp->prime_len);
1649 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(x)",
1650 pos, sae->tmp->prime_len);
1651 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(y)",
1652 pos + sae->tmp->prime_len, sae->tmp->prime_len);
1653 } else {
1654 pos = wpabuf_put(buf, sae->tmp->prime_len);
1655 crypto_bignum_to_bin(sae->tmp->own_commit_element_ffc, pos,
1656 sae->tmp->prime_len, sae->tmp->prime_len);
1657 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element",
1658 pos, sae->tmp->prime_len);
1659 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001660
1661 if (identifier) {
1662 /* Password Identifier element */
1663 wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
1664 wpabuf_put_u8(buf, 1 + os_strlen(identifier));
1665 wpabuf_put_u8(buf, WLAN_EID_EXT_PASSWORD_IDENTIFIER);
1666 wpabuf_put_str(buf, identifier);
1667 wpa_printf(MSG_DEBUG, "SAE: own Password Identifier: %s",
1668 identifier);
1669 }
Hai Shalomc3565922019-10-28 11:58:20 -07001670
1671 if (sae->tmp->h2e && sae->tmp->own_rejected_groups) {
1672 wpa_hexdump_buf(MSG_DEBUG, "SAE: own Rejected Groups",
1673 sae->tmp->own_rejected_groups);
1674 wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
1675 wpabuf_put_u8(buf,
1676 1 + wpabuf_len(sae->tmp->own_rejected_groups));
1677 wpabuf_put_u8(buf, WLAN_EID_EXT_REJECTED_GROUPS);
1678 wpabuf_put_buf(buf, sae->tmp->own_rejected_groups);
1679 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001680}
1681
1682
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001683u16 sae_group_allowed(struct sae_data *sae, int *allowed_groups, u16 group)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001684{
1685 if (allowed_groups) {
1686 int i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001687 for (i = 0; allowed_groups[i] > 0; i++) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001688 if (allowed_groups[i] == group)
1689 break;
1690 }
1691 if (allowed_groups[i] != group) {
1692 wpa_printf(MSG_DEBUG, "SAE: Proposed group %u not "
1693 "enabled in the current configuration",
1694 group);
1695 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1696 }
1697 }
1698
1699 if (sae->state == SAE_COMMITTED && group != sae->group) {
1700 wpa_printf(MSG_DEBUG, "SAE: Do not allow group to be changed");
1701 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1702 }
1703
1704 if (group != sae->group && sae_set_group(sae, group) < 0) {
1705 wpa_printf(MSG_DEBUG, "SAE: Unsupported Finite Cyclic Group %u",
1706 group);
1707 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1708 }
1709
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001710 if (sae->tmp == NULL) {
1711 wpa_printf(MSG_DEBUG, "SAE: Group information not yet initialized");
1712 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1713 }
1714
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001715 if (sae->tmp->dh && !allowed_groups) {
1716 wpa_printf(MSG_DEBUG, "SAE: Do not allow FFC group %u without "
1717 "explicit configuration enabling it", group);
1718 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
1719 }
1720
1721 return WLAN_STATUS_SUCCESS;
1722}
1723
1724
Roshan Pius3a1667e2018-07-03 15:17:14 -07001725static int sae_is_password_id_elem(const u8 *pos, const u8 *end)
1726{
1727 return end - pos >= 3 &&
1728 pos[0] == WLAN_EID_EXTENSION &&
1729 pos[1] >= 1 &&
1730 end - pos - 2 >= pos[1] &&
1731 pos[2] == WLAN_EID_EXT_PASSWORD_IDENTIFIER;
1732}
1733
1734
Hai Shalomc3565922019-10-28 11:58:20 -07001735static int sae_is_rejected_groups_elem(const u8 *pos, const u8 *end)
1736{
1737 return end - pos >= 3 &&
1738 pos[0] == WLAN_EID_EXTENSION &&
1739 pos[1] >= 2 &&
1740 end - pos - 2 >= pos[1] &&
1741 pos[2] == WLAN_EID_EXT_REJECTED_GROUPS;
1742}
1743
1744
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001745static void sae_parse_commit_token(struct sae_data *sae, const u8 **pos,
1746 const u8 *end, const u8 **token,
Hai Shalomc3565922019-10-28 11:58:20 -07001747 size_t *token_len, int h2e)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001748{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001749 size_t scalar_elem_len, tlen;
1750 const u8 *elem;
1751
1752 if (token)
1753 *token = NULL;
1754 if (token_len)
1755 *token_len = 0;
1756
1757 scalar_elem_len = (sae->tmp->ec ? 3 : 2) * sae->tmp->prime_len;
1758 if (scalar_elem_len >= (size_t) (end - *pos))
1759 return; /* No extra data beyond peer scalar and element */
1760
1761 /* It is a bit difficult to parse this now that there is an
1762 * optional variable length Anti-Clogging Token field and
1763 * optional variable length Password Identifier element in the
1764 * frame. We are sending out fixed length Anti-Clogging Token
1765 * fields, so use that length as a requirement for the received
1766 * token and check for the presence of possible Password
1767 * Identifier element based on the element header information.
Hai Shalomc3565922019-10-28 11:58:20 -07001768 * When parsing H2E case, also consider the Rejected Groupd element
1769 * similarly.
Roshan Pius3a1667e2018-07-03 15:17:14 -07001770 */
1771 tlen = end - (*pos + scalar_elem_len);
1772
1773 if (tlen < SHA256_MAC_LEN) {
1774 wpa_printf(MSG_DEBUG,
1775 "SAE: Too short optional data (%u octets) to include our Anti-Clogging Token",
1776 (unsigned int) tlen);
1777 return;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001778 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001779
1780 elem = *pos + scalar_elem_len;
1781 if (sae_is_password_id_elem(elem, end)) {
1782 /* Password Identifier element takes out all available
1783 * extra octets, so there can be no Anti-Clogging token in
1784 * this frame. */
1785 return;
1786 }
Hai Shalomc3565922019-10-28 11:58:20 -07001787 if (h2e && sae_is_rejected_groups_elem(elem, end)) {
1788 /* Rejected Groups takes out all available extra octets, so
1789 * there can be no Anti-Clogging token in this frame. */
1790 return;
1791 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001792
1793 elem += SHA256_MAC_LEN;
1794 if (sae_is_password_id_elem(elem, end)) {
1795 /* Password Identifier element is included in the end, so
1796 * remove its length from the Anti-Clogging token field. */
1797 tlen -= 2 + elem[1];
Hai Shalomc3565922019-10-28 11:58:20 -07001798 elem += 2 + elem[1];
1799 if (h2e && sae_is_rejected_groups_elem(elem, end)) {
1800 /* Also remove Rejected Groups element from the
1801 * Anti-Clogging token field length */
1802 tlen -= 2 + elem[1];
1803 }
1804 } else if (h2e && sae_is_rejected_groups_elem(elem, end)) {
1805 /* Rejected Groups element is included in the end, so
1806 * remove its length from the Anti-Clogging token field. */
1807 tlen -= 2 + elem[1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07001808 }
1809
1810 wpa_hexdump(MSG_DEBUG, "SAE: Anti-Clogging Token", *pos, tlen);
1811 if (token)
1812 *token = *pos;
1813 if (token_len)
1814 *token_len = tlen;
1815 *pos += tlen;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001816}
1817
1818
1819static u16 sae_parse_commit_scalar(struct sae_data *sae, const u8 **pos,
1820 const u8 *end)
1821{
1822 struct crypto_bignum *peer_scalar;
1823
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001824 if (sae->tmp->prime_len > end - *pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001825 wpa_printf(MSG_DEBUG, "SAE: Not enough data for scalar");
1826 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1827 }
1828
1829 peer_scalar = crypto_bignum_init_set(*pos, sae->tmp->prime_len);
1830 if (peer_scalar == NULL)
1831 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1832
1833 /*
1834 * IEEE Std 802.11-2012, 11.3.8.6.1: If there is a protocol instance for
1835 * the peer and it is in Authenticated state, the new Commit Message
1836 * shall be dropped if the peer-scalar is identical to the one used in
1837 * the existing protocol instance.
1838 */
1839 if (sae->state == SAE_ACCEPTED && sae->peer_commit_scalar &&
1840 crypto_bignum_cmp(sae->peer_commit_scalar, peer_scalar) == 0) {
1841 wpa_printf(MSG_DEBUG, "SAE: Do not accept re-use of previous "
1842 "peer-commit-scalar");
1843 crypto_bignum_deinit(peer_scalar, 0);
1844 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1845 }
1846
Dmitry Shmidt41712582015-06-29 11:02:15 -07001847 /* 1 < scalar < r */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001848 if (crypto_bignum_is_zero(peer_scalar) ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07001849 crypto_bignum_is_one(peer_scalar) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001850 crypto_bignum_cmp(peer_scalar, sae->tmp->order) >= 0) {
1851 wpa_printf(MSG_DEBUG, "SAE: Invalid peer scalar");
1852 crypto_bignum_deinit(peer_scalar, 0);
1853 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1854 }
1855
1856
1857 crypto_bignum_deinit(sae->peer_commit_scalar, 0);
1858 sae->peer_commit_scalar = peer_scalar;
1859 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-scalar",
1860 *pos, sae->tmp->prime_len);
1861 *pos += sae->tmp->prime_len;
1862
1863 return WLAN_STATUS_SUCCESS;
1864}
1865
1866
Roshan Pius3a1667e2018-07-03 15:17:14 -07001867static u16 sae_parse_commit_element_ecc(struct sae_data *sae, const u8 **pos,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001868 const u8 *end)
1869{
1870 u8 prime[SAE_MAX_ECC_PRIME_LEN];
1871
Roshan Pius3a1667e2018-07-03 15:17:14 -07001872 if (2 * sae->tmp->prime_len > end - *pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001873 wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
1874 "commit-element");
1875 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1876 }
1877
1878 if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
1879 sae->tmp->prime_len) < 0)
1880 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1881
1882 /* element x and y coordinates < p */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001883 if (os_memcmp(*pos, prime, sae->tmp->prime_len) >= 0 ||
1884 os_memcmp(*pos + sae->tmp->prime_len, prime,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001885 sae->tmp->prime_len) >= 0) {
1886 wpa_printf(MSG_DEBUG, "SAE: Invalid coordinates in peer "
1887 "element");
1888 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1889 }
1890
1891 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(x)",
Roshan Pius3a1667e2018-07-03 15:17:14 -07001892 *pos, sae->tmp->prime_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001893 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(y)",
Roshan Pius3a1667e2018-07-03 15:17:14 -07001894 *pos + sae->tmp->prime_len, sae->tmp->prime_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001895
1896 crypto_ec_point_deinit(sae->tmp->peer_commit_element_ecc, 0);
1897 sae->tmp->peer_commit_element_ecc =
Roshan Pius3a1667e2018-07-03 15:17:14 -07001898 crypto_ec_point_from_bin(sae->tmp->ec, *pos);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001899 if (sae->tmp->peer_commit_element_ecc == NULL)
1900 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1901
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001902 if (!crypto_ec_point_is_on_curve(sae->tmp->ec,
1903 sae->tmp->peer_commit_element_ecc)) {
1904 wpa_printf(MSG_DEBUG, "SAE: Peer element is not on curve");
1905 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1906 }
1907
Roshan Pius3a1667e2018-07-03 15:17:14 -07001908 *pos += 2 * sae->tmp->prime_len;
1909
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001910 return WLAN_STATUS_SUCCESS;
1911}
1912
1913
Roshan Pius3a1667e2018-07-03 15:17:14 -07001914static u16 sae_parse_commit_element_ffc(struct sae_data *sae, const u8 **pos,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001915 const u8 *end)
1916{
Dmitry Shmidt41712582015-06-29 11:02:15 -07001917 struct crypto_bignum *res, *one;
1918 const u8 one_bin[1] = { 0x01 };
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001919
Roshan Pius3a1667e2018-07-03 15:17:14 -07001920 if (sae->tmp->prime_len > end - *pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001921 wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
1922 "commit-element");
1923 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1924 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001925 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element", *pos,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001926 sae->tmp->prime_len);
1927
1928 crypto_bignum_deinit(sae->tmp->peer_commit_element_ffc, 0);
1929 sae->tmp->peer_commit_element_ffc =
Roshan Pius3a1667e2018-07-03 15:17:14 -07001930 crypto_bignum_init_set(*pos, sae->tmp->prime_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001931 if (sae->tmp->peer_commit_element_ffc == NULL)
1932 return WLAN_STATUS_UNSPECIFIED_FAILURE;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001933 /* 1 < element < p - 1 */
1934 res = crypto_bignum_init();
1935 one = crypto_bignum_init_set(one_bin, sizeof(one_bin));
1936 if (!res || !one ||
1937 crypto_bignum_sub(sae->tmp->prime, one, res) ||
1938 crypto_bignum_is_zero(sae->tmp->peer_commit_element_ffc) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001939 crypto_bignum_is_one(sae->tmp->peer_commit_element_ffc) ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07001940 crypto_bignum_cmp(sae->tmp->peer_commit_element_ffc, res) >= 0) {
1941 crypto_bignum_deinit(res, 0);
1942 crypto_bignum_deinit(one, 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001943 wpa_printf(MSG_DEBUG, "SAE: Invalid peer element");
1944 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1945 }
Dmitry Shmidt41712582015-06-29 11:02:15 -07001946 crypto_bignum_deinit(one, 0);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001947
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001948 /* scalar-op(r, ELEMENT) = 1 modulo p */
Dmitry Shmidt41712582015-06-29 11:02:15 -07001949 if (crypto_bignum_exptmod(sae->tmp->peer_commit_element_ffc,
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001950 sae->tmp->order, sae->tmp->prime, res) < 0 ||
1951 !crypto_bignum_is_one(res)) {
1952 wpa_printf(MSG_DEBUG, "SAE: Invalid peer element (scalar-op)");
1953 crypto_bignum_deinit(res, 0);
1954 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1955 }
1956 crypto_bignum_deinit(res, 0);
1957
Roshan Pius3a1667e2018-07-03 15:17:14 -07001958 *pos += sae->tmp->prime_len;
1959
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001960 return WLAN_STATUS_SUCCESS;
1961}
1962
1963
Roshan Pius3a1667e2018-07-03 15:17:14 -07001964static u16 sae_parse_commit_element(struct sae_data *sae, const u8 **pos,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001965 const u8 *end)
1966{
1967 if (sae->tmp->dh)
1968 return sae_parse_commit_element_ffc(sae, pos, end);
1969 return sae_parse_commit_element_ecc(sae, pos, end);
1970}
1971
1972
Roshan Pius3a1667e2018-07-03 15:17:14 -07001973static int sae_parse_password_identifier(struct sae_data *sae,
Hai Shalomc3565922019-10-28 11:58:20 -07001974 const u8 **pos, const u8 *end)
Roshan Pius3a1667e2018-07-03 15:17:14 -07001975{
1976 wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
Hai Shalomc3565922019-10-28 11:58:20 -07001977 *pos, end - *pos);
1978 if (!sae_is_password_id_elem(*pos, end)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001979 if (sae->tmp->pw_id) {
1980 wpa_printf(MSG_DEBUG,
1981 "SAE: No Password Identifier included, but expected one (%s)",
1982 sae->tmp->pw_id);
1983 return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
1984 }
1985 os_free(sae->tmp->pw_id);
1986 sae->tmp->pw_id = NULL;
1987 return WLAN_STATUS_SUCCESS; /* No Password Identifier */
1988 }
1989
1990 if (sae->tmp->pw_id &&
Hai Shalomc3565922019-10-28 11:58:20 -07001991 ((*pos)[1] - 1 != (int) os_strlen(sae->tmp->pw_id) ||
1992 os_memcmp(sae->tmp->pw_id, (*pos) + 3, (*pos)[1] - 1) != 0)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001993 wpa_printf(MSG_DEBUG,
1994 "SAE: The included Password Identifier does not match the expected one (%s)",
1995 sae->tmp->pw_id);
1996 return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
1997 }
1998
1999 os_free(sae->tmp->pw_id);
Hai Shalomc3565922019-10-28 11:58:20 -07002000 sae->tmp->pw_id = os_malloc((*pos)[1]);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002001 if (!sae->tmp->pw_id)
2002 return WLAN_STATUS_UNSPECIFIED_FAILURE;
Hai Shalomc3565922019-10-28 11:58:20 -07002003 os_memcpy(sae->tmp->pw_id, (*pos) + 3, (*pos)[1] - 1);
2004 sae->tmp->pw_id[(*pos)[1] - 1] = '\0';
Roshan Pius3a1667e2018-07-03 15:17:14 -07002005 wpa_hexdump_ascii(MSG_DEBUG, "SAE: Received Password Identifier",
Hai Shalomc3565922019-10-28 11:58:20 -07002006 sae->tmp->pw_id, (*pos)[1] - 1);
2007 *pos = *pos + 2 + (*pos)[1];
2008 return WLAN_STATUS_SUCCESS;
2009}
2010
2011
2012static int sae_parse_rejected_groups(struct sae_data *sae,
2013 const u8 *pos, const u8 *end)
2014{
2015 wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
2016 pos, end - pos);
2017 if (!sae_is_rejected_groups_elem(pos, end))
2018 return WLAN_STATUS_SUCCESS;
2019 wpabuf_free(sae->tmp->peer_rejected_groups);
2020 sae->tmp->peer_rejected_groups = wpabuf_alloc(pos[1] - 1);
2021 if (!sae->tmp->peer_rejected_groups)
2022 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2023 wpabuf_put_data(sae->tmp->peer_rejected_groups, pos + 3, pos[1] - 1);
2024 wpa_hexdump_buf(MSG_DEBUG, "SAE: Received Rejected Groups list",
2025 sae->tmp->peer_rejected_groups);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002026 return WLAN_STATUS_SUCCESS;
2027}
2028
2029
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002030u16 sae_parse_commit(struct sae_data *sae, const u8 *data, size_t len,
Hai Shalomc3565922019-10-28 11:58:20 -07002031 const u8 **token, size_t *token_len, int *allowed_groups,
2032 int h2e)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002033{
2034 const u8 *pos = data, *end = data + len;
2035 u16 res;
2036
2037 /* Check Finite Cyclic Group */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002038 if (end - pos < 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002039 return WLAN_STATUS_UNSPECIFIED_FAILURE;
2040 res = sae_group_allowed(sae, allowed_groups, WPA_GET_LE16(pos));
2041 if (res != WLAN_STATUS_SUCCESS)
2042 return res;
2043 pos += 2;
2044
2045 /* Optional Anti-Clogging Token */
Hai Shalomc3565922019-10-28 11:58:20 -07002046 sae_parse_commit_token(sae, &pos, end, token, token_len, h2e);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002047
2048 /* commit-scalar */
2049 res = sae_parse_commit_scalar(sae, &pos, end);
2050 if (res != WLAN_STATUS_SUCCESS)
2051 return res;
2052
2053 /* commit-element */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002054 res = sae_parse_commit_element(sae, &pos, end);
2055 if (res != WLAN_STATUS_SUCCESS)
2056 return res;
2057
2058 /* Optional Password Identifier element */
Hai Shalomc3565922019-10-28 11:58:20 -07002059 res = sae_parse_password_identifier(sae, &pos, end);
Dmitry Shmidt41712582015-06-29 11:02:15 -07002060 if (res != WLAN_STATUS_SUCCESS)
2061 return res;
2062
Hai Shalomc3565922019-10-28 11:58:20 -07002063 /* Conditional Rejected Groups element */
2064 if (h2e) {
2065 res = sae_parse_rejected_groups(sae, pos, end);
2066 if (res != WLAN_STATUS_SUCCESS)
2067 return res;
2068 }
2069
Dmitry Shmidt41712582015-06-29 11:02:15 -07002070 /*
2071 * Check whether peer-commit-scalar and PEER-COMMIT-ELEMENT are same as
2072 * the values we sent which would be evidence of a reflection attack.
2073 */
2074 if (!sae->tmp->own_commit_scalar ||
2075 crypto_bignum_cmp(sae->tmp->own_commit_scalar,
2076 sae->peer_commit_scalar) != 0 ||
2077 (sae->tmp->dh &&
2078 (!sae->tmp->own_commit_element_ffc ||
2079 crypto_bignum_cmp(sae->tmp->own_commit_element_ffc,
2080 sae->tmp->peer_commit_element_ffc) != 0)) ||
2081 (sae->tmp->ec &&
2082 (!sae->tmp->own_commit_element_ecc ||
2083 crypto_ec_point_cmp(sae->tmp->ec,
2084 sae->tmp->own_commit_element_ecc,
2085 sae->tmp->peer_commit_element_ecc) != 0)))
2086 return WLAN_STATUS_SUCCESS; /* scalars/elements are different */
2087
2088 /*
2089 * This is a reflection attack - return special value to trigger caller
2090 * to silently discard the frame instead of replying with a specific
2091 * status code.
2092 */
2093 return SAE_SILENTLY_DISCARD;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002094}
2095
2096
Hai Shalomc3565922019-10-28 11:58:20 -07002097static int sae_cn_confirm(struct sae_data *sae, const u8 *sc,
2098 const struct crypto_bignum *scalar1,
2099 const u8 *element1, size_t element1_len,
2100 const struct crypto_bignum *scalar2,
2101 const u8 *element2, size_t element2_len,
2102 u8 *confirm)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002103{
2104 const u8 *addr[5];
2105 size_t len[5];
2106 u8 scalar_b1[SAE_MAX_PRIME_LEN], scalar_b2[SAE_MAX_PRIME_LEN];
2107
2108 /* Confirm
2109 * CN(key, X, Y, Z, ...) =
2110 * HMAC-SHA256(key, D2OS(X) || D2OS(Y) || D2OS(Z) | ...)
2111 * confirm = CN(KCK, send-confirm, commit-scalar, COMMIT-ELEMENT,
2112 * peer-commit-scalar, PEER-COMMIT-ELEMENT)
2113 * verifier = CN(KCK, peer-send-confirm, peer-commit-scalar,
2114 * PEER-COMMIT-ELEMENT, commit-scalar, COMMIT-ELEMENT)
2115 */
Hai Shalomc3565922019-10-28 11:58:20 -07002116 if (crypto_bignum_to_bin(scalar1, scalar_b1, sizeof(scalar_b1),
2117 sae->tmp->prime_len) < 0 ||
2118 crypto_bignum_to_bin(scalar2, scalar_b2, sizeof(scalar_b2),
2119 sae->tmp->prime_len) < 0)
2120 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002121 addr[0] = sc;
2122 len[0] = 2;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002123 addr[1] = scalar_b1;
2124 len[1] = sae->tmp->prime_len;
2125 addr[2] = element1;
2126 len[2] = element1_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002127 addr[3] = scalar_b2;
2128 len[3] = sae->tmp->prime_len;
2129 addr[4] = element2;
2130 len[4] = element2_len;
Hai Shalomc3565922019-10-28 11:58:20 -07002131 return hkdf_extract(sae->tmp->kck_len, sae->tmp->kck, sae->tmp->kck_len,
2132 5, addr, len, confirm);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002133}
2134
2135
Hai Shalomc3565922019-10-28 11:58:20 -07002136static int sae_cn_confirm_ecc(struct sae_data *sae, const u8 *sc,
2137 const struct crypto_bignum *scalar1,
2138 const struct crypto_ec_point *element1,
2139 const struct crypto_bignum *scalar2,
2140 const struct crypto_ec_point *element2,
2141 u8 *confirm)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002142{
2143 u8 element_b1[2 * SAE_MAX_ECC_PRIME_LEN];
2144 u8 element_b2[2 * SAE_MAX_ECC_PRIME_LEN];
2145
Hai Shalomc3565922019-10-28 11:58:20 -07002146 if (crypto_ec_point_to_bin(sae->tmp->ec, element1, element_b1,
2147 element_b1 + sae->tmp->prime_len) < 0 ||
2148 crypto_ec_point_to_bin(sae->tmp->ec, element2, element_b2,
2149 element_b2 + sae->tmp->prime_len) < 0 ||
2150 sae_cn_confirm(sae, sc, scalar1, element_b1,
2151 2 * sae->tmp->prime_len,
2152 scalar2, element_b2, 2 * sae->tmp->prime_len,
2153 confirm) < 0)
2154 return -1;
2155 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002156}
2157
2158
Hai Shalomc3565922019-10-28 11:58:20 -07002159static int sae_cn_confirm_ffc(struct sae_data *sae, const u8 *sc,
2160 const struct crypto_bignum *scalar1,
2161 const struct crypto_bignum *element1,
2162 const struct crypto_bignum *scalar2,
2163 const struct crypto_bignum *element2,
2164 u8 *confirm)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002165{
2166 u8 element_b1[SAE_MAX_PRIME_LEN];
2167 u8 element_b2[SAE_MAX_PRIME_LEN];
2168
Hai Shalomc3565922019-10-28 11:58:20 -07002169 if (crypto_bignum_to_bin(element1, element_b1, sizeof(element_b1),
2170 sae->tmp->prime_len) < 0 ||
2171 crypto_bignum_to_bin(element2, element_b2, sizeof(element_b2),
2172 sae->tmp->prime_len) < 0 ||
2173 sae_cn_confirm(sae, sc, scalar1, element_b1, sae->tmp->prime_len,
2174 scalar2, element_b2, sae->tmp->prime_len,
2175 confirm) < 0)
2176 return -1;
2177 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002178}
2179
2180
2181void sae_write_confirm(struct sae_data *sae, struct wpabuf *buf)
2182{
2183 const u8 *sc;
Hai Shalomc3565922019-10-28 11:58:20 -07002184 size_t hash_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002185
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002186 if (sae->tmp == NULL)
2187 return;
2188
Hai Shalomc3565922019-10-28 11:58:20 -07002189 hash_len = sae->tmp->kck_len;
2190
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002191 /* Send-Confirm */
2192 sc = wpabuf_put(buf, 0);
2193 wpabuf_put_le16(buf, sae->send_confirm);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002194 if (sae->send_confirm < 0xffff)
2195 sae->send_confirm++;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002196
2197 if (sae->tmp->ec)
2198 sae_cn_confirm_ecc(sae, sc, sae->tmp->own_commit_scalar,
2199 sae->tmp->own_commit_element_ecc,
2200 sae->peer_commit_scalar,
2201 sae->tmp->peer_commit_element_ecc,
Hai Shalomc3565922019-10-28 11:58:20 -07002202 wpabuf_put(buf, hash_len));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002203 else
2204 sae_cn_confirm_ffc(sae, sc, sae->tmp->own_commit_scalar,
2205 sae->tmp->own_commit_element_ffc,
2206 sae->peer_commit_scalar,
2207 sae->tmp->peer_commit_element_ffc,
Hai Shalomc3565922019-10-28 11:58:20 -07002208 wpabuf_put(buf, hash_len));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002209}
2210
2211
2212int sae_check_confirm(struct sae_data *sae, const u8 *data, size_t len)
2213{
Hai Shalomc3565922019-10-28 11:58:20 -07002214 u8 verifier[SAE_MAX_HASH_LEN];
2215 size_t hash_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002216
Hai Shalomc3565922019-10-28 11:58:20 -07002217 if (!sae->tmp)
2218 return -1;
2219
2220 hash_len = sae->tmp->kck_len;
2221 if (len < 2 + hash_len) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002222 wpa_printf(MSG_DEBUG, "SAE: Too short confirm message");
2223 return -1;
2224 }
2225
2226 wpa_printf(MSG_DEBUG, "SAE: peer-send-confirm %u", WPA_GET_LE16(data));
2227
Hai Shalomc3565922019-10-28 11:58:20 -07002228 if (!sae->peer_commit_scalar || !sae->tmp->own_commit_scalar) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002229 wpa_printf(MSG_DEBUG, "SAE: Temporary data not yet available");
2230 return -1;
2231 }
2232
Hai Shalom021b0b52019-04-10 11:17:58 -07002233 if (sae->tmp->ec) {
2234 if (!sae->tmp->peer_commit_element_ecc ||
Hai Shalomc3565922019-10-28 11:58:20 -07002235 !sae->tmp->own_commit_element_ecc ||
2236 sae_cn_confirm_ecc(sae, data, sae->peer_commit_scalar,
2237 sae->tmp->peer_commit_element_ecc,
2238 sae->tmp->own_commit_scalar,
2239 sae->tmp->own_commit_element_ecc,
2240 verifier) < 0)
Hai Shalom021b0b52019-04-10 11:17:58 -07002241 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -07002242 } else {
2243 if (!sae->tmp->peer_commit_element_ffc ||
Hai Shalomc3565922019-10-28 11:58:20 -07002244 !sae->tmp->own_commit_element_ffc ||
2245 sae_cn_confirm_ffc(sae, data, sae->peer_commit_scalar,
2246 sae->tmp->peer_commit_element_ffc,
2247 sae->tmp->own_commit_scalar,
2248 sae->tmp->own_commit_element_ffc,
2249 verifier) < 0)
Hai Shalom021b0b52019-04-10 11:17:58 -07002250 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -07002251 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002252
Hai Shalomc3565922019-10-28 11:58:20 -07002253 if (os_memcmp_const(verifier, data + 2, hash_len) != 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002254 wpa_printf(MSG_DEBUG, "SAE: Confirm mismatch");
2255 wpa_hexdump(MSG_DEBUG, "SAE: Received confirm",
Hai Shalomc3565922019-10-28 11:58:20 -07002256 data + 2, hash_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002257 wpa_hexdump(MSG_DEBUG, "SAE: Calculated verifier",
Hai Shalomc3565922019-10-28 11:58:20 -07002258 verifier, hash_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002259 return -1;
2260 }
2261
2262 return 0;
2263}
Roshan Pius3a1667e2018-07-03 15:17:14 -07002264
2265
2266const char * sae_state_txt(enum sae_state state)
2267{
2268 switch (state) {
2269 case SAE_NOTHING:
2270 return "Nothing";
2271 case SAE_COMMITTED:
2272 return "Committed";
2273 case SAE_CONFIRMED:
2274 return "Confirmed";
2275 case SAE_ACCEPTED:
2276 return "Accepted";
2277 }
2278 return "?";
2279}