18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Software WEP encryption implementation 48c2ecf20Sopenharmony_ci * Copyright 2002, Jouni Malinen <jkmaline@cc.hut.fi> 58c2ecf20Sopenharmony_ci * Copyright 2003, Instant802 Networks, Inc. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/netdevice.h> 98c2ecf20Sopenharmony_ci#include <linux/types.h> 108c2ecf20Sopenharmony_ci#include <linux/random.h> 118c2ecf20Sopenharmony_ci#include <linux/compiler.h> 128c2ecf20Sopenharmony_ci#include <linux/crc32.h> 138c2ecf20Sopenharmony_ci#include <linux/crypto.h> 148c2ecf20Sopenharmony_ci#include <linux/err.h> 158c2ecf20Sopenharmony_ci#include <linux/mm.h> 168c2ecf20Sopenharmony_ci#include <linux/scatterlist.h> 178c2ecf20Sopenharmony_ci#include <linux/slab.h> 188c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include <net/mac80211.h> 218c2ecf20Sopenharmony_ci#include "ieee80211_i.h" 228c2ecf20Sopenharmony_ci#include "wep.h" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_civoid ieee80211_wep_init(struct ieee80211_local *local) 268c2ecf20Sopenharmony_ci{ 278c2ecf20Sopenharmony_ci /* start WEP IV from a random value */ 288c2ecf20Sopenharmony_ci get_random_bytes(&local->wep_iv, IEEE80211_WEP_IV_LEN); 298c2ecf20Sopenharmony_ci} 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic inline bool ieee80211_wep_weak_iv(u32 iv, int keylen) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci /* 348c2ecf20Sopenharmony_ci * Fluhrer, Mantin, and Shamir have reported weaknesses in the 358c2ecf20Sopenharmony_ci * key scheduling algorithm of RC4. At least IVs (KeyByte + 3, 368c2ecf20Sopenharmony_ci * 0xff, N) can be used to speedup attacks, so avoid using them. 378c2ecf20Sopenharmony_ci */ 388c2ecf20Sopenharmony_ci if ((iv & 0xff00) == 0xff00) { 398c2ecf20Sopenharmony_ci u8 B = (iv >> 16) & 0xff; 408c2ecf20Sopenharmony_ci if (B >= 3 && B < 3 + keylen) 418c2ecf20Sopenharmony_ci return true; 428c2ecf20Sopenharmony_ci } 438c2ecf20Sopenharmony_ci return false; 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_cistatic void ieee80211_wep_get_iv(struct ieee80211_local *local, 488c2ecf20Sopenharmony_ci int keylen, int keyidx, u8 *iv) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci local->wep_iv++; 518c2ecf20Sopenharmony_ci if (ieee80211_wep_weak_iv(local->wep_iv, keylen)) 528c2ecf20Sopenharmony_ci local->wep_iv += 0x0100; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci if (!iv) 558c2ecf20Sopenharmony_ci return; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci *iv++ = (local->wep_iv >> 16) & 0xff; 588c2ecf20Sopenharmony_ci *iv++ = (local->wep_iv >> 8) & 0xff; 598c2ecf20Sopenharmony_ci *iv++ = local->wep_iv & 0xff; 608c2ecf20Sopenharmony_ci *iv++ = keyidx << 6; 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic u8 *ieee80211_wep_add_iv(struct ieee80211_local *local, 658c2ecf20Sopenharmony_ci struct sk_buff *skb, 668c2ecf20Sopenharmony_ci int keylen, int keyidx) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 698c2ecf20Sopenharmony_ci struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 708c2ecf20Sopenharmony_ci unsigned int hdrlen; 718c2ecf20Sopenharmony_ci u8 *newhdr; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED); 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci if (WARN_ON(skb_headroom(skb) < IEEE80211_WEP_IV_LEN)) 768c2ecf20Sopenharmony_ci return NULL; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci hdrlen = ieee80211_hdrlen(hdr->frame_control); 798c2ecf20Sopenharmony_ci newhdr = skb_push(skb, IEEE80211_WEP_IV_LEN); 808c2ecf20Sopenharmony_ci memmove(newhdr, newhdr + IEEE80211_WEP_IV_LEN, hdrlen); 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci /* the HW only needs room for the IV, but not the actual IV */ 838c2ecf20Sopenharmony_ci if (info->control.hw_key && 848c2ecf20Sopenharmony_ci (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) 858c2ecf20Sopenharmony_ci return newhdr + hdrlen; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci ieee80211_wep_get_iv(local, keylen, keyidx, newhdr + hdrlen); 888c2ecf20Sopenharmony_ci return newhdr + hdrlen; 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic void ieee80211_wep_remove_iv(struct ieee80211_local *local, 938c2ecf20Sopenharmony_ci struct sk_buff *skb, 948c2ecf20Sopenharmony_ci struct ieee80211_key *key) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 978c2ecf20Sopenharmony_ci unsigned int hdrlen; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci hdrlen = ieee80211_hdrlen(hdr->frame_control); 1008c2ecf20Sopenharmony_ci memmove(skb->data + IEEE80211_WEP_IV_LEN, skb->data, hdrlen); 1018c2ecf20Sopenharmony_ci skb_pull(skb, IEEE80211_WEP_IV_LEN); 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci/* Perform WEP encryption using given key. data buffer must have tailroom 1068c2ecf20Sopenharmony_ci * for 4-byte ICV. data_len must not include this ICV. Note: this function 1078c2ecf20Sopenharmony_ci * does _not_ add IV. data = RC4(data | CRC32(data)) */ 1088c2ecf20Sopenharmony_ciint ieee80211_wep_encrypt_data(struct arc4_ctx *ctx, u8 *rc4key, 1098c2ecf20Sopenharmony_ci size_t klen, u8 *data, size_t data_len) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci __le32 icv; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci icv = cpu_to_le32(~crc32_le(~0, data, data_len)); 1148c2ecf20Sopenharmony_ci put_unaligned(icv, (__le32 *)(data + data_len)); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci arc4_setkey(ctx, rc4key, klen); 1178c2ecf20Sopenharmony_ci arc4_crypt(ctx, data, data, data_len + IEEE80211_WEP_ICV_LEN); 1188c2ecf20Sopenharmony_ci memzero_explicit(ctx, sizeof(*ctx)); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci return 0; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci/* Perform WEP encryption on given skb. 4 bytes of extra space (IV) in the 1258c2ecf20Sopenharmony_ci * beginning of the buffer 4 bytes of extra space (ICV) in the end of the 1268c2ecf20Sopenharmony_ci * buffer will be added. Both IV and ICV will be transmitted, so the 1278c2ecf20Sopenharmony_ci * payload length increases with 8 bytes. 1288c2ecf20Sopenharmony_ci * 1298c2ecf20Sopenharmony_ci * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data)) 1308c2ecf20Sopenharmony_ci */ 1318c2ecf20Sopenharmony_ciint ieee80211_wep_encrypt(struct ieee80211_local *local, 1328c2ecf20Sopenharmony_ci struct sk_buff *skb, 1338c2ecf20Sopenharmony_ci const u8 *key, int keylen, int keyidx) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci u8 *iv; 1368c2ecf20Sopenharmony_ci size_t len; 1378c2ecf20Sopenharmony_ci u8 rc4key[3 + WLAN_KEY_LEN_WEP104]; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci if (WARN_ON(skb_tailroom(skb) < IEEE80211_WEP_ICV_LEN)) 1408c2ecf20Sopenharmony_ci return -1; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci iv = ieee80211_wep_add_iv(local, skb, keylen, keyidx); 1438c2ecf20Sopenharmony_ci if (!iv) 1448c2ecf20Sopenharmony_ci return -1; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci len = skb->len - (iv + IEEE80211_WEP_IV_LEN - skb->data); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci /* Prepend 24-bit IV to RC4 key */ 1498c2ecf20Sopenharmony_ci memcpy(rc4key, iv, 3); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci /* Copy rest of the WEP key (the secret part) */ 1528c2ecf20Sopenharmony_ci memcpy(rc4key + 3, key, keylen); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci /* Add room for ICV */ 1558c2ecf20Sopenharmony_ci skb_put(skb, IEEE80211_WEP_ICV_LEN); 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci return ieee80211_wep_encrypt_data(&local->wep_tx_ctx, rc4key, keylen + 3, 1588c2ecf20Sopenharmony_ci iv + IEEE80211_WEP_IV_LEN, len); 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci/* Perform WEP decryption using given key. data buffer includes encrypted 1638c2ecf20Sopenharmony_ci * payload, including 4-byte ICV, but _not_ IV. data_len must not include ICV. 1648c2ecf20Sopenharmony_ci * Return 0 on success and -1 on ICV mismatch. */ 1658c2ecf20Sopenharmony_ciint ieee80211_wep_decrypt_data(struct arc4_ctx *ctx, u8 *rc4key, 1668c2ecf20Sopenharmony_ci size_t klen, u8 *data, size_t data_len) 1678c2ecf20Sopenharmony_ci{ 1688c2ecf20Sopenharmony_ci __le32 crc; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci arc4_setkey(ctx, rc4key, klen); 1718c2ecf20Sopenharmony_ci arc4_crypt(ctx, data, data, data_len + IEEE80211_WEP_ICV_LEN); 1728c2ecf20Sopenharmony_ci memzero_explicit(ctx, sizeof(*ctx)); 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci crc = cpu_to_le32(~crc32_le(~0, data, data_len)); 1758c2ecf20Sopenharmony_ci if (memcmp(&crc, data + data_len, IEEE80211_WEP_ICV_LEN) != 0) 1768c2ecf20Sopenharmony_ci /* ICV mismatch */ 1778c2ecf20Sopenharmony_ci return -1; 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci return 0; 1808c2ecf20Sopenharmony_ci} 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci/* Perform WEP decryption on given skb. Buffer includes whole WEP part of 1848c2ecf20Sopenharmony_ci * the frame: IV (4 bytes), encrypted payload (including SNAP header), 1858c2ecf20Sopenharmony_ci * ICV (4 bytes). skb->len includes both IV and ICV. 1868c2ecf20Sopenharmony_ci * 1878c2ecf20Sopenharmony_ci * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on 1888c2ecf20Sopenharmony_ci * failure. If frame is OK, IV and ICV will be removed, i.e., decrypted payload 1898c2ecf20Sopenharmony_ci * is moved to the beginning of the skb and skb length will be reduced. 1908c2ecf20Sopenharmony_ci */ 1918c2ecf20Sopenharmony_cistatic int ieee80211_wep_decrypt(struct ieee80211_local *local, 1928c2ecf20Sopenharmony_ci struct sk_buff *skb, 1938c2ecf20Sopenharmony_ci struct ieee80211_key *key) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci u32 klen; 1968c2ecf20Sopenharmony_ci u8 rc4key[3 + WLAN_KEY_LEN_WEP104]; 1978c2ecf20Sopenharmony_ci u8 keyidx; 1988c2ecf20Sopenharmony_ci struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 1998c2ecf20Sopenharmony_ci unsigned int hdrlen; 2008c2ecf20Sopenharmony_ci size_t len; 2018c2ecf20Sopenharmony_ci int ret = 0; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci if (!ieee80211_has_protected(hdr->frame_control)) 2048c2ecf20Sopenharmony_ci return -1; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci hdrlen = ieee80211_hdrlen(hdr->frame_control); 2078c2ecf20Sopenharmony_ci if (skb->len < hdrlen + IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN) 2088c2ecf20Sopenharmony_ci return -1; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci len = skb->len - hdrlen - IEEE80211_WEP_IV_LEN - IEEE80211_WEP_ICV_LEN; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci keyidx = skb->data[hdrlen + 3] >> 6; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci if (!key || keyidx != key->conf.keyidx) 2158c2ecf20Sopenharmony_ci return -1; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci klen = 3 + key->conf.keylen; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci /* Prepend 24-bit IV to RC4 key */ 2208c2ecf20Sopenharmony_ci memcpy(rc4key, skb->data + hdrlen, 3); 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci /* Copy rest of the WEP key (the secret part) */ 2238c2ecf20Sopenharmony_ci memcpy(rc4key + 3, key->conf.key, key->conf.keylen); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci if (ieee80211_wep_decrypt_data(&local->wep_rx_ctx, rc4key, klen, 2268c2ecf20Sopenharmony_ci skb->data + hdrlen + 2278c2ecf20Sopenharmony_ci IEEE80211_WEP_IV_LEN, len)) 2288c2ecf20Sopenharmony_ci ret = -1; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci /* Trim ICV */ 2318c2ecf20Sopenharmony_ci skb_trim(skb, skb->len - IEEE80211_WEP_ICV_LEN); 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci /* Remove IV */ 2348c2ecf20Sopenharmony_ci memmove(skb->data + IEEE80211_WEP_IV_LEN, skb->data, hdrlen); 2358c2ecf20Sopenharmony_ci skb_pull(skb, IEEE80211_WEP_IV_LEN); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci return ret; 2388c2ecf20Sopenharmony_ci} 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ciieee80211_rx_result 2418c2ecf20Sopenharmony_ciieee80211_crypto_wep_decrypt(struct ieee80211_rx_data *rx) 2428c2ecf20Sopenharmony_ci{ 2438c2ecf20Sopenharmony_ci struct sk_buff *skb = rx->skb; 2448c2ecf20Sopenharmony_ci struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 2458c2ecf20Sopenharmony_ci struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 2468c2ecf20Sopenharmony_ci __le16 fc = hdr->frame_control; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci if (!ieee80211_is_data(fc) && !ieee80211_is_auth(fc)) 2498c2ecf20Sopenharmony_ci return RX_CONTINUE; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci if (!(status->flag & RX_FLAG_DECRYPTED)) { 2528c2ecf20Sopenharmony_ci if (skb_linearize(rx->skb)) 2538c2ecf20Sopenharmony_ci return RX_DROP_UNUSABLE; 2548c2ecf20Sopenharmony_ci if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) 2558c2ecf20Sopenharmony_ci return RX_DROP_UNUSABLE; 2568c2ecf20Sopenharmony_ci } else if (!(status->flag & RX_FLAG_IV_STRIPPED)) { 2578c2ecf20Sopenharmony_ci if (!pskb_may_pull(rx->skb, ieee80211_hdrlen(fc) + 2588c2ecf20Sopenharmony_ci IEEE80211_WEP_IV_LEN)) 2598c2ecf20Sopenharmony_ci return RX_DROP_UNUSABLE; 2608c2ecf20Sopenharmony_ci ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key); 2618c2ecf20Sopenharmony_ci /* remove ICV */ 2628c2ecf20Sopenharmony_ci if (!(status->flag & RX_FLAG_ICV_STRIPPED) && 2638c2ecf20Sopenharmony_ci pskb_trim(rx->skb, rx->skb->len - IEEE80211_WEP_ICV_LEN)) 2648c2ecf20Sopenharmony_ci return RX_DROP_UNUSABLE; 2658c2ecf20Sopenharmony_ci } 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci return RX_CONTINUE; 2688c2ecf20Sopenharmony_ci} 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_cistatic int wep_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb) 2718c2ecf20Sopenharmony_ci{ 2728c2ecf20Sopenharmony_ci struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 2738c2ecf20Sopenharmony_ci struct ieee80211_key_conf *hw_key = info->control.hw_key; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci if (!hw_key) { 2768c2ecf20Sopenharmony_ci if (ieee80211_wep_encrypt(tx->local, skb, tx->key->conf.key, 2778c2ecf20Sopenharmony_ci tx->key->conf.keylen, 2788c2ecf20Sopenharmony_ci tx->key->conf.keyidx)) 2798c2ecf20Sopenharmony_ci return -1; 2808c2ecf20Sopenharmony_ci } else if ((hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) || 2818c2ecf20Sopenharmony_ci (hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) { 2828c2ecf20Sopenharmony_ci if (!ieee80211_wep_add_iv(tx->local, skb, 2838c2ecf20Sopenharmony_ci tx->key->conf.keylen, 2848c2ecf20Sopenharmony_ci tx->key->conf.keyidx)) 2858c2ecf20Sopenharmony_ci return -1; 2868c2ecf20Sopenharmony_ci } 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci return 0; 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ciieee80211_tx_result 2928c2ecf20Sopenharmony_ciieee80211_crypto_wep_encrypt(struct ieee80211_tx_data *tx) 2938c2ecf20Sopenharmony_ci{ 2948c2ecf20Sopenharmony_ci struct sk_buff *skb; 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci ieee80211_tx_set_protected(tx); 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci skb_queue_walk(&tx->skbs, skb) { 2998c2ecf20Sopenharmony_ci if (wep_encrypt_skb(tx, skb) < 0) { 3008c2ecf20Sopenharmony_ci I802_DEBUG_INC(tx->local->tx_handlers_drop_wep); 3018c2ecf20Sopenharmony_ci return TX_DROP; 3028c2ecf20Sopenharmony_ci } 3038c2ecf20Sopenharmony_ci } 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci return TX_CONTINUE; 3068c2ecf20Sopenharmony_ci} 307