1 // SPDX-License-Identifier: (GPL-2.0 OR MPL-1.1)
2 /* src/p80211/p80211wep.c
4 * WEP encode/decode for P80211.
6 * Copyright (C) 2002 AbsoluteValue Systems, Inc. All Rights Reserved.
7 * --------------------------------------------------------------------
11 * The contents of this file are subject to the Mozilla Public
12 * License Version 1.1 (the "License"); you may not use this file
13 * except in compliance with the License. You may obtain a copy of
14 * the License at http://www.mozilla.org/MPL/
16 * Software distributed under the License is distributed on an "AS
17 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
18 * implied. See the License for the specific language governing
19 * rights and limitations under the License.
21 * Alternatively, the contents of this file may be used under the
22 * terms of the GNU Public License version 2 (the "GPL"), in which
23 * case the provisions of the GPL are applicable instead of the
24 * above. If you wish to allow the use of your version of this file
25 * only under the terms of the GPL and not to allow others to use
26 * your version of this file under the MPL, indicate your decision
27 * by deleting the provisions above and replace them with the notice
28 * and other provisions required by the GPL. If you do not delete
29 * the provisions above, a recipient may use your version of this
30 * file under either the MPL or the GPL.
32 * --------------------------------------------------------------------
34 * Inquiries regarding the linux-wlan Open Source project can be
37 * AbsoluteValue Systems Inc.
39 * http://www.linux-wlan.com
41 * --------------------------------------------------------------------
43 * Portions of the development of this software were funded by
44 * Intersil Corporation as part of PRISM(R) chipset product development.
46 * --------------------------------------------------------------------
49 /*================================================================*/
52 #include <linux/crc32.h>
53 #include <linux/netdevice.h>
54 #include <linux/wireless.h>
55 #include <linux/random.h>
56 #include <linux/kernel.h>
57 #include "p80211hdr.h"
58 #include "p80211types.h"
59 #include "p80211msg.h"
60 #include "p80211conv.h"
61 #include "p80211netdev.h"
63 #define WEP_KEY(x) (((x) & 0xC0) >> 6)
65 /* keylen in bytes! */
67 int wep_change_key(struct wlandevice
*wlandev
, int keynum
, u8
*key
, int keylen
)
71 if (keylen
>= MAX_KEYLEN
)
77 if (keynum
>= NUM_WEPKEYS
)
80 wlandev
->wep_keylens
[keynum
] = keylen
;
81 memcpy(wlandev
->wep_keys
[keynum
], key
, keylen
);
87 * 4-byte IV at start of buffer, 4-byte ICV at end of buffer.
88 * if successful, buf start is payload begin, length -= 8;
90 int wep_decrypt(struct wlandevice
*wlandev
, u8
*buf
, u32 len
, int key_override
,
93 u32 i
, j
, k
, crc
, keylen
;
94 u8 s
[256], key
[64], c_crc
[4];
97 /* Needs to be at least 8 bytes of payload */
101 /* initialize the first bytes of the key from the IV */
105 keyidx
= WEP_KEY(iv
[3]);
107 if (key_override
>= 0)
108 keyidx
= key_override
;
110 if (keyidx
>= NUM_WEPKEYS
)
113 keylen
= wlandev
->wep_keylens
[keyidx
];
118 /* copy the rest of the key over from the designated key */
119 memcpy(key
+ 3, wlandev
->wep_keys
[keyidx
], keylen
);
121 keylen
+= 3; /* add in IV bytes */
123 /* set up the RC4 state */
124 for (i
= 0; i
< 256; i
++)
127 for (i
= 0; i
< 256; i
++) {
128 j
= (j
+ s
[i
] + key
[i
% keylen
]) & 0xff;
132 /* Apply the RC4 to the data, update the CRC32 */
135 for (k
= 0; k
< len
; k
++) {
137 j
= (j
+ s
[i
]) & 0xff;
139 buf
[k
] ^= s
[(s
[i
] + s
[j
]) & 0xff];
141 crc
= ~crc32_le(~0, buf
, len
);
143 /* now let's check the crc */
146 c_crc
[2] = crc
>> 16;
147 c_crc
[3] = crc
>> 24;
149 for (k
= 0; k
< 4; k
++) {
151 j
= (j
+ s
[i
]) & 0xff;
153 if ((c_crc
[k
] ^ s
[(s
[i
] + s
[j
]) & 0xff]) != icv
[k
])
154 return -(4 | (k
<< 4)); /* ICV mismatch */
160 /* encrypts in-place. */
161 int wep_encrypt(struct wlandevice
*wlandev
, u8
*buf
,
162 u8
*dst
, u32 len
, int keynum
, u8
*iv
, u8
*icv
)
164 u32 i
, j
, k
, crc
, keylen
;
167 /* no point in WEPping an empty frame */
171 /* we need to have a real key.. */
172 if (keynum
>= NUM_WEPKEYS
)
174 keylen
= wlandev
->wep_keylens
[keynum
];
178 /* use a random IV. And skip known weak ones. */
179 get_random_bytes(iv
, 3);
180 while ((iv
[1] == 0xff) && (iv
[0] >= 3) && (iv
[0] < keylen
))
181 get_random_bytes(iv
, 3);
183 iv
[3] = (keynum
& 0x03) << 6;
189 /* copy the rest of the key over from the designated key */
190 memcpy(key
+ 3, wlandev
->wep_keys
[keynum
], keylen
);
192 keylen
+= 3; /* add in IV bytes */
194 /* set up the RC4 state */
195 for (i
= 0; i
< 256; i
++)
198 for (i
= 0; i
< 256; i
++) {
199 j
= (j
+ s
[i
] + key
[i
% keylen
]) & 0xff;
203 /* Update CRC32 then apply RC4 to the data */
206 for (k
= 0; k
< len
; k
++) {
208 j
= (j
+ s
[i
]) & 0xff;
210 dst
[k
] = buf
[k
] ^ s
[(s
[i
] + s
[j
]) & 0xff];
212 crc
= ~crc32_le(~0, buf
, len
);
214 /* now let's encrypt the crc */
220 for (k
= 0; k
< 4; k
++) {
222 j
= (j
+ s
[i
]) & 0xff;
224 icv
[k
] ^= s
[(s
[i
] + s
[j
]) & 0xff];