10ba2cbe9Sxc /*
2*ff3124efSff  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30ba2cbe9Sxc  * Use is subject to license terms.
40ba2cbe9Sxc  */
50ba2cbe9Sxc 
60ba2cbe9Sxc /*
70ba2cbe9Sxc  * Copyright (c) 2001 Atsushi Onoe
80ba2cbe9Sxc  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
90ba2cbe9Sxc  * All rights reserved.
100ba2cbe9Sxc  *
110ba2cbe9Sxc  * Redistribution and use in source and binary forms, with or without
120ba2cbe9Sxc  * modification, are permitted provided that the following conditions
130ba2cbe9Sxc  * are met:
140ba2cbe9Sxc  * 1. Redistributions of source code must retain the above copyright
150ba2cbe9Sxc  *    notice, this list of conditions and the following disclaimer.
160ba2cbe9Sxc  * 2. Redistributions in binary form must reproduce the above copyright
170ba2cbe9Sxc  *    notice, this list of conditions and the following disclaimer in the
180ba2cbe9Sxc  *    documentation and/or other materials provided with the distribution.
190ba2cbe9Sxc  * 3. The name of the author may not be used to endorse or promote products
200ba2cbe9Sxc  *    derived from this software without specific prior written permission.
210ba2cbe9Sxc  *
220ba2cbe9Sxc  * Alternatively, this software may be distributed under the terms of the
230ba2cbe9Sxc  * GNU General Public License ("GPL") version 2 as published by the Free
240ba2cbe9Sxc  * Software Foundation.
250ba2cbe9Sxc  *
260ba2cbe9Sxc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
270ba2cbe9Sxc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
280ba2cbe9Sxc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
290ba2cbe9Sxc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
300ba2cbe9Sxc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
310ba2cbe9Sxc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
320ba2cbe9Sxc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
330ba2cbe9Sxc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
340ba2cbe9Sxc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
350ba2cbe9Sxc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
360ba2cbe9Sxc  */
370ba2cbe9Sxc 
380ba2cbe9Sxc /*
390ba2cbe9Sxc  * IEEE 802.11 generic crypto support
400ba2cbe9Sxc  */
410ba2cbe9Sxc #include <sys/types.h>
420ba2cbe9Sxc #include <sys/note.h>
433a1a8936Szf #include <sys/crypto/common.h>
443a1a8936Szf #include <sys/crypto/api.h>
45*ff3124efSff #include <sys/strsun.h>
460ba2cbe9Sxc #include "net80211_impl.h"
470ba2cbe9Sxc 
480ba2cbe9Sxc extern const struct ieee80211_cipher wep;
49a399b765Szf extern const struct ieee80211_cipher tkip;
50a399b765Szf extern const struct ieee80211_cipher ccmp;
510ba2cbe9Sxc 
520ba2cbe9Sxc /*
530ba2cbe9Sxc  * Table of registered cipher modules.
540ba2cbe9Sxc  */
550ba2cbe9Sxc static const char *cipher_modnames[] = {
560ba2cbe9Sxc 	"wlan_wep",	/* IEEE80211_CIPHER_WEP */
570ba2cbe9Sxc 	"wlan_tkip",	/* IEEE80211_CIPHER_TKIP */
580ba2cbe9Sxc 	"wlan_aes_ocb",	/* IEEE80211_CIPHER_AES_OCB */
590ba2cbe9Sxc 	"wlan_ccmp",	/* IEEE80211_CIPHER_AES_CCM */
600ba2cbe9Sxc 	"wlan_ckip",	/* IEEE80211_CIPHER_CKIP */
610ba2cbe9Sxc };
620ba2cbe9Sxc 
630ba2cbe9Sxc /*
640ba2cbe9Sxc  * Default "null" key management routines.
650ba2cbe9Sxc  */
660ba2cbe9Sxc /* ARGSUSED */
670ba2cbe9Sxc static int
nulldev_key_alloc(ieee80211com_t * ic,const struct ieee80211_key * k,ieee80211_keyix * keyix,ieee80211_keyix * rxkeyix)680ba2cbe9Sxc nulldev_key_alloc(ieee80211com_t *ic, const struct ieee80211_key *k,
690ba2cbe9Sxc 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
700ba2cbe9Sxc {
710ba2cbe9Sxc 	*keyix = 0;	/* use key index 0 for ucast key */
720ba2cbe9Sxc 	*rxkeyix = IEEE80211_KEYIX_NONE;
730ba2cbe9Sxc 	return (1);
740ba2cbe9Sxc }
750ba2cbe9Sxc 
760ba2cbe9Sxc /* ARGSUSED */
770ba2cbe9Sxc static int
nulldev_key_delete(ieee80211com_t * ic,const struct ieee80211_key * k)780ba2cbe9Sxc nulldev_key_delete(ieee80211com_t *ic, const struct ieee80211_key *k)
790ba2cbe9Sxc {
800ba2cbe9Sxc 	return (1);
810ba2cbe9Sxc }
820ba2cbe9Sxc 
830ba2cbe9Sxc /* ARGSUSED */
840ba2cbe9Sxc static int
nulldev_key_set(ieee80211com_t * ic,const struct ieee80211_key * k,const uint8_t * mac)850ba2cbe9Sxc nulldev_key_set(ieee80211com_t *ic, const struct ieee80211_key *k,
860ba2cbe9Sxc 	const uint8_t *mac)
870ba2cbe9Sxc {
880ba2cbe9Sxc 	return (1);
890ba2cbe9Sxc }
900ba2cbe9Sxc 
910ba2cbe9Sxc /* ARGSUSED */
920ba2cbe9Sxc static void
nulldev_key_update(ieee80211com_t * ic)930ba2cbe9Sxc nulldev_key_update(ieee80211com_t *ic)
940ba2cbe9Sxc {
950ba2cbe9Sxc 	/* noop */
960ba2cbe9Sxc }
970ba2cbe9Sxc 
980ba2cbe9Sxc /*
990ba2cbe9Sxc  * Reset key state to an unused state.  The crypto
1000ba2cbe9Sxc  * key allocation mechanism insures other state (e.g.
1010ba2cbe9Sxc  * key data) is properly setup before a key is used.
1020ba2cbe9Sxc  */
1030ba2cbe9Sxc void
ieee80211_crypto_resetkey(ieee80211com_t * ic,struct ieee80211_key * k,ieee80211_keyix ix)1040ba2cbe9Sxc ieee80211_crypto_resetkey(ieee80211com_t *ic,
105239e91abShx     struct ieee80211_key *k, ieee80211_keyix ix)
1060ba2cbe9Sxc {
1070ba2cbe9Sxc 	k->wk_cipher = &ieee80211_cipher_none;
1080ba2cbe9Sxc 	k->wk_private = k->wk_cipher->ic_attach(ic, k);
1090ba2cbe9Sxc 	k->wk_keyix = ix;
1100ba2cbe9Sxc 	k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
1110ba2cbe9Sxc }
1120ba2cbe9Sxc 
1130ba2cbe9Sxc /*
1140ba2cbe9Sxc  * Establish a relationship between the specified key and cipher
1150ba2cbe9Sxc  * and, if necessary, allocate a hardware index from the driver.
1160ba2cbe9Sxc  * Note that when a fixed key index is required it must be specified
1170ba2cbe9Sxc  * and we blindly assign it w/o consulting the driver.
1180ba2cbe9Sxc  *
1190ba2cbe9Sxc  * This must be the first call applied to a key; all the other key
1200ba2cbe9Sxc  * routines assume wk_cipher is setup.
1210ba2cbe9Sxc  *
1220ba2cbe9Sxc  * Locking must be handled by the caller using:
1230ba2cbe9Sxc  *	ieee80211_key_update_begin(ic);
1240ba2cbe9Sxc  *	ieee80211_key_update_end(ic);
1250ba2cbe9Sxc  */
1260ba2cbe9Sxc int
ieee80211_crypto_newkey(ieee80211com_t * ic,int cipher,int flags,struct ieee80211_key * key)1270ba2cbe9Sxc ieee80211_crypto_newkey(ieee80211com_t *ic, int cipher, int flags,
1280ba2cbe9Sxc     struct ieee80211_key *key)
1290ba2cbe9Sxc {
1300ba2cbe9Sxc 	const struct ieee80211_cipher *cip;
1310ba2cbe9Sxc 	ieee80211_keyix keyix, rxkeyix;
1320ba2cbe9Sxc 	void *keyctx;
1330ba2cbe9Sxc 	uint16_t oflags;
1340ba2cbe9Sxc 
1350ba2cbe9Sxc 	/*
1360ba2cbe9Sxc 	 * Validate cipher and set reference to cipher routines.
1370ba2cbe9Sxc 	 */
1380ba2cbe9Sxc 	if (cipher >= IEEE80211_CIPHER_MAX) {
1390ba2cbe9Sxc 		ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_newkey: "
140239e91abShx 		    "invalid cipher %u\n", cipher);
1410ba2cbe9Sxc 		return (0);
1420ba2cbe9Sxc 	}
143239e91abShx 	cip = ic->ic_ciphers[cipher];
1440ba2cbe9Sxc 	/* already load all the ciphers, cip can't be NULL */
1450ba2cbe9Sxc 	if (cip == NULL) {
1460ba2cbe9Sxc 		ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_newkey: "
147239e91abShx 		    "unable to load cipher %u, module %s\n",
148239e91abShx 		    cipher, cipher < IEEE80211_N(cipher_modnames) ?
149239e91abShx 		    cipher_modnames[cipher] : "<unknown>");
1500ba2cbe9Sxc 		return (0);
1510ba2cbe9Sxc 	}
1520ba2cbe9Sxc 
1530ba2cbe9Sxc 	oflags = key->wk_flags;
1540ba2cbe9Sxc 	flags &= IEEE80211_KEY_COMMON;
1550ba2cbe9Sxc 	/*
1560ba2cbe9Sxc 	 * If the hardware does not support the cipher then
1570ba2cbe9Sxc 	 * fallback to a host-based implementation.
1580ba2cbe9Sxc 	 */
1590ba2cbe9Sxc 	if ((ic->ic_caps & (1<<cipher)) == 0) {
1600ba2cbe9Sxc 		ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_newkey: "
161239e91abShx 		    "no h/w support for cipher %s, falling back to s/w\n",
162239e91abShx 		    cip->ic_name);
1630ba2cbe9Sxc 		flags |= IEEE80211_KEY_SWCRYPT;
1640ba2cbe9Sxc 	}
165a399b765Szf 	/*
166a399b765Szf 	 * Hardware TKIP with software MIC is an important
167a399b765Szf 	 * combination; we handle it by flagging each key,
168a399b765Szf 	 * the cipher modules honor it.
169a399b765Szf 	 */
170a399b765Szf 	if (cipher == IEEE80211_CIPHER_TKIP &&
171a399b765Szf 	    (ic->ic_caps & IEEE80211_C_TKIPMIC) == 0) {
172a399b765Szf 		ieee80211_dbg(IEEE80211_MSG_CRYPTO,
173a399b765Szf 		    "no h/w support for TKIP MIC, falling back to s/w\n");
174a399b765Szf 		flags |= IEEE80211_KEY_SWMIC;
175a399b765Szf 	}
1760ba2cbe9Sxc 
1770ba2cbe9Sxc 	/*
1780ba2cbe9Sxc 	 * Bind cipher to key instance.  Note we do this
1790ba2cbe9Sxc 	 * after checking the device capabilities so the
1800ba2cbe9Sxc 	 * cipher module can optimize space usage based on
1810ba2cbe9Sxc 	 * whether or not it needs to do the cipher work.
1820ba2cbe9Sxc 	 */
1830ba2cbe9Sxc 	if (key->wk_cipher != cip || key->wk_flags != flags) {
1840ba2cbe9Sxc again:
1850ba2cbe9Sxc 		/*
1860ba2cbe9Sxc 		 * Fillin the flags so cipher modules can see s/w
1870ba2cbe9Sxc 		 * crypto requirements and potentially allocate
1880ba2cbe9Sxc 		 * different state and/or attach different method
1890ba2cbe9Sxc 		 * pointers.
1900ba2cbe9Sxc 		 */
1910ba2cbe9Sxc 		key->wk_flags = (uint16_t)flags;
1920ba2cbe9Sxc 		keyctx = cip->ic_attach(ic, key);
1930ba2cbe9Sxc 		if (keyctx == NULL) {
1940ba2cbe9Sxc 			ieee80211_dbg(IEEE80211_MSG_CRYPTO, "crypto_setkey: "
195239e91abShx 			    "unable to attach cipher %s\n", cip->ic_name);
1960ba2cbe9Sxc 			key->wk_flags = oflags;	/* restore old flags */
1970ba2cbe9Sxc 			return (0);
1980ba2cbe9Sxc 		}
1990ba2cbe9Sxc 		CIPHER_DETACH(key);		/* Detach old cipher */
2000ba2cbe9Sxc 		key->wk_cipher = cip;
2010ba2cbe9Sxc 		key->wk_private = keyctx;
2020ba2cbe9Sxc 	}
2030ba2cbe9Sxc 	/*
2040ba2cbe9Sxc 	 * Commit to requested usage so driver can see the flags.
2050ba2cbe9Sxc 	 */
2060ba2cbe9Sxc 	key->wk_flags = (uint16_t)flags;
2070ba2cbe9Sxc 
2080ba2cbe9Sxc 	/*
2090ba2cbe9Sxc 	 * Ask the driver for a key index if we don't have one.
2100ba2cbe9Sxc 	 * Note that entries in the global key table always have
2110ba2cbe9Sxc 	 * an index; this means it's safe to call this routine
2120ba2cbe9Sxc 	 * for these entries just to setup the reference to the
2130ba2cbe9Sxc 	 * cipher template.  Note also that when using software
2140ba2cbe9Sxc 	 * crypto we also call the driver to give us a key index.
2150ba2cbe9Sxc 	 */
2160ba2cbe9Sxc 	if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
2170ba2cbe9Sxc 		if (!DEV_KEY_ALLOC(ic, key, &keyix, &rxkeyix)) {
2180ba2cbe9Sxc 			/*
2190ba2cbe9Sxc 			 * Driver has no room; fallback to doing crypto
2200ba2cbe9Sxc 			 * in the host.  We change the flags and start the
2210ba2cbe9Sxc 			 * procedure over.  If we get back here then there's
2220ba2cbe9Sxc 			 * no hope and we bail.  Note that this can leave
2230ba2cbe9Sxc 			 * the key in a inconsistent state if the caller
2240ba2cbe9Sxc 			 * continues to use it.
2250ba2cbe9Sxc 			 */
2260ba2cbe9Sxc 			if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
2270ba2cbe9Sxc 				ieee80211_dbg(IEEE80211_MSG_CRYPTO,
228239e91abShx 				    "crypto_setkey: "
229239e91abShx 				    "no h/w resources for cipher %s, "
230239e91abShx 				    "falling back to s/w\n", cip->ic_name);
2310ba2cbe9Sxc 				oflags = key->wk_flags;
2320ba2cbe9Sxc 				flags |= IEEE80211_KEY_SWCRYPT;
2330ba2cbe9Sxc 				if (cipher == IEEE80211_CIPHER_TKIP)
2340ba2cbe9Sxc 					flags |= IEEE80211_KEY_SWMIC;
2350ba2cbe9Sxc 				goto again;
2360ba2cbe9Sxc 			}
2370ba2cbe9Sxc 			ieee80211_dbg(IEEE80211_MSG_CRYPTO, "crypto_setkey: "
238239e91abShx 			    "unable to setup cipher %s\n", cip->ic_name);
2390ba2cbe9Sxc 			return (0);
2400ba2cbe9Sxc 		}
2410ba2cbe9Sxc 		key->wk_keyix = keyix;
2420ba2cbe9Sxc 		key->wk_rxkeyix = rxkeyix;
2430ba2cbe9Sxc 	}
2440ba2cbe9Sxc 	return (1);
2450ba2cbe9Sxc }
2460ba2cbe9Sxc 
2470ba2cbe9Sxc /*
2480ba2cbe9Sxc  * Remove the key (no locking, for internal use).
2490ba2cbe9Sxc  */
2500ba2cbe9Sxc static int
ieee80211_crypto_delkey_locked(ieee80211com_t * ic,struct ieee80211_key * key)2510ba2cbe9Sxc ieee80211_crypto_delkey_locked(ieee80211com_t *ic, struct ieee80211_key *key)
2520ba2cbe9Sxc {
2530ba2cbe9Sxc 	uint16_t keyix;
2540ba2cbe9Sxc 
2550ba2cbe9Sxc 	ASSERT(key->wk_cipher != NULL);
2560ba2cbe9Sxc 
2570ba2cbe9Sxc 	keyix = key->wk_keyix;
2580ba2cbe9Sxc 	if (keyix != IEEE80211_KEYIX_NONE) {
2590ba2cbe9Sxc 		/*
2600ba2cbe9Sxc 		 * Remove hardware entry.
2610ba2cbe9Sxc 		 */
2620ba2cbe9Sxc 		if (!DEV_KEY_DELETE(ic, key)) {
2630ba2cbe9Sxc 			ieee80211_dbg(IEEE80211_MSG_CRYPTO,
264239e91abShx 			    "ieee80211_crypto_delkey_locked: ",
265239e91abShx 			    "driverdeletes key %u failed\n", keyix);
2660ba2cbe9Sxc 		}
2670ba2cbe9Sxc 	}
2680ba2cbe9Sxc 	CIPHER_DETACH(key);
2690ba2cbe9Sxc 	bzero(key, sizeof (struct ieee80211_key));
2700ba2cbe9Sxc 	/* NB: cannot depend on key index to decide this */
2710ba2cbe9Sxc 	ieee80211_crypto_resetkey(ic, key, IEEE80211_KEYIX_NONE);
2720ba2cbe9Sxc 	return (1);
2730ba2cbe9Sxc }
2740ba2cbe9Sxc 
2750ba2cbe9Sxc /*
2760ba2cbe9Sxc  * Remove the specified key.
2770ba2cbe9Sxc  */
2780ba2cbe9Sxc int
ieee80211_crypto_delkey(ieee80211com_t * ic,struct ieee80211_key * key)2790ba2cbe9Sxc ieee80211_crypto_delkey(ieee80211com_t *ic, struct ieee80211_key *key)
2800ba2cbe9Sxc {
2810ba2cbe9Sxc 	int status;
2820ba2cbe9Sxc 
2830ba2cbe9Sxc 	KEY_UPDATE_BEGIN(ic);
2840ba2cbe9Sxc 	status = ieee80211_crypto_delkey_locked(ic, key);
2850ba2cbe9Sxc 	KEY_UPDATE_END(ic);
2860ba2cbe9Sxc 	return (status);
2870ba2cbe9Sxc }
2880ba2cbe9Sxc 
2890ba2cbe9Sxc /*
2900ba2cbe9Sxc  * Clear the global key table.
2910ba2cbe9Sxc  */
2920ba2cbe9Sxc static void
ieee80211_crypto_delglobalkeys(ieee80211com_t * ic)2930ba2cbe9Sxc ieee80211_crypto_delglobalkeys(ieee80211com_t *ic)
2940ba2cbe9Sxc {
2950ba2cbe9Sxc 	int i;
2960ba2cbe9Sxc 
2970ba2cbe9Sxc 	KEY_UPDATE_BEGIN(ic);
2980ba2cbe9Sxc 	for (i = 0; i < IEEE80211_WEP_NKID; i++)
2990ba2cbe9Sxc 		(void) ieee80211_crypto_delkey_locked(ic, &ic->ic_nw_keys[i]);
3000ba2cbe9Sxc 	KEY_UPDATE_END(ic);
3010ba2cbe9Sxc }
3020ba2cbe9Sxc 
3030ba2cbe9Sxc /*
3040ba2cbe9Sxc  * Set the contents of the specified key.
3050ba2cbe9Sxc  *
3060ba2cbe9Sxc  * Locking must be handled by the caller using:
3070ba2cbe9Sxc  *	ieee80211_key_update_begin(ic);
3080ba2cbe9Sxc  *	ieee80211_key_update_end(ic);
3090ba2cbe9Sxc  */
3100ba2cbe9Sxc int
ieee80211_crypto_setkey(ieee80211com_t * ic,struct ieee80211_key * key,const uint8_t * macaddr)3110ba2cbe9Sxc ieee80211_crypto_setkey(ieee80211com_t *ic, struct ieee80211_key *key,
3120ba2cbe9Sxc     const uint8_t *macaddr)
3130ba2cbe9Sxc {
3140ba2cbe9Sxc 	const struct ieee80211_cipher *cip = key->wk_cipher;
3150ba2cbe9Sxc 
3160ba2cbe9Sxc 	ASSERT(cip != NULL);
3170ba2cbe9Sxc 
3180ba2cbe9Sxc 	ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_setkey: "
319239e91abShx 	    "%s keyix %u flags 0x%x mac %s len %u\n",
320239e91abShx 	    cip->ic_name, key->wk_keyix, key->wk_flags,
321239e91abShx 	    ieee80211_macaddr_sprintf(macaddr), key->wk_keylen);
3220ba2cbe9Sxc 
3230ba2cbe9Sxc 	/*
3240ba2cbe9Sxc 	 * Give cipher a chance to validate key contents.
3250ba2cbe9Sxc 	 * should happen before modifying state.
3260ba2cbe9Sxc 	 */
3270ba2cbe9Sxc 	if (cip->ic_setkey(key) == 0) {
3280ba2cbe9Sxc 		ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_setkey: "
329239e91abShx 		    "cipher %s rejected key index %u len %u flags 0x%x\n",
330239e91abShx 		    cip->ic_name, key->wk_keyix, key->wk_keylen,
331239e91abShx 		    key->wk_flags);
3320ba2cbe9Sxc 		return (0);
3330ba2cbe9Sxc 	}
3340ba2cbe9Sxc 	if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
3350ba2cbe9Sxc 		ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_setkey: "
336239e91abShx 		    "no key index; should not happen!\n");
3370ba2cbe9Sxc 		return (0);
3380ba2cbe9Sxc 	}
3390ba2cbe9Sxc 	return (DEV_KEY_SET(ic, key, macaddr));
3400ba2cbe9Sxc }
3410ba2cbe9Sxc 
3420ba2cbe9Sxc /*
3430ba2cbe9Sxc  * Return the transmit key to use in sending a frame.
3440ba2cbe9Sxc  */
3450ba2cbe9Sxc struct ieee80211_key *
ieee80211_crypto_getkey(ieee80211com_t * ic)3460ba2cbe9Sxc ieee80211_crypto_getkey(ieee80211com_t *ic)
3470ba2cbe9Sxc {
3480ba2cbe9Sxc 	if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
3490ba2cbe9Sxc 	    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
3500ba2cbe9Sxc 		return (NULL);
3510ba2cbe9Sxc 	return (&ic->ic_nw_keys[ic->ic_def_txkey]);
3520ba2cbe9Sxc }
3530ba2cbe9Sxc 
3540ba2cbe9Sxc uint8_t
ieee80211_crypto_getciphertype(ieee80211com_t * ic)3550ba2cbe9Sxc ieee80211_crypto_getciphertype(ieee80211com_t *ic)
3560ba2cbe9Sxc {
3570ba2cbe9Sxc 	struct ieee80211_key *key;
3580ba2cbe9Sxc 	uint32_t cipher;
3590ba2cbe9Sxc 	static const uint8_t ciphermap[] = {
3600ba2cbe9Sxc 		WIFI_SEC_WEP,	/* IEEE80211_CIPHER_WEP */
361a399b765Szf 		WIFI_SEC_WPA,	/* IEEE80211_CIPHER_TKIP */
3620ba2cbe9Sxc 		(uint8_t)-1,	/* IEEE80211_CIPHER_AES_OCB */
363a399b765Szf 		WIFI_SEC_WPA,	/* IEEE80211_CIPHER_AES_CCM */
3640ba2cbe9Sxc 		(uint8_t)-1,	/* IEEE80211_CIPHER_CKIP */
3650ba2cbe9Sxc 		WIFI_SEC_NONE,	/* IEEE80211_CIPHER_NONE */
3660ba2cbe9Sxc 	};
3670ba2cbe9Sxc 
3680ba2cbe9Sxc 	if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
3690ba2cbe9Sxc 		return (WIFI_SEC_NONE);
3700ba2cbe9Sxc 
3710ba2cbe9Sxc 	key = ieee80211_crypto_getkey(ic);
3720ba2cbe9Sxc 	if (key == NULL)
3730ba2cbe9Sxc 		return (WIFI_SEC_NONE);
3740ba2cbe9Sxc 
3750ba2cbe9Sxc 	cipher = key->wk_cipher->ic_cipher;
3760ba2cbe9Sxc 	ASSERT(cipher < IEEE80211_N(ciphermap));
3770ba2cbe9Sxc 	return (ciphermap[cipher]);
3780ba2cbe9Sxc }
3790ba2cbe9Sxc 
3800ba2cbe9Sxc /*
3810ba2cbe9Sxc  * Add privacy headers appropriate for the specified key.
3820ba2cbe9Sxc  */
3830ba2cbe9Sxc struct ieee80211_key *
ieee80211_crypto_encap(ieee80211com_t * ic,mblk_t * mp)3840ba2cbe9Sxc ieee80211_crypto_encap(ieee80211com_t *ic, mblk_t *mp)
3850ba2cbe9Sxc {
3860ba2cbe9Sxc 	struct ieee80211_key *k;
3870ba2cbe9Sxc 	const struct ieee80211_cipher *cip;
3880ba2cbe9Sxc 	uint8_t keyix;
3890ba2cbe9Sxc 
3900ba2cbe9Sxc 	if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE) {
3910ba2cbe9Sxc 		ieee80211_dbg(IEEE80211_MSG_CRYPTO,
392239e91abShx 		    "ieee80211_crypto_encap: %s",
393239e91abShx 		    " No default xmit key for frame\n");
3940ba2cbe9Sxc 		return (NULL);
3950ba2cbe9Sxc 	}
3960ba2cbe9Sxc 	keyix = ic->ic_def_txkey;
3970ba2cbe9Sxc 	k = &ic->ic_nw_keys[ic->ic_def_txkey];
3980ba2cbe9Sxc 	cip = k->wk_cipher;
3990ba2cbe9Sxc 	return (cip->ic_encap(k, mp, keyix<<6) ? k : NULL);
4000ba2cbe9Sxc }
4010ba2cbe9Sxc 
4020ba2cbe9Sxc /*
4030ba2cbe9Sxc  * Validate and strip privacy headers (and trailer) for a
4040ba2cbe9Sxc  * received frame that has the WEP/Privacy bit set.
4050ba2cbe9Sxc  */
4060ba2cbe9Sxc struct ieee80211_key *
ieee80211_crypto_decap(ieee80211com_t * ic,mblk_t * mp,int hdrlen)4070ba2cbe9Sxc ieee80211_crypto_decap(ieee80211com_t *ic, mblk_t *mp, int hdrlen)
4080ba2cbe9Sxc {
4090ba2cbe9Sxc 	struct ieee80211_key *k;
4100ba2cbe9Sxc 	const struct ieee80211_cipher *cip;
4110ba2cbe9Sxc 	uint8_t *ivp;
4120ba2cbe9Sxc 	uint8_t keyid;
4130ba2cbe9Sxc 
4140ba2cbe9Sxc 	/* NB: this minimum size data frame could be bigger */
415*ff3124efSff 	if (MBLKL(mp) < IEEE80211_WEP_MINLEN) {
4160ba2cbe9Sxc 		ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_decap:"
417239e91abShx 		    " WEP data frame too short, len %u\n",
418*ff3124efSff 		    MBLKL(mp));
4190ba2cbe9Sxc 		return (NULL);
4200ba2cbe9Sxc 	}
4210ba2cbe9Sxc 	/*
4220ba2cbe9Sxc 	 * Locate the key. If unicast and there is no unicast
4230ba2cbe9Sxc 	 * key then we fall back to the key id in the header.
4240ba2cbe9Sxc 	 * This assumes unicast keys are only configured when
4250ba2cbe9Sxc 	 * the key id in the header is meaningless (typically 0).
4260ba2cbe9Sxc 	 */
4270ba2cbe9Sxc 	ivp = mp->b_rptr + hdrlen;
4280ba2cbe9Sxc 	keyid = ivp[IEEE80211_WEP_IVLEN];
4290ba2cbe9Sxc 	k = &ic->ic_nw_keys[keyid >> 6];
4300ba2cbe9Sxc 
4310ba2cbe9Sxc 	/* check to avoid panic when wep is on but key is not set */
4320ba2cbe9Sxc 	if (k->wk_cipher == &ieee80211_cipher_none ||
4330ba2cbe9Sxc 	    k->wk_cipher == NULL)
4340ba2cbe9Sxc 		return (NULL);
4350ba2cbe9Sxc 
4360ba2cbe9Sxc 	cip = k->wk_cipher;
4370ba2cbe9Sxc 	return ((cip->ic_decap)(k, mp, hdrlen) ? k : NULL);
4380ba2cbe9Sxc }
4390ba2cbe9Sxc 
4400ba2cbe9Sxc /*
4410ba2cbe9Sxc  * Setup crypto support.
4420ba2cbe9Sxc  */
4430ba2cbe9Sxc void
ieee80211_crypto_attach(ieee80211com_t * ic)4440ba2cbe9Sxc ieee80211_crypto_attach(ieee80211com_t *ic)
4450ba2cbe9Sxc {
4460ba2cbe9Sxc 	struct ieee80211_crypto_state *cs = &ic->ic_crypto;
4470ba2cbe9Sxc 	int i;
4480ba2cbe9Sxc 
4493a1a8936Szf 	(void) crypto_mech2id(SUN_CKM_RC4); /* Load RC4 */
4503a1a8936Szf 	(void) crypto_mech2id(SUN_CKM_AES_CBC); /* Load AES-CBC */
4513a1a8936Szf 	(void) crypto_mech2id(SUN_CKM_AES_CCM); /* Load AES-CCM */
4523a1a8936Szf 
4530ba2cbe9Sxc 	/* NB: we assume everything is pre-zero'd */
4540ba2cbe9Sxc 	cs->cs_def_txkey = IEEE80211_KEYIX_NONE;
4550ba2cbe9Sxc 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
4560ba2cbe9Sxc 		ieee80211_crypto_resetkey(ic, &cs->cs_nw_keys[i],
457239e91abShx 		    IEEE80211_KEYIX_NONE);
4580ba2cbe9Sxc 	}
4590ba2cbe9Sxc 
4600ba2cbe9Sxc 	/*
4610ba2cbe9Sxc 	 * Initialize the driver key support routines to noop entries.
4620ba2cbe9Sxc 	 * This is useful especially for the cipher test modules.
4630ba2cbe9Sxc 	 */
4640ba2cbe9Sxc 	cs->cs_key_alloc = nulldev_key_alloc;
4650ba2cbe9Sxc 	cs->cs_key_set = nulldev_key_set;
4660ba2cbe9Sxc 	cs->cs_key_delete = nulldev_key_delete;
4670ba2cbe9Sxc 	cs->cs_key_update_begin = nulldev_key_update;
4680ba2cbe9Sxc 	cs->cs_key_update_end = nulldev_key_update;
4690ba2cbe9Sxc 
470239e91abShx 	ieee80211_crypto_register(ic, &wep);
471239e91abShx 	ieee80211_crypto_register(ic, &tkip);
472239e91abShx 	ieee80211_crypto_register(ic, &ccmp);
4730ba2cbe9Sxc }
4740ba2cbe9Sxc 
4750ba2cbe9Sxc /*
4760ba2cbe9Sxc  * Teardown crypto support.
4770ba2cbe9Sxc  */
4780ba2cbe9Sxc void
ieee80211_crypto_detach(ieee80211com_t * ic)4790ba2cbe9Sxc ieee80211_crypto_detach(ieee80211com_t *ic)
4800ba2cbe9Sxc {
4810ba2cbe9Sxc 	ieee80211_crypto_delglobalkeys(ic);
4820ba2cbe9Sxc 
483239e91abShx 	ieee80211_crypto_unregister(ic, &wep);
484239e91abShx 	ieee80211_crypto_unregister(ic, &tkip);
485239e91abShx 	ieee80211_crypto_unregister(ic, &ccmp);
4860ba2cbe9Sxc }
4870ba2cbe9Sxc 
4880ba2cbe9Sxc /*
4890ba2cbe9Sxc  * Register a crypto cipher module.
4900ba2cbe9Sxc  */
4910ba2cbe9Sxc void
ieee80211_crypto_register(ieee80211com_t * ic,const struct ieee80211_cipher * cip)492239e91abShx ieee80211_crypto_register(ieee80211com_t *ic,
493239e91abShx     const struct ieee80211_cipher *cip)
4940ba2cbe9Sxc {
4950ba2cbe9Sxc 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
4960ba2cbe9Sxc 		ieee80211_err("ieee80211_crypto_register: "
497239e91abShx 		    "cipher %s has an invalid cipher index %u\n",
498239e91abShx 		    cip->ic_name, cip->ic_cipher);
4990ba2cbe9Sxc 		return;
5000ba2cbe9Sxc 	}
501239e91abShx 	if (ic->ic_ciphers[cip->ic_cipher] != NULL &&
502239e91abShx 	    ic->ic_ciphers[cip->ic_cipher] != cip) {
5030ba2cbe9Sxc 		ieee80211_err("ieee80211_crypto_register: "
504239e91abShx 		    "cipher %s registered with a different template\n",
505239e91abShx 		    cip->ic_name);
5060ba2cbe9Sxc 		return;
5070ba2cbe9Sxc 	}
508239e91abShx 	ic->ic_ciphers[cip->ic_cipher] = cip;
5090ba2cbe9Sxc }
5100ba2cbe9Sxc 
5110ba2cbe9Sxc /*
5120ba2cbe9Sxc  * Unregister a crypto cipher module.
5130ba2cbe9Sxc  */
5140ba2cbe9Sxc void
ieee80211_crypto_unregister(ieee80211com_t * ic,const struct ieee80211_cipher * cip)515239e91abShx ieee80211_crypto_unregister(ieee80211com_t *ic,
516239e91abShx     const struct ieee80211_cipher *cip)
5170ba2cbe9Sxc {
5180ba2cbe9Sxc 	if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
5190ba2cbe9Sxc 		ieee80211_err("ieee80211_crypto_unregister: "
520239e91abShx 		    "cipher %s has an invalid cipher index %u\n",
521239e91abShx 		    cip->ic_name, cip->ic_cipher);
5220ba2cbe9Sxc 		return;
5230ba2cbe9Sxc 	}
524239e91abShx 	if (ic->ic_ciphers[cip->ic_cipher] != NULL &&
525239e91abShx 	    ic->ic_ciphers[cip->ic_cipher] != cip) {
5260ba2cbe9Sxc 		ieee80211_err("ieee80211_crypto_unregister: "
527239e91abShx 		    "cipher %s registered with a different template\n",
528239e91abShx 		    cip->ic_name);
5290ba2cbe9Sxc 		return;
5300ba2cbe9Sxc 	}
5310ba2cbe9Sxc 	/* NB: don't complain about not being registered */
532239e91abShx 	ic->ic_ciphers[cip->ic_cipher] = NULL;
5330ba2cbe9Sxc }
534