ieee80211_crypto.h revision 170530
1301169Slidl/*-
2301169Slidl * Copyright (c) 2001 Atsushi Onoe
3301169Slidl * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
4301169Slidl * All rights reserved.
5301169Slidl *
6301169Slidl * Redistribution and use in source and binary forms, with or without
7301169Slidl * modification, are permitted provided that the following conditions
8301169Slidl * are met:
9301169Slidl * 1. Redistributions of source code must retain the above copyright
10301169Slidl *    notice, this list of conditions and the following disclaimer.
11301169Slidl * 2. Redistributions in binary form must reproduce the above copyright
12301169Slidl *    notice, this list of conditions and the following disclaimer in the
13301169Slidl *    documentation and/or other materials provided with the distribution.
14301169Slidl *
15301169Slidl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16301169Slidl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17301169Slidl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18301169Slidl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19301169Slidl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20301169Slidl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21301169Slidl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22301169Slidl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23301169Slidl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24301169Slidl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25301169Slidl *
26301169Slidl * $FreeBSD: head/sys/net80211/ieee80211_crypto.h 170530 2007-06-11 03:36:55Z sam $
27301169Slidl */
28301169Slidl#ifndef _NET80211_IEEE80211_CRYPTO_H_
29301169Slidl#define _NET80211_IEEE80211_CRYPTO_H_
30301169Slidl
31301169Slidl/*
32301169Slidl * 802.11 protocol crypto-related definitions.
33301169Slidl */
34301169Slidl#define	IEEE80211_KEYBUF_SIZE	16
35301169Slidl#define	IEEE80211_MICBUF_SIZE	(8+8)	/* space for both tx+rx keys */
36301169Slidl
37301169Slidl/*
38301169Slidl * Old WEP-style key.  Deprecated.
39301169Slidl */
40301169Slidlstruct ieee80211_wepkey {
41301169Slidl	u_int		wk_len;		/* key length in bytes */
42301169Slidl	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE];
43301169Slidl};
44301169Slidl
45301169Slidlstruct ieee80211_cipher;
46301169Slidl
47301169Slidl/*
48301169Slidl * Crypto key state.  There is sufficient room for all supported
49301169Slidl * ciphers (see below).  The underlying ciphers are handled
50301169Slidl * separately through loadable cipher modules that register with
51301169Slidl * the generic crypto support.  A key has a reference to an instance
52301169Slidl * of the cipher; any per-key state is hung off wk_private by the
53301169Slidl * cipher when it is attached.  Ciphers are automatically called
54301169Slidl * to detach and cleanup any such state when the key is deleted.
55301169Slidl *
56301169Slidl * The generic crypto support handles encap/decap of cipher-related
57301169Slidl * frame contents for both hardware- and software-based implementations.
58301169Slidl * A key requiring software crypto support is automatically flagged and
59301169Slidl * the cipher is expected to honor this and do the necessary work.
60301169Slidl * Ciphers such as TKIP may also support mixed hardware/software
61301169Slidl * encrypt/decrypt and MIC processing.
62301169Slidl */
63301169Slidltypedef uint16_t ieee80211_keyix;	/* h/w key index */
64301169Slidl
65301169Slidlstruct ieee80211_key {
66301169Slidl	uint8_t		wk_keylen;	/* key length in bytes */
67301169Slidl	uint8_t		wk_pad;
68301169Slidl	uint16_t	wk_flags;
69301169Slidl#define	IEEE80211_KEY_XMIT	0x01	/* key used for xmit */
70301169Slidl#define	IEEE80211_KEY_RECV	0x02	/* key used for recv */
71301169Slidl#define	IEEE80211_KEY_GROUP	0x04	/* key used for WPA group operation */
72301169Slidl#define	IEEE80211_KEY_SWCRYPT	0x10	/* host-based encrypt/decrypt */
73301169Slidl#define	IEEE80211_KEY_SWMIC	0x20	/* host-based enmic/demic */
74301169Slidl	ieee80211_keyix	wk_keyix;	/* h/w key index */
75301169Slidl	ieee80211_keyix	wk_rxkeyix;	/* optional h/w rx key index */
76301169Slidl	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
77301169Slidl#define	wk_txmic	wk_key+IEEE80211_KEYBUF_SIZE+0	/* XXX can't () right */
78301169Slidl#define	wk_rxmic	wk_key+IEEE80211_KEYBUF_SIZE+8	/* XXX can't () right */
79301169Slidl	uint64_t	wk_keyrsc;	/* key receive sequence counter */
80301169Slidl	uint64_t	wk_keytsc;	/* key transmit sequence counter */
81301169Slidl	const struct ieee80211_cipher *wk_cipher;
82301169Slidl	void		*wk_private;	/* private cipher state */
83301169Slidl};
84301169Slidl#define	IEEE80211_KEY_COMMON 		/* common flags passed in by apps */\
85301169Slidl	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP)
86301169Slidl
87301169Slidl/*
88301169Slidl * NB: these values are ordered carefully; there are lots of
89301169Slidl * of implications in any reordering.  In particular beware
90301169Slidl * that 4 is not used to avoid conflicting with IEEE80211_F_PRIVACY.
91301169Slidl */
92#define	IEEE80211_CIPHER_WEP		0
93#define	IEEE80211_CIPHER_TKIP		1
94#define	IEEE80211_CIPHER_AES_OCB	2
95#define	IEEE80211_CIPHER_AES_CCM	3
96#define	IEEE80211_CIPHER_CKIP		5
97#define	IEEE80211_CIPHER_NONE		6	/* pseudo value */
98
99#define	IEEE80211_CIPHER_MAX		(IEEE80211_CIPHER_NONE+1)
100
101#define	IEEE80211_KEYIX_NONE	((ieee80211_keyix) -1)
102
103#if defined(__KERNEL__) || defined(_KERNEL)
104
105struct ieee80211com;
106struct ieee80211_node;
107struct mbuf;
108
109/*
110 * Crypto state kept in each ieee80211com.  Some of this
111 * can/should be shared when virtual AP's are supported.
112 *
113 * XXX save reference to ieee80211com to properly encapsulate state.
114 * XXX split out crypto capabilities from ic_caps
115 */
116struct ieee80211_crypto_state {
117	struct ieee80211_key	cs_nw_keys[IEEE80211_WEP_NKID];
118	ieee80211_keyix		cs_def_txkey;	/* default/group tx key index */
119	uint16_t		cs_max_keyix;	/* max h/w key index */
120
121	int			(*cs_key_alloc)(struct ieee80211com *,
122					const struct ieee80211_key *,
123					ieee80211_keyix *, ieee80211_keyix *);
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 uint8_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, int flags, struct ieee80211_key *);
137int	ieee80211_crypto_delkey(struct ieee80211com *,
138		struct ieee80211_key *);
139int	ieee80211_crypto_setkey(struct ieee80211com *,
140		struct ieee80211_key *, const uint8_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			uint8_t keyid);
160	int	(*ic_decap)(struct ieee80211_key *, struct mbuf *, int);
161	int	(*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);
162	int	(*ic_demic)(struct ieee80211_key *, struct mbuf *, int);
163};
164extern	const struct ieee80211_cipher ieee80211_cipher_none;
165
166#define	IEEE80211_KEY_UNDEFINED(k) \
167	((k)->wk_cipher == &ieee80211_cipher_none)
168
169void	ieee80211_crypto_register(const struct ieee80211_cipher *);
170void	ieee80211_crypto_unregister(const struct ieee80211_cipher *);
171int	ieee80211_crypto_available(u_int cipher);
172
173struct ieee80211_key *ieee80211_crypto_encap(struct ieee80211com *,
174		struct ieee80211_node *, struct mbuf *);
175struct ieee80211_key *ieee80211_crypto_decap(struct ieee80211com *,
176		struct ieee80211_node *, struct mbuf *, int);
177
178/*
179 * Check and remove any MIC.
180 */
181static __inline int
182ieee80211_crypto_demic(struct ieee80211com *ic, struct ieee80211_key *k,
183	struct mbuf *m, int force)
184{
185	const struct ieee80211_cipher *cip = k->wk_cipher;
186	return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
187}
188
189/*
190 * Add any MIC.
191 */
192static __inline int
193ieee80211_crypto_enmic(struct ieee80211com *ic,
194	struct ieee80211_key *k, struct mbuf *m, int force)
195{
196	const struct ieee80211_cipher *cip = k->wk_cipher;
197	return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);
198}
199
200/*
201 * Reset key state to an unused state.  The crypto
202 * key allocation mechanism insures other state (e.g.
203 * key data) is properly setup before a key is used.
204 */
205static __inline void
206ieee80211_crypto_resetkey(struct ieee80211com *ic,
207	struct ieee80211_key *k, ieee80211_keyix ix)
208{
209	k->wk_cipher = &ieee80211_cipher_none;;
210	k->wk_private = k->wk_cipher->ic_attach(ic, k);
211	k->wk_keyix = k->wk_rxkeyix = ix;
212	k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
213}
214
215/*
216 * Crypt-related notification methods.
217 */
218void	ieee80211_notify_replay_failure(struct ieee80211com *,
219		const struct ieee80211_frame *, const struct ieee80211_key *,
220		u_int64_t rsc);
221void	ieee80211_notify_michael_failure(struct ieee80211com *,
222		const struct ieee80211_frame *, u_int keyix);
223#endif /* defined(__KERNEL__) || defined(_KERNEL) */
224#endif /* _NET80211_IEEE80211_CRYPTO_H_ */
225