2 * Software WEP encryption implementation
3 * Copyright 2002, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2003, Instant802 Networks, Inc.
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.
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>
20 #include <net/d80211.h>
21 #include "ieee80211_i.h"
25 int ieee80211_wep_init(struct ieee80211_local
*local
)
27 /* start WEP IV from a random value */
28 get_random_bytes(&local
->wep_iv
, WEP_IV_LEN
);
30 local
->wep_tx_tfm
= crypto_alloc_blkcipher("ecb(arc4)", 0,
32 if (IS_ERR(local
->wep_tx_tfm
))
35 local
->wep_rx_tfm
= crypto_alloc_blkcipher("ecb(arc4)", 0,
37 if (IS_ERR(local
->wep_rx_tfm
)) {
38 crypto_free_blkcipher(local
->wep_tx_tfm
);
45 void ieee80211_wep_free(struct ieee80211_local
*local
)
47 crypto_free_blkcipher(local
->wep_tx_tfm
);
48 crypto_free_blkcipher(local
->wep_rx_tfm
);
51 static inline int ieee80211_wep_weak_iv(u32 iv
, int keylen
)
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
)
65 void ieee80211_wep_get_iv(struct ieee80211_local
*local
,
66 struct ieee80211_key
*key
, u8
*iv
)
69 if (ieee80211_wep_weak_iv(local
->wep_iv
, key
->keylen
))
70 local
->wep_iv
+= 0x0100;
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;
82 u8
* ieee80211_wep_add_iv(struct ieee80211_local
*local
,
84 struct ieee80211_key
*key
)
86 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
91 fc
= le16_to_cpu(hdr
->frame_control
);
92 fc
|= IEEE80211_FCTL_PROTECTED
;
93 hdr
->frame_control
= cpu_to_le16(fc
);
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
,
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
;
111 void ieee80211_wep_remove_iv(struct ieee80211_local
*local
,
113 struct ieee80211_key
*key
)
115 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
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
);
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
)
132 struct blkcipher_desc desc
= { .tfm
= tfm
};
133 struct scatterlist sg
;
136 icv
= (__le32
*)(data
+ data_len
);
137 *icv
= cpu_to_le32(~crc32_le(~0, data
, data_len
));
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
);
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.
152 * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
154 int ieee80211_wep_encrypt(struct ieee80211_local
*local
, struct sk_buff
*skb
,
155 struct ieee80211_key
*key
)
161 if (!key
|| key
->alg
!= ALG_WEP
)
164 klen
= 3 + key
->keylen
;
165 rc4key
= kmalloc(klen
, GFP_ATOMIC
);
169 iv
= ieee80211_wep_add_iv(local
, skb
, key
);
175 len
= skb
->len
- (iv
+ WEP_IV_LEN
- skb
->data
);
177 /* Prepend 24-bit IV to RC4 key */
178 memcpy(rc4key
, iv
, 3);
180 /* Copy rest of the WEP key (the secret part) */
181 memcpy(rc4key
+ 3, key
->key
, key
->keylen
);
183 /* Add room for ICV */
184 skb_put(skb
, WEP_ICV_LEN
);
186 ieee80211_wep_encrypt_data(local
->wep_tx_tfm
, rc4key
, klen
,
187 iv
+ WEP_IV_LEN
, len
);
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
)
201 struct blkcipher_desc desc
= { .tfm
= tfm
};
202 struct scatterlist sg
;
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
);
211 crc
= cpu_to_le32(~crc32_le(~0, data
, data_len
));
212 if (memcmp(&crc
, data
+ data_len
, WEP_ICV_LEN
) != 0)
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.
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.
228 int ieee80211_wep_decrypt(struct ieee80211_local
*local
, struct sk_buff
*skb
,
229 struct ieee80211_key
*key
)
234 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
240 fc
= le16_to_cpu(hdr
->frame_control
);
241 if (!(fc
& IEEE80211_FCTL_PROTECTED
))
244 hdrlen
= ieee80211_get_hdrlen(fc
);
246 if (skb
->len
< 8 + hdrlen
)
249 len
= skb
->len
- hdrlen
- 8;
251 keyidx
= skb
->data
[hdrlen
+ 3] >> 6;
253 if (!key
|| keyidx
!= key
->keyidx
|| key
->alg
!= ALG_WEP
)
256 klen
= 3 + key
->keylen
;
258 rc4key
= kmalloc(klen
, GFP_ATOMIC
);
262 /* Prepend 24-bit IV to RC4 key */
263 memcpy(rc4key
, skb
->data
+ hdrlen
, 3);
265 /* Copy rest of the WEP key (the secret part) */
266 memcpy(rc4key
+ 3, key
->key
, key
->keylen
);
268 if (ieee80211_wep_decrypt_data(local
->wep_rx_tfm
, rc4key
, klen
,
269 skb
->data
+ hdrlen
+ WEP_IV_LEN
,
271 printk(KERN_DEBUG
"WEP decrypt failed (ICV)\n");
278 skb_trim(skb
, skb
->len
- WEP_ICV_LEN
);
281 memmove(skb
->data
+ WEP_IV_LEN
, skb
->data
, hdrlen
);
282 skb_pull(skb
, WEP_IV_LEN
);
288 int ieee80211_wep_get_keyidx(struct sk_buff
*skb
)
290 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
294 fc
= le16_to_cpu(hdr
->frame_control
);
295 if (!(fc
& IEEE80211_FCTL_PROTECTED
))
298 hdrlen
= ieee80211_get_hdrlen(fc
);
300 if (skb
->len
< 8 + hdrlen
)
303 return skb
->data
[hdrlen
+ 3] >> 6;
307 u8
* ieee80211_wep_is_weak_iv(struct sk_buff
*skb
, struct ieee80211_key
*key
)
309 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
315 fc
= le16_to_cpu(hdr
->frame_control
);
316 if (!(fc
& IEEE80211_FCTL_PROTECTED
))
319 hdrlen
= ieee80211_get_hdrlen(fc
);
320 ivpos
= skb
->data
+ hdrlen
;
321 iv
= (ivpos
[0] << 16) | (ivpos
[1] << 8) | ivpos
[2];
323 if (ieee80211_wep_weak_iv(iv
, key
->keylen
))
This page took 0.058611 seconds and 5 git commands to generate.