Blame view
net/mac80211/wep.c
8.31 KB
d2912cb15
|
1 |
// SPDX-License-Identifier: GPL-2.0-only |
f0706e828
|
2 3 4 5 |
/* * Software WEP encryption implementation * Copyright 2002, Jouni Malinen <jkmaline@cc.hut.fi> * Copyright 2003, Instant802 Networks, Inc. |
f0706e828
|
6 7 8 9 10 11 12 13 14 15 |
*/ #include <linux/netdevice.h> #include <linux/types.h> #include <linux/random.h> #include <linux/compiler.h> #include <linux/crc32.h> #include <linux/crypto.h> #include <linux/err.h> #include <linux/mm.h> |
117636092
|
16 |
#include <linux/scatterlist.h> |
5a0e3ad6a
|
17 |
#include <linux/slab.h> |
860c6e6a1
|
18 |
#include <asm/unaligned.h> |
f0706e828
|
19 20 21 22 23 24 25 26 27 |
#include <net/mac80211.h> #include "ieee80211_i.h" #include "wep.h" int ieee80211_wep_init(struct ieee80211_local *local) { /* start WEP IV from a random value */ |
4325f6caa
|
28 |
get_random_bytes(&local->wep_iv, IEEE80211_WEP_IV_LEN); |
f0706e828
|
29 |
|
f0706e828
|
30 31 |
return 0; } |
c6a1fa12d
|
32 |
static inline bool ieee80211_wep_weak_iv(u32 iv, int keylen) |
f0706e828
|
33 |
{ |
c6a1fa12d
|
34 35 |
/* * Fluhrer, Mantin, and Shamir have reported weaknesses in the |
f0706e828
|
36 |
* key scheduling algorithm of RC4. At least IVs (KeyByte + 3, |
c6a1fa12d
|
37 38 |
* 0xff, N) can be used to speedup attacks, so avoid using them. */ |
f0706e828
|
39 40 41 |
if ((iv & 0xff00) == 0xff00) { u8 B = (iv >> 16) & 0xff; if (B >= 3 && B < 3 + keylen) |
c6a1fa12d
|
42 |
return true; |
f0706e828
|
43 |
} |
c6a1fa12d
|
44 |
return false; |
f0706e828
|
45 |
} |
4f0d18e26
|
46 |
static void ieee80211_wep_get_iv(struct ieee80211_local *local, |
c9cf01226
|
47 |
int keylen, int keyidx, u8 *iv) |
f0706e828
|
48 49 |
{ local->wep_iv++; |
c9cf01226
|
50 |
if (ieee80211_wep_weak_iv(local->wep_iv, keylen)) |
f0706e828
|
51 52 53 54 55 56 57 58 |
local->wep_iv += 0x0100; if (!iv) return; *iv++ = (local->wep_iv >> 16) & 0xff; *iv++ = (local->wep_iv >> 8) & 0xff; *iv++ = local->wep_iv & 0xff; |
c9cf01226
|
59 |
*iv++ = keyidx << 6; |
f0706e828
|
60 |
} |
6a22a59d4
|
61 62 |
static u8 *ieee80211_wep_add_iv(struct ieee80211_local *local, struct sk_buff *skb, |
c9cf01226
|
63 |
int keylen, int keyidx) |
f0706e828
|
64 |
{ |
70217d7f8
|
65 |
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; |
ee70108fa
|
66 |
struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
70217d7f8
|
67 |
unsigned int hdrlen; |
f0706e828
|
68 |
u8 *newhdr; |
70217d7f8
|
69 |
hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED); |
f0706e828
|
70 |
|
47b4e1fc4
|
71 |
if (WARN_ON(skb_headroom(skb) < IEEE80211_WEP_IV_LEN)) |
23c0752a2
|
72 |
return NULL; |
f0706e828
|
73 |
|
70217d7f8
|
74 |
hdrlen = ieee80211_hdrlen(hdr->frame_control); |
4325f6caa
|
75 76 |
newhdr = skb_push(skb, IEEE80211_WEP_IV_LEN); memmove(newhdr, newhdr + IEEE80211_WEP_IV_LEN, hdrlen); |
ee70108fa
|
77 78 79 80 81 |
/* the HW only needs room for the IV, but not the actual IV */ if (info->control.hw_key && (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) return newhdr + hdrlen; |
c9cf01226
|
82 |
ieee80211_wep_get_iv(local, keylen, keyidx, newhdr + hdrlen); |
f0706e828
|
83 84 |
return newhdr + hdrlen; } |
4f0d18e26
|
85 86 87 |
static void ieee80211_wep_remove_iv(struct ieee80211_local *local, struct sk_buff *skb, struct ieee80211_key *key) |
f0706e828
|
88 |
{ |
70217d7f8
|
89 90 |
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; unsigned int hdrlen; |
f0706e828
|
91 |
|
70217d7f8
|
92 |
hdrlen = ieee80211_hdrlen(hdr->frame_control); |
4325f6caa
|
93 94 |
memmove(skb->data + IEEE80211_WEP_IV_LEN, skb->data, hdrlen); skb_pull(skb, IEEE80211_WEP_IV_LEN); |
f0706e828
|
95 96 97 98 99 100 |
} /* Perform WEP encryption using given key. data buffer must have tailroom * for 4-byte ICV. data_len must not include this ICV. Note: this function * does _not_ add IV. data = RC4(data | CRC32(data)) */ |
5fdb37357
|
101 |
int ieee80211_wep_encrypt_data(struct arc4_ctx *ctx, u8 *rc4key, |
3473187d2
|
102 |
size_t klen, u8 *data, size_t data_len) |
f0706e828
|
103 |
{ |
860c6e6a1
|
104 |
__le32 icv; |
3473187d2
|
105 |
|
860c6e6a1
|
106 107 |
icv = cpu_to_le32(~crc32_le(~0, data, data_len)); put_unaligned(icv, (__le32 *)(data + data_len)); |
f0706e828
|
108 |
|
5fdb37357
|
109 110 111 |
arc4_setkey(ctx, rc4key, klen); arc4_crypt(ctx, data, data, data_len + IEEE80211_WEP_ICV_LEN); memzero_explicit(ctx, sizeof(*ctx)); |
3473187d2
|
112 113 |
return 0; |
f0706e828
|
114 115 116 117 118 119 120 121 122 123 |
} /* Perform WEP encryption on given skb. 4 bytes of extra space (IV) in the * beginning of the buffer 4 bytes of extra space (ICV) in the end of the * buffer will be added. Both IV and ICV will be transmitted, so the * payload length increases with 8 bytes. * * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data)) */ |
fffd0934b
|
124 125 126 |
int ieee80211_wep_encrypt(struct ieee80211_local *local, struct sk_buff *skb, const u8 *key, int keylen, int keyidx) |
f0706e828
|
127 |
{ |
c9cf01226
|
128 |
u8 *iv; |
f0706e828
|
129 |
size_t len; |
c9cf01226
|
130 |
u8 rc4key[3 + WLAN_KEY_LEN_WEP104]; |
f0706e828
|
131 |
|
47b4e1fc4
|
132 133 |
if (WARN_ON(skb_tailroom(skb) < IEEE80211_WEP_ICV_LEN)) return -1; |
c9cf01226
|
134 135 |
iv = ieee80211_wep_add_iv(local, skb, keylen, keyidx); if (!iv) |
f0706e828
|
136 |
return -1; |
f0706e828
|
137 |
|
4325f6caa
|
138 |
len = skb->len - (iv + IEEE80211_WEP_IV_LEN - skb->data); |
f0706e828
|
139 140 141 142 143 |
/* Prepend 24-bit IV to RC4 key */ memcpy(rc4key, iv, 3); /* Copy rest of the WEP key (the secret part) */ |
c9cf01226
|
144 |
memcpy(rc4key + 3, key, keylen); |
f0706e828
|
145 146 |
/* Add room for ICV */ |
4325f6caa
|
147 |
skb_put(skb, IEEE80211_WEP_ICV_LEN); |
f0706e828
|
148 |
|
5fdb37357
|
149 |
return ieee80211_wep_encrypt_data(&local->wep_tx_ctx, rc4key, keylen + 3, |
4325f6caa
|
150 |
iv + IEEE80211_WEP_IV_LEN, len); |
f0706e828
|
151 152 153 154 155 156 |
} /* Perform WEP decryption using given key. data buffer includes encrypted * payload, including 4-byte ICV, but _not_ IV. data_len must not include ICV. * Return 0 on success and -1 on ICV mismatch. */ |
5fdb37357
|
157 |
int ieee80211_wep_decrypt_data(struct arc4_ctx *ctx, u8 *rc4key, |
f0706e828
|
158 159 |
size_t klen, u8 *data, size_t data_len) { |
f0706e828
|
160 |
__le32 crc; |
3473187d2
|
161 |
|
5fdb37357
|
162 163 164 |
arc4_setkey(ctx, rc4key, klen); arc4_crypt(ctx, data, data, data_len + IEEE80211_WEP_ICV_LEN); memzero_explicit(ctx, sizeof(*ctx)); |
f0706e828
|
165 166 |
crc = cpu_to_le32(~crc32_le(~0, data, data_len)); |
4325f6caa
|
167 |
if (memcmp(&crc, data + data_len, IEEE80211_WEP_ICV_LEN) != 0) |
f0706e828
|
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
/* ICV mismatch */ return -1; return 0; } /* Perform WEP decryption on given skb. Buffer includes whole WEP part of * the frame: IV (4 bytes), encrypted payload (including SNAP header), * ICV (4 bytes). skb->len includes both IV and ICV. * * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on * failure. If frame is OK, IV and ICV will be removed, i.e., decrypted payload * is moved to the beginning of the skb and skb length will be reduced. */ |
c9cf01226
|
183 184 185 |
static int ieee80211_wep_decrypt(struct ieee80211_local *local, struct sk_buff *skb, struct ieee80211_key *key) |
f0706e828
|
186 187 |
{ u32 klen; |
730bd83b0
|
188 |
u8 rc4key[3 + WLAN_KEY_LEN_WEP104]; |
f0706e828
|
189 |
u8 keyidx; |
70217d7f8
|
190 191 |
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; unsigned int hdrlen; |
f0706e828
|
192 193 |
size_t len; int ret = 0; |
70217d7f8
|
194 |
if (!ieee80211_has_protected(hdr->frame_control)) |
f0706e828
|
195 |
return -1; |
70217d7f8
|
196 |
hdrlen = ieee80211_hdrlen(hdr->frame_control); |
4325f6caa
|
197 |
if (skb->len < hdrlen + IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN) |
f0706e828
|
198 |
return -1; |
4325f6caa
|
199 |
len = skb->len - hdrlen - IEEE80211_WEP_IV_LEN - IEEE80211_WEP_ICV_LEN; |
f0706e828
|
200 201 |
keyidx = skb->data[hdrlen + 3] >> 6; |
97359d123
|
202 |
if (!key || keyidx != key->conf.keyidx) |
f0706e828
|
203 |
return -1; |
8f20fc249
|
204 |
klen = 3 + key->conf.keylen; |
f0706e828
|
205 |
|
f0706e828
|
206 207 208 209 |
/* Prepend 24-bit IV to RC4 key */ memcpy(rc4key, skb->data + hdrlen, 3); /* Copy rest of the WEP key (the secret part) */ |
8f20fc249
|
210 |
memcpy(rc4key + 3, key->conf.key, key->conf.keylen); |
f0706e828
|
211 |
|
5fdb37357
|
212 |
if (ieee80211_wep_decrypt_data(&local->wep_rx_ctx, rc4key, klen, |
4325f6caa
|
213 214 |
skb->data + hdrlen + IEEE80211_WEP_IV_LEN, len)) |
f0706e828
|
215 |
ret = -1; |
f0706e828
|
216 |
|
f0706e828
|
217 |
/* Trim ICV */ |
4325f6caa
|
218 |
skb_trim(skb, skb->len - IEEE80211_WEP_ICV_LEN); |
f0706e828
|
219 220 |
/* Remove IV */ |
4325f6caa
|
221 222 |
memmove(skb->data + IEEE80211_WEP_IV_LEN, skb->data, hdrlen); skb_pull(skb, IEEE80211_WEP_IV_LEN); |
f0706e828
|
223 224 225 |
return ret; } |
9ae54c846
|
226 |
ieee80211_rx_result |
5cf121c3c
|
227 |
ieee80211_crypto_wep_decrypt(struct ieee80211_rx_data *rx) |
4f0d18e26
|
228 |
{ |
eb9fb5b88
|
229 230 231 |
struct sk_buff *skb = rx->skb; struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; |
a82869118
|
232 |
__le16 fc = hdr->frame_control; |
358c8d9d3
|
233 |
|
a82869118
|
234 |
if (!ieee80211_is_data(fc) && !ieee80211_is_auth(fc)) |
9ae54c846
|
235 |
return RX_CONTINUE; |
4f0d18e26
|
236 |
|
eb9fb5b88
|
237 |
if (!(status->flag & RX_FLAG_DECRYPTED)) { |
a82869118
|
238 239 |
if (skb_linearize(rx->skb)) return RX_DROP_UNUSABLE; |
f4ea83dd7
|
240 |
if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) |
e4c26add8
|
241 |
return RX_DROP_UNUSABLE; |
eb9fb5b88
|
242 |
} else if (!(status->flag & RX_FLAG_IV_STRIPPED)) { |
4325f6caa
|
243 244 |
if (!pskb_may_pull(rx->skb, ieee80211_hdrlen(fc) + IEEE80211_WEP_IV_LEN)) |
a82869118
|
245 |
return RX_DROP_UNUSABLE; |
4f0d18e26
|
246 247 |
ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key); /* remove ICV */ |
cef0acd4d
|
248 249 |
if (!(status->flag & RX_FLAG_ICV_STRIPPED) && pskb_trim(rx->skb, rx->skb->len - IEEE80211_WEP_ICV_LEN)) |
a82869118
|
250 |
return RX_DROP_UNUSABLE; |
4f0d18e26
|
251 |
} |
9ae54c846
|
252 |
return RX_CONTINUE; |
4f0d18e26
|
253 |
} |
6a22a59d4
|
254 |
|
5cf121c3c
|
255 |
static int wep_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb) |
6a22a59d4
|
256 |
{ |
e039fa4a4
|
257 |
struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
ee70108fa
|
258 |
struct ieee80211_key_conf *hw_key = info->control.hw_key; |
e039fa4a4
|
259 |
|
ee70108fa
|
260 |
if (!hw_key) { |
c9cf01226
|
261 262 263 |
if (ieee80211_wep_encrypt(tx->local, skb, tx->key->conf.key, tx->key->conf.keylen, tx->key->conf.keyidx)) |
6a22a59d4
|
264 |
return -1; |
ee70108fa
|
265 266 |
} else if ((hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) || (hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) { |
813d76694
|
267 268 269 270 271 |
if (!ieee80211_wep_add_iv(tx->local, skb, tx->key->conf.keylen, tx->key->conf.keyidx)) return -1; } |
6a22a59d4
|
272 273 |
return 0; } |
9ae54c846
|
274 |
ieee80211_tx_result |
5cf121c3c
|
275 |
ieee80211_crypto_wep_encrypt(struct ieee80211_tx_data *tx) |
6a22a59d4
|
276 |
{ |
2de8e0d99
|
277 |
struct sk_buff *skb; |
c6a1fa12d
|
278 |
|
5cf121c3c
|
279 |
ieee80211_tx_set_protected(tx); |
6a22a59d4
|
280 |
|
252b86c43
|
281 |
skb_queue_walk(&tx->skbs, skb) { |
2de8e0d99
|
282 283 284 |
if (wep_encrypt_skb(tx, skb) < 0) { I802_DEBUG_INC(tx->local->tx_handlers_drop_wep); return TX_DROP; |
6a22a59d4
|
285 |
} |
252b86c43
|
286 |
} |
6a22a59d4
|
287 |
|
9ae54c846
|
288 |
return TX_CONTINUE; |
6a22a59d4
|
289 |
} |