1*3147Sxc151355 /* 2*3147Sxc151355 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 3*3147Sxc151355 * Use is subject to license terms. 4*3147Sxc151355 */ 5*3147Sxc151355 6*3147Sxc151355 /* 7*3147Sxc151355 * Copyright (c) 2001 Atsushi Onoe 8*3147Sxc151355 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 9*3147Sxc151355 * All rights reserved. 10*3147Sxc151355 * 11*3147Sxc151355 * Redistribution and use in source and binary forms, with or without 12*3147Sxc151355 * modification, are permitted provided that the following conditions 13*3147Sxc151355 * are met: 14*3147Sxc151355 * 1. Redistributions of source code must retain the above copyright 15*3147Sxc151355 * notice, this list of conditions and the following disclaimer. 16*3147Sxc151355 * 2. Redistributions in binary form must reproduce the above copyright 17*3147Sxc151355 * notice, this list of conditions and the following disclaimer in the 18*3147Sxc151355 * documentation and/or other materials provided with the distribution. 19*3147Sxc151355 * 3. The name of the author may not be used to endorse or promote products 20*3147Sxc151355 * derived from this software without specific prior written permission. 21*3147Sxc151355 * 22*3147Sxc151355 * Alternatively, this software may be distributed under the terms of the 23*3147Sxc151355 * GNU General Public License ("GPL") version 2 as published by the Free 24*3147Sxc151355 * Software Foundation. 25*3147Sxc151355 * 26*3147Sxc151355 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27*3147Sxc151355 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28*3147Sxc151355 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29*3147Sxc151355 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30*3147Sxc151355 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31*3147Sxc151355 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32*3147Sxc151355 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33*3147Sxc151355 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34*3147Sxc151355 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35*3147Sxc151355 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36*3147Sxc151355 */ 37*3147Sxc151355 38*3147Sxc151355 #pragma ident "%Z%%M% %I% %E% SMI" 39*3147Sxc151355 40*3147Sxc151355 #ifndef _SYS_NET80211_CRYPTO_H 41*3147Sxc151355 #define _SYS_NET80211_CRYPTO_H 42*3147Sxc151355 43*3147Sxc151355 #include <sys/types.h> 44*3147Sxc151355 #include <sys/mac.h> 45*3147Sxc151355 #include <sys/net80211_proto.h> 46*3147Sxc151355 47*3147Sxc151355 /* 48*3147Sxc151355 * 802.11 protocol crypto-related definitions. 49*3147Sxc151355 */ 50*3147Sxc151355 51*3147Sxc151355 #ifdef __cplusplus 52*3147Sxc151355 extern "C" { 53*3147Sxc151355 #endif 54*3147Sxc151355 55*3147Sxc151355 /* 56*3147Sxc151355 * NB: these values are ordered carefully; there are lots of 57*3147Sxc151355 * of implications in any reordering. 58*3147Sxc151355 */ 59*3147Sxc151355 #define IEEE80211_CIPHER_WEP 0 60*3147Sxc151355 #define IEEE80211_CIPHER_TKIP 1 61*3147Sxc151355 #define IEEE80211_CIPHER_AES_OCB 2 62*3147Sxc151355 #define IEEE80211_CIPHER_AES_CCM 3 63*3147Sxc151355 #define IEEE80211_CIPHER_CKIP 4 64*3147Sxc151355 #define IEEE80211_CIPHER_NONE 5 /* pseudo value */ 65*3147Sxc151355 66*3147Sxc151355 #define IEEE80211_CIPHER_MAX (IEEE80211_CIPHER_NONE+1) 67*3147Sxc151355 68*3147Sxc151355 /* 69*3147Sxc151355 * Maxmium length of key in bytes 70*3147Sxc151355 * WEP key length present in the 802.11 standard is 40-bit. 71*3147Sxc151355 * Many implementations also support 104-bit WEP keys. 72*3147Sxc151355 * 802.11i standardize TKIP/CCMP use 128-bit key 73*3147Sxc151355 */ 74*3147Sxc151355 #define IEEE80211_KEYBUF_SIZE 16 75*3147Sxc151355 #define IEEE80211_MICBUF_SIZE (8+8) /* space for both tx+rx keys */ 76*3147Sxc151355 77*3147Sxc151355 /* Key Flags */ 78*3147Sxc151355 #define IEEE80211_KEY_XMIT 0x01 /* key used for xmit */ 79*3147Sxc151355 #define IEEE80211_KEY_RECV 0x02 /* key used for recv */ 80*3147Sxc151355 #define IEEE80211_KEY_GROUP /* key used for WPA group operation */ \ 81*3147Sxc151355 0x04 82*3147Sxc151355 #define IEEE80211_KEY_SWCRYPT 0x10 /* host-based encrypt/decrypt */ 83*3147Sxc151355 #define IEEE80211_KEY_SWMIC 0x20 /* host-based enmic/demic */ 84*3147Sxc151355 #define IEEE80211_KEY_COMMON /* common flags passed in by apps */ \ 85*3147Sxc151355 (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP) 86*3147Sxc151355 87*3147Sxc151355 /* WEP */ 88*3147Sxc151355 #define IEEE80211_WEP_KEYLEN 5 /* 40bit */ 89*3147Sxc151355 #define IEEE80211_WEP_IVLEN 3 /* 24bit */ 90*3147Sxc151355 #define IEEE80211_WEP_KIDLEN 1 /* 1 octet */ 91*3147Sxc151355 #define IEEE80211_WEP_CRCLEN 4 /* CRC-32 */ 92*3147Sxc151355 #define IEEE80211_WEP_NKID 4 /* number of key ids */ 93*3147Sxc151355 94*3147Sxc151355 #define IEEE80211_WEP_HDRLEN \ 95*3147Sxc151355 (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 96*3147Sxc151355 #define IEEE80211_WEP_MINLEN \ 97*3147Sxc151355 (sizeof (struct ieee80211_frame) + \ 98*3147Sxc151355 IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 99*3147Sxc151355 100*3147Sxc151355 /* Maximum number of keys */ 101*3147Sxc151355 #define IEEE80211_KEY_MAX IEEE80211_WEP_NKID 102*3147Sxc151355 103*3147Sxc151355 struct ieee80211com; 104*3147Sxc151355 struct ieee80211_node; 105*3147Sxc151355 struct ieee80211_key; 106*3147Sxc151355 typedef uint16_t ieee80211_keyix; /* h/w key index */ 107*3147Sxc151355 108*3147Sxc151355 #define IEEE80211_KEYIX_NONE ((ieee80211_keyix) -1) 109*3147Sxc151355 110*3147Sxc151355 /* 111*3147Sxc151355 * Template for a supported cipher. Ciphers register with the 112*3147Sxc151355 * crypto code. 113*3147Sxc151355 * 114*3147Sxc151355 * ic_attach - Initialize cipher. The return value is set to wk_private 115*3147Sxc151355 * ic_detach - Destruct a cipher. 116*3147Sxc151355 * ic_setkey - Validate key contents 117*3147Sxc151355 * ic_encap - Encrypt the 802.11 MAC payload 118*3147Sxc151355 * ic_decap - Decrypt the 802.11 MAC payload 119*3147Sxc151355 * ic_enmic - Add MIC 120*3147Sxc151355 * ic_demic - Check and remove MIC 121*3147Sxc151355 */ 122*3147Sxc151355 struct ieee80211_cipher { 123*3147Sxc151355 const char *ic_name; /* printable name */ 124*3147Sxc151355 uint32_t ic_cipher; /* IEEE80211_CIPHER_* */ 125*3147Sxc151355 uint32_t ic_header; /* size of privacy header (bytes) */ 126*3147Sxc151355 uint32_t ic_trailer; /* size of privacy trailer (bytes) */ 127*3147Sxc151355 uint32_t ic_miclen; /* size of mic trailer (bytes) */ 128*3147Sxc151355 void *(*ic_attach)(struct ieee80211com *, 129*3147Sxc151355 struct ieee80211_key *); 130*3147Sxc151355 void (*ic_detach)(struct ieee80211_key *); 131*3147Sxc151355 int32_t (*ic_setkey)(struct ieee80211_key *); 132*3147Sxc151355 int32_t (*ic_encap)(struct ieee80211_key *, mblk_t *, 133*3147Sxc151355 uint8_t keyid); 134*3147Sxc151355 int32_t (*ic_decap)(struct ieee80211_key *, mblk_t *, int); 135*3147Sxc151355 int32_t (*ic_enmic)(struct ieee80211_key *, mblk_t *, int); 136*3147Sxc151355 int32_t (*ic_demic)(struct ieee80211_key *, mblk_t *, int); 137*3147Sxc151355 }; 138*3147Sxc151355 extern const struct ieee80211_cipher ieee80211_cipher_none; 139*3147Sxc151355 140*3147Sxc151355 struct ieee80211_key { 141*3147Sxc151355 uint8_t wk_keylen; /* key length in bytes */ 142*3147Sxc151355 uint8_t wk_pad; 143*3147Sxc151355 uint16_t wk_flags; 144*3147Sxc151355 uint8_t wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE]; 145*3147Sxc151355 ieee80211_keyix wk_keyix; /* h/w key index */ 146*3147Sxc151355 ieee80211_keyix wk_rxkeyix; /* optional h/w rx key index */ 147*3147Sxc151355 uint64_t wk_keyrsc; /* key receive sequence counter */ 148*3147Sxc151355 uint64_t wk_keytsc; /* key transmit sequence counter */ 149*3147Sxc151355 const struct ieee80211_cipher *wk_cipher; 150*3147Sxc151355 void *wk_private; /* private cipher state */ 151*3147Sxc151355 }; 152*3147Sxc151355 #define wk_txmic wk_key+IEEE80211_KEYBUF_SIZE+0 153*3147Sxc151355 #define wk_rxmic wk_key+IEEE80211_KEYBUF_SIZE+8 154*3147Sxc151355 155*3147Sxc151355 /* 156*3147Sxc151355 * Crypto state kept in each ieee80211com. 157*3147Sxc151355 */ 158*3147Sxc151355 struct ieee80211_crypto_state { 159*3147Sxc151355 struct ieee80211_key cs_nw_keys[IEEE80211_KEY_MAX]; 160*3147Sxc151355 ieee80211_keyix cs_def_txkey; /* default/group tx key index */ 161*3147Sxc151355 uint16_t cs_max_keyix; /* max h/w key index */ 162*3147Sxc151355 163*3147Sxc151355 int (*cs_key_alloc)(struct ieee80211com *, 164*3147Sxc151355 const struct ieee80211_key *, 165*3147Sxc151355 ieee80211_keyix *, ieee80211_keyix *); 166*3147Sxc151355 int (*cs_key_delete)(struct ieee80211com *, 167*3147Sxc151355 const struct ieee80211_key *); 168*3147Sxc151355 int (*cs_key_set)(struct ieee80211com *, 169*3147Sxc151355 const struct ieee80211_key *, 170*3147Sxc151355 const uint8_t mac[IEEE80211_ADDR_LEN]); 171*3147Sxc151355 void (*cs_key_update_begin)(struct ieee80211com *); 172*3147Sxc151355 void (*cs_key_update_end)(struct ieee80211com *); 173*3147Sxc151355 }; 174*3147Sxc151355 175*3147Sxc151355 /* 176*3147Sxc151355 * Key update synchronization methods. 177*3147Sxc151355 */ 178*3147Sxc151355 #define KEY_UPDATE_BEGIN(ic) \ 179*3147Sxc151355 (ic)->ic_crypto.cs_key_update_begin(ic) 180*3147Sxc151355 #define KEY_UPDATE_END(ic) \ 181*3147Sxc151355 (ic)->ic_crypto.cs_key_update_end(ic) 182*3147Sxc151355 #define KEY_UNDEFINED(k) \ 183*3147Sxc151355 ((k).wk_cipher == &ieee80211_cipher_none) 184*3147Sxc151355 185*3147Sxc151355 #define DEV_KEY_ALLOC(ic, k, kix, rkix) \ 186*3147Sxc151355 (ic)->ic_crypto.cs_key_alloc(ic, k, kix, rkix) 187*3147Sxc151355 #define DEV_KEY_DELETE(ic, k) \ 188*3147Sxc151355 (ic)->ic_crypto.cs_key_delete(ic, k) 189*3147Sxc151355 #define DEV_KEY_SET(ic, k, m) \ 190*3147Sxc151355 (ic)->ic_crypto.cs_key_set(ic, k, m) 191*3147Sxc151355 192*3147Sxc151355 #define CIPHER_DETACH(k) \ 193*3147Sxc151355 (k)->wk_cipher->ic_detach(k) 194*3147Sxc151355 #define CIPHER_ATTACH(k) \ 195*3147Sxc151355 (k)->wk_cipher->ic_attach(k) 196*3147Sxc151355 197*3147Sxc151355 #define ieee80211_crypto_demic(ic, k, m, force) \ 198*3147Sxc151355 (((k)->wk_cipher->ic_miclen > 0) ? \ 199*3147Sxc151355 (k)->wk_cipher->ic_demic(k, m, force) : \ 200*3147Sxc151355 1) 201*3147Sxc151355 202*3147Sxc151355 #define ieee80211_crypto_enmic(ic, k, m, force) \ 203*3147Sxc151355 ((k)->wk_cipher->ic_miclen > 0 ? \ 204*3147Sxc151355 (k)->wk_cipher->ic_enmic(k, m, force) : \ 205*3147Sxc151355 1) 206*3147Sxc151355 207*3147Sxc151355 void ieee80211_crypto_attach(struct ieee80211com *ic); 208*3147Sxc151355 void ieee80211_crypto_detach(struct ieee80211com *ic); 209*3147Sxc151355 void ieee80211_crypto_register(const struct ieee80211_cipher *); 210*3147Sxc151355 void ieee80211_crypto_unregister(const struct ieee80211_cipher *); 211*3147Sxc151355 void ieee80211_crypto_resetkey(struct ieee80211com *, struct ieee80211_key *, 212*3147Sxc151355 ieee80211_keyix); 213*3147Sxc151355 214*3147Sxc151355 #ifdef __cplusplus 215*3147Sxc151355 } 216*3147Sxc151355 #endif 217*3147Sxc151355 218*3147Sxc151355 #endif /* _SYS_NET80211_CRYPTO_H */ 219