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