2 * Simultaneous authentication of equals
3 * Copyright (c) 2012-2016, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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"
22 int sae_set_group(struct sae_data *sae, int group)
24 struct sae_temporary_data *tmp;
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);
33 #endif /* CONFIG_TESTING_OPTIONS */
36 tmp = sae->tmp = os_zalloc(sizeof(*tmp));
40 /* First, check if this is an ECC group */
41 tmp->ec = crypto_ec_init(group);
43 wpa_printf(MSG_DEBUG, "SAE: Selecting supported ECC group %d",
46 tmp->prime_len = crypto_ec_prime_len(tmp->ec);
47 tmp->prime = crypto_ec_get_prime(tmp->ec);
48 tmp->order_len = crypto_ec_order_len(tmp->ec);
49 tmp->order = crypto_ec_get_order(tmp->ec);
53 /* Not an ECC group, check FFC */
54 tmp->dh = dh_groups_get(group);
56 wpa_printf(MSG_DEBUG, "SAE: Selecting supported FFC group %d",
59 tmp->prime_len = tmp->dh->prime_len;
60 if (tmp->prime_len > SAE_MAX_PRIME_LEN) {
65 tmp->prime_buf = crypto_bignum_init_set(tmp->dh->prime,
67 if (tmp->prime_buf == NULL) {
71 tmp->prime = tmp->prime_buf;
73 tmp->order_len = tmp->dh->order_len;
74 tmp->order_buf = crypto_bignum_init_set(tmp->dh->order,
76 if (tmp->order_buf == NULL) {
80 tmp->order = tmp->order_buf;
85 /* Unsupported group */
87 "SAE: Group %d not supported by the crypto library", group);
92 void sae_clear_temp_data(struct sae_data *sae)
94 struct sae_temporary_data *tmp;
95 if (sae == NULL || sae->tmp == NULL)
98 crypto_ec_deinit(tmp->ec);
99 crypto_bignum_deinit(tmp->prime_buf, 0);
100 crypto_bignum_deinit(tmp->order_buf, 0);
101 crypto_bignum_deinit(tmp->sae_rand, 1);
102 crypto_bignum_deinit(tmp->pwe_ffc, 1);
103 crypto_bignum_deinit(tmp->own_commit_scalar, 0);
104 crypto_bignum_deinit(tmp->own_commit_element_ffc, 0);
105 crypto_bignum_deinit(tmp->peer_commit_element_ffc, 0);
106 crypto_ec_point_deinit(tmp->pwe_ecc, 1);
107 crypto_ec_point_deinit(tmp->own_commit_element_ecc, 0);
108 crypto_ec_point_deinit(tmp->peer_commit_element_ecc, 0);
109 wpabuf_free(tmp->anti_clogging_token);
111 bin_clear_free(tmp, sizeof(*tmp));
116 void sae_clear_data(struct sae_data *sae)
120 sae_clear_temp_data(sae);
121 crypto_bignum_deinit(sae->peer_commit_scalar, 0);
122 os_memset(sae, 0, sizeof(*sae));
126 static void sae_pwd_seed_key(const u8 *addr1, const u8 *addr2, u8 *key)
128 wpa_printf(MSG_DEBUG, "SAE: PWE derivation - addr1=" MACSTR
129 " addr2=" MACSTR, MAC2STR(addr1), MAC2STR(addr2));
130 if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) {
131 os_memcpy(key, addr1, ETH_ALEN);
132 os_memcpy(key + ETH_ALEN, addr2, ETH_ALEN);
134 os_memcpy(key, addr2, ETH_ALEN);
135 os_memcpy(key + ETH_ALEN, addr1, ETH_ALEN);
140 static int sae_test_pwd_seed_ecc(struct sae_data *sae, const u8 *pwd_seed,
141 const u8 *prime, const u8 *qr, const u8 *qnr,
144 struct crypto_bignum *y_sqr, *x_cand;
148 unsigned int in_range;
150 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
152 /* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
153 bits = crypto_ec_prime_len_bits(sae->tmp->ec);
154 if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking",
155 prime, sae->tmp->prime_len, pwd_value, bits) < 0)
158 buf_shift_right(pwd_value, sae->tmp->prime_len, 8 - bits % 8);
159 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value",
160 pwd_value, sae->tmp->prime_len);
162 cmp_prime = const_time_memcmp(pwd_value, prime, sae->tmp->prime_len);
163 /* Create a const_time mask for selection based on prf result
164 * being smaller than prime. */
165 in_range = const_time_fill_msb((unsigned int) cmp_prime);
166 /* The algorithm description would skip the next steps if
167 * cmp_prime >= 0 (reutnr 0 here), but go through them regardless to
168 * minimize externally observable differences in behavior. */
170 x_cand = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
173 y_sqr = crypto_ec_point_compute_y_sqr(sae->tmp->ec, x_cand);
174 crypto_bignum_deinit(x_cand, 1);
178 res = dragonfly_is_quadratic_residue_blind(sae->tmp->ec, qr, qnr,
180 crypto_bignum_deinit(y_sqr, 1);
183 return const_time_select_int(in_range, res, 0);
187 /* Returns -1 on fatal failure, 0 if PWE cannot be derived from the provided
188 * pwd-seed, or 1 if a valid PWE was derived from pwd-seed. */
189 static int sae_test_pwd_seed_ffc(struct sae_data *sae, const u8 *pwd_seed,
190 struct crypto_bignum *pwe)
192 u8 pwd_value[SAE_MAX_PRIME_LEN];
193 size_t bits = sae->tmp->prime_len * 8;
195 struct crypto_bignum *a, *b = NULL;
199 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN);
201 /* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
202 if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking",
203 sae->tmp->dh->prime, sae->tmp->prime_len, pwd_value,
206 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value", pwd_value,
207 sae->tmp->prime_len);
209 /* Check whether pwd-value < p */
210 res = const_time_memcmp(pwd_value, sae->tmp->dh->prime,
211 sae->tmp->prime_len);
212 /* pwd-value >= p is invalid, so res is < 0 for the valid cases and
213 * the negative sign can be used to fill the mask for constant time
215 pwd_value_valid = const_time_fill_msb(res);
217 /* If pwd-value >= p, force pwd-value to be < p and perform the
218 * calculations anyway to hide timing difference. The derived PWE will
219 * be ignored in that case. */
220 pwd_value[0] = const_time_select_u8(pwd_value_valid, pwd_value[0], 0);
222 /* PWE = pwd-value^((p-1)/r) modulo p */
225 a = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len);
229 /* This is an optimization based on the used group that does not depend
230 * on the password in any way, so it is fine to use separate branches
231 * for this step without constant time operations. */
232 if (sae->tmp->dh->safe_prime) {
234 * r = (p-1)/2 for the group used here, so this becomes:
235 * PWE = pwd-value^2 modulo p
238 b = crypto_bignum_init_set(exp, sizeof(exp));
240 /* Calculate exponent: (p-1)/r */
242 b = crypto_bignum_init_set(exp, sizeof(exp));
244 crypto_bignum_sub(sae->tmp->prime, b, b) < 0 ||
245 crypto_bignum_div(b, sae->tmp->order, b) < 0)
252 res = crypto_bignum_exptmod(a, b, sae->tmp->prime, pwe);
256 /* There were no fatal errors in calculations, so determine the return
257 * value using constant time operations. We get here for number of
258 * invalid cases which are cleared here after having performed all the
259 * computation. PWE is valid if pwd-value was less than prime and
260 * PWE > 1. Start with pwd-value check first and then use constant time
261 * operations to clear res to 0 if PWE is 0 or 1.
263 res = const_time_select_u8(pwd_value_valid, 1, 0);
264 is_val = crypto_bignum_is_zero(pwe);
265 res = const_time_select_u8(const_time_is_zero(is_val), res, 0);
266 is_val = crypto_bignum_is_one(pwe);
267 res = const_time_select_u8(const_time_is_zero(is_val), res, 0);
270 crypto_bignum_deinit(a, 1);
271 crypto_bignum_deinit(b, 1);
276 static int sae_derive_pwe_ecc(struct sae_data *sae, const u8 *addr1,
277 const u8 *addr2, const u8 *password,
278 size_t password_len, const char *identifier)
281 u8 addrs[2 * ETH_ALEN];
285 u8 *dummy_password, *tmp_password;
286 int pwd_seed_odd = 0;
287 u8 prime[SAE_MAX_ECC_PRIME_LEN];
289 struct crypto_bignum *x = NULL, *qr = NULL, *qnr = NULL;
290 u8 x_bin[SAE_MAX_ECC_PRIME_LEN];
291 u8 x_cand_bin[SAE_MAX_ECC_PRIME_LEN];
292 u8 qr_bin[SAE_MAX_ECC_PRIME_LEN];
293 u8 qnr_bin[SAE_MAX_ECC_PRIME_LEN];
295 u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
298 os_memset(x_bin, 0, sizeof(x_bin));
300 dummy_password = os_malloc(password_len);
301 tmp_password = os_malloc(password_len);
302 if (!dummy_password || !tmp_password ||
303 random_get_bytes(dummy_password, password_len) < 0)
306 prime_len = sae->tmp->prime_len;
307 if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
312 * Create a random quadratic residue (qr) and quadratic non-residue
313 * (qnr) modulo p for blinding purposes during the loop.
315 if (dragonfly_get_random_qr_qnr(sae->tmp->prime, &qr, &qnr) < 0 ||
316 crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin), prime_len) < 0 ||
317 crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin), prime_len) < 0)
320 wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
321 password, password_len);
323 wpa_printf(MSG_DEBUG, "SAE: password identifier: %s",
327 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
328 * base = password [|| identifier]
329 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
332 sae_pwd_seed_key(addr1, addr2, addrs);
334 addr[0] = tmp_password;
335 len[0] = password_len;
338 addr[num_elem] = (const u8 *) identifier;
339 len[num_elem] = os_strlen(identifier);
342 addr[num_elem] = &counter;
343 len[num_elem] = sizeof(counter);
347 * Continue for at least k iterations to protect against side-channel
348 * attacks that attempt to determine the number of iterations required
351 k = dragonfly_min_pwe_loop_iter(sae->group);
353 for (counter = 1; counter <= k || !found; counter++) {
354 u8 pwd_seed[SHA256_MAC_LEN];
357 /* This should not happen in practice */
358 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
362 wpa_printf(MSG_DEBUG, "SAE: counter = %03u", counter);
363 const_time_select_bin(found, dummy_password, password,
364 password_len, tmp_password);
365 if (hmac_sha256_vector(addrs, sizeof(addrs), num_elem,
366 addr, len, pwd_seed) < 0)
369 res = sae_test_pwd_seed_ecc(sae, pwd_seed,
370 prime, qr_bin, qnr_bin, x_cand_bin);
371 const_time_select_bin(found, x_bin, x_cand_bin, prime_len,
373 pwd_seed_odd = const_time_select_u8(
375 pwd_seed[SHA256_MAC_LEN - 1] & 0x01);
376 os_memset(pwd_seed, 0, sizeof(pwd_seed));
379 /* Need to minimize differences in handling res == 0 and 1 here
380 * to avoid differences in timing and instruction cache access,
381 * so use const_time_select_*() to make local copies of the
382 * values based on whether this loop iteration was the one that
383 * found the pwd-seed/x. */
385 /* found is 0 or 0xff here and res is 0 or 1. Bitwise OR of them
386 * (with res converted to 0/0xff) handles this in constant time.
389 wpa_printf(MSG_DEBUG, "SAE: pwd-seed result %d found=0x%02x",
394 wpa_printf(MSG_DEBUG, "SAE: Could not generate PWE");
399 x = crypto_bignum_init_set(x_bin, prime_len);
405 if (!sae->tmp->pwe_ecc)
406 sae->tmp->pwe_ecc = crypto_ec_point_init(sae->tmp->ec);
407 if (!sae->tmp->pwe_ecc)
410 res = crypto_ec_point_solve_y_coord(sae->tmp->ec,
411 sae->tmp->pwe_ecc, x,
415 * This should not happen since we already checked that there
418 wpa_printf(MSG_DEBUG, "SAE: Could not solve y");
422 crypto_bignum_deinit(qr, 0);
423 crypto_bignum_deinit(qnr, 0);
424 os_free(dummy_password);
425 bin_clear_free(tmp_password, password_len);
426 crypto_bignum_deinit(x, 1);
427 os_memset(x_bin, 0, sizeof(x_bin));
428 os_memset(x_cand_bin, 0, sizeof(x_cand_bin));
434 static int sae_derive_pwe_ffc(struct sae_data *sae, const u8 *addr1,
435 const u8 *addr2, const u8 *password,
436 size_t password_len, const char *identifier)
438 u8 counter, k, sel_counter = 0;
439 u8 addrs[2 * ETH_ALEN];
443 u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_*
446 struct crypto_bignum *pwe;
447 size_t prime_len = sae->tmp->prime_len * 8;
450 crypto_bignum_deinit(sae->tmp->pwe_ffc, 1);
451 sae->tmp->pwe_ffc = NULL;
453 /* Allocate a buffer to maintain selected and candidate PWE for constant
455 pwe_buf = os_zalloc(prime_len * 2);
456 pwe = crypto_bignum_init();
457 if (!pwe_buf || !pwe)
460 wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
461 password, password_len);
464 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
465 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
466 * password [|| identifier] || counter)
468 sae_pwd_seed_key(addr1, addr2, addrs);
471 len[0] = password_len;
474 addr[num_elem] = (const u8 *) identifier;
475 len[num_elem] = os_strlen(identifier);
478 addr[num_elem] = &counter;
479 len[num_elem] = sizeof(counter);
482 k = dragonfly_min_pwe_loop_iter(sae->group);
484 for (counter = 1; counter <= k || !found; counter++) {
485 u8 pwd_seed[SHA256_MAC_LEN];
489 /* This should not happen in practice */
490 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
494 wpa_printf(MSG_DEBUG, "SAE: counter = %02u", counter);
495 if (hmac_sha256_vector(addrs, sizeof(addrs), num_elem,
496 addr, len, pwd_seed) < 0)
498 res = sae_test_pwd_seed_ffc(sae, pwd_seed, pwe);
499 /* res is -1 for fatal failure, 0 if a valid PWE was not found,
500 * or 1 if a valid PWE was found. */
503 /* Store the candidate PWE into the second half of pwe_buf and
504 * the selected PWE in the beginning of pwe_buf using constant
506 if (crypto_bignum_to_bin(pwe, pwe_buf + prime_len, prime_len,
509 const_time_select_bin(found, pwe_buf, pwe_buf + prime_len,
511 sel_counter = const_time_select_u8(found, sel_counter, counter);
512 mask = const_time_eq_u8(res, 1);
513 found = const_time_select_u8(found, found, mask);
519 wpa_printf(MSG_DEBUG, "SAE: Use PWE from counter = %02u", sel_counter);
520 sae->tmp->pwe_ffc = crypto_bignum_init_set(pwe_buf, prime_len);
522 crypto_bignum_deinit(pwe, 1);
523 bin_clear_free(pwe_buf, prime_len * 2);
524 return sae->tmp->pwe_ffc ? 0 : -1;
528 static int sae_derive_commit_element_ecc(struct sae_data *sae,
529 struct crypto_bignum *mask)
531 /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
532 if (!sae->tmp->own_commit_element_ecc) {
533 sae->tmp->own_commit_element_ecc =
534 crypto_ec_point_init(sae->tmp->ec);
535 if (!sae->tmp->own_commit_element_ecc)
539 if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc, mask,
540 sae->tmp->own_commit_element_ecc) < 0 ||
541 crypto_ec_point_invert(sae->tmp->ec,
542 sae->tmp->own_commit_element_ecc) < 0) {
543 wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
551 static int sae_derive_commit_element_ffc(struct sae_data *sae,
552 struct crypto_bignum *mask)
554 /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
555 if (!sae->tmp->own_commit_element_ffc) {
556 sae->tmp->own_commit_element_ffc = crypto_bignum_init();
557 if (!sae->tmp->own_commit_element_ffc)
561 if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, mask, sae->tmp->prime,
562 sae->tmp->own_commit_element_ffc) < 0 ||
563 crypto_bignum_inverse(sae->tmp->own_commit_element_ffc,
565 sae->tmp->own_commit_element_ffc) < 0) {
566 wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element");
574 static int sae_derive_commit(struct sae_data *sae)
576 struct crypto_bignum *mask;
579 mask = crypto_bignum_init();
580 if (!sae->tmp->sae_rand)
581 sae->tmp->sae_rand = crypto_bignum_init();
582 if (!sae->tmp->own_commit_scalar)
583 sae->tmp->own_commit_scalar = crypto_bignum_init();
584 ret = !mask || !sae->tmp->sae_rand || !sae->tmp->own_commit_scalar ||
585 dragonfly_generate_scalar(sae->tmp->order, sae->tmp->sae_rand,
587 sae->tmp->own_commit_scalar) < 0 ||
589 sae_derive_commit_element_ecc(sae, mask) < 0) ||
591 sae_derive_commit_element_ffc(sae, mask) < 0);
592 crypto_bignum_deinit(mask, 1);
597 int sae_prepare_commit(const u8 *addr1, const u8 *addr2,
598 const u8 *password, size_t password_len,
599 const char *identifier, struct sae_data *sae)
601 if (sae->tmp == NULL ||
602 (sae->tmp->ec && sae_derive_pwe_ecc(sae, addr1, addr2, password,
605 (sae->tmp->dh && sae_derive_pwe_ffc(sae, addr1, addr2, password,
608 sae_derive_commit(sae) < 0)
614 static int sae_derive_k_ecc(struct sae_data *sae, u8 *k)
616 struct crypto_ec_point *K;
619 K = crypto_ec_point_init(sae->tmp->ec);
624 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
625 * PEER-COMMIT-ELEMENT)))
626 * If K is identity element (point-at-infinity), reject
627 * k = F(K) (= x coordinate)
630 if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc,
631 sae->peer_commit_scalar, K) < 0 ||
632 crypto_ec_point_add(sae->tmp->ec, K,
633 sae->tmp->peer_commit_element_ecc, K) < 0 ||
634 crypto_ec_point_mul(sae->tmp->ec, K, sae->tmp->sae_rand, K) < 0 ||
635 crypto_ec_point_is_at_infinity(sae->tmp->ec, K) ||
636 crypto_ec_point_to_bin(sae->tmp->ec, K, k, NULL) < 0) {
637 wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
641 wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
645 crypto_ec_point_deinit(K, 1);
650 static int sae_derive_k_ffc(struct sae_data *sae, u8 *k)
652 struct crypto_bignum *K;
655 K = crypto_bignum_init();
660 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
661 * PEER-COMMIT-ELEMENT)))
662 * If K is identity element (one), reject.
663 * k = F(K) (= x coordinate)
666 if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, sae->peer_commit_scalar,
667 sae->tmp->prime, K) < 0 ||
668 crypto_bignum_mulmod(K, sae->tmp->peer_commit_element_ffc,
669 sae->tmp->prime, K) < 0 ||
670 crypto_bignum_exptmod(K, sae->tmp->sae_rand, sae->tmp->prime, K) < 0
672 crypto_bignum_is_one(K) ||
673 crypto_bignum_to_bin(K, k, SAE_MAX_PRIME_LEN, sae->tmp->prime_len) <
675 wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
679 wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len);
683 crypto_bignum_deinit(K, 1);
688 static int sae_derive_keys(struct sae_data *sae, const u8 *k)
690 u8 null_key[SAE_KEYSEED_KEY_LEN], val[SAE_MAX_PRIME_LEN];
691 u8 keyseed[SHA256_MAC_LEN];
692 u8 keys[SAE_KCK_LEN + SAE_PMK_LEN];
693 struct crypto_bignum *tmp;
696 tmp = crypto_bignum_init();
700 /* keyseed = H(<0>32, k)
701 * KCK || PMK = KDF-512(keyseed, "SAE KCK and PMK",
702 * (commit-scalar + peer-commit-scalar) modulo r)
703 * PMKID = L((commit-scalar + peer-commit-scalar) modulo r, 0, 128)
706 os_memset(null_key, 0, sizeof(null_key));
707 hmac_sha256(null_key, sizeof(null_key), k, sae->tmp->prime_len,
709 wpa_hexdump_key(MSG_DEBUG, "SAE: keyseed", keyseed, sizeof(keyseed));
711 crypto_bignum_add(sae->tmp->own_commit_scalar, sae->peer_commit_scalar,
713 crypto_bignum_mod(tmp, sae->tmp->order, tmp);
714 /* IEEE Std 802.11-2016 is not exactly clear on the encoding of the bit
715 * string that is needed for KCK, PMK, and PMKID derivation, but it
716 * seems to make most sense to encode the
717 * (commit-scalar + peer-commit-scalar) mod r part as a bit string by
718 * zero padding it from left to the length of the order (in full
720 crypto_bignum_to_bin(tmp, val, sizeof(val), sae->tmp->order_len);
721 wpa_hexdump(MSG_DEBUG, "SAE: PMKID", val, SAE_PMKID_LEN);
722 if (sha256_prf(keyseed, sizeof(keyseed), "SAE KCK and PMK",
723 val, sae->tmp->order_len, keys, sizeof(keys)) < 0)
725 os_memset(keyseed, 0, sizeof(keyseed));
726 os_memcpy(sae->tmp->kck, keys, SAE_KCK_LEN);
727 os_memcpy(sae->pmk, keys + SAE_KCK_LEN, SAE_PMK_LEN);
728 os_memcpy(sae->pmkid, val, SAE_PMKID_LEN);
729 os_memset(keys, 0, sizeof(keys));
730 wpa_hexdump_key(MSG_DEBUG, "SAE: KCK", sae->tmp->kck, SAE_KCK_LEN);
731 wpa_hexdump_key(MSG_DEBUG, "SAE: PMK", sae->pmk, SAE_PMK_LEN);
735 crypto_bignum_deinit(tmp, 0);
740 int sae_process_commit(struct sae_data *sae)
742 u8 k[SAE_MAX_PRIME_LEN];
743 if (sae->tmp == NULL ||
744 (sae->tmp->ec && sae_derive_k_ecc(sae, k) < 0) ||
745 (sae->tmp->dh && sae_derive_k_ffc(sae, k) < 0) ||
746 sae_derive_keys(sae, k) < 0)
752 void sae_write_commit(struct sae_data *sae, struct wpabuf *buf,
753 const struct wpabuf *token, const char *identifier)
757 if (sae->tmp == NULL)
760 wpabuf_put_le16(buf, sae->group); /* Finite Cyclic Group */
762 wpabuf_put_buf(buf, token);
763 wpa_hexdump(MSG_DEBUG, "SAE: Anti-clogging token",
764 wpabuf_head(token), wpabuf_len(token));
766 pos = wpabuf_put(buf, sae->tmp->prime_len);
767 crypto_bignum_to_bin(sae->tmp->own_commit_scalar, pos,
768 sae->tmp->prime_len, sae->tmp->prime_len);
769 wpa_hexdump(MSG_DEBUG, "SAE: own commit-scalar",
770 pos, sae->tmp->prime_len);
772 pos = wpabuf_put(buf, 2 * sae->tmp->prime_len);
773 crypto_ec_point_to_bin(sae->tmp->ec,
774 sae->tmp->own_commit_element_ecc,
775 pos, pos + sae->tmp->prime_len);
776 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(x)",
777 pos, sae->tmp->prime_len);
778 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(y)",
779 pos + sae->tmp->prime_len, sae->tmp->prime_len);
781 pos = wpabuf_put(buf, sae->tmp->prime_len);
782 crypto_bignum_to_bin(sae->tmp->own_commit_element_ffc, pos,
783 sae->tmp->prime_len, sae->tmp->prime_len);
784 wpa_hexdump(MSG_DEBUG, "SAE: own commit-element",
785 pos, sae->tmp->prime_len);
789 /* Password Identifier element */
790 wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
791 wpabuf_put_u8(buf, 1 + os_strlen(identifier));
792 wpabuf_put_u8(buf, WLAN_EID_EXT_PASSWORD_IDENTIFIER);
793 wpabuf_put_str(buf, identifier);
794 wpa_printf(MSG_DEBUG, "SAE: own Password Identifier: %s",
800 u16 sae_group_allowed(struct sae_data *sae, int *allowed_groups, u16 group)
802 if (allowed_groups) {
804 for (i = 0; allowed_groups[i] > 0; i++) {
805 if (allowed_groups[i] == group)
808 if (allowed_groups[i] != group) {
809 wpa_printf(MSG_DEBUG, "SAE: Proposed group %u not "
810 "enabled in the current configuration",
812 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
816 if (sae->state == SAE_COMMITTED && group != sae->group) {
817 wpa_printf(MSG_DEBUG, "SAE: Do not allow group to be changed");
818 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
821 if (group != sae->group && sae_set_group(sae, group) < 0) {
822 wpa_printf(MSG_DEBUG, "SAE: Unsupported Finite Cyclic Group %u",
824 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
827 if (sae->tmp == NULL) {
828 wpa_printf(MSG_DEBUG, "SAE: Group information not yet initialized");
829 return WLAN_STATUS_UNSPECIFIED_FAILURE;
832 if (sae->tmp->dh && !allowed_groups) {
833 wpa_printf(MSG_DEBUG, "SAE: Do not allow FFC group %u without "
834 "explicit configuration enabling it", group);
835 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
838 return WLAN_STATUS_SUCCESS;
842 static int sae_is_password_id_elem(const u8 *pos, const u8 *end)
844 return end - pos >= 3 &&
845 pos[0] == WLAN_EID_EXTENSION &&
847 end - pos - 2 >= pos[1] &&
848 pos[2] == WLAN_EID_EXT_PASSWORD_IDENTIFIER;
852 static void sae_parse_commit_token(struct sae_data *sae, const u8 **pos,
853 const u8 *end, const u8 **token,
856 size_t scalar_elem_len, tlen;
864 scalar_elem_len = (sae->tmp->ec ? 3 : 2) * sae->tmp->prime_len;
865 if (scalar_elem_len >= (size_t) (end - *pos))
866 return; /* No extra data beyond peer scalar and element */
868 /* It is a bit difficult to parse this now that there is an
869 * optional variable length Anti-Clogging Token field and
870 * optional variable length Password Identifier element in the
871 * frame. We are sending out fixed length Anti-Clogging Token
872 * fields, so use that length as a requirement for the received
873 * token and check for the presence of possible Password
874 * Identifier element based on the element header information.
876 tlen = end - (*pos + scalar_elem_len);
878 if (tlen < SHA256_MAC_LEN) {
879 wpa_printf(MSG_DEBUG,
880 "SAE: Too short optional data (%u octets) to include our Anti-Clogging Token",
881 (unsigned int) tlen);
885 elem = *pos + scalar_elem_len;
886 if (sae_is_password_id_elem(elem, end)) {
887 /* Password Identifier element takes out all available
888 * extra octets, so there can be no Anti-Clogging token in
893 elem += SHA256_MAC_LEN;
894 if (sae_is_password_id_elem(elem, end)) {
895 /* Password Identifier element is included in the end, so
896 * remove its length from the Anti-Clogging token field. */
900 wpa_hexdump(MSG_DEBUG, "SAE: Anti-Clogging Token", *pos, tlen);
909 static u16 sae_parse_commit_scalar(struct sae_data *sae, const u8 **pos,
912 struct crypto_bignum *peer_scalar;
914 if (sae->tmp->prime_len > end - *pos) {
915 wpa_printf(MSG_DEBUG, "SAE: Not enough data for scalar");
916 return WLAN_STATUS_UNSPECIFIED_FAILURE;
919 peer_scalar = crypto_bignum_init_set(*pos, sae->tmp->prime_len);
920 if (peer_scalar == NULL)
921 return WLAN_STATUS_UNSPECIFIED_FAILURE;
924 * IEEE Std 802.11-2012, 11.3.8.6.1: If there is a protocol instance for
925 * the peer and it is in Authenticated state, the new Commit Message
926 * shall be dropped if the peer-scalar is identical to the one used in
927 * the existing protocol instance.
929 if (sae->state == SAE_ACCEPTED && sae->peer_commit_scalar &&
930 crypto_bignum_cmp(sae->peer_commit_scalar, peer_scalar) == 0) {
931 wpa_printf(MSG_DEBUG, "SAE: Do not accept re-use of previous "
932 "peer-commit-scalar");
933 crypto_bignum_deinit(peer_scalar, 0);
934 return WLAN_STATUS_UNSPECIFIED_FAILURE;
938 if (crypto_bignum_is_zero(peer_scalar) ||
939 crypto_bignum_is_one(peer_scalar) ||
940 crypto_bignum_cmp(peer_scalar, sae->tmp->order) >= 0) {
941 wpa_printf(MSG_DEBUG, "SAE: Invalid peer scalar");
942 crypto_bignum_deinit(peer_scalar, 0);
943 return WLAN_STATUS_UNSPECIFIED_FAILURE;
947 crypto_bignum_deinit(sae->peer_commit_scalar, 0);
948 sae->peer_commit_scalar = peer_scalar;
949 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-scalar",
950 *pos, sae->tmp->prime_len);
951 *pos += sae->tmp->prime_len;
953 return WLAN_STATUS_SUCCESS;
957 static u16 sae_parse_commit_element_ecc(struct sae_data *sae, const u8 **pos,
960 u8 prime[SAE_MAX_ECC_PRIME_LEN];
962 if (2 * sae->tmp->prime_len > end - *pos) {
963 wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
965 return WLAN_STATUS_UNSPECIFIED_FAILURE;
968 if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime),
969 sae->tmp->prime_len) < 0)
970 return WLAN_STATUS_UNSPECIFIED_FAILURE;
972 /* element x and y coordinates < p */
973 if (os_memcmp(*pos, prime, sae->tmp->prime_len) >= 0 ||
974 os_memcmp(*pos + sae->tmp->prime_len, prime,
975 sae->tmp->prime_len) >= 0) {
976 wpa_printf(MSG_DEBUG, "SAE: Invalid coordinates in peer "
978 return WLAN_STATUS_UNSPECIFIED_FAILURE;
981 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(x)",
982 *pos, sae->tmp->prime_len);
983 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(y)",
984 *pos + sae->tmp->prime_len, sae->tmp->prime_len);
986 crypto_ec_point_deinit(sae->tmp->peer_commit_element_ecc, 0);
987 sae->tmp->peer_commit_element_ecc =
988 crypto_ec_point_from_bin(sae->tmp->ec, *pos);
989 if (sae->tmp->peer_commit_element_ecc == NULL)
990 return WLAN_STATUS_UNSPECIFIED_FAILURE;
992 if (!crypto_ec_point_is_on_curve(sae->tmp->ec,
993 sae->tmp->peer_commit_element_ecc)) {
994 wpa_printf(MSG_DEBUG, "SAE: Peer element is not on curve");
995 return WLAN_STATUS_UNSPECIFIED_FAILURE;
998 *pos += 2 * sae->tmp->prime_len;
1000 return WLAN_STATUS_SUCCESS;
1004 static u16 sae_parse_commit_element_ffc(struct sae_data *sae, const u8 **pos,
1007 struct crypto_bignum *res, *one;
1008 const u8 one_bin[1] = { 0x01 };
1010 if (sae->tmp->prime_len > end - *pos) {
1011 wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
1013 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1015 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element", *pos,
1016 sae->tmp->prime_len);
1018 crypto_bignum_deinit(sae->tmp->peer_commit_element_ffc, 0);
1019 sae->tmp->peer_commit_element_ffc =
1020 crypto_bignum_init_set(*pos, sae->tmp->prime_len);
1021 if (sae->tmp->peer_commit_element_ffc == NULL)
1022 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1023 /* 1 < element < p - 1 */
1024 res = crypto_bignum_init();
1025 one = crypto_bignum_init_set(one_bin, sizeof(one_bin));
1027 crypto_bignum_sub(sae->tmp->prime, one, res) ||
1028 crypto_bignum_is_zero(sae->tmp->peer_commit_element_ffc) ||
1029 crypto_bignum_is_one(sae->tmp->peer_commit_element_ffc) ||
1030 crypto_bignum_cmp(sae->tmp->peer_commit_element_ffc, res) >= 0) {
1031 crypto_bignum_deinit(res, 0);
1032 crypto_bignum_deinit(one, 0);
1033 wpa_printf(MSG_DEBUG, "SAE: Invalid peer element");
1034 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1036 crypto_bignum_deinit(one, 0);
1038 /* scalar-op(r, ELEMENT) = 1 modulo p */
1039 if (crypto_bignum_exptmod(sae->tmp->peer_commit_element_ffc,
1040 sae->tmp->order, sae->tmp->prime, res) < 0 ||
1041 !crypto_bignum_is_one(res)) {
1042 wpa_printf(MSG_DEBUG, "SAE: Invalid peer element (scalar-op)");
1043 crypto_bignum_deinit(res, 0);
1044 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1046 crypto_bignum_deinit(res, 0);
1048 *pos += sae->tmp->prime_len;
1050 return WLAN_STATUS_SUCCESS;
1054 static u16 sae_parse_commit_element(struct sae_data *sae, const u8 **pos,
1058 return sae_parse_commit_element_ffc(sae, pos, end);
1059 return sae_parse_commit_element_ecc(sae, pos, end);
1063 static int sae_parse_password_identifier(struct sae_data *sae,
1064 const u8 *pos, const u8 *end)
1066 wpa_hexdump(MSG_DEBUG, "SAE: Possible elements at the end of the frame",
1068 if (!sae_is_password_id_elem(pos, end)) {
1069 if (sae->tmp->pw_id) {
1070 wpa_printf(MSG_DEBUG,
1071 "SAE: No Password Identifier included, but expected one (%s)",
1073 return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
1075 os_free(sae->tmp->pw_id);
1076 sae->tmp->pw_id = NULL;
1077 return WLAN_STATUS_SUCCESS; /* No Password Identifier */
1080 if (sae->tmp->pw_id &&
1081 (pos[1] - 1 != (int) os_strlen(sae->tmp->pw_id) ||
1082 os_memcmp(sae->tmp->pw_id, pos + 3, pos[1] - 1) != 0)) {
1083 wpa_printf(MSG_DEBUG,
1084 "SAE: The included Password Identifier does not match the expected one (%s)",
1086 return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
1089 os_free(sae->tmp->pw_id);
1090 sae->tmp->pw_id = os_malloc(pos[1]);
1091 if (!sae->tmp->pw_id)
1092 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1093 os_memcpy(sae->tmp->pw_id, pos + 3, pos[1] - 1);
1094 sae->tmp->pw_id[pos[1] - 1] = '\0';
1095 wpa_hexdump_ascii(MSG_DEBUG, "SAE: Received Password Identifier",
1096 sae->tmp->pw_id, pos[1] - 1);
1097 return WLAN_STATUS_SUCCESS;
1101 u16 sae_parse_commit(struct sae_data *sae, const u8 *data, size_t len,
1102 const u8 **token, size_t *token_len, int *allowed_groups)
1104 const u8 *pos = data, *end = data + len;
1107 /* Check Finite Cyclic Group */
1109 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1110 res = sae_group_allowed(sae, allowed_groups, WPA_GET_LE16(pos));
1111 if (res != WLAN_STATUS_SUCCESS)
1115 /* Optional Anti-Clogging Token */
1116 sae_parse_commit_token(sae, &pos, end, token, token_len);
1119 res = sae_parse_commit_scalar(sae, &pos, end);
1120 if (res != WLAN_STATUS_SUCCESS)
1123 /* commit-element */
1124 res = sae_parse_commit_element(sae, &pos, end);
1125 if (res != WLAN_STATUS_SUCCESS)
1128 /* Optional Password Identifier element */
1129 res = sae_parse_password_identifier(sae, pos, end);
1130 if (res != WLAN_STATUS_SUCCESS)
1134 * Check whether peer-commit-scalar and PEER-COMMIT-ELEMENT are same as
1135 * the values we sent which would be evidence of a reflection attack.
1137 if (!sae->tmp->own_commit_scalar ||
1138 crypto_bignum_cmp(sae->tmp->own_commit_scalar,
1139 sae->peer_commit_scalar) != 0 ||
1141 (!sae->tmp->own_commit_element_ffc ||
1142 crypto_bignum_cmp(sae->tmp->own_commit_element_ffc,
1143 sae->tmp->peer_commit_element_ffc) != 0)) ||
1145 (!sae->tmp->own_commit_element_ecc ||
1146 crypto_ec_point_cmp(sae->tmp->ec,
1147 sae->tmp->own_commit_element_ecc,
1148 sae->tmp->peer_commit_element_ecc) != 0)))
1149 return WLAN_STATUS_SUCCESS; /* scalars/elements are different */
1152 * This is a reflection attack - return special value to trigger caller
1153 * to silently discard the frame instead of replying with a specific
1156 return SAE_SILENTLY_DISCARD;
1160 static void sae_cn_confirm(struct sae_data *sae, const u8 *sc,
1161 const struct crypto_bignum *scalar1,
1162 const u8 *element1, size_t element1_len,
1163 const struct crypto_bignum *scalar2,
1164 const u8 *element2, size_t element2_len,
1169 u8 scalar_b1[SAE_MAX_PRIME_LEN], scalar_b2[SAE_MAX_PRIME_LEN];
1172 * CN(key, X, Y, Z, ...) =
1173 * HMAC-SHA256(key, D2OS(X) || D2OS(Y) || D2OS(Z) | ...)
1174 * confirm = CN(KCK, send-confirm, commit-scalar, COMMIT-ELEMENT,
1175 * peer-commit-scalar, PEER-COMMIT-ELEMENT)
1176 * verifier = CN(KCK, peer-send-confirm, peer-commit-scalar,
1177 * PEER-COMMIT-ELEMENT, commit-scalar, COMMIT-ELEMENT)
1181 crypto_bignum_to_bin(scalar1, scalar_b1, sizeof(scalar_b1),
1182 sae->tmp->prime_len);
1183 addr[1] = scalar_b1;
1184 len[1] = sae->tmp->prime_len;
1186 len[2] = element1_len;
1187 crypto_bignum_to_bin(scalar2, scalar_b2, sizeof(scalar_b2),
1188 sae->tmp->prime_len);
1189 addr[3] = scalar_b2;
1190 len[3] = sae->tmp->prime_len;
1192 len[4] = element2_len;
1193 hmac_sha256_vector(sae->tmp->kck, sizeof(sae->tmp->kck), 5, addr, len,
1198 static void sae_cn_confirm_ecc(struct sae_data *sae, const u8 *sc,
1199 const struct crypto_bignum *scalar1,
1200 const struct crypto_ec_point *element1,
1201 const struct crypto_bignum *scalar2,
1202 const struct crypto_ec_point *element2,
1205 u8 element_b1[2 * SAE_MAX_ECC_PRIME_LEN];
1206 u8 element_b2[2 * SAE_MAX_ECC_PRIME_LEN];
1208 crypto_ec_point_to_bin(sae->tmp->ec, element1, element_b1,
1209 element_b1 + sae->tmp->prime_len);
1210 crypto_ec_point_to_bin(sae->tmp->ec, element2, element_b2,
1211 element_b2 + sae->tmp->prime_len);
1213 sae_cn_confirm(sae, sc, scalar1, element_b1, 2 * sae->tmp->prime_len,
1214 scalar2, element_b2, 2 * sae->tmp->prime_len, confirm);
1218 static void sae_cn_confirm_ffc(struct sae_data *sae, const u8 *sc,
1219 const struct crypto_bignum *scalar1,
1220 const struct crypto_bignum *element1,
1221 const struct crypto_bignum *scalar2,
1222 const struct crypto_bignum *element2,
1225 u8 element_b1[SAE_MAX_PRIME_LEN];
1226 u8 element_b2[SAE_MAX_PRIME_LEN];
1228 crypto_bignum_to_bin(element1, element_b1, sizeof(element_b1),
1229 sae->tmp->prime_len);
1230 crypto_bignum_to_bin(element2, element_b2, sizeof(element_b2),
1231 sae->tmp->prime_len);
1233 sae_cn_confirm(sae, sc, scalar1, element_b1, sae->tmp->prime_len,
1234 scalar2, element_b2, sae->tmp->prime_len, confirm);
1238 void sae_write_confirm(struct sae_data *sae, struct wpabuf *buf)
1242 if (sae->tmp == NULL)
1246 sc = wpabuf_put(buf, 0);
1247 wpabuf_put_le16(buf, sae->send_confirm);
1248 if (sae->send_confirm < 0xffff)
1249 sae->send_confirm++;
1252 sae_cn_confirm_ecc(sae, sc, sae->tmp->own_commit_scalar,
1253 sae->tmp->own_commit_element_ecc,
1254 sae->peer_commit_scalar,
1255 sae->tmp->peer_commit_element_ecc,
1256 wpabuf_put(buf, SHA256_MAC_LEN));
1258 sae_cn_confirm_ffc(sae, sc, sae->tmp->own_commit_scalar,
1259 sae->tmp->own_commit_element_ffc,
1260 sae->peer_commit_scalar,
1261 sae->tmp->peer_commit_element_ffc,
1262 wpabuf_put(buf, SHA256_MAC_LEN));
1266 int sae_check_confirm(struct sae_data *sae, const u8 *data, size_t len)
1268 u8 verifier[SHA256_MAC_LEN];
1270 if (len < 2 + SHA256_MAC_LEN) {
1271 wpa_printf(MSG_DEBUG, "SAE: Too short confirm message");
1275 wpa_printf(MSG_DEBUG, "SAE: peer-send-confirm %u", WPA_GET_LE16(data));
1277 if (!sae->tmp || !sae->peer_commit_scalar ||
1278 !sae->tmp->own_commit_scalar) {
1279 wpa_printf(MSG_DEBUG, "SAE: Temporary data not yet available");
1284 if (!sae->tmp->peer_commit_element_ecc ||
1285 !sae->tmp->own_commit_element_ecc)
1287 sae_cn_confirm_ecc(sae, data, sae->peer_commit_scalar,
1288 sae->tmp->peer_commit_element_ecc,
1289 sae->tmp->own_commit_scalar,
1290 sae->tmp->own_commit_element_ecc,
1293 if (!sae->tmp->peer_commit_element_ffc ||
1294 !sae->tmp->own_commit_element_ffc)
1296 sae_cn_confirm_ffc(sae, data, sae->peer_commit_scalar,
1297 sae->tmp->peer_commit_element_ffc,
1298 sae->tmp->own_commit_scalar,
1299 sae->tmp->own_commit_element_ffc,
1303 if (os_memcmp_const(verifier, data + 2, SHA256_MAC_LEN) != 0) {
1304 wpa_printf(MSG_DEBUG, "SAE: Confirm mismatch");
1305 wpa_hexdump(MSG_DEBUG, "SAE: Received confirm",
1306 data + 2, SHA256_MAC_LEN);
1307 wpa_hexdump(MSG_DEBUG, "SAE: Calculated verifier",
1308 verifier, SHA256_MAC_LEN);
1316 const char * sae_state_txt(enum sae_state state)