xref: /freebsd/sys/net80211/ieee80211_crypto.h (revision b032f27c365b992e9d8e42214183b39acfb8c6ac)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
3b032f27cSSam Leffler  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
101a1e1d21SSam Leffler  *    notice, this list of conditions and the following disclaimer.
111a1e1d21SSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
121a1e1d21SSam Leffler  *    notice, this list of conditions and the following disclaimer in the
131a1e1d21SSam Leffler  *    documentation and/or other materials provided with the distribution.
147535e66aSSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  *
261a1e1d21SSam Leffler  * $FreeBSD$
271a1e1d21SSam Leffler  */
281a1e1d21SSam Leffler #ifndef _NET80211_IEEE80211_CRYPTO_H_
291a1e1d21SSam Leffler #define _NET80211_IEEE80211_CRYPTO_H_
301a1e1d21SSam Leffler 
311a1e1d21SSam Leffler /*
321a1e1d21SSam Leffler  * 802.11 protocol crypto-related definitions.
331a1e1d21SSam Leffler  */
341a1e1d21SSam Leffler #define	IEEE80211_KEYBUF_SIZE	16
358a1b9b6aSSam Leffler #define	IEEE80211_MICBUF_SIZE	(8+8)	/* space for both tx+rx keys */
361a1e1d21SSam Leffler 
378a1b9b6aSSam Leffler /*
388a1b9b6aSSam Leffler  * Old WEP-style key.  Deprecated.
398a1b9b6aSSam Leffler  */
401a1e1d21SSam Leffler struct ieee80211_wepkey {
418a1b9b6aSSam Leffler 	u_int		wk_len;		/* key length in bytes */
4268e8e04eSSam Leffler 	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE];
431a1e1d21SSam Leffler };
441a1e1d21SSam Leffler 
45b032f27cSSam Leffler struct ieee80211_rsnparms {
46b032f27cSSam Leffler 	uint8_t		rsn_mcastcipher;	/* mcast/group cipher */
47b032f27cSSam Leffler 	uint8_t		rsn_mcastkeylen;	/* mcast key length */
48b032f27cSSam Leffler 	uint8_t		rsn_ucastcipher;	/* selected unicast cipher */
49b032f27cSSam Leffler 	uint8_t		rsn_ucastkeylen;	/* unicast key length */
50b032f27cSSam Leffler 	uint8_t		rsn_keymgmt;		/* selected key mgmt algo */
51b032f27cSSam Leffler 	uint16_t	rsn_caps;		/* capabilities */
52b032f27cSSam Leffler };
53b032f27cSSam Leffler 
548a1b9b6aSSam Leffler struct ieee80211_cipher;
558a1b9b6aSSam Leffler 
568a1b9b6aSSam Leffler /*
578a1b9b6aSSam Leffler  * Crypto key state.  There is sufficient room for all supported
588a1b9b6aSSam Leffler  * ciphers (see below).  The underlying ciphers are handled
598a1b9b6aSSam Leffler  * separately through loadable cipher modules that register with
608a1b9b6aSSam Leffler  * the generic crypto support.  A key has a reference to an instance
618a1b9b6aSSam Leffler  * of the cipher; any per-key state is hung off wk_private by the
628a1b9b6aSSam Leffler  * cipher when it is attached.  Ciphers are automatically called
638a1b9b6aSSam Leffler  * to detach and cleanup any such state when the key is deleted.
648a1b9b6aSSam Leffler  *
658a1b9b6aSSam Leffler  * The generic crypto support handles encap/decap of cipher-related
668a1b9b6aSSam Leffler  * frame contents for both hardware- and software-based implementations.
678a1b9b6aSSam Leffler  * A key requiring software crypto support is automatically flagged and
688a1b9b6aSSam Leffler  * the cipher is expected to honor this and do the necessary work.
698a1b9b6aSSam Leffler  * Ciphers such as TKIP may also support mixed hardware/software
708a1b9b6aSSam Leffler  * encrypt/decrypt and MIC processing.
718a1b9b6aSSam Leffler  */
7268e8e04eSSam Leffler typedef uint16_t ieee80211_keyix;	/* h/w key index */
73c1225b52SSam Leffler 
748a1b9b6aSSam Leffler struct ieee80211_key {
7568e8e04eSSam Leffler 	uint8_t		wk_keylen;	/* key length in bytes */
7668e8e04eSSam Leffler 	uint8_t		wk_pad;
7768e8e04eSSam Leffler 	uint16_t	wk_flags;
788a1b9b6aSSam Leffler #define	IEEE80211_KEY_XMIT	0x01	/* key used for xmit */
798a1b9b6aSSam Leffler #define	IEEE80211_KEY_RECV	0x02	/* key used for recv */
80dd70e17bSSam Leffler #define	IEEE80211_KEY_GROUP	0x04	/* key used for WPA group operation */
81dd70e17bSSam Leffler #define	IEEE80211_KEY_SWCRYPT	0x10	/* host-based encrypt/decrypt */
82dd70e17bSSam Leffler #define	IEEE80211_KEY_SWMIC	0x20	/* host-based enmic/demic */
83c1225b52SSam Leffler 	ieee80211_keyix	wk_keyix;	/* h/w key index */
84c1225b52SSam Leffler 	ieee80211_keyix	wk_rxkeyix;	/* optional h/w rx key index */
8568e8e04eSSam Leffler 	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
868a1b9b6aSSam Leffler #define	wk_txmic	wk_key+IEEE80211_KEYBUF_SIZE+0	/* XXX can't () right */
878a1b9b6aSSam Leffler #define	wk_rxmic	wk_key+IEEE80211_KEYBUF_SIZE+8	/* XXX can't () right */
88b032f27cSSam Leffler 					/* key receive sequence counter */
89b032f27cSSam Leffler 	uint64_t	wk_keyrsc[IEEE80211_TID_SIZE];
9068e8e04eSSam Leffler 	uint64_t	wk_keytsc;	/* key transmit sequence counter */
918a1b9b6aSSam Leffler 	const struct ieee80211_cipher *wk_cipher;
928a1b9b6aSSam Leffler 	void		*wk_private;	/* private cipher state */
938a1b9b6aSSam Leffler };
94dd70e17bSSam Leffler #define	IEEE80211_KEY_COMMON 		/* common flags passed in by apps */\
95dd70e17bSSam Leffler 	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP)
968a1b9b6aSSam Leffler 
97b032f27cSSam Leffler #define	IEEE80211_KEYIX_NONE	((ieee80211_keyix) -1)
98b032f27cSSam Leffler 
998a1b9b6aSSam Leffler /*
1008a1b9b6aSSam Leffler  * NB: these values are ordered carefully; there are lots of
101b032f27cSSam Leffler  * of implications in any reordering.  Beware that 4 is used
102b032f27cSSam Leffler  * only to indicate h/w TKIP MIC support in driver capabilities;
103b032f27cSSam Leffler  * there is no separate cipher support (it's rolled into the
104b032f27cSSam Leffler  * TKIP cipher support).
1058a1b9b6aSSam Leffler  */
1068a1b9b6aSSam Leffler #define	IEEE80211_CIPHER_WEP		0
1078a1b9b6aSSam Leffler #define	IEEE80211_CIPHER_TKIP		1
1088a1b9b6aSSam Leffler #define	IEEE80211_CIPHER_AES_OCB	2
1098a1b9b6aSSam Leffler #define	IEEE80211_CIPHER_AES_CCM	3
110b032f27cSSam Leffler #define	IEEE80211_CIPHER_TKIPMIC	4	/* TKIP MIC capability */
1118a1b9b6aSSam Leffler #define	IEEE80211_CIPHER_CKIP		5
1128a1b9b6aSSam Leffler #define	IEEE80211_CIPHER_NONE		6	/* pseudo value */
1138a1b9b6aSSam Leffler 
1148a1b9b6aSSam Leffler #define	IEEE80211_CIPHER_MAX		(IEEE80211_CIPHER_NONE+1)
1158a1b9b6aSSam Leffler 
116b032f27cSSam Leffler /* capability bits in ic_cryptocaps/iv_cryptocaps */
117b032f27cSSam Leffler #define	IEEE80211_CRYPTO_WEP		(1<<IEEE80211_CIPHER_WEP)
118b032f27cSSam Leffler #define	IEEE80211_CRYPTO_TKIP		(1<<IEEE80211_CIPHER_TKIP)
119b032f27cSSam Leffler #define	IEEE80211_CRYPTO_AES_OCB	(1<<IEEE80211_CIPHER_AES_OCB)
120b032f27cSSam Leffler #define	IEEE80211_CRYPTO_AES_CCM	(1<<IEEE80211_CIPHER_AES_CCM)
121b032f27cSSam Leffler #define	IEEE80211_CRYPTO_TKIPMIC	(1<<IEEE80211_CIPHER_TKIPMIC)
122b032f27cSSam Leffler #define	IEEE80211_CRYPTO_CKIP		(1<<IEEE80211_CIPHER_CKIP)
1238a1b9b6aSSam Leffler 
1248a1b9b6aSSam Leffler #if defined(__KERNEL__) || defined(_KERNEL)
1258a1b9b6aSSam Leffler 
1268a1b9b6aSSam Leffler struct ieee80211com;
127b032f27cSSam Leffler struct ieee80211vap;
1288a1b9b6aSSam Leffler struct ieee80211_node;
1298a1b9b6aSSam Leffler struct mbuf;
1308a1b9b6aSSam Leffler 
131b032f27cSSam Leffler MALLOC_DECLARE(M_80211_CRYPTO);
1328a1b9b6aSSam Leffler 
1330942c81cSSam Leffler void	ieee80211_crypto_attach(struct ieee80211com *);
1340942c81cSSam Leffler void	ieee80211_crypto_detach(struct ieee80211com *);
135b032f27cSSam Leffler void	ieee80211_crypto_vattach(struct ieee80211vap *);
136b032f27cSSam Leffler void	ieee80211_crypto_vdetach(struct ieee80211vap *);
137b032f27cSSam Leffler int	ieee80211_crypto_newkey(struct ieee80211vap *,
138dd70e17bSSam Leffler 		int cipher, int flags, struct ieee80211_key *);
139b032f27cSSam Leffler int	ieee80211_crypto_delkey(struct ieee80211vap *,
1408a1b9b6aSSam Leffler 		struct ieee80211_key *);
141b032f27cSSam Leffler int	ieee80211_crypto_setkey(struct ieee80211vap *,
142b032f27cSSam Leffler 		struct ieee80211_key *,
143b032f27cSSam Leffler 		const uint8_t macaddr[IEEE80211_ADDR_LEN]);
144b032f27cSSam Leffler void	ieee80211_crypto_delglobalkeys(struct ieee80211vap *);
1458a1b9b6aSSam Leffler 
1468a1b9b6aSSam Leffler /*
1478a1b9b6aSSam Leffler  * Template for a supported cipher.  Ciphers register with the
1488a1b9b6aSSam Leffler  * crypto code and are typically loaded as separate modules
1498a1b9b6aSSam Leffler  * (the null cipher is always present).
1508a1b9b6aSSam Leffler  * XXX may need refcnts
1518a1b9b6aSSam Leffler  */
1528a1b9b6aSSam Leffler struct ieee80211_cipher {
1538a1b9b6aSSam Leffler 	const char *ic_name;		/* printable name */
1548a1b9b6aSSam Leffler 	u_int	ic_cipher;		/* IEEE80211_CIPHER_* */
1558a1b9b6aSSam Leffler 	u_int	ic_header;		/* size of privacy header (bytes) */
1568a1b9b6aSSam Leffler 	u_int	ic_trailer;		/* size of privacy trailer (bytes) */
1578a1b9b6aSSam Leffler 	u_int	ic_miclen;		/* size of mic trailer (bytes) */
158b032f27cSSam Leffler 	void*	(*ic_attach)(struct ieee80211vap *, struct ieee80211_key *);
1598a1b9b6aSSam Leffler 	void	(*ic_detach)(struct ieee80211_key *);
1608a1b9b6aSSam Leffler 	int	(*ic_setkey)(struct ieee80211_key *);
1618a1b9b6aSSam Leffler 	int	(*ic_encap)(struct ieee80211_key *, struct mbuf *,
16268e8e04eSSam Leffler 			uint8_t keyid);
1632cc12adeSSam Leffler 	int	(*ic_decap)(struct ieee80211_key *, struct mbuf *, int);
16496d88463SSam Leffler 	int	(*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);
16596d88463SSam Leffler 	int	(*ic_demic)(struct ieee80211_key *, struct mbuf *, int);
1668a1b9b6aSSam Leffler };
1678a1b9b6aSSam Leffler extern	const struct ieee80211_cipher ieee80211_cipher_none;
1688a1b9b6aSSam Leffler 
169cda15ce1SSam Leffler #define	IEEE80211_KEY_UNDEFINED(k) \
170cda15ce1SSam Leffler 	((k)->wk_cipher == &ieee80211_cipher_none)
171cda15ce1SSam Leffler 
1720942c81cSSam Leffler void	ieee80211_crypto_register(const struct ieee80211_cipher *);
1730942c81cSSam Leffler void	ieee80211_crypto_unregister(const struct ieee80211_cipher *);
1740942c81cSSam Leffler int	ieee80211_crypto_available(u_int cipher);
1758a1b9b6aSSam Leffler 
176b032f27cSSam Leffler struct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *,
177b032f27cSSam Leffler 		struct mbuf *);
178b032f27cSSam Leffler struct ieee80211_key *ieee80211_crypto_decap(struct ieee80211_node *,
179b032f27cSSam Leffler 		struct mbuf *, int);
1808a1b9b6aSSam Leffler 
1818a1b9b6aSSam Leffler /*
1828a1b9b6aSSam Leffler  * Check and remove any MIC.
1838a1b9b6aSSam Leffler  */
1848a1b9b6aSSam Leffler static __inline int
185b032f27cSSam Leffler ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
18696d88463SSam Leffler 	struct mbuf *m, int force)
1878a1b9b6aSSam Leffler {
1888a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip = k->wk_cipher;
18996d88463SSam Leffler 	return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
1908a1b9b6aSSam Leffler }
1918a1b9b6aSSam Leffler 
1928a1b9b6aSSam Leffler /*
1938a1b9b6aSSam Leffler  * Add any MIC.
1948a1b9b6aSSam Leffler  */
1958a1b9b6aSSam Leffler static __inline int
196b032f27cSSam Leffler ieee80211_crypto_enmic(struct ieee80211vap *vap,
19796d88463SSam Leffler 	struct ieee80211_key *k, struct mbuf *m, int force)
1988a1b9b6aSSam Leffler {
1998a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip = k->wk_cipher;
20096d88463SSam Leffler 	return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);
2018a1b9b6aSSam Leffler }
2028a1b9b6aSSam Leffler 
2038a1b9b6aSSam Leffler /*
2048a1b9b6aSSam Leffler  * Reset key state to an unused state.  The crypto
2058a1b9b6aSSam Leffler  * key allocation mechanism insures other state (e.g.
2068a1b9b6aSSam Leffler  * key data) is properly setup before a key is used.
2078a1b9b6aSSam Leffler  */
2088a1b9b6aSSam Leffler static __inline void
209b032f27cSSam Leffler ieee80211_crypto_resetkey(struct ieee80211vap *vap,
210c1225b52SSam Leffler 	struct ieee80211_key *k, ieee80211_keyix ix)
2118a1b9b6aSSam Leffler {
2128a1b9b6aSSam Leffler 	k->wk_cipher = &ieee80211_cipher_none;;
213b032f27cSSam Leffler 	k->wk_private = k->wk_cipher->ic_attach(vap, k);
214c1225b52SSam Leffler 	k->wk_keyix = k->wk_rxkeyix = ix;
2158a1b9b6aSSam Leffler 	k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
2168a1b9b6aSSam Leffler }
2178a1b9b6aSSam Leffler 
2188a1b9b6aSSam Leffler /*
2198a1b9b6aSSam Leffler  * Crypt-related notification methods.
2208a1b9b6aSSam Leffler  */
221b032f27cSSam Leffler void	ieee80211_notify_replay_failure(struct ieee80211vap *,
2228a1b9b6aSSam Leffler 		const struct ieee80211_frame *, const struct ieee80211_key *,
223b032f27cSSam Leffler 		uint64_t rsc);
224b032f27cSSam Leffler void	ieee80211_notify_michael_failure(struct ieee80211vap *,
2258a1b9b6aSSam Leffler 		const struct ieee80211_frame *, u_int keyix);
2268a1b9b6aSSam Leffler #endif /* defined(__KERNEL__) || defined(_KERNEL) */
2271a1e1d21SSam Leffler #endif /* _NET80211_IEEE80211_CRYPTO_H_ */
228