blob: 0ebf235f693932e4c09241b7402658bd14d6620a [file] [log] [blame]
Jeff Garzikb4538722005-05-12 22:48:20 -04001/*
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
Jeff Garzikb4538722005-05-12 22:48:20 -040012#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
Jeff Garzikb4538722005-05-12 22:48:20 -040021#include <linux/crypto.h>
22#include <asm/scatterlist.h>
23#include <linux/crc32.h>
24
25MODULE_AUTHOR("Jouni Malinen");
26MODULE_DESCRIPTION("Host AP crypt: WEP");
27MODULE_LICENSE("GPL");
28
Jeff Garzikb4538722005-05-12 22:48:20 -040029struct 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
James Ketrenos6eb6edf2005-09-22 10:34:15 +000038static void *prism2_wep_init(int keyidx)
Jeff Garzikb4538722005-05-12 22:48:20 -040039{
40 struct prism2_wep_data *priv;
41
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070042 priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
Jeff Garzikb4538722005-05-12 22:48:20 -040043 if (priv == NULL)
44 goto fail;
Jeff Garzikb4538722005-05-12 22:48:20 -040045 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
Jeff Garzik0edd5b42005-09-07 00:48:31 -040059 fail:
Jeff Garzikb4538722005-05-12 22:48:20 -040060 if (priv) {
61 if (priv->tfm)
62 crypto_free_tfm(priv->tfm);
63 kfree(priv);
64 }
65 return NULL;
66}
67
Jeff Garzikb4538722005-05-12 22:48:20 -040068static 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
Johannes Berga4bf26f2005-12-31 11:35:20 +010076/* Add WEP IV/key info to a frame that has at least 4 bytes of headroom */
Zhu Yi9184d932006-01-19 16:22:32 +080077static int prism2_wep_build_iv(struct sk_buff *skb, int hdr_len,
78 u8 *key, int keylen, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -040079{
80 struct prism2_wep_data *wep = priv;
Johannes Berga4bf26f2005-12-31 11:35:20 +010081 u32 klen, len;
82 u8 *pos;
83
84 if (skb_headroom(skb) < 4 || skb->len < hdr_len)
Jeff Garzikb4538722005-05-12 22:48:20 -040085 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 */
Johannes Berga4bf26f2005-12-31 11:35:20 +0100106 *pos++ = (wep->iv >> 16) & 0xff;
107 *pos++ = (wep->iv >> 8) & 0xff;
108 *pos++ = wep->iv & 0xff;
Jeff Garzikb4538722005-05-12 22:48:20 -0400109 *pos++ = wep->key_idx << 6;
110
Johannes Berga4bf26f2005-12-31 11:35:20 +0100111 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 */
120static 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 */
Zhu Yi9184d932006-01-19 16:22:32 +0800133 if (prism2_wep_build_iv(skb, hdr_len, NULL, 0, priv))
Johannes Berga4bf26f2005-12-31 11:35:20 +0100134 return -1;
135
136 /* Copy the IV into the first 3 bytes of the key */
137 memcpy(key, skb->data + hdr_len, 3);
138
Jeff Garzikb4538722005-05-12 22:48:20 -0400139 /* Copy rest of the WEP key (the secret part) */
140 memcpy(key + 3, wep->key, wep->key_len);
Johannes Berga4bf26f2005-12-31 11:35:20 +0100141
142 len = skb->len - hdr_len - 4;
143 pos = skb->data + hdr_len + 4;
144 klen = 3 + wep->key_len;
Jeff Garzikb4538722005-05-12 22:48:20 -0400145
Johannes Berga4bf26f2005-12-31 11:35:20 +0100146 /* Append little-endian CRC32 over only the data and encrypt it to produce ICV */
Jeff Garzikb4538722005-05-12 22:48:20 -0400147 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
Jeff Garzikb4538722005-05-12 22:48:20 -0400163/* 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 */
170static 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
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400221static int prism2_wep_set_key(void *key, int len, u8 * seq, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -0400222{
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
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400234static int prism2_wep_get_key(void *key, int len, u8 * seq, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -0400235{
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
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400246static char *prism2_wep_print_stats(char *p, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -0400247{
248 struct prism2_wep_data *wep = priv;
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400249 p += sprintf(p, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
Jeff Garzikb4538722005-05-12 22:48:20 -0400250 return p;
251}
252
Jeff Garzikb4538722005-05-12 22:48:20 -0400253static struct ieee80211_crypto_ops ieee80211_crypt_wep = {
James Ketrenos74079fd2005-09-13 17:35:21 -0500254 .name = "WEP",
255 .init = prism2_wep_init,
256 .deinit = prism2_wep_deinit,
Johannes Berga4bf26f2005-12-31 11:35:20 +0100257 .build_iv = prism2_wep_build_iv,
James Ketrenos74079fd2005-09-13 17:35:21 -0500258 .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,
James Ketrenos1264fc02005-09-21 11:54:53 -0500265 .extra_mpdu_prefix_len = 4, /* IV */
266 .extra_mpdu_postfix_len = 4, /* ICV */
James Ketrenos74079fd2005-09-13 17:35:21 -0500267 .owner = THIS_MODULE,
Jeff Garzikb4538722005-05-12 22:48:20 -0400268};
269
Jeff Garzikb4538722005-05-12 22:48:20 -0400270static int __init ieee80211_crypto_wep_init(void)
271{
272 return ieee80211_register_crypto_ops(&ieee80211_crypt_wep);
273}
274
Jeff Garzikb4538722005-05-12 22:48:20 -0400275static void __exit ieee80211_crypto_wep_exit(void)
276{
277 ieee80211_unregister_crypto_ops(&ieee80211_crypt_wep);
278}
279
Jeff Garzikb4538722005-05-12 22:48:20 -0400280module_init(ieee80211_crypto_wep_init);
281module_exit(ieee80211_crypto_wep_exit);