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