xref: /freebsd/sys/net80211/ieee80211_crypto.h (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Atsushi Onoe
5  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #ifndef _NET80211_IEEE80211_CRYPTO_H_
29 #define _NET80211_IEEE80211_CRYPTO_H_
30 
31 /*
32  * 802.11 protocol crypto-related definitions.
33  */
34 #define	IEEE80211_KEYBUF_SIZE	16
35 #define	IEEE80211_MICBUF_SIZE	(8+8)	/* space for both tx+rx keys */
36 
37 /*
38  * Old WEP-style key.  Deprecated.
39  */
40 struct ieee80211_wepkey {
41 	u_int		wk_len;		/* key length in bytes */
42 	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE];
43 };
44 
45 struct ieee80211_rsnparms {
46 	uint8_t		rsn_mcastcipher;	/* mcast/group cipher */
47 	uint8_t		rsn_mcastkeylen;	/* mcast key length */
48 	uint8_t		rsn_ucastcipher;	/* selected unicast cipher */
49 	uint8_t		rsn_ucastkeylen;	/* unicast key length */
50 	uint8_t		rsn_keymgmt;		/* selected key mgmt algo */
51 	uint16_t	rsn_caps;		/* capabilities */
52 };
53 
54 struct ieee80211_cipher;
55 
56 /*
57  * Crypto key state.  There is sufficient room for all supported
58  * ciphers (see below).  The underlying ciphers are handled
59  * separately through loadable cipher modules that register with
60  * the generic crypto support.  A key has a reference to an instance
61  * of the cipher; any per-key state is hung off wk_private by the
62  * cipher when it is attached.  Ciphers are automatically called
63  * to detach and cleanup any such state when the key is deleted.
64  *
65  * The generic crypto support handles encap/decap of cipher-related
66  * frame contents for both hardware- and software-based implementations.
67  * A key requiring software crypto support is automatically flagged and
68  * the cipher is expected to honor this and do the necessary work.
69  * Ciphers such as TKIP may also support mixed hardware/software
70  * encrypt/decrypt and MIC processing.
71  */
72 typedef uint16_t ieee80211_keyix;	/* h/w key index */
73 
74 struct ieee80211_key {
75 	uint8_t		wk_keylen;	/* key length in bytes */
76 	uint8_t		wk_pad;		/* .. some drivers use this. Fix that. */
77 	uint8_t		wk_pad1[2];
78 	uint32_t	wk_flags;
79 #define	IEEE80211_KEY_XMIT	0x00000001	/* key used for xmit */
80 #define	IEEE80211_KEY_RECV	0x00000002	/* key used for recv */
81 #define	IEEE80211_KEY_GROUP	0x00000004	/* key used for WPA group operation */
82 #define	IEEE80211_KEY_NOREPLAY	0x00000008	/* ignore replay failures */
83 #define	IEEE80211_KEY_SWENCRYPT	0x00000010	/* host-based encrypt */
84 #define	IEEE80211_KEY_SWDECRYPT	0x00000020	/* host-based decrypt */
85 #define	IEEE80211_KEY_SWENMIC	0x00000040	/* host-based enmic */
86 #define	IEEE80211_KEY_SWDEMIC	0x00000080	/* host-based demic */
87 #define	IEEE80211_KEY_DEVKEY	0x00000100	/* device key request completed */
88 #define	IEEE80211_KEY_CIPHER0	0x00001000	/* cipher-specific action 0 */
89 #define	IEEE80211_KEY_CIPHER1	0x00002000	/* cipher-specific action 1 */
90 #define	IEEE80211_KEY_NOIV	0x00004000	/* don't insert IV/MIC for !mgmt */
91 #define	IEEE80211_KEY_NOIVMGT	0x00008000	/* don't insert IV/MIC for mgmt */
92 #define	IEEE80211_KEY_NOMIC	0x00010000	/* don't insert MIC for !mgmt */
93 #define	IEEE80211_KEY_NOMICMGT	0x00020000	/* don't insert MIC for mgmt */
94 
95 	ieee80211_keyix	wk_keyix;	/* h/w key index */
96 	ieee80211_keyix	wk_rxkeyix;	/* optional h/w rx key index */
97 	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
98 #define	wk_txmic	wk_key+IEEE80211_KEYBUF_SIZE+0	/* XXX can't () right */
99 #define	wk_rxmic	wk_key+IEEE80211_KEYBUF_SIZE+8	/* XXX can't () right */
100 					/* key receive sequence counter */
101 	uint64_t	wk_keyrsc[IEEE80211_TID_SIZE];
102 	uint64_t	wk_keytsc;	/* key transmit sequence counter */
103 	const struct ieee80211_cipher *wk_cipher;
104 	void		*wk_private;	/* private cipher state */
105 	uint8_t		wk_macaddr[IEEE80211_ADDR_LEN];
106 };
107 #define	IEEE80211_KEY_COMMON 		/* common flags passed in by apps */\
108 	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP | \
109 	 IEEE80211_KEY_NOREPLAY)
110 
111 #define	IEEE80211_KEY_SWCRYPT \
112 	(IEEE80211_KEY_SWENCRYPT | IEEE80211_KEY_SWDECRYPT)
113 #define	IEEE80211_KEY_SWMIC	(IEEE80211_KEY_SWENMIC | IEEE80211_KEY_SWDEMIC)
114 
115 #define IEEE80211_KEY_DEVICE		/* flags owned by device driver */\
116 	(IEEE80211_KEY_DEVKEY|IEEE80211_KEY_CIPHER0|IEEE80211_KEY_CIPHER1| \
117 	 IEEE80211_KEY_SWCRYPT|IEEE80211_KEY_SWMIC|IEEE80211_KEY_NOIV | \
118 	 IEEE80211_KEY_NOIVMGT|IEEE80211_KEY_NOMIC|IEEE80211_KEY_NOMICMGT)
119 
120 #define	IEEE80211_KEY_BITS \
121 	"\20\1XMIT\2RECV\3GROUP\4SWENCRYPT\5SWDECRYPT\6SWENMIC\7SWDEMIC" \
122 	"\10DEVKEY\11CIPHER0\12CIPHER1"
123 
124 #define	IEEE80211_KEYIX_NONE	((ieee80211_keyix) -1)
125 
126 /*
127  * NB: these values are ordered carefully; there are lots of
128  * of implications in any reordering.  Beware that 4 is used
129  * only to indicate h/w TKIP MIC support in driver capabilities;
130  * there is no separate cipher support (it's rolled into the
131  * TKIP cipher support).
132  */
133 #define	IEEE80211_CIPHER_WEP		0
134 #define	IEEE80211_CIPHER_TKIP		1
135 #define	IEEE80211_CIPHER_AES_OCB	2
136 #define	IEEE80211_CIPHER_AES_CCM	3
137 #define	IEEE80211_CIPHER_TKIPMIC	4	/* TKIP MIC capability */
138 #define	IEEE80211_CIPHER_CKIP		5
139 #define	IEEE80211_CIPHER_NONE		6	/* pseudo value */
140 
141 #define	IEEE80211_CIPHER_MAX		(IEEE80211_CIPHER_NONE+1)
142 
143 /* capability bits in ic_cryptocaps/iv_cryptocaps */
144 #define	IEEE80211_CRYPTO_WEP		(1<<IEEE80211_CIPHER_WEP)
145 #define	IEEE80211_CRYPTO_TKIP		(1<<IEEE80211_CIPHER_TKIP)
146 #define	IEEE80211_CRYPTO_AES_OCB	(1<<IEEE80211_CIPHER_AES_OCB)
147 #define	IEEE80211_CRYPTO_AES_CCM	(1<<IEEE80211_CIPHER_AES_CCM)
148 #define	IEEE80211_CRYPTO_TKIPMIC	(1<<IEEE80211_CIPHER_TKIPMIC)
149 #define	IEEE80211_CRYPTO_CKIP		(1<<IEEE80211_CIPHER_CKIP)
150 
151 #define	IEEE80211_CRYPTO_BITS \
152 	"\20\1WEP\2TKIP\3AES\4AES_CCM\5TKIPMIC\6CKIP"
153 
154 #if defined(__KERNEL__) || defined(_KERNEL)
155 
156 struct ieee80211com;
157 struct ieee80211vap;
158 struct ieee80211_node;
159 struct mbuf;
160 
161 MALLOC_DECLARE(M_80211_CRYPTO);
162 
163 void	ieee80211_crypto_attach(struct ieee80211com *);
164 void	ieee80211_crypto_detach(struct ieee80211com *);
165 void	ieee80211_crypto_vattach(struct ieee80211vap *);
166 void	ieee80211_crypto_vdetach(struct ieee80211vap *);
167 int	ieee80211_crypto_newkey(struct ieee80211vap *,
168 		int cipher, int flags, struct ieee80211_key *);
169 int	ieee80211_crypto_delkey(struct ieee80211vap *,
170 		struct ieee80211_key *);
171 int	ieee80211_crypto_setkey(struct ieee80211vap *, struct ieee80211_key *);
172 void	ieee80211_crypto_delglobalkeys(struct ieee80211vap *);
173 void	ieee80211_crypto_reload_keys(struct ieee80211com *);
174 void	ieee80211_crypto_set_deftxkey(struct ieee80211vap *,
175 	    ieee80211_keyix kid);
176 
177 /*
178  * Template for a supported cipher.  Ciphers register with the
179  * crypto code and are typically loaded as separate modules
180  * (the null cipher is always present).
181  * XXX may need refcnts
182  */
183 struct ieee80211_cipher {
184 	const char *ic_name;		/* printable name */
185 	u_int	ic_cipher;		/* IEEE80211_CIPHER_* */
186 	u_int	ic_header;		/* size of privacy header (bytes) */
187 	u_int	ic_trailer;		/* size of privacy trailer (bytes) */
188 	u_int	ic_miclen;		/* size of mic trailer (bytes) */
189 	void*	(*ic_attach)(struct ieee80211vap *, struct ieee80211_key *);
190 	void	(*ic_detach)(struct ieee80211_key *);
191 	int	(*ic_setkey)(struct ieee80211_key *);
192 	void	(*ic_setiv)(struct ieee80211_key *, uint8_t *);
193 	int	(*ic_encap)(struct ieee80211_key *, struct mbuf *);
194 	int	(*ic_decap)(struct ieee80211_key *, struct mbuf *, int);
195 	int	(*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);
196 	int	(*ic_demic)(struct ieee80211_key *, struct mbuf *, int);
197 };
198 extern	const struct ieee80211_cipher ieee80211_cipher_none;
199 
200 #define	IEEE80211_KEY_UNDEFINED(k) \
201 	((k)->wk_cipher == &ieee80211_cipher_none)
202 
203 void	ieee80211_crypto_register(const struct ieee80211_cipher *);
204 void	ieee80211_crypto_unregister(const struct ieee80211_cipher *);
205 int	ieee80211_crypto_available(u_int cipher);
206 
207 int	ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *,
208 	    const struct ieee80211_key *k);
209 uint8_t	ieee80211_crypto_get_keyid(struct ieee80211vap *vap,
210 		struct ieee80211_key *k);
211 struct ieee80211_key *ieee80211_crypto_get_txkey(struct ieee80211_node *,
212 		struct mbuf *);
213 struct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *,
214 		struct mbuf *);
215 int	ieee80211_crypto_decap(struct ieee80211_node *,
216 		struct mbuf *, int, struct ieee80211_key **);
217 int ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
218 		struct mbuf *, int);
219 /*
220  * Add any MIC.
221  */
222 static __inline int
223 ieee80211_crypto_enmic(struct ieee80211vap *vap,
224 	struct ieee80211_key *k, struct mbuf *m, int force)
225 {
226 	const struct ieee80211_cipher *cip = k->wk_cipher;
227 	return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);
228 }
229 
230 /*
231  * Reset key state to an unused state.  The crypto
232  * key allocation mechanism insures other state (e.g.
233  * key data) is properly setup before a key is used.
234  */
235 static __inline void
236 ieee80211_crypto_resetkey(struct ieee80211vap *vap,
237 	struct ieee80211_key *k, ieee80211_keyix ix)
238 {
239 	k->wk_cipher = &ieee80211_cipher_none;
240 	k->wk_private = k->wk_cipher->ic_attach(vap, k);
241 	k->wk_keyix = k->wk_rxkeyix = ix;
242 	k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
243 }
244 
245 /*
246  * Crypt-related notification methods.
247  */
248 void	ieee80211_notify_replay_failure(struct ieee80211vap *,
249 		const struct ieee80211_frame *, const struct ieee80211_key *,
250 		uint64_t rsc, int tid);
251 void	ieee80211_notify_michael_failure(struct ieee80211vap *,
252 		const struct ieee80211_frame *, u_int keyix);
253 #endif /* defined(__KERNEL__) || defined(_KERNEL) */
254 #endif /* _NET80211_IEEE80211_CRYPTO_H_ */
255