1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52#include <linux/crc32.h>
53#include <linux/netdevice.h>
54#include <linux/wireless.h>
55#include <linux/random.h>
56#include <linux/kernel.h>
57#include "p80211hdr.h"
58#include "p80211types.h"
59#include "p80211msg.h"
60#include "p80211conv.h"
61#include "p80211netdev.h"
62
63#define WEP_KEY(x) (((x) & 0xC0) >> 6)
64
65
66
67int wep_change_key(struct wlandevice *wlandev, int keynum, u8 *key, int keylen)
68{
69 if (keylen < 0)
70 return -1;
71 if (keylen >= MAX_KEYLEN)
72 return -1;
73 if (!key)
74 return -1;
75 if (keynum < 0)
76 return -1;
77 if (keynum >= NUM_WEPKEYS)
78 return -1;
79
80 wlandev->wep_keylens[keynum] = keylen;
81 memcpy(wlandev->wep_keys[keynum], key, keylen);
82
83 return 0;
84}
85
86
87
88
89
90int wep_decrypt(struct wlandevice *wlandev, u8 *buf, u32 len, int key_override,
91 u8 *iv, u8 *icv)
92{
93 u32 i, j, k, crc, keylen;
94 u8 s[256], key[64], c_crc[4];
95 u8 keyidx;
96
97
98 if (len <= 0)
99 return -1;
100
101
102 key[0] = iv[0];
103 key[1] = iv[1];
104 key[2] = iv[2];
105 keyidx = WEP_KEY(iv[3]);
106
107 if (key_override >= 0)
108 keyidx = key_override;
109
110 if (keyidx >= NUM_WEPKEYS)
111 return -2;
112
113 keylen = wlandev->wep_keylens[keyidx];
114
115 if (keylen == 0)
116 return -3;
117
118
119 memcpy(key + 3, wlandev->wep_keys[keyidx], keylen);
120
121 keylen += 3;
122
123
124 for (i = 0; i < 256; i++)
125 s[i] = i;
126 j = 0;
127 for (i = 0; i < 256; i++) {
128 j = (j + s[i] + key[i % keylen]) & 0xff;
129 swap(i, j);
130 }
131
132
133 i = 0;
134 j = 0;
135 for (k = 0; k < len; k++) {
136 i = (i + 1) & 0xff;
137 j = (j + s[i]) & 0xff;
138 swap(i, j);
139 buf[k] ^= s[(s[i] + s[j]) & 0xff];
140 }
141 crc = ~crc32_le(~0, buf, len);
142
143
144 c_crc[0] = crc;
145 c_crc[1] = crc >> 8;
146 c_crc[2] = crc >> 16;
147 c_crc[3] = crc >> 24;
148
149 for (k = 0; k < 4; k++) {
150 i = (i + 1) & 0xff;
151 j = (j + s[i]) & 0xff;
152 swap(i, j);
153 if ((c_crc[k] ^ s[(s[i] + s[j]) & 0xff]) != icv[k])
154 return -(4 | (k << 4));
155 }
156
157 return 0;
158}
159
160
161int wep_encrypt(struct wlandevice *wlandev, u8 *buf,
162 u8 *dst, u32 len, int keynum, u8 *iv, u8 *icv)
163{
164 u32 i, j, k, crc, keylen;
165 u8 s[256], key[64];
166
167
168 if (len <= 0)
169 return -1;
170
171
172 if (keynum >= NUM_WEPKEYS)
173 return -2;
174 keylen = wlandev->wep_keylens[keynum];
175 if (keylen <= 0)
176 return -3;
177
178
179 get_random_bytes(iv, 3);
180 while ((iv[1] == 0xff) && (iv[0] >= 3) && (iv[0] < keylen))
181 get_random_bytes(iv, 3);
182
183 iv[3] = (keynum & 0x03) << 6;
184
185 key[0] = iv[0];
186 key[1] = iv[1];
187 key[2] = iv[2];
188
189
190 memcpy(key + 3, wlandev->wep_keys[keynum], keylen);
191
192 keylen += 3;
193
194
195 for (i = 0; i < 256; i++)
196 s[i] = i;
197 j = 0;
198 for (i = 0; i < 256; i++) {
199 j = (j + s[i] + key[i % keylen]) & 0xff;
200 swap(i, j);
201 }
202
203
204 i = 0;
205 j = 0;
206 for (k = 0; k < len; k++) {
207 i = (i + 1) & 0xff;
208 j = (j + s[i]) & 0xff;
209 swap(i, j);
210 dst[k] = buf[k] ^ s[(s[i] + s[j]) & 0xff];
211 }
212 crc = ~crc32_le(~0, buf, len);
213
214
215 icv[0] = crc;
216 icv[1] = crc >> 8;
217 icv[2] = crc >> 16;
218 icv[3] = crc >> 24;
219
220 for (k = 0; k < 4; k++) {
221 i = (i + 1) & 0xff;
222 j = (j + s[i]) & 0xff;
223 swap(i, j);
224 icv[k] ^= s[(s[i] + s[j]) & 0xff];
225 }
226
227 return 0;
228}
229