1 // SPDX-License-Identifier: GPL-2.0-only
3 * libipw crypt: host-based WEP encryption implementation for libipw
5 * Copyright (c) 2002-2004, Jouni Malinen <j@w1.fi>
6 * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com>
10 #include <linux/fips.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/random.h>
15 #include <linux/scatterlist.h>
16 #include <linux/skbuff.h>
18 #include <asm/string.h>
19 #include <crypto/arc4.h>
20 #include <linux/crc32.h>
23 struct libipw_wep_data
{
25 #define WEP_KEY_LEN 13
26 u8 key
[WEP_KEY_LEN
+ 1];
29 struct arc4_ctx tx_ctx
;
30 struct arc4_ctx rx_ctx
;
33 static void *libipw_wep_init(int keyidx
)
35 struct libipw_wep_data
*priv
;
40 priv
= kzalloc(sizeof(*priv
), GFP_ATOMIC
);
43 priv
->key_idx
= keyidx
;
45 /* start WEP IV from a random value */
46 get_random_bytes(&priv
->iv
, 4);
51 static void libipw_wep_deinit(void *priv
)
53 kfree_sensitive(priv
);
56 /* Add WEP IV/key info to a frame that has at least 4 bytes of headroom */
57 static int libipw_wep_build_iv(struct sk_buff
*skb
, int hdr_len
,
58 u8
*key
, int keylen
, void *priv
)
60 struct libipw_wep_data
*wep
= priv
;
64 if (skb_headroom(skb
) < 4 || skb
->len
< hdr_len
)
67 pos
= skb_push(skb
, 4);
68 memmove(pos
, pos
+ 4, hdr_len
);
71 klen
= 3 + wep
->key_len
;
75 /* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
76 * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
77 * can be used to speedup attacks, so avoid using them. */
78 if ((wep
->iv
& 0xff00) == 0xff00) {
79 u8 B
= (wep
->iv
>> 16) & 0xff;
80 if (B
>= 3 && B
< klen
)
84 /* Prepend 24-bit IV to RC4 key and TX frame */
85 *pos
++ = (wep
->iv
>> 16) & 0xff;
86 *pos
++ = (wep
->iv
>> 8) & 0xff;
87 *pos
++ = wep
->iv
& 0xff;
88 *pos
++ = wep
->key_idx
<< 6;
93 /* Perform WEP encryption on given skb that has at least 4 bytes of headroom
94 * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
95 * so the payload length increases with 8 bytes.
97 * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
99 static int libipw_wep_encrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
101 struct libipw_wep_data
*wep
= priv
;
104 u8 key
[WEP_KEY_LEN
+ 3];
106 /* other checks are in libipw_wep_build_iv */
107 if (skb_tailroom(skb
) < 4)
110 /* add the IV to the frame */
111 if (libipw_wep_build_iv(skb
, hdr_len
, NULL
, 0, priv
))
114 /* Copy the IV into the first 3 bytes of the key */
115 skb_copy_from_linear_data_offset(skb
, hdr_len
, key
, 3);
117 /* Copy rest of the WEP key (the secret part) */
118 memcpy(key
+ 3, wep
->key
, wep
->key_len
);
120 len
= skb
->len
- hdr_len
- 4;
121 pos
= skb
->data
+ hdr_len
+ 4;
122 klen
= 3 + wep
->key_len
;
124 /* Append little-endian CRC32 over only the data and encrypt it to produce ICV */
125 crc
= ~crc32_le(~0, pos
, len
);
126 icv
= skb_put(skb
, 4);
132 arc4_setkey(&wep
->tx_ctx
, key
, klen
);
133 arc4_crypt(&wep
->tx_ctx
, pos
, pos
, len
+ 4);
138 /* Perform WEP decryption on given buffer. Buffer includes whole WEP part of
139 * the frame: IV (4 bytes), encrypted payload (including SNAP header),
140 * ICV (4 bytes). len includes both IV and ICV.
142 * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
143 * failure. If frame is OK, IV and ICV will be removed.
145 static int libipw_wep_decrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
147 struct libipw_wep_data
*wep
= priv
;
149 u8 key
[WEP_KEY_LEN
+ 3];
150 u8 keyidx
, *pos
, icv
[4];
152 if (skb
->len
< hdr_len
+ 8)
155 pos
= skb
->data
+ hdr_len
;
159 keyidx
= *pos
++ >> 6;
160 if (keyidx
!= wep
->key_idx
)
163 klen
= 3 + wep
->key_len
;
165 /* Copy rest of the WEP key (the secret part) */
166 memcpy(key
+ 3, wep
->key
, wep
->key_len
);
168 /* Apply RC4 to data and compute CRC32 over decrypted data */
169 plen
= skb
->len
- hdr_len
- 8;
171 arc4_setkey(&wep
->rx_ctx
, key
, klen
);
172 arc4_crypt(&wep
->rx_ctx
, pos
, pos
, plen
+ 4);
174 crc
= ~crc32_le(~0, pos
, plen
);
179 if (memcmp(icv
, pos
+ plen
, 4) != 0) {
180 /* ICV mismatch - drop frame */
184 /* Remove IV and ICV */
185 memmove(skb
->data
+ 4, skb
->data
, hdr_len
);
187 skb_trim(skb
, skb
->len
- 4);
192 static int libipw_wep_set_key(void *key
, int len
, u8
* seq
, void *priv
)
194 struct libipw_wep_data
*wep
= priv
;
196 if (len
< 0 || len
> WEP_KEY_LEN
)
199 memcpy(wep
->key
, key
, len
);
205 static int libipw_wep_get_key(void *key
, int len
, u8
* seq
, void *priv
)
207 struct libipw_wep_data
*wep
= priv
;
209 if (len
< wep
->key_len
)
212 memcpy(key
, wep
->key
, wep
->key_len
);
217 static void libipw_wep_print_stats(struct seq_file
*m
, void *priv
)
219 struct libipw_wep_data
*wep
= priv
;
220 seq_printf(m
, "key[%d] alg=WEP len=%d\n", wep
->key_idx
, wep
->key_len
);
223 static const struct libipw_crypto_ops libipw_crypt_wep
= {
225 .init
= libipw_wep_init
,
226 .deinit
= libipw_wep_deinit
,
227 .encrypt_mpdu
= libipw_wep_encrypt
,
228 .decrypt_mpdu
= libipw_wep_decrypt
,
229 .encrypt_msdu
= NULL
,
230 .decrypt_msdu
= NULL
,
231 .set_key
= libipw_wep_set_key
,
232 .get_key
= libipw_wep_get_key
,
233 .print_stats
= libipw_wep_print_stats
,
234 .extra_mpdu_prefix_len
= 4, /* IV */
235 .extra_mpdu_postfix_len
= 4, /* ICV */
236 .owner
= THIS_MODULE
,
239 int __init
libipw_crypto_wep_init(void)
241 return libipw_register_crypto_ops(&libipw_crypt_wep
);
244 void __exit
libipw_crypto_wep_exit(void)
246 libipw_unregister_crypto_ops(&libipw_crypt_wep
);