xref: /illumos-gate/usr/src/uts/common/io/net80211/net80211_crypto_ccmp.c (revision b31b5de1357c915fe7dab4d9646d9d84f9fe69bc)
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.11i CCMP crypto support.
40  */
41 #include <sys/byteorder.h>
42 #include <sys/crypto/common.h>
43 #include <sys/crypto/api.h>
44 #include <sys/crc32.h>
45 #include <sys/random.h>
46 #include <sys/strsun.h>
47 #include "net80211_impl.h"
48 
49 struct ccmp_ctx {
50 	struct ieee80211com *cc_ic;	/* for diagnostics */
51 };
52 
53 #define	AES_BLOCK_LEN	16
54 #define	AES_NONCE_LEN	13
55 
56 static void *ccmp_attach(struct ieee80211com *, struct ieee80211_key *);
57 static void ccmp_detach(struct ieee80211_key *);
58 static int ccmp_setkey(struct ieee80211_key *);
59 static int ccmp_encap(struct ieee80211_key *k, mblk_t *, uint8_t);
60 static int ccmp_decap(struct ieee80211_key *, mblk_t *, int);
61 static int ccmp_enmic(struct ieee80211_key *, mblk_t *, int);
62 static int ccmp_demic(struct ieee80211_key *, mblk_t *, int);
63 
64 static int ccmp_encrypt(struct ieee80211_key *, mblk_t *, int);
65 static int ccmp_decrypt(struct ieee80211_key *, uint64_t pn, mblk_t *, int);
66 
67 const struct ieee80211_cipher ccmp = {
68 	"AES-CCM",
69 	IEEE80211_CIPHER_AES_CCM,
70 	IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
71 	    IEEE80211_WEP_EXTIVLEN,
72 	IEEE80211_WEP_MICLEN,
73 	0,
74 	ccmp_attach,
75 	ccmp_detach,
76 	ccmp_setkey,
77 	ccmp_encap,
78 	ccmp_decap,
79 	ccmp_enmic,
80 	ccmp_demic,
81 };
82 
83 /* ARGSUSED */
84 static void *
85 ccmp_attach(struct ieee80211com *ic, struct ieee80211_key *k)
86 {
87 	struct ccmp_ctx *ctx;
88 
89 	ctx = kmem_zalloc(sizeof (struct ccmp_ctx), KM_SLEEP);
90 	if (ctx == NULL)
91 		return (NULL);
92 
93 	ctx->cc_ic = ic;
94 	return (ctx);
95 }
96 
97 static void
98 ccmp_detach(struct ieee80211_key *k)
99 {
100 	struct ccmp_ctx *ctx = k->wk_private;
101 
102 	if (ctx != NULL)
103 		kmem_free(ctx, sizeof (struct ccmp_ctx));
104 }
105 
106 static int
107 ccmp_setkey(struct ieee80211_key *k)
108 {
109 	if (k->wk_keylen != (128/NBBY))
110 		return (0);
111 
112 	return (1);
113 }
114 
115 /*
116  * Add privacy headers appropriate for the specified key.
117  */
118 static int
119 ccmp_encap(struct ieee80211_key *k, mblk_t *mp, uint8_t keyid)
120 {
121 	uint8_t *ivp;
122 	int hdrlen;
123 
124 	hdrlen = ieee80211_hdrspace(mp->b_rptr);
125 	/*
126 	 * Copy down 802.11 header and add the IV, KeyID, and ExtIV.
127 	 */
128 	ivp = mp->b_rptr;
129 	ivp += hdrlen;
130 
131 	k->wk_keytsc++;				/* wrap at 48 bits */
132 	ivp[0] = k->wk_keytsc >> 0;		/* PN0 */
133 	ivp[1] = k->wk_keytsc >> 8;		/* PN1 */
134 	ivp[2] = 0;				/* Reserved */
135 	ivp[3] = keyid | IEEE80211_WEP_EXTIV;	/* KeyID | ExtID */
136 	ivp[4] = k->wk_keytsc >> 16;		/* PN2 */
137 	ivp[5] = k->wk_keytsc >> 24;		/* PN3 */
138 	ivp[6] = k->wk_keytsc >> 32;		/* PN4 */
139 	ivp[7] = k->wk_keytsc >> 40;		/* PN5 */
140 
141 	/*
142 	 * Finally, do software encrypt if neeed.
143 	 */
144 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
145 	    !ccmp_encrypt(k, mp, hdrlen))
146 		return (0);
147 
148 	return (1);
149 }
150 
151 /*
152  * Validate and strip privacy headers (and trailer) for a
153  * received frame. The specified key should be correct but
154  * is also verified.
155  */
156 static int
157 ccmp_decap(struct ieee80211_key *k, mblk_t *mp, int hdrlen)
158 {
159 	struct ieee80211_frame tmp;
160 	uint8_t *ivp;
161 	uint64_t pn;
162 
163 	/*
164 	 * Header should have extended IV and sequence number;
165 	 * verify the former and validate the latter.
166 	 */
167 	ivp = mp->b_rptr + hdrlen;
168 	if ((ivp[IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV) == 0) {
169 		/*
170 		 * No extended IV; discard frame.
171 		 */
172 		return (0);
173 	}
174 
175 	pn = ieee80211_read_6(ivp[0], ivp[1], ivp[4], ivp[5], ivp[6], ivp[7]);
176 	if (pn <= k->wk_keyrsc) {
177 		/*
178 		 * Replay violation.
179 		 */
180 		return (0);
181 	}
182 
183 	/*
184 	 * Check if the device handled the decrypt in hardware.
185 	 * If so we just strip the header; otherwise we need to
186 	 * handle the decrypt in software.  Note that for the
187 	 * latter we leave the header in place for use in the
188 	 * decryption work.
189 	 */
190 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
191 	    !ccmp_decrypt(k, pn, mp, hdrlen))
192 		return (0);
193 
194 	/*
195 	 * Copy up 802.11 header and strip crypto bits.
196 	 */
197 	bcopy(mp->b_rptr, &tmp, hdrlen);
198 	bcopy(&tmp, mp->b_rptr + ccmp.ic_header, hdrlen);
199 	mp->b_rptr += ccmp.ic_header;
200 	mp->b_wptr -= ccmp.ic_trailer;
201 
202 	/*
203 	 * Ok to update rsc now.
204 	 */
205 	k->wk_keyrsc = pn;
206 
207 	return (1);
208 }
209 
210 /*
211  * Add MIC to the frame as needed.
212  */
213 /* ARGSUSED */
214 static int
215 ccmp_enmic(struct ieee80211_key *k, mblk_t *mp, int force)
216 {
217 	return (1);
218 }
219 
220 /*
221  * Verify and strip MIC from the frame.
222  */
223 /* ARGSUSED */
224 static int
225 ccmp_demic(struct ieee80211_key *k, mblk_t *mp, int force)
226 {
227 	return (1);
228 }
229 
230 static int
231 aes_ccm_encrypt(CK_AES_CCM_PARAMS *cmparam, const uint8_t *key, int keylen,
232     const uint8_t *plaintext, int plain_len,
233     uint8_t *ciphertext, int cipher_len)
234 {
235 	crypto_mechanism_t mech;
236 	crypto_key_t crkey;
237 	crypto_data_t d1, d2;
238 
239 	int rv;
240 
241 	ieee80211_dbg(IEEE80211_MSG_CRYPTO,
242 	    "aes_ccm_encrypt(len=%d, keylen=%d)", plain_len, keylen);
243 
244 	bzero(&crkey, sizeof (crkey));
245 
246 	crkey.ck_format = CRYPTO_KEY_RAW;
247 	crkey.ck_data   = (char *)key;
248 	/* keys are measured in bits, not bytes, so multiply by 8 */
249 	crkey.ck_length = keylen * 8;
250 
251 	mech.cm_type	  = crypto_mech2id(SUN_CKM_AES_CCM);
252 	mech.cm_param	  = (caddr_t)cmparam;
253 	mech.cm_param_len = sizeof (CK_AES_CCM_PARAMS);
254 
255 #if defined(__amd64) || defined(__sparc)
256 	ieee80211_dbg(IEEE80211_MSG_CRYPTO, "cm_type=%lx", mech.cm_type);
257 #else
258 	ieee80211_dbg(IEEE80211_MSG_CRYPTO, "cm_type=%llx", mech.cm_type);
259 #endif
260 
261 	bzero(&d1, sizeof (d1));
262 	bzero(&d2, sizeof (d2));
263 
264 	d1.cd_format = CRYPTO_DATA_RAW;
265 	d1.cd_offset = 0;
266 	d1.cd_length = plain_len;
267 	d1.cd_raw.iov_base = (char *)plaintext;
268 	d1.cd_raw.iov_len  = plain_len;
269 
270 	d2.cd_format = CRYPTO_DATA_RAW;
271 	d2.cd_offset = 0;
272 	d2.cd_length = cipher_len;
273 	d2.cd_raw.iov_base = (char *)ciphertext;
274 	d2.cd_raw.iov_len  = cipher_len;
275 
276 
277 	rv = crypto_encrypt(&mech, &d1, &crkey, NULL, &d2, NULL);
278 	if (rv != CRYPTO_SUCCESS)
279 		ieee80211_err("aes_ccm_encrypt failed (%x)", rv);
280 	return (rv);
281 }
282 
283 static int
284 aes_ccm_decrypt(CK_AES_CCM_PARAMS *cmparam, const uint8_t *key, int keylen,
285     const uint8_t *ciphertext, int cipher_len,
286     uint8_t *plaintext, int plain_len)
287 {
288 	crypto_mechanism_t mech;
289 	crypto_key_t crkey;
290 	crypto_data_t d1, d2;
291 
292 	int rv;
293 
294 	ieee80211_dbg(IEEE80211_MSG_CRYPTO,
295 	    "aes_ccm_decrypt(len=%d, keylen=%d)", cipher_len, keylen);
296 
297 	bzero(&crkey, sizeof (crkey));
298 
299 	crkey.ck_format = CRYPTO_KEY_RAW;
300 	crkey.ck_data   = (char *)key;
301 	/* keys are measured in bits, not bytes, so multiply by 8 */
302 	crkey.ck_length = keylen * 8;
303 
304 	mech.cm_type	  = crypto_mech2id(SUN_CKM_AES_CCM);
305 	mech.cm_param	  = (caddr_t)cmparam;
306 	mech.cm_param_len = sizeof (CK_AES_CCM_PARAMS);
307 
308 #if defined(__amd64) || defined(__sparc)
309 	ieee80211_dbg(IEEE80211_MSG_CRYPTO, "cm_type=%lx", mech.cm_type);
310 #else
311 	ieee80211_dbg(IEEE80211_MSG_CRYPTO, "cm_type=%llx", mech.cm_type);
312 #endif
313 
314 	bzero(&d1, sizeof (d1));
315 	bzero(&d2, sizeof (d2));
316 
317 	d1.cd_format = CRYPTO_DATA_RAW;
318 	d1.cd_offset = 0;
319 	d1.cd_length = cipher_len;
320 	d1.cd_raw.iov_base = (char *)ciphertext;
321 	d1.cd_raw.iov_len  = cipher_len;
322 
323 	d2.cd_format = CRYPTO_DATA_RAW;
324 	d2.cd_offset = 0;
325 	d2.cd_length = plain_len;
326 	d2.cd_raw.iov_base = (char *)plaintext;
327 	d2.cd_raw.iov_len  = plain_len;
328 
329 
330 	rv = crypto_decrypt(&mech, &d1, &crkey, NULL, &d2, NULL);
331 	if (rv != CRYPTO_SUCCESS)
332 		ieee80211_err("aes_ccm_decrypt failed (%x)", rv);
333 	return (rv);
334 }
335 
336 /*
337  * For the avoidance of doubt, except that if any license choice other
338  * than GPL or LGPL is available it will apply instead, Sun elects to
339  * use only the General Public License version 2 (GPLv2) at this time
340  * for any software where a choice of GPL license versions is made
341  * available with the language indicating that GPLv2 or any later
342  * version may be used, or where a choice of which version of the GPL
343  * is applied is otherwise unspecified.
344  */
345 
346 /*
347  * Host AP crypt: host-based CCMP encryption implementation for Host AP driver
348  *
349  * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
350  *
351  * This program is free software; you can redistribute it and/or modify
352  * it under the terms of the GNU General Public License version 2 as
353  * published by the Free Software Foundation. See README and COPYING for
354  * more details.
355  *
356  * Alternatively, this software may be distributed under the terms of BSD
357  * license.
358  */
359 
360 static void
361 ccmp_init(struct ieee80211_frame *wh, uint64_t pn, size_t dlen,
362     uint8_t b0[AES_BLOCK_LEN], uint8_t aad[2 * AES_BLOCK_LEN])
363 {
364 	/*
365 	 * CCM Initial Block:
366 	 * Flag (Include authentication header, M=3 (8-octet MIC),
367 	 * L=1 (2-octet Dlen))
368 	 * Nonce: 0x00 | A2 | PN
369 	 * Dlen
370 	 */
371 	b0[0] = 0x59;
372 	/* b0[1] set below */
373 	IEEE80211_ADDR_COPY(b0 + 2, wh->i_addr2);
374 	b0[8] = pn >> 40;
375 	b0[9] = pn >> 32;
376 	b0[10] = pn >> 24;
377 	b0[11] = pn >> 16;
378 	b0[12] = pn >> 8;
379 	b0[13] = (uint8_t)(pn >> 0);
380 	b0[14] = (dlen >> 8) & 0xff;
381 	b0[15] = dlen & 0xff;
382 
383 	/*
384 	 * AAD:
385 	 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
386 	 * A1 | A2 | A3
387 	 * SC with bits 4..15 (seq#) masked to zero
388 	 * A4 (if present)
389 	 * QC (if present)
390 	 */
391 	aad[0] = 0;	/* AAD length >> 8 */
392 	/* aad[1] set below */
393 	aad[2] = wh->i_fc[0] & 0x8f;	/* magic #s */
394 	aad[3] = wh->i_fc[1] & 0xc7;	/* magic #s */
395 	/* we know 3 addresses are contiguous */
396 	(void) memcpy(aad + 4, wh->i_addr1, 3 * IEEE80211_ADDR_LEN);
397 	aad[22] = wh->i_seq[0] & IEEE80211_SEQ_FRAG_MASK;
398 	aad[23] = 0; /* all bits masked */
399 	/*
400 	 * Construct variable-length portion of AAD based
401 	 * on whether this is a 4-address frame/QOS frame.
402 	 * We always zero-pad to 32 bytes before running it
403 	 * through the cipher.
404 	 *
405 	 * We also fill in the priority bits of the CCM
406 	 * initial block as we know whether or not we have
407 	 * a QOS frame.
408 	 */
409 	*(uint16_t *)&aad[24] = 0;
410 	b0[1] = 0;
411 	aad[1] = 22;
412 	*(uint16_t *)&aad[26] = 0;
413 	*(uint32_t *)&aad[28] = 0;
414 }
415 
416 static int
417 ccmp_encrypt(struct ieee80211_key *key, mblk_t *mp, int hdrlen)
418 {
419 	struct ieee80211_frame *wh;
420 	int rv, data_len;
421 	uint8_t aad[2 * AES_BLOCK_LEN], b0[AES_BLOCK_LEN];
422 	uint8_t *pos;
423 	CK_AES_CCM_PARAMS cmparam;
424 
425 	wh = (struct ieee80211_frame *)mp->b_rptr;
426 	data_len = MBLKL(mp) - (hdrlen + ccmp.ic_header);
427 	pos = mp->b_rptr + hdrlen + ccmp.ic_header;
428 
429 	ccmp_init(wh, key->wk_keytsc, data_len, b0, aad);
430 
431 	cmparam.ulMACSize = IEEE80211_WEP_MICLEN;
432 	cmparam.ulNonceSize = AES_NONCE_LEN; /* N size */
433 	cmparam.ulAuthDataSize = aad[1]; /* A size */
434 	cmparam.ulDataSize = data_len;	/* data length; */
435 	cmparam.nonce = &b0[1]; /* N */
436 	cmparam.authData = &aad[2]; /* A */
437 
438 	rv = aes_ccm_encrypt(&cmparam,
439 	    key->wk_key, key->wk_keylen,
440 	    pos, data_len, pos, data_len + IEEE80211_WEP_MICLEN);
441 
442 	mp->b_wptr += ccmp.ic_trailer;
443 
444 	return ((rv == CRYPTO_SUCCESS)? 1 : 0);
445 }
446 
447 static int
448 ccmp_decrypt(struct ieee80211_key *key, uint64_t pn, mblk_t *mp, int hdrlen)
449 {
450 	struct ieee80211_frame *wh;
451 	int rv, data_len;
452 	uint8_t aad[2 * AES_BLOCK_LEN], b0[AES_BLOCK_LEN];
453 	uint8_t *pos;
454 	CK_AES_CCM_PARAMS cmparam;
455 
456 	wh = (struct ieee80211_frame *)mp->b_rptr;
457 	data_len = MBLKL(mp) - (hdrlen + ccmp.ic_header);
458 	pos = mp->b_rptr + hdrlen + ccmp.ic_header;
459 
460 	ccmp_init(wh, pn, data_len, b0, aad);
461 
462 	cmparam.ulMACSize = IEEE80211_WEP_MICLEN; /* MIC = 8 */
463 	cmparam.ulNonceSize = AES_NONCE_LEN; /* N size */
464 	cmparam.ulAuthDataSize = aad[1]; /* A size */
465 	cmparam.ulDataSize = data_len;
466 	cmparam.nonce = &b0[1]; /* N */
467 	cmparam.authData = &aad[2]; /* A */
468 
469 	rv = aes_ccm_decrypt(&cmparam,
470 	    key->wk_key, key->wk_keylen, pos, data_len, pos, data_len);
471 
472 	return ((rv == CRYPTO_SUCCESS)? 1 : 0);
473 }
474