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