Thomas Gleixner | 21042e4 | 2019-06-04 10:11:34 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 2 | /* |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 3 | * lib80211 crypt: host-based WEP encryption implementation for lib80211 |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 4 | * |
Jouni Malinen | 85d32e7 | 2007-03-24 17:15:30 -0700 | [diff] [blame] | 5 | * Copyright (c) 2002-2004, Jouni Malinen <j@w1.fi> |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 6 | * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 7 | */ |
| 8 | |
Herbert Xu | f12cc20 | 2006-08-22 20:36:13 +1000 | [diff] [blame] | 9 | #include <linux/err.h> |
Ard Biesheuvel | af1f3d3 | 2019-06-12 18:19:55 +0200 | [diff] [blame] | 10 | #include <linux/fips.h> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/random.h> |
Ralf Baechle | 1176360 | 2007-10-23 20:42:11 +0200 | [diff] [blame] | 15 | #include <linux/scatterlist.h> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 16 | #include <linux/skbuff.h> |
Al Viro | d7fe0f2 | 2006-12-03 23:15:30 -0500 | [diff] [blame] | 17 | #include <linux/mm.h> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 18 | #include <asm/string.h> |
| 19 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 20 | #include <net/lib80211.h> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 21 | |
Ard Biesheuvel | af1f3d3 | 2019-06-12 18:19:55 +0200 | [diff] [blame] | 22 | #include <crypto/arc4.h> |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 23 | #include <linux/crc32.h> |
| 24 | |
| 25 | MODULE_AUTHOR("Jouni Malinen"); |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 26 | MODULE_DESCRIPTION("lib80211 crypt: WEP"); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 27 | MODULE_LICENSE("GPL"); |
| 28 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 29 | struct lib80211_wep_data { |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 30 | u32 iv; |
| 31 | #define WEP_KEY_LEN 13 |
| 32 | u8 key[WEP_KEY_LEN + 1]; |
| 33 | u8 key_len; |
| 34 | u8 key_idx; |
Ard Biesheuvel | af1f3d3 | 2019-06-12 18:19:55 +0200 | [diff] [blame] | 35 | struct arc4_ctx tx_ctx; |
| 36 | struct arc4_ctx rx_ctx; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 37 | }; |
| 38 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 39 | static void *lib80211_wep_init(int keyidx) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 40 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 41 | struct lib80211_wep_data *priv; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 42 | |
Ard Biesheuvel | af1f3d3 | 2019-06-12 18:19:55 +0200 | [diff] [blame] | 43 | if (fips_enabled) |
| 44 | return NULL; |
| 45 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 46 | priv = kzalloc(sizeof(*priv), GFP_ATOMIC); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 47 | if (priv == NULL) |
Ard Biesheuvel | af1f3d3 | 2019-06-12 18:19:55 +0200 | [diff] [blame] | 48 | return NULL; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 49 | priv->key_idx = keyidx; |
| 50 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 51 | /* start WEP IV from a random value */ |
| 52 | get_random_bytes(&priv->iv, 4); |
| 53 | |
| 54 | return priv; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 55 | } |
| 56 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 57 | static void lib80211_wep_deinit(void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 58 | { |
Ard Biesheuvel | af1f3d3 | 2019-06-12 18:19:55 +0200 | [diff] [blame] | 59 | kzfree(priv); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 62 | /* Add WEP IV/key info to a frame that has at least 4 bytes of headroom */ |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 63 | static int lib80211_wep_build_iv(struct sk_buff *skb, int hdr_len, |
Zhu Yi | 9184d93 | 2006-01-19 16:22:32 +0800 | [diff] [blame] | 64 | u8 *key, int keylen, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 65 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 66 | struct lib80211_wep_data *wep = priv; |
Rajkumar Manoharan | 6572e91 | 2011-04-25 15:56:16 +0530 | [diff] [blame] | 67 | u32 klen; |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 68 | u8 *pos; |
YOSHIFUJI Hideaki | 6426565 | 2007-02-09 23:24:46 +0900 | [diff] [blame] | 69 | |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 70 | if (skb_headroom(skb) < 4 || skb->len < hdr_len) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 71 | return -1; |
| 72 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 73 | pos = skb_push(skb, 4); |
| 74 | memmove(pos, pos + 4, hdr_len); |
| 75 | pos += hdr_len; |
| 76 | |
| 77 | klen = 3 + wep->key_len; |
| 78 | |
| 79 | wep->iv++; |
| 80 | |
| 81 | /* Fluhrer, Mantin, and Shamir have reported weaknesses in the key |
| 82 | * scheduling algorithm of RC4. At least IVs (KeyByte + 3, 0xff, N) |
| 83 | * can be used to speedup attacks, so avoid using them. */ |
| 84 | if ((wep->iv & 0xff00) == 0xff00) { |
| 85 | u8 B = (wep->iv >> 16) & 0xff; |
| 86 | if (B >= 3 && B < klen) |
| 87 | wep->iv += 0x0100; |
| 88 | } |
| 89 | |
| 90 | /* Prepend 24-bit IV to RC4 key and TX frame */ |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 91 | *pos++ = (wep->iv >> 16) & 0xff; |
| 92 | *pos++ = (wep->iv >> 8) & 0xff; |
| 93 | *pos++ = wep->iv & 0xff; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 94 | *pos++ = wep->key_idx << 6; |
| 95 | |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /* Perform WEP encryption on given skb that has at least 4 bytes of headroom |
| 100 | * for IV and 4 bytes of tailroom for ICV. Both IV and ICV will be transmitted, |
| 101 | * so the payload length increases with 8 bytes. |
| 102 | * |
| 103 | * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data)) |
| 104 | */ |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 105 | static int lib80211_wep_encrypt(struct sk_buff *skb, int hdr_len, void *priv) |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 106 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 107 | struct lib80211_wep_data *wep = priv; |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 108 | u32 crc, klen, len; |
| 109 | u8 *pos, *icv; |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 110 | u8 key[WEP_KEY_LEN + 3]; |
| 111 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 112 | /* other checks are in lib80211_wep_build_iv */ |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 113 | if (skb_tailroom(skb) < 4) |
| 114 | return -1; |
YOSHIFUJI Hideaki | 6426565 | 2007-02-09 23:24:46 +0900 | [diff] [blame] | 115 | |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 116 | /* add the IV to the frame */ |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 117 | if (lib80211_wep_build_iv(skb, hdr_len, NULL, 0, priv)) |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 118 | return -1; |
YOSHIFUJI Hideaki | 6426565 | 2007-02-09 23:24:46 +0900 | [diff] [blame] | 119 | |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 120 | /* Copy the IV into the first 3 bytes of the key */ |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 121 | skb_copy_from_linear_data_offset(skb, hdr_len, key, 3); |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 122 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 123 | /* Copy rest of the WEP key (the secret part) */ |
| 124 | memcpy(key + 3, wep->key, wep->key_len); |
YOSHIFUJI Hideaki | 6426565 | 2007-02-09 23:24:46 +0900 | [diff] [blame] | 125 | |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 126 | len = skb->len - hdr_len - 4; |
| 127 | pos = skb->data + hdr_len + 4; |
| 128 | klen = 3 + wep->key_len; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 129 | |
Johannes Berg | a4bf26f | 2005-12-31 11:35:20 +0100 | [diff] [blame] | 130 | /* Append little-endian CRC32 over only the data and encrypt it to produce ICV */ |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 131 | crc = ~crc32_le(~0, pos, len); |
| 132 | icv = skb_put(skb, 4); |
| 133 | icv[0] = crc; |
| 134 | icv[1] = crc >> 8; |
| 135 | icv[2] = crc >> 16; |
| 136 | icv[3] = crc >> 24; |
| 137 | |
Ard Biesheuvel | af1f3d3 | 2019-06-12 18:19:55 +0200 | [diff] [blame] | 138 | arc4_setkey(&wep->tx_ctx, key, klen); |
| 139 | arc4_crypt(&wep->tx_ctx, pos, pos, len + 4); |
Johannes Berg | b802a5d | 2018-10-01 09:16:08 +0200 | [diff] [blame] | 140 | |
| 141 | return 0; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 144 | /* Perform WEP decryption on given buffer. Buffer includes whole WEP part of |
| 145 | * the frame: IV (4 bytes), encrypted payload (including SNAP header), |
| 146 | * ICV (4 bytes). len includes both IV and ICV. |
| 147 | * |
| 148 | * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on |
| 149 | * failure. If frame is OK, IV and ICV will be removed. |
| 150 | */ |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 151 | static int lib80211_wep_decrypt(struct sk_buff *skb, int hdr_len, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 152 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 153 | struct lib80211_wep_data *wep = priv; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 154 | u32 crc, klen, plen; |
| 155 | u8 key[WEP_KEY_LEN + 3]; |
| 156 | u8 keyidx, *pos, icv[4]; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 157 | |
| 158 | if (skb->len < hdr_len + 8) |
| 159 | return -1; |
| 160 | |
| 161 | pos = skb->data + hdr_len; |
| 162 | key[0] = *pos++; |
| 163 | key[1] = *pos++; |
| 164 | key[2] = *pos++; |
| 165 | keyidx = *pos++ >> 6; |
| 166 | if (keyidx != wep->key_idx) |
| 167 | return -1; |
| 168 | |
| 169 | klen = 3 + wep->key_len; |
| 170 | |
| 171 | /* Copy rest of the WEP key (the secret part) */ |
| 172 | memcpy(key + 3, wep->key, wep->key_len); |
| 173 | |
| 174 | /* Apply RC4 to data and compute CRC32 over decrypted data */ |
| 175 | plen = skb->len - hdr_len - 8; |
| 176 | |
Ard Biesheuvel | af1f3d3 | 2019-06-12 18:19:55 +0200 | [diff] [blame] | 177 | arc4_setkey(&wep->rx_ctx, key, klen); |
| 178 | arc4_crypt(&wep->rx_ctx, pos, pos, plen + 4); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 179 | |
| 180 | crc = ~crc32_le(~0, pos, plen); |
| 181 | icv[0] = crc; |
| 182 | icv[1] = crc >> 8; |
| 183 | icv[2] = crc >> 16; |
| 184 | icv[3] = crc >> 24; |
| 185 | if (memcmp(icv, pos + plen, 4) != 0) { |
| 186 | /* ICV mismatch - drop frame */ |
| 187 | return -2; |
| 188 | } |
| 189 | |
| 190 | /* Remove IV and ICV */ |
| 191 | memmove(skb->data + 4, skb->data, hdr_len); |
| 192 | skb_pull(skb, 4); |
| 193 | skb_trim(skb, skb->len - 4); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 198 | static int lib80211_wep_set_key(void *key, int len, u8 * seq, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 199 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 200 | struct lib80211_wep_data *wep = priv; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 201 | |
| 202 | if (len < 0 || len > WEP_KEY_LEN) |
| 203 | return -1; |
| 204 | |
| 205 | memcpy(wep->key, key, len); |
| 206 | wep->key_len = len; |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 211 | static int lib80211_wep_get_key(void *key, int len, u8 * seq, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 212 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 213 | struct lib80211_wep_data *wep = priv; |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 214 | |
| 215 | if (len < wep->key_len) |
| 216 | return -1; |
| 217 | |
| 218 | memcpy(key, wep->key, wep->key_len); |
| 219 | |
| 220 | return wep->key_len; |
| 221 | } |
| 222 | |
David Howells | 6bbefe8 | 2013-04-10 21:13:23 +0100 | [diff] [blame] | 223 | static void lib80211_wep_print_stats(struct seq_file *m, void *priv) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 224 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 225 | struct lib80211_wep_data *wep = priv; |
David Howells | 6bbefe8 | 2013-04-10 21:13:23 +0100 | [diff] [blame] | 226 | seq_printf(m, "key[%d] alg=WEP len=%d\n", wep->key_idx, wep->key_len); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 227 | } |
| 228 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 229 | static struct lib80211_crypto_ops lib80211_crypt_wep = { |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 230 | .name = "WEP", |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 231 | .init = lib80211_wep_init, |
| 232 | .deinit = lib80211_wep_deinit, |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 233 | .encrypt_mpdu = lib80211_wep_encrypt, |
| 234 | .decrypt_mpdu = lib80211_wep_decrypt, |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 235 | .encrypt_msdu = NULL, |
| 236 | .decrypt_msdu = NULL, |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 237 | .set_key = lib80211_wep_set_key, |
| 238 | .get_key = lib80211_wep_get_key, |
| 239 | .print_stats = lib80211_wep_print_stats, |
James Ketrenos | 1264fc0 | 2005-09-21 11:54:53 -0500 | [diff] [blame] | 240 | .extra_mpdu_prefix_len = 4, /* IV */ |
| 241 | .extra_mpdu_postfix_len = 4, /* ICV */ |
James Ketrenos | 74079fd | 2005-09-13 17:35:21 -0500 | [diff] [blame] | 242 | .owner = THIS_MODULE, |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 243 | }; |
| 244 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 245 | static int __init lib80211_crypto_wep_init(void) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 246 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 247 | return lib80211_register_crypto_ops(&lib80211_crypt_wep); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 248 | } |
| 249 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 250 | static void __exit lib80211_crypto_wep_exit(void) |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 251 | { |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 252 | lib80211_unregister_crypto_ops(&lib80211_crypt_wep); |
Jeff Garzik | b453872 | 2005-05-12 22:48:20 -0400 | [diff] [blame] | 253 | } |
| 254 | |
John W. Linville | 274bfb8 | 2008-10-29 11:35:05 -0400 | [diff] [blame] | 255 | module_init(lib80211_crypto_wep_init); |
| 256 | module_exit(lib80211_crypto_wep_exit); |