Path: blob/main/sys/net80211/ieee80211_crypto_tkip.c
39475 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2002-2008 Sam Leffler, Errno Consulting4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR16* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES17* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.18* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT20* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,21* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY22* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF24* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*/2627#include <sys/cdefs.h>28/*29* IEEE 802.11i TKIP crypto support.30*31* Part of this module is derived from similar code in the Host32* AP driver. The code is used with the consent of the author and33* it's license is included below.34*/35#include "opt_wlan.h"3637#include <sys/param.h>38#include <sys/systm.h>39#include <sys/mbuf.h>40#include <sys/malloc.h>41#include <sys/kernel.h>42#include <sys/module.h>43#include <sys/endian.h>4445#include <sys/socket.h>4647#include <net/if.h>48#include <net/if_media.h>49#include <net/ethernet.h>5051#include <net80211/ieee80211_var.h>5253static void *tkip_attach(struct ieee80211vap *, struct ieee80211_key *);54static void tkip_detach(struct ieee80211_key *);55static int tkip_setkey(struct ieee80211_key *);56static void tkip_setiv(struct ieee80211_key *, uint8_t *);57static int tkip_encap(struct ieee80211_key *, struct mbuf *);58static int tkip_enmic(struct ieee80211_key *, struct mbuf *, int);59static int tkip_decap(struct ieee80211_key *, struct mbuf *, int);60static int tkip_demic(struct ieee80211_key *, struct mbuf *, int);6162static const struct ieee80211_cipher tkip = {63.ic_name = "TKIP",64.ic_cipher = IEEE80211_CIPHER_TKIP,65.ic_header = IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +66IEEE80211_WEP_EXTIVLEN,67.ic_trailer = IEEE80211_WEP_CRCLEN,68.ic_miclen = IEEE80211_WEP_MICLEN,69.ic_attach = tkip_attach,70.ic_detach = tkip_detach,71.ic_setkey = tkip_setkey,72.ic_setiv = tkip_setiv,73.ic_encap = tkip_encap,74.ic_decap = tkip_decap,75.ic_enmic = tkip_enmic,76.ic_demic = tkip_demic,77};7879typedef uint8_t u8;80typedef uint16_t u16;81typedef uint32_t __u32;82typedef uint32_t u32;8384struct tkip_ctx {85struct ieee80211vap *tc_vap; /* for diagnostics+statistics */8687u16 tx_ttak[5];88u8 tx_rc4key[16]; /* XXX for test module; make locals? */8990u16 rx_ttak[5];91int rx_phase1_done;92u8 rx_rc4key[16]; /* XXX for test module; make locals? */93uint64_t rx_rsc; /* held until MIC verified */94};9596static void michael_mic(struct tkip_ctx *, const u8 *key,97struct mbuf *m, u_int off, size_t data_len,98u8 mic[IEEE80211_WEP_MICLEN]);99static int tkip_encrypt(struct tkip_ctx *, struct ieee80211_key *,100struct mbuf *, int hdr_len);101static int tkip_decrypt(struct tkip_ctx *, struct ieee80211_key *,102struct mbuf *, int hdr_len);103104/* number of references from net80211 layer */105static int nrefs = 0;106107static void *108tkip_attach(struct ieee80211vap *vap, struct ieee80211_key *k)109{110struct tkip_ctx *ctx;111112ctx = (struct tkip_ctx *) IEEE80211_MALLOC(sizeof(struct tkip_ctx),113M_80211_CRYPTO, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);114if (ctx == NULL) {115vap->iv_stats.is_crypto_nomem++;116return NULL;117}118119ctx->tc_vap = vap;120nrefs++; /* NB: we assume caller locking */121return ctx;122}123124static void125tkip_detach(struct ieee80211_key *k)126{127struct tkip_ctx *ctx = k->wk_private;128129IEEE80211_FREE(ctx, M_80211_CRYPTO);130KASSERT(nrefs > 0, ("imbalanced attach/detach"));131nrefs--; /* NB: we assume caller locking */132}133134static int135tkip_setkey(struct ieee80211_key *k)136{137struct tkip_ctx *ctx = k->wk_private;138139if (k->wk_keylen != (128/NBBY)) {140(void) ctx; /* XXX */141IEEE80211_DPRINTF(ctx->tc_vap, IEEE80211_MSG_CRYPTO,142"%s: Invalid key length %u, expecting %u\n",143__func__, k->wk_keylen, 128/NBBY);144return 0;145}146ctx->rx_phase1_done = 0;147return 1;148}149150static void151tkip_setiv(struct ieee80211_key *k, uint8_t *ivp)152{153struct tkip_ctx *ctx = k->wk_private;154struct ieee80211vap *vap = ctx->tc_vap;155uint8_t keyid;156157keyid = ieee80211_crypto_get_keyid(vap, k) << 6;158159k->wk_keytsc++;160ivp[0] = k->wk_keytsc >> 8; /* TSC1 */161ivp[1] = (ivp[0] | 0x20) & 0x7f; /* WEP seed */162ivp[2] = k->wk_keytsc >> 0; /* TSC0 */163ivp[3] = keyid | IEEE80211_WEP_EXTIV; /* KeyID | ExtID */164ivp[4] = k->wk_keytsc >> 16; /* TSC2 */165ivp[5] = k->wk_keytsc >> 24; /* TSC3 */166ivp[6] = k->wk_keytsc >> 32; /* TSC4 */167ivp[7] = k->wk_keytsc >> 40; /* TSC5 */168}169170/*171* Add privacy headers and do any s/w encryption required.172*/173static int174tkip_encap(struct ieee80211_key *k, struct mbuf *m)175{176struct tkip_ctx *ctx = k->wk_private;177struct ieee80211vap *vap = ctx->tc_vap;178struct ieee80211com *ic = vap->iv_ic;179struct ieee80211_frame *wh;180uint8_t *ivp;181int hdrlen;182int is_mgmt;183184wh = mtod(m, struct ieee80211_frame *);185is_mgmt = IEEE80211_IS_MGMT(wh);186187/*188* Handle TKIP counter measures requirement.189*/190if (vap->iv_flags & IEEE80211_F_COUNTERM) {191#ifdef IEEE80211_DEBUG192struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);193#endif194195IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,196"discard frame due to countermeasures (%s)", __func__);197vap->iv_stats.is_crypto_tkipcm++;198return 0;199}200201/*202* Check to see whether IV needs to be included.203*/204if (is_mgmt && (k->wk_flags & IEEE80211_KEY_NOIVMGT))205return 1;206if ((! is_mgmt) && (k->wk_flags & IEEE80211_KEY_NOIV))207return 1;208209hdrlen = ieee80211_hdrspace(ic, mtod(m, void *));210211/*212* Copy down 802.11 header and add the IV, KeyID, and ExtIV.213*/214M_PREPEND(m, tkip.ic_header, IEEE80211_M_NOWAIT);215if (m == NULL)216return 0;217ivp = mtod(m, uint8_t *);218memmove(ivp, ivp + tkip.ic_header, hdrlen);219ivp += hdrlen;220221tkip_setiv(k, ivp);222223/*224* Finally, do software encrypt if needed.225*/226if ((k->wk_flags & IEEE80211_KEY_SWENCRYPT) &&227!tkip_encrypt(ctx, k, m, hdrlen))228return 0;229230return 1;231}232233/*234* Add MIC to the frame as needed.235*/236static int237tkip_enmic(struct ieee80211_key *k, struct mbuf *m, int force)238{239struct tkip_ctx *ctx = k->wk_private;240struct ieee80211_frame *wh;241int is_mgmt;242243wh = mtod(m, struct ieee80211_frame *);244is_mgmt = IEEE80211_IS_MGMT(wh);245246/*247* Check to see whether MIC needs to be included.248*/249if (is_mgmt && (k->wk_flags & IEEE80211_KEY_NOMICMGT))250return 1;251if ((! is_mgmt) && (k->wk_flags & IEEE80211_KEY_NOMIC))252return 1;253254if (force || (k->wk_flags & IEEE80211_KEY_SWENMIC)) {255struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);256struct ieee80211vap *vap = ctx->tc_vap;257struct ieee80211com *ic = vap->iv_ic;258int hdrlen;259uint8_t mic[IEEE80211_WEP_MICLEN];260261vap->iv_stats.is_crypto_tkipenmic++;262263hdrlen = ieee80211_hdrspace(ic, wh);264265michael_mic(ctx, k->wk_txmic,266m, hdrlen, m->m_pkthdr.len - hdrlen, mic);267return m_append(m, tkip.ic_miclen, mic);268}269return 1;270}271272static __inline uint64_t273READ_6(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5)274{275uint32_t iv32 = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);276uint16_t iv16 = (b4 << 0) | (b5 << 8);277return (((uint64_t)iv16) << 32) | iv32;278}279280/*281* Validate and strip privacy headers (and trailer) for a282* received frame. If necessary, decrypt the frame using283* the specified key.284*/285static int286tkip_decap(struct ieee80211_key *k, struct mbuf *m, int hdrlen)287{288const struct ieee80211_rx_stats *rxs;289struct tkip_ctx *ctx = k->wk_private;290struct ieee80211vap *vap = ctx->tc_vap;291struct ieee80211_frame *wh;292uint8_t *ivp, tid;293294rxs = ieee80211_get_rx_params_ptr(m);295296/*297* If IV has been stripped, we skip most of the below.298*/299if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP))300goto finish;301302/*303* Header should have extended IV and sequence number;304* verify the former and validate the latter.305*/306wh = mtod(m, struct ieee80211_frame *);307ivp = mtod(m, uint8_t *) + hdrlen;308if ((ivp[IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV) == 0) {309/*310* No extended IV; discard frame.311*/312IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,313"%s", "missing ExtIV for TKIP cipher");314vap->iv_stats.is_rx_tkipformat++;315return 0;316}317/*318* Handle TKIP counter measures requirement.319*/320if (vap->iv_flags & IEEE80211_F_COUNTERM) {321IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,322"discard frame due to countermeasures (%s)", __func__);323vap->iv_stats.is_crypto_tkipcm++;324return 0;325}326327tid = ieee80211_gettid(wh);328ctx->rx_rsc = READ_6(ivp[2], ivp[0], ivp[4], ivp[5], ivp[6], ivp[7]);329if (ctx->rx_rsc <= k->wk_keyrsc[tid] &&330(k->wk_flags & IEEE80211_KEY_NOREPLAY) == 0) {331/*332* Replay violation; notify upper layer.333*/334ieee80211_notify_replay_failure(vap, wh, k, ctx->rx_rsc, tid);335vap->iv_stats.is_rx_tkipreplay++;336return 0;337}338/*339* NB: We can't update the rsc in the key until MIC is verified.340*341* We assume we are not preempted between doing the check above342* and updating wk_keyrsc when stripping the MIC in tkip_demic.343* Otherwise we might process another packet and discard it as344* a replay.345*/346347/*348* Check if the device handled the decrypt in hardware.349* If so we just strip the header; otherwise we need to350* handle the decrypt in software.351*/352if ((k->wk_flags & IEEE80211_KEY_SWDECRYPT) &&353!tkip_decrypt(ctx, k, m, hdrlen))354return 0;355356finish:357358/*359* Copy up 802.11 header and strip crypto bits - but only if we360* are required to.361*/362if (! ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP))) {363/* XXX this assumes the header + IV are contiguous in an mbuf. */364memmove(mtod(m, uint8_t *) + tkip.ic_header, mtod(m, void *),365hdrlen);366m_adj(m, tkip.ic_header);367}368369/*370* Strip the ICV if hardware has not done so already.371*/372if ((rxs == NULL) || (rxs->c_pktflags & IEEE80211_RX_F_ICV_STRIP) == 0)373m_adj(m, -tkip.ic_trailer);374375return 1;376}377378/*379* Verify and strip MIC from the frame.380*/381static int382tkip_demic(struct ieee80211_key *k, struct mbuf *m, int force)383{384const struct ieee80211_rx_stats *rxs;385struct tkip_ctx *ctx = k->wk_private;386struct ieee80211_frame *wh;387uint8_t tid;388389wh = mtod(m, struct ieee80211_frame *);390rxs = ieee80211_get_rx_params_ptr(m);391392/*393* If we are told about a MIC failure from the driver,394* directly notify as a michael failure to the upper395* layers.396*/397if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_FAIL_MMIC)) {398struct ieee80211vap *vap = ctx->tc_vap;399ieee80211_notify_michael_failure(vap, wh,400k->wk_rxkeyix != IEEE80211_KEYIX_NONE ?401k->wk_rxkeyix : k->wk_keyix);402return 0;403}404405/*406* If MMIC has been stripped, we skip most of the below.407*/408if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_MMIC_STRIP))409goto finish;410411if ((k->wk_flags & IEEE80211_KEY_SWDEMIC) || force) {412struct ieee80211vap *vap = ctx->tc_vap;413int hdrlen = ieee80211_hdrspace(vap->iv_ic, wh);414u8 mic[IEEE80211_WEP_MICLEN];415u8 mic0[IEEE80211_WEP_MICLEN];416417vap->iv_stats.is_crypto_tkipdemic++;418419michael_mic(ctx, k->wk_rxmic,420m, hdrlen, m->m_pkthdr.len - (hdrlen + tkip.ic_miclen),421mic);422m_copydata(m, m->m_pkthdr.len - tkip.ic_miclen,423tkip.ic_miclen, mic0);424if (memcmp(mic, mic0, tkip.ic_miclen)) {425/* NB: 802.11 layer handles statistic and debug msg */426ieee80211_notify_michael_failure(vap, wh,427k->wk_rxkeyix != IEEE80211_KEYIX_NONE ?428k->wk_rxkeyix : k->wk_keyix);429return 0;430}431}432/*433* Strip MIC from the tail.434*/435m_adj(m, -tkip.ic_miclen);436437/*438* Ok to update rsc now that MIC has been verified.439*/440tid = ieee80211_gettid(wh);441k->wk_keyrsc[tid] = ctx->rx_rsc;442443finish:444return 1;445}446447/*448* Host AP crypt: host-based TKIP encryption implementation for Host AP driver449*450* Copyright (c) 2003-2004, Jouni Malinen <[email protected]>451*452* This program is free software; you can redistribute it and/or modify453* it under the terms of the GNU General Public License version 2 as454* published by the Free Software Foundation. See README and COPYING for455* more details.456*457* Alternatively, this software may be distributed under the terms of BSD458* license.459*/460461static const __u32 crc32_table[256] = {4620x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,4630x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,4640xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,4650x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,4660x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,4670x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,4680xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,4690xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,4700x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,4710x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,4720xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,4730xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,4740x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,4750x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,4760x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,4770xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,4780x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,4790x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,4800x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,4810xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,4820x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,4830x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,4840xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,4850xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,4860x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,4870x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,4880x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,4890x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,4900xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,4910x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,4920x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,4930x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,4940xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,4950xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,4960x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,4970x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,4980xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,4990xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,5000x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,5010x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,5020x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,5030xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,5040x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,5050x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,5060x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,5070xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,5080x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,5090x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,5100xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,5110xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,5120x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,5130x2d02ef8dL514};515516static __inline u16 RotR1(u16 val)517{518return (val >> 1) | (val << 15);519}520521static __inline u8 Lo8(u16 val)522{523return val & 0xff;524}525526static __inline u8 Hi8(u16 val)527{528return val >> 8;529}530531static __inline u16 Lo16(u32 val)532{533return val & 0xffff;534}535536static __inline u16 Hi16(u32 val)537{538return val >> 16;539}540541static __inline u16 Mk16(u8 hi, u8 lo)542{543return lo | (((u16) hi) << 8);544}545546static __inline u16 Mk16_le(const u16 *v)547{548return le16toh(*v);549}550551static const u16 Sbox[256] = {5520xC6A5, 0xF884, 0xEE99, 0xF68D, 0xFF0D, 0xD6BD, 0xDEB1, 0x9154,5530x6050, 0x0203, 0xCEA9, 0x567D, 0xE719, 0xB562, 0x4DE6, 0xEC9A,5540x8F45, 0x1F9D, 0x8940, 0xFA87, 0xEF15, 0xB2EB, 0x8EC9, 0xFB0B,5550x41EC, 0xB367, 0x5FFD, 0x45EA, 0x23BF, 0x53F7, 0xE496, 0x9B5B,5560x75C2, 0xE11C, 0x3DAE, 0x4C6A, 0x6C5A, 0x7E41, 0xF502, 0x834F,5570x685C, 0x51F4, 0xD134, 0xF908, 0xE293, 0xAB73, 0x6253, 0x2A3F,5580x080C, 0x9552, 0x4665, 0x9D5E, 0x3028, 0x37A1, 0x0A0F, 0x2FB5,5590x0E09, 0x2436, 0x1B9B, 0xDF3D, 0xCD26, 0x4E69, 0x7FCD, 0xEA9F,5600x121B, 0x1D9E, 0x5874, 0x342E, 0x362D, 0xDCB2, 0xB4EE, 0x5BFB,5610xA4F6, 0x764D, 0xB761, 0x7DCE, 0x527B, 0xDD3E, 0x5E71, 0x1397,5620xA6F5, 0xB968, 0x0000, 0xC12C, 0x4060, 0xE31F, 0x79C8, 0xB6ED,5630xD4BE, 0x8D46, 0x67D9, 0x724B, 0x94DE, 0x98D4, 0xB0E8, 0x854A,5640xBB6B, 0xC52A, 0x4FE5, 0xED16, 0x86C5, 0x9AD7, 0x6655, 0x1194,5650x8ACF, 0xE910, 0x0406, 0xFE81, 0xA0F0, 0x7844, 0x25BA, 0x4BE3,5660xA2F3, 0x5DFE, 0x80C0, 0x058A, 0x3FAD, 0x21BC, 0x7048, 0xF104,5670x63DF, 0x77C1, 0xAF75, 0x4263, 0x2030, 0xE51A, 0xFD0E, 0xBF6D,5680x814C, 0x1814, 0x2635, 0xC32F, 0xBEE1, 0x35A2, 0x88CC, 0x2E39,5690x9357, 0x55F2, 0xFC82, 0x7A47, 0xC8AC, 0xBAE7, 0x322B, 0xE695,5700xC0A0, 0x1998, 0x9ED1, 0xA37F, 0x4466, 0x547E, 0x3BAB, 0x0B83,5710x8CCA, 0xC729, 0x6BD3, 0x283C, 0xA779, 0xBCE2, 0x161D, 0xAD76,5720xDB3B, 0x6456, 0x744E, 0x141E, 0x92DB, 0x0C0A, 0x486C, 0xB8E4,5730x9F5D, 0xBD6E, 0x43EF, 0xC4A6, 0x39A8, 0x31A4, 0xD337, 0xF28B,5740xD532, 0x8B43, 0x6E59, 0xDAB7, 0x018C, 0xB164, 0x9CD2, 0x49E0,5750xD8B4, 0xACFA, 0xF307, 0xCF25, 0xCAAF, 0xF48E, 0x47E9, 0x1018,5760x6FD5, 0xF088, 0x4A6F, 0x5C72, 0x3824, 0x57F1, 0x73C7, 0x9751,5770xCB23, 0xA17C, 0xE89C, 0x3E21, 0x96DD, 0x61DC, 0x0D86, 0x0F85,5780xE090, 0x7C42, 0x71C4, 0xCCAA, 0x90D8, 0x0605, 0xF701, 0x1C12,5790xC2A3, 0x6A5F, 0xAEF9, 0x69D0, 0x1791, 0x9958, 0x3A27, 0x27B9,5800xD938, 0xEB13, 0x2BB3, 0x2233, 0xD2BB, 0xA970, 0x0789, 0x33A7,5810x2DB6, 0x3C22, 0x1592, 0xC920, 0x8749, 0xAAFF, 0x5078, 0xA57A,5820x038F, 0x59F8, 0x0980, 0x1A17, 0x65DA, 0xD731, 0x84C6, 0xD0B8,5830x82C3, 0x29B0, 0x5A77, 0x1E11, 0x7BCB, 0xA8FC, 0x6DD6, 0x2C3A,584};585586static __inline u16 _S_(u16 v)587{588u16 t = Sbox[Hi8(v)];589return Sbox[Lo8(v)] ^ ((t << 8) | (t >> 8));590}591592#define PHASE1_LOOP_COUNT 8593594static void tkip_mixing_phase1(u16 *TTAK, const u8 *TK, const u8 *TA, u32 IV32)595{596int i, j;597598/* Initialize the 80-bit TTAK from TSC (IV32) and TA[0..5] */599TTAK[0] = Lo16(IV32);600TTAK[1] = Hi16(IV32);601TTAK[2] = Mk16(TA[1], TA[0]);602TTAK[3] = Mk16(TA[3], TA[2]);603TTAK[4] = Mk16(TA[5], TA[4]);604605for (i = 0; i < PHASE1_LOOP_COUNT; i++) {606j = 2 * (i & 1);607TTAK[0] += _S_(TTAK[4] ^ Mk16(TK[1 + j], TK[0 + j]));608TTAK[1] += _S_(TTAK[0] ^ Mk16(TK[5 + j], TK[4 + j]));609TTAK[2] += _S_(TTAK[1] ^ Mk16(TK[9 + j], TK[8 + j]));610TTAK[3] += _S_(TTAK[2] ^ Mk16(TK[13 + j], TK[12 + j]));611TTAK[4] += _S_(TTAK[3] ^ Mk16(TK[1 + j], TK[0 + j])) + i;612}613}614615#ifndef _BYTE_ORDER616#error "Don't know native byte order"617#endif618619static void tkip_mixing_phase2(u8 *WEPSeed, const u8 *TK, const u16 *TTAK,620u16 IV16)621{622/* Make temporary area overlap WEP seed so that the final copy can be623* avoided on little endian hosts. */624u16 *PPK = (u16 *) &WEPSeed[4];625626/* Step 1 - make copy of TTAK and bring in TSC */627PPK[0] = TTAK[0];628PPK[1] = TTAK[1];629PPK[2] = TTAK[2];630PPK[3] = TTAK[3];631PPK[4] = TTAK[4];632PPK[5] = TTAK[4] + IV16;633634/* Step 2 - 96-bit bijective mixing using S-box */635PPK[0] += _S_(PPK[5] ^ Mk16_le((const u16 *) &TK[0]));636PPK[1] += _S_(PPK[0] ^ Mk16_le((const u16 *) &TK[2]));637PPK[2] += _S_(PPK[1] ^ Mk16_le((const u16 *) &TK[4]));638PPK[3] += _S_(PPK[2] ^ Mk16_le((const u16 *) &TK[6]));639PPK[4] += _S_(PPK[3] ^ Mk16_le((const u16 *) &TK[8]));640PPK[5] += _S_(PPK[4] ^ Mk16_le((const u16 *) &TK[10]));641642PPK[0] += RotR1(PPK[5] ^ Mk16_le((const u16 *) &TK[12]));643PPK[1] += RotR1(PPK[0] ^ Mk16_le((const u16 *) &TK[14]));644PPK[2] += RotR1(PPK[1]);645PPK[3] += RotR1(PPK[2]);646PPK[4] += RotR1(PPK[3]);647PPK[5] += RotR1(PPK[4]);648649/* Step 3 - bring in last of TK bits, assign 24-bit WEP IV value650* WEPSeed[0..2] is transmitted as WEP IV */651WEPSeed[0] = Hi8(IV16);652WEPSeed[1] = (Hi8(IV16) | 0x20) & 0x7F;653WEPSeed[2] = Lo8(IV16);654WEPSeed[3] = Lo8((PPK[5] ^ Mk16_le((const u16 *) &TK[0])) >> 1);655656#if _BYTE_ORDER == _BIG_ENDIAN657{658int i;659for (i = 0; i < 6; i++)660PPK[i] = (PPK[i] << 8) | (PPK[i] >> 8);661}662#endif663}664665static void666wep_encrypt(u8 *key, struct mbuf *m0, u_int off, size_t data_len,667uint8_t icv[IEEE80211_WEP_CRCLEN])668{669u32 i, j, k, crc;670size_t buflen;671u8 S[256];672u8 *pos;673struct mbuf *m;674#define S_SWAP(a,b) do { u8 t = S[a]; S[a] = S[b]; S[b] = t; } while(0)675676/* Setup RC4 state */677for (i = 0; i < 256; i++)678S[i] = i;679j = 0;680for (i = 0; i < 256; i++) {681j = (j + S[i] + key[i & 0x0f]) & 0xff;682S_SWAP(i, j);683}684685/* Compute CRC32 over unencrypted data and apply RC4 to data */686crc = ~0;687i = j = 0;688m = m0;689pos = mtod(m, uint8_t *) + off;690buflen = m->m_len - off;691for (;;) {692if (buflen > data_len)693buflen = data_len;694data_len -= buflen;695for (k = 0; k < buflen; k++) {696crc = crc32_table[(crc ^ *pos) & 0xff] ^ (crc >> 8);697i = (i + 1) & 0xff;698j = (j + S[i]) & 0xff;699S_SWAP(i, j);700*pos++ ^= S[(S[i] + S[j]) & 0xff];701}702m = m->m_next;703if (m == NULL) {704KASSERT(data_len == 0,705("out of buffers with data_len %zu\n", data_len));706break;707}708pos = mtod(m, uint8_t *);709buflen = m->m_len;710}711crc = ~crc;712713/* Append little-endian CRC32 and encrypt it to produce ICV */714icv[0] = crc;715icv[1] = crc >> 8;716icv[2] = crc >> 16;717icv[3] = crc >> 24;718for (k = 0; k < IEEE80211_WEP_CRCLEN; k++) {719i = (i + 1) & 0xff;720j = (j + S[i]) & 0xff;721S_SWAP(i, j);722icv[k] ^= S[(S[i] + S[j]) & 0xff];723}724}725726static int727wep_decrypt(u8 *key, struct mbuf *m, u_int off, size_t data_len)728{729u32 i, j, k, crc;730u8 S[256];731u8 *pos, icv[4];732size_t buflen;733734/* Setup RC4 state */735for (i = 0; i < 256; i++)736S[i] = i;737j = 0;738for (i = 0; i < 256; i++) {739j = (j + S[i] + key[i & 0x0f]) & 0xff;740S_SWAP(i, j);741}742743/* Apply RC4 to data and compute CRC32 over decrypted data */744crc = ~0;745i = j = 0;746pos = mtod(m, uint8_t *) + off;747buflen = m->m_len - off;748for (;;) {749if (buflen > data_len)750buflen = data_len;751data_len -= buflen;752for (k = 0; k < buflen; k++) {753i = (i + 1) & 0xff;754j = (j + S[i]) & 0xff;755S_SWAP(i, j);756*pos ^= S[(S[i] + S[j]) & 0xff];757crc = crc32_table[(crc ^ *pos) & 0xff] ^ (crc >> 8);758pos++;759}760m = m->m_next;761if (m == NULL) {762KASSERT(data_len == 0,763("out of buffers with data_len %zu\n", data_len));764break;765}766pos = mtod(m, uint8_t *);767buflen = m->m_len;768}769crc = ~crc;770771/* Encrypt little-endian CRC32 and verify that it matches with the772* received ICV */773icv[0] = crc;774icv[1] = crc >> 8;775icv[2] = crc >> 16;776icv[3] = crc >> 24;777for (k = 0; k < 4; k++) {778i = (i + 1) & 0xff;779j = (j + S[i]) & 0xff;780S_SWAP(i, j);781if ((icv[k] ^ S[(S[i] + S[j]) & 0xff]) != *pos++) {782/* ICV mismatch - drop frame */783return -1;784}785}786787return 0;788}789790static __inline u32 rotl(u32 val, int bits)791{792return (val << bits) | (val >> (32 - bits));793}794795static __inline u32 rotr(u32 val, int bits)796{797return (val >> bits) | (val << (32 - bits));798}799800static __inline u32 xswap(u32 val)801{802return ((val & 0x00ff00ff) << 8) | ((val & 0xff00ff00) >> 8);803}804805#define michael_block(l, r) \806do { \807r ^= rotl(l, 17); \808l += r; \809r ^= xswap(l); \810l += r; \811r ^= rotl(l, 3); \812l += r; \813r ^= rotr(l, 2); \814l += r; \815} while (0)816817static __inline u32 get_le32_split(u8 b0, u8 b1, u8 b2, u8 b3)818{819return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);820}821822static __inline u32 get_le32(const u8 *p)823{824return get_le32_split(p[0], p[1], p[2], p[3]);825}826827static __inline void put_le32(u8 *p, u32 v)828{829p[0] = v;830p[1] = v >> 8;831p[2] = v >> 16;832p[3] = v >> 24;833}834835/*836* Craft pseudo header used to calculate the MIC.837*/838static void839michael_mic_hdr(const struct ieee80211_frame *wh0, uint8_t hdr[16])840{841const struct ieee80211_frame_addr4 *wh =842(const struct ieee80211_frame_addr4 *) wh0;843844switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {845case IEEE80211_FC1_DIR_NODS:846IEEE80211_ADDR_COPY(hdr, wh->i_addr1); /* DA */847IEEE80211_ADDR_COPY(hdr + IEEE80211_ADDR_LEN, wh->i_addr2);848break;849case IEEE80211_FC1_DIR_TODS:850IEEE80211_ADDR_COPY(hdr, wh->i_addr3); /* DA */851IEEE80211_ADDR_COPY(hdr + IEEE80211_ADDR_LEN, wh->i_addr2);852break;853case IEEE80211_FC1_DIR_FROMDS:854IEEE80211_ADDR_COPY(hdr, wh->i_addr1); /* DA */855IEEE80211_ADDR_COPY(hdr + IEEE80211_ADDR_LEN, wh->i_addr3);856break;857case IEEE80211_FC1_DIR_DSTODS:858IEEE80211_ADDR_COPY(hdr, wh->i_addr3); /* DA */859IEEE80211_ADDR_COPY(hdr + IEEE80211_ADDR_LEN, wh->i_addr4);860break;861}862863/* Match on any QOS frame, not just data */864if (IEEE80211_IS_QOS_ANY(wh)) {865const struct ieee80211_qosframe *qwh =866(const struct ieee80211_qosframe *) wh;867hdr[12] = qwh->i_qos[0] & IEEE80211_QOS_TID;868} else869hdr[12] = 0;870hdr[13] = hdr[14] = hdr[15] = 0; /* reserved */871}872873static void874michael_mic(struct tkip_ctx *ctx, const u8 *key,875struct mbuf *m, u_int off, size_t data_len,876u8 mic[IEEE80211_WEP_MICLEN])877{878uint8_t hdr[16];879u32 l, r;880const uint8_t *data;881u_int space;882883michael_mic_hdr(mtod(m, struct ieee80211_frame *), hdr);884885l = get_le32(key);886r = get_le32(key + 4);887888/* Michael MIC pseudo header: DA, SA, 3 x 0, Priority */889l ^= get_le32(hdr);890michael_block(l, r);891l ^= get_le32(&hdr[4]);892michael_block(l, r);893l ^= get_le32(&hdr[8]);894michael_block(l, r);895l ^= get_le32(&hdr[12]);896michael_block(l, r);897898/* first buffer has special handling */899data = mtod(m, const uint8_t *) + off;900space = m->m_len - off;901for (;;) {902if (space > data_len)903space = data_len;904/* collect 32-bit blocks from current buffer */905while (space >= sizeof(uint32_t)) {906l ^= get_le32(data);907michael_block(l, r);908data += sizeof(uint32_t), space -= sizeof(uint32_t);909data_len -= sizeof(uint32_t);910}911/*912* NB: when space is zero we make one more trip around913* the loop to advance to the next mbuf where there is914* data. This handles the case where there are 4*n915* bytes in an mbuf followed by <4 bytes in a later mbuf.916* By making an extra trip we'll drop out of the loop917* with m pointing at the mbuf with 3 bytes and space918* set as required by the remainder handling below.919*/920if (data_len == 0 ||921(data_len < sizeof(uint32_t) && space != 0))922break;923m = m->m_next;924if (m == NULL) {925KASSERT(0, ("out of data, data_len %zu\n", data_len));926break;927}928if (space != 0) {929const uint8_t *data_next;930/*931* Block straddles buffers, split references.932*/933data_next = mtod(m, const uint8_t *);934KASSERT(m->m_len >= sizeof(uint32_t) - space,935("not enough data in following buffer, "936"m_len %u need %zu\n", m->m_len,937sizeof(uint32_t) - space));938switch (space) {939case 1:940l ^= get_le32_split(data[0], data_next[0],941data_next[1], data_next[2]);942data = data_next + 3;943space = m->m_len - 3;944break;945case 2:946l ^= get_le32_split(data[0], data[1],947data_next[0], data_next[1]);948data = data_next + 2;949space = m->m_len - 2;950break;951case 3:952l ^= get_le32_split(data[0], data[1],953data[2], data_next[0]);954data = data_next + 1;955space = m->m_len - 1;956break;957}958michael_block(l, r);959data_len -= sizeof(uint32_t);960} else {961/*962* Setup for next buffer.963*/964data = mtod(m, const uint8_t *);965space = m->m_len;966}967}968/*969* Catch degenerate cases like mbuf[4*n+1 bytes] followed by970* mbuf[2 bytes]. I don't believe these should happen; if they971* do then we'll need more involved logic.972*/973KASSERT(data_len <= space,974("not enough data, data_len %zu space %u\n", data_len, space));975976/* Last block and padding (0x5a, 4..7 x 0) */977switch (data_len) {978case 0:979l ^= get_le32_split(0x5a, 0, 0, 0);980break;981case 1:982l ^= get_le32_split(data[0], 0x5a, 0, 0);983break;984case 2:985l ^= get_le32_split(data[0], data[1], 0x5a, 0);986break;987case 3:988l ^= get_le32_split(data[0], data[1], data[2], 0x5a);989break;990}991michael_block(l, r);992/* l ^= 0; */993michael_block(l, r);994995put_le32(mic, l);996put_le32(mic + 4, r);997}998999static int1000tkip_encrypt(struct tkip_ctx *ctx, struct ieee80211_key *key,1001struct mbuf *m, int hdrlen)1002{1003struct ieee80211_frame *wh;1004uint8_t icv[IEEE80211_WEP_CRCLEN];10051006ctx->tc_vap->iv_stats.is_crypto_tkip++;10071008wh = mtod(m, struct ieee80211_frame *);1009if ((u16)(key->wk_keytsc) == 0 || key->wk_keytsc == 1) {1010tkip_mixing_phase1(ctx->tx_ttak, key->wk_key, wh->i_addr2,1011(u32)(key->wk_keytsc >> 16));1012}1013tkip_mixing_phase2(ctx->tx_rc4key, key->wk_key, ctx->tx_ttak,1014(u16) key->wk_keytsc);10151016wep_encrypt(ctx->tx_rc4key,1017m, hdrlen + tkip.ic_header,1018m->m_pkthdr.len - (hdrlen + tkip.ic_header),1019icv);1020(void) m_append(m, IEEE80211_WEP_CRCLEN, icv); /* XXX check return */10211022return 1;1023}10241025static int1026tkip_decrypt(struct tkip_ctx *ctx, struct ieee80211_key *key,1027struct mbuf *m, int hdrlen)1028{1029struct ieee80211_frame *wh;1030struct ieee80211vap *vap = ctx->tc_vap;1031u32 iv32;1032u16 iv16;1033u8 tid;10341035vap->iv_stats.is_crypto_tkip++;10361037wh = mtod(m, struct ieee80211_frame *);1038/* NB: tkip_decap already verified header and left seq in rx_rsc */1039iv16 = (u16) ctx->rx_rsc;1040iv32 = (u32) (ctx->rx_rsc >> 16);10411042tid = ieee80211_gettid(wh);1043if (iv32 != (u32)(key->wk_keyrsc[tid] >> 16) || !ctx->rx_phase1_done) {1044tkip_mixing_phase1(ctx->rx_ttak, key->wk_key,1045wh->i_addr2, iv32);1046ctx->rx_phase1_done = 1;1047}1048tkip_mixing_phase2(ctx->rx_rc4key, key->wk_key, ctx->rx_ttak, iv16);10491050/* NB: m is unstripped; deduct headers + ICV to get payload */1051if (wep_decrypt(ctx->rx_rc4key,1052m, hdrlen + tkip.ic_header,1053m->m_pkthdr.len - (hdrlen + tkip.ic_header + tkip.ic_trailer))) {1054if (iv32 != (u32)(key->wk_keyrsc[tid] >> 16)) {1055/* Previously cached Phase1 result was already lost, so1056* it needs to be recalculated for the next packet. */1057ctx->rx_phase1_done = 0;1058}1059IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,1060"%s", "TKIP ICV mismatch on decrypt");1061vap->iv_stats.is_rx_tkipicv++;1062return 0;1063}1064return 1;1065}10661067/*1068* Module glue.1069*/1070IEEE80211_CRYPTO_MODULE(tkip, 1);107110721073