Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / net / ieee80211 / ieee80211_crypt_wep.c
1 /*
2  * Host AP crypt: host-based WEP encryption implementation for Host AP driver
3  *
4  * Copyright (c) 2002-2004, Jouni Malinen <jkmaline@cc.hut.fi>
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. See README and COPYING for
9  * more details.
10  */
11
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>
18
19 #include <net/ieee80211.h>
20
21 #include <linux/crypto.h>
22 #include <asm/scatterlist.h>
23 #include <linux/crc32.h>
24
25 MODULE_AUTHOR("Jouni Malinen");
26 MODULE_DESCRIPTION("Host AP crypt: WEP");
27 MODULE_LICENSE("GPL");
28
29 struct prism2_wep_data {
30         u32 iv;
31 #define WEP_KEY_LEN 13
32         u8 key[WEP_KEY_LEN + 1];
33         u8 key_len;
34         u8 key_idx;
35         struct crypto_tfm *tfm;
36 };
37
38 static void *prism2_wep_init(int keyidx)
39 {
40         struct prism2_wep_data *priv;
41
42         priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
43         if (priv == NULL)
44                 goto fail;
45         priv->key_idx = keyidx;
46
47         priv->tfm = crypto_alloc_tfm("arc4", 0);
48         if (priv->tfm == NULL) {
49                 printk(KERN_DEBUG "ieee80211_crypt_wep: could not allocate "
50                        "crypto API arc4\n");
51                 goto fail;
52         }
53
54         /* start WEP IV from a random value */
55         get_random_bytes(&priv->iv, 4);
56
57         return priv;
58
59       fail:
60         if (priv) {
61                 if (priv->tfm)
62                         crypto_free_tfm(priv->tfm);
63                 kfree(priv);
64         }
65         return NULL;
66 }
67
68 static void prism2_wep_deinit(void *priv)
69 {
70         struct prism2_wep_data *_priv = priv;
71         if (_priv && _priv->tfm)
72                 crypto_free_tfm(_priv->tfm);
73         kfree(priv);
74 }
75
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)
79 {
80         struct prism2_wep_data *wep = priv;
81         u32 klen, len;
82         u8 *pos;
83         
84         if (skb_headroom(skb) < 4 || skb->len < hdr_len)
85                 return -1;
86
87         len = skb->len - hdr_len;
88         pos = skb_push(skb, 4);
89         memmove(pos, pos + 4, hdr_len);
90         pos += hdr_len;
91
92         klen = 3 + wep->key_len;
93
94         wep->iv++;
95
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)
102                         wep->iv += 0x0100;
103         }
104
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;
110
111         return 0;
112 }
113
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.
117  *
118  * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
119  */
120 static int prism2_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
121 {
122         struct prism2_wep_data *wep = priv;
123         u32 crc, klen, len;
124         u8 *pos, *icv;
125         struct scatterlist sg;
126         u8 key[WEP_KEY_LEN + 3];
127
128         /* other checks are in prism2_wep_build_iv */
129         if (skb_tailroom(skb) < 4)
130                 return -1;
131         
132         /* add the IV to the frame */
133         if (prism2_wep_build_iv(skb, hdr_len, NULL, 0, priv))
134                 return -1;
135         
136         /* Copy the IV into the first 3 bytes of the key */
137         memcpy(key, skb->data + hdr_len, 3);
138
139         /* Copy rest of the WEP key (the secret part) */
140         memcpy(key + 3, wep->key, wep->key_len);
141         
142         len = skb->len - hdr_len - 4;
143         pos = skb->data + hdr_len + 4;
144         klen = 3 + wep->key_len;
145
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);
149         icv[0] = crc;
150         icv[1] = crc >> 8;
151         icv[2] = crc >> 16;
152         icv[3] = crc >> 24;
153
154         crypto_cipher_setkey(wep->tfm, key, klen);
155         sg.page = virt_to_page(pos);
156         sg.offset = offset_in_page(pos);
157         sg.length = len + 4;
158         crypto_cipher_encrypt(wep->tfm, &sg, &sg, len + 4);
159
160         return 0;
161 }
162
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.
166  *
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.
169  */
170 static int prism2_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
171 {
172         struct prism2_wep_data *wep = priv;
173         u32 crc, klen, plen;
174         u8 key[WEP_KEY_LEN + 3];
175         u8 keyidx, *pos, icv[4];
176         struct scatterlist sg;
177
178         if (skb->len < hdr_len + 8)
179                 return -1;
180
181         pos = skb->data + hdr_len;
182         key[0] = *pos++;
183         key[1] = *pos++;
184         key[2] = *pos++;
185         keyidx = *pos++ >> 6;
186         if (keyidx != wep->key_idx)
187                 return -1;
188
189         klen = 3 + wep->key_len;
190
191         /* Copy rest of the WEP key (the secret part) */
192         memcpy(key + 3, wep->key, wep->key_len);
193
194         /* Apply RC4 to data and compute CRC32 over decrypted data */
195         plen = skb->len - hdr_len - 8;
196
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);
202
203         crc = ~crc32_le(~0, pos, plen);
204         icv[0] = crc;
205         icv[1] = crc >> 8;
206         icv[2] = crc >> 16;
207         icv[3] = crc >> 24;
208         if (memcmp(icv, pos + plen, 4) != 0) {
209                 /* ICV mismatch - drop frame */
210                 return -2;
211         }
212
213         /* Remove IV and ICV */
214         memmove(skb->data + 4, skb->data, hdr_len);
215         skb_pull(skb, 4);
216         skb_trim(skb, skb->len - 4);
217
218         return 0;
219 }
220
221 static int prism2_wep_set_key(void *key, int len, u8 * seq, void *priv)
222 {
223         struct prism2_wep_data *wep = priv;
224
225         if (len < 0 || len > WEP_KEY_LEN)
226                 return -1;
227
228         memcpy(wep->key, key, len);
229         wep->key_len = len;
230
231         return 0;
232 }
233
234 static int prism2_wep_get_key(void *key, int len, u8 * seq, void *priv)
235 {
236         struct prism2_wep_data *wep = priv;
237
238         if (len < wep->key_len)
239                 return -1;
240
241         memcpy(key, wep->key, wep->key_len);
242
243         return wep->key_len;
244 }
245
246 static char *prism2_wep_print_stats(char *p, void *priv)
247 {
248         struct prism2_wep_data *wep = priv;
249         p += sprintf(p, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
250         return p;
251 }
252
253 static struct ieee80211_crypto_ops ieee80211_crypt_wep = {
254         .name = "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,
268 };
269
270 static int __init ieee80211_crypto_wep_init(void)
271 {
272         return ieee80211_register_crypto_ops(&ieee80211_crypt_wep);
273 }
274
275 static void __exit ieee80211_crypto_wep_exit(void)
276 {
277         ieee80211_unregister_crypto_ops(&ieee80211_crypt_wep);
278 }
279
280 module_init(ieee80211_crypto_wep_init);
281 module_exit(ieee80211_crypto_wep_exit);