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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <gpxe/net80211.h>
  20. #include <gpxe/crypto.h>
  21. #include <gpxe/hmac.h>
  22. #include <gpxe/sha1.h>
  23. #include <gpxe/md5.h>
  24. #include <gpxe/crc32.h>
  25. #include <gpxe/arc4.h>
  26. #include <gpxe/wpa.h>
  27. #include <byteswap.h>
  28. #include <errno.h>
  29. /** @file
  30. *
  31. * Backend for WPA using the TKIP encryption standard.
  32. */
  33. /** Context for one direction of TKIP, either encryption or decryption */
  34. struct tkip_dir_ctx
  35. {
  36. /** High 32 bits of last sequence counter value used */
  37. u32 tsc_hi;
  38. /** Low 32 bits of last sequence counter value used */
  39. u16 tsc_lo;
  40. /** MAC address used to derive TTAK */
  41. u8 mac[ETH_ALEN];
  42. /** If TRUE, TTAK is valid */
  43. u16 ttak_ok;
  44. /** TKIP-mixed transmit address and key, depends on tsc_hi and MAC */
  45. u16 ttak[5];
  46. };
  47. /** Context for TKIP encryption and decryption */
  48. struct tkip_ctx
  49. {
  50. /** Temporal key to use */
  51. struct tkip_tk tk;
  52. /** State for encryption */
  53. struct tkip_dir_ctx enc;
  54. /** State for decryption */
  55. struct tkip_dir_ctx dec;
  56. };
  57. /** Header structure at the beginning of TKIP frame data */
  58. struct tkip_head
  59. {
  60. u8 tsc1; /**< High byte of low 16 bits of TSC */
  61. u8 seed1; /**< Second byte of WEP seed */
  62. u8 tsc0; /**< Low byte of TSC */
  63. u8 kid; /**< Key ID and ExtIV byte */
  64. u32 tsc_hi; /**< High 32 bits of TSC, as an ExtIV */
  65. } __attribute__ (( packed ));
  66. /** TKIP header overhead (IV + KID + ExtIV) */
  67. #define TKIP_HEAD_LEN 8
  68. /** TKIP trailer overhead (MIC + ICV) [assumes unfragmented] */
  69. #define TKIP_FOOT_LEN 12
  70. /** TKIP MIC length */
  71. #define TKIP_MIC_LEN 8
  72. /** TKIP ICV length */
  73. #define TKIP_ICV_LEN 4
  74. /** TKIP S-box */
  75. static const u16 Sbox[256] = {
  76. 0xC6A5, 0xF884, 0xEE99, 0xF68D, 0xFF0D, 0xD6BD, 0xDEB1, 0x9154,
  77. 0x6050, 0x0203, 0xCEA9, 0x567D, 0xE719, 0xB562, 0x4DE6, 0xEC9A,
  78. 0x8F45, 0x1F9D, 0x8940, 0xFA87, 0xEF15, 0xB2EB, 0x8EC9, 0xFB0B,
  79. 0x41EC, 0xB367, 0x5FFD, 0x45EA, 0x23BF, 0x53F7, 0xE496, 0x9B5B,
  80. 0x75C2, 0xE11C, 0x3DAE, 0x4C6A, 0x6C5A, 0x7E41, 0xF502, 0x834F,
  81. 0x685C, 0x51F4, 0xD134, 0xF908, 0xE293, 0xAB73, 0x6253, 0x2A3F,
  82. 0x080C, 0x9552, 0x4665, 0x9D5E, 0x3028, 0x37A1, 0x0A0F, 0x2FB5,
  83. 0x0E09, 0x2436, 0x1B9B, 0xDF3D, 0xCD26, 0x4E69, 0x7FCD, 0xEA9F,
  84. 0x121B, 0x1D9E, 0x5874, 0x342E, 0x362D, 0xDCB2, 0xB4EE, 0x5BFB,
  85. 0xA4F6, 0x764D, 0xB761, 0x7DCE, 0x527B, 0xDD3E, 0x5E71, 0x1397,
  86. 0xA6F5, 0xB968, 0x0000, 0xC12C, 0x4060, 0xE31F, 0x79C8, 0xB6ED,
  87. 0xD4BE, 0x8D46, 0x67D9, 0x724B, 0x94DE, 0x98D4, 0xB0E8, 0x854A,
  88. 0xBB6B, 0xC52A, 0x4FE5, 0xED16, 0x86C5, 0x9AD7, 0x6655, 0x1194,
  89. 0x8ACF, 0xE910, 0x0406, 0xFE81, 0xA0F0, 0x7844, 0x25BA, 0x4BE3,
  90. 0xA2F3, 0x5DFE, 0x80C0, 0x058A, 0x3FAD, 0x21BC, 0x7048, 0xF104,
  91. 0x63DF, 0x77C1, 0xAF75, 0x4263, 0x2030, 0xE51A, 0xFD0E, 0xBF6D,
  92. 0x814C, 0x1814, 0x2635, 0xC32F, 0xBEE1, 0x35A2, 0x88CC, 0x2E39,
  93. 0x9357, 0x55F2, 0xFC82, 0x7A47, 0xC8AC, 0xBAE7, 0x322B, 0xE695,
  94. 0xC0A0, 0x1998, 0x9ED1, 0xA37F, 0x4466, 0x547E, 0x3BAB, 0x0B83,
  95. 0x8CCA, 0xC729, 0x6BD3, 0x283C, 0xA779, 0xBCE2, 0x161D, 0xAD76,
  96. 0xDB3B, 0x6456, 0x744E, 0x141E, 0x92DB, 0x0C0A, 0x486C, 0xB8E4,
  97. 0x9F5D, 0xBD6E, 0x43EF, 0xC4A6, 0x39A8, 0x31A4, 0xD337, 0xF28B,
  98. 0xD532, 0x8B43, 0x6E59, 0xDAB7, 0x018C, 0xB164, 0x9CD2, 0x49E0,
  99. 0xD8B4, 0xACFA, 0xF307, 0xCF25, 0xCAAF, 0xF48E, 0x47E9, 0x1018,
  100. 0x6FD5, 0xF088, 0x4A6F, 0x5C72, 0x3824, 0x57F1, 0x73C7, 0x9751,
  101. 0xCB23, 0xA17C, 0xE89C, 0x3E21, 0x96DD, 0x61DC, 0x0D86, 0x0F85,
  102. 0xE090, 0x7C42, 0x71C4, 0xCCAA, 0x90D8, 0x0605, 0xF701, 0x1C12,
  103. 0xC2A3, 0x6A5F, 0xAEF9, 0x69D0, 0x1791, 0x9958, 0x3A27, 0x27B9,
  104. 0xD938, 0xEB13, 0x2BB3, 0x2233, 0xD2BB, 0xA970, 0x0789, 0x33A7,
  105. 0x2DB6, 0x3C22, 0x1592, 0xC920, 0x8749, 0xAAFF, 0x5078, 0xA57A,
  106. 0x038F, 0x59F8, 0x0980, 0x1A17, 0x65DA, 0xD731, 0x84C6, 0xD0B8,
  107. 0x82C3, 0x29B0, 0x5A77, 0x1E11, 0x7BCB, 0xA8FC, 0x6DD6, 0x2C3A,
  108. };
  109. /**
  110. * Perform S-box mapping on a 16-bit value
  111. *
  112. * @v v Value to perform S-box mapping on
  113. * @ret Sv S-box mapped value
  114. */
  115. static inline u16 S ( u16 v )
  116. {
  117. return Sbox[v & 0xFF] ^ swap16 ( Sbox[v >> 8] );
  118. }
  119. /**
  120. * Rotate 16-bit value right
  121. *
  122. * @v v Value to rotate
  123. * @v bits Number of bits to rotate by
  124. * @ret rotv Rotated value
  125. */
  126. static inline u16 ror16 ( u16 v, int bits )
  127. {
  128. return ( v >> bits ) | ( v << ( 16 - bits ) );
  129. }
  130. /**
  131. * Rotate 32-bit value right
  132. *
  133. * @v v Value to rotate
  134. * @v bits Number of bits to rotate by
  135. * @ret rotv Rotated value
  136. */
  137. static inline u32 ror32 ( u32 v, int bits )
  138. {
  139. return ( v >> bits ) | ( v << ( 32 - bits ) );
  140. }
  141. /**
  142. * Rotate 32-bit value left
  143. *
  144. * @v v Value to rotate
  145. * @v bits Number of bits to rotate by
  146. * @ret rotv Rotated value
  147. */
  148. static inline u32 rol32 ( u32 v, int bits )
  149. {
  150. return ( v << bits ) | ( v >> ( 32 - bits ) );
  151. }
  152. /**
  153. * Initialise TKIP state and install key
  154. *
  155. * @v crypto TKIP cryptosystem structure
  156. * @v key Pointer to tkip_tk to install
  157. * @v keylen Length of key (32 bytes)
  158. * @v rsc Initial receive sequence counter
  159. */
  160. static int tkip_init ( struct net80211_crypto *crypto, const void *key,
  161. int keylen, const void *rsc )
  162. {
  163. struct tkip_ctx *ctx = crypto->priv;
  164. const u8 *rscb = rsc;
  165. if ( keylen != sizeof ( ctx->tk ) )
  166. return -EINVAL;
  167. if ( rscb ) {
  168. ctx->dec.tsc_lo = ( rscb[1] << 8 ) | rscb[0];
  169. ctx->dec.tsc_hi = ( ( rscb[5] << 24 ) | ( rscb[4] << 16 ) |
  170. ( rscb[3] << 8 ) | rscb[2] );
  171. }
  172. memcpy ( &ctx->tk, key, sizeof ( ctx->tk ) );
  173. return 0;
  174. }
  175. /**
  176. * Perform TKIP key mixing, phase 1
  177. *
  178. * @v dctx TKIP directional context
  179. * @v tk TKIP temporal key
  180. * @v mac MAC address of transmitter
  181. *
  182. * This recomputes the TTAK in @a dctx if necessary, and sets
  183. * @c dctx->ttak_ok.
  184. */
  185. static void tkip_mix_1 ( struct tkip_dir_ctx *dctx, struct tkip_tk *tk, u8 *mac )
  186. {
  187. int i, j;
  188. if ( dctx->ttak_ok && ! memcmp ( mac, dctx->mac, ETH_ALEN ) )
  189. return;
  190. memcpy ( dctx->mac, mac, ETH_ALEN );
  191. dctx->ttak[0] = dctx->tsc_hi & 0xFFFF;
  192. dctx->ttak[1] = dctx->tsc_hi >> 16;
  193. dctx->ttak[2] = ( mac[1] << 8 ) | mac[0];
  194. dctx->ttak[3] = ( mac[3] << 8 ) | mac[2];
  195. dctx->ttak[4] = ( mac[5] << 8 ) | mac[4];
  196. for ( i = 0; i < 8; i++ ) {
  197. j = 2 * ( i & 1 );
  198. dctx->ttak[0] += S ( dctx->ttak[4] ^ ( ( tk->key[1 + j] << 8 ) |
  199. tk->key[0 + j] ) );
  200. dctx->ttak[1] += S ( dctx->ttak[0] ^ ( ( tk->key[5 + j] << 8 ) |
  201. tk->key[4 + j] ) );
  202. dctx->ttak[2] += S ( dctx->ttak[1] ^ ( ( tk->key[9 + j] << 8 ) |
  203. tk->key[8 + j] ) );
  204. dctx->ttak[3] += S ( dctx->ttak[2] ^ ( ( tk->key[13+ j] << 8 ) |
  205. tk->key[12+ j] ) );
  206. dctx->ttak[4] += S ( dctx->ttak[3] ^ ( ( tk->key[1 + j] << 8 ) |
  207. tk->key[0 + j] ) ) + i;
  208. }
  209. dctx->ttak_ok = 1;
  210. }
  211. /**
  212. * Perform TKIP key mixing, phase 2
  213. *
  214. * @v dctx TKIP directional context
  215. * @v tk TKIP temporal key
  216. * @ret key ARC4 key, 16 bytes long
  217. */
  218. static void tkip_mix_2 ( struct tkip_dir_ctx *dctx, struct tkip_tk *tk,
  219. void *key )
  220. {
  221. u8 *kb = key;
  222. u16 ppk[6];
  223. int i;
  224. memcpy ( ppk, dctx->ttak, sizeof ( dctx->ttak ) );
  225. ppk[5] = dctx->ttak[4] + dctx->tsc_lo;
  226. ppk[0] += S ( ppk[5] ^ ( ( tk->key[1] << 8 ) | tk->key[0] ) );
  227. ppk[1] += S ( ppk[0] ^ ( ( tk->key[3] << 8 ) | tk->key[2] ) );
  228. ppk[2] += S ( ppk[1] ^ ( ( tk->key[5] << 8 ) | tk->key[4] ) );
  229. ppk[3] += S ( ppk[2] ^ ( ( tk->key[7] << 8 ) | tk->key[6] ) );
  230. ppk[4] += S ( ppk[3] ^ ( ( tk->key[9] << 8 ) | tk->key[8] ) );
  231. ppk[5] += S ( ppk[4] ^ ( ( tk->key[11] << 8 ) | tk->key[10] ) );
  232. ppk[0] += ror16 ( ppk[5] ^ ( ( tk->key[13] << 8 ) | tk->key[12] ), 1 );
  233. ppk[1] += ror16 ( ppk[0] ^ ( ( tk->key[15] << 8 ) | tk->key[14] ), 1 );
  234. ppk[2] += ror16 ( ppk[1], 1 );
  235. ppk[3] += ror16 ( ppk[2], 1 );
  236. ppk[4] += ror16 ( ppk[3], 1 );
  237. ppk[5] += ror16 ( ppk[4], 1 );
  238. kb[0] = dctx->tsc_lo >> 8;
  239. kb[1] = ( ( dctx->tsc_lo >> 8 ) | 0x20 ) & 0x7F;
  240. kb[2] = dctx->tsc_lo & 0xFF;
  241. kb[3] = ( ( ppk[5] ^ ( ( tk->key[1] << 8 ) | tk->key[0] ) ) >> 1 )
  242. & 0xFF;
  243. for ( i = 0; i < 6; i++ ) {
  244. kb[4 + 2*i] = ppk[i] & 0xFF;
  245. kb[5 + 2*i] = ppk[i] >> 8;
  246. }
  247. }
  248. /**
  249. * Update Michael message integrity code based on next 32-bit word of data
  250. *
  251. * @v V Michael code state (two 32-bit words)
  252. * @v word Next 32-bit word of data
  253. */
  254. static void tkip_feed_michael ( u32 *V, u32 word )
  255. {
  256. V[0] ^= word;
  257. V[1] ^= rol32 ( V[0], 17 );
  258. V[0] += V[1];
  259. V[1] ^= ( ( V[0] & 0xFF00FF00 ) >> 8 ) | ( ( V[0] & 0x00FF00FF ) << 8 );
  260. V[0] += V[1];
  261. V[1] ^= rol32 ( V[0], 3 );
  262. V[0] += V[1];
  263. V[1] ^= ror32 ( V[0], 2 );
  264. V[0] += V[1];
  265. }
  266. /**
  267. * Calculate Michael message integrity code
  268. *
  269. * @v key MIC key to use (8 bytes)
  270. * @v da Destination link-layer address
  271. * @v sa Source link-layer address
  272. * @v data Start of data to calculate over
  273. * @v len Length of header + data
  274. * @ret mic Calculated Michael MIC (8 bytes)
  275. */
  276. static void tkip_michael ( const void *key, const void *da, const void *sa,
  277. const void *data, size_t len, void *mic )
  278. {
  279. u32 V[2]; /* V[0] = "l", V[1] = "r" in 802.11 */
  280. union {
  281. u8 byte[12];
  282. u32 word[3];
  283. } cap;
  284. const u8 *ptr = data;
  285. const u8 *end = ptr + len;
  286. int i;
  287. memcpy ( V, key, sizeof ( V ) );
  288. V[0] = le32_to_cpu ( V[0] );
  289. V[1] = le32_to_cpu ( V[1] );
  290. /* Feed in header (we assume non-QoS, so Priority = 0) */
  291. memcpy ( &cap.byte[0], da, ETH_ALEN );
  292. memcpy ( &cap.byte[6], sa, ETH_ALEN );
  293. tkip_feed_michael ( V, le32_to_cpu ( cap.word[0] ) );
  294. tkip_feed_michael ( V, le32_to_cpu ( cap.word[1] ) );
  295. tkip_feed_michael ( V, le32_to_cpu ( cap.word[2] ) );
  296. tkip_feed_michael ( V, 0 );
  297. /* Feed in data */
  298. while ( ptr + 4 <= end ) {
  299. tkip_feed_michael ( V, le32_to_cpu ( *( u32 * ) ptr ) );
  300. ptr += 4;
  301. }
  302. /* Add unaligned part and padding */
  303. for ( i = 0; ptr < end; i++ )
  304. cap.byte[i] = *ptr++;
  305. cap.byte[i++] = 0x5a;
  306. for ( ; i < 8; i++ )
  307. cap.byte[i] = 0;
  308. /* Feed in padding */
  309. tkip_feed_michael ( V, le32_to_cpu ( cap.word[0] ) );
  310. tkip_feed_michael ( V, le32_to_cpu ( cap.word[1] ) );
  311. /* Output MIC */
  312. V[0] = cpu_to_le32 ( V[0] );
  313. V[1] = cpu_to_le32 ( V[1] );
  314. memcpy ( mic, V, sizeof ( V ) );
  315. }
  316. /**
  317. * Encrypt a packet using TKIP
  318. *
  319. * @v crypto TKIP cryptosystem
  320. * @v iob I/O buffer containing cleartext packet
  321. * @ret eiob I/O buffer containing encrypted packet
  322. */
  323. static struct io_buffer * tkip_encrypt ( struct net80211_crypto *crypto,
  324. struct io_buffer *iob )
  325. {
  326. struct tkip_ctx *ctx = crypto->priv;
  327. struct ieee80211_frame *hdr = iob->data;
  328. struct io_buffer *eiob;
  329. struct arc4_ctx arc4;
  330. u8 key[16];
  331. struct tkip_head head;
  332. u8 mic[8];
  333. u32 icv;
  334. const int hdrlen = IEEE80211_TYP_FRAME_HEADER_LEN;
  335. int datalen = iob_len ( iob ) - hdrlen;
  336. ctx->enc.tsc_lo++;
  337. if ( ctx->enc.tsc_lo == 0 ) {
  338. ctx->enc.tsc_hi++;
  339. ctx->enc.ttak_ok = 0;
  340. }
  341. tkip_mix_1 ( &ctx->enc, &ctx->tk, hdr->addr2 );
  342. tkip_mix_2 ( &ctx->enc, &ctx->tk, key );
  343. eiob = alloc_iob ( iob_len ( iob ) + TKIP_HEAD_LEN + TKIP_FOOT_LEN );
  344. if ( ! eiob )
  345. return NULL;
  346. /* Copy frame header */
  347. memcpy ( iob_put ( eiob, hdrlen ), iob->data, hdrlen );
  348. hdr = eiob->data;
  349. hdr->fc |= IEEE80211_FC_PROTECTED;
  350. /* Fill in IV and key ID byte, and extended IV */
  351. memcpy ( &head, key, 3 );
  352. head.kid = 0x20; /* have Extended IV, key ID 0 */
  353. head.tsc_hi = cpu_to_le32 ( ctx->enc.tsc_hi );
  354. memcpy ( iob_put ( eiob, sizeof ( head ) ), &head, sizeof ( head ) );
  355. /* Copy and encrypt the data */
  356. cipher_setkey ( &arc4_algorithm, &arc4, key, 16 );
  357. cipher_encrypt ( &arc4_algorithm, &arc4, iob->data + hdrlen,
  358. iob_put ( eiob, datalen ), datalen );
  359. /* Add MIC */
  360. hdr = iob->data;
  361. tkip_michael ( &ctx->tk.mic.tx, hdr->addr3, hdr->addr2,
  362. iob->data + hdrlen, datalen, mic );
  363. cipher_encrypt ( &arc4_algorithm, &arc4, mic,
  364. iob_put ( eiob, sizeof ( mic ) ), sizeof ( mic ) );
  365. /* Add ICV */
  366. icv = crc32_le ( ~0, iob->data + hdrlen, datalen );
  367. icv = crc32_le ( icv, mic, sizeof ( mic ) );
  368. icv = cpu_to_le32 ( ~icv );
  369. cipher_encrypt ( &arc4_algorithm, &arc4, &icv,
  370. iob_put ( eiob, TKIP_ICV_LEN ), TKIP_ICV_LEN );
  371. DBGC2 ( ctx, "WPA-TKIP %p: encrypted packet %p -> %p\n", ctx,
  372. iob, eiob );
  373. return eiob;
  374. }
  375. /**
  376. * Decrypt a packet using TKIP
  377. *
  378. * @v crypto TKIP cryptosystem
  379. * @v eiob I/O buffer containing encrypted packet
  380. * @ret iob I/O buffer containing cleartext packet
  381. */
  382. static struct io_buffer * tkip_decrypt ( struct net80211_crypto *crypto,
  383. struct io_buffer *eiob )
  384. {
  385. struct tkip_ctx *ctx = crypto->priv;
  386. struct ieee80211_frame *hdr;
  387. struct io_buffer *iob;
  388. const int hdrlen = IEEE80211_TYP_FRAME_HEADER_LEN;
  389. int datalen = iob_len ( eiob ) - hdrlen - TKIP_HEAD_LEN - TKIP_FOOT_LEN;
  390. struct tkip_head *head;
  391. struct arc4_ctx arc4;
  392. u16 rx_tsc_lo;
  393. u8 key[16];
  394. u8 mic[8];
  395. u32 icv, crc;
  396. iob = alloc_iob ( hdrlen + datalen + TKIP_FOOT_LEN );
  397. if ( ! iob )
  398. return NULL;
  399. /* Copy frame header */
  400. memcpy ( iob_put ( iob, hdrlen ), eiob->data, hdrlen );
  401. hdr = iob->data;
  402. hdr->fc &= ~IEEE80211_FC_PROTECTED;
  403. /* Check and update TSC */
  404. head = eiob->data + hdrlen;
  405. rx_tsc_lo = ( head->tsc1 << 8 ) | head->tsc0;
  406. if ( head->tsc_hi < ctx->dec.tsc_hi ||
  407. ( head->tsc_hi == ctx->dec.tsc_hi &&
  408. rx_tsc_lo <= ctx->dec.tsc_lo ) ) {
  409. DBGC ( ctx, "WPA-TKIP %p: packet received out of order "
  410. "(%08x:%04x <= %08x:%04x)\n", ctx, head->tsc_hi,
  411. rx_tsc_lo, ctx->dec.tsc_hi, ctx->dec.tsc_lo );
  412. free_iob ( iob );
  413. return NULL;
  414. }
  415. ctx->dec.tsc_lo = rx_tsc_lo;
  416. if ( ctx->dec.tsc_hi != head->tsc_hi ) {
  417. ctx->dec.ttak_ok = 0;
  418. ctx->dec.tsc_hi = head->tsc_hi;
  419. }
  420. /* Calculate key */
  421. tkip_mix_1 ( &ctx->dec, &ctx->tk, hdr->addr2 );
  422. tkip_mix_2 ( &ctx->dec, &ctx->tk, key );
  423. /* Copy-decrypt data, MIC, ICV */
  424. cipher_setkey ( &arc4_algorithm, &arc4, key, 16 );
  425. cipher_decrypt ( &arc4_algorithm, &arc4,
  426. eiob->data + hdrlen + TKIP_HEAD_LEN,
  427. iob_put ( iob, datalen ), datalen + TKIP_FOOT_LEN );
  428. /* Check ICV */
  429. icv = le32_to_cpu ( *( u32 * ) ( iob->tail + TKIP_MIC_LEN ) );
  430. crc = ~crc32_le ( ~0, iob->data + hdrlen, datalen + TKIP_MIC_LEN );
  431. if ( crc != icv ) {
  432. DBGC ( ctx, "WPA-TKIP %p CRC mismatch: expect %08x, get %08x\n",
  433. ctx, icv, crc );
  434. free_iob ( iob );
  435. return NULL;
  436. }
  437. /* Check MIC */
  438. tkip_michael ( &ctx->tk.mic.rx, hdr->addr1, hdr->addr3,
  439. iob->data + hdrlen, datalen, mic );
  440. if ( memcmp ( mic, iob->tail, TKIP_MIC_LEN ) != 0 ) {
  441. DBGC ( ctx, "WPA-TKIP %p ALERT! MIC failure\n", ctx );
  442. /* XXX we should do the countermeasures here */
  443. free_iob ( iob );
  444. return NULL;
  445. }
  446. DBGC2 ( ctx, "WPA-TKIP %p: decrypted packet %p -> %p\n", ctx,
  447. eiob, iob );
  448. return iob;
  449. }
  450. /** TKIP cryptosystem */
  451. struct net80211_crypto tkip_crypto __net80211_crypto = {
  452. .algorithm = NET80211_CRYPT_TKIP,
  453. .init = tkip_init,
  454. .encrypt = tkip_encrypt,
  455. .decrypt = tkip_decrypt,
  456. .priv_len = sizeof ( struct tkip_ctx ),
  457. };
  458. /**
  459. * Calculate HMAC-MD5 MIC for EAPOL-Key frame
  460. *
  461. * @v kck Key Confirmation Key, 16 bytes
  462. * @v msg Message to calculate MIC over
  463. * @v len Number of bytes to calculate MIC over
  464. * @ret mic Calculated MIC, 16 bytes long
  465. */
  466. static void tkip_kie_mic ( const void *kck, const void *msg, size_t len,
  467. void *mic )
  468. {
  469. struct md5_ctx md5;
  470. u8 kckb[16];
  471. size_t kck_len = 16;
  472. memcpy ( kckb, kck, kck_len );
  473. hmac_init ( &md5_algorithm, &md5, kckb, &kck_len );
  474. hmac_update ( &md5_algorithm, &md5, msg, len );
  475. hmac_final ( &md5_algorithm, &md5, kckb, &kck_len, mic );
  476. }
  477. /**
  478. * Decrypt key data in EAPOL-Key frame
  479. *
  480. * @v kek Key Encryption Key, 16 bytes
  481. * @v iv Initialisation vector, 16 bytes
  482. * @v msg Message to decrypt
  483. * @v len Length of message
  484. * @ret msg Decrypted message in place of original
  485. * @ret len Unchanged
  486. * @ret rc Always 0 for success
  487. */
  488. static int tkip_kie_decrypt ( const void *kek, const void *iv,
  489. void *msg, u16 *len )
  490. {
  491. u8 key[32];
  492. memcpy ( key, iv, 16 );
  493. memcpy ( key + 16, kek, 16 );
  494. arc4_skip ( key, 32, 256, msg, msg, *len );
  495. return 0;
  496. }
  497. /** TKIP-style key integrity and encryption handler */
  498. struct wpa_kie tkip_kie __wpa_kie = {
  499. .version = EAPOL_KEY_VERSION_WPA,
  500. .mic = tkip_kie_mic,
  501. .decrypt = tkip_kie_decrypt,
  502. };