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 /* TODO: deprecate direct access to wk_key, wk_txmic, wk_rxmic */
98 uint8_t wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
99 #define wk_txmic wk_key+IEEE80211_KEYBUF_SIZE+0 /* XXX can't () right */
100 #define wk_rxmic wk_key+IEEE80211_KEYBUF_SIZE+8 /* XXX can't () right */
101 /* key receive sequence counter */
102 uint64_t wk_keyrsc[IEEE80211_TID_SIZE];
103 uint64_t wk_keytsc; /* key transmit sequence counter */
104 const struct ieee80211_cipher *wk_cipher;
105 void *wk_private; /* private cipher state */
106 uint8_t wk_macaddr[IEEE80211_ADDR_LEN];
107 };
108 #define IEEE80211_KEY_COMMON /* common flags passed in by apps */\
109 (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP | \
110 IEEE80211_KEY_NOREPLAY)
111
112 #define IEEE80211_KEY_SWCRYPT \
113 (IEEE80211_KEY_SWENCRYPT | IEEE80211_KEY_SWDECRYPT)
114 #define IEEE80211_KEY_SWMIC (IEEE80211_KEY_SWENMIC | IEEE80211_KEY_SWDEMIC)
115
116 #define IEEE80211_KEY_DEVICE /* flags owned by device driver */\
117 (IEEE80211_KEY_DEVKEY|IEEE80211_KEY_CIPHER0|IEEE80211_KEY_CIPHER1| \
118 IEEE80211_KEY_SWCRYPT|IEEE80211_KEY_SWMIC|IEEE80211_KEY_NOIV | \
119 IEEE80211_KEY_NOIVMGT|IEEE80211_KEY_NOMIC|IEEE80211_KEY_NOMICMGT)
120
121 #define IEEE80211_KEY_BITS \
122 "\20\1XMIT\2RECV\3GROUP\4NOREPLAY\5SWENCRYPT\6SWDECRYPT\7SWENMIC\10SWDEMIC" \
123 "\11DEVKEY\12CIPHER0\13CIPHER1\14NOIV\15NOIVMGT\16NOMIC\17NOMICMGT"
124
125 #define IEEE80211_KEYIX_NONE ((ieee80211_keyix) -1)
126
127 /*
128 * NB: these values are ordered carefully; there are lots of
129 * of implications in any reordering. Beware that 4 is used
130 * only to indicate h/w TKIP MIC support in driver capabilities;
131 * there is no separate cipher support (it's rolled into the
132 * TKIP cipher support).
133 */
134 #define IEEE80211_CIPHER_WEP 0
135 #define IEEE80211_CIPHER_TKIP 1
136 #define IEEE80211_CIPHER_AES_OCB 2
137 #define IEEE80211_CIPHER_AES_CCM 3
138 #define IEEE80211_CIPHER_TKIPMIC 4 /* TKIP MIC capability */
139 #define IEEE80211_CIPHER_CKIP 5
140 #define IEEE80211_CIPHER_NONE 6 /* pseudo value */
141 #define IEEE80211_CIPHER_AES_CCM_256 7
142 #define IEEE80211_CIPHER_BIP_CMAC_128 8
143 #define IEEE80211_CIPHER_BIP_CMAC_256 9
144 #define IEEE80211_CIPHER_BIP_GMAC_128 10
145 #define IEEE80211_CIPHER_BIP_GMAC_256 11
146 #define IEEE80211_CIPHER_AES_GCM_128 12
147 #define IEEE80211_CIPHER_AES_GCM_256 13
148
149 #define IEEE80211_CIPHER_LAST 13
150
151 #define IEEE80211_CIPHER_MAX (IEEE80211_CIPHER_LAST+1)
152
153 /* capability bits in ic_cryptocaps/iv_cryptocaps */
154 #define IEEE80211_CRYPTO_WEP (1<<IEEE80211_CIPHER_WEP)
155 #define IEEE80211_CRYPTO_TKIP (1<<IEEE80211_CIPHER_TKIP)
156 #define IEEE80211_CRYPTO_AES_OCB (1<<IEEE80211_CIPHER_AES_OCB)
157 #define IEEE80211_CRYPTO_AES_CCM (1<<IEEE80211_CIPHER_AES_CCM)
158 #define IEEE80211_CRYPTO_TKIPMIC (1<<IEEE80211_CIPHER_TKIPMIC)
159 #define IEEE80211_CRYPTO_CKIP (1<<IEEE80211_CIPHER_CKIP)
160 #define IEEE80211_CRYPTO_AES_CCM_256 (1<<IEEE80211_CIPHER_AES_CCM_256)
161 #define IEEE80211_CRYPTO_BIP_CMAC_128 (1<<IEEE80211_CIPHER_BIP_CMAC_128)
162 #define IEEE80211_CRYPTO_BIP_CMAC_256 (1<<IEEE80211_CIPHER_BIP_CMAC_256)
163 #define IEEE80211_CRYPTO_BIP_GMAC_128 (1<<IEEE80211_CIPHER_BIP_GMAC_128)
164 #define IEEE80211_CRYPTO_BIP_GMAC_256 (1<<IEEE80211_CIPHER_BIP_GMAC_256)
165 #define IEEE80211_CRYPTO_AES_GCM_128 (1<<IEEE80211_CIPHER_AES_GCM_128)
166 #define IEEE80211_CRYPTO_AES_GCM_256 (1<<IEEE80211_CIPHER_AES_GCM_256)
167
168 #define IEEE80211_CRYPTO_BITS \
169 "\20\1WEP\2TKIP\3AES\4AES_CCM\5TKIPMIC\6CKIP\10AES_CCM_256" \
170 "\11BIP_CMAC_128\12BIP_CMAC_256\13BIP_GMAC_128\14BIP_CMAC_256" \
171 "\15AES_GCM_128\16AES_GCM_256"
172
173 #if defined(__KERNEL__) || defined(_KERNEL)
174
175 struct ieee80211com;
176 struct ieee80211vap;
177 struct ieee80211_node;
178 struct mbuf;
179
180 MALLOC_DECLARE(M_80211_CRYPTO);
181
182 void ieee80211_crypto_attach(struct ieee80211com *);
183 void ieee80211_crypto_detach(struct ieee80211com *);
184 void ieee80211_crypto_set_supported_software_ciphers(struct ieee80211com *,
185 uint32_t cipher_set);
186 void ieee80211_crypto_set_supported_hardware_ciphers(struct ieee80211com *,
187 uint32_t cipher_set);
188 void ieee80211_crypto_set_supported_driver_keymgmt(struct ieee80211com *,
189 uint32_t keymgmt_set);
190 void ieee80211_crypto_vattach(struct ieee80211vap *);
191 void ieee80211_crypto_vdetach(struct ieee80211vap *);
192 int ieee80211_crypto_newkey(struct ieee80211vap *,
193 int cipher, int flags, struct ieee80211_key *);
194 int ieee80211_crypto_delkey(struct ieee80211vap *,
195 struct ieee80211_key *);
196 int ieee80211_crypto_setkey(struct ieee80211vap *, struct ieee80211_key *);
197 void ieee80211_crypto_delglobalkeys(struct ieee80211vap *);
198 void ieee80211_crypto_reload_keys(struct ieee80211com *);
199 void ieee80211_crypto_set_deftxkey(struct ieee80211vap *,
200 ieee80211_keyix kid);
201
202 /*
203 * Template for a supported cipher. Ciphers register with the
204 * crypto code and are typically loaded as separate modules
205 * (the null cipher is always present).
206 * XXX may need refcnts
207 */
208 struct ieee80211_cipher {
209 const char *ic_name; /* printable name */
210 u_int ic_cipher; /* IEEE80211_CIPHER_* */
211 u_int ic_header; /* size of privacy header (bytes) */
212 u_int ic_trailer; /* size of privacy trailer (bytes) */
213 u_int ic_miclen; /* size of mic trailer (bytes) */
214 void* (*ic_attach)(struct ieee80211vap *, struct ieee80211_key *);
215 void (*ic_detach)(struct ieee80211_key *);
216 int (*ic_setkey)(struct ieee80211_key *);
217 void (*ic_setiv)(struct ieee80211_key *, uint8_t *);
218 int (*ic_encap)(struct ieee80211_key *, struct mbuf *);
219 int (*ic_decap)(struct ieee80211_key *, struct mbuf *, int);
220 /*
221 * ic_enmic() and ic_demic() are currently only used by TKIP.
222 * Please see ieee80211_crypto_enmic() and ieee80211_crypto_demic()
223 * for more information.
224 */
225 int (*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);
226 int (*ic_demic)(struct ieee80211_key *, struct mbuf *, int);
227 };
228 extern const struct ieee80211_cipher ieee80211_cipher_none;
229
230 #define IEEE80211_KEY_UNDEFINED(k) \
231 ((k)->wk_cipher == &ieee80211_cipher_none)
232
233 void ieee80211_crypto_register(const struct ieee80211_cipher *);
234 void ieee80211_crypto_unregister(const struct ieee80211_cipher *);
235 int ieee80211_crypto_available(u_int cipher);
236
237 int ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *,
238 const struct ieee80211_key *k);
239 uint8_t ieee80211_crypto_get_keyid(struct ieee80211vap *vap,
240 struct ieee80211_key *k);
241 struct ieee80211_key *ieee80211_crypto_get_txkey(struct ieee80211_node *,
242 struct mbuf *);
243 struct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *,
244 struct mbuf *);
245 int ieee80211_crypto_decap(struct ieee80211_node *,
246 struct mbuf *, int, struct ieee80211_key **);
247 int ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
248 struct mbuf *, int);
249 /**
250 * @brief Add any pre-fragmentation MIC to an MSDU.
251 *
252 * This is called before 802.11 fragmentation. Crypto types that implement
253 * a MIC/ICV check per MSDU will not implement this function.
254 *
255 * As an example, TKIP implements a Michael MIC check over the entire
256 * unencrypted MSDU before fragmenting it into MPDUs and passing each
257 * MPDU to be separately encrypted with their own MIC/ICV.
258 *
259 * Please see 802.11-2020 12.5.2.1.2 (TKIP cryptographic encapsulation)
260 * for more information.
261 *
262 * @param vap the current VAP
263 * @param k the current key
264 * @param m the mbuf representing the MSDU
265 * @param f set to 1 to force a MSDU MIC check, even if HW encrypted
266 * @returns 0 if error / MIC encap failed, 1 if OK
267 */
268 static __inline int
ieee80211_crypto_enmic(struct ieee80211vap * vap,struct ieee80211_key * k,struct mbuf * m,int force)269 ieee80211_crypto_enmic(struct ieee80211vap *vap,
270 struct ieee80211_key *k, struct mbuf *m, int force)
271 {
272 const struct ieee80211_cipher *cip = k->wk_cipher;
273 return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);
274 }
275
276 /*
277 * Reset key state to an unused state. The crypto
278 * key allocation mechanism insures other state (e.g.
279 * key data) is properly setup before a key is used.
280 */
281 static __inline void
ieee80211_crypto_resetkey(struct ieee80211vap * vap,struct ieee80211_key * k,ieee80211_keyix ix)282 ieee80211_crypto_resetkey(struct ieee80211vap *vap,
283 struct ieee80211_key *k, ieee80211_keyix ix)
284 {
285 k->wk_cipher = &ieee80211_cipher_none;
286 k->wk_private = k->wk_cipher->ic_attach(vap, k);
287 k->wk_keyix = k->wk_rxkeyix = ix;
288 k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
289 }
290
291 /*
292 * Crypt-related notification methods.
293 */
294 void ieee80211_notify_replay_failure(struct ieee80211vap *,
295 const struct ieee80211_frame *, const struct ieee80211_key *,
296 uint64_t rsc, int tid);
297 void ieee80211_notify_michael_failure(struct ieee80211vap *,
298 const struct ieee80211_frame *, ieee80211_keyix keyix);
299
300 /* AAD assembly for CCMP/GCMP. */
301 uint16_t ieee80211_crypto_init_aad(const struct ieee80211_frame *,
302 uint8_t *, int);
303
304 /**
305 * @brief Return the key data.
306 *
307 * This returns a pointer to the key data. Note it does not
308 * guarantee the TX/RX MIC will be immediately after the key.
309 * Callers must use ieee80211_crypto_get_key_txmic_data()
310 * and ieee80211_crypto_get_key_rxmic_data() for that.
311 *
312 * Note: there's no locking; this needs to be called in
313 * a situation where the ieee80211_key won't disappear.
314 *
315 * @param k ieee80211_key
316 * @returns NULL if no key data is available, or a pointer
317 * to the key data.
318 */
319 static inline const uint8_t *
ieee80211_crypto_get_key_data(const struct ieee80211_key * k)320 ieee80211_crypto_get_key_data(const struct ieee80211_key *k)
321 {
322 return (k->wk_key);
323 }
324
325 /**
326 * @brief Return the key length in bytes.
327 *
328 * This doesn't include any TX/RX MIC (eg from TKIP).
329 *
330 * Note: there's no locking; this needs to be called in
331 * a situation where the ieee80211_key won't disappear.
332 *
333 * @param k ieee80211_key
334 * @returns the key length (without any MIC) in bytes
335 */
336 static inline const uint16_t
ieee80211_crypto_get_key_len(const struct ieee80211_key * k)337 ieee80211_crypto_get_key_len(const struct ieee80211_key *k)
338 {
339 return (k->wk_keylen);
340 }
341
342 /**
343 * @brief Return the TX MIC data.
344 *
345 * This returns a pointer to the TX MIC data.
346 *
347 * Note: there's no locking; this needs to be called in
348 * a situation where the ieee80211_key won't disappear.
349 *
350 * @param k ieee80211_key
351 * @returns NULL if no key data is available, or a pointer
352 * to the TX MIC data.
353 */
354 static inline const uint8_t *
ieee80211_crypto_get_key_txmic_data(const struct ieee80211_key * k)355 ieee80211_crypto_get_key_txmic_data(const struct ieee80211_key *k)
356 {
357 return (k->wk_txmic);
358 }
359
360 /**
361 * @brief Return the TX MIC length in bytes.
362 *
363 * Note: there's no locking; this needs to be called in
364 * a situation where the ieee80211_key won't disappear.
365 *
366 * @param k ieee80211_key
367 * @returns the TX MIC length in bytes
368 */
369 static inline const uint16_t
ieee80211_crypto_get_key_txmic_len(const struct ieee80211_key * k)370 ieee80211_crypto_get_key_txmic_len(const struct ieee80211_key *k)
371 {
372 return (k->wk_cipher->ic_miclen);
373 }
374
375 /**
376 * @brief Return the RX MIC data.
377 *
378 * This returns a pointer to the RX MIC data.
379 *
380 * Note: there's no locking; this needs to be called in
381 * a situation where the ieee80211_key won't disappear.
382 *
383 * @param k ieee80211_key
384 * @returns NULL if no key data is available, or a pointer
385 * to the RX MIC data.
386 */
387 static inline const uint8_t *
ieee80211_crypto_get_key_rxmic_data(const struct ieee80211_key * k)388 ieee80211_crypto_get_key_rxmic_data(const struct ieee80211_key *k)
389 {
390 return (k->wk_rxmic);
391 }
392
393 /**
394 * @brief Return the RX MIC length in bytes.
395 *
396 * Note: there's no locking; this needs to be called in
397 * a situation where the ieee80211_key won't disappear.
398 *
399 * @param k ieee80211_key
400 * @returns the RX MIC length in bytes
401 */
402 static inline const uint16_t
ieee80211_crypto_get_key_rxmic_len(const struct ieee80211_key * k)403 ieee80211_crypto_get_key_rxmic_len(const struct ieee80211_key *k)
404 {
405 return (k->wk_cipher->ic_miclen);
406 }
407
408 #endif /* defined(__KERNEL__) || defined(_KERNEL) */
409 #endif /* _NET80211_IEEE80211_CRYPTO_H_ */
410