xref: /titanic_41/usr/src/uts/common/sys/net80211_crypto.h (revision cb6207858a9fcc2feaee22e626912fba281ac969)
1 /*
2  * Copyright 2006 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 #define	IEEE80211_WEP_HDRLEN					\
95 	(IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
96 #define	IEEE80211_WEP_MINLEN					\
97 	(sizeof (struct ieee80211_frame) +			\
98 	IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
99 
100 /* Maximum number of keys */
101 #define	IEEE80211_KEY_MAX		IEEE80211_WEP_NKID
102 
103 struct ieee80211com;
104 struct ieee80211_node;
105 struct ieee80211_key;
106 typedef uint16_t	ieee80211_keyix;	/* h/w key index */
107 
108 #define	IEEE80211_KEYIX_NONE	((ieee80211_keyix) -1)
109 
110 /*
111  * Template for a supported cipher.  Ciphers register with the
112  * crypto code.
113  *
114  * ic_attach - Initialize cipher. The return value is set to wk_private
115  * ic_detach - Destruct a cipher.
116  * ic_setkey - Validate key contents
117  * ic_encap  - Encrypt the 802.11 MAC payload
118  * ic_decap  - Decrypt the 802.11 MAC payload
119  * ic_enmic  - Add MIC
120  * ic_demic  - Check and remove MIC
121  */
122 struct ieee80211_cipher {
123 	const char	*ic_name;	/* printable name */
124 	uint32_t	ic_cipher;	/* IEEE80211_CIPHER_* */
125 	uint32_t	ic_header;	/* size of privacy header (bytes) */
126 	uint32_t	ic_trailer;	/* size of privacy trailer (bytes) */
127 	uint32_t	ic_miclen;	/* size of mic trailer (bytes) */
128 	void		*(*ic_attach)(struct ieee80211com *,
129 				struct ieee80211_key *);
130 	void		(*ic_detach)(struct ieee80211_key *);
131 	int32_t		(*ic_setkey)(struct ieee80211_key *);
132 	int32_t		(*ic_encap)(struct ieee80211_key *, mblk_t *,
133 				uint8_t keyid);
134 	int32_t		(*ic_decap)(struct ieee80211_key *, mblk_t *, int);
135 	int32_t		(*ic_enmic)(struct ieee80211_key *, mblk_t *, int);
136 	int32_t		(*ic_demic)(struct ieee80211_key *, mblk_t *, int);
137 };
138 extern	const struct ieee80211_cipher ieee80211_cipher_none;
139 
140 struct ieee80211_key {
141 	uint8_t		wk_keylen;	/* key length in bytes */
142 	uint8_t		wk_pad;
143 	uint16_t	wk_flags;
144 	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
145 	ieee80211_keyix	wk_keyix;	/* h/w key index */
146 	ieee80211_keyix	wk_rxkeyix;	/* optional h/w rx key index */
147 	uint64_t	wk_keyrsc;	/* key receive sequence counter */
148 	uint64_t	wk_keytsc;	/* key transmit sequence counter */
149 	const struct ieee80211_cipher	*wk_cipher;
150 	void		*wk_private;	/* private cipher state */
151 };
152 #define	wk_txmic	wk_key+IEEE80211_KEYBUF_SIZE+0
153 #define	wk_rxmic	wk_key+IEEE80211_KEYBUF_SIZE+8
154 
155 /*
156  * Crypto state kept in each ieee80211com.
157  */
158 struct ieee80211_crypto_state {
159 	struct ieee80211_key	cs_nw_keys[IEEE80211_KEY_MAX];
160 	ieee80211_keyix		cs_def_txkey;	/* default/group tx key index */
161 	uint16_t		cs_max_keyix;	/* max h/w key index */
162 
163 	int			(*cs_key_alloc)(struct ieee80211com *,
164 					const struct ieee80211_key *,
165 					ieee80211_keyix *, ieee80211_keyix *);
166 	int			(*cs_key_delete)(struct ieee80211com *,
167 					const struct ieee80211_key *);
168 	int			(*cs_key_set)(struct ieee80211com *,
169 					const struct ieee80211_key *,
170 					const uint8_t mac[IEEE80211_ADDR_LEN]);
171 	void			(*cs_key_update_begin)(struct ieee80211com *);
172 	void			(*cs_key_update_end)(struct ieee80211com *);
173 };
174 
175 /*
176  * Key update synchronization methods.
177  */
178 #define	KEY_UPDATE_BEGIN(ic)		\
179 	(ic)->ic_crypto.cs_key_update_begin(ic)
180 #define	KEY_UPDATE_END(ic)		\
181 	(ic)->ic_crypto.cs_key_update_end(ic)
182 #define	KEY_UNDEFINED(k)		\
183 	((k).wk_cipher == &ieee80211_cipher_none)
184 
185 #define	DEV_KEY_ALLOC(ic, k, kix, rkix) \
186 	(ic)->ic_crypto.cs_key_alloc(ic, k, kix, rkix)
187 #define	DEV_KEY_DELETE(ic, k)		\
188 	(ic)->ic_crypto.cs_key_delete(ic, k)
189 #define	DEV_KEY_SET(ic, k, m)		\
190 	(ic)->ic_crypto.cs_key_set(ic, k, m)
191 
192 #define	CIPHER_DETACH(k)		\
193 	(k)->wk_cipher->ic_detach(k)
194 #define	CIPHER_ATTACH(k)		\
195 	(k)->wk_cipher->ic_attach(k)
196 
197 #define	ieee80211_crypto_demic(ic, k, m, force)		\
198 	(((k)->wk_cipher->ic_miclen > 0) ?		\
199 	(k)->wk_cipher->ic_demic(k, m, force) :		\
200 	1)
201 
202 #define	ieee80211_crypto_enmic(ic, k, m, force)		\
203 	((k)->wk_cipher->ic_miclen > 0 ?		\
204 	(k)->wk_cipher->ic_enmic(k, m, force) :		\
205 	1)
206 
207 void ieee80211_crypto_attach(struct ieee80211com *ic);
208 void ieee80211_crypto_detach(struct ieee80211com *ic);
209 void ieee80211_crypto_register(const struct ieee80211_cipher *);
210 void ieee80211_crypto_unregister(const struct ieee80211_cipher *);
211 void ieee80211_crypto_resetkey(struct ieee80211com *, struct ieee80211_key *,
212 	ieee80211_keyix);
213 
214 #ifdef	__cplusplus
215 }
216 #endif
217 
218 #endif	/* _SYS_NET80211_CRYPTO_H */
219