wlantest: Add support for decrypting WEP frames
[mech_eap.git] / wlantest / wep.c
1 /*
2  * Wired Equivalent Privacy (WEP)
3  * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "common/ieee802_11_defs.h"
19 #include "wlantest.h"
20
21
22 void wep_crypt(u8 *key, u8 *buf, size_t plen)
23 {
24         u32 i, j, k;
25         u8 S[256];
26 #define S_SWAP(a,b) do { u8 t = S[a]; S[a] = S[b]; S[b] = t; } while(0)
27         u8 *pos;
28
29         /* Setup RC4 state */
30         for (i = 0; i < 256; i++)
31                 S[i] = i;
32         j = 0;
33         for (i = 0; i < 256; i++) {
34                 j = (j + S[i] + key[i & 0x0f]) & 0xff;
35                 S_SWAP(i, j);
36         }
37
38         /* Apply RC4 to data */
39         pos = buf;
40         i = j = 0;
41         for (k = 0; k < plen; k++) {
42                 i = (i + 1) & 0xff;
43                 j = (j + S[i]) & 0xff;
44                 S_SWAP(i, j);
45                 *pos ^= S[(S[i] + S[j]) & 0xff];
46                 pos++;
47         }
48 }
49
50
51 static int try_wep(const u8 *key, size_t key_len, const u8 *data,
52                    size_t data_len, u8 *plain)
53 {
54         u32 icv, rx_icv;
55         u8 k[16];
56         int i, j;
57
58         for (i = 0, j = 0; i < sizeof(k); i++) {
59                 k[i] = key[j];
60                 j++;
61                 if (j >= key_len)
62                         j = 0;
63         }
64
65         os_memcpy(plain, data, data_len);
66         wep_crypt(k, plain, data_len);
67         icv = crc32(plain, data_len - 4);
68         rx_icv = WPA_GET_LE32(plain + data_len - 4);
69         if (icv != rx_icv)
70                 return -1;
71
72         return 0;
73 }
74
75
76 u8 * wep_decrypt(struct wlantest *wt, const struct ieee80211_hdr *hdr,
77                  const u8 *data, size_t data_len, size_t *decrypted_len)
78 {
79         u8 *plain;
80         struct wlantest_wep *w;
81         int found = 0;
82         u8 key[16];
83
84         if (dl_list_empty(&wt->wep))
85                 return NULL;
86
87         if (data_len < 4 + 4)
88                 return NULL;
89         plain = os_malloc(data_len - 4);
90         if (plain == NULL)
91                 return NULL;
92
93         dl_list_for_each(w, &wt->wep, struct wlantest_wep, list) {
94                 os_memcpy(key, data, 3);
95                 os_memcpy(key + 3, w->key, w->key_len);
96                 if (try_wep(key, 3 + w->key_len, data + 4, data_len - 4, plain)
97                     == 0) {
98                         found = 1;
99                         break;
100                 }
101         }
102         if (!found) {
103                 os_free(plain);
104                 return NULL;
105         }
106
107         *decrypted_len = data_len - 4 - 4;
108         return plain;
109 }