Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Simultaneous authentication of equals |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2016, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 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" |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 12 | #include "common/defs.h" |
| 13 | #include "common/wpa_common.h" |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 14 | #include "utils/const_time.h" |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 15 | #include "crypto/crypto.h" |
| 16 | #include "crypto/sha256.h" |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 17 | #include "crypto/sha384.h" |
| 18 | #include "crypto/sha512.h" |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 19 | #include "crypto/random.h" |
| 20 | #include "crypto/dh_groups.h" |
| 21 | #include "ieee802_11_defs.h" |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 22 | #include "dragonfly.h" |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 23 | #include "sae.h" |
| 24 | |
| 25 | |
| 26 | int sae_set_group(struct sae_data *sae, int group) |
| 27 | { |
| 28 | struct sae_temporary_data *tmp; |
| 29 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 30 | #ifdef CONFIG_TESTING_OPTIONS |
| 31 | /* Allow all groups for testing purposes in non-production builds. */ |
| 32 | #else /* CONFIG_TESTING_OPTIONS */ |
| 33 | if (!dragonfly_suitable_group(group, 0)) { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 34 | wpa_printf(MSG_DEBUG, "SAE: Reject unsuitable group %d", group); |
| 35 | return -1; |
| 36 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 37 | #endif /* CONFIG_TESTING_OPTIONS */ |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 38 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 39 | sae_clear_data(sae); |
| 40 | tmp = sae->tmp = os_zalloc(sizeof(*tmp)); |
| 41 | if (tmp == NULL) |
| 42 | return -1; |
| 43 | |
| 44 | /* First, check if this is an ECC group */ |
| 45 | tmp->ec = crypto_ec_init(group); |
| 46 | if (tmp->ec) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 47 | wpa_printf(MSG_DEBUG, "SAE: Selecting supported ECC group %d", |
| 48 | group); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 49 | sae->group = group; |
| 50 | tmp->prime_len = crypto_ec_prime_len(tmp->ec); |
| 51 | tmp->prime = crypto_ec_get_prime(tmp->ec); |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 52 | tmp->order_len = crypto_ec_order_len(tmp->ec); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 53 | tmp->order = crypto_ec_get_order(tmp->ec); |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | /* Not an ECC group, check FFC */ |
| 58 | tmp->dh = dh_groups_get(group); |
| 59 | if (tmp->dh) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 60 | wpa_printf(MSG_DEBUG, "SAE: Selecting supported FFC group %d", |
| 61 | group); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 62 | sae->group = group; |
| 63 | tmp->prime_len = tmp->dh->prime_len; |
| 64 | if (tmp->prime_len > SAE_MAX_PRIME_LEN) { |
| 65 | sae_clear_data(sae); |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | tmp->prime_buf = crypto_bignum_init_set(tmp->dh->prime, |
| 70 | tmp->prime_len); |
| 71 | if (tmp->prime_buf == NULL) { |
| 72 | sae_clear_data(sae); |
| 73 | return -1; |
| 74 | } |
| 75 | tmp->prime = tmp->prime_buf; |
| 76 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 77 | tmp->order_len = tmp->dh->order_len; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 78 | tmp->order_buf = crypto_bignum_init_set(tmp->dh->order, |
| 79 | tmp->dh->order_len); |
| 80 | if (tmp->order_buf == NULL) { |
| 81 | sae_clear_data(sae); |
| 82 | return -1; |
| 83 | } |
| 84 | tmp->order = tmp->order_buf; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /* Unsupported group */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 90 | wpa_printf(MSG_DEBUG, |
| 91 | "SAE: Group %d not supported by the crypto library", group); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void sae_clear_temp_data(struct sae_data *sae) |
| 97 | { |
| 98 | struct sae_temporary_data *tmp; |
| 99 | if (sae == NULL || sae->tmp == NULL) |
| 100 | return; |
| 101 | tmp = sae->tmp; |
| 102 | crypto_ec_deinit(tmp->ec); |
| 103 | crypto_bignum_deinit(tmp->prime_buf, 0); |
| 104 | crypto_bignum_deinit(tmp->order_buf, 0); |
| 105 | crypto_bignum_deinit(tmp->sae_rand, 1); |
| 106 | crypto_bignum_deinit(tmp->pwe_ffc, 1); |
| 107 | crypto_bignum_deinit(tmp->own_commit_scalar, 0); |
| 108 | crypto_bignum_deinit(tmp->own_commit_element_ffc, 0); |
| 109 | crypto_bignum_deinit(tmp->peer_commit_element_ffc, 0); |
| 110 | crypto_ec_point_deinit(tmp->pwe_ecc, 1); |
| 111 | crypto_ec_point_deinit(tmp->own_commit_element_ecc, 0); |
| 112 | crypto_ec_point_deinit(tmp->peer_commit_element_ecc, 0); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 113 | wpabuf_free(tmp->anti_clogging_token); |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 114 | wpabuf_free(tmp->own_rejected_groups); |
| 115 | wpabuf_free(tmp->peer_rejected_groups); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 116 | os_free(tmp->pw_id); |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 117 | os_free(tmp->parsed_pw_id); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 118 | bin_clear_free(tmp, sizeof(*tmp)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 119 | sae->tmp = NULL; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void sae_clear_data(struct sae_data *sae) |
| 124 | { |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 125 | unsigned int no_pw_id; |
| 126 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 127 | if (sae == NULL) |
| 128 | return; |
| 129 | sae_clear_temp_data(sae); |
| 130 | crypto_bignum_deinit(sae->peer_commit_scalar, 0); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 131 | crypto_bignum_deinit(sae->peer_commit_scalar_accepted, 0); |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 132 | no_pw_id = sae->no_pw_id; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 133 | os_memset(sae, 0, sizeof(*sae)); |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 134 | sae->no_pw_id = no_pw_id; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 138 | static void sae_pwd_seed_key(const u8 *addr1, const u8 *addr2, u8 *key) |
| 139 | { |
| 140 | wpa_printf(MSG_DEBUG, "SAE: PWE derivation - addr1=" MACSTR |
| 141 | " addr2=" MACSTR, MAC2STR(addr1), MAC2STR(addr2)); |
| 142 | if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) { |
| 143 | os_memcpy(key, addr1, ETH_ALEN); |
| 144 | os_memcpy(key + ETH_ALEN, addr2, ETH_ALEN); |
| 145 | } else { |
| 146 | os_memcpy(key, addr2, ETH_ALEN); |
| 147 | os_memcpy(key + ETH_ALEN, addr1, ETH_ALEN); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 152 | static int sae_test_pwd_seed_ecc(struct sae_data *sae, const u8 *pwd_seed, |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 153 | const u8 *prime, const u8 *qr, const u8 *qnr, |
| 154 | u8 *pwd_value) |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 155 | { |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 156 | struct crypto_bignum *y_sqr, *x_cand; |
| 157 | int res; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 158 | size_t bits; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 159 | int cmp_prime; |
| 160 | unsigned int in_range; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 161 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 162 | wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN); |
| 163 | |
| 164 | /* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */ |
| 165 | bits = crypto_ec_prime_len_bits(sae->tmp->ec); |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 166 | if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking", |
| 167 | prime, sae->tmp->prime_len, pwd_value, bits) < 0) |
| 168 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 169 | if (bits % 8) |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 170 | buf_shift_right(pwd_value, sae->tmp->prime_len, 8 - bits % 8); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 171 | wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value", |
| 172 | pwd_value, sae->tmp->prime_len); |
| 173 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 174 | cmp_prime = const_time_memcmp(pwd_value, prime, sae->tmp->prime_len); |
| 175 | /* Create a const_time mask for selection based on prf result |
| 176 | * being smaller than prime. */ |
| 177 | in_range = const_time_fill_msb((unsigned int) cmp_prime); |
| 178 | /* The algorithm description would skip the next steps if |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 179 | * cmp_prime >= 0 (return 0 here), but go through them regardless to |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 180 | * minimize externally observable differences in behavior. */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 181 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 182 | x_cand = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len); |
| 183 | if (!x_cand) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 184 | return -1; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 185 | y_sqr = crypto_ec_point_compute_y_sqr(sae->tmp->ec, x_cand); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 186 | crypto_bignum_deinit(x_cand, 1); |
| 187 | if (!y_sqr) |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 188 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 189 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 190 | res = dragonfly_is_quadratic_residue_blind(sae->tmp->ec, qr, qnr, |
| 191 | y_sqr); |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 192 | crypto_bignum_deinit(y_sqr, 1); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 193 | if (res < 0) |
| 194 | return res; |
| 195 | return const_time_select_int(in_range, res, 0); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 199 | /* Returns -1 on fatal failure, 0 if PWE cannot be derived from the provided |
| 200 | * pwd-seed, or 1 if a valid PWE was derived from pwd-seed. */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 201 | static int sae_test_pwd_seed_ffc(struct sae_data *sae, const u8 *pwd_seed, |
| 202 | struct crypto_bignum *pwe) |
| 203 | { |
| 204 | u8 pwd_value[SAE_MAX_PRIME_LEN]; |
| 205 | size_t bits = sae->tmp->prime_len * 8; |
| 206 | u8 exp[1]; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 207 | struct crypto_bignum *a, *b = NULL; |
| 208 | int res, is_val; |
| 209 | u8 pwd_value_valid; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 210 | |
| 211 | wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, SHA256_MAC_LEN); |
| 212 | |
| 213 | /* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */ |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 214 | if (sha256_prf_bits(pwd_seed, SHA256_MAC_LEN, "SAE Hunting and Pecking", |
| 215 | sae->tmp->dh->prime, sae->tmp->prime_len, pwd_value, |
| 216 | bits) < 0) |
| 217 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 218 | wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value", pwd_value, |
| 219 | sae->tmp->prime_len); |
| 220 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 221 | /* Check whether pwd-value < p */ |
| 222 | res = const_time_memcmp(pwd_value, sae->tmp->dh->prime, |
| 223 | sae->tmp->prime_len); |
| 224 | /* pwd-value >= p is invalid, so res is < 0 for the valid cases and |
| 225 | * the negative sign can be used to fill the mask for constant time |
| 226 | * selection */ |
| 227 | pwd_value_valid = const_time_fill_msb(res); |
| 228 | |
| 229 | /* If pwd-value >= p, force pwd-value to be < p and perform the |
| 230 | * calculations anyway to hide timing difference. The derived PWE will |
| 231 | * be ignored in that case. */ |
| 232 | pwd_value[0] = const_time_select_u8(pwd_value_valid, pwd_value[0], 0); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 233 | |
| 234 | /* PWE = pwd-value^((p-1)/r) modulo p */ |
| 235 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 236 | res = -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 237 | a = crypto_bignum_init_set(pwd_value, sae->tmp->prime_len); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 238 | if (!a) |
| 239 | goto fail; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 240 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 241 | /* This is an optimization based on the used group that does not depend |
| 242 | * on the password in any way, so it is fine to use separate branches |
| 243 | * for this step without constant time operations. */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 244 | if (sae->tmp->dh->safe_prime) { |
| 245 | /* |
| 246 | * r = (p-1)/2 for the group used here, so this becomes: |
| 247 | * PWE = pwd-value^2 modulo p |
| 248 | */ |
| 249 | exp[0] = 2; |
| 250 | b = crypto_bignum_init_set(exp, sizeof(exp)); |
| 251 | } else { |
| 252 | /* Calculate exponent: (p-1)/r */ |
| 253 | exp[0] = 1; |
| 254 | b = crypto_bignum_init_set(exp, sizeof(exp)); |
| 255 | if (b == NULL || |
| 256 | crypto_bignum_sub(sae->tmp->prime, b, b) < 0 || |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 257 | crypto_bignum_div(b, sae->tmp->order, b) < 0) |
| 258 | goto fail; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 261 | if (!b) |
| 262 | goto fail; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 263 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 264 | res = crypto_bignum_exptmod(a, b, sae->tmp->prime, pwe); |
| 265 | if (res < 0) |
| 266 | goto fail; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 267 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 268 | /* There were no fatal errors in calculations, so determine the return |
| 269 | * value using constant time operations. We get here for number of |
| 270 | * invalid cases which are cleared here after having performed all the |
| 271 | * computation. PWE is valid if pwd-value was less than prime and |
| 272 | * PWE > 1. Start with pwd-value check first and then use constant time |
| 273 | * operations to clear res to 0 if PWE is 0 or 1. |
| 274 | */ |
| 275 | res = const_time_select_u8(pwd_value_valid, 1, 0); |
| 276 | is_val = crypto_bignum_is_zero(pwe); |
| 277 | res = const_time_select_u8(const_time_is_zero(is_val), res, 0); |
| 278 | is_val = crypto_bignum_is_one(pwe); |
| 279 | res = const_time_select_u8(const_time_is_zero(is_val), res, 0); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 280 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 281 | fail: |
| 282 | crypto_bignum_deinit(a, 1); |
| 283 | crypto_bignum_deinit(b, 1); |
| 284 | return res; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
| 288 | static int sae_derive_pwe_ecc(struct sae_data *sae, const u8 *addr1, |
| 289 | const u8 *addr2, const u8 *password, |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 290 | size_t password_len) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 291 | { |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 292 | u8 counter, k; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 293 | u8 addrs[2 * ETH_ALEN]; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 294 | const u8 *addr[2]; |
| 295 | size_t len[2]; |
| 296 | u8 *stub_password, *tmp_password; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 297 | int pwd_seed_odd = 0; |
| 298 | u8 prime[SAE_MAX_ECC_PRIME_LEN]; |
| 299 | size_t prime_len; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 300 | struct crypto_bignum *x = NULL, *y = NULL, *qr = NULL, *qnr = NULL; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 301 | u8 x_bin[SAE_MAX_ECC_PRIME_LEN]; |
| 302 | u8 x_cand_bin[SAE_MAX_ECC_PRIME_LEN]; |
| 303 | u8 qr_bin[SAE_MAX_ECC_PRIME_LEN]; |
| 304 | u8 qnr_bin[SAE_MAX_ECC_PRIME_LEN]; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 305 | u8 x_y[2 * SAE_MAX_ECC_PRIME_LEN]; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 306 | int res = -1; |
| 307 | u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_* |
| 308 | * mask */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 309 | unsigned int is_eq; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 310 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 311 | os_memset(x_bin, 0, sizeof(x_bin)); |
| 312 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 313 | stub_password = os_malloc(password_len); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 314 | tmp_password = os_malloc(password_len); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 315 | if (!stub_password || !tmp_password || |
| 316 | random_get_bytes(stub_password, password_len) < 0) |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 317 | goto fail; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 318 | |
| 319 | prime_len = sae->tmp->prime_len; |
| 320 | if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime), |
| 321 | prime_len) < 0) |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 322 | goto fail; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 323 | |
| 324 | /* |
| 325 | * Create a random quadratic residue (qr) and quadratic non-residue |
| 326 | * (qnr) modulo p for blinding purposes during the loop. |
| 327 | */ |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 328 | if (dragonfly_get_random_qr_qnr(sae->tmp->prime, &qr, &qnr) < 0 || |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 329 | crypto_bignum_to_bin(qr, qr_bin, sizeof(qr_bin), prime_len) < 0 || |
| 330 | crypto_bignum_to_bin(qnr, qnr_bin, sizeof(qnr_bin), prime_len) < 0) |
| 331 | goto fail; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 332 | |
| 333 | wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password", |
| 334 | password, password_len); |
| 335 | |
| 336 | /* |
| 337 | * H(salt, ikm) = HMAC-SHA256(salt, ikm) |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 338 | * base = password |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 339 | * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC), |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 340 | * base || counter) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 341 | */ |
| 342 | sae_pwd_seed_key(addr1, addr2, addrs); |
| 343 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 344 | addr[0] = tmp_password; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 345 | len[0] = password_len; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 346 | addr[1] = &counter; |
| 347 | len[1] = sizeof(counter); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 348 | |
| 349 | /* |
| 350 | * Continue for at least k iterations to protect against side-channel |
| 351 | * attacks that attempt to determine the number of iterations required |
| 352 | * in the loop. |
| 353 | */ |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 354 | k = dragonfly_min_pwe_loop_iter(sae->group); |
| 355 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 356 | for (counter = 1; counter <= k || !found; counter++) { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 357 | u8 pwd_seed[SHA256_MAC_LEN]; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 358 | |
| 359 | if (counter > 200) { |
| 360 | /* This should not happen in practice */ |
| 361 | wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE"); |
| 362 | break; |
| 363 | } |
| 364 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 365 | wpa_printf(MSG_DEBUG, "SAE: counter = %03u", counter); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 366 | const_time_select_bin(found, stub_password, password, |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 367 | password_len, tmp_password); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 368 | if (hmac_sha256_vector(addrs, sizeof(addrs), 2, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 369 | addr, len, pwd_seed) < 0) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 370 | break; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 371 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 372 | res = sae_test_pwd_seed_ecc(sae, pwd_seed, |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 373 | prime, qr_bin, qnr_bin, x_cand_bin); |
| 374 | const_time_select_bin(found, x_bin, x_cand_bin, prime_len, |
| 375 | x_bin); |
| 376 | pwd_seed_odd = const_time_select_u8( |
| 377 | found, pwd_seed_odd, |
| 378 | pwd_seed[SHA256_MAC_LEN - 1] & 0x01); |
| 379 | os_memset(pwd_seed, 0, sizeof(pwd_seed)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 380 | if (res < 0) |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 381 | goto fail; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 382 | /* Need to minimize differences in handling res == 0 and 1 here |
| 383 | * to avoid differences in timing and instruction cache access, |
| 384 | * so use const_time_select_*() to make local copies of the |
| 385 | * values based on whether this loop iteration was the one that |
| 386 | * found the pwd-seed/x. */ |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 387 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 388 | /* found is 0 or 0xff here and res is 0 or 1. Bitwise OR of them |
| 389 | * (with res converted to 0/0xff) handles this in constant time. |
| 390 | */ |
| 391 | found |= res * 0xff; |
| 392 | wpa_printf(MSG_DEBUG, "SAE: pwd-seed result %d found=0x%02x", |
| 393 | res, found); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 396 | if (!found) { |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 397 | wpa_printf(MSG_DEBUG, "SAE: Could not generate PWE"); |
| 398 | res = -1; |
| 399 | goto fail; |
| 400 | } |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 401 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 402 | x = crypto_bignum_init_set(x_bin, prime_len); |
| 403 | if (!x) { |
| 404 | res = -1; |
| 405 | goto fail; |
| 406 | } |
| 407 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 408 | /* y = sqrt(x^3 + ax + b) mod p |
| 409 | * if LSB(save) == LSB(y): PWE = (x, y) |
| 410 | * else: PWE = (x, p - y) |
| 411 | * |
| 412 | * Calculate y and the two possible values for PWE and after that, |
| 413 | * use constant time selection to copy the correct alternative. |
| 414 | */ |
| 415 | y = crypto_ec_point_compute_y_sqr(sae->tmp->ec, x); |
| 416 | if (!y || |
| 417 | dragonfly_sqrt(sae->tmp->ec, y, y) < 0 || |
| 418 | crypto_bignum_to_bin(y, x_y, SAE_MAX_ECC_PRIME_LEN, |
| 419 | prime_len) < 0 || |
| 420 | crypto_bignum_sub(sae->tmp->prime, y, y) < 0 || |
| 421 | crypto_bignum_to_bin(y, x_y + SAE_MAX_ECC_PRIME_LEN, |
| 422 | SAE_MAX_ECC_PRIME_LEN, prime_len) < 0) { |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 423 | wpa_printf(MSG_DEBUG, "SAE: Could not solve y"); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 424 | goto fail; |
| 425 | } |
| 426 | |
| 427 | is_eq = const_time_eq(pwd_seed_odd, x_y[prime_len - 1] & 0x01); |
| 428 | const_time_select_bin(is_eq, x_y, x_y + SAE_MAX_ECC_PRIME_LEN, |
| 429 | prime_len, x_y + prime_len); |
| 430 | os_memcpy(x_y, x_bin, prime_len); |
| 431 | wpa_hexdump_key(MSG_DEBUG, "SAE: PWE", x_y, 2 * prime_len); |
| 432 | crypto_ec_point_deinit(sae->tmp->pwe_ecc, 1); |
| 433 | sae->tmp->pwe_ecc = crypto_ec_point_from_bin(sae->tmp->ec, x_y); |
| 434 | if (!sae->tmp->pwe_ecc) { |
| 435 | wpa_printf(MSG_DEBUG, "SAE: Could not generate PWE"); |
| 436 | res = -1; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | fail: |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 440 | forced_memzero(x_y, sizeof(x_y)); |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 441 | crypto_bignum_deinit(qr, 0); |
| 442 | crypto_bignum_deinit(qnr, 0); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 443 | crypto_bignum_deinit(y, 1); |
| 444 | os_free(stub_password); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 445 | bin_clear_free(tmp_password, password_len); |
| 446 | crypto_bignum_deinit(x, 1); |
| 447 | os_memset(x_bin, 0, sizeof(x_bin)); |
| 448 | os_memset(x_cand_bin, 0, sizeof(x_cand_bin)); |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 449 | |
| 450 | return res; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | |
| 454 | static int sae_derive_pwe_ffc(struct sae_data *sae, const u8 *addr1, |
| 455 | const u8 *addr2, const u8 *password, |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 456 | size_t password_len) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 457 | { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 458 | u8 counter, k, sel_counter = 0; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 459 | u8 addrs[2 * ETH_ALEN]; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 460 | const u8 *addr[2]; |
| 461 | size_t len[2]; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 462 | u8 found = 0; /* 0 (false) or 0xff (true) to be used as const_time_* |
| 463 | * mask */ |
| 464 | u8 mask; |
| 465 | struct crypto_bignum *pwe; |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 466 | size_t prime_len = sae->tmp->prime_len; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 467 | u8 *pwe_buf; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 468 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 469 | crypto_bignum_deinit(sae->tmp->pwe_ffc, 1); |
| 470 | sae->tmp->pwe_ffc = NULL; |
| 471 | |
| 472 | /* Allocate a buffer to maintain selected and candidate PWE for constant |
| 473 | * time selection. */ |
| 474 | pwe_buf = os_zalloc(prime_len * 2); |
| 475 | pwe = crypto_bignum_init(); |
| 476 | if (!pwe_buf || !pwe) |
| 477 | goto fail; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 478 | |
| 479 | wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password", |
| 480 | password, password_len); |
| 481 | |
| 482 | /* |
| 483 | * H(salt, ikm) = HMAC-SHA256(salt, ikm) |
| 484 | * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC), |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 485 | * password || counter) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 486 | */ |
| 487 | sae_pwd_seed_key(addr1, addr2, addrs); |
| 488 | |
| 489 | addr[0] = password; |
| 490 | len[0] = password_len; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 491 | addr[1] = &counter; |
| 492 | len[1] = sizeof(counter); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 493 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 494 | k = dragonfly_min_pwe_loop_iter(sae->group); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 495 | |
| 496 | for (counter = 1; counter <= k || !found; counter++) { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 497 | u8 pwd_seed[SHA256_MAC_LEN]; |
| 498 | int res; |
| 499 | |
| 500 | if (counter > 200) { |
| 501 | /* This should not happen in practice */ |
| 502 | wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE"); |
| 503 | break; |
| 504 | } |
| 505 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 506 | wpa_printf(MSG_DEBUG, "SAE: counter = %02u", counter); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 507 | if (hmac_sha256_vector(addrs, sizeof(addrs), 2, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 508 | addr, len, pwd_seed) < 0) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 509 | break; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 510 | res = sae_test_pwd_seed_ffc(sae, pwd_seed, pwe); |
| 511 | /* res is -1 for fatal failure, 0 if a valid PWE was not found, |
| 512 | * or 1 if a valid PWE was found. */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 513 | if (res < 0) |
| 514 | break; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 515 | /* Store the candidate PWE into the second half of pwe_buf and |
| 516 | * the selected PWE in the beginning of pwe_buf using constant |
| 517 | * time selection. */ |
| 518 | if (crypto_bignum_to_bin(pwe, pwe_buf + prime_len, prime_len, |
| 519 | prime_len) < 0) |
| 520 | break; |
| 521 | const_time_select_bin(found, pwe_buf, pwe_buf + prime_len, |
| 522 | prime_len, pwe_buf); |
| 523 | sel_counter = const_time_select_u8(found, sel_counter, counter); |
| 524 | mask = const_time_eq_u8(res, 1); |
| 525 | found = const_time_select_u8(found, found, mask); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 528 | if (!found) |
| 529 | goto fail; |
| 530 | |
| 531 | wpa_printf(MSG_DEBUG, "SAE: Use PWE from counter = %02u", sel_counter); |
| 532 | sae->tmp->pwe_ffc = crypto_bignum_init_set(pwe_buf, prime_len); |
| 533 | fail: |
| 534 | crypto_bignum_deinit(pwe, 1); |
| 535 | bin_clear_free(pwe_buf, prime_len * 2); |
| 536 | return sae->tmp->pwe_ffc ? 0 : -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 540 | static int hkdf_extract(size_t hash_len, const u8 *salt, size_t salt_len, |
| 541 | size_t num_elem, const u8 *addr[], const size_t len[], |
| 542 | u8 *prk) |
| 543 | { |
| 544 | if (hash_len == 32) |
| 545 | return hmac_sha256_vector(salt, salt_len, num_elem, addr, len, |
| 546 | prk); |
| 547 | #ifdef CONFIG_SHA384 |
| 548 | if (hash_len == 48) |
| 549 | return hmac_sha384_vector(salt, salt_len, num_elem, addr, len, |
| 550 | prk); |
| 551 | #endif /* CONFIG_SHA384 */ |
| 552 | #ifdef CONFIG_SHA512 |
| 553 | if (hash_len == 64) |
| 554 | return hmac_sha512_vector(salt, salt_len, num_elem, addr, len, |
| 555 | prk); |
| 556 | #endif /* CONFIG_SHA512 */ |
| 557 | return -1; |
| 558 | } |
| 559 | |
| 560 | |
| 561 | static int hkdf_expand(size_t hash_len, const u8 *prk, size_t prk_len, |
| 562 | const char *info, u8 *okm, size_t okm_len) |
| 563 | { |
| 564 | size_t info_len = os_strlen(info); |
| 565 | |
| 566 | if (hash_len == 32) |
| 567 | return hmac_sha256_kdf(prk, prk_len, NULL, |
| 568 | (const u8 *) info, info_len, |
| 569 | okm, okm_len); |
| 570 | #ifdef CONFIG_SHA384 |
| 571 | if (hash_len == 48) |
| 572 | return hmac_sha384_kdf(prk, prk_len, NULL, |
| 573 | (const u8 *) info, info_len, |
| 574 | okm, okm_len); |
| 575 | #endif /* CONFIG_SHA384 */ |
| 576 | #ifdef CONFIG_SHA512 |
| 577 | if (hash_len == 64) |
| 578 | return hmac_sha512_kdf(prk, prk_len, NULL, |
| 579 | (const u8 *) info, info_len, |
| 580 | okm, okm_len); |
| 581 | #endif /* CONFIG_SHA512 */ |
| 582 | return -1; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | static int sswu_curve_param(int group, int *z) |
| 587 | { |
| 588 | switch (group) { |
| 589 | case 19: |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 590 | *z = -10; |
| 591 | return 0; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 592 | case 20: |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 593 | *z = -12; |
| 594 | return 0; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 595 | case 21: |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 596 | *z = -4; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 597 | return 0; |
| 598 | case 25: |
| 599 | case 29: |
| 600 | *z = -5; |
| 601 | return 0; |
| 602 | case 26: |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 603 | *z = 31; |
| 604 | return 0; |
| 605 | case 28: |
| 606 | *z = -2; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 607 | return 0; |
| 608 | case 30: |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 609 | *z = 7; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 610 | return 0; |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 611 | default: |
| 612 | return -1; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 613 | } |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | |
| 617 | static void debug_print_bignum(const char *title, const struct crypto_bignum *a, |
| 618 | size_t prime_len) |
| 619 | { |
| 620 | u8 *bin; |
| 621 | |
| 622 | bin = os_malloc(prime_len); |
| 623 | if (bin && crypto_bignum_to_bin(a, bin, prime_len, prime_len) >= 0) |
| 624 | wpa_hexdump_key(MSG_DEBUG, title, bin, prime_len); |
| 625 | else |
| 626 | wpa_printf(MSG_DEBUG, "Could not print bignum (%s)", title); |
| 627 | bin_clear_free(bin, prime_len); |
| 628 | } |
| 629 | |
| 630 | |
| 631 | static struct crypto_ec_point * sswu(struct crypto_ec *ec, int group, |
| 632 | const struct crypto_bignum *u) |
| 633 | { |
| 634 | int z_int; |
| 635 | const struct crypto_bignum *a, *b, *prime; |
| 636 | struct crypto_bignum *u2, *t1, *t2, *z, *t, *zero, *one, *two, *three, |
| 637 | *x1a, *x1b, *y = NULL; |
| 638 | struct crypto_bignum *x1 = NULL, *x2, *gx1, *gx2, *v = NULL; |
| 639 | unsigned int m_is_zero, is_qr, is_eq; |
| 640 | size_t prime_len; |
| 641 | u8 bin[SAE_MAX_ECC_PRIME_LEN]; |
| 642 | u8 bin1[SAE_MAX_ECC_PRIME_LEN]; |
| 643 | u8 bin2[SAE_MAX_ECC_PRIME_LEN]; |
| 644 | u8 x_y[2 * SAE_MAX_ECC_PRIME_LEN]; |
| 645 | struct crypto_ec_point *p = NULL; |
| 646 | |
| 647 | if (sswu_curve_param(group, &z_int) < 0) |
| 648 | return NULL; |
| 649 | |
| 650 | prime = crypto_ec_get_prime(ec); |
| 651 | prime_len = crypto_ec_prime_len(ec); |
| 652 | a = crypto_ec_get_a(ec); |
| 653 | b = crypto_ec_get_b(ec); |
| 654 | |
| 655 | u2 = crypto_bignum_init(); |
| 656 | t1 = crypto_bignum_init(); |
| 657 | t2 = crypto_bignum_init(); |
| 658 | z = crypto_bignum_init_uint(abs(z_int)); |
| 659 | t = crypto_bignum_init(); |
| 660 | zero = crypto_bignum_init_uint(0); |
| 661 | one = crypto_bignum_init_uint(1); |
| 662 | two = crypto_bignum_init_uint(2); |
| 663 | three = crypto_bignum_init_uint(3); |
| 664 | x1a = crypto_bignum_init(); |
| 665 | x1b = crypto_bignum_init(); |
| 666 | x2 = crypto_bignum_init(); |
| 667 | gx1 = crypto_bignum_init(); |
| 668 | gx2 = crypto_bignum_init(); |
| 669 | if (!u2 || !t1 || !t2 || !z || !t || !zero || !one || !two || !three || |
| 670 | !x1a || !x1b || !x2 || !gx1 || !gx2) |
| 671 | goto fail; |
| 672 | |
| 673 | if (z_int < 0 && crypto_bignum_sub(prime, z, z) < 0) |
| 674 | goto fail; |
| 675 | |
| 676 | /* m = z^2 * u^4 + z * u^2 */ |
| 677 | /* --> tmp = z * u^2, m = tmp^2 + tmp */ |
| 678 | |
| 679 | /* u2 = u^2 |
| 680 | * t1 = z * u2 |
| 681 | * t2 = t1^2 |
| 682 | * m = t1 = t1 + t2 */ |
| 683 | if (crypto_bignum_sqrmod(u, prime, u2) < 0 || |
| 684 | crypto_bignum_mulmod(z, u2, prime, t1) < 0 || |
| 685 | crypto_bignum_sqrmod(t1, prime, t2) < 0 || |
| 686 | crypto_bignum_addmod(t1, t2, prime, t1) < 0) |
| 687 | goto fail; |
| 688 | debug_print_bignum("SSWU: m", t1, prime_len); |
| 689 | |
| 690 | /* l = CEQ(m, 0) |
| 691 | * t = CSEL(l, 0, inverse(m); where inverse(x) is calculated as |
| 692 | * x^(p-2) modulo p which will handle m == 0 case correctly */ |
| 693 | /* TODO: Make sure crypto_bignum_is_zero() is constant time */ |
| 694 | m_is_zero = const_time_eq(crypto_bignum_is_zero(t1), 1); |
| 695 | /* t = m^(p-2) modulo p */ |
| 696 | if (crypto_bignum_sub(prime, two, t2) < 0 || |
| 697 | crypto_bignum_exptmod(t1, t2, prime, t) < 0) |
| 698 | goto fail; |
| 699 | debug_print_bignum("SSWU: t", t, prime_len); |
| 700 | |
| 701 | /* b / (z * a) */ |
| 702 | if (crypto_bignum_mulmod(z, a, prime, t1) < 0 || |
| 703 | crypto_bignum_inverse(t1, prime, t1) < 0 || |
| 704 | crypto_bignum_mulmod(b, t1, prime, x1a) < 0) |
| 705 | goto fail; |
| 706 | debug_print_bignum("SSWU: x1a = b / (z * a)", x1a, prime_len); |
| 707 | |
| 708 | /* (-b/a) * (1 + t) */ |
| 709 | if (crypto_bignum_sub(prime, b, t1) < 0 || |
| 710 | crypto_bignum_inverse(a, prime, t2) < 0 || |
| 711 | crypto_bignum_mulmod(t1, t2, prime, t1) < 0 || |
| 712 | crypto_bignum_addmod(one, t, prime, t2) < 0 || |
| 713 | crypto_bignum_mulmod(t1, t2, prime, x1b) < 0) |
| 714 | goto fail; |
| 715 | debug_print_bignum("SSWU: x1b = (-b/a) * (1 + t)", x1b, prime_len); |
| 716 | |
| 717 | /* x1 = CSEL(CEQ(m, 0), x1a, x1b) */ |
| 718 | if (crypto_bignum_to_bin(x1a, bin1, sizeof(bin1), prime_len) < 0 || |
| 719 | crypto_bignum_to_bin(x1b, bin2, sizeof(bin2), prime_len) < 0) |
| 720 | goto fail; |
| 721 | const_time_select_bin(m_is_zero, bin1, bin2, prime_len, bin); |
| 722 | x1 = crypto_bignum_init_set(bin, prime_len); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 723 | if (!x1) |
| 724 | goto fail; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 725 | debug_print_bignum("SSWU: x1 = CSEL(l, x1a, x1b)", x1, prime_len); |
| 726 | |
| 727 | /* gx1 = x1^3 + a * x1 + b */ |
| 728 | if (crypto_bignum_exptmod(x1, three, prime, t1) < 0 || |
| 729 | crypto_bignum_mulmod(a, x1, prime, t2) < 0 || |
| 730 | crypto_bignum_addmod(t1, t2, prime, t1) < 0 || |
| 731 | crypto_bignum_addmod(t1, b, prime, gx1) < 0) |
| 732 | goto fail; |
| 733 | debug_print_bignum("SSWU: gx1 = x1^3 + a * x1 + b", gx1, prime_len); |
| 734 | |
| 735 | /* x2 = z * u^2 * x1 */ |
| 736 | if (crypto_bignum_mulmod(z, u2, prime, t1) < 0 || |
| 737 | crypto_bignum_mulmod(t1, x1, prime, x2) < 0) |
| 738 | goto fail; |
| 739 | debug_print_bignum("SSWU: x2 = z * u^2 * x1", x2, prime_len); |
| 740 | |
| 741 | /* gx2 = x2^3 + a * x2 + b */ |
| 742 | if (crypto_bignum_exptmod(x2, three, prime, t1) < 0 || |
| 743 | crypto_bignum_mulmod(a, x2, prime, t2) < 0 || |
| 744 | crypto_bignum_addmod(t1, t2, prime, t1) < 0 || |
| 745 | crypto_bignum_addmod(t1, b, prime, gx2) < 0) |
| 746 | goto fail; |
| 747 | debug_print_bignum("SSWU: gx2 = x2^3 + a * x2 + b", gx2, prime_len); |
| 748 | |
| 749 | /* l = gx1 is a quadratic residue modulo p |
| 750 | * --> gx1^((p-1)/2) modulo p is zero or one */ |
| 751 | if (crypto_bignum_sub(prime, one, t1) < 0 || |
| 752 | crypto_bignum_rshift(t1, 1, t1) < 0 || |
| 753 | crypto_bignum_exptmod(gx1, t1, prime, t1) < 0) |
| 754 | goto fail; |
| 755 | debug_print_bignum("SSWU: gx1^((p-1)/2) modulo p", t1, prime_len); |
| 756 | is_qr = const_time_eq(crypto_bignum_is_zero(t1) | |
| 757 | crypto_bignum_is_one(t1), 1); |
| 758 | |
| 759 | /* v = CSEL(l, gx1, gx2) */ |
| 760 | if (crypto_bignum_to_bin(gx1, bin1, sizeof(bin1), prime_len) < 0 || |
| 761 | crypto_bignum_to_bin(gx2, bin2, sizeof(bin2), prime_len) < 0) |
| 762 | goto fail; |
| 763 | const_time_select_bin(is_qr, bin1, bin2, prime_len, bin); |
| 764 | v = crypto_bignum_init_set(bin, prime_len); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 765 | if (!v) |
| 766 | goto fail; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 767 | debug_print_bignum("SSWU: v = CSEL(l, gx1, gx2)", v, prime_len); |
| 768 | |
| 769 | /* x = CSEL(l, x1, x2) */ |
| 770 | if (crypto_bignum_to_bin(x1, bin1, sizeof(bin1), prime_len) < 0 || |
| 771 | crypto_bignum_to_bin(x2, bin2, sizeof(bin2), prime_len) < 0) |
| 772 | goto fail; |
| 773 | const_time_select_bin(is_qr, bin1, bin2, prime_len, x_y); |
| 774 | wpa_hexdump_key(MSG_DEBUG, "SSWU: x = CSEL(l, x1, x2)", x_y, prime_len); |
| 775 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 776 | /* y = sqrt(v) */ |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 777 | y = crypto_bignum_init(); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 778 | if (!y || dragonfly_sqrt(ec, v, y) < 0) |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 779 | goto fail; |
| 780 | debug_print_bignum("SSWU: y = sqrt(v)", y, prime_len); |
| 781 | |
| 782 | /* l = CEQ(LSB(u), LSB(y)) */ |
| 783 | if (crypto_bignum_to_bin(u, bin1, sizeof(bin1), prime_len) < 0 || |
| 784 | crypto_bignum_to_bin(y, bin2, sizeof(bin2), prime_len) < 0) |
| 785 | goto fail; |
| 786 | is_eq = const_time_eq(bin1[prime_len - 1] & 0x01, |
| 787 | bin2[prime_len - 1] & 0x01); |
| 788 | |
| 789 | /* P = CSEL(l, (x,y), (x, p-y)) */ |
| 790 | if (crypto_bignum_sub(prime, y, t1) < 0) |
| 791 | goto fail; |
| 792 | debug_print_bignum("SSWU: p - y", t1, prime_len); |
| 793 | if (crypto_bignum_to_bin(y, bin1, sizeof(bin1), prime_len) < 0 || |
| 794 | crypto_bignum_to_bin(t1, bin2, sizeof(bin2), prime_len) < 0) |
| 795 | goto fail; |
| 796 | const_time_select_bin(is_eq, bin1, bin2, prime_len, &x_y[prime_len]); |
| 797 | |
| 798 | /* output P */ |
| 799 | wpa_hexdump_key(MSG_DEBUG, "SSWU: P.x", x_y, prime_len); |
| 800 | wpa_hexdump_key(MSG_DEBUG, "SSWU: P.y", &x_y[prime_len], prime_len); |
| 801 | p = crypto_ec_point_from_bin(ec, x_y); |
| 802 | |
| 803 | fail: |
| 804 | crypto_bignum_deinit(u2, 1); |
| 805 | crypto_bignum_deinit(t1, 1); |
| 806 | crypto_bignum_deinit(t2, 1); |
| 807 | crypto_bignum_deinit(z, 0); |
| 808 | crypto_bignum_deinit(t, 1); |
| 809 | crypto_bignum_deinit(x1a, 1); |
| 810 | crypto_bignum_deinit(x1b, 1); |
| 811 | crypto_bignum_deinit(x1, 1); |
| 812 | crypto_bignum_deinit(x2, 1); |
| 813 | crypto_bignum_deinit(gx1, 1); |
| 814 | crypto_bignum_deinit(gx2, 1); |
| 815 | crypto_bignum_deinit(y, 1); |
| 816 | crypto_bignum_deinit(v, 1); |
| 817 | crypto_bignum_deinit(zero, 0); |
| 818 | crypto_bignum_deinit(one, 0); |
| 819 | crypto_bignum_deinit(two, 0); |
| 820 | crypto_bignum_deinit(three, 0); |
| 821 | forced_memzero(bin, sizeof(bin)); |
| 822 | forced_memzero(bin1, sizeof(bin1)); |
| 823 | forced_memzero(bin2, sizeof(bin2)); |
| 824 | forced_memzero(x_y, sizeof(x_y)); |
| 825 | return p; |
| 826 | } |
| 827 | |
| 828 | |
| 829 | static int sae_pwd_seed(size_t hash_len, const u8 *ssid, size_t ssid_len, |
| 830 | const u8 *password, size_t password_len, |
| 831 | const char *identifier, u8 *pwd_seed) |
| 832 | { |
| 833 | const u8 *addr[2]; |
| 834 | size_t len[2]; |
| 835 | size_t num_elem; |
| 836 | |
| 837 | /* pwd-seed = HKDF-Extract(ssid, password [ || identifier ]) */ |
| 838 | addr[0] = password; |
| 839 | len[0] = password_len; |
| 840 | num_elem = 1; |
| 841 | wpa_hexdump_ascii(MSG_DEBUG, "SAE: SSID", ssid, ssid_len); |
| 842 | wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password", |
| 843 | password, password_len); |
| 844 | if (identifier) { |
| 845 | wpa_printf(MSG_DEBUG, "SAE: password identifier: %s", |
| 846 | identifier); |
| 847 | addr[num_elem] = (const u8 *) identifier; |
| 848 | len[num_elem] = os_strlen(identifier); |
| 849 | num_elem++; |
| 850 | } |
| 851 | if (hkdf_extract(hash_len, ssid, ssid_len, num_elem, addr, len, |
| 852 | pwd_seed) < 0) |
| 853 | return -1; |
| 854 | wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, hash_len); |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | |
| 859 | size_t sae_ecc_prime_len_2_hash_len(size_t prime_len) |
| 860 | { |
| 861 | if (prime_len <= 256 / 8) |
| 862 | return 32; |
| 863 | if (prime_len <= 384 / 8) |
| 864 | return 48; |
| 865 | return 64; |
| 866 | } |
| 867 | |
| 868 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 869 | static struct crypto_ec_point * |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 870 | sae_derive_pt_ecc(struct crypto_ec *ec, int group, |
| 871 | const u8 *ssid, size_t ssid_len, |
| 872 | const u8 *password, size_t password_len, |
| 873 | const char *identifier) |
| 874 | { |
| 875 | u8 pwd_seed[64]; |
| 876 | u8 pwd_value[SAE_MAX_ECC_PRIME_LEN * 2]; |
| 877 | size_t pwd_value_len, hash_len, prime_len; |
| 878 | const struct crypto_bignum *prime; |
| 879 | struct crypto_bignum *bn = NULL; |
| 880 | struct crypto_ec_point *p1 = NULL, *p2 = NULL, *pt = NULL; |
| 881 | |
| 882 | prime = crypto_ec_get_prime(ec); |
| 883 | prime_len = crypto_ec_prime_len(ec); |
| 884 | if (prime_len > SAE_MAX_ECC_PRIME_LEN) |
| 885 | goto fail; |
| 886 | hash_len = sae_ecc_prime_len_2_hash_len(prime_len); |
| 887 | |
| 888 | /* len = olen(p) + ceil(olen(p)/2) */ |
| 889 | pwd_value_len = prime_len + (prime_len + 1) / 2; |
| 890 | |
| 891 | if (sae_pwd_seed(hash_len, ssid, ssid_len, password, password_len, |
| 892 | identifier, pwd_seed) < 0) |
| 893 | goto fail; |
| 894 | |
| 895 | /* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u1 P1", len) |
| 896 | */ |
| 897 | if (hkdf_expand(hash_len, pwd_seed, hash_len, |
| 898 | "SAE Hash to Element u1 P1", pwd_value, pwd_value_len) < |
| 899 | 0) |
| 900 | goto fail; |
| 901 | wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value (u1 P1)", |
| 902 | pwd_value, pwd_value_len); |
| 903 | |
| 904 | /* u1 = pwd-value modulo p */ |
| 905 | bn = crypto_bignum_init_set(pwd_value, pwd_value_len); |
| 906 | if (!bn || crypto_bignum_mod(bn, prime, bn) < 0 || |
| 907 | crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value), |
| 908 | prime_len) < 0) |
| 909 | goto fail; |
| 910 | wpa_hexdump_key(MSG_DEBUG, "SAE: u1", pwd_value, prime_len); |
| 911 | |
| 912 | /* P1 = SSWU(u1) */ |
| 913 | p1 = sswu(ec, group, bn); |
| 914 | if (!p1) |
| 915 | goto fail; |
| 916 | |
| 917 | /* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u2 P2", len) |
| 918 | */ |
| 919 | if (hkdf_expand(hash_len, pwd_seed, hash_len, |
| 920 | "SAE Hash to Element u2 P2", pwd_value, |
| 921 | pwd_value_len) < 0) |
| 922 | goto fail; |
| 923 | wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value (u2 P2)", |
| 924 | pwd_value, pwd_value_len); |
| 925 | |
| 926 | /* u2 = pwd-value modulo p */ |
| 927 | crypto_bignum_deinit(bn, 1); |
| 928 | bn = crypto_bignum_init_set(pwd_value, pwd_value_len); |
| 929 | if (!bn || crypto_bignum_mod(bn, prime, bn) < 0 || |
| 930 | crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value), |
| 931 | prime_len) < 0) |
| 932 | goto fail; |
| 933 | wpa_hexdump_key(MSG_DEBUG, "SAE: u2", pwd_value, prime_len); |
| 934 | |
| 935 | /* P2 = SSWU(u2) */ |
| 936 | p2 = sswu(ec, group, bn); |
| 937 | if (!p2) |
| 938 | goto fail; |
| 939 | |
| 940 | /* PT = elem-op(P1, P2) */ |
| 941 | pt = crypto_ec_point_init(ec); |
| 942 | if (!pt) |
| 943 | goto fail; |
| 944 | if (crypto_ec_point_add(ec, p1, p2, pt) < 0) { |
| 945 | crypto_ec_point_deinit(pt, 1); |
| 946 | pt = NULL; |
| 947 | } |
| 948 | |
| 949 | fail: |
| 950 | forced_memzero(pwd_seed, sizeof(pwd_seed)); |
| 951 | forced_memzero(pwd_value, sizeof(pwd_value)); |
| 952 | crypto_bignum_deinit(bn, 1); |
| 953 | crypto_ec_point_deinit(p1, 1); |
| 954 | crypto_ec_point_deinit(p2, 1); |
| 955 | return pt; |
| 956 | } |
| 957 | |
| 958 | |
| 959 | size_t sae_ffc_prime_len_2_hash_len(size_t prime_len) |
| 960 | { |
| 961 | if (prime_len <= 2048 / 8) |
| 962 | return 32; |
| 963 | if (prime_len <= 3072 / 8) |
| 964 | return 48; |
| 965 | return 64; |
| 966 | } |
| 967 | |
| 968 | |
| 969 | static struct crypto_bignum * |
| 970 | sae_derive_pt_ffc(const struct dh_group *dh, int group, |
| 971 | const u8 *ssid, size_t ssid_len, |
| 972 | const u8 *password, size_t password_len, |
| 973 | const char *identifier) |
| 974 | { |
| 975 | size_t hash_len, prime_len, pwd_value_len; |
| 976 | struct crypto_bignum *prime, *order; |
| 977 | struct crypto_bignum *one = NULL, *two = NULL, *bn = NULL, *tmp = NULL, |
| 978 | *pt = NULL; |
| 979 | u8 pwd_seed[64]; |
| 980 | u8 pwd_value[SAE_MAX_PRIME_LEN + SAE_MAX_PRIME_LEN / 2]; |
| 981 | |
| 982 | prime = crypto_bignum_init_set(dh->prime, dh->prime_len); |
| 983 | order = crypto_bignum_init_set(dh->order, dh->order_len); |
| 984 | if (!prime || !order) |
| 985 | goto fail; |
| 986 | prime_len = dh->prime_len; |
| 987 | if (prime_len > SAE_MAX_PRIME_LEN) |
| 988 | goto fail; |
| 989 | hash_len = sae_ffc_prime_len_2_hash_len(prime_len); |
| 990 | |
| 991 | /* len = olen(p) + ceil(olen(p)/2) */ |
| 992 | pwd_value_len = prime_len + (prime_len + 1) / 2; |
| 993 | if (pwd_value_len > sizeof(pwd_value)) |
| 994 | goto fail; |
| 995 | |
| 996 | if (sae_pwd_seed(hash_len, ssid, ssid_len, password, password_len, |
| 997 | identifier, pwd_seed) < 0) |
| 998 | goto fail; |
| 999 | |
| 1000 | /* pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element", len) */ |
| 1001 | if (hkdf_expand(hash_len, pwd_seed, hash_len, |
| 1002 | "SAE Hash to Element", pwd_value, pwd_value_len) < 0) |
| 1003 | goto fail; |
| 1004 | wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value", |
| 1005 | pwd_value, pwd_value_len); |
| 1006 | |
| 1007 | /* pwd-value = (pwd-value modulo (p-2)) + 2 */ |
| 1008 | bn = crypto_bignum_init_set(pwd_value, pwd_value_len); |
| 1009 | one = crypto_bignum_init_uint(1); |
| 1010 | two = crypto_bignum_init_uint(2); |
| 1011 | tmp = crypto_bignum_init(); |
| 1012 | if (!bn || !one || !two || !tmp || |
| 1013 | crypto_bignum_sub(prime, two, tmp) < 0 || |
| 1014 | crypto_bignum_mod(bn, tmp, bn) < 0 || |
| 1015 | crypto_bignum_add(bn, two, bn) < 0 || |
| 1016 | crypto_bignum_to_bin(bn, pwd_value, sizeof(pwd_value), |
| 1017 | prime_len) < 0) |
| 1018 | goto fail; |
| 1019 | wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value(reduced)", |
| 1020 | pwd_value, prime_len); |
| 1021 | |
| 1022 | /* PT = pwd-value^((p-1)/q) modulo p */ |
| 1023 | pt = crypto_bignum_init(); |
| 1024 | if (!pt || |
| 1025 | crypto_bignum_sub(prime, one, tmp) < 0 || |
| 1026 | crypto_bignum_div(tmp, order, tmp) < 0 || |
| 1027 | crypto_bignum_exptmod(bn, tmp, prime, pt) < 0) { |
| 1028 | crypto_bignum_deinit(pt, 1); |
| 1029 | pt = NULL; |
| 1030 | goto fail; |
| 1031 | } |
| 1032 | debug_print_bignum("SAE: PT", pt, prime_len); |
| 1033 | |
| 1034 | fail: |
| 1035 | forced_memzero(pwd_seed, sizeof(pwd_seed)); |
| 1036 | forced_memzero(pwd_value, sizeof(pwd_value)); |
| 1037 | crypto_bignum_deinit(bn, 1); |
| 1038 | crypto_bignum_deinit(tmp, 1); |
| 1039 | crypto_bignum_deinit(one, 0); |
| 1040 | crypto_bignum_deinit(two, 0); |
| 1041 | crypto_bignum_deinit(prime, 0); |
| 1042 | crypto_bignum_deinit(order, 0); |
| 1043 | return pt; |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | static struct sae_pt * |
| 1048 | sae_derive_pt_group(int group, const u8 *ssid, size_t ssid_len, |
| 1049 | const u8 *password, size_t password_len, |
| 1050 | const char *identifier) |
| 1051 | { |
| 1052 | struct sae_pt *pt; |
| 1053 | |
| 1054 | wpa_printf(MSG_DEBUG, "SAE: Derive PT - group %d", group); |
| 1055 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1056 | if (ssid_len > 32) |
| 1057 | return NULL; |
| 1058 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1059 | pt = os_zalloc(sizeof(*pt)); |
| 1060 | if (!pt) |
| 1061 | return NULL; |
| 1062 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1063 | #ifdef CONFIG_SAE_PK |
| 1064 | os_memcpy(pt->ssid, ssid, ssid_len); |
| 1065 | pt->ssid_len = ssid_len; |
| 1066 | #endif /* CONFIG_SAE_PK */ |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1067 | pt->group = group; |
| 1068 | pt->ec = crypto_ec_init(group); |
| 1069 | if (pt->ec) { |
| 1070 | pt->ecc_pt = sae_derive_pt_ecc(pt->ec, group, ssid, ssid_len, |
| 1071 | password, password_len, |
| 1072 | identifier); |
| 1073 | if (!pt->ecc_pt) { |
| 1074 | wpa_printf(MSG_DEBUG, "SAE: Failed to derive PT"); |
| 1075 | goto fail; |
| 1076 | } |
| 1077 | |
| 1078 | return pt; |
| 1079 | } |
| 1080 | |
| 1081 | pt->dh = dh_groups_get(group); |
| 1082 | if (!pt->dh) { |
| 1083 | wpa_printf(MSG_DEBUG, "SAE: Unsupported group %d", group); |
| 1084 | goto fail; |
| 1085 | } |
| 1086 | |
| 1087 | pt->ffc_pt = sae_derive_pt_ffc(pt->dh, group, ssid, ssid_len, |
| 1088 | password, password_len, identifier); |
| 1089 | if (!pt->ffc_pt) { |
| 1090 | wpa_printf(MSG_DEBUG, "SAE: Failed to derive PT"); |
| 1091 | goto fail; |
| 1092 | } |
| 1093 | |
| 1094 | return pt; |
| 1095 | fail: |
| 1096 | sae_deinit_pt(pt); |
| 1097 | return NULL; |
| 1098 | } |
| 1099 | |
| 1100 | |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 1101 | struct sae_pt * sae_derive_pt(const int *groups, |
| 1102 | const u8 *ssid, size_t ssid_len, |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1103 | const u8 *password, size_t password_len, |
| 1104 | const char *identifier) |
| 1105 | { |
| 1106 | struct sae_pt *pt = NULL, *last = NULL, *tmp; |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 1107 | const int default_groups[] = { 19, 0 }; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1108 | int i; |
| 1109 | |
| 1110 | if (!groups) |
| 1111 | groups = default_groups; |
| 1112 | for (i = 0; groups[i] > 0; i++) { |
| 1113 | tmp = sae_derive_pt_group(groups[i], ssid, ssid_len, password, |
| 1114 | password_len, identifier); |
| 1115 | if (!tmp) |
| 1116 | continue; |
| 1117 | |
| 1118 | if (last) |
| 1119 | last->next = tmp; |
| 1120 | else |
| 1121 | pt = tmp; |
| 1122 | last = tmp; |
| 1123 | } |
| 1124 | |
| 1125 | return pt; |
| 1126 | } |
| 1127 | |
| 1128 | |
| 1129 | static void sae_max_min_addr(const u8 *addr[], size_t len[], |
| 1130 | const u8 *addr1, const u8 *addr2) |
| 1131 | { |
| 1132 | len[0] = ETH_ALEN; |
| 1133 | len[1] = ETH_ALEN; |
| 1134 | if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) { |
| 1135 | addr[0] = addr1; |
| 1136 | addr[1] = addr2; |
| 1137 | } else { |
| 1138 | addr[0] = addr2; |
| 1139 | addr[1] = addr1; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | |
| 1144 | struct crypto_ec_point * |
| 1145 | sae_derive_pwe_from_pt_ecc(const struct sae_pt *pt, |
| 1146 | const u8 *addr1, const u8 *addr2) |
| 1147 | { |
| 1148 | u8 bin[SAE_MAX_ECC_PRIME_LEN * 2]; |
| 1149 | size_t prime_len; |
| 1150 | const u8 *addr[2]; |
| 1151 | size_t len[2]; |
| 1152 | u8 salt[64], hash[64]; |
| 1153 | size_t hash_len; |
| 1154 | const struct crypto_bignum *order; |
| 1155 | struct crypto_bignum *tmp = NULL, *val = NULL, *one = NULL; |
| 1156 | struct crypto_ec_point *pwe = NULL; |
| 1157 | |
| 1158 | wpa_printf(MSG_DEBUG, "SAE: Derive PWE from PT"); |
| 1159 | prime_len = crypto_ec_prime_len(pt->ec); |
| 1160 | if (crypto_ec_point_to_bin(pt->ec, pt->ecc_pt, |
| 1161 | bin, bin + prime_len) < 0) |
| 1162 | return NULL; |
| 1163 | wpa_hexdump_key(MSG_DEBUG, "SAE: PT.x", bin, prime_len); |
| 1164 | wpa_hexdump_key(MSG_DEBUG, "SAE: PT.y", bin + prime_len, prime_len); |
| 1165 | |
| 1166 | sae_max_min_addr(addr, len, addr1, addr2); |
| 1167 | |
| 1168 | /* val = H(0^n, |
| 1169 | * MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC)) */ |
| 1170 | wpa_printf(MSG_DEBUG, "SAE: val = H(0^n, MAX(addrs) || MIN(addrs))"); |
| 1171 | hash_len = sae_ecc_prime_len_2_hash_len(prime_len); |
| 1172 | os_memset(salt, 0, hash_len); |
| 1173 | if (hkdf_extract(hash_len, salt, hash_len, 2, addr, len, hash) < 0) |
| 1174 | goto fail; |
| 1175 | wpa_hexdump(MSG_DEBUG, "SAE: val", hash, hash_len); |
| 1176 | |
| 1177 | /* val = val modulo (q - 1) + 1 */ |
| 1178 | order = crypto_ec_get_order(pt->ec); |
| 1179 | tmp = crypto_bignum_init(); |
| 1180 | val = crypto_bignum_init_set(hash, hash_len); |
| 1181 | one = crypto_bignum_init_uint(1); |
| 1182 | if (!tmp || !val || !one || |
| 1183 | crypto_bignum_sub(order, one, tmp) < 0 || |
| 1184 | crypto_bignum_mod(val, tmp, val) < 0 || |
| 1185 | crypto_bignum_add(val, one, val) < 0) |
| 1186 | goto fail; |
| 1187 | debug_print_bignum("SAE: val(reduced to 1..q-1)", val, prime_len); |
| 1188 | |
| 1189 | /* PWE = scalar-op(val, PT) */ |
| 1190 | pwe = crypto_ec_point_init(pt->ec); |
| 1191 | if (!pwe || |
| 1192 | crypto_ec_point_mul(pt->ec, pt->ecc_pt, val, pwe) < 0 || |
| 1193 | crypto_ec_point_to_bin(pt->ec, pwe, bin, bin + prime_len) < 0) { |
| 1194 | crypto_ec_point_deinit(pwe, 1); |
| 1195 | pwe = NULL; |
| 1196 | goto fail; |
| 1197 | } |
| 1198 | wpa_hexdump_key(MSG_DEBUG, "SAE: PWE.x", bin, prime_len); |
| 1199 | wpa_hexdump_key(MSG_DEBUG, "SAE: PWE.y", bin + prime_len, prime_len); |
| 1200 | |
| 1201 | fail: |
| 1202 | crypto_bignum_deinit(tmp, 1); |
| 1203 | crypto_bignum_deinit(val, 1); |
| 1204 | crypto_bignum_deinit(one, 0); |
| 1205 | return pwe; |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | struct crypto_bignum * |
| 1210 | sae_derive_pwe_from_pt_ffc(const struct sae_pt *pt, |
| 1211 | const u8 *addr1, const u8 *addr2) |
| 1212 | { |
| 1213 | size_t prime_len; |
| 1214 | const u8 *addr[2]; |
| 1215 | size_t len[2]; |
| 1216 | u8 salt[64], hash[64]; |
| 1217 | size_t hash_len; |
| 1218 | struct crypto_bignum *tmp = NULL, *val = NULL, *one = NULL; |
| 1219 | struct crypto_bignum *pwe = NULL, *order = NULL, *prime = NULL; |
| 1220 | |
| 1221 | wpa_printf(MSG_DEBUG, "SAE: Derive PWE from PT"); |
| 1222 | prime = crypto_bignum_init_set(pt->dh->prime, pt->dh->prime_len); |
| 1223 | order = crypto_bignum_init_set(pt->dh->order, pt->dh->order_len); |
| 1224 | if (!prime || !order) |
| 1225 | goto fail; |
| 1226 | prime_len = pt->dh->prime_len; |
| 1227 | |
| 1228 | sae_max_min_addr(addr, len, addr1, addr2); |
| 1229 | |
| 1230 | /* val = H(0^n, |
| 1231 | * MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC)) */ |
| 1232 | wpa_printf(MSG_DEBUG, "SAE: val = H(0^n, MAX(addrs) || MIN(addrs))"); |
| 1233 | hash_len = sae_ffc_prime_len_2_hash_len(prime_len); |
| 1234 | os_memset(salt, 0, hash_len); |
| 1235 | if (hkdf_extract(hash_len, salt, hash_len, 2, addr, len, hash) < 0) |
| 1236 | goto fail; |
| 1237 | wpa_hexdump(MSG_DEBUG, "SAE: val", hash, hash_len); |
| 1238 | |
| 1239 | /* val = val modulo (q - 1) + 1 */ |
| 1240 | tmp = crypto_bignum_init(); |
| 1241 | val = crypto_bignum_init_set(hash, hash_len); |
| 1242 | one = crypto_bignum_init_uint(1); |
| 1243 | if (!tmp || !val || !one || |
| 1244 | crypto_bignum_sub(order, one, tmp) < 0 || |
| 1245 | crypto_bignum_mod(val, tmp, val) < 0 || |
| 1246 | crypto_bignum_add(val, one, val) < 0) |
| 1247 | goto fail; |
| 1248 | debug_print_bignum("SAE: val(reduced to 1..q-1)", val, prime_len); |
| 1249 | |
| 1250 | /* PWE = scalar-op(val, PT) */ |
| 1251 | pwe = crypto_bignum_init(); |
| 1252 | if (!pwe || crypto_bignum_exptmod(pt->ffc_pt, val, prime, pwe) < 0) { |
| 1253 | crypto_bignum_deinit(pwe, 1); |
| 1254 | pwe = NULL; |
| 1255 | goto fail; |
| 1256 | } |
| 1257 | debug_print_bignum("SAE: PWE", pwe, prime_len); |
| 1258 | |
| 1259 | fail: |
| 1260 | crypto_bignum_deinit(tmp, 1); |
| 1261 | crypto_bignum_deinit(val, 1); |
| 1262 | crypto_bignum_deinit(one, 0); |
| 1263 | crypto_bignum_deinit(prime, 0); |
| 1264 | crypto_bignum_deinit(order, 0); |
| 1265 | return pwe; |
| 1266 | } |
| 1267 | |
| 1268 | |
| 1269 | void sae_deinit_pt(struct sae_pt *pt) |
| 1270 | { |
| 1271 | struct sae_pt *prev; |
| 1272 | |
| 1273 | while (pt) { |
| 1274 | crypto_ec_point_deinit(pt->ecc_pt, 1); |
| 1275 | crypto_bignum_deinit(pt->ffc_pt, 1); |
| 1276 | crypto_ec_deinit(pt->ec); |
| 1277 | prev = pt; |
| 1278 | pt = pt->next; |
| 1279 | os_free(prev); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1284 | static int sae_derive_commit_element_ecc(struct sae_data *sae, |
| 1285 | struct crypto_bignum *mask) |
| 1286 | { |
| 1287 | /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */ |
| 1288 | if (!sae->tmp->own_commit_element_ecc) { |
| 1289 | sae->tmp->own_commit_element_ecc = |
| 1290 | crypto_ec_point_init(sae->tmp->ec); |
| 1291 | if (!sae->tmp->own_commit_element_ecc) |
| 1292 | return -1; |
| 1293 | } |
| 1294 | |
| 1295 | if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc, mask, |
| 1296 | sae->tmp->own_commit_element_ecc) < 0 || |
| 1297 | crypto_ec_point_invert(sae->tmp->ec, |
| 1298 | sae->tmp->own_commit_element_ecc) < 0) { |
| 1299 | wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element"); |
| 1300 | return -1; |
| 1301 | } |
| 1302 | |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | |
| 1307 | static int sae_derive_commit_element_ffc(struct sae_data *sae, |
| 1308 | struct crypto_bignum *mask) |
| 1309 | { |
| 1310 | /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */ |
| 1311 | if (!sae->tmp->own_commit_element_ffc) { |
| 1312 | sae->tmp->own_commit_element_ffc = crypto_bignum_init(); |
| 1313 | if (!sae->tmp->own_commit_element_ffc) |
| 1314 | return -1; |
| 1315 | } |
| 1316 | |
| 1317 | if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, mask, sae->tmp->prime, |
| 1318 | sae->tmp->own_commit_element_ffc) < 0 || |
| 1319 | crypto_bignum_inverse(sae->tmp->own_commit_element_ffc, |
| 1320 | sae->tmp->prime, |
| 1321 | sae->tmp->own_commit_element_ffc) < 0) { |
| 1322 | wpa_printf(MSG_DEBUG, "SAE: Could not compute commit-element"); |
| 1323 | return -1; |
| 1324 | } |
| 1325 | |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
| 1329 | |
| 1330 | static int sae_derive_commit(struct sae_data *sae) |
| 1331 | { |
| 1332 | struct crypto_bignum *mask; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1333 | int ret; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1334 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1335 | mask = crypto_bignum_init(); |
| 1336 | if (!sae->tmp->sae_rand) |
| 1337 | sae->tmp->sae_rand = crypto_bignum_init(); |
| 1338 | if (!sae->tmp->own_commit_scalar) |
| 1339 | sae->tmp->own_commit_scalar = crypto_bignum_init(); |
| 1340 | ret = !mask || !sae->tmp->sae_rand || !sae->tmp->own_commit_scalar || |
| 1341 | dragonfly_generate_scalar(sae->tmp->order, sae->tmp->sae_rand, |
| 1342 | mask, |
| 1343 | sae->tmp->own_commit_scalar) < 0 || |
| 1344 | (sae->tmp->ec && |
| 1345 | sae_derive_commit_element_ecc(sae, mask) < 0) || |
| 1346 | (sae->tmp->dh && |
| 1347 | sae_derive_commit_element_ffc(sae, mask) < 0); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1348 | crypto_bignum_deinit(mask, 1); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1349 | return ret ? -1 : 0; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | |
| 1353 | int sae_prepare_commit(const u8 *addr1, const u8 *addr2, |
| 1354 | const u8 *password, size_t password_len, |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1355 | struct sae_data *sae) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1356 | { |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1357 | if (sae->tmp == NULL || |
| 1358 | (sae->tmp->ec && sae_derive_pwe_ecc(sae, addr1, addr2, password, |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1359 | password_len) < 0) || |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1360 | (sae->tmp->dh && sae_derive_pwe_ffc(sae, addr1, addr2, password, |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1361 | password_len) < 0)) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1362 | return -1; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1363 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1364 | sae->h2e = 0; |
| 1365 | sae->pk = 0; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1366 | return sae_derive_commit(sae); |
| 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | int sae_prepare_commit_pt(struct sae_data *sae, const struct sae_pt *pt, |
| 1371 | const u8 *addr1, const u8 *addr2, |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1372 | int *rejected_groups, const struct sae_pk *pk) |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1373 | { |
| 1374 | if (!sae->tmp) |
| 1375 | return -1; |
| 1376 | |
| 1377 | while (pt) { |
| 1378 | if (pt->group == sae->group) |
| 1379 | break; |
| 1380 | pt = pt->next; |
| 1381 | } |
| 1382 | if (!pt) { |
| 1383 | wpa_printf(MSG_INFO, "SAE: Could not find PT for group %u", |
| 1384 | sae->group); |
| 1385 | return -1; |
| 1386 | } |
| 1387 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1388 | #ifdef CONFIG_SAE_PK |
| 1389 | os_memcpy(sae->tmp->ssid, pt->ssid, pt->ssid_len); |
| 1390 | sae->tmp->ssid_len = pt->ssid_len; |
| 1391 | sae->tmp->ap_pk = pk; |
| 1392 | #endif /* CONFIG_SAE_PK */ |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1393 | sae->tmp->own_addr_higher = os_memcmp(addr1, addr2, ETH_ALEN) > 0; |
| 1394 | wpabuf_free(sae->tmp->own_rejected_groups); |
| 1395 | sae->tmp->own_rejected_groups = NULL; |
| 1396 | if (rejected_groups) { |
| 1397 | int count, i; |
| 1398 | struct wpabuf *groups; |
| 1399 | |
| 1400 | count = int_array_len(rejected_groups); |
| 1401 | groups = wpabuf_alloc(count * 2); |
| 1402 | if (!groups) |
| 1403 | return -1; |
| 1404 | for (i = 0; i < count; i++) |
| 1405 | wpabuf_put_le16(groups, rejected_groups[i]); |
| 1406 | sae->tmp->own_rejected_groups = groups; |
| 1407 | } |
| 1408 | |
| 1409 | if (pt->ec) { |
| 1410 | crypto_ec_point_deinit(sae->tmp->pwe_ecc, 1); |
| 1411 | sae->tmp->pwe_ecc = sae_derive_pwe_from_pt_ecc(pt, addr1, |
| 1412 | addr2); |
| 1413 | if (!sae->tmp->pwe_ecc) |
| 1414 | return -1; |
| 1415 | } |
| 1416 | |
| 1417 | if (pt->dh) { |
| 1418 | crypto_bignum_deinit(sae->tmp->pwe_ffc, 1); |
| 1419 | sae->tmp->pwe_ffc = sae_derive_pwe_from_pt_ffc(pt, addr1, |
| 1420 | addr2); |
| 1421 | if (!sae->tmp->pwe_ffc) |
| 1422 | return -1; |
| 1423 | } |
| 1424 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1425 | sae->h2e = 1; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1426 | return sae_derive_commit(sae); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | |
| 1430 | static int sae_derive_k_ecc(struct sae_data *sae, u8 *k) |
| 1431 | { |
| 1432 | struct crypto_ec_point *K; |
| 1433 | int ret = -1; |
| 1434 | |
| 1435 | K = crypto_ec_point_init(sae->tmp->ec); |
| 1436 | if (K == NULL) |
| 1437 | goto fail; |
| 1438 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1439 | /* |
| 1440 | * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE), |
| 1441 | * PEER-COMMIT-ELEMENT))) |
| 1442 | * If K is identity element (point-at-infinity), reject |
| 1443 | * k = F(K) (= x coordinate) |
| 1444 | */ |
| 1445 | |
| 1446 | if (crypto_ec_point_mul(sae->tmp->ec, sae->tmp->pwe_ecc, |
| 1447 | sae->peer_commit_scalar, K) < 0 || |
| 1448 | crypto_ec_point_add(sae->tmp->ec, K, |
| 1449 | sae->tmp->peer_commit_element_ecc, K) < 0 || |
| 1450 | crypto_ec_point_mul(sae->tmp->ec, K, sae->tmp->sae_rand, K) < 0 || |
| 1451 | crypto_ec_point_is_at_infinity(sae->tmp->ec, K) || |
| 1452 | crypto_ec_point_to_bin(sae->tmp->ec, K, k, NULL) < 0) { |
| 1453 | wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k"); |
| 1454 | goto fail; |
| 1455 | } |
| 1456 | |
| 1457 | wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len); |
| 1458 | |
| 1459 | ret = 0; |
| 1460 | fail: |
| 1461 | crypto_ec_point_deinit(K, 1); |
| 1462 | return ret; |
| 1463 | } |
| 1464 | |
| 1465 | |
| 1466 | static int sae_derive_k_ffc(struct sae_data *sae, u8 *k) |
| 1467 | { |
| 1468 | struct crypto_bignum *K; |
| 1469 | int ret = -1; |
| 1470 | |
| 1471 | K = crypto_bignum_init(); |
| 1472 | if (K == NULL) |
| 1473 | goto fail; |
| 1474 | |
| 1475 | /* |
| 1476 | * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE), |
| 1477 | * PEER-COMMIT-ELEMENT))) |
| 1478 | * If K is identity element (one), reject. |
| 1479 | * k = F(K) (= x coordinate) |
| 1480 | */ |
| 1481 | |
| 1482 | if (crypto_bignum_exptmod(sae->tmp->pwe_ffc, sae->peer_commit_scalar, |
| 1483 | sae->tmp->prime, K) < 0 || |
| 1484 | crypto_bignum_mulmod(K, sae->tmp->peer_commit_element_ffc, |
| 1485 | sae->tmp->prime, K) < 0 || |
| 1486 | crypto_bignum_exptmod(K, sae->tmp->sae_rand, sae->tmp->prime, K) < 0 |
| 1487 | || |
| 1488 | crypto_bignum_is_one(K) || |
| 1489 | crypto_bignum_to_bin(K, k, SAE_MAX_PRIME_LEN, sae->tmp->prime_len) < |
| 1490 | 0) { |
| 1491 | wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k"); |
| 1492 | goto fail; |
| 1493 | } |
| 1494 | |
| 1495 | wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, sae->tmp->prime_len); |
| 1496 | |
| 1497 | ret = 0; |
| 1498 | fail: |
| 1499 | crypto_bignum_deinit(K, 1); |
| 1500 | return ret; |
| 1501 | } |
| 1502 | |
| 1503 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1504 | static int sae_kdf_hash(size_t hash_len, const u8 *k, const char *label, |
| 1505 | const u8 *context, size_t context_len, |
| 1506 | u8 *out, size_t out_len) |
| 1507 | { |
| 1508 | if (hash_len == 32) |
| 1509 | return sha256_prf(k, hash_len, label, |
| 1510 | context, context_len, out, out_len); |
| 1511 | #ifdef CONFIG_SHA384 |
| 1512 | if (hash_len == 48) |
| 1513 | return sha384_prf(k, hash_len, label, |
| 1514 | context, context_len, out, out_len); |
| 1515 | #endif /* CONFIG_SHA384 */ |
| 1516 | #ifdef CONFIG_SHA512 |
| 1517 | if (hash_len == 64) |
| 1518 | return sha512_prf(k, hash_len, label, |
| 1519 | context, context_len, out, out_len); |
| 1520 | #endif /* CONFIG_SHA512 */ |
| 1521 | return -1; |
| 1522 | } |
| 1523 | |
| 1524 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1525 | static int sae_derive_keys(struct sae_data *sae, const u8 *k) |
| 1526 | { |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1527 | u8 zero[SAE_MAX_HASH_LEN], val[SAE_MAX_PRIME_LEN]; |
| 1528 | const u8 *salt; |
| 1529 | struct wpabuf *rejected_groups = NULL; |
| 1530 | u8 keyseed[SAE_MAX_HASH_LEN]; |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1531 | u8 keys[2 * SAE_MAX_HASH_LEN + SAE_PMK_LEN_MAX]; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1532 | struct crypto_bignum *tmp; |
| 1533 | int ret = -1; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1534 | size_t hash_len, salt_len, prime_len = sae->tmp->prime_len; |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1535 | size_t pmk_len; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1536 | const u8 *addr[1]; |
| 1537 | size_t len[1]; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1538 | |
| 1539 | tmp = crypto_bignum_init(); |
| 1540 | if (tmp == NULL) |
| 1541 | goto fail; |
| 1542 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1543 | /* keyseed = H(salt, k) |
| 1544 | * KCK || PMK = KDF-Hash-Length(keyseed, "SAE KCK and PMK", |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1545 | * (commit-scalar + peer-commit-scalar) modulo r) |
| 1546 | * PMKID = L((commit-scalar + peer-commit-scalar) modulo r, 0, 128) |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1547 | * |
| 1548 | * When SAE-PK is used, |
| 1549 | * KCK || PMK || KEK = KDF-Hash-Length(keyseed, "SAE-PK keys", context) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1550 | */ |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1551 | if (!sae->h2e) |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1552 | hash_len = SHA256_MAC_LEN; |
| 1553 | else if (sae->tmp->dh) |
| 1554 | hash_len = sae_ffc_prime_len_2_hash_len(prime_len); |
| 1555 | else |
| 1556 | hash_len = sae_ecc_prime_len_2_hash_len(prime_len); |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1557 | if (wpa_key_mgmt_sae_ext_key(sae->akmp)) |
| 1558 | pmk_len = hash_len; |
| 1559 | else |
| 1560 | pmk_len = SAE_PMK_LEN; |
| 1561 | wpa_printf(MSG_DEBUG, "SAE: Derive keys - H2E=%d AKMP=0x%x = %08x (%s)", |
| 1562 | sae->h2e, sae->akmp, |
| 1563 | wpa_akm_to_suite(sae->akmp), |
| 1564 | wpa_key_mgmt_txt(sae->akmp, WPA_PROTO_RSN)); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1565 | if (sae->h2e && (sae->tmp->own_rejected_groups || |
| 1566 | sae->tmp->peer_rejected_groups)) { |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1567 | struct wpabuf *own, *peer; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1568 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1569 | own = sae->tmp->own_rejected_groups; |
| 1570 | peer = sae->tmp->peer_rejected_groups; |
| 1571 | salt_len = 0; |
| 1572 | if (own) |
| 1573 | salt_len += wpabuf_len(own); |
| 1574 | if (peer) |
| 1575 | salt_len += wpabuf_len(peer); |
| 1576 | rejected_groups = wpabuf_alloc(salt_len); |
| 1577 | if (!rejected_groups) |
| 1578 | goto fail; |
| 1579 | if (sae->tmp->own_addr_higher) { |
| 1580 | if (own) |
| 1581 | wpabuf_put_buf(rejected_groups, own); |
| 1582 | if (peer) |
| 1583 | wpabuf_put_buf(rejected_groups, peer); |
| 1584 | } else { |
| 1585 | if (peer) |
| 1586 | wpabuf_put_buf(rejected_groups, peer); |
| 1587 | if (own) |
| 1588 | wpabuf_put_buf(rejected_groups, own); |
| 1589 | } |
| 1590 | salt = wpabuf_head(rejected_groups); |
| 1591 | salt_len = wpabuf_len(rejected_groups); |
| 1592 | } else { |
| 1593 | os_memset(zero, 0, hash_len); |
| 1594 | salt = zero; |
| 1595 | salt_len = hash_len; |
| 1596 | } |
| 1597 | wpa_hexdump(MSG_DEBUG, "SAE: salt for keyseed derivation", |
| 1598 | salt, salt_len); |
| 1599 | addr[0] = k; |
| 1600 | len[0] = prime_len; |
| 1601 | if (hkdf_extract(hash_len, salt, salt_len, 1, addr, len, keyseed) < 0) |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1602 | goto fail; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1603 | wpa_hexdump_key(MSG_DEBUG, "SAE: keyseed", keyseed, hash_len); |
| 1604 | |
| 1605 | if (crypto_bignum_add(sae->tmp->own_commit_scalar, |
| 1606 | sae->peer_commit_scalar, tmp) < 0 || |
| 1607 | crypto_bignum_mod(tmp, sae->tmp->order, tmp) < 0) |
| 1608 | goto fail; |
| 1609 | /* IEEE Std 802.11-2016 is not exactly clear on the encoding of the bit |
| 1610 | * string that is needed for KCK, PMK, and PMKID derivation, but it |
| 1611 | * seems to make most sense to encode the |
| 1612 | * (commit-scalar + peer-commit-scalar) mod r part as a bit string by |
| 1613 | * zero padding it from left to the length of the order (in full |
| 1614 | * octets). */ |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1615 | if (crypto_bignum_to_bin(tmp, val, sizeof(val), |
| 1616 | sae->tmp->order_len) < 0) |
| 1617 | goto fail; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1618 | wpa_hexdump(MSG_DEBUG, "SAE: PMKID", val, SAE_PMKID_LEN); |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1619 | |
| 1620 | #ifdef CONFIG_SAE_PK |
| 1621 | if (sae->pk) { |
| 1622 | if (sae_kdf_hash(hash_len, keyseed, "SAE-PK keys", |
| 1623 | val, sae->tmp->order_len, |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1624 | keys, 2 * hash_len + pmk_len) < 0) |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1625 | goto fail; |
| 1626 | } else { |
| 1627 | if (sae_kdf_hash(hash_len, keyseed, "SAE KCK and PMK", |
| 1628 | val, sae->tmp->order_len, |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1629 | keys, hash_len + pmk_len) < 0) |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1630 | goto fail; |
| 1631 | } |
| 1632 | #else /* CONFIG_SAE_PK */ |
| 1633 | if (sae_kdf_hash(hash_len, keyseed, "SAE KCK and PMK", |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1634 | val, sae->tmp->order_len, |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1635 | keys, hash_len + pmk_len) < 0) |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1636 | goto fail; |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1637 | #endif /* !CONFIG_SAE_PK */ |
| 1638 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1639 | forced_memzero(keyseed, sizeof(keyseed)); |
| 1640 | os_memcpy(sae->tmp->kck, keys, hash_len); |
| 1641 | sae->tmp->kck_len = hash_len; |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1642 | os_memcpy(sae->pmk, keys + hash_len, pmk_len); |
| 1643 | sae->pmk_len = pmk_len; |
Dmitry Shmidt | d97138d | 2015-12-28 13:27:49 -0800 | [diff] [blame] | 1644 | os_memcpy(sae->pmkid, val, SAE_PMKID_LEN); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1645 | #ifdef CONFIG_SAE_PK |
| 1646 | if (sae->pk) { |
| 1647 | os_memcpy(sae->tmp->kek, keys + hash_len + SAE_PMK_LEN, |
| 1648 | hash_len); |
| 1649 | sae->tmp->kek_len = hash_len; |
| 1650 | wpa_hexdump_key(MSG_DEBUG, "SAE: KEK for SAE-PK", |
| 1651 | sae->tmp->kek, sae->tmp->kek_len); |
| 1652 | } |
| 1653 | #endif /* CONFIG_SAE_PK */ |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1654 | forced_memzero(keys, sizeof(keys)); |
| 1655 | wpa_hexdump_key(MSG_DEBUG, "SAE: KCK", |
| 1656 | sae->tmp->kck, sae->tmp->kck_len); |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1657 | wpa_hexdump_key(MSG_DEBUG, "SAE: PMK", sae->pmk, sae->pmk_len); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1658 | |
| 1659 | ret = 0; |
| 1660 | fail: |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1661 | wpabuf_free(rejected_groups); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1662 | crypto_bignum_deinit(tmp, 0); |
| 1663 | return ret; |
| 1664 | } |
| 1665 | |
| 1666 | |
| 1667 | int sae_process_commit(struct sae_data *sae) |
| 1668 | { |
| 1669 | u8 k[SAE_MAX_PRIME_LEN]; |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 1670 | if (sae->tmp == NULL || |
| 1671 | (sae->tmp->ec && sae_derive_k_ecc(sae, k) < 0) || |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1672 | (sae->tmp->dh && sae_derive_k_ffc(sae, k) < 0) || |
| 1673 | sae_derive_keys(sae, k) < 0) |
| 1674 | return -1; |
| 1675 | return 0; |
| 1676 | } |
| 1677 | |
| 1678 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1679 | int sae_write_commit(struct sae_data *sae, struct wpabuf *buf, |
| 1680 | const struct wpabuf *token, const char *identifier) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1681 | { |
| 1682 | u8 *pos; |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 1683 | |
| 1684 | if (sae->tmp == NULL) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1685 | return -1; |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 1686 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1687 | wpabuf_put_le16(buf, sae->group); /* Finite Cyclic Group */ |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1688 | if (!sae->h2e && token) { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1689 | wpabuf_put_buf(buf, token); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1690 | wpa_hexdump(MSG_DEBUG, "SAE: Anti-clogging token", |
| 1691 | wpabuf_head(token), wpabuf_len(token)); |
| 1692 | } |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1693 | pos = wpabuf_put(buf, sae->tmp->prime_len); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1694 | if (crypto_bignum_to_bin(sae->tmp->own_commit_scalar, pos, |
| 1695 | sae->tmp->prime_len, sae->tmp->prime_len) < 0) |
| 1696 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1697 | wpa_hexdump(MSG_DEBUG, "SAE: own commit-scalar", |
| 1698 | pos, sae->tmp->prime_len); |
| 1699 | if (sae->tmp->ec) { |
| 1700 | pos = wpabuf_put(buf, 2 * sae->tmp->prime_len); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1701 | if (crypto_ec_point_to_bin(sae->tmp->ec, |
| 1702 | sae->tmp->own_commit_element_ecc, |
| 1703 | pos, pos + sae->tmp->prime_len) < 0) |
| 1704 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1705 | wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(x)", |
| 1706 | pos, sae->tmp->prime_len); |
| 1707 | wpa_hexdump(MSG_DEBUG, "SAE: own commit-element(y)", |
| 1708 | pos + sae->tmp->prime_len, sae->tmp->prime_len); |
| 1709 | } else { |
| 1710 | pos = wpabuf_put(buf, sae->tmp->prime_len); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1711 | if (crypto_bignum_to_bin(sae->tmp->own_commit_element_ffc, pos, |
| 1712 | sae->tmp->prime_len, |
| 1713 | sae->tmp->prime_len) < 0) |
| 1714 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1715 | wpa_hexdump(MSG_DEBUG, "SAE: own commit-element", |
| 1716 | pos, sae->tmp->prime_len); |
| 1717 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1718 | |
| 1719 | if (identifier) { |
| 1720 | /* Password Identifier element */ |
| 1721 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); |
| 1722 | wpabuf_put_u8(buf, 1 + os_strlen(identifier)); |
| 1723 | wpabuf_put_u8(buf, WLAN_EID_EXT_PASSWORD_IDENTIFIER); |
| 1724 | wpabuf_put_str(buf, identifier); |
| 1725 | wpa_printf(MSG_DEBUG, "SAE: own Password Identifier: %s", |
| 1726 | identifier); |
| 1727 | } |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1728 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1729 | if (sae->h2e && sae->tmp->own_rejected_groups) { |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1730 | wpa_hexdump_buf(MSG_DEBUG, "SAE: own Rejected Groups", |
| 1731 | sae->tmp->own_rejected_groups); |
| 1732 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); |
| 1733 | wpabuf_put_u8(buf, |
| 1734 | 1 + wpabuf_len(sae->tmp->own_rejected_groups)); |
| 1735 | wpabuf_put_u8(buf, WLAN_EID_EXT_REJECTED_GROUPS); |
| 1736 | wpabuf_put_buf(buf, sae->tmp->own_rejected_groups); |
| 1737 | } |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1738 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1739 | if (sae->h2e && token) { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1740 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); |
| 1741 | wpabuf_put_u8(buf, 1 + wpabuf_len(token)); |
| 1742 | wpabuf_put_u8(buf, WLAN_EID_EXT_ANTI_CLOGGING_TOKEN); |
| 1743 | wpabuf_put_buf(buf, token); |
| 1744 | wpa_hexdump_buf(MSG_DEBUG, |
| 1745 | "SAE: Anti-clogging token (in container)", |
| 1746 | token); |
| 1747 | } |
| 1748 | |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1749 | if (wpa_key_mgmt_sae_ext_key(sae->akmp)) { |
| 1750 | u32 suite = wpa_akm_to_suite(sae->akmp); |
| 1751 | |
| 1752 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); |
| 1753 | wpabuf_put_u8(buf, 1 + RSN_SELECTOR_LEN); |
| 1754 | wpabuf_put_u8(buf, WLAN_EID_EXT_AKM_SUITE_SELECTOR); |
| 1755 | RSN_SELECTOR_PUT(wpabuf_put(buf, RSN_SELECTOR_LEN), suite); |
| 1756 | wpa_printf(MSG_DEBUG, "SAE: AKM Suite Selector: %08x", suite); |
| 1757 | sae->own_akm_suite_selector = suite; |
| 1758 | } |
| 1759 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1760 | return 0; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1764 | u16 sae_group_allowed(struct sae_data *sae, int *allowed_groups, u16 group) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1765 | { |
| 1766 | if (allowed_groups) { |
| 1767 | int i; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1768 | for (i = 0; allowed_groups[i] > 0; i++) { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1769 | if (allowed_groups[i] == group) |
| 1770 | break; |
| 1771 | } |
| 1772 | if (allowed_groups[i] != group) { |
| 1773 | wpa_printf(MSG_DEBUG, "SAE: Proposed group %u not " |
| 1774 | "enabled in the current configuration", |
| 1775 | group); |
| 1776 | return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED; |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | if (sae->state == SAE_COMMITTED && group != sae->group) { |
| 1781 | wpa_printf(MSG_DEBUG, "SAE: Do not allow group to be changed"); |
| 1782 | return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED; |
| 1783 | } |
| 1784 | |
| 1785 | if (group != sae->group && sae_set_group(sae, group) < 0) { |
| 1786 | wpa_printf(MSG_DEBUG, "SAE: Unsupported Finite Cyclic Group %u", |
| 1787 | group); |
| 1788 | return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED; |
| 1789 | } |
| 1790 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1791 | if (sae->tmp == NULL) { |
| 1792 | wpa_printf(MSG_DEBUG, "SAE: Group information not yet initialized"); |
| 1793 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 1794 | } |
| 1795 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1796 | if (sae->tmp->dh && !allowed_groups) { |
| 1797 | wpa_printf(MSG_DEBUG, "SAE: Do not allow FFC group %u without " |
| 1798 | "explicit configuration enabling it", group); |
| 1799 | return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED; |
| 1800 | } |
| 1801 | |
| 1802 | return WLAN_STATUS_SUCCESS; |
| 1803 | } |
| 1804 | |
| 1805 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1806 | static int sae_is_password_id_elem(const u8 *pos, const u8 *end) |
| 1807 | { |
| 1808 | return end - pos >= 3 && |
| 1809 | pos[0] == WLAN_EID_EXTENSION && |
| 1810 | pos[1] >= 1 && |
| 1811 | end - pos - 2 >= pos[1] && |
| 1812 | pos[2] == WLAN_EID_EXT_PASSWORD_IDENTIFIER; |
| 1813 | } |
| 1814 | |
| 1815 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1816 | static int sae_is_rejected_groups_elem(const u8 *pos, const u8 *end) |
| 1817 | { |
| 1818 | return end - pos >= 3 && |
| 1819 | pos[0] == WLAN_EID_EXTENSION && |
| 1820 | pos[1] >= 2 && |
| 1821 | end - pos - 2 >= pos[1] && |
| 1822 | pos[2] == WLAN_EID_EXT_REJECTED_GROUPS; |
| 1823 | } |
| 1824 | |
| 1825 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1826 | static int sae_is_token_container_elem(const u8 *pos, const u8 *end) |
| 1827 | { |
| 1828 | return end - pos >= 3 && |
| 1829 | pos[0] == WLAN_EID_EXTENSION && |
| 1830 | pos[1] >= 1 && |
| 1831 | end - pos - 2 >= pos[1] && |
| 1832 | pos[2] == WLAN_EID_EXT_ANTI_CLOGGING_TOKEN; |
| 1833 | } |
| 1834 | |
| 1835 | |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 1836 | static int sae_is_akm_suite_selector_elem(const u8 *pos, const u8 *end) |
| 1837 | { |
| 1838 | return end - pos >= 2 + 1 + RSN_SELECTOR_LEN && |
| 1839 | pos[0] == WLAN_EID_EXTENSION && |
| 1840 | pos[1] >= 1 + RSN_SELECTOR_LEN && |
| 1841 | end - pos - 2 >= pos[1] && |
| 1842 | pos[2] == WLAN_EID_EXT_AKM_SUITE_SELECTOR; |
| 1843 | } |
| 1844 | |
| 1845 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1846 | static void sae_parse_commit_token(struct sae_data *sae, const u8 **pos, |
| 1847 | const u8 *end, const u8 **token, |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1848 | size_t *token_len, int h2e) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1849 | { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1850 | size_t scalar_elem_len, tlen; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1851 | |
| 1852 | if (token) |
| 1853 | *token = NULL; |
| 1854 | if (token_len) |
| 1855 | *token_len = 0; |
| 1856 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1857 | if (h2e) |
| 1858 | return; /* No Anti-Clogging Token field outside container IE */ |
| 1859 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1860 | scalar_elem_len = (sae->tmp->ec ? 3 : 2) * sae->tmp->prime_len; |
| 1861 | if (scalar_elem_len >= (size_t) (end - *pos)) |
| 1862 | return; /* No extra data beyond peer scalar and element */ |
| 1863 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1864 | tlen = end - (*pos + scalar_elem_len); |
| 1865 | |
| 1866 | if (tlen < SHA256_MAC_LEN) { |
| 1867 | wpa_printf(MSG_DEBUG, |
| 1868 | "SAE: Too short optional data (%u octets) to include our Anti-Clogging Token", |
| 1869 | (unsigned int) tlen); |
| 1870 | return; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1871 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1872 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1873 | wpa_hexdump(MSG_DEBUG, "SAE: Anti-Clogging Token", *pos, tlen); |
| 1874 | if (token) |
| 1875 | *token = *pos; |
| 1876 | if (token_len) |
| 1877 | *token_len = tlen; |
| 1878 | *pos += tlen; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1882 | static void sae_parse_token_container(struct sae_data *sae, |
| 1883 | const u8 *pos, const u8 *end, |
| 1884 | const u8 **token, size_t *token_len) |
| 1885 | { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1886 | if (!sae_is_token_container_elem(pos, end)) |
| 1887 | return; |
| 1888 | *token = pos + 3; |
| 1889 | *token_len = pos[1] - 1; |
| 1890 | wpa_hexdump(MSG_DEBUG, "SAE: Anti-Clogging Token (in container)", |
| 1891 | *token, *token_len); |
| 1892 | } |
| 1893 | |
| 1894 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1895 | static u16 sae_parse_commit_scalar(struct sae_data *sae, const u8 **pos, |
| 1896 | const u8 *end) |
| 1897 | { |
| 1898 | struct crypto_bignum *peer_scalar; |
| 1899 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1900 | if (sae->tmp->prime_len > end - *pos) { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1901 | wpa_printf(MSG_DEBUG, "SAE: Not enough data for scalar"); |
| 1902 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 1903 | } |
| 1904 | |
| 1905 | peer_scalar = crypto_bignum_init_set(*pos, sae->tmp->prime_len); |
| 1906 | if (peer_scalar == NULL) |
| 1907 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 1908 | |
| 1909 | /* |
| 1910 | * IEEE Std 802.11-2012, 11.3.8.6.1: If there is a protocol instance for |
| 1911 | * the peer and it is in Authenticated state, the new Commit Message |
| 1912 | * shall be dropped if the peer-scalar is identical to the one used in |
| 1913 | * the existing protocol instance. |
| 1914 | */ |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1915 | if (sae->state == SAE_ACCEPTED && sae->peer_commit_scalar_accepted && |
| 1916 | crypto_bignum_cmp(sae->peer_commit_scalar_accepted, |
| 1917 | peer_scalar) == 0) { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1918 | wpa_printf(MSG_DEBUG, "SAE: Do not accept re-use of previous " |
| 1919 | "peer-commit-scalar"); |
| 1920 | crypto_bignum_deinit(peer_scalar, 0); |
| 1921 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 1922 | } |
| 1923 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1924 | /* 1 < scalar < r */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1925 | if (crypto_bignum_is_zero(peer_scalar) || |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1926 | crypto_bignum_is_one(peer_scalar) || |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1927 | crypto_bignum_cmp(peer_scalar, sae->tmp->order) >= 0) { |
| 1928 | wpa_printf(MSG_DEBUG, "SAE: Invalid peer scalar"); |
| 1929 | crypto_bignum_deinit(peer_scalar, 0); |
| 1930 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | crypto_bignum_deinit(sae->peer_commit_scalar, 0); |
| 1935 | sae->peer_commit_scalar = peer_scalar; |
| 1936 | wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-scalar", |
| 1937 | *pos, sae->tmp->prime_len); |
| 1938 | *pos += sae->tmp->prime_len; |
| 1939 | |
| 1940 | return WLAN_STATUS_SUCCESS; |
| 1941 | } |
| 1942 | |
| 1943 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1944 | static u16 sae_parse_commit_element_ecc(struct sae_data *sae, const u8 **pos, |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1945 | const u8 *end) |
| 1946 | { |
| 1947 | u8 prime[SAE_MAX_ECC_PRIME_LEN]; |
| 1948 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1949 | if (2 * sae->tmp->prime_len > end - *pos) { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1950 | wpa_printf(MSG_DEBUG, "SAE: Not enough data for " |
| 1951 | "commit-element"); |
| 1952 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 1953 | } |
| 1954 | |
| 1955 | if (crypto_bignum_to_bin(sae->tmp->prime, prime, sizeof(prime), |
| 1956 | sae->tmp->prime_len) < 0) |
| 1957 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 1958 | |
| 1959 | /* element x and y coordinates < p */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1960 | if (os_memcmp(*pos, prime, sae->tmp->prime_len) >= 0 || |
| 1961 | os_memcmp(*pos + sae->tmp->prime_len, prime, |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1962 | sae->tmp->prime_len) >= 0) { |
| 1963 | wpa_printf(MSG_DEBUG, "SAE: Invalid coordinates in peer " |
| 1964 | "element"); |
| 1965 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 1966 | } |
| 1967 | |
| 1968 | wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(x)", |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1969 | *pos, sae->tmp->prime_len); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1970 | wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(y)", |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1971 | *pos + sae->tmp->prime_len, sae->tmp->prime_len); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1972 | |
| 1973 | crypto_ec_point_deinit(sae->tmp->peer_commit_element_ecc, 0); |
| 1974 | sae->tmp->peer_commit_element_ecc = |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1975 | crypto_ec_point_from_bin(sae->tmp->ec, *pos); |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1976 | if (!sae->tmp->peer_commit_element_ecc) { |
| 1977 | wpa_printf(MSG_DEBUG, "SAE: Peer element is not a valid point"); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1978 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1979 | } |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1980 | |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 1981 | if (!crypto_ec_point_is_on_curve(sae->tmp->ec, |
| 1982 | sae->tmp->peer_commit_element_ecc)) { |
| 1983 | wpa_printf(MSG_DEBUG, "SAE: Peer element is not on curve"); |
| 1984 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 1985 | } |
| 1986 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1987 | *pos += 2 * sae->tmp->prime_len; |
| 1988 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1989 | return WLAN_STATUS_SUCCESS; |
| 1990 | } |
| 1991 | |
| 1992 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1993 | static u16 sae_parse_commit_element_ffc(struct sae_data *sae, const u8 **pos, |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1994 | const u8 *end) |
| 1995 | { |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1996 | struct crypto_bignum *res, *one; |
| 1997 | const u8 one_bin[1] = { 0x01 }; |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 1998 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1999 | if (sae->tmp->prime_len > end - *pos) { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2000 | wpa_printf(MSG_DEBUG, "SAE: Not enough data for " |
| 2001 | "commit-element"); |
| 2002 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 2003 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2004 | wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element", *pos, |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2005 | sae->tmp->prime_len); |
| 2006 | |
| 2007 | crypto_bignum_deinit(sae->tmp->peer_commit_element_ffc, 0); |
| 2008 | sae->tmp->peer_commit_element_ffc = |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2009 | crypto_bignum_init_set(*pos, sae->tmp->prime_len); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2010 | if (sae->tmp->peer_commit_element_ffc == NULL) |
| 2011 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2012 | /* 1 < element < p - 1 */ |
| 2013 | res = crypto_bignum_init(); |
| 2014 | one = crypto_bignum_init_set(one_bin, sizeof(one_bin)); |
| 2015 | if (!res || !one || |
| 2016 | crypto_bignum_sub(sae->tmp->prime, one, res) || |
| 2017 | crypto_bignum_is_zero(sae->tmp->peer_commit_element_ffc) || |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2018 | crypto_bignum_is_one(sae->tmp->peer_commit_element_ffc) || |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2019 | crypto_bignum_cmp(sae->tmp->peer_commit_element_ffc, res) >= 0) { |
| 2020 | crypto_bignum_deinit(res, 0); |
| 2021 | crypto_bignum_deinit(one, 0); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2022 | wpa_printf(MSG_DEBUG, "SAE: Invalid peer element"); |
| 2023 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 2024 | } |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2025 | crypto_bignum_deinit(one, 0); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2026 | |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 2027 | /* scalar-op(r, ELEMENT) = 1 modulo p */ |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2028 | if (crypto_bignum_exptmod(sae->tmp->peer_commit_element_ffc, |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 2029 | sae->tmp->order, sae->tmp->prime, res) < 0 || |
| 2030 | !crypto_bignum_is_one(res)) { |
| 2031 | wpa_printf(MSG_DEBUG, "SAE: Invalid peer element (scalar-op)"); |
| 2032 | crypto_bignum_deinit(res, 0); |
| 2033 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 2034 | } |
| 2035 | crypto_bignum_deinit(res, 0); |
| 2036 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2037 | *pos += sae->tmp->prime_len; |
| 2038 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2039 | return WLAN_STATUS_SUCCESS; |
| 2040 | } |
| 2041 | |
| 2042 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2043 | static u16 sae_parse_commit_element(struct sae_data *sae, const u8 **pos, |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2044 | const u8 *end) |
| 2045 | { |
| 2046 | if (sae->tmp->dh) |
| 2047 | return sae_parse_commit_element_ffc(sae, pos, end); |
| 2048 | return sae_parse_commit_element_ecc(sae, pos, end); |
| 2049 | } |
| 2050 | |
| 2051 | |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 2052 | static int sae_parse_password_identifier(struct sae_data *sae, bool h2e, |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2053 | const u8 **pos, const u8 *end) |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2054 | { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2055 | const u8 *epos; |
| 2056 | u8 len; |
| 2057 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2058 | if (!sae_is_password_id_elem(*pos, end)) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2059 | if (sae->tmp->pw_id) { |
| 2060 | wpa_printf(MSG_DEBUG, |
| 2061 | "SAE: No Password Identifier included, but expected one (%s)", |
| 2062 | sae->tmp->pw_id); |
| 2063 | return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER; |
| 2064 | } |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 2065 | os_free(sae->tmp->parsed_pw_id); |
| 2066 | sae->tmp->parsed_pw_id = NULL; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2067 | return WLAN_STATUS_SUCCESS; /* No Password Identifier */ |
| 2068 | } |
| 2069 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2070 | epos = *pos; |
| 2071 | epos++; /* skip IE type */ |
| 2072 | len = *epos++; /* IE length */ |
| 2073 | if (len > end - epos || len < 1) |
| 2074 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 2075 | epos++; /* skip ext ID */ |
| 2076 | len--; |
| 2077 | |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 2078 | if (!h2e) { |
| 2079 | wpa_printf(MSG_DEBUG, |
| 2080 | "SAE: Password Identifier included, but H2E is not used"); |
| 2081 | return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER; |
| 2082 | } |
| 2083 | |
| 2084 | if (sae->no_pw_id) { |
| 2085 | wpa_printf(MSG_DEBUG, |
| 2086 | "SAE: Password Identifier included, but none has been enabled"); |
| 2087 | return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER; |
| 2088 | } |
| 2089 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2090 | if (sae->tmp->pw_id && |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2091 | (len != os_strlen(sae->tmp->pw_id) || |
| 2092 | os_memcmp(sae->tmp->pw_id, epos, len) != 0)) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2093 | wpa_printf(MSG_DEBUG, |
| 2094 | "SAE: The included Password Identifier does not match the expected one (%s)", |
| 2095 | sae->tmp->pw_id); |
| 2096 | return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER; |
| 2097 | } |
| 2098 | |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 2099 | os_free(sae->tmp->parsed_pw_id); |
| 2100 | sae->tmp->parsed_pw_id = os_malloc(len + 1); |
| 2101 | if (!sae->tmp->parsed_pw_id) |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2102 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 2103 | os_memcpy(sae->tmp->parsed_pw_id, epos, len); |
| 2104 | sae->tmp->parsed_pw_id[len] = '\0'; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2105 | wpa_hexdump_ascii(MSG_DEBUG, "SAE: Received Password Identifier", |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 2106 | sae->tmp->parsed_pw_id, len); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2107 | *pos = epos + len; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2108 | return WLAN_STATUS_SUCCESS; |
| 2109 | } |
| 2110 | |
| 2111 | |
| 2112 | static int sae_parse_rejected_groups(struct sae_data *sae, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2113 | const u8 **pos, const u8 *end) |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2114 | { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2115 | const u8 *epos; |
| 2116 | u8 len; |
| 2117 | |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 2118 | if (!sae_is_rejected_groups_elem(*pos, end)) { |
| 2119 | wpabuf_free(sae->tmp->peer_rejected_groups); |
| 2120 | sae->tmp->peer_rejected_groups = NULL; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2121 | return WLAN_STATUS_SUCCESS; |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 2122 | } |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2123 | |
| 2124 | epos = *pos; |
| 2125 | epos++; /* skip IE type */ |
| 2126 | len = *epos++; /* IE length */ |
| 2127 | if (len > end - epos || len < 1) |
| 2128 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 2129 | epos++; /* skip ext ID */ |
| 2130 | len--; |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 2131 | if (len & 1) { |
| 2132 | wpa_printf(MSG_DEBUG, |
| 2133 | "SAE: Invalid length of the Rejected Groups element payload: %u", |
| 2134 | len); |
| 2135 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 2136 | } |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2137 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2138 | wpabuf_free(sae->tmp->peer_rejected_groups); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2139 | sae->tmp->peer_rejected_groups = wpabuf_alloc(len); |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2140 | if (!sae->tmp->peer_rejected_groups) |
| 2141 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2142 | wpabuf_put_data(sae->tmp->peer_rejected_groups, epos, len); |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2143 | wpa_hexdump_buf(MSG_DEBUG, "SAE: Received Rejected Groups list", |
| 2144 | sae->tmp->peer_rejected_groups); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2145 | *pos = epos + len; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2146 | return WLAN_STATUS_SUCCESS; |
| 2147 | } |
| 2148 | |
| 2149 | |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 2150 | static int sae_parse_akm_suite_selector(struct sae_data *sae, |
| 2151 | const u8 **pos, const u8 *end) |
| 2152 | { |
| 2153 | const u8 *epos; |
| 2154 | u8 len; |
| 2155 | |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 2156 | if (!sae_is_akm_suite_selector_elem(*pos, end)) |
| 2157 | return WLAN_STATUS_SUCCESS; |
| 2158 | |
| 2159 | epos = *pos; |
| 2160 | epos++; /* skip IE type */ |
| 2161 | len = *epos++; /* IE length */ |
| 2162 | if (len > end - epos || len < 1) |
| 2163 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 2164 | epos++; /* skip ext ID */ |
| 2165 | len--; |
| 2166 | |
| 2167 | if (len < RSN_SELECTOR_LEN) |
| 2168 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 2169 | sae->peer_akm_suite_selector = RSN_SELECTOR_GET(epos); |
| 2170 | wpa_printf(MSG_DEBUG, "SAE: Received AKM Suite Selector: %08x", |
| 2171 | sae->peer_akm_suite_selector); |
| 2172 | *pos = epos + len; |
| 2173 | return WLAN_STATUS_SUCCESS; |
| 2174 | } |
| 2175 | |
| 2176 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2177 | u16 sae_parse_commit(struct sae_data *sae, const u8 *data, size_t len, |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2178 | const u8 **token, size_t *token_len, int *allowed_groups, |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 2179 | int h2e, int *ie_offset) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2180 | { |
| 2181 | const u8 *pos = data, *end = data + len; |
| 2182 | u16 res; |
| 2183 | |
| 2184 | /* Check Finite Cyclic Group */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2185 | if (end - pos < 2) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2186 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 2187 | res = sae_group_allowed(sae, allowed_groups, WPA_GET_LE16(pos)); |
| 2188 | if (res != WLAN_STATUS_SUCCESS) |
| 2189 | return res; |
| 2190 | pos += 2; |
| 2191 | |
| 2192 | /* Optional Anti-Clogging Token */ |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2193 | sae_parse_commit_token(sae, &pos, end, token, token_len, h2e); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2194 | |
| 2195 | /* commit-scalar */ |
| 2196 | res = sae_parse_commit_scalar(sae, &pos, end); |
| 2197 | if (res != WLAN_STATUS_SUCCESS) |
| 2198 | return res; |
| 2199 | |
| 2200 | /* commit-element */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2201 | res = sae_parse_commit_element(sae, &pos, end); |
| 2202 | if (res != WLAN_STATUS_SUCCESS) |
| 2203 | return res; |
| 2204 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 2205 | if (ie_offset) |
| 2206 | *ie_offset = pos - data; |
| 2207 | |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 2208 | if (end > pos) |
| 2209 | wpa_hexdump(MSG_DEBUG, |
| 2210 | "SAE: Possible elements at the end of the frame", |
| 2211 | pos, end - pos); |
| 2212 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2213 | /* Optional Password Identifier element */ |
Sunil Ravi | 79e6c4f | 2025-01-04 00:47:06 +0000 | [diff] [blame] | 2214 | res = sae_parse_password_identifier(sae, h2e, &pos, end); |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2215 | if (res != WLAN_STATUS_SUCCESS) |
| 2216 | return res; |
| 2217 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2218 | /* Conditional Rejected Groups element */ |
| 2219 | if (h2e) { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2220 | res = sae_parse_rejected_groups(sae, &pos, end); |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2221 | if (res != WLAN_STATUS_SUCCESS) |
| 2222 | return res; |
Sunil Ravi | 7f76929 | 2024-07-23 22:21:32 +0000 | [diff] [blame] | 2223 | } else { |
| 2224 | wpabuf_free(sae->tmp->peer_rejected_groups); |
| 2225 | sae->tmp->peer_rejected_groups = NULL; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2228 | /* Optional Anti-Clogging Token Container element */ |
| 2229 | if (h2e) |
| 2230 | sae_parse_token_container(sae, pos, end, token, token_len); |
| 2231 | |
Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 2232 | /* Conditional AKM Suite Selector element */ |
| 2233 | if (h2e) { |
| 2234 | res = sae_parse_akm_suite_selector(sae, &pos, end); |
| 2235 | if (res != WLAN_STATUS_SUCCESS) |
| 2236 | return res; |
| 2237 | } |
| 2238 | |
| 2239 | if (sae->own_akm_suite_selector && |
| 2240 | sae->own_akm_suite_selector != sae->peer_akm_suite_selector) { |
| 2241 | wpa_printf(MSG_DEBUG, |
| 2242 | "SAE: AKM suite selector mismatch: own=%08x peer=%08x", |
| 2243 | sae->own_akm_suite_selector, |
| 2244 | sae->peer_akm_suite_selector); |
| 2245 | return WLAN_STATUS_UNSPECIFIED_FAILURE; |
| 2246 | } |
| 2247 | |
| 2248 | if (!sae->akmp) { |
| 2249 | if (sae->peer_akm_suite_selector == |
| 2250 | RSN_AUTH_KEY_MGMT_SAE_EXT_KEY) |
| 2251 | sae->akmp = WPA_KEY_MGMT_SAE_EXT_KEY; |
| 2252 | else if (sae->peer_akm_suite_selector == |
| 2253 | RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY) |
| 2254 | sae->akmp = WPA_KEY_MGMT_FT_SAE_EXT_KEY; |
| 2255 | } |
| 2256 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2257 | /* |
| 2258 | * Check whether peer-commit-scalar and PEER-COMMIT-ELEMENT are same as |
| 2259 | * the values we sent which would be evidence of a reflection attack. |
| 2260 | */ |
| 2261 | if (!sae->tmp->own_commit_scalar || |
| 2262 | crypto_bignum_cmp(sae->tmp->own_commit_scalar, |
| 2263 | sae->peer_commit_scalar) != 0 || |
| 2264 | (sae->tmp->dh && |
| 2265 | (!sae->tmp->own_commit_element_ffc || |
| 2266 | crypto_bignum_cmp(sae->tmp->own_commit_element_ffc, |
| 2267 | sae->tmp->peer_commit_element_ffc) != 0)) || |
| 2268 | (sae->tmp->ec && |
| 2269 | (!sae->tmp->own_commit_element_ecc || |
| 2270 | crypto_ec_point_cmp(sae->tmp->ec, |
| 2271 | sae->tmp->own_commit_element_ecc, |
| 2272 | sae->tmp->peer_commit_element_ecc) != 0))) |
| 2273 | return WLAN_STATUS_SUCCESS; /* scalars/elements are different */ |
| 2274 | |
| 2275 | /* |
| 2276 | * This is a reflection attack - return special value to trigger caller |
| 2277 | * to silently discard the frame instead of replying with a specific |
| 2278 | * status code. |
| 2279 | */ |
| 2280 | return SAE_SILENTLY_DISCARD; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2284 | static int sae_cn_confirm(struct sae_data *sae, const u8 *sc, |
| 2285 | const struct crypto_bignum *scalar1, |
| 2286 | const u8 *element1, size_t element1_len, |
| 2287 | const struct crypto_bignum *scalar2, |
| 2288 | const u8 *element2, size_t element2_len, |
| 2289 | u8 *confirm) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2290 | { |
| 2291 | const u8 *addr[5]; |
| 2292 | size_t len[5]; |
| 2293 | u8 scalar_b1[SAE_MAX_PRIME_LEN], scalar_b2[SAE_MAX_PRIME_LEN]; |
| 2294 | |
| 2295 | /* Confirm |
| 2296 | * CN(key, X, Y, Z, ...) = |
| 2297 | * HMAC-SHA256(key, D2OS(X) || D2OS(Y) || D2OS(Z) | ...) |
| 2298 | * confirm = CN(KCK, send-confirm, commit-scalar, COMMIT-ELEMENT, |
| 2299 | * peer-commit-scalar, PEER-COMMIT-ELEMENT) |
| 2300 | * verifier = CN(KCK, peer-send-confirm, peer-commit-scalar, |
| 2301 | * PEER-COMMIT-ELEMENT, commit-scalar, COMMIT-ELEMENT) |
| 2302 | */ |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2303 | if (crypto_bignum_to_bin(scalar1, scalar_b1, sizeof(scalar_b1), |
| 2304 | sae->tmp->prime_len) < 0 || |
| 2305 | crypto_bignum_to_bin(scalar2, scalar_b2, sizeof(scalar_b2), |
| 2306 | sae->tmp->prime_len) < 0) |
| 2307 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2308 | addr[0] = sc; |
| 2309 | len[0] = 2; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2310 | addr[1] = scalar_b1; |
| 2311 | len[1] = sae->tmp->prime_len; |
| 2312 | addr[2] = element1; |
| 2313 | len[2] = element1_len; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2314 | addr[3] = scalar_b2; |
| 2315 | len[3] = sae->tmp->prime_len; |
| 2316 | addr[4] = element2; |
| 2317 | len[4] = element2_len; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2318 | return hkdf_extract(sae->tmp->kck_len, sae->tmp->kck, sae->tmp->kck_len, |
| 2319 | 5, addr, len, confirm); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2323 | static int sae_cn_confirm_ecc(struct sae_data *sae, const u8 *sc, |
| 2324 | const struct crypto_bignum *scalar1, |
| 2325 | const struct crypto_ec_point *element1, |
| 2326 | const struct crypto_bignum *scalar2, |
| 2327 | const struct crypto_ec_point *element2, |
| 2328 | u8 *confirm) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2329 | { |
| 2330 | u8 element_b1[2 * SAE_MAX_ECC_PRIME_LEN]; |
| 2331 | u8 element_b2[2 * SAE_MAX_ECC_PRIME_LEN]; |
| 2332 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2333 | if (crypto_ec_point_to_bin(sae->tmp->ec, element1, element_b1, |
| 2334 | element_b1 + sae->tmp->prime_len) < 0 || |
| 2335 | crypto_ec_point_to_bin(sae->tmp->ec, element2, element_b2, |
| 2336 | element_b2 + sae->tmp->prime_len) < 0 || |
| 2337 | sae_cn_confirm(sae, sc, scalar1, element_b1, |
| 2338 | 2 * sae->tmp->prime_len, |
| 2339 | scalar2, element_b2, 2 * sae->tmp->prime_len, |
| 2340 | confirm) < 0) |
| 2341 | return -1; |
| 2342 | return 0; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2346 | static int sae_cn_confirm_ffc(struct sae_data *sae, const u8 *sc, |
| 2347 | const struct crypto_bignum *scalar1, |
| 2348 | const struct crypto_bignum *element1, |
| 2349 | const struct crypto_bignum *scalar2, |
| 2350 | const struct crypto_bignum *element2, |
| 2351 | u8 *confirm) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2352 | { |
| 2353 | u8 element_b1[SAE_MAX_PRIME_LEN]; |
| 2354 | u8 element_b2[SAE_MAX_PRIME_LEN]; |
| 2355 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2356 | if (crypto_bignum_to_bin(element1, element_b1, sizeof(element_b1), |
| 2357 | sae->tmp->prime_len) < 0 || |
| 2358 | crypto_bignum_to_bin(element2, element_b2, sizeof(element_b2), |
| 2359 | sae->tmp->prime_len) < 0 || |
| 2360 | sae_cn_confirm(sae, sc, scalar1, element_b1, sae->tmp->prime_len, |
| 2361 | scalar2, element_b2, sae->tmp->prime_len, |
| 2362 | confirm) < 0) |
| 2363 | return -1; |
| 2364 | return 0; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2368 | int sae_write_confirm(struct sae_data *sae, struct wpabuf *buf) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2369 | { |
| 2370 | const u8 *sc; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2371 | size_t hash_len; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2372 | int res; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2373 | |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 2374 | if (sae->tmp == NULL) |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2375 | return -1; |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 2376 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2377 | hash_len = sae->tmp->kck_len; |
| 2378 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2379 | /* Send-Confirm */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2380 | if (sae->send_confirm < 0xffff) |
| 2381 | sae->send_confirm++; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2382 | sc = wpabuf_put(buf, 0); |
| 2383 | wpabuf_put_le16(buf, sae->send_confirm); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2384 | |
| 2385 | if (sae->tmp->ec) |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2386 | res = sae_cn_confirm_ecc(sae, sc, sae->tmp->own_commit_scalar, |
| 2387 | sae->tmp->own_commit_element_ecc, |
| 2388 | sae->peer_commit_scalar, |
| 2389 | sae->tmp->peer_commit_element_ecc, |
| 2390 | wpabuf_put(buf, hash_len)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2391 | else |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2392 | res = sae_cn_confirm_ffc(sae, sc, sae->tmp->own_commit_scalar, |
| 2393 | sae->tmp->own_commit_element_ffc, |
| 2394 | sae->peer_commit_scalar, |
| 2395 | sae->tmp->peer_commit_element_ffc, |
| 2396 | wpabuf_put(buf, hash_len)); |
| 2397 | if (res) |
| 2398 | return res; |
| 2399 | |
| 2400 | #ifdef CONFIG_SAE_PK |
| 2401 | if (sae_write_confirm_pk(sae, buf) < 0) |
| 2402 | return -1; |
| 2403 | #endif /* CONFIG_SAE_PK */ |
| 2404 | |
| 2405 | return 0; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 2409 | int sae_check_confirm(struct sae_data *sae, const u8 *data, size_t len, |
| 2410 | int *ie_offset) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2411 | { |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2412 | u8 verifier[SAE_MAX_HASH_LEN]; |
| 2413 | size_t hash_len; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2414 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2415 | if (!sae->tmp) |
| 2416 | return -1; |
| 2417 | |
| 2418 | hash_len = sae->tmp->kck_len; |
| 2419 | if (len < 2 + hash_len) { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2420 | wpa_printf(MSG_DEBUG, "SAE: Too short confirm message"); |
| 2421 | return -1; |
| 2422 | } |
| 2423 | |
| 2424 | wpa_printf(MSG_DEBUG, "SAE: peer-send-confirm %u", WPA_GET_LE16(data)); |
| 2425 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2426 | if (!sae->peer_commit_scalar || !sae->tmp->own_commit_scalar) { |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 2427 | wpa_printf(MSG_DEBUG, "SAE: Temporary data not yet available"); |
| 2428 | return -1; |
| 2429 | } |
| 2430 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2431 | if (sae->tmp->ec) { |
| 2432 | if (!sae->tmp->peer_commit_element_ecc || |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2433 | !sae->tmp->own_commit_element_ecc || |
| 2434 | sae_cn_confirm_ecc(sae, data, sae->peer_commit_scalar, |
| 2435 | sae->tmp->peer_commit_element_ecc, |
| 2436 | sae->tmp->own_commit_scalar, |
| 2437 | sae->tmp->own_commit_element_ecc, |
| 2438 | verifier) < 0) |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2439 | return -1; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2440 | } else { |
| 2441 | if (!sae->tmp->peer_commit_element_ffc || |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2442 | !sae->tmp->own_commit_element_ffc || |
| 2443 | sae_cn_confirm_ffc(sae, data, sae->peer_commit_scalar, |
| 2444 | sae->tmp->peer_commit_element_ffc, |
| 2445 | sae->tmp->own_commit_scalar, |
| 2446 | sae->tmp->own_commit_element_ffc, |
| 2447 | verifier) < 0) |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2448 | return -1; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2449 | } |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2450 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2451 | if (os_memcmp_const(verifier, data + 2, hash_len) != 0) { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2452 | wpa_printf(MSG_DEBUG, "SAE: Confirm mismatch"); |
| 2453 | wpa_hexdump(MSG_DEBUG, "SAE: Received confirm", |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2454 | data + 2, hash_len); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2455 | wpa_hexdump(MSG_DEBUG, "SAE: Calculated verifier", |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2456 | verifier, hash_len); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2457 | return -1; |
| 2458 | } |
| 2459 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2460 | #ifdef CONFIG_SAE_PK |
| 2461 | if (sae_check_confirm_pk(sae, data + 2 + hash_len, |
| 2462 | len - 2 - hash_len) < 0) |
| 2463 | return -1; |
| 2464 | #endif /* CONFIG_SAE_PK */ |
| 2465 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 2466 | /* 2 bytes are for send-confirm, then the hash, followed by IEs */ |
| 2467 | if (ie_offset) |
| 2468 | *ie_offset = 2 + hash_len; |
| 2469 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2470 | return 0; |
| 2471 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2472 | |
| 2473 | |
| 2474 | const char * sae_state_txt(enum sae_state state) |
| 2475 | { |
| 2476 | switch (state) { |
| 2477 | case SAE_NOTHING: |
| 2478 | return "Nothing"; |
| 2479 | case SAE_COMMITTED: |
| 2480 | return "Committed"; |
| 2481 | case SAE_CONFIRMED: |
| 2482 | return "Confirmed"; |
| 2483 | case SAE_ACCEPTED: |
| 2484 | return "Accepted"; |
| 2485 | } |
| 2486 | return "?"; |
| 2487 | } |