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