1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Host AP crypt: host-based WEP encryption implementation for Host AP driver
4  *
5  * Copyright (c) 2002-2004, Jouni Malinen <jkmaline@cc.hut.fi>
6  */
7 
8 #include <crypto/skcipher.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/random.h>
13 #include <linux/skbuff.h>
14 #include <linux/string.h>
15 #include "rtllib.h"
16 
17 #include <linux/scatterlist.h>
18 #include <linux/crc32.h>
19 
20 struct prism2_wep_data {
21 	u32 iv;
22 #define WEP_KEY_LEN 13
23 	u8 key[WEP_KEY_LEN + 1];
24 	u8 key_len;
25 	u8 key_idx;
26 	struct crypto_sync_skcipher *tx_tfm;
27 	struct crypto_sync_skcipher *rx_tfm;
28 };
29 
30 
31 static void *prism2_wep_init(int keyidx)
32 {
33 	struct prism2_wep_data *priv;
34 
35 	priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
36 	if (priv == NULL)
37 		goto fail;
38 	priv->key_idx = keyidx;
39 
40 	priv->tx_tfm = crypto_alloc_sync_skcipher("ecb(arc4)", 0, 0);
41 	if (IS_ERR(priv->tx_tfm)) {
42 		pr_debug("rtllib_crypt_wep: could not allocate crypto API arc4\n");
43 		priv->tx_tfm = NULL;
44 		goto fail;
45 	}
46 	priv->rx_tfm = crypto_alloc_sync_skcipher("ecb(arc4)", 0, 0);
47 	if (IS_ERR(priv->rx_tfm)) {
48 		pr_debug("rtllib_crypt_wep: could not allocate crypto API arc4\n");
49 		priv->rx_tfm = NULL;
50 		goto fail;
51 	}
52 
53 	/* start WEP IV from a random value */
54 	get_random_bytes(&priv->iv, 4);
55 
56 	return priv;
57 
58 fail:
59 	if (priv) {
60 		crypto_free_sync_skcipher(priv->tx_tfm);
61 		crypto_free_sync_skcipher(priv->rx_tfm);
62 		kfree(priv);
63 	}
64 	return NULL;
65 }
66 
67 
68 static void prism2_wep_deinit(void *priv)
69 {
70 	struct prism2_wep_data *_priv = priv;
71 
72 	if (_priv) {
73 		crypto_free_sync_skcipher(_priv->tx_tfm);
74 		crypto_free_sync_skcipher(_priv->rx_tfm);
75 	}
76 	kfree(priv);
77 }
78 
79 /* Perform WEP encryption on given skb that has at least 4 bytes of headroom
80  * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
81  * so the payload length increases with 8 bytes.
82  *
83  * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
84  */
85 static int prism2_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
86 {
87 	struct prism2_wep_data *wep = priv;
88 	u32 klen, len;
89 	u8 key[WEP_KEY_LEN + 3];
90 	u8 *pos;
91 	struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
92 				    MAX_DEV_ADDR_SIZE);
93 	u32 crc;
94 	u8 *icv;
95 	struct scatterlist sg;
96 	int err;
97 
98 	if (skb_headroom(skb) < 4 || skb_tailroom(skb) < 4 ||
99 	    skb->len < hdr_len){
100 		pr_err("Error!!! headroom=%d tailroom=%d skblen=%d hdr_len=%d\n",
101 		       skb_headroom(skb), skb_tailroom(skb), skb->len, hdr_len);
102 		return -1;
103 	}
104 	len = skb->len - hdr_len;
105 	pos = skb_push(skb, 4);
106 	memmove(pos, pos + 4, hdr_len);
107 	pos += hdr_len;
108 
109 	klen = 3 + wep->key_len;
110 
111 	wep->iv++;
112 
113 	/* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
114 	 * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
115 	 * can be used to speedup attacks, so avoid using them.
116 	 */
117 	if ((wep->iv & 0xff00) == 0xff00) {
118 		u8 B = (wep->iv >> 16) & 0xff;
119 
120 		if (B >= 3 && B < klen)
121 			wep->iv += 0x0100;
122 	}
123 
124 	/* Prepend 24-bit IV to RC4 key and TX frame */
125 	*pos++ = key[0] = (wep->iv >> 16) & 0xff;
126 	*pos++ = key[1] = (wep->iv >> 8) & 0xff;
127 	*pos++ = key[2] = wep->iv & 0xff;
128 	*pos++ = wep->key_idx << 6;
129 
130 	/* Copy rest of the WEP key (the secret part) */
131 	memcpy(key + 3, wep->key, wep->key_len);
132 
133 	if (!tcb_desc->bHwSec) {
134 		SYNC_SKCIPHER_REQUEST_ON_STACK(req, wep->tx_tfm);
135 
136 		/* Append little-endian CRC32 and encrypt it to produce ICV */
137 		crc = ~crc32_le(~0, pos, len);
138 		icv = skb_put(skb, 4);
139 		icv[0] = crc;
140 		icv[1] = crc >> 8;
141 		icv[2] = crc >> 16;
142 		icv[3] = crc >> 24;
143 
144 		sg_init_one(&sg, pos, len+4);
145 		crypto_sync_skcipher_setkey(wep->tx_tfm, key, klen);
146 		skcipher_request_set_sync_tfm(req, wep->tx_tfm);
147 		skcipher_request_set_callback(req, 0, NULL, NULL);
148 		skcipher_request_set_crypt(req, &sg, &sg, len + 4, NULL);
149 		err = crypto_skcipher_encrypt(req);
150 		skcipher_request_zero(req);
151 		return err;
152 	}
153 
154 	return 0;
155 }
156 
157 
158 /* Perform WEP decryption on given struct buffer. Buffer includes whole WEP
159  * part of the frame: IV (4 bytes), encrypted payload (including SNAP header),
160  * ICV (4 bytes). len includes both IV and ICV.
161  *
162  * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
163  * failure. If frame is OK, IV and ICV will be removed.
164  */
165 static int prism2_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
166 {
167 	struct prism2_wep_data *wep = priv;
168 	u32  klen, plen;
169 	u8 key[WEP_KEY_LEN + 3];
170 	u8 keyidx, *pos;
171 	struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
172 				    MAX_DEV_ADDR_SIZE);
173 	u32 crc;
174 	u8 icv[4];
175 	struct scatterlist sg;
176 	int err;
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 	if (!tcb_desc->bHwSec) {
198 		SYNC_SKCIPHER_REQUEST_ON_STACK(req, wep->rx_tfm);
199 
200 		sg_init_one(&sg, pos, plen+4);
201 		crypto_sync_skcipher_setkey(wep->rx_tfm, key, klen);
202 		skcipher_request_set_sync_tfm(req, wep->rx_tfm);
203 		skcipher_request_set_callback(req, 0, NULL, NULL);
204 		skcipher_request_set_crypt(req, &sg, &sg, plen + 4, NULL);
205 		err = crypto_skcipher_decrypt(req);
206 		skcipher_request_zero(req);
207 		if (err)
208 			return -7;
209 		crc = ~crc32_le(~0, pos, plen);
210 		icv[0] = crc;
211 		icv[1] = crc >> 8;
212 		icv[2] = crc >> 16;
213 		icv[3] = crc >> 24;
214 		if (memcmp(icv, pos + plen, 4) != 0) {
215 			/* ICV mismatch - drop frame */
216 			return -2;
217 		}
218 	}
219 	/* Remove IV and ICV */
220 	memmove(skb->data + 4, skb->data, hdr_len);
221 	skb_pull(skb, 4);
222 	skb_trim(skb, skb->len - 4);
223 
224 	return 0;
225 }
226 
227 
228 static int prism2_wep_set_key(void *key, int len, u8 *seq, void *priv)
229 {
230 	struct prism2_wep_data *wep = priv;
231 
232 	if (len < 0 || len > WEP_KEY_LEN)
233 		return -1;
234 
235 	memcpy(wep->key, key, len);
236 	wep->key_len = len;
237 
238 	return 0;
239 }
240 
241 
242 static int prism2_wep_get_key(void *key, int len, u8 *seq, void *priv)
243 {
244 	struct prism2_wep_data *wep = priv;
245 
246 	if (len < wep->key_len)
247 		return -1;
248 
249 	memcpy(key, wep->key, wep->key_len);
250 
251 	return wep->key_len;
252 }
253 
254 
255 static void prism2_wep_print_stats(struct seq_file *m, void *priv)
256 {
257 	struct prism2_wep_data *wep = priv;
258 
259 	seq_printf(m, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
260 }
261 
262 static struct lib80211_crypto_ops rtllib_crypt_wep = {
263 	.name			= "R-WEP",
264 	.init			= prism2_wep_init,
265 	.deinit			= prism2_wep_deinit,
266 	.encrypt_mpdu		= prism2_wep_encrypt,
267 	.decrypt_mpdu		= prism2_wep_decrypt,
268 	.encrypt_msdu		= NULL,
269 	.decrypt_msdu		= NULL,
270 	.set_key		= prism2_wep_set_key,
271 	.get_key		= prism2_wep_get_key,
272 	.print_stats		= prism2_wep_print_stats,
273 	.extra_mpdu_prefix_len  = 4,	/* IV */
274 	.extra_mpdu_postfix_len = 4,	/* ICV */
275 	.owner			= THIS_MODULE,
276 };
277 
278 
279 static int __init rtllib_crypto_wep_init(void)
280 {
281 	return lib80211_register_crypto_ops(&rtllib_crypt_wep);
282 }
283 
284 
285 static void __exit rtllib_crypto_wep_exit(void)
286 {
287 	lib80211_unregister_crypto_ops(&rtllib_crypt_wep);
288 }
289 
290 module_init(rtllib_crypto_wep_init);
291 module_exit(rtllib_crypto_wep_exit);
292 
293 MODULE_LICENSE("GPL");
294