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/version.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/random.h>
17 #include <linux/skbuff.h>
18 #include <asm/string.h>
19 #include "rtllib.h"
20 
21 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,20))
22 #endif
23 
24 
25 #if defined(BUILT_IN_CRYPTO) || (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
26 #include "rtl_crypto.h"
27 #else
28 #include <linux/crypto.h>
29 #endif
30 
31 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
32     #include <asm/scatterlist.h>
33 #else
34     #include <linux/scatterlist.h>
35 #endif
36 #include <linux/crc32.h>
37 /*
38 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
39 #include "rtl_crypto.h"
40 #else
41 #include <linux/crypto.h>
42 #endif
43 
44 #include <asm/scatterlist.h>
45 #include <linux/crc32.h>
46 */
47 struct prism2_wep_data {
48 	u32 iv;
49 #define WEP_KEY_LEN 13
50 	u8 key[WEP_KEY_LEN + 1];
51 	u8 key_len;
52 	u8 key_idx;
53 	#if ( defined(BUILT_IN_CRYPTO) || ((LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)) && (!OPENSUSE_SLED)) )
54 	struct crypto_tfm *tfm;
55 	#else
56         struct crypto_blkcipher *tx_tfm;
57         struct crypto_blkcipher *rx_tfm;
58         #endif
59 };
60 
61 
62 static void * prism2_wep_init(int keyidx)
63 {
64 	struct prism2_wep_data *priv;
65 
66 	priv = kmalloc(sizeof(*priv), GFP_ATOMIC);
67 	if (priv == NULL)
68 		goto fail;
69 	memset(priv, 0, sizeof(*priv));
70 	priv->key_idx = keyidx;
71 
72 	#if ( defined(BUILT_IN_CRYPTO) || ((LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)) && (!OPENSUSE_SLED)) )
73 	priv->tfm = crypto_alloc_tfm("arc4", 0);
74 	if (priv->tfm == NULL) {
75 		printk(KERN_DEBUG "rtllib_crypt_wep: could not allocate "
76 		       "crypto API arc4\n");
77 		goto fail;
78 	}
79 	#else
80 	priv->tx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
81         if (IS_ERR(priv->tx_tfm)) {
82                 printk(KERN_DEBUG "rtllib_crypt_wep: could not allocate "
83                        "crypto API arc4\n");
84                 priv->tx_tfm = NULL;
85                 goto fail;
86         }
87         priv->rx_tfm = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
88         if (IS_ERR(priv->rx_tfm)) {
89                 printk(KERN_DEBUG "rtllib_crypt_wep: could not allocate "
90                        "crypto API arc4\n");
91                 priv->rx_tfm = NULL;
92                 goto fail;
93         }
94         #endif
95 
96 	/* start WEP IV from a random value */
97 	get_random_bytes(&priv->iv, 4);
98 
99 	return priv;
100 
101 fail:
102 	#if ( defined(BUILT_IN_CRYPTO) || ((LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)) && (!OPENSUSE_SLED)) )
103 	if (priv) {
104 		if (priv->tfm)
105 			crypto_free_tfm(priv->tfm);
106 		kfree(priv);
107 	}
108 	#else
109 	if (priv) {
110                 if (priv->tx_tfm)
111                         crypto_free_blkcipher(priv->tx_tfm);
112                 if (priv->rx_tfm)
113                         crypto_free_blkcipher(priv->rx_tfm);
114                 kfree(priv);
115         }
116         #endif
117 	return NULL;
118 }
119 
120 
121 static void prism2_wep_deinit(void *priv)
122 {
123 	struct prism2_wep_data *_priv = priv;
124 	#if ( defined(BUILT_IN_CRYPTO) || ((LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)) && (!OPENSUSE_SLED)) )
125 	if (_priv && _priv->tfm)
126 		crypto_free_tfm(_priv->tfm);
127 	#else
128 	if (_priv) {
129                 if (_priv->tx_tfm)
130                         crypto_free_blkcipher(_priv->tx_tfm);
131                 if (_priv->rx_tfm)
132                         crypto_free_blkcipher(_priv->rx_tfm);
133         }
134         #endif
135 	kfree(priv);
136 }
137 
138 /* Perform WEP encryption on given skb that has at least 4 bytes of headroom
139  * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
140  * so the payload length increases with 8 bytes.
141  *
142  * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
143  */
144 static int prism2_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
145 {
146 	struct prism2_wep_data *wep = priv;
147 	u32 klen, len;
148 	u8 key[WEP_KEY_LEN + 3];
149 	u8 *pos;
150 	cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
151 	#if ( !defined(BUILT_IN_CRYPTO) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)) || (OPENSUSE_SLED)) )
152 	struct blkcipher_desc desc = {.tfm = wep->tx_tfm};
153 	#endif
154 	u32 crc;
155 	u8 *icv;
156 	struct scatterlist sg;
157 	if (skb_headroom(skb) < 4 || skb_tailroom(skb) < 4 ||
158 	    skb->len < hdr_len){
159 		printk("Error!!!headroom=%d tailroom=%d skblen=%d hdr_len=%d\n",skb_headroom(skb),skb_tailroom(skb),skb->len,hdr_len);
160 		return -1;
161 	}
162 	len = skb->len - hdr_len;
163 	pos = skb_push(skb, 4);
164 	memmove(pos, pos + 4, hdr_len);
165 	pos += hdr_len;
166 
167 	klen = 3 + wep->key_len;
168 
169 	wep->iv++;
170 
171 	/* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
172 	 * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
173 	 * can be used to speedup attacks, so avoid using them. */
174 	if ((wep->iv & 0xff00) == 0xff00) {
175 		u8 B = (wep->iv >> 16) & 0xff;
176 		if (B >= 3 && B < klen)
177 			wep->iv += 0x0100;
178 	}
179 
180 	/* Prepend 24-bit IV to RC4 key and TX frame */
181 	*pos++ = key[0] = (wep->iv >> 16) & 0xff;
182 	*pos++ = key[1] = (wep->iv >> 8) & 0xff;
183 	*pos++ = key[2] = wep->iv & 0xff;
184 	*pos++ = wep->key_idx << 6;
185 
186 	/* Copy rest of the WEP key (the secret part) */
187 	memcpy(key + 3, wep->key, wep->key_len);
188 
189 	if (!tcb_desc->bHwSec)
190 	{
191 
192 		/* Append little-endian CRC32 and encrypt it to produce ICV */
193 	#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
194 		crc = ~crc32_le(~0, pos, len);
195 	#else
196 		crc = ~ether_crc_le(len, pos);
197 	#endif
198 		icv = skb_put(skb, 4);
199 		icv[0] = crc;
200 		icv[1] = crc >> 8;
201 		icv[2] = crc >> 16;
202 		icv[3] = crc >> 24;
203 
204 	#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
205 		sg.page = virt_to_page(pos);
206 		sg.offset = offset_in_page(pos);
207 		sg.length = len + 4;
208 	#else
209 		sg_init_one(&sg, pos, len+4);
210 	#endif
211 	#if ( defined(BUILT_IN_CRYPTO) || ((LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)) && (!OPENSUSE_SLED)) )
212 		crypto_cipher_setkey(wep->tfm, key, klen);
213 		crypto_cipher_encrypt(wep->tfm, &sg, &sg, len + 4);
214 		return 0;
215 	#else
216 		crypto_blkcipher_setkey(wep->tx_tfm, key, klen);
217 		return crypto_blkcipher_encrypt(&desc, &sg, &sg, len + 4);
218 	#endif
219 	}
220 
221 	return 0;
222 }
223 
224 
225 /* Perform WEP decryption on given buffer. Buffer includes whole WEP part of
226  * the frame: IV (4 bytes), encrypted payload (including SNAP header),
227  * ICV (4 bytes). len includes both IV and ICV.
228  *
229  * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
230  * failure. If frame is OK, IV and ICV will be removed.
231  */
232 static int prism2_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
233 {
234 	struct prism2_wep_data *wep = priv;
235 	u32  klen, plen;
236 	u8 key[WEP_KEY_LEN + 3];
237 	u8 keyidx, *pos;
238 	cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
239 	#if ( !defined(BUILT_IN_CRYPTO) && ((LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)) || (OPENSUSE_SLED)) )
240 	struct blkcipher_desc desc = {.tfm = wep->rx_tfm};
241 	#endif
242 	u32 crc;
243 	u8 icv[4];
244 	struct scatterlist sg;
245 	if (skb->len < hdr_len + 8)
246 		return -1;
247 
248 	pos = skb->data + hdr_len;
249 	key[0] = *pos++;
250 	key[1] = *pos++;
251 	key[2] = *pos++;
252 	keyidx = *pos++ >> 6;
253 	if (keyidx != wep->key_idx)
254 		return -1;
255 
256 	klen = 3 + wep->key_len;
257 
258 	/* Copy rest of the WEP key (the secret part) */
259 	memcpy(key + 3, wep->key, wep->key_len);
260 
261 	/* Apply RC4 to data and compute CRC32 over decrypted data */
262 	plen = skb->len - hdr_len - 8;
263 
264 	if (!tcb_desc->bHwSec)
265 	{
266 	#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
267 		sg.page = virt_to_page(pos);
268 		sg.offset = offset_in_page(pos);
269 		sg.length = plen + 4;
270 	#else
271 		sg_init_one(&sg, pos, plen+4);
272 	#endif
273 	#if ( defined(BUILT_IN_CRYPTO) || ((LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)) && (!OPENSUSE_SLED)) )
274 		crypto_cipher_setkey(wep->tfm, key, klen);
275 		crypto_cipher_decrypt(wep->tfm, &sg, &sg, plen + 4);
276 	#else
277 		crypto_blkcipher_setkey(wep->rx_tfm, key, klen);
278 		if (crypto_blkcipher_decrypt(&desc, &sg, &sg, plen + 4))
279 			return -7;
280 	#endif
281 	#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
282 		crc = ~crc32_le(~0, pos, plen);
283 	#else
284 		crc = ~ether_crc_le(plen, pos);
285 	#endif
286 		icv[0] = crc;
287 		icv[1] = crc >> 8;
288 		icv[2] = crc >> 16;
289 		icv[3] = crc >> 24;
290 		if (memcmp(icv, pos + plen, 4) != 0) {
291 			/* ICV mismatch - drop frame */
292 			return -2;
293 		}
294 	}
295 	/* Remove IV and ICV */
296 	memmove(skb->data + 4, skb->data, hdr_len);
297 	skb_pull(skb, 4);
298 	skb_trim(skb, skb->len - 4);
299 
300 	return 0;
301 }
302 
303 
304 static int prism2_wep_set_key(void *key, int len, u8 *seq, void *priv)
305 {
306 	struct prism2_wep_data *wep = priv;
307 
308 	if (len < 0 || len > WEP_KEY_LEN)
309 		return -1;
310 
311 	memcpy(wep->key, key, len);
312 	wep->key_len = len;
313 
314 	return 0;
315 }
316 
317 
318 static int prism2_wep_get_key(void *key, int len, u8 *seq, void *priv)
319 {
320 	struct prism2_wep_data *wep = priv;
321 
322 	if (len < wep->key_len)
323 		return -1;
324 
325 	memcpy(key, wep->key, wep->key_len);
326 
327 	return wep->key_len;
328 }
329 
330 
331 static char * prism2_wep_print_stats(char *p, void *priv)
332 {
333 	struct prism2_wep_data *wep = priv;
334 	p += sprintf(p, "key[%d] alg=WEP len=%d\n",
335 		     wep->key_idx, wep->key_len);
336 	return p;
337 }
338 
339 
340 static struct rtllib_crypto_ops rtllib_crypt_wep = {
341 	.name			= "WEP",
342 	.init			= prism2_wep_init,
343 	.deinit			= prism2_wep_deinit,
344 	.encrypt_mpdu		= prism2_wep_encrypt,
345 	.decrypt_mpdu		= prism2_wep_decrypt,
346 	.encrypt_msdu		= NULL,
347 	.decrypt_msdu		= NULL,
348 	.set_key		= prism2_wep_set_key,
349 	.get_key		= prism2_wep_get_key,
350 	.print_stats		= prism2_wep_print_stats,
351 	.extra_prefix_len	= 4, /* IV */
352 	.extra_postfix_len	= 4, /* ICV */
353 	.owner			= THIS_MODULE,
354 };
355 
356 
357 int __init rtllib_crypto_wep_init(void)
358 {
359 	return rtllib_register_crypto_ops(&rtllib_crypt_wep);
360 }
361 
362 
363 void __exit rtllib_crypto_wep_exit(void)
364 {
365 	rtllib_unregister_crypto_ops(&rtllib_crypt_wep);
366 }
367 
368 void rtllib_wep_null(void)
369 {
370         return;
371 }
372