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