2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
7 * Copyright (c) 2001 Atsushi Onoe
8 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * Alternatively, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2 as published by the Free
24 * Software Foundation.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #pragma ident "%Z%%M% %I% %E% SMI"
41 * IEEE 802.11 generic crypto support
43 #include <sys/types.h>
45 #include <sys/crypto/common.h>
46 #include <sys/crypto/api.h>
47 #include <sys/strsun.h>
48 #include "net80211_impl.h"
50 extern const struct ieee80211_cipher wep
;
51 extern const struct ieee80211_cipher tkip
;
52 extern const struct ieee80211_cipher ccmp
;
55 * Table of registered cipher modules.
57 static const char *cipher_modnames
[] = {
58 "wlan_wep", /* IEEE80211_CIPHER_WEP */
59 "wlan_tkip", /* IEEE80211_CIPHER_TKIP */
60 "wlan_aes_ocb", /* IEEE80211_CIPHER_AES_OCB */
61 "wlan_ccmp", /* IEEE80211_CIPHER_AES_CCM */
62 "wlan_ckip", /* IEEE80211_CIPHER_CKIP */
66 * Default "null" key management routines.
70 nulldev_key_alloc(ieee80211com_t
*ic
, const struct ieee80211_key
*k
,
71 ieee80211_keyix
*keyix
, ieee80211_keyix
*rxkeyix
)
73 *keyix
= 0; /* use key index 0 for ucast key */
74 *rxkeyix
= IEEE80211_KEYIX_NONE
;
80 nulldev_key_delete(ieee80211com_t
*ic
, const struct ieee80211_key
*k
)
87 nulldev_key_set(ieee80211com_t
*ic
, const struct ieee80211_key
*k
,
95 nulldev_key_update(ieee80211com_t
*ic
)
101 * Reset key state to an unused state. The crypto
102 * key allocation mechanism insures other state (e.g.
103 * key data) is properly setup before a key is used.
106 ieee80211_crypto_resetkey(ieee80211com_t
*ic
,
107 struct ieee80211_key
*k
, ieee80211_keyix ix
)
109 k
->wk_cipher
= &ieee80211_cipher_none
;
110 k
->wk_private
= k
->wk_cipher
->ic_attach(ic
, k
);
112 k
->wk_flags
= IEEE80211_KEY_XMIT
| IEEE80211_KEY_RECV
;
116 * Establish a relationship between the specified key and cipher
117 * and, if necessary, allocate a hardware index from the driver.
118 * Note that when a fixed key index is required it must be specified
119 * and we blindly assign it w/o consulting the driver.
121 * This must be the first call applied to a key; all the other key
122 * routines assume wk_cipher is setup.
124 * Locking must be handled by the caller using:
125 * ieee80211_key_update_begin(ic);
126 * ieee80211_key_update_end(ic);
129 ieee80211_crypto_newkey(ieee80211com_t
*ic
, int cipher
, int flags
,
130 struct ieee80211_key
*key
)
132 const struct ieee80211_cipher
*cip
;
133 ieee80211_keyix keyix
, rxkeyix
;
138 * Validate cipher and set reference to cipher routines.
140 if (cipher
>= IEEE80211_CIPHER_MAX
) {
141 ieee80211_dbg(IEEE80211_MSG_CRYPTO
, "ieee80211_crypto_newkey: "
142 "invalid cipher %u\n", cipher
);
145 cip
= ic
->ic_ciphers
[cipher
];
146 /* already load all the ciphers, cip can't be NULL */
148 ieee80211_dbg(IEEE80211_MSG_CRYPTO
, "ieee80211_crypto_newkey: "
149 "unable to load cipher %u, module %s\n",
150 cipher
, cipher
< IEEE80211_N(cipher_modnames
) ?
151 cipher_modnames
[cipher
] : "<unknown>");
155 oflags
= key
->wk_flags
;
156 flags
&= IEEE80211_KEY_COMMON
;
158 * If the hardware does not support the cipher then
159 * fallback to a host-based implementation.
161 if ((ic
->ic_caps
& (1<<cipher
)) == 0) {
162 ieee80211_dbg(IEEE80211_MSG_CRYPTO
, "ieee80211_crypto_newkey: "
163 "no h/w support for cipher %s, falling back to s/w\n",
165 flags
|= IEEE80211_KEY_SWCRYPT
;
168 * Hardware TKIP with software MIC is an important
169 * combination; we handle it by flagging each key,
170 * the cipher modules honor it.
172 if (cipher
== IEEE80211_CIPHER_TKIP
&&
173 (ic
->ic_caps
& IEEE80211_C_TKIPMIC
) == 0) {
174 ieee80211_dbg(IEEE80211_MSG_CRYPTO
,
175 "no h/w support for TKIP MIC, falling back to s/w\n");
176 flags
|= IEEE80211_KEY_SWMIC
;
180 * Bind cipher to key instance. Note we do this
181 * after checking the device capabilities so the
182 * cipher module can optimize space usage based on
183 * whether or not it needs to do the cipher work.
185 if (key
->wk_cipher
!= cip
|| key
->wk_flags
!= flags
) {
188 * Fillin the flags so cipher modules can see s/w
189 * crypto requirements and potentially allocate
190 * different state and/or attach different method
193 key
->wk_flags
= (uint16_t)flags
;
194 keyctx
= cip
->ic_attach(ic
, key
);
195 if (keyctx
== NULL
) {
196 ieee80211_dbg(IEEE80211_MSG_CRYPTO
, "crypto_setkey: "
197 "unable to attach cipher %s\n", cip
->ic_name
);
198 key
->wk_flags
= oflags
; /* restore old flags */
201 CIPHER_DETACH(key
); /* Detach old cipher */
202 key
->wk_cipher
= cip
;
203 key
->wk_private
= keyctx
;
206 * Commit to requested usage so driver can see the flags.
208 key
->wk_flags
= (uint16_t)flags
;
211 * Ask the driver for a key index if we don't have one.
212 * Note that entries in the global key table always have
213 * an index; this means it's safe to call this routine
214 * for these entries just to setup the reference to the
215 * cipher template. Note also that when using software
216 * crypto we also call the driver to give us a key index.
218 if (key
->wk_keyix
== IEEE80211_KEYIX_NONE
) {
219 if (!DEV_KEY_ALLOC(ic
, key
, &keyix
, &rxkeyix
)) {
221 * Driver has no room; fallback to doing crypto
222 * in the host. We change the flags and start the
223 * procedure over. If we get back here then there's
224 * no hope and we bail. Note that this can leave
225 * the key in a inconsistent state if the caller
226 * continues to use it.
228 if ((key
->wk_flags
& IEEE80211_KEY_SWCRYPT
) == 0) {
229 ieee80211_dbg(IEEE80211_MSG_CRYPTO
,
231 "no h/w resources for cipher %s, "
232 "falling back to s/w\n", cip
->ic_name
);
233 oflags
= key
->wk_flags
;
234 flags
|= IEEE80211_KEY_SWCRYPT
;
235 if (cipher
== IEEE80211_CIPHER_TKIP
)
236 flags
|= IEEE80211_KEY_SWMIC
;
239 ieee80211_dbg(IEEE80211_MSG_CRYPTO
, "crypto_setkey: "
240 "unable to setup cipher %s\n", cip
->ic_name
);
243 key
->wk_keyix
= keyix
;
244 key
->wk_rxkeyix
= rxkeyix
;
250 * Remove the key (no locking, for internal use).
253 ieee80211_crypto_delkey_locked(ieee80211com_t
*ic
, struct ieee80211_key
*key
)
257 ASSERT(key
->wk_cipher
!= NULL
);
259 keyix
= key
->wk_keyix
;
260 if (keyix
!= IEEE80211_KEYIX_NONE
) {
262 * Remove hardware entry.
264 if (!DEV_KEY_DELETE(ic
, key
)) {
265 ieee80211_dbg(IEEE80211_MSG_CRYPTO
,
266 "ieee80211_crypto_delkey_locked: ",
267 "driverdeletes key %u failed\n", keyix
);
271 bzero(key
, sizeof (struct ieee80211_key
));
272 /* NB: cannot depend on key index to decide this */
273 ieee80211_crypto_resetkey(ic
, key
, IEEE80211_KEYIX_NONE
);
278 * Remove the specified key.
281 ieee80211_crypto_delkey(ieee80211com_t
*ic
, struct ieee80211_key
*key
)
285 KEY_UPDATE_BEGIN(ic
);
286 status
= ieee80211_crypto_delkey_locked(ic
, key
);
292 * Clear the global key table.
295 ieee80211_crypto_delglobalkeys(ieee80211com_t
*ic
)
299 KEY_UPDATE_BEGIN(ic
);
300 for (i
= 0; i
< IEEE80211_WEP_NKID
; i
++)
301 (void) ieee80211_crypto_delkey_locked(ic
, &ic
->ic_nw_keys
[i
]);
306 * Set the contents of the specified key.
308 * Locking must be handled by the caller using:
309 * ieee80211_key_update_begin(ic);
310 * ieee80211_key_update_end(ic);
313 ieee80211_crypto_setkey(ieee80211com_t
*ic
, struct ieee80211_key
*key
,
314 const uint8_t *macaddr
)
316 const struct ieee80211_cipher
*cip
= key
->wk_cipher
;
320 ieee80211_dbg(IEEE80211_MSG_CRYPTO
, "ieee80211_crypto_setkey: "
321 "%s keyix %u flags 0x%x mac %s len %u\n",
322 cip
->ic_name
, key
->wk_keyix
, key
->wk_flags
,
323 ieee80211_macaddr_sprintf(macaddr
), key
->wk_keylen
);
326 * Give cipher a chance to validate key contents.
327 * should happen before modifying state.
329 if (cip
->ic_setkey(key
) == 0) {
330 ieee80211_dbg(IEEE80211_MSG_CRYPTO
, "ieee80211_crypto_setkey: "
331 "cipher %s rejected key index %u len %u flags 0x%x\n",
332 cip
->ic_name
, key
->wk_keyix
, key
->wk_keylen
,
336 if (key
->wk_keyix
== IEEE80211_KEYIX_NONE
) {
337 ieee80211_dbg(IEEE80211_MSG_CRYPTO
, "ieee80211_crypto_setkey: "
338 "no key index; should not happen!\n");
341 return (DEV_KEY_SET(ic
, key
, macaddr
));
345 * Return the transmit key to use in sending a frame.
347 struct ieee80211_key
*
348 ieee80211_crypto_getkey(ieee80211com_t
*ic
)
350 if (ic
->ic_def_txkey
== IEEE80211_KEYIX_NONE
||
351 KEY_UNDEFINED(ic
->ic_nw_keys
[ic
->ic_def_txkey
]))
353 return (&ic
->ic_nw_keys
[ic
->ic_def_txkey
]);
357 ieee80211_crypto_getciphertype(ieee80211com_t
*ic
)
359 struct ieee80211_key
*key
;
361 static const uint8_t ciphermap
[] = {
362 WIFI_SEC_WEP
, /* IEEE80211_CIPHER_WEP */
363 WIFI_SEC_WPA
, /* IEEE80211_CIPHER_TKIP */
364 (uint8_t)-1, /* IEEE80211_CIPHER_AES_OCB */
365 WIFI_SEC_WPA
, /* IEEE80211_CIPHER_AES_CCM */
366 (uint8_t)-1, /* IEEE80211_CIPHER_CKIP */
367 WIFI_SEC_NONE
, /* IEEE80211_CIPHER_NONE */
370 if ((ic
->ic_flags
& IEEE80211_F_PRIVACY
) == 0)
371 return (WIFI_SEC_NONE
);
373 key
= ieee80211_crypto_getkey(ic
);
375 return (WIFI_SEC_NONE
);
377 cipher
= key
->wk_cipher
->ic_cipher
;
378 ASSERT(cipher
< IEEE80211_N(ciphermap
));
379 return (ciphermap
[cipher
]);
383 * Add privacy headers appropriate for the specified key.
385 struct ieee80211_key
*
386 ieee80211_crypto_encap(ieee80211com_t
*ic
, mblk_t
*mp
)
388 struct ieee80211_key
*k
;
389 const struct ieee80211_cipher
*cip
;
392 if (ic
->ic_def_txkey
== IEEE80211_KEYIX_NONE
) {
393 ieee80211_dbg(IEEE80211_MSG_CRYPTO
,
394 "ieee80211_crypto_encap: %s",
395 " No default xmit key for frame\n");
398 keyix
= ic
->ic_def_txkey
;
399 k
= &ic
->ic_nw_keys
[ic
->ic_def_txkey
];
401 return (cip
->ic_encap(k
, mp
, keyix
<<6) ? k
: NULL
);
405 * Validate and strip privacy headers (and trailer) for a
406 * received frame that has the WEP/Privacy bit set.
408 struct ieee80211_key
*
409 ieee80211_crypto_decap(ieee80211com_t
*ic
, mblk_t
*mp
, int hdrlen
)
411 struct ieee80211_key
*k
;
412 const struct ieee80211_cipher
*cip
;
416 /* NB: this minimum size data frame could be bigger */
417 if (MBLKL(mp
) < IEEE80211_WEP_MINLEN
) {
418 ieee80211_dbg(IEEE80211_MSG_CRYPTO
, "ieee80211_crypto_decap:"
419 " WEP data frame too short, len %u\n",
424 * Locate the key. If unicast and there is no unicast
425 * key then we fall back to the key id in the header.
426 * This assumes unicast keys are only configured when
427 * the key id in the header is meaningless (typically 0).
429 ivp
= mp
->b_rptr
+ hdrlen
;
430 keyid
= ivp
[IEEE80211_WEP_IVLEN
];
431 k
= &ic
->ic_nw_keys
[keyid
>> 6];
433 /* check to avoid panic when wep is on but key is not set */
434 if (k
->wk_cipher
== &ieee80211_cipher_none
||
435 k
->wk_cipher
== NULL
)
439 return ((cip
->ic_decap
)(k
, mp
, hdrlen
) ? k
: NULL
);
443 * Setup crypto support.
446 ieee80211_crypto_attach(ieee80211com_t
*ic
)
448 struct ieee80211_crypto_state
*cs
= &ic
->ic_crypto
;
451 (void) crypto_mech2id(SUN_CKM_RC4
); /* Load RC4 */
452 (void) crypto_mech2id(SUN_CKM_AES_CBC
); /* Load AES-CBC */
453 (void) crypto_mech2id(SUN_CKM_AES_CCM
); /* Load AES-CCM */
455 /* NB: we assume everything is pre-zero'd */
456 cs
->cs_def_txkey
= IEEE80211_KEYIX_NONE
;
457 for (i
= 0; i
< IEEE80211_WEP_NKID
; i
++) {
458 ieee80211_crypto_resetkey(ic
, &cs
->cs_nw_keys
[i
],
459 IEEE80211_KEYIX_NONE
);
463 * Initialize the driver key support routines to noop entries.
464 * This is useful especially for the cipher test modules.
466 cs
->cs_key_alloc
= nulldev_key_alloc
;
467 cs
->cs_key_set
= nulldev_key_set
;
468 cs
->cs_key_delete
= nulldev_key_delete
;
469 cs
->cs_key_update_begin
= nulldev_key_update
;
470 cs
->cs_key_update_end
= nulldev_key_update
;
472 ieee80211_crypto_register(ic
, &wep
);
473 ieee80211_crypto_register(ic
, &tkip
);
474 ieee80211_crypto_register(ic
, &ccmp
);
478 * Teardown crypto support.
481 ieee80211_crypto_detach(ieee80211com_t
*ic
)
483 ieee80211_crypto_delglobalkeys(ic
);
485 ieee80211_crypto_unregister(ic
, &wep
);
486 ieee80211_crypto_unregister(ic
, &tkip
);
487 ieee80211_crypto_unregister(ic
, &ccmp
);
491 * Register a crypto cipher module.
494 ieee80211_crypto_register(ieee80211com_t
*ic
,
495 const struct ieee80211_cipher
*cip
)
497 if (cip
->ic_cipher
>= IEEE80211_CIPHER_MAX
) {
498 ieee80211_err("ieee80211_crypto_register: "
499 "cipher %s has an invalid cipher index %u\n",
500 cip
->ic_name
, cip
->ic_cipher
);
503 if (ic
->ic_ciphers
[cip
->ic_cipher
] != NULL
&&
504 ic
->ic_ciphers
[cip
->ic_cipher
] != cip
) {
505 ieee80211_err("ieee80211_crypto_register: "
506 "cipher %s registered with a different template\n",
510 ic
->ic_ciphers
[cip
->ic_cipher
] = cip
;
514 * Unregister a crypto cipher module.
517 ieee80211_crypto_unregister(ieee80211com_t
*ic
,
518 const struct ieee80211_cipher
*cip
)
520 if (cip
->ic_cipher
>= IEEE80211_CIPHER_MAX
) {
521 ieee80211_err("ieee80211_crypto_unregister: "
522 "cipher %s has an invalid cipher index %u\n",
523 cip
->ic_name
, cip
->ic_cipher
);
526 if (ic
->ic_ciphers
[cip
->ic_cipher
] != NULL
&&
527 ic
->ic_ciphers
[cip
->ic_cipher
] != cip
) {
528 ieee80211_err("ieee80211_crypto_unregister: "
529 "cipher %s registered with a different template\n",
533 /* NB: don't complain about not being registered */
534 ic
->ic_ciphers
[cip
->ic_cipher
] = NULL
;