2 * Simultaneous authentication of equals
3 * Copyright (c) 2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
10 /* TODO: move OpenSSL dependencies into crypto/crypto_openssl.c */
11 #include <openssl/bn.h>
12 #include <openssl/ec.h>
13 #include <openssl/obj_mac.h>
16 #include "crypto/sha256.h"
17 #include "crypto/random.h"
18 #include "ieee802_11_defs.h"
22 static const u8 group19_prime[] = {
23 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
24 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
26 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
29 static const u8 group19_order[] = {
30 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
31 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
32 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
33 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
37 static int val_zero_or_one(const u8 *val, size_t len)
41 for (i = 0; i < len - 1; i++) {
46 return val[len - 1] <= 1;
50 static int val_zero(const u8 *val, size_t len)
53 for (i = 0; i < len; i++) {
61 static int sae_get_rand(u8 *val)
66 if (random_get_bytes(val, sizeof(group19_prime)) < 0)
70 } while (os_memcmp(val, group19_order, sizeof(group19_prime)) >= 0 ||
71 val_zero_or_one(val, sizeof(group19_prime)));
77 static EC_POINT * alloc_elem(EC_GROUP *group, const u8 *val, size_t len)
82 x = BN_bin2bn(val, len, NULL);
83 y = BN_bin2bn(val + len, len, NULL);
84 elem = EC_POINT_new(group);
85 if (x == NULL || y == NULL || elem == NULL) {
92 if (!EC_POINT_set_affine_coordinates_GFp(group, elem, x, y, NULL)) {
104 static void sae_bn_to_bin(const BIGNUM *bn, u8 *bin, size_t len)
106 int offset = len - BN_num_bytes(bn);
107 os_memset(bin, 0, offset);
108 BN_bn2bin(bn, bin + offset);
112 static int sae_ec_point_to_bin(BN_CTX *bnctx, EC_GROUP *group, EC_POINT *point,
122 EC_POINT_get_affine_coordinates_GFp(group, point, x, y, bnctx)) {
123 sae_bn_to_bin(x, bin, 32);
124 sae_bn_to_bin(y, bin + 32, 32);
134 static void sae_pwd_seed_key(const u8 *addr1, const u8 *addr2, u8 *key)
136 wpa_printf(MSG_DEBUG, "SAE: PWE derivation - addr1=" MACSTR
137 " addr2=" MACSTR, MAC2STR(addr1), MAC2STR(addr2));
138 if (os_memcmp(addr1, addr2, ETH_ALEN) > 0) {
139 os_memcpy(key, addr1, ETH_ALEN);
140 os_memcpy(key + ETH_ALEN, addr2, ETH_ALEN);
142 os_memcpy(key, addr2, ETH_ALEN);
143 os_memcpy(key + ETH_ALEN, addr1, ETH_ALEN);
148 static int sae_test_pwd_seed(BN_CTX *bnctx, EC_GROUP *group, const u8 *pwd_seed,
149 EC_POINT *pwe, u8 *pwe_bin)
155 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-seed", pwd_seed, 32);
157 /* pwd-value = KDF-z(pwd-seed, "SAE Hunting and Pecking", p) */
158 sha256_prf(pwd_seed, 32, "SAE Hunting and Pecking",
159 group19_prime, sizeof(group19_prime),
160 pwd_value, sizeof(pwd_value));
161 wpa_hexdump_key(MSG_DEBUG, "SAE: pwd-value",
162 pwd_value, sizeof(pwd_value));
164 if (os_memcmp(pwd_value, group19_prime, sizeof(group19_prime)) >= 0)
167 y_bit = pwd_seed[SHA256_MAC_LEN - 1] & 0x01;
169 x = BN_bin2bn(pwd_value, sizeof(pwd_value), NULL);
172 if (!EC_POINT_set_compressed_coordinates_GFp(group, pwe, x, y_bit,
174 !EC_POINT_is_on_curve(group, pwe, bnctx)) {
176 wpa_printf(MSG_DEBUG, "SAE: No solution found");
181 wpa_printf(MSG_DEBUG, "SAE: PWE found");
183 if (sae_ec_point_to_bin(bnctx, group, pwe, pwe_bin) < 0)
186 wpa_hexdump_key(MSG_DEBUG, "SAE: PWE x", pwe_bin, 32);
187 wpa_hexdump_key(MSG_DEBUG, "SAE: PWE y", pwe_bin + 32, 32);
192 static int sae_derive_pwe(BN_CTX *bnctx, EC_GROUP *group, const u8 *addr1,
193 const u8 *addr2, const u8 *password,
194 size_t password_len, EC_POINT *pwe, u8 *pwe_bin)
197 u8 addrs[2 * ETH_ALEN];
202 u8 pwe_bin_tmp[2 * 32];
204 pwe_tmp = EC_POINT_new(group);
208 wpa_hexdump_ascii_key(MSG_DEBUG, "SAE: password",
209 password, password_len);
212 * H(salt, ikm) = HMAC-SHA256(salt, ikm)
213 * pwd-seed = H(MAX(STA-A-MAC, STA-B-MAC) || MIN(STA-A-MAC, STA-B-MAC),
214 * password || counter)
216 sae_pwd_seed_key(addr1, addr2, addrs);
219 len[0] = password_len;
221 len[1] = sizeof(counter);
224 * Continue for at least k iterations to protect against side-channel
225 * attacks that attempt to determine the number of iterations required
228 for (counter = 1; counter < k || !found; counter++) {
229 u8 pwd_seed[SHA256_MAC_LEN];
232 wpa_printf(MSG_DEBUG, "SAE: counter = %u", counter);
233 if (hmac_sha256_vector(addrs, sizeof(addrs), 2, addr, len,
236 res = sae_test_pwd_seed(bnctx, group, pwd_seed,
237 found ? pwe_tmp : pwe,
238 found ? pwe_bin_tmp : pwe_bin);
244 wpa_printf(MSG_DEBUG, "SAE: Ignore this PWE (one was "
245 "already selected)");
247 wpa_printf(MSG_DEBUG, "SAE: Use this PWE");
252 /* This should not happen in practice */
253 wpa_printf(MSG_DEBUG, "SAE: Failed to derive PWE");
258 EC_POINT_clear_free(pwe_tmp);
260 return found ? 0 : -1;
264 static int sae_derive_commit(struct sae_data *sae, BN_CTX *bnctx,
265 EC_GROUP *group, EC_POINT *pwe)
267 BIGNUM *x, *bn_rand, *bn_mask, *order;
272 if (sae_get_rand(sae->sae_rand) < 0 || sae_get_rand(mask) < 0)
274 wpa_hexdump_key(MSG_DEBUG, "SAE: rand",
275 sae->sae_rand, sizeof(sae->sae_rand));
276 wpa_hexdump_key(MSG_DEBUG, "SAE: mask", mask, sizeof(mask));
279 bn_rand = BN_bin2bn(sae->sae_rand, 32, NULL);
280 bn_mask = BN_bin2bn(mask, sizeof(mask), NULL);
281 order = BN_bin2bn(group19_order, sizeof(group19_order), NULL);
282 elem = EC_POINT_new(group);
283 if (x == NULL || bn_rand == NULL || bn_mask == NULL || order == NULL ||
287 /* commit-scalar = (rand + mask) modulo r */
288 BN_add(x, bn_rand, bn_mask);
289 BN_mod(x, x, order, bnctx);
290 sae_bn_to_bin(x, sae->own_commit_scalar, 32);
291 wpa_hexdump(MSG_DEBUG, "SAE: commit-scalar",
292 sae->own_commit_scalar, 32);
294 /* COMMIT-ELEMENT = inverse(scalar-op(mask, PWE)) */
295 if (!EC_POINT_mul(group, elem, NULL, pwe, bn_mask, bnctx) ||
296 !EC_POINT_invert(group, elem, bnctx) ||
297 sae_ec_point_to_bin(bnctx, group, elem, sae->own_commit_element) <
301 wpa_hexdump(MSG_DEBUG, "SAE: commit-element x",
302 sae->own_commit_element, 32);
303 wpa_hexdump(MSG_DEBUG, "SAE: commit-element y",
304 sae->own_commit_element + 32, 32);
310 BN_clear_free(bn_mask);
311 os_memset(mask, 0, sizeof(mask));
312 BN_clear_free(bn_rand);
318 int sae_prepare_commit(const u8 *addr1, const u8 *addr2,
319 const u8 *password, size_t password_len,
320 struct sae_data *sae)
327 bnctx = BN_CTX_new();
328 group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
329 pwe = EC_POINT_new(group);
330 if (bnctx == NULL || group == NULL || pwe == NULL ||
331 sae_derive_pwe(bnctx, group, addr1, addr2, password, password_len,
332 pwe, sae->pwe) < 0 ||
333 sae_derive_commit(sae, bnctx, group, pwe) < 0)
336 EC_POINT_clear_free(pwe);
337 EC_GROUP_free(group);
344 static int sae_check_peer_commit(struct sae_data *sae)
347 if (val_zero(sae->peer_commit_scalar, 32) ||
348 os_memcmp(sae->peer_commit_scalar, group19_order,
349 sizeof(group19_prime)) >= 0) {
350 wpa_printf(MSG_DEBUG, "SAE: Invalid peer scalar");
354 /* element x and y coordinates < p */
355 if (os_memcmp(sae->peer_commit_element, group19_prime,
356 sizeof(group19_prime)) >= 0 ||
357 os_memcmp(sae->peer_commit_element + 32, group19_prime,
358 sizeof(group19_prime)) >= 0) {
359 wpa_printf(MSG_DEBUG, "SAE: Invalid coordinates in peer "
368 static int sae_derive_k(struct sae_data *sae, u8 *k, BN_CTX *bnctx,
371 EC_POINT *pwe, *peer_elem, *K;
372 BIGNUM *k_bn, *rand_bn, *peer_scalar;
375 pwe = alloc_elem(group, sae->pwe, 32);
376 peer_scalar = BN_bin2bn(sae->peer_commit_scalar, 32, NULL);
377 peer_elem = alloc_elem(group, sae->peer_commit_element, 32);
378 K = EC_POINT_new(group);
380 rand_bn = BN_bin2bn(sae->sae_rand, 32, NULL);
381 if (pwe == NULL || peer_elem == NULL || peer_scalar == NULL ||
382 K == NULL || k_bn == NULL || rand_bn == NULL)
385 if (!EC_POINT_is_on_curve(group, peer_elem, NULL)) {
386 wpa_printf(MSG_DEBUG, "SAE: Peer element is not on curve");
391 * K = scalar-op(rand, (elem-op(scalar-op(peer-commit-scalar, PWE),
392 * PEER-COMMIT-ELEMENT)))
393 * If K is identity element (point-at-infinity), reject
394 * k = F(K) (= x coordinate)
397 if (!EC_POINT_mul(group, K, NULL, pwe, peer_scalar, bnctx) ||
398 !EC_POINT_add(group, K, K, peer_elem, bnctx) ||
399 !EC_POINT_mul(group, K, NULL, K, rand_bn, bnctx) ||
400 EC_POINT_is_at_infinity(group, K) ||
401 !EC_POINT_get_affine_coordinates_GFp(group, K, k_bn, NULL, bnctx)) {
402 wpa_printf(MSG_DEBUG, "SAE: Failed to calculate K and k");
406 sae_bn_to_bin(k_bn, k, 32);
407 wpa_hexdump_key(MSG_DEBUG, "SAE: k", k, 32);
412 EC_POINT_free(peer_elem);
413 EC_POINT_clear_free(K);
420 static int sae_derive_keys(struct sae_data *sae, const u8 *k, BN_CTX *bnctx)
422 u8 null_key[32], val[32];
423 u8 keyseed[SHA256_MAC_LEN];
425 BIGNUM *order, *own_scalar, *peer_scalar, *tmp;
428 order = BN_bin2bn(group19_order, sizeof(group19_order), NULL);
429 own_scalar = BN_bin2bn(sae->own_commit_scalar, 32, NULL);
430 peer_scalar = BN_bin2bn(sae->peer_commit_scalar, 32, NULL);
432 if (order == NULL || own_scalar == NULL || peer_scalar == NULL ||
436 /* keyseed = H(<0>32, k)
437 * KCK || PMK = KDF-512(keyseed, "SAE KCK and PMK",
438 * (commit-scalar + peer-commit-scalar) modulo r)
439 * PMKID = L((commit-scalar + peer-commit-scalar) modulo r, 0, 128)
442 os_memset(null_key, 0, sizeof(null_key));
443 hmac_sha256(null_key, sizeof(null_key), k, 32, keyseed);
444 wpa_hexdump_key(MSG_DEBUG, "SAE: keyseed", keyseed, sizeof(keyseed));
446 BN_add(tmp, own_scalar, peer_scalar);
447 BN_mod(tmp, tmp, order, bnctx);
448 sae_bn_to_bin(tmp, val, sizeof(group19_prime));
449 wpa_hexdump(MSG_DEBUG, "SAE: PMKID", val, 16);
450 sha256_prf(keyseed, sizeof(keyseed), "SAE KCK and PMK",
451 val, sizeof(val), keys, sizeof(keys));
452 os_memcpy(sae->kck, keys, 32);
453 os_memcpy(sae->pmk, keys + 32, 32);
454 wpa_hexdump_key(MSG_DEBUG, "SAE: KCK", sae->kck, 32);
455 wpa_hexdump_key(MSG_DEBUG, "SAE: PMK", sae->pmk, 32);
466 int sae_process_commit(struct sae_data *sae)
473 if (sae_check_peer_commit(sae) < 0)
476 bnctx = BN_CTX_new();
477 group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
478 if (bnctx == NULL || group == NULL ||
479 sae_derive_k(sae, k, bnctx, group) < 0 ||
480 sae_derive_keys(sae, k, bnctx) < 0)
483 EC_GROUP_free(group);
490 void sae_write_commit(struct sae_data *sae, struct wpabuf *buf)
492 wpabuf_put_le16(buf, 19); /* Finite Cyclic Group */
493 /* TODO: Anti-Clogging Token (if requested) */
494 wpabuf_put_data(buf, sae->own_commit_scalar, 32);
495 wpabuf_put_data(buf, sae->own_commit_element, 2 * 32);
499 u16 sae_parse_commit(struct sae_data *sae, const u8 *data, size_t len)
501 const u8 *pos = data, *end = data + len;
504 wpa_hexdump(MSG_DEBUG, "SAE: Commit fields", data, len);
506 /* Check Finite Cyclic Group */
508 return WLAN_STATUS_UNSPECIFIED_FAILURE;
509 if (WPA_GET_LE16(pos) != 19) {
510 wpa_printf(MSG_DEBUG, "SAE: Unsupported Finite Cyclic Group %u",
512 return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
517 if (pos + val_len > end) {
518 wpa_printf(MSG_DEBUG, "SAE: Not enough data for scalar");
519 return WLAN_STATUS_UNSPECIFIED_FAILURE;
521 os_memcpy(sae->peer_commit_scalar, pos, val_len);
522 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-scalar",
523 sae->peer_commit_scalar, val_len);
526 if (pos + 2 * val_len > end) {
527 wpa_printf(MSG_DEBUG, "SAE: Not enough data for "
529 return WLAN_STATUS_UNSPECIFIED_FAILURE;
531 os_memcpy(sae->peer_commit_element, pos, 2 * val_len);
532 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(x)",
533 sae->peer_commit_element, val_len);
534 wpa_hexdump(MSG_DEBUG, "SAE: Peer commit-element(y)",
535 sae->peer_commit_element + val_len, val_len);
539 wpa_hexdump(MSG_DEBUG, "SAE: Unexpected extra data in commit",
543 return WLAN_STATUS_SUCCESS;