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$
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 */
81209636Sbschmidt#define	IEEE80211_KEY_NOREPLAY	0x0008	/* ignore replay failures */
82183248Ssam#define	IEEE80211_KEY_SWENCRYPT	0x0010	/* host-based encrypt */
83183248Ssam#define	IEEE80211_KEY_SWDECRYPT	0x0020	/* host-based decrypt */
84183248Ssam#define	IEEE80211_KEY_SWENMIC	0x0040	/* host-based enmic */
85183248Ssam#define	IEEE80211_KEY_SWDEMIC	0x0080	/* host-based demic */
86183248Ssam#define	IEEE80211_KEY_DEVKEY	0x0100	/* device key request completed */
87183248Ssam#define	IEEE80211_KEY_CIPHER0	0x1000	/* cipher-specific action 0 */
88183248Ssam#define	IEEE80211_KEY_CIPHER1	0x2000	/* cipher-specific action 1 */
89148863Ssam	ieee80211_keyix	wk_keyix;	/* h/w key index */
90148863Ssam	ieee80211_keyix	wk_rxkeyix;	/* optional h/w rx key index */
91170530Ssam	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
92138568Ssam#define	wk_txmic	wk_key+IEEE80211_KEYBUF_SIZE+0	/* XXX can't () right */
93138568Ssam#define	wk_rxmic	wk_key+IEEE80211_KEYBUF_SIZE+8	/* XXX can't () right */
94178354Ssam					/* key receive sequence counter */
95178354Ssam	uint64_t	wk_keyrsc[IEEE80211_TID_SIZE];
96170530Ssam	uint64_t	wk_keytsc;	/* key transmit sequence counter */
97138568Ssam	const struct ieee80211_cipher *wk_cipher;
98138568Ssam	void		*wk_private;	/* private cipher state */
99179395Ssam	uint8_t		wk_macaddr[IEEE80211_ADDR_LEN];
100138568Ssam};
101144960Ssam#define	IEEE80211_KEY_COMMON 		/* common flags passed in by apps */\
102209636Sbschmidt	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP | \
103209636Sbschmidt	 IEEE80211_KEY_NOREPLAY)
104195812Ssam#define	IEEE80211_KEY_DEVICE		/* flags owned by device driver */\
105195812Ssam	(IEEE80211_KEY_DEVKEY|IEEE80211_KEY_CIPHER0|IEEE80211_KEY_CIPHER1)
106138568Ssam
107179394Ssam#define	IEEE80211_KEY_SWCRYPT \
108179394Ssam	(IEEE80211_KEY_SWENCRYPT | IEEE80211_KEY_SWDECRYPT)
109179394Ssam#define	IEEE80211_KEY_SWMIC	(IEEE80211_KEY_SWENMIC | IEEE80211_KEY_SWDEMIC)
110179394Ssam
111188170Ssam#define	IEEE80211_KEY_BITS \
112188170Ssam	"\20\1XMIT\2RECV\3GROUP\4SWENCRYPT\5SWDECRYPT\6SWENMIC\7SWDEMIC" \
113188170Ssam	"\10DEVKEY\11CIPHER0\12CIPHER1"
114188170Ssam
115178354Ssam#define	IEEE80211_KEYIX_NONE	((ieee80211_keyix) -1)
116178354Ssam
117138568Ssam/*
118138568Ssam * NB: these values are ordered carefully; there are lots of
119178354Ssam * of implications in any reordering.  Beware that 4 is used
120178354Ssam * only to indicate h/w TKIP MIC support in driver capabilities;
121178354Ssam * there is no separate cipher support (it's rolled into the
122178354Ssam * TKIP cipher support).
123138568Ssam */
124138568Ssam#define	IEEE80211_CIPHER_WEP		0
125138568Ssam#define	IEEE80211_CIPHER_TKIP		1
126138568Ssam#define	IEEE80211_CIPHER_AES_OCB	2
127138568Ssam#define	IEEE80211_CIPHER_AES_CCM	3
128178354Ssam#define	IEEE80211_CIPHER_TKIPMIC	4	/* TKIP MIC capability */
129138568Ssam#define	IEEE80211_CIPHER_CKIP		5
130138568Ssam#define	IEEE80211_CIPHER_NONE		6	/* pseudo value */
131138568Ssam
132138568Ssam#define	IEEE80211_CIPHER_MAX		(IEEE80211_CIPHER_NONE+1)
133138568Ssam
134178354Ssam/* capability bits in ic_cryptocaps/iv_cryptocaps */
135178354Ssam#define	IEEE80211_CRYPTO_WEP		(1<<IEEE80211_CIPHER_WEP)
136178354Ssam#define	IEEE80211_CRYPTO_TKIP		(1<<IEEE80211_CIPHER_TKIP)
137178354Ssam#define	IEEE80211_CRYPTO_AES_OCB	(1<<IEEE80211_CIPHER_AES_OCB)
138178354Ssam#define	IEEE80211_CRYPTO_AES_CCM	(1<<IEEE80211_CIPHER_AES_CCM)
139178354Ssam#define	IEEE80211_CRYPTO_TKIPMIC	(1<<IEEE80211_CIPHER_TKIPMIC)
140178354Ssam#define	IEEE80211_CRYPTO_CKIP		(1<<IEEE80211_CIPHER_CKIP)
141138568Ssam
142187797Ssam#define	IEEE80211_CRYPTO_BITS \
143187797Ssam	"\20\1WEP\2TKIP\3AES\4AES_CCM\5TKIPMIC\6CKIP"
144187797Ssam
145138568Ssam#if defined(__KERNEL__) || defined(_KERNEL)
146138568Ssam
147138568Ssamstruct ieee80211com;
148178354Ssamstruct ieee80211vap;
149138568Ssamstruct ieee80211_node;
150138568Ssamstruct mbuf;
151138568Ssam
152178354SsamMALLOC_DECLARE(M_80211_CRYPTO);
153138568Ssam
154144618Ssamvoid	ieee80211_crypto_attach(struct ieee80211com *);
155144618Ssamvoid	ieee80211_crypto_detach(struct ieee80211com *);
156178354Ssamvoid	ieee80211_crypto_vattach(struct ieee80211vap *);
157178354Ssamvoid	ieee80211_crypto_vdetach(struct ieee80211vap *);
158178354Ssamint	ieee80211_crypto_newkey(struct ieee80211vap *,
159144960Ssam		int cipher, int flags, struct ieee80211_key *);
160178354Ssamint	ieee80211_crypto_delkey(struct ieee80211vap *,
161138568Ssam		struct ieee80211_key *);
162179395Ssamint	ieee80211_crypto_setkey(struct ieee80211vap *, struct ieee80211_key *);
163178354Ssamvoid	ieee80211_crypto_delglobalkeys(struct ieee80211vap *);
164179396Ssamvoid	ieee80211_crypto_reload_keys(struct ieee80211com *);
165138568Ssam
166138568Ssam/*
167138568Ssam * Template for a supported cipher.  Ciphers register with the
168138568Ssam * crypto code and are typically loaded as separate modules
169138568Ssam * (the null cipher is always present).
170138568Ssam * XXX may need refcnts
171138568Ssam */
172138568Ssamstruct ieee80211_cipher {
173138568Ssam	const char *ic_name;		/* printable name */
174138568Ssam	u_int	ic_cipher;		/* IEEE80211_CIPHER_* */
175138568Ssam	u_int	ic_header;		/* size of privacy header (bytes) */
176138568Ssam	u_int	ic_trailer;		/* size of privacy trailer (bytes) */
177138568Ssam	u_int	ic_miclen;		/* size of mic trailer (bytes) */
178178354Ssam	void*	(*ic_attach)(struct ieee80211vap *, struct ieee80211_key *);
179138568Ssam	void	(*ic_detach)(struct ieee80211_key *);
180138568Ssam	int	(*ic_setkey)(struct ieee80211_key *);
181288526Sadrian	void	(*ic_setiv)(struct ieee80211_key *, uint8_t *);
182288523Sadrian	int	(*ic_encap)(struct ieee80211_key *, struct mbuf *);
183147252Ssam	int	(*ic_decap)(struct ieee80211_key *, struct mbuf *, int);
184147045Ssam	int	(*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);
185147045Ssam	int	(*ic_demic)(struct ieee80211_key *, struct mbuf *, int);
186138568Ssam};
187138568Ssamextern	const struct ieee80211_cipher ieee80211_cipher_none;
188138568Ssam
189167432Ssam#define	IEEE80211_KEY_UNDEFINED(k) \
190167432Ssam	((k)->wk_cipher == &ieee80211_cipher_none)
191167432Ssam
192144618Ssamvoid	ieee80211_crypto_register(const struct ieee80211_cipher *);
193144618Ssamvoid	ieee80211_crypto_unregister(const struct ieee80211_cipher *);
194144618Ssamint	ieee80211_crypto_available(u_int cipher);
195138568Ssam
196288523Sadrianuint8_t	ieee80211_crypto_get_keyid(struct ieee80211vap *vap,
197288523Sadrian		struct ieee80211_key *k);
198288527Sadrianstruct ieee80211_key *ieee80211_crypto_get_txkey(struct ieee80211_node *,
199288527Sadrian		struct mbuf *);
200178354Ssamstruct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *,
201178354Ssam		struct mbuf *);
202178354Ssamstruct ieee80211_key *ieee80211_crypto_decap(struct ieee80211_node *,
203178354Ssam		struct mbuf *, int);
204138568Ssam
205138568Ssam/*
206138568Ssam * Check and remove any MIC.
207138568Ssam */
208138568Ssamstatic __inline int
209178354Ssamieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
210147045Ssam	struct mbuf *m, int force)
211138568Ssam{
212138568Ssam	const struct ieee80211_cipher *cip = k->wk_cipher;
213147045Ssam	return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
214138568Ssam}
215138568Ssam
216138568Ssam/*
217138568Ssam * Add any MIC.
218138568Ssam */
219138568Ssamstatic __inline int
220178354Ssamieee80211_crypto_enmic(struct ieee80211vap *vap,
221147045Ssam	struct ieee80211_key *k, struct mbuf *m, int force)
222138568Ssam{
223138568Ssam	const struct ieee80211_cipher *cip = k->wk_cipher;
224147045Ssam	return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);
225138568Ssam}
226138568Ssam
227138568Ssam/*
228138568Ssam * Reset key state to an unused state.  The crypto
229138568Ssam * key allocation mechanism insures other state (e.g.
230138568Ssam * key data) is properly setup before a key is used.
231138568Ssam */
232138568Ssamstatic __inline void
233178354Ssamieee80211_crypto_resetkey(struct ieee80211vap *vap,
234148863Ssam	struct ieee80211_key *k, ieee80211_keyix ix)
235138568Ssam{
236186143Ssam	k->wk_cipher = &ieee80211_cipher_none;
237178354Ssam	k->wk_private = k->wk_cipher->ic_attach(vap, k);
238148863Ssam	k->wk_keyix = k->wk_rxkeyix = ix;
239138568Ssam	k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
240138568Ssam}
241138568Ssam
242138568Ssam/*
243138568Ssam * Crypt-related notification methods.
244138568Ssam */
245178354Ssamvoid	ieee80211_notify_replay_failure(struct ieee80211vap *,
246138568Ssam		const struct ieee80211_frame *, const struct ieee80211_key *,
247193541Ssam		uint64_t rsc, int tid);
248178354Ssamvoid	ieee80211_notify_michael_failure(struct ieee80211vap *,
249138568Ssam		const struct ieee80211_frame *, u_int keyix);
250138568Ssam#endif /* defined(__KERNEL__) || defined(_KERNEL) */
251116742Ssam#endif /* _NET80211_IEEE80211_CRYPTO_H_ */
252