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