2 * Host AP crypt: host-based WEP encryption implementation for Host AP driver
4 * Copyright (c) 2002-2004, Jouni Malinen <jkmaline@cc.hut.fi>
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. See README and COPYING for
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/random.h>
16 #include <linux/skbuff.h>
17 #include <asm/string.h>
19 #include <net/ieee80211.h>
21 #include <linux/crypto.h>
22 #include <asm/scatterlist.h>
23 #include <linux/crc32.h>
25 MODULE_AUTHOR("Jouni Malinen");
26 MODULE_DESCRIPTION("Host AP crypt: WEP");
27 MODULE_LICENSE("GPL");
29 struct prism2_wep_data
{
31 #define WEP_KEY_LEN 13
32 u8 key
[WEP_KEY_LEN
+ 1];
35 struct crypto_tfm
*tfm
;
38 static void *prism2_wep_init(int keyidx
)
40 struct prism2_wep_data
*priv
;
42 priv
= kzalloc(sizeof(*priv
), GFP_ATOMIC
);
45 priv
->key_idx
= keyidx
;
47 priv
->tfm
= crypto_alloc_tfm("arc4", 0);
48 if (priv
->tfm
== NULL
) {
49 printk(KERN_DEBUG
"ieee80211_crypt_wep: could not allocate "
54 /* start WEP IV from a random value */
55 get_random_bytes(&priv
->iv
, 4);
62 crypto_free_tfm(priv
->tfm
);
68 static void prism2_wep_deinit(void *priv
)
70 struct prism2_wep_data
*_priv
= priv
;
71 if (_priv
&& _priv
->tfm
)
72 crypto_free_tfm(_priv
->tfm
);
76 /* Add WEP IV/key info to a frame that has at least 4 bytes of headroom */
77 static int prism2_wep_build_iv(struct sk_buff
*skb
, int hdr_len
,
78 u8
*key
, int keylen
, void *priv
)
80 struct prism2_wep_data
*wep
= priv
;
84 if (skb_headroom(skb
) < 4 || skb
->len
< hdr_len
)
87 len
= skb
->len
- hdr_len
;
88 pos
= skb_push(skb
, 4);
89 memmove(pos
, pos
+ 4, hdr_len
);
92 klen
= 3 + wep
->key_len
;
96 /* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
97 * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
98 * can be used to speedup attacks, so avoid using them. */
99 if ((wep
->iv
& 0xff00) == 0xff00) {
100 u8 B
= (wep
->iv
>> 16) & 0xff;
101 if (B
>= 3 && B
< klen
)
105 /* Prepend 24-bit IV to RC4 key and TX frame */
106 *pos
++ = (wep
->iv
>> 16) & 0xff;
107 *pos
++ = (wep
->iv
>> 8) & 0xff;
108 *pos
++ = wep
->iv
& 0xff;
109 *pos
++ = wep
->key_idx
<< 6;
114 /* Perform WEP encryption on given skb that has at least 4 bytes of headroom
115 * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
116 * so the payload length increases with 8 bytes.
118 * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
120 static int prism2_wep_encrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
122 struct prism2_wep_data
*wep
= priv
;
125 struct scatterlist sg
;
126 u8 key
[WEP_KEY_LEN
+ 3];
128 /* other checks are in prism2_wep_build_iv */
129 if (skb_tailroom(skb
) < 4)
132 /* add the IV to the frame */
133 if (prism2_wep_build_iv(skb
, hdr_len
, NULL
, 0, priv
))
136 /* Copy the IV into the first 3 bytes of the key */
137 memcpy(key
, skb
->data
+ hdr_len
, 3);
139 /* Copy rest of the WEP key (the secret part) */
140 memcpy(key
+ 3, wep
->key
, wep
->key_len
);
142 len
= skb
->len
- hdr_len
- 4;
143 pos
= skb
->data
+ hdr_len
+ 4;
144 klen
= 3 + wep
->key_len
;
146 /* Append little-endian CRC32 over only the data and encrypt it to produce ICV */
147 crc
= ~crc32_le(~0, pos
, len
);
148 icv
= skb_put(skb
, 4);
154 crypto_cipher_setkey(wep
->tfm
, key
, klen
);
155 sg
.page
= virt_to_page(pos
);
156 sg
.offset
= offset_in_page(pos
);
158 crypto_cipher_encrypt(wep
->tfm
, &sg
, &sg
, len
+ 4);
163 /* Perform WEP decryption on given buffer. Buffer includes whole WEP part of
164 * the frame: IV (4 bytes), encrypted payload (including SNAP header),
165 * ICV (4 bytes). len includes both IV and ICV.
167 * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
168 * failure. If frame is OK, IV and ICV will be removed.
170 static int prism2_wep_decrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
172 struct prism2_wep_data
*wep
= priv
;
174 u8 key
[WEP_KEY_LEN
+ 3];
175 u8 keyidx
, *pos
, icv
[4];
176 struct scatterlist sg
;
178 if (skb
->len
< hdr_len
+ 8)
181 pos
= skb
->data
+ hdr_len
;
185 keyidx
= *pos
++ >> 6;
186 if (keyidx
!= wep
->key_idx
)
189 klen
= 3 + wep
->key_len
;
191 /* Copy rest of the WEP key (the secret part) */
192 memcpy(key
+ 3, wep
->key
, wep
->key_len
);
194 /* Apply RC4 to data and compute CRC32 over decrypted data */
195 plen
= skb
->len
- hdr_len
- 8;
197 crypto_cipher_setkey(wep
->tfm
, key
, klen
);
198 sg
.page
= virt_to_page(pos
);
199 sg
.offset
= offset_in_page(pos
);
200 sg
.length
= plen
+ 4;
201 crypto_cipher_decrypt(wep
->tfm
, &sg
, &sg
, plen
+ 4);
203 crc
= ~crc32_le(~0, pos
, plen
);
208 if (memcmp(icv
, pos
+ plen
, 4) != 0) {
209 /* ICV mismatch - drop frame */
213 /* Remove IV and ICV */
214 memmove(skb
->data
+ 4, skb
->data
, hdr_len
);
216 skb_trim(skb
, skb
->len
- 4);
221 static int prism2_wep_set_key(void *key
, int len
, u8
* seq
, void *priv
)
223 struct prism2_wep_data
*wep
= priv
;
225 if (len
< 0 || len
> WEP_KEY_LEN
)
228 memcpy(wep
->key
, key
, len
);
234 static int prism2_wep_get_key(void *key
, int len
, u8
* seq
, void *priv
)
236 struct prism2_wep_data
*wep
= priv
;
238 if (len
< wep
->key_len
)
241 memcpy(key
, wep
->key
, wep
->key_len
);
246 static char *prism2_wep_print_stats(char *p
, void *priv
)
248 struct prism2_wep_data
*wep
= priv
;
249 p
+= sprintf(p
, "key[%d] alg=WEP len=%d\n", wep
->key_idx
, wep
->key_len
);
253 static struct ieee80211_crypto_ops ieee80211_crypt_wep
= {
255 .init
= prism2_wep_init
,
256 .deinit
= prism2_wep_deinit
,
257 .build_iv
= prism2_wep_build_iv
,
258 .encrypt_mpdu
= prism2_wep_encrypt
,
259 .decrypt_mpdu
= prism2_wep_decrypt
,
260 .encrypt_msdu
= NULL
,
261 .decrypt_msdu
= NULL
,
262 .set_key
= prism2_wep_set_key
,
263 .get_key
= prism2_wep_get_key
,
264 .print_stats
= prism2_wep_print_stats
,
265 .extra_mpdu_prefix_len
= 4, /* IV */
266 .extra_mpdu_postfix_len
= 4, /* ICV */
267 .owner
= THIS_MODULE
,
270 static int __init
ieee80211_crypto_wep_init(void)
272 return ieee80211_register_crypto_ops(&ieee80211_crypt_wep
);
275 static void __exit
ieee80211_crypto_wep_exit(void)
277 ieee80211_unregister_crypto_ops(&ieee80211_crypt_wep
);
280 module_init(ieee80211_crypto_wep_init
);
281 module_exit(ieee80211_crypto_wep_exit
);