13147Sxc151355 /* 2*6411Szf162725 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 33147Sxc151355 * Use is subject to license terms. 43147Sxc151355 */ 53147Sxc151355 63147Sxc151355 /* 73147Sxc151355 * Copyright (c) 2001 Atsushi Onoe 83147Sxc151355 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 93147Sxc151355 * All rights reserved. 103147Sxc151355 * 113147Sxc151355 * Redistribution and use in source and binary forms, with or without 123147Sxc151355 * modification, are permitted provided that the following conditions 133147Sxc151355 * are met: 143147Sxc151355 * 1. Redistributions of source code must retain the above copyright 153147Sxc151355 * notice, this list of conditions and the following disclaimer. 163147Sxc151355 * 2. Redistributions in binary form must reproduce the above copyright 173147Sxc151355 * notice, this list of conditions and the following disclaimer in the 183147Sxc151355 * documentation and/or other materials provided with the distribution. 193147Sxc151355 * 3. The name of the author may not be used to endorse or promote products 203147Sxc151355 * derived from this software without specific prior written permission. 213147Sxc151355 * 223147Sxc151355 * Alternatively, this software may be distributed under the terms of the 233147Sxc151355 * GNU General Public License ("GPL") version 2 as published by the Free 243147Sxc151355 * Software Foundation. 253147Sxc151355 * 263147Sxc151355 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 273147Sxc151355 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 283147Sxc151355 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 293147Sxc151355 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 303147Sxc151355 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 313147Sxc151355 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 323147Sxc151355 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 333147Sxc151355 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 343147Sxc151355 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 353147Sxc151355 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 363147Sxc151355 */ 373147Sxc151355 383147Sxc151355 #ifndef _SYS_NET80211_CRYPTO_H 393147Sxc151355 #define _SYS_NET80211_CRYPTO_H 403147Sxc151355 41*6411Szf162725 #pragma ident "%Z%%M% %I% %E% SMI" 42*6411Szf162725 433147Sxc151355 #include <sys/types.h> 443147Sxc151355 #include <sys/mac.h> 453147Sxc151355 #include <sys/net80211_proto.h> 463147Sxc151355 473147Sxc151355 /* 483147Sxc151355 * 802.11 protocol crypto-related definitions. 493147Sxc151355 */ 503147Sxc151355 513147Sxc151355 #ifdef __cplusplus 523147Sxc151355 extern "C" { 533147Sxc151355 #endif 543147Sxc151355 55*6411Szf162725 #define IEEE80211_MAX_WPA_IE 40 /* IEEE802.11i */ 56*6411Szf162725 /* 57*6411Szf162725 * Max size of optional information elements. We artificially 58*6411Szf162725 * constrain this; it's limited only by the max frame size (and 59*6411Szf162725 * the max parameter size of the wireless extensions). 60*6411Szf162725 */ 61*6411Szf162725 #define IEEE80211_MAX_OPT_IE 256 62*6411Szf162725 63*6411Szf162725 #define IEEE80211_MLME_ASSOC 1 /* associate station */ 64*6411Szf162725 #define IEEE80211_MLME_DISASSOC 2 /* disassociate station */ 65*6411Szf162725 #define IEEE80211_MLME_DEAUTH 3 /* deauthenticate station */ 66*6411Szf162725 #define IEEE80211_MLME_AUTHORIZE 4 /* authorize station */ 67*6411Szf162725 #define IEEE80211_MLME_UNAUTHORIZE 5 /* unauthorize station */ 68*6411Szf162725 693147Sxc151355 /* 703147Sxc151355 * NB: these values are ordered carefully; there are lots of 713147Sxc151355 * of implications in any reordering. 723147Sxc151355 */ 733147Sxc151355 #define IEEE80211_CIPHER_WEP 0 743147Sxc151355 #define IEEE80211_CIPHER_TKIP 1 753147Sxc151355 #define IEEE80211_CIPHER_AES_OCB 2 763147Sxc151355 #define IEEE80211_CIPHER_AES_CCM 3 773147Sxc151355 #define IEEE80211_CIPHER_CKIP 4 783147Sxc151355 #define IEEE80211_CIPHER_NONE 5 /* pseudo value */ 793147Sxc151355 803147Sxc151355 #define IEEE80211_CIPHER_MAX (IEEE80211_CIPHER_NONE+1) 813147Sxc151355 823147Sxc151355 /* 833147Sxc151355 * Maxmium length of key in bytes 843147Sxc151355 * WEP key length present in the 802.11 standard is 40-bit. 853147Sxc151355 * Many implementations also support 104-bit WEP keys. 863147Sxc151355 * 802.11i standardize TKIP/CCMP use 128-bit key 873147Sxc151355 */ 883147Sxc151355 #define IEEE80211_KEYBUF_SIZE 16 893147Sxc151355 #define IEEE80211_MICBUF_SIZE (8+8) /* space for both tx+rx keys */ 903147Sxc151355 913147Sxc151355 /* Key Flags */ 923147Sxc151355 #define IEEE80211_KEY_XMIT 0x01 /* key used for xmit */ 933147Sxc151355 #define IEEE80211_KEY_RECV 0x02 /* key used for recv */ 943147Sxc151355 #define IEEE80211_KEY_GROUP /* key used for WPA group operation */ \ 953147Sxc151355 0x04 963147Sxc151355 #define IEEE80211_KEY_SWCRYPT 0x10 /* host-based encrypt/decrypt */ 973147Sxc151355 #define IEEE80211_KEY_SWMIC 0x20 /* host-based enmic/demic */ 983147Sxc151355 #define IEEE80211_KEY_COMMON /* common flags passed in by apps */ \ 993147Sxc151355 (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP) 1003147Sxc151355 101*6411Szf162725 #define IEEE80211_KEY_DEFAULT 0x80 /* default xmit key */ 102*6411Szf162725 1033147Sxc151355 /* WEP */ 1043147Sxc151355 #define IEEE80211_WEP_KEYLEN 5 /* 40bit */ 1053147Sxc151355 #define IEEE80211_WEP_IVLEN 3 /* 24bit */ 1063147Sxc151355 #define IEEE80211_WEP_KIDLEN 1 /* 1 octet */ 1073147Sxc151355 #define IEEE80211_WEP_CRCLEN 4 /* CRC-32 */ 1083147Sxc151355 #define IEEE80211_WEP_NKID 4 /* number of key ids */ 1093147Sxc151355 1104126Szf162725 /* 1114126Szf162725 * 802.11i defines an extended IV for use with non-WEP ciphers. 1124126Szf162725 * When the EXTIV bit is set in the key id byte an additional 1134126Szf162725 * 4 bytes immediately follow the IV for TKIP. For CCMP the 1144126Szf162725 * EXTIV bit is likewise set but the 8 bytes represent the 1154126Szf162725 * CCMP header rather than IV+extended-IV. 1164126Szf162725 */ 1174126Szf162725 #define IEEE80211_WEP_EXTIV 0x20 1184126Szf162725 #define IEEE80211_WEP_EXTIVLEN 4 /* extended IV length */ 1194126Szf162725 #define IEEE80211_WEP_MICLEN 8 /* trailing MIC */ 1204126Szf162725 1213147Sxc151355 #define IEEE80211_WEP_HDRLEN \ 1223147Sxc151355 (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 1233147Sxc151355 #define IEEE80211_WEP_MINLEN \ 1243147Sxc151355 (sizeof (struct ieee80211_frame) + \ 1253147Sxc151355 IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 1263147Sxc151355 1273147Sxc151355 /* Maximum number of keys */ 1283147Sxc151355 #define IEEE80211_KEY_MAX IEEE80211_WEP_NKID 1293147Sxc151355 1303147Sxc151355 struct ieee80211com; 1313147Sxc151355 struct ieee80211_node; 1323147Sxc151355 struct ieee80211_key; 1333147Sxc151355 typedef uint16_t ieee80211_keyix; /* h/w key index */ 1343147Sxc151355 1353147Sxc151355 #define IEEE80211_KEYIX_NONE ((ieee80211_keyix) -1) 1363147Sxc151355 1373147Sxc151355 /* 1383147Sxc151355 * Template for a supported cipher. Ciphers register with the 1393147Sxc151355 * crypto code. 1403147Sxc151355 * 1413147Sxc151355 * ic_attach - Initialize cipher. The return value is set to wk_private 1423147Sxc151355 * ic_detach - Destruct a cipher. 1433147Sxc151355 * ic_setkey - Validate key contents 1443147Sxc151355 * ic_encap - Encrypt the 802.11 MAC payload 1453147Sxc151355 * ic_decap - Decrypt the 802.11 MAC payload 1463147Sxc151355 * ic_enmic - Add MIC 1473147Sxc151355 * ic_demic - Check and remove MIC 1483147Sxc151355 */ 1493147Sxc151355 struct ieee80211_cipher { 1503147Sxc151355 const char *ic_name; /* printable name */ 1513147Sxc151355 uint32_t ic_cipher; /* IEEE80211_CIPHER_* */ 1523147Sxc151355 uint32_t ic_header; /* size of privacy header (bytes) */ 1533147Sxc151355 uint32_t ic_trailer; /* size of privacy trailer (bytes) */ 1543147Sxc151355 uint32_t ic_miclen; /* size of mic trailer (bytes) */ 1553147Sxc151355 void *(*ic_attach)(struct ieee80211com *, 1563147Sxc151355 struct ieee80211_key *); 1573147Sxc151355 void (*ic_detach)(struct ieee80211_key *); 1583147Sxc151355 int32_t (*ic_setkey)(struct ieee80211_key *); 1593147Sxc151355 int32_t (*ic_encap)(struct ieee80211_key *, mblk_t *, 1603147Sxc151355 uint8_t keyid); 1613147Sxc151355 int32_t (*ic_decap)(struct ieee80211_key *, mblk_t *, int); 1623147Sxc151355 int32_t (*ic_enmic)(struct ieee80211_key *, mblk_t *, int); 1633147Sxc151355 int32_t (*ic_demic)(struct ieee80211_key *, mblk_t *, int); 1643147Sxc151355 }; 1653147Sxc151355 extern const struct ieee80211_cipher ieee80211_cipher_none; 1663147Sxc151355 1673147Sxc151355 struct ieee80211_key { 1683147Sxc151355 uint8_t wk_keylen; /* key length in bytes */ 1693147Sxc151355 uint8_t wk_pad; 1703147Sxc151355 uint16_t wk_flags; 1713147Sxc151355 uint8_t wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE]; 1723147Sxc151355 ieee80211_keyix wk_keyix; /* h/w key index */ 1733147Sxc151355 ieee80211_keyix wk_rxkeyix; /* optional h/w rx key index */ 1743147Sxc151355 uint64_t wk_keyrsc; /* key receive sequence counter */ 1753147Sxc151355 uint64_t wk_keytsc; /* key transmit sequence counter */ 1763147Sxc151355 const struct ieee80211_cipher *wk_cipher; 1773147Sxc151355 void *wk_private; /* private cipher state */ 1783147Sxc151355 }; 1793147Sxc151355 #define wk_txmic wk_key+IEEE80211_KEYBUF_SIZE+0 1803147Sxc151355 #define wk_rxmic wk_key+IEEE80211_KEYBUF_SIZE+8 1813147Sxc151355 1823147Sxc151355 /* 1833147Sxc151355 * Crypto state kept in each ieee80211com. 1843147Sxc151355 */ 1853147Sxc151355 struct ieee80211_crypto_state { 1863147Sxc151355 struct ieee80211_key cs_nw_keys[IEEE80211_KEY_MAX]; 1873147Sxc151355 ieee80211_keyix cs_def_txkey; /* default/group tx key index */ 1883147Sxc151355 uint16_t cs_max_keyix; /* max h/w key index */ 1893147Sxc151355 1903147Sxc151355 int (*cs_key_alloc)(struct ieee80211com *, 1913147Sxc151355 const struct ieee80211_key *, 1923147Sxc151355 ieee80211_keyix *, ieee80211_keyix *); 1933147Sxc151355 int (*cs_key_delete)(struct ieee80211com *, 1943147Sxc151355 const struct ieee80211_key *); 1953147Sxc151355 int (*cs_key_set)(struct ieee80211com *, 1963147Sxc151355 const struct ieee80211_key *, 1973147Sxc151355 const uint8_t mac[IEEE80211_ADDR_LEN]); 1983147Sxc151355 void (*cs_key_update_begin)(struct ieee80211com *); 1993147Sxc151355 void (*cs_key_update_end)(struct ieee80211com *); 2003147Sxc151355 }; 2013147Sxc151355 2023147Sxc151355 /* 2033147Sxc151355 * Key update synchronization methods. 2043147Sxc151355 */ 2053147Sxc151355 #define KEY_UPDATE_BEGIN(ic) \ 2063147Sxc151355 (ic)->ic_crypto.cs_key_update_begin(ic) 2073147Sxc151355 #define KEY_UPDATE_END(ic) \ 2083147Sxc151355 (ic)->ic_crypto.cs_key_update_end(ic) 2093147Sxc151355 #define KEY_UNDEFINED(k) \ 2103147Sxc151355 ((k).wk_cipher == &ieee80211_cipher_none) 2113147Sxc151355 2123147Sxc151355 #define DEV_KEY_ALLOC(ic, k, kix, rkix) \ 2133147Sxc151355 (ic)->ic_crypto.cs_key_alloc(ic, k, kix, rkix) 2143147Sxc151355 #define DEV_KEY_DELETE(ic, k) \ 2153147Sxc151355 (ic)->ic_crypto.cs_key_delete(ic, k) 2163147Sxc151355 #define DEV_KEY_SET(ic, k, m) \ 2173147Sxc151355 (ic)->ic_crypto.cs_key_set(ic, k, m) 2183147Sxc151355 2193147Sxc151355 #define CIPHER_DETACH(k) \ 2203147Sxc151355 (k)->wk_cipher->ic_detach(k) 2213147Sxc151355 #define CIPHER_ATTACH(k) \ 2223147Sxc151355 (k)->wk_cipher->ic_attach(k) 2233147Sxc151355 2243147Sxc151355 #define ieee80211_crypto_demic(ic, k, m, force) \ 2253147Sxc151355 (((k)->wk_cipher->ic_miclen > 0) ? \ 2263147Sxc151355 (k)->wk_cipher->ic_demic(k, m, force) : \ 2273147Sxc151355 1) 2283147Sxc151355 2293147Sxc151355 #define ieee80211_crypto_enmic(ic, k, m, force) \ 2303147Sxc151355 ((k)->wk_cipher->ic_miclen > 0 ? \ 2313147Sxc151355 (k)->wk_cipher->ic_enmic(k, m, force) : \ 2323147Sxc151355 1) 2333147Sxc151355 2343147Sxc151355 void ieee80211_crypto_attach(struct ieee80211com *ic); 2353147Sxc151355 void ieee80211_crypto_detach(struct ieee80211com *ic); 2364499Shx147065 void ieee80211_crypto_register(struct ieee80211com *ic, 2374499Shx147065 const struct ieee80211_cipher *); 2384499Shx147065 void ieee80211_crypto_unregister(struct ieee80211com *ic, 2394499Shx147065 const struct ieee80211_cipher *); 2403147Sxc151355 void ieee80211_crypto_resetkey(struct ieee80211com *, struct ieee80211_key *, 2413147Sxc151355 ieee80211_keyix); 2423147Sxc151355 2433147Sxc151355 #ifdef __cplusplus 2443147Sxc151355 } 2453147Sxc151355 #endif 2463147Sxc151355 2473147Sxc151355 #endif /* _SYS_NET80211_CRYPTO_H */ 248