13147Sxc151355 /* 2*9107Sjames.d.carlson@sun.com * Copyright 2009 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 413147Sxc151355 #include <sys/types.h> 42*9107Sjames.d.carlson@sun.com #ifdef _KERNEL 43*9107Sjames.d.carlson@sun.com #include <sys/stream.h> 443147Sxc151355 #include <sys/mac.h> 45*9107Sjames.d.carlson@sun.com #endif 463147Sxc151355 #include <sys/net80211_proto.h> 473147Sxc151355 483147Sxc151355 /* 493147Sxc151355 * 802.11 protocol crypto-related definitions. 503147Sxc151355 */ 513147Sxc151355 523147Sxc151355 #ifdef __cplusplus 533147Sxc151355 extern "C" { 543147Sxc151355 #endif 553147Sxc151355 566411Szf162725 #define IEEE80211_MAX_WPA_IE 40 /* IEEE802.11i */ 576411Szf162725 /* 586411Szf162725 * Max size of optional information elements. We artificially 596411Szf162725 * constrain this; it's limited only by the max frame size (and 606411Szf162725 * the max parameter size of the wireless extensions). 616411Szf162725 */ 626411Szf162725 #define IEEE80211_MAX_OPT_IE 256 636411Szf162725 646411Szf162725 #define IEEE80211_MLME_ASSOC 1 /* associate station */ 656411Szf162725 #define IEEE80211_MLME_DISASSOC 2 /* disassociate station */ 666411Szf162725 #define IEEE80211_MLME_DEAUTH 3 /* deauthenticate station */ 676411Szf162725 #define IEEE80211_MLME_AUTHORIZE 4 /* authorize station */ 686411Szf162725 #define IEEE80211_MLME_UNAUTHORIZE 5 /* unauthorize station */ 696411Szf162725 703147Sxc151355 /* 713147Sxc151355 * NB: these values are ordered carefully; there are lots of 723147Sxc151355 * of implications in any reordering. 733147Sxc151355 */ 743147Sxc151355 #define IEEE80211_CIPHER_WEP 0 753147Sxc151355 #define IEEE80211_CIPHER_TKIP 1 763147Sxc151355 #define IEEE80211_CIPHER_AES_OCB 2 773147Sxc151355 #define IEEE80211_CIPHER_AES_CCM 3 783147Sxc151355 #define IEEE80211_CIPHER_CKIP 4 793147Sxc151355 #define IEEE80211_CIPHER_NONE 5 /* pseudo value */ 803147Sxc151355 813147Sxc151355 #define IEEE80211_CIPHER_MAX (IEEE80211_CIPHER_NONE+1) 823147Sxc151355 833147Sxc151355 /* 843147Sxc151355 * Maxmium length of key in bytes 853147Sxc151355 * WEP key length present in the 802.11 standard is 40-bit. 863147Sxc151355 * Many implementations also support 104-bit WEP keys. 873147Sxc151355 * 802.11i standardize TKIP/CCMP use 128-bit key 883147Sxc151355 */ 893147Sxc151355 #define IEEE80211_KEYBUF_SIZE 16 903147Sxc151355 #define IEEE80211_MICBUF_SIZE (8+8) /* space for both tx+rx keys */ 913147Sxc151355 923147Sxc151355 /* Key Flags */ 933147Sxc151355 #define IEEE80211_KEY_XMIT 0x01 /* key used for xmit */ 943147Sxc151355 #define IEEE80211_KEY_RECV 0x02 /* key used for recv */ 953147Sxc151355 #define IEEE80211_KEY_GROUP /* key used for WPA group operation */ \ 963147Sxc151355 0x04 973147Sxc151355 #define IEEE80211_KEY_SWCRYPT 0x10 /* host-based encrypt/decrypt */ 983147Sxc151355 #define IEEE80211_KEY_SWMIC 0x20 /* host-based enmic/demic */ 993147Sxc151355 #define IEEE80211_KEY_COMMON /* common flags passed in by apps */ \ 1003147Sxc151355 (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP) 1013147Sxc151355 1026411Szf162725 #define IEEE80211_KEY_DEFAULT 0x80 /* default xmit key */ 1036411Szf162725 1043147Sxc151355 /* WEP */ 1053147Sxc151355 #define IEEE80211_WEP_KEYLEN 5 /* 40bit */ 1063147Sxc151355 #define IEEE80211_WEP_IVLEN 3 /* 24bit */ 1073147Sxc151355 #define IEEE80211_WEP_KIDLEN 1 /* 1 octet */ 1083147Sxc151355 #define IEEE80211_WEP_CRCLEN 4 /* CRC-32 */ 1093147Sxc151355 #define IEEE80211_WEP_NKID 4 /* number of key ids */ 1103147Sxc151355 1114126Szf162725 /* 1124126Szf162725 * 802.11i defines an extended IV for use with non-WEP ciphers. 1134126Szf162725 * When the EXTIV bit is set in the key id byte an additional 1144126Szf162725 * 4 bytes immediately follow the IV for TKIP. For CCMP the 1154126Szf162725 * EXTIV bit is likewise set but the 8 bytes represent the 1164126Szf162725 * CCMP header rather than IV+extended-IV. 1174126Szf162725 */ 1184126Szf162725 #define IEEE80211_WEP_EXTIV 0x20 1194126Szf162725 #define IEEE80211_WEP_EXTIVLEN 4 /* extended IV length */ 1204126Szf162725 #define IEEE80211_WEP_MICLEN 8 /* trailing MIC */ 1214126Szf162725 1223147Sxc151355 #define IEEE80211_WEP_HDRLEN \ 1233147Sxc151355 (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 1243147Sxc151355 #define IEEE80211_WEP_MINLEN \ 1253147Sxc151355 (sizeof (struct ieee80211_frame) + \ 1263147Sxc151355 IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 1273147Sxc151355 1283147Sxc151355 /* Maximum number of keys */ 1293147Sxc151355 #define IEEE80211_KEY_MAX IEEE80211_WEP_NKID 1303147Sxc151355 1313147Sxc151355 typedef uint16_t ieee80211_keyix; /* h/w key index */ 1323147Sxc151355 1333147Sxc151355 #define IEEE80211_KEYIX_NONE ((ieee80211_keyix) -1) 1343147Sxc151355 135*9107Sjames.d.carlson@sun.com #ifdef _KERNEL 136*9107Sjames.d.carlson@sun.com 137*9107Sjames.d.carlson@sun.com struct ieee80211com; 138*9107Sjames.d.carlson@sun.com struct ieee80211_key; 139*9107Sjames.d.carlson@sun.com 1403147Sxc151355 /* 1413147Sxc151355 * Template for a supported cipher. Ciphers register with the 1423147Sxc151355 * crypto code. 1433147Sxc151355 * 1443147Sxc151355 * ic_attach - Initialize cipher. The return value is set to wk_private 1453147Sxc151355 * ic_detach - Destruct a cipher. 1463147Sxc151355 * ic_setkey - Validate key contents 1473147Sxc151355 * ic_encap - Encrypt the 802.11 MAC payload 1483147Sxc151355 * ic_decap - Decrypt the 802.11 MAC payload 1493147Sxc151355 * ic_enmic - Add MIC 1503147Sxc151355 * ic_demic - Check and remove MIC 1513147Sxc151355 */ 1523147Sxc151355 struct ieee80211_cipher { 1533147Sxc151355 const char *ic_name; /* printable name */ 1543147Sxc151355 uint32_t ic_cipher; /* IEEE80211_CIPHER_* */ 1553147Sxc151355 uint32_t ic_header; /* size of privacy header (bytes) */ 1563147Sxc151355 uint32_t ic_trailer; /* size of privacy trailer (bytes) */ 1573147Sxc151355 uint32_t ic_miclen; /* size of mic trailer (bytes) */ 1583147Sxc151355 void *(*ic_attach)(struct ieee80211com *, 1593147Sxc151355 struct ieee80211_key *); 1603147Sxc151355 void (*ic_detach)(struct ieee80211_key *); 1613147Sxc151355 int32_t (*ic_setkey)(struct ieee80211_key *); 1623147Sxc151355 int32_t (*ic_encap)(struct ieee80211_key *, mblk_t *, 1633147Sxc151355 uint8_t keyid); 1643147Sxc151355 int32_t (*ic_decap)(struct ieee80211_key *, mblk_t *, int); 1653147Sxc151355 int32_t (*ic_enmic)(struct ieee80211_key *, mblk_t *, int); 1663147Sxc151355 int32_t (*ic_demic)(struct ieee80211_key *, mblk_t *, int); 1673147Sxc151355 }; 1683147Sxc151355 extern const struct ieee80211_cipher ieee80211_cipher_none; 1693147Sxc151355 1703147Sxc151355 struct ieee80211_key { 1713147Sxc151355 uint8_t wk_keylen; /* key length in bytes */ 1723147Sxc151355 uint8_t wk_pad; 1733147Sxc151355 uint16_t wk_flags; 1743147Sxc151355 uint8_t wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE]; 1753147Sxc151355 ieee80211_keyix wk_keyix; /* h/w key index */ 1763147Sxc151355 ieee80211_keyix wk_rxkeyix; /* optional h/w rx key index */ 1773147Sxc151355 uint64_t wk_keyrsc; /* key receive sequence counter */ 1783147Sxc151355 uint64_t wk_keytsc; /* key transmit sequence counter */ 1793147Sxc151355 const struct ieee80211_cipher *wk_cipher; 1803147Sxc151355 void *wk_private; /* private cipher state */ 1813147Sxc151355 }; 1823147Sxc151355 #define wk_txmic wk_key+IEEE80211_KEYBUF_SIZE+0 1833147Sxc151355 #define wk_rxmic wk_key+IEEE80211_KEYBUF_SIZE+8 1843147Sxc151355 1853147Sxc151355 /* 1863147Sxc151355 * Crypto state kept in each ieee80211com. 1873147Sxc151355 */ 1883147Sxc151355 struct ieee80211_crypto_state { 1893147Sxc151355 struct ieee80211_key cs_nw_keys[IEEE80211_KEY_MAX]; 1903147Sxc151355 ieee80211_keyix cs_def_txkey; /* default/group tx key index */ 1913147Sxc151355 uint16_t cs_max_keyix; /* max h/w key index */ 1923147Sxc151355 1933147Sxc151355 int (*cs_key_alloc)(struct ieee80211com *, 1943147Sxc151355 const struct ieee80211_key *, 1953147Sxc151355 ieee80211_keyix *, ieee80211_keyix *); 1963147Sxc151355 int (*cs_key_delete)(struct ieee80211com *, 1973147Sxc151355 const struct ieee80211_key *); 1983147Sxc151355 int (*cs_key_set)(struct ieee80211com *, 1993147Sxc151355 const struct ieee80211_key *, 2003147Sxc151355 const uint8_t mac[IEEE80211_ADDR_LEN]); 2013147Sxc151355 void (*cs_key_update_begin)(struct ieee80211com *); 2023147Sxc151355 void (*cs_key_update_end)(struct ieee80211com *); 2033147Sxc151355 }; 2043147Sxc151355 2053147Sxc151355 /* 2063147Sxc151355 * Key update synchronization methods. 2073147Sxc151355 */ 2083147Sxc151355 #define KEY_UPDATE_BEGIN(ic) \ 2093147Sxc151355 (ic)->ic_crypto.cs_key_update_begin(ic) 2103147Sxc151355 #define KEY_UPDATE_END(ic) \ 2113147Sxc151355 (ic)->ic_crypto.cs_key_update_end(ic) 2123147Sxc151355 #define KEY_UNDEFINED(k) \ 2133147Sxc151355 ((k).wk_cipher == &ieee80211_cipher_none) 2143147Sxc151355 2153147Sxc151355 #define DEV_KEY_ALLOC(ic, k, kix, rkix) \ 2163147Sxc151355 (ic)->ic_crypto.cs_key_alloc(ic, k, kix, rkix) 2173147Sxc151355 #define DEV_KEY_DELETE(ic, k) \ 2183147Sxc151355 (ic)->ic_crypto.cs_key_delete(ic, k) 2193147Sxc151355 #define DEV_KEY_SET(ic, k, m) \ 2203147Sxc151355 (ic)->ic_crypto.cs_key_set(ic, k, m) 2213147Sxc151355 2223147Sxc151355 #define CIPHER_DETACH(k) \ 2233147Sxc151355 (k)->wk_cipher->ic_detach(k) 2243147Sxc151355 #define CIPHER_ATTACH(k) \ 2253147Sxc151355 (k)->wk_cipher->ic_attach(k) 2263147Sxc151355 2273147Sxc151355 #define ieee80211_crypto_demic(ic, k, m, force) \ 2283147Sxc151355 (((k)->wk_cipher->ic_miclen > 0) ? \ 2293147Sxc151355 (k)->wk_cipher->ic_demic(k, m, force) : \ 2303147Sxc151355 1) 2313147Sxc151355 2323147Sxc151355 #define ieee80211_crypto_enmic(ic, k, m, force) \ 2333147Sxc151355 ((k)->wk_cipher->ic_miclen > 0 ? \ 2343147Sxc151355 (k)->wk_cipher->ic_enmic(k, m, force) : \ 2353147Sxc151355 1) 2363147Sxc151355 2373147Sxc151355 void ieee80211_crypto_attach(struct ieee80211com *ic); 2383147Sxc151355 void ieee80211_crypto_detach(struct ieee80211com *ic); 2394499Shx147065 void ieee80211_crypto_register(struct ieee80211com *ic, 2404499Shx147065 const struct ieee80211_cipher *); 2414499Shx147065 void ieee80211_crypto_unregister(struct ieee80211com *ic, 2424499Shx147065 const struct ieee80211_cipher *); 2433147Sxc151355 void ieee80211_crypto_resetkey(struct ieee80211com *, struct ieee80211_key *, 2443147Sxc151355 ieee80211_keyix); 2453147Sxc151355 246*9107Sjames.d.carlson@sun.com #endif /* _KERNEL */ 247*9107Sjames.d.carlson@sun.com 2483147Sxc151355 #ifdef __cplusplus 2493147Sxc151355 } 2503147Sxc151355 #endif 2513147Sxc151355 2523147Sxc151355 #endif /* _SYS_NET80211_CRYPTO_H */ 253