1 /*
2 * Simultaneous authentication of equals
3 * Copyright (c) 2012-2016, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #ifdef CONFIG_WPA3_SAE
10
11 #include "utils/includes.h"
12 #include "utils/common.h"
13 #include "crypto/crypto.h"
14 #include "crypto/sha256.h"
15 #include "crypto/random.h"
16 #include "crypto/dh_groups.h"
17 #include "ieee802_11_defs.h"
18 #include "sae.h"
19 #include "esp_wifi_crypto_types.h"
20
sae_set_group(struct sae_data * sae,int group)21 int sae_set_group(struct sae_data *sae, int group)
22 {
23 struct sae_temporary_data *tmp;
24
25 sae_clear_data(sae);
26 tmp = sae->tmp = os_zalloc(sizeof(*tmp));
27 if (tmp == NULL)
28 return ESP_FAIL;
29
30 /* First, check if this is an ECC group */
31 tmp->ec = crypto_ec_init(group);
32 if (tmp->ec) {
33 wpa_printf(MSG_DEBUG, "SAE: Selecting supported ECC group %d",
34 group);
35 sae->group = group;
36 tmp->prime_len = crypto_ec_prime_len(tmp->ec);
37 tmp->prime = crypto_ec_get_prime(tmp->ec);
38 tmp->order = crypto_ec_get_order(tmp->ec);
39 return ESP_OK;
40 }
41
42 /* Not an ECC group, check FFC */
43 tmp->dh = dh_groups_get(group);
44 if (tmp->dh) {
45 wpa_printf(MSG_DEBUG, "SAE: Selecting supported FFC group %d",
46 group);
47 sae->group = group;
48 tmp->prime_len = tmp->dh->prime_len;
49 if (tmp->prime_len > SAE_MAX_PRIME_LEN) {
50 sae_clear_data(sae);
51 os_free(tmp);
52 return ESP_FAIL;
53 }
54
55 tmp->prime_buf = crypto_bignum_init_set(tmp->dh->prime,
56 tmp->prime_len);
57 if (tmp->prime_buf == NULL) {
58 sae_clear_data(sae);
59 os_free(tmp);
60 return ESP_FAIL;
61 }
62 tmp->prime = tmp->prime_buf;
63
64 tmp->order_buf = crypto_bignum_init_set(tmp->dh->order,
65 tmp->dh->order_len);
66 if (tmp->order_buf == NULL) {
67 sae_clear_data(sae);
68 os_free(tmp);
69 return ESP_FAIL;
70 }
71 tmp->order = tmp->order_buf;
72
73 return ESP_OK;
74 }
75
76 /* Unsupported group */
77 wpa_printf(MSG_DEBUG,
78 "SAE: Group %d not supported by the crypto library", group);
79 os_free(tmp);
80 return ESP_FAIL;
81 }
82
sae_clear_temp_data(struct sae_data * sae)83 void sae_clear_temp_data(struct sae_data *sae)
84 {
85 struct sae_temporary_data *tmp;
86 if (sae == NULL || sae->tmp == NULL)
87 return;
88 tmp = sae->tmp;
89 crypto_ec_deinit(tmp->ec);
90 crypto_bignum_deinit(tmp->prime_buf, 0);
91 crypto_bignum_deinit(tmp->order_buf, 0);
92 crypto_bignum_deinit(tmp->sae_rand, 1);
93 crypto_bignum_deinit(tmp->pwe_ffc, 1);
94 crypto_bignum_deinit(tmp->own_commit_scalar, 0);
95 crypto_bignum_deinit(tmp->own_commit_element_ffc, 0);
96 crypto_bignum_deinit(tmp->peer_commit_element_ffc, 0);
97 crypto_ec_point_deinit(tmp->pwe_ecc, 1);
98 crypto_ec_point_deinit(tmp->own_commit_element_ecc, 0);
99 crypto_ec_point_deinit(tmp->peer_commit_element_ecc, 0);
100 os_free(tmp->pw_id);
101 bin_clear_free(tmp, sizeof(*tmp));
102 sae->tmp = NULL;
103 }
104
sae_clear_data(struct sae_data * sae)105 void sae_clear_data(struct sae_data *sae)
106 {
107 if (sae == NULL)
108 return;
109 sae_clear_temp_data(sae);
110 crypto_bignum_deinit(sae->peer_commit_scalar, 0);
111 os_memset(sae, 0, sizeof(*sae));
112 }
113
buf_shift_right(u8 * buf,size_t len,size_t bits)114 static void buf_shift_right(u8 *buf, size_t len, size_t bits)
115 {
116 size_t i;
117 for (i = len - 1; i > 0; i--)
118 buf[i] = (buf[i - 1] << (8 - bits)) | (buf[i] >> bits);
119 buf[0] >>= bits;
120 }
121
sae_get_rand(struct sae_data * sae)122 static struct crypto_bignum * sae_get_rand(struct sae_data *sae)
123 {
124 u8 val[SAE_MAX_PRIME_LEN];
125 int iter = 0;
126 struct crypto_bignum *bn = NULL;
127 int order_len_bits = crypto_bignum_bits(sae->tmp->order);
128 size_t order_len = (order_len_bits + 7) / 8;
129
130 if (order_len > sizeof(val))
131 return NULL;
132
133 for (;;) {
134 if (iter++ > 100 || random_get_bytes(val, order_len) < 0)
135 return NULL;
136 if (order_len_bits % 8)
137 buf_shift_right(val, order_len, 8 - order_len_bits % 8);
138 bn = crypto_bignum_init_set(val, order_len);
139 if (bn == NULL)
140 return NULL;
141 if (crypto_bignum_is_zero(bn) ||
142 crypto_bignum_is_one(bn) ||
143 crypto_bignum_cmp(bn, sae->tmp->order) >= 0) {
144 crypto_bignum_deinit(bn, 0);
145 continue;
146 }
147 break;
148 }
149
150 os_memset(val, 0, order_len);
151 return bn;
152 }
153
sae_get_rand_and_mask(struct sae_data * sae)154 static struct crypto_bignum * sae_get_rand_and_mask(struct sae_data *sae)
155 {
156 crypto_bignum_deinit(sae->tmp->sae_rand, 1);
157 sae->tmp->sae_rand = sae_get_rand(sae);
158 if (sae->tmp->sae_rand == NULL)
159 return NULL;
160 return sae_get_rand(sae);
161 }
162
sae_pwd_seed_key(const u8 * addr1,const u8 * addr2,u8 * key)163 static void sae_pwd_seed_key(const u8 *addr1, const u8 *addr2, u8 *key)
164 {
165 wpa_printf(MSG_DEBUG, "SAE: PWE derivation - addr1=" MACSTR
166 " addr2=" MACSTR, MAC2STR(addr1), MAC2STR(addr2));
167 if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) {
168 os_memcpy(key, addr1, ETH_ALEN);
169 os_memcpy(key + ETH_ALEN, addr2, ETH_ALEN);
170 } else {
171 os_memcpy(key, addr2, ETH_ALEN);
172 os_memcpy(key + ETH_ALEN, addr1, ETH_ALEN);
173 }
174 }
175
176 static struct crypto_bignum *
get_rand_1_to_p_1(const u8 * prime,size_t prime_len,size_t prime_bits,int * r_odd)177 get_rand_1_to_p_1(const u8 *prime, size_t prime_len, size_t prime_bits,
178 int *r_odd)
179 {
180 for (;;) {
181 struct crypto_bignum *r;
182 u8 tmp[SAE_MAX_ECC_PRIME_LEN];
183
184 if (random_get_bytes(tmp, prime_len) < 0)
185 break;
186 if (prime_bits % 8)
187 buf_shift_right(tmp, prime_len, 8 - prime_bits % 8);
188 if (os_memcmp(tmp, prime, prime_len) >= 0)
189 continue;
190 r = crypto_bignum_init_set(tmp, prime_len);
191 if (!r)
192 break;
193 if (crypto_bignum_is_zero(r)) {
194 crypto_bignum_deinit(r, 0);
195 continue;
196 }
197
198 *r_odd = tmp[prime_len - 1] & 0x01;
199 return r;
200 }
201
202 return NULL;
203 }
204
is_quadratic_residue_blind(struct sae_data * sae,const u8 * prime,size_t bits,const struct crypto_bignum * qr,const struct crypto_bignum * qnr,const struct crypto_bignum * y_sqr)205 static int is_quadratic_residue_blind(struct sae_data *sae,
206 const u8 *prime, size_t bits,
207 const struct crypto_bignum *qr,
208 const struct crypto_bignum *qnr,
209 const struct crypto_bignum *y_sqr)
210 {
211 struct crypto_bignum *r, *num;
212 int r_odd, check, res = -1;
213
214 /*
215 * Use the blinding technique to mask y_sqr while determining
216 * whether it is a quadratic residue modulo p to avoid leaking
217 * timing information while determining the Legendre symbol.
218 *
219 * v = y_sqr
220 * r = a random number between 1 and p-1, inclusive
221 * num = (v * r * r) modulo p
222 */
223 r = get_rand_1_to_p_1(prime, sae->tmp->prime_len, bits, &r_odd);
224 if (!r)
225 return ESP_FAIL;
226
227 num = crypto_bignum_init();
228 if (!num ||
229 crypto_bignum_mulmod(y_sqr, r, sae->tmp->prime, num) < 0 ||
230 crypto_bignum_mulmod(num, r, sae->tmp->prime, num) < 0)
231 goto fail;
232
233 if (r_odd) {
234 /*
235 * num = (num * qr) module p
236 * LGR(num, p) = 1 ==> quadratic residue
237 */
238 if (crypto_bignum_mulmod(num, qr, sae->tmp->prime, num) < 0)
239 goto fail;
240 check = 1;
241 } else {
242 /*
243 * num = (num * qnr) module p
244 * LGR(num, p) = -1 ==> quadratic residue
245 */
246 if (crypto_bignum_mulmod(num, qnr, sae->tmp->prime, num) < 0)
247 goto fail;
248 check = -1;
249 }
250
251 res = crypto_bignum_legendre(num, sae->tmp->prime);
252 if (res == -2) {
253 res = -1;
254 goto fail;
255 }
256 res = res == check;
257 fail:
258 crypto_bignum_deinit(num, 1);
259 crypto_bignum_deinit(r, 1);
260 return res;
261 }
262
sae_test_pwd_seed_ecc(struct sae_data * sae,const u8 * pwd_seed,const u8 * prime,const struct crypto_bignum * qr,const struct crypto_bignum * qnr,struct crypto_bignum ** ret_x_cand)263 static int sae_test_pwd_seed_ecc(struct sae_data *sae, const u8 *pwd_seed,
264 const u8 *prime,
265 const struct crypto_bignum *qr,
266 const struct crypto_bignum *qnr,
267 struct crypto_bignum **ret_x_cand)
268 {
269 u8 pwd_value[SAE_MAX_ECC_PRIME_LEN];
270 struct crypto_bignum *y_sqr, *x_cand;
271 int res;
272 size_t bits;
273
274 *ret_x_cand = NULL;
275
276 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
277
278 /* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
279 bits = crypto_ec_prime_len_bits(sae->tmp->ec);
280 if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking",
281 prime, sae->tmp->prime_len, pwd_value, bits) < 0)
282 return ESP_FAIL;
283 if (bits % 8)
284 buf_shift_right(pwd_value, sizeof(pwd_value), 8 - bits % 8);
285 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value",
286 pwd_value, sae->tmp->prime_len);
287
288 if (os_memcmp(pwd_value, prime, sae->tmp->prime_len) >= 0)
289 return ESP_OK;
290
291 x_cand = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
292 if (!x_cand)
293 return ESP_FAIL;
294 y_sqr = crypto_ec_point_compute_y_sqr(sae->tmp->ec, x_cand);
295 if (!y_sqr) {
296 crypto_bignum_deinit(x_cand, 1);
297 return ESP_FAIL;
298 }
299
300 res = is_quadratic_residue_blind(sae, prime, bits, qr, qnr, y_sqr);
301 crypto_bignum_deinit(y_sqr, 1);
302 if (res <= 0) {
303 crypto_bignum_deinit(x_cand, 1);
304 return res;
305 }
306
307 *ret_x_cand = x_cand;
308 return 1;
309 }
310
sae_test_pwd_seed_ffc(struct sae_data * sae,const u8 * pwd_seed,struct crypto_bignum * pwe)311 static int sae_test_pwd_seed_ffc(struct sae_data *sae, const u8 *pwd_seed,
312 struct crypto_bignum *pwe)
313 {
314 u8 pwd_value[SAE_MAX_PRIME_LEN];
315 size_t bits = sae->tmp->prime_len * 8;
316 u8 exp[1];
317 struct crypto_bignum *a, *b;
318 int res;
319
320 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
321
322 /* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
323 if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking",
324 sae->tmp->dh->prime, sae->tmp->prime_len, pwd_value,
325 bits) < 0)
326 return ESP_FAIL;
327 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value", pwd_value,
328 sae->tmp->prime_len);
329
330 if (os_memcmp(pwd_value, sae->tmp->dh->prime, sae->tmp->prime_len) >= 0)
331 {
332 wpa_printf(MSG_DEBUG, "SAE: pwd-value >= p");
333 return ESP_OK;
334 }
335
336 /* PWE = pwd-value^((p-1)/r) modulo p */
337
338 a = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
339
340 if (sae->tmp->dh->safe_prime) {
341 /*
342 * r = (p-1)/2 for the group used here, so this becomes:
343 * PWE = pwd-value^2 modulo p
344 */
345 exp[0] = 2;
346 b = crypto_bignum_init_set(exp, sizeof(exp));
347 } else {
348 /* Calculate exponent: (p-1)/r */
349 exp[0] = 1;
350 b = crypto_bignum_init_set(exp, sizeof(exp));
351 if (b == NULL ||
352 crypto_bignum_sub(sae->tmp->prime, b, b) < 0 ||
353 crypto_bignum_div(b, sae->tmp->order, b) < 0) {
354 crypto_bignum_deinit(b, 0);
355 b = NULL;
356 }
357 }
358
359 if (a == NULL || b == NULL)
360 res = -1;
361 else
362 res = crypto_bignum_exptmod(a, b, sae->tmp->prime, pwe);
363
364 crypto_bignum_deinit(a, 0);
365 crypto_bignum_deinit(b, 0);
366
367 if (res < 0) {
368 wpa_printf(MSG_DEBUG, "SAE: Failed to calculate PWE");
369 return ESP_FAIL;
370 }
371
372 /* if (PWE > 1) --> found */
373 if (crypto_bignum_is_zero(pwe) || crypto_bignum_is_one(pwe)) {
374 wpa_printf(MSG_DEBUG, "SAE: PWE <= 1");
375 return ESP_OK;
376 }
377
378 wpa_printf(MSG_DEBUG, "SAE: PWE found");
379 return 1;
380 }
381
get_random_qr_qnr(const u8 * prime,size_t prime_len,const struct crypto_bignum * prime_bn,size_t prime_bits,struct crypto_bignum ** qr,struct crypto_bignum ** qnr)382 static int get_random_qr_qnr(const u8 *prime, size_t prime_len,
383 const struct crypto_bignum *prime_bn,
384 size_t prime_bits, struct crypto_bignum **qr,
385 struct crypto_bignum **qnr)
386 {
387 *qr = NULL;
388 *qnr = NULL;
389
390 while (!(*qr) || !(*qnr)) {
391 u8 tmp[SAE_MAX_ECC_PRIME_LEN];
392 struct crypto_bignum *q;
393 int res;
394
395 if (random_get_bytes(tmp, prime_len) < 0)
396 break;
397 if (prime_bits % 8)
398 buf_shift_right(tmp, prime_len, 8 - prime_bits % 8);
399 if (os_memcmp(tmp, prime, prime_len) >= 0)
400 continue;
401 q = crypto_bignum_init_set(tmp, prime_len);
402 if (!q)
403 break;
404 res = crypto_bignum_legendre(q, prime_bn);
405
406 if (res == 1 && !(*qr))
407 *qr = q;
408 else if (res == -1 && !(*qnr))
409 *qnr = q;
410 else
411 crypto_bignum_deinit(q, 0);
412 }
413
414 return (*qr && *qnr) ? 0 : -1;
415 }
416
sae_derive_pwe_ecc(struct sae_data * sae,const u8 * addr1,const u8 * addr2,const u8 * password,size_t password_len,const char * identifier)417 static int sae_derive_pwe_ecc(struct sae_data *sae, const u8 *addr1,
418 const u8 *addr2, const u8 *password,
419 size_t password_len, const char *identifier)
420 {
421 u8 counter, k = 40;
422 u8 addrs[2 * ETH_ALEN];
423 const u8 *addr[3];
424 size_t len[3];
425 size_t num_elem;
426 u8 dummy_password[32];
427 size_t dummy_password_len;
428 int pwd_seed_odd = 0;
429 u8 prime[SAE_MAX_ECC_PRIME_LEN];
430 size_t prime_len;
431 struct crypto_bignum *x = NULL, *qr, *qnr;
432 size_t bits;
433 int res;
434
435 dummy_password_len = password_len;
436 if (dummy_password_len > sizeof(dummy_password))
437 dummy_password_len = sizeof(dummy_password);
438 if (random_get_bytes(dummy_password, dummy_password_len) < 0)
439 return ESP_FAIL;
440
441 prime_len = sae->tmp->prime_len;
442 if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
443 prime_len) < 0)
444 return ESP_FAIL;
445 bits = crypto_ec_prime_len_bits(sae->tmp->ec);
446
447 /*
448 * Create a random quadratic residue (qr) and quadratic non-residue
449 * (qnr) modulo p for blinding purposes during the loop.
450 */
451 if (get_random_qr_qnr(prime, prime_len, sae->tmp->prime, bits,
452 &qr, &qnr) < 0)
453 return ESP_FAIL;
454
455 wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
456 password, password_len);
457 if (identifier)
458 wpa_printf(MSG_DEBUG, "SAE: password identifier: %s",
459 identifier);
460
461 /*
462 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
463 * base = password [|| identifier]
464 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
465 * base || counter)
466 */
467 sae_pwd_seed_key(addr1, addr2, addrs);
468
469 addr[0] = password;
470 len[0] = password_len;
471 num_elem = 1;
472 if (identifier) {
473 addr[num_elem] = (const u8 *) identifier;
474 len[num_elem] = os_strlen(identifier);
475 num_elem++;
476 }
477 addr[num_elem] = &counter;
478 len[num_elem] = sizeof(counter);
479 num_elem++;
480
481 /*
482 * Continue for at least k iterations to protect against side-channel
483 * attacks that attempt to determine the number of iterations required
484 * in the loop.
485 */
486 for (counter = 1; counter <= k || !x; counter++) {
487 u8 pwd_seed[SHA256_MAC_LEN];
488 struct crypto_bignum *x_cand;
489
490 if (counter > 200) {
491 /* This should not happen in practice */
492 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
493 break;
494 }
495
496 wpa_printf(MSG_DEBUG, "SAE: counter = %u", counter);
497 if (hmac_sha256_vector(addrs, sizeof(addrs), num_elem,
498 addr, len, pwd_seed) < 0)
499 break;
500
501 res = sae_test_pwd_seed_ecc(sae, pwd_seed,
502 prime, qr, qnr, &x_cand);
503 if (res < 0)
504 goto fail;
505 if (res > 0 && !x) {
506 wpa_printf(MSG_DEBUG,
507 "SAE: Selected pwd-seed with counter %u",
508 counter);
509 x = x_cand;
510 pwd_seed_odd = pwd_seed[SHA256_MAC_LEN - 1] & 0x01;
511 os_memset(pwd_seed, 0, sizeof(pwd_seed));
512
513 /*
514 * Use a dummy password for the following rounds, if
515 * any.
516 */
517 addr[0] = dummy_password;
518 len[0] = dummy_password_len;
519 } else if (res > 0) {
520 crypto_bignum_deinit(x_cand, 1);
521 }
522 }
523
524 if (!x) {
525 wpa_printf(MSG_DEBUG, "SAE: Could not generate PWE");
526 res = -1;
527 goto fail;
528 }
529
530 if (!sae->tmp->pwe_ecc)
531 sae->tmp->pwe_ecc = crypto_ec_point_init(sae->tmp->ec);
532 if (!sae->tmp->pwe_ecc)
533 res = -1;
534 else
535 res = crypto_ec_point_solve_y_coord(sae->tmp->ec,
536 sae->tmp->pwe_ecc, x,
537 pwd_seed_odd);
538 crypto_bignum_deinit(x, 1);
539 if (res < 0) {
540 /*
541 * This should not happen since we already checked that there
542 * is a result.
543 */
544 wpa_printf(MSG_DEBUG, "SAE: Could not solve y");
545 }
546
547 fail:
548 crypto_bignum_deinit(qr, 0);
549 crypto_bignum_deinit(qnr, 0);
550
551 return res;
552 }
553
sae_derive_pwe_ffc(struct sae_data * sae,const u8 * addr1,const u8 * addr2,const u8 * password,size_t password_len,const char * identifier)554 static int sae_derive_pwe_ffc(struct sae_data *sae, const u8 *addr1,
555 const u8 *addr2, const u8 *password,
556 size_t password_len, const char *identifier)
557 {
558 u8 counter;
559 u8 addrs[2 * ETH_ALEN];
560 const u8 *addr[3];
561 size_t len[3];
562 size_t num_elem;
563 int found = 0;
564
565 if (sae->tmp->pwe_ffc == NULL) {
566 sae->tmp->pwe_ffc = crypto_bignum_init();
567 if (sae->tmp->pwe_ffc == NULL)
568 return ESP_FAIL;
569 }
570
571 wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
572 password, password_len);
573
574 /*
575 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
576 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
577 * password [|| identifier] || counter)
578 */
579 sae_pwd_seed_key(addr1, addr2, addrs);
580
581 addr[0] = password;
582 len[0] = password_len;
583 num_elem = 1;
584 if (identifier) {
585 addr[num_elem] = (const u8 *) identifier;
586 len[num_elem] = os_strlen(identifier);
587 num_elem++;
588 }
589 addr[num_elem] = &counter;
590 len[num_elem] = sizeof(counter);
591 num_elem++;
592
593 for (counter = 1; !found; counter++) {
594 u8 pwd_seed[SHA256_MAC_LEN];
595 int res;
596
597 if (counter > 200) {
598 /* This should not happen in practice */
599 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
600 break;
601 }
602
603 wpa_printf(MSG_DEBUG, "SAE: counter = %u", counter);
604 if (hmac_sha256_vector(addrs, sizeof(addrs), num_elem,
605 addr, len, pwd_seed) < 0)
606 break;
607 res = sae_test_pwd_seed_ffc(sae, pwd_seed, sae->tmp->pwe_ffc);
608 if (res < 0)
609 break;
610 if (res > 0) {
611 wpa_printf(MSG_DEBUG, "SAE: Use this PWE");
612 found = 1;
613 }
614 }
615
616 return found ? 0 : -1;
617 }
618
sae_derive_commit_element_ecc(struct sae_data * sae,struct crypto_bignum * mask)619 static int sae_derive_commit_element_ecc(struct sae_data *sae,
620 struct crypto_bignum *mask)
621 {
622 /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
623 if (!sae->tmp->own_commit_element_ecc) {
624 sae->tmp->own_commit_element_ecc =
625 crypto_ec_point_init(sae->tmp->ec);
626 if (!sae->tmp->own_commit_element_ecc)
627 return ESP_FAIL;
628 }
629
630 if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc, mask,
631 sae->tmp->own_commit_element_ecc) < 0 ||
632 crypto_ec_point_invert(sae->tmp->ec,
633 sae->tmp->own_commit_element_ecc) < 0) {
634 wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
635 return ESP_FAIL;
636 }
637
638 return ESP_OK;
639 }
640
sae_derive_commit_element_ffc(struct sae_data * sae,struct crypto_bignum * mask)641 static int sae_derive_commit_element_ffc(struct sae_data *sae,
642 struct crypto_bignum *mask)
643 {
644 /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
645 if (!sae->tmp->own_commit_element_ffc) {
646 sae->tmp->own_commit_element_ffc = crypto_bignum_init();
647 if (!sae->tmp->own_commit_element_ffc)
648 return ESP_FAIL;
649 }
650
651 if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, mask, sae->tmp->prime,
652 sae->tmp->own_commit_element_ffc) < 0 ||
653 crypto_bignum_inverse(sae->tmp->own_commit_element_ffc,
654 sae->tmp->prime,
655 sae->tmp->own_commit_element_ffc) < 0) {
656 wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
657 return ESP_FAIL;
658 }
659
660 return ESP_OK;
661 }
662
sae_derive_commit(struct sae_data * sae)663 static int sae_derive_commit(struct sae_data *sae)
664 {
665 struct crypto_bignum *mask = NULL;
666 int ret = -1;
667 unsigned int counter = 0;
668
669 do {
670 counter++;
671 if (counter > 100) {
672 /*
673 * This cannot really happen in practice if the random
674 * number generator is working. Anyway, to avoid even a
675 * theoretical infinite loop, break out after 100
676 * attemps.
677 */
678 crypto_bignum_deinit(mask, 1);
679 return ESP_FAIL;
680 }
681
682 if (mask) {
683 crypto_bignum_deinit(mask, 1);
684 }
685 mask = sae_get_rand_and_mask(sae);
686 if (mask == NULL) {
687 wpa_printf(MSG_DEBUG, "SAE: Could not get rand/mask");
688 return ESP_FAIL;
689 }
690
691 /* commit-scalar = (rand + mask) modulo r */
692 if (!sae->tmp->own_commit_scalar) {
693 sae->tmp->own_commit_scalar = crypto_bignum_init();
694 if (!sae->tmp->own_commit_scalar)
695 goto fail;
696 }
697 crypto_bignum_add(sae->tmp->sae_rand, mask,
698 sae->tmp->own_commit_scalar);
699 crypto_bignum_mod(sae->tmp->own_commit_scalar, sae->tmp->order,
700 sae->tmp->own_commit_scalar);
701 } while (crypto_bignum_is_zero(sae->tmp->own_commit_scalar) ||
702 crypto_bignum_is_one(sae->tmp->own_commit_scalar));
703
704 if ((sae->tmp->ec && sae_derive_commit_element_ecc(sae, mask) < 0) ||
705 (sae->tmp->dh && sae_derive_commit_element_ffc(sae, mask) < 0))
706 goto fail;
707
708 ret = 0;
709 fail:
710 crypto_bignum_deinit(mask, 1);
711 return ret;
712 }
713
sae_prepare_commit(const u8 * addr1,const u8 * addr2,const u8 * password,size_t password_len,const char * identifier,struct sae_data * sae)714 int sae_prepare_commit(const u8 *addr1, const u8 *addr2,
715 const u8 *password, size_t password_len,
716 const char *identifier, struct sae_data *sae)
717 {
718 if (sae->tmp == NULL ||
719 (sae->tmp->ec && sae_derive_pwe_ecc(sae, addr1, addr2, password,
720 password_len,
721 identifier) < 0) ||
722 (sae->tmp->dh && sae_derive_pwe_ffc(sae, addr1, addr2, password,
723 password_len,
724 identifier) < 0) ||
725 sae_derive_commit(sae) < 0)
726 return ESP_FAIL;
727 return ESP_OK;
728 }
729
sae_derive_k_ecc(struct sae_data * sae,u8 * k)730 static int sae_derive_k_ecc(struct sae_data *sae, u8 *k)
731 {
732 struct crypto_ec_point *K;
733 int ret = -1;
734
735 K = crypto_ec_point_init(sae->tmp->ec);
736 if (K == NULL)
737 goto fail;
738
739 /*
740 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
741 * PEER-COMMIT-ELEMENT)))
742 * If K is identity element (point-at-infinity), reject
743 * k = F(K) (= x coordinate)
744 */
745
746 if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc,
747 sae->peer_commit_scalar, K) < 0 ||
748 crypto_ec_point_add(sae->tmp->ec, K,
749 sae->tmp->peer_commit_element_ecc, K) < 0 ||
750 crypto_ec_point_mul(sae->tmp->ec, K, sae->tmp->sae_rand, K) < 0 ||
751 crypto_ec_point_is_at_infinity(sae->tmp->ec, K) ||
752 crypto_ec_point_to_bin(sae->tmp->ec, K, k, NULL) < 0) {
753 wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
754 goto fail;
755 }
756
757 wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
758
759 ret = 0;
760 fail:
761 crypto_ec_point_deinit(K, 1);
762 return ret;
763 }
764
sae_derive_k_ffc(struct sae_data * sae,u8 * k)765 static int sae_derive_k_ffc(struct sae_data *sae, u8 *k)
766 {
767 struct crypto_bignum *K;
768 int ret = -1;
769
770 K = crypto_bignum_init();
771 if (K == NULL)
772 goto fail;
773
774 /*
775 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
776 * PEER-COMMIT-ELEMENT)))
777 * If K is identity element (one), reject.
778 * k = F(K) (= x coordinate)
779 */
780
781 if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, sae->peer_commit_scalar,
782 sae->tmp->prime, K) < 0 ||
783 crypto_bignum_mulmod(K, sae->tmp->peer_commit_element_ffc,
784 sae->tmp->prime, K) < 0 ||
785 crypto_bignum_exptmod(K, sae->tmp->sae_rand, sae->tmp->prime, K) < 0
786 ||
787 crypto_bignum_is_one(K) ||
788 crypto_bignum_to_bin(K, k, SAE_MAX_PRIME_LEN, sae->tmp->prime_len) <
789 0) {
790 wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
791 goto fail;
792 }
793
794 wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
795
796 ret = 0;
797 fail:
798 crypto_bignum_deinit(K, 1);
799 return ret;
800 }
801
sae_derive_keys(struct sae_data * sae,const u8 * k)802 static int sae_derive_keys(struct sae_data *sae, const u8 *k)
803 {
804 u8 null_key[SAE_KEYSEED_KEY_LEN], val[SAE_MAX_PRIME_LEN];
805 u8 keyseed[SHA256_MAC_LEN];
806 u8 keys[SAE_KCK_LEN + SAE_PMK_LEN];
807 struct crypto_bignum *tmp;
808 int ret = -1;
809
810 tmp = crypto_bignum_init();
811 if (tmp == NULL)
812 goto fail;
813
814 /* keyseed = H(<0>32, k)
815 * KCK || PMK = KDF-512(keyseed, "SAE KCK and PMK",
816 * (commit-scalar + peer-commit-scalar) modulo r)
817 * PMKID = L((commit-scalar + peer-commit-scalar) modulo r, 0, 128)
818 */
819
820 os_memset(null_key, 0, sizeof(null_key));
821 hmac_sha256(null_key, sizeof(null_key), k, sae->tmp->prime_len,
822 keyseed);
823 wpa_hexdump_key(MSG_DEBUG, "SAE: keyseed", keyseed, sizeof(keyseed));
824
825 crypto_bignum_add(sae->tmp->own_commit_scalar, sae->peer_commit_scalar,
826 tmp);
827 crypto_bignum_mod(tmp, sae->tmp->order, tmp);
828 crypto_bignum_to_bin(tmp, val, sizeof(val), sae->tmp->prime_len);
829 wpa_hexdump(MSG_DEBUG, "SAE: PMKID", val, SAE_PMKID_LEN);
830 if (sha256_prf(keyseed, sizeof(keyseed), "SAE KCK and PMK",
831 val, sae->tmp->prime_len, keys, sizeof(keys)) < 0)
832 goto fail;
833 os_memset(keyseed, 0, sizeof(keyseed));
834 os_memcpy(sae->tmp->kck, keys, SAE_KCK_LEN);
835 os_memcpy(sae->pmk, keys + SAE_KCK_LEN, SAE_PMK_LEN);
836 os_memcpy(sae->pmkid, val, SAE_PMKID_LEN);
837 os_memset(keys, 0, sizeof(keys));
838 wpa_hexdump_key(MSG_DEBUG, "SAE: KCK", sae->tmp->kck, SAE_KCK_LEN);
839 wpa_hexdump_key(MSG_DEBUG, "SAE: PMK", sae->pmk, SAE_PMK_LEN);
840
841 ret = 0;
842 fail:
843 crypto_bignum_deinit(tmp, 0);
844 return ret;
845 }
846
sae_process_commit(struct sae_data * sae)847 int sae_process_commit(struct sae_data *sae)
848 {
849 u8 k[SAE_MAX_PRIME_LEN];
850 if (sae->tmp == NULL ||
851 (sae->tmp->ec && sae_derive_k_ecc(sae, k) < 0) ||
852 (sae->tmp->dh && sae_derive_k_ffc(sae, k) < 0) ||
853 sae_derive_keys(sae, k) < 0)
854 return ESP_FAIL;
855 return ESP_OK;
856 }
857
sae_write_commit(struct sae_data * sae,struct wpabuf * buf,const struct wpabuf * token,const char * identifier)858 int sae_write_commit(struct sae_data *sae, struct wpabuf *buf,
859 const struct wpabuf *token, const char *identifier)
860 {
861 u8 *pos;
862
863 if (sae->tmp == NULL)
864 return ESP_FAIL;
865
866 wpabuf_put_le16(buf, sae->group); /* Finite Cyclic Group */
867 if (token) {
868 wpabuf_put_buf(buf, token);
869 wpa_hexdump(MSG_DEBUG, "SAE: Anti-clogging token",
870 wpabuf_head(token), wpabuf_len(token));
871 }
872 pos = wpabuf_put(buf, sae->tmp->prime_len);
873 if (crypto_bignum_to_bin(sae->tmp->own_commit_scalar, pos,
874 sae->tmp->prime_len, sae->tmp->prime_len) < 0) {
875 wpa_printf(MSG_ERROR, "SAE: failed bignum operation on own commit scalar");
876 return ESP_FAIL;
877 }
878 wpa_hexdump(MSG_DEBUG, "SAE: own commit-scalar",
879 pos, sae->tmp->prime_len);
880 if (sae->tmp->ec) {
881 pos = wpabuf_put(buf, 2 * sae->tmp->prime_len);
882 if (crypto_ec_point_to_bin(sae->tmp->ec,
883 sae->tmp->own_commit_element_ecc,
884 pos, pos + sae->tmp->prime_len) < 0) {
885 wpa_printf(MSG_ERROR, "SAE: failed bignum op while deriving ec point");
886 return ESP_FAIL;
887 }
888 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(x)",
889 pos, sae->tmp->prime_len);
890 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(y)",
891 pos + sae->tmp->prime_len, sae->tmp->prime_len);
892 } else {
893 pos = wpabuf_put(buf, sae->tmp->prime_len);
894 if (crypto_bignum_to_bin(sae->tmp->own_commit_element_ffc, pos,
895 sae->tmp->prime_len, sae->tmp->prime_len) < 0) {
896 wpa_printf(MSG_ERROR, "SAE: failed bignum operation on commit elem ffc");
897 return ESP_FAIL;
898 }
899 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element",
900 pos, sae->tmp->prime_len);
901 }
902
903 if (identifier) {
904 /* Password Identifier element */
905 wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
906 wpabuf_put_u8(buf, 1 + os_strlen(identifier));
907 wpabuf_put_u8(buf, WLAN_EID_EXT_PASSWORD_IDENTIFIER);
908 wpabuf_put_str(buf, identifier);
909 wpa_printf(MSG_DEBUG, "SAE: own Password Identifier: %s",
910 identifier);
911 }
912 return ESP_OK;
913 }
914
sae_group_allowed(struct sae_data * sae,int * allowed_groups,u16 group)915 u16 sae_group_allowed(struct sae_data *sae, int *allowed_groups, u16 group)
916 {
917 if (allowed_groups) {
918 int i;
919 for (i = 0; allowed_groups[i] > 0; i++) {
920 if (allowed_groups[i] == group)
921 break;
922 }
923 if (allowed_groups[i] != group) {
924 wpa_printf(MSG_DEBUG, "SAE: Proposed group %u not "
925 "enabled in the current configuration",
926 group);
927 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
928 }
929 }
930
931 if (sae->state == SAE_COMMITTED && group != sae->group) {
932 wpa_printf(MSG_DEBUG, "SAE: Do not allow group to be changed");
933 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
934 }
935
936 if (group != sae->group && sae_set_group(sae, group) < 0) {
937 wpa_printf(MSG_DEBUG, "SAE: Unsupported Finite Cyclic Group %u",
938 group);
939 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
940 }
941
942 if (sae->tmp == NULL) {
943 wpa_printf(MSG_DEBUG, "SAE: Group information not yet initialized");
944 return WLAN_STATUS_UNSPECIFIED_FAILURE;
945 }
946
947 if (sae->tmp->dh && !allowed_groups) {
948 wpa_printf(MSG_DEBUG, "SAE: Do not allow FFC group %u without "
949 "explicit configuration enabling it", group);
950 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
951 }
952
953 return WLAN_STATUS_SUCCESS;
954 }
955
sae_is_password_id_elem(const u8 * pos,const u8 * end)956 static int sae_is_password_id_elem(const u8 *pos, const u8 *end)
957 {
958 int ret = end - pos >= 3 &&
959 pos[0] == WLAN_EID_EXTENSION &&
960 pos[1] >= 1 &&
961 end - pos - 2 >= pos[1] &&
962 pos[2] == WLAN_EID_EXT_PASSWORD_IDENTIFIER;
963
964 return ret;
965 }
966
sae_parse_commit_token(struct sae_data * sae,const u8 ** pos,const u8 * end,const u8 ** token,size_t * token_len)967 static void sae_parse_commit_token(struct sae_data *sae, const u8 **pos,
968 const u8 *end, const u8 **token,
969 size_t *token_len)
970 {
971 size_t scalar_elem_len, tlen;
972 const u8 *elem;
973
974 if (token)
975 *token = NULL;
976 if (token_len)
977 *token_len = 0;
978
979 scalar_elem_len = (sae->tmp->ec ? 3 : 2) * sae->tmp->prime_len;
980 if (scalar_elem_len >= (size_t) (end - *pos))
981 return; /* No extra data beyond peer scalar and element */
982
983 /* It is a bit difficult to parse this now that there is an
984 * optional variable length Anti-Clogging Token field and
985 * optional variable length Password Identifier element in the
986 * frame. We are sending out fixed length Anti-Clogging Token
987 * fields, so use that length as a requirement for the received
988 * token and check for the presence of possible Password
989 * Identifier element based on the element header information.
990 */
991 tlen = end - (*pos + scalar_elem_len);
992
993 if (tlen < SHA256_MAC_LEN) {
994 wpa_printf(MSG_DEBUG,
995 "SAE: Too short optional data (%u octets) to include our Anti-Clogging Token",
996 (unsigned int) tlen);
997 return;
998 }
999
1000 elem = *pos + scalar_elem_len;
1001 if (sae_is_password_id_elem(elem, end)) {
1002 /* Password Identifier element takes out all available
1003 * extra octets, so there can be no Anti-Clogging token in
1004 * this frame. */
1005 return;
1006 }
1007
1008 elem += SHA256_MAC_LEN;
1009 if (sae_is_password_id_elem(elem, end)) {
1010 /* Password Identifier element is included in the end, so
1011 * remove its length from the Anti-Clogging token field. */
1012 tlen -= 2 + elem[1];
1013 }
1014
1015 wpa_hexdump(MSG_DEBUG, "SAE: Anti-Clogging Token", *pos, tlen);
1016 if (token)
1017 *token = *pos;
1018 if (token_len)
1019 *token_len = tlen;
1020 *pos += tlen;
1021 }
1022
sae_parse_commit_scalar(struct sae_data * sae,const u8 ** pos,const u8 * end)1023 static u16 sae_parse_commit_scalar(struct sae_data *sae, const u8 **pos,
1024 const u8 *end)
1025 {
1026 struct crypto_bignum *peer_scalar;
1027
1028 if (sae->tmp->prime_len > end - *pos) {
1029 wpa_printf(MSG_DEBUG, "SAE: Not enough data for scalar");
1030 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1031 }
1032
1033 peer_scalar = crypto_bignum_init_set(*pos, sae->tmp->prime_len);
1034 if (peer_scalar == NULL)
1035 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1036
1037 /*
1038 * IEEE Std 802.11-2012, 11.3.8.6.1: If there is a protocol instance for
1039 * the peer and it is in Authenticated state, the new Commit Message
1040 * shall be dropped if the peer-scalar is identical to the one used in
1041 * the existing protocol instance.
1042 */
1043 if (sae->state == SAE_ACCEPTED && sae->peer_commit_scalar &&
1044 crypto_bignum_cmp(sae->peer_commit_scalar, peer_scalar) == 0) {
1045 wpa_printf(MSG_DEBUG, "SAE: Do not accept re-use of previous "
1046 "peer-commit-scalar");
1047 crypto_bignum_deinit(peer_scalar, 0);
1048 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1049 }
1050
1051 /* 1 < scalar < r */
1052 if (crypto_bignum_is_zero(peer_scalar) ||
1053 crypto_bignum_is_one(peer_scalar) ||
1054 crypto_bignum_cmp(peer_scalar, sae->tmp->order) >= 0) {
1055 wpa_printf(MSG_DEBUG, "SAE: Invalid peer scalar");
1056 crypto_bignum_deinit(peer_scalar, 0);
1057 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1058 }
1059
1060 crypto_bignum_deinit(sae->peer_commit_scalar, 0);
1061 sae->peer_commit_scalar = peer_scalar;
1062 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-scalar",
1063 *pos, sae->tmp->prime_len);
1064 *pos += sae->tmp->prime_len;
1065
1066 return WLAN_STATUS_SUCCESS;
1067 }
1068
sae_parse_commit_element_ecc(struct sae_data * sae,const u8 ** pos,const u8 * end)1069 static u16 sae_parse_commit_element_ecc(struct sae_data *sae, const u8 **pos,
1070 const u8 *end)
1071 {
1072 u8 prime[SAE_MAX_ECC_PRIME_LEN];
1073
1074 if (2 * sae->tmp->prime_len > end - *pos) {
1075 wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
1076 "commit-element");
1077 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1078 }
1079
1080 if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
1081 sae->tmp->prime_len) < 0)
1082 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1083
1084 /* element x and y coordinates < p */
1085 if (os_memcmp(*pos, prime, sae->tmp->prime_len) >= 0 ||
1086 os_memcmp(*pos + sae->tmp->prime_len, prime,
1087 sae->tmp->prime_len) >= 0) {
1088 wpa_printf(MSG_DEBUG, "SAE: Invalid coordinates in peer "
1089 "element");
1090 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1091 }
1092
1093 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(x)",
1094 *pos, sae->tmp->prime_len);
1095 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(y)",
1096 *pos + sae->tmp->prime_len, sae->tmp->prime_len);
1097
1098 crypto_ec_point_deinit(sae->tmp->peer_commit_element_ecc, 0);
1099 sae->tmp->peer_commit_element_ecc =
1100 crypto_ec_point_from_bin(sae->tmp->ec, *pos);
1101 if (sae->tmp->peer_commit_element_ecc == NULL)
1102 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1103
1104 if (!crypto_ec_point_is_on_curve(sae->tmp->ec,
1105 sae->tmp->peer_commit_element_ecc)) {
1106 wpa_printf(MSG_DEBUG, "SAE: Peer element is not on curve");
1107 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1108 }
1109
1110 *pos += 2 * sae->tmp->prime_len;
1111
1112 return WLAN_STATUS_SUCCESS;
1113 }
1114
sae_parse_commit_element_ffc(struct sae_data * sae,const u8 ** pos,const u8 * end)1115 static u16 sae_parse_commit_element_ffc(struct sae_data *sae, const u8 **pos,
1116 const u8 *end)
1117 {
1118 struct crypto_bignum *res, *one;
1119 const u8 one_bin[1] = { 0x01 };
1120
1121 if (sae->tmp->prime_len > end - *pos) {
1122 wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
1123 "commit-element");
1124 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1125 }
1126 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element", *pos,
1127 sae->tmp->prime_len);
1128
1129 crypto_bignum_deinit(sae->tmp->peer_commit_element_ffc, 0);
1130 sae->tmp->peer_commit_element_ffc =
1131 crypto_bignum_init_set(*pos, sae->tmp->prime_len);
1132 if (sae->tmp->peer_commit_element_ffc == NULL)
1133 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1134 /* 1 < element < p - 1 */
1135 res = crypto_bignum_init();
1136 one = crypto_bignum_init_set(one_bin, sizeof(one_bin));
1137 if (!res || !one ||
1138 crypto_bignum_sub(sae->tmp->prime, one, res) ||
1139 crypto_bignum_is_zero(sae->tmp->peer_commit_element_ffc) ||
1140 crypto_bignum_is_one(sae->tmp->peer_commit_element_ffc) ||
1141 crypto_bignum_cmp(sae->tmp->peer_commit_element_ffc, res) >= 0) {
1142 crypto_bignum_deinit(res, 0);
1143 crypto_bignum_deinit(one, 0);
1144 wpa_printf(MSG_DEBUG, "SAE: Invalid peer element");
1145 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1146 }
1147 crypto_bignum_deinit(one, 0);
1148
1149 /* scalar-op(r, ELEMENT) = 1 modulo p */
1150 if (crypto_bignum_exptmod(sae->tmp->peer_commit_element_ffc,
1151 sae->tmp->order, sae->tmp->prime, res) < 0 ||
1152 !crypto_bignum_is_one(res)) {
1153 wpa_printf(MSG_DEBUG, "SAE: Invalid peer element (scalar-op)");
1154 crypto_bignum_deinit(res, 0);
1155 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1156 }
1157 crypto_bignum_deinit(res, 0);
1158
1159 *pos += sae->tmp->prime_len;
1160
1161 return WLAN_STATUS_SUCCESS;
1162 }
1163
sae_parse_commit_element(struct sae_data * sae,const u8 ** pos,const u8 * end)1164 static u16 sae_parse_commit_element(struct sae_data *sae, const u8 **pos,
1165 const u8 *end)
1166 {
1167 if (sae->tmp->dh)
1168 return sae_parse_commit_element_ffc(sae, pos, end);
1169 return sae_parse_commit_element_ecc(sae, pos, end);
1170 }
1171
sae_parse_password_identifier(struct sae_data * sae,const u8 * pos,const u8 * end)1172 static int sae_parse_password_identifier(struct sae_data *sae,
1173 const u8 *pos, const u8 *end)
1174 {
1175 wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
1176 pos, end - pos);
1177 if (!sae_is_password_id_elem(pos, end)) {
1178 if (sae->tmp->pw_id) {
1179 wpa_printf(MSG_DEBUG,
1180 "SAE: No Password Identifier included, but expected one (%s)",
1181 sae->tmp->pw_id);
1182 return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
1183 }
1184 os_free(sae->tmp->pw_id);
1185 sae->tmp->pw_id = NULL;
1186 return WLAN_STATUS_SUCCESS; /* No Password Identifier */
1187 }
1188
1189 if (sae->tmp->pw_id &&
1190 (pos[1] - 1 != (int) os_strlen(sae->tmp->pw_id) ||
1191 os_memcmp(sae->tmp->pw_id, pos + 3, pos[1] - 1) != 0)) {
1192 wpa_printf(MSG_DEBUG,
1193 "SAE: The included Password Identifier does not match the expected one (%s)",
1194 sae->tmp->pw_id);
1195 return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
1196 }
1197
1198 os_free(sae->tmp->pw_id);
1199 sae->tmp->pw_id = os_malloc(pos[1]);
1200 if (!sae->tmp->pw_id)
1201 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1202 os_memcpy(sae->tmp->pw_id, pos + 3, pos[1] - 1);
1203 sae->tmp->pw_id[pos[1] - 1] = '\0';
1204 return WLAN_STATUS_SUCCESS;
1205 }
1206
sae_parse_commit(struct sae_data * sae,const u8 * data,size_t len,const u8 ** token,size_t * token_len,int * allowed_groups)1207 u16 sae_parse_commit(struct sae_data *sae, const u8 *data, size_t len,
1208 const u8 **token, size_t *token_len, int *allowed_groups)
1209 {
1210 const u8 *pos = data, *end = data + len;
1211 u16 res;
1212
1213 /* Check Finite Cyclic Group */
1214 if (end - pos < 2)
1215 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1216 res = sae_group_allowed(sae, allowed_groups, WPA_GET_LE16(pos));
1217 if (res != WLAN_STATUS_SUCCESS)
1218 return res;
1219 pos += 2;
1220
1221 /* Optional Anti-Clogging Token */
1222 sae_parse_commit_token(sae, &pos, end, token, token_len);
1223
1224 /* commit-scalar */
1225 res = sae_parse_commit_scalar(sae, &pos, end);
1226 if (res != WLAN_STATUS_SUCCESS)
1227 return res;
1228
1229 /* commit-element */
1230 res = sae_parse_commit_element(sae, &pos, end);
1231 if (res != WLAN_STATUS_SUCCESS)
1232 return res;
1233
1234 /* Optional Password Identifier element */
1235 res = sae_parse_password_identifier(sae, pos, end);
1236 if (res != WLAN_STATUS_SUCCESS)
1237 return res;
1238
1239 /*
1240 * Check whether peer-commit-scalar and PEER-COMMIT-ELEMENT are same as
1241 * the values we sent which would be evidence of a reflection attack.
1242 */
1243 if (!sae->tmp->own_commit_scalar ||
1244 crypto_bignum_cmp(sae->tmp->own_commit_scalar,
1245 sae->peer_commit_scalar) != 0 ||
1246 (sae->tmp->dh &&
1247 (!sae->tmp->own_commit_element_ffc ||
1248 crypto_bignum_cmp(sae->tmp->own_commit_element_ffc,
1249 sae->tmp->peer_commit_element_ffc) != 0)) ||
1250 (sae->tmp->ec &&
1251 (!sae->tmp->own_commit_element_ecc ||
1252 crypto_ec_point_cmp(sae->tmp->ec,
1253 sae->tmp->own_commit_element_ecc,
1254 sae->tmp->peer_commit_element_ecc) != 0)))
1255 return WLAN_STATUS_SUCCESS; /* scalars/elements are different */
1256
1257 /*
1258 * This is a reflection attack - return special value to trigger caller
1259 * to silently discard the frame instead of replying with a specific
1260 * status code.
1261 */
1262 return SAE_SILENTLY_DISCARD;
1263 }
1264
sae_cn_confirm(struct sae_data * sae,const u8 * sc,const struct crypto_bignum * scalar1,const u8 * element1,size_t element1_len,const struct crypto_bignum * scalar2,const u8 * element2,size_t element2_len,u8 * confirm)1265 static void sae_cn_confirm(struct sae_data *sae, const u8 *sc,
1266 const struct crypto_bignum *scalar1,
1267 const u8 *element1, size_t element1_len,
1268 const struct crypto_bignum *scalar2,
1269 const u8 *element2, size_t element2_len,
1270 u8 *confirm)
1271 {
1272 const u8 *addr[5];
1273 size_t len[5];
1274 u8 scalar_b1[SAE_MAX_PRIME_LEN], scalar_b2[SAE_MAX_PRIME_LEN];
1275
1276 /* Confirm
1277 * CN(key, X, Y, Z, ...) =
1278 * HMAC-SHA256(key, D2OS(X) || D2OS(Y) || D2OS(Z) | ...)
1279 * confirm = CN(KCK, send-confirm, commit-scalar, COMMIT-ELEMENT,
1280 * peer-commit-scalar, PEER-COMMIT-ELEMENT)
1281 * verifier = CN(KCK, peer-send-confirm, peer-commit-scalar,
1282 * PEER-COMMIT-ELEMENT, commit-scalar, COMMIT-ELEMENT)
1283 */
1284 addr[0] = sc;
1285 len[0] = 2;
1286 crypto_bignum_to_bin(scalar1, scalar_b1, sizeof(scalar_b1),
1287 sae->tmp->prime_len);
1288 addr[1] = scalar_b1;
1289 len[1] = sae->tmp->prime_len;
1290 addr[2] = element1;
1291 len[2] = element1_len;
1292 crypto_bignum_to_bin(scalar2, scalar_b2, sizeof(scalar_b2),
1293 sae->tmp->prime_len);
1294 addr[3] = scalar_b2;
1295 len[3] = sae->tmp->prime_len;
1296 addr[4] = element2;
1297 len[4] = element2_len;
1298 hmac_sha256_vector(sae->tmp->kck, sizeof(sae->tmp->kck), 5, addr, len,
1299 confirm);
1300 }
1301
sae_cn_confirm_ecc(struct sae_data * sae,const u8 * sc,const struct crypto_bignum * scalar1,const struct crypto_ec_point * element1,const struct crypto_bignum * scalar2,const struct crypto_ec_point * element2,u8 * confirm)1302 static int sae_cn_confirm_ecc(struct sae_data *sae, const u8 *sc,
1303 const struct crypto_bignum *scalar1,
1304 const struct crypto_ec_point *element1,
1305 const struct crypto_bignum *scalar2,
1306 const struct crypto_ec_point *element2,
1307 u8 *confirm)
1308 {
1309 u8 element_b1[2 * SAE_MAX_ECC_PRIME_LEN];
1310 u8 element_b2[2 * SAE_MAX_ECC_PRIME_LEN];
1311
1312 if (crypto_ec_point_to_bin(sae->tmp->ec, element1, element_b1,
1313 element_b1 + sae->tmp->prime_len) < 0) {
1314 wpa_printf(MSG_ERROR, "SAE: failed bignum op while deriving ec point");
1315 return ESP_FAIL;
1316 }
1317 if (crypto_ec_point_to_bin(sae->tmp->ec, element2, element_b2,
1318 element_b2 + sae->tmp->prime_len) < 0) {
1319 wpa_printf(MSG_ERROR, "SAE: failed bignum op while deriving ec point");
1320 return ESP_FAIL;
1321 }
1322
1323 sae_cn_confirm(sae, sc, scalar1, element_b1, 2 * sae->tmp->prime_len,
1324 scalar2, element_b2, 2 * sae->tmp->prime_len, confirm);
1325 return ESP_OK;
1326 }
1327
sae_cn_confirm_ffc(struct sae_data * sae,const u8 * sc,const struct crypto_bignum * scalar1,const struct crypto_bignum * element1,const struct crypto_bignum * scalar2,const struct crypto_bignum * element2,u8 * confirm)1328 static int sae_cn_confirm_ffc(struct sae_data *sae, const u8 *sc,
1329 const struct crypto_bignum *scalar1,
1330 const struct crypto_bignum *element1,
1331 const struct crypto_bignum *scalar2,
1332 const struct crypto_bignum *element2,
1333 u8 *confirm)
1334 {
1335 u8 element_b1[SAE_MAX_PRIME_LEN];
1336 u8 element_b2[SAE_MAX_PRIME_LEN];
1337
1338 if (crypto_bignum_to_bin(element1, element_b1, sizeof(element_b1),
1339 sae->tmp->prime_len) < 0) {
1340 wpa_printf(MSG_ERROR, "SAE: failed bignum op while generating SAE confirm - e1");
1341 return ESP_FAIL;
1342 }
1343 if (crypto_bignum_to_bin(element2, element_b2, sizeof(element_b2),
1344 sae->tmp->prime_len) < 0) {
1345 wpa_printf(MSG_ERROR, "SAE: failed bignum op while generating SAE confirm - e2");
1346 return ESP_FAIL;
1347 }
1348
1349 sae_cn_confirm(sae, sc, scalar1, element_b1, sae->tmp->prime_len,
1350 scalar2, element_b2, sae->tmp->prime_len, confirm);
1351 return ESP_OK;
1352 }
1353
sae_write_confirm(struct sae_data * sae,struct wpabuf * buf)1354 int sae_write_confirm(struct sae_data *sae, struct wpabuf *buf)
1355 {
1356 const u8 *sc;
1357
1358 if (sae->tmp == NULL)
1359 return ESP_FAIL;
1360
1361 /* Send-Confirm */
1362 sc = wpabuf_put(buf, 0);
1363 wpabuf_put_le16(buf, sae->send_confirm);
1364 if (sae->send_confirm < 0xffff)
1365 sae->send_confirm++;
1366
1367 if (sae->tmp->ec) {
1368 if (sae_cn_confirm_ecc(sae, sc, sae->tmp->own_commit_scalar,
1369 sae->tmp->own_commit_element_ecc,
1370 sae->peer_commit_scalar,
1371 sae->tmp->peer_commit_element_ecc,
1372 wpabuf_put(buf, SHA256_MAC_LEN))) {
1373 wpa_printf(MSG_ERROR, "SAE: failed generate SAE confirm (ecc)");
1374 return ESP_FAIL;
1375 }
1376 } else {
1377 if (sae_cn_confirm_ffc(sae, sc, sae->tmp->own_commit_scalar,
1378 sae->tmp->own_commit_element_ffc,
1379 sae->peer_commit_scalar,
1380 sae->tmp->peer_commit_element_ffc,
1381 wpabuf_put(buf, SHA256_MAC_LEN))) {
1382 wpa_printf(MSG_ERROR, "SAE: failed generate SAE confirm (ffc)");
1383 return ESP_FAIL;
1384 }
1385 }
1386 return ESP_OK;
1387 }
1388
sae_check_confirm(struct sae_data * sae,const u8 * data,size_t len)1389 int sae_check_confirm(struct sae_data *sae, const u8 *data, size_t len)
1390 {
1391 u8 verifier[SHA256_MAC_LEN];
1392
1393 if (len < 2 + SHA256_MAC_LEN) {
1394 wpa_printf(MSG_DEBUG, "SAE: Too short confirm message");
1395 return ESP_FAIL;
1396 }
1397
1398 wpa_printf(MSG_DEBUG, "SAE: peer-send-confirm %u", WPA_GET_LE16(data));
1399
1400 if (sae->tmp == NULL) {
1401 wpa_printf(MSG_DEBUG, "SAE: Temporary data not yet available");
1402 return ESP_FAIL;
1403 }
1404
1405 if (sae->tmp->ec) {
1406 if (sae_cn_confirm_ecc(sae, data, sae->peer_commit_scalar,
1407 sae->tmp->peer_commit_element_ecc,
1408 sae->tmp->own_commit_scalar,
1409 sae->tmp->own_commit_element_ecc,
1410 verifier)) {
1411 wpa_printf(MSG_ERROR, "SAE: failed to check SAE confirm (ecc)");
1412 return ESP_FAIL;
1413 }
1414 } else {
1415 if (sae_cn_confirm_ffc(sae, data, sae->peer_commit_scalar,
1416 sae->tmp->peer_commit_element_ffc,
1417 sae->tmp->own_commit_scalar,
1418 sae->tmp->own_commit_element_ffc,
1419 verifier)) {
1420 wpa_printf(MSG_ERROR, "SAE: failed check SAE confirm (ffc)");
1421 return ESP_FAIL;
1422 }
1423 }
1424
1425 if (os_memcmp(verifier, data + 2, SHA256_MAC_LEN) != 0) {
1426 wpa_printf(MSG_DEBUG, "SAE: Confirm mismatch");
1427 wpa_hexdump(MSG_DEBUG, "SAE: Received confirm",
1428 data + 2, SHA256_MAC_LEN);
1429 wpa_hexdump(MSG_DEBUG, "SAE: Calculated verifier",
1430 verifier, SHA256_MAC_LEN);
1431 return ESP_FAIL;
1432 }
1433
1434 return ESP_OK;
1435 }
1436
sae_state_txt(enum sae_state state)1437 const char * sae_state_txt(enum sae_state state)
1438 {
1439 switch (state) {
1440 case SAE_NOTHING:
1441 return "Nothing";
1442 case SAE_COMMITTED:
1443 return "Committed";
1444 case SAE_CONFIRMED:
1445 return "Confirmed";
1446 case SAE_ACCEPTED:
1447 return "Accepted";
1448 }
1449 return "?";
1450 }
1451
1452 #endif /* CONFIG_WPA3_SAE */
1453