1 /*
2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
5
6 /*
7 * Copyright (c) 2001 Atsushi Onoe
8 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
21 *
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.
25 *
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.
36 */
37
38 /*
39 * IEEE 802.11 generic crypto support
40 */
41 #include <sys/types.h>
42 #include <sys/note.h>
43 #include <sys/crypto/common.h>
44 #include <sys/crypto/api.h>
45 #include <sys/strsun.h>
46 #include "net80211_impl.h"
47
48 extern const struct ieee80211_cipher wep;
49 extern const struct ieee80211_cipher tkip;
50 extern const struct ieee80211_cipher ccmp;
51
52 /*
53 * Table of registered cipher modules.
54 */
55 static const char *cipher_modnames[] = {
56 "wlan_wep", /* IEEE80211_CIPHER_WEP */
57 "wlan_tkip", /* IEEE80211_CIPHER_TKIP */
58 "wlan_aes_ocb", /* IEEE80211_CIPHER_AES_OCB */
59 "wlan_ccmp", /* IEEE80211_CIPHER_AES_CCM */
60 "wlan_ckip", /* IEEE80211_CIPHER_CKIP */
61 };
62
63 /*
64 * Default "null" key management routines.
65 */
66 /* ARGSUSED */
67 static int
nulldev_key_alloc(ieee80211com_t * ic,const struct ieee80211_key * k,ieee80211_keyix * keyix,ieee80211_keyix * rxkeyix)68 nulldev_key_alloc(ieee80211com_t *ic, const struct ieee80211_key *k,
69 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
70 {
71 *keyix = 0; /* use key index 0 for ucast key */
72 *rxkeyix = IEEE80211_KEYIX_NONE;
73 return (1);
74 }
75
76 /* ARGSUSED */
77 static int
nulldev_key_delete(ieee80211com_t * ic,const struct ieee80211_key * k)78 nulldev_key_delete(ieee80211com_t *ic, const struct ieee80211_key *k)
79 {
80 return (1);
81 }
82
83 /* ARGSUSED */
84 static int
nulldev_key_set(ieee80211com_t * ic,const struct ieee80211_key * k,const uint8_t * mac)85 nulldev_key_set(ieee80211com_t *ic, const struct ieee80211_key *k,
86 const uint8_t *mac)
87 {
88 return (1);
89 }
90
91 /* ARGSUSED */
92 static void
nulldev_key_update(ieee80211com_t * ic)93 nulldev_key_update(ieee80211com_t *ic)
94 {
95 /* noop */
96 }
97
98 /*
99 * Reset key state to an unused state. The crypto
100 * key allocation mechanism insures other state (e.g.
101 * key data) is properly setup before a key is used.
102 */
103 void
ieee80211_crypto_resetkey(ieee80211com_t * ic,struct ieee80211_key * k,ieee80211_keyix ix)104 ieee80211_crypto_resetkey(ieee80211com_t *ic,
105 struct ieee80211_key *k, ieee80211_keyix ix)
106 {
107 k->wk_cipher = &ieee80211_cipher_none;
108 k->wk_private = k->wk_cipher->ic_attach(ic, k);
109 k->wk_keyix = ix;
110 k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
111 }
112
113 /*
114 * Establish a relationship between the specified key and cipher
115 * and, if necessary, allocate a hardware index from the driver.
116 * Note that when a fixed key index is required it must be specified
117 * and we blindly assign it w/o consulting the driver.
118 *
119 * This must be the first call applied to a key; all the other key
120 * routines assume wk_cipher is setup.
121 *
122 * Locking must be handled by the caller using:
123 * ieee80211_key_update_begin(ic);
124 * ieee80211_key_update_end(ic);
125 */
126 int
ieee80211_crypto_newkey(ieee80211com_t * ic,int cipher,int flags,struct ieee80211_key * key)127 ieee80211_crypto_newkey(ieee80211com_t *ic, int cipher, int flags,
128 struct ieee80211_key *key)
129 {
130 const struct ieee80211_cipher *cip;
131 ieee80211_keyix keyix, rxkeyix;
132 void *keyctx;
133 uint16_t oflags;
134
135 /*
136 * Validate cipher and set reference to cipher routines.
137 */
138 if (cipher >= IEEE80211_CIPHER_MAX) {
139 ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_newkey: "
140 "invalid cipher %u\n", cipher);
141 return (0);
142 }
143 cip = ic->ic_ciphers[cipher];
144 /* already load all the ciphers, cip can't be NULL */
145 if (cip == NULL) {
146 ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_newkey: "
147 "unable to load cipher %u, module %s\n",
148 cipher, cipher < IEEE80211_N(cipher_modnames) ?
149 cipher_modnames[cipher] : "<unknown>");
150 return (0);
151 }
152
153 oflags = key->wk_flags;
154 flags &= IEEE80211_KEY_COMMON;
155 /*
156 * If the hardware does not support the cipher then
157 * fallback to a host-based implementation.
158 */
159 if ((ic->ic_caps & (1<<cipher)) == 0) {
160 ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_newkey: "
161 "no h/w support for cipher %s, falling back to s/w\n",
162 cip->ic_name);
163 flags |= IEEE80211_KEY_SWCRYPT;
164 }
165 /*
166 * Hardware TKIP with software MIC is an important
167 * combination; we handle it by flagging each key,
168 * the cipher modules honor it.
169 */
170 if (cipher == IEEE80211_CIPHER_TKIP &&
171 (ic->ic_caps & IEEE80211_C_TKIPMIC) == 0) {
172 ieee80211_dbg(IEEE80211_MSG_CRYPTO,
173 "no h/w support for TKIP MIC, falling back to s/w\n");
174 flags |= IEEE80211_KEY_SWMIC;
175 }
176
177 /*
178 * Bind cipher to key instance. Note we do this
179 * after checking the device capabilities so the
180 * cipher module can optimize space usage based on
181 * whether or not it needs to do the cipher work.
182 */
183 if (key->wk_cipher != cip || key->wk_flags != flags) {
184 again:
185 /*
186 * Fillin the flags so cipher modules can see s/w
187 * crypto requirements and potentially allocate
188 * different state and/or attach different method
189 * pointers.
190 */
191 key->wk_flags = (uint16_t)flags;
192 keyctx = cip->ic_attach(ic, key);
193 if (keyctx == NULL) {
194 ieee80211_dbg(IEEE80211_MSG_CRYPTO, "crypto_setkey: "
195 "unable to attach cipher %s\n", cip->ic_name);
196 key->wk_flags = oflags; /* restore old flags */
197 return (0);
198 }
199 CIPHER_DETACH(key); /* Detach old cipher */
200 key->wk_cipher = cip;
201 key->wk_private = keyctx;
202 }
203 /*
204 * Commit to requested usage so driver can see the flags.
205 */
206 key->wk_flags = (uint16_t)flags;
207
208 /*
209 * Ask the driver for a key index if we don't have one.
210 * Note that entries in the global key table always have
211 * an index; this means it's safe to call this routine
212 * for these entries just to setup the reference to the
213 * cipher template. Note also that when using software
214 * crypto we also call the driver to give us a key index.
215 */
216 if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
217 if (!DEV_KEY_ALLOC(ic, key, &keyix, &rxkeyix)) {
218 /*
219 * Driver has no room; fallback to doing crypto
220 * in the host. We change the flags and start the
221 * procedure over. If we get back here then there's
222 * no hope and we bail. Note that this can leave
223 * the key in a inconsistent state if the caller
224 * continues to use it.
225 */
226 if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
227 ieee80211_dbg(IEEE80211_MSG_CRYPTO,
228 "crypto_setkey: "
229 "no h/w resources for cipher %s, "
230 "falling back to s/w\n", cip->ic_name);
231 oflags = key->wk_flags;
232 flags |= IEEE80211_KEY_SWCRYPT;
233 if (cipher == IEEE80211_CIPHER_TKIP)
234 flags |= IEEE80211_KEY_SWMIC;
235 goto again;
236 }
237 ieee80211_dbg(IEEE80211_MSG_CRYPTO, "crypto_setkey: "
238 "unable to setup cipher %s\n", cip->ic_name);
239 return (0);
240 }
241 key->wk_keyix = keyix;
242 key->wk_rxkeyix = rxkeyix;
243 }
244 return (1);
245 }
246
247 /*
248 * Remove the key (no locking, for internal use).
249 */
250 static int
ieee80211_crypto_delkey_locked(ieee80211com_t * ic,struct ieee80211_key * key)251 ieee80211_crypto_delkey_locked(ieee80211com_t *ic, struct ieee80211_key *key)
252 {
253 uint16_t keyix;
254
255 ASSERT(key->wk_cipher != NULL);
256
257 keyix = key->wk_keyix;
258 if (keyix != IEEE80211_KEYIX_NONE) {
259 /*
260 * Remove hardware entry.
261 */
262 if (!DEV_KEY_DELETE(ic, key)) {
263 ieee80211_dbg(IEEE80211_MSG_CRYPTO,
264 "ieee80211_crypto_delkey_locked: ",
265 "driverdeletes key %u failed\n", keyix);
266 }
267 }
268 CIPHER_DETACH(key);
269 bzero(key, sizeof (struct ieee80211_key));
270 /* NB: cannot depend on key index to decide this */
271 ieee80211_crypto_resetkey(ic, key, IEEE80211_KEYIX_NONE);
272 return (1);
273 }
274
275 /*
276 * Remove the specified key.
277 */
278 int
ieee80211_crypto_delkey(ieee80211com_t * ic,struct ieee80211_key * key)279 ieee80211_crypto_delkey(ieee80211com_t *ic, struct ieee80211_key *key)
280 {
281 int status;
282
283 KEY_UPDATE_BEGIN(ic);
284 status = ieee80211_crypto_delkey_locked(ic, key);
285 KEY_UPDATE_END(ic);
286 return (status);
287 }
288
289 /*
290 * Clear the global key table.
291 */
292 static void
ieee80211_crypto_delglobalkeys(ieee80211com_t * ic)293 ieee80211_crypto_delglobalkeys(ieee80211com_t *ic)
294 {
295 int i;
296
297 KEY_UPDATE_BEGIN(ic);
298 for (i = 0; i < IEEE80211_WEP_NKID; i++)
299 (void) ieee80211_crypto_delkey_locked(ic, &ic->ic_nw_keys[i]);
300 KEY_UPDATE_END(ic);
301 }
302
303 /*
304 * Set the contents of the specified key.
305 *
306 * Locking must be handled by the caller using:
307 * ieee80211_key_update_begin(ic);
308 * ieee80211_key_update_end(ic);
309 */
310 int
ieee80211_crypto_setkey(ieee80211com_t * ic,struct ieee80211_key * key,const uint8_t * macaddr)311 ieee80211_crypto_setkey(ieee80211com_t *ic, struct ieee80211_key *key,
312 const uint8_t *macaddr)
313 {
314 const struct ieee80211_cipher *cip = key->wk_cipher;
315
316 ASSERT(cip != NULL);
317
318 ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_setkey: "
319 "%s keyix %u flags 0x%x mac %s len %u\n",
320 cip->ic_name, key->wk_keyix, key->wk_flags,
321 ieee80211_macaddr_sprintf(macaddr), key->wk_keylen);
322
323 /*
324 * Give cipher a chance to validate key contents.
325 * should happen before modifying state.
326 */
327 if (cip->ic_setkey(key) == 0) {
328 ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_setkey: "
329 "cipher %s rejected key index %u len %u flags 0x%x\n",
330 cip->ic_name, key->wk_keyix, key->wk_keylen,
331 key->wk_flags);
332 return (0);
333 }
334 if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
335 ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_setkey: "
336 "no key index; should not happen!\n");
337 return (0);
338 }
339 return (DEV_KEY_SET(ic, key, macaddr));
340 }
341
342 /*
343 * Return the transmit key to use in sending a frame.
344 */
345 struct ieee80211_key *
ieee80211_crypto_getkey(ieee80211com_t * ic)346 ieee80211_crypto_getkey(ieee80211com_t *ic)
347 {
348 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
349 KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
350 return (NULL);
351 return (&ic->ic_nw_keys[ic->ic_def_txkey]);
352 }
353
354 uint8_t
ieee80211_crypto_getciphertype(ieee80211com_t * ic)355 ieee80211_crypto_getciphertype(ieee80211com_t *ic)
356 {
357 struct ieee80211_key *key;
358 uint32_t cipher;
359 static const uint8_t ciphermap[] = {
360 WIFI_SEC_WEP, /* IEEE80211_CIPHER_WEP */
361 WIFI_SEC_WPA, /* IEEE80211_CIPHER_TKIP */
362 (uint8_t)-1, /* IEEE80211_CIPHER_AES_OCB */
363 WIFI_SEC_WPA, /* IEEE80211_CIPHER_AES_CCM */
364 (uint8_t)-1, /* IEEE80211_CIPHER_CKIP */
365 WIFI_SEC_NONE, /* IEEE80211_CIPHER_NONE */
366 };
367
368 if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
369 return (WIFI_SEC_NONE);
370
371 key = ieee80211_crypto_getkey(ic);
372 if (key == NULL)
373 return (WIFI_SEC_NONE);
374
375 cipher = key->wk_cipher->ic_cipher;
376 ASSERT(cipher < IEEE80211_N(ciphermap));
377 return (ciphermap[cipher]);
378 }
379
380 /*
381 * Add privacy headers appropriate for the specified key.
382 */
383 struct ieee80211_key *
ieee80211_crypto_encap(ieee80211com_t * ic,mblk_t * mp)384 ieee80211_crypto_encap(ieee80211com_t *ic, mblk_t *mp)
385 {
386 struct ieee80211_key *k;
387 const struct ieee80211_cipher *cip;
388 uint8_t keyix;
389
390 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE) {
391 ieee80211_dbg(IEEE80211_MSG_CRYPTO,
392 "ieee80211_crypto_encap: %s",
393 " No default xmit key for frame\n");
394 return (NULL);
395 }
396 keyix = ic->ic_def_txkey;
397 k = &ic->ic_nw_keys[ic->ic_def_txkey];
398 cip = k->wk_cipher;
399 return (cip->ic_encap(k, mp, keyix<<6) ? k : NULL);
400 }
401
402 /*
403 * Validate and strip privacy headers (and trailer) for a
404 * received frame that has the WEP/Privacy bit set.
405 */
406 struct ieee80211_key *
ieee80211_crypto_decap(ieee80211com_t * ic,mblk_t * mp,int hdrlen)407 ieee80211_crypto_decap(ieee80211com_t *ic, mblk_t *mp, int hdrlen)
408 {
409 struct ieee80211_key *k;
410 const struct ieee80211_cipher *cip;
411 uint8_t *ivp;
412 uint8_t keyid;
413
414 /* NB: this minimum size data frame could be bigger */
415 if (MBLKL(mp) < IEEE80211_WEP_MINLEN) {
416 ieee80211_dbg(IEEE80211_MSG_CRYPTO, "ieee80211_crypto_decap:"
417 " WEP data frame too short, len %u\n",
418 MBLKL(mp));
419 return (NULL);
420 }
421 /*
422 * Locate the key. If unicast and there is no unicast
423 * key then we fall back to the key id in the header.
424 * This assumes unicast keys are only configured when
425 * the key id in the header is meaningless (typically 0).
426 */
427 ivp = mp->b_rptr + hdrlen;
428 keyid = ivp[IEEE80211_WEP_IVLEN];
429 k = &ic->ic_nw_keys[keyid >> 6];
430
431 /* check to avoid panic when wep is on but key is not set */
432 if (k->wk_cipher == &ieee80211_cipher_none ||
433 k->wk_cipher == NULL)
434 return (NULL);
435
436 cip = k->wk_cipher;
437 return ((cip->ic_decap)(k, mp, hdrlen) ? k : NULL);
438 }
439
440 /*
441 * Setup crypto support.
442 */
443 void
ieee80211_crypto_attach(ieee80211com_t * ic)444 ieee80211_crypto_attach(ieee80211com_t *ic)
445 {
446 struct ieee80211_crypto_state *cs = &ic->ic_crypto;
447 int i;
448
449 (void) crypto_mech2id(SUN_CKM_RC4); /* Load RC4 */
450 (void) crypto_mech2id(SUN_CKM_AES_CBC); /* Load AES-CBC */
451 (void) crypto_mech2id(SUN_CKM_AES_CCM); /* Load AES-CCM */
452
453 /* NB: we assume everything is pre-zero'd */
454 cs->cs_def_txkey = IEEE80211_KEYIX_NONE;
455 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
456 ieee80211_crypto_resetkey(ic, &cs->cs_nw_keys[i],
457 IEEE80211_KEYIX_NONE);
458 }
459
460 /*
461 * Initialize the driver key support routines to noop entries.
462 * This is useful especially for the cipher test modules.
463 */
464 cs->cs_key_alloc = nulldev_key_alloc;
465 cs->cs_key_set = nulldev_key_set;
466 cs->cs_key_delete = nulldev_key_delete;
467 cs->cs_key_update_begin = nulldev_key_update;
468 cs->cs_key_update_end = nulldev_key_update;
469
470 ieee80211_crypto_register(ic, &wep);
471 ieee80211_crypto_register(ic, &tkip);
472 ieee80211_crypto_register(ic, &ccmp);
473 }
474
475 /*
476 * Teardown crypto support.
477 */
478 void
ieee80211_crypto_detach(ieee80211com_t * ic)479 ieee80211_crypto_detach(ieee80211com_t *ic)
480 {
481 ieee80211_crypto_delglobalkeys(ic);
482
483 ieee80211_crypto_unregister(ic, &wep);
484 ieee80211_crypto_unregister(ic, &tkip);
485 ieee80211_crypto_unregister(ic, &ccmp);
486 }
487
488 /*
489 * Register a crypto cipher module.
490 */
491 void
ieee80211_crypto_register(ieee80211com_t * ic,const struct ieee80211_cipher * cip)492 ieee80211_crypto_register(ieee80211com_t *ic,
493 const struct ieee80211_cipher *cip)
494 {
495 if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
496 ieee80211_err("ieee80211_crypto_register: "
497 "cipher %s has an invalid cipher index %u\n",
498 cip->ic_name, cip->ic_cipher);
499 return;
500 }
501 if (ic->ic_ciphers[cip->ic_cipher] != NULL &&
502 ic->ic_ciphers[cip->ic_cipher] != cip) {
503 ieee80211_err("ieee80211_crypto_register: "
504 "cipher %s registered with a different template\n",
505 cip->ic_name);
506 return;
507 }
508 ic->ic_ciphers[cip->ic_cipher] = cip;
509 }
510
511 /*
512 * Unregister a crypto cipher module.
513 */
514 void
ieee80211_crypto_unregister(ieee80211com_t * ic,const struct ieee80211_cipher * cip)515 ieee80211_crypto_unregister(ieee80211com_t *ic,
516 const struct ieee80211_cipher *cip)
517 {
518 if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
519 ieee80211_err("ieee80211_crypto_unregister: "
520 "cipher %s has an invalid cipher index %u\n",
521 cip->ic_name, cip->ic_cipher);
522 return;
523 }
524 if (ic->ic_ciphers[cip->ic_cipher] != NULL &&
525 ic->ic_ciphers[cip->ic_cipher] != cip) {
526 ieee80211_err("ieee80211_crypto_unregister: "
527 "cipher %s registered with a different template\n",
528 cip->ic_name);
529 return;
530 }
531 /* NB: don't complain about not being registered */
532 ic->ic_ciphers[cip->ic_cipher] = NULL;
533 }
534