[PATCH] ieee80211: TKIP and CCMP replay check rework
[linux-2.6-block.git] / net / ieee80211 / ieee80211_crypt_wep.c
CommitLineData
b453872c
JG
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
b453872c
JG
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
b453872c
JG
21#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
b453872c
JG
29struct 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
6eb6edf0 38static void *prism2_wep_init(int keyidx)
b453872c
JG
39{
40 struct prism2_wep_data *priv;
41
0da974f4 42 priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
b453872c
JG
43 if (priv == NULL)
44 goto fail;
b453872c
JG
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
0edd5b44 59 fail:
b453872c
JG
60 if (priv) {
61 if (priv->tfm)
62 crypto_free_tfm(priv->tfm);
63 kfree(priv);
64 }
65 return NULL;
66}
67
b453872c
JG
68static 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
a4bf26f3 76/* Add WEP IV/key info to a frame that has at least 4 bytes of headroom */
9184d934
ZY
77static int prism2_wep_build_iv(struct sk_buff *skb, int hdr_len,
78 u8 *key, int keylen, void *priv)
b453872c
JG
79{
80 struct prism2_wep_data *wep = priv;
a4bf26f3
JB
81 u32 klen, len;
82 u8 *pos;
83
84 if (skb_headroom(skb) < 4 || skb->len < hdr_len)
b453872c
JG
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 */
a4bf26f3
JB
106 *pos++ = (wep->iv >> 16) & 0xff;
107 *pos++ = (wep->iv >> 8) & 0xff;
108 *pos++ = wep->iv & 0xff;
b453872c
JG
109 *pos++ = wep->key_idx << 6;
110
a4bf26f3
JB
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 */
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 */
9184d934 133 if (prism2_wep_build_iv(skb, hdr_len, NULL, 0, priv))
a4bf26f3
JB
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
b453872c
JG
139 /* Copy rest of the WEP key (the secret part) */
140 memcpy(key + 3, wep->key, wep->key_len);
a4bf26f3
JB
141
142 len = skb->len - hdr_len - 4;
143 pos = skb->data + hdr_len + 4;
144 klen = 3 + wep->key_len;
b453872c 145
a4bf26f3 146 /* Append little-endian CRC32 over only the data and encrypt it to produce ICV */
b453872c
JG
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
b453872c
JG
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 */
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
0edd5b44 221static int prism2_wep_set_key(void *key, int len, u8 * seq, void *priv)
b453872c
JG
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
0edd5b44 234static int prism2_wep_get_key(void *key, int len, u8 * seq, void *priv)
b453872c
JG
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
0edd5b44 246static char *prism2_wep_print_stats(char *p, void *priv)
b453872c
JG
247{
248 struct prism2_wep_data *wep = priv;
0edd5b44 249 p += sprintf(p, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
b453872c
JG
250 return p;
251}
252
b453872c 253static struct ieee80211_crypto_ops ieee80211_crypt_wep = {
74079fdc
JK
254 .name = "WEP",
255 .init = prism2_wep_init,
256 .deinit = prism2_wep_deinit,
a4bf26f3 257 .build_iv = prism2_wep_build_iv,
74079fdc
JK
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,
1264fc04
JK
265 .extra_mpdu_prefix_len = 4, /* IV */
266 .extra_mpdu_postfix_len = 4, /* ICV */
74079fdc 267 .owner = THIS_MODULE,
b453872c
JG
268};
269
b453872c
JG
270static int __init ieee80211_crypto_wep_init(void)
271{
272 return ieee80211_register_crypto_ops(&ieee80211_crypt_wep);
273}
274
b453872c
JG
275static void __exit ieee80211_crypto_wep_exit(void)
276{
277 ieee80211_unregister_crypto_ops(&ieee80211_crypt_wep);
278}
279
b453872c
JG
280module_init(ieee80211_crypto_wep_init);
281module_exit(ieee80211_crypto_wep_exit);