13147Sxc151355 /* 24126Szf162725 * Copyright 2007 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 #pragma ident "%Z%%M% %I% %E% SMI" 393147Sxc151355 403147Sxc151355 #ifndef _SYS_NET80211_CRYPTO_H 413147Sxc151355 #define _SYS_NET80211_CRYPTO_H 423147Sxc151355 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 553147Sxc151355 /* 563147Sxc151355 * NB: these values are ordered carefully; there are lots of 573147Sxc151355 * of implications in any reordering. 583147Sxc151355 */ 593147Sxc151355 #define IEEE80211_CIPHER_WEP 0 603147Sxc151355 #define IEEE80211_CIPHER_TKIP 1 613147Sxc151355 #define IEEE80211_CIPHER_AES_OCB 2 623147Sxc151355 #define IEEE80211_CIPHER_AES_CCM 3 633147Sxc151355 #define IEEE80211_CIPHER_CKIP 4 643147Sxc151355 #define IEEE80211_CIPHER_NONE 5 /* pseudo value */ 653147Sxc151355 663147Sxc151355 #define IEEE80211_CIPHER_MAX (IEEE80211_CIPHER_NONE+1) 673147Sxc151355 683147Sxc151355 /* 693147Sxc151355 * Maxmium length of key in bytes 703147Sxc151355 * WEP key length present in the 802.11 standard is 40-bit. 713147Sxc151355 * Many implementations also support 104-bit WEP keys. 723147Sxc151355 * 802.11i standardize TKIP/CCMP use 128-bit key 733147Sxc151355 */ 743147Sxc151355 #define IEEE80211_KEYBUF_SIZE 16 753147Sxc151355 #define IEEE80211_MICBUF_SIZE (8+8) /* space for both tx+rx keys */ 763147Sxc151355 773147Sxc151355 /* Key Flags */ 783147Sxc151355 #define IEEE80211_KEY_XMIT 0x01 /* key used for xmit */ 793147Sxc151355 #define IEEE80211_KEY_RECV 0x02 /* key used for recv */ 803147Sxc151355 #define IEEE80211_KEY_GROUP /* key used for WPA group operation */ \ 813147Sxc151355 0x04 823147Sxc151355 #define IEEE80211_KEY_SWCRYPT 0x10 /* host-based encrypt/decrypt */ 833147Sxc151355 #define IEEE80211_KEY_SWMIC 0x20 /* host-based enmic/demic */ 843147Sxc151355 #define IEEE80211_KEY_COMMON /* common flags passed in by apps */ \ 853147Sxc151355 (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP) 863147Sxc151355 873147Sxc151355 /* WEP */ 883147Sxc151355 #define IEEE80211_WEP_KEYLEN 5 /* 40bit */ 893147Sxc151355 #define IEEE80211_WEP_IVLEN 3 /* 24bit */ 903147Sxc151355 #define IEEE80211_WEP_KIDLEN 1 /* 1 octet */ 913147Sxc151355 #define IEEE80211_WEP_CRCLEN 4 /* CRC-32 */ 923147Sxc151355 #define IEEE80211_WEP_NKID 4 /* number of key ids */ 933147Sxc151355 944126Szf162725 /* 954126Szf162725 * 802.11i defines an extended IV for use with non-WEP ciphers. 964126Szf162725 * When the EXTIV bit is set in the key id byte an additional 974126Szf162725 * 4 bytes immediately follow the IV for TKIP. For CCMP the 984126Szf162725 * EXTIV bit is likewise set but the 8 bytes represent the 994126Szf162725 * CCMP header rather than IV+extended-IV. 1004126Szf162725 */ 1014126Szf162725 #define IEEE80211_WEP_EXTIV 0x20 1024126Szf162725 #define IEEE80211_WEP_EXTIVLEN 4 /* extended IV length */ 1034126Szf162725 #define IEEE80211_WEP_MICLEN 8 /* trailing MIC */ 1044126Szf162725 1053147Sxc151355 #define IEEE80211_WEP_HDRLEN \ 1063147Sxc151355 (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 1073147Sxc151355 #define IEEE80211_WEP_MINLEN \ 1083147Sxc151355 (sizeof (struct ieee80211_frame) + \ 1093147Sxc151355 IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 1103147Sxc151355 1113147Sxc151355 /* Maximum number of keys */ 1123147Sxc151355 #define IEEE80211_KEY_MAX IEEE80211_WEP_NKID 1133147Sxc151355 1143147Sxc151355 struct ieee80211com; 1153147Sxc151355 struct ieee80211_node; 1163147Sxc151355 struct ieee80211_key; 1173147Sxc151355 typedef uint16_t ieee80211_keyix; /* h/w key index */ 1183147Sxc151355 1193147Sxc151355 #define IEEE80211_KEYIX_NONE ((ieee80211_keyix) -1) 1203147Sxc151355 1213147Sxc151355 /* 1223147Sxc151355 * Template for a supported cipher. Ciphers register with the 1233147Sxc151355 * crypto code. 1243147Sxc151355 * 1253147Sxc151355 * ic_attach - Initialize cipher. The return value is set to wk_private 1263147Sxc151355 * ic_detach - Destruct a cipher. 1273147Sxc151355 * ic_setkey - Validate key contents 1283147Sxc151355 * ic_encap - Encrypt the 802.11 MAC payload 1293147Sxc151355 * ic_decap - Decrypt the 802.11 MAC payload 1303147Sxc151355 * ic_enmic - Add MIC 1313147Sxc151355 * ic_demic - Check and remove MIC 1323147Sxc151355 */ 1333147Sxc151355 struct ieee80211_cipher { 1343147Sxc151355 const char *ic_name; /* printable name */ 1353147Sxc151355 uint32_t ic_cipher; /* IEEE80211_CIPHER_* */ 1363147Sxc151355 uint32_t ic_header; /* size of privacy header (bytes) */ 1373147Sxc151355 uint32_t ic_trailer; /* size of privacy trailer (bytes) */ 1383147Sxc151355 uint32_t ic_miclen; /* size of mic trailer (bytes) */ 1393147Sxc151355 void *(*ic_attach)(struct ieee80211com *, 1403147Sxc151355 struct ieee80211_key *); 1413147Sxc151355 void (*ic_detach)(struct ieee80211_key *); 1423147Sxc151355 int32_t (*ic_setkey)(struct ieee80211_key *); 1433147Sxc151355 int32_t (*ic_encap)(struct ieee80211_key *, mblk_t *, 1443147Sxc151355 uint8_t keyid); 1453147Sxc151355 int32_t (*ic_decap)(struct ieee80211_key *, mblk_t *, int); 1463147Sxc151355 int32_t (*ic_enmic)(struct ieee80211_key *, mblk_t *, int); 1473147Sxc151355 int32_t (*ic_demic)(struct ieee80211_key *, mblk_t *, int); 1483147Sxc151355 }; 1493147Sxc151355 extern const struct ieee80211_cipher ieee80211_cipher_none; 1503147Sxc151355 1513147Sxc151355 struct ieee80211_key { 1523147Sxc151355 uint8_t wk_keylen; /* key length in bytes */ 1533147Sxc151355 uint8_t wk_pad; 1543147Sxc151355 uint16_t wk_flags; 1553147Sxc151355 uint8_t wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE]; 1563147Sxc151355 ieee80211_keyix wk_keyix; /* h/w key index */ 1573147Sxc151355 ieee80211_keyix wk_rxkeyix; /* optional h/w rx key index */ 1583147Sxc151355 uint64_t wk_keyrsc; /* key receive sequence counter */ 1593147Sxc151355 uint64_t wk_keytsc; /* key transmit sequence counter */ 1603147Sxc151355 const struct ieee80211_cipher *wk_cipher; 1613147Sxc151355 void *wk_private; /* private cipher state */ 1623147Sxc151355 }; 1633147Sxc151355 #define wk_txmic wk_key+IEEE80211_KEYBUF_SIZE+0 1643147Sxc151355 #define wk_rxmic wk_key+IEEE80211_KEYBUF_SIZE+8 1653147Sxc151355 1663147Sxc151355 /* 1673147Sxc151355 * Crypto state kept in each ieee80211com. 1683147Sxc151355 */ 1693147Sxc151355 struct ieee80211_crypto_state { 1703147Sxc151355 struct ieee80211_key cs_nw_keys[IEEE80211_KEY_MAX]; 1713147Sxc151355 ieee80211_keyix cs_def_txkey; /* default/group tx key index */ 1723147Sxc151355 uint16_t cs_max_keyix; /* max h/w key index */ 1733147Sxc151355 1743147Sxc151355 int (*cs_key_alloc)(struct ieee80211com *, 1753147Sxc151355 const struct ieee80211_key *, 1763147Sxc151355 ieee80211_keyix *, ieee80211_keyix *); 1773147Sxc151355 int (*cs_key_delete)(struct ieee80211com *, 1783147Sxc151355 const struct ieee80211_key *); 1793147Sxc151355 int (*cs_key_set)(struct ieee80211com *, 1803147Sxc151355 const struct ieee80211_key *, 1813147Sxc151355 const uint8_t mac[IEEE80211_ADDR_LEN]); 1823147Sxc151355 void (*cs_key_update_begin)(struct ieee80211com *); 1833147Sxc151355 void (*cs_key_update_end)(struct ieee80211com *); 1843147Sxc151355 }; 1853147Sxc151355 1863147Sxc151355 /* 1873147Sxc151355 * Key update synchronization methods. 1883147Sxc151355 */ 1893147Sxc151355 #define KEY_UPDATE_BEGIN(ic) \ 1903147Sxc151355 (ic)->ic_crypto.cs_key_update_begin(ic) 1913147Sxc151355 #define KEY_UPDATE_END(ic) \ 1923147Sxc151355 (ic)->ic_crypto.cs_key_update_end(ic) 1933147Sxc151355 #define KEY_UNDEFINED(k) \ 1943147Sxc151355 ((k).wk_cipher == &ieee80211_cipher_none) 1953147Sxc151355 1963147Sxc151355 #define DEV_KEY_ALLOC(ic, k, kix, rkix) \ 1973147Sxc151355 (ic)->ic_crypto.cs_key_alloc(ic, k, kix, rkix) 1983147Sxc151355 #define DEV_KEY_DELETE(ic, k) \ 1993147Sxc151355 (ic)->ic_crypto.cs_key_delete(ic, k) 2003147Sxc151355 #define DEV_KEY_SET(ic, k, m) \ 2013147Sxc151355 (ic)->ic_crypto.cs_key_set(ic, k, m) 2023147Sxc151355 2033147Sxc151355 #define CIPHER_DETACH(k) \ 2043147Sxc151355 (k)->wk_cipher->ic_detach(k) 2053147Sxc151355 #define CIPHER_ATTACH(k) \ 2063147Sxc151355 (k)->wk_cipher->ic_attach(k) 2073147Sxc151355 2083147Sxc151355 #define ieee80211_crypto_demic(ic, k, m, force) \ 2093147Sxc151355 (((k)->wk_cipher->ic_miclen > 0) ? \ 2103147Sxc151355 (k)->wk_cipher->ic_demic(k, m, force) : \ 2113147Sxc151355 1) 2123147Sxc151355 2133147Sxc151355 #define ieee80211_crypto_enmic(ic, k, m, force) \ 2143147Sxc151355 ((k)->wk_cipher->ic_miclen > 0 ? \ 2153147Sxc151355 (k)->wk_cipher->ic_enmic(k, m, force) : \ 2163147Sxc151355 1) 2173147Sxc151355 2183147Sxc151355 void ieee80211_crypto_attach(struct ieee80211com *ic); 2193147Sxc151355 void ieee80211_crypto_detach(struct ieee80211com *ic); 220*4499Shx147065 void ieee80211_crypto_register(struct ieee80211com *ic, 221*4499Shx147065 const struct ieee80211_cipher *); 222*4499Shx147065 void ieee80211_crypto_unregister(struct ieee80211com *ic, 223*4499Shx147065 const struct ieee80211_cipher *); 2243147Sxc151355 void ieee80211_crypto_resetkey(struct ieee80211com *, struct ieee80211_key *, 2253147Sxc151355 ieee80211_keyix); 2263147Sxc151355 2273147Sxc151355 #ifdef __cplusplus 2283147Sxc151355 } 2293147Sxc151355 #endif 2303147Sxc151355 2313147Sxc151355 #endif /* _SYS_NET80211_CRYPTO_H */ 232