blob: 04e4d66ea19ddd6be1575750e6c9b3600586a8c0 [file] [log] [blame]
Jeff Garzikb4538722005-05-12 22:48:20 -04001/*
John W. Linville274bfb82008-10-29 11:35:05 -04002 * lib80211 crypt: host-based WEP encryption implementation for lib80211
Jeff Garzikb4538722005-05-12 22:48:20 -04003 *
Jouni Malinen85d32e72007-03-24 17:15:30 -07004 * Copyright (c) 2002-2004, Jouni Malinen <j@w1.fi>
John W. Linville274bfb82008-10-29 11:35:05 -04005 * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com>
Jeff Garzikb4538722005-05-12 22:48:20 -04006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation. See README and COPYING for
10 * more details.
11 */
12
Herbert Xuf12cc202006-08-22 20:36:13 +100013#include <linux/err.h>
Ard Biesheuvelaf1f3d32019-06-12 18:19:55 +020014#include <linux/fips.h>
Jeff Garzikb4538722005-05-12 22:48:20 -040015#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/random.h>
Ralf Baechle11763602007-10-23 20:42:11 +020019#include <linux/scatterlist.h>
Jeff Garzikb4538722005-05-12 22:48:20 -040020#include <linux/skbuff.h>
Al Virod7fe0f22006-12-03 23:15:30 -050021#include <linux/mm.h>
Jeff Garzikb4538722005-05-12 22:48:20 -040022#include <asm/string.h>
23
John W. Linville274bfb82008-10-29 11:35:05 -040024#include <net/lib80211.h>
Jeff Garzikb4538722005-05-12 22:48:20 -040025
Ard Biesheuvelaf1f3d32019-06-12 18:19:55 +020026#include <crypto/arc4.h>
Jeff Garzikb4538722005-05-12 22:48:20 -040027#include <linux/crc32.h>
28
29MODULE_AUTHOR("Jouni Malinen");
John W. Linville274bfb82008-10-29 11:35:05 -040030MODULE_DESCRIPTION("lib80211 crypt: WEP");
Jeff Garzikb4538722005-05-12 22:48:20 -040031MODULE_LICENSE("GPL");
32
John W. Linville274bfb82008-10-29 11:35:05 -040033struct lib80211_wep_data {
Jeff Garzikb4538722005-05-12 22:48:20 -040034 u32 iv;
35#define WEP_KEY_LEN 13
36 u8 key[WEP_KEY_LEN + 1];
37 u8 key_len;
38 u8 key_idx;
Ard Biesheuvelaf1f3d32019-06-12 18:19:55 +020039 struct arc4_ctx tx_ctx;
40 struct arc4_ctx rx_ctx;
Jeff Garzikb4538722005-05-12 22:48:20 -040041};
42
John W. Linville274bfb82008-10-29 11:35:05 -040043static void *lib80211_wep_init(int keyidx)
Jeff Garzikb4538722005-05-12 22:48:20 -040044{
John W. Linville274bfb82008-10-29 11:35:05 -040045 struct lib80211_wep_data *priv;
Jeff Garzikb4538722005-05-12 22:48:20 -040046
Ard Biesheuvelaf1f3d32019-06-12 18:19:55 +020047 if (fips_enabled)
48 return NULL;
49
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070050 priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
Jeff Garzikb4538722005-05-12 22:48:20 -040051 if (priv == NULL)
Ard Biesheuvelaf1f3d32019-06-12 18:19:55 +020052 return NULL;
Jeff Garzikb4538722005-05-12 22:48:20 -040053 priv->key_idx = keyidx;
54
Jeff Garzikb4538722005-05-12 22:48:20 -040055 /* start WEP IV from a random value */
56 get_random_bytes(&priv->iv, 4);
57
58 return priv;
Jeff Garzikb4538722005-05-12 22:48:20 -040059}
60
John W. Linville274bfb82008-10-29 11:35:05 -040061static void lib80211_wep_deinit(void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -040062{
Ard Biesheuvelaf1f3d32019-06-12 18:19:55 +020063 kzfree(priv);
Jeff Garzikb4538722005-05-12 22:48:20 -040064}
65
Johannes Berga4bf26f2005-12-31 11:35:20 +010066/* Add WEP IV/key info to a frame that has at least 4 bytes of headroom */
John W. Linville274bfb82008-10-29 11:35:05 -040067static int lib80211_wep_build_iv(struct sk_buff *skb, int hdr_len,
Zhu Yi9184d932006-01-19 16:22:32 +080068 u8 *key, int keylen, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -040069{
John W. Linville274bfb82008-10-29 11:35:05 -040070 struct lib80211_wep_data *wep = priv;
Rajkumar Manoharan6572e912011-04-25 15:56:16 +053071 u32 klen;
Johannes Berga4bf26f2005-12-31 11:35:20 +010072 u8 *pos;
YOSHIFUJI Hideaki64265652007-02-09 23:24:46 +090073
Johannes Berga4bf26f2005-12-31 11:35:20 +010074 if (skb_headroom(skb) < 4 || skb->len < hdr_len)
Jeff Garzikb4538722005-05-12 22:48:20 -040075 return -1;
76
Jeff Garzikb4538722005-05-12 22:48:20 -040077 pos = skb_push(skb, 4);
78 memmove(pos, pos + 4, hdr_len);
79 pos += hdr_len;
80
81 klen = 3 + wep->key_len;
82
83 wep->iv++;
84
85 /* Fluhrer, Mantin, and Shamir have reported weaknesses in the key
86 * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N)
87 * can be used to speedup attacks, so avoid using them. */
88 if ((wep->iv & 0xff00) == 0xff00) {
89 u8 B = (wep->iv >> 16) & 0xff;
90 if (B >= 3 && B < klen)
91 wep->iv += 0x0100;
92 }
93
94 /* Prepend 24-bit IV to RC4 key and TX frame */
Johannes Berga4bf26f2005-12-31 11:35:20 +010095 *pos++ = (wep->iv >> 16) & 0xff;
96 *pos++ = (wep->iv >> 8) & 0xff;
97 *pos++ = wep->iv & 0xff;
Jeff Garzikb4538722005-05-12 22:48:20 -040098 *pos++ = wep->key_idx << 6;
99
Johannes Berga4bf26f2005-12-31 11:35:20 +0100100 return 0;
101}
102
103/* Perform WEP encryption on given skb that has at least 4 bytes of headroom
104 * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted,
105 * so the payload length increases with 8 bytes.
106 *
107 * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data))
108 */
John W. Linville274bfb82008-10-29 11:35:05 -0400109static int lib80211_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
Johannes Berga4bf26f2005-12-31 11:35:20 +0100110{
John W. Linville274bfb82008-10-29 11:35:05 -0400111 struct lib80211_wep_data *wep = priv;
Johannes Berga4bf26f2005-12-31 11:35:20 +0100112 u32 crc, klen, len;
113 u8 *pos, *icv;
Johannes Berga4bf26f2005-12-31 11:35:20 +0100114 u8 key[WEP_KEY_LEN + 3];
115
John W. Linville274bfb82008-10-29 11:35:05 -0400116 /* other checks are in lib80211_wep_build_iv */
Johannes Berga4bf26f2005-12-31 11:35:20 +0100117 if (skb_tailroom(skb) < 4)
118 return -1;
YOSHIFUJI Hideaki64265652007-02-09 23:24:46 +0900119
Johannes Berga4bf26f2005-12-31 11:35:20 +0100120 /* add the IV to the frame */
John W. Linville274bfb82008-10-29 11:35:05 -0400121 if (lib80211_wep_build_iv(skb, hdr_len, NULL, 0, priv))
Johannes Berga4bf26f2005-12-31 11:35:20 +0100122 return -1;
YOSHIFUJI Hideaki64265652007-02-09 23:24:46 +0900123
Johannes Berga4bf26f2005-12-31 11:35:20 +0100124 /* Copy the IV into the first 3 bytes of the key */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300125 skb_copy_from_linear_data_offset(skb, hdr_len, key, 3);
Johannes Berga4bf26f2005-12-31 11:35:20 +0100126
Jeff Garzikb4538722005-05-12 22:48:20 -0400127 /* Copy rest of the WEP key (the secret part) */
128 memcpy(key + 3, wep->key, wep->key_len);
YOSHIFUJI Hideaki64265652007-02-09 23:24:46 +0900129
Johannes Berga4bf26f2005-12-31 11:35:20 +0100130 len = skb->len - hdr_len - 4;
131 pos = skb->data + hdr_len + 4;
132 klen = 3 + wep->key_len;
Jeff Garzikb4538722005-05-12 22:48:20 -0400133
Johannes Berga4bf26f2005-12-31 11:35:20 +0100134 /* Append little-endian CRC32 over only the data and encrypt it to produce ICV */
Jeff Garzikb4538722005-05-12 22:48:20 -0400135 crc = ~crc32_le(~0, pos, len);
136 icv = skb_put(skb, 4);
137 icv[0] = crc;
138 icv[1] = crc >> 8;
139 icv[2] = crc >> 16;
140 icv[3] = crc >> 24;
141
Ard Biesheuvelaf1f3d32019-06-12 18:19:55 +0200142 arc4_setkey(&wep->tx_ctx, key, klen);
143 arc4_crypt(&wep->tx_ctx, pos, pos, len + 4);
Johannes Bergb802a5d2018-10-01 09:16:08 +0200144
145 return 0;
Jeff Garzikb4538722005-05-12 22:48:20 -0400146}
147
Jeff Garzikb4538722005-05-12 22:48:20 -0400148/* Perform WEP decryption on given buffer. Buffer includes whole WEP part of
149 * the frame: IV (4 bytes), encrypted payload (including SNAP header),
150 * ICV (4 bytes). len includes both IV and ICV.
151 *
152 * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on
153 * failure. If frame is OK, IV and ICV will be removed.
154 */
John W. Linville274bfb82008-10-29 11:35:05 -0400155static int lib80211_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -0400156{
John W. Linville274bfb82008-10-29 11:35:05 -0400157 struct lib80211_wep_data *wep = priv;
Jeff Garzikb4538722005-05-12 22:48:20 -0400158 u32 crc, klen, plen;
159 u8 key[WEP_KEY_LEN + 3];
160 u8 keyidx, *pos, icv[4];
Jeff Garzikb4538722005-05-12 22:48:20 -0400161
162 if (skb->len < hdr_len + 8)
163 return -1;
164
165 pos = skb->data + hdr_len;
166 key[0] = *pos++;
167 key[1] = *pos++;
168 key[2] = *pos++;
169 keyidx = *pos++ >> 6;
170 if (keyidx != wep->key_idx)
171 return -1;
172
173 klen = 3 + wep->key_len;
174
175 /* Copy rest of the WEP key (the secret part) */
176 memcpy(key + 3, wep->key, wep->key_len);
177
178 /* Apply RC4 to data and compute CRC32 over decrypted data */
179 plen = skb->len - hdr_len - 8;
180
Ard Biesheuvelaf1f3d32019-06-12 18:19:55 +0200181 arc4_setkey(&wep->rx_ctx, key, klen);
182 arc4_crypt(&wep->rx_ctx, pos, pos, plen + 4);
Jeff Garzikb4538722005-05-12 22:48:20 -0400183
184 crc = ~crc32_le(~0, pos, plen);
185 icv[0] = crc;
186 icv[1] = crc >> 8;
187 icv[2] = crc >> 16;
188 icv[3] = crc >> 24;
189 if (memcmp(icv, pos + plen, 4) != 0) {
190 /* ICV mismatch - drop frame */
191 return -2;
192 }
193
194 /* Remove IV and ICV */
195 memmove(skb->data + 4, skb->data, hdr_len);
196 skb_pull(skb, 4);
197 skb_trim(skb, skb->len - 4);
198
199 return 0;
200}
201
John W. Linville274bfb82008-10-29 11:35:05 -0400202static int lib80211_wep_set_key(void *key, int len, u8 * seq, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -0400203{
John W. Linville274bfb82008-10-29 11:35:05 -0400204 struct lib80211_wep_data *wep = priv;
Jeff Garzikb4538722005-05-12 22:48:20 -0400205
206 if (len < 0 || len > WEP_KEY_LEN)
207 return -1;
208
209 memcpy(wep->key, key, len);
210 wep->key_len = len;
211
212 return 0;
213}
214
John W. Linville274bfb82008-10-29 11:35:05 -0400215static int lib80211_wep_get_key(void *key, int len, u8 * seq, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -0400216{
John W. Linville274bfb82008-10-29 11:35:05 -0400217 struct lib80211_wep_data *wep = priv;
Jeff Garzikb4538722005-05-12 22:48:20 -0400218
219 if (len < wep->key_len)
220 return -1;
221
222 memcpy(key, wep->key, wep->key_len);
223
224 return wep->key_len;
225}
226
David Howells6bbefe82013-04-10 21:13:23 +0100227static void lib80211_wep_print_stats(struct seq_file *m, void *priv)
Jeff Garzikb4538722005-05-12 22:48:20 -0400228{
John W. Linville274bfb82008-10-29 11:35:05 -0400229 struct lib80211_wep_data *wep = priv;
David Howells6bbefe82013-04-10 21:13:23 +0100230 seq_printf(m, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len);
Jeff Garzikb4538722005-05-12 22:48:20 -0400231}
232
John W. Linville274bfb82008-10-29 11:35:05 -0400233static struct lib80211_crypto_ops lib80211_crypt_wep = {
James Ketrenos74079fd2005-09-13 17:35:21 -0500234 .name = "WEP",
John W. Linville274bfb82008-10-29 11:35:05 -0400235 .init = lib80211_wep_init,
236 .deinit = lib80211_wep_deinit,
John W. Linville274bfb82008-10-29 11:35:05 -0400237 .encrypt_mpdu = lib80211_wep_encrypt,
238 .decrypt_mpdu = lib80211_wep_decrypt,
James Ketrenos74079fd2005-09-13 17:35:21 -0500239 .encrypt_msdu = NULL,
240 .decrypt_msdu = NULL,
John W. Linville274bfb82008-10-29 11:35:05 -0400241 .set_key = lib80211_wep_set_key,
242 .get_key = lib80211_wep_get_key,
243 .print_stats = lib80211_wep_print_stats,
James Ketrenos1264fc02005-09-21 11:54:53 -0500244 .extra_mpdu_prefix_len = 4, /* IV */
245 .extra_mpdu_postfix_len = 4, /* ICV */
James Ketrenos74079fd2005-09-13 17:35:21 -0500246 .owner = THIS_MODULE,
Jeff Garzikb4538722005-05-12 22:48:20 -0400247};
248
John W. Linville274bfb82008-10-29 11:35:05 -0400249static int __init lib80211_crypto_wep_init(void)
Jeff Garzikb4538722005-05-12 22:48:20 -0400250{
John W. Linville274bfb82008-10-29 11:35:05 -0400251 return lib80211_register_crypto_ops(&lib80211_crypt_wep);
Jeff Garzikb4538722005-05-12 22:48:20 -0400252}
253
John W. Linville274bfb82008-10-29 11:35:05 -0400254static void __exit lib80211_crypto_wep_exit(void)
Jeff Garzikb4538722005-05-12 22:48:20 -0400255{
John W. Linville274bfb82008-10-29 11:35:05 -0400256 lib80211_unregister_crypto_ops(&lib80211_crypt_wep);
Jeff Garzikb4538722005-05-12 22:48:20 -0400257}
258
John W. Linville274bfb82008-10-29 11:35:05 -0400259module_init(lib80211_crypto_wep_init);
260module_exit(lib80211_crypto_wep_exit);