/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2001 Atsushi Onoe4* Copyright (c) 2002-2008 Sam Leffler, Errno Consulting5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR17* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES18* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.19* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,20* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT21* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF25* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26*/27#ifndef _NET80211_IEEE80211_CRYPTO_H_28#define _NET80211_IEEE80211_CRYPTO_H_2930/*31* 802.11 protocol crypto-related definitions.32*/33#define IEEE80211_KEYBUF_SIZE 1634#define IEEE80211_MICBUF_SIZE (8+8) /* space for both tx+rx keys */3536/*37* Old WEP-style key. Deprecated.38*/39struct ieee80211_wepkey {40u_int wk_len; /* key length in bytes */41uint8_t wk_key[IEEE80211_KEYBUF_SIZE];42};4344struct ieee80211_rsnparms {45uint8_t rsn_mcastcipher; /* mcast/group cipher */46uint8_t rsn_mcastkeylen; /* mcast key length */47uint8_t rsn_ucastcipher; /* selected unicast cipher */48uint8_t rsn_ucastkeylen; /* unicast key length */49uint8_t rsn_keymgmt; /* selected key mgmt algo */50uint16_t rsn_caps; /* capabilities */51};5253struct ieee80211_cipher;5455/*56* Crypto key state. There is sufficient room for all supported57* ciphers (see below). The underlying ciphers are handled58* separately through loadable cipher modules that register with59* the generic crypto support. A key has a reference to an instance60* of the cipher; any per-key state is hung off wk_private by the61* cipher when it is attached. Ciphers are automatically called62* to detach and cleanup any such state when the key is deleted.63*64* The generic crypto support handles encap/decap of cipher-related65* frame contents for both hardware- and software-based implementations.66* A key requiring software crypto support is automatically flagged and67* the cipher is expected to honor this and do the necessary work.68* Ciphers such as TKIP may also support mixed hardware/software69* encrypt/decrypt and MIC processing.70*/71typedef uint16_t ieee80211_keyix; /* h/w key index */7273struct ieee80211_key {74uint8_t wk_keylen; /* key length in bytes */75uint8_t wk_pad; /* .. some drivers use this. Fix that. */76uint8_t wk_pad1[2];77uint32_t wk_flags;78#define IEEE80211_KEY_XMIT 0x00000001 /* key used for xmit */79#define IEEE80211_KEY_RECV 0x00000002 /* key used for recv */80#define IEEE80211_KEY_GROUP 0x00000004 /* key used for WPA group operation */81#define IEEE80211_KEY_NOREPLAY 0x00000008 /* ignore replay failures */82#define IEEE80211_KEY_SWENCRYPT 0x00000010 /* host-based encrypt */83#define IEEE80211_KEY_SWDECRYPT 0x00000020 /* host-based decrypt */84#define IEEE80211_KEY_SWENMIC 0x00000040 /* host-based enmic */85#define IEEE80211_KEY_SWDEMIC 0x00000080 /* host-based demic */86#define IEEE80211_KEY_DEVKEY 0x00000100 /* device key request completed */87#define IEEE80211_KEY_CIPHER0 0x00001000 /* cipher-specific action 0 */88#define IEEE80211_KEY_CIPHER1 0x00002000 /* cipher-specific action 1 */89#define IEEE80211_KEY_NOIV 0x00004000 /* don't insert IV/MIC for !mgmt */90#define IEEE80211_KEY_NOIVMGT 0x00008000 /* don't insert IV/MIC for mgmt */91#define IEEE80211_KEY_NOMIC 0x00010000 /* don't insert MIC for !mgmt */92#define IEEE80211_KEY_NOMICMGT 0x00020000 /* don't insert MIC for mgmt */9394ieee80211_keyix wk_keyix; /* h/w key index */95ieee80211_keyix wk_rxkeyix; /* optional h/w rx key index */96/* TODO: deprecate direct access to wk_key, wk_txmic, wk_rxmic */97uint8_t wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];98#define wk_txmic wk_key+IEEE80211_KEYBUF_SIZE+0 /* XXX can't () right */99#define wk_rxmic wk_key+IEEE80211_KEYBUF_SIZE+8 /* XXX can't () right */100/* key receive sequence counter */101uint64_t wk_keyrsc[IEEE80211_TID_SIZE];102uint64_t wk_keytsc; /* key transmit sequence counter */103const struct ieee80211_cipher *wk_cipher;104void *wk_private; /* private cipher state */105uint8_t wk_macaddr[IEEE80211_ADDR_LEN];106};107#define IEEE80211_KEY_COMMON /* common flags passed in by apps */\108(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP | \109IEEE80211_KEY_NOREPLAY)110111#define IEEE80211_KEY_SWCRYPT \112(IEEE80211_KEY_SWENCRYPT | IEEE80211_KEY_SWDECRYPT)113#define IEEE80211_KEY_SWMIC (IEEE80211_KEY_SWENMIC | IEEE80211_KEY_SWDEMIC)114115#define IEEE80211_KEY_DEVICE /* flags owned by device driver */\116(IEEE80211_KEY_DEVKEY|IEEE80211_KEY_CIPHER0|IEEE80211_KEY_CIPHER1| \117IEEE80211_KEY_SWCRYPT|IEEE80211_KEY_SWMIC|IEEE80211_KEY_NOIV | \118IEEE80211_KEY_NOIVMGT|IEEE80211_KEY_NOMIC|IEEE80211_KEY_NOMICMGT)119120#define IEEE80211_KEY_BITS \121"\20\1XMIT\2RECV\3GROUP\4NOREPLAY\5SWENCRYPT\6SWDECRYPT\7SWENMIC\10SWDEMIC" \122"\11DEVKEY\12CIPHER0\13CIPHER1\14NOIV\15NOIVMGT\16NOMIC\17NOMICMGT"123124#define IEEE80211_KEYIX_NONE ((ieee80211_keyix) -1)125126/*127* NB: these values are ordered carefully; there are lots of128* of implications in any reordering. Beware that 4 is used129* only to indicate h/w TKIP MIC support in driver capabilities;130* there is no separate cipher support (it's rolled into the131* TKIP cipher support).132*/133#define IEEE80211_CIPHER_WEP 0134#define IEEE80211_CIPHER_TKIP 1135#define IEEE80211_CIPHER_AES_OCB 2136#define IEEE80211_CIPHER_AES_CCM 3137#define IEEE80211_CIPHER_TKIPMIC 4 /* TKIP MIC capability */138#define IEEE80211_CIPHER_CKIP 5139#define IEEE80211_CIPHER_NONE 6 /* pseudo value */140#define IEEE80211_CIPHER_AES_CCM_256 7141#define IEEE80211_CIPHER_BIP_CMAC_128 8142#define IEEE80211_CIPHER_BIP_CMAC_256 9143#define IEEE80211_CIPHER_BIP_GMAC_128 10144#define IEEE80211_CIPHER_BIP_GMAC_256 11145#define IEEE80211_CIPHER_AES_GCM_128 12146#define IEEE80211_CIPHER_AES_GCM_256 13147148#define IEEE80211_CIPHER_LAST 13149150#define IEEE80211_CIPHER_MAX (IEEE80211_CIPHER_LAST+1)151152/* capability bits in ic_cryptocaps/iv_cryptocaps */153#define IEEE80211_CRYPTO_WEP (1<<IEEE80211_CIPHER_WEP)154#define IEEE80211_CRYPTO_TKIP (1<<IEEE80211_CIPHER_TKIP)155#define IEEE80211_CRYPTO_AES_OCB (1<<IEEE80211_CIPHER_AES_OCB)156#define IEEE80211_CRYPTO_AES_CCM (1<<IEEE80211_CIPHER_AES_CCM)157#define IEEE80211_CRYPTO_TKIPMIC (1<<IEEE80211_CIPHER_TKIPMIC)158#define IEEE80211_CRYPTO_CKIP (1<<IEEE80211_CIPHER_CKIP)159#define IEEE80211_CRYPTO_AES_CCM_256 (1<<IEEE80211_CIPHER_AES_CCM_256)160#define IEEE80211_CRYPTO_BIP_CMAC_128 (1<<IEEE80211_CIPHER_BIP_CMAC_128)161#define IEEE80211_CRYPTO_BIP_CMAC_256 (1<<IEEE80211_CIPHER_BIP_CMAC_256)162#define IEEE80211_CRYPTO_BIP_GMAC_128 (1<<IEEE80211_CIPHER_BIP_GMAC_128)163#define IEEE80211_CRYPTO_BIP_GMAC_256 (1<<IEEE80211_CIPHER_BIP_GMAC_256)164#define IEEE80211_CRYPTO_AES_GCM_128 (1<<IEEE80211_CIPHER_AES_GCM_128)165#define IEEE80211_CRYPTO_AES_GCM_256 (1<<IEEE80211_CIPHER_AES_GCM_256)166167#define IEEE80211_CRYPTO_BITS \168"\20\1WEP\2TKIP\3AES\4AES_CCM\5TKIPMIC\6CKIP\10AES_CCM_256" \169"\11BIP_CMAC_128\12BIP_CMAC_256\13BIP_GMAC_128\14BIP_CMAC_256" \170"\15AES_GCM_128\16AES_GCM_256"171172#if defined(__KERNEL__) || defined(_KERNEL)173174struct ieee80211com;175struct ieee80211vap;176struct ieee80211_node;177struct mbuf;178179MALLOC_DECLARE(M_80211_CRYPTO);180181void ieee80211_crypto_attach(struct ieee80211com *);182void ieee80211_crypto_detach(struct ieee80211com *);183void ieee80211_crypto_set_supported_software_ciphers(struct ieee80211com *,184uint32_t cipher_set);185void ieee80211_crypto_set_supported_hardware_ciphers(struct ieee80211com *,186uint32_t cipher_set);187void ieee80211_crypto_set_supported_driver_keymgmt(struct ieee80211com *,188uint32_t keymgmt_set);189void ieee80211_crypto_vattach(struct ieee80211vap *);190void ieee80211_crypto_vdetach(struct ieee80211vap *);191int ieee80211_crypto_newkey(struct ieee80211vap *,192int cipher, int flags, struct ieee80211_key *);193int ieee80211_crypto_delkey(struct ieee80211vap *,194struct ieee80211_key *);195int ieee80211_crypto_setkey(struct ieee80211vap *, struct ieee80211_key *);196void ieee80211_crypto_delglobalkeys(struct ieee80211vap *);197void ieee80211_crypto_reload_keys(struct ieee80211com *);198void ieee80211_crypto_set_deftxkey(struct ieee80211vap *,199ieee80211_keyix kid);200201/*202* Template for a supported cipher. Ciphers register with the203* crypto code and are typically loaded as separate modules204* (the null cipher is always present).205* XXX may need refcnts206*/207struct ieee80211_cipher {208const char *ic_name; /* printable name */209u_int ic_cipher; /* IEEE80211_CIPHER_* */210u_int ic_header; /* size of privacy header (bytes) */211u_int ic_trailer; /* size of privacy trailer (bytes) */212u_int ic_miclen; /* size of mic trailer (bytes) */213void* (*ic_attach)(struct ieee80211vap *, struct ieee80211_key *);214void (*ic_detach)(struct ieee80211_key *);215int (*ic_setkey)(struct ieee80211_key *);216void (*ic_setiv)(struct ieee80211_key *, uint8_t *);217int (*ic_encap)(struct ieee80211_key *, struct mbuf *);218int (*ic_decap)(struct ieee80211_key *, struct mbuf *, int);219/*220* ic_enmic() and ic_demic() are currently only used by TKIP.221* Please see ieee80211_crypto_enmic() and ieee80211_crypto_demic()222* for more information.223*/224int (*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);225int (*ic_demic)(struct ieee80211_key *, struct mbuf *, int);226};227extern const struct ieee80211_cipher ieee80211_cipher_none;228229#define IEEE80211_KEY_UNDEFINED(k) \230((k)->wk_cipher == &ieee80211_cipher_none)231232void ieee80211_crypto_register(const struct ieee80211_cipher *);233void ieee80211_crypto_unregister(const struct ieee80211_cipher *);234int ieee80211_crypto_available(u_int cipher);235236int ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *,237const struct ieee80211_key *k);238uint8_t ieee80211_crypto_get_keyid(struct ieee80211vap *vap,239struct ieee80211_key *k);240struct ieee80211_key *ieee80211_crypto_get_txkey(struct ieee80211_node *,241struct mbuf *);242struct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *,243struct mbuf *);244int ieee80211_crypto_decap(struct ieee80211_node *,245struct mbuf *, int, struct ieee80211_key **);246int ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,247struct mbuf *, int);248/**249* @brief Add any pre-fragmentation MIC to an MSDU.250*251* This is called before 802.11 fragmentation. Crypto types that implement252* a MIC/ICV check per MSDU will not implement this function.253*254* As an example, TKIP implements a Michael MIC check over the entire255* unencrypted MSDU before fragmenting it into MPDUs and passing each256* MPDU to be separately encrypted with their own MIC/ICV.257*258* Please see 802.11-2020 12.5.2.1.2 (TKIP cryptographic encapsulation)259* for more information.260*261* @param vap the current VAP262* @param k the current key263* @param m the mbuf representing the MSDU264* @param f set to 1 to force a MSDU MIC check, even if HW encrypted265* @returns 0 if error / MIC encap failed, 1 if OK266*/267static __inline int268ieee80211_crypto_enmic(struct ieee80211vap *vap,269struct ieee80211_key *k, struct mbuf *m, int force)270{271const struct ieee80211_cipher *cip = k->wk_cipher;272return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);273}274275/*276* Reset key state to an unused state. The crypto277* key allocation mechanism insures other state (e.g.278* key data) is properly setup before a key is used.279*/280static __inline void281ieee80211_crypto_resetkey(struct ieee80211vap *vap,282struct ieee80211_key *k, ieee80211_keyix ix)283{284k->wk_cipher = &ieee80211_cipher_none;285k->wk_private = k->wk_cipher->ic_attach(vap, k);286k->wk_keyix = k->wk_rxkeyix = ix;287k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;288}289290/*291* Crypt-related notification methods.292*/293void ieee80211_notify_replay_failure(struct ieee80211vap *,294const struct ieee80211_frame *, const struct ieee80211_key *,295uint64_t rsc, int tid);296void ieee80211_notify_michael_failure(struct ieee80211vap *,297const struct ieee80211_frame *, ieee80211_keyix keyix);298299/* AAD assembly for CCMP/GCMP. */300uint16_t ieee80211_crypto_init_aad(const struct ieee80211_frame *,301uint8_t *, int);302303/**304* @brief Return the key data.305*306* This returns a pointer to the key data. Note it does not307* guarantee the TX/RX MIC will be immediately after the key.308* Callers must use ieee80211_crypto_get_key_txmic_data()309* and ieee80211_crypto_get_key_rxmic_data() for that.310*311* Note: there's no locking; this needs to be called in312* a situation where the ieee80211_key won't disappear.313*314* @param k ieee80211_key315* @returns NULL if no key data is available, or a pointer316* to the key data.317*/318static inline const uint8_t *319ieee80211_crypto_get_key_data(const struct ieee80211_key *k)320{321return (k->wk_key);322}323324/**325* @brief Return the key length in bytes.326*327* This doesn't include any TX/RX MIC (eg from TKIP).328*329* Note: there's no locking; this needs to be called in330* a situation where the ieee80211_key won't disappear.331*332* @param k ieee80211_key333* @returns the key length (without any MIC) in bytes334*/335static inline const uint16_t336ieee80211_crypto_get_key_len(const struct ieee80211_key *k)337{338return (k->wk_keylen);339}340341/**342* @brief Return the TX MIC data.343*344* This returns a pointer to the TX MIC data.345*346* Note: there's no locking; this needs to be called in347* a situation where the ieee80211_key won't disappear.348*349* @param k ieee80211_key350* @returns NULL if no key data is available, or a pointer351* to the TX MIC data.352*/353static inline const uint8_t *354ieee80211_crypto_get_key_txmic_data(const struct ieee80211_key *k)355{356return (k->wk_txmic);357}358359/**360* @brief Return the TX MIC length in bytes.361*362* Note: there's no locking; this needs to be called in363* a situation where the ieee80211_key won't disappear.364*365* @param k ieee80211_key366* @returns the TX MIC length in bytes367*/368static inline const uint16_t369ieee80211_crypto_get_key_txmic_len(const struct ieee80211_key *k)370{371return (k->wk_cipher->ic_miclen);372}373374/**375* @brief Return the RX MIC data.376*377* This returns a pointer to the RX MIC data.378*379* Note: there's no locking; this needs to be called in380* a situation where the ieee80211_key won't disappear.381*382* @param k ieee80211_key383* @returns NULL if no key data is available, or a pointer384* to the RX MIC data.385*/386static inline const uint8_t *387ieee80211_crypto_get_key_rxmic_data(const struct ieee80211_key *k)388{389return (k->wk_rxmic);390}391392/**393* @brief Return the RX MIC length in bytes.394*395* Note: there's no locking; this needs to be called in396* a situation where the ieee80211_key won't disappear.397*398* @param k ieee80211_key399* @returns the RX MIC length in bytes400*/401static inline const uint16_t402ieee80211_crypto_get_key_rxmic_len(const struct ieee80211_key *k)403{404return (k->wk_cipher->ic_miclen);405}406407#endif /* defined(__KERNEL__) || defined(_KERNEL) */408#endif /* _NET80211_IEEE80211_CRYPTO_H_ */409410411