1 /*
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * GNU General Public License ("GPL") version 2 as published by the Free
19  * Software Foundation.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/net80211/ieee80211_crypto.h,v 1.9.2.1 2005/09/03 22:40:02 sam Exp $
33  * $DragonFly: src/sys/netproto/802_11/ieee80211_crypto.h,v 1.4 2007/05/07 14:12:16 sephe Exp $
34  */
35 #ifndef _NET80211_IEEE80211_CRYPTO_H_
36 #define _NET80211_IEEE80211_CRYPTO_H_
37 
38 /*
39  * 802.11 protocol crypto-related definitions.
40  */
41 #define	IEEE80211_KEYBUF_SIZE	16
42 #define	IEEE80211_MICBUF_SIZE	(8+8)	/* space for both tx+rx keys */
43 
44 /*
45  * Old WEP-style key.  Deprecated.
46  */
47 struct ieee80211_wepkey {
48 	u_int		wk_len;		/* key length in bytes */
49 	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE];
50 };
51 
52 struct ieee80211_cipher;
53 
54 /*
55  * Crypto key state.  There is sufficient room for all supported
56  * ciphers (see below).  The underlying ciphers are handled
57  * separately through loadable cipher modules that register with
58  * the generic crypto support.  A key has a reference to an instance
59  * of the cipher; any per-key state is hung off wk_private by the
60  * cipher when it is attached.  Ciphers are automatically called
61  * to detach and cleanup any such state when the key is deleted.
62  *
63  * The generic crypto support handles encap/decap of cipher-related
64  * frame contents for both hardware- and software-based implementations.
65  * A key requiring software crypto support is automatically flagged and
66  * the cipher is expected to honor this and do the necessary work.
67  * Ciphers such as TKIP may also support mixed hardware/software
68  * encrypt/decrypt and MIC processing.
69  */
70 typedef uint16_t ieee80211_keyix;	/* h/w key index */
71 
72 struct ieee80211_key {
73 	uint8_t		wk_keylen;	/* key length in bytes */
74 	uint8_t		wk_keyid;	/* key id */
75 	uint16_t	wk_flags;
76 #define	IEEE80211_KEY_XMIT	0x01	/* key used for xmit */
77 #define	IEEE80211_KEY_RECV	0x02	/* key used for recv */
78 #define	IEEE80211_KEY_GROUP	0x04	/* key used for WPA group operation */
79 #define	IEEE80211_KEY_SWCRYPT	0x10	/* host-based encrypt/decrypt */
80 #define	IEEE80211_KEY_SWMIC	0x20	/* host-based enmic/demic */
81 #define IEEE80211_KEY_NOHDR	0x40	/* driver appends crypto header */
82 #define IEEE80211_KEY_NOMIC	0x80	/* driver strips TKIP MIC */
83 	ieee80211_keyix	wk_keyix;	/* h/w key index */
84 	ieee80211_keyix	wk_rxkeyix;	/* optional h/w rx key index */
85 	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
86 #define	wk_txmic	wk_key+IEEE80211_KEYBUF_SIZE+0	/* XXX can't () right */
87 #define	wk_rxmic	wk_key+IEEE80211_KEYBUF_SIZE+8	/* XXX can't () right */
88 	uint64_t	wk_keyrsc;	/* key receive sequence counter */
89 	uint64_t	wk_keytsc;	/* key transmit sequence counter */
90 	const struct ieee80211_cipher *wk_cipher;
91 	void		*wk_private;	/* private cipher state */
92 };
93 #define	IEEE80211_KEY_COMMON 		/* common flags passed in by apps */\
94 	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP)
95 
96 /*
97  * NB: these values are ordered carefully; there are lots of
98  * of implications in any reordering.  In particular beware
99  * that 4 is not used to avoid conflicting with IEEE80211_F_PRIVACY.
100  */
101 #define	IEEE80211_CIPHER_WEP		0
102 #define	IEEE80211_CIPHER_TKIP		1
103 #define	IEEE80211_CIPHER_AES_OCB	2
104 #define	IEEE80211_CIPHER_AES_CCM	3
105 #define	IEEE80211_CIPHER_CKIP		5
106 #define	IEEE80211_CIPHER_NONE		6	/* pseudo value */
107 
108 #define	IEEE80211_CIPHER_MAX		(IEEE80211_CIPHER_NONE+1)
109 
110 #define	IEEE80211_KEYIX_NONE	((ieee80211_keyix) -1)
111 
112 #if defined(__KERNEL__) || defined(_KERNEL)
113 
114 struct ieee80211com;
115 struct ieee80211_node;
116 struct mbuf;
117 
118 /*
119  * Crypto state kept in each ieee80211com.  Some of this
120  * can/should be shared when virtual AP's are supported.
121  *
122  * XXX save reference to ieee80211com to properly encapsulate state.
123  * XXX split out crypto capabilities from ic_caps
124  */
125 struct ieee80211_crypto_state {
126 	struct ieee80211_key	cs_nw_keys[IEEE80211_WEP_NKID];
127 	ieee80211_keyix		cs_def_txkey;	/* default/group tx key index */
128 	uint16_t		cs_max_keyix;	/* max h/w key index */
129 
130 	int			(*cs_key_alloc)(struct ieee80211com *,
131 					const struct ieee80211_key *,
132 					ieee80211_keyix *, ieee80211_keyix *);
133 	int			(*cs_key_delete)(struct ieee80211com *,
134 					const struct ieee80211_key *);
135 	int			(*cs_key_set)(struct ieee80211com *,
136 					const struct ieee80211_key *,
137 					const uint8_t mac[IEEE80211_ADDR_LEN]);
138 	void			(*cs_key_update_begin)(struct ieee80211com *);
139 	void			(*cs_key_update_end)(struct ieee80211com *);
140 };
141 
142 void	ieee80211_crypto_attach(struct ieee80211com *);
143 void	ieee80211_crypto_detach(struct ieee80211com *);
144 int	ieee80211_crypto_newkey(struct ieee80211com *,
145 		int cipher, int flags, struct ieee80211_key *);
146 int	ieee80211_crypto_delkey(struct ieee80211com *,
147 		struct ieee80211_key *);
148 int	ieee80211_crypto_setkey(struct ieee80211com *,
149 		struct ieee80211_key *, const uint8_t macaddr[IEEE80211_ADDR_LEN]);
150 void	ieee80211_crypto_delglobalkeys(struct ieee80211com *);
151 
152 struct ieee80211_crypto_iv {
153 	/*
154 	 * Keep this layout!
155 	 * Crypto modules will assume that ic_iv and ic_eiv are contiguous.
156 	 */
157 	uint8_t	ic_iv[4];
158 	uint8_t	ic_eiv[4];
159 };
160 
161 /*
162  * Template for a supported cipher.  Ciphers register with the
163  * crypto code and are typically loaded as separate modules
164  * (the null cipher is always present).
165  * XXX may need refcnts
166  */
167 struct ieee80211_cipher {
168 	const char *ic_name;		/* printable name */
169 	u_int	ic_cipher;		/* IEEE80211_CIPHER_* */
170 	u_int	ic_header;		/* size of privacy header (bytes) */
171 	u_int	ic_trailer;		/* size of privacy trailer (bytes) */
172 	u_int	ic_miclen;		/* size of mic trailer (bytes) */
173 	void*	(*ic_attach)(struct ieee80211com *, struct ieee80211_key *);
174 	void	(*ic_detach)(struct ieee80211_key *);
175 	int	(*ic_setkey)(struct ieee80211_key *);
176 	int	(*ic_encap)(struct ieee80211_key *, struct mbuf *,
177 			uint8_t keyid);
178 	int	(*ic_decap)(struct ieee80211_key *, struct mbuf *, int);
179 	int	(*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);
180 	int	(*ic_demic)(struct ieee80211_key *, struct mbuf *, int);
181 	int	(*ic_getiv)(struct ieee80211_key *,
182 			struct ieee80211_crypto_iv *, uint8_t);
183 	int	(*ic_update)(struct ieee80211_key *,
184 			const struct ieee80211_crypto_iv *,
185 			const struct ieee80211_frame *);
186 };
187 extern	const struct ieee80211_cipher ieee80211_cipher_none;
188 
189 void	ieee80211_crypto_register(const struct ieee80211_cipher *);
190 void	ieee80211_crypto_unregister(const struct ieee80211_cipher *);
191 int	ieee80211_crypto_available(u_int cipher);
192 const struct ieee80211_cipher *ieee80211_crypto_cipher(u_int cipher);
193 
194 void	ieee80211_crypto_resetkey(struct ieee80211com *,
195 		struct ieee80211_key *, ieee80211_keyix);
196 
197 struct ieee80211_key *ieee80211_crypto_encap(struct ieee80211com *,
198 		struct ieee80211_node *, struct mbuf *);
199 struct ieee80211_key *ieee80211_crypto_decap(struct ieee80211com *,
200 		struct ieee80211_node *, struct mbuf *, int);
201 struct ieee80211_key *ieee80211_crypto_findkey(struct ieee80211com *,
202 		struct ieee80211_node *, struct mbuf *);
203 struct ieee80211_key *ieee80211_crypto_encap_withkey(struct ieee80211com *,
204 		struct mbuf *, struct ieee80211_key *);
205 
206 struct ieee80211_key *ieee80211_crypto_update(struct ieee80211com *,
207 		struct ieee80211_node *, const struct ieee80211_crypto_iv *,
208 		const struct ieee80211_frame *);
209 struct ieee80211_key *ieee80211_crypto_getiv(struct ieee80211com *,
210 		struct ieee80211_crypto_iv *, struct ieee80211_key *);
211 
212 /*
213  * Check and remove any MIC.
214  */
215 static __inline int
216 ieee80211_crypto_demic(struct ieee80211com *ic, struct ieee80211_key *k,
217 	struct mbuf *m, int force)
218 {
219 	const struct ieee80211_cipher *cip = k->wk_cipher;
220 	return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
221 }
222 
223 /*
224  * Add any MIC.
225  */
226 static __inline int
227 ieee80211_crypto_enmic(struct ieee80211com *ic,
228 	struct ieee80211_key *k, struct mbuf *m, int force)
229 {
230 	const struct ieee80211_cipher *cip = k->wk_cipher;
231 	return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);
232 }
233 
234 /*
235  * Crypt-related notification methods.
236  */
237 void	ieee80211_notify_replay_failure(struct ieee80211com *,
238 		const struct ieee80211_frame *, const struct ieee80211_key *,
239 		uint64_t rsc);
240 void	ieee80211_notify_michael_failure(struct ieee80211com *,
241 		const struct ieee80211_frame *, u_int keyix);
242 
243 #endif /* defined(__KERNEL__) || defined(_KERNEL) */
244 #endif /* _NET80211_IEEE80211_CRYPTO_H_ */
245