xref: /dflybsd-src/sys/netproto/802_11/wlan_ccmp/ieee80211_crypto_ccmp.c (revision ae788f37fe53d5d1ca1e12a184a662192caad3c5)
1 /*
2  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * Alternatively, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL") version 2 as published by the Free
18  * Software Foundation.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD: src/sys/net80211/ieee80211_crypto_ccmp.c,v 1.7.2.1 2005/12/22 19:02:08 sam Exp $
32  * $DragonFly: src/sys/netproto/802_11/wlan_ccmp/ieee80211_crypto_ccmp.c,v 1.6 2007/09/15 07:19:23 sephe Exp $
33  */
34 
35 /*
36  * IEEE 802.11i AES-CCMP crypto support.
37  *
38  * Part of this module is derived from similar code in the Host
39  * AP driver. The code is used with the consent of the author and
40  * it's license is included below.
41  */
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 
49 #include <sys/socket.h>
50 
51 #include <net/if.h>
52 #include <net/if_arp.h>
53 #include <net/if_media.h>
54 #include <net/ethernet.h>
55 
56 #include <netproto/802_11/ieee80211_var.h>
57 
58 #include <crypto/rijndael/rijndael.h>
59 
60 #define AES_BLOCK_LEN 16
61 
62 struct ccmp_ctx {
63 	struct ieee80211com *cc_ic;	/* for diagnostics */
64 	rijndael_ctx	     cc_aes;
65 };
66 
67 static	void *ccmp_attach(struct ieee80211com *, struct ieee80211_key *);
68 static	void ccmp_detach(struct ieee80211_key *);
69 static	int ccmp_setkey(struct ieee80211_key *);
70 static	int ccmp_encap(struct ieee80211_key *k, struct mbuf *, uint8_t keyid);
71 static	int ccmp_decap(struct ieee80211_key *, struct mbuf *, int);
72 static	int ccmp_enmic(struct ieee80211_key *, struct mbuf *, int);
73 static	int ccmp_demic(struct ieee80211_key *, struct mbuf *, int);
74 static	int ccmp_getiv(struct ieee80211_key *, struct ieee80211_crypto_iv *,
75 		uint8_t);
76 static	int ccmp_update(struct ieee80211_key *,
77 		const struct ieee80211_crypto_iv *,
78 		const struct ieee80211_frame *);
79 
80 static const struct ieee80211_cipher ccmp = {
81 	.ic_name	= "AES-CCM",
82 	.ic_cipher	= IEEE80211_CIPHER_AES_CCM,
83 	.ic_header	= IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
84 			  IEEE80211_WEP_EXTIVLEN,
85 	.ic_trailer	= IEEE80211_WEP_MICLEN,
86 	.ic_miclen	= 0,
87 	.ic_attach	= ccmp_attach,
88 	.ic_detach	= ccmp_detach,
89 	.ic_setkey	= ccmp_setkey,
90 	.ic_encap	= ccmp_encap,
91 	.ic_decap	= ccmp_decap,
92 	.ic_enmic	= ccmp_enmic,
93 	.ic_demic	= ccmp_demic,
94 	.ic_getiv	= ccmp_getiv,
95 	.ic_update	= ccmp_update
96 };
97 
98 static	int ccmp_encrypt(struct ieee80211_key *, struct mbuf *, int hdrlen);
99 static	int ccmp_decrypt(struct ieee80211_key *, uint64_t pn,
100 		struct mbuf *, int hdrlen);
101 
102 /* number of references from net80211 layer */
103 static	int nrefs = 0;
104 
105 static void *
106 ccmp_attach(struct ieee80211com *ic, struct ieee80211_key *k)
107 {
108 	struct ccmp_ctx *ctx;
109 
110 	ctx = kmalloc(sizeof(struct ccmp_ctx), M_DEVBUF, M_NOWAIT | M_ZERO);
111 	if (ctx == NULL) {
112 		ic->ic_stats.is_crypto_nomem++;
113 		return NULL;
114 	}
115 	ctx->cc_ic = ic;
116 	nrefs++;			/* NB: we assume caller locking */
117 	return ctx;
118 }
119 
120 static void
121 ccmp_detach(struct ieee80211_key *k)
122 {
123 	struct ccmp_ctx *ctx = k->wk_private;
124 
125 	kfree(ctx, M_DEVBUF);
126 	KASSERT(nrefs > 0, ("imbalanced attach/detach"));
127 	nrefs--;			/* NB: we assume caller locking */
128 }
129 
130 static int
131 ccmp_setkey(struct ieee80211_key *k)
132 {
133 	struct ccmp_ctx *ctx = k->wk_private;
134 
135 	if (k->wk_keylen != (128/NBBY)) {
136 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
137 			"%s: Invalid key length %u, expecting %u\n",
138 			__func__, k->wk_keylen, 128/NBBY);
139 		return 0;
140 	}
141 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT)
142 		rijndael_set_key(&ctx->cc_aes, k->wk_key, k->wk_keylen * NBBY);
143 	return 1;
144 }
145 
146 /*
147  * Add privacy headers appropriate for the specified key.
148  */
149 static int
150 ccmp_encap(struct ieee80211_key *k, struct mbuf *m, uint8_t keyid)
151 {
152 	struct ccmp_ctx *ctx = k->wk_private;
153 	struct ieee80211com *ic = ctx->cc_ic;
154 	uint8_t *ivp;
155 	int hdrlen;
156 
157 	hdrlen = ieee80211_hdrspace(ic, mtod(m, void *));
158 
159 	/*
160 	 * Copy down 802.11 header and add the IV, KeyID, and ExtIV.
161 	 */
162 	M_PREPEND(m, ccmp.ic_header, MB_DONTWAIT);
163 	if (m == NULL)
164 		return 0;
165 	ivp = mtod(m, uint8_t *);
166 	ovbcopy(ivp + ccmp.ic_header, ivp, hdrlen);
167 	ivp += hdrlen;
168 
169 	k->wk_keytsc++;		/* XXX wrap at 48 bits */
170 	ivp[0] = k->wk_keytsc >> 0;		/* PN0 */
171 	ivp[1] = k->wk_keytsc >> 8;		/* PN1 */
172 	ivp[2] = 0;				/* Reserved */
173 	ivp[3] = keyid | IEEE80211_WEP_EXTIV;	/* KeyID | ExtID */
174 	ivp[4] = k->wk_keytsc >> 16;		/* PN2 */
175 	ivp[5] = k->wk_keytsc >> 24;		/* PN3 */
176 	ivp[6] = k->wk_keytsc >> 32;		/* PN4 */
177 	ivp[7] = k->wk_keytsc >> 40;		/* PN5 */
178 
179 	/*
180 	 * Finally, do software encrypt if neeed.
181 	 */
182 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
183 	    !ccmp_encrypt(k, m, hdrlen))
184 		return 0;
185 
186 	return 1;
187 }
188 
189 static int
190 ccmp_getiv(struct ieee80211_key *k, struct ieee80211_crypto_iv *iv,
191 	uint8_t keyid)
192 {
193 	uint8_t *ivp = (uint8_t *)iv;
194 
195 	k->wk_keytsc++;		/* XXX wrap at 48 bits */
196 	ivp[0] = k->wk_keytsc >> 0;		/* PN0 */
197 	ivp[1] = k->wk_keytsc >> 8;		/* PN1 */
198 	ivp[2] = 0;				/* Reserved */
199 	ivp[3] = keyid | IEEE80211_WEP_EXTIV;	/* KeyID | ExtID */
200 	ivp[4] = k->wk_keytsc >> 16;		/* PN2 */
201 	ivp[5] = k->wk_keytsc >> 24;		/* PN3 */
202 	ivp[6] = k->wk_keytsc >> 32;		/* PN4 */
203 	ivp[7] = k->wk_keytsc >> 40;		/* PN5 */
204 
205 	return 1;
206 }
207 
208 /*
209  * Add MIC to the frame as needed.
210  */
211 static int
212 ccmp_enmic(struct ieee80211_key *k, struct mbuf *m, int force)
213 {
214 	return 1;
215 }
216 
217 static __inline uint64_t
218 READ_6(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5)
219 {
220 	uint32_t iv32 = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
221 	uint16_t iv16 = (b4 << 0) | (b5 << 8);
222 	return (((uint64_t)iv16) << 32) | iv32;
223 }
224 
225 /*
226  * Validate and strip privacy headers (and trailer) for a
227  * received frame. The specified key should be correct but
228  * is also verified.
229  */
230 static int
231 ccmp_decap(struct ieee80211_key *k, struct mbuf *m, int hdrlen)
232 {
233 	struct ccmp_ctx *ctx = k->wk_private;
234 	struct ieee80211_frame *wh;
235 	uint8_t *ivp;
236 	uint64_t pn;
237 
238 	/*
239 	 * Header should have extended IV and sequence number;
240 	 * verify the former and validate the latter.
241 	 */
242 	wh = mtod(m, struct ieee80211_frame *);
243 	ivp = mtod(m, uint8_t *) + hdrlen;
244 	if ((ivp[IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV) == 0) {
245 		/*
246 		 * No extended IV; discard frame.
247 		 */
248 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
249 			"[%6D] Missing ExtIV for AES-CCM cipher\n",
250 			wh->i_addr2, ":");
251 		ctx->cc_ic->ic_stats.is_rx_ccmpformat++;
252 		return 0;
253 	}
254 	pn = READ_6(ivp[0], ivp[1], ivp[4], ivp[5], ivp[6], ivp[7]);
255 	if (pn <= k->wk_keyrsc) {
256 		/*
257 		 * Replay violation.
258 		 */
259 		ieee80211_notify_replay_failure(ctx->cc_ic, wh, k, pn);
260 		ctx->cc_ic->ic_stats.is_rx_ccmpreplay++;
261 		return 0;
262 	}
263 
264 	/*
265 	 * Check if the device handled the decrypt in hardware.
266 	 * If so we just strip the header; otherwise we need to
267 	 * handle the decrypt in software.  Note that for the
268 	 * latter we leave the header in place for use in the
269 	 * decryption work.
270 	 */
271 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
272 	    !ccmp_decrypt(k, pn, m, hdrlen))
273 		return 0;
274 
275 	/*
276 	 * Copy up 802.11 header and strip crypto bits.
277 	 */
278 	ovbcopy(mtod(m, void *), mtod(m, uint8_t *) + ccmp.ic_header, hdrlen);
279 	m_adj(m, ccmp.ic_header);
280 	m_adj(m, -ccmp.ic_trailer);
281 
282 	/*
283 	 * Ok to update rsc now.
284 	 */
285 	k->wk_keyrsc = pn;
286 
287 	return 1;
288 }
289 
290 static int
291 ccmp_update(struct ieee80211_key *k, const struct ieee80211_crypto_iv *iv,
292 	const struct ieee80211_frame *wh)
293 {
294 	struct ccmp_ctx *ctx = k->wk_private;
295 	const uint8_t *ivp = (const uint8_t *)iv;
296 	uint64_t pn;
297 
298 	/*
299 	 * Header should have extended IV and sequence number;
300 	 * verify the former and validate the latter.
301 	 */
302 	if ((ivp[IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV) == 0) {
303 		/*
304 		 * No extended IV; discard frame.
305 		 */
306 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
307 			"[%6D] Missing ExtIV for AES-CCM cipher\n",
308 			wh->i_addr2, ":");
309 		ctx->cc_ic->ic_stats.is_rx_ccmpformat++;
310 		return 0;
311 	}
312 	pn = READ_6(ivp[0], ivp[1], ivp[4], ivp[5], ivp[6], ivp[7]);
313 	if (pn <= k->wk_keyrsc) {
314 		/*
315 		 * Replay violation.
316 		 */
317 		ieee80211_notify_replay_failure(ctx->cc_ic, wh, k, pn);
318 		ctx->cc_ic->ic_stats.is_rx_ccmpreplay++;
319 		return 0;
320 	}
321 
322 	/*
323 	 * Ok to update rsc now.
324 	 */
325 	k->wk_keyrsc = pn;
326 	return 1;
327 }
328 
329 /*
330  * Verify and strip MIC from the frame.
331  */
332 static int
333 ccmp_demic(struct ieee80211_key *k, struct mbuf *m, int force)
334 {
335 	return 1;
336 }
337 
338 static __inline void
339 xor_block(uint8_t *b, const uint8_t *a, size_t len)
340 {
341 	int i;
342 	for (i = 0; i < len; i++)
343 		b[i] ^= a[i];
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_blocks(rijndael_ctx *ctx, struct ieee80211_frame *wh,
362 	uint64_t pn, size_t dlen,
363 	uint8_t b0[AES_BLOCK_LEN], uint8_t aad[2 * AES_BLOCK_LEN],
364 	uint8_t auth[AES_BLOCK_LEN], uint8_t s0[AES_BLOCK_LEN])
365 {
366 #define	IS_4ADDRESS(wh) \
367 	((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
368 #define	IS_QOS_DATA(wh)	IEEE80211_QOS_HAS_SEQ(wh)
369 
370 	/* CCM Initial Block:
371 	 * Flag (Include authentication header, M=3 (8-octet MIC),
372 	 *       L=1 (2-octet Dlen))
373 	 * Nonce: 0x00 | A2 | PN
374 	 * Dlen */
375 	b0[0] = 0x59;
376 	/* NB: b0[1] set below */
377 	IEEE80211_ADDR_COPY(b0 + 2, wh->i_addr2);
378 	b0[8] = pn >> 40;
379 	b0[9] = pn >> 32;
380 	b0[10] = pn >> 24;
381 	b0[11] = pn >> 16;
382 	b0[12] = pn >> 8;
383 	b0[13] = pn >> 0;
384 	b0[14] = (dlen >> 8) & 0xff;
385 	b0[15] = dlen & 0xff;
386 
387 	/* AAD:
388 	 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
389 	 * A1 | A2 | A3
390 	 * SC with bits 4..15 (seq#) masked to zero
391 	 * A4 (if present)
392 	 * QC (if present)
393 	 */
394 	aad[0] = 0;	/* AAD length >> 8 */
395 	/* NB: aad[1] set below */
396 	aad[2] = wh->i_fc[0] & 0x8f;	/* XXX magic #s */
397 	aad[3] = wh->i_fc[1] & 0xc7;	/* XXX magic #s */
398 	/* NB: we know 3 addresses are contiguous */
399 	memcpy(aad + 4, wh->i_addr1, 3 * IEEE80211_ADDR_LEN);
400 	aad[22] = wh->i_seq[0] & IEEE80211_SEQ_FRAG_MASK;
401 	aad[23] = 0; /* all bits masked */
402 	/*
403 	 * Construct variable-length portion of AAD based
404 	 * on whether this is a 4-address frame/QOS frame.
405 	 * We always zero-pad to 32 bytes before running it
406 	 * through the cipher.
407 	 *
408 	 * We also fill in the priority bits of the CCM
409 	 * initial block as we know whether or not we have
410 	 * a QOS frame.
411 	 */
412 	if (IS_4ADDRESS(wh)) {
413 		IEEE80211_ADDR_COPY(aad + 24,
414 			((struct ieee80211_frame_addr4 *)wh)->i_addr4);
415 		if (IS_QOS_DATA(wh)) {
416 			struct ieee80211_qosframe_addr4 *qwh4 =
417 				(struct ieee80211_qosframe_addr4 *) wh;
418 			aad[30] = qwh4->i_qos[0] & 0x0f;/* just priority bits */
419 			aad[31] = 0;
420 			b0[1] = aad[30];
421 			aad[1] = 22 + IEEE80211_ADDR_LEN + 2;
422 		} else {
423 			*(uint16_t *)&aad[30] = 0;
424 			b0[1] = 0;
425 			aad[1] = 22 + IEEE80211_ADDR_LEN;
426 		}
427 	} else {
428 		if (IS_QOS_DATA(wh)) {
429 			struct ieee80211_qosframe *qwh =
430 				(struct ieee80211_qosframe*) wh;
431 			aad[24] = qwh->i_qos[0] & 0x0f;	/* just priority bits */
432 			aad[25] = 0;
433 			b0[1] = aad[24];
434 			aad[1] = 22 + 2;
435 		} else {
436 			*(uint16_t *)&aad[24] = 0;
437 			b0[1] = 0;
438 			aad[1] = 22;
439 		}
440 		*(uint16_t *)&aad[26] = 0;
441 		*(uint32_t *)&aad[28] = 0;
442 	}
443 
444 	/* Start with the first block and AAD */
445 	rijndael_encrypt(ctx, b0, auth);
446 	xor_block(auth, aad, AES_BLOCK_LEN);
447 	rijndael_encrypt(ctx, auth, auth);
448 	xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
449 	rijndael_encrypt(ctx, auth, auth);
450 	b0[0] &= 0x07;
451 	b0[14] = b0[15] = 0;
452 	rijndael_encrypt(ctx, b0, s0);
453 #undef	IS_QOS_DATA
454 #undef	IS_4ADDRESS
455 }
456 
457 #define	CCMP_ENCRYPT(_i, _b, _b0, _pos, _e, _len) do {	\
458 	/* Authentication */				\
459 	xor_block(_b, _pos, _len);			\
460 	rijndael_encrypt(&ctx->cc_aes, _b, _b);		\
461 	/* Encryption, with counter */			\
462 	_b0[14] = (_i >> 8) & 0xff;			\
463 	_b0[15] = _i & 0xff;				\
464 	rijndael_encrypt(&ctx->cc_aes, _b0, _e);	\
465 	xor_block(_pos, _e, _len);			\
466 } while (0)
467 
468 static int
469 ccmp_encrypt(struct ieee80211_key *key, struct mbuf *m0, int hdrlen)
470 {
471 	struct ccmp_ctx *ctx = key->wk_private;
472 	struct ieee80211_frame *wh;
473 	struct mbuf *m = m0;
474 	int data_len, i, space;
475 	uint8_t aad[2 * AES_BLOCK_LEN], b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN],
476 		e[AES_BLOCK_LEN], s0[AES_BLOCK_LEN];
477 	uint8_t *pos;
478 
479 	ctx->cc_ic->ic_stats.is_crypto_ccmp++;
480 
481 	wh = mtod(m, struct ieee80211_frame *);
482 	data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header);
483 	ccmp_init_blocks(&ctx->cc_aes, wh, key->wk_keytsc,
484 		data_len, b0, aad, b, s0);
485 
486 	i = 1;
487 	pos = mtod(m, uint8_t *) + hdrlen + ccmp.ic_header;
488 	/* NB: assumes header is entirely in first mbuf */
489 	space = m->m_len - (hdrlen + ccmp.ic_header);
490 	for (;;) {
491 		if (space > data_len)
492 			space = data_len;
493 		/*
494 		 * Do full blocks.
495 		 */
496 		while (space >= AES_BLOCK_LEN) {
497 			CCMP_ENCRYPT(i, b, b0, pos, e, AES_BLOCK_LEN);
498 			pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
499 			data_len -= AES_BLOCK_LEN;
500 			i++;
501 		}
502 		if (data_len <= 0)		/* no more data */
503 			break;
504 		m = m->m_next;
505 		if (m == NULL) {		/* last buffer */
506 			if (space != 0) {
507 				/*
508 				 * Short last block.
509 				 */
510 				CCMP_ENCRYPT(i, b, b0, pos, e, space);
511 			}
512 			break;
513 		}
514 		if (space != 0) {
515 			uint8_t *pos_next;
516 			int space_next;
517 			int len, dl, sp;
518 			struct mbuf *n;
519 
520 			/*
521 			 * Block straddles one or more mbufs, gather data
522 			 * into the block buffer b, apply the cipher, then
523 			 * scatter the results back into the mbuf chain.
524 			 * The buffer will automatically get space bytes
525 			 * of data at offset 0 copied in+out by the
526 			 * CCMP_ENCRYPT request so we must take care of
527 			 * the remaining data.
528 			 */
529 			n = m;
530 			dl = data_len;
531 			sp = space;
532 			for (;;) {
533 				pos_next = mtod(n, uint8_t *);
534 				len = min(dl, AES_BLOCK_LEN);
535 				space_next = len > sp ? len - sp : 0;
536 				if (n->m_len >= space_next) {
537 					/*
538 					 * This mbuf has enough data; just grab
539 					 * what we need and stop.
540 					 */
541 					xor_block(b+sp, pos_next, space_next);
542 					break;
543 				}
544 				/*
545 				 * This mbuf's contents are insufficient,
546 				 * take 'em all and prepare to advance to
547 				 * the next mbuf.
548 				 */
549 				xor_block(b+sp, pos_next, n->m_len);
550 				sp += n->m_len, dl -= n->m_len;
551 				n = n->m_next;
552 				if (n == NULL)
553 					break;
554 			}
555 
556 			CCMP_ENCRYPT(i, b, b0, pos, e, space);
557 
558 			/* NB: just like above, but scatter data to mbufs */
559 			dl = data_len;
560 			sp = space;
561 			for (;;) {
562 				pos_next = mtod(m, uint8_t *);
563 				len = min(dl, AES_BLOCK_LEN);
564 				space_next = len > sp ? len - sp : 0;
565 				if (m->m_len >= space_next) {
566 					xor_block(pos_next, e+sp, space_next);
567 					break;
568 				}
569 				xor_block(pos_next, e+sp, m->m_len);
570 				sp += m->m_len, dl -= m->m_len;
571 				m = m->m_next;
572 				if (m == NULL)
573 					goto done;
574 			}
575 			/*
576 			 * Do bookkeeping.  m now points to the last mbuf
577 			 * we grabbed data from.  We know we consumed a
578 			 * full block of data as otherwise we'd have hit
579 			 * the end of the mbuf chain, so deduct from data_len.
580 			 * Otherwise advance the block number (i) and setup
581 			 * pos+space to reflect contents of the new mbuf.
582 			 */
583 			data_len -= AES_BLOCK_LEN;
584 			i++;
585 			pos = pos_next + space_next;
586 			space = m->m_len - space_next;
587 		} else {
588 			/*
589 			 * Setup for next buffer.
590 			 */
591 			pos = mtod(m, uint8_t *);
592 			space = m->m_len;
593 		}
594 	}
595 done:
596 	/* tack on MIC */
597 	xor_block(b, s0, ccmp.ic_trailer);
598 	return ieee80211_mbuf_append(m0, ccmp.ic_trailer, b);
599 }
600 #undef CCMP_ENCRYPT
601 
602 #define	CCMP_DECRYPT(_i, _b, _b0, _pos, _a, _len) do {	\
603 	/* Decrypt, with counter */			\
604 	_b0[14] = (_i >> 8) & 0xff;			\
605 	_b0[15] = _i & 0xff;				\
606 	rijndael_encrypt(&ctx->cc_aes, _b0, _b);	\
607 	xor_block(_pos, _b, _len);			\
608 	/* Authentication */				\
609 	xor_block(_a, _pos, _len);			\
610 	rijndael_encrypt(&ctx->cc_aes, _a, _a);		\
611 } while (0)
612 
613 static int
614 ccmp_decrypt(struct ieee80211_key *key, uint64_t pn, struct mbuf *m, int hdrlen)
615 {
616 	struct ccmp_ctx *ctx = key->wk_private;
617 	struct ieee80211_frame *wh;
618 	uint8_t aad[2 * AES_BLOCK_LEN];
619 	uint8_t b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN], a[AES_BLOCK_LEN];
620 	uint8_t mic[AES_BLOCK_LEN];
621 	size_t data_len;
622 	int i;
623 	uint8_t *pos;
624 	u_int space;
625 
626 	ctx->cc_ic->ic_stats.is_crypto_ccmp++;
627 
628 	wh = mtod(m, struct ieee80211_frame *);
629 	data_len = m->m_pkthdr.len -
630 		   (hdrlen + ccmp.ic_header + ccmp.ic_trailer);
631 	ccmp_init_blocks(&ctx->cc_aes, wh, pn, data_len, b0, aad, a, b);
632 	m_copydata(m, m->m_pkthdr.len - ccmp.ic_trailer, ccmp.ic_trailer,
633 		   (caddr_t)mic);
634 	xor_block(mic, b, ccmp.ic_trailer);
635 
636 	i = 1;
637 	pos = mtod(m, uint8_t *) + hdrlen + ccmp.ic_header;
638 	space = m->m_len - (hdrlen + ccmp.ic_header);
639 	for (;;) {
640 		if (space > data_len)
641 			space = data_len;
642 		while (space >= AES_BLOCK_LEN) {
643 			CCMP_DECRYPT(i, b, b0, pos, a, AES_BLOCK_LEN);
644 			pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
645 			data_len -= AES_BLOCK_LEN;
646 			i++;
647 		}
648 		if (data_len <= 0)		/* no more data */
649 			break;
650 		m = m->m_next;
651 		if (m == NULL) {		/* last buffer */
652 			if (space != 0)		/* short last block */
653 				CCMP_DECRYPT(i, b, b0, pos, a, space);
654 			break;
655 		}
656 		if (space != 0) {
657 			uint8_t *pos_next;
658 			u_int space_next;
659 			u_int len;
660 
661 			/*
662 			 * Block straddles buffers, split references.  We
663 			 * do not handle splits that require >2 buffers
664 			 * since rx'd frames are never badly fragmented
665 			 * because drivers typically recv in clusters.
666 			 */
667 			pos_next = mtod(m, uint8_t *);
668 			len = min(data_len, AES_BLOCK_LEN);
669 			space_next = len > space ? len - space : 0;
670 			KASSERT(m->m_len >= space_next,
671 				("not enough data in following buffer, "
672 				"m_len %u need %u\n", m->m_len, space_next));
673 
674 			xor_block(b+space, pos_next, space_next);
675 			CCMP_DECRYPT(i, b, b0, pos, a, space);
676 			xor_block(pos_next, b+space, space_next);
677 			data_len -= len;
678 			i++;
679 
680 			pos = pos_next + space_next;
681 			space = m->m_len - space_next;
682 		} else {
683 			/*
684 			 * Setup for next buffer.
685 			 */
686 			pos = mtod(m, uint8_t *);
687 			space = m->m_len;
688 		}
689 	}
690 	if (memcmp(mic, a, ccmp.ic_trailer) != 0) {
691 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
692 			"[%6D] AES-CCM decrypt failed; MIC mismatch\n",
693 			wh->i_addr2, ":");
694 		ctx->cc_ic->ic_stats.is_rx_ccmpmic++;
695 		return 0;
696 	}
697 	return 1;
698 }
699 #undef CCMP_DECRYPT
700 
701 /*
702  * Module glue.
703  */
704 static int
705 ccmp_modevent(module_t mod, int type, void *unused)
706 {
707 	switch (type) {
708 	case MOD_LOAD:
709 		ieee80211_crypto_register(&ccmp);
710 		return 0;
711 	case MOD_UNLOAD:
712 		if (nrefs) {
713 			kprintf("wlan_ccmp: still in use (%u dynamic refs)\n",
714 				nrefs);
715 			return EBUSY;
716 		}
717 		ieee80211_crypto_unregister(&ccmp);
718 		return 0;
719 	}
720 	return EINVAL;
721 }
722 
723 static moduledata_t ccmp_mod = {
724 	"wlan_ccmp",
725 	ccmp_modevent,
726 	0
727 };
728 DECLARE_MODULE(wlan_ccmp, ccmp_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
729 MODULE_VERSION(wlan_ccmp, 1);
730 MODULE_DEPEND(wlan_ccmp, wlan, 1, 1, 1);
731