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