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 <crypto/skcipher.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 <linux/string.h>
21 #include <linux/scatterlist.h>
22 #include <linux/crc32.h>
24 struct prism2_wep_data
{
26 #define WEP_KEY_LEN 13
27 u8 key
[WEP_KEY_LEN
+ 1];
30 struct crypto_skcipher
*tx_tfm
;
31 struct crypto_skcipher
*rx_tfm
;
35 static void *prism2_wep_init(int keyidx
)
37 struct prism2_wep_data
*priv
;
39 priv
= kzalloc(sizeof(*priv
), GFP_ATOMIC
);
42 priv
->key_idx
= keyidx
;
44 priv
->tx_tfm
= crypto_alloc_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC
);
45 if (IS_ERR(priv
->tx_tfm
)) {
46 pr_debug("rtllib_crypt_wep: could not allocate crypto API arc4\n");
50 priv
->rx_tfm
= crypto_alloc_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC
);
51 if (IS_ERR(priv
->rx_tfm
)) {
52 pr_debug("rtllib_crypt_wep: could not allocate crypto API arc4\n");
57 /* start WEP IV from a random value */
58 get_random_bytes(&priv
->iv
, 4);
64 crypto_free_skcipher(priv
->tx_tfm
);
65 crypto_free_skcipher(priv
->rx_tfm
);
72 static void prism2_wep_deinit(void *priv
)
74 struct prism2_wep_data
*_priv
= priv
;
77 crypto_free_skcipher(_priv
->tx_tfm
);
78 crypto_free_skcipher(_priv
->rx_tfm
);
83 /* Perform WEP encryption on given skb that has at least 4 bytes of headroom
84 * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
85 * so the payload length increases with 8 bytes.
87 * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
89 static int prism2_wep_encrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
91 struct prism2_wep_data
*wep
= priv
;
93 u8 key
[WEP_KEY_LEN
+ 3];
95 struct cb_desc
*tcb_desc
= (struct cb_desc
*)(skb
->cb
+
99 struct scatterlist sg
;
102 if (skb_headroom(skb
) < 4 || skb_tailroom(skb
) < 4 ||
104 pr_err("Error!!! headroom=%d tailroom=%d skblen=%d hdr_len=%d\n",
105 skb_headroom(skb
), skb_tailroom(skb
), skb
->len
, hdr_len
);
108 len
= skb
->len
- hdr_len
;
109 pos
= skb_push(skb
, 4);
110 memmove(pos
, pos
+ 4, hdr_len
);
113 klen
= 3 + wep
->key_len
;
117 /* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
118 * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
119 * can be used to speedup attacks, so avoid using them.
121 if ((wep
->iv
& 0xff00) == 0xff00) {
122 u8 B
= (wep
->iv
>> 16) & 0xff;
124 if (B
>= 3 && B
< klen
)
128 /* Prepend 24-bit IV to RC4 key and TX frame */
129 *pos
++ = key
[0] = (wep
->iv
>> 16) & 0xff;
130 *pos
++ = key
[1] = (wep
->iv
>> 8) & 0xff;
131 *pos
++ = key
[2] = wep
->iv
& 0xff;
132 *pos
++ = wep
->key_idx
<< 6;
134 /* Copy rest of the WEP key (the secret part) */
135 memcpy(key
+ 3, wep
->key
, wep
->key_len
);
137 if (!tcb_desc
->bHwSec
) {
138 SKCIPHER_REQUEST_ON_STACK(req
, wep
->tx_tfm
);
140 /* Append little-endian CRC32 and encrypt it to produce ICV */
141 crc
= ~crc32_le(~0, pos
, len
);
142 icv
= skb_put(skb
, 4);
148 sg_init_one(&sg
, pos
, len
+4);
149 crypto_skcipher_setkey(wep
->tx_tfm
, key
, klen
);
150 skcipher_request_set_tfm(req
, wep
->tx_tfm
);
151 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
152 skcipher_request_set_crypt(req
, &sg
, &sg
, len
+ 4, NULL
);
153 err
= crypto_skcipher_encrypt(req
);
154 skcipher_request_zero(req
);
162 /* Perform WEP decryption on given struct buffer. Buffer includes whole WEP
163 * part of the frame: IV (4 bytes), encrypted payload (including SNAP header),
164 * ICV (4 bytes). len includes both IV and ICV.
166 * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
167 * failure. If frame is OK, IV and ICV will be removed.
169 static int prism2_wep_decrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
171 struct prism2_wep_data
*wep
= priv
;
173 u8 key
[WEP_KEY_LEN
+ 3];
175 struct cb_desc
*tcb_desc
= (struct cb_desc
*)(skb
->cb
+
179 struct scatterlist sg
;
182 if (skb
->len
< hdr_len
+ 8)
185 pos
= skb
->data
+ hdr_len
;
189 keyidx
= *pos
++ >> 6;
190 if (keyidx
!= wep
->key_idx
)
193 klen
= 3 + wep
->key_len
;
195 /* Copy rest of the WEP key (the secret part) */
196 memcpy(key
+ 3, wep
->key
, wep
->key_len
);
198 /* Apply RC4 to data and compute CRC32 over decrypted data */
199 plen
= skb
->len
- hdr_len
- 8;
201 if (!tcb_desc
->bHwSec
) {
202 SKCIPHER_REQUEST_ON_STACK(req
, wep
->rx_tfm
);
204 sg_init_one(&sg
, pos
, plen
+4);
205 crypto_skcipher_setkey(wep
->rx_tfm
, key
, klen
);
206 skcipher_request_set_tfm(req
, wep
->rx_tfm
);
207 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
208 skcipher_request_set_crypt(req
, &sg
, &sg
, plen
+ 4, NULL
);
209 err
= crypto_skcipher_decrypt(req
);
210 skcipher_request_zero(req
);
213 crc
= ~crc32_le(~0, pos
, plen
);
218 if (memcmp(icv
, pos
+ plen
, 4) != 0) {
219 /* ICV mismatch - drop frame */
223 /* Remove IV and ICV */
224 memmove(skb
->data
+ 4, skb
->data
, hdr_len
);
226 skb_trim(skb
, skb
->len
- 4);
232 static int prism2_wep_set_key(void *key
, int len
, u8
*seq
, void *priv
)
234 struct prism2_wep_data
*wep
= priv
;
236 if (len
< 0 || len
> WEP_KEY_LEN
)
239 memcpy(wep
->key
, key
, len
);
246 static int prism2_wep_get_key(void *key
, int len
, u8
*seq
, void *priv
)
248 struct prism2_wep_data
*wep
= priv
;
250 if (len
< wep
->key_len
)
253 memcpy(key
, wep
->key
, wep
->key_len
);
259 static void prism2_wep_print_stats(struct seq_file
*m
, void *priv
)
261 struct prism2_wep_data
*wep
= priv
;
263 seq_printf(m
, "key[%d] alg=WEP len=%d\n", wep
->key_idx
, wep
->key_len
);
266 static struct lib80211_crypto_ops rtllib_crypt_wep
= {
268 .init
= prism2_wep_init
,
269 .deinit
= prism2_wep_deinit
,
270 .encrypt_mpdu
= prism2_wep_encrypt
,
271 .decrypt_mpdu
= prism2_wep_decrypt
,
272 .encrypt_msdu
= NULL
,
273 .decrypt_msdu
= NULL
,
274 .set_key
= prism2_wep_set_key
,
275 .get_key
= prism2_wep_get_key
,
276 .print_stats
= prism2_wep_print_stats
,
277 .extra_mpdu_prefix_len
= 4, /* IV */
278 .extra_mpdu_postfix_len
= 4, /* ICV */
279 .owner
= THIS_MODULE
,
283 static int __init
rtllib_crypto_wep_init(void)
285 return lib80211_register_crypto_ops(&rtllib_crypt_wep
);
289 static void __exit
rtllib_crypto_wep_exit(void)
291 lib80211_unregister_crypto_ops(&rtllib_crypt_wep
);
294 module_init(rtllib_crypto_wep_init
);
295 module_exit(rtllib_crypto_wep_exit
);
297 MODULE_LICENSE("GPL");