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/arc4.h>
9 #include <linux/fips.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/random.h>
14 #include <linux/skbuff.h>
15 #include <linux/string.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 arc4_ctx rx_ctx_arc4;
27 struct arc4_ctx tx_ctx_arc4;
31 static void *prism2_wep_init(int keyidx)
33 struct prism2_wep_data *priv;
38 priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
41 priv->key_idx = keyidx;
43 /* start WEP IV from a random value */
44 get_random_bytes(&priv->iv, 4);
50 static void prism2_wep_deinit(void *priv)
52 kfree_sensitive(priv);
55 /* Perform WEP encryption on given skb that has at least 4 bytes of headroom
56 * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
57 * so the payload length increases with 8 bytes.
59 * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
61 static int prism2_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
63 struct prism2_wep_data *wep = priv;
65 u8 key[WEP_KEY_LEN + 3];
67 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
72 if (skb_headroom(skb) < 4 || skb_tailroom(skb) < 4 ||
74 pr_err("Error!!! headroom=%d tailroom=%d skblen=%d hdr_len=%d\n",
75 skb_headroom(skb), skb_tailroom(skb), skb->len, hdr_len);
78 len = skb->len - hdr_len;
79 pos = skb_push(skb, 4);
80 memmove(pos, pos + 4, hdr_len);
83 klen = 3 + wep->key_len;
87 /* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
88 * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
89 * can be used to speedup attacks, so avoid using them.
91 if ((wep->iv & 0xff00) == 0xff00) {
92 u8 B = (wep->iv >> 16) & 0xff;
94 if (B >= 3 && B < klen)
98 /* Prepend 24-bit IV to RC4 key and TX frame */
99 *pos++ = key[0] = (wep->iv >> 16) & 0xff;
100 *pos++ = key[1] = (wep->iv >> 8) & 0xff;
101 *pos++ = key[2] = wep->iv & 0xff;
102 *pos++ = wep->key_idx << 6;
104 /* Copy rest of the WEP key (the secret part) */
105 memcpy(key + 3, wep->key, wep->key_len);
107 if (!tcb_desc->bHwSec) {
108 /* Append little-endian CRC32 and encrypt it to produce ICV */
109 crc = ~crc32_le(~0, pos, len);
110 icv = skb_put(skb, 4);
116 arc4_setkey(&wep->tx_ctx_arc4, key, klen);
117 arc4_crypt(&wep->tx_ctx_arc4, pos, pos, len + 4);
124 /* Perform WEP decryption on given struct buffer. Buffer includes whole WEP
125 * part of the frame: IV (4 bytes), encrypted payload (including SNAP header),
126 * ICV (4 bytes). len includes both IV and ICV.
128 * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
129 * failure. If frame is OK, IV and ICV will be removed.
131 static int prism2_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
133 struct prism2_wep_data *wep = priv;
135 u8 key[WEP_KEY_LEN + 3];
137 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
142 if (skb->len < hdr_len + 8)
145 pos = skb->data + hdr_len;
149 keyidx = *pos++ >> 6;
150 if (keyidx != wep->key_idx)
153 klen = 3 + wep->key_len;
155 /* Copy rest of the WEP key (the secret part) */
156 memcpy(key + 3, wep->key, wep->key_len);
158 /* Apply RC4 to data and compute CRC32 over decrypted data */
159 plen = skb->len - hdr_len - 8;
161 if (!tcb_desc->bHwSec) {
162 arc4_setkey(&wep->rx_ctx_arc4, key, klen);
163 arc4_crypt(&wep->rx_ctx_arc4, pos, pos, plen + 4);
165 crc = ~crc32_le(~0, pos, plen);
170 if (memcmp(icv, pos + plen, 4) != 0) {
171 /* ICV mismatch - drop frame */
175 /* Remove IV and ICV */
176 memmove(skb->data + 4, skb->data, hdr_len);
178 skb_trim(skb, skb->len - 4);
184 static int prism2_wep_set_key(void *key, int len, u8 *seq, void *priv)
186 struct prism2_wep_data *wep = priv;
188 if (len < 0 || len > WEP_KEY_LEN)
191 memcpy(wep->key, key, len);
198 static int prism2_wep_get_key(void *key, int len, u8 *seq, void *priv)
200 struct prism2_wep_data *wep = priv;
202 if (len < wep->key_len)
205 memcpy(key, wep->key, wep->key_len);
211 static void prism2_wep_print_stats(struct seq_file *m, void *priv)
213 struct prism2_wep_data *wep = priv;
215 seq_printf(m, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
218 static struct lib80211_crypto_ops rtllib_crypt_wep = {
220 .init = prism2_wep_init,
221 .deinit = prism2_wep_deinit,
222 .encrypt_mpdu = prism2_wep_encrypt,
223 .decrypt_mpdu = prism2_wep_decrypt,
224 .encrypt_msdu = NULL,
225 .decrypt_msdu = NULL,
226 .set_key = prism2_wep_set_key,
227 .get_key = prism2_wep_get_key,
228 .print_stats = prism2_wep_print_stats,
229 .extra_mpdu_prefix_len = 4, /* IV */
230 .extra_mpdu_postfix_len = 4, /* ICV */
231 .owner = THIS_MODULE,
235 static int __init rtllib_crypto_wep_init(void)
237 return lib80211_register_crypto_ops(&rtllib_crypt_wep);
241 static void __exit rtllib_crypto_wep_exit(void)
243 lib80211_unregister_crypto_ops(&rtllib_crypt_wep);
246 module_init(rtllib_crypto_wep_init);
247 module_exit(rtllib_crypto_wep_exit);
249 MODULE_LICENSE("GPL");