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