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