ieee80211_crypto.h revision 178354
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 178354 2008-04-20 20:35:46Z 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;
78138568Ssam#define	IEEE80211_KEY_XMIT	0x01	/* key used for xmit */
79138568Ssam#define	IEEE80211_KEY_RECV	0x02	/* key used for recv */
80144960Ssam#define	IEEE80211_KEY_GROUP	0x04	/* key used for WPA group operation */
81144960Ssam#define	IEEE80211_KEY_SWCRYPT	0x10	/* host-based encrypt/decrypt */
82144960Ssam#define	IEEE80211_KEY_SWMIC	0x20	/* host-based enmic/demic */
83148863Ssam	ieee80211_keyix	wk_keyix;	/* h/w key index */
84148863Ssam	ieee80211_keyix	wk_rxkeyix;	/* optional h/w rx key index */
85170530Ssam	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
86138568Ssam#define	wk_txmic	wk_key+IEEE80211_KEYBUF_SIZE+0	/* XXX can't () right */
87138568Ssam#define	wk_rxmic	wk_key+IEEE80211_KEYBUF_SIZE+8	/* XXX can't () right */
88178354Ssam					/* key receive sequence counter */
89178354Ssam	uint64_t	wk_keyrsc[IEEE80211_TID_SIZE];
90170530Ssam	uint64_t	wk_keytsc;	/* key transmit sequence counter */
91138568Ssam	const struct ieee80211_cipher *wk_cipher;
92138568Ssam	void		*wk_private;	/* private cipher state */
93138568Ssam};
94144960Ssam#define	IEEE80211_KEY_COMMON 		/* common flags passed in by apps */\
95144960Ssam	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP)
96138568Ssam
97178354Ssam#define	IEEE80211_KEYIX_NONE	((ieee80211_keyix) -1)
98178354Ssam
99138568Ssam/*
100138568Ssam * NB: these values are ordered carefully; there are lots of
101178354Ssam * of implications in any reordering.  Beware that 4 is used
102178354Ssam * only to indicate h/w TKIP MIC support in driver capabilities;
103178354Ssam * there is no separate cipher support (it's rolled into the
104178354Ssam * TKIP cipher support).
105138568Ssam */
106138568Ssam#define	IEEE80211_CIPHER_WEP		0
107138568Ssam#define	IEEE80211_CIPHER_TKIP		1
108138568Ssam#define	IEEE80211_CIPHER_AES_OCB	2
109138568Ssam#define	IEEE80211_CIPHER_AES_CCM	3
110178354Ssam#define	IEEE80211_CIPHER_TKIPMIC	4	/* TKIP MIC capability */
111138568Ssam#define	IEEE80211_CIPHER_CKIP		5
112138568Ssam#define	IEEE80211_CIPHER_NONE		6	/* pseudo value */
113138568Ssam
114138568Ssam#define	IEEE80211_CIPHER_MAX		(IEEE80211_CIPHER_NONE+1)
115138568Ssam
116178354Ssam/* capability bits in ic_cryptocaps/iv_cryptocaps */
117178354Ssam#define	IEEE80211_CRYPTO_WEP		(1<<IEEE80211_CIPHER_WEP)
118178354Ssam#define	IEEE80211_CRYPTO_TKIP		(1<<IEEE80211_CIPHER_TKIP)
119178354Ssam#define	IEEE80211_CRYPTO_AES_OCB	(1<<IEEE80211_CIPHER_AES_OCB)
120178354Ssam#define	IEEE80211_CRYPTO_AES_CCM	(1<<IEEE80211_CIPHER_AES_CCM)
121178354Ssam#define	IEEE80211_CRYPTO_TKIPMIC	(1<<IEEE80211_CIPHER_TKIPMIC)
122178354Ssam#define	IEEE80211_CRYPTO_CKIP		(1<<IEEE80211_CIPHER_CKIP)
123138568Ssam
124138568Ssam#if defined(__KERNEL__) || defined(_KERNEL)
125138568Ssam
126138568Ssamstruct ieee80211com;
127178354Ssamstruct ieee80211vap;
128138568Ssamstruct ieee80211_node;
129138568Ssamstruct mbuf;
130138568Ssam
131178354SsamMALLOC_DECLARE(M_80211_CRYPTO);
132138568Ssam
133144618Ssamvoid	ieee80211_crypto_attach(struct ieee80211com *);
134144618Ssamvoid	ieee80211_crypto_detach(struct ieee80211com *);
135178354Ssamvoid	ieee80211_crypto_vattach(struct ieee80211vap *);
136178354Ssamvoid	ieee80211_crypto_vdetach(struct ieee80211vap *);
137178354Ssamint	ieee80211_crypto_newkey(struct ieee80211vap *,
138144960Ssam		int cipher, int flags, struct ieee80211_key *);
139178354Ssamint	ieee80211_crypto_delkey(struct ieee80211vap *,
140138568Ssam		struct ieee80211_key *);
141178354Ssamint	ieee80211_crypto_setkey(struct ieee80211vap *,
142178354Ssam		struct ieee80211_key *,
143178354Ssam		const uint8_t macaddr[IEEE80211_ADDR_LEN]);
144178354Ssamvoid	ieee80211_crypto_delglobalkeys(struct ieee80211vap *);
145138568Ssam
146138568Ssam/*
147138568Ssam * Template for a supported cipher.  Ciphers register with the
148138568Ssam * crypto code and are typically loaded as separate modules
149138568Ssam * (the null cipher is always present).
150138568Ssam * XXX may need refcnts
151138568Ssam */
152138568Ssamstruct ieee80211_cipher {
153138568Ssam	const char *ic_name;		/* printable name */
154138568Ssam	u_int	ic_cipher;		/* IEEE80211_CIPHER_* */
155138568Ssam	u_int	ic_header;		/* size of privacy header (bytes) */
156138568Ssam	u_int	ic_trailer;		/* size of privacy trailer (bytes) */
157138568Ssam	u_int	ic_miclen;		/* size of mic trailer (bytes) */
158178354Ssam	void*	(*ic_attach)(struct ieee80211vap *, struct ieee80211_key *);
159138568Ssam	void	(*ic_detach)(struct ieee80211_key *);
160138568Ssam	int	(*ic_setkey)(struct ieee80211_key *);
161138568Ssam	int	(*ic_encap)(struct ieee80211_key *, struct mbuf *,
162170530Ssam			uint8_t keyid);
163147252Ssam	int	(*ic_decap)(struct ieee80211_key *, struct mbuf *, int);
164147045Ssam	int	(*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);
165147045Ssam	int	(*ic_demic)(struct ieee80211_key *, struct mbuf *, int);
166138568Ssam};
167138568Ssamextern	const struct ieee80211_cipher ieee80211_cipher_none;
168138568Ssam
169167432Ssam#define	IEEE80211_KEY_UNDEFINED(k) \
170167432Ssam	((k)->wk_cipher == &ieee80211_cipher_none)
171167432Ssam
172144618Ssamvoid	ieee80211_crypto_register(const struct ieee80211_cipher *);
173144618Ssamvoid	ieee80211_crypto_unregister(const struct ieee80211_cipher *);
174144618Ssamint	ieee80211_crypto_available(u_int cipher);
175138568Ssam
176178354Ssamstruct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *,
177178354Ssam		struct mbuf *);
178178354Ssamstruct ieee80211_key *ieee80211_crypto_decap(struct ieee80211_node *,
179178354Ssam		struct mbuf *, int);
180138568Ssam
181138568Ssam/*
182138568Ssam * Check and remove any MIC.
183138568Ssam */
184138568Ssamstatic __inline int
185178354Ssamieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
186147045Ssam	struct mbuf *m, int force)
187138568Ssam{
188138568Ssam	const struct ieee80211_cipher *cip = k->wk_cipher;
189147045Ssam	return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
190138568Ssam}
191138568Ssam
192138568Ssam/*
193138568Ssam * Add any MIC.
194138568Ssam */
195138568Ssamstatic __inline int
196178354Ssamieee80211_crypto_enmic(struct ieee80211vap *vap,
197147045Ssam	struct ieee80211_key *k, struct mbuf *m, int force)
198138568Ssam{
199138568Ssam	const struct ieee80211_cipher *cip = k->wk_cipher;
200147045Ssam	return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);
201138568Ssam}
202138568Ssam
203138568Ssam/*
204138568Ssam * Reset key state to an unused state.  The crypto
205138568Ssam * key allocation mechanism insures other state (e.g.
206138568Ssam * key data) is properly setup before a key is used.
207138568Ssam */
208138568Ssamstatic __inline void
209178354Ssamieee80211_crypto_resetkey(struct ieee80211vap *vap,
210148863Ssam	struct ieee80211_key *k, ieee80211_keyix ix)
211138568Ssam{
212138568Ssam	k->wk_cipher = &ieee80211_cipher_none;;
213178354Ssam	k->wk_private = k->wk_cipher->ic_attach(vap, k);
214148863Ssam	k->wk_keyix = k->wk_rxkeyix = ix;
215138568Ssam	k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
216138568Ssam}
217138568Ssam
218138568Ssam/*
219138568Ssam * Crypt-related notification methods.
220138568Ssam */
221178354Ssamvoid	ieee80211_notify_replay_failure(struct ieee80211vap *,
222138568Ssam		const struct ieee80211_frame *, const struct ieee80211_key *,
223178354Ssam		uint64_t rsc);
224178354Ssamvoid	ieee80211_notify_michael_failure(struct ieee80211vap *,
225138568Ssam		const struct ieee80211_frame *, u_int keyix);
226138568Ssam#endif /* defined(__KERNEL__) || defined(_KERNEL) */
227116742Ssam#endif /* _NET80211_IEEE80211_CRYPTO_H_ */
228