1 // SPDX-License-Identifier: GPL-2.0
3 * Host AP crypt: host-based WEP encryption implementation for Host AP driver
5 * Copyright (c) 2002-2004, Jouni Malinen <jkmaline@cc.hut.fi>
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>
17 #include <linux/scatterlist.h>
18 #include <linux/crc32.h>
20 struct prism2_wep_data {
22 #define WEP_KEY_LEN 13
23 u8 key[WEP_KEY_LEN + 1];
26 struct crypto_sync_skcipher *tx_tfm;
27 struct crypto_sync_skcipher *rx_tfm;
31 static void *prism2_wep_init(int keyidx)
33 struct prism2_wep_data *priv;
35 priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
38 priv->key_idx = keyidx;
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");
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");
53 /* start WEP IV from a random value */
54 get_random_bytes(&priv->iv, 4);
60 crypto_free_sync_skcipher(priv->tx_tfm);
61 crypto_free_sync_skcipher(priv->rx_tfm);
68 static void prism2_wep_deinit(void *priv)
70 struct prism2_wep_data *_priv = priv;
73 crypto_free_sync_skcipher(_priv->tx_tfm);
74 crypto_free_sync_skcipher(_priv->rx_tfm);
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.
83 * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
85 static int prism2_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
87 struct prism2_wep_data *wep = priv;
89 u8 key[WEP_KEY_LEN + 3];
91 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
95 struct scatterlist sg;
98 if (skb_headroom(skb) < 4 || skb_tailroom(skb) < 4 ||
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);
104 len = skb->len - hdr_len;
105 pos = skb_push(skb, 4);
106 memmove(pos, pos + 4, hdr_len);
109 klen = 3 + wep->key_len;
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.
117 if ((wep->iv & 0xff00) == 0xff00) {
118 u8 B = (wep->iv >> 16) & 0xff;
120 if (B >= 3 && B < klen)
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;
130 /* Copy rest of the WEP key (the secret part) */
131 memcpy(key + 3, wep->key, wep->key_len);
133 if (!tcb_desc->bHwSec) {
134 SYNC_SKCIPHER_REQUEST_ON_STACK(req, wep->tx_tfm);
136 /* Append little-endian CRC32 and encrypt it to produce ICV */
137 crc = ~crc32_le(~0, pos, len);
138 icv = skb_put(skb, 4);
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);
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.
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.
165 static int prism2_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
167 struct prism2_wep_data *wep = priv;
169 u8 key[WEP_KEY_LEN + 3];
171 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
175 struct scatterlist sg;
178 if (skb->len < hdr_len + 8)
181 pos = skb->data + hdr_len;
185 keyidx = *pos++ >> 6;
186 if (keyidx != wep->key_idx)
189 klen = 3 + wep->key_len;
191 /* Copy rest of the WEP key (the secret part) */
192 memcpy(key + 3, wep->key, wep->key_len);
194 /* Apply RC4 to data and compute CRC32 over decrypted data */
195 plen = skb->len - hdr_len - 8;
197 if (!tcb_desc->bHwSec) {
198 SYNC_SKCIPHER_REQUEST_ON_STACK(req, wep->rx_tfm);
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);
209 crc = ~crc32_le(~0, pos, plen);
214 if (memcmp(icv, pos + plen, 4) != 0) {
215 /* ICV mismatch - drop frame */
219 /* Remove IV and ICV */
220 memmove(skb->data + 4, skb->data, hdr_len);
222 skb_trim(skb, skb->len - 4);
228 static int prism2_wep_set_key(void *key, int len, u8 *seq, void *priv)
230 struct prism2_wep_data *wep = priv;
232 if (len < 0 || len > WEP_KEY_LEN)
235 memcpy(wep->key, key, len);
242 static int prism2_wep_get_key(void *key, int len, u8 *seq, void *priv)
244 struct prism2_wep_data *wep = priv;
246 if (len < wep->key_len)
249 memcpy(key, wep->key, wep->key_len);
255 static void prism2_wep_print_stats(struct seq_file *m, void *priv)
257 struct prism2_wep_data *wep = priv;
259 seq_printf(m, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
262 static struct lib80211_crypto_ops rtllib_crypt_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,
279 static int __init rtllib_crypto_wep_init(void)
281 return lib80211_register_crypto_ops(&rtllib_crypt_wep);
285 static void __exit rtllib_crypto_wep_exit(void)
287 lib80211_unregister_crypto_ops(&rtllib_crypt_wep);
290 module_init(rtllib_crypto_wep_init);
291 module_exit(rtllib_crypto_wep_exit);
293 MODULE_LICENSE("GPL");