dee8eaec0fdb408a8fcf69c203d5fd9775f75172
[openwrt/staging/florian.git] / package / d80211 / src / wep.c
1 /*
2 * Software WEP encryption implementation
3 * Copyright 2002, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2003, Instant802 Networks, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11 #include <linux/netdevice.h>
12 #include <linux/types.h>
13 #include <linux/random.h>
14 #include <linux/compiler.h>
15 #include <linux/crc32.h>
16 #include <linux/crypto.h>
17 #include <linux/err.h>
18 #include <asm/scatterlist.h>
19
20 #include <net/d80211.h>
21 #include "ieee80211_i.h"
22 #include "wep.h"
23
24
25 int ieee80211_wep_init(struct ieee80211_local *local)
26 {
27 /* start WEP IV from a random value */
28 get_random_bytes(&local->wep_iv, WEP_IV_LEN);
29
30 local->wep_tx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0,
31 CRYPTO_ALG_ASYNC);
32 if (IS_ERR(local->wep_tx_tfm))
33 return -ENOMEM;
34
35 local->wep_rx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0,
36 CRYPTO_ALG_ASYNC);
37 if (IS_ERR(local->wep_rx_tfm)) {
38 crypto_free_blkcipher(local->wep_tx_tfm);
39 return -ENOMEM;
40 }
41
42 return 0;
43 }
44
45 void ieee80211_wep_free(struct ieee80211_local *local)
46 {
47 crypto_free_blkcipher(local->wep_tx_tfm);
48 crypto_free_blkcipher(local->wep_rx_tfm);
49 }
50
51 static inline int ieee80211_wep_weak_iv(u32 iv, int keylen)
52 {
53 /* Fluhrer, Mantin, and Shamir have reported weaknesses in the
54 * key scheduling algorithm of RC4. At least IVs (KeyByte + 3,
55 * 0xff, N) can be used to speedup attacks, so avoid using them. */
56 if ((iv & 0xff00) == 0xff00) {
57 u8 B = (iv >> 16) & 0xff;
58 if (B >= 3 && B < 3 + keylen)
59 return 1;
60 }
61 return 0;
62 }
63
64
65 void ieee80211_wep_get_iv(struct ieee80211_local *local,
66 struct ieee80211_key *key, u8 *iv)
67 {
68 local->wep_iv++;
69 if (ieee80211_wep_weak_iv(local->wep_iv, key->keylen))
70 local->wep_iv += 0x0100;
71
72 if (!iv)
73 return;
74
75 *iv++ = (local->wep_iv >> 16) & 0xff;
76 *iv++ = (local->wep_iv >> 8) & 0xff;
77 *iv++ = local->wep_iv & 0xff;
78 *iv++ = key->keyidx << 6;
79 }
80
81
82 u8 * ieee80211_wep_add_iv(struct ieee80211_local *local,
83 struct sk_buff *skb,
84 struct ieee80211_key *key)
85 {
86 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
87 u16 fc;
88 int hdrlen;
89 u8 *newhdr;
90
91 fc = le16_to_cpu(hdr->frame_control);
92 fc |= IEEE80211_FCTL_PROTECTED;
93 hdr->frame_control = cpu_to_le16(fc);
94
95 if ((skb_headroom(skb) < WEP_IV_LEN ||
96 skb_tailroom(skb) < WEP_ICV_LEN)) {
97 I802_DEBUG_INC(local->tx_expand_skb_head);
98 if (unlikely(pskb_expand_head(skb, WEP_IV_LEN, WEP_ICV_LEN,
99 GFP_ATOMIC)))
100 return NULL;
101 }
102
103 hdrlen = ieee80211_get_hdrlen(fc);
104 newhdr = skb_push(skb, WEP_IV_LEN);
105 memmove(newhdr, newhdr + WEP_IV_LEN, hdrlen);
106 ieee80211_wep_get_iv(local, key, newhdr + hdrlen);
107 return newhdr + hdrlen;
108 }
109
110
111 void ieee80211_wep_remove_iv(struct ieee80211_local *local,
112 struct sk_buff *skb,
113 struct ieee80211_key *key)
114 {
115 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
116 u16 fc;
117 int hdrlen;
118
119 fc = le16_to_cpu(hdr->frame_control);
120 hdrlen = ieee80211_get_hdrlen(fc);
121 memmove(skb->data + WEP_IV_LEN, skb->data, hdrlen);
122 skb_pull(skb, WEP_IV_LEN);
123 }
124
125
126 /* Perform WEP encryption using given key. data buffer must have tailroom
127 * for 4-byte ICV. data_len must not include this ICV. Note: this function
128 * does _not_ add IV. data = RC4(data | CRC32(data)) */
129 void ieee80211_wep_encrypt_data(struct crypto_blkcipher *tfm, u8 *rc4key,
130 size_t klen, u8 *data, size_t data_len)
131 {
132 struct blkcipher_desc desc = { .tfm = tfm };
133 struct scatterlist sg;
134 __le32 *icv;
135
136 icv = (__le32 *)(data + data_len);
137 *icv = cpu_to_le32(~crc32_le(~0, data, data_len));
138
139 crypto_blkcipher_setkey(tfm, rc4key, klen);
140 sg.page = virt_to_page(data);
141 sg.offset = offset_in_page(data);
142 sg.length = data_len + WEP_ICV_LEN;
143 crypto_blkcipher_encrypt(&desc, &sg, &sg, sg.length);
144 }
145
146
147 /* Perform WEP encryption on given skb. 4 bytes of extra space (IV) in the
148 * beginning of the buffer 4 bytes of extra space (ICV) in the end of the
149 * buffer will be added. Both IV and ICV will be transmitted, so the
150 * payload length increases with 8 bytes.
151 *
152 * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
153 */
154 int ieee80211_wep_encrypt(struct ieee80211_local *local, struct sk_buff *skb,
155 struct ieee80211_key *key)
156 {
157 u32 klen;
158 u8 *rc4key, *iv;
159 size_t len;
160
161 if (!key || key->alg != ALG_WEP)
162 return -1;
163
164 klen = 3 + key->keylen;
165 rc4key = kmalloc(klen, GFP_ATOMIC);
166 if (!rc4key)
167 return -1;
168
169 iv = ieee80211_wep_add_iv(local, skb, key);
170 if (!iv) {
171 kfree(rc4key);
172 return -1;
173 }
174
175 len = skb->len - (iv + WEP_IV_LEN - skb->data);
176
177 /* Prepend 24-bit IV to RC4 key */
178 memcpy(rc4key, iv, 3);
179
180 /* Copy rest of the WEP key (the secret part) */
181 memcpy(rc4key + 3, key->key, key->keylen);
182
183 /* Add room for ICV */
184 skb_put(skb, WEP_ICV_LEN);
185
186 ieee80211_wep_encrypt_data(local->wep_tx_tfm, rc4key, klen,
187 iv + WEP_IV_LEN, len);
188
189 kfree(rc4key);
190
191 return 0;
192 }
193
194
195 /* Perform WEP decryption using given key. data buffer includes encrypted
196 * payload, including 4-byte ICV, but _not_ IV. data_len must not include ICV.
197 * Return 0 on success and -1 on ICV mismatch. */
198 int ieee80211_wep_decrypt_data(struct crypto_blkcipher *tfm, u8 *rc4key,
199 size_t klen, u8 *data, size_t data_len)
200 {
201 struct blkcipher_desc desc = { .tfm = tfm };
202 struct scatterlist sg;
203 __le32 crc;
204
205 crypto_blkcipher_setkey(tfm, rc4key, klen);
206 sg.page = virt_to_page(data);
207 sg.offset = offset_in_page(data);
208 sg.length = data_len + WEP_ICV_LEN;
209 crypto_blkcipher_decrypt(&desc, &sg, &sg, sg.length);
210
211 crc = cpu_to_le32(~crc32_le(~0, data, data_len));
212 if (memcmp(&crc, data + data_len, WEP_ICV_LEN) != 0)
213 /* ICV mismatch */
214 return -1;
215
216 return 0;
217 }
218
219
220 /* Perform WEP decryption on given skb. Buffer includes whole WEP part of
221 * the frame: IV (4 bytes), encrypted payload (including SNAP header),
222 * ICV (4 bytes). skb->len includes both IV and ICV.
223 *
224 * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
225 * failure. If frame is OK, IV and ICV will be removed, i.e., decrypted payload
226 * is moved to the beginning of the skb and skb length will be reduced.
227 */
228 int ieee80211_wep_decrypt(struct ieee80211_local *local, struct sk_buff *skb,
229 struct ieee80211_key *key)
230 {
231 u32 klen;
232 u8 *rc4key;
233 u8 keyidx;
234 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
235 u16 fc;
236 int hdrlen;
237 size_t len;
238 int ret = 0;
239
240 fc = le16_to_cpu(hdr->frame_control);
241 if (!(fc & IEEE80211_FCTL_PROTECTED))
242 return -1;
243
244 hdrlen = ieee80211_get_hdrlen(fc);
245
246 if (skb->len < 8 + hdrlen)
247 return -1;
248
249 len = skb->len - hdrlen - 8;
250
251 keyidx = skb->data[hdrlen + 3] >> 6;
252
253 if (!key || keyidx != key->keyidx || key->alg != ALG_WEP)
254 return -1;
255
256 klen = 3 + key->keylen;
257
258 rc4key = kmalloc(klen, GFP_ATOMIC);
259 if (!rc4key)
260 return -1;
261
262 /* Prepend 24-bit IV to RC4 key */
263 memcpy(rc4key, skb->data + hdrlen, 3);
264
265 /* Copy rest of the WEP key (the secret part) */
266 memcpy(rc4key + 3, key->key, key->keylen);
267
268 if (ieee80211_wep_decrypt_data(local->wep_rx_tfm, rc4key, klen,
269 skb->data + hdrlen + WEP_IV_LEN,
270 len)) {
271 printk(KERN_DEBUG "WEP decrypt failed (ICV)\n");
272 ret = -1;
273 }
274
275 kfree(rc4key);
276
277 /* Trim ICV */
278 skb_trim(skb, skb->len - WEP_ICV_LEN);
279
280 /* Remove IV */
281 memmove(skb->data + WEP_IV_LEN, skb->data, hdrlen);
282 skb_pull(skb, WEP_IV_LEN);
283
284 return ret;
285 }
286
287
288 int ieee80211_wep_get_keyidx(struct sk_buff *skb)
289 {
290 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
291 u16 fc;
292 int hdrlen;
293
294 fc = le16_to_cpu(hdr->frame_control);
295 if (!(fc & IEEE80211_FCTL_PROTECTED))
296 return -1;
297
298 hdrlen = ieee80211_get_hdrlen(fc);
299
300 if (skb->len < 8 + hdrlen)
301 return -1;
302
303 return skb->data[hdrlen + 3] >> 6;
304 }
305
306
307 u8 * ieee80211_wep_is_weak_iv(struct sk_buff *skb, struct ieee80211_key *key)
308 {
309 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
310 u16 fc;
311 int hdrlen;
312 u8 *ivpos;
313 u32 iv;
314
315 fc = le16_to_cpu(hdr->frame_control);
316 if (!(fc & IEEE80211_FCTL_PROTECTED))
317 return NULL;
318
319 hdrlen = ieee80211_get_hdrlen(fc);
320 ivpos = skb->data + hdrlen;
321 iv = (ivpos[0] << 16) | (ivpos[1] << 8) | ivpos[2];
322
323 if (ieee80211_wep_weak_iv(iv, key->keylen))
324 return ivpos;
325
326 return NULL;
327 }