wpa_common.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * WPA definitions shared between hostapd and wpa_supplicant
  3. * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #ifndef WPA_COMMON_H
  15. #define WPA_COMMON_H
  16. #define WPA_MAX_SSID_LEN 32
  17. /* IEEE 802.11i */
  18. #define PMKID_LEN 16
  19. #define PMK_LEN 32
  20. #define WPA_REPLAY_COUNTER_LEN 8
  21. #define WPA_NONCE_LEN 32
  22. #define WPA_KEY_RSC_LEN 8
  23. #define WPA_GMK_LEN 32
  24. #define WPA_GTK_MAX_LEN 32
  25. #define WPA_SELECTOR_LEN 4
  26. #define WPA_VERSION 1
  27. #define RSN_SELECTOR_LEN 4
  28. #define RSN_VERSION 1
  29. #define RSN_SELECTOR(a, b, c, d) \
  30. ((((u32) (a)) << 24) | (((u32) (b)) << 16) | (((u32) (c)) << 8) | \
  31. (u32) (d))
  32. #define WPA_AUTH_KEY_MGMT_NONE RSN_SELECTOR(0x00, 0x50, 0xf2, 0)
  33. #define WPA_AUTH_KEY_MGMT_UNSPEC_802_1X RSN_SELECTOR(0x00, 0x50, 0xf2, 1)
  34. #define WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X RSN_SELECTOR(0x00, 0x50, 0xf2, 2)
  35. #define WPA_CIPHER_SUITE_NONE RSN_SELECTOR(0x00, 0x50, 0xf2, 0)
  36. #define WPA_CIPHER_SUITE_WEP40 RSN_SELECTOR(0x00, 0x50, 0xf2, 1)
  37. #define WPA_CIPHER_SUITE_TKIP RSN_SELECTOR(0x00, 0x50, 0xf2, 2)
  38. #if 0
  39. #define WPA_CIPHER_SUITE_WRAP RSN_SELECTOR(0x00, 0x50, 0xf2, 3)
  40. #endif
  41. #define WPA_CIPHER_SUITE_CCMP RSN_SELECTOR(0x00, 0x50, 0xf2, 4)
  42. #define WPA_CIPHER_SUITE_WEP104 RSN_SELECTOR(0x00, 0x50, 0xf2, 5)
  43. #define RSN_AUTH_KEY_MGMT_UNSPEC_802_1X RSN_SELECTOR(0x00, 0x0f, 0xac, 1)
  44. #define RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X RSN_SELECTOR(0x00, 0x0f, 0xac, 2)
  45. #ifdef CONFIG_IEEE80211R
  46. #define RSN_AUTH_KEY_MGMT_FT_802_1X RSN_SELECTOR(0x00, 0x0f, 0xac, 3)
  47. #define RSN_AUTH_KEY_MGMT_FT_PSK RSN_SELECTOR(0x00, 0x0f, 0xac, 4)
  48. #endif /* CONFIG_IEEE80211R */
  49. #define RSN_AUTH_KEY_MGMT_802_1X_SHA256 RSN_SELECTOR(0x00, 0x0f, 0xac, 5)
  50. #define RSN_AUTH_KEY_MGMT_PSK_SHA256 RSN_SELECTOR(0x00, 0x0f, 0xac, 6)
  51. #define RSN_CIPHER_SUITE_NONE RSN_SELECTOR(0x00, 0x0f, 0xac, 0)
  52. #define RSN_CIPHER_SUITE_WEP40 RSN_SELECTOR(0x00, 0x0f, 0xac, 1)
  53. #define RSN_CIPHER_SUITE_TKIP RSN_SELECTOR(0x00, 0x0f, 0xac, 2)
  54. #if 0
  55. #define RSN_CIPHER_SUITE_WRAP RSN_SELECTOR(0x00, 0x0f, 0xac, 3)
  56. #endif
  57. #define RSN_CIPHER_SUITE_CCMP RSN_SELECTOR(0x00, 0x0f, 0xac, 4)
  58. #define RSN_CIPHER_SUITE_WEP104 RSN_SELECTOR(0x00, 0x0f, 0xac, 5)
  59. #ifdef CONFIG_IEEE80211W
  60. #define RSN_CIPHER_SUITE_AES_128_CMAC RSN_SELECTOR(0x00, 0x0f, 0xac, 6)
  61. #endif /* CONFIG_IEEE80211W */
  62. /* EAPOL-Key Key Data Encapsulation
  63. * GroupKey and PeerKey require encryption, otherwise, encryption is optional.
  64. */
  65. #define RSN_KEY_DATA_GROUPKEY RSN_SELECTOR(0x00, 0x0f, 0xac, 1)
  66. #if 0
  67. #define RSN_KEY_DATA_STAKEY RSN_SELECTOR(0x00, 0x0f, 0xac, 2)
  68. #endif
  69. #define RSN_KEY_DATA_MAC_ADDR RSN_SELECTOR(0x00, 0x0f, 0xac, 3)
  70. #define RSN_KEY_DATA_PMKID RSN_SELECTOR(0x00, 0x0f, 0xac, 4)
  71. #ifdef CONFIG_PEERKEY
  72. #define RSN_KEY_DATA_SMK RSN_SELECTOR(0x00, 0x0f, 0xac, 5)
  73. #define RSN_KEY_DATA_NONCE RSN_SELECTOR(0x00, 0x0f, 0xac, 6)
  74. #define RSN_KEY_DATA_LIFETIME RSN_SELECTOR(0x00, 0x0f, 0xac, 7)
  75. #define RSN_KEY_DATA_ERROR RSN_SELECTOR(0x00, 0x0f, 0xac, 8)
  76. #endif /* CONFIG_PEERKEY */
  77. #ifdef CONFIG_IEEE80211W
  78. #define RSN_KEY_DATA_IGTK RSN_SELECTOR(0x00, 0x0f, 0xac, 9)
  79. #endif /* CONFIG_IEEE80211W */
  80. #define WPA_OUI_TYPE RSN_SELECTOR(0x00, 0x50, 0xf2, 1)
  81. #define RSN_SELECTOR_PUT(a, val) WPA_PUT_BE32((u8 *) (a), (val))
  82. #define RSN_SELECTOR_GET(a) WPA_GET_BE32((const u8 *) (a))
  83. #define RSN_NUM_REPLAY_COUNTERS_1 0
  84. #define RSN_NUM_REPLAY_COUNTERS_2 1
  85. #define RSN_NUM_REPLAY_COUNTERS_4 2
  86. #define RSN_NUM_REPLAY_COUNTERS_16 3
  87. #ifdef _MSC_VER
  88. #pragma pack(push, 1)
  89. #endif /* _MSC_VER */
  90. #ifdef CONFIG_IEEE80211W
  91. #define WPA_IGTK_LEN 16
  92. #endif /* CONFIG_IEEE80211W */
  93. /* IEEE 802.11, 7.3.2.25.3 RSN Capabilities */
  94. #define WPA_CAPABILITY_PREAUTH BIT(0)
  95. #define WPA_CAPABILITY_NO_PAIRWISE BIT(1)
  96. /* B2-B3: PTKSA Replay Counter */
  97. /* B4-B5: GTKSA Replay Counter */
  98. #define WPA_CAPABILITY_MFPR BIT(6)
  99. #define WPA_CAPABILITY_MFPC BIT(7)
  100. #define WPA_CAPABILITY_PEERKEY_ENABLED BIT(9)
  101. /* IEEE 802.11r */
  102. #define MOBILITY_DOMAIN_ID_LEN 2
  103. #define FT_R0KH_ID_MAX_LEN 48
  104. #define FT_R1KH_ID_LEN 6
  105. #define WPA_PMK_NAME_LEN 16
  106. /* IEEE 802.11, 8.5.2 EAPOL-Key frames */
  107. #define WPA_KEY_INFO_TYPE_MASK ((u16) (BIT(0) | BIT(1) | BIT(2)))
  108. #define WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 BIT(0)
  109. #define WPA_KEY_INFO_TYPE_HMAC_SHA1_AES BIT(1)
  110. #define WPA_KEY_INFO_TYPE_AES_128_CMAC 3
  111. #define WPA_KEY_INFO_KEY_TYPE BIT(3) /* 1 = Pairwise, 0 = Group key */
  112. /* bit4..5 is used in WPA, but is reserved in IEEE 802.11i/RSN */
  113. #define WPA_KEY_INFO_KEY_INDEX_MASK (BIT(4) | BIT(5))
  114. #define WPA_KEY_INFO_KEY_INDEX_SHIFT 4
  115. #define WPA_KEY_INFO_INSTALL BIT(6) /* pairwise */
  116. #define WPA_KEY_INFO_TXRX BIT(6) /* group */
  117. #define WPA_KEY_INFO_ACK BIT(7)
  118. #define WPA_KEY_INFO_MIC BIT(8)
  119. #define WPA_KEY_INFO_SECURE BIT(9)
  120. #define WPA_KEY_INFO_ERROR BIT(10)
  121. #define WPA_KEY_INFO_REQUEST BIT(11)
  122. #define WPA_KEY_INFO_ENCR_KEY_DATA BIT(12) /* IEEE 802.11i/RSN only */
  123. #define WPA_KEY_INFO_SMK_MESSAGE BIT(13)
  124. struct wpa_eapol_key {
  125. u8 type;
  126. /* Note: key_info, key_length, and key_data_length are unaligned */
  127. u8 key_info[2]; /* big endian */
  128. u8 key_length[2]; /* big endian */
  129. u8 replay_counter[WPA_REPLAY_COUNTER_LEN];
  130. u8 key_nonce[WPA_NONCE_LEN];
  131. u8 key_iv[16];
  132. u8 key_rsc[WPA_KEY_RSC_LEN];
  133. u8 key_id[8]; /* Reserved in IEEE 802.11i/RSN */
  134. u8 key_mic[16];
  135. u8 key_data_length[2]; /* big endian */
  136. /* followed by key_data_length bytes of key_data */
  137. } STRUCT_PACKED;
  138. /**
  139. * struct wpa_ptk - WPA Pairwise Transient Key
  140. * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
  141. */
  142. struct wpa_ptk {
  143. u8 kck[16]; /* EAPOL-Key Key Confirmation Key (KCK) */
  144. u8 kek[16]; /* EAPOL-Key Key Encryption Key (KEK) */
  145. u8 tk1[16]; /* Temporal Key 1 (TK1) */
  146. union {
  147. u8 tk2[16]; /* Temporal Key 2 (TK2) */
  148. struct {
  149. u8 tx_mic_key[8];
  150. u8 rx_mic_key[8];
  151. } auth;
  152. } u;
  153. } STRUCT_PACKED;
  154. /* WPA IE version 1
  155. * 00-50-f2:1 (OUI:OUI type)
  156. * 0x01 0x00 (version; little endian)
  157. * (all following fields are optional:)
  158. * Group Suite Selector (4 octets) (default: TKIP)
  159. * Pairwise Suite Count (2 octets, little endian) (default: 1)
  160. * Pairwise Suite List (4 * n octets) (default: TKIP)
  161. * Authenticated Key Management Suite Count (2 octets, little endian)
  162. * (default: 1)
  163. * Authenticated Key Management Suite List (4 * n octets)
  164. * (default: unspec 802.1X)
  165. * WPA Capabilities (2 octets, little endian) (default: 0)
  166. */
  167. struct wpa_ie_hdr {
  168. u8 elem_id;
  169. u8 len;
  170. u8 oui[4]; /* 24-bit OUI followed by 8-bit OUI type */
  171. u8 version[2]; /* little endian */
  172. } STRUCT_PACKED;
  173. /* 1/4: PMKID
  174. * 2/4: RSN IE
  175. * 3/4: one or two RSN IEs + GTK IE (encrypted)
  176. * 4/4: empty
  177. * 1/2: GTK IE (encrypted)
  178. * 2/2: empty
  179. */
  180. /* RSN IE version 1
  181. * 0x01 0x00 (version; little endian)
  182. * (all following fields are optional:)
  183. * Group Suite Selector (4 octets) (default: CCMP)
  184. * Pairwise Suite Count (2 octets, little endian) (default: 1)
  185. * Pairwise Suite List (4 * n octets) (default: CCMP)
  186. * Authenticated Key Management Suite Count (2 octets, little endian)
  187. * (default: 1)
  188. * Authenticated Key Management Suite List (4 * n octets)
  189. * (default: unspec 802.1X)
  190. * RSN Capabilities (2 octets, little endian) (default: 0)
  191. * PMKID Count (2 octets) (default: 0)
  192. * PMKID List (16 * n octets)
  193. * Management Group Cipher Suite (4 octets) (default: AES-128-CMAC)
  194. */
  195. struct rsn_ie_hdr {
  196. u8 elem_id; /* WLAN_EID_RSN */
  197. u8 len;
  198. u8 version[2]; /* little endian */
  199. } STRUCT_PACKED;
  200. #ifdef CONFIG_PEERKEY
  201. enum {
  202. STK_MUI_4WAY_STA_AP = 1,
  203. STK_MUI_4WAY_STAT_STA = 2,
  204. STK_MUI_GTK = 3,
  205. STK_MUI_SMK = 4
  206. };
  207. enum {
  208. STK_ERR_STA_NR = 1,
  209. STK_ERR_STA_NRSN = 2,
  210. STK_ERR_CPHR_NS = 3,
  211. STK_ERR_NO_STSL = 4
  212. };
  213. #endif /* CONFIG_PEERKEY */
  214. struct rsn_error_kde {
  215. be16 mui;
  216. be16 error_type;
  217. } STRUCT_PACKED;
  218. #ifdef CONFIG_IEEE80211W
  219. struct wpa_igtk_kde {
  220. u8 keyid[2];
  221. u8 pn[6];
  222. u8 igtk[WPA_IGTK_LEN];
  223. } STRUCT_PACKED;
  224. #endif /* CONFIG_IEEE80211W */
  225. #ifdef CONFIG_IEEE80211R
  226. struct rsn_mdie {
  227. u8 mobility_domain[MOBILITY_DOMAIN_ID_LEN];
  228. u8 ft_capab;
  229. } STRUCT_PACKED;
  230. #define RSN_FT_CAPAB_FT_OVER_DS BIT(0)
  231. #define RSN_FT_CAPAB_FT_RESOURCE_REQ_SUPP BIT(1)
  232. struct rsn_ftie {
  233. u8 mic_control[2];
  234. u8 mic[16];
  235. u8 anonce[WPA_NONCE_LEN];
  236. u8 snonce[WPA_NONCE_LEN];
  237. /* followed by optional parameters */
  238. } STRUCT_PACKED;
  239. #define FTIE_SUBELEM_R1KH_ID 1
  240. #define FTIE_SUBELEM_GTK 2
  241. #define FTIE_SUBELEM_R0KH_ID 3
  242. #define FTIE_SUBELEM_IGTK 4
  243. struct rsn_rdie {
  244. u8 id;
  245. u8 descr_count;
  246. le16 status_code;
  247. } STRUCT_PACKED;
  248. #endif /* CONFIG_IEEE80211R */
  249. #ifdef _MSC_VER
  250. #pragma pack(pop)
  251. #endif /* _MSC_VER */
  252. int wpa_eapol_key_mic(const u8 *key, int ver, const u8 *buf, size_t len,
  253. u8 *mic);
  254. void wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label,
  255. const u8 *addr1, const u8 *addr2,
  256. const u8 *nonce1, const u8 *nonce2,
  257. u8 *ptk, size_t ptk_len, int use_sha256);
  258. #ifdef CONFIG_IEEE80211R
  259. int wpa_ft_mic(const u8 *kck, const u8 *sta_addr, const u8 *ap_addr,
  260. u8 transaction_seqnum, const u8 *mdie, size_t mdie_len,
  261. const u8 *ftie, size_t ftie_len,
  262. const u8 *rsnie, size_t rsnie_len,
  263. const u8 *ric, size_t ric_len, u8 *mic);
  264. void wpa_derive_pmk_r0(const u8 *xxkey, size_t xxkey_len,
  265. const u8 *ssid, size_t ssid_len,
  266. const u8 *mdid, const u8 *r0kh_id, size_t r0kh_id_len,
  267. const u8 *s0kh_id, u8 *pmk_r0, u8 *pmk_r0_name);
  268. void wpa_derive_pmk_r1_name(const u8 *pmk_r0_name, const u8 *r1kh_id,
  269. const u8 *s1kh_id, u8 *pmk_r1_name);
  270. void wpa_derive_pmk_r1(const u8 *pmk_r0, const u8 *pmk_r0_name,
  271. const u8 *r1kh_id, const u8 *s1kh_id,
  272. u8 *pmk_r1, u8 *pmk_r1_name);
  273. void wpa_pmk_r1_to_ptk(const u8 *pmk_r1, const u8 *snonce, const u8 *anonce,
  274. const u8 *sta_addr, const u8 *bssid,
  275. const u8 *pmk_r1_name,
  276. u8 *ptk, size_t ptk_len, u8 *ptk_name);
  277. #endif /* CONFIG_IEEE80211R */
  278. struct wpa_ie_data {
  279. int proto;
  280. int pairwise_cipher;
  281. int group_cipher;
  282. int key_mgmt;
  283. int capabilities;
  284. size_t num_pmkid;
  285. const u8 *pmkid;
  286. int mgmt_group_cipher;
  287. };
  288. int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len,
  289. struct wpa_ie_data *data);
  290. void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
  291. u8 *pmkid, int use_sha256);
  292. const char * wpa_cipher_txt(int cipher);
  293. const char * wpa_key_mgmt_txt(int key_mgmt, int proto);
  294. int wpa_compare_rsn_ie(int ft_initial_assoc,
  295. const u8 *ie1, size_t ie1len,
  296. const u8 *ie2, size_t ie2len);
  297. int wpa_insert_pmkid(u8 *ies, size_t ies_len, const u8 *pmkid);
  298. #endif /* WPA_COMMON_H */