18a1b9b6aSSam Leffler /*- 2b032f27cSSam Leffler * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 38a1b9b6aSSam Leffler * All rights reserved. 48a1b9b6aSSam Leffler * 58a1b9b6aSSam Leffler * Redistribution and use in source and binary forms, with or without 68a1b9b6aSSam Leffler * modification, are permitted provided that the following conditions 78a1b9b6aSSam Leffler * are met: 88a1b9b6aSSam Leffler * 1. Redistributions of source code must retain the above copyright 98a1b9b6aSSam Leffler * notice, this list of conditions and the following disclaimer. 108a1b9b6aSSam Leffler * 2. Redistributions in binary form must reproduce the above copyright 118a1b9b6aSSam Leffler * notice, this list of conditions and the following disclaimer in the 128a1b9b6aSSam Leffler * documentation and/or other materials provided with the distribution. 138a1b9b6aSSam Leffler * 148a1b9b6aSSam Leffler * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 158a1b9b6aSSam Leffler * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 168a1b9b6aSSam Leffler * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 178a1b9b6aSSam Leffler * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 188a1b9b6aSSam Leffler * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 198a1b9b6aSSam Leffler * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 208a1b9b6aSSam Leffler * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 218a1b9b6aSSam Leffler * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 228a1b9b6aSSam Leffler * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 238a1b9b6aSSam Leffler * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 248a1b9b6aSSam Leffler */ 258a1b9b6aSSam Leffler 268a1b9b6aSSam Leffler #include <sys/cdefs.h> 278a1b9b6aSSam Leffler __FBSDID("$FreeBSD$"); 288a1b9b6aSSam Leffler 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 * 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 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 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 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 163ef0d8f63SAdrian Chadd ccmp_encap(struct ieee80211_key *k, struct mbuf *m) 1648a1b9b6aSSam Leffler { 1654e844c94SSam Leffler struct ccmp_ctx *ctx = k->wk_private; 1664e844c94SSam Leffler struct ieee80211com *ic = ctx->cc_ic; 16768e8e04eSSam Leffler uint8_t *ivp; 1688a1b9b6aSSam Leffler int hdrlen; 1698a1b9b6aSSam Leffler 1704e844c94SSam Leffler hdrlen = ieee80211_hdrspace(ic, mtod(m, void *)); 1718a1b9b6aSSam Leffler 1728a1b9b6aSSam Leffler /* 1738a1b9b6aSSam Leffler * Copy down 802.11 header and add the IV, KeyID, and ExtIV. 1748a1b9b6aSSam Leffler */ 1758a1b9b6aSSam Leffler M_PREPEND(m, ccmp.ic_header, M_NOWAIT); 1768a1b9b6aSSam Leffler if (m == NULL) 1778a1b9b6aSSam Leffler return 0; 17868e8e04eSSam Leffler ivp = mtod(m, uint8_t *); 1798a1b9b6aSSam Leffler ovbcopy(ivp + ccmp.ic_header, ivp, hdrlen); 1808a1b9b6aSSam Leffler ivp += hdrlen; 1818a1b9b6aSSam Leffler 182c0cb9349SAdrian Chadd ccmp_setiv(k, ivp); 1838a1b9b6aSSam Leffler 1848a1b9b6aSSam Leffler /* 185bf0b7b45SAdrian Chadd * Finally, do software encrypt if needed. 1868a1b9b6aSSam Leffler */ 1875c1f7f19SSam Leffler if ((k->wk_flags & IEEE80211_KEY_SWENCRYPT) && 1888a1b9b6aSSam Leffler !ccmp_encrypt(k, m, hdrlen)) 1898a1b9b6aSSam Leffler return 0; 1908a1b9b6aSSam Leffler 1918a1b9b6aSSam Leffler return 1; 1928a1b9b6aSSam Leffler } 1938a1b9b6aSSam Leffler 1948a1b9b6aSSam Leffler /* 1958a1b9b6aSSam Leffler * Add MIC to the frame as needed. 1968a1b9b6aSSam Leffler */ 1978a1b9b6aSSam Leffler static int 19896d88463SSam Leffler ccmp_enmic(struct ieee80211_key *k, struct mbuf *m, int force) 1998a1b9b6aSSam Leffler { 2008a1b9b6aSSam Leffler 2018a1b9b6aSSam Leffler return 1; 2028a1b9b6aSSam Leffler } 2038a1b9b6aSSam Leffler 2048a1b9b6aSSam Leffler static __inline uint64_t 2058a1b9b6aSSam Leffler READ_6(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5) 2068a1b9b6aSSam Leffler { 2078a1b9b6aSSam Leffler uint32_t iv32 = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24); 2088a1b9b6aSSam Leffler uint16_t iv16 = (b4 << 0) | (b5 << 8); 2098a1b9b6aSSam Leffler return (((uint64_t)iv16) << 32) | iv32; 2108a1b9b6aSSam Leffler } 2118a1b9b6aSSam Leffler 2128a1b9b6aSSam Leffler /* 2138a1b9b6aSSam Leffler * Validate and strip privacy headers (and trailer) for a 2148a1b9b6aSSam Leffler * received frame. The specified key should be correct but 2158a1b9b6aSSam Leffler * is also verified. 2168a1b9b6aSSam Leffler */ 2178a1b9b6aSSam Leffler static int 2182cc12adeSSam Leffler ccmp_decap(struct ieee80211_key *k, struct mbuf *m, int hdrlen) 2198a1b9b6aSSam Leffler { 2208a1b9b6aSSam Leffler struct ccmp_ctx *ctx = k->wk_private; 221b032f27cSSam Leffler struct ieee80211vap *vap = ctx->cc_vap; 2228a1b9b6aSSam Leffler struct ieee80211_frame *wh; 223b032f27cSSam Leffler uint8_t *ivp, tid; 2248a1b9b6aSSam Leffler uint64_t pn; 2258a1b9b6aSSam Leffler 2268a1b9b6aSSam Leffler /* 2278a1b9b6aSSam Leffler * Header should have extended IV and sequence number; 2288a1b9b6aSSam Leffler * verify the former and validate the latter. 2298a1b9b6aSSam Leffler */ 2308a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *); 2318a1b9b6aSSam Leffler ivp = mtod(m, uint8_t *) + hdrlen; 2328a1b9b6aSSam Leffler if ((ivp[IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV) == 0) { 2338a1b9b6aSSam Leffler /* 2348a1b9b6aSSam Leffler * No extended IV; discard frame. 2358a1b9b6aSSam Leffler */ 236b032f27cSSam Leffler IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 237b032f27cSSam Leffler "%s", "missing ExtIV for AES-CCM cipher"); 238b032f27cSSam Leffler vap->iv_stats.is_rx_ccmpformat++; 2398a1b9b6aSSam Leffler return 0; 2408a1b9b6aSSam Leffler } 241b032f27cSSam Leffler tid = ieee80211_gettid(wh); 2428a1b9b6aSSam Leffler pn = READ_6(ivp[0], ivp[1], ivp[4], ivp[5], ivp[6], ivp[7]); 2435d766a09SBernhard Schmidt if (pn <= k->wk_keyrsc[tid] && 2445d766a09SBernhard Schmidt (k->wk_flags & IEEE80211_KEY_NOREPLAY) == 0) { 2458a1b9b6aSSam Leffler /* 2468a1b9b6aSSam Leffler * Replay violation. 2478a1b9b6aSSam Leffler */ 248ebaf87ebSSam Leffler ieee80211_notify_replay_failure(vap, wh, k, pn, tid); 249b032f27cSSam Leffler vap->iv_stats.is_rx_ccmpreplay++; 2508a1b9b6aSSam Leffler return 0; 2518a1b9b6aSSam Leffler } 2528a1b9b6aSSam Leffler 2538a1b9b6aSSam Leffler /* 2548a1b9b6aSSam Leffler * Check if the device handled the decrypt in hardware. 2558a1b9b6aSSam Leffler * If so we just strip the header; otherwise we need to 2568a1b9b6aSSam Leffler * handle the decrypt in software. Note that for the 2578a1b9b6aSSam Leffler * latter we leave the header in place for use in the 2588a1b9b6aSSam Leffler * decryption work. 2598a1b9b6aSSam Leffler */ 2605c1f7f19SSam Leffler if ((k->wk_flags & IEEE80211_KEY_SWDECRYPT) && 2618a1b9b6aSSam Leffler !ccmp_decrypt(k, pn, m, hdrlen)) 2628a1b9b6aSSam Leffler return 0; 2638a1b9b6aSSam Leffler 2648a1b9b6aSSam Leffler /* 2658a1b9b6aSSam Leffler * Copy up 802.11 header and strip crypto bits. 2668a1b9b6aSSam Leffler */ 26768e8e04eSSam Leffler ovbcopy(mtod(m, void *), mtod(m, uint8_t *) + ccmp.ic_header, hdrlen); 2688a1b9b6aSSam Leffler m_adj(m, ccmp.ic_header); 2698a1b9b6aSSam Leffler m_adj(m, -ccmp.ic_trailer); 2708a1b9b6aSSam Leffler 2718a1b9b6aSSam Leffler /* 2728a1b9b6aSSam Leffler * Ok to update rsc now. 2738a1b9b6aSSam Leffler */ 274b032f27cSSam Leffler k->wk_keyrsc[tid] = pn; 2758a1b9b6aSSam Leffler 2768a1b9b6aSSam Leffler return 1; 2778a1b9b6aSSam Leffler } 2788a1b9b6aSSam Leffler 2798a1b9b6aSSam Leffler /* 2808a1b9b6aSSam Leffler * Verify and strip MIC from the frame. 2818a1b9b6aSSam Leffler */ 2828a1b9b6aSSam Leffler static int 28396d88463SSam Leffler ccmp_demic(struct ieee80211_key *k, struct mbuf *m, int force) 2848a1b9b6aSSam Leffler { 2858a1b9b6aSSam Leffler return 1; 2868a1b9b6aSSam Leffler } 2878a1b9b6aSSam Leffler 2888a1b9b6aSSam Leffler static __inline void 2898a1b9b6aSSam Leffler xor_block(uint8_t *b, const uint8_t *a, size_t len) 2908a1b9b6aSSam Leffler { 2918a1b9b6aSSam Leffler int i; 2928a1b9b6aSSam Leffler for (i = 0; i < len; i++) 2938a1b9b6aSSam Leffler b[i] ^= a[i]; 2948a1b9b6aSSam Leffler } 2958a1b9b6aSSam Leffler 2968a1b9b6aSSam Leffler /* 2978a1b9b6aSSam Leffler * Host AP crypt: host-based CCMP encryption implementation for Host AP driver 2988a1b9b6aSSam Leffler * 2998a1b9b6aSSam Leffler * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi> 3008a1b9b6aSSam Leffler * 3018a1b9b6aSSam Leffler * This program is free software; you can redistribute it and/or modify 3028a1b9b6aSSam Leffler * it under the terms of the GNU General Public License version 2 as 3038a1b9b6aSSam Leffler * published by the Free Software Foundation. See README and COPYING for 3048a1b9b6aSSam Leffler * more details. 3058a1b9b6aSSam Leffler * 3068a1b9b6aSSam Leffler * Alternatively, this software may be distributed under the terms of BSD 3078a1b9b6aSSam Leffler * license. 3088a1b9b6aSSam Leffler */ 3098a1b9b6aSSam Leffler 3108a1b9b6aSSam Leffler static void 3118a1b9b6aSSam Leffler ccmp_init_blocks(rijndael_ctx *ctx, struct ieee80211_frame *wh, 3128a1b9b6aSSam Leffler u_int64_t pn, size_t dlen, 3138a1b9b6aSSam Leffler uint8_t b0[AES_BLOCK_LEN], uint8_t aad[2 * AES_BLOCK_LEN], 3148a1b9b6aSSam Leffler uint8_t auth[AES_BLOCK_LEN], uint8_t s0[AES_BLOCK_LEN]) 3158a1b9b6aSSam Leffler { 3168a1b9b6aSSam Leffler #define IS_QOS_DATA(wh) IEEE80211_QOS_HAS_SEQ(wh) 3178a1b9b6aSSam Leffler 3188a1b9b6aSSam Leffler /* CCM Initial Block: 3198a1b9b6aSSam Leffler * Flag (Include authentication header, M=3 (8-octet MIC), 3208a1b9b6aSSam Leffler * L=1 (2-octet Dlen)) 3218a1b9b6aSSam Leffler * Nonce: 0x00 | A2 | PN 3228a1b9b6aSSam Leffler * Dlen */ 3238a1b9b6aSSam Leffler b0[0] = 0x59; 3248a1b9b6aSSam Leffler /* NB: b0[1] set below */ 3258a1b9b6aSSam Leffler IEEE80211_ADDR_COPY(b0 + 2, wh->i_addr2); 3268a1b9b6aSSam Leffler b0[8] = pn >> 40; 3278a1b9b6aSSam Leffler b0[9] = pn >> 32; 3288a1b9b6aSSam Leffler b0[10] = pn >> 24; 3298a1b9b6aSSam Leffler b0[11] = pn >> 16; 3308a1b9b6aSSam Leffler b0[12] = pn >> 8; 3318a1b9b6aSSam Leffler b0[13] = pn >> 0; 3328a1b9b6aSSam Leffler b0[14] = (dlen >> 8) & 0xff; 3338a1b9b6aSSam Leffler b0[15] = dlen & 0xff; 3348a1b9b6aSSam Leffler 3358a1b9b6aSSam Leffler /* AAD: 3368a1b9b6aSSam Leffler * FC with bits 4..6 and 11..13 masked to zero; 14 is always one 3378a1b9b6aSSam Leffler * A1 | A2 | A3 3388a1b9b6aSSam Leffler * SC with bits 4..15 (seq#) masked to zero 3398a1b9b6aSSam Leffler * A4 (if present) 3408a1b9b6aSSam Leffler * QC (if present) 3418a1b9b6aSSam Leffler */ 3428a1b9b6aSSam Leffler aad[0] = 0; /* AAD length >> 8 */ 3438a1b9b6aSSam Leffler /* NB: aad[1] set below */ 3448a1b9b6aSSam Leffler aad[2] = wh->i_fc[0] & 0x8f; /* XXX magic #s */ 3458a1b9b6aSSam Leffler aad[3] = wh->i_fc[1] & 0xc7; /* XXX magic #s */ 3468a1b9b6aSSam Leffler /* NB: we know 3 addresses are contiguous */ 3478a1b9b6aSSam Leffler memcpy(aad + 4, wh->i_addr1, 3 * IEEE80211_ADDR_LEN); 3488a1b9b6aSSam Leffler aad[22] = wh->i_seq[0] & IEEE80211_SEQ_FRAG_MASK; 3498a1b9b6aSSam Leffler aad[23] = 0; /* all bits masked */ 3508a1b9b6aSSam Leffler /* 3518a1b9b6aSSam Leffler * Construct variable-length portion of AAD based 3528a1b9b6aSSam Leffler * on whether this is a 4-address frame/QOS frame. 3538a1b9b6aSSam Leffler * We always zero-pad to 32 bytes before running it 3548a1b9b6aSSam Leffler * through the cipher. 3558a1b9b6aSSam Leffler * 3568a1b9b6aSSam Leffler * We also fill in the priority bits of the CCM 3578a1b9b6aSSam Leffler * initial block as we know whether or not we have 3588a1b9b6aSSam Leffler * a QOS frame. 3598a1b9b6aSSam Leffler */ 36032bdd57bSSam Leffler if (IEEE80211_IS_DSTODS(wh)) { 3618a1b9b6aSSam Leffler IEEE80211_ADDR_COPY(aad + 24, 3628a1b9b6aSSam Leffler ((struct ieee80211_frame_addr4 *)wh)->i_addr4); 3638a1b9b6aSSam Leffler if (IS_QOS_DATA(wh)) { 3648a1b9b6aSSam Leffler struct ieee80211_qosframe_addr4 *qwh4 = 3658a1b9b6aSSam Leffler (struct ieee80211_qosframe_addr4 *) wh; 3668a1b9b6aSSam Leffler aad[30] = qwh4->i_qos[0] & 0x0f;/* just priority bits */ 3678a1b9b6aSSam Leffler aad[31] = 0; 3688a1b9b6aSSam Leffler b0[1] = aad[30]; 3698a1b9b6aSSam Leffler aad[1] = 22 + IEEE80211_ADDR_LEN + 2; 3708a1b9b6aSSam Leffler } else { 37168e8e04eSSam Leffler *(uint16_t *)&aad[30] = 0; 3728a1b9b6aSSam Leffler b0[1] = 0; 3738a1b9b6aSSam Leffler aad[1] = 22 + IEEE80211_ADDR_LEN; 3748a1b9b6aSSam Leffler } 3758a1b9b6aSSam Leffler } else { 3768a1b9b6aSSam Leffler if (IS_QOS_DATA(wh)) { 3778a1b9b6aSSam Leffler struct ieee80211_qosframe *qwh = 3788a1b9b6aSSam Leffler (struct ieee80211_qosframe*) wh; 3798a1b9b6aSSam Leffler aad[24] = qwh->i_qos[0] & 0x0f; /* just priority bits */ 3808a1b9b6aSSam Leffler aad[25] = 0; 3818a1b9b6aSSam Leffler b0[1] = aad[24]; 3828a1b9b6aSSam Leffler aad[1] = 22 + 2; 3838a1b9b6aSSam Leffler } else { 38468e8e04eSSam Leffler *(uint16_t *)&aad[24] = 0; 3858a1b9b6aSSam Leffler b0[1] = 0; 3868a1b9b6aSSam Leffler aad[1] = 22; 3878a1b9b6aSSam Leffler } 38868e8e04eSSam Leffler *(uint16_t *)&aad[26] = 0; 38968e8e04eSSam Leffler *(uint32_t *)&aad[28] = 0; 3908a1b9b6aSSam Leffler } 3918a1b9b6aSSam Leffler 3928a1b9b6aSSam Leffler /* Start with the first block and AAD */ 3938a1b9b6aSSam Leffler rijndael_encrypt(ctx, b0, auth); 3948a1b9b6aSSam Leffler xor_block(auth, aad, AES_BLOCK_LEN); 3958a1b9b6aSSam Leffler rijndael_encrypt(ctx, auth, auth); 3968a1b9b6aSSam Leffler xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN); 3978a1b9b6aSSam Leffler rijndael_encrypt(ctx, auth, auth); 3988a1b9b6aSSam Leffler b0[0] &= 0x07; 3998a1b9b6aSSam Leffler b0[14] = b0[15] = 0; 4008a1b9b6aSSam Leffler rijndael_encrypt(ctx, b0, s0); 4018a1b9b6aSSam Leffler #undef IS_QOS_DATA 4028a1b9b6aSSam Leffler } 4038a1b9b6aSSam Leffler 4048a1b9b6aSSam Leffler #define CCMP_ENCRYPT(_i, _b, _b0, _pos, _e, _len) do { \ 4058a1b9b6aSSam Leffler /* Authentication */ \ 4068a1b9b6aSSam Leffler xor_block(_b, _pos, _len); \ 4078a1b9b6aSSam Leffler rijndael_encrypt(&ctx->cc_aes, _b, _b); \ 4088a1b9b6aSSam Leffler /* Encryption, with counter */ \ 4098a1b9b6aSSam Leffler _b0[14] = (_i >> 8) & 0xff; \ 4108a1b9b6aSSam Leffler _b0[15] = _i & 0xff; \ 4118a1b9b6aSSam Leffler rijndael_encrypt(&ctx->cc_aes, _b0, _e); \ 4128a1b9b6aSSam Leffler xor_block(_pos, _e, _len); \ 4138a1b9b6aSSam Leffler } while (0) 4148a1b9b6aSSam Leffler 4158a1b9b6aSSam Leffler static int 4168a1b9b6aSSam Leffler ccmp_encrypt(struct ieee80211_key *key, struct mbuf *m0, int hdrlen) 4178a1b9b6aSSam Leffler { 4188a1b9b6aSSam Leffler struct ccmp_ctx *ctx = key->wk_private; 4198a1b9b6aSSam Leffler struct ieee80211_frame *wh; 4208a1b9b6aSSam Leffler struct mbuf *m = m0; 421dd423201SSam Leffler int data_len, i, space; 4228a1b9b6aSSam Leffler uint8_t aad[2 * AES_BLOCK_LEN], b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN], 4238a1b9b6aSSam Leffler e[AES_BLOCK_LEN], s0[AES_BLOCK_LEN]; 4248a1b9b6aSSam Leffler uint8_t *pos; 4258a1b9b6aSSam Leffler 426b032f27cSSam Leffler ctx->cc_vap->iv_stats.is_crypto_ccmp++; 4278a1b9b6aSSam Leffler 4288a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *); 4298a1b9b6aSSam Leffler data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header); 4308a1b9b6aSSam Leffler ccmp_init_blocks(&ctx->cc_aes, wh, key->wk_keytsc, 4318a1b9b6aSSam Leffler data_len, b0, aad, b, s0); 4328a1b9b6aSSam Leffler 4338a1b9b6aSSam Leffler i = 1; 4348a1b9b6aSSam Leffler pos = mtod(m, uint8_t *) + hdrlen + ccmp.ic_header; 4358a1b9b6aSSam Leffler /* NB: assumes header is entirely in first mbuf */ 4368a1b9b6aSSam Leffler space = m->m_len - (hdrlen + ccmp.ic_header); 4378a1b9b6aSSam Leffler for (;;) { 4388a1b9b6aSSam Leffler if (space > data_len) 4398a1b9b6aSSam Leffler space = data_len; 4408a1b9b6aSSam Leffler /* 4418a1b9b6aSSam Leffler * Do full blocks. 4428a1b9b6aSSam Leffler */ 4438a1b9b6aSSam Leffler while (space >= AES_BLOCK_LEN) { 4448a1b9b6aSSam Leffler CCMP_ENCRYPT(i, b, b0, pos, e, AES_BLOCK_LEN); 4458a1b9b6aSSam Leffler pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN; 4468a1b9b6aSSam Leffler data_len -= AES_BLOCK_LEN; 4478a1b9b6aSSam Leffler i++; 4488a1b9b6aSSam Leffler } 4498a1b9b6aSSam Leffler if (data_len <= 0) /* no more data */ 4508a1b9b6aSSam Leffler break; 4518a1b9b6aSSam Leffler m = m->m_next; 4528a1b9b6aSSam Leffler if (m == NULL) { /* last buffer */ 4538a1b9b6aSSam Leffler if (space != 0) { 4548a1b9b6aSSam Leffler /* 4558a1b9b6aSSam Leffler * Short last block. 4568a1b9b6aSSam Leffler */ 4578a1b9b6aSSam Leffler CCMP_ENCRYPT(i, b, b0, pos, e, space); 4588a1b9b6aSSam Leffler } 4598a1b9b6aSSam Leffler break; 4608a1b9b6aSSam Leffler } 4618a1b9b6aSSam Leffler if (space != 0) { 4628a1b9b6aSSam Leffler uint8_t *pos_next; 463dd423201SSam Leffler int space_next; 464dd423201SSam Leffler int len, dl, sp; 465dd423201SSam Leffler struct mbuf *n; 4668a1b9b6aSSam Leffler 4678a1b9b6aSSam Leffler /* 468dd423201SSam Leffler * Block straddles one or more mbufs, gather data 469dd423201SSam Leffler * into the block buffer b, apply the cipher, then 470dd423201SSam Leffler * scatter the results back into the mbuf chain. 471dd423201SSam Leffler * The buffer will automatically get space bytes 472dd423201SSam Leffler * of data at offset 0 copied in+out by the 473dd423201SSam Leffler * CCMP_ENCRYPT request so we must take care of 474dd423201SSam Leffler * the remaining data. 4758a1b9b6aSSam Leffler */ 476dd423201SSam Leffler n = m; 477dd423201SSam Leffler dl = data_len; 478dd423201SSam Leffler sp = space; 479dd423201SSam Leffler for (;;) { 480dd423201SSam Leffler pos_next = mtod(n, uint8_t *); 481dd423201SSam Leffler len = min(dl, AES_BLOCK_LEN); 482dd423201SSam Leffler space_next = len > sp ? len - sp : 0; 483dd423201SSam Leffler if (n->m_len >= space_next) { 484dd423201SSam Leffler /* 485dd423201SSam Leffler * This mbuf has enough data; just grab 486dd423201SSam Leffler * what we need and stop. 487dd423201SSam Leffler */ 488dd423201SSam Leffler xor_block(b+sp, pos_next, space_next); 489dd423201SSam Leffler break; 490dd423201SSam Leffler } 491dd423201SSam Leffler /* 492dd423201SSam Leffler * This mbuf's contents are insufficient, 493dd423201SSam Leffler * take 'em all and prepare to advance to 494dd423201SSam Leffler * the next mbuf. 495dd423201SSam Leffler */ 496dd423201SSam Leffler xor_block(b+sp, pos_next, n->m_len); 497dd423201SSam Leffler sp += n->m_len, dl -= n->m_len; 498dd423201SSam Leffler n = n->m_next; 499dd423201SSam Leffler if (n == NULL) 500dd423201SSam Leffler break; 501dd423201SSam Leffler } 5028a1b9b6aSSam Leffler 5038a1b9b6aSSam Leffler CCMP_ENCRYPT(i, b, b0, pos, e, space); 5048a1b9b6aSSam Leffler 505dd423201SSam Leffler /* NB: just like above, but scatter data to mbufs */ 506dd423201SSam Leffler dl = data_len; 507dd423201SSam Leffler sp = space; 508dd423201SSam Leffler for (;;) { 509dd423201SSam Leffler pos_next = mtod(m, uint8_t *); 510dd423201SSam Leffler len = min(dl, AES_BLOCK_LEN); 511dd423201SSam Leffler space_next = len > sp ? len - sp : 0; 512dd423201SSam Leffler if (m->m_len >= space_next) { 513dd423201SSam Leffler xor_block(pos_next, e+sp, space_next); 514dd423201SSam Leffler break; 515dd423201SSam Leffler } 516dd423201SSam Leffler xor_block(pos_next, e+sp, m->m_len); 517dd423201SSam Leffler sp += m->m_len, dl -= m->m_len; 518dd423201SSam Leffler m = m->m_next; 519dd423201SSam Leffler if (m == NULL) 520dd423201SSam Leffler goto done; 521dd423201SSam Leffler } 522dd423201SSam Leffler /* 523dd423201SSam Leffler * Do bookkeeping. m now points to the last mbuf 524dd423201SSam Leffler * we grabbed data from. We know we consumed a 525dd423201SSam Leffler * full block of data as otherwise we'd have hit 526dd423201SSam Leffler * the end of the mbuf chain, so deduct from data_len. 527dd423201SSam Leffler * Otherwise advance the block number (i) and setup 528dd423201SSam Leffler * pos+space to reflect contents of the new mbuf. 529dd423201SSam Leffler */ 530dd423201SSam Leffler data_len -= AES_BLOCK_LEN; 531dd423201SSam Leffler i++; 5328a1b9b6aSSam Leffler pos = pos_next + space_next; 5338a1b9b6aSSam Leffler space = m->m_len - space_next; 5348a1b9b6aSSam Leffler } else { 5358a1b9b6aSSam Leffler /* 5368a1b9b6aSSam Leffler * Setup for next buffer. 5378a1b9b6aSSam Leffler */ 5388a1b9b6aSSam Leffler pos = mtod(m, uint8_t *); 5398a1b9b6aSSam Leffler space = m->m_len; 5408a1b9b6aSSam Leffler } 5418a1b9b6aSSam Leffler } 542dd423201SSam Leffler done: 5438a1b9b6aSSam Leffler /* tack on MIC */ 5448a1b9b6aSSam Leffler xor_block(b, s0, ccmp.ic_trailer); 5458a1b9b6aSSam Leffler return m_append(m0, ccmp.ic_trailer, b); 5468a1b9b6aSSam Leffler } 5478a1b9b6aSSam Leffler #undef CCMP_ENCRYPT 5488a1b9b6aSSam Leffler 5498a1b9b6aSSam Leffler #define CCMP_DECRYPT(_i, _b, _b0, _pos, _a, _len) do { \ 5508a1b9b6aSSam Leffler /* Decrypt, with counter */ \ 5518a1b9b6aSSam Leffler _b0[14] = (_i >> 8) & 0xff; \ 5528a1b9b6aSSam Leffler _b0[15] = _i & 0xff; \ 5538a1b9b6aSSam Leffler rijndael_encrypt(&ctx->cc_aes, _b0, _b); \ 5548a1b9b6aSSam Leffler xor_block(_pos, _b, _len); \ 5558a1b9b6aSSam Leffler /* Authentication */ \ 5568a1b9b6aSSam Leffler xor_block(_a, _pos, _len); \ 5578a1b9b6aSSam Leffler rijndael_encrypt(&ctx->cc_aes, _a, _a); \ 5588a1b9b6aSSam Leffler } while (0) 5598a1b9b6aSSam Leffler 5608a1b9b6aSSam Leffler static int 5618a1b9b6aSSam Leffler ccmp_decrypt(struct ieee80211_key *key, u_int64_t pn, struct mbuf *m, int hdrlen) 5628a1b9b6aSSam Leffler { 5638a1b9b6aSSam Leffler struct ccmp_ctx *ctx = key->wk_private; 564b032f27cSSam Leffler struct ieee80211vap *vap = ctx->cc_vap; 5658a1b9b6aSSam Leffler struct ieee80211_frame *wh; 5668a1b9b6aSSam Leffler uint8_t aad[2 * AES_BLOCK_LEN]; 5678a1b9b6aSSam Leffler uint8_t b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN], a[AES_BLOCK_LEN]; 5688a1b9b6aSSam Leffler uint8_t mic[AES_BLOCK_LEN]; 5698a1b9b6aSSam Leffler size_t data_len; 5708a1b9b6aSSam Leffler int i; 5718a1b9b6aSSam Leffler uint8_t *pos; 5728a1b9b6aSSam Leffler u_int space; 5738a1b9b6aSSam Leffler 574b032f27cSSam Leffler ctx->cc_vap->iv_stats.is_crypto_ccmp++; 5758a1b9b6aSSam Leffler 5768a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *); 5778a1b9b6aSSam Leffler data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header + ccmp.ic_trailer); 5788a1b9b6aSSam Leffler ccmp_init_blocks(&ctx->cc_aes, wh, pn, data_len, b0, aad, a, b); 5798a1b9b6aSSam Leffler m_copydata(m, m->m_pkthdr.len - ccmp.ic_trailer, ccmp.ic_trailer, mic); 5808a1b9b6aSSam Leffler xor_block(mic, b, ccmp.ic_trailer); 5818a1b9b6aSSam Leffler 5828a1b9b6aSSam Leffler i = 1; 5838a1b9b6aSSam Leffler pos = mtod(m, uint8_t *) + hdrlen + ccmp.ic_header; 5848a1b9b6aSSam Leffler space = m->m_len - (hdrlen + ccmp.ic_header); 5858a1b9b6aSSam Leffler for (;;) { 5868a1b9b6aSSam Leffler if (space > data_len) 5878a1b9b6aSSam Leffler space = data_len; 5888a1b9b6aSSam Leffler while (space >= AES_BLOCK_LEN) { 5898a1b9b6aSSam Leffler CCMP_DECRYPT(i, b, b0, pos, a, AES_BLOCK_LEN); 5908a1b9b6aSSam Leffler pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN; 5918a1b9b6aSSam Leffler data_len -= AES_BLOCK_LEN; 5928a1b9b6aSSam Leffler i++; 5938a1b9b6aSSam Leffler } 5948a1b9b6aSSam Leffler if (data_len <= 0) /* no more data */ 5958a1b9b6aSSam Leffler break; 5968a1b9b6aSSam Leffler m = m->m_next; 5978a1b9b6aSSam Leffler if (m == NULL) { /* last buffer */ 5988a1b9b6aSSam Leffler if (space != 0) /* short last block */ 5998a1b9b6aSSam Leffler CCMP_DECRYPT(i, b, b0, pos, a, space); 6008a1b9b6aSSam Leffler break; 6018a1b9b6aSSam Leffler } 6028a1b9b6aSSam Leffler if (space != 0) { 6038a1b9b6aSSam Leffler uint8_t *pos_next; 6048a1b9b6aSSam Leffler u_int space_next; 6058a1b9b6aSSam Leffler u_int len; 6068a1b9b6aSSam Leffler 6078a1b9b6aSSam Leffler /* 6088a1b9b6aSSam Leffler * Block straddles buffers, split references. We 609dd423201SSam Leffler * do not handle splits that require >2 buffers 610dd423201SSam Leffler * since rx'd frames are never badly fragmented 611dd423201SSam Leffler * because drivers typically recv in clusters. 6128a1b9b6aSSam Leffler */ 6138a1b9b6aSSam Leffler pos_next = mtod(m, uint8_t *); 6148a1b9b6aSSam Leffler len = min(data_len, AES_BLOCK_LEN); 6158a1b9b6aSSam Leffler space_next = len > space ? len - space : 0; 6168a1b9b6aSSam Leffler KASSERT(m->m_len >= space_next, 6178a1b9b6aSSam Leffler ("not enough data in following buffer, " 6188a1b9b6aSSam Leffler "m_len %u need %u\n", m->m_len, space_next)); 6198a1b9b6aSSam Leffler 6208a1b9b6aSSam Leffler xor_block(b+space, pos_next, space_next); 6218a1b9b6aSSam Leffler CCMP_DECRYPT(i, b, b0, pos, a, space); 6228a1b9b6aSSam Leffler xor_block(pos_next, b+space, space_next); 6238a1b9b6aSSam Leffler data_len -= len; 6248a1b9b6aSSam Leffler i++; 6258a1b9b6aSSam Leffler 6268a1b9b6aSSam Leffler pos = pos_next + space_next; 6278a1b9b6aSSam Leffler space = m->m_len - space_next; 6288a1b9b6aSSam Leffler } else { 6298a1b9b6aSSam Leffler /* 6308a1b9b6aSSam Leffler * Setup for next buffer. 6318a1b9b6aSSam Leffler */ 6328a1b9b6aSSam Leffler pos = mtod(m, uint8_t *); 6338a1b9b6aSSam Leffler space = m->m_len; 6348a1b9b6aSSam Leffler } 6358a1b9b6aSSam Leffler } 6368a1b9b6aSSam Leffler if (memcmp(mic, a, ccmp.ic_trailer) != 0) { 637b032f27cSSam Leffler IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 638b032f27cSSam Leffler "%s", "AES-CCM decrypt failed; MIC mismatch"); 639b032f27cSSam Leffler vap->iv_stats.is_rx_ccmpmic++; 6408a1b9b6aSSam Leffler return 0; 6418a1b9b6aSSam Leffler } 6428a1b9b6aSSam Leffler return 1; 6438a1b9b6aSSam Leffler } 6448a1b9b6aSSam Leffler #undef CCMP_DECRYPT 6458a1b9b6aSSam Leffler 6468a1b9b6aSSam Leffler /* 6478a1b9b6aSSam Leffler * Module glue. 6488a1b9b6aSSam Leffler */ 64968e8e04eSSam Leffler IEEE80211_CRYPTO_MODULE(ccmp, 1); 650