tkip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Temporal Key Integrity Protocol (CCMP)
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/crc32.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "wlantest.h"
  13. void wep_crypt(u8 *key, u8 *buf, size_t plen);
  14. static inline u16 RotR1(u16 val)
  15. {
  16. return (val >> 1) | (val << 15);
  17. }
  18. static inline u8 Lo8(u16 val)
  19. {
  20. return val & 0xff;
  21. }
  22. static inline u8 Hi8(u16 val)
  23. {
  24. return val >> 8;
  25. }
  26. static inline u16 Lo16(u32 val)
  27. {
  28. return val & 0xffff;
  29. }
  30. static inline u16 Hi16(u32 val)
  31. {
  32. return val >> 16;
  33. }
  34. static inline u16 Mk16(u8 hi, u8 lo)
  35. {
  36. return lo | (((u16) hi) << 8);
  37. }
  38. static inline u16 Mk16_le(u16 *v)
  39. {
  40. return le_to_host16(*v);
  41. }
  42. static const u16 Sbox[256] =
  43. {
  44. 0xC6A5, 0xF884, 0xEE99, 0xF68D, 0xFF0D, 0xD6BD, 0xDEB1, 0x9154,
  45. 0x6050, 0x0203, 0xCEA9, 0x567D, 0xE719, 0xB562, 0x4DE6, 0xEC9A,
  46. 0x8F45, 0x1F9D, 0x8940, 0xFA87, 0xEF15, 0xB2EB, 0x8EC9, 0xFB0B,
  47. 0x41EC, 0xB367, 0x5FFD, 0x45EA, 0x23BF, 0x53F7, 0xE496, 0x9B5B,
  48. 0x75C2, 0xE11C, 0x3DAE, 0x4C6A, 0x6C5A, 0x7E41, 0xF502, 0x834F,
  49. 0x685C, 0x51F4, 0xD134, 0xF908, 0xE293, 0xAB73, 0x6253, 0x2A3F,
  50. 0x080C, 0x9552, 0x4665, 0x9D5E, 0x3028, 0x37A1, 0x0A0F, 0x2FB5,
  51. 0x0E09, 0x2436, 0x1B9B, 0xDF3D, 0xCD26, 0x4E69, 0x7FCD, 0xEA9F,
  52. 0x121B, 0x1D9E, 0x5874, 0x342E, 0x362D, 0xDCB2, 0xB4EE, 0x5BFB,
  53. 0xA4F6, 0x764D, 0xB761, 0x7DCE, 0x527B, 0xDD3E, 0x5E71, 0x1397,
  54. 0xA6F5, 0xB968, 0x0000, 0xC12C, 0x4060, 0xE31F, 0x79C8, 0xB6ED,
  55. 0xD4BE, 0x8D46, 0x67D9, 0x724B, 0x94DE, 0x98D4, 0xB0E8, 0x854A,
  56. 0xBB6B, 0xC52A, 0x4FE5, 0xED16, 0x86C5, 0x9AD7, 0x6655, 0x1194,
  57. 0x8ACF, 0xE910, 0x0406, 0xFE81, 0xA0F0, 0x7844, 0x25BA, 0x4BE3,
  58. 0xA2F3, 0x5DFE, 0x80C0, 0x058A, 0x3FAD, 0x21BC, 0x7048, 0xF104,
  59. 0x63DF, 0x77C1, 0xAF75, 0x4263, 0x2030, 0xE51A, 0xFD0E, 0xBF6D,
  60. 0x814C, 0x1814, 0x2635, 0xC32F, 0xBEE1, 0x35A2, 0x88CC, 0x2E39,
  61. 0x9357, 0x55F2, 0xFC82, 0x7A47, 0xC8AC, 0xBAE7, 0x322B, 0xE695,
  62. 0xC0A0, 0x1998, 0x9ED1, 0xA37F, 0x4466, 0x547E, 0x3BAB, 0x0B83,
  63. 0x8CCA, 0xC729, 0x6BD3, 0x283C, 0xA779, 0xBCE2, 0x161D, 0xAD76,
  64. 0xDB3B, 0x6456, 0x744E, 0x141E, 0x92DB, 0x0C0A, 0x486C, 0xB8E4,
  65. 0x9F5D, 0xBD6E, 0x43EF, 0xC4A6, 0x39A8, 0x31A4, 0xD337, 0xF28B,
  66. 0xD532, 0x8B43, 0x6E59, 0xDAB7, 0x018C, 0xB164, 0x9CD2, 0x49E0,
  67. 0xD8B4, 0xACFA, 0xF307, 0xCF25, 0xCAAF, 0xF48E, 0x47E9, 0x1018,
  68. 0x6FD5, 0xF088, 0x4A6F, 0x5C72, 0x3824, 0x57F1, 0x73C7, 0x9751,
  69. 0xCB23, 0xA17C, 0xE89C, 0x3E21, 0x96DD, 0x61DC, 0x0D86, 0x0F85,
  70. 0xE090, 0x7C42, 0x71C4, 0xCCAA, 0x90D8, 0x0605, 0xF701, 0x1C12,
  71. 0xC2A3, 0x6A5F, 0xAEF9, 0x69D0, 0x1791, 0x9958, 0x3A27, 0x27B9,
  72. 0xD938, 0xEB13, 0x2BB3, 0x2233, 0xD2BB, 0xA970, 0x0789, 0x33A7,
  73. 0x2DB6, 0x3C22, 0x1592, 0xC920, 0x8749, 0xAAFF, 0x5078, 0xA57A,
  74. 0x038F, 0x59F8, 0x0980, 0x1A17, 0x65DA, 0xD731, 0x84C6, 0xD0B8,
  75. 0x82C3, 0x29B0, 0x5A77, 0x1E11, 0x7BCB, 0xA8FC, 0x6DD6, 0x2C3A,
  76. };
  77. static inline u16 _S_(u16 v)
  78. {
  79. u16 t = Sbox[Hi8(v)];
  80. return Sbox[Lo8(v)] ^ ((t << 8) | (t >> 8));
  81. }
  82. #define PHASE1_LOOP_COUNT 8
  83. static void tkip_mixing_phase1(u16 *TTAK, const u8 *TK, const u8 *TA, u32 IV32)
  84. {
  85. int i, j;
  86. /* Initialize the 80-bit TTAK from TSC (IV32) and TA[0..5] */
  87. TTAK[0] = Lo16(IV32);
  88. TTAK[1] = Hi16(IV32);
  89. TTAK[2] = Mk16(TA[1], TA[0]);
  90. TTAK[3] = Mk16(TA[3], TA[2]);
  91. TTAK[4] = Mk16(TA[5], TA[4]);
  92. for (i = 0; i < PHASE1_LOOP_COUNT; i++) {
  93. j = 2 * (i & 1);
  94. TTAK[0] += _S_(TTAK[4] ^ Mk16(TK[1 + j], TK[0 + j]));
  95. TTAK[1] += _S_(TTAK[0] ^ Mk16(TK[5 + j], TK[4 + j]));
  96. TTAK[2] += _S_(TTAK[1] ^ Mk16(TK[9 + j], TK[8 + j]));
  97. TTAK[3] += _S_(TTAK[2] ^ Mk16(TK[13 + j], TK[12 + j]));
  98. TTAK[4] += _S_(TTAK[3] ^ Mk16(TK[1 + j], TK[0 + j])) + i;
  99. }
  100. }
  101. static void tkip_mixing_phase2(u8 *WEPSeed, const u8 *TK, const u16 *TTAK,
  102. u16 IV16)
  103. {
  104. u16 PPK[6];
  105. /* Step 1 - make copy of TTAK and bring in TSC */
  106. PPK[0] = TTAK[0];
  107. PPK[1] = TTAK[1];
  108. PPK[2] = TTAK[2];
  109. PPK[3] = TTAK[3];
  110. PPK[4] = TTAK[4];
  111. PPK[5] = TTAK[4] + IV16;
  112. /* Step 2 - 96-bit bijective mixing using S-box */
  113. PPK[0] += _S_(PPK[5] ^ Mk16_le((u16 *) &TK[0]));
  114. PPK[1] += _S_(PPK[0] ^ Mk16_le((u16 *) &TK[2]));
  115. PPK[2] += _S_(PPK[1] ^ Mk16_le((u16 *) &TK[4]));
  116. PPK[3] += _S_(PPK[2] ^ Mk16_le((u16 *) &TK[6]));
  117. PPK[4] += _S_(PPK[3] ^ Mk16_le((u16 *) &TK[8]));
  118. PPK[5] += _S_(PPK[4] ^ Mk16_le((u16 *) &TK[10]));
  119. PPK[0] += RotR1(PPK[5] ^ Mk16_le((u16 *) &TK[12]));
  120. PPK[1] += RotR1(PPK[0] ^ Mk16_le((u16 *) &TK[14]));
  121. PPK[2] += RotR1(PPK[1]);
  122. PPK[3] += RotR1(PPK[2]);
  123. PPK[4] += RotR1(PPK[3]);
  124. PPK[5] += RotR1(PPK[4]);
  125. /* Step 3 - bring in last of TK bits, assign 24-bit WEP IV value
  126. * WEPSeed[0..2] is transmitted as WEP IV */
  127. WEPSeed[0] = Hi8(IV16);
  128. WEPSeed[1] = (Hi8(IV16) | 0x20) & 0x7F;
  129. WEPSeed[2] = Lo8(IV16);
  130. WEPSeed[3] = Lo8((PPK[5] ^ Mk16_le((u16 *) &TK[0])) >> 1);
  131. WPA_PUT_LE16(&WEPSeed[4], PPK[0]);
  132. WPA_PUT_LE16(&WEPSeed[6], PPK[1]);
  133. WPA_PUT_LE16(&WEPSeed[8], PPK[2]);
  134. WPA_PUT_LE16(&WEPSeed[10], PPK[3]);
  135. WPA_PUT_LE16(&WEPSeed[12], PPK[4]);
  136. WPA_PUT_LE16(&WEPSeed[14], PPK[5]);
  137. }
  138. static inline u32 rotl(u32 val, int bits)
  139. {
  140. return (val << bits) | (val >> (32 - bits));
  141. }
  142. static inline u32 rotr(u32 val, int bits)
  143. {
  144. return (val >> bits) | (val << (32 - bits));
  145. }
  146. static inline u32 xswap(u32 val)
  147. {
  148. return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);
  149. }
  150. #define michael_block(l, r) \
  151. do { \
  152. r ^= rotl(l, 17); \
  153. l += r; \
  154. r ^= xswap(l); \
  155. l += r; \
  156. r ^= rotl(l, 3); \
  157. l += r; \
  158. r ^= rotr(l, 2); \
  159. l += r; \
  160. } while (0)
  161. static void michael_mic(const u8 *key, const u8 *hdr, const u8 *data,
  162. size_t data_len, u8 *mic)
  163. {
  164. u32 l, r;
  165. int i, blocks, last;
  166. l = WPA_GET_LE32(key);
  167. r = WPA_GET_LE32(key + 4);
  168. /* Michael MIC pseudo header: DA, SA, 3 x 0, Priority */
  169. l ^= WPA_GET_LE32(hdr);
  170. michael_block(l, r);
  171. l ^= WPA_GET_LE32(&hdr[4]);
  172. michael_block(l, r);
  173. l ^= WPA_GET_LE32(&hdr[8]);
  174. michael_block(l, r);
  175. l ^= WPA_GET_LE32(&hdr[12]);
  176. michael_block(l, r);
  177. /* 32-bit blocks of data */
  178. blocks = data_len / 4;
  179. last = data_len % 4;
  180. for (i = 0; i < blocks; i++) {
  181. l ^= WPA_GET_LE32(&data[4 * i]);
  182. michael_block(l, r);
  183. }
  184. /* Last block and padding (0x5a, 4..7 x 0) */
  185. switch (last) {
  186. case 0:
  187. l ^= 0x5a;
  188. break;
  189. case 1:
  190. l ^= data[4 * i] | 0x5a00;
  191. break;
  192. case 2:
  193. l ^= data[4 * i] | (data[4 * i + 1] << 8) | 0x5a0000;
  194. break;
  195. case 3:
  196. l ^= data[4 * i] | (data[4 * i + 1] << 8) |
  197. (data[4 * i + 2] << 16) | 0x5a000000;
  198. break;
  199. }
  200. michael_block(l, r);
  201. /* l ^= 0; */
  202. michael_block(l, r);
  203. WPA_PUT_LE32(mic, l);
  204. WPA_PUT_LE32(mic + 4, r);
  205. }
  206. static void michael_mic_hdr(const struct ieee80211_hdr *hdr11, u8 *hdr)
  207. {
  208. int hdrlen = 24;
  209. u16 fc = le_to_host16(hdr11->frame_control);
  210. switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
  211. case WLAN_FC_TODS:
  212. os_memcpy(hdr, hdr11->addr3, ETH_ALEN); /* DA */
  213. os_memcpy(hdr + ETH_ALEN, hdr11->addr2, ETH_ALEN); /* SA */
  214. break;
  215. case WLAN_FC_FROMDS:
  216. os_memcpy(hdr, hdr11->addr1, ETH_ALEN); /* DA */
  217. os_memcpy(hdr + ETH_ALEN, hdr11->addr3, ETH_ALEN); /* SA */
  218. break;
  219. case WLAN_FC_FROMDS | WLAN_FC_TODS:
  220. os_memcpy(hdr, hdr11->addr3, ETH_ALEN); /* DA */
  221. os_memcpy(hdr + ETH_ALEN, hdr11 + 1, ETH_ALEN); /* SA */
  222. hdrlen += ETH_ALEN;
  223. break;
  224. case 0:
  225. os_memcpy(hdr, hdr11->addr1, ETH_ALEN); /* DA */
  226. os_memcpy(hdr + ETH_ALEN, hdr11->addr2, ETH_ALEN); /* SA */
  227. break;
  228. }
  229. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
  230. (WLAN_FC_GET_STYPE(fc) & 0x08)) {
  231. const u8 *qos = ((const u8 *) hdr11) + hdrlen;
  232. hdr[12] = qos[0] & 0x0f; /* priority */
  233. } else
  234. hdr[12] = 0; /* priority */
  235. hdr[13] = hdr[14] = hdr[15] = 0; /* reserved */
  236. }
  237. u8 * tkip_decrypt(const u8 *tk, const struct ieee80211_hdr *hdr,
  238. const u8 *data, size_t data_len, size_t *decrypted_len)
  239. {
  240. u16 iv16;
  241. u32 iv32;
  242. u16 ttak[5];
  243. u8 rc4key[16];
  244. u8 *plain;
  245. size_t plain_len;
  246. u32 icv, rx_icv;
  247. const u8 *mic_key;
  248. u8 michael_hdr[16];
  249. u8 mic[8];
  250. u16 fc = le_to_host16(hdr->frame_control);
  251. if (data_len < 8 + 4)
  252. return NULL;
  253. iv16 = (data[0] << 8) | data[2];
  254. iv32 = WPA_GET_LE32(&data[4]);
  255. wpa_printf(MSG_EXCESSIVE, "TKIP decrypt: iv32=%08x iv16=%04x",
  256. iv32, iv16);
  257. tkip_mixing_phase1(ttak, tk, hdr->addr2, iv32);
  258. wpa_hexdump(MSG_EXCESSIVE, "TKIP TTAK", (u8 *) ttak, sizeof(ttak));
  259. tkip_mixing_phase2(rc4key, tk, ttak, iv16);
  260. wpa_hexdump(MSG_EXCESSIVE, "TKIP RC4KEY", rc4key, sizeof(rc4key));
  261. plain_len = data_len - 8;
  262. plain = os_memdup(data + 8, plain_len);
  263. if (plain == NULL)
  264. return NULL;
  265. wep_crypt(rc4key, plain, plain_len);
  266. icv = crc32(plain, plain_len - 4);
  267. rx_icv = WPA_GET_LE32(plain + plain_len - 4);
  268. if (icv != rx_icv) {
  269. wpa_printf(MSG_INFO, "TKIP ICV mismatch in frame from " MACSTR,
  270. MAC2STR(hdr->addr2));
  271. wpa_printf(MSG_DEBUG, "TKIP calculated ICV %08x received ICV "
  272. "%08x", icv, rx_icv);
  273. os_free(plain);
  274. return NULL;
  275. }
  276. plain_len -= 4;
  277. /* TODO: MSDU reassembly */
  278. if (plain_len < 8) {
  279. wpa_printf(MSG_INFO, "TKIP: Not enough room for Michael MIC "
  280. "in a frame from " MACSTR, MAC2STR(hdr->addr2));
  281. os_free(plain);
  282. return NULL;
  283. }
  284. michael_mic_hdr(hdr, michael_hdr);
  285. mic_key = tk + ((fc & WLAN_FC_FROMDS) ? 16 : 24);
  286. michael_mic(mic_key, michael_hdr, plain, plain_len - 8, mic);
  287. if (os_memcmp(mic, plain + plain_len - 8, 8) != 0) {
  288. wpa_printf(MSG_INFO, "TKIP: Michael MIC mismatch in a frame "
  289. "from " MACSTR, MAC2STR(hdr->addr2));
  290. wpa_hexdump(MSG_DEBUG, "TKIP: Calculated MIC", mic, 8);
  291. wpa_hexdump(MSG_DEBUG, "TKIP: Received MIC",
  292. plain + plain_len - 8, 8);
  293. os_free(plain);
  294. return NULL;
  295. }
  296. *decrypted_len = plain_len - 8;
  297. return plain;
  298. }
  299. void tkip_get_pn(u8 *pn, const u8 *data)
  300. {
  301. pn[0] = data[7]; /* PN5 */
  302. pn[1] = data[6]; /* PN4 */
  303. pn[2] = data[5]; /* PN3 */
  304. pn[3] = data[4]; /* PN2 */
  305. pn[4] = data[0]; /* PN1 */
  306. pn[5] = data[2]; /* PN0 */
  307. }
  308. u8 * tkip_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen, u8 *qos,
  309. u8 *pn, int keyid, size_t *encrypted_len)
  310. {
  311. u8 michael_hdr[16];
  312. u8 mic[8];
  313. struct ieee80211_hdr *hdr;
  314. u16 fc;
  315. const u8 *mic_key;
  316. u8 *crypt, *pos;
  317. u16 iv16;
  318. u32 iv32;
  319. u16 ttak[5];
  320. u8 rc4key[16];
  321. if (len < sizeof(*hdr) || len < hdrlen)
  322. return NULL;
  323. hdr = (struct ieee80211_hdr *) frame;
  324. fc = le_to_host16(hdr->frame_control);
  325. michael_mic_hdr(hdr, michael_hdr);
  326. mic_key = tk + ((fc & WLAN_FC_FROMDS) ? 16 : 24);
  327. michael_mic(mic_key, michael_hdr, frame + hdrlen, len - hdrlen, mic);
  328. wpa_hexdump(MSG_EXCESSIVE, "TKIP: MIC", mic, sizeof(mic));
  329. iv32 = WPA_GET_BE32(pn);
  330. iv16 = WPA_GET_BE16(pn + 4);
  331. tkip_mixing_phase1(ttak, tk, hdr->addr2, iv32);
  332. wpa_hexdump(MSG_EXCESSIVE, "TKIP TTAK", (u8 *) ttak, sizeof(ttak));
  333. tkip_mixing_phase2(rc4key, tk, ttak, iv16);
  334. wpa_hexdump(MSG_EXCESSIVE, "TKIP RC4KEY", rc4key, sizeof(rc4key));
  335. crypt = os_malloc(len + 8 + sizeof(mic) + 4);
  336. if (crypt == NULL)
  337. return NULL;
  338. os_memcpy(crypt, frame, hdrlen);
  339. pos = crypt + hdrlen;
  340. os_memcpy(pos, rc4key, 3);
  341. pos += 3;
  342. *pos++ = keyid << 6 | BIT(5);
  343. *pos++ = pn[3];
  344. *pos++ = pn[2];
  345. *pos++ = pn[1];
  346. *pos++ = pn[0];
  347. os_memcpy(pos, frame + hdrlen, len - hdrlen);
  348. os_memcpy(pos + len - hdrlen, mic, sizeof(mic));
  349. WPA_PUT_LE32(pos + len - hdrlen + sizeof(mic),
  350. crc32(pos, len - hdrlen + sizeof(mic)));
  351. wep_crypt(rc4key, pos, len - hdrlen + sizeof(mic) + 4);
  352. *encrypted_len = len + 8 + sizeof(mic) + 4;
  353. return crypt;
  354. }