ieee80211_crypto.h revision 193541
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3178354Ssam * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116742Ssam *    notice, this list of conditions and the following disclaimer.
11116742Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116742Ssam *    notice, this list of conditions and the following disclaimer in the
13116742Ssam *    documentation and/or other materials provided with the distribution.
14116742Ssam *
15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25116904Ssam *
26116742Ssam * $FreeBSD: head/sys/net80211/ieee80211_crypto.h 193541 2009-06-05 23:10:30Z sam $
27116742Ssam */
28116742Ssam#ifndef _NET80211_IEEE80211_CRYPTO_H_
29116742Ssam#define _NET80211_IEEE80211_CRYPTO_H_
30116742Ssam
31116742Ssam/*
32116742Ssam * 802.11 protocol crypto-related definitions.
33116742Ssam */
34116742Ssam#define	IEEE80211_KEYBUF_SIZE	16
35138568Ssam#define	IEEE80211_MICBUF_SIZE	(8+8)	/* space for both tx+rx keys */
36116742Ssam
37138568Ssam/*
38138568Ssam * Old WEP-style key.  Deprecated.
39138568Ssam */
40116742Ssamstruct ieee80211_wepkey {
41138568Ssam	u_int		wk_len;		/* key length in bytes */
42170530Ssam	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE];
43116742Ssam};
44116742Ssam
45178354Ssamstruct ieee80211_rsnparms {
46178354Ssam	uint8_t		rsn_mcastcipher;	/* mcast/group cipher */
47178354Ssam	uint8_t		rsn_mcastkeylen;	/* mcast key length */
48178354Ssam	uint8_t		rsn_ucastcipher;	/* selected unicast cipher */
49178354Ssam	uint8_t		rsn_ucastkeylen;	/* unicast key length */
50178354Ssam	uint8_t		rsn_keymgmt;		/* selected key mgmt algo */
51178354Ssam	uint16_t	rsn_caps;		/* capabilities */
52178354Ssam};
53178354Ssam
54138568Ssamstruct ieee80211_cipher;
55138568Ssam
56138568Ssam/*
57138568Ssam * Crypto key state.  There is sufficient room for all supported
58138568Ssam * ciphers (see below).  The underlying ciphers are handled
59138568Ssam * separately through loadable cipher modules that register with
60138568Ssam * the generic crypto support.  A key has a reference to an instance
61138568Ssam * of the cipher; any per-key state is hung off wk_private by the
62138568Ssam * cipher when it is attached.  Ciphers are automatically called
63138568Ssam * to detach and cleanup any such state when the key is deleted.
64138568Ssam *
65138568Ssam * The generic crypto support handles encap/decap of cipher-related
66138568Ssam * frame contents for both hardware- and software-based implementations.
67138568Ssam * A key requiring software crypto support is automatically flagged and
68138568Ssam * the cipher is expected to honor this and do the necessary work.
69138568Ssam * Ciphers such as TKIP may also support mixed hardware/software
70138568Ssam * encrypt/decrypt and MIC processing.
71138568Ssam */
72170530Ssamtypedef uint16_t ieee80211_keyix;	/* h/w key index */
73148863Ssam
74138568Ssamstruct ieee80211_key {
75170530Ssam	uint8_t		wk_keylen;	/* key length in bytes */
76170530Ssam	uint8_t		wk_pad;
77170530Ssam	uint16_t	wk_flags;
78183248Ssam#define	IEEE80211_KEY_XMIT	0x0001	/* key used for xmit */
79183248Ssam#define	IEEE80211_KEY_RECV	0x0002	/* key used for recv */
80183248Ssam#define	IEEE80211_KEY_GROUP	0x0004	/* key used for WPA group operation */
81183248Ssam#define	IEEE80211_KEY_SWENCRYPT	0x0010	/* host-based encrypt */
82183248Ssam#define	IEEE80211_KEY_SWDECRYPT	0x0020	/* host-based decrypt */
83183248Ssam#define	IEEE80211_KEY_SWENMIC	0x0040	/* host-based enmic */
84183248Ssam#define	IEEE80211_KEY_SWDEMIC	0x0080	/* host-based demic */
85183248Ssam#define	IEEE80211_KEY_DEVKEY	0x0100	/* device key request completed */
86183248Ssam#define	IEEE80211_KEY_CIPHER0	0x1000	/* cipher-specific action 0 */
87183248Ssam#define	IEEE80211_KEY_CIPHER1	0x2000	/* cipher-specific action 1 */
88148863Ssam	ieee80211_keyix	wk_keyix;	/* h/w key index */
89148863Ssam	ieee80211_keyix	wk_rxkeyix;	/* optional h/w rx key index */
90170530Ssam	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
91138568Ssam#define	wk_txmic	wk_key+IEEE80211_KEYBUF_SIZE+0	/* XXX can't () right */
92138568Ssam#define	wk_rxmic	wk_key+IEEE80211_KEYBUF_SIZE+8	/* XXX can't () right */
93178354Ssam					/* key receive sequence counter */
94178354Ssam	uint64_t	wk_keyrsc[IEEE80211_TID_SIZE];
95170530Ssam	uint64_t	wk_keytsc;	/* key transmit sequence counter */
96138568Ssam	const struct ieee80211_cipher *wk_cipher;
97138568Ssam	void		*wk_private;	/* private cipher state */
98179395Ssam	uint8_t		wk_macaddr[IEEE80211_ADDR_LEN];
99138568Ssam};
100144960Ssam#define	IEEE80211_KEY_COMMON 		/* common flags passed in by apps */\
101144960Ssam	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP)
102138568Ssam
103179394Ssam#define	IEEE80211_KEY_SWCRYPT \
104179394Ssam	(IEEE80211_KEY_SWENCRYPT | IEEE80211_KEY_SWDECRYPT)
105179394Ssam#define	IEEE80211_KEY_SWMIC	(IEEE80211_KEY_SWENMIC | IEEE80211_KEY_SWDEMIC)
106179394Ssam
107188170Ssam#define	IEEE80211_KEY_BITS \
108188170Ssam	"\20\1XMIT\2RECV\3GROUP\4SWENCRYPT\5SWDECRYPT\6SWENMIC\7SWDEMIC" \
109188170Ssam	"\10DEVKEY\11CIPHER0\12CIPHER1"
110188170Ssam
111178354Ssam#define	IEEE80211_KEYIX_NONE	((ieee80211_keyix) -1)
112178354Ssam
113138568Ssam/*
114138568Ssam * NB: these values are ordered carefully; there are lots of
115178354Ssam * of implications in any reordering.  Beware that 4 is used
116178354Ssam * only to indicate h/w TKIP MIC support in driver capabilities;
117178354Ssam * there is no separate cipher support (it's rolled into the
118178354Ssam * TKIP cipher support).
119138568Ssam */
120138568Ssam#define	IEEE80211_CIPHER_WEP		0
121138568Ssam#define	IEEE80211_CIPHER_TKIP		1
122138568Ssam#define	IEEE80211_CIPHER_AES_OCB	2
123138568Ssam#define	IEEE80211_CIPHER_AES_CCM	3
124178354Ssam#define	IEEE80211_CIPHER_TKIPMIC	4	/* TKIP MIC capability */
125138568Ssam#define	IEEE80211_CIPHER_CKIP		5
126138568Ssam#define	IEEE80211_CIPHER_NONE		6	/* pseudo value */
127138568Ssam
128138568Ssam#define	IEEE80211_CIPHER_MAX		(IEEE80211_CIPHER_NONE+1)
129138568Ssam
130178354Ssam/* capability bits in ic_cryptocaps/iv_cryptocaps */
131178354Ssam#define	IEEE80211_CRYPTO_WEP		(1<<IEEE80211_CIPHER_WEP)
132178354Ssam#define	IEEE80211_CRYPTO_TKIP		(1<<IEEE80211_CIPHER_TKIP)
133178354Ssam#define	IEEE80211_CRYPTO_AES_OCB	(1<<IEEE80211_CIPHER_AES_OCB)
134178354Ssam#define	IEEE80211_CRYPTO_AES_CCM	(1<<IEEE80211_CIPHER_AES_CCM)
135178354Ssam#define	IEEE80211_CRYPTO_TKIPMIC	(1<<IEEE80211_CIPHER_TKIPMIC)
136178354Ssam#define	IEEE80211_CRYPTO_CKIP		(1<<IEEE80211_CIPHER_CKIP)
137138568Ssam
138187797Ssam#define	IEEE80211_CRYPTO_BITS \
139187797Ssam	"\20\1WEP\2TKIP\3AES\4AES_CCM\5TKIPMIC\6CKIP"
140187797Ssam
141138568Ssam#if defined(__KERNEL__) || defined(_KERNEL)
142138568Ssam
143138568Ssamstruct ieee80211com;
144178354Ssamstruct ieee80211vap;
145138568Ssamstruct ieee80211_node;
146138568Ssamstruct mbuf;
147138568Ssam
148178354SsamMALLOC_DECLARE(M_80211_CRYPTO);
149138568Ssam
150144618Ssamvoid	ieee80211_crypto_attach(struct ieee80211com *);
151144618Ssamvoid	ieee80211_crypto_detach(struct ieee80211com *);
152178354Ssamvoid	ieee80211_crypto_vattach(struct ieee80211vap *);
153178354Ssamvoid	ieee80211_crypto_vdetach(struct ieee80211vap *);
154178354Ssamint	ieee80211_crypto_newkey(struct ieee80211vap *,
155144960Ssam		int cipher, int flags, struct ieee80211_key *);
156178354Ssamint	ieee80211_crypto_delkey(struct ieee80211vap *,
157138568Ssam		struct ieee80211_key *);
158179395Ssamint	ieee80211_crypto_setkey(struct ieee80211vap *, struct ieee80211_key *);
159178354Ssamvoid	ieee80211_crypto_delglobalkeys(struct ieee80211vap *);
160179396Ssamvoid	ieee80211_crypto_reload_keys(struct ieee80211com *);
161138568Ssam
162138568Ssam/*
163138568Ssam * Template for a supported cipher.  Ciphers register with the
164138568Ssam * crypto code and are typically loaded as separate modules
165138568Ssam * (the null cipher is always present).
166138568Ssam * XXX may need refcnts
167138568Ssam */
168138568Ssamstruct ieee80211_cipher {
169138568Ssam	const char *ic_name;		/* printable name */
170138568Ssam	u_int	ic_cipher;		/* IEEE80211_CIPHER_* */
171138568Ssam	u_int	ic_header;		/* size of privacy header (bytes) */
172138568Ssam	u_int	ic_trailer;		/* size of privacy trailer (bytes) */
173138568Ssam	u_int	ic_miclen;		/* size of mic trailer (bytes) */
174178354Ssam	void*	(*ic_attach)(struct ieee80211vap *, struct ieee80211_key *);
175138568Ssam	void	(*ic_detach)(struct ieee80211_key *);
176138568Ssam	int	(*ic_setkey)(struct ieee80211_key *);
177138568Ssam	int	(*ic_encap)(struct ieee80211_key *, struct mbuf *,
178170530Ssam			uint8_t keyid);
179147252Ssam	int	(*ic_decap)(struct ieee80211_key *, struct mbuf *, int);
180147045Ssam	int	(*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);
181147045Ssam	int	(*ic_demic)(struct ieee80211_key *, struct mbuf *, int);
182138568Ssam};
183138568Ssamextern	const struct ieee80211_cipher ieee80211_cipher_none;
184138568Ssam
185167432Ssam#define	IEEE80211_KEY_UNDEFINED(k) \
186167432Ssam	((k)->wk_cipher == &ieee80211_cipher_none)
187167432Ssam
188144618Ssamvoid	ieee80211_crypto_register(const struct ieee80211_cipher *);
189144618Ssamvoid	ieee80211_crypto_unregister(const struct ieee80211_cipher *);
190144618Ssamint	ieee80211_crypto_available(u_int cipher);
191138568Ssam
192178354Ssamstruct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *,
193178354Ssam		struct mbuf *);
194178354Ssamstruct ieee80211_key *ieee80211_crypto_decap(struct ieee80211_node *,
195178354Ssam		struct mbuf *, int);
196138568Ssam
197138568Ssam/*
198138568Ssam * Check and remove any MIC.
199138568Ssam */
200138568Ssamstatic __inline int
201178354Ssamieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
202147045Ssam	struct mbuf *m, int force)
203138568Ssam{
204138568Ssam	const struct ieee80211_cipher *cip = k->wk_cipher;
205147045Ssam	return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
206138568Ssam}
207138568Ssam
208138568Ssam/*
209138568Ssam * Add any MIC.
210138568Ssam */
211138568Ssamstatic __inline int
212178354Ssamieee80211_crypto_enmic(struct ieee80211vap *vap,
213147045Ssam	struct ieee80211_key *k, struct mbuf *m, int force)
214138568Ssam{
215138568Ssam	const struct ieee80211_cipher *cip = k->wk_cipher;
216147045Ssam	return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);
217138568Ssam}
218138568Ssam
219138568Ssam/*
220138568Ssam * Reset key state to an unused state.  The crypto
221138568Ssam * key allocation mechanism insures other state (e.g.
222138568Ssam * key data) is properly setup before a key is used.
223138568Ssam */
224138568Ssamstatic __inline void
225178354Ssamieee80211_crypto_resetkey(struct ieee80211vap *vap,
226148863Ssam	struct ieee80211_key *k, ieee80211_keyix ix)
227138568Ssam{
228186143Ssam	k->wk_cipher = &ieee80211_cipher_none;
229178354Ssam	k->wk_private = k->wk_cipher->ic_attach(vap, k);
230148863Ssam	k->wk_keyix = k->wk_rxkeyix = ix;
231138568Ssam	k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
232138568Ssam}
233138568Ssam
234138568Ssam/*
235138568Ssam * Crypt-related notification methods.
236138568Ssam */
237178354Ssamvoid	ieee80211_notify_replay_failure(struct ieee80211vap *,
238138568Ssam		const struct ieee80211_frame *, const struct ieee80211_key *,
239193541Ssam		uint64_t rsc, int tid);
240178354Ssamvoid	ieee80211_notify_michael_failure(struct ieee80211vap *,
241138568Ssam		const struct ieee80211_frame *, u_int keyix);
242138568Ssam#endif /* defined(__KERNEL__) || defined(_KERNEL) */
243116742Ssam#endif /* _NET80211_IEEE80211_CRYPTO_H_ */
244