Blame view

net/mac80211/wep.c 8.31 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
f0706e828   Jiri Benc   [MAC80211]: Add m...
2
3
4
5
  /*
   * Software WEP encryption implementation
   * Copyright 2002, Jouni Malinen <jkmaline@cc.hut.fi>
   * Copyright 2003, Instant802 Networks, Inc.
f0706e828   Jiri Benc   [MAC80211]: Add m...
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   Ralf Baechle   [PATCH] Fix break...
16
  #include <linux/scatterlist.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
17
  #include <linux/slab.h>
860c6e6a1   Ivan Kuten   mac80211: fix una...
18
  #include <asm/unaligned.h>
f0706e828   Jiri Benc   [MAC80211]: Add m...
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   Johannes Berg   wireless: move cr...
28
  	get_random_bytes(&local->wep_iv, IEEE80211_WEP_IV_LEN);
f0706e828   Jiri Benc   [MAC80211]: Add m...
29

f0706e828   Jiri Benc   [MAC80211]: Add m...
30
31
  	return 0;
  }
c6a1fa12d   Johannes Berg   mac80211: minor c...
32
  static inline bool ieee80211_wep_weak_iv(u32 iv, int keylen)
f0706e828   Jiri Benc   [MAC80211]: Add m...
33
  {
c6a1fa12d   Johannes Berg   mac80211: minor c...
34
35
  	/*
  	 * Fluhrer, Mantin, and Shamir have reported weaknesses in the
f0706e828   Jiri Benc   [MAC80211]: Add m...
36
  	 * key scheduling algorithm of RC4. At least IVs (KeyByte + 3,
c6a1fa12d   Johannes Berg   mac80211: minor c...
37
38
  	 * 0xff, N) can be used to speedup attacks, so avoid using them.
  	 */
f0706e828   Jiri Benc   [MAC80211]: Add m...
39
40
41
  	if ((iv & 0xff00) == 0xff00) {
  		u8 B = (iv >> 16) & 0xff;
  		if (B >= 3 && B < 3 + keylen)
c6a1fa12d   Johannes Berg   mac80211: minor c...
42
  			return true;
f0706e828   Jiri Benc   [MAC80211]: Add m...
43
  	}
c6a1fa12d   Johannes Berg   mac80211: minor c...
44
  	return false;
f0706e828   Jiri Benc   [MAC80211]: Add m...
45
  }
4f0d18e26   Johannes Berg   [PATCH] mac80211:...
46
  static void ieee80211_wep_get_iv(struct ieee80211_local *local,
c9cf01226   Johannes Berg   mac80211: refacto...
47
  				 int keylen, int keyidx, u8 *iv)
f0706e828   Jiri Benc   [MAC80211]: Add m...
48
49
  {
  	local->wep_iv++;
c9cf01226   Johannes Berg   mac80211: refacto...
50
  	if (ieee80211_wep_weak_iv(local->wep_iv, keylen))
f0706e828   Jiri Benc   [MAC80211]: Add m...
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   Johannes Berg   mac80211: refacto...
59
  	*iv++ = keyidx << 6;
f0706e828   Jiri Benc   [MAC80211]: Add m...
60
  }
6a22a59d4   Johannes Berg   [PATCH] mac80211:...
61
62
  static u8 *ieee80211_wep_add_iv(struct ieee80211_local *local,
  				struct sk_buff *skb,
c9cf01226   Johannes Berg   mac80211: refacto...
63
  				int keylen, int keyidx)
f0706e828   Jiri Benc   [MAC80211]: Add m...
64
  {
70217d7f8   Harvey Harrison   mac80211: wep.c u...
65
  	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
ee70108fa   Janusz.Dziedzic@tieto.com   mac80211: Add IV-...
66
  	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
70217d7f8   Harvey Harrison   mac80211: wep.c u...
67
  	unsigned int hdrlen;
f0706e828   Jiri Benc   [MAC80211]: Add m...
68
  	u8 *newhdr;
70217d7f8   Harvey Harrison   mac80211: wep.c u...
69
  	hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
f0706e828   Jiri Benc   [MAC80211]: Add m...
70

47b4e1fc4   Janusz Dziedzic   mac80211: move WE...
71
  	if (WARN_ON(skb_headroom(skb) < IEEE80211_WEP_IV_LEN))
23c0752a2   Johannes Berg   mac80211: clean u...
72
  		return NULL;
f0706e828   Jiri Benc   [MAC80211]: Add m...
73

70217d7f8   Harvey Harrison   mac80211: wep.c u...
74
  	hdrlen = ieee80211_hdrlen(hdr->frame_control);
4325f6caa   Johannes Berg   wireless: move cr...
75
76
  	newhdr = skb_push(skb, IEEE80211_WEP_IV_LEN);
  	memmove(newhdr, newhdr + IEEE80211_WEP_IV_LEN, hdrlen);
ee70108fa   Janusz.Dziedzic@tieto.com   mac80211: Add IV-...
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   Johannes Berg   mac80211: refacto...
82
  	ieee80211_wep_get_iv(local, keylen, keyidx, newhdr + hdrlen);
f0706e828   Jiri Benc   [MAC80211]: Add m...
83
84
  	return newhdr + hdrlen;
  }
4f0d18e26   Johannes Berg   [PATCH] mac80211:...
85
86
87
  static void ieee80211_wep_remove_iv(struct ieee80211_local *local,
  				    struct sk_buff *skb,
  				    struct ieee80211_key *key)
f0706e828   Jiri Benc   [MAC80211]: Add m...
88
  {
70217d7f8   Harvey Harrison   mac80211: wep.c u...
89
90
  	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  	unsigned int hdrlen;
f0706e828   Jiri Benc   [MAC80211]: Add m...
91

70217d7f8   Harvey Harrison   mac80211: wep.c u...
92
  	hdrlen = ieee80211_hdrlen(hdr->frame_control);
4325f6caa   Johannes Berg   wireless: move cr...
93
94
  	memmove(skb->data + IEEE80211_WEP_IV_LEN, skb->data, hdrlen);
  	skb_pull(skb, IEEE80211_WEP_IV_LEN);
f0706e828   Jiri Benc   [MAC80211]: Add m...
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   Ard Biesheuvel   net/mac80211: mov...
101
  int ieee80211_wep_encrypt_data(struct arc4_ctx *ctx, u8 *rc4key,
3473187d2   John W. Linville   mac80211: remove ...
102
  			       size_t klen, u8 *data, size_t data_len)
f0706e828   Jiri Benc   [MAC80211]: Add m...
103
  {
860c6e6a1   Ivan Kuten   mac80211: fix una...
104
  	__le32 icv;
3473187d2   John W. Linville   mac80211: remove ...
105

860c6e6a1   Ivan Kuten   mac80211: fix una...
106
107
  	icv = cpu_to_le32(~crc32_le(~0, data, data_len));
  	put_unaligned(icv, (__le32 *)(data + data_len));
f0706e828   Jiri Benc   [MAC80211]: Add m...
108

5fdb37357   Ard Biesheuvel   net/mac80211: mov...
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   John W. Linville   mac80211: remove ...
112
113
  
  	return 0;
f0706e828   Jiri Benc   [MAC80211]: Add m...
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   Johannes Berg   cfg80211: rework ...
124
125
126
  int ieee80211_wep_encrypt(struct ieee80211_local *local,
  			  struct sk_buff *skb,
  			  const u8 *key, int keylen, int keyidx)
f0706e828   Jiri Benc   [MAC80211]: Add m...
127
  {
c9cf01226   Johannes Berg   mac80211: refacto...
128
  	u8 *iv;
f0706e828   Jiri Benc   [MAC80211]: Add m...
129
  	size_t len;
c9cf01226   Johannes Berg   mac80211: refacto...
130
  	u8 rc4key[3 + WLAN_KEY_LEN_WEP104];
f0706e828   Jiri Benc   [MAC80211]: Add m...
131

47b4e1fc4   Janusz Dziedzic   mac80211: move WE...
132
133
  	if (WARN_ON(skb_tailroom(skb) < IEEE80211_WEP_ICV_LEN))
  		return -1;
c9cf01226   Johannes Berg   mac80211: refacto...
134
135
  	iv = ieee80211_wep_add_iv(local, skb, keylen, keyidx);
  	if (!iv)
f0706e828   Jiri Benc   [MAC80211]: Add m...
136
  		return -1;
f0706e828   Jiri Benc   [MAC80211]: Add m...
137

4325f6caa   Johannes Berg   wireless: move cr...
138
  	len = skb->len - (iv + IEEE80211_WEP_IV_LEN - skb->data);
f0706e828   Jiri Benc   [MAC80211]: Add m...
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   Johannes Berg   mac80211: refacto...
144
  	memcpy(rc4key + 3, key, keylen);
f0706e828   Jiri Benc   [MAC80211]: Add m...
145
146
  
  	/* Add room for ICV */
4325f6caa   Johannes Berg   wireless: move cr...
147
  	skb_put(skb, IEEE80211_WEP_ICV_LEN);
f0706e828   Jiri Benc   [MAC80211]: Add m...
148

5fdb37357   Ard Biesheuvel   net/mac80211: mov...
149
  	return ieee80211_wep_encrypt_data(&local->wep_tx_ctx, rc4key, keylen + 3,
4325f6caa   Johannes Berg   wireless: move cr...
150
  					  iv + IEEE80211_WEP_IV_LEN, len);
f0706e828   Jiri Benc   [MAC80211]: Add m...
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   Ard Biesheuvel   net/mac80211: mov...
157
  int ieee80211_wep_decrypt_data(struct arc4_ctx *ctx, u8 *rc4key,
f0706e828   Jiri Benc   [MAC80211]: Add m...
158
159
  			       size_t klen, u8 *data, size_t data_len)
  {
f0706e828   Jiri Benc   [MAC80211]: Add m...
160
  	__le32 crc;
3473187d2   John W. Linville   mac80211: remove ...
161

5fdb37357   Ard Biesheuvel   net/mac80211: mov...
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   Jiri Benc   [MAC80211]: Add m...
165
166
  
  	crc = cpu_to_le32(~crc32_le(~0, data, data_len));
4325f6caa   Johannes Berg   wireless: move cr...
167
  	if (memcmp(&crc, data + data_len, IEEE80211_WEP_ICV_LEN) != 0)
f0706e828   Jiri Benc   [MAC80211]: Add m...
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   Johannes Berg   mac80211: refacto...
183
184
185
  static int ieee80211_wep_decrypt(struct ieee80211_local *local,
  				 struct sk_buff *skb,
  				 struct ieee80211_key *key)
f0706e828   Jiri Benc   [MAC80211]: Add m...
186
187
  {
  	u32 klen;
730bd83b0   Johannes Berg   mac80211: don't k...
188
  	u8 rc4key[3 + WLAN_KEY_LEN_WEP104];
f0706e828   Jiri Benc   [MAC80211]: Add m...
189
  	u8 keyidx;
70217d7f8   Harvey Harrison   mac80211: wep.c u...
190
191
  	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  	unsigned int hdrlen;
f0706e828   Jiri Benc   [MAC80211]: Add m...
192
193
  	size_t len;
  	int ret = 0;
70217d7f8   Harvey Harrison   mac80211: wep.c u...
194
  	if (!ieee80211_has_protected(hdr->frame_control))
f0706e828   Jiri Benc   [MAC80211]: Add m...
195
  		return -1;
70217d7f8   Harvey Harrison   mac80211: wep.c u...
196
  	hdrlen = ieee80211_hdrlen(hdr->frame_control);
4325f6caa   Johannes Berg   wireless: move cr...
197
  	if (skb->len < hdrlen + IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN)
f0706e828   Jiri Benc   [MAC80211]: Add m...
198
  		return -1;
4325f6caa   Johannes Berg   wireless: move cr...
199
  	len = skb->len - hdrlen - IEEE80211_WEP_IV_LEN - IEEE80211_WEP_ICV_LEN;
f0706e828   Jiri Benc   [MAC80211]: Add m...
200
201
  
  	keyidx = skb->data[hdrlen + 3] >> 6;
97359d123   Johannes Berg   mac80211: use cip...
202
  	if (!key || keyidx != key->conf.keyidx)
f0706e828   Jiri Benc   [MAC80211]: Add m...
203
  		return -1;
8f20fc249   Johannes Berg   [MAC80211]: embed...
204
  	klen = 3 + key->conf.keylen;
f0706e828   Jiri Benc   [MAC80211]: Add m...
205

f0706e828   Jiri Benc   [MAC80211]: Add m...
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   Johannes Berg   [MAC80211]: embed...
210
  	memcpy(rc4key + 3, key->conf.key, key->conf.keylen);
f0706e828   Jiri Benc   [MAC80211]: Add m...
211

5fdb37357   Ard Biesheuvel   net/mac80211: mov...
212
  	if (ieee80211_wep_decrypt_data(&local->wep_rx_ctx, rc4key, klen,
4325f6caa   Johannes Berg   wireless: move cr...
213
214
  				       skb->data + hdrlen +
  				       IEEE80211_WEP_IV_LEN, len))
f0706e828   Jiri Benc   [MAC80211]: Add m...
215
  		ret = -1;
f0706e828   Jiri Benc   [MAC80211]: Add m...
216

f0706e828   Jiri Benc   [MAC80211]: Add m...
217
  	/* Trim ICV */
4325f6caa   Johannes Berg   wireless: move cr...
218
  	skb_trim(skb, skb->len - IEEE80211_WEP_ICV_LEN);
f0706e828   Jiri Benc   [MAC80211]: Add m...
219
220
  
  	/* Remove IV */
4325f6caa   Johannes Berg   wireless: move cr...
221
222
  	memmove(skb->data + IEEE80211_WEP_IV_LEN, skb->data, hdrlen);
  	skb_pull(skb, IEEE80211_WEP_IV_LEN);
f0706e828   Jiri Benc   [MAC80211]: Add m...
223
224
225
  
  	return ret;
  }
9ae54c846   Johannes Berg   mac80211: split i...
226
  ieee80211_rx_result
5cf121c3c   Johannes Berg   mac80211: split i...
227
  ieee80211_crypto_wep_decrypt(struct ieee80211_rx_data *rx)
4f0d18e26   Johannes Berg   [PATCH] mac80211:...
228
  {
eb9fb5b88   Johannes Berg   mac80211: trim RX...
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   Johannes Berg   mac80211: lineari...
232
  	__le16 fc = hdr->frame_control;
358c8d9d3   Harvey Harrison   mac80211: use iee...
233

a82869118   Johannes Berg   mac80211: lineari...
234
  	if (!ieee80211_is_data(fc) && !ieee80211_is_auth(fc))
9ae54c846   Johannes Berg   mac80211: split i...
235
  		return RX_CONTINUE;
4f0d18e26   Johannes Berg   [PATCH] mac80211:...
236

eb9fb5b88   Johannes Berg   mac80211: trim RX...
237
  	if (!(status->flag & RX_FLAG_DECRYPTED)) {
a82869118   Johannes Berg   mac80211: lineari...
238
239
  		if (skb_linearize(rx->skb))
  			return RX_DROP_UNUSABLE;
f4ea83dd7   Johannes Berg   mac80211: rework ...
240
  		if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key))
e4c26add8   Johannes Berg   mac80211: split R...
241
  			return RX_DROP_UNUSABLE;
eb9fb5b88   Johannes Berg   mac80211: trim RX...
242
  	} else if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
4325f6caa   Johannes Berg   wireless: move cr...
243
244
  		if (!pskb_may_pull(rx->skb, ieee80211_hdrlen(fc) +
  					    IEEE80211_WEP_IV_LEN))
a82869118   Johannes Berg   mac80211: lineari...
245
  			return RX_DROP_UNUSABLE;
4f0d18e26   Johannes Berg   [PATCH] mac80211:...
246
247
  		ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
  		/* remove ICV */
cef0acd4d   David Spinadel   mac80211: Add RX ...
248
249
  		if (!(status->flag & RX_FLAG_ICV_STRIPPED) &&
  		    pskb_trim(rx->skb, rx->skb->len - IEEE80211_WEP_ICV_LEN))
a82869118   Johannes Berg   mac80211: lineari...
250
  			return RX_DROP_UNUSABLE;
4f0d18e26   Johannes Berg   [PATCH] mac80211:...
251
  	}
9ae54c846   Johannes Berg   mac80211: split i...
252
  	return RX_CONTINUE;
4f0d18e26   Johannes Berg   [PATCH] mac80211:...
253
  }
6a22a59d4   Johannes Berg   [PATCH] mac80211:...
254

5cf121c3c   Johannes Berg   mac80211: split i...
255
  static int wep_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
6a22a59d4   Johannes Berg   [PATCH] mac80211:...
256
  {
e039fa4a4   Johannes Berg   mac80211: move TX...
257
  	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
ee70108fa   Janusz.Dziedzic@tieto.com   mac80211: Add IV-...
258
  	struct ieee80211_key_conf *hw_key = info->control.hw_key;
e039fa4a4   Johannes Berg   mac80211: move TX...
259

ee70108fa   Janusz.Dziedzic@tieto.com   mac80211: Add IV-...
260
  	if (!hw_key) {
c9cf01226   Johannes Berg   mac80211: refacto...
261
262
263
  		if (ieee80211_wep_encrypt(tx->local, skb, tx->key->conf.key,
  					  tx->key->conf.keylen,
  					  tx->key->conf.keyidx))
6a22a59d4   Johannes Berg   [PATCH] mac80211:...
264
  			return -1;
ee70108fa   Janusz.Dziedzic@tieto.com   mac80211: Add IV-...
265
266
  	} else if ((hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
  		   (hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
813d76694   Johannes Berg   mac80211: move co...
267
268
269
270
271
  		if (!ieee80211_wep_add_iv(tx->local, skb,
  					  tx->key->conf.keylen,
  					  tx->key->conf.keyidx))
  			return -1;
  	}
6a22a59d4   Johannes Berg   [PATCH] mac80211:...
272
273
  	return 0;
  }
9ae54c846   Johannes Berg   mac80211: split i...
274
  ieee80211_tx_result
5cf121c3c   Johannes Berg   mac80211: split i...
275
  ieee80211_crypto_wep_encrypt(struct ieee80211_tx_data *tx)
6a22a59d4   Johannes Berg   [PATCH] mac80211:...
276
  {
2de8e0d99   Johannes Berg   mac80211: rewrite...
277
  	struct sk_buff *skb;
c6a1fa12d   Johannes Berg   mac80211: minor c...
278

5cf121c3c   Johannes Berg   mac80211: split i...
279
  	ieee80211_tx_set_protected(tx);
6a22a59d4   Johannes Berg   [PATCH] mac80211:...
280

252b86c43   Johannes Berg   mac80211: use skb...
281
  	skb_queue_walk(&tx->skbs, skb) {
2de8e0d99   Johannes Berg   mac80211: rewrite...
282
283
284
  		if (wep_encrypt_skb(tx, skb) < 0) {
  			I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
  			return TX_DROP;
6a22a59d4   Johannes Berg   [PATCH] mac80211:...
285
  		}
252b86c43   Johannes Berg   mac80211: use skb...
286
  	}
6a22a59d4   Johannes Berg   [PATCH] mac80211:...
287

9ae54c846   Johannes Berg   mac80211: split i...
288
  	return TX_CONTINUE;
6a22a59d4   Johannes Berg   [PATCH] mac80211:...
289
  }