xref: /freebsd/sys/net80211/ieee80211_crypto.h (revision 62c1865c9aaef436498c444b460e6ec2fbcaf44d)
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 
35 /*
36  * Legacy 128 bit key size storage for WEP, TKIP, CCMP key sizes.
37  * This has been used to store keys in net80211 for various things
38  * (eg the rc4key in WEP) as well as driver definitions for their
39  * own hardware programming.
40  *
41  * This should eventually be used by the ioctl and drivers instead of
42  * IEEE80211_KEYBUF_SIZE as the key size will eventually grow.
43  */
44 #define	IEEE80211_KEYBUF_128_SIZE	16
45 #define	IEEE80211_MICBUF_128_SIZE	(8+8)	/* space for both tx+rx keys */
46 
47 /*
48  * Temporary definition whilst I clean up where this is still being used.
49  */
50 #define	IEEE80211_KEYBUF_SIZE		IEEE80211_KEYBUF_128_SIZE
51 #define	IEEE80211_MICBUF_SIZE		IEEE80211_MICBUF_128_SIZE
52 
53 struct ieee80211_rsnparms {
54 	uint8_t		rsn_mcastcipher;	/* mcast/group cipher */
55 	uint8_t		rsn_mcastkeylen;	/* mcast key length */
56 	uint8_t		rsn_ucastcipher;	/* selected unicast cipher */
57 	uint8_t		rsn_ucastkeylen;	/* unicast key length */
58 	uint8_t		rsn_keymgmt;		/* selected key mgmt algo */
59 	uint16_t	rsn_caps;		/* capabilities */
60 };
61 
62 struct ieee80211_cipher;
63 
64 /*
65  * Crypto key state.  There is sufficient room for all supported
66  * ciphers (see below).  The underlying ciphers are handled
67  * separately through loadable cipher modules that register with
68  * the generic crypto support.  A key has a reference to an instance
69  * of the cipher; any per-key state is hung off wk_private by the
70  * cipher when it is attached.  Ciphers are automatically called
71  * to detach and cleanup any such state when the key is deleted.
72  *
73  * The generic crypto support handles encap/decap of cipher-related
74  * frame contents for both hardware- and software-based implementations.
75  * A key requiring software crypto support is automatically flagged and
76  * the cipher is expected to honor this and do the necessary work.
77  * Ciphers such as TKIP may also support mixed hardware/software
78  * encrypt/decrypt and MIC processing.
79  */
80 typedef uint16_t ieee80211_keyix;	/* h/w key index */
81 
82 struct ieee80211_key {
83 	uint8_t		wk_keylen;	/* key length in bytes */
84 	uint8_t		wk_pad;		/* .. some drivers use this. Fix that. */
85 	uint8_t		wk_pad1[2];
86 	uint32_t	wk_flags;
87 #define	IEEE80211_KEY_XMIT	0x00000001	/* key used for xmit */
88 #define	IEEE80211_KEY_RECV	0x00000002	/* key used for recv */
89 #define	IEEE80211_KEY_GROUP	0x00000004	/* key used for WPA group operation */
90 #define	IEEE80211_KEY_NOREPLAY	0x00000008	/* ignore replay failures */
91 #define	IEEE80211_KEY_SWENCRYPT	0x00000010	/* host-based encrypt */
92 #define	IEEE80211_KEY_SWDECRYPT	0x00000020	/* host-based decrypt */
93 #define	IEEE80211_KEY_SWENMIC	0x00000040	/* host-based enmic */
94 #define	IEEE80211_KEY_SWDEMIC	0x00000080	/* host-based demic */
95 #define	IEEE80211_KEY_DEVKEY	0x00000100	/* device key request completed */
96 #define	IEEE80211_KEY_CIPHER0	0x00001000	/* cipher-specific action 0 */
97 #define	IEEE80211_KEY_CIPHER1	0x00002000	/* cipher-specific action 1 */
98 #define	IEEE80211_KEY_NOIV	0x00004000	/* don't insert IV/MIC for !mgmt */
99 #define	IEEE80211_KEY_NOIVMGT	0x00008000	/* don't insert IV/MIC for mgmt */
100 #define	IEEE80211_KEY_NOMIC	0x00010000	/* don't insert MIC for !mgmt */
101 #define	IEEE80211_KEY_NOMICMGT	0x00020000	/* don't insert MIC for mgmt */
102 
103 	ieee80211_keyix	wk_keyix;	/* h/w key index */
104 	ieee80211_keyix	wk_rxkeyix;	/* optional h/w rx key index */
105 	/* TODO: deprecate direct access to wk_key, wk_txmic, wk_rxmic */
106 	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
107 #define	wk_txmic	wk_key+IEEE80211_KEYBUF_SIZE+0	/* XXX can't () right */
108 #define	wk_rxmic	wk_key+IEEE80211_KEYBUF_SIZE+8	/* XXX can't () right */
109 					/* key receive sequence counter */
110 	uint64_t	wk_keyrsc[IEEE80211_TID_SIZE];
111 	uint64_t	wk_keytsc;	/* key transmit sequence counter */
112 	const struct ieee80211_cipher *wk_cipher;
113 	void		*wk_private;	/* private cipher state */
114 	uint8_t		wk_macaddr[IEEE80211_ADDR_LEN];
115 };
116 #define	IEEE80211_KEY_COMMON 		/* common flags passed in by apps */\
117 	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP | \
118 	 IEEE80211_KEY_NOREPLAY)
119 
120 #define	IEEE80211_KEY_SWCRYPT \
121 	(IEEE80211_KEY_SWENCRYPT | IEEE80211_KEY_SWDECRYPT)
122 #define	IEEE80211_KEY_SWMIC	(IEEE80211_KEY_SWENMIC | IEEE80211_KEY_SWDEMIC)
123 
124 #define IEEE80211_KEY_DEVICE		/* flags owned by device driver */\
125 	(IEEE80211_KEY_DEVKEY|IEEE80211_KEY_CIPHER0|IEEE80211_KEY_CIPHER1| \
126 	 IEEE80211_KEY_SWCRYPT|IEEE80211_KEY_SWMIC|IEEE80211_KEY_NOIV | \
127 	 IEEE80211_KEY_NOIVMGT|IEEE80211_KEY_NOMIC|IEEE80211_KEY_NOMICMGT)
128 
129 #define	IEEE80211_KEY_BITS \
130 	"\20\1XMIT\2RECV\3GROUP\4NOREPLAY\5SWENCRYPT\6SWDECRYPT\7SWENMIC\10SWDEMIC" \
131 	"\11DEVKEY\12CIPHER0\13CIPHER1\14NOIV\15NOIVMGT\16NOMIC\17NOMICMGT"
132 
133 #define	IEEE80211_KEYIX_NONE	((ieee80211_keyix) -1)
134 
135 /*
136  * NB: these values are ordered carefully; there are lots of
137  * of implications in any reordering.  Beware that 4 is used
138  * only to indicate h/w TKIP MIC support in driver capabilities;
139  * there is no separate cipher support (it's rolled into the
140  * TKIP cipher support).
141  */
142 #define	IEEE80211_CIPHER_WEP		0
143 #define	IEEE80211_CIPHER_TKIP		1
144 #define	IEEE80211_CIPHER_AES_OCB	2
145 #define	IEEE80211_CIPHER_AES_CCM	3
146 #define	IEEE80211_CIPHER_TKIPMIC	4	/* TKIP MIC capability */
147 #define	IEEE80211_CIPHER_CKIP		5
148 #define	IEEE80211_CIPHER_NONE		6	/* pseudo value */
149 #define	IEEE80211_CIPHER_AES_CCM_256	7
150 #define	IEEE80211_CIPHER_BIP_CMAC_128	8
151 #define	IEEE80211_CIPHER_BIP_CMAC_256	9
152 #define	IEEE80211_CIPHER_BIP_GMAC_128	10
153 #define	IEEE80211_CIPHER_BIP_GMAC_256	11
154 #define	IEEE80211_CIPHER_AES_GCM_128	12
155 #define	IEEE80211_CIPHER_AES_GCM_256	13
156 
157 #define	IEEE80211_CIPHER_LAST		13
158 
159 #define	IEEE80211_CIPHER_MAX		(IEEE80211_CIPHER_LAST+1)
160 
161 /* capability bits in ic_cryptocaps/iv_cryptocaps */
162 #define	IEEE80211_CRYPTO_WEP		(1<<IEEE80211_CIPHER_WEP)
163 #define	IEEE80211_CRYPTO_TKIP		(1<<IEEE80211_CIPHER_TKIP)
164 #define	IEEE80211_CRYPTO_AES_OCB	(1<<IEEE80211_CIPHER_AES_OCB)
165 #define	IEEE80211_CRYPTO_AES_CCM	(1<<IEEE80211_CIPHER_AES_CCM)
166 #define	IEEE80211_CRYPTO_TKIPMIC	(1<<IEEE80211_CIPHER_TKIPMIC)
167 #define	IEEE80211_CRYPTO_CKIP		(1<<IEEE80211_CIPHER_CKIP)
168 #define	IEEE80211_CRYPTO_AES_CCM_256	(1<<IEEE80211_CIPHER_AES_CCM_256)
169 #define	IEEE80211_CRYPTO_BIP_CMAC_128	(1<<IEEE80211_CIPHER_BIP_CMAC_128)
170 #define	IEEE80211_CRYPTO_BIP_CMAC_256	(1<<IEEE80211_CIPHER_BIP_CMAC_256)
171 #define	IEEE80211_CRYPTO_BIP_GMAC_128	(1<<IEEE80211_CIPHER_BIP_GMAC_128)
172 #define	IEEE80211_CRYPTO_BIP_GMAC_256	(1<<IEEE80211_CIPHER_BIP_GMAC_256)
173 #define	IEEE80211_CRYPTO_AES_GCM_128	(1<<IEEE80211_CIPHER_AES_GCM_128)
174 #define	IEEE80211_CRYPTO_AES_GCM_256	(1<<IEEE80211_CIPHER_AES_GCM_256)
175 
176 #define	IEEE80211_CRYPTO_BITS \
177 	"\20\1WEP\2TKIP\3AES\4AES_CCM\5TKIPMIC\6CKIP\10AES_CCM_256" \
178 	"\11BIP_CMAC_128\12BIP_CMAC_256\13BIP_GMAC_128\14BIP_CMAC_256" \
179 	"\15AES_GCM_128\16AES_GCM_256"
180 
181 #if defined(__KERNEL__) || defined(_KERNEL)
182 
183 struct ieee80211com;
184 struct ieee80211vap;
185 struct ieee80211_node;
186 struct mbuf;
187 
188 MALLOC_DECLARE(M_80211_CRYPTO);
189 
190 void	ieee80211_crypto_attach(struct ieee80211com *);
191 void	ieee80211_crypto_detach(struct ieee80211com *);
192 void	ieee80211_crypto_set_supported_software_ciphers(struct ieee80211com *,
193 	    uint32_t cipher_set);
194 void	ieee80211_crypto_set_supported_hardware_ciphers(struct ieee80211com *,
195 	    uint32_t cipher_set);
196 void	ieee80211_crypto_set_supported_driver_keymgmt(struct ieee80211com *,
197 	    uint32_t keymgmt_set);
198 void	ieee80211_crypto_vattach(struct ieee80211vap *);
199 void	ieee80211_crypto_vdetach(struct ieee80211vap *);
200 int	ieee80211_crypto_newkey(struct ieee80211vap *,
201 		int cipher, int flags, struct ieee80211_key *);
202 int	ieee80211_crypto_delkey(struct ieee80211vap *,
203 		struct ieee80211_key *);
204 int	ieee80211_crypto_setkey(struct ieee80211vap *, struct ieee80211_key *);
205 void	ieee80211_crypto_delglobalkeys(struct ieee80211vap *);
206 void	ieee80211_crypto_reload_keys(struct ieee80211com *);
207 void	ieee80211_crypto_set_deftxkey(struct ieee80211vap *,
208 	    ieee80211_keyix kid);
209 
210 /*
211  * Template for a supported cipher.  Ciphers register with the
212  * crypto code and are typically loaded as separate modules
213  * (the null cipher is always present).
214  * XXX may need refcnts
215  */
216 struct ieee80211_cipher {
217 	const char *ic_name;		/* printable name */
218 	u_int	ic_cipher;		/* IEEE80211_CIPHER_* */
219 	u_int	ic_header;		/* size of privacy header (bytes) */
220 	u_int	ic_trailer;		/* size of privacy trailer (bytes) */
221 	u_int	ic_miclen;		/* size of mic trailer (bytes) */
222 	void*	(*ic_attach)(struct ieee80211vap *, struct ieee80211_key *);
223 	void	(*ic_detach)(struct ieee80211_key *);
224 	int	(*ic_setkey)(struct ieee80211_key *);
225 	void	(*ic_setiv)(struct ieee80211_key *, uint8_t *);
226 	int	(*ic_encap)(struct ieee80211_key *, struct mbuf *);
227 	int	(*ic_decap)(struct ieee80211_key *, struct mbuf *, int);
228 	/*
229 	 * ic_enmic() and ic_demic() are currently only used by TKIP.
230 	 * Please see ieee80211_crypto_enmic() and ieee80211_crypto_demic()
231 	 * for more information.
232 	 */
233 	int	(*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);
234 	int	(*ic_demic)(struct ieee80211_key *, struct mbuf *, int);
235 };
236 extern	const struct ieee80211_cipher ieee80211_cipher_none;
237 
238 #define	IEEE80211_KEY_UNDEFINED(k) \
239 	((k)->wk_cipher == &ieee80211_cipher_none)
240 
241 void	ieee80211_crypto_register(const struct ieee80211_cipher *);
242 void	ieee80211_crypto_unregister(const struct ieee80211_cipher *);
243 int	ieee80211_crypto_available(u_int cipher);
244 
245 int	ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *,
246 	    const struct ieee80211_key *k);
247 uint8_t	ieee80211_crypto_get_keyid(struct ieee80211vap *vap,
248 		struct ieee80211_key *k);
249 struct ieee80211_key *ieee80211_crypto_get_txkey(struct ieee80211_node *,
250 		struct mbuf *);
251 struct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *,
252 		struct mbuf *);
253 int	ieee80211_crypto_decap(struct ieee80211_node *,
254 		struct mbuf *, int, struct ieee80211_key **);
255 int ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
256 		struct mbuf *, int);
257 /**
258  * @brief Add any pre-fragmentation MIC to an MSDU.
259  *
260  * This is called before 802.11 fragmentation.  Crypto types that implement
261  * a MIC/ICV check per MSDU will not implement this function.
262  *
263  * As an example, TKIP implements a Michael MIC check over the entire
264  * unencrypted MSDU before fragmenting it into MPDUs and passing each
265  * MPDU to be separately encrypted with their own MIC/ICV.
266  *
267  * Please see 802.11-2020 12.5.2.1.2 (TKIP cryptographic encapsulation)
268  * for more information.
269  *
270  * @param vap	the current VAP
271  * @param k	the current key
272  * @param m	the mbuf representing the MSDU
273  * @param f	set to 1 to force a MSDU MIC check, even if HW encrypted
274  * @returns	0 if error / MIC encap failed, 1 if OK
275  */
276 static __inline int
277 ieee80211_crypto_enmic(struct ieee80211vap *vap,
278 	struct ieee80211_key *k, struct mbuf *m, int force)
279 {
280 	const struct ieee80211_cipher *cip = k->wk_cipher;
281 	return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);
282 }
283 
284 /*
285  * Reset key state to an unused state.  The crypto
286  * key allocation mechanism insures other state (e.g.
287  * key data) is properly setup before a key is used.
288  */
289 static __inline void
290 ieee80211_crypto_resetkey(struct ieee80211vap *vap,
291 	struct ieee80211_key *k, ieee80211_keyix ix)
292 {
293 	k->wk_cipher = &ieee80211_cipher_none;
294 	k->wk_private = k->wk_cipher->ic_attach(vap, k);
295 	k->wk_keyix = k->wk_rxkeyix = ix;
296 	k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
297 }
298 
299 /*
300  * Crypt-related notification methods.
301  */
302 void	ieee80211_notify_replay_failure(struct ieee80211vap *,
303 		const struct ieee80211_frame *, const struct ieee80211_key *,
304 		uint64_t rsc, int tid);
305 void	ieee80211_notify_michael_failure(struct ieee80211vap *,
306 		const struct ieee80211_frame *, ieee80211_keyix keyix);
307 
308 /* AAD assembly for CCMP/GCMP. */
309 uint16_t	ieee80211_crypto_init_aad(const struct ieee80211_frame *,
310 		uint8_t *, int);
311 
312 /**
313  * @brief Return the key data.
314  *
315  * This returns a pointer to the key data.  Note it does not
316  * guarantee the TX/RX MIC will be immediately after the key.
317  * Callers must use ieee80211_crypto_get_key_txmic_data()
318  * and ieee80211_crypto_get_key_rxmic_data() for that.
319  *
320  * Note: there's no locking; this needs to be called in
321  * a situation where the ieee80211_key won't disappear.
322  *
323  * @param k ieee80211_key
324  * @returns NULL if no key data is available, or a pointer
325  *  to the key data.
326  */
327 static inline const uint8_t *
328 ieee80211_crypto_get_key_data(const struct ieee80211_key *k)
329 {
330 	return (k->wk_key);
331 }
332 
333 /**
334  * @brief Return the key length in bytes.
335  *
336  * This doesn't include any TX/RX MIC (eg from TKIP).
337  *
338  * Note: there's no locking; this needs to be called in
339  * a situation where the ieee80211_key won't disappear.
340  *
341  * @param k ieee80211_key
342  * @returns the key length (without any MIC) in bytes
343  */
344 static inline const uint16_t
345 ieee80211_crypto_get_key_len(const struct ieee80211_key *k)
346 {
347 	return (k->wk_keylen);
348 }
349 
350 /**
351  * @brief Return the TX MIC data.
352  *
353  * This returns a pointer to the TX MIC data.
354  *
355  * Note: there's no locking; this needs to be called in
356  * a situation where the ieee80211_key won't disappear.
357  *
358  * @param k ieee80211_key
359  * @returns NULL if no key data is available, or a pointer
360  *  to the TX MIC data.
361  */
362 static inline const uint8_t *
363 ieee80211_crypto_get_key_txmic_data(const struct ieee80211_key *k)
364 {
365 	return (k->wk_txmic);
366 }
367 
368 /**
369  * @brief Return the TX MIC length in bytes.
370  *
371  * Note: there's no locking; this needs to be called in
372  * a situation where the ieee80211_key won't disappear.
373  *
374  * @param k ieee80211_key
375  * @returns the TX MIC length in bytes
376  */
377 static inline const uint16_t
378 ieee80211_crypto_get_key_txmic_len(const struct ieee80211_key *k)
379 {
380 	return (k->wk_cipher->ic_miclen);
381 }
382 
383 /**
384  * @brief Return the RX MIC data.
385  *
386  * This returns a pointer to the RX MIC data.
387  *
388  * Note: there's no locking; this needs to be called in
389  * a situation where the ieee80211_key won't disappear.
390  *
391  * @param k ieee80211_key
392  * @returns NULL if no key data is available, or a pointer
393  *  to the RX MIC data.
394  */
395 static inline const uint8_t *
396 ieee80211_crypto_get_key_rxmic_data(const struct ieee80211_key *k)
397 {
398 	return (k->wk_rxmic);
399 }
400 
401 /**
402  * @brief Return the RX MIC length in bytes.
403  *
404  * Note: there's no locking; this needs to be called in
405  * a situation where the ieee80211_key won't disappear.
406  *
407  * @param k ieee80211_key
408  * @returns the RX MIC length in bytes
409  */
410 static inline const uint16_t
411 ieee80211_crypto_get_key_rxmic_len(const struct ieee80211_key *k)
412 {
413 	return (k->wk_cipher->ic_miclen);
414 }
415 
416 #endif /* defined(__KERNEL__) || defined(_KERNEL) */
417 #endif /* _NET80211_IEEE80211_CRYPTO_H_ */
418