ff3124eff995e6cd8ebd8c6543648e0670920034ff * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Use is subject to license terms.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Copyright (c) 2001 Atsushi Onoe
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * All rights reserved.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Redistribution and use in source and binary forms, with or without
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * modification, are permitted provided that the following conditions
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * are met:
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * 1. Redistributions of source code must retain the above copyright
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * notice, this list of conditions and the following disclaimer.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * 2. Redistributions in binary form must reproduce the above copyright
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * notice, this list of conditions and the following disclaimer in the
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * documentation and/or other materials provided with the distribution.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * 3. The name of the author may not be used to endorse or promote products
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * derived from this software without specific prior written permission.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Alternatively, this software may be distributed under the terms of the
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * GNU General Public License ("GPL") version 2 as published by the Free
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Software Foundation.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc#pragma ident "%Z%%M% %I% %E% SMI"
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * IEEE 802.11 generic crypto support
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Table of registered cipher modules.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Default "null" key management routines.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc/* ARGSUSED */
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcnulldev_key_alloc(ieee80211com_t *ic, const struct ieee80211_key *k,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return (1);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc/* ARGSUSED */
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcnulldev_key_delete(ieee80211com_t *ic, const struct ieee80211_key *k)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return (1);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc/* ARGSUSED */
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcnulldev_key_set(ieee80211com_t *ic, const struct ieee80211_key *k,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return (1);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc/* ARGSUSED */
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcstatic void
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* noop */
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Reset key state to an unused state. The crypto
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * key allocation mechanism insures other state (e.g.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * key data) is properly setup before a key is used.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Establish a relationship between the specified key and cipher
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * and, if necessary, allocate a hardware index from the driver.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Note that when a fixed key index is required it must be specified
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * and we blindly assign it w/o consulting the driver.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * This must be the first call applied to a key; all the other key
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * routines assume wk_cipher is setup.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Locking must be handled by the caller using:
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * ieee80211_key_update_begin(ic);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * ieee80211_key_update_end(ic);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcieee80211_crypto_newkey(ieee80211com_t *ic, int cipher, int flags,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Validate cipher and set reference to cipher routines.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_newkey: "
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return (0);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* already load all the ciphers, cip can't be NULL */
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_newkey: "
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "unable to load cipher %u, module %s\n",
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return (0);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * If the hardware does not support the cipher then
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * fallback to a host-based implementation.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_newkey: "
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "no h/w support for cipher %s, falling back to s/w\n",
a399b7655a1d835aa8606c2b29e4e777baac8635zf * Hardware TKIP with software MIC is an important
a399b7655a1d835aa8606c2b29e4e777baac8635zf * combination; we handle it by flagging each key,
a399b7655a1d835aa8606c2b29e4e777baac8635zf * the cipher modules honor it.
a399b7655a1d835aa8606c2b29e4e777baac8635zf "no h/w support for TKIP MIC, falling back to s/w\n");
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Bind cipher to key instance. Note we do this
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * after checking the device capabilities so the
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * cipher module can optimize space usage based on
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * whether or not it needs to do the cipher work.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Fillin the flags so cipher modules can see s/w
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * crypto requirements and potentially allocate
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * different state and/or attach different method
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * pointers.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return (0);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Commit to requested usage so driver can see the flags.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Ask the driver for a key index if we don't have one.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Note that entries in the global key table always have
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * an index; this means it's safe to call this routine
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * for these entries just to setup the reference to the
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * cipher template. Note also that when using software
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * crypto we also call the driver to give us a key index.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Driver has no room; fallback to doing crypto
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * in the host. We change the flags and start the
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * procedure over. If we get back here then there's
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * no hope and we bail. Note that this can leave
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * the key in a inconsistent state if the caller
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * continues to use it.
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "crypto_setkey: "
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "no h/w resources for cipher %s, "
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return (0);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return (1);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Remove the key (no locking, for internal use).
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcieee80211_crypto_delkey_locked(ieee80211com_t *ic, struct ieee80211_key *key)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Remove hardware entry.
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "ieee80211_crypto_delkey_locked: ",
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* NB: cannot depend on key index to decide this */
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc ieee80211_crypto_resetkey(ic, key, IEEE80211_KEYIX_NONE);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return (1);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Remove the specified key.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcieee80211_crypto_delkey(ieee80211com_t *ic, struct ieee80211_key *key)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Clear the global key table.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcstatic void
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc for (i = 0; i < IEEE80211_WEP_NKID; i++)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc (void) ieee80211_crypto_delkey_locked(ic, &ic->ic_nw_keys[i]);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Set the contents of the specified key.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Locking must be handled by the caller using:
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * ieee80211_key_update_begin(ic);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * ieee80211_key_update_end(ic);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcieee80211_crypto_setkey(ieee80211com_t *ic, struct ieee80211_key *key,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_setkey: "
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "%s keyix %u flags 0x%x mac %s len %u\n",
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Give cipher a chance to validate key contents.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * should happen before modifying state.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_setkey: "
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "cipher %s rejected key index %u len %u flags 0x%x\n",
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return (0);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_setkey: "
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "no key index; should not happen!\n");
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return (0);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Return the transmit key to use in sending a frame.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Add privacy headers appropriate for the specified key.
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "ieee80211_crypto_encap: %s",
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx " No default xmit key for frame\n");
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Validate and strip privacy headers (and trailer) for a
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * received frame that has the WEP/Privacy bit set.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcieee80211_crypto_decap(ieee80211com_t *ic, mblk_t *mp, int hdrlen)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* NB: this minimum size data frame could be bigger */
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_decap:"
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx " WEP data frame too short, len %u\n",
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Locate the key. If unicast and there is no unicast
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * key then we fall back to the key id in the header.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * This assumes unicast keys are only configured when
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * the key id in the header is meaningless (typically 0).
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* check to avoid panic when wep is on but key is not set */
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Setup crypto support.
3a1a8936dac0ebe7e956fa122b0b0d15e62d4108zf (void) crypto_mech2id(SUN_CKM_AES_CBC); /* Load AES-CBC */
3a1a8936dac0ebe7e956fa122b0b0d15e62d4108zf (void) crypto_mech2id(SUN_CKM_AES_CCM); /* Load AES-CCM */
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* NB: we assume everything is pre-zero'd */
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc for (i = 0; i < IEEE80211_WEP_NKID; i++) {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Initialize the driver key support routines to noop entries.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * This is useful especially for the cipher test modules.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Teardown crypto support.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Register a crypto cipher module.
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "cipher %s has an invalid cipher index %u\n",
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "cipher %s registered with a different template\n",
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Unregister a crypto cipher module.
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "cipher %s has an invalid cipher index %u\n",
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx "cipher %s registered with a different template\n",
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* NB: don't complain about not being registered */