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