18a1b9b6aSSam Leffler /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
4b032f27cSSam Leffler  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
58a1b9b6aSSam Leffler  * All rights reserved.
68a1b9b6aSSam Leffler  *
78a1b9b6aSSam Leffler  * Redistribution and use in source and binary forms, with or without
88a1b9b6aSSam Leffler  * modification, are permitted provided that the following conditions
98a1b9b6aSSam Leffler  * are met:
108a1b9b6aSSam Leffler  * 1. Redistributions of source code must retain the above copyright
118a1b9b6aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
128a1b9b6aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
138a1b9b6aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
148a1b9b6aSSam Leffler  *    documentation and/or other materials provided with the distribution.
158a1b9b6aSSam Leffler  *
168a1b9b6aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
178a1b9b6aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
188a1b9b6aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198a1b9b6aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
208a1b9b6aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
218a1b9b6aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
228a1b9b6aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
238a1b9b6aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
248a1b9b6aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
258a1b9b6aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
268a1b9b6aSSam Leffler  */
278a1b9b6aSSam Leffler 
288a1b9b6aSSam Leffler #include <sys/cdefs.h>
298a1b9b6aSSam Leffler /*
308a1b9b6aSSam Leffler  * IEEE 802.11i AES-CCMP crypto support.
318a1b9b6aSSam Leffler  *
328a1b9b6aSSam Leffler  * Part of this module is derived from similar code in the Host
338a1b9b6aSSam Leffler  * AP driver. The code is used with the consent of the author and
348a1b9b6aSSam Leffler  * it's license is included below.
358a1b9b6aSSam Leffler  */
36b032f27cSSam Leffler #include "opt_wlan.h"
37b032f27cSSam Leffler 
388a1b9b6aSSam Leffler #include <sys/param.h>
398a1b9b6aSSam Leffler #include <sys/systm.h>
408a1b9b6aSSam Leffler #include <sys/mbuf.h>
418a1b9b6aSSam Leffler #include <sys/malloc.h>
428a1b9b6aSSam Leffler #include <sys/kernel.h>
438a1b9b6aSSam Leffler #include <sys/module.h>
448a1b9b6aSSam Leffler 
458a1b9b6aSSam Leffler #include <sys/socket.h>
468a1b9b6aSSam Leffler 
478a1b9b6aSSam Leffler #include <net/if.h>
488a1b9b6aSSam Leffler #include <net/if_media.h>
498a1b9b6aSSam Leffler #include <net/ethernet.h>
508a1b9b6aSSam Leffler 
518a1b9b6aSSam Leffler #include <net80211/ieee80211_var.h>
528a1b9b6aSSam Leffler 
538a1b9b6aSSam Leffler #include <crypto/rijndael/rijndael.h>
548a1b9b6aSSam Leffler 
558a1b9b6aSSam Leffler #define AES_BLOCK_LEN 16
568a1b9b6aSSam Leffler 
578a1b9b6aSSam Leffler struct ccmp_ctx {
58b032f27cSSam Leffler 	struct ieee80211vap *cc_vap;	/* for diagnostics+statistics */
59b032f27cSSam Leffler 	struct ieee80211com *cc_ic;
608a1b9b6aSSam Leffler 	rijndael_ctx	     cc_aes;
618a1b9b6aSSam Leffler };
628a1b9b6aSSam Leffler 
63b032f27cSSam Leffler static	void *ccmp_attach(struct ieee80211vap *, struct ieee80211_key *);
648a1b9b6aSSam Leffler static	void ccmp_detach(struct ieee80211_key *);
658a1b9b6aSSam Leffler static	int ccmp_setkey(struct ieee80211_key *);
66c0cb9349SAdrian Chadd static	void ccmp_setiv(struct ieee80211_key *, uint8_t *);
67ef0d8f63SAdrian Chadd static	int ccmp_encap(struct ieee80211_key *, struct mbuf *);
682cc12adeSSam Leffler static	int ccmp_decap(struct ieee80211_key *, struct mbuf *, int);
6996d88463SSam Leffler static	int ccmp_enmic(struct ieee80211_key *, struct mbuf *, int);
7096d88463SSam Leffler static	int ccmp_demic(struct ieee80211_key *, struct mbuf *, int);
718a1b9b6aSSam Leffler 
728a1b9b6aSSam Leffler static const struct ieee80211_cipher ccmp = {
738a1b9b6aSSam Leffler 	.ic_name	= "AES-CCM",
748a1b9b6aSSam Leffler 	.ic_cipher	= IEEE80211_CIPHER_AES_CCM,
758a1b9b6aSSam Leffler 	.ic_header	= IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
768a1b9b6aSSam Leffler 			  IEEE80211_WEP_EXTIVLEN,
778a1b9b6aSSam Leffler 	.ic_trailer	= IEEE80211_WEP_MICLEN,
788a1b9b6aSSam Leffler 	.ic_miclen	= 0,
798a1b9b6aSSam Leffler 	.ic_attach	= ccmp_attach,
808a1b9b6aSSam Leffler 	.ic_detach	= ccmp_detach,
818a1b9b6aSSam Leffler 	.ic_setkey	= ccmp_setkey,
82c0cb9349SAdrian Chadd 	.ic_setiv	= ccmp_setiv,
838a1b9b6aSSam Leffler 	.ic_encap	= ccmp_encap,
848a1b9b6aSSam Leffler 	.ic_decap	= ccmp_decap,
858a1b9b6aSSam Leffler 	.ic_enmic	= ccmp_enmic,
868a1b9b6aSSam Leffler 	.ic_demic	= ccmp_demic,
878a1b9b6aSSam Leffler };
888a1b9b6aSSam Leffler 
898a1b9b6aSSam Leffler static	int ccmp_encrypt(struct ieee80211_key *, struct mbuf *, int hdrlen);
908a1b9b6aSSam Leffler static	int ccmp_decrypt(struct ieee80211_key *, u_int64_t pn,
918a1b9b6aSSam Leffler 		struct mbuf *, int hdrlen);
928a1b9b6aSSam Leffler 
93d16441fdSSam Leffler /* number of references from net80211 layer */
94d16441fdSSam Leffler static	int nrefs = 0;
95d16441fdSSam Leffler 
968a1b9b6aSSam Leffler static void *
ccmp_attach(struct ieee80211vap * vap,struct ieee80211_key * k)97b032f27cSSam Leffler ccmp_attach(struct ieee80211vap *vap, struct ieee80211_key *k)
988a1b9b6aSSam Leffler {
998a1b9b6aSSam Leffler 	struct ccmp_ctx *ctx;
1008a1b9b6aSSam Leffler 
101b9b53389SAdrian Chadd 	ctx = (struct ccmp_ctx *) IEEE80211_MALLOC(sizeof(struct ccmp_ctx),
102b9b53389SAdrian Chadd 		M_80211_CRYPTO, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
1038a1b9b6aSSam Leffler 	if (ctx == NULL) {
104b032f27cSSam Leffler 		vap->iv_stats.is_crypto_nomem++;
1058a1b9b6aSSam Leffler 		return NULL;
1068a1b9b6aSSam Leffler 	}
107b032f27cSSam Leffler 	ctx->cc_vap = vap;
108b032f27cSSam Leffler 	ctx->cc_ic = vap->iv_ic;
109d16441fdSSam Leffler 	nrefs++;			/* NB: we assume caller locking */
1108a1b9b6aSSam Leffler 	return ctx;
1118a1b9b6aSSam Leffler }
1128a1b9b6aSSam Leffler 
1138a1b9b6aSSam Leffler static void
ccmp_detach(struct ieee80211_key * k)1148a1b9b6aSSam Leffler ccmp_detach(struct ieee80211_key *k)
1158a1b9b6aSSam Leffler {
1168a1b9b6aSSam Leffler 	struct ccmp_ctx *ctx = k->wk_private;
1178a1b9b6aSSam Leffler 
118b9b53389SAdrian Chadd 	IEEE80211_FREE(ctx, M_80211_CRYPTO);
119d16441fdSSam Leffler 	KASSERT(nrefs > 0, ("imbalanced attach/detach"));
120d16441fdSSam Leffler 	nrefs--;			/* NB: we assume caller locking */
1218a1b9b6aSSam Leffler }
1228a1b9b6aSSam Leffler 
1238a1b9b6aSSam Leffler static int
ccmp_setkey(struct ieee80211_key * k)1248a1b9b6aSSam Leffler ccmp_setkey(struct ieee80211_key *k)
1258a1b9b6aSSam Leffler {
1268a1b9b6aSSam Leffler 	struct ccmp_ctx *ctx = k->wk_private;
1278a1b9b6aSSam Leffler 
1288a1b9b6aSSam Leffler 	if (k->wk_keylen != (128/NBBY)) {
129b032f27cSSam Leffler 		IEEE80211_DPRINTF(ctx->cc_vap, IEEE80211_MSG_CRYPTO,
1308a1b9b6aSSam Leffler 			"%s: Invalid key length %u, expecting %u\n",
1318a1b9b6aSSam Leffler 			__func__, k->wk_keylen, 128/NBBY);
1328a1b9b6aSSam Leffler 		return 0;
1338a1b9b6aSSam Leffler 	}
1345c1f7f19SSam Leffler 	if (k->wk_flags & IEEE80211_KEY_SWENCRYPT)
1358a1b9b6aSSam Leffler 		rijndael_set_key(&ctx->cc_aes, k->wk_key, k->wk_keylen*NBBY);
1368a1b9b6aSSam Leffler 	return 1;
1378a1b9b6aSSam Leffler }
1388a1b9b6aSSam Leffler 
139c0cb9349SAdrian Chadd static void
ccmp_setiv(struct ieee80211_key * k,uint8_t * ivp)140c0cb9349SAdrian Chadd ccmp_setiv(struct ieee80211_key *k, uint8_t *ivp)
141c0cb9349SAdrian Chadd {
142c0cb9349SAdrian Chadd 	struct ccmp_ctx *ctx = k->wk_private;
143c0cb9349SAdrian Chadd 	struct ieee80211vap *vap = ctx->cc_vap;
144c0cb9349SAdrian Chadd 	uint8_t keyid;
145c0cb9349SAdrian Chadd 
146c0cb9349SAdrian Chadd 	keyid = ieee80211_crypto_get_keyid(vap, k) << 6;
147c0cb9349SAdrian Chadd 
148c0cb9349SAdrian Chadd 	k->wk_keytsc++;
149c0cb9349SAdrian Chadd 	ivp[0] = k->wk_keytsc >> 0;		/* PN0 */
150c0cb9349SAdrian Chadd 	ivp[1] = k->wk_keytsc >> 8;		/* PN1 */
151c0cb9349SAdrian Chadd 	ivp[2] = 0;				/* Reserved */
152c0cb9349SAdrian Chadd 	ivp[3] = keyid | IEEE80211_WEP_EXTIV;	/* KeyID | ExtID */
153c0cb9349SAdrian Chadd 	ivp[4] = k->wk_keytsc >> 16;		/* PN2 */
154c0cb9349SAdrian Chadd 	ivp[5] = k->wk_keytsc >> 24;		/* PN3 */
155c0cb9349SAdrian Chadd 	ivp[6] = k->wk_keytsc >> 32;		/* PN4 */
156c0cb9349SAdrian Chadd 	ivp[7] = k->wk_keytsc >> 40;		/* PN5 */
157c0cb9349SAdrian Chadd }
158c0cb9349SAdrian Chadd 
1598a1b9b6aSSam Leffler /*
1608a1b9b6aSSam Leffler  * Add privacy headers appropriate for the specified key.
1618a1b9b6aSSam Leffler  */
1628a1b9b6aSSam Leffler static int
ccmp_encap(struct ieee80211_key * k,struct mbuf * m)163ef0d8f63SAdrian Chadd ccmp_encap(struct ieee80211_key *k, struct mbuf *m)
1648a1b9b6aSSam Leffler {
165fe75b452SAdrian Chadd 	const struct ieee80211_frame *wh;
1664e844c94SSam Leffler 	struct ccmp_ctx *ctx = k->wk_private;
1674e844c94SSam Leffler 	struct ieee80211com *ic = ctx->cc_ic;
16868e8e04eSSam Leffler 	uint8_t *ivp;
1698a1b9b6aSSam Leffler 	int hdrlen;
170fe75b452SAdrian Chadd 	int is_mgmt;
1718a1b9b6aSSam Leffler 
1724e844c94SSam Leffler 	hdrlen = ieee80211_hdrspace(ic, mtod(m, void *));
173fe75b452SAdrian Chadd 	wh = mtod(m, const struct ieee80211_frame *);
174fe75b452SAdrian Chadd 	is_mgmt = IEEE80211_IS_MGMT(wh);
175fe75b452SAdrian Chadd 
176fe75b452SAdrian Chadd 	/*
177fe75b452SAdrian Chadd 	 * Check to see if we need to insert IV/MIC.
178fe75b452SAdrian Chadd 	 *
179fe75b452SAdrian Chadd 	 * Some offload devices don't require the IV to be inserted
180fe75b452SAdrian Chadd 	 * as part of the hardware encryption.
181fe75b452SAdrian Chadd 	 */
182fe75b452SAdrian Chadd 	if (is_mgmt && (k->wk_flags & IEEE80211_KEY_NOIVMGT))
183fe75b452SAdrian Chadd 		return 1;
184fe75b452SAdrian Chadd 	if ((! is_mgmt) && (k->wk_flags & IEEE80211_KEY_NOIV))
185fe75b452SAdrian Chadd 		return 1;
1868a1b9b6aSSam Leffler 
1878a1b9b6aSSam Leffler 	/*
1888a1b9b6aSSam Leffler 	 * Copy down 802.11 header and add the IV, KeyID, and ExtIV.
1898a1b9b6aSSam Leffler 	 */
190bd29f817SBjoern A. Zeeb 	M_PREPEND(m, ccmp.ic_header, IEEE80211_M_NOWAIT);
1918a1b9b6aSSam Leffler 	if (m == NULL)
1928a1b9b6aSSam Leffler 		return 0;
19368e8e04eSSam Leffler 	ivp = mtod(m, uint8_t *);
1948a1b9b6aSSam Leffler 	ovbcopy(ivp + ccmp.ic_header, ivp, hdrlen);
1958a1b9b6aSSam Leffler 	ivp += hdrlen;
1968a1b9b6aSSam Leffler 
197c0cb9349SAdrian Chadd 	ccmp_setiv(k, ivp);
1988a1b9b6aSSam Leffler 
1998a1b9b6aSSam Leffler 	/*
200bf0b7b45SAdrian Chadd 	 * Finally, do software encrypt if needed.
2018a1b9b6aSSam Leffler 	 */
2025c1f7f19SSam Leffler 	if ((k->wk_flags & IEEE80211_KEY_SWENCRYPT) &&
2038a1b9b6aSSam Leffler 	    !ccmp_encrypt(k, m, hdrlen))
2048a1b9b6aSSam Leffler 		return 0;
2058a1b9b6aSSam Leffler 
2068a1b9b6aSSam Leffler 	return 1;
2078a1b9b6aSSam Leffler }
2088a1b9b6aSSam Leffler 
2098a1b9b6aSSam Leffler /*
2108a1b9b6aSSam Leffler  * Add MIC to the frame as needed.
2118a1b9b6aSSam Leffler  */
2128a1b9b6aSSam Leffler static int
ccmp_enmic(struct ieee80211_key * k,struct mbuf * m,int force)21396d88463SSam Leffler ccmp_enmic(struct ieee80211_key *k, struct mbuf *m, int force)
2148a1b9b6aSSam Leffler {
2158a1b9b6aSSam Leffler 
2168a1b9b6aSSam Leffler 	return 1;
2178a1b9b6aSSam Leffler }
2188a1b9b6aSSam Leffler 
2198a1b9b6aSSam Leffler static __inline uint64_t
READ_6(uint8_t b0,uint8_t b1,uint8_t b2,uint8_t b3,uint8_t b4,uint8_t b5)2208a1b9b6aSSam Leffler READ_6(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5)
2218a1b9b6aSSam Leffler {
2228a1b9b6aSSam Leffler 	uint32_t iv32 = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
2238a1b9b6aSSam Leffler 	uint16_t iv16 = (b4 << 0) | (b5 << 8);
2248a1b9b6aSSam Leffler 	return (((uint64_t)iv16) << 32) | iv32;
2258a1b9b6aSSam Leffler }
2268a1b9b6aSSam Leffler 
2278a1b9b6aSSam Leffler /*
2288a1b9b6aSSam Leffler  * Validate and strip privacy headers (and trailer) for a
2298a1b9b6aSSam Leffler  * received frame. The specified key should be correct but
2308a1b9b6aSSam Leffler  * is also verified.
2318a1b9b6aSSam Leffler  */
2328a1b9b6aSSam Leffler static int
ccmp_decap(struct ieee80211_key * k,struct mbuf * m,int hdrlen)2332cc12adeSSam Leffler ccmp_decap(struct ieee80211_key *k, struct mbuf *m, int hdrlen)
2348a1b9b6aSSam Leffler {
235fe75b452SAdrian Chadd 	const struct ieee80211_rx_stats *rxs;
2368a1b9b6aSSam Leffler 	struct ccmp_ctx *ctx = k->wk_private;
237b032f27cSSam Leffler 	struct ieee80211vap *vap = ctx->cc_vap;
2388a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
239b032f27cSSam Leffler 	uint8_t *ivp, tid;
2408a1b9b6aSSam Leffler 	uint64_t pn;
2418a1b9b6aSSam Leffler 
242fe75b452SAdrian Chadd 	rxs = ieee80211_get_rx_params_ptr(m);
243fe75b452SAdrian Chadd 
2445899368aSAdrian Chadd 	if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP))
245fe75b452SAdrian Chadd 		goto finish;
246fe75b452SAdrian Chadd 
2478a1b9b6aSSam Leffler 	/*
2488a1b9b6aSSam Leffler 	 * Header should have extended IV and sequence number;
2498a1b9b6aSSam Leffler 	 * verify the former and validate the latter.
2508a1b9b6aSSam Leffler 	 */
2518a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
2528a1b9b6aSSam Leffler 	ivp = mtod(m, uint8_t *) + hdrlen;
2538a1b9b6aSSam Leffler 	if ((ivp[IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV) == 0) {
2548a1b9b6aSSam Leffler 		/*
2558a1b9b6aSSam Leffler 		 * No extended IV; discard frame.
2568a1b9b6aSSam Leffler 		 */
257b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
258b032f27cSSam Leffler 			"%s", "missing ExtIV for AES-CCM cipher");
259b032f27cSSam Leffler 		vap->iv_stats.is_rx_ccmpformat++;
2608a1b9b6aSSam Leffler 		return 0;
2618a1b9b6aSSam Leffler 	}
262b032f27cSSam Leffler 	tid = ieee80211_gettid(wh);
2638a1b9b6aSSam Leffler 	pn = READ_6(ivp[0], ivp[1], ivp[4], ivp[5], ivp[6], ivp[7]);
2645d766a09SBernhard Schmidt 	if (pn <= k->wk_keyrsc[tid] &&
2655d766a09SBernhard Schmidt 	    (k->wk_flags & IEEE80211_KEY_NOREPLAY) == 0) {
2668a1b9b6aSSam Leffler 		/*
2678a1b9b6aSSam Leffler 		 * Replay violation.
2688a1b9b6aSSam Leffler 		 */
269ebaf87ebSSam Leffler 		ieee80211_notify_replay_failure(vap, wh, k, pn, tid);
270b032f27cSSam Leffler 		vap->iv_stats.is_rx_ccmpreplay++;
2718a1b9b6aSSam Leffler 		return 0;
2728a1b9b6aSSam Leffler 	}
2738a1b9b6aSSam Leffler 
2748a1b9b6aSSam Leffler 	/*
2758a1b9b6aSSam Leffler 	 * Check if the device handled the decrypt in hardware.
2768a1b9b6aSSam Leffler 	 * If so we just strip the header; otherwise we need to
2778a1b9b6aSSam Leffler 	 * handle the decrypt in software.  Note that for the
2788a1b9b6aSSam Leffler 	 * latter we leave the header in place for use in the
2798a1b9b6aSSam Leffler 	 * decryption work.
2808a1b9b6aSSam Leffler 	 */
2815c1f7f19SSam Leffler 	if ((k->wk_flags & IEEE80211_KEY_SWDECRYPT) &&
2828a1b9b6aSSam Leffler 	    !ccmp_decrypt(k, pn, m, hdrlen))
2838a1b9b6aSSam Leffler 		return 0;
2848a1b9b6aSSam Leffler 
285fe75b452SAdrian Chadd finish:
2868a1b9b6aSSam Leffler 	/*
2878a1b9b6aSSam Leffler 	 * Copy up 802.11 header and strip crypto bits.
2888a1b9b6aSSam Leffler 	 */
2895899368aSAdrian Chadd 	if (! ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP))) {
290fe75b452SAdrian Chadd 		ovbcopy(mtod(m, void *), mtod(m, uint8_t *) + ccmp.ic_header,
291fe75b452SAdrian Chadd 		    hdrlen);
2928a1b9b6aSSam Leffler 		m_adj(m, ccmp.ic_header);
293fe75b452SAdrian Chadd 	}
294fe75b452SAdrian Chadd 
295fe75b452SAdrian Chadd 	/*
296fe75b452SAdrian Chadd 	 * XXX TODO: see if MMIC_STRIP also covers CCMP MIC trailer.
297fe75b452SAdrian Chadd 	 */
2985899368aSAdrian Chadd 	if (! ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_MMIC_STRIP)))
2998a1b9b6aSSam Leffler 		m_adj(m, -ccmp.ic_trailer);
3008a1b9b6aSSam Leffler 
3018a1b9b6aSSam Leffler 	/*
3028a1b9b6aSSam Leffler 	 * Ok to update rsc now.
3038a1b9b6aSSam Leffler 	 */
3045899368aSAdrian Chadd 	if (! ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP))) {
305b032f27cSSam Leffler 		k->wk_keyrsc[tid] = pn;
306fe75b452SAdrian Chadd 	}
3078a1b9b6aSSam Leffler 
3088a1b9b6aSSam Leffler 	return 1;
3098a1b9b6aSSam Leffler }
3108a1b9b6aSSam Leffler 
3118a1b9b6aSSam Leffler /*
3128a1b9b6aSSam Leffler  * Verify and strip MIC from the frame.
3138a1b9b6aSSam Leffler  */
3148a1b9b6aSSam Leffler static int
ccmp_demic(struct ieee80211_key * k,struct mbuf * m,int force)31596d88463SSam Leffler ccmp_demic(struct ieee80211_key *k, struct mbuf *m, int force)
3168a1b9b6aSSam Leffler {
3178a1b9b6aSSam Leffler 	return 1;
3188a1b9b6aSSam Leffler }
3198a1b9b6aSSam Leffler 
3208a1b9b6aSSam Leffler static __inline void
xor_block(uint8_t * b,const uint8_t * a,size_t len)3218a1b9b6aSSam Leffler xor_block(uint8_t *b, const uint8_t *a, size_t len)
3228a1b9b6aSSam Leffler {
3238a1b9b6aSSam Leffler 	int i;
3248a1b9b6aSSam Leffler 	for (i = 0; i < len; i++)
3258a1b9b6aSSam Leffler 		b[i] ^= a[i];
3268a1b9b6aSSam Leffler }
3278a1b9b6aSSam Leffler 
3288a1b9b6aSSam Leffler /*
3298a1b9b6aSSam Leffler  * Host AP crypt: host-based CCMP encryption implementation for Host AP driver
3308a1b9b6aSSam Leffler  *
3318a1b9b6aSSam Leffler  * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
3328a1b9b6aSSam Leffler  *
3338a1b9b6aSSam Leffler  * This program is free software; you can redistribute it and/or modify
3348a1b9b6aSSam Leffler  * it under the terms of the GNU General Public License version 2 as
3358a1b9b6aSSam Leffler  * published by the Free Software Foundation. See README and COPYING for
3368a1b9b6aSSam Leffler  * more details.
3378a1b9b6aSSam Leffler  *
3388a1b9b6aSSam Leffler  * Alternatively, this software may be distributed under the terms of BSD
3398a1b9b6aSSam Leffler  * license.
3408a1b9b6aSSam Leffler  */
3418a1b9b6aSSam Leffler 
3428a1b9b6aSSam Leffler static void
ccmp_init_blocks(rijndael_ctx * ctx,struct ieee80211_frame * wh,u_int64_t pn,size_t dlen,uint8_t b0[AES_BLOCK_LEN],uint8_t aad[2* AES_BLOCK_LEN],uint8_t auth[AES_BLOCK_LEN],uint8_t s0[AES_BLOCK_LEN])3438a1b9b6aSSam Leffler ccmp_init_blocks(rijndael_ctx *ctx, struct ieee80211_frame *wh,
3448a1b9b6aSSam Leffler 	u_int64_t pn, size_t dlen,
3458a1b9b6aSSam Leffler 	uint8_t b0[AES_BLOCK_LEN], uint8_t aad[2 * AES_BLOCK_LEN],
3468a1b9b6aSSam Leffler 	uint8_t auth[AES_BLOCK_LEN], uint8_t s0[AES_BLOCK_LEN])
3478a1b9b6aSSam Leffler {
3488a1b9b6aSSam Leffler #define	IS_QOS_DATA(wh)	IEEE80211_QOS_HAS_SEQ(wh)
3498a1b9b6aSSam Leffler 
3508a1b9b6aSSam Leffler 	/* CCM Initial Block:
3518a1b9b6aSSam Leffler 	 * Flag (Include authentication header, M=3 (8-octet MIC),
3528a1b9b6aSSam Leffler 	 *       L=1 (2-octet Dlen))
3538a1b9b6aSSam Leffler 	 * Nonce: 0x00 | A2 | PN
3548a1b9b6aSSam Leffler 	 * Dlen */
3558a1b9b6aSSam Leffler 	b0[0] = 0x59;
3568a1b9b6aSSam Leffler 	/* NB: b0[1] set below */
3578a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(b0 + 2, wh->i_addr2);
3588a1b9b6aSSam Leffler 	b0[8] = pn >> 40;
3598a1b9b6aSSam Leffler 	b0[9] = pn >> 32;
3608a1b9b6aSSam Leffler 	b0[10] = pn >> 24;
3618a1b9b6aSSam Leffler 	b0[11] = pn >> 16;
3628a1b9b6aSSam Leffler 	b0[12] = pn >> 8;
3638a1b9b6aSSam Leffler 	b0[13] = pn >> 0;
3648a1b9b6aSSam Leffler 	b0[14] = (dlen >> 8) & 0xff;
3658a1b9b6aSSam Leffler 	b0[15] = dlen & 0xff;
3668a1b9b6aSSam Leffler 
3678a1b9b6aSSam Leffler 	/* AAD:
3688a1b9b6aSSam Leffler 	 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
3698a1b9b6aSSam Leffler 	 * A1 | A2 | A3
3708a1b9b6aSSam Leffler 	 * SC with bits 4..15 (seq#) masked to zero
3718a1b9b6aSSam Leffler 	 * A4 (if present)
3728a1b9b6aSSam Leffler 	 * QC (if present)
3738a1b9b6aSSam Leffler 	 */
3748a1b9b6aSSam Leffler 	aad[0] = 0;	/* AAD length >> 8 */
3758a1b9b6aSSam Leffler 	/* NB: aad[1] set below */
3768a1b9b6aSSam Leffler 	aad[2] = wh->i_fc[0] & 0x8f;	/* XXX magic #s */
3778a1b9b6aSSam Leffler 	aad[3] = wh->i_fc[1] & 0xc7;	/* XXX magic #s */
3788a1b9b6aSSam Leffler 	/* NB: we know 3 addresses are contiguous */
3798a1b9b6aSSam Leffler 	memcpy(aad + 4, wh->i_addr1, 3 * IEEE80211_ADDR_LEN);
3808a1b9b6aSSam Leffler 	aad[22] = wh->i_seq[0] & IEEE80211_SEQ_FRAG_MASK;
3818a1b9b6aSSam Leffler 	aad[23] = 0; /* all bits masked */
3828a1b9b6aSSam Leffler 	/*
3838a1b9b6aSSam Leffler 	 * Construct variable-length portion of AAD based
3848a1b9b6aSSam Leffler 	 * on whether this is a 4-address frame/QOS frame.
3858a1b9b6aSSam Leffler 	 * We always zero-pad to 32 bytes before running it
3868a1b9b6aSSam Leffler 	 * through the cipher.
3878a1b9b6aSSam Leffler 	 *
3888a1b9b6aSSam Leffler 	 * We also fill in the priority bits of the CCM
3898a1b9b6aSSam Leffler 	 * initial block as we know whether or not we have
3908a1b9b6aSSam Leffler 	 * a QOS frame.
3918a1b9b6aSSam Leffler 	 */
39232bdd57bSSam Leffler 	if (IEEE80211_IS_DSTODS(wh)) {
3938a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(aad + 24,
3948a1b9b6aSSam Leffler 			((struct ieee80211_frame_addr4 *)wh)->i_addr4);
3958a1b9b6aSSam Leffler 		if (IS_QOS_DATA(wh)) {
3968a1b9b6aSSam Leffler 			struct ieee80211_qosframe_addr4 *qwh4 =
3978a1b9b6aSSam Leffler 				(struct ieee80211_qosframe_addr4 *) wh;
3988a1b9b6aSSam Leffler 			aad[30] = qwh4->i_qos[0] & 0x0f;/* just priority bits */
3998a1b9b6aSSam Leffler 			aad[31] = 0;
4008a1b9b6aSSam Leffler 			b0[1] = aad[30];
4018a1b9b6aSSam Leffler 			aad[1] = 22 + IEEE80211_ADDR_LEN + 2;
4028a1b9b6aSSam Leffler 		} else {
40368e8e04eSSam Leffler 			*(uint16_t *)&aad[30] = 0;
4048a1b9b6aSSam Leffler 			b0[1] = 0;
4058a1b9b6aSSam Leffler 			aad[1] = 22 + IEEE80211_ADDR_LEN;
4068a1b9b6aSSam Leffler 		}
4078a1b9b6aSSam Leffler 	} else {
4088a1b9b6aSSam Leffler 		if (IS_QOS_DATA(wh)) {
4098a1b9b6aSSam Leffler 			struct ieee80211_qosframe *qwh =
4108a1b9b6aSSam Leffler 				(struct ieee80211_qosframe*) wh;
4118a1b9b6aSSam Leffler 			aad[24] = qwh->i_qos[0] & 0x0f;	/* just priority bits */
4128a1b9b6aSSam Leffler 			aad[25] = 0;
4138a1b9b6aSSam Leffler 			b0[1] = aad[24];
4148a1b9b6aSSam Leffler 			aad[1] = 22 + 2;
4158a1b9b6aSSam Leffler 		} else {
41668e8e04eSSam Leffler 			*(uint16_t *)&aad[24] = 0;
4178a1b9b6aSSam Leffler 			b0[1] = 0;
4188a1b9b6aSSam Leffler 			aad[1] = 22;
4198a1b9b6aSSam Leffler 		}
42068e8e04eSSam Leffler 		*(uint16_t *)&aad[26] = 0;
42168e8e04eSSam Leffler 		*(uint32_t *)&aad[28] = 0;
4228a1b9b6aSSam Leffler 	}
4238a1b9b6aSSam Leffler 
4248a1b9b6aSSam Leffler 	/* Start with the first block and AAD */
4258a1b9b6aSSam Leffler 	rijndael_encrypt(ctx, b0, auth);
4268a1b9b6aSSam Leffler 	xor_block(auth, aad, AES_BLOCK_LEN);
4278a1b9b6aSSam Leffler 	rijndael_encrypt(ctx, auth, auth);
4288a1b9b6aSSam Leffler 	xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
4298a1b9b6aSSam Leffler 	rijndael_encrypt(ctx, auth, auth);
4308a1b9b6aSSam Leffler 	b0[0] &= 0x07;
4318a1b9b6aSSam Leffler 	b0[14] = b0[15] = 0;
4328a1b9b6aSSam Leffler 	rijndael_encrypt(ctx, b0, s0);
4338a1b9b6aSSam Leffler #undef	IS_QOS_DATA
4348a1b9b6aSSam Leffler }
4358a1b9b6aSSam Leffler 
4368a1b9b6aSSam Leffler #define	CCMP_ENCRYPT(_i, _b, _b0, _pos, _e, _len) do {	\
4378a1b9b6aSSam Leffler 	/* Authentication */				\
4388a1b9b6aSSam Leffler 	xor_block(_b, _pos, _len);			\
4398a1b9b6aSSam Leffler 	rijndael_encrypt(&ctx->cc_aes, _b, _b);		\
4408a1b9b6aSSam Leffler 	/* Encryption, with counter */			\
4418a1b9b6aSSam Leffler 	_b0[14] = (_i >> 8) & 0xff;			\
4428a1b9b6aSSam Leffler 	_b0[15] = _i & 0xff;				\
4438a1b9b6aSSam Leffler 	rijndael_encrypt(&ctx->cc_aes, _b0, _e);	\
4448a1b9b6aSSam Leffler 	xor_block(_pos, _e, _len);			\
4458a1b9b6aSSam Leffler } while (0)
4468a1b9b6aSSam Leffler 
4478a1b9b6aSSam Leffler static int
ccmp_encrypt(struct ieee80211_key * key,struct mbuf * m0,int hdrlen)4488a1b9b6aSSam Leffler ccmp_encrypt(struct ieee80211_key *key, struct mbuf *m0, int hdrlen)
4498a1b9b6aSSam Leffler {
4508a1b9b6aSSam Leffler 	struct ccmp_ctx *ctx = key->wk_private;
4518a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
4528a1b9b6aSSam Leffler 	struct mbuf *m = m0;
453dd423201SSam Leffler 	int data_len, i, space;
4548a1b9b6aSSam Leffler 	uint8_t aad[2 * AES_BLOCK_LEN], b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN],
4558a1b9b6aSSam Leffler 		e[AES_BLOCK_LEN], s0[AES_BLOCK_LEN];
4568a1b9b6aSSam Leffler 	uint8_t *pos;
4578a1b9b6aSSam Leffler 
458b032f27cSSam Leffler 	ctx->cc_vap->iv_stats.is_crypto_ccmp++;
4598a1b9b6aSSam Leffler 
4608a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
4618a1b9b6aSSam Leffler 	data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header);
4628a1b9b6aSSam Leffler 	ccmp_init_blocks(&ctx->cc_aes, wh, key->wk_keytsc,
4638a1b9b6aSSam Leffler 		data_len, b0, aad, b, s0);
4648a1b9b6aSSam Leffler 
4658a1b9b6aSSam Leffler 	i = 1;
4668a1b9b6aSSam Leffler 	pos = mtod(m, uint8_t *) + hdrlen + ccmp.ic_header;
4678a1b9b6aSSam Leffler 	/* NB: assumes header is entirely in first mbuf */
4688a1b9b6aSSam Leffler 	space = m->m_len - (hdrlen + ccmp.ic_header);
4698a1b9b6aSSam Leffler 	for (;;) {
4708a1b9b6aSSam Leffler 		if (space > data_len)
4718a1b9b6aSSam Leffler 			space = data_len;
4728a1b9b6aSSam Leffler 		/*
4738a1b9b6aSSam Leffler 		 * Do full blocks.
4748a1b9b6aSSam Leffler 		 */
4758a1b9b6aSSam Leffler 		while (space >= AES_BLOCK_LEN) {
4768a1b9b6aSSam Leffler 			CCMP_ENCRYPT(i, b, b0, pos, e, AES_BLOCK_LEN);
4778a1b9b6aSSam Leffler 			pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
4788a1b9b6aSSam Leffler 			data_len -= AES_BLOCK_LEN;
4798a1b9b6aSSam Leffler 			i++;
4808a1b9b6aSSam Leffler 		}
4818a1b9b6aSSam Leffler 		if (data_len <= 0)		/* no more data */
4828a1b9b6aSSam Leffler 			break;
4838a1b9b6aSSam Leffler 		m = m->m_next;
4848a1b9b6aSSam Leffler 		if (m == NULL) {		/* last buffer */
4858a1b9b6aSSam Leffler 			if (space != 0) {
4868a1b9b6aSSam Leffler 				/*
4878a1b9b6aSSam Leffler 				 * Short last block.
4888a1b9b6aSSam Leffler 				 */
4898a1b9b6aSSam Leffler 				CCMP_ENCRYPT(i, b, b0, pos, e, space);
4908a1b9b6aSSam Leffler 			}
4918a1b9b6aSSam Leffler 			break;
4928a1b9b6aSSam Leffler 		}
4938a1b9b6aSSam Leffler 		if (space != 0) {
4948a1b9b6aSSam Leffler 			uint8_t *pos_next;
495dd423201SSam Leffler 			int space_next;
496dd423201SSam Leffler 			int len, dl, sp;
497dd423201SSam Leffler 			struct mbuf *n;
4988a1b9b6aSSam Leffler 
4998a1b9b6aSSam Leffler 			/*
500dd423201SSam Leffler 			 * Block straddles one or more mbufs, gather data
501dd423201SSam Leffler 			 * into the block buffer b, apply the cipher, then
502dd423201SSam Leffler 			 * scatter the results back into the mbuf chain.
503dd423201SSam Leffler 			 * The buffer will automatically get space bytes
504dd423201SSam Leffler 			 * of data at offset 0 copied in+out by the
505dd423201SSam Leffler 			 * CCMP_ENCRYPT request so we must take care of
506dd423201SSam Leffler 			 * the remaining data.
5078a1b9b6aSSam Leffler 			 */
508dd423201SSam Leffler 			n = m;
509dd423201SSam Leffler 			dl = data_len;
510dd423201SSam Leffler 			sp = space;
511dd423201SSam Leffler 			for (;;) {
512dd423201SSam Leffler 				pos_next = mtod(n, uint8_t *);
513dd423201SSam Leffler 				len = min(dl, AES_BLOCK_LEN);
514dd423201SSam Leffler 				space_next = len > sp ? len - sp : 0;
515dd423201SSam Leffler 				if (n->m_len >= space_next) {
516dd423201SSam Leffler 					/*
517dd423201SSam Leffler 					 * This mbuf has enough data; just grab
518dd423201SSam Leffler 					 * what we need and stop.
519dd423201SSam Leffler 					 */
520dd423201SSam Leffler 					xor_block(b+sp, pos_next, space_next);
521dd423201SSam Leffler 					break;
522dd423201SSam Leffler 				}
523dd423201SSam Leffler 				/*
524dd423201SSam Leffler 				 * This mbuf's contents are insufficient,
525dd423201SSam Leffler 				 * take 'em all and prepare to advance to
526dd423201SSam Leffler 				 * the next mbuf.
527dd423201SSam Leffler 				 */
528dd423201SSam Leffler 				xor_block(b+sp, pos_next, n->m_len);
529dd423201SSam Leffler 				sp += n->m_len, dl -= n->m_len;
530dd423201SSam Leffler 				n = n->m_next;
531dd423201SSam Leffler 				if (n == NULL)
532dd423201SSam Leffler 					break;
533dd423201SSam Leffler 			}
5348a1b9b6aSSam Leffler 
5358a1b9b6aSSam Leffler 			CCMP_ENCRYPT(i, b, b0, pos, e, space);
5368a1b9b6aSSam Leffler 
537dd423201SSam Leffler 			/* NB: just like above, but scatter data to mbufs */
538dd423201SSam Leffler 			dl = data_len;
539dd423201SSam Leffler 			sp = space;
540dd423201SSam Leffler 			for (;;) {
541dd423201SSam Leffler 				pos_next = mtod(m, uint8_t *);
542dd423201SSam Leffler 				len = min(dl, AES_BLOCK_LEN);
543dd423201SSam Leffler 				space_next = len > sp ? len - sp : 0;
544dd423201SSam Leffler 				if (m->m_len >= space_next) {
545dd423201SSam Leffler 					xor_block(pos_next, e+sp, space_next);
546dd423201SSam Leffler 					break;
547dd423201SSam Leffler 				}
548dd423201SSam Leffler 				xor_block(pos_next, e+sp, m->m_len);
549dd423201SSam Leffler 				sp += m->m_len, dl -= m->m_len;
550dd423201SSam Leffler 				m = m->m_next;
551dd423201SSam Leffler 				if (m == NULL)
552dd423201SSam Leffler 					goto done;
553dd423201SSam Leffler 			}
554dd423201SSam Leffler 			/*
555dd423201SSam Leffler 			 * Do bookkeeping.  m now points to the last mbuf
556dd423201SSam Leffler 			 * we grabbed data from.  We know we consumed a
557dd423201SSam Leffler 			 * full block of data as otherwise we'd have hit
558dd423201SSam Leffler 			 * the end of the mbuf chain, so deduct from data_len.
559dd423201SSam Leffler 			 * Otherwise advance the block number (i) and setup
560dd423201SSam Leffler 			 * pos+space to reflect contents of the new mbuf.
561dd423201SSam Leffler 			 */
562dd423201SSam Leffler 			data_len -= AES_BLOCK_LEN;
563dd423201SSam Leffler 			i++;
5648a1b9b6aSSam Leffler 			pos = pos_next + space_next;
5658a1b9b6aSSam Leffler 			space = m->m_len - space_next;
5668a1b9b6aSSam Leffler 		} else {
5678a1b9b6aSSam Leffler 			/*
5688a1b9b6aSSam Leffler 			 * Setup for next buffer.
5698a1b9b6aSSam Leffler 			 */
5708a1b9b6aSSam Leffler 			pos = mtod(m, uint8_t *);
5718a1b9b6aSSam Leffler 			space = m->m_len;
5728a1b9b6aSSam Leffler 		}
5738a1b9b6aSSam Leffler 	}
574dd423201SSam Leffler done:
5758a1b9b6aSSam Leffler 	/* tack on MIC */
5768a1b9b6aSSam Leffler 	xor_block(b, s0, ccmp.ic_trailer);
5778a1b9b6aSSam Leffler 	return m_append(m0, ccmp.ic_trailer, b);
5788a1b9b6aSSam Leffler }
5798a1b9b6aSSam Leffler #undef CCMP_ENCRYPT
5808a1b9b6aSSam Leffler 
5818a1b9b6aSSam Leffler #define	CCMP_DECRYPT(_i, _b, _b0, _pos, _a, _len) do {	\
5828a1b9b6aSSam Leffler 	/* Decrypt, with counter */			\
5838a1b9b6aSSam Leffler 	_b0[14] = (_i >> 8) & 0xff;			\
5848a1b9b6aSSam Leffler 	_b0[15] = _i & 0xff;				\
5858a1b9b6aSSam Leffler 	rijndael_encrypt(&ctx->cc_aes, _b0, _b);	\
5868a1b9b6aSSam Leffler 	xor_block(_pos, _b, _len);			\
5878a1b9b6aSSam Leffler 	/* Authentication */				\
5888a1b9b6aSSam Leffler 	xor_block(_a, _pos, _len);			\
5898a1b9b6aSSam Leffler 	rijndael_encrypt(&ctx->cc_aes, _a, _a);		\
5908a1b9b6aSSam Leffler } while (0)
5918a1b9b6aSSam Leffler 
5928a1b9b6aSSam Leffler static int
ccmp_decrypt(struct ieee80211_key * key,u_int64_t pn,struct mbuf * m,int hdrlen)5938a1b9b6aSSam Leffler ccmp_decrypt(struct ieee80211_key *key, u_int64_t pn, struct mbuf *m, int hdrlen)
5948a1b9b6aSSam Leffler {
5958a1b9b6aSSam Leffler 	struct ccmp_ctx *ctx = key->wk_private;
596b032f27cSSam Leffler 	struct ieee80211vap *vap = ctx->cc_vap;
5978a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
5988a1b9b6aSSam Leffler 	uint8_t aad[2 * AES_BLOCK_LEN];
5998a1b9b6aSSam Leffler 	uint8_t b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN], a[AES_BLOCK_LEN];
6008a1b9b6aSSam Leffler 	uint8_t mic[AES_BLOCK_LEN];
6018a1b9b6aSSam Leffler 	size_t data_len;
6028a1b9b6aSSam Leffler 	int i;
6038a1b9b6aSSam Leffler 	uint8_t *pos;
6048a1b9b6aSSam Leffler 	u_int space;
6058a1b9b6aSSam Leffler 
606b032f27cSSam Leffler 	ctx->cc_vap->iv_stats.is_crypto_ccmp++;
6078a1b9b6aSSam Leffler 
6088a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
6098a1b9b6aSSam Leffler 	data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header + ccmp.ic_trailer);
6108a1b9b6aSSam Leffler 	ccmp_init_blocks(&ctx->cc_aes, wh, pn, data_len, b0, aad, a, b);
6118a1b9b6aSSam Leffler 	m_copydata(m, m->m_pkthdr.len - ccmp.ic_trailer, ccmp.ic_trailer, mic);
6128a1b9b6aSSam Leffler 	xor_block(mic, b, ccmp.ic_trailer);
6138a1b9b6aSSam Leffler 
6148a1b9b6aSSam Leffler 	i = 1;
6158a1b9b6aSSam Leffler 	pos = mtod(m, uint8_t *) + hdrlen + ccmp.ic_header;
6168a1b9b6aSSam Leffler 	space = m->m_len - (hdrlen + ccmp.ic_header);
6178a1b9b6aSSam Leffler 	for (;;) {
6188a1b9b6aSSam Leffler 		if (space > data_len)
6198a1b9b6aSSam Leffler 			space = data_len;
6208a1b9b6aSSam Leffler 		while (space >= AES_BLOCK_LEN) {
6218a1b9b6aSSam Leffler 			CCMP_DECRYPT(i, b, b0, pos, a, AES_BLOCK_LEN);
6228a1b9b6aSSam Leffler 			pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
6238a1b9b6aSSam Leffler 			data_len -= AES_BLOCK_LEN;
6248a1b9b6aSSam Leffler 			i++;
6258a1b9b6aSSam Leffler 		}
6268a1b9b6aSSam Leffler 		if (data_len <= 0)		/* no more data */
6278a1b9b6aSSam Leffler 			break;
6288a1b9b6aSSam Leffler 		m = m->m_next;
6298a1b9b6aSSam Leffler 		if (m == NULL) {		/* last buffer */
6308a1b9b6aSSam Leffler 			if (space != 0)		/* short last block */
6318a1b9b6aSSam Leffler 				CCMP_DECRYPT(i, b, b0, pos, a, space);
6328a1b9b6aSSam Leffler 			break;
6338a1b9b6aSSam Leffler 		}
6348a1b9b6aSSam Leffler 		if (space != 0) {
6358a1b9b6aSSam Leffler 			uint8_t *pos_next;
6368a1b9b6aSSam Leffler 			u_int space_next;
6378a1b9b6aSSam Leffler 			u_int len;
6388a1b9b6aSSam Leffler 
6398a1b9b6aSSam Leffler 			/*
6408a1b9b6aSSam Leffler 			 * Block straddles buffers, split references.  We
641dd423201SSam Leffler 			 * do not handle splits that require >2 buffers
642dd423201SSam Leffler 			 * since rx'd frames are never badly fragmented
643dd423201SSam Leffler 			 * because drivers typically recv in clusters.
6448a1b9b6aSSam Leffler 			 */
6458a1b9b6aSSam Leffler 			pos_next = mtod(m, uint8_t *);
6468a1b9b6aSSam Leffler 			len = min(data_len, AES_BLOCK_LEN);
6478a1b9b6aSSam Leffler 			space_next = len > space ? len - space : 0;
6488a1b9b6aSSam Leffler 			KASSERT(m->m_len >= space_next,
6498a1b9b6aSSam Leffler 				("not enough data in following buffer, "
6508a1b9b6aSSam Leffler 				"m_len %u need %u\n", m->m_len, space_next));
6518a1b9b6aSSam Leffler 
6528a1b9b6aSSam Leffler 			xor_block(b+space, pos_next, space_next);
6538a1b9b6aSSam Leffler 			CCMP_DECRYPT(i, b, b0, pos, a, space);
6548a1b9b6aSSam Leffler 			xor_block(pos_next, b+space, space_next);
6558a1b9b6aSSam Leffler 			data_len -= len;
6568a1b9b6aSSam Leffler 			i++;
6578a1b9b6aSSam Leffler 
6588a1b9b6aSSam Leffler 			pos = pos_next + space_next;
6598a1b9b6aSSam Leffler 			space = m->m_len - space_next;
6608a1b9b6aSSam Leffler 		} else {
6618a1b9b6aSSam Leffler 			/*
6628a1b9b6aSSam Leffler 			 * Setup for next buffer.
6638a1b9b6aSSam Leffler 			 */
6648a1b9b6aSSam Leffler 			pos = mtod(m, uint8_t *);
6658a1b9b6aSSam Leffler 			space = m->m_len;
6668a1b9b6aSSam Leffler 		}
6678a1b9b6aSSam Leffler 	}
6688a1b9b6aSSam Leffler 	if (memcmp(mic, a, ccmp.ic_trailer) != 0) {
669b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
670b032f27cSSam Leffler 		    "%s", "AES-CCM decrypt failed; MIC mismatch");
671b032f27cSSam Leffler 		vap->iv_stats.is_rx_ccmpmic++;
6728a1b9b6aSSam Leffler 		return 0;
6738a1b9b6aSSam Leffler 	}
6748a1b9b6aSSam Leffler 	return 1;
6758a1b9b6aSSam Leffler }
6768a1b9b6aSSam Leffler #undef CCMP_DECRYPT
6778a1b9b6aSSam Leffler 
6788a1b9b6aSSam Leffler /*
6798a1b9b6aSSam Leffler  * Module glue.
6808a1b9b6aSSam Leffler  */
68168e8e04eSSam Leffler IEEE80211_CRYPTO_MODULE(ccmp, 1);
682