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 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
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_media.h>
53 #include <net/ethernet.h>
54 
55 #include <net80211/ieee80211_var.h>
56 
57 #include <crypto/rijndael/rijndael.h>
58 
59 #define AES_BLOCK_LEN 16
60 
61 struct ccmp_ctx {
62 	struct ieee80211com *cc_ic;	/* for diagnostics */
63 	rijndael_ctx	     cc_aes;
64 };
65 
66 static	void *ccmp_attach(struct ieee80211com *, struct ieee80211_key *);
67 static	void ccmp_detach(struct ieee80211_key *);
68 static	int ccmp_setkey(struct ieee80211_key *);
69 static	int ccmp_encap(struct ieee80211_key *k, struct mbuf *, u_int8_t keyid);
70 static	int ccmp_decap(struct ieee80211_key *, struct mbuf *, int);
71 static	int ccmp_enmic(struct ieee80211_key *, struct mbuf *, int);
72 static	int ccmp_demic(struct ieee80211_key *, struct mbuf *, int);
73 
74 static const struct ieee80211_cipher ccmp = {
75 	.ic_name	= "AES-CCM",
76 	.ic_cipher	= IEEE80211_CIPHER_AES_CCM,
77 	.ic_header	= IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
78 			  IEEE80211_WEP_EXTIVLEN,
79 	.ic_trailer	= IEEE80211_WEP_MICLEN,
80 	.ic_miclen	= 0,
81 	.ic_attach	= ccmp_attach,
82 	.ic_detach	= ccmp_detach,
83 	.ic_setkey	= ccmp_setkey,
84 	.ic_encap	= ccmp_encap,
85 	.ic_decap	= ccmp_decap,
86 	.ic_enmic	= ccmp_enmic,
87 	.ic_demic	= ccmp_demic,
88 };
89 
90 static	int ccmp_encrypt(struct ieee80211_key *, struct mbuf *, int hdrlen);
91 static	int ccmp_decrypt(struct ieee80211_key *, u_int64_t pn,
92 		struct mbuf *, int hdrlen);
93 
94 /* number of references from net80211 layer */
95 static	int nrefs = 0;
96 
97 static void *
98 ccmp_attach(struct ieee80211com *ic, struct ieee80211_key *k)
99 {
100 	struct ccmp_ctx *ctx;
101 
102 	MALLOC(ctx, struct ccmp_ctx *, sizeof(struct ccmp_ctx),
103 		M_DEVBUF, M_NOWAIT | M_ZERO);
104 	if (ctx == NULL) {
105 		ic->ic_stats.is_crypto_nomem++;
106 		return NULL;
107 	}
108 	ctx->cc_ic = ic;
109 	nrefs++;			/* NB: we assume caller locking */
110 	return ctx;
111 }
112 
113 static void
114 ccmp_detach(struct ieee80211_key *k)
115 {
116 	struct ccmp_ctx *ctx = k->wk_private;
117 
118 	FREE(ctx, M_DEVBUF);
119 	KASSERT(nrefs > 0, ("imbalanced attach/detach"));
120 	nrefs--;			/* NB: we assume caller locking */
121 }
122 
123 static int
124 ccmp_setkey(struct ieee80211_key *k)
125 {
126 	struct ccmp_ctx *ctx = k->wk_private;
127 
128 	if (k->wk_keylen != (128/NBBY)) {
129 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
130 			"%s: Invalid key length %u, expecting %u\n",
131 			__func__, k->wk_keylen, 128/NBBY);
132 		return 0;
133 	}
134 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT)
135 		rijndael_set_key(&ctx->cc_aes, k->wk_key, k->wk_keylen*NBBY);
136 	return 1;
137 }
138 
139 /*
140  * Add privacy headers appropriate for the specified key.
141  */
142 static int
143 ccmp_encap(struct ieee80211_key *k, struct mbuf *m, u_int8_t keyid)
144 {
145 	struct ccmp_ctx *ctx = k->wk_private;
146 	struct ieee80211com *ic = ctx->cc_ic;
147 	u_int8_t *ivp;
148 	int hdrlen;
149 
150 	hdrlen = ieee80211_hdrspace(ic, mtod(m, void *));
151 
152 	/*
153 	 * Copy down 802.11 header and add the IV, KeyID, and ExtIV.
154 	 */
155 	M_PREPEND(m, ccmp.ic_header, M_NOWAIT);
156 	if (m == NULL)
157 		return 0;
158 	ivp = mtod(m, u_int8_t *);
159 	ovbcopy(ivp + ccmp.ic_header, ivp, hdrlen);
160 	ivp += hdrlen;
161 
162 	k->wk_keytsc++;		/* XXX wrap at 48 bits */
163 	ivp[0] = k->wk_keytsc >> 0;		/* PN0 */
164 	ivp[1] = k->wk_keytsc >> 8;		/* PN1 */
165 	ivp[2] = 0;				/* Reserved */
166 	ivp[3] = keyid | IEEE80211_WEP_EXTIV;	/* KeyID | ExtID */
167 	ivp[4] = k->wk_keytsc >> 16;		/* PN2 */
168 	ivp[5] = k->wk_keytsc >> 24;		/* PN3 */
169 	ivp[6] = k->wk_keytsc >> 32;		/* PN4 */
170 	ivp[7] = k->wk_keytsc >> 40;		/* PN5 */
171 
172 	/*
173 	 * Finally, do software encrypt if neeed.
174 	 */
175 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
176 	    !ccmp_encrypt(k, m, hdrlen))
177 		return 0;
178 
179 	return 1;
180 }
181 
182 /*
183  * Add MIC to the frame as needed.
184  */
185 static int
186 ccmp_enmic(struct ieee80211_key *k, struct mbuf *m, int force)
187 {
188 
189 	return 1;
190 }
191 
192 static __inline uint64_t
193 READ_6(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5)
194 {
195 	uint32_t iv32 = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
196 	uint16_t iv16 = (b4 << 0) | (b5 << 8);
197 	return (((uint64_t)iv16) << 32) | iv32;
198 }
199 
200 /*
201  * Validate and strip privacy headers (and trailer) for a
202  * received frame. The specified key should be correct but
203  * is also verified.
204  */
205 static int
206 ccmp_decap(struct ieee80211_key *k, struct mbuf *m, int hdrlen)
207 {
208 	struct ccmp_ctx *ctx = k->wk_private;
209 	struct ieee80211_frame *wh;
210 	uint8_t *ivp;
211 	uint64_t pn;
212 
213 	/*
214 	 * Header should have extended IV and sequence number;
215 	 * verify the former and validate the latter.
216 	 */
217 	wh = mtod(m, struct ieee80211_frame *);
218 	ivp = mtod(m, uint8_t *) + hdrlen;
219 	if ((ivp[IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV) == 0) {
220 		/*
221 		 * No extended IV; discard frame.
222 		 */
223 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
224 			"[%s] Missing ExtIV for AES-CCM cipher\n",
225 			ether_sprintf(wh->i_addr2));
226 		ctx->cc_ic->ic_stats.is_rx_ccmpformat++;
227 		return 0;
228 	}
229 	pn = READ_6(ivp[0], ivp[1], ivp[4], ivp[5], ivp[6], ivp[7]);
230 	if (pn <= k->wk_keyrsc) {
231 		/*
232 		 * Replay violation.
233 		 */
234 		ieee80211_notify_replay_failure(ctx->cc_ic, wh, k, pn);
235 		ctx->cc_ic->ic_stats.is_rx_ccmpreplay++;
236 		return 0;
237 	}
238 
239 	/*
240 	 * Check if the device handled the decrypt in hardware.
241 	 * If so we just strip the header; otherwise we need to
242 	 * handle the decrypt in software.  Note that for the
243 	 * latter we leave the header in place for use in the
244 	 * decryption work.
245 	 */
246 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
247 	    !ccmp_decrypt(k, pn, m, hdrlen))
248 		return 0;
249 
250 	/*
251 	 * Copy up 802.11 header and strip crypto bits.
252 	 */
253 	ovbcopy(mtod(m, void *), mtod(m, u_int8_t *) + ccmp.ic_header, hdrlen);
254 	m_adj(m, ccmp.ic_header);
255 	m_adj(m, -ccmp.ic_trailer);
256 
257 	/*
258 	 * Ok to update rsc now.
259 	 */
260 	k->wk_keyrsc = pn;
261 
262 	return 1;
263 }
264 
265 /*
266  * Verify and strip MIC from the frame.
267  */
268 static int
269 ccmp_demic(struct ieee80211_key *k, struct mbuf *m, int force)
270 {
271 	return 1;
272 }
273 
274 static __inline void
275 xor_block(uint8_t *b, const uint8_t *a, size_t len)
276 {
277 	int i;
278 	for (i = 0; i < len; i++)
279 		b[i] ^= a[i];
280 }
281 
282 /*
283  * Host AP crypt: host-based CCMP encryption implementation for Host AP driver
284  *
285  * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
286  *
287  * This program is free software; you can redistribute it and/or modify
288  * it under the terms of the GNU General Public License version 2 as
289  * published by the Free Software Foundation. See README and COPYING for
290  * more details.
291  *
292  * Alternatively, this software may be distributed under the terms of BSD
293  * license.
294  */
295 
296 static void
297 ccmp_init_blocks(rijndael_ctx *ctx, struct ieee80211_frame *wh,
298 	u_int64_t pn, size_t dlen,
299 	uint8_t b0[AES_BLOCK_LEN], uint8_t aad[2 * AES_BLOCK_LEN],
300 	uint8_t auth[AES_BLOCK_LEN], uint8_t s0[AES_BLOCK_LEN])
301 {
302 #define	IS_4ADDRESS(wh) \
303 	((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
304 #define	IS_QOS_DATA(wh)	IEEE80211_QOS_HAS_SEQ(wh)
305 
306 	/* CCM Initial Block:
307 	 * Flag (Include authentication header, M=3 (8-octet MIC),
308 	 *       L=1 (2-octet Dlen))
309 	 * Nonce: 0x00 | A2 | PN
310 	 * Dlen */
311 	b0[0] = 0x59;
312 	/* NB: b0[1] set below */
313 	IEEE80211_ADDR_COPY(b0 + 2, wh->i_addr2);
314 	b0[8] = pn >> 40;
315 	b0[9] = pn >> 32;
316 	b0[10] = pn >> 24;
317 	b0[11] = pn >> 16;
318 	b0[12] = pn >> 8;
319 	b0[13] = pn >> 0;
320 	b0[14] = (dlen >> 8) & 0xff;
321 	b0[15] = dlen & 0xff;
322 
323 	/* AAD:
324 	 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
325 	 * A1 | A2 | A3
326 	 * SC with bits 4..15 (seq#) masked to zero
327 	 * A4 (if present)
328 	 * QC (if present)
329 	 */
330 	aad[0] = 0;	/* AAD length >> 8 */
331 	/* NB: aad[1] set below */
332 	aad[2] = wh->i_fc[0] & 0x8f;	/* XXX magic #s */
333 	aad[3] = wh->i_fc[1] & 0xc7;	/* XXX magic #s */
334 	/* NB: we know 3 addresses are contiguous */
335 	memcpy(aad + 4, wh->i_addr1, 3 * IEEE80211_ADDR_LEN);
336 	aad[22] = wh->i_seq[0] & IEEE80211_SEQ_FRAG_MASK;
337 	aad[23] = 0; /* all bits masked */
338 	/*
339 	 * Construct variable-length portion of AAD based
340 	 * on whether this is a 4-address frame/QOS frame.
341 	 * We always zero-pad to 32 bytes before running it
342 	 * through the cipher.
343 	 *
344 	 * We also fill in the priority bits of the CCM
345 	 * initial block as we know whether or not we have
346 	 * a QOS frame.
347 	 */
348 	if (IS_4ADDRESS(wh)) {
349 		IEEE80211_ADDR_COPY(aad + 24,
350 			((struct ieee80211_frame_addr4 *)wh)->i_addr4);
351 		if (IS_QOS_DATA(wh)) {
352 			struct ieee80211_qosframe_addr4 *qwh4 =
353 				(struct ieee80211_qosframe_addr4 *) wh;
354 			aad[30] = qwh4->i_qos[0] & 0x0f;/* just priority bits */
355 			aad[31] = 0;
356 			b0[1] = aad[30];
357 			aad[1] = 22 + IEEE80211_ADDR_LEN + 2;
358 		} else {
359 			*(u_int16_t *)&aad[30] = 0;
360 			b0[1] = 0;
361 			aad[1] = 22 + IEEE80211_ADDR_LEN;
362 		}
363 	} else {
364 		if (IS_QOS_DATA(wh)) {
365 			struct ieee80211_qosframe *qwh =
366 				(struct ieee80211_qosframe*) wh;
367 			aad[24] = qwh->i_qos[0] & 0x0f;	/* just priority bits */
368 			aad[25] = 0;
369 			b0[1] = aad[24];
370 			aad[1] = 22 + 2;
371 		} else {
372 			*(u_int16_t *)&aad[24] = 0;
373 			b0[1] = 0;
374 			aad[1] = 22;
375 		}
376 		*(u_int16_t *)&aad[26] = 0;
377 		*(u_int32_t *)&aad[28] = 0;
378 	}
379 
380 	/* Start with the first block and AAD */
381 	rijndael_encrypt(ctx, b0, auth);
382 	xor_block(auth, aad, AES_BLOCK_LEN);
383 	rijndael_encrypt(ctx, auth, auth);
384 	xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
385 	rijndael_encrypt(ctx, auth, auth);
386 	b0[0] &= 0x07;
387 	b0[14] = b0[15] = 0;
388 	rijndael_encrypt(ctx, b0, s0);
389 #undef	IS_QOS_DATA
390 #undef	IS_4ADDRESS
391 }
392 
393 #define	CCMP_ENCRYPT(_i, _b, _b0, _pos, _e, _len) do {	\
394 	/* Authentication */				\
395 	xor_block(_b, _pos, _len);			\
396 	rijndael_encrypt(&ctx->cc_aes, _b, _b);		\
397 	/* Encryption, with counter */			\
398 	_b0[14] = (_i >> 8) & 0xff;			\
399 	_b0[15] = _i & 0xff;				\
400 	rijndael_encrypt(&ctx->cc_aes, _b0, _e);	\
401 	xor_block(_pos, _e, _len);			\
402 } while (0)
403 
404 static int
405 ccmp_encrypt(struct ieee80211_key *key, struct mbuf *m0, int hdrlen)
406 {
407 	struct ccmp_ctx *ctx = key->wk_private;
408 	struct ieee80211_frame *wh;
409 	struct mbuf *m = m0;
410 	int data_len, i, space;
411 	uint8_t aad[2 * AES_BLOCK_LEN], b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN],
412 		e[AES_BLOCK_LEN], s0[AES_BLOCK_LEN];
413 	uint8_t *pos;
414 
415 	ctx->cc_ic->ic_stats.is_crypto_ccmp++;
416 
417 	wh = mtod(m, struct ieee80211_frame *);
418 	data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header);
419 	ccmp_init_blocks(&ctx->cc_aes, wh, key->wk_keytsc,
420 		data_len, b0, aad, b, s0);
421 
422 	i = 1;
423 	pos = mtod(m, uint8_t *) + hdrlen + ccmp.ic_header;
424 	/* NB: assumes header is entirely in first mbuf */
425 	space = m->m_len - (hdrlen + ccmp.ic_header);
426 	for (;;) {
427 		if (space > data_len)
428 			space = data_len;
429 		/*
430 		 * Do full blocks.
431 		 */
432 		while (space >= AES_BLOCK_LEN) {
433 			CCMP_ENCRYPT(i, b, b0, pos, e, AES_BLOCK_LEN);
434 			pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
435 			data_len -= AES_BLOCK_LEN;
436 			i++;
437 		}
438 		if (data_len <= 0)		/* no more data */
439 			break;
440 		m = m->m_next;
441 		if (m == NULL) {		/* last buffer */
442 			if (space != 0) {
443 				/*
444 				 * Short last block.
445 				 */
446 				CCMP_ENCRYPT(i, b, b0, pos, e, space);
447 			}
448 			break;
449 		}
450 		if (space != 0) {
451 			uint8_t *pos_next;
452 			int space_next;
453 			int len, dl, sp;
454 			struct mbuf *n;
455 
456 			/*
457 			 * Block straddles one or more mbufs, gather data
458 			 * into the block buffer b, apply the cipher, then
459 			 * scatter the results back into the mbuf chain.
460 			 * The buffer will automatically get space bytes
461 			 * of data at offset 0 copied in+out by the
462 			 * CCMP_ENCRYPT request so we must take care of
463 			 * the remaining data.
464 			 */
465 			n = m;
466 			dl = data_len;
467 			sp = space;
468 			for (;;) {
469 				pos_next = mtod(n, uint8_t *);
470 				len = min(dl, AES_BLOCK_LEN);
471 				space_next = len > sp ? len - sp : 0;
472 				if (n->m_len >= space_next) {
473 					/*
474 					 * This mbuf has enough data; just grab
475 					 * what we need and stop.
476 					 */
477 					xor_block(b+sp, pos_next, space_next);
478 					break;
479 				}
480 				/*
481 				 * This mbuf's contents are insufficient,
482 				 * take 'em all and prepare to advance to
483 				 * the next mbuf.
484 				 */
485 				xor_block(b+sp, pos_next, n->m_len);
486 				sp += n->m_len, dl -= n->m_len;
487 				n = n->m_next;
488 				if (n == NULL)
489 					break;
490 			}
491 
492 			CCMP_ENCRYPT(i, b, b0, pos, e, space);
493 
494 			/* NB: just like above, but scatter data to mbufs */
495 			dl = data_len;
496 			sp = space;
497 			for (;;) {
498 				pos_next = mtod(m, uint8_t *);
499 				len = min(dl, AES_BLOCK_LEN);
500 				space_next = len > sp ? len - sp : 0;
501 				if (m->m_len >= space_next) {
502 					xor_block(pos_next, e+sp, space_next);
503 					break;
504 				}
505 				xor_block(pos_next, e+sp, m->m_len);
506 				sp += m->m_len, dl -= m->m_len;
507 				m = m->m_next;
508 				if (m == NULL)
509 					goto done;
510 			}
511 			/*
512 			 * Do bookkeeping.  m now points to the last mbuf
513 			 * we grabbed data from.  We know we consumed a
514 			 * full block of data as otherwise we'd have hit
515 			 * the end of the mbuf chain, so deduct from data_len.
516 			 * Otherwise advance the block number (i) and setup
517 			 * pos+space to reflect contents of the new mbuf.
518 			 */
519 			data_len -= AES_BLOCK_LEN;
520 			i++;
521 			pos = pos_next + space_next;
522 			space = m->m_len - space_next;
523 		} else {
524 			/*
525 			 * Setup for next buffer.
526 			 */
527 			pos = mtod(m, uint8_t *);
528 			space = m->m_len;
529 		}
530 	}
531 done:
532 	/* tack on MIC */
533 	xor_block(b, s0, ccmp.ic_trailer);
534 	return m_append(m0, ccmp.ic_trailer, b);
535 }
536 #undef CCMP_ENCRYPT
537 
538 #define	CCMP_DECRYPT(_i, _b, _b0, _pos, _a, _len) do {	\
539 	/* Decrypt, with counter */			\
540 	_b0[14] = (_i >> 8) & 0xff;			\
541 	_b0[15] = _i & 0xff;				\
542 	rijndael_encrypt(&ctx->cc_aes, _b0, _b);	\
543 	xor_block(_pos, _b, _len);			\
544 	/* Authentication */				\
545 	xor_block(_a, _pos, _len);			\
546 	rijndael_encrypt(&ctx->cc_aes, _a, _a);		\
547 } while (0)
548 
549 static int
550 ccmp_decrypt(struct ieee80211_key *key, u_int64_t pn, struct mbuf *m, int hdrlen)
551 {
552 	struct ccmp_ctx *ctx = key->wk_private;
553 	struct ieee80211_frame *wh;
554 	uint8_t aad[2 * AES_BLOCK_LEN];
555 	uint8_t b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN], a[AES_BLOCK_LEN];
556 	uint8_t mic[AES_BLOCK_LEN];
557 	size_t data_len;
558 	int i;
559 	uint8_t *pos;
560 	u_int space;
561 
562 	ctx->cc_ic->ic_stats.is_crypto_ccmp++;
563 
564 	wh = mtod(m, struct ieee80211_frame *);
565 	data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header + ccmp.ic_trailer);
566 	ccmp_init_blocks(&ctx->cc_aes, wh, pn, data_len, b0, aad, a, b);
567 	m_copydata(m, m->m_pkthdr.len - ccmp.ic_trailer, ccmp.ic_trailer, mic);
568 	xor_block(mic, b, ccmp.ic_trailer);
569 
570 	i = 1;
571 	pos = mtod(m, uint8_t *) + hdrlen + ccmp.ic_header;
572 	space = m->m_len - (hdrlen + ccmp.ic_header);
573 	for (;;) {
574 		if (space > data_len)
575 			space = data_len;
576 		while (space >= AES_BLOCK_LEN) {
577 			CCMP_DECRYPT(i, b, b0, pos, a, AES_BLOCK_LEN);
578 			pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
579 			data_len -= AES_BLOCK_LEN;
580 			i++;
581 		}
582 		if (data_len <= 0)		/* no more data */
583 			break;
584 		m = m->m_next;
585 		if (m == NULL) {		/* last buffer */
586 			if (space != 0)		/* short last block */
587 				CCMP_DECRYPT(i, b, b0, pos, a, space);
588 			break;
589 		}
590 		if (space != 0) {
591 			uint8_t *pos_next;
592 			u_int space_next;
593 			u_int len;
594 
595 			/*
596 			 * Block straddles buffers, split references.  We
597 			 * do not handle splits that require >2 buffers
598 			 * since rx'd frames are never badly fragmented
599 			 * because drivers typically recv in clusters.
600 			 */
601 			pos_next = mtod(m, uint8_t *);
602 			len = min(data_len, AES_BLOCK_LEN);
603 			space_next = len > space ? len - space : 0;
604 			KASSERT(m->m_len >= space_next,
605 				("not enough data in following buffer, "
606 				"m_len %u need %u\n", m->m_len, space_next));
607 
608 			xor_block(b+space, pos_next, space_next);
609 			CCMP_DECRYPT(i, b, b0, pos, a, space);
610 			xor_block(pos_next, b+space, space_next);
611 			data_len -= len;
612 			i++;
613 
614 			pos = pos_next + space_next;
615 			space = m->m_len - space_next;
616 		} else {
617 			/*
618 			 * Setup for next buffer.
619 			 */
620 			pos = mtod(m, uint8_t *);
621 			space = m->m_len;
622 		}
623 	}
624 	if (memcmp(mic, a, ccmp.ic_trailer) != 0) {
625 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
626 			"[%s] AES-CCM decrypt failed; MIC mismatch\n",
627 			ether_sprintf(wh->i_addr2));
628 		ctx->cc_ic->ic_stats.is_rx_ccmpmic++;
629 		return 0;
630 	}
631 	return 1;
632 }
633 #undef CCMP_DECRYPT
634 
635 /*
636  * Module glue.
637  */
638 static int
639 ccmp_modevent(module_t mod, int type, void *unused)
640 {
641 	switch (type) {
642 	case MOD_LOAD:
643 		ieee80211_crypto_register(&ccmp);
644 		return 0;
645 	case MOD_UNLOAD:
646 	case MOD_QUIESCE:
647 		if (nrefs) {
648 			printf("wlan_ccmp: still in use (%u dynamic refs)\n",
649 				nrefs);
650 			return EBUSY;
651 		}
652 		if (type == MOD_UNLOAD)
653 			ieee80211_crypto_unregister(&ccmp);
654 		return 0;
655 	}
656 	return EINVAL;
657 }
658 
659 static moduledata_t ccmp_mod = {
660 	"wlan_ccmp",
661 	ccmp_modevent,
662 	0
663 };
664 DECLARE_MODULE(wlan_ccmp, ccmp_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
665 MODULE_VERSION(wlan_ccmp, 1);
666 MODULE_DEPEND(wlan_ccmp, wlan, 1, 1, 1);
667