ieee80211_crypto.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /* $OpenBSD: ieee80211_crypto.c,v 1.65 2014/12/23 03:24:08 tedu Exp $ */
  2. /*-
  3. * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/param.h>
  18. #include <sys/systm.h>
  19. #include <sys/mbuf.h>
  20. #include <sys/malloc.h>
  21. #include <sys/kernel.h>
  22. #include <sys/socket.h>
  23. #include <sys/sockio.h>
  24. #include <sys/endian.h>
  25. #include <sys/errno.h>
  26. #include <sys/sysctl.h>
  27. #include <net/if.h>
  28. #include <net/if_dl.h>
  29. #include <net/if_media.h>
  30. #include <net/if_arp.h>
  31. #include <netinet/in.h>
  32. #include <netinet/if_ether.h>
  33. #include <net80211/ieee80211_var.h>
  34. #include <net80211/ieee80211_priv.h>
  35. #include <crypto/arc4.h>
  36. #include <crypto/md5.h>
  37. #include <crypto/sha1.h>
  38. #include <crypto/sha2.h>
  39. #include <crypto/hmac.h>
  40. #include <crypto/rijndael.h>
  41. #include <crypto/cmac.h>
  42. #include <crypto/key_wrap.h>
  43. void ieee80211_prf(const u_int8_t *, size_t, const u_int8_t *, size_t,
  44. const u_int8_t *, size_t, u_int8_t *, size_t);
  45. void ieee80211_kdf(const u_int8_t *, size_t, const u_int8_t *, size_t,
  46. const u_int8_t *, size_t, u_int8_t *, size_t);
  47. void ieee80211_derive_pmkid(enum ieee80211_akm, const u_int8_t *,
  48. const u_int8_t *, const u_int8_t *, u_int8_t *);
  49. void
  50. ieee80211_crypto_attach(struct ifnet *ifp)
  51. {
  52. struct ieee80211com *ic = (void *)ifp;
  53. TAILQ_INIT(&ic->ic_pmksa);
  54. if (ic->ic_caps & IEEE80211_C_RSN) {
  55. ic->ic_rsnprotos = IEEE80211_PROTO_WPA | IEEE80211_PROTO_RSN;
  56. ic->ic_rsnakms = IEEE80211_AKM_PSK;
  57. ic->ic_rsnciphers = IEEE80211_CIPHER_TKIP |
  58. IEEE80211_CIPHER_CCMP;
  59. ic->ic_rsngroupcipher = IEEE80211_CIPHER_TKIP;
  60. ic->ic_rsngroupmgmtcipher = IEEE80211_CIPHER_BIP;
  61. }
  62. ic->ic_set_key = ieee80211_set_key;
  63. ic->ic_delete_key = ieee80211_delete_key;
  64. }
  65. void
  66. ieee80211_crypto_detach(struct ifnet *ifp)
  67. {
  68. struct ieee80211com *ic = (void *)ifp;
  69. struct ieee80211_pmk *pmk;
  70. int i;
  71. /* purge the PMKSA cache */
  72. while ((pmk = TAILQ_FIRST(&ic->ic_pmksa)) != NULL) {
  73. TAILQ_REMOVE(&ic->ic_pmksa, pmk, pmk_next);
  74. explicit_bzero(pmk, sizeof(*pmk));
  75. free(pmk, M_DEVBUF, 0);
  76. }
  77. /* clear all group keys from memory */
  78. for (i = 0; i < IEEE80211_GROUP_NKID; i++) {
  79. struct ieee80211_key *k = &ic->ic_nw_keys[i];
  80. if (k->k_cipher != IEEE80211_CIPHER_NONE)
  81. (*ic->ic_delete_key)(ic, NULL, k);
  82. explicit_bzero(k, sizeof(*k));
  83. }
  84. /* clear pre-shared key from memory */
  85. explicit_bzero(ic->ic_psk, IEEE80211_PMK_LEN);
  86. }
  87. /*
  88. * Return the length in bytes of a cipher suite key (see Table 60).
  89. */
  90. int
  91. ieee80211_cipher_keylen(enum ieee80211_cipher cipher)
  92. {
  93. switch (cipher) {
  94. case IEEE80211_CIPHER_WEP40:
  95. return 5;
  96. case IEEE80211_CIPHER_TKIP:
  97. return 32;
  98. case IEEE80211_CIPHER_CCMP:
  99. return 16;
  100. case IEEE80211_CIPHER_WEP104:
  101. return 13;
  102. case IEEE80211_CIPHER_BIP:
  103. return 16;
  104. default: /* unknown cipher */
  105. return 0;
  106. }
  107. }
  108. int
  109. ieee80211_set_key(struct ieee80211com *ic, struct ieee80211_node *ni,
  110. struct ieee80211_key *k)
  111. {
  112. int error;
  113. switch (k->k_cipher) {
  114. case IEEE80211_CIPHER_WEP40:
  115. case IEEE80211_CIPHER_WEP104:
  116. error = ieee80211_wep_set_key(ic, k);
  117. break;
  118. case IEEE80211_CIPHER_TKIP:
  119. error = ieee80211_tkip_set_key(ic, k);
  120. break;
  121. case IEEE80211_CIPHER_CCMP:
  122. error = ieee80211_ccmp_set_key(ic, k);
  123. break;
  124. case IEEE80211_CIPHER_BIP:
  125. error = ieee80211_bip_set_key(ic, k);
  126. break;
  127. default:
  128. /* should not get there */
  129. error = EINVAL;
  130. }
  131. return error;
  132. }
  133. void
  134. ieee80211_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni,
  135. struct ieee80211_key *k)
  136. {
  137. switch (k->k_cipher) {
  138. case IEEE80211_CIPHER_WEP40:
  139. case IEEE80211_CIPHER_WEP104:
  140. ieee80211_wep_delete_key(ic, k);
  141. break;
  142. case IEEE80211_CIPHER_TKIP:
  143. ieee80211_tkip_delete_key(ic, k);
  144. break;
  145. case IEEE80211_CIPHER_CCMP:
  146. ieee80211_ccmp_delete_key(ic, k);
  147. break;
  148. case IEEE80211_CIPHER_BIP:
  149. ieee80211_bip_delete_key(ic, k);
  150. break;
  151. default:
  152. /* should not get there */
  153. break;
  154. }
  155. explicit_bzero(k, sizeof(*k));
  156. }
  157. struct ieee80211_key *
  158. ieee80211_get_txkey(struct ieee80211com *ic, const struct ieee80211_frame *wh,
  159. struct ieee80211_node *ni)
  160. {
  161. int kid;
  162. if ((ic->ic_flags & IEEE80211_F_RSNON) &&
  163. !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
  164. ni->ni_rsncipher != IEEE80211_CIPHER_USEGROUP)
  165. return &ni->ni_pairwise_key;
  166. if (!IEEE80211_IS_MULTICAST(wh->i_addr1) ||
  167. (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
  168. IEEE80211_FC0_TYPE_MGT)
  169. kid = ic->ic_def_txkey;
  170. else
  171. kid = ic->ic_igtk_kid;
  172. return &ic->ic_nw_keys[kid];
  173. }
  174. struct mbuf *
  175. ieee80211_encrypt(struct ieee80211com *ic, struct mbuf *m0,
  176. struct ieee80211_key *k)
  177. {
  178. switch (k->k_cipher) {
  179. case IEEE80211_CIPHER_WEP40:
  180. case IEEE80211_CIPHER_WEP104:
  181. m0 = ieee80211_wep_encrypt(ic, m0, k);
  182. break;
  183. case IEEE80211_CIPHER_TKIP:
  184. m0 = ieee80211_tkip_encrypt(ic, m0, k);
  185. break;
  186. case IEEE80211_CIPHER_CCMP:
  187. m0 = ieee80211_ccmp_encrypt(ic, m0, k);
  188. break;
  189. case IEEE80211_CIPHER_BIP:
  190. m0 = ieee80211_bip_encap(ic, m0, k);
  191. break;
  192. default:
  193. /* should not get there */
  194. m_freem(m0);
  195. m0 = NULL;
  196. }
  197. return m0;
  198. }
  199. struct mbuf *
  200. ieee80211_decrypt(struct ieee80211com *ic, struct mbuf *m0,
  201. struct ieee80211_node *ni)
  202. {
  203. struct ieee80211_frame *wh;
  204. struct ieee80211_key *k;
  205. u_int8_t *ivp, *mmie;
  206. u_int16_t kid;
  207. int hdrlen;
  208. /* find key for decryption */
  209. wh = mtod(m0, struct ieee80211_frame *);
  210. if ((ic->ic_flags & IEEE80211_F_RSNON) &&
  211. !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
  212. ni->ni_rsncipher != IEEE80211_CIPHER_USEGROUP) {
  213. k = &ni->ni_pairwise_key;
  214. } else if (!IEEE80211_IS_MULTICAST(wh->i_addr1) ||
  215. (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
  216. IEEE80211_FC0_TYPE_MGT) {
  217. /* retrieve group data key id from IV field */
  218. hdrlen = ieee80211_get_hdrlen(wh);
  219. /* check that IV field is present */
  220. if (m0->m_len < hdrlen + 4) {
  221. m_freem(m0);
  222. return NULL;
  223. }
  224. ivp = (u_int8_t *)wh + hdrlen;
  225. kid = ivp[3] >> 6;
  226. k = &ic->ic_nw_keys[kid];
  227. } else {
  228. /* retrieve integrity group key id from MMIE */
  229. if (m0->m_len < sizeof(*wh) + IEEE80211_MMIE_LEN) {
  230. m_freem(m0);
  231. return NULL;
  232. }
  233. /* it is assumed management frames are contiguous */
  234. mmie = (u_int8_t *)wh + m0->m_len - IEEE80211_MMIE_LEN;
  235. /* check that MMIE is valid */
  236. if (mmie[0] != IEEE80211_ELEMID_MMIE || mmie[1] != 16) {
  237. m_freem(m0);
  238. return NULL;
  239. }
  240. kid = LE_READ_2(&mmie[2]);
  241. if (kid != 4 && kid != 5) {
  242. m_freem(m0);
  243. return NULL;
  244. }
  245. k = &ic->ic_nw_keys[kid];
  246. }
  247. switch (k->k_cipher) {
  248. case IEEE80211_CIPHER_WEP40:
  249. case IEEE80211_CIPHER_WEP104:
  250. m0 = ieee80211_wep_decrypt(ic, m0, k);
  251. break;
  252. case IEEE80211_CIPHER_TKIP:
  253. m0 = ieee80211_tkip_decrypt(ic, m0, k);
  254. break;
  255. case IEEE80211_CIPHER_CCMP:
  256. m0 = ieee80211_ccmp_decrypt(ic, m0, k);
  257. break;
  258. case IEEE80211_CIPHER_BIP:
  259. m0 = ieee80211_bip_decap(ic, m0, k);
  260. break;
  261. default:
  262. /* key not defined */
  263. m_freem(m0);
  264. m0 = NULL;
  265. }
  266. return m0;
  267. }
  268. /*
  269. * SHA1-based Pseudo-Random Function (see 8.5.1.1).
  270. */
  271. void
  272. ieee80211_prf(const u_int8_t *key, size_t key_len, const u_int8_t *label,
  273. size_t label_len, const u_int8_t *context, size_t context_len,
  274. u_int8_t *output, size_t len)
  275. {
  276. HMAC_SHA1_CTX ctx;
  277. u_int8_t digest[SHA1_DIGEST_LENGTH];
  278. u_int8_t count;
  279. for (count = 0; len != 0; count++) {
  280. HMAC_SHA1_Init(&ctx, key, key_len);
  281. HMAC_SHA1_Update(&ctx, label, label_len);
  282. HMAC_SHA1_Update(&ctx, context, context_len);
  283. HMAC_SHA1_Update(&ctx, &count, 1);
  284. if (len < SHA1_DIGEST_LENGTH) {
  285. HMAC_SHA1_Final(digest, &ctx);
  286. /* truncate HMAC-SHA1 to len bytes */
  287. memcpy(output, digest, len);
  288. break;
  289. }
  290. HMAC_SHA1_Final(output, &ctx);
  291. output += SHA1_DIGEST_LENGTH;
  292. len -= SHA1_DIGEST_LENGTH;
  293. }
  294. }
  295. /*
  296. * SHA256-based Key Derivation Function (see 8.5.1.5.2).
  297. */
  298. void
  299. ieee80211_kdf(const u_int8_t *key, size_t key_len, const u_int8_t *label,
  300. size_t label_len, const u_int8_t *context, size_t context_len,
  301. u_int8_t *output, size_t len)
  302. {
  303. HMAC_SHA256_CTX ctx;
  304. u_int8_t digest[SHA256_DIGEST_LENGTH];
  305. u_int16_t i, iter, length;
  306. length = htole16(len * NBBY);
  307. for (i = 1; len != 0; i++) {
  308. HMAC_SHA256_Init(&ctx, key, key_len);
  309. iter = htole16(i);
  310. HMAC_SHA256_Update(&ctx, (u_int8_t *)&iter, sizeof iter);
  311. HMAC_SHA256_Update(&ctx, label, label_len);
  312. HMAC_SHA256_Update(&ctx, context, context_len);
  313. HMAC_SHA256_Update(&ctx, (u_int8_t *)&length, sizeof length);
  314. if (len < SHA256_DIGEST_LENGTH) {
  315. HMAC_SHA256_Final(digest, &ctx);
  316. /* truncate HMAC-SHA-256 to len bytes */
  317. memcpy(output, digest, len);
  318. break;
  319. }
  320. HMAC_SHA256_Final(output, &ctx);
  321. output += SHA256_DIGEST_LENGTH;
  322. len -= SHA256_DIGEST_LENGTH;
  323. }
  324. }
  325. /*
  326. * Derive Pairwise Transient Key (PTK) (see 8.5.1.2).
  327. */
  328. void
  329. ieee80211_derive_ptk(enum ieee80211_akm akm, const u_int8_t *pmk,
  330. const u_int8_t *aa, const u_int8_t *spa, const u_int8_t *anonce,
  331. const u_int8_t *snonce, struct ieee80211_ptk *ptk)
  332. {
  333. void (*kdf)(const u_int8_t *, size_t, const u_int8_t *, size_t,
  334. const u_int8_t *, size_t, u_int8_t *, size_t);
  335. u_int8_t buf[2 * IEEE80211_ADDR_LEN + 2 * EAPOL_KEY_NONCE_LEN];
  336. int ret;
  337. /* Min(AA,SPA) || Max(AA,SPA) */
  338. ret = memcmp(aa, spa, IEEE80211_ADDR_LEN) < 0;
  339. memcpy(&buf[ 0], ret ? aa : spa, IEEE80211_ADDR_LEN);
  340. memcpy(&buf[ 6], ret ? spa : aa, IEEE80211_ADDR_LEN);
  341. /* Min(ANonce,SNonce) || Max(ANonce,SNonce) */
  342. ret = memcmp(anonce, snonce, EAPOL_KEY_NONCE_LEN) < 0;
  343. memcpy(&buf[12], ret ? anonce : snonce, EAPOL_KEY_NONCE_LEN);
  344. memcpy(&buf[44], ret ? snonce : anonce, EAPOL_KEY_NONCE_LEN);
  345. kdf = ieee80211_is_sha256_akm(akm) ? ieee80211_kdf : ieee80211_prf;
  346. (*kdf)(pmk, IEEE80211_PMK_LEN, "Pairwise key expansion", 23,
  347. buf, sizeof buf, (u_int8_t *)ptk, sizeof(*ptk));
  348. }
  349. static void
  350. ieee80211_pmkid_sha1(const u_int8_t *pmk, const u_int8_t *aa,
  351. const u_int8_t *spa, u_int8_t *pmkid)
  352. {
  353. HMAC_SHA1_CTX ctx;
  354. u_int8_t digest[SHA1_DIGEST_LENGTH];
  355. HMAC_SHA1_Init(&ctx, pmk, IEEE80211_PMK_LEN);
  356. HMAC_SHA1_Update(&ctx, "PMK Name", 8);
  357. HMAC_SHA1_Update(&ctx, aa, IEEE80211_ADDR_LEN);
  358. HMAC_SHA1_Update(&ctx, spa, IEEE80211_ADDR_LEN);
  359. HMAC_SHA1_Final(digest, &ctx);
  360. /* use the first 128 bits of HMAC-SHA1 */
  361. memcpy(pmkid, digest, IEEE80211_PMKID_LEN);
  362. }
  363. static void
  364. ieee80211_pmkid_sha256(const u_int8_t *pmk, const u_int8_t *aa,
  365. const u_int8_t *spa, u_int8_t *pmkid)
  366. {
  367. HMAC_SHA256_CTX ctx;
  368. u_int8_t digest[SHA256_DIGEST_LENGTH];
  369. HMAC_SHA256_Init(&ctx, pmk, IEEE80211_PMK_LEN);
  370. HMAC_SHA256_Update(&ctx, "PMK Name", 8);
  371. HMAC_SHA256_Update(&ctx, aa, IEEE80211_ADDR_LEN);
  372. HMAC_SHA256_Update(&ctx, spa, IEEE80211_ADDR_LEN);
  373. HMAC_SHA256_Final(digest, &ctx);
  374. /* use the first 128 bits of HMAC-SHA-256 */
  375. memcpy(pmkid, digest, IEEE80211_PMKID_LEN);
  376. }
  377. /*
  378. * Derive Pairwise Master Key Identifier (PMKID) (see 8.5.1.2).
  379. */
  380. void
  381. ieee80211_derive_pmkid(enum ieee80211_akm akm, const u_int8_t *pmk,
  382. const u_int8_t *aa, const u_int8_t *spa, u_int8_t *pmkid)
  383. {
  384. if (ieee80211_is_sha256_akm(akm))
  385. ieee80211_pmkid_sha256(pmk, aa, spa, pmkid);
  386. else
  387. ieee80211_pmkid_sha1(pmk, aa, spa, pmkid);
  388. }
  389. typedef union _ANY_CTX {
  390. HMAC_MD5_CTX md5;
  391. HMAC_SHA1_CTX sha1;
  392. AES_CMAC_CTX cmac;
  393. } ANY_CTX;
  394. /*
  395. * Compute the Key MIC field of an EAPOL-Key frame using the specified Key
  396. * Confirmation Key (KCK). The hash function can be HMAC-MD5, HMAC-SHA1
  397. * or AES-128-CMAC depending on the EAPOL-Key Key Descriptor Version.
  398. */
  399. void
  400. ieee80211_eapol_key_mic(struct ieee80211_eapol_key *key, const u_int8_t *kck)
  401. {
  402. u_int8_t digest[SHA1_DIGEST_LENGTH];
  403. ANY_CTX ctx; /* XXX off stack? */
  404. u_int len;
  405. len = BE_READ_2(key->len) + 4;
  406. switch (BE_READ_2(key->info) & EAPOL_KEY_VERSION_MASK) {
  407. case EAPOL_KEY_DESC_V1:
  408. HMAC_MD5_Init(&ctx.md5, kck, 16);
  409. HMAC_MD5_Update(&ctx.md5, (u_int8_t *)key, len);
  410. HMAC_MD5_Final(key->mic, &ctx.md5);
  411. break;
  412. case EAPOL_KEY_DESC_V2:
  413. HMAC_SHA1_Init(&ctx.sha1, kck, 16);
  414. HMAC_SHA1_Update(&ctx.sha1, (u_int8_t *)key, len);
  415. HMAC_SHA1_Final(digest, &ctx.sha1);
  416. /* truncate HMAC-SHA1 to its 128 MSBs */
  417. memcpy(key->mic, digest, EAPOL_KEY_MIC_LEN);
  418. break;
  419. case EAPOL_KEY_DESC_V3:
  420. AES_CMAC_Init(&ctx.cmac);
  421. AES_CMAC_SetKey(&ctx.cmac, kck);
  422. AES_CMAC_Update(&ctx.cmac, (u_int8_t *)key, len);
  423. AES_CMAC_Final(key->mic, &ctx.cmac);
  424. break;
  425. }
  426. }
  427. /*
  428. * Check the MIC of a received EAPOL-Key frame using the specified Key
  429. * Confirmation Key (KCK).
  430. */
  431. int
  432. ieee80211_eapol_key_check_mic(struct ieee80211_eapol_key *key,
  433. const u_int8_t *kck)
  434. {
  435. u_int8_t mic[EAPOL_KEY_MIC_LEN];
  436. memcpy(mic, key->mic, EAPOL_KEY_MIC_LEN);
  437. memset(key->mic, 0, EAPOL_KEY_MIC_LEN);
  438. ieee80211_eapol_key_mic(key, kck);
  439. return timingsafe_bcmp(key->mic, mic, EAPOL_KEY_MIC_LEN) != 0;
  440. }
  441. #ifndef IEEE80211_STA_ONLY
  442. /*
  443. * Encrypt the Key Data field of an EAPOL-Key frame using the specified Key
  444. * Encryption Key (KEK). The encryption algorithm can be either ARC4 or
  445. * AES Key Wrap depending on the EAPOL-Key Key Descriptor Version.
  446. */
  447. void
  448. ieee80211_eapol_key_encrypt(struct ieee80211com *ic,
  449. struct ieee80211_eapol_key *key, const u_int8_t *kek)
  450. {
  451. union {
  452. struct rc4_ctx rc4;
  453. aes_key_wrap_ctx aes;
  454. } ctx; /* XXX off stack? */
  455. u_int8_t keybuf[EAPOL_KEY_IV_LEN + 16];
  456. u_int16_t len, info;
  457. u_int8_t *data;
  458. int n;
  459. len = BE_READ_2(key->paylen);
  460. info = BE_READ_2(key->info);
  461. data = (u_int8_t *)(key + 1);
  462. switch (info & EAPOL_KEY_VERSION_MASK) {
  463. case EAPOL_KEY_DESC_V1:
  464. /* set IV to the lower 16 octets of our global key counter */
  465. memcpy(key->iv, ic->ic_globalcnt + 16, 16);
  466. /* increment our global key counter (256-bit, big-endian) */
  467. for (n = 31; n >= 0 && ++ic->ic_globalcnt[n] == 0; n--);
  468. /* concatenate the EAPOL-Key IV field and the KEK */
  469. memcpy(keybuf, key->iv, EAPOL_KEY_IV_LEN);
  470. memcpy(keybuf + EAPOL_KEY_IV_LEN, kek, 16);
  471. rc4_keysetup(&ctx.rc4, keybuf, sizeof keybuf);
  472. /* discard the first 256 octets of the ARC4 key stream */
  473. rc4_skip(&ctx.rc4, RC4STATE);
  474. rc4_crypt(&ctx.rc4, data, data, len);
  475. break;
  476. case EAPOL_KEY_DESC_V2:
  477. case EAPOL_KEY_DESC_V3:
  478. if (len < 16 || (len & 7) != 0) {
  479. /* insert padding */
  480. n = (len < 16) ? 16 - len : 8 - (len & 7);
  481. data[len++] = IEEE80211_ELEMID_VENDOR;
  482. memset(&data[len], 0, n - 1);
  483. len += n - 1;
  484. }
  485. aes_key_wrap_set_key_wrap_only(&ctx.aes, kek, 16);
  486. aes_key_wrap(&ctx.aes, data, len / 8, data);
  487. len += 8; /* AES Key Wrap adds 8 bytes */
  488. /* update key data length */
  489. BE_WRITE_2(key->paylen, len);
  490. /* update packet body length */
  491. BE_WRITE_2(key->len, sizeof(*key) + len - 4);
  492. break;
  493. }
  494. }
  495. #endif /* IEEE80211_STA_ONLY */
  496. /*
  497. * Decrypt the Key Data field of an EAPOL-Key frame using the specified Key
  498. * Encryption Key (KEK). The encryption algorithm can be either ARC4 or
  499. * AES Key Wrap depending on the EAPOL-Key Key Descriptor Version.
  500. */
  501. int
  502. ieee80211_eapol_key_decrypt(struct ieee80211_eapol_key *key,
  503. const u_int8_t *kek)
  504. {
  505. union {
  506. struct rc4_ctx rc4;
  507. aes_key_wrap_ctx aes;
  508. } ctx; /* XXX off stack? */
  509. u_int8_t keybuf[EAPOL_KEY_IV_LEN + 16];
  510. u_int16_t len, info;
  511. u_int8_t *data;
  512. len = BE_READ_2(key->paylen);
  513. info = BE_READ_2(key->info);
  514. data = (u_int8_t *)(key + 1);
  515. switch (info & EAPOL_KEY_VERSION_MASK) {
  516. case EAPOL_KEY_DESC_V1:
  517. /* concatenate the EAPOL-Key IV field and the KEK */
  518. memcpy(keybuf, key->iv, EAPOL_KEY_IV_LEN);
  519. memcpy(keybuf + EAPOL_KEY_IV_LEN, kek, 16);
  520. rc4_keysetup(&ctx.rc4, keybuf, sizeof keybuf);
  521. /* discard the first 256 octets of the ARC4 key stream */
  522. rc4_skip(&ctx.rc4, RC4STATE);
  523. rc4_crypt(&ctx.rc4, data, data, len);
  524. return 0;
  525. case EAPOL_KEY_DESC_V2:
  526. case EAPOL_KEY_DESC_V3:
  527. /* Key Data Length must be a multiple of 8 */
  528. if (len < 16 + 8 || (len & 7) != 0)
  529. return 1;
  530. len -= 8; /* AES Key Wrap adds 8 bytes */
  531. aes_key_wrap_set_key(&ctx.aes, kek, 16);
  532. return aes_key_unwrap(&ctx.aes, data, data, len / 8);
  533. }
  534. return 1; /* unknown Key Descriptor Version */
  535. }
  536. /*
  537. * Add a PMK entry to the PMKSA cache.
  538. */
  539. struct ieee80211_pmk *
  540. ieee80211_pmksa_add(struct ieee80211com *ic, enum ieee80211_akm akm,
  541. const u_int8_t *macaddr, const u_int8_t *key, u_int32_t lifetime)
  542. {
  543. struct ieee80211_pmk *pmk;
  544. /* check if an entry already exists for this (STA,AKMP) */
  545. TAILQ_FOREACH(pmk, &ic->ic_pmksa, pmk_next) {
  546. if (pmk->pmk_akm == akm &&
  547. IEEE80211_ADDR_EQ(pmk->pmk_macaddr, macaddr))
  548. break;
  549. }
  550. if (pmk == NULL) {
  551. /* allocate a new PMKSA entry */
  552. if ((pmk = malloc(sizeof(*pmk), M_DEVBUF, M_NOWAIT)) == NULL)
  553. return NULL;
  554. pmk->pmk_akm = akm;
  555. IEEE80211_ADDR_COPY(pmk->pmk_macaddr, macaddr);
  556. TAILQ_INSERT_TAIL(&ic->ic_pmksa, pmk, pmk_next);
  557. }
  558. memcpy(pmk->pmk_key, key, IEEE80211_PMK_LEN);
  559. pmk->pmk_lifetime = lifetime; /* XXX not used yet */
  560. #ifndef IEEE80211_STA_ONLY
  561. if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
  562. ieee80211_derive_pmkid(pmk->pmk_akm, pmk->pmk_key,
  563. ic->ic_myaddr, macaddr, pmk->pmk_pmkid);
  564. } else
  565. #endif
  566. {
  567. ieee80211_derive_pmkid(pmk->pmk_akm, pmk->pmk_key,
  568. macaddr, ic->ic_myaddr, pmk->pmk_pmkid);
  569. }
  570. return pmk;
  571. }
  572. /*
  573. * Check if we have a cached PMK entry for the specified node and PMKID.
  574. */
  575. struct ieee80211_pmk *
  576. ieee80211_pmksa_find(struct ieee80211com *ic, struct ieee80211_node *ni,
  577. const u_int8_t *pmkid)
  578. {
  579. struct ieee80211_pmk *pmk;
  580. TAILQ_FOREACH(pmk, &ic->ic_pmksa, pmk_next) {
  581. if (pmk->pmk_akm == ni->ni_rsnakms &&
  582. IEEE80211_ADDR_EQ(pmk->pmk_macaddr, ni->ni_macaddr) &&
  583. (pmkid == NULL ||
  584. memcmp(pmk->pmk_pmkid, pmkid, IEEE80211_PMKID_LEN) == 0))
  585. break;
  586. }
  587. return pmk;
  588. }