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