1f0706e82SJiri Benc /* 2f0706e82SJiri Benc * Software WEP encryption implementation 3f0706e82SJiri Benc * Copyright 2002, Jouni Malinen <jkmaline@cc.hut.fi> 4f0706e82SJiri Benc * Copyright 2003, Instant802 Networks, Inc. 5f0706e82SJiri Benc * 6f0706e82SJiri Benc * This program is free software; you can redistribute it and/or modify 7f0706e82SJiri Benc * it under the terms of the GNU General Public License version 2 as 8f0706e82SJiri Benc * published by the Free Software Foundation. 9f0706e82SJiri Benc */ 10f0706e82SJiri Benc 11f0706e82SJiri Benc #include <linux/netdevice.h> 12f0706e82SJiri Benc #include <linux/types.h> 13f0706e82SJiri Benc #include <linux/random.h> 14f0706e82SJiri Benc #include <linux/compiler.h> 15f0706e82SJiri Benc #include <linux/crc32.h> 16f0706e82SJiri Benc #include <linux/crypto.h> 17f0706e82SJiri Benc #include <linux/err.h> 18f0706e82SJiri Benc #include <linux/mm.h> 1911763609SRalf Baechle #include <linux/scatterlist.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 21860c6e6aSIvan Kuten #include <asm/unaligned.h> 22f0706e82SJiri Benc 23f0706e82SJiri Benc #include <net/mac80211.h> 24f0706e82SJiri Benc #include "ieee80211_i.h" 25f0706e82SJiri Benc #include "wep.h" 26f0706e82SJiri Benc 27f0706e82SJiri Benc 28f0706e82SJiri Benc int ieee80211_wep_init(struct ieee80211_local *local) 29f0706e82SJiri Benc { 30f0706e82SJiri Benc /* start WEP IV from a random value */ 314325f6caSJohannes Berg get_random_bytes(&local->wep_iv, IEEE80211_WEP_IV_LEN); 32f0706e82SJiri Benc 335f9f1812SFelix Fietkau local->wep_tx_tfm = crypto_alloc_cipher("arc4", 0, CRYPTO_ALG_ASYNC); 34088c8726SJohn W. Linville if (IS_ERR(local->wep_tx_tfm)) { 35088c8726SJohn W. Linville local->wep_rx_tfm = ERR_PTR(-EINVAL); 36023a04beSJeremy Fitzhardinge return PTR_ERR(local->wep_tx_tfm); 37088c8726SJohn W. Linville } 38f0706e82SJiri Benc 395f9f1812SFelix Fietkau local->wep_rx_tfm = crypto_alloc_cipher("arc4", 0, CRYPTO_ALG_ASYNC); 40f0706e82SJiri Benc if (IS_ERR(local->wep_rx_tfm)) { 415f9f1812SFelix Fietkau crypto_free_cipher(local->wep_tx_tfm); 42088c8726SJohn W. Linville local->wep_tx_tfm = ERR_PTR(-EINVAL); 43023a04beSJeremy Fitzhardinge return PTR_ERR(local->wep_rx_tfm); 44f0706e82SJiri Benc } 45f0706e82SJiri Benc 46f0706e82SJiri Benc return 0; 47f0706e82SJiri Benc } 48f0706e82SJiri Benc 49f0706e82SJiri Benc void ieee80211_wep_free(struct ieee80211_local *local) 50f0706e82SJiri Benc { 513473187dSJohn W. Linville if (!IS_ERR(local->wep_tx_tfm)) 525f9f1812SFelix Fietkau crypto_free_cipher(local->wep_tx_tfm); 533473187dSJohn W. Linville if (!IS_ERR(local->wep_rx_tfm)) 545f9f1812SFelix Fietkau crypto_free_cipher(local->wep_rx_tfm); 55f0706e82SJiri Benc } 56f0706e82SJiri Benc 57c6a1fa12SJohannes Berg static inline bool ieee80211_wep_weak_iv(u32 iv, int keylen) 58f0706e82SJiri Benc { 59c6a1fa12SJohannes Berg /* 60c6a1fa12SJohannes Berg * Fluhrer, Mantin, and Shamir have reported weaknesses in the 61f0706e82SJiri Benc * key scheduling algorithm of RC4. At least IVs (KeyByte + 3, 62c6a1fa12SJohannes Berg * 0xff, N) can be used to speedup attacks, so avoid using them. 63c6a1fa12SJohannes Berg */ 64f0706e82SJiri Benc if ((iv & 0xff00) == 0xff00) { 65f0706e82SJiri Benc u8 B = (iv >> 16) & 0xff; 66f0706e82SJiri Benc if (B >= 3 && B < 3 + keylen) 67c6a1fa12SJohannes Berg return true; 68f0706e82SJiri Benc } 69c6a1fa12SJohannes Berg return false; 70f0706e82SJiri Benc } 71f0706e82SJiri Benc 72f0706e82SJiri Benc 734f0d18e2SJohannes Berg static void ieee80211_wep_get_iv(struct ieee80211_local *local, 74c9cf0122SJohannes Berg int keylen, int keyidx, u8 *iv) 75f0706e82SJiri Benc { 76f0706e82SJiri Benc local->wep_iv++; 77c9cf0122SJohannes Berg if (ieee80211_wep_weak_iv(local->wep_iv, keylen)) 78f0706e82SJiri Benc local->wep_iv += 0x0100; 79f0706e82SJiri Benc 80f0706e82SJiri Benc if (!iv) 81f0706e82SJiri Benc return; 82f0706e82SJiri Benc 83f0706e82SJiri Benc *iv++ = (local->wep_iv >> 16) & 0xff; 84f0706e82SJiri Benc *iv++ = (local->wep_iv >> 8) & 0xff; 85f0706e82SJiri Benc *iv++ = local->wep_iv & 0xff; 86c9cf0122SJohannes Berg *iv++ = keyidx << 6; 87f0706e82SJiri Benc } 88f0706e82SJiri Benc 89f0706e82SJiri Benc 906a22a59dSJohannes Berg static u8 *ieee80211_wep_add_iv(struct ieee80211_local *local, 91f0706e82SJiri Benc struct sk_buff *skb, 92c9cf0122SJohannes Berg int keylen, int keyidx) 93f0706e82SJiri Benc { 94f0706e82SJiri Benc struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 95ee70108fSJanusz.Dziedzic@tieto.com struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 9670217d7fSHarvey Harrison unsigned int hdrlen; 97f0706e82SJiri Benc u8 *newhdr; 98f0706e82SJiri Benc 9970217d7fSHarvey Harrison hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED); 100f0706e82SJiri Benc 10147b4e1fcSJanusz Dziedzic if (WARN_ON(skb_headroom(skb) < IEEE80211_WEP_IV_LEN)) 102f0706e82SJiri Benc return NULL; 103f0706e82SJiri Benc 10470217d7fSHarvey Harrison hdrlen = ieee80211_hdrlen(hdr->frame_control); 1054325f6caSJohannes Berg newhdr = skb_push(skb, IEEE80211_WEP_IV_LEN); 1064325f6caSJohannes Berg memmove(newhdr, newhdr + IEEE80211_WEP_IV_LEN, hdrlen); 107ee70108fSJanusz.Dziedzic@tieto.com 108ee70108fSJanusz.Dziedzic@tieto.com /* the HW only needs room for the IV, but not the actual IV */ 109ee70108fSJanusz.Dziedzic@tieto.com if (info->control.hw_key && 110ee70108fSJanusz.Dziedzic@tieto.com (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) 111ee70108fSJanusz.Dziedzic@tieto.com return newhdr + hdrlen; 112ee70108fSJanusz.Dziedzic@tieto.com 113c9cf0122SJohannes Berg ieee80211_wep_get_iv(local, keylen, keyidx, newhdr + hdrlen); 114f0706e82SJiri Benc return newhdr + hdrlen; 115f0706e82SJiri Benc } 116f0706e82SJiri Benc 117f0706e82SJiri Benc 1184f0d18e2SJohannes Berg static void ieee80211_wep_remove_iv(struct ieee80211_local *local, 119f0706e82SJiri Benc struct sk_buff *skb, 120f0706e82SJiri Benc struct ieee80211_key *key) 121f0706e82SJiri Benc { 122f0706e82SJiri Benc struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 12370217d7fSHarvey Harrison unsigned int hdrlen; 124f0706e82SJiri Benc 12570217d7fSHarvey Harrison hdrlen = ieee80211_hdrlen(hdr->frame_control); 1264325f6caSJohannes Berg memmove(skb->data + IEEE80211_WEP_IV_LEN, skb->data, hdrlen); 1274325f6caSJohannes Berg skb_pull(skb, IEEE80211_WEP_IV_LEN); 128f0706e82SJiri Benc } 129f0706e82SJiri Benc 130f0706e82SJiri Benc 131f0706e82SJiri Benc /* Perform WEP encryption using given key. data buffer must have tailroom 132f0706e82SJiri Benc * for 4-byte ICV. data_len must not include this ICV. Note: this function 133f0706e82SJiri Benc * does _not_ add IV. data = RC4(data | CRC32(data)) */ 1345f9f1812SFelix Fietkau int ieee80211_wep_encrypt_data(struct crypto_cipher *tfm, u8 *rc4key, 135f0706e82SJiri Benc size_t klen, u8 *data, size_t data_len) 136f0706e82SJiri Benc { 137860c6e6aSIvan Kuten __le32 icv; 1385f9f1812SFelix Fietkau int i; 139f0706e82SJiri Benc 1403473187dSJohn W. Linville if (IS_ERR(tfm)) 1413473187dSJohn W. Linville return -1; 1423473187dSJohn W. Linville 143860c6e6aSIvan Kuten icv = cpu_to_le32(~crc32_le(~0, data, data_len)); 144860c6e6aSIvan Kuten put_unaligned(icv, (__le32 *)(data + data_len)); 145f0706e82SJiri Benc 1465f9f1812SFelix Fietkau crypto_cipher_setkey(tfm, rc4key, klen); 1474325f6caSJohannes Berg for (i = 0; i < data_len + IEEE80211_WEP_ICV_LEN; i++) 1485f9f1812SFelix Fietkau crypto_cipher_encrypt_one(tfm, data + i, data + i); 1493473187dSJohn W. Linville 1503473187dSJohn W. Linville return 0; 151f0706e82SJiri Benc } 152f0706e82SJiri Benc 153f0706e82SJiri Benc 154f0706e82SJiri Benc /* Perform WEP encryption on given skb. 4 bytes of extra space (IV) in the 155f0706e82SJiri Benc * beginning of the buffer 4 bytes of extra space (ICV) in the end of the 156f0706e82SJiri Benc * buffer will be added. Both IV and ICV will be transmitted, so the 157f0706e82SJiri Benc * payload length increases with 8 bytes. 158f0706e82SJiri Benc * 159f0706e82SJiri Benc * WEP frame payload: IV + TX key idx, RC4(data), ICV = RC4(CRC32(data)) 160f0706e82SJiri Benc */ 161fffd0934SJohannes Berg int ieee80211_wep_encrypt(struct ieee80211_local *local, 162c9cf0122SJohannes Berg struct sk_buff *skb, 163c9cf0122SJohannes Berg const u8 *key, int keylen, int keyidx) 164f0706e82SJiri Benc { 165c9cf0122SJohannes Berg u8 *iv; 166f0706e82SJiri Benc size_t len; 167c9cf0122SJohannes Berg u8 rc4key[3 + WLAN_KEY_LEN_WEP104]; 168f0706e82SJiri Benc 16947b4e1fcSJanusz Dziedzic if (WARN_ON(skb_tailroom(skb) < IEEE80211_WEP_ICV_LEN)) 17047b4e1fcSJanusz Dziedzic return -1; 17147b4e1fcSJanusz Dziedzic 172c9cf0122SJohannes Berg iv = ieee80211_wep_add_iv(local, skb, keylen, keyidx); 173c9cf0122SJohannes Berg if (!iv) 174f0706e82SJiri Benc return -1; 175f0706e82SJiri Benc 1764325f6caSJohannes Berg len = skb->len - (iv + IEEE80211_WEP_IV_LEN - skb->data); 177f0706e82SJiri Benc 178f0706e82SJiri Benc /* Prepend 24-bit IV to RC4 key */ 179f0706e82SJiri Benc memcpy(rc4key, iv, 3); 180f0706e82SJiri Benc 181f0706e82SJiri Benc /* Copy rest of the WEP key (the secret part) */ 182c9cf0122SJohannes Berg memcpy(rc4key + 3, key, keylen); 183f0706e82SJiri Benc 184f0706e82SJiri Benc /* Add room for ICV */ 1854325f6caSJohannes Berg skb_put(skb, IEEE80211_WEP_ICV_LEN); 186f0706e82SJiri Benc 1873473187dSJohn W. Linville return ieee80211_wep_encrypt_data(local->wep_tx_tfm, rc4key, keylen + 3, 1884325f6caSJohannes Berg iv + IEEE80211_WEP_IV_LEN, len); 189f0706e82SJiri Benc } 190f0706e82SJiri Benc 191f0706e82SJiri Benc 192f0706e82SJiri Benc /* Perform WEP decryption using given key. data buffer includes encrypted 193f0706e82SJiri Benc * payload, including 4-byte ICV, but _not_ IV. data_len must not include ICV. 194f0706e82SJiri Benc * Return 0 on success and -1 on ICV mismatch. */ 1955f9f1812SFelix Fietkau int ieee80211_wep_decrypt_data(struct crypto_cipher *tfm, u8 *rc4key, 196f0706e82SJiri Benc size_t klen, u8 *data, size_t data_len) 197f0706e82SJiri Benc { 198f0706e82SJiri Benc __le32 crc; 1995f9f1812SFelix Fietkau int i; 200f0706e82SJiri Benc 2013473187dSJohn W. Linville if (IS_ERR(tfm)) 2023473187dSJohn W. Linville return -1; 2033473187dSJohn W. Linville 2045f9f1812SFelix Fietkau crypto_cipher_setkey(tfm, rc4key, klen); 2054325f6caSJohannes Berg for (i = 0; i < data_len + IEEE80211_WEP_ICV_LEN; i++) 2065f9f1812SFelix Fietkau crypto_cipher_decrypt_one(tfm, data + i, data + i); 207f0706e82SJiri Benc 208f0706e82SJiri Benc crc = cpu_to_le32(~crc32_le(~0, data, data_len)); 2094325f6caSJohannes Berg if (memcmp(&crc, data + data_len, IEEE80211_WEP_ICV_LEN) != 0) 210f0706e82SJiri Benc /* ICV mismatch */ 211f0706e82SJiri Benc return -1; 212f0706e82SJiri Benc 213f0706e82SJiri Benc return 0; 214f0706e82SJiri Benc } 215f0706e82SJiri Benc 216f0706e82SJiri Benc 217f0706e82SJiri Benc /* Perform WEP decryption on given skb. Buffer includes whole WEP part of 218f0706e82SJiri Benc * the frame: IV (4 bytes), encrypted payload (including SNAP header), 219f0706e82SJiri Benc * ICV (4 bytes). skb->len includes both IV and ICV. 220f0706e82SJiri Benc * 221f0706e82SJiri Benc * Returns 0 if frame was decrypted successfully and ICV was correct and -1 on 222f0706e82SJiri Benc * failure. If frame is OK, IV and ICV will be removed, i.e., decrypted payload 223f0706e82SJiri Benc * is moved to the beginning of the skb and skb length will be reduced. 224f0706e82SJiri Benc */ 225c9cf0122SJohannes Berg static int ieee80211_wep_decrypt(struct ieee80211_local *local, 226c9cf0122SJohannes Berg struct sk_buff *skb, 227f0706e82SJiri Benc struct ieee80211_key *key) 228f0706e82SJiri Benc { 229f0706e82SJiri Benc u32 klen; 230730bd83bSJohannes Berg u8 rc4key[3 + WLAN_KEY_LEN_WEP104]; 231f0706e82SJiri Benc u8 keyidx; 232f0706e82SJiri Benc struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 23370217d7fSHarvey Harrison unsigned int hdrlen; 234f0706e82SJiri Benc size_t len; 235f0706e82SJiri Benc int ret = 0; 236f0706e82SJiri Benc 23770217d7fSHarvey Harrison if (!ieee80211_has_protected(hdr->frame_control)) 238f0706e82SJiri Benc return -1; 239f0706e82SJiri Benc 24070217d7fSHarvey Harrison hdrlen = ieee80211_hdrlen(hdr->frame_control); 2414325f6caSJohannes Berg if (skb->len < hdrlen + IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN) 242f0706e82SJiri Benc return -1; 243f0706e82SJiri Benc 2444325f6caSJohannes Berg len = skb->len - hdrlen - IEEE80211_WEP_IV_LEN - IEEE80211_WEP_ICV_LEN; 245f0706e82SJiri Benc 246f0706e82SJiri Benc keyidx = skb->data[hdrlen + 3] >> 6; 247f0706e82SJiri Benc 24897359d12SJohannes Berg if (!key || keyidx != key->conf.keyidx) 249f0706e82SJiri Benc return -1; 250f0706e82SJiri Benc 2518f20fc24SJohannes Berg klen = 3 + key->conf.keylen; 252f0706e82SJiri Benc 253f0706e82SJiri Benc /* Prepend 24-bit IV to RC4 key */ 254f0706e82SJiri Benc memcpy(rc4key, skb->data + hdrlen, 3); 255f0706e82SJiri Benc 256f0706e82SJiri Benc /* Copy rest of the WEP key (the secret part) */ 2578f20fc24SJohannes Berg memcpy(rc4key + 3, key->conf.key, key->conf.keylen); 258f0706e82SJiri Benc 259f0706e82SJiri Benc if (ieee80211_wep_decrypt_data(local->wep_rx_tfm, rc4key, klen, 2604325f6caSJohannes Berg skb->data + hdrlen + 2614325f6caSJohannes Berg IEEE80211_WEP_IV_LEN, len)) 262f0706e82SJiri Benc ret = -1; 263f0706e82SJiri Benc 264f0706e82SJiri Benc /* Trim ICV */ 2654325f6caSJohannes Berg skb_trim(skb, skb->len - IEEE80211_WEP_ICV_LEN); 266f0706e82SJiri Benc 267f0706e82SJiri Benc /* Remove IV */ 2684325f6caSJohannes Berg memmove(skb->data + IEEE80211_WEP_IV_LEN, skb->data, hdrlen); 2694325f6caSJohannes Berg skb_pull(skb, IEEE80211_WEP_IV_LEN); 270f0706e82SJiri Benc 271f0706e82SJiri Benc return ret; 272f0706e82SJiri Benc } 273f0706e82SJiri Benc 2749ae54c84SJohannes Berg ieee80211_rx_result 2755cf121c3SJohannes Berg ieee80211_crypto_wep_decrypt(struct ieee80211_rx_data *rx) 2764f0d18e2SJohannes Berg { 277eb9fb5b8SJohannes Berg struct sk_buff *skb = rx->skb; 278eb9fb5b8SJohannes Berg struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 279eb9fb5b8SJohannes Berg struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 280a8286911SJohannes Berg __le16 fc = hdr->frame_control; 281358c8d9dSHarvey Harrison 282a8286911SJohannes Berg if (!ieee80211_is_data(fc) && !ieee80211_is_auth(fc)) 2839ae54c84SJohannes Berg return RX_CONTINUE; 2844f0d18e2SJohannes Berg 285eb9fb5b8SJohannes Berg if (!(status->flag & RX_FLAG_DECRYPTED)) { 286a8286911SJohannes Berg if (skb_linearize(rx->skb)) 287a8286911SJohannes Berg return RX_DROP_UNUSABLE; 288f4ea83ddSJohannes Berg if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) 289e4c26addSJohannes Berg return RX_DROP_UNUSABLE; 290eb9fb5b8SJohannes Berg } else if (!(status->flag & RX_FLAG_IV_STRIPPED)) { 2914325f6caSJohannes Berg if (!pskb_may_pull(rx->skb, ieee80211_hdrlen(fc) + 2924325f6caSJohannes Berg IEEE80211_WEP_IV_LEN)) 293a8286911SJohannes Berg return RX_DROP_UNUSABLE; 2944f0d18e2SJohannes Berg ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key); 2954f0d18e2SJohannes Berg /* remove ICV */ 296*cef0acd4SDavid Spinadel if (!(status->flag & RX_FLAG_ICV_STRIPPED) && 297*cef0acd4SDavid Spinadel pskb_trim(rx->skb, rx->skb->len - IEEE80211_WEP_ICV_LEN)) 298a8286911SJohannes Berg return RX_DROP_UNUSABLE; 2994f0d18e2SJohannes Berg } 3004f0d18e2SJohannes Berg 3019ae54c84SJohannes Berg return RX_CONTINUE; 3024f0d18e2SJohannes Berg } 3036a22a59dSJohannes Berg 3045cf121c3SJohannes Berg static int wep_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb) 3056a22a59dSJohannes Berg { 306e039fa4aSJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 307ee70108fSJanusz.Dziedzic@tieto.com struct ieee80211_key_conf *hw_key = info->control.hw_key; 308e039fa4aSJohannes Berg 309ee70108fSJanusz.Dziedzic@tieto.com if (!hw_key) { 310c9cf0122SJohannes Berg if (ieee80211_wep_encrypt(tx->local, skb, tx->key->conf.key, 311c9cf0122SJohannes Berg tx->key->conf.keylen, 312c9cf0122SJohannes Berg tx->key->conf.keyidx)) 3136a22a59dSJohannes Berg return -1; 314ee70108fSJanusz.Dziedzic@tieto.com } else if ((hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) || 315ee70108fSJanusz.Dziedzic@tieto.com (hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) { 316c9cf0122SJohannes Berg if (!ieee80211_wep_add_iv(tx->local, skb, 317c9cf0122SJohannes Berg tx->key->conf.keylen, 318c9cf0122SJohannes Berg tx->key->conf.keyidx)) 3196a22a59dSJohannes Berg return -1; 3206a22a59dSJohannes Berg } 321813d7669SJohannes Berg 3226a22a59dSJohannes Berg return 0; 3236a22a59dSJohannes Berg } 3246a22a59dSJohannes Berg 3259ae54c84SJohannes Berg ieee80211_tx_result 3265cf121c3SJohannes Berg ieee80211_crypto_wep_encrypt(struct ieee80211_tx_data *tx) 3276a22a59dSJohannes Berg { 3282de8e0d9SJohannes Berg struct sk_buff *skb; 329c6a1fa12SJohannes Berg 3305cf121c3SJohannes Berg ieee80211_tx_set_protected(tx); 3316a22a59dSJohannes Berg 332252b86c4SJohannes Berg skb_queue_walk(&tx->skbs, skb) { 3332de8e0d9SJohannes Berg if (wep_encrypt_skb(tx, skb) < 0) { 3346a22a59dSJohannes Berg I802_DEBUG_INC(tx->local->tx_handlers_drop_wep); 3359ae54c84SJohannes Berg return TX_DROP; 3366a22a59dSJohannes Berg } 337252b86c4SJohannes Berg } 3386a22a59dSJohannes Berg 3399ae54c84SJohannes Berg return TX_CONTINUE; 3406a22a59dSJohannes Berg } 341