^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * lib80211 crypt: host-based TKIP encryption implementation for lib80211
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/fips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <asm/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/wireless.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/ieee80211.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <net/iw_handler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <crypto/arc4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <net/lib80211.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) MODULE_AUTHOR("Jouni Malinen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) MODULE_DESCRIPTION("lib80211 crypt: TKIP");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define TKIP_HDR_LEN 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct lib80211_tkip_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define TKIP_KEY_LEN 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u8 key[TKIP_KEY_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int key_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) u32 tx_iv32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) u16 tx_iv16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u16 tx_ttak[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int tx_phase1_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) u32 rx_iv32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) u16 rx_iv16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u16 rx_ttak[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int rx_phase1_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) u32 rx_iv32_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) u16 rx_iv16_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) u32 dot11RSNAStatsTKIPReplays;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) u32 dot11RSNAStatsTKIPICVErrors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) u32 dot11RSNAStatsTKIPLocalMICFailures;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int key_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct arc4_ctx rx_ctx_arc4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct arc4_ctx tx_ctx_arc4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct crypto_shash *rx_tfm_michael;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct crypto_shash *tx_tfm_michael;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* scratch buffers for virt_to_page() (crypto API) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) u8 rx_hdr[16], tx_hdr[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static unsigned long lib80211_tkip_set_flags(unsigned long flags, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct lib80211_tkip_data *_priv = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsigned long old_flags = _priv->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) _priv->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return old_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static unsigned long lib80211_tkip_get_flags(void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct lib80211_tkip_data *_priv = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return _priv->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static void *lib80211_tkip_init(int key_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct lib80211_tkip_data *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (fips_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (priv == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) priv->key_idx = key_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) priv->tx_tfm_michael = crypto_alloc_shash("michael_mic", 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (IS_ERR(priv->tx_tfm_michael)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) priv->tx_tfm_michael = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) priv->rx_tfm_michael = crypto_alloc_shash("michael_mic", 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (IS_ERR(priv->rx_tfm_michael)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) priv->rx_tfm_michael = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (priv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) crypto_free_shash(priv->tx_tfm_michael);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) crypto_free_shash(priv->rx_tfm_michael);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static void lib80211_tkip_deinit(void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct lib80211_tkip_data *_priv = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (_priv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) crypto_free_shash(_priv->tx_tfm_michael);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) crypto_free_shash(_priv->rx_tfm_michael);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) kfree_sensitive(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static inline u16 RotR1(u16 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return (val >> 1) | (val << 15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static inline u8 Lo8(u16 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return val & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static inline u8 Hi8(u16 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return val >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static inline u16 Lo16(u32 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return val & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static inline u16 Hi16(u32 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return val >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static inline u16 Mk16(u8 hi, u8 lo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return lo | (((u16) hi) << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static inline u16 Mk16_le(__le16 * v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return le16_to_cpu(*v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static const u16 Sbox[256] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 0xC6A5, 0xF884, 0xEE99, 0xF68D, 0xFF0D, 0xD6BD, 0xDEB1, 0x9154,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 0x6050, 0x0203, 0xCEA9, 0x567D, 0xE719, 0xB562, 0x4DE6, 0xEC9A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 0x8F45, 0x1F9D, 0x8940, 0xFA87, 0xEF15, 0xB2EB, 0x8EC9, 0xFB0B,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 0x41EC, 0xB367, 0x5FFD, 0x45EA, 0x23BF, 0x53F7, 0xE496, 0x9B5B,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 0x75C2, 0xE11C, 0x3DAE, 0x4C6A, 0x6C5A, 0x7E41, 0xF502, 0x834F,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 0x685C, 0x51F4, 0xD134, 0xF908, 0xE293, 0xAB73, 0x6253, 0x2A3F,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 0x080C, 0x9552, 0x4665, 0x9D5E, 0x3028, 0x37A1, 0x0A0F, 0x2FB5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 0x0E09, 0x2436, 0x1B9B, 0xDF3D, 0xCD26, 0x4E69, 0x7FCD, 0xEA9F,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 0x121B, 0x1D9E, 0x5874, 0x342E, 0x362D, 0xDCB2, 0xB4EE, 0x5BFB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 0xA4F6, 0x764D, 0xB761, 0x7DCE, 0x527B, 0xDD3E, 0x5E71, 0x1397,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 0xA6F5, 0xB968, 0x0000, 0xC12C, 0x4060, 0xE31F, 0x79C8, 0xB6ED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 0xD4BE, 0x8D46, 0x67D9, 0x724B, 0x94DE, 0x98D4, 0xB0E8, 0x854A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 0xBB6B, 0xC52A, 0x4FE5, 0xED16, 0x86C5, 0x9AD7, 0x6655, 0x1194,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 0x8ACF, 0xE910, 0x0406, 0xFE81, 0xA0F0, 0x7844, 0x25BA, 0x4BE3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 0xA2F3, 0x5DFE, 0x80C0, 0x058A, 0x3FAD, 0x21BC, 0x7048, 0xF104,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 0x63DF, 0x77C1, 0xAF75, 0x4263, 0x2030, 0xE51A, 0xFD0E, 0xBF6D,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 0x814C, 0x1814, 0x2635, 0xC32F, 0xBEE1, 0x35A2, 0x88CC, 0x2E39,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 0x9357, 0x55F2, 0xFC82, 0x7A47, 0xC8AC, 0xBAE7, 0x322B, 0xE695,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 0xC0A0, 0x1998, 0x9ED1, 0xA37F, 0x4466, 0x547E, 0x3BAB, 0x0B83,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 0x8CCA, 0xC729, 0x6BD3, 0x283C, 0xA779, 0xBCE2, 0x161D, 0xAD76,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 0xDB3B, 0x6456, 0x744E, 0x141E, 0x92DB, 0x0C0A, 0x486C, 0xB8E4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 0x9F5D, 0xBD6E, 0x43EF, 0xC4A6, 0x39A8, 0x31A4, 0xD337, 0xF28B,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 0xD532, 0x8B43, 0x6E59, 0xDAB7, 0x018C, 0xB164, 0x9CD2, 0x49E0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 0xD8B4, 0xACFA, 0xF307, 0xCF25, 0xCAAF, 0xF48E, 0x47E9, 0x1018,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 0x6FD5, 0xF088, 0x4A6F, 0x5C72, 0x3824, 0x57F1, 0x73C7, 0x9751,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 0xCB23, 0xA17C, 0xE89C, 0x3E21, 0x96DD, 0x61DC, 0x0D86, 0x0F85,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 0xE090, 0x7C42, 0x71C4, 0xCCAA, 0x90D8, 0x0605, 0xF701, 0x1C12,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 0xC2A3, 0x6A5F, 0xAEF9, 0x69D0, 0x1791, 0x9958, 0x3A27, 0x27B9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 0xD938, 0xEB13, 0x2BB3, 0x2233, 0xD2BB, 0xA970, 0x0789, 0x33A7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 0x2DB6, 0x3C22, 0x1592, 0xC920, 0x8749, 0xAAFF, 0x5078, 0xA57A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 0x038F, 0x59F8, 0x0980, 0x1A17, 0x65DA, 0xD731, 0x84C6, 0xD0B8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 0x82C3, 0x29B0, 0x5A77, 0x1E11, 0x7BCB, 0xA8FC, 0x6DD6, 0x2C3A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static inline u16 _S_(u16 v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) u16 t = Sbox[Hi8(v)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return Sbox[Lo8(v)] ^ ((t << 8) | (t >> 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) #define PHASE1_LOOP_COUNT 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static void tkip_mixing_phase1(u16 * TTAK, const u8 * TK, const u8 * TA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) u32 IV32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /* Initialize the 80-bit TTAK from TSC (IV32) and TA[0..5] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) TTAK[0] = Lo16(IV32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) TTAK[1] = Hi16(IV32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) TTAK[2] = Mk16(TA[1], TA[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) TTAK[3] = Mk16(TA[3], TA[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) TTAK[4] = Mk16(TA[5], TA[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) for (i = 0; i < PHASE1_LOOP_COUNT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) j = 2 * (i & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) TTAK[0] += _S_(TTAK[4] ^ Mk16(TK[1 + j], TK[0 + j]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) TTAK[1] += _S_(TTAK[0] ^ Mk16(TK[5 + j], TK[4 + j]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) TTAK[2] += _S_(TTAK[1] ^ Mk16(TK[9 + j], TK[8 + j]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) TTAK[3] += _S_(TTAK[2] ^ Mk16(TK[13 + j], TK[12 + j]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) TTAK[4] += _S_(TTAK[3] ^ Mk16(TK[1 + j], TK[0 + j])) + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static void tkip_mixing_phase2(u8 * WEPSeed, const u8 * TK, const u16 * TTAK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) u16 IV16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /* Make temporary area overlap WEP seed so that the final copy can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * avoided on little endian hosts. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) u16 *PPK = (u16 *) & WEPSeed[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /* Step 1 - make copy of TTAK and bring in TSC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) PPK[0] = TTAK[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) PPK[1] = TTAK[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) PPK[2] = TTAK[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) PPK[3] = TTAK[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) PPK[4] = TTAK[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) PPK[5] = TTAK[4] + IV16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /* Step 2 - 96-bit bijective mixing using S-box */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) PPK[0] += _S_(PPK[5] ^ Mk16_le((__le16 *) & TK[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) PPK[1] += _S_(PPK[0] ^ Mk16_le((__le16 *) & TK[2]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) PPK[2] += _S_(PPK[1] ^ Mk16_le((__le16 *) & TK[4]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) PPK[3] += _S_(PPK[2] ^ Mk16_le((__le16 *) & TK[6]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) PPK[4] += _S_(PPK[3] ^ Mk16_le((__le16 *) & TK[8]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) PPK[5] += _S_(PPK[4] ^ Mk16_le((__le16 *) & TK[10]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) PPK[0] += RotR1(PPK[5] ^ Mk16_le((__le16 *) & TK[12]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) PPK[1] += RotR1(PPK[0] ^ Mk16_le((__le16 *) & TK[14]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) PPK[2] += RotR1(PPK[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) PPK[3] += RotR1(PPK[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) PPK[4] += RotR1(PPK[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) PPK[5] += RotR1(PPK[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* Step 3 - bring in last of TK bits, assign 24-bit WEP IV value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * WEPSeed[0..2] is transmitted as WEP IV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) WEPSeed[0] = Hi8(IV16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) WEPSeed[1] = (Hi8(IV16) | 0x20) & 0x7F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) WEPSeed[2] = Lo8(IV16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) WEPSeed[3] = Lo8((PPK[5] ^ Mk16_le((__le16 *) & TK[0])) >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) #ifdef __BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) for (i = 0; i < 6; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) PPK[i] = (PPK[i] << 8) | (PPK[i] >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static int lib80211_tkip_hdr(struct sk_buff *skb, int hdr_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) u8 * rc4key, int keylen, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct lib80211_tkip_data *tkey = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) u8 *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct ieee80211_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) hdr = (struct ieee80211_hdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (skb_headroom(skb) < TKIP_HDR_LEN || skb->len < hdr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (rc4key == NULL || keylen < 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (!tkey->tx_phase1_done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) tkip_mixing_phase1(tkey->tx_ttak, tkey->key, hdr->addr2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) tkey->tx_iv32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) tkey->tx_phase1_done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) tkip_mixing_phase2(rc4key, tkey->key, tkey->tx_ttak, tkey->tx_iv16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) pos = skb_push(skb, TKIP_HDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) memmove(pos, pos + TKIP_HDR_LEN, hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) pos += hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) *pos++ = *rc4key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) *pos++ = *(rc4key + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) *pos++ = *(rc4key + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) *pos++ = (tkey->key_idx << 6) | (1 << 5) /* Ext IV included */ ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) *pos++ = tkey->tx_iv32 & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) *pos++ = (tkey->tx_iv32 >> 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) *pos++ = (tkey->tx_iv32 >> 16) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) *pos++ = (tkey->tx_iv32 >> 24) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) tkey->tx_iv16++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (tkey->tx_iv16 == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) tkey->tx_phase1_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) tkey->tx_iv32++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return TKIP_HDR_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int lib80211_tkip_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct lib80211_tkip_data *tkey = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) u8 rc4key[16], *pos, *icv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) u32 crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (tkey->flags & IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) net_dbg_ratelimited("TKIP countermeasures: dropped TX packet to %pM\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) hdr->addr1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (skb_tailroom(skb) < 4 || skb->len < hdr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) len = skb->len - hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) pos = skb->data + hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if ((lib80211_tkip_hdr(skb, hdr_len, rc4key, 16, priv)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) crc = ~crc32_le(~0, pos, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) icv = skb_put(skb, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) icv[0] = crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) icv[1] = crc >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) icv[2] = crc >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) icv[3] = crc >> 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) arc4_setkey(&tkey->tx_ctx_arc4, rc4key, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) arc4_crypt(&tkey->tx_ctx_arc4, pos, pos, len + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * deal with seq counter wrapping correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * refer to timer_after() for jiffies wrapping handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static inline int tkip_replay_check(u32 iv32_n, u16 iv16_n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) u32 iv32_o, u16 iv16_o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if ((s32)iv32_n - (s32)iv32_o < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) (iv32_n == iv32_o && iv16_n <= iv16_o))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static int lib80211_tkip_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct lib80211_tkip_data *tkey = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) u8 rc4key[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) u8 keyidx, *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) u32 iv32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) u16 iv16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct ieee80211_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) u8 icv[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) u32 crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) int plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) hdr = (struct ieee80211_hdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (tkey->flags & IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) net_dbg_ratelimited("TKIP countermeasures: dropped received packet from %pM\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) hdr->addr2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (skb->len < hdr_len + TKIP_HDR_LEN + 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) pos = skb->data + hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) keyidx = pos[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (!(keyidx & (1 << 5))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) net_dbg_ratelimited("TKIP: received packet without ExtIV flag from %pM\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) hdr->addr2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return -2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) keyidx >>= 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (tkey->key_idx != keyidx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) net_dbg_ratelimited("TKIP: RX tkey->key_idx=%d frame keyidx=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) tkey->key_idx, keyidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) return -6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (!tkey->key_set) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) net_dbg_ratelimited("TKIP: received packet from %pM with keyid=%d that does not have a configured key\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) hdr->addr2, keyidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return -3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) iv16 = (pos[0] << 8) | pos[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) iv32 = pos[4] | (pos[5] << 8) | (pos[6] << 16) | (pos[7] << 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) pos += TKIP_HDR_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (tkip_replay_check(iv32, iv16, tkey->rx_iv32, tkey->rx_iv16)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) #ifdef CONFIG_LIB80211_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) net_dbg_ratelimited("TKIP: replay detected: STA=%pM previous TSC %08x%04x received TSC %08x%04x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) hdr->addr2, tkey->rx_iv32, tkey->rx_iv16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) iv32, iv16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) tkey->dot11RSNAStatsTKIPReplays++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return -4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (iv32 != tkey->rx_iv32 || !tkey->rx_phase1_done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) tkip_mixing_phase1(tkey->rx_ttak, tkey->key, hdr->addr2, iv32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) tkey->rx_phase1_done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) tkip_mixing_phase2(rc4key, tkey->key, tkey->rx_ttak, iv16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) plen = skb->len - hdr_len - 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) arc4_setkey(&tkey->rx_ctx_arc4, rc4key, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) arc4_crypt(&tkey->rx_ctx_arc4, pos, pos, plen + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) crc = ~crc32_le(~0, pos, plen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) icv[0] = crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) icv[1] = crc >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) icv[2] = crc >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) icv[3] = crc >> 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (memcmp(icv, pos + plen, 4) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (iv32 != tkey->rx_iv32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /* Previously cached Phase1 result was already lost, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * it needs to be recalculated for the next packet. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) tkey->rx_phase1_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) #ifdef CONFIG_LIB80211_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) net_dbg_ratelimited("TKIP: ICV error detected: STA=%pM\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) hdr->addr2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) tkey->dot11RSNAStatsTKIPICVErrors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return -5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) /* Update real counters only after Michael MIC verification has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * completed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) tkey->rx_iv32_new = iv32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) tkey->rx_iv16_new = iv16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /* Remove IV and ICV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) memmove(skb->data + TKIP_HDR_LEN, skb->data, hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) skb_pull(skb, TKIP_HDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) skb_trim(skb, skb->len - 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return keyidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static int michael_mic(struct crypto_shash *tfm_michael, u8 *key, u8 *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) u8 *data, size_t data_len, u8 *mic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) SHASH_DESC_ON_STACK(desc, tfm_michael);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (tfm_michael == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) pr_warn("%s(): tfm_michael == NULL\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) desc->tfm = tfm_michael;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (crypto_shash_setkey(tfm_michael, key, 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) err = crypto_shash_init(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) err = crypto_shash_update(desc, hdr, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) err = crypto_shash_update(desc, data, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) err = crypto_shash_final(desc, mic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) shash_desc_zero(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) static void michael_mic_hdr(struct sk_buff *skb, u8 * hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) struct ieee80211_hdr *hdr11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) hdr11 = (struct ieee80211_hdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) switch (le16_to_cpu(hdr11->frame_control) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) case IEEE80211_FCTL_TODS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) memcpy(hdr, hdr11->addr3, ETH_ALEN); /* DA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) memcpy(hdr + ETH_ALEN, hdr11->addr2, ETH_ALEN); /* SA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) case IEEE80211_FCTL_FROMDS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) memcpy(hdr, hdr11->addr1, ETH_ALEN); /* DA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) memcpy(hdr + ETH_ALEN, hdr11->addr3, ETH_ALEN); /* SA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) memcpy(hdr, hdr11->addr3, ETH_ALEN); /* DA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) memcpy(hdr + ETH_ALEN, hdr11->addr4, ETH_ALEN); /* SA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) memcpy(hdr, hdr11->addr1, ETH_ALEN); /* DA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) memcpy(hdr + ETH_ALEN, hdr11->addr2, ETH_ALEN); /* SA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (ieee80211_is_data_qos(hdr11->frame_control)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) hdr[12] = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(hdr11)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) & IEEE80211_QOS_CTL_TID_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) hdr[12] = 0; /* priority */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) hdr[13] = hdr[14] = hdr[15] = 0; /* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) static int lib80211_michael_mic_add(struct sk_buff *skb, int hdr_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) struct lib80211_tkip_data *tkey = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) u8 *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (skb_tailroom(skb) < 8 || skb->len < hdr_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) printk(KERN_DEBUG "Invalid packet for Michael MIC add "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) "(tailroom=%d hdr_len=%d skb->len=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) skb_tailroom(skb), hdr_len, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) michael_mic_hdr(skb, tkey->tx_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) pos = skb_put(skb, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (michael_mic(tkey->tx_tfm_michael, &tkey->key[16], tkey->tx_hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) skb->data + hdr_len, skb->len - 8 - hdr_len, pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) static void lib80211_michael_mic_failure(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) struct ieee80211_hdr *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) int keyidx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) union iwreq_data wrqu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct iw_michaelmicfailure ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) /* TODO: needed parameters: count, keyid, key type, TSC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) memset(&ev, 0, sizeof(ev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) ev.flags = keyidx & IW_MICFAILURE_KEY_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (hdr->addr1[0] & 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) ev.flags |= IW_MICFAILURE_GROUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) ev.flags |= IW_MICFAILURE_PAIRWISE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) ev.src_addr.sa_family = ARPHRD_ETHER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) memcpy(ev.src_addr.sa_data, hdr->addr2, ETH_ALEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) memset(&wrqu, 0, sizeof(wrqu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) wrqu.data.length = sizeof(ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) wireless_send_event(dev, IWEVMICHAELMICFAILURE, &wrqu, (char *)&ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) static int lib80211_michael_mic_verify(struct sk_buff *skb, int keyidx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) int hdr_len, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) struct lib80211_tkip_data *tkey = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) u8 mic[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (!tkey->key_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) michael_mic_hdr(skb, tkey->rx_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) if (michael_mic(tkey->rx_tfm_michael, &tkey->key[24], tkey->rx_hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) skb->data + hdr_len, skb->len - 8 - hdr_len, mic))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (memcmp(mic, skb->data + skb->len - 8, 8) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) struct ieee80211_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) hdr = (struct ieee80211_hdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) printk(KERN_DEBUG "%s: Michael MIC verification failed for "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) "MSDU from %pM keyidx=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) skb->dev ? skb->dev->name : "N/A", hdr->addr2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) keyidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (skb->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) lib80211_michael_mic_failure(skb->dev, hdr, keyidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) tkey->dot11RSNAStatsTKIPLocalMICFailures++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) /* Update TSC counters for RX now that the packet verification has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * completed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) tkey->rx_iv32 = tkey->rx_iv32_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) tkey->rx_iv16 = tkey->rx_iv16_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) skb_trim(skb, skb->len - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) static int lib80211_tkip_set_key(void *key, int len, u8 * seq, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) struct lib80211_tkip_data *tkey = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) int keyidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) struct crypto_shash *tfm = tkey->tx_tfm_michael;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) struct arc4_ctx *tfm2 = &tkey->tx_ctx_arc4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) struct crypto_shash *tfm3 = tkey->rx_tfm_michael;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) struct arc4_ctx *tfm4 = &tkey->rx_ctx_arc4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) keyidx = tkey->key_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) memset(tkey, 0, sizeof(*tkey));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) tkey->key_idx = keyidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) tkey->tx_tfm_michael = tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) tkey->tx_ctx_arc4 = *tfm2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) tkey->rx_tfm_michael = tfm3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) tkey->rx_ctx_arc4 = *tfm4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (len == TKIP_KEY_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) memcpy(tkey->key, key, TKIP_KEY_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) tkey->key_set = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) tkey->tx_iv16 = 1; /* TSC is initialized to 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) tkey->rx_iv32 = (seq[5] << 24) | (seq[4] << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) (seq[3] << 8) | seq[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) tkey->rx_iv16 = (seq[1] << 8) | seq[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) } else if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) tkey->key_set = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) static int lib80211_tkip_get_key(void *key, int len, u8 * seq, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) struct lib80211_tkip_data *tkey = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if (len < TKIP_KEY_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (!tkey->key_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) memcpy(key, tkey->key, TKIP_KEY_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) /* Return the sequence number of the last transmitted frame. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) u16 iv16 = tkey->tx_iv16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) u32 iv32 = tkey->tx_iv32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (iv16 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) iv32--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) iv16--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) seq[0] = tkey->tx_iv16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) seq[1] = tkey->tx_iv16 >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) seq[2] = tkey->tx_iv32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) seq[3] = tkey->tx_iv32 >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) seq[4] = tkey->tx_iv32 >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) seq[5] = tkey->tx_iv32 >> 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) return TKIP_KEY_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) static void lib80211_tkip_print_stats(struct seq_file *m, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) struct lib80211_tkip_data *tkip = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) seq_printf(m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) "key[%d] alg=TKIP key_set=%d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) "tx_pn=%02x%02x%02x%02x%02x%02x "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) "rx_pn=%02x%02x%02x%02x%02x%02x "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) "replays=%d icv_errors=%d local_mic_failures=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) tkip->key_idx, tkip->key_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) (tkip->tx_iv32 >> 24) & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) (tkip->tx_iv32 >> 16) & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) (tkip->tx_iv32 >> 8) & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) tkip->tx_iv32 & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) (tkip->tx_iv16 >> 8) & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) tkip->tx_iv16 & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) (tkip->rx_iv32 >> 24) & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) (tkip->rx_iv32 >> 16) & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) (tkip->rx_iv32 >> 8) & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) tkip->rx_iv32 & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) (tkip->rx_iv16 >> 8) & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) tkip->rx_iv16 & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) tkip->dot11RSNAStatsTKIPReplays,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) tkip->dot11RSNAStatsTKIPICVErrors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) tkip->dot11RSNAStatsTKIPLocalMICFailures);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) static struct lib80211_crypto_ops lib80211_crypt_tkip = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) .name = "TKIP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) .init = lib80211_tkip_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) .deinit = lib80211_tkip_deinit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) .encrypt_mpdu = lib80211_tkip_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) .decrypt_mpdu = lib80211_tkip_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) .encrypt_msdu = lib80211_michael_mic_add,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) .decrypt_msdu = lib80211_michael_mic_verify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) .set_key = lib80211_tkip_set_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) .get_key = lib80211_tkip_get_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) .print_stats = lib80211_tkip_print_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) .extra_mpdu_prefix_len = 4 + 4, /* IV + ExtIV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) .extra_mpdu_postfix_len = 4, /* ICV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) .extra_msdu_postfix_len = 8, /* MIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) .get_flags = lib80211_tkip_get_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) .set_flags = lib80211_tkip_set_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) static int __init lib80211_crypto_tkip_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) return lib80211_register_crypto_ops(&lib80211_crypt_tkip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) static void __exit lib80211_crypto_tkip_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) lib80211_unregister_crypto_ops(&lib80211_crypt_tkip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) module_init(lib80211_crypto_tkip_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) module_exit(lib80211_crypto_tkip_exit);