Home | History | Annotate | Line # | Download | only in net80211
ieee80211_crypto_ccmp.c revision 1.13.2.2
      1 /*	$NetBSD: ieee80211_crypto_ccmp.c,v 1.13.2.2 2018/09/06 06:56:44 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * Alternatively, this software may be distributed under the terms of the
     19  * GNU General Public License ("GPL") version 2 as published by the Free
     20  * Software Foundation.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifdef __FreeBSD__
     36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_crypto_ccmp.c,v 1.7 2005/07/11 03:06:23 sam Exp $");
     37 #endif
     38 #ifdef __NetBSD__
     39 __KERNEL_RCSID(0, "$NetBSD: ieee80211_crypto_ccmp.c,v 1.13.2.2 2018/09/06 06:56:44 pgoyette Exp $");
     40 #endif
     41 
     42 /*
     43  * IEEE 802.11i AES-CCMP crypto support.
     44  *
     45  * Part of this module is derived from similar code in the Host
     46  * AP driver. The code is used with the consent of the author and
     47  * its license is included below.
     48  */
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/malloc.h>
     53 #include <sys/kernel.h>
     54 
     55 #include <sys/socket.h>
     56 
     57 #include <net/if.h>
     58 #include <net/if_ether.h>
     59 #include <net/if_media.h>
     60 
     61 #include <net80211/ieee80211_var.h>
     62 
     63 #include <crypto/rijndael/rijndael.h>
     64 
     65 #define AES_BLOCK_LEN 16
     66 
     67 struct ccmp_ctx {
     68 	struct ieee80211com *cc_ic;	/* for diagnostics */
     69 	rijndael_ctx cc_aes;
     70 };
     71 
     72 static	void *ccmp_attach(struct ieee80211com *, struct ieee80211_key *);
     73 static	void ccmp_detach(struct ieee80211_key *);
     74 static	int ccmp_setkey(struct ieee80211_key *);
     75 static	int ccmp_encap(struct ieee80211_key *k, struct mbuf *, u_int8_t keyid);
     76 static	int ccmp_decap(struct ieee80211_key *, struct mbuf *, int);
     77 static	int ccmp_enmic(struct ieee80211_key *, struct mbuf *, int);
     78 static	int ccmp_demic(struct ieee80211_key *, struct mbuf *, int);
     79 
     80 const struct ieee80211_cipher 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 };
     95 
     96 #define	ccmp	ieee80211_cipher_ccmp
     97 
     98 static	int ccmp_encrypt(struct ieee80211_key *, struct mbuf *, int hdrlen);
     99 static	int ccmp_decrypt(struct ieee80211_key *, u_int64_t pn,
    100 		struct mbuf *, int hdrlen);
    101 
    102 static void *
    103 ccmp_attach(struct ieee80211com *ic, struct ieee80211_key *k)
    104 {
    105 	struct ccmp_ctx *ctx;
    106 
    107 	ctx = malloc(sizeof(struct ccmp_ctx), M_DEVBUF, M_NOWAIT | M_ZERO);
    108 	if (ctx == NULL) {
    109 		ic->ic_stats.is_crypto_nomem++;
    110 		return NULL;
    111 	}
    112 	ctx->cc_ic = ic;
    113 	return ctx;
    114 }
    115 
    116 static void
    117 ccmp_detach(struct ieee80211_key *k)
    118 {
    119 	struct ccmp_ctx *ctx = k->wk_private;
    120 
    121 	free(ctx, M_DEVBUF);
    122 }
    123 
    124 static int
    125 ccmp_setkey(struct ieee80211_key *k)
    126 {
    127 	struct ccmp_ctx *ctx = k->wk_private;
    128 
    129 	if (k->wk_keylen != (128/NBBY)) {
    130 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
    131 			"%s: Invalid key length %u, expecting %u\n",
    132 			__func__, k->wk_keylen, 128/NBBY);
    133 		return 0;
    134 	}
    135 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT)
    136 		rijndael_set_key(&ctx->cc_aes, k->wk_key, k->wk_keylen*NBBY);
    137 	return 1;
    138 }
    139 
    140 /*
    141  * Add privacy headers appropriate for the specified key.
    142  */
    143 static int
    144 ccmp_encap(struct ieee80211_key *k, struct mbuf *m, u_int8_t keyid)
    145 {
    146 	struct ccmp_ctx *ctx = k->wk_private;
    147 	struct ieee80211com *ic = ctx->cc_ic;
    148 	u_int8_t *ivp;
    149 	int hdrlen;
    150 
    151 	hdrlen = ieee80211_hdrspace(ic, mtod(m, void *));
    152 	ivp = mtod(m, u_int8_t *) + hdrlen;
    153 
    154 	k->wk_keytsc++;		/* XXX wrap at 48 bits */
    155 	ivp[0] = k->wk_keytsc >> 0;		/* PN0 */
    156 	ivp[1] = k->wk_keytsc >> 8;		/* PN1 */
    157 	ivp[2] = 0;				/* Reserved */
    158 	ivp[3] = keyid | IEEE80211_WEP_EXTIV;	/* KeyID | ExtID */
    159 	ivp[4] = k->wk_keytsc >> 16;		/* PN2 */
    160 	ivp[5] = k->wk_keytsc >> 24;		/* PN3 */
    161 	ivp[6] = k->wk_keytsc >> 32;		/* PN4 */
    162 	ivp[7] = k->wk_keytsc >> 40;		/* PN5 */
    163 
    164 	/*
    165 	 * Finally, do software encrypt if neeed.
    166 	 */
    167 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
    168 	    !ccmp_encrypt(k, m, hdrlen))
    169 		return 0;
    170 
    171 	return 1;
    172 }
    173 
    174 /*
    175  * Add MIC to the frame as needed.
    176  */
    177 static int
    178 ccmp_enmic(struct ieee80211_key *k, struct mbuf *m,
    179     int force)
    180 {
    181 
    182 	return 1;
    183 }
    184 
    185 static __inline uint64_t
    186 READ_6(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5)
    187 {
    188 	uint32_t iv32 = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
    189 	uint16_t iv16 = (b4 << 0) | (b5 << 8);
    190 	return (((uint64_t)iv16) << 32) | iv32;
    191 }
    192 
    193 /*
    194  * Validate and strip privacy headers (and trailer) for a
    195  * received frame. The specified key should be correct but
    196  * is also verified.
    197  */
    198 static int
    199 ccmp_decap(struct ieee80211_key *k, struct mbuf *m, int hdrlen)
    200 {
    201 	struct ccmp_ctx *ctx = k->wk_private;
    202 	struct ieee80211_frame *wh;
    203 	uint8_t *ivp;
    204 	uint64_t pn;
    205 
    206 	/*
    207 	 * Header should have extended IV and sequence number;
    208 	 * verify the former and validate the latter.
    209 	 */
    210 	wh = mtod(m, struct ieee80211_frame *);
    211 	ivp = mtod(m, uint8_t *) + hdrlen;
    212 	if ((ivp[IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV) == 0) {
    213 		/*
    214 		 * No extended IV; discard frame.
    215 		 */
    216 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
    217 			"[%s] Missing ExtIV for AES-CCM cipher\n",
    218 			ether_sprintf(wh->i_addr2));
    219 		ctx->cc_ic->ic_stats.is_rx_ccmpformat++;
    220 		return 0;
    221 	}
    222 	pn = READ_6(ivp[0], ivp[1], ivp[4], ivp[5], ivp[6], ivp[7]);
    223 	if (pn <= k->wk_keyrsc) {
    224 		/*
    225 		 * Replay violation.
    226 		 */
    227 		ieee80211_notify_replay_failure(ctx->cc_ic, wh, k, pn);
    228 		ctx->cc_ic->ic_stats.is_rx_ccmpreplay++;
    229 		return 0;
    230 	}
    231 
    232 	/*
    233 	 * Check if the device handled the decrypt in hardware.
    234 	 * If so we just strip the header; otherwise we need to
    235 	 * handle the decrypt in software.  Note that for the
    236 	 * latter we leave the header in place for use in the
    237 	 * decryption work.
    238 	 */
    239 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
    240 	    !ccmp_decrypt(k, pn, m, hdrlen))
    241 		return 0;
    242 
    243 	/*
    244 	 * Copy up 802.11 header and strip crypto bits.
    245 	 */
    246 	memmove(mtod(m, u_int8_t *) + ccmp.ic_header, mtod(m, void *), hdrlen);
    247 	m_adj(m, ccmp.ic_header);
    248 	m_adj(m, -ccmp.ic_trailer);
    249 
    250 	/*
    251 	 * Ok to update rsc now.
    252 	 */
    253 	k->wk_keyrsc = pn;
    254 
    255 	return 1;
    256 }
    257 
    258 /*
    259  * Verify and strip MIC from the frame.
    260  */
    261 static int
    262 ccmp_demic(struct ieee80211_key *k, struct mbuf *m, int force)
    263 {
    264 	return 1;
    265 }
    266 
    267 static __inline void
    268 xor_block(uint8_t *b, const uint8_t *a, size_t len)
    269 {
    270 	int i;
    271 	for (i = 0; i < len; i++)
    272 		b[i] ^= a[i];
    273 }
    274 
    275 /*
    276  * Host AP crypt: host-based CCMP encryption implementation for Host AP driver
    277  *
    278  * Copyright (c) 2003-2004, Jouni Malinen <jkmaline (at) cc.hut.fi>
    279  *
    280  * This program is free software; you can redistribute it and/or modify
    281  * it under the terms of the GNU General Public License version 2 as
    282  * published by the Free Software Foundation. See README and COPYING for
    283  * more details.
    284  *
    285  * Alternatively, this software may be distributed under the terms of BSD
    286  * license.
    287  */
    288 
    289 static void
    290 ccmp_init_blocks(rijndael_ctx *ctx, struct ieee80211_frame *wh,
    291 	u_int64_t pn, size_t dlen,
    292 	uint8_t b0[AES_BLOCK_LEN], uint8_t aad[2 * AES_BLOCK_LEN],
    293 	uint8_t auth[AES_BLOCK_LEN], uint8_t s0[AES_BLOCK_LEN])
    294 {
    295 #define	IS_4ADDRESS(wh) \
    296 	((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
    297 #define	IS_QOS_DATA(wh)	ieee80211_has_qos(wh)
    298 
    299 	/* CCM Initial Block:
    300 	 * Flag (Include authentication header, M=3 (8-octet MIC),
    301 	 *       L=1 (2-octet Dlen))
    302 	 * Nonce: 0x00 | A2 | PN
    303 	 * Dlen */
    304 	b0[0] = 0x59;
    305 	/* NB: b0[1] set below */
    306 	IEEE80211_ADDR_COPY(b0 + 2, wh->i_addr2);
    307 	b0[8] = pn >> 40;
    308 	b0[9] = pn >> 32;
    309 	b0[10] = pn >> 24;
    310 	b0[11] = pn >> 16;
    311 	b0[12] = pn >> 8;
    312 	b0[13] = pn >> 0;
    313 	b0[14] = (dlen >> 8) & 0xff;
    314 	b0[15] = dlen & 0xff;
    315 
    316 	/* AAD:
    317 	 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
    318 	 * A1 | A2 | A3
    319 	 * SC with bits 4..15 (seq#) masked to zero
    320 	 * A4 (if present)
    321 	 * QC (if present)
    322 	 */
    323 	aad[0] = 0;	/* AAD length >> 8 */
    324 	/* NB: aad[1] set below */
    325 	aad[2] = wh->i_fc[0] & 0x8f;	/* XXX magic #s */
    326 	aad[3] = wh->i_fc[1] & 0xc7;	/* XXX magic #s */
    327 	/* NB: we know 3 addresses are contiguous */
    328 	memcpy(aad + 4, wh->i_addr1, 3 * IEEE80211_ADDR_LEN);
    329 	aad[22] = wh->i_seq[0] & IEEE80211_SEQ_FRAG_MASK;
    330 	aad[23] = 0; /* all bits masked */
    331 	/*
    332 	 * Construct variable-length portion of AAD based
    333 	 * on whether this is a 4-address frame/QOS frame.
    334 	 * We always zero-pad to 32 bytes before running it
    335 	 * through the cipher.
    336 	 *
    337 	 * We also fill in the priority bits of the CCM
    338 	 * initial block as we know whether or not we have
    339 	 * a QOS frame.
    340 	 */
    341 	if (IS_4ADDRESS(wh)) {
    342 		IEEE80211_ADDR_COPY(aad + 24,
    343 			((struct ieee80211_frame_addr4 *)wh)->i_addr4);
    344 		if (IS_QOS_DATA(wh)) {
    345 			struct ieee80211_qosframe_addr4 *qwh4 =
    346 				(struct ieee80211_qosframe_addr4 *) wh;
    347 			aad[30] = qwh4->i_qos[0] & 0x0f;/* just priority bits */
    348 			aad[31] = 0;
    349 			b0[1] = aad[30];
    350 			aad[1] = 22 + IEEE80211_ADDR_LEN + 2;
    351 		} else {
    352 			*(u_int16_t *)&aad[30] = 0;
    353 			b0[1] = 0;
    354 			aad[1] = 22 + IEEE80211_ADDR_LEN;
    355 		}
    356 	} else {
    357 		if (IS_QOS_DATA(wh)) {
    358 			struct ieee80211_qosframe *qwh =
    359 				(struct ieee80211_qosframe*) wh;
    360 			aad[24] = qwh->i_qos[0] & 0x0f;	/* just priority bits */
    361 			aad[25] = 0;
    362 			b0[1] = aad[24];
    363 			aad[1] = 22 + 2;
    364 		} else {
    365 			*(u_int16_t *)&aad[24] = 0;
    366 			b0[1] = 0;
    367 			aad[1] = 22;
    368 		}
    369 		*(u_int16_t *)&aad[26] = 0;
    370 		*(u_int32_t *)&aad[28] = 0;
    371 	}
    372 
    373 	/* Start with the first block and AAD */
    374 	rijndael_encrypt(ctx, b0, auth);
    375 	xor_block(auth, aad, AES_BLOCK_LEN);
    376 	rijndael_encrypt(ctx, auth, auth);
    377 	xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
    378 	rijndael_encrypt(ctx, auth, auth);
    379 	b0[0] &= 0x07;
    380 	b0[14] = b0[15] = 0;
    381 	rijndael_encrypt(ctx, b0, s0);
    382 #undef	IS_QOS_DATA
    383 #undef	IS_4ADDRESS
    384 }
    385 
    386 #define	CCMP_ENCRYPT(_i, _b, _b0, _pos, _e, _len) do {	\
    387 	/* Authentication */				\
    388 	xor_block(_b, _pos, _len);			\
    389 	rijndael_encrypt(&ctx->cc_aes, _b, _b);		\
    390 	/* Encryption, with counter */			\
    391 	_b0[14] = (_i >> 8) & 0xff;			\
    392 	_b0[15] = _i & 0xff;				\
    393 	rijndael_encrypt(&ctx->cc_aes, _b0, _e);	\
    394 	xor_block(_pos, _e, _len);			\
    395 } while (0)
    396 
    397 static int
    398 ccmp_encrypt(struct ieee80211_key *key, struct mbuf *m0, int hdrlen)
    399 {
    400 	struct ccmp_ctx *ctx = key->wk_private;
    401 	struct ieee80211_frame *wh;
    402 	struct mbuf *m = m0;
    403 	int data_len, i, space;
    404 	uint8_t aad[2 * AES_BLOCK_LEN], b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN],
    405 		e[AES_BLOCK_LEN], s0[AES_BLOCK_LEN];
    406 	uint8_t *pos;
    407 
    408 	ctx->cc_ic->ic_stats.is_crypto_ccmp++;
    409 
    410 	wh = mtod(m, struct ieee80211_frame *);
    411 	data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header);
    412 	ccmp_init_blocks(&ctx->cc_aes, wh, key->wk_keytsc,
    413 		data_len, b0, aad, b, s0);
    414 
    415 	i = 1;
    416 	pos = mtod(m, uint8_t *) + hdrlen + ccmp.ic_header;
    417 	/* NB: assumes header is entirely in first mbuf */
    418 	space = m->m_len - (hdrlen + ccmp.ic_header);
    419 	for (;;) {
    420 		if (space > data_len)
    421 			space = data_len;
    422 
    423 		/*
    424 		 * Do full blocks.
    425 		 */
    426 		while (space >= AES_BLOCK_LEN) {
    427 			CCMP_ENCRYPT(i, b, b0, pos, e, AES_BLOCK_LEN);
    428 			pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
    429 			data_len -= AES_BLOCK_LEN;
    430 			i++;
    431 		}
    432 		if (data_len <= 0)		/* no more data */
    433 			break;
    434 
    435 		m = m->m_next;
    436 		if (m == NULL) {		/* last buffer */
    437 			if (space != 0) {
    438 				/*
    439 				 * Short last block.
    440 				 */
    441 				CCMP_ENCRYPT(i, b, b0, pos, e, space);
    442 			}
    443 			break;
    444 		}
    445 		if (space != 0) {
    446 			uint8_t *pos_next;
    447 			int space_next;
    448 			int len, dl, sp;
    449 			struct mbuf *n;
    450 
    451 			/*
    452 			 * Block straddles one or more mbufs, gather data
    453 			 * into the block buffer b, apply the cipher, then
    454 			 * scatter the results back into the mbuf chain.
    455 			 * The buffer will automatically get space bytes
    456 			 * of data at offset 0 copied in+out by the
    457 			 * CCMP_ENCRYPT request so we must take care of
    458 			 * the remaining data.
    459 			 */
    460 			n = m;
    461 			dl = data_len;
    462 			sp = space;
    463 			for (;;) {
    464 				pos_next = mtod(n, uint8_t *);
    465 				len = uimin(dl, AES_BLOCK_LEN);
    466 				space_next = len > sp ? len - sp : 0;
    467 				if (n->m_len >= space_next) {
    468 					/*
    469 					 * This mbuf has enough data; just grab
    470 					 * what we need and stop.
    471 					 */
    472 					xor_block(b+sp, pos_next, space_next);
    473 					break;
    474 				}
    475 				/*
    476 				 * This mbuf's contents are insufficient,
    477 				 * take them all and prepare to advance to
    478 				 * the next mbuf.
    479 				 */
    480 				xor_block(b+sp, pos_next, n->m_len);
    481 				sp += n->m_len, dl -= n->m_len;
    482 				n = n->m_next;
    483 				if (n == NULL)
    484 					break;
    485 			}
    486 
    487 			CCMP_ENCRYPT(i, b, b0, pos, e, space);
    488 
    489 			/* NB: just like above, but scatter data to mbufs */
    490 			dl = data_len;
    491 			sp = space;
    492 			for (;;) {
    493 				pos_next = mtod(m, uint8_t *);
    494 				len = uimin(dl, AES_BLOCK_LEN);
    495 				space_next = len > sp ? len - sp : 0;
    496 				if (m->m_len >= space_next) {
    497 					xor_block(pos_next, e+sp, space_next);
    498 					break;
    499 				}
    500 				xor_block(pos_next, e+sp, m->m_len);
    501 				sp += m->m_len, dl -= m->m_len;
    502 				m = m->m_next;
    503 				if (m == NULL)
    504 					goto done;
    505 			}
    506 
    507 			/*
    508 			 * Do bookkeeping.  m now points to the last mbuf
    509 			 * we grabbed data from.  We know we consumed a
    510 			 * full block of data as otherwise we'd have hit
    511 			 * the end of the mbuf chain, so deduct from data_len.
    512 			 * Otherwise advance the block number (i) and setup
    513 			 * pos+space to reflect contents of the new mbuf.
    514 			 */
    515 			data_len -= AES_BLOCK_LEN;
    516 			i++;
    517 			pos = pos_next + space_next;
    518 			space = m->m_len - space_next;
    519 		} else {
    520 			/*
    521 			 * Setup for next buffer.
    522 			 */
    523 			pos = mtod(m, uint8_t *);
    524 			space = m->m_len;
    525 		}
    526 	}
    527 
    528 done:
    529 	/* tack on MIC */
    530 	xor_block(b, s0, ccmp.ic_trailer);
    531 	return m_append(m0, ccmp.ic_trailer, b);
    532 }
    533 #undef CCMP_ENCRYPT
    534 
    535 #define	CCMP_DECRYPT(_i, _b, _b0, _pos, _a, _len) do {	\
    536 	/* Decrypt, with counter */			\
    537 	_b0[14] = (_i >> 8) & 0xff;			\
    538 	_b0[15] = _i & 0xff;				\
    539 	rijndael_encrypt(&ctx->cc_aes, _b0, _b);	\
    540 	xor_block(_pos, _b, _len);			\
    541 	/* Authentication */				\
    542 	xor_block(_a, _pos, _len);			\
    543 	rijndael_encrypt(&ctx->cc_aes, _a, _a);		\
    544 } while (0)
    545 
    546 static int
    547 ccmp_decrypt(struct ieee80211_key *key, u_int64_t pn, struct mbuf *m,
    548     int hdrlen)
    549 {
    550 	struct ccmp_ctx *ctx = key->wk_private;
    551 	struct ieee80211_frame *wh;
    552 	uint8_t aad[2 * AES_BLOCK_LEN];
    553 	uint8_t b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN], a[AES_BLOCK_LEN];
    554 	uint8_t mic[AES_BLOCK_LEN];
    555 	size_t data_len;
    556 	int i;
    557 	uint8_t *pos;
    558 	u_int space;
    559 
    560 	ctx->cc_ic->ic_stats.is_crypto_ccmp++;
    561 
    562 	wh = mtod(m, struct ieee80211_frame *);
    563 	data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header + ccmp.ic_trailer);
    564 	ccmp_init_blocks(&ctx->cc_aes, wh, pn, data_len, b0, aad, a, b);
    565 	m_copydata(m, m->m_pkthdr.len - ccmp.ic_trailer, ccmp.ic_trailer, mic);
    566 	xor_block(mic, b, ccmp.ic_trailer);
    567 
    568 	i = 1;
    569 	pos = mtod(m, uint8_t *) + hdrlen + ccmp.ic_header;
    570 	space = m->m_len - (hdrlen + ccmp.ic_header);
    571 	for (;;) {
    572 		if (space > data_len)
    573 			space = data_len;
    574 		while (space >= AES_BLOCK_LEN) {
    575 			CCMP_DECRYPT(i, b, b0, pos, a, AES_BLOCK_LEN);
    576 			pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
    577 			data_len -= AES_BLOCK_LEN;
    578 			i++;
    579 		}
    580 		if (data_len <= 0)		/* no more data */
    581 			break;
    582 
    583 		m = m->m_next;
    584 		if (m == NULL) {		/* last buffer */
    585 			if (space != 0)		/* short last block */
    586 				CCMP_DECRYPT(i, b, b0, pos, a, space);
    587 			break;
    588 		}
    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 = uimin(data_len, AES_BLOCK_LEN);
    603 			space_next = len > space ? len - space : 0;
    604 			IASSERT(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 
    625 	if (memcmp(mic, a, ccmp.ic_trailer) != 0) {
    626 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
    627 			"[%s] AES-CCM decrypt failed; MIC mismatch\n",
    628 			ether_sprintf(wh->i_addr2));
    629 		ctx->cc_ic->ic_stats.is_rx_ccmpmic++;
    630 		return 0;
    631 	}
    632 
    633 	return 1;
    634 }
    635 #undef CCMP_DECRYPT
    636 
    637 IEEE80211_CRYPTO_SETUP(ccmp_register)
    638 {
    639 	ieee80211_crypto_register(&ccmp);
    640 }
    641