You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wpa.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #ifndef _GPXE_WPA_H
  19. #define _GPXE_WPA_H
  20. #include <gpxe/ieee80211.h>
  21. #include <gpxe/list.h>
  22. FILE_LICENCE ( GPL2_OR_LATER );
  23. /** @file
  24. *
  25. * Common definitions for all types of WPA-protected networks.
  26. */
  27. /** EAPOL-Key type field for modern 802.11i/RSN WPA packets */
  28. #define EAPOL_KEY_TYPE_RSN 2
  29. /** Old EAPOL-Key type field used by WPA1 hardware before 802.11i ratified */
  30. #define EAPOL_KEY_TYPE_WPA 254
  31. /**
  32. * @defgroup eapol_key_info EAPOL-Key Info field bits
  33. * @{
  34. */
  35. /** Key descriptor version, indicating WPA or WPA2 */
  36. #define EAPOL_KEY_INFO_VERSION 0x0007
  37. /** Key type bit, indicating pairwise or group */
  38. #define EAPOL_KEY_INFO_TYPE 0x0008
  39. /** Key install bit; set on message 3 except when legacy hacks are used */
  40. #define EAPOL_KEY_INFO_INSTALL 0x0040
  41. /** Key ACK bit; set when a response is required, on all messages except #4 */
  42. #define EAPOL_KEY_INFO_KEY_ACK 0x0080
  43. /** Key MIC bit; set when the MIC field is valid, on messages 3 and 4 */
  44. #define EAPOL_KEY_INFO_KEY_MIC 0x0100
  45. /** Secure bit; set when both sides have both keys, on messages 3 and 4 */
  46. #define EAPOL_KEY_INFO_SECURE 0x0200
  47. /** Error bit; set on a MIC failure for TKIP */
  48. #define EAPOL_KEY_INFO_ERROR 0x0400
  49. /** Request bit; set when authentication is initiated by the Peer (unusual) */
  50. #define EAPOL_KEY_INFO_REQUEST 0x0800
  51. /** Key Encrypted bit; set when the Key Data field is encrypted */
  52. #define EAPOL_KEY_INFO_KEY_ENC 0x1000
  53. /** SMC Message bit; set when this frame is part of an IBSS SMK handshake */
  54. #define EAPOL_KEY_INFO_SMC_MESS 0x2000
  55. /** Key descriptor version field value for WPA (TKIP) */
  56. #define EAPOL_KEY_VERSION_WPA 1
  57. /** Key descriptor version field value for WPA2 (CCMP) */
  58. #define EAPOL_KEY_VERSION_WPA2 2
  59. /** Key type field value for a PTK (pairwise) key handshake */
  60. #define EAPOL_KEY_TYPE_PTK 0x0008
  61. /** Key type field value for a GTK (group) key handshake */
  62. #define EAPOL_KEY_TYPE_GTK 0x0000
  63. /** @} */
  64. /** An EAPOL-Key packet.
  65. *
  66. * These are used for the WPA 4-Way Handshake, whether or not prior
  67. * authentication has been performed using EAP.
  68. *
  69. * On LANs, an eapol_key_pkt is always encapsulated in the data field
  70. * of an eapol_frame, with the frame's type code set to EAPOL_TYPE_KEY.
  71. *
  72. * Unlike 802.11 frame headers, the fields in this structure are
  73. * stored in big-endian!
  74. */
  75. struct eapol_key_pkt
  76. {
  77. /** One of the EAPOL_KEY_TYPE_* defines. */
  78. u8 type;
  79. /** Bitfield of key characteristics, network byte order */
  80. u16 info;
  81. /** Length of encryption key to be used, network byte order
  82. *
  83. * This is 16 for CCMP, 32 for TKIP, and 5 or 13 for WEP.
  84. */
  85. u16 keysize;
  86. /** Monotonically increasing value for EAPOL-Key conversations
  87. *
  88. * In another classic demonstration of overengineering, this
  89. * 8-byte value will rarely be anything above 1. It's stored
  90. * in network byte order.
  91. */
  92. u64 replay;
  93. /** Nonce value
  94. *
  95. * This is the authenticator's ANonce in frame 1, the peer's
  96. * SNonce in frame 2, and 0 in frames 3 and 4.
  97. */
  98. u8 nonce[32];
  99. /** Initialization vector
  100. *
  101. * This contains the IV used with the Key Encryption Key, or 0
  102. * if the key is unencrypted or encrypted using an algorithm
  103. * that does not require an IV.
  104. */
  105. u8 iv[16];
  106. /** Receive sequence counter for GTK
  107. *
  108. * This is used to synchronize the client's replay counter for
  109. * ordinary data packets. The first six bytes contain PN0
  110. * through PN5 for CCMP mode, or TSC0 through TSC5 for TKIP
  111. * mode. The last two bytes are zero.
  112. */
  113. u8 rsc[8];
  114. /** Reserved bytes */
  115. u8 _reserved[8];
  116. /** Message integrity code over the entire EAPOL frame
  117. *
  118. * This is calculated using HMAC-MD5 when the key descriptor
  119. * version field in @a info is 1, and HMAC-SHA1 ignoring the
  120. * last 4 bytes of the hash when the version field in @a info
  121. * is 2.
  122. */
  123. u8 mic[16];
  124. /** Length of the @a data field in bytes, network byte order */
  125. u16 datalen;
  126. /** Key data
  127. *
  128. * This is formatted as a series of 802.11 information
  129. * elements, with cryptographic data encapsulated using a
  130. * "vendor-specific IE" code and an IEEE-specified OUI.
  131. */
  132. u8 data[0];
  133. } __attribute__ (( packed ));
  134. /** WPA handshaking state */
  135. enum wpa_state {
  136. /** Waiting for PMK to be set */
  137. WPA_WAITING = 0,
  138. /** Ready for 4-Way Handshake */
  139. WPA_READY,
  140. /** Performing 4-Way Handshake */
  141. WPA_WORKING,
  142. /** 4-Way Handshake succeeded */
  143. WPA_SUCCESS,
  144. /** 4-Way Handshake failed */
  145. WPA_FAILURE,
  146. };
  147. /** Bitfield indicating a selection of WPA transient keys */
  148. enum wpa_keymask {
  149. /** Pairwise transient key */
  150. WPA_PTK = 1,
  151. /** Group transient key */
  152. WPA_GTK = 2,
  153. };
  154. /** Length of a nonce */
  155. #define WPA_NONCE_LEN 32
  156. /** Length of a TKIP main key */
  157. #define WPA_TKIP_KEY_LEN 16
  158. /** Length of a TKIP MIC key */
  159. #define WPA_TKIP_MIC_KEY_LEN 8
  160. /** Length of a CCMP key */
  161. #define WPA_CCMP_KEY_LEN 16
  162. /** Length of an EAPOL Key Confirmation Key */
  163. #define WPA_KCK_LEN 16
  164. /** Length of an EAPOL Key Encryption Key */
  165. #define WPA_KEK_LEN 16
  166. /** Usual length of a Pairwise Master Key */
  167. #define WPA_PMK_LEN 32
  168. /** Length of a PMKID */
  169. #define WPA_PMKID_LEN 16
  170. /** Structure of the Temporal Key for TKIP encryption */
  171. struct tkip_tk
  172. {
  173. /** Main key: input to TKIP Phase 1 and Phase 2 key mixing functions */
  174. u8 key[WPA_TKIP_KEY_LEN];
  175. /** Michael MIC keys */
  176. struct {
  177. /** MIC key for packets from the AP */
  178. u8 rx[WPA_TKIP_MIC_KEY_LEN];
  179. /** MIC key for packets to the AP */
  180. u8 tx[WPA_TKIP_MIC_KEY_LEN];
  181. } __attribute__ (( packed )) mic;
  182. } __attribute__ (( packed ));
  183. /** Structure of a generic Temporal Key */
  184. union wpa_tk
  185. {
  186. /** CCMP key */
  187. u8 ccmp[WPA_CCMP_KEY_LEN];
  188. /** TKIP keys */
  189. struct tkip_tk tkip;
  190. };
  191. /** Structure of the Pairwise Transient Key */
  192. struct wpa_ptk
  193. {
  194. /** EAPOL-Key Key Confirmation Key (KCK) */
  195. u8 kck[WPA_KCK_LEN];
  196. /** EAPOL-Key Key Encryption Key (KEK) */
  197. u8 kek[WPA_KEK_LEN];
  198. /** Temporal key */
  199. union wpa_tk tk;
  200. } __attribute__ (( packed ));
  201. /** Structure of the Group Transient Key */
  202. struct wpa_gtk
  203. {
  204. /** Temporal key */
  205. union wpa_tk tk;
  206. } __attribute__ (( packed ));
  207. /** Common context for WPA security handshaking
  208. *
  209. * Any implementor of a particular handshaking type (e.g. PSK or EAP)
  210. * must include this structure at the very beginning of their private
  211. * data context structure, to allow the EAPOL-Key handling code to
  212. * work. When the preliminary authentication is done, it is necessary
  213. * to call wpa_start(), passing the PMK (derived from PSK or EAP MSK)
  214. * as an argument. The handshaker can use its @a step function to
  215. * monitor @a state in this wpa_ctx structure for success or
  216. * failure. On success, the keys will be available in @a ptk and @a
  217. * gtk according to the state of the @a valid bitmask.
  218. *
  219. * After an initial success, the parent handshaker does not need to
  220. * concern itself with rekeying; the WPA common code takes care of
  221. * that.
  222. */
  223. struct wpa_common_ctx
  224. {
  225. /** 802.11 device we are authenticating for */
  226. struct net80211_device *dev;
  227. /** The Pairwise Master Key to use in handshaking
  228. *
  229. * This is set either by running the PBKDF2 algorithm on a
  230. * passphrase with the SSID as salt to generate a pre-shared
  231. * key, or by copying the first 32 bytes of the EAP Master
  232. * Session Key in 802.1X-served authentication.
  233. */
  234. u8 pmk[WPA_PMK_LEN];
  235. /** Length of the Pairwise Master Key
  236. *
  237. * This is always 32 except with one EAP method which only
  238. * gives 16 bytes.
  239. */
  240. int pmk_len;
  241. /** State of EAPOL-Key handshaking */
  242. enum wpa_state state;
  243. /** Replay counter for this association
  244. *
  245. * This stores the replay counter value for the most recent
  246. * packet we've accepted. It is initially initialised to ~0 to
  247. * show we'll accept anything.
  248. */
  249. u64 replay;
  250. /** Mask of valid keys after authentication success
  251. *
  252. * If the PTK is not valid, the GTK should be used for both
  253. * unicast and multicast decryption; if the GTK is not valid,
  254. * multicast packets cannot be decrypted.
  255. */
  256. enum wpa_keymask valid;
  257. /** The cipher to use for unicast RX and all TX */
  258. enum net80211_crypto_alg crypt;
  259. /** The cipher to use for broadcast and multicast RX */
  260. enum net80211_crypto_alg gcrypt;
  261. /** The Pairwise Transient Key derived from the handshake */
  262. struct wpa_ptk ptk;
  263. /** The Group Transient Key derived from the handshake */
  264. struct wpa_gtk gtk;
  265. /** Authenticator-provided nonce */
  266. u8 Anonce[WPA_NONCE_LEN];
  267. /** Supplicant-generated nonce (that's us) */
  268. u8 Snonce[WPA_NONCE_LEN];
  269. /** Whether we should refrain from generating another SNonce */
  270. int have_Snonce;
  271. /** Data in WPA or RSN IE from AP's beacon frame */
  272. void *ap_rsn_ie;
  273. /** Length of @a ap_rsn_ie */
  274. int ap_rsn_ie_len;
  275. /** Whether @a ap_rsn_ie is an RSN IE (as opposed to old WPA) */
  276. int ap_rsn_is_rsn;
  277. /** List entry */
  278. struct list_head list;
  279. };
  280. /** WPA handshake key integrity and encryption handler
  281. *
  282. * Note that due to the structure of the 4-Way Handshake we never
  283. * actually need to encrypt key data, only decrypt it.
  284. */
  285. struct wpa_kie {
  286. /** Value of version bits in EAPOL-Key info field for which to use
  287. *
  288. * This should be one of the @c EAPOL_KEY_VERSION_* constants.
  289. */
  290. int version;
  291. /** Calculate MIC over message
  292. *
  293. * @v kck Key Confirmation Key, 16 bytes
  294. * @v msg Message to calculate MIC over
  295. * @v len Number of bytes to calculate MIC over
  296. * @ret mic Calculated MIC, 16 bytes long
  297. *
  298. * The @a mic return may point within @a msg, so it must not
  299. * be filled until the calculation has been performed.
  300. */
  301. void ( * mic ) ( const void *kck, const void *msg, size_t len,
  302. void *mic );
  303. /** Decrypt key data
  304. *
  305. * @v kek Key Encryption Key, 16 bytes
  306. * @v iv Initialisation vector for encryption, 16 bytes
  307. * @v msg Message to decrypt (Key Data field)
  308. * @v len Length of message
  309. * @ret msg Decrypted message in place of original
  310. * @ret len Updated to reflect encrypted length
  311. * @ret rc Return status code
  312. *
  313. * The decrypted message is written over the encrypted one.
  314. */
  315. int ( * decrypt ) ( const void *kek, const void *iv, void *msg,
  316. u16 *len );
  317. };
  318. #define WPA_KIES __table ( struct wpa_kie, "wpa_kies" )
  319. #define __wpa_kie __table_entry ( WPA_KIES, 01 )
  320. /**
  321. * @defgroup wpa_kde Key descriptor element types
  322. * @{
  323. */
  324. /** Payload structure of the GTK-encapsulating KDE
  325. *
  326. * This does not include the IE type, length, or OUI bytes, which are
  327. * generic to all KDEs.
  328. */
  329. struct wpa_kde_gtk_encap
  330. {
  331. /** Key ID and TX bit */
  332. u8 id;
  333. /** Reserved byte */
  334. u8 _rsvd;
  335. /** Encapsulated group transient key */
  336. struct wpa_gtk gtk;
  337. } __attribute__ (( packed ));
  338. /** Mask for Key ID in wpa_kde_gtk::id field */
  339. #define WPA_GTK_KID 0x03
  340. /** Mask for Tx bit in wpa_kde_gtk::id field */
  341. #define WPA_GTK_TXBIT 0x04
  342. /** KDE type for an encapsulated Group Transient Key (requires encryption) */
  343. #define WPA_KDE_GTK _MKOUI ( 0x00, 0x0F, 0xAC, 0x01 )
  344. /** KDE type for a MAC address */
  345. #define WPA_KDE_MAC _MKOUI ( 0x00, 0x0F, 0xAC, 0x03 )
  346. /** KDE type for a PMKID */
  347. #define WPA_KDE_PMKID _MKOUI ( 0x00, 0x0F, 0xAC, 0x04 )
  348. /** KDE type for a nonce */
  349. #define WPA_KDE_NONCE _MKOUI ( 0x00, 0x0F, 0xAC, 0x06 )
  350. /** KDE type for a lifetime value */
  351. #define WPA_KDE_LIFETIME _MKOUI ( 0x00, 0x0F, 0xAC, 0x07 )
  352. /** Any key descriptor element type
  353. *
  354. * KDEs follow the 802.11 information element format of a type byte
  355. * (in this case "vendor-specific", with the requisite OUI+subtype
  356. * after length) and a length byte whose value does not include the
  357. * length of the type and length bytes.
  358. */
  359. struct wpa_kde
  360. {
  361. /** Information element type: always 0xDD (IEEE80211_IE_VENDOR) */
  362. u8 ie_type;
  363. /** Length, not including ie_type and length fields */
  364. u8 len;
  365. /** OUI + type byte */
  366. u32 oui_type;
  367. /** Payload data */
  368. union {
  369. /** For GTK-type KDEs, encapsulated GTK */
  370. struct wpa_kde_gtk_encap gtk_encap;
  371. /** For MAC-type KDEs, the MAC address */
  372. u8 mac[ETH_ALEN];
  373. /** For PMKID-type KDEs, the PMKID */
  374. u8 pmkid[WPA_PMKID_LEN];
  375. /** For Nonce-type KDEs, the nonce */
  376. u8 nonce[WPA_NONCE_LEN];
  377. /** For Lifetime-type KDEs, the lifetime in seconds
  378. *
  379. * This is in network byte order!
  380. */
  381. u32 lifetime;
  382. };
  383. } __attribute__ (( packed ));
  384. /** @} */
  385. int wpa_make_rsn_ie ( struct net80211_device *dev, union ieee80211_ie **ie );
  386. int wpa_start ( struct net80211_device *dev, struct wpa_common_ctx *ctx,
  387. const void *pmk, size_t pmk_len );
  388. void wpa_stop ( struct net80211_device *dev );
  389. #endif /* _GPXE_WPA_H */