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