GNU Linux-libre 4.9.318-gnu1
[releases.git] / drivers / staging / rtl8192e / rtllib_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 <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>
19 #include "rtllib.h"
20
21 #include <linux/scatterlist.h>
22 #include <linux/crc32.h>
23
24 struct prism2_wep_data {
25         u32 iv;
26 #define WEP_KEY_LEN 13
27         u8 key[WEP_KEY_LEN + 1];
28         u8 key_len;
29         u8 key_idx;
30         struct crypto_skcipher *tx_tfm;
31         struct crypto_skcipher *rx_tfm;
32 };
33
34
35 static void *prism2_wep_init(int keyidx)
36 {
37         struct prism2_wep_data *priv;
38
39         priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
40         if (priv == NULL)
41                 goto fail;
42         priv->key_idx = keyidx;
43
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");
47                 priv->tx_tfm = NULL;
48                 goto fail;
49         }
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");
53                 priv->rx_tfm = NULL;
54                 goto fail;
55         }
56
57         /* start WEP IV from a random value */
58         get_random_bytes(&priv->iv, 4);
59
60         return priv;
61
62 fail:
63         if (priv) {
64                 crypto_free_skcipher(priv->tx_tfm);
65                 crypto_free_skcipher(priv->rx_tfm);
66                 kfree(priv);
67         }
68         return NULL;
69 }
70
71
72 static void prism2_wep_deinit(void *priv)
73 {
74         struct prism2_wep_data *_priv = priv;
75
76         if (_priv) {
77                 crypto_free_skcipher(_priv->tx_tfm);
78                 crypto_free_skcipher(_priv->rx_tfm);
79         }
80         kfree(priv);
81 }
82
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.
86  *
87  * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
88  */
89 static int prism2_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
90 {
91         struct prism2_wep_data *wep = priv;
92         u32 klen, len;
93         u8 key[WEP_KEY_LEN + 3];
94         u8 *pos;
95         struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
96                                     MAX_DEV_ADDR_SIZE);
97         u32 crc;
98         u8 *icv;
99         struct scatterlist sg;
100         int err;
101
102         if (skb_headroom(skb) < 4 || skb_tailroom(skb) < 4 ||
103             skb->len < hdr_len){
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);
106                 return -1;
107         }
108         len = skb->len - hdr_len;
109         pos = skb_push(skb, 4);
110         memmove(pos, pos + 4, hdr_len);
111         pos += hdr_len;
112
113         klen = 3 + wep->key_len;
114
115         wep->iv++;
116
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.
120          */
121         if ((wep->iv & 0xff00) == 0xff00) {
122                 u8 B = (wep->iv >> 16) & 0xff;
123
124                 if (B >= 3 && B < klen)
125                         wep->iv += 0x0100;
126         }
127
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;
133
134         /* Copy rest of the WEP key (the secret part) */
135         memcpy(key + 3, wep->key, wep->key_len);
136
137         if (!tcb_desc->bHwSec) {
138                 SKCIPHER_REQUEST_ON_STACK(req, wep->tx_tfm);
139
140                 /* Append little-endian CRC32 and encrypt it to produce ICV */
141                 crc = ~crc32_le(~0, pos, len);
142                 icv = skb_put(skb, 4);
143                 icv[0] = crc;
144                 icv[1] = crc >> 8;
145                 icv[2] = crc >> 16;
146                 icv[3] = crc >> 24;
147
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);
155                 return err;
156         }
157
158         return 0;
159 }
160
161
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.
165  *
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.
168  */
169 static int prism2_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
170 {
171         struct prism2_wep_data *wep = priv;
172         u32  klen, plen;
173         u8 key[WEP_KEY_LEN + 3];
174         u8 keyidx, *pos;
175         struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
176                                     MAX_DEV_ADDR_SIZE);
177         u32 crc;
178         u8 icv[4];
179         struct scatterlist sg;
180         int err;
181
182         if (skb->len < hdr_len + 8)
183                 return -1;
184
185         pos = skb->data + hdr_len;
186         key[0] = *pos++;
187         key[1] = *pos++;
188         key[2] = *pos++;
189         keyidx = *pos++ >> 6;
190         if (keyidx != wep->key_idx)
191                 return -1;
192
193         klen = 3 + wep->key_len;
194
195         /* Copy rest of the WEP key (the secret part) */
196         memcpy(key + 3, wep->key, wep->key_len);
197
198         /* Apply RC4 to data and compute CRC32 over decrypted data */
199         plen = skb->len - hdr_len - 8;
200
201         if (!tcb_desc->bHwSec) {
202                 SKCIPHER_REQUEST_ON_STACK(req, wep->rx_tfm);
203
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);
211                 if (err)
212                         return -7;
213                 crc = ~crc32_le(~0, pos, plen);
214                 icv[0] = crc;
215                 icv[1] = crc >> 8;
216                 icv[2] = crc >> 16;
217                 icv[3] = crc >> 24;
218                 if (memcmp(icv, pos + plen, 4) != 0) {
219                         /* ICV mismatch - drop frame */
220                         return -2;
221                 }
222         }
223         /* Remove IV and ICV */
224         memmove(skb->data + 4, skb->data, hdr_len);
225         skb_pull(skb, 4);
226         skb_trim(skb, skb->len - 4);
227
228         return 0;
229 }
230
231
232 static int prism2_wep_set_key(void *key, int len, u8 *seq, void *priv)
233 {
234         struct prism2_wep_data *wep = priv;
235
236         if (len < 0 || len > WEP_KEY_LEN)
237                 return -1;
238
239         memcpy(wep->key, key, len);
240         wep->key_len = len;
241
242         return 0;
243 }
244
245
246 static int prism2_wep_get_key(void *key, int len, u8 *seq, void *priv)
247 {
248         struct prism2_wep_data *wep = priv;
249
250         if (len < wep->key_len)
251                 return -1;
252
253         memcpy(key, wep->key, wep->key_len);
254
255         return wep->key_len;
256 }
257
258
259 static void prism2_wep_print_stats(struct seq_file *m, void *priv)
260 {
261         struct prism2_wep_data *wep = priv;
262
263         seq_printf(m, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
264 }
265
266 static struct lib80211_crypto_ops rtllib_crypt_wep = {
267         .name                   = "R-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,
280 };
281
282
283 static int __init rtllib_crypto_wep_init(void)
284 {
285         return lib80211_register_crypto_ops(&rtllib_crypt_wep);
286 }
287
288
289 static void __exit rtllib_crypto_wep_exit(void)
290 {
291         lib80211_unregister_crypto_ops(&rtllib_crypt_wep);
292 }
293
294 module_init(rtllib_crypto_wep_init);
295 module_exit(rtllib_crypto_wep_exit);
296
297 MODULE_LICENSE("GPL");