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