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