ieee80211_crypto.h revision 178354
1/*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2008 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/net80211/ieee80211_crypto.h 178354 2008-04-20 20:35:46Z sam $
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 */
40struct ieee80211_wepkey {
41	u_int		wk_len;		/* key length in bytes */
42	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE];
43};
44
45struct 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
54struct 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 */
72typedef uint16_t ieee80211_keyix;	/* h/w key index */
73
74struct ieee80211_key {
75	uint8_t		wk_keylen;	/* key length in bytes */
76	uint8_t		wk_pad;
77	uint16_t	wk_flags;
78#define	IEEE80211_KEY_XMIT	0x01	/* key used for xmit */
79#define	IEEE80211_KEY_RECV	0x02	/* key used for recv */
80#define	IEEE80211_KEY_GROUP	0x04	/* key used for WPA group operation */
81#define	IEEE80211_KEY_SWCRYPT	0x10	/* host-based encrypt/decrypt */
82#define	IEEE80211_KEY_SWMIC	0x20	/* host-based enmic/demic */
83	ieee80211_keyix	wk_keyix;	/* h/w key index */
84	ieee80211_keyix	wk_rxkeyix;	/* optional h/w rx key index */
85	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
86#define	wk_txmic	wk_key+IEEE80211_KEYBUF_SIZE+0	/* XXX can't () right */
87#define	wk_rxmic	wk_key+IEEE80211_KEYBUF_SIZE+8	/* XXX can't () right */
88					/* key receive sequence counter */
89	uint64_t	wk_keyrsc[IEEE80211_TID_SIZE];
90	uint64_t	wk_keytsc;	/* key transmit sequence counter */
91	const struct ieee80211_cipher *wk_cipher;
92	void		*wk_private;	/* private cipher state */
93};
94#define	IEEE80211_KEY_COMMON 		/* common flags passed in by apps */\
95	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP)
96
97#define	IEEE80211_KEYIX_NONE	((ieee80211_keyix) -1)
98
99/*
100 * NB: these values are ordered carefully; there are lots of
101 * of implications in any reordering.  Beware that 4 is used
102 * only to indicate h/w TKIP MIC support in driver capabilities;
103 * there is no separate cipher support (it's rolled into the
104 * TKIP cipher support).
105 */
106#define	IEEE80211_CIPHER_WEP		0
107#define	IEEE80211_CIPHER_TKIP		1
108#define	IEEE80211_CIPHER_AES_OCB	2
109#define	IEEE80211_CIPHER_AES_CCM	3
110#define	IEEE80211_CIPHER_TKIPMIC	4	/* TKIP MIC capability */
111#define	IEEE80211_CIPHER_CKIP		5
112#define	IEEE80211_CIPHER_NONE		6	/* pseudo value */
113
114#define	IEEE80211_CIPHER_MAX		(IEEE80211_CIPHER_NONE+1)
115
116/* capability bits in ic_cryptocaps/iv_cryptocaps */
117#define	IEEE80211_CRYPTO_WEP		(1<<IEEE80211_CIPHER_WEP)
118#define	IEEE80211_CRYPTO_TKIP		(1<<IEEE80211_CIPHER_TKIP)
119#define	IEEE80211_CRYPTO_AES_OCB	(1<<IEEE80211_CIPHER_AES_OCB)
120#define	IEEE80211_CRYPTO_AES_CCM	(1<<IEEE80211_CIPHER_AES_CCM)
121#define	IEEE80211_CRYPTO_TKIPMIC	(1<<IEEE80211_CIPHER_TKIPMIC)
122#define	IEEE80211_CRYPTO_CKIP		(1<<IEEE80211_CIPHER_CKIP)
123
124#if defined(__KERNEL__) || defined(_KERNEL)
125
126struct ieee80211com;
127struct ieee80211vap;
128struct ieee80211_node;
129struct mbuf;
130
131MALLOC_DECLARE(M_80211_CRYPTO);
132
133void	ieee80211_crypto_attach(struct ieee80211com *);
134void	ieee80211_crypto_detach(struct ieee80211com *);
135void	ieee80211_crypto_vattach(struct ieee80211vap *);
136void	ieee80211_crypto_vdetach(struct ieee80211vap *);
137int	ieee80211_crypto_newkey(struct ieee80211vap *,
138		int cipher, int flags, struct ieee80211_key *);
139int	ieee80211_crypto_delkey(struct ieee80211vap *,
140		struct ieee80211_key *);
141int	ieee80211_crypto_setkey(struct ieee80211vap *,
142		struct ieee80211_key *,
143		const uint8_t macaddr[IEEE80211_ADDR_LEN]);
144void	ieee80211_crypto_delglobalkeys(struct ieee80211vap *);
145
146/*
147 * Template for a supported cipher.  Ciphers register with the
148 * crypto code and are typically loaded as separate modules
149 * (the null cipher is always present).
150 * XXX may need refcnts
151 */
152struct ieee80211_cipher {
153	const char *ic_name;		/* printable name */
154	u_int	ic_cipher;		/* IEEE80211_CIPHER_* */
155	u_int	ic_header;		/* size of privacy header (bytes) */
156	u_int	ic_trailer;		/* size of privacy trailer (bytes) */
157	u_int	ic_miclen;		/* size of mic trailer (bytes) */
158	void*	(*ic_attach)(struct ieee80211vap *, struct ieee80211_key *);
159	void	(*ic_detach)(struct ieee80211_key *);
160	int	(*ic_setkey)(struct ieee80211_key *);
161	int	(*ic_encap)(struct ieee80211_key *, struct mbuf *,
162			uint8_t keyid);
163	int	(*ic_decap)(struct ieee80211_key *, struct mbuf *, int);
164	int	(*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);
165	int	(*ic_demic)(struct ieee80211_key *, struct mbuf *, int);
166};
167extern	const struct ieee80211_cipher ieee80211_cipher_none;
168
169#define	IEEE80211_KEY_UNDEFINED(k) \
170	((k)->wk_cipher == &ieee80211_cipher_none)
171
172void	ieee80211_crypto_register(const struct ieee80211_cipher *);
173void	ieee80211_crypto_unregister(const struct ieee80211_cipher *);
174int	ieee80211_crypto_available(u_int cipher);
175
176struct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *,
177		struct mbuf *);
178struct ieee80211_key *ieee80211_crypto_decap(struct ieee80211_node *,
179		struct mbuf *, int);
180
181/*
182 * Check and remove any MIC.
183 */
184static __inline int
185ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
186	struct mbuf *m, int force)
187{
188	const struct ieee80211_cipher *cip = k->wk_cipher;
189	return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
190}
191
192/*
193 * Add any MIC.
194 */
195static __inline int
196ieee80211_crypto_enmic(struct ieee80211vap *vap,
197	struct ieee80211_key *k, struct mbuf *m, int force)
198{
199	const struct ieee80211_cipher *cip = k->wk_cipher;
200	return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);
201}
202
203/*
204 * Reset key state to an unused state.  The crypto
205 * key allocation mechanism insures other state (e.g.
206 * key data) is properly setup before a key is used.
207 */
208static __inline void
209ieee80211_crypto_resetkey(struct ieee80211vap *vap,
210	struct ieee80211_key *k, ieee80211_keyix ix)
211{
212	k->wk_cipher = &ieee80211_cipher_none;;
213	k->wk_private = k->wk_cipher->ic_attach(vap, k);
214	k->wk_keyix = k->wk_rxkeyix = ix;
215	k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
216}
217
218/*
219 * Crypt-related notification methods.
220 */
221void	ieee80211_notify_replay_failure(struct ieee80211vap *,
222		const struct ieee80211_frame *, const struct ieee80211_key *,
223		uint64_t rsc);
224void	ieee80211_notify_michael_failure(struct ieee80211vap *,
225		const struct ieee80211_frame *, u_int keyix);
226#endif /* defined(__KERNEL__) || defined(_KERNEL) */
227#endif /* _NET80211_IEEE80211_CRYPTO_H_ */
228