Home | History | Annotate | Line # | Download | only in net80211
ieee80211_crypto_ccmp.c revision 1.17
      1  1.17  riastrad /*	$NetBSD: ieee80211_crypto_ccmp.c,v 1.17 2020/07/25 22:27:05 riastradh Exp $	*/
      2  1.13      maxv 
      3  1.13      maxv /*
      4   1.1    dyoung  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
      5   1.1    dyoung  * All rights reserved.
      6   1.1    dyoung  *
      7   1.1    dyoung  * Redistribution and use in source and binary forms, with or without
      8   1.1    dyoung  * modification, are permitted provided that the following conditions
      9   1.1    dyoung  * are met:
     10   1.1    dyoung  * 1. Redistributions of source code must retain the above copyright
     11   1.1    dyoung  *    notice, this list of conditions and the following disclaimer.
     12   1.1    dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    dyoung  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    dyoung  *    documentation and/or other materials provided with the distribution.
     15   1.1    dyoung  * 3. The name of the author may not be used to endorse or promote products
     16   1.1    dyoung  *    derived from this software without specific prior written permission.
     17   1.1    dyoung  *
     18   1.1    dyoung  * Alternatively, this software may be distributed under the terms of the
     19   1.1    dyoung  * GNU General Public License ("GPL") version 2 as published by the Free
     20   1.1    dyoung  * Software Foundation.
     21   1.1    dyoung  *
     22   1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1    dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1    dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1    dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1    dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1    dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1    dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1    dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1    dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1    dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1    dyoung  */
     33   1.1    dyoung 
     34   1.1    dyoung #include <sys/cdefs.h>
     35   1.2    dyoung #ifdef __FreeBSD__
     36   1.3    dyoung __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_crypto_ccmp.c,v 1.7 2005/07/11 03:06:23 sam Exp $");
     37   1.2    dyoung #endif
     38   1.2    dyoung #ifdef __NetBSD__
     39  1.17  riastrad __KERNEL_RCSID(0, "$NetBSD: ieee80211_crypto_ccmp.c,v 1.17 2020/07/25 22:27:05 riastradh Exp $");
     40   1.2    dyoung #endif
     41   1.1    dyoung 
     42   1.1    dyoung /*
     43   1.1    dyoung  * IEEE 802.11i AES-CCMP crypto support.
     44   1.1    dyoung  *
     45   1.1    dyoung  * Part of this module is derived from similar code in the Host
     46   1.1    dyoung  * AP driver. The code is used with the consent of the author and
     47  1.11       snj  * its license is included below.
     48   1.1    dyoung  */
     49   1.1    dyoung #include <sys/param.h>
     50  1.17  riastrad #include <sys/kernel.h>
     51  1.17  riastrad #include <sys/kmem.h>
     52  1.17  riastrad #include <sys/mbuf.h>
     53   1.9  christos #include <sys/systm.h>
     54   1.1    dyoung 
     55   1.1    dyoung #include <sys/socket.h>
     56   1.1    dyoung 
     57   1.1    dyoung #include <net/if.h>
     58   1.5  christos #include <net/if_ether.h>
     59   1.1    dyoung #include <net/if_media.h>
     60   1.1    dyoung 
     61   1.1    dyoung #include <net80211/ieee80211_var.h>
     62   1.1    dyoung 
     63  1.16  riastrad #include <crypto/aes/aes.h>
     64  1.16  riastrad #include <crypto/aes/aes_ccm.h>
     65  1.16  riastrad #include <crypto/aes/aes_ccm_mbuf.h>
     66   1.1    dyoung 
     67   1.1    dyoung #define AES_BLOCK_LEN 16
     68   1.1    dyoung 
     69   1.1    dyoung struct ccmp_ctx {
     70  1.17  riastrad 	struct aesenc cc_aes;
     71   1.1    dyoung 	struct ieee80211com *cc_ic;	/* for diagnostics */
     72   1.1    dyoung };
     73   1.1    dyoung 
     74   1.1    dyoung static	void *ccmp_attach(struct ieee80211com *, struct ieee80211_key *);
     75   1.1    dyoung static	void ccmp_detach(struct ieee80211_key *);
     76   1.1    dyoung static	int ccmp_setkey(struct ieee80211_key *);
     77   1.1    dyoung static	int ccmp_encap(struct ieee80211_key *k, struct mbuf *, u_int8_t keyid);
     78   1.3    dyoung static	int ccmp_decap(struct ieee80211_key *, struct mbuf *, int);
     79   1.3    dyoung static	int ccmp_enmic(struct ieee80211_key *, struct mbuf *, int);
     80   1.3    dyoung static	int ccmp_demic(struct ieee80211_key *, struct mbuf *, int);
     81   1.1    dyoung 
     82   1.2    dyoung const struct ieee80211_cipher ieee80211_cipher_ccmp = {
     83   1.1    dyoung 	.ic_name	= "AES-CCM",
     84   1.1    dyoung 	.ic_cipher	= IEEE80211_CIPHER_AES_CCM,
     85   1.1    dyoung 	.ic_header	= IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
     86   1.1    dyoung 			  IEEE80211_WEP_EXTIVLEN,
     87   1.1    dyoung 	.ic_trailer	= IEEE80211_WEP_MICLEN,
     88   1.1    dyoung 	.ic_miclen	= 0,
     89   1.1    dyoung 	.ic_attach	= ccmp_attach,
     90   1.1    dyoung 	.ic_detach	= ccmp_detach,
     91   1.1    dyoung 	.ic_setkey	= ccmp_setkey,
     92   1.1    dyoung 	.ic_encap	= ccmp_encap,
     93   1.1    dyoung 	.ic_decap	= ccmp_decap,
     94   1.1    dyoung 	.ic_enmic	= ccmp_enmic,
     95   1.1    dyoung 	.ic_demic	= ccmp_demic,
     96   1.1    dyoung };
     97   1.1    dyoung 
     98   1.2    dyoung #define	ccmp	ieee80211_cipher_ccmp
     99   1.2    dyoung 
    100   1.1    dyoung static	int ccmp_encrypt(struct ieee80211_key *, struct mbuf *, int hdrlen);
    101   1.1    dyoung static	int ccmp_decrypt(struct ieee80211_key *, u_int64_t pn,
    102   1.1    dyoung 		struct mbuf *, int hdrlen);
    103   1.1    dyoung 
    104   1.1    dyoung static void *
    105   1.7  christos ccmp_attach(struct ieee80211com *ic, struct ieee80211_key *k)
    106   1.1    dyoung {
    107   1.1    dyoung 	struct ccmp_ctx *ctx;
    108   1.1    dyoung 
    109  1.17  riastrad 	ctx = kmem_zalloc(sizeof(*ctx), KM_NOSLEEP);
    110   1.1    dyoung 	if (ctx == NULL) {
    111   1.1    dyoung 		ic->ic_stats.is_crypto_nomem++;
    112   1.1    dyoung 		return NULL;
    113   1.1    dyoung 	}
    114   1.1    dyoung 	ctx->cc_ic = ic;
    115   1.1    dyoung 	return ctx;
    116   1.1    dyoung }
    117   1.1    dyoung 
    118   1.1    dyoung static void
    119   1.1    dyoung ccmp_detach(struct ieee80211_key *k)
    120   1.1    dyoung {
    121   1.1    dyoung 	struct ccmp_ctx *ctx = k->wk_private;
    122   1.1    dyoung 
    123  1.17  riastrad 	kmem_free(ctx, sizeof(*ctx));
    124   1.1    dyoung }
    125   1.1    dyoung 
    126   1.1    dyoung static int
    127   1.1    dyoung ccmp_setkey(struct ieee80211_key *k)
    128   1.1    dyoung {
    129   1.1    dyoung 	struct ccmp_ctx *ctx = k->wk_private;
    130   1.1    dyoung 
    131   1.1    dyoung 	if (k->wk_keylen != (128/NBBY)) {
    132   1.1    dyoung 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
    133   1.1    dyoung 			"%s: Invalid key length %u, expecting %u\n",
    134   1.1    dyoung 			__func__, k->wk_keylen, 128/NBBY);
    135   1.1    dyoung 		return 0;
    136   1.1    dyoung 	}
    137   1.1    dyoung 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT)
    138  1.16  riastrad 		aes_setenckey128(&ctx->cc_aes, k->wk_key);
    139   1.1    dyoung 	return 1;
    140   1.1    dyoung }
    141   1.1    dyoung 
    142   1.1    dyoung /*
    143   1.1    dyoung  * Add privacy headers appropriate for the specified key.
    144   1.1    dyoung  */
    145   1.1    dyoung static int
    146   1.1    dyoung ccmp_encap(struct ieee80211_key *k, struct mbuf *m, u_int8_t keyid)
    147   1.1    dyoung {
    148   1.1    dyoung 	struct ccmp_ctx *ctx = k->wk_private;
    149   1.1    dyoung 	struct ieee80211com *ic = ctx->cc_ic;
    150   1.1    dyoung 	u_int8_t *ivp;
    151   1.1    dyoung 	int hdrlen;
    152   1.1    dyoung 
    153   1.1    dyoung 	hdrlen = ieee80211_hdrspace(ic, mtod(m, void *));
    154  1.12      maxv 	ivp = mtod(m, u_int8_t *) + hdrlen;
    155   1.1    dyoung 
    156   1.1    dyoung 	k->wk_keytsc++;		/* XXX wrap at 48 bits */
    157   1.1    dyoung 	ivp[0] = k->wk_keytsc >> 0;		/* PN0 */
    158   1.1    dyoung 	ivp[1] = k->wk_keytsc >> 8;		/* PN1 */
    159   1.1    dyoung 	ivp[2] = 0;				/* Reserved */
    160   1.1    dyoung 	ivp[3] = keyid | IEEE80211_WEP_EXTIV;	/* KeyID | ExtID */
    161   1.1    dyoung 	ivp[4] = k->wk_keytsc >> 16;		/* PN2 */
    162   1.1    dyoung 	ivp[5] = k->wk_keytsc >> 24;		/* PN3 */
    163   1.1    dyoung 	ivp[6] = k->wk_keytsc >> 32;		/* PN4 */
    164   1.1    dyoung 	ivp[7] = k->wk_keytsc >> 40;		/* PN5 */
    165   1.1    dyoung 
    166   1.1    dyoung 	/*
    167   1.1    dyoung 	 * Finally, do software encrypt if neeed.
    168   1.1    dyoung 	 */
    169   1.1    dyoung 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
    170   1.1    dyoung 	    !ccmp_encrypt(k, m, hdrlen))
    171   1.1    dyoung 		return 0;
    172   1.1    dyoung 
    173   1.1    dyoung 	return 1;
    174   1.1    dyoung }
    175   1.1    dyoung 
    176   1.1    dyoung /*
    177   1.1    dyoung  * Add MIC to the frame as needed.
    178   1.1    dyoung  */
    179   1.1    dyoung static int
    180   1.7  christos ccmp_enmic(struct ieee80211_key *k, struct mbuf *m,
    181   1.7  christos     int force)
    182   1.1    dyoung {
    183   1.1    dyoung 
    184   1.1    dyoung 	return 1;
    185   1.1    dyoung }
    186   1.1    dyoung 
    187   1.1    dyoung static __inline uint64_t
    188   1.1    dyoung READ_6(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5)
    189   1.1    dyoung {
    190   1.1    dyoung 	uint32_t iv32 = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
    191   1.1    dyoung 	uint16_t iv16 = (b4 << 0) | (b5 << 8);
    192   1.1    dyoung 	return (((uint64_t)iv16) << 32) | iv32;
    193   1.1    dyoung }
    194   1.1    dyoung 
    195   1.1    dyoung /*
    196   1.1    dyoung  * Validate and strip privacy headers (and trailer) for a
    197   1.1    dyoung  * received frame. The specified key should be correct but
    198   1.1    dyoung  * is also verified.
    199   1.1    dyoung  */
    200   1.1    dyoung static int
    201   1.3    dyoung ccmp_decap(struct ieee80211_key *k, struct mbuf *m, int hdrlen)
    202   1.1    dyoung {
    203   1.1    dyoung 	struct ccmp_ctx *ctx = k->wk_private;
    204   1.1    dyoung 	struct ieee80211_frame *wh;
    205   1.1    dyoung 	uint8_t *ivp;
    206   1.1    dyoung 	uint64_t pn;
    207   1.1    dyoung 
    208   1.1    dyoung 	/*
    209   1.1    dyoung 	 * Header should have extended IV and sequence number;
    210   1.1    dyoung 	 * verify the former and validate the latter.
    211   1.1    dyoung 	 */
    212   1.1    dyoung 	wh = mtod(m, struct ieee80211_frame *);
    213   1.1    dyoung 	ivp = mtod(m, uint8_t *) + hdrlen;
    214   1.1    dyoung 	if ((ivp[IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV) == 0) {
    215   1.1    dyoung 		/*
    216   1.1    dyoung 		 * No extended IV; discard frame.
    217   1.1    dyoung 		 */
    218   1.1    dyoung 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
    219   1.1    dyoung 			"[%s] Missing ExtIV for AES-CCM cipher\n",
    220   1.1    dyoung 			ether_sprintf(wh->i_addr2));
    221   1.1    dyoung 		ctx->cc_ic->ic_stats.is_rx_ccmpformat++;
    222   1.1    dyoung 		return 0;
    223   1.1    dyoung 	}
    224   1.1    dyoung 	pn = READ_6(ivp[0], ivp[1], ivp[4], ivp[5], ivp[6], ivp[7]);
    225   1.1    dyoung 	if (pn <= k->wk_keyrsc) {
    226   1.1    dyoung 		/*
    227   1.1    dyoung 		 * Replay violation.
    228   1.1    dyoung 		 */
    229   1.1    dyoung 		ieee80211_notify_replay_failure(ctx->cc_ic, wh, k, pn);
    230   1.1    dyoung 		ctx->cc_ic->ic_stats.is_rx_ccmpreplay++;
    231   1.1    dyoung 		return 0;
    232   1.1    dyoung 	}
    233   1.1    dyoung 
    234   1.1    dyoung 	/*
    235   1.1    dyoung 	 * Check if the device handled the decrypt in hardware.
    236   1.1    dyoung 	 * If so we just strip the header; otherwise we need to
    237   1.1    dyoung 	 * handle the decrypt in software.  Note that for the
    238   1.1    dyoung 	 * latter we leave the header in place for use in the
    239   1.1    dyoung 	 * decryption work.
    240   1.1    dyoung 	 */
    241   1.1    dyoung 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
    242   1.1    dyoung 	    !ccmp_decrypt(k, pn, m, hdrlen))
    243   1.1    dyoung 		return 0;
    244   1.1    dyoung 
    245   1.1    dyoung 	/*
    246   1.1    dyoung 	 * Copy up 802.11 header and strip crypto bits.
    247   1.1    dyoung 	 */
    248  1.14      maxv 	memmove(mtod(m, u_int8_t *) + ccmp.ic_header, mtod(m, void *), hdrlen);
    249   1.1    dyoung 	m_adj(m, ccmp.ic_header);
    250   1.1    dyoung 	m_adj(m, -ccmp.ic_trailer);
    251   1.1    dyoung 
    252   1.1    dyoung 	/*
    253   1.1    dyoung 	 * Ok to update rsc now.
    254   1.1    dyoung 	 */
    255   1.1    dyoung 	k->wk_keyrsc = pn;
    256   1.1    dyoung 
    257   1.1    dyoung 	return 1;
    258   1.1    dyoung }
    259   1.1    dyoung 
    260   1.1    dyoung /*
    261   1.1    dyoung  * Verify and strip MIC from the frame.
    262   1.1    dyoung  */
    263   1.1    dyoung static int
    264  1.13      maxv ccmp_demic(struct ieee80211_key *k, struct mbuf *m, int force)
    265   1.1    dyoung {
    266   1.1    dyoung 	return 1;
    267   1.1    dyoung }
    268   1.1    dyoung 
    269   1.1    dyoung static __inline void
    270   1.1    dyoung xor_block(uint8_t *b, const uint8_t *a, size_t len)
    271   1.1    dyoung {
    272   1.1    dyoung 	int i;
    273   1.1    dyoung 	for (i = 0; i < len; i++)
    274   1.1    dyoung 		b[i] ^= a[i];
    275   1.1    dyoung }
    276   1.1    dyoung 
    277   1.1    dyoung /*
    278   1.1    dyoung  * Host AP crypt: host-based CCMP encryption implementation for Host AP driver
    279   1.1    dyoung  *
    280   1.1    dyoung  * Copyright (c) 2003-2004, Jouni Malinen <jkmaline (at) cc.hut.fi>
    281   1.1    dyoung  *
    282   1.1    dyoung  * This program is free software; you can redistribute it and/or modify
    283   1.1    dyoung  * it under the terms of the GNU General Public License version 2 as
    284   1.1    dyoung  * published by the Free Software Foundation. See README and COPYING for
    285   1.1    dyoung  * more details.
    286   1.1    dyoung  *
    287   1.1    dyoung  * Alternatively, this software may be distributed under the terms of BSD
    288   1.1    dyoung  * license.
    289   1.1    dyoung  */
    290   1.1    dyoung 
    291   1.1    dyoung static void
    292  1.16  riastrad ccmp_init_blocks(struct aesenc *ctx, struct ieee80211_frame *wh,
    293  1.16  riastrad     u_int64_t pn, size_t data_len, struct aes_ccm *aes_ccm)
    294   1.1    dyoung {
    295  1.16  riastrad 	uint8_t nonce[13];
    296  1.16  riastrad 	uint8_t ad[32];
    297  1.16  riastrad 	uint8_t qos;
    298  1.16  riastrad 	size_t adlen;
    299  1.16  riastrad 
    300   1.1    dyoung #define	IS_4ADDRESS(wh) \
    301   1.1    dyoung 	((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
    302  1.10  christos #define	IS_QOS_DATA(wh)	ieee80211_has_qos(wh)
    303   1.1    dyoung 
    304  1.16  riastrad 	/* nonce[0] is qos, determined later */
    305  1.16  riastrad 	IEEE80211_ADDR_COPY(nonce + 1, wh->i_addr2);
    306  1.16  riastrad 	nonce[7] = pn >> 40;
    307  1.16  riastrad 	nonce[8] = pn >> 32;
    308  1.16  riastrad 	nonce[9] = pn >> 24;
    309  1.16  riastrad 	nonce[10] = pn >> 16;
    310  1.16  riastrad 	nonce[11] = pn >> 8;
    311  1.16  riastrad 	nonce[12] = pn >> 0;
    312  1.16  riastrad 
    313  1.16  riastrad 	ad[0] = wh->i_fc[0] & 0x8f;	/* XXX magic #s */
    314  1.16  riastrad 	ad[1] = wh->i_fc[1] & 0xc7;	/* XXX magic #s */
    315   1.1    dyoung 	/* NB: we know 3 addresses are contiguous */
    316  1.16  riastrad 	memcpy(ad + 2, wh->i_addr1, 3 * IEEE80211_ADDR_LEN);
    317  1.16  riastrad 	ad[20] = wh->i_seq[0] & IEEE80211_SEQ_FRAG_MASK;
    318  1.16  riastrad 	ad[21] = 0; /* all bits masked */
    319  1.16  riastrad 
    320   1.1    dyoung 	/*
    321   1.1    dyoung 	 * Construct variable-length portion of AAD based
    322   1.1    dyoung 	 * on whether this is a 4-address frame/QOS frame.
    323   1.1    dyoung 	 *
    324   1.1    dyoung 	 * We also fill in the priority bits of the CCM
    325   1.1    dyoung 	 * initial block as we know whether or not we have
    326   1.1    dyoung 	 * a QOS frame.
    327   1.1    dyoung 	 */
    328   1.1    dyoung 	if (IS_4ADDRESS(wh)) {
    329  1.16  riastrad 		IEEE80211_ADDR_COPY(ad + 22,
    330  1.16  riastrad 		    ((const struct ieee80211_frame_addr4 *)wh)->i_addr4);
    331   1.1    dyoung 		if (IS_QOS_DATA(wh)) {
    332  1.16  riastrad 			const struct ieee80211_qosframe_addr4 *qwh4 =
    333  1.16  riastrad 			    (const struct ieee80211_qosframe_addr4 *)wh;
    334  1.16  riastrad 			qos = qwh4->i_qos[0] & 0x0f; /* just priority bits */
    335  1.16  riastrad 			ad[28] = qos;
    336  1.16  riastrad 			ad[29] = 0;
    337  1.16  riastrad 			adlen = 22 + IEEE80211_ADDR_LEN + 2;
    338   1.1    dyoung 		} else {
    339  1.16  riastrad 			qos = 0;
    340  1.16  riastrad 			adlen = 22 + IEEE80211_ADDR_LEN;
    341   1.1    dyoung 		}
    342   1.1    dyoung 	} else {
    343   1.1    dyoung 		if (IS_QOS_DATA(wh)) {
    344  1.16  riastrad 			const struct ieee80211_qosframe *qwh =
    345  1.16  riastrad 			    (const struct ieee80211_qosframe *)wh;
    346  1.16  riastrad 			qos = qwh->i_qos[0] & 0x0f; /* just priority bits */
    347  1.16  riastrad 			ad[22] = qos;
    348  1.16  riastrad 			ad[23] = 0;
    349  1.16  riastrad 			adlen = 22 + 2;
    350   1.1    dyoung 		} else {
    351  1.16  riastrad 			qos = 0;
    352  1.16  riastrad 			adlen = 22;
    353   1.1    dyoung 		}
    354   1.1    dyoung 	}
    355  1.16  riastrad 	nonce[0] = qos;
    356  1.16  riastrad 
    357  1.16  riastrad 	aes_ccm_init(aes_ccm, AES_128_NROUNDS, ctx, 2 /* L, counter octets */,
    358  1.16  riastrad 	    IEEE80211_WEP_MICLEN, nonce, sizeof nonce, ad, adlen, data_len);
    359   1.1    dyoung 
    360   1.1    dyoung #undef	IS_QOS_DATA
    361   1.1    dyoung #undef	IS_4ADDRESS
    362   1.1    dyoung }
    363   1.1    dyoung 
    364   1.1    dyoung static int
    365  1.16  riastrad ccmp_encrypt(struct ieee80211_key *key, struct mbuf *m, int hdrlen)
    366   1.1    dyoung {
    367   1.1    dyoung 	struct ccmp_ctx *ctx = key->wk_private;
    368   1.1    dyoung 	struct ieee80211_frame *wh;
    369  1.16  riastrad 	struct aes_ccm aes_ccm;
    370  1.16  riastrad 	size_t data_len;
    371  1.16  riastrad 	uint8_t mic[IEEE80211_WEP_MICLEN];
    372  1.16  riastrad 
    373  1.16  riastrad 	KASSERT(hdrlen >= 0);
    374  1.16  riastrad 	KASSERT(hdrlen < m->m_pkthdr.len);
    375  1.16  riastrad 	KASSERT(ccmp.ic_header <= m->m_pkthdr.len - hdrlen);
    376   1.1    dyoung 
    377   1.1    dyoung 	ctx->cc_ic->ic_stats.is_crypto_ccmp++;
    378   1.1    dyoung 
    379   1.1    dyoung 	wh = mtod(m, struct ieee80211_frame *);
    380   1.1    dyoung 	data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header);
    381  1.16  riastrad 	ccmp_init_blocks(&ctx->cc_aes, wh, key->wk_keytsc, data_len, &aes_ccm);
    382  1.16  riastrad 	aes_ccm_enc_mbuf(&aes_ccm, m, hdrlen + ccmp.ic_header, data_len, mic);
    383   1.1    dyoung 
    384  1.16  riastrad 	return m_append(m, ccmp.ic_trailer, mic);
    385  1.16  riastrad }
    386   1.1    dyoung 
    387   1.1    dyoung static int
    388  1.13      maxv ccmp_decrypt(struct ieee80211_key *key, u_int64_t pn, struct mbuf *m,
    389  1.13      maxv     int hdrlen)
    390   1.1    dyoung {
    391   1.1    dyoung 	struct ccmp_ctx *ctx = key->wk_private;
    392   1.1    dyoung 	struct ieee80211_frame *wh;
    393  1.16  riastrad 	struct aes_ccm aes_ccm;
    394   1.1    dyoung 	size_t data_len;
    395  1.16  riastrad 	uint8_t mic[IEEE80211_WEP_MICLEN];
    396  1.16  riastrad 
    397  1.16  riastrad 	KASSERT(hdrlen >= 0);
    398  1.16  riastrad 	KASSERT(hdrlen < m->m_pkthdr.len);
    399  1.16  riastrad 	KASSERT(ccmp.ic_header < m->m_pkthdr.len - hdrlen);
    400  1.16  riastrad 	KASSERT(ccmp.ic_trailer < m->m_pkthdr.len - (hdrlen + ccmp.ic_header));
    401   1.1    dyoung 
    402   1.1    dyoung 	ctx->cc_ic->ic_stats.is_crypto_ccmp++;
    403   1.1    dyoung 
    404   1.1    dyoung 	wh = mtod(m, struct ieee80211_frame *);
    405   1.1    dyoung 	data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header + ccmp.ic_trailer);
    406  1.16  riastrad 	ccmp_init_blocks(&ctx->cc_aes, wh, pn, data_len, &aes_ccm);
    407   1.1    dyoung 	m_copydata(m, m->m_pkthdr.len - ccmp.ic_trailer, ccmp.ic_trailer, mic);
    408  1.13      maxv 
    409  1.16  riastrad 	if (!aes_ccm_dec_mbuf(&aes_ccm, m, hdrlen + ccmp.ic_header, data_len,
    410  1.16  riastrad 		mic)) {
    411   1.1    dyoung 		IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
    412  1.16  riastrad 		    "[%s] AES-CCM decrypt failed; MIC mismatch\n",
    413  1.16  riastrad 		    ether_sprintf(wh->i_addr2));
    414   1.1    dyoung 		ctx->cc_ic->ic_stats.is_rx_ccmpmic++;
    415   1.1    dyoung 		return 0;
    416   1.1    dyoung 	}
    417  1.13      maxv 
    418   1.1    dyoung 	return 1;
    419   1.1    dyoung }
    420   1.4     skrll 
    421   1.4     skrll IEEE80211_CRYPTO_SETUP(ccmp_register)
    422   1.4     skrll {
    423   1.4     skrll 	ieee80211_crypto_register(&ccmp);
    424   1.4     skrll }
    425