xref: /titanic_41/usr/src/uts/common/sys/net80211_crypto.h (revision 82a2fc4751cef28c0bdc327d02012bf8796083b9)
10ba2cbe9Sxc151355 /*
2*82a2fc47SJames Carlson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
30ba2cbe9Sxc151355  * Use is subject to license terms.
40ba2cbe9Sxc151355  */
50ba2cbe9Sxc151355 
60ba2cbe9Sxc151355 /*
70ba2cbe9Sxc151355  * Copyright (c) 2001 Atsushi Onoe
80ba2cbe9Sxc151355  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
90ba2cbe9Sxc151355  * All rights reserved.
100ba2cbe9Sxc151355  *
110ba2cbe9Sxc151355  * Redistribution and use in source and binary forms, with or without
120ba2cbe9Sxc151355  * modification, are permitted provided that the following conditions
130ba2cbe9Sxc151355  * are met:
140ba2cbe9Sxc151355  * 1. Redistributions of source code must retain the above copyright
150ba2cbe9Sxc151355  *    notice, this list of conditions and the following disclaimer.
160ba2cbe9Sxc151355  * 2. Redistributions in binary form must reproduce the above copyright
170ba2cbe9Sxc151355  *    notice, this list of conditions and the following disclaimer in the
180ba2cbe9Sxc151355  *    documentation and/or other materials provided with the distribution.
190ba2cbe9Sxc151355  * 3. The name of the author may not be used to endorse or promote products
200ba2cbe9Sxc151355  *    derived from this software without specific prior written permission.
210ba2cbe9Sxc151355  *
220ba2cbe9Sxc151355  * Alternatively, this software may be distributed under the terms of the
230ba2cbe9Sxc151355  * GNU General Public License ("GPL") version 2 as published by the Free
240ba2cbe9Sxc151355  * Software Foundation.
250ba2cbe9Sxc151355  *
260ba2cbe9Sxc151355  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
270ba2cbe9Sxc151355  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
280ba2cbe9Sxc151355  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
290ba2cbe9Sxc151355  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
300ba2cbe9Sxc151355  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
310ba2cbe9Sxc151355  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
320ba2cbe9Sxc151355  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
330ba2cbe9Sxc151355  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
340ba2cbe9Sxc151355  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
350ba2cbe9Sxc151355  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
360ba2cbe9Sxc151355  */
370ba2cbe9Sxc151355 
380ba2cbe9Sxc151355 #ifndef _SYS_NET80211_CRYPTO_H
390ba2cbe9Sxc151355 #define	_SYS_NET80211_CRYPTO_H
400ba2cbe9Sxc151355 
410ba2cbe9Sxc151355 #include <sys/types.h>
42*82a2fc47SJames Carlson #ifdef _KERNEL
43*82a2fc47SJames Carlson #include <sys/stream.h>
440ba2cbe9Sxc151355 #include <sys/mac.h>
45*82a2fc47SJames Carlson #endif
460ba2cbe9Sxc151355 #include <sys/net80211_proto.h>
470ba2cbe9Sxc151355 
480ba2cbe9Sxc151355 /*
490ba2cbe9Sxc151355  * 802.11 protocol crypto-related definitions.
500ba2cbe9Sxc151355  */
510ba2cbe9Sxc151355 
520ba2cbe9Sxc151355 #ifdef	__cplusplus
530ba2cbe9Sxc151355 extern "C" {
540ba2cbe9Sxc151355 #endif
550ba2cbe9Sxc151355 
56fb91fd8aSzf162725 #define	IEEE80211_MAX_WPA_IE		40	/* IEEE802.11i */
57fb91fd8aSzf162725 /*
58fb91fd8aSzf162725  * Max size of optional information elements.  We artificially
59fb91fd8aSzf162725  * constrain this; it's limited only by the max frame size (and
60fb91fd8aSzf162725  * the max parameter size of the wireless extensions).
61fb91fd8aSzf162725  */
62fb91fd8aSzf162725 #define	IEEE80211_MAX_OPT_IE		256
63fb91fd8aSzf162725 
64fb91fd8aSzf162725 #define	IEEE80211_MLME_ASSOC		1	/* associate station */
65fb91fd8aSzf162725 #define	IEEE80211_MLME_DISASSOC		2	/* disassociate station */
66fb91fd8aSzf162725 #define	IEEE80211_MLME_DEAUTH		3	/* deauthenticate station */
67fb91fd8aSzf162725 #define	IEEE80211_MLME_AUTHORIZE	4	/* authorize station */
68fb91fd8aSzf162725 #define	IEEE80211_MLME_UNAUTHORIZE	5	/* unauthorize station */
69fb91fd8aSzf162725 
700ba2cbe9Sxc151355 /*
710ba2cbe9Sxc151355  * NB: these values are ordered carefully; there are lots of
720ba2cbe9Sxc151355  * of implications in any reordering.
730ba2cbe9Sxc151355  */
740ba2cbe9Sxc151355 #define	IEEE80211_CIPHER_WEP		0
750ba2cbe9Sxc151355 #define	IEEE80211_CIPHER_TKIP		1
760ba2cbe9Sxc151355 #define	IEEE80211_CIPHER_AES_OCB	2
770ba2cbe9Sxc151355 #define	IEEE80211_CIPHER_AES_CCM	3
780ba2cbe9Sxc151355 #define	IEEE80211_CIPHER_CKIP		4
790ba2cbe9Sxc151355 #define	IEEE80211_CIPHER_NONE		5	/* pseudo value */
800ba2cbe9Sxc151355 
810ba2cbe9Sxc151355 #define	IEEE80211_CIPHER_MAX		(IEEE80211_CIPHER_NONE+1)
820ba2cbe9Sxc151355 
830ba2cbe9Sxc151355 /*
840ba2cbe9Sxc151355  * Maxmium length of key in bytes
850ba2cbe9Sxc151355  * WEP key length present in the 802.11 standard is 40-bit.
860ba2cbe9Sxc151355  * Many implementations also support 104-bit WEP keys.
870ba2cbe9Sxc151355  * 802.11i standardize TKIP/CCMP use 128-bit key
880ba2cbe9Sxc151355  */
890ba2cbe9Sxc151355 #define	IEEE80211_KEYBUF_SIZE		16
900ba2cbe9Sxc151355 #define	IEEE80211_MICBUF_SIZE		(8+8)	/* space for both tx+rx keys */
910ba2cbe9Sxc151355 
920ba2cbe9Sxc151355 /* Key Flags */
930ba2cbe9Sxc151355 #define	IEEE80211_KEY_XMIT		0x01	/* key used for xmit */
940ba2cbe9Sxc151355 #define	IEEE80211_KEY_RECV		0x02	/* key used for recv */
950ba2cbe9Sxc151355 #define	IEEE80211_KEY_GROUP		/* key used for WPA group operation */ \
960ba2cbe9Sxc151355 					0x04
970ba2cbe9Sxc151355 #define	IEEE80211_KEY_SWCRYPT		0x10	/* host-based encrypt/decrypt */
980ba2cbe9Sxc151355 #define	IEEE80211_KEY_SWMIC		0x20	/* host-based enmic/demic */
990ba2cbe9Sxc151355 #define	IEEE80211_KEY_COMMON 		/* common flags passed in by apps */ \
1000ba2cbe9Sxc151355 	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP)
1010ba2cbe9Sxc151355 
102fb91fd8aSzf162725 #define	IEEE80211_KEY_DEFAULT		0x80	/* default xmit key */
103fb91fd8aSzf162725 
1040ba2cbe9Sxc151355 /* WEP */
1050ba2cbe9Sxc151355 #define	IEEE80211_WEP_KEYLEN		5	/* 40bit */
1060ba2cbe9Sxc151355 #define	IEEE80211_WEP_IVLEN		3	/* 24bit */
1070ba2cbe9Sxc151355 #define	IEEE80211_WEP_KIDLEN		1	/* 1 octet */
1080ba2cbe9Sxc151355 #define	IEEE80211_WEP_CRCLEN		4	/* CRC-32 */
1090ba2cbe9Sxc151355 #define	IEEE80211_WEP_NKID		4	/* number of key ids */
1100ba2cbe9Sxc151355 
111a399b765Szf162725 /*
112a399b765Szf162725  * 802.11i defines an extended IV for use with non-WEP ciphers.
113a399b765Szf162725  * When the EXTIV bit is set in the key id byte an additional
114a399b765Szf162725  * 4 bytes immediately follow the IV for TKIP.  For CCMP the
115a399b765Szf162725  * EXTIV bit is likewise set but the 8 bytes represent the
116a399b765Szf162725  * CCMP header rather than IV+extended-IV.
117a399b765Szf162725  */
118a399b765Szf162725 #define	IEEE80211_WEP_EXTIV		0x20
119a399b765Szf162725 #define	IEEE80211_WEP_EXTIVLEN		4	/* extended IV length */
120a399b765Szf162725 #define	IEEE80211_WEP_MICLEN		8	/* trailing MIC */
121a399b765Szf162725 
1220ba2cbe9Sxc151355 #define	IEEE80211_WEP_HDRLEN					\
1230ba2cbe9Sxc151355 	(IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
1240ba2cbe9Sxc151355 #define	IEEE80211_WEP_MINLEN					\
1250ba2cbe9Sxc151355 	(sizeof (struct ieee80211_frame) +			\
1260ba2cbe9Sxc151355 	IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
1270ba2cbe9Sxc151355 
1280ba2cbe9Sxc151355 /* Maximum number of keys */
1290ba2cbe9Sxc151355 #define	IEEE80211_KEY_MAX		IEEE80211_WEP_NKID
1300ba2cbe9Sxc151355 
1310ba2cbe9Sxc151355 typedef uint16_t	ieee80211_keyix;	/* h/w key index */
1320ba2cbe9Sxc151355 
1330ba2cbe9Sxc151355 #define	IEEE80211_KEYIX_NONE	((ieee80211_keyix) -1)
1340ba2cbe9Sxc151355 
135*82a2fc47SJames Carlson #ifdef _KERNEL
136*82a2fc47SJames Carlson 
137*82a2fc47SJames Carlson struct ieee80211com;
138*82a2fc47SJames Carlson struct ieee80211_key;
139*82a2fc47SJames Carlson 
1400ba2cbe9Sxc151355 /*
1410ba2cbe9Sxc151355  * Template for a supported cipher.  Ciphers register with the
1420ba2cbe9Sxc151355  * crypto code.
1430ba2cbe9Sxc151355  *
1440ba2cbe9Sxc151355  * ic_attach - Initialize cipher. The return value is set to wk_private
1450ba2cbe9Sxc151355  * ic_detach - Destruct a cipher.
1460ba2cbe9Sxc151355  * ic_setkey - Validate key contents
1470ba2cbe9Sxc151355  * ic_encap  - Encrypt the 802.11 MAC payload
1480ba2cbe9Sxc151355  * ic_decap  - Decrypt the 802.11 MAC payload
1490ba2cbe9Sxc151355  * ic_enmic  - Add MIC
1500ba2cbe9Sxc151355  * ic_demic  - Check and remove MIC
1510ba2cbe9Sxc151355  */
1520ba2cbe9Sxc151355 struct ieee80211_cipher {
1530ba2cbe9Sxc151355 	const char	*ic_name;	/* printable name */
1540ba2cbe9Sxc151355 	uint32_t	ic_cipher;	/* IEEE80211_CIPHER_* */
1550ba2cbe9Sxc151355 	uint32_t	ic_header;	/* size of privacy header (bytes) */
1560ba2cbe9Sxc151355 	uint32_t	ic_trailer;	/* size of privacy trailer (bytes) */
1570ba2cbe9Sxc151355 	uint32_t	ic_miclen;	/* size of mic trailer (bytes) */
1580ba2cbe9Sxc151355 	void		*(*ic_attach)(struct ieee80211com *,
1590ba2cbe9Sxc151355 				struct ieee80211_key *);
1600ba2cbe9Sxc151355 	void		(*ic_detach)(struct ieee80211_key *);
1610ba2cbe9Sxc151355 	int32_t		(*ic_setkey)(struct ieee80211_key *);
1620ba2cbe9Sxc151355 	int32_t		(*ic_encap)(struct ieee80211_key *, mblk_t *,
1630ba2cbe9Sxc151355 				uint8_t keyid);
1640ba2cbe9Sxc151355 	int32_t		(*ic_decap)(struct ieee80211_key *, mblk_t *, int);
1650ba2cbe9Sxc151355 	int32_t		(*ic_enmic)(struct ieee80211_key *, mblk_t *, int);
1660ba2cbe9Sxc151355 	int32_t		(*ic_demic)(struct ieee80211_key *, mblk_t *, int);
1670ba2cbe9Sxc151355 };
1680ba2cbe9Sxc151355 extern	const struct ieee80211_cipher ieee80211_cipher_none;
1690ba2cbe9Sxc151355 
1700ba2cbe9Sxc151355 struct ieee80211_key {
1710ba2cbe9Sxc151355 	uint8_t		wk_keylen;	/* key length in bytes */
1720ba2cbe9Sxc151355 	uint8_t		wk_pad;
1730ba2cbe9Sxc151355 	uint16_t	wk_flags;
1740ba2cbe9Sxc151355 	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
1750ba2cbe9Sxc151355 	ieee80211_keyix	wk_keyix;	/* h/w key index */
1760ba2cbe9Sxc151355 	ieee80211_keyix	wk_rxkeyix;	/* optional h/w rx key index */
1770ba2cbe9Sxc151355 	uint64_t	wk_keyrsc;	/* key receive sequence counter */
1780ba2cbe9Sxc151355 	uint64_t	wk_keytsc;	/* key transmit sequence counter */
1790ba2cbe9Sxc151355 	const struct ieee80211_cipher	*wk_cipher;
1800ba2cbe9Sxc151355 	void		*wk_private;	/* private cipher state */
1810ba2cbe9Sxc151355 };
1820ba2cbe9Sxc151355 #define	wk_txmic	wk_key+IEEE80211_KEYBUF_SIZE+0
1830ba2cbe9Sxc151355 #define	wk_rxmic	wk_key+IEEE80211_KEYBUF_SIZE+8
1840ba2cbe9Sxc151355 
1850ba2cbe9Sxc151355 /*
1860ba2cbe9Sxc151355  * Crypto state kept in each ieee80211com.
1870ba2cbe9Sxc151355  */
1880ba2cbe9Sxc151355 struct ieee80211_crypto_state {
1890ba2cbe9Sxc151355 	struct ieee80211_key	cs_nw_keys[IEEE80211_KEY_MAX];
1900ba2cbe9Sxc151355 	ieee80211_keyix		cs_def_txkey;	/* default/group tx key index */
1910ba2cbe9Sxc151355 	uint16_t		cs_max_keyix;	/* max h/w key index */
1920ba2cbe9Sxc151355 
1930ba2cbe9Sxc151355 	int			(*cs_key_alloc)(struct ieee80211com *,
1940ba2cbe9Sxc151355 					const struct ieee80211_key *,
1950ba2cbe9Sxc151355 					ieee80211_keyix *, ieee80211_keyix *);
1960ba2cbe9Sxc151355 	int			(*cs_key_delete)(struct ieee80211com *,
1970ba2cbe9Sxc151355 					const struct ieee80211_key *);
1980ba2cbe9Sxc151355 	int			(*cs_key_set)(struct ieee80211com *,
1990ba2cbe9Sxc151355 					const struct ieee80211_key *,
2000ba2cbe9Sxc151355 					const uint8_t mac[IEEE80211_ADDR_LEN]);
2010ba2cbe9Sxc151355 	void			(*cs_key_update_begin)(struct ieee80211com *);
2020ba2cbe9Sxc151355 	void			(*cs_key_update_end)(struct ieee80211com *);
2030ba2cbe9Sxc151355 };
2040ba2cbe9Sxc151355 
2050ba2cbe9Sxc151355 /*
2060ba2cbe9Sxc151355  * Key update synchronization methods.
2070ba2cbe9Sxc151355  */
2080ba2cbe9Sxc151355 #define	KEY_UPDATE_BEGIN(ic)		\
2090ba2cbe9Sxc151355 	(ic)->ic_crypto.cs_key_update_begin(ic)
2100ba2cbe9Sxc151355 #define	KEY_UPDATE_END(ic)		\
2110ba2cbe9Sxc151355 	(ic)->ic_crypto.cs_key_update_end(ic)
2120ba2cbe9Sxc151355 #define	KEY_UNDEFINED(k)		\
2130ba2cbe9Sxc151355 	((k).wk_cipher == &ieee80211_cipher_none)
2140ba2cbe9Sxc151355 
2150ba2cbe9Sxc151355 #define	DEV_KEY_ALLOC(ic, k, kix, rkix) \
2160ba2cbe9Sxc151355 	(ic)->ic_crypto.cs_key_alloc(ic, k, kix, rkix)
2170ba2cbe9Sxc151355 #define	DEV_KEY_DELETE(ic, k)		\
2180ba2cbe9Sxc151355 	(ic)->ic_crypto.cs_key_delete(ic, k)
2190ba2cbe9Sxc151355 #define	DEV_KEY_SET(ic, k, m)		\
2200ba2cbe9Sxc151355 	(ic)->ic_crypto.cs_key_set(ic, k, m)
2210ba2cbe9Sxc151355 
2220ba2cbe9Sxc151355 #define	CIPHER_DETACH(k)		\
2230ba2cbe9Sxc151355 	(k)->wk_cipher->ic_detach(k)
2240ba2cbe9Sxc151355 #define	CIPHER_ATTACH(k)		\
2250ba2cbe9Sxc151355 	(k)->wk_cipher->ic_attach(k)
2260ba2cbe9Sxc151355 
2270ba2cbe9Sxc151355 #define	ieee80211_crypto_demic(ic, k, m, force)		\
2280ba2cbe9Sxc151355 	(((k)->wk_cipher->ic_miclen > 0) ?		\
2290ba2cbe9Sxc151355 	(k)->wk_cipher->ic_demic(k, m, force) :		\
2300ba2cbe9Sxc151355 	1)
2310ba2cbe9Sxc151355 
2320ba2cbe9Sxc151355 #define	ieee80211_crypto_enmic(ic, k, m, force)		\
2330ba2cbe9Sxc151355 	((k)->wk_cipher->ic_miclen > 0 ?		\
2340ba2cbe9Sxc151355 	(k)->wk_cipher->ic_enmic(k, m, force) :		\
2350ba2cbe9Sxc151355 	1)
2360ba2cbe9Sxc151355 
2370ba2cbe9Sxc151355 void ieee80211_crypto_attach(struct ieee80211com *ic);
2380ba2cbe9Sxc151355 void ieee80211_crypto_detach(struct ieee80211com *ic);
239239e91abShx147065 void ieee80211_crypto_register(struct ieee80211com *ic,
240239e91abShx147065     const struct ieee80211_cipher *);
241239e91abShx147065 void ieee80211_crypto_unregister(struct ieee80211com *ic,
242239e91abShx147065     const struct ieee80211_cipher *);
2430ba2cbe9Sxc151355 void ieee80211_crypto_resetkey(struct ieee80211com *, struct ieee80211_key *,
2440ba2cbe9Sxc151355 	ieee80211_keyix);
2450ba2cbe9Sxc151355 
246*82a2fc47SJames Carlson #endif /* _KERNEL */
247*82a2fc47SJames Carlson 
2480ba2cbe9Sxc151355 #ifdef	__cplusplus
2490ba2cbe9Sxc151355 }
2500ba2cbe9Sxc151355 #endif
2510ba2cbe9Sxc151355 
2520ba2cbe9Sxc151355 #endif	/* _SYS_NET80211_CRYPTO_H */
253