Home | History | Annotate | Line # | Download | only in net80211
ieee80211_crypto.h revision 1.12.4.1
      1       1.1  dyoung /*-
      2  1.12.4.1    phil  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      3  1.12.4.1    phil  *
      4       1.1  dyoung  * Copyright (c) 2001 Atsushi Onoe
      5  1.12.4.1    phil  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
      6       1.1  dyoung  * All rights reserved.
      7       1.1  dyoung  *
      8       1.1  dyoung  * Redistribution and use in source and binary forms, with or without
      9       1.1  dyoung  * modification, are permitted provided that the following conditions
     10       1.1  dyoung  * are met:
     11       1.1  dyoung  * 1. Redistributions of source code must retain the above copyright
     12       1.1  dyoung  *    notice, this list of conditions and the following disclaimer.
     13       1.1  dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1  dyoung  *    notice, this list of conditions and the following disclaimer in the
     15       1.1  dyoung  *    documentation and/or other materials provided with the distribution.
     16       1.1  dyoung  *
     17       1.1  dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18       1.1  dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19       1.1  dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20       1.1  dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21       1.1  dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22       1.1  dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23       1.1  dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24       1.1  dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25       1.1  dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26       1.1  dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27       1.1  dyoung  *
     28  1.12.4.1    phil  * $FreeBSD$
     29       1.1  dyoung  */
     30       1.1  dyoung #ifndef _NET80211_IEEE80211_CRYPTO_H_
     31       1.1  dyoung #define _NET80211_IEEE80211_CRYPTO_H_
     32       1.1  dyoung 
     33       1.1  dyoung /*
     34       1.1  dyoung  * 802.11 protocol crypto-related definitions.
     35       1.1  dyoung  */
     36       1.1  dyoung #define	IEEE80211_KEYBUF_SIZE	16
     37       1.3  dyoung #define	IEEE80211_MICBUF_SIZE	(8+8)	/* space for both tx+rx keys */
     38       1.1  dyoung 
     39       1.3  dyoung /*
     40       1.3  dyoung  * Old WEP-style key.  Deprecated.
     41       1.3  dyoung  */
     42       1.1  dyoung struct ieee80211_wepkey {
     43       1.3  dyoung 	u_int		wk_len;		/* key length in bytes */
     44  1.12.4.1    phil 	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE];
     45  1.12.4.1    phil };
     46  1.12.4.1    phil 
     47  1.12.4.1    phil struct ieee80211_rsnparms {
     48  1.12.4.1    phil 	uint8_t		rsn_mcastcipher;	/* mcast/group cipher */
     49  1.12.4.1    phil 	uint8_t		rsn_mcastkeylen;	/* mcast key length */
     50  1.12.4.1    phil 	uint8_t		rsn_ucastcipher;	/* selected unicast cipher */
     51  1.12.4.1    phil 	uint8_t		rsn_ucastkeylen;	/* unicast key length */
     52  1.12.4.1    phil 	uint8_t		rsn_keymgmt;		/* selected key mgmt algo */
     53  1.12.4.1    phil 	uint16_t	rsn_caps;		/* capabilities */
     54       1.3  dyoung };
     55       1.3  dyoung 
     56       1.3  dyoung struct ieee80211_cipher;
     57       1.3  dyoung 
     58       1.3  dyoung /*
     59       1.3  dyoung  * Crypto key state.  There is sufficient room for all supported
     60       1.3  dyoung  * ciphers (see below).  The underlying ciphers are handled
     61       1.3  dyoung  * separately through loadable cipher modules that register with
     62       1.3  dyoung  * the generic crypto support.  A key has a reference to an instance
     63       1.3  dyoung  * of the cipher; any per-key state is hung off wk_private by the
     64       1.3  dyoung  * cipher when it is attached.  Ciphers are automatically called
     65       1.3  dyoung  * to detach and cleanup any such state when the key is deleted.
     66       1.3  dyoung  *
     67       1.3  dyoung  * The generic crypto support handles encap/decap of cipher-related
     68       1.3  dyoung  * frame contents for both hardware- and software-based implementations.
     69       1.3  dyoung  * A key requiring software crypto support is automatically flagged and
     70       1.3  dyoung  * the cipher is expected to honor this and do the necessary work.
     71       1.3  dyoung  * Ciphers such as TKIP may also support mixed hardware/software
     72       1.3  dyoung  * encrypt/decrypt and MIC processing.
     73       1.3  dyoung  */
     74  1.12.4.1    phil typedef uint16_t ieee80211_keyix;	/* h/w key index */
     75       1.7   skrll 
     76       1.3  dyoung struct ieee80211_key {
     77  1.12.4.1    phil 	uint8_t		wk_keylen;	/* key length in bytes */
     78  1.12.4.1    phil 	uint8_t		wk_pad;		/* .. some drivers use this. Fix that. */
     79  1.12.4.1    phil 	uint8_t		wk_pad1[2];
     80  1.12.4.1    phil 	uint32_t	wk_flags;
     81  1.12.4.1    phil #define	IEEE80211_KEY_XMIT	0x00000001	/* key used for xmit */
     82  1.12.4.1    phil #define	IEEE80211_KEY_RECV	0x00000002	/* key used for recv */
     83  1.12.4.1    phil #define	IEEE80211_KEY_GROUP	0x00000004	/* key used for WPA group operation */
     84  1.12.4.1    phil #define	IEEE80211_KEY_NOREPLAY	0x00000008	/* ignore replay failures */
     85  1.12.4.1    phil #define	IEEE80211_KEY_SWENCRYPT	0x00000010	/* host-based encrypt */
     86  1.12.4.1    phil #define	IEEE80211_KEY_SWDECRYPT	0x00000020	/* host-based decrypt */
     87  1.12.4.1    phil #define	IEEE80211_KEY_SWENMIC	0x00000040	/* host-based enmic */
     88  1.12.4.1    phil #define	IEEE80211_KEY_SWDEMIC	0x00000080	/* host-based demic */
     89  1.12.4.1    phil #define	IEEE80211_KEY_DEVKEY	0x00000100	/* device key request completed */
     90  1.12.4.1    phil #define	IEEE80211_KEY_CIPHER0	0x00001000	/* cipher-specific action 0 */
     91  1.12.4.1    phil #define	IEEE80211_KEY_CIPHER1	0x00002000	/* cipher-specific action 1 */
     92  1.12.4.1    phil #define	IEEE80211_KEY_NOIV	0x00004000	/* don't insert IV/MIC for !mgmt */
     93  1.12.4.1    phil #define	IEEE80211_KEY_NOIVMGT	0x00008000	/* don't insert IV/MIC for mgmt */
     94  1.12.4.1    phil #define	IEEE80211_KEY_NOMIC	0x00010000	/* don't insert MIC for !mgmt */
     95  1.12.4.1    phil #define	IEEE80211_KEY_NOMICMGT	0x00020000	/* don't insert MIC for mgmt */
     96  1.12.4.1    phil 
     97       1.7   skrll 	ieee80211_keyix	wk_keyix;	/* h/w key index */
     98       1.7   skrll 	ieee80211_keyix	wk_rxkeyix;	/* optional h/w rx key index */
     99  1.12.4.1    phil 	uint8_t		wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
    100       1.3  dyoung #define	wk_txmic	wk_key+IEEE80211_KEYBUF_SIZE+0	/* XXX can't () right */
    101       1.3  dyoung #define	wk_rxmic	wk_key+IEEE80211_KEYBUF_SIZE+8	/* XXX can't () right */
    102  1.12.4.1    phil 					/* key receive sequence counter */
    103  1.12.4.1    phil 	uint64_t	wk_keyrsc[IEEE80211_TID_SIZE];
    104  1.12.4.1    phil 	uint64_t	wk_keytsc;	/* key transmit sequence counter */
    105       1.3  dyoung 	const struct ieee80211_cipher *wk_cipher;
    106       1.3  dyoung 	void		*wk_private;	/* private cipher state */
    107  1.12.4.1    phil 	uint8_t		wk_macaddr[IEEE80211_ADDR_LEN];
    108       1.3  dyoung };
    109       1.3  dyoung #define	IEEE80211_KEY_COMMON 		/* common flags passed in by apps */\
    110  1.12.4.1    phil 	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP | \
    111  1.12.4.1    phil 	 IEEE80211_KEY_NOREPLAY)
    112  1.12.4.1    phil 
    113  1.12.4.1    phil #define	IEEE80211_KEY_SWCRYPT \
    114  1.12.4.1    phil 	(IEEE80211_KEY_SWENCRYPT | IEEE80211_KEY_SWDECRYPT)
    115  1.12.4.1    phil #define	IEEE80211_KEY_SWMIC	(IEEE80211_KEY_SWENMIC | IEEE80211_KEY_SWDEMIC)
    116  1.12.4.1    phil 
    117  1.12.4.1    phil #define IEEE80211_KEY_DEVICE		/* flags owned by device driver */\
    118  1.12.4.1    phil 	(IEEE80211_KEY_DEVKEY|IEEE80211_KEY_CIPHER0|IEEE80211_KEY_CIPHER1| \
    119  1.12.4.1    phil 	 IEEE80211_KEY_SWCRYPT|IEEE80211_KEY_SWMIC|IEEE80211_KEY_NOIV | \
    120  1.12.4.1    phil 	 IEEE80211_KEY_NOIVMGT|IEEE80211_KEY_NOMIC|IEEE80211_KEY_NOMICMGT)
    121  1.12.4.1    phil 
    122  1.12.4.1    phil #define	IEEE80211_KEY_BITS \
    123  1.12.4.1    phil 	"\20\1XMIT\2RECV\3GROUP\4SWENCRYPT\5SWDECRYPT\6SWENMIC\7SWDEMIC" \
    124  1.12.4.1    phil 	"\10DEVKEY\11CIPHER0\12CIPHER1"
    125  1.12.4.1    phil 
    126  1.12.4.1    phil #define	IEEE80211_KEYIX_NONE	((ieee80211_keyix) -1)
    127       1.3  dyoung 
    128       1.3  dyoung /*
    129       1.3  dyoung  * NB: these values are ordered carefully; there are lots of
    130  1.12.4.1    phil  * of implications in any reordering.  Beware that 4 is used
    131  1.12.4.1    phil  * only to indicate h/w TKIP MIC support in driver capabilities;
    132  1.12.4.1    phil  * there is no separate cipher support (it's rolled into the
    133  1.12.4.1    phil  * TKIP cipher support).
    134       1.3  dyoung  */
    135       1.3  dyoung #define	IEEE80211_CIPHER_WEP		0
    136       1.3  dyoung #define	IEEE80211_CIPHER_TKIP		1
    137       1.3  dyoung #define	IEEE80211_CIPHER_AES_OCB	2
    138       1.3  dyoung #define	IEEE80211_CIPHER_AES_CCM	3
    139  1.12.4.1    phil #define	IEEE80211_CIPHER_TKIPMIC	4	/* TKIP MIC capability */
    140       1.3  dyoung #define	IEEE80211_CIPHER_CKIP		5
    141       1.3  dyoung #define	IEEE80211_CIPHER_NONE		6	/* pseudo value */
    142       1.3  dyoung 
    143       1.3  dyoung #define	IEEE80211_CIPHER_MAX		(IEEE80211_CIPHER_NONE+1)
    144       1.3  dyoung 
    145  1.12.4.1    phil /* capability bits in ic_cryptocaps/iv_cryptocaps */
    146  1.12.4.1    phil #define	IEEE80211_CRYPTO_WEP		(1<<IEEE80211_CIPHER_WEP)
    147  1.12.4.1    phil #define	IEEE80211_CRYPTO_TKIP		(1<<IEEE80211_CIPHER_TKIP)
    148  1.12.4.1    phil #define	IEEE80211_CRYPTO_AES_OCB	(1<<IEEE80211_CIPHER_AES_OCB)
    149  1.12.4.1    phil #define	IEEE80211_CRYPTO_AES_CCM	(1<<IEEE80211_CIPHER_AES_CCM)
    150  1.12.4.1    phil #define	IEEE80211_CRYPTO_TKIPMIC	(1<<IEEE80211_CIPHER_TKIPMIC)
    151  1.12.4.1    phil #define	IEEE80211_CRYPTO_CKIP		(1<<IEEE80211_CIPHER_CKIP)
    152  1.12.4.1    phil 
    153  1.12.4.1    phil #define	IEEE80211_CRYPTO_BITS \
    154  1.12.4.1    phil 	"\20\1WEP\2TKIP\3AES\4AES_CCM\5TKIPMIC\6CKIP"
    155       1.3  dyoung 
    156       1.3  dyoung #if defined(__KERNEL__) || defined(_KERNEL)
    157       1.3  dyoung 
    158       1.3  dyoung struct ieee80211com;
    159  1.12.4.1    phil struct ieee80211vap;
    160       1.3  dyoung struct ieee80211_node;
    161       1.3  dyoung struct mbuf;
    162       1.3  dyoung 
    163  1.12.4.1    phil MALLOC_DECLARE(M_80211_CRYPTO);
    164       1.3  dyoung 
    165       1.3  dyoung void	ieee80211_crypto_attach(struct ieee80211com *);
    166       1.3  dyoung void	ieee80211_crypto_detach(struct ieee80211com *);
    167  1.12.4.1    phil void	ieee80211_crypto_vattach(struct ieee80211vap *);
    168  1.12.4.1    phil void	ieee80211_crypto_vdetach(struct ieee80211vap *);
    169  1.12.4.1    phil int	ieee80211_crypto_newkey(struct ieee80211vap *,
    170       1.3  dyoung 		int cipher, int flags, struct ieee80211_key *);
    171  1.12.4.1    phil int	ieee80211_crypto_delkey(struct ieee80211vap *,
    172       1.3  dyoung 		struct ieee80211_key *);
    173  1.12.4.1    phil int	ieee80211_crypto_setkey(struct ieee80211vap *, struct ieee80211_key *);
    174  1.12.4.1    phil void	ieee80211_crypto_delglobalkeys(struct ieee80211vap *);
    175  1.12.4.1    phil void	ieee80211_crypto_reload_keys(struct ieee80211com *);
    176  1.12.4.1    phil void	ieee80211_crypto_set_deftxkey(struct ieee80211vap *,
    177  1.12.4.1    phil 	    ieee80211_keyix kid);
    178       1.3  dyoung 
    179       1.3  dyoung /*
    180       1.3  dyoung  * Template for a supported cipher.  Ciphers register with the
    181       1.3  dyoung  * crypto code and are typically loaded as separate modules
    182       1.3  dyoung  * (the null cipher is always present).
    183       1.3  dyoung  * XXX may need refcnts
    184       1.3  dyoung  */
    185       1.3  dyoung struct ieee80211_cipher {
    186       1.3  dyoung 	const char *ic_name;		/* printable name */
    187       1.3  dyoung 	u_int	ic_cipher;		/* IEEE80211_CIPHER_* */
    188       1.3  dyoung 	u_int	ic_header;		/* size of privacy header (bytes) */
    189       1.3  dyoung 	u_int	ic_trailer;		/* size of privacy trailer (bytes) */
    190       1.3  dyoung 	u_int	ic_miclen;		/* size of mic trailer (bytes) */
    191  1.12.4.1    phil 	void*	(*ic_attach)(struct ieee80211vap *, struct ieee80211_key *);
    192       1.3  dyoung 	void	(*ic_detach)(struct ieee80211_key *);
    193       1.3  dyoung 	int	(*ic_setkey)(struct ieee80211_key *);
    194  1.12.4.1    phil 	void	(*ic_setiv)(struct ieee80211_key *, uint8_t *);
    195  1.12.4.1    phil 	int	(*ic_encap)(struct ieee80211_key *, struct mbuf *);
    196       1.5  dyoung 	int	(*ic_decap)(struct ieee80211_key *, struct mbuf *, int);
    197       1.5  dyoung 	int	(*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);
    198       1.5  dyoung 	int	(*ic_demic)(struct ieee80211_key *, struct mbuf *, int);
    199       1.1  dyoung };
    200       1.3  dyoung extern	const struct ieee80211_cipher ieee80211_cipher_none;
    201  1.12.4.1    phil 
    202  1.12.4.1    phil #define	IEEE80211_KEY_UNDEFINED(k) \
    203  1.12.4.1    phil 	((k)->wk_cipher == &ieee80211_cipher_none)
    204       1.3  dyoung 
    205       1.3  dyoung void	ieee80211_crypto_register(const struct ieee80211_cipher *);
    206       1.3  dyoung void	ieee80211_crypto_unregister(const struct ieee80211_cipher *);
    207       1.3  dyoung int	ieee80211_crypto_available(u_int cipher);
    208       1.1  dyoung 
    209  1.12.4.1    phil int	ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *,
    210  1.12.4.1    phil 	    const struct ieee80211_key *k);
    211  1.12.4.1    phil uint8_t	ieee80211_crypto_get_keyid(struct ieee80211vap *vap,
    212  1.12.4.1    phil 		struct ieee80211_key *k);
    213  1.12.4.1    phil struct ieee80211_key *ieee80211_crypto_get_txkey(struct ieee80211_node *,
    214  1.12.4.1    phil 		struct mbuf *);
    215  1.12.4.1    phil struct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *,
    216  1.12.4.1    phil 		struct mbuf *);
    217  1.12.4.1    phil int	ieee80211_crypto_decap(struct ieee80211_node *,
    218  1.12.4.1    phil 		struct mbuf *, int, struct ieee80211_key **);
    219  1.12.4.1    phil int ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
    220  1.12.4.1    phil 		struct mbuf *, int);
    221       1.3  dyoung /*
    222       1.3  dyoung  * Add any MIC.
    223       1.3  dyoung  */
    224       1.3  dyoung static __inline int
    225  1.12.4.1    phil ieee80211_crypto_enmic(struct ieee80211vap *vap,
    226       1.5  dyoung 	struct ieee80211_key *k, struct mbuf *m, int force)
    227       1.3  dyoung {
    228       1.3  dyoung 	const struct ieee80211_cipher *cip = k->wk_cipher;
    229       1.5  dyoung 	return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);
    230       1.3  dyoung }
    231       1.3  dyoung 
    232       1.3  dyoung /*
    233       1.3  dyoung  * Reset key state to an unused state.  The crypto
    234       1.3  dyoung  * key allocation mechanism insures other state (e.g.
    235       1.3  dyoung  * key data) is properly setup before a key is used.
    236       1.3  dyoung  */
    237       1.3  dyoung static __inline void
    238  1.12.4.1    phil ieee80211_crypto_resetkey(struct ieee80211vap *vap,
    239       1.7   skrll 	struct ieee80211_key *k, ieee80211_keyix ix)
    240       1.3  dyoung {
    241      1.11    yamt 	k->wk_cipher = &ieee80211_cipher_none;
    242  1.12.4.1    phil 	k->wk_private = k->wk_cipher->ic_attach(vap, k);
    243       1.7   skrll 	k->wk_keyix = k->wk_rxkeyix = ix;
    244       1.3  dyoung 	k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
    245       1.3  dyoung }
    246       1.3  dyoung 
    247       1.3  dyoung /*
    248       1.3  dyoung  * Crypt-related notification methods.
    249       1.3  dyoung  */
    250  1.12.4.1    phil void	ieee80211_notify_replay_failure(struct ieee80211vap *,
    251       1.3  dyoung 		const struct ieee80211_frame *, const struct ieee80211_key *,
    252  1.12.4.1    phil 		uint64_t rsc, int tid);
    253  1.12.4.1    phil void	ieee80211_notify_michael_failure(struct ieee80211vap *,
    254       1.3  dyoung 		const struct ieee80211_frame *, u_int keyix);
    255       1.3  dyoung #endif /* defined(__KERNEL__) || defined(_KERNEL) */
    256  1.12.4.1    phil #endif /* _NET80211_IEEE80211_CRYPTO_H_ */
    257