ieee80211_crypto.h revision 187797
1116742Ssam/*- 2116904Ssam * Copyright (c) 2001 Atsushi Onoe 3178354Ssam * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 4116742Ssam * All rights reserved. 5116742Ssam * 6116742Ssam * Redistribution and use in source and binary forms, with or without 7116742Ssam * modification, are permitted provided that the following conditions 8116742Ssam * are met: 9116742Ssam * 1. Redistributions of source code must retain the above copyright 10116742Ssam * notice, this list of conditions and the following disclaimer. 11116742Ssam * 2. Redistributions in binary form must reproduce the above copyright 12116742Ssam * notice, this list of conditions and the following disclaimer in the 13116742Ssam * documentation and/or other materials provided with the distribution. 14116742Ssam * 15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25116904Ssam * 26116742Ssam * $FreeBSD: head/sys/net80211/ieee80211_crypto.h 187797 2009-01-27 23:00:38Z sam $ 27116742Ssam */ 28116742Ssam#ifndef _NET80211_IEEE80211_CRYPTO_H_ 29116742Ssam#define _NET80211_IEEE80211_CRYPTO_H_ 30116742Ssam 31116742Ssam/* 32116742Ssam * 802.11 protocol crypto-related definitions. 33116742Ssam */ 34116742Ssam#define IEEE80211_KEYBUF_SIZE 16 35138568Ssam#define IEEE80211_MICBUF_SIZE (8+8) /* space for both tx+rx keys */ 36116742Ssam 37138568Ssam/* 38138568Ssam * Old WEP-style key. Deprecated. 39138568Ssam */ 40116742Ssamstruct ieee80211_wepkey { 41138568Ssam u_int wk_len; /* key length in bytes */ 42170530Ssam uint8_t wk_key[IEEE80211_KEYBUF_SIZE]; 43116742Ssam}; 44116742Ssam 45178354Ssamstruct ieee80211_rsnparms { 46178354Ssam uint8_t rsn_mcastcipher; /* mcast/group cipher */ 47178354Ssam uint8_t rsn_mcastkeylen; /* mcast key length */ 48178354Ssam uint8_t rsn_ucastcipher; /* selected unicast cipher */ 49178354Ssam uint8_t rsn_ucastkeylen; /* unicast key length */ 50178354Ssam uint8_t rsn_keymgmt; /* selected key mgmt algo */ 51178354Ssam uint16_t rsn_caps; /* capabilities */ 52178354Ssam}; 53178354Ssam 54138568Ssamstruct ieee80211_cipher; 55138568Ssam 56138568Ssam/* 57138568Ssam * Crypto key state. There is sufficient room for all supported 58138568Ssam * ciphers (see below). The underlying ciphers are handled 59138568Ssam * separately through loadable cipher modules that register with 60138568Ssam * the generic crypto support. A key has a reference to an instance 61138568Ssam * of the cipher; any per-key state is hung off wk_private by the 62138568Ssam * cipher when it is attached. Ciphers are automatically called 63138568Ssam * to detach and cleanup any such state when the key is deleted. 64138568Ssam * 65138568Ssam * The generic crypto support handles encap/decap of cipher-related 66138568Ssam * frame contents for both hardware- and software-based implementations. 67138568Ssam * A key requiring software crypto support is automatically flagged and 68138568Ssam * the cipher is expected to honor this and do the necessary work. 69138568Ssam * Ciphers such as TKIP may also support mixed hardware/software 70138568Ssam * encrypt/decrypt and MIC processing. 71138568Ssam */ 72170530Ssamtypedef uint16_t ieee80211_keyix; /* h/w key index */ 73148863Ssam 74138568Ssamstruct ieee80211_key { 75170530Ssam uint8_t wk_keylen; /* key length in bytes */ 76170530Ssam uint8_t wk_pad; 77170530Ssam uint16_t wk_flags; 78183248Ssam#define IEEE80211_KEY_XMIT 0x0001 /* key used for xmit */ 79183248Ssam#define IEEE80211_KEY_RECV 0x0002 /* key used for recv */ 80183248Ssam#define IEEE80211_KEY_GROUP 0x0004 /* key used for WPA group operation */ 81183248Ssam#define IEEE80211_KEY_SWENCRYPT 0x0010 /* host-based encrypt */ 82183248Ssam#define IEEE80211_KEY_SWDECRYPT 0x0020 /* host-based decrypt */ 83183248Ssam#define IEEE80211_KEY_SWENMIC 0x0040 /* host-based enmic */ 84183248Ssam#define IEEE80211_KEY_SWDEMIC 0x0080 /* host-based demic */ 85183248Ssam#define IEEE80211_KEY_DEVKEY 0x0100 /* device key request completed */ 86183248Ssam#define IEEE80211_KEY_CIPHER0 0x1000 /* cipher-specific action 0 */ 87183248Ssam#define IEEE80211_KEY_CIPHER1 0x2000 /* cipher-specific action 1 */ 88148863Ssam ieee80211_keyix wk_keyix; /* h/w key index */ 89148863Ssam ieee80211_keyix wk_rxkeyix; /* optional h/w rx key index */ 90170530Ssam uint8_t wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE]; 91138568Ssam#define wk_txmic wk_key+IEEE80211_KEYBUF_SIZE+0 /* XXX can't () right */ 92138568Ssam#define wk_rxmic wk_key+IEEE80211_KEYBUF_SIZE+8 /* XXX can't () right */ 93178354Ssam /* key receive sequence counter */ 94178354Ssam uint64_t wk_keyrsc[IEEE80211_TID_SIZE]; 95170530Ssam uint64_t wk_keytsc; /* key transmit sequence counter */ 96138568Ssam const struct ieee80211_cipher *wk_cipher; 97138568Ssam void *wk_private; /* private cipher state */ 98179395Ssam uint8_t wk_macaddr[IEEE80211_ADDR_LEN]; 99138568Ssam}; 100144960Ssam#define IEEE80211_KEY_COMMON /* common flags passed in by apps */\ 101144960Ssam (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP) 102138568Ssam 103179394Ssam#define IEEE80211_KEY_SWCRYPT \ 104179394Ssam (IEEE80211_KEY_SWENCRYPT | IEEE80211_KEY_SWDECRYPT) 105179394Ssam#define IEEE80211_KEY_SWMIC (IEEE80211_KEY_SWENMIC | IEEE80211_KEY_SWDEMIC) 106179394Ssam 107178354Ssam#define IEEE80211_KEYIX_NONE ((ieee80211_keyix) -1) 108178354Ssam 109138568Ssam/* 110138568Ssam * NB: these values are ordered carefully; there are lots of 111178354Ssam * of implications in any reordering. Beware that 4 is used 112178354Ssam * only to indicate h/w TKIP MIC support in driver capabilities; 113178354Ssam * there is no separate cipher support (it's rolled into the 114178354Ssam * TKIP cipher support). 115138568Ssam */ 116138568Ssam#define IEEE80211_CIPHER_WEP 0 117138568Ssam#define IEEE80211_CIPHER_TKIP 1 118138568Ssam#define IEEE80211_CIPHER_AES_OCB 2 119138568Ssam#define IEEE80211_CIPHER_AES_CCM 3 120178354Ssam#define IEEE80211_CIPHER_TKIPMIC 4 /* TKIP MIC capability */ 121138568Ssam#define IEEE80211_CIPHER_CKIP 5 122138568Ssam#define IEEE80211_CIPHER_NONE 6 /* pseudo value */ 123138568Ssam 124138568Ssam#define IEEE80211_CIPHER_MAX (IEEE80211_CIPHER_NONE+1) 125138568Ssam 126178354Ssam/* capability bits in ic_cryptocaps/iv_cryptocaps */ 127178354Ssam#define IEEE80211_CRYPTO_WEP (1<<IEEE80211_CIPHER_WEP) 128178354Ssam#define IEEE80211_CRYPTO_TKIP (1<<IEEE80211_CIPHER_TKIP) 129178354Ssam#define IEEE80211_CRYPTO_AES_OCB (1<<IEEE80211_CIPHER_AES_OCB) 130178354Ssam#define IEEE80211_CRYPTO_AES_CCM (1<<IEEE80211_CIPHER_AES_CCM) 131178354Ssam#define IEEE80211_CRYPTO_TKIPMIC (1<<IEEE80211_CIPHER_TKIPMIC) 132178354Ssam#define IEEE80211_CRYPTO_CKIP (1<<IEEE80211_CIPHER_CKIP) 133138568Ssam 134187797Ssam#define IEEE80211_CRYPTO_BITS \ 135187797Ssam "\20\1WEP\2TKIP\3AES\4AES_CCM\5TKIPMIC\6CKIP" 136187797Ssam 137138568Ssam#if defined(__KERNEL__) || defined(_KERNEL) 138138568Ssam 139138568Ssamstruct ieee80211com; 140178354Ssamstruct ieee80211vap; 141138568Ssamstruct ieee80211_node; 142138568Ssamstruct mbuf; 143138568Ssam 144178354SsamMALLOC_DECLARE(M_80211_CRYPTO); 145138568Ssam 146144618Ssamvoid ieee80211_crypto_attach(struct ieee80211com *); 147144618Ssamvoid ieee80211_crypto_detach(struct ieee80211com *); 148178354Ssamvoid ieee80211_crypto_vattach(struct ieee80211vap *); 149178354Ssamvoid ieee80211_crypto_vdetach(struct ieee80211vap *); 150178354Ssamint ieee80211_crypto_newkey(struct ieee80211vap *, 151144960Ssam int cipher, int flags, struct ieee80211_key *); 152178354Ssamint ieee80211_crypto_delkey(struct ieee80211vap *, 153138568Ssam struct ieee80211_key *); 154179395Ssamint ieee80211_crypto_setkey(struct ieee80211vap *, struct ieee80211_key *); 155178354Ssamvoid ieee80211_crypto_delglobalkeys(struct ieee80211vap *); 156179396Ssamvoid ieee80211_crypto_reload_keys(struct ieee80211com *); 157138568Ssam 158138568Ssam/* 159138568Ssam * Template for a supported cipher. Ciphers register with the 160138568Ssam * crypto code and are typically loaded as separate modules 161138568Ssam * (the null cipher is always present). 162138568Ssam * XXX may need refcnts 163138568Ssam */ 164138568Ssamstruct ieee80211_cipher { 165138568Ssam const char *ic_name; /* printable name */ 166138568Ssam u_int ic_cipher; /* IEEE80211_CIPHER_* */ 167138568Ssam u_int ic_header; /* size of privacy header (bytes) */ 168138568Ssam u_int ic_trailer; /* size of privacy trailer (bytes) */ 169138568Ssam u_int ic_miclen; /* size of mic trailer (bytes) */ 170178354Ssam void* (*ic_attach)(struct ieee80211vap *, struct ieee80211_key *); 171138568Ssam void (*ic_detach)(struct ieee80211_key *); 172138568Ssam int (*ic_setkey)(struct ieee80211_key *); 173138568Ssam int (*ic_encap)(struct ieee80211_key *, struct mbuf *, 174170530Ssam uint8_t keyid); 175147252Ssam int (*ic_decap)(struct ieee80211_key *, struct mbuf *, int); 176147045Ssam int (*ic_enmic)(struct ieee80211_key *, struct mbuf *, int); 177147045Ssam int (*ic_demic)(struct ieee80211_key *, struct mbuf *, int); 178138568Ssam}; 179138568Ssamextern const struct ieee80211_cipher ieee80211_cipher_none; 180138568Ssam 181167432Ssam#define IEEE80211_KEY_UNDEFINED(k) \ 182167432Ssam ((k)->wk_cipher == &ieee80211_cipher_none) 183167432Ssam 184144618Ssamvoid ieee80211_crypto_register(const struct ieee80211_cipher *); 185144618Ssamvoid ieee80211_crypto_unregister(const struct ieee80211_cipher *); 186144618Ssamint ieee80211_crypto_available(u_int cipher); 187138568Ssam 188178354Ssamstruct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *, 189178354Ssam struct mbuf *); 190178354Ssamstruct ieee80211_key *ieee80211_crypto_decap(struct ieee80211_node *, 191178354Ssam struct mbuf *, int); 192138568Ssam 193138568Ssam/* 194138568Ssam * Check and remove any MIC. 195138568Ssam */ 196138568Ssamstatic __inline int 197178354Ssamieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k, 198147045Ssam struct mbuf *m, int force) 199138568Ssam{ 200138568Ssam const struct ieee80211_cipher *cip = k->wk_cipher; 201147045Ssam return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1); 202138568Ssam} 203138568Ssam 204138568Ssam/* 205138568Ssam * Add any MIC. 206138568Ssam */ 207138568Ssamstatic __inline int 208178354Ssamieee80211_crypto_enmic(struct ieee80211vap *vap, 209147045Ssam struct ieee80211_key *k, struct mbuf *m, int force) 210138568Ssam{ 211138568Ssam const struct ieee80211_cipher *cip = k->wk_cipher; 212147045Ssam return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1); 213138568Ssam} 214138568Ssam 215138568Ssam/* 216138568Ssam * Reset key state to an unused state. The crypto 217138568Ssam * key allocation mechanism insures other state (e.g. 218138568Ssam * key data) is properly setup before a key is used. 219138568Ssam */ 220138568Ssamstatic __inline void 221178354Ssamieee80211_crypto_resetkey(struct ieee80211vap *vap, 222148863Ssam struct ieee80211_key *k, ieee80211_keyix ix) 223138568Ssam{ 224186143Ssam k->wk_cipher = &ieee80211_cipher_none; 225178354Ssam k->wk_private = k->wk_cipher->ic_attach(vap, k); 226148863Ssam k->wk_keyix = k->wk_rxkeyix = ix; 227138568Ssam k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV; 228138568Ssam} 229138568Ssam 230138568Ssam/* 231138568Ssam * Crypt-related notification methods. 232138568Ssam */ 233178354Ssamvoid ieee80211_notify_replay_failure(struct ieee80211vap *, 234138568Ssam const struct ieee80211_frame *, const struct ieee80211_key *, 235178354Ssam uint64_t rsc); 236178354Ssamvoid ieee80211_notify_michael_failure(struct ieee80211vap *, 237138568Ssam const struct ieee80211_frame *, u_int keyix); 238138568Ssam#endif /* defined(__KERNEL__) || defined(_KERNEL) */ 239116742Ssam#endif /* _NET80211_IEEE80211_CRYPTO_H_ */ 240