ieee80211_crypto_tkip.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /* $OpenBSD: ieee80211_crypto_tkip.c,v 1.24 2015/07/15 22:16:42 deraadt 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. /*
  18. * This code implements the Temporal Key Integrity Protocol (TKIP) defined
  19. * in IEEE Std 802.11-2007 section 8.3.2.
  20. */
  21. #include <sys/param.h>
  22. #include <sys/systm.h>
  23. #include <sys/mbuf.h>
  24. #include <sys/malloc.h>
  25. #include <sys/kernel.h>
  26. #include <sys/socket.h>
  27. #include <sys/endian.h>
  28. #include <sys/syslog.h>
  29. #include <net/if.h>
  30. #include <net/if_dl.h>
  31. #include <net/if_media.h>
  32. #include <net/if_arp.h>
  33. #include <netinet/in.h>
  34. #include <netinet/if_ether.h>
  35. #include <net80211/ieee80211_var.h>
  36. #include <net80211/ieee80211_crypto.h>
  37. #include <crypto/arc4.h>
  38. #include <crypto/michael.h>
  39. typedef u_int8_t byte; /* 8-bit byte (octet) */
  40. typedef u_int16_t u16b; /* 16-bit unsigned word */
  41. typedef u_int32_t u32b; /* 32-bit unsigned word */
  42. static void Phase1(u16b *, const byte *, const byte *, u32b);
  43. static void Phase2(byte *, const byte *, const u16b *, u16b);
  44. /* TKIP software crypto context */
  45. struct ieee80211_tkip_ctx {
  46. struct rc4_ctx rc4;
  47. const u_int8_t *txmic;
  48. const u_int8_t *rxmic;
  49. u_int16_t txttak[5];
  50. u_int16_t rxttak[5];
  51. u_int8_t txttak_ok;
  52. u_int8_t rxttak_ok;
  53. };
  54. /*
  55. * Initialize software crypto context. This function can be overridden
  56. * by drivers doing hardware crypto.
  57. */
  58. int
  59. ieee80211_tkip_set_key(struct ieee80211com *ic, struct ieee80211_key *k)
  60. {
  61. struct ieee80211_tkip_ctx *ctx;
  62. ctx = malloc(sizeof(*ctx), M_DEVBUF, M_NOWAIT | M_ZERO);
  63. if (ctx == NULL)
  64. return ENOMEM;
  65. /*
  66. * Use bits 128-191 as the Michael key for AA->SPA and bits
  67. * 192-255 as the Michael key for SPA->AA.
  68. */
  69. #ifndef IEEE80211_STA_ONLY
  70. if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
  71. ctx->txmic = &k->k_key[16];
  72. ctx->rxmic = &k->k_key[24];
  73. } else
  74. #endif
  75. {
  76. ctx->rxmic = &k->k_key[16];
  77. ctx->txmic = &k->k_key[24];
  78. }
  79. k->k_priv = ctx;
  80. return 0;
  81. }
  82. void
  83. ieee80211_tkip_delete_key(struct ieee80211com *ic, struct ieee80211_key *k)
  84. {
  85. if (k->k_priv != NULL)
  86. free(k->k_priv, M_DEVBUF, 0);
  87. k->k_priv = NULL;
  88. }
  89. /* pseudo-header used for TKIP MIC computation */
  90. struct ieee80211_tkip_frame {
  91. u_int8_t i_da[IEEE80211_ADDR_LEN];
  92. u_int8_t i_sa[IEEE80211_ADDR_LEN];
  93. u_int8_t i_pri;
  94. u_int8_t i_pad[3];
  95. } __packed;
  96. /*
  97. * Compute TKIP MIC over an mbuf chain starting "off" bytes from the
  98. * beginning. This function should be kept independant from the software
  99. * TKIP crypto code so that drivers doing hardware crypto but not MIC can
  100. * call it without a software crypto context.
  101. */
  102. void
  103. ieee80211_tkip_mic(struct mbuf *m0, int off, const u_int8_t *key,
  104. u_int8_t mic[IEEE80211_TKIP_MICLEN])
  105. {
  106. const struct ieee80211_frame *wh;
  107. struct ieee80211_tkip_frame wht;
  108. MICHAEL_CTX ctx; /* small enough */
  109. struct mbuf *m;
  110. caddr_t pos;
  111. int len;
  112. /* assumes 802.11 header is contiguous */
  113. wh = mtod(m0, struct ieee80211_frame *);
  114. /* construct pseudo-header for TKIP MIC computation */
  115. switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
  116. case IEEE80211_FC1_DIR_NODS:
  117. IEEE80211_ADDR_COPY(wht.i_da, wh->i_addr1);
  118. IEEE80211_ADDR_COPY(wht.i_sa, wh->i_addr2);
  119. break;
  120. case IEEE80211_FC1_DIR_TODS:
  121. IEEE80211_ADDR_COPY(wht.i_da, wh->i_addr3);
  122. IEEE80211_ADDR_COPY(wht.i_sa, wh->i_addr2);
  123. break;
  124. case IEEE80211_FC1_DIR_FROMDS:
  125. IEEE80211_ADDR_COPY(wht.i_da, wh->i_addr1);
  126. IEEE80211_ADDR_COPY(wht.i_sa, wh->i_addr3);
  127. break;
  128. case IEEE80211_FC1_DIR_DSTODS:
  129. IEEE80211_ADDR_COPY(wht.i_da, wh->i_addr3);
  130. IEEE80211_ADDR_COPY(wht.i_sa,
  131. ((const struct ieee80211_frame_addr4 *)wh)->i_addr4);
  132. break;
  133. }
  134. if (ieee80211_has_qos(wh))
  135. wht.i_pri = ieee80211_get_qos(wh) & IEEE80211_QOS_TID;
  136. else
  137. wht.i_pri = 0;
  138. wht.i_pad[0] = wht.i_pad[1] = wht.i_pad[2] = 0;
  139. michael_init(&ctx);
  140. michael_key(key, &ctx);
  141. michael_update(&ctx, (caddr_t)&wht, sizeof(wht));
  142. m = m0;
  143. /* assumes the first "off" bytes are contiguous */
  144. pos = mtod(m, caddr_t) + off;
  145. len = m->m_len - off;
  146. for (;;) {
  147. michael_update(&ctx, pos, len);
  148. if ((m = m->m_next) == NULL)
  149. break;
  150. pos = mtod(m, caddr_t);
  151. len = m->m_len;
  152. }
  153. michael_final(mic, &ctx);
  154. }
  155. /* shortcuts */
  156. #define IEEE80211_TKIP_TAILLEN \
  157. (IEEE80211_TKIP_MICLEN + IEEE80211_WEP_CRCLEN)
  158. #define IEEE80211_TKIP_OVHD \
  159. (IEEE80211_TKIP_HDRLEN + IEEE80211_TKIP_TAILLEN)
  160. struct mbuf *
  161. ieee80211_tkip_encrypt(struct ieee80211com *ic, struct mbuf *m0,
  162. struct ieee80211_key *k)
  163. {
  164. struct ieee80211_tkip_ctx *ctx = k->k_priv;
  165. u_int16_t wepseed[8]; /* needs to be 16-bit aligned for Phase2 */
  166. const struct ieee80211_frame *wh;
  167. u_int8_t *ivp, *mic, *icvp;
  168. struct mbuf *n0, *m, *n;
  169. u_int32_t crc;
  170. int left, moff, noff, len, hdrlen;
  171. MGET(n0, M_DONTWAIT, m0->m_type);
  172. if (n0 == NULL)
  173. goto nospace;
  174. if (m_dup_pkthdr(n0, m0, M_DONTWAIT))
  175. goto nospace;
  176. n0->m_pkthdr.len += IEEE80211_TKIP_HDRLEN;
  177. n0->m_len = MHLEN;
  178. if (n0->m_pkthdr.len >= MINCLSIZE - IEEE80211_TKIP_TAILLEN) {
  179. MCLGET(n0, M_DONTWAIT);
  180. if (n0->m_flags & M_EXT)
  181. n0->m_len = n0->m_ext.ext_size;
  182. }
  183. if (n0->m_len > n0->m_pkthdr.len)
  184. n0->m_len = n0->m_pkthdr.len;
  185. /* copy 802.11 header */
  186. wh = mtod(m0, struct ieee80211_frame *);
  187. hdrlen = ieee80211_get_hdrlen(wh);
  188. memcpy(mtod(n0, caddr_t), wh, hdrlen);
  189. k->k_tsc++; /* increment the 48-bit TSC */
  190. /* construct TKIP header */
  191. ivp = mtod(n0, u_int8_t *) + hdrlen;
  192. ivp[0] = k->k_tsc >> 8; /* TSC1 */
  193. /* WEP Seed = (TSC1 | 0x20) & 0x7f (see 8.3.2.2) */
  194. ivp[1] = (ivp[0] | 0x20) & 0x7f;
  195. ivp[2] = k->k_tsc; /* TSC0 */
  196. ivp[3] = k->k_id << 6 | IEEE80211_WEP_EXTIV; /* KeyID | ExtIV */
  197. ivp[4] = k->k_tsc >> 16; /* TSC2 */
  198. ivp[5] = k->k_tsc >> 24; /* TSC3 */
  199. ivp[6] = k->k_tsc >> 32; /* TSC4 */
  200. ivp[7] = k->k_tsc >> 40; /* TSC5 */
  201. /* compute WEP seed */
  202. if (!ctx->txttak_ok || (k->k_tsc & 0xffff) == 0) {
  203. Phase1(ctx->txttak, k->k_key, wh->i_addr2, k->k_tsc >> 16);
  204. ctx->txttak_ok = 1;
  205. }
  206. Phase2((u_int8_t *)wepseed, k->k_key, ctx->txttak, k->k_tsc & 0xffff);
  207. rc4_keysetup(&ctx->rc4, (u_int8_t *)wepseed, 16);
  208. /* encrypt frame body and compute WEP ICV */
  209. m = m0;
  210. n = n0;
  211. moff = hdrlen;
  212. noff = hdrlen + IEEE80211_TKIP_HDRLEN;
  213. left = m0->m_pkthdr.len - moff;
  214. crc = ~0;
  215. while (left > 0) {
  216. if (moff == m->m_len) {
  217. /* nothing left to copy from m */
  218. m = m->m_next;
  219. moff = 0;
  220. }
  221. if (noff == n->m_len) {
  222. /* n is full and there's more data to copy */
  223. MGET(n->m_next, M_DONTWAIT, n->m_type);
  224. if (n->m_next == NULL)
  225. goto nospace;
  226. n = n->m_next;
  227. n->m_len = MLEN;
  228. if (left >= MINCLSIZE - IEEE80211_TKIP_TAILLEN) {
  229. MCLGET(n, M_DONTWAIT);
  230. if (n->m_flags & M_EXT)
  231. n->m_len = n->m_ext.ext_size;
  232. }
  233. if (n->m_len > left)
  234. n->m_len = left;
  235. noff = 0;
  236. }
  237. len = min(m->m_len - moff, n->m_len - noff);
  238. crc = ether_crc32_le_update(crc, mtod(m, caddr_t) + moff, len);
  239. rc4_crypt(&ctx->rc4, mtod(m, caddr_t) + moff,
  240. mtod(n, caddr_t) + noff, len);
  241. moff += len;
  242. noff += len;
  243. left -= len;
  244. }
  245. /* reserve trailing space for TKIP MIC and WEP ICV */
  246. if (M_TRAILINGSPACE(n) < IEEE80211_TKIP_TAILLEN) {
  247. MGET(n->m_next, M_DONTWAIT, n->m_type);
  248. if (n->m_next == NULL)
  249. goto nospace;
  250. n = n->m_next;
  251. n->m_len = 0;
  252. }
  253. /* compute TKIP MIC over clear text */
  254. mic = mtod(n, caddr_t) + n->m_len;
  255. ieee80211_tkip_mic(m0, hdrlen, ctx->txmic, mic);
  256. crc = ether_crc32_le_update(crc, mic, IEEE80211_TKIP_MICLEN);
  257. rc4_crypt(&ctx->rc4, mic, mic, IEEE80211_TKIP_MICLEN);
  258. n->m_len += IEEE80211_TKIP_MICLEN;
  259. /* finalize WEP ICV */
  260. icvp = mtod(n, caddr_t) + n->m_len;
  261. crc = ~crc;
  262. icvp[0] = crc;
  263. icvp[1] = crc >> 8;
  264. icvp[2] = crc >> 16;
  265. icvp[3] = crc >> 24;
  266. rc4_crypt(&ctx->rc4, icvp, icvp, IEEE80211_WEP_CRCLEN);
  267. n->m_len += IEEE80211_WEP_CRCLEN;
  268. n0->m_pkthdr.len += IEEE80211_TKIP_TAILLEN;
  269. m_freem(m0);
  270. return n0;
  271. nospace:
  272. ic->ic_stats.is_tx_nombuf++;
  273. m_freem(m0);
  274. m_freem(n0);
  275. return NULL;
  276. }
  277. struct mbuf *
  278. ieee80211_tkip_decrypt(struct ieee80211com *ic, struct mbuf *m0,
  279. struct ieee80211_key *k)
  280. {
  281. struct ieee80211_tkip_ctx *ctx = k->k_priv;
  282. struct ieee80211_frame *wh;
  283. u_int16_t wepseed[8]; /* needs to be 16-bit aligned for Phase2 */
  284. u_int8_t buf[IEEE80211_TKIP_MICLEN + IEEE80211_WEP_CRCLEN];
  285. u_int8_t mic[IEEE80211_TKIP_MICLEN];
  286. u_int64_t tsc, *prsc;
  287. u_int32_t crc, crc0;
  288. u_int8_t *ivp, *mic0;
  289. u_int8_t tid;
  290. struct mbuf *n0, *m, *n;
  291. int hdrlen, left, moff, noff, len;
  292. wh = mtod(m0, struct ieee80211_frame *);
  293. hdrlen = ieee80211_get_hdrlen(wh);
  294. if (m0->m_pkthdr.len < hdrlen + IEEE80211_TKIP_OVHD) {
  295. m_freem(m0);
  296. return NULL;
  297. }
  298. ivp = (u_int8_t *)wh + hdrlen;
  299. /* check that ExtIV bit is set */
  300. if (!(ivp[3] & IEEE80211_WEP_EXTIV)) {
  301. m_freem(m0);
  302. return NULL;
  303. }
  304. /* retrieve last seen packet number for this frame priority */
  305. tid = ieee80211_has_qos(wh) ?
  306. ieee80211_get_qos(wh) & IEEE80211_QOS_TID : 0;
  307. prsc = &k->k_rsc[tid];
  308. /* extract the 48-bit TSC from the TKIP header */
  309. tsc = (u_int64_t)ivp[2] |
  310. (u_int64_t)ivp[0] << 8 |
  311. (u_int64_t)ivp[4] << 16 |
  312. (u_int64_t)ivp[5] << 24 |
  313. (u_int64_t)ivp[6] << 32 |
  314. (u_int64_t)ivp[7] << 40;
  315. if (tsc <= *prsc) {
  316. /* replayed frame, discard */
  317. ic->ic_stats.is_tkip_replays++;
  318. m_freem(m0);
  319. return NULL;
  320. }
  321. MGET(n0, M_DONTWAIT, m0->m_type);
  322. if (n0 == NULL)
  323. goto nospace;
  324. if (m_dup_pkthdr(n0, m0, M_DONTWAIT))
  325. goto nospace;
  326. n0->m_pkthdr.len -= IEEE80211_TKIP_OVHD;
  327. n0->m_len = MHLEN;
  328. if (n0->m_pkthdr.len >= MINCLSIZE) {
  329. MCLGET(n0, M_DONTWAIT);
  330. if (n0->m_flags & M_EXT)
  331. n0->m_len = n0->m_ext.ext_size;
  332. }
  333. if (n0->m_len > n0->m_pkthdr.len)
  334. n0->m_len = n0->m_pkthdr.len;
  335. /* copy 802.11 header and clear protected bit */
  336. memcpy(mtod(n0, caddr_t), wh, hdrlen);
  337. wh = mtod(n0, struct ieee80211_frame *);
  338. wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
  339. /* compute WEP seed */
  340. if (!ctx->rxttak_ok || (tsc >> 16) != (*prsc >> 16)) {
  341. ctx->rxttak_ok = 0; /* invalidate cached TTAK (if any) */
  342. Phase1(ctx->rxttak, k->k_key, wh->i_addr2, tsc >> 16);
  343. }
  344. Phase2((u_int8_t *)wepseed, k->k_key, ctx->rxttak, tsc & 0xffff);
  345. rc4_keysetup(&ctx->rc4, (u_int8_t *)wepseed, 16);
  346. /* decrypt frame body and compute WEP ICV */
  347. m = m0;
  348. n = n0;
  349. moff = hdrlen + IEEE80211_TKIP_HDRLEN;
  350. noff = hdrlen;
  351. left = n0->m_pkthdr.len - noff;
  352. crc = ~0;
  353. while (left > 0) {
  354. if (moff == m->m_len) {
  355. /* nothing left to copy from m */
  356. m = m->m_next;
  357. moff = 0;
  358. }
  359. if (noff == n->m_len) {
  360. /* n is full and there's more data to copy */
  361. MGET(n->m_next, M_DONTWAIT, n->m_type);
  362. if (n->m_next == NULL)
  363. goto nospace;
  364. n = n->m_next;
  365. n->m_len = MLEN;
  366. if (left >= MINCLSIZE) {
  367. MCLGET(n, M_DONTWAIT);
  368. if (n->m_flags & M_EXT)
  369. n->m_len = n->m_ext.ext_size;
  370. }
  371. if (n->m_len > left)
  372. n->m_len = left;
  373. noff = 0;
  374. }
  375. len = min(m->m_len - moff, n->m_len - noff);
  376. rc4_crypt(&ctx->rc4, mtod(m, caddr_t) + moff,
  377. mtod(n, caddr_t) + noff, len);
  378. crc = ether_crc32_le_update(crc, mtod(n, caddr_t) + noff, len);
  379. moff += len;
  380. noff += len;
  381. left -= len;
  382. }
  383. /* extract and decrypt TKIP MIC and WEP ICV from m0's tail */
  384. m_copydata(m, moff, IEEE80211_TKIP_TAILLEN, buf);
  385. rc4_crypt(&ctx->rc4, buf, buf, IEEE80211_TKIP_TAILLEN);
  386. /* include TKIP MIC in WEP ICV */
  387. mic0 = buf;
  388. crc = ether_crc32_le_update(crc, mic0, IEEE80211_TKIP_MICLEN);
  389. crc = ~crc;
  390. /* decrypt ICV and compare it with calculated ICV */
  391. crc0 = *(u_int32_t *)(buf + IEEE80211_TKIP_MICLEN);
  392. if (crc != letoh32(crc0)) {
  393. ic->ic_stats.is_tkip_icv_errs++;
  394. m_freem(m0);
  395. m_freem(n0);
  396. return NULL;
  397. }
  398. /* compute TKIP MIC over decrypted message */
  399. ieee80211_tkip_mic(n0, hdrlen, ctx->rxmic, mic);
  400. /* check that it matches the MIC in received frame */
  401. if (timingsafe_bcmp(mic0, mic, IEEE80211_TKIP_MICLEN) != 0) {
  402. m_freem(m0);
  403. m_freem(n0);
  404. ic->ic_stats.is_rx_locmicfail++;
  405. ieee80211_michael_mic_failure(ic, tsc);
  406. return NULL;
  407. }
  408. /* update last seen packet number (MIC is validated) */
  409. *prsc = tsc;
  410. /* mark cached TTAK as valid */
  411. ctx->rxttak_ok = 1;
  412. m_freem(m0);
  413. return n0;
  414. nospace:
  415. ic->ic_stats.is_rx_nombuf++;
  416. m_freem(m0);
  417. m_freem(n0);
  418. return NULL;
  419. }
  420. #ifndef IEEE80211_STA_ONLY
  421. /*
  422. * This function is called in HostAP mode to deauthenticate all STAs using
  423. * TKIP as their pairwise or group cipher (as part of TKIP countermeasures).
  424. */
  425. static void
  426. ieee80211_tkip_deauth(void *arg, struct ieee80211_node *ni)
  427. {
  428. struct ieee80211com *ic = arg;
  429. if (ni->ni_state == IEEE80211_STA_ASSOC &&
  430. (ic->ic_bss->ni_rsngroupcipher == IEEE80211_CIPHER_TKIP ||
  431. ni->ni_rsncipher == IEEE80211_CIPHER_TKIP)) {
  432. /* deauthenticate STA */
  433. IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
  434. IEEE80211_REASON_MIC_FAILURE);
  435. ieee80211_node_leave(ic, ni);
  436. }
  437. }
  438. #endif /* IEEE80211_STA_ONLY */
  439. /*
  440. * This function can be called by the software TKIP crypto code or by the
  441. * drivers when their hardware crypto engines detect a Michael MIC failure.
  442. */
  443. void
  444. ieee80211_michael_mic_failure(struct ieee80211com *ic, u_int64_t tsc)
  445. {
  446. extern int ticks;
  447. if (ic->ic_flags & IEEE80211_F_COUNTERM)
  448. return; /* countermeasures already active */
  449. log(LOG_WARNING, "%s: Michael MIC failure\n", ic->ic_if.if_xname);
  450. /*
  451. * NB. do not send Michael MIC Failure reports as recommended since
  452. * these may be used as an oracle to verify CRC guesses as described
  453. * in Beck, M. and Tews S. "Practical attacks against WEP and WPA"
  454. * http://dl.aircrack-ng.org/breakingwepandwpa.pdf
  455. */
  456. /*
  457. * Activate TKIP countermeasures (see 8.3.2.4) if less than 60
  458. * seconds have passed since the most recent previous MIC failure.
  459. */
  460. if (ic->ic_tkip_micfail == 0 ||
  461. ticks - (ic->ic_tkip_micfail + 60 * hz) >= 0) {
  462. ic->ic_tkip_micfail = ticks;
  463. ic->ic_tkip_micfail_last_tsc = tsc;
  464. return;
  465. }
  466. switch (ic->ic_opmode) {
  467. #ifndef IEEE80211_STA_ONLY
  468. case IEEE80211_M_HOSTAP:
  469. /* refuse new TKIP associations for the next 60 seconds */
  470. ic->ic_flags |= IEEE80211_F_COUNTERM;
  471. /* deauthenticate all currently associated STAs using TKIP */
  472. ieee80211_iterate_nodes(ic, ieee80211_tkip_deauth, ic);
  473. break;
  474. #endif
  475. case IEEE80211_M_STA:
  476. /*
  477. * Notify the AP of MIC failures: send two Michael
  478. * MIC Failure Report frames back-to-back to trigger
  479. * countermeasures at the AP end.
  480. */
  481. (void)ieee80211_send_eapol_key_req(ic, ic->ic_bss,
  482. EAPOL_KEY_KEYMIC | EAPOL_KEY_ERROR | EAPOL_KEY_SECURE,
  483. ic->ic_tkip_micfail_last_tsc);
  484. (void)ieee80211_send_eapol_key_req(ic, ic->ic_bss,
  485. EAPOL_KEY_KEYMIC | EAPOL_KEY_ERROR | EAPOL_KEY_SECURE,
  486. tsc);
  487. /* deauthenticate from the AP.. */
  488. IEEE80211_SEND_MGMT(ic, ic->ic_bss,
  489. IEEE80211_FC0_SUBTYPE_DEAUTH,
  490. IEEE80211_REASON_MIC_FAILURE);
  491. /* ..and find another one */
  492. (void)ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
  493. break;
  494. default:
  495. break;
  496. }
  497. ic->ic_tkip_micfail = ticks;
  498. ic->ic_tkip_micfail_last_tsc = tsc;
  499. }
  500. /***********************************************************************
  501. Contents: Generate IEEE 802.11 per-frame RC4 key hash test vectors
  502. Date: April 19, 2002
  503. Notes:
  504. This code is written for pedagogical purposes, NOT for performance.
  505. ************************************************************************/
  506. /* macros for extraction/creation of byte/u16b values */
  507. #define RotR1(v16) ((((v16) >> 1) & 0x7FFF) ^ (((v16) & 1) << 15))
  508. #define Lo8(v16) ((byte)( (v16) & 0x00FF))
  509. #define Hi8(v16) ((byte)(((v16) >> 8) & 0x00FF))
  510. #define Lo16(v32) ((u16b)( (v32) & 0xFFFF))
  511. #define Hi16(v32) ((u16b)(((v32) >>16) & 0xFFFF))
  512. #define Mk16(hi,lo) ((lo) ^ (((u16b)(hi)) << 8))
  513. /* select the Nth 16-bit word of the Temporal Key byte array TK[] */
  514. #define TK16(N) Mk16(TK[2 * (N) + 1], TK[2 * (N)])
  515. /* S-box lookup: 16 bits --> 16 bits */
  516. #define _S_(v16) (Sbox[Lo8(v16)] ^ swap16(Sbox[Hi8(v16)]))
  517. /* fixed algorithm "parameters" */
  518. #define PHASE1_LOOP_CNT 8 /* this needs to be "big enough" */
  519. #define TA_SIZE 6 /* 48-bit transmitter address */
  520. #define TK_SIZE 16 /* 128-bit Temporal Key */
  521. #define P1K_SIZE 10 /* 80-bit Phase1 key */
  522. #define RC4_KEY_SIZE 16 /* 128-bit RC4KEY (104 bits unknown) */
  523. /* 2-byte by 2-byte subset of the full AES S-box table */
  524. static const u16b Sbox[256]= /* Sbox for hash */
  525. {
  526. 0xC6A5, 0xF884, 0xEE99, 0xF68D, 0xFF0D, 0xD6BD, 0xDEB1, 0x9154,
  527. 0x6050, 0x0203, 0xCEA9, 0x567D, 0xE719, 0xB562, 0x4DE6, 0xEC9A,
  528. 0x8F45, 0x1F9D, 0x8940, 0xFA87, 0xEF15, 0xB2EB, 0x8EC9, 0xFB0B,
  529. 0x41EC, 0xB367, 0x5FFD, 0x45EA, 0x23BF, 0x53F7, 0xE496, 0x9B5B,
  530. 0x75C2, 0xE11C, 0x3DAE, 0x4C6A, 0x6C5A, 0x7E41, 0xF502, 0x834F,
  531. 0x685C, 0x51F4, 0xD134, 0xF908, 0xE293, 0xAB73, 0x6253, 0x2A3F,
  532. 0x080C, 0x9552, 0x4665, 0x9D5E, 0x3028, 0x37A1, 0x0A0F, 0x2FB5,
  533. 0x0E09, 0x2436, 0x1B9B, 0xDF3D, 0xCD26, 0x4E69, 0x7FCD, 0xEA9F,
  534. 0x121B, 0x1D9E, 0x5874, 0x342E, 0x362D, 0xDCB2, 0xB4EE, 0x5BFB,
  535. 0xA4F6, 0x764D, 0xB761, 0x7DCE, 0x527B, 0xDD3E, 0x5E71, 0x1397,
  536. 0xA6F5, 0xB968, 0x0000, 0xC12C, 0x4060, 0xE31F, 0x79C8, 0xB6ED,
  537. 0xD4BE, 0x8D46, 0x67D9, 0x724B, 0x94DE, 0x98D4, 0xB0E8, 0x854A,
  538. 0xBB6B, 0xC52A, 0x4FE5, 0xED16, 0x86C5, 0x9AD7, 0x6655, 0x1194,
  539. 0x8ACF, 0xE910, 0x0406, 0xFE81, 0xA0F0, 0x7844, 0x25BA, 0x4BE3,
  540. 0xA2F3, 0x5DFE, 0x80C0, 0x058A, 0x3FAD, 0x21BC, 0x7048, 0xF104,
  541. 0x63DF, 0x77C1, 0xAF75, 0x4263, 0x2030, 0xE51A, 0xFD0E, 0xBF6D,
  542. 0x814C, 0x1814, 0x2635, 0xC32F, 0xBEE1, 0x35A2, 0x88CC, 0x2E39,
  543. 0x9357, 0x55F2, 0xFC82, 0x7A47, 0xC8AC, 0xBAE7, 0x322B, 0xE695,
  544. 0xC0A0, 0x1998, 0x9ED1, 0xA37F, 0x4466, 0x547E, 0x3BAB, 0x0B83,
  545. 0x8CCA, 0xC729, 0x6BD3, 0x283C, 0xA779, 0xBCE2, 0x161D, 0xAD76,
  546. 0xDB3B, 0x6456, 0x744E, 0x141E, 0x92DB, 0x0C0A, 0x486C, 0xB8E4,
  547. 0x9F5D, 0xBD6E, 0x43EF, 0xC4A6, 0x39A8, 0x31A4, 0xD337, 0xF28B,
  548. 0xD532, 0x8B43, 0x6E59, 0xDAB7, 0x018C, 0xB164, 0x9CD2, 0x49E0,
  549. 0xD8B4, 0xACFA, 0xF307, 0xCF25, 0xCAAF, 0xF48E, 0x47E9, 0x1018,
  550. 0x6FD5, 0xF088, 0x4A6F, 0x5C72, 0x3824, 0x57F1, 0x73C7, 0x9751,
  551. 0xCB23, 0xA17C, 0xE89C, 0x3E21, 0x96DD, 0x61DC, 0x0D86, 0x0F85,
  552. 0xE090, 0x7C42, 0x71C4, 0xCCAA, 0x90D8, 0x0605, 0xF701, 0x1C12,
  553. 0xC2A3, 0x6A5F, 0xAEF9, 0x69D0, 0x1791, 0x9958, 0x3A27, 0x27B9,
  554. 0xD938, 0xEB13, 0x2BB3, 0x2233, 0xD2BB, 0xA970, 0x0789, 0x33A7,
  555. 0x2DB6, 0x3C22, 0x1592, 0xC920, 0x8749, 0xAAFF, 0x5078, 0xA57A,
  556. 0x038F, 0x59F8, 0x0980, 0x1A17, 0x65DA, 0xD731, 0x84C6, 0xD0B8,
  557. 0x82C3, 0x29B0, 0x5A77, 0x1E11, 0x7BCB, 0xA8FC, 0x6DD6, 0x2C3A
  558. };
  559. /*
  560. **********************************************************************
  561. * Routine: Phase 1 -- generate P1K, given TA, TK, IV32
  562. *
  563. * Inputs:
  564. * TK[] = Temporal Key [128 bits]
  565. * TA[] = transmitter's MAC address [ 48 bits]
  566. * IV32 = upper 32 bits of IV [ 32 bits]
  567. * Output:
  568. * P1K[] = Phase 1 key [ 80 bits]
  569. *
  570. * Note:
  571. * This function only needs to be called every 2**16 frames,
  572. * although in theory it could be called every frame.
  573. *
  574. **********************************************************************
  575. */
  576. static void
  577. Phase1(u16b *P1K, const byte *TK, const byte *TA, u32b IV32)
  578. {
  579. int i;
  580. /* Initialize the 80 bits of P1K[] from IV32 and TA[0..5] */
  581. P1K[0] = Lo16(IV32);
  582. P1K[1] = Hi16(IV32);
  583. P1K[2] = Mk16(TA[1], TA[0]); /* use TA[] as little-endian */
  584. P1K[3] = Mk16(TA[3], TA[2]);
  585. P1K[4] = Mk16(TA[5], TA[4]);
  586. /* Now compute an unbalanced Feistel cipher with 80-bit block */
  587. /* size on the 80-bit block P1K[], using the 128-bit key TK[] */
  588. for (i = 0; i < PHASE1_LOOP_CNT; i++) {
  589. /* Each add operation here is mod 2**16 */
  590. P1K[0] += _S_(P1K[4] ^ TK16((i & 1) + 0));
  591. P1K[1] += _S_(P1K[0] ^ TK16((i & 1) + 2));
  592. P1K[2] += _S_(P1K[1] ^ TK16((i & 1) + 4));
  593. P1K[3] += _S_(P1K[2] ^ TK16((i & 1) + 6));
  594. P1K[4] += _S_(P1K[3] ^ TK16((i & 1) + 0));
  595. P1K[4] += i; /* avoid "slide attacks" */
  596. }
  597. }
  598. /*
  599. **********************************************************************
  600. * Routine: Phase 2 -- generate RC4KEY, given TK, P1K, IV16
  601. *
  602. * Inputs:
  603. * TK[] = Temporal Key [128 bits]
  604. * P1K[] = Phase 1 output key [ 80 bits]
  605. * IV16 = low 16 bits of IV counter [ 16 bits]
  606. * Output:
  607. * RC4KEY[] = the key used to encrypt the frame [128 bits]
  608. *
  609. * Note:
  610. * The value {TA,IV32,IV16} for Phase1/Phase2 must be unique
  611. * across all frames using the same key TK value. Then, for a
  612. * given value of TK[], this TKIP48 construction guarantees that
  613. * the final RC4KEY value is unique across all frames.
  614. *
  615. **********************************************************************
  616. */
  617. static void
  618. Phase2(byte *RC4KEY, const byte *TK, const u16b *P1K, u16b IV16)
  619. {
  620. u16b *PPK; /* temporary key for mixing */
  621. int i;
  622. /*
  623. * Suggested implementation optimization: if PPK[] is "overlaid"
  624. * appropriately on RC4KEY[], there is no need for the final for
  625. * loop that copies the PPK[] result into RC4KEY[].
  626. */
  627. PPK = (u16b *)&RC4KEY[4];
  628. /* all adds in the PPK[] equations below are mod 2**16 */
  629. for (i = 0; i < 5; i++)
  630. PPK[i] = P1K[i]; /* first, copy P1K to PPK */
  631. PPK[5] = P1K[4] + IV16; /* next, add in IV16 */
  632. /* Bijective non-linear mixing of the 96 bits of PPK[0..5] */
  633. PPK[0] += _S_(PPK[5] ^ TK16(0)); /* Mix key in each "round" */
  634. PPK[1] += _S_(PPK[0] ^ TK16(1));
  635. PPK[2] += _S_(PPK[1] ^ TK16(2));
  636. PPK[3] += _S_(PPK[2] ^ TK16(3));
  637. PPK[4] += _S_(PPK[3] ^ TK16(4));
  638. PPK[5] += _S_(PPK[4] ^ TK16(5)); /* Total # S-box lookups == 6 */
  639. /* Final sweep: bijective, linear. Rotates kill LSB correlations */
  640. PPK[0] += RotR1(PPK[5] ^ TK16(6));
  641. PPK[1] += RotR1(PPK[0] ^ TK16(7)); /* Use all of TK[] in Phase2 */
  642. PPK[2] += RotR1(PPK[1]);
  643. PPK[3] += RotR1(PPK[2]);
  644. PPK[4] += RotR1(PPK[3]);
  645. PPK[5] += RotR1(PPK[4]);
  646. /* At this point, for a given key TK[0..15], the 96-bit output */
  647. /* value PPK[0..5] is guaranteed to be unique, as a function */
  648. /* of the 96-bit "input" value {TA,IV32,IV16}. That is, P1K */
  649. /* is now a keyed permutation of {TA,IV32,IV16}. */
  650. /* Set RC4KEY[0..3], which includes cleartext portion of RC4 key */
  651. RC4KEY[0] = Hi8(IV16); /* RC4KEY[0..2] is the WEP IV */
  652. RC4KEY[1] =(Hi8(IV16) | 0x20) & 0x7F; /* Help avoid FMS weak keys */
  653. RC4KEY[2] = Lo8(IV16);
  654. RC4KEY[3] = Lo8((PPK[5] ^ TK16(0)) >> 1);
  655. #if BYTE_ORDER == BIG_ENDIAN
  656. /* Copy 96 bits of PPK[0..5] to RC4KEY[4..15] (little-endian) */
  657. for (i = 0; i < 6; i++)
  658. PPK[i] = swap16(PPK[i]);
  659. #endif
  660. }