2 * WPA/RSN - Shared functions for supplicant and authenticator
3 * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/md5.h"
13 #include "crypto/sha1.h"
14 #include "crypto/sha256.h"
15 #include "crypto/aes_wrap.h"
16 #include "crypto/crypto.h"
17 #include "ieee802_11_defs.h"
19 #include "wpa_common.h"
23 * wpa_eapol_key_mic - Calculate EAPOL-Key MIC
24 * @key: EAPOL-Key Key Confirmation Key (KCK)
25 * @ver: Key descriptor version (WPA_KEY_INFO_TYPE_*)
26 * @buf: Pointer to the beginning of the EAPOL header (version field)
27 * @len: Length of the EAPOL frame (from EAPOL header to the end of the frame)
28 * @mic: Pointer to the buffer to which the EAPOL-Key MIC is written
29 * Returns: 0 on success, -1 on failure
31 * Calculate EAPOL-Key MIC for an EAPOL-Key packet. The EAPOL-Key MIC field has
32 * to be cleared (all zeroes) when calling this function.
34 * Note: 'IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames' has an error in the
35 * description of the Key MIC calculation. It includes packet data from the
36 * beginning of the EAPOL-Key header, not EAPOL header. This incorrect change
37 * happened during final editing of the standard and the correct behavior is
38 * defined in the last draft (IEEE 802.11i/D10).
40 int wpa_eapol_key_mic(const u8 *key, int ver, const u8 *buf, size_t len,
43 u8 hash[SHA1_MAC_LEN];
47 case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
48 return hmac_md5(key, 16, buf, len, mic);
49 #endif /* CONFIG_FIPS */
50 case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
51 if (hmac_sha1(key, 16, buf, len, hash))
53 os_memcpy(mic, hash, MD5_MAC_LEN);
55 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
56 case WPA_KEY_INFO_TYPE_AES_128_CMAC:
57 return omac1_aes_128(key, buf, len, mic);
58 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
68 * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces
69 * @pmk: Pairwise master key
70 * @pmk_len: Length of PMK
71 * @label: Label to use in derivation
74 * @nonce1: ANonce or SNonce
75 * @nonce2: SNonce or ANonce
76 * @ptk: Buffer for pairwise transient key
77 * @ptk_len: Length of PTK
78 * @use_sha256: Whether to use SHA256-based KDF
80 * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
81 * PTK = PRF-X(PMK, "Pairwise key expansion",
82 * Min(AA, SA) || Max(AA, SA) ||
83 * Min(ANonce, SNonce) || Max(ANonce, SNonce))
85 * STK = PRF-X(SMK, "Peer key expansion",
86 * Min(MAC_I, MAC_P) || Max(MAC_I, MAC_P) ||
87 * Min(INonce, PNonce) || Max(INonce, PNonce))
89 void wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label,
90 const u8 *addr1, const u8 *addr2,
91 const u8 *nonce1, const u8 *nonce2,
92 u8 *ptk, size_t ptk_len, int use_sha256)
94 u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN];
96 if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) {
97 os_memcpy(data, addr1, ETH_ALEN);
98 os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN);
100 os_memcpy(data, addr2, ETH_ALEN);
101 os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN);
104 if (os_memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) {
105 os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN);
106 os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2,
109 os_memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN);
110 os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1,
114 #ifdef CONFIG_IEEE80211W
116 sha256_prf(pmk, pmk_len, label, data, sizeof(data),
119 #endif /* CONFIG_IEEE80211W */
120 sha1_prf(pmk, pmk_len, label, data, sizeof(data), ptk,
123 wpa_printf(MSG_DEBUG, "WPA: PTK derivation - A1=" MACSTR " A2=" MACSTR,
124 MAC2STR(addr1), MAC2STR(addr2));
125 wpa_hexdump(MSG_DEBUG, "WPA: Nonce1", nonce1, WPA_NONCE_LEN);
126 wpa_hexdump(MSG_DEBUG, "WPA: Nonce2", nonce2, WPA_NONCE_LEN);
127 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len);
128 wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", ptk, ptk_len);
132 #ifdef CONFIG_IEEE80211R
133 int wpa_ft_mic(const u8 *kck, const u8 *sta_addr, const u8 *ap_addr,
134 u8 transaction_seqnum, const u8 *mdie, size_t mdie_len,
135 const u8 *ftie, size_t ftie_len,
136 const u8 *rsnie, size_t rsnie_len,
137 const u8 *ric, size_t ric_len, u8 *mic)
142 buf_len = 2 * ETH_ALEN + 1 + mdie_len + ftie_len + rsnie_len + ric_len;
143 buf = os_malloc(buf_len);
148 os_memcpy(pos, sta_addr, ETH_ALEN);
150 os_memcpy(pos, ap_addr, ETH_ALEN);
152 *pos++ = transaction_seqnum;
154 os_memcpy(pos, rsnie, rsnie_len);
158 os_memcpy(pos, mdie, mdie_len);
162 struct rsn_ftie *_ftie;
163 os_memcpy(pos, ftie, ftie_len);
164 if (ftie_len < 2 + sizeof(*_ftie)) {
168 _ftie = (struct rsn_ftie *) (pos + 2);
169 os_memset(_ftie->mic, 0, sizeof(_ftie->mic));
173 os_memcpy(pos, ric, ric_len);
177 wpa_hexdump(MSG_MSGDUMP, "FT: MIC data", buf, pos - buf);
178 if (omac1_aes_128(kck, buf, pos - buf, mic)) {
189 static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
190 struct wpa_ft_ies *parse)
195 parse->ftie_len = ie_len;
197 pos = ie + sizeof(struct rsn_ftie);
200 while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
202 case FTIE_SUBELEM_R1KH_ID:
203 if (pos[1] != FT_R1KH_ID_LEN) {
204 wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
205 "length in FTIE: %d", pos[1]);
208 parse->r1kh_id = pos + 2;
210 case FTIE_SUBELEM_GTK:
211 parse->gtk = pos + 2;
212 parse->gtk_len = pos[1];
214 case FTIE_SUBELEM_R0KH_ID:
215 if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
216 wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
217 "length in FTIE: %d", pos[1]);
220 parse->r0kh_id = pos + 2;
221 parse->r0kh_id_len = pos[1];
223 #ifdef CONFIG_IEEE80211W
224 case FTIE_SUBELEM_IGTK:
225 parse->igtk = pos + 2;
226 parse->igtk_len = pos[1];
228 #endif /* CONFIG_IEEE80211W */
238 int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
239 struct wpa_ft_ies *parse)
242 struct wpa_ie_data data;
244 const struct rsn_ftie *ftie;
245 int prot_ie_count = 0;
247 os_memset(parse, 0, sizeof(*parse));
253 while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
256 parse->rsn = pos + 2;
257 parse->rsn_len = pos[1];
258 ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
262 wpa_printf(MSG_DEBUG, "FT: Failed to parse "
266 if (data.num_pmkid == 1 && data.pmkid)
267 parse->rsn_pmkid = data.pmkid;
269 case WLAN_EID_MOBILITY_DOMAIN:
270 parse->mdie = pos + 2;
271 parse->mdie_len = pos[1];
273 case WLAN_EID_FAST_BSS_TRANSITION:
274 if (pos[1] < sizeof(*ftie))
276 ftie = (const struct rsn_ftie *) (pos + 2);
277 prot_ie_count = ftie->mic_control[1];
278 if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
281 case WLAN_EID_TIMEOUT_INTERVAL:
282 parse->tie = pos + 2;
283 parse->tie_len = pos[1];
285 case WLAN_EID_RIC_DATA:
286 if (parse->ric == NULL)
294 if (prot_ie_count == 0)
295 return 0; /* no MIC */
298 * Check that the protected IE count matches with IEs included in the
307 if (prot_ie_count < 0) {
308 wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in "
309 "the protected IE count");
313 if (prot_ie_count == 0 && parse->ric) {
314 wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not "
315 "included in protected IE count");
319 /* Determine the end of the RIC IE(s) */
321 while (pos && pos + 2 <= end && pos + 2 + pos[1] <= end &&
326 parse->ric_len = pos - parse->ric;
328 wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from "
329 "frame", (int) prot_ie_count);
335 #endif /* CONFIG_IEEE80211R */
338 #ifndef CONFIG_NO_WPA2
339 static int rsn_selector_to_bitfield(const u8 *s)
341 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NONE)
342 return WPA_CIPHER_NONE;
343 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_WEP40)
344 return WPA_CIPHER_WEP40;
345 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_TKIP)
346 return WPA_CIPHER_TKIP;
347 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP)
348 return WPA_CIPHER_CCMP;
349 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_WEP104)
350 return WPA_CIPHER_WEP104;
351 #ifdef CONFIG_IEEE80211W
352 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_AES_128_CMAC)
353 return WPA_CIPHER_AES_128_CMAC;
354 #endif /* CONFIG_IEEE80211W */
355 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP)
356 return WPA_CIPHER_GCMP;
361 static int rsn_key_mgmt_to_bitfield(const u8 *s)
363 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_UNSPEC_802_1X)
364 return WPA_KEY_MGMT_IEEE8021X;
365 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X)
366 return WPA_KEY_MGMT_PSK;
367 #ifdef CONFIG_IEEE80211R
368 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X)
369 return WPA_KEY_MGMT_FT_IEEE8021X;
370 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_PSK)
371 return WPA_KEY_MGMT_FT_PSK;
372 #endif /* CONFIG_IEEE80211R */
373 #ifdef CONFIG_IEEE80211W
374 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SHA256)
375 return WPA_KEY_MGMT_IEEE8021X_SHA256;
376 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_SHA256)
377 return WPA_KEY_MGMT_PSK_SHA256;
378 #endif /* CONFIG_IEEE80211W */
380 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_SAE)
381 return WPA_KEY_MGMT_SAE;
382 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_SAE)
383 return WPA_KEY_MGMT_FT_SAE;
384 #endif /* CONFIG_SAE */
387 #endif /* CONFIG_NO_WPA2 */
391 * wpa_parse_wpa_ie_rsn - Parse RSN IE
392 * @rsn_ie: Buffer containing RSN IE
393 * @rsn_ie_len: RSN IE buffer length (including IE number and length octets)
394 * @data: Pointer to structure that will be filled in with parsed data
395 * Returns: 0 on success, <0 on failure
397 int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len,
398 struct wpa_ie_data *data)
400 #ifndef CONFIG_NO_WPA2
401 const struct rsn_ie_hdr *hdr;
406 os_memset(data, 0, sizeof(*data));
407 data->proto = WPA_PROTO_RSN;
408 data->pairwise_cipher = WPA_CIPHER_CCMP;
409 data->group_cipher = WPA_CIPHER_CCMP;
410 data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
411 data->capabilities = 0;
414 #ifdef CONFIG_IEEE80211W
415 data->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
416 #else /* CONFIG_IEEE80211W */
417 data->mgmt_group_cipher = 0;
418 #endif /* CONFIG_IEEE80211W */
420 if (rsn_ie_len == 0) {
421 /* No RSN IE - fail silently */
425 if (rsn_ie_len < sizeof(struct rsn_ie_hdr)) {
426 wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
427 __func__, (unsigned long) rsn_ie_len);
431 hdr = (const struct rsn_ie_hdr *) rsn_ie;
433 if (hdr->elem_id != WLAN_EID_RSN ||
434 hdr->len != rsn_ie_len - 2 ||
435 WPA_GET_LE16(hdr->version) != RSN_VERSION) {
436 wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
441 pos = (const u8 *) (hdr + 1);
442 left = rsn_ie_len - sizeof(*hdr);
444 if (left >= RSN_SELECTOR_LEN) {
445 data->group_cipher = rsn_selector_to_bitfield(pos);
446 #ifdef CONFIG_IEEE80211W
447 if (data->group_cipher == WPA_CIPHER_AES_128_CMAC) {
448 wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as group "
452 #endif /* CONFIG_IEEE80211W */
453 pos += RSN_SELECTOR_LEN;
454 left -= RSN_SELECTOR_LEN;
455 } else if (left > 0) {
456 wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
462 data->pairwise_cipher = 0;
463 count = WPA_GET_LE16(pos);
466 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
467 wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
468 "count %u left %u", __func__, count, left);
471 for (i = 0; i < count; i++) {
472 data->pairwise_cipher |= rsn_selector_to_bitfield(pos);
473 pos += RSN_SELECTOR_LEN;
474 left -= RSN_SELECTOR_LEN;
476 #ifdef CONFIG_IEEE80211W
477 if (data->pairwise_cipher & WPA_CIPHER_AES_128_CMAC) {
478 wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as "
479 "pairwise cipher", __func__);
482 #endif /* CONFIG_IEEE80211W */
483 } else if (left == 1) {
484 wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
491 count = WPA_GET_LE16(pos);
494 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
495 wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
496 "count %u left %u", __func__, count, left);
499 for (i = 0; i < count; i++) {
500 data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos);
501 pos += RSN_SELECTOR_LEN;
502 left -= RSN_SELECTOR_LEN;
504 } else if (left == 1) {
505 wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
511 data->capabilities = WPA_GET_LE16(pos);
517 data->num_pmkid = WPA_GET_LE16(pos);
520 if (left < (int) data->num_pmkid * PMKID_LEN) {
521 wpa_printf(MSG_DEBUG, "%s: PMKID underflow "
522 "(num_pmkid=%lu left=%d)",
523 __func__, (unsigned long) data->num_pmkid,
529 pos += data->num_pmkid * PMKID_LEN;
530 left -= data->num_pmkid * PMKID_LEN;
534 #ifdef CONFIG_IEEE80211W
536 data->mgmt_group_cipher = rsn_selector_to_bitfield(pos);
537 if (data->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC) {
538 wpa_printf(MSG_DEBUG, "%s: Unsupported management "
539 "group cipher 0x%x", __func__,
540 data->mgmt_group_cipher);
543 pos += RSN_SELECTOR_LEN;
544 left -= RSN_SELECTOR_LEN;
546 #endif /* CONFIG_IEEE80211W */
549 wpa_printf(MSG_DEBUG, "%s: ie has %u trailing bytes - ignored",
554 #else /* CONFIG_NO_WPA2 */
556 #endif /* CONFIG_NO_WPA2 */
560 static int wpa_selector_to_bitfield(const u8 *s)
562 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_NONE)
563 return WPA_CIPHER_NONE;
564 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP40)
565 return WPA_CIPHER_WEP40;
566 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_TKIP)
567 return WPA_CIPHER_TKIP;
568 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_CCMP)
569 return WPA_CIPHER_CCMP;
570 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP104)
571 return WPA_CIPHER_WEP104;
576 static int wpa_key_mgmt_to_bitfield(const u8 *s)
578 if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_UNSPEC_802_1X)
579 return WPA_KEY_MGMT_IEEE8021X;
580 if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X)
581 return WPA_KEY_MGMT_PSK;
582 if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_NONE)
583 return WPA_KEY_MGMT_WPA_NONE;
588 int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len,
589 struct wpa_ie_data *data)
591 const struct wpa_ie_hdr *hdr;
596 os_memset(data, 0, sizeof(*data));
597 data->proto = WPA_PROTO_WPA;
598 data->pairwise_cipher = WPA_CIPHER_TKIP;
599 data->group_cipher = WPA_CIPHER_TKIP;
600 data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
601 data->capabilities = 0;
604 data->mgmt_group_cipher = 0;
606 if (wpa_ie_len == 0) {
607 /* No WPA IE - fail silently */
611 if (wpa_ie_len < sizeof(struct wpa_ie_hdr)) {
612 wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
613 __func__, (unsigned long) wpa_ie_len);
617 hdr = (const struct wpa_ie_hdr *) wpa_ie;
619 if (hdr->elem_id != WLAN_EID_VENDOR_SPECIFIC ||
620 hdr->len != wpa_ie_len - 2 ||
621 RSN_SELECTOR_GET(hdr->oui) != WPA_OUI_TYPE ||
622 WPA_GET_LE16(hdr->version) != WPA_VERSION) {
623 wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
628 pos = (const u8 *) (hdr + 1);
629 left = wpa_ie_len - sizeof(*hdr);
631 if (left >= WPA_SELECTOR_LEN) {
632 data->group_cipher = wpa_selector_to_bitfield(pos);
633 pos += WPA_SELECTOR_LEN;
634 left -= WPA_SELECTOR_LEN;
635 } else if (left > 0) {
636 wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
642 data->pairwise_cipher = 0;
643 count = WPA_GET_LE16(pos);
646 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
647 wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
648 "count %u left %u", __func__, count, left);
651 for (i = 0; i < count; i++) {
652 data->pairwise_cipher |= wpa_selector_to_bitfield(pos);
653 pos += WPA_SELECTOR_LEN;
654 left -= WPA_SELECTOR_LEN;
656 } else if (left == 1) {
657 wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
664 count = WPA_GET_LE16(pos);
667 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
668 wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
669 "count %u left %u", __func__, count, left);
672 for (i = 0; i < count; i++) {
673 data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos);
674 pos += WPA_SELECTOR_LEN;
675 left -= WPA_SELECTOR_LEN;
677 } else if (left == 1) {
678 wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
684 data->capabilities = WPA_GET_LE16(pos);
690 wpa_printf(MSG_DEBUG, "%s: ie has %u trailing bytes - ignored",
698 #ifdef CONFIG_IEEE80211R
701 * wpa_derive_pmk_r0 - Derive PMK-R0 and PMKR0Name
703 * IEEE Std 802.11r-2008 - 8.5.1.5.3
705 void wpa_derive_pmk_r0(const u8 *xxkey, size_t xxkey_len,
706 const u8 *ssid, size_t ssid_len,
707 const u8 *mdid, const u8 *r0kh_id, size_t r0kh_id_len,
708 const u8 *s0kh_id, u8 *pmk_r0, u8 *pmk_r0_name)
710 u8 buf[1 + WPA_MAX_SSID_LEN + MOBILITY_DOMAIN_ID_LEN + 1 +
711 FT_R0KH_ID_MAX_LEN + ETH_ALEN];
712 u8 *pos, r0_key_data[48], hash[32];
717 * R0-Key-Data = KDF-384(XXKey, "FT-R0",
718 * SSIDlength || SSID || MDID || R0KHlength ||
719 * R0KH-ID || S0KH-ID)
720 * XXKey is either the second 256 bits of MSK or PSK.
721 * PMK-R0 = L(R0-Key-Data, 0, 256)
722 * PMK-R0Name-Salt = L(R0-Key-Data, 256, 128)
724 if (ssid_len > WPA_MAX_SSID_LEN || r0kh_id_len > FT_R0KH_ID_MAX_LEN)
728 os_memcpy(pos, ssid, ssid_len);
730 os_memcpy(pos, mdid, MOBILITY_DOMAIN_ID_LEN);
731 pos += MOBILITY_DOMAIN_ID_LEN;
732 *pos++ = r0kh_id_len;
733 os_memcpy(pos, r0kh_id, r0kh_id_len);
735 os_memcpy(pos, s0kh_id, ETH_ALEN);
738 sha256_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf,
739 r0_key_data, sizeof(r0_key_data));
740 os_memcpy(pmk_r0, r0_key_data, PMK_LEN);
743 * PMKR0Name = Truncate-128(SHA-256("FT-R0N" || PMK-R0Name-Salt)
745 addr[0] = (const u8 *) "FT-R0N";
747 addr[1] = r0_key_data + PMK_LEN;
750 sha256_vector(2, addr, len, hash);
751 os_memcpy(pmk_r0_name, hash, WPA_PMK_NAME_LEN);
756 * wpa_derive_pmk_r1_name - Derive PMKR1Name
758 * IEEE Std 802.11r-2008 - 8.5.1.5.4
760 void wpa_derive_pmk_r1_name(const u8 *pmk_r0_name, const u8 *r1kh_id,
761 const u8 *s1kh_id, u8 *pmk_r1_name)
768 * PMKR1Name = Truncate-128(SHA-256("FT-R1N" || PMKR0Name ||
769 * R1KH-ID || S1KH-ID))
771 addr[0] = (const u8 *) "FT-R1N";
773 addr[1] = pmk_r0_name;
774 len[1] = WPA_PMK_NAME_LEN;
776 len[2] = FT_R1KH_ID_LEN;
780 sha256_vector(4, addr, len, hash);
781 os_memcpy(pmk_r1_name, hash, WPA_PMK_NAME_LEN);
786 * wpa_derive_pmk_r1 - Derive PMK-R1 and PMKR1Name from PMK-R0
788 * IEEE Std 802.11r-2008 - 8.5.1.5.4
790 void wpa_derive_pmk_r1(const u8 *pmk_r0, const u8 *pmk_r0_name,
791 const u8 *r1kh_id, const u8 *s1kh_id,
792 u8 *pmk_r1, u8 *pmk_r1_name)
794 u8 buf[FT_R1KH_ID_LEN + ETH_ALEN];
797 /* PMK-R1 = KDF-256(PMK-R0, "FT-R1", R1KH-ID || S1KH-ID) */
799 os_memcpy(pos, r1kh_id, FT_R1KH_ID_LEN);
800 pos += FT_R1KH_ID_LEN;
801 os_memcpy(pos, s1kh_id, ETH_ALEN);
804 sha256_prf(pmk_r0, PMK_LEN, "FT-R1", buf, pos - buf, pmk_r1, PMK_LEN);
806 wpa_derive_pmk_r1_name(pmk_r0_name, r1kh_id, s1kh_id, pmk_r1_name);
811 * wpa_pmk_r1_to_ptk - Derive PTK and PTKName from PMK-R1
813 * IEEE Std 802.11r-2008 - 8.5.1.5.5
815 void wpa_pmk_r1_to_ptk(const u8 *pmk_r1, const u8 *snonce, const u8 *anonce,
816 const u8 *sta_addr, const u8 *bssid,
817 const u8 *pmk_r1_name,
818 u8 *ptk, size_t ptk_len, u8 *ptk_name)
820 u8 buf[2 * WPA_NONCE_LEN + 2 * ETH_ALEN];
826 * PTK = KDF-PTKLen(PMK-R1, "FT-PTK", SNonce || ANonce ||
830 os_memcpy(pos, snonce, WPA_NONCE_LEN);
831 pos += WPA_NONCE_LEN;
832 os_memcpy(pos, anonce, WPA_NONCE_LEN);
833 pos += WPA_NONCE_LEN;
834 os_memcpy(pos, bssid, ETH_ALEN);
836 os_memcpy(pos, sta_addr, ETH_ALEN);
839 sha256_prf(pmk_r1, PMK_LEN, "FT-PTK", buf, pos - buf, ptk, ptk_len);
842 * PTKName = Truncate-128(SHA-256(PMKR1Name || "FT-PTKN" || SNonce ||
843 * ANonce || BSSID || STA-ADDR))
845 addr[0] = pmk_r1_name;
846 len[0] = WPA_PMK_NAME_LEN;
847 addr[1] = (const u8 *) "FT-PTKN";
850 len[2] = WPA_NONCE_LEN;
852 len[3] = WPA_NONCE_LEN;
858 sha256_vector(6, addr, len, hash);
859 os_memcpy(ptk_name, hash, WPA_PMK_NAME_LEN);
862 #endif /* CONFIG_IEEE80211R */
866 * rsn_pmkid - Calculate PMK identifier
867 * @pmk: Pairwise master key
868 * @pmk_len: Length of pmk in bytes
869 * @aa: Authenticator address
870 * @spa: Supplicant address
871 * @pmkid: Buffer for PMKID
872 * @use_sha256: Whether to use SHA256-based KDF
874 * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
875 * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA)
877 void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
878 u8 *pmkid, int use_sha256)
880 char *title = "PMK Name";
882 const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
883 unsigned char hash[SHA256_MAC_LEN];
885 addr[0] = (u8 *) title;
889 #ifdef CONFIG_IEEE80211W
891 hmac_sha256_vector(pmk, pmk_len, 3, addr, len, hash);
893 #endif /* CONFIG_IEEE80211W */
894 hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash);
895 os_memcpy(pmkid, hash, PMKID_LEN);
900 * wpa_cipher_txt - Convert cipher suite to a text string
901 * @cipher: Cipher suite (WPA_CIPHER_* enum)
902 * Returns: Pointer to a text string of the cipher suite name
904 const char * wpa_cipher_txt(int cipher)
907 case WPA_CIPHER_NONE:
909 case WPA_CIPHER_WEP40:
911 case WPA_CIPHER_WEP104:
913 case WPA_CIPHER_TKIP:
915 case WPA_CIPHER_CCMP:
917 case WPA_CIPHER_CCMP | WPA_CIPHER_TKIP:
919 case WPA_CIPHER_GCMP:
928 * wpa_key_mgmt_txt - Convert key management suite to a text string
929 * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum)
930 * @proto: WPA/WPA2 version (WPA_PROTO_*)
931 * Returns: Pointer to a text string of the key management suite name
933 const char * wpa_key_mgmt_txt(int key_mgmt, int proto)
936 case WPA_KEY_MGMT_IEEE8021X:
937 if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA))
938 return "WPA2+WPA/IEEE 802.1X/EAP";
939 return proto == WPA_PROTO_RSN ?
940 "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP";
941 case WPA_KEY_MGMT_PSK:
942 if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA))
943 return "WPA2-PSK+WPA-PSK";
944 return proto == WPA_PROTO_RSN ?
945 "WPA2-PSK" : "WPA-PSK";
946 case WPA_KEY_MGMT_NONE:
948 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
949 return "IEEE 802.1X (no WPA)";
950 #ifdef CONFIG_IEEE80211R
951 case WPA_KEY_MGMT_FT_IEEE8021X:
953 case WPA_KEY_MGMT_FT_PSK:
955 #endif /* CONFIG_IEEE80211R */
956 #ifdef CONFIG_IEEE80211W
957 case WPA_KEY_MGMT_IEEE8021X_SHA256:
958 return "WPA2-EAP-SHA256";
959 case WPA_KEY_MGMT_PSK_SHA256:
960 return "WPA2-PSK-SHA256";
961 #endif /* CONFIG_IEEE80211W */
968 int wpa_compare_rsn_ie(int ft_initial_assoc,
969 const u8 *ie1, size_t ie1len,
970 const u8 *ie2, size_t ie2len)
972 if (ie1 == NULL || ie2 == NULL)
975 if (ie1len == ie2len && os_memcmp(ie1, ie2, ie1len) == 0)
976 return 0; /* identical IEs */
978 #ifdef CONFIG_IEEE80211R
979 if (ft_initial_assoc) {
980 struct wpa_ie_data ie1d, ie2d;
982 * The PMKID-List in RSN IE is different between Beacon/Probe
983 * Response/(Re)Association Request frames and EAPOL-Key
984 * messages in FT initial mobility domain association. Allow
985 * for this, but verify that other parts of the RSN IEs are
988 if (wpa_parse_wpa_ie_rsn(ie1, ie1len, &ie1d) < 0 ||
989 wpa_parse_wpa_ie_rsn(ie2, ie2len, &ie2d) < 0)
991 if (ie1d.proto == ie2d.proto &&
992 ie1d.pairwise_cipher == ie2d.pairwise_cipher &&
993 ie1d.group_cipher == ie2d.group_cipher &&
994 ie1d.key_mgmt == ie2d.key_mgmt &&
995 ie1d.capabilities == ie2d.capabilities &&
996 ie1d.mgmt_group_cipher == ie2d.mgmt_group_cipher)
999 #endif /* CONFIG_IEEE80211R */
1005 #ifdef CONFIG_IEEE80211R
1006 int wpa_insert_pmkid(u8 *ies, size_t ies_len, const u8 *pmkid)
1008 u8 *start, *end, *rpos, *rend;
1012 end = ies + ies_len;
1014 while (start < end) {
1015 if (*start == WLAN_EID_RSN)
1017 start += 2 + start[1];
1020 wpa_printf(MSG_ERROR, "FT: Could not find RSN IE in "
1024 wpa_hexdump(MSG_DEBUG, "FT: RSN IE before modification",
1025 start, 2 + start[1]);
1027 /* Find start of PMKID-Count */
1029 rend = rpos + start[1];
1031 /* Skip Version and Group Data Cipher Suite */
1033 /* Skip Pairwise Cipher Suite Count and List */
1034 rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
1035 /* Skip AKM Suite Count and List */
1036 rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
1039 /* Add RSN Capabilities */
1040 os_memmove(rpos + 2, rpos, end - rpos);
1044 /* Skip RSN Capabilities */
1047 wpa_printf(MSG_ERROR, "FT: Could not parse RSN IE in "
1054 /* No PMKID-Count field included; add it */
1055 os_memmove(rpos + 2 + PMKID_LEN, rpos, end - rpos);
1056 WPA_PUT_LE16(rpos, 1);
1058 os_memcpy(rpos, pmkid, PMKID_LEN);
1059 added += 2 + PMKID_LEN;
1060 start[1] += 2 + PMKID_LEN;
1062 /* PMKID-Count was included; use it */
1063 if (WPA_GET_LE16(rpos) != 0) {
1064 wpa_printf(MSG_ERROR, "FT: Unexpected PMKID "
1065 "in RSN IE in EAPOL-Key data");
1068 WPA_PUT_LE16(rpos, 1);
1070 os_memmove(rpos + PMKID_LEN, rpos, end - rpos);
1071 os_memcpy(rpos, pmkid, PMKID_LEN);
1073 start[1] += PMKID_LEN;
1076 wpa_hexdump(MSG_DEBUG, "FT: RSN IE after modification "
1077 "(PMKID inserted)", start, 2 + start[1]);
1081 #endif /* CONFIG_IEEE80211R */
1084 int wpa_cipher_key_len(int cipher)
1087 case WPA_CIPHER_CCMP:
1088 case WPA_CIPHER_GCMP:
1090 case WPA_CIPHER_TKIP:
1092 case WPA_CIPHER_WEP104:
1094 case WPA_CIPHER_WEP40:
1102 int wpa_cipher_rsc_len(int cipher)
1105 case WPA_CIPHER_CCMP:
1106 case WPA_CIPHER_GCMP:
1107 case WPA_CIPHER_TKIP:
1109 case WPA_CIPHER_WEP104:
1110 case WPA_CIPHER_WEP40:
1118 int wpa_cipher_to_alg(int cipher)
1121 case WPA_CIPHER_CCMP:
1122 return WPA_ALG_CCMP;
1123 case WPA_CIPHER_GCMP:
1124 return WPA_ALG_GCMP;
1125 case WPA_CIPHER_TKIP:
1126 return WPA_ALG_TKIP;
1127 case WPA_CIPHER_WEP104:
1128 case WPA_CIPHER_WEP40:
1131 return WPA_ALG_NONE;
1135 int wpa_cipher_valid_pairwise(int cipher)
1137 return cipher == WPA_CIPHER_CCMP ||
1138 cipher == WPA_CIPHER_GCMP ||
1139 cipher == WPA_CIPHER_TKIP;
1143 u32 wpa_cipher_to_suite(int proto, int cipher)
1145 if (cipher & WPA_CIPHER_CCMP)
1146 return (proto == WPA_PROTO_RSN ?
1147 RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
1148 if (cipher & WPA_CIPHER_GCMP)
1149 return RSN_CIPHER_SUITE_GCMP;
1150 if (cipher & WPA_CIPHER_TKIP)
1151 return (proto == WPA_PROTO_RSN ?
1152 RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
1153 if (cipher & WPA_CIPHER_WEP104)
1154 return (proto == WPA_PROTO_RSN ?
1155 RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104);
1156 if (cipher & WPA_CIPHER_WEP40)
1157 return (proto == WPA_PROTO_RSN ?
1158 RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40);
1159 if (cipher & WPA_CIPHER_NONE)
1160 return (proto == WPA_PROTO_RSN ?
1161 RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
1166 int rsn_cipher_put_suites(u8 *pos, int ciphers)
1170 if (ciphers & WPA_CIPHER_CCMP) {
1171 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1172 pos += RSN_SELECTOR_LEN;
1175 if (ciphers & WPA_CIPHER_GCMP) {
1176 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP);
1177 pos += RSN_SELECTOR_LEN;
1180 if (ciphers & WPA_CIPHER_TKIP) {
1181 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
1182 pos += RSN_SELECTOR_LEN;
1185 if (ciphers & WPA_CIPHER_NONE) {
1186 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NONE);
1187 pos += RSN_SELECTOR_LEN;
1195 int wpa_cipher_put_suites(u8 *pos, int ciphers)
1199 if (ciphers & WPA_CIPHER_CCMP) {
1200 RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_CCMP);
1201 pos += WPA_SELECTOR_LEN;
1204 if (ciphers & WPA_CIPHER_TKIP) {
1205 RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_TKIP);
1206 pos += WPA_SELECTOR_LEN;
1209 if (ciphers & WPA_CIPHER_NONE) {
1210 RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_NONE);
1211 pos += WPA_SELECTOR_LEN;