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 #define CCMP_128_MIC_LEN 8
58 #define CCMP_256_MIC_LEN 16
59
60 struct ccmp_ctx {
61 struct ieee80211vap *cc_vap; /* for diagnostics+statistics */
62 struct ieee80211com *cc_ic;
63 rijndael_ctx cc_aes;
64 };
65
66 static void *ccmp_attach(struct ieee80211vap *, struct ieee80211_key *);
67 static void ccmp_detach(struct ieee80211_key *);
68 static int ccmp_setkey(struct ieee80211_key *);
69 static void ccmp_setiv(struct ieee80211_key *, uint8_t *);
70 static int ccmp_encap(struct ieee80211_key *, struct mbuf *);
71 static int ccmp_decap(struct ieee80211_key *, struct mbuf *, int);
72 static int ccmp_enmic(struct ieee80211_key *, struct mbuf *, int);
73 static int ccmp_demic(struct ieee80211_key *, struct mbuf *, int);
74
75 static const struct ieee80211_cipher ccmp = {
76 .ic_name = "AES-CCM",
77 .ic_cipher = IEEE80211_CIPHER_AES_CCM,
78 .ic_header = IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
79 IEEE80211_WEP_EXTIVLEN,
80 .ic_trailer = CCMP_128_MIC_LEN,
81 .ic_miclen = 0,
82 .ic_attach = ccmp_attach,
83 .ic_detach = ccmp_detach,
84 .ic_setkey = ccmp_setkey,
85 .ic_setiv = ccmp_setiv,
86 .ic_encap = ccmp_encap,
87 .ic_decap = ccmp_decap,
88 .ic_enmic = ccmp_enmic,
89 .ic_demic = ccmp_demic,
90 };
91
92 static const struct ieee80211_cipher ccmp_256 = {
93 .ic_name = "AES-CCM-256",
94 .ic_cipher = IEEE80211_CIPHER_AES_CCM_256,
95 .ic_header = IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
96 IEEE80211_WEP_EXTIVLEN,
97 .ic_trailer = CCMP_256_MIC_LEN,
98 .ic_miclen = 0,
99 .ic_attach = ccmp_attach,
100 .ic_detach = ccmp_detach,
101 .ic_setkey = ccmp_setkey,
102 .ic_setiv = ccmp_setiv,
103 .ic_encap = ccmp_encap,
104 .ic_decap = ccmp_decap,
105 .ic_enmic = ccmp_enmic,
106 .ic_demic = ccmp_demic,
107 };
108
109 static int ccmp_encrypt(struct ieee80211_key *, struct mbuf *, int hdrlen);
110 static int ccmp_decrypt(struct ieee80211_key *, u_int64_t pn,
111 struct mbuf *, int hdrlen);
112
113 /* number of references from net80211 layer */
114 static int nrefs = 0;
115
116 static void *
ccmp_attach(struct ieee80211vap * vap,struct ieee80211_key * k)117 ccmp_attach(struct ieee80211vap *vap, struct ieee80211_key *k)
118 {
119 struct ccmp_ctx *ctx;
120
121 ctx = (struct ccmp_ctx *) IEEE80211_MALLOC(sizeof(struct ccmp_ctx),
122 M_80211_CRYPTO, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
123 if (ctx == NULL) {
124 vap->iv_stats.is_crypto_nomem++;
125 return NULL;
126 }
127 ctx->cc_vap = vap;
128 ctx->cc_ic = vap->iv_ic;
129 nrefs++; /* NB: we assume caller locking */
130 return ctx;
131 }
132
133 static void
ccmp_detach(struct ieee80211_key * k)134 ccmp_detach(struct ieee80211_key *k)
135 {
136 struct ccmp_ctx *ctx = k->wk_private;
137
138 IEEE80211_FREE(ctx, M_80211_CRYPTO);
139 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
140 nrefs--; /* NB: we assume caller locking */
141 }
142
143 static int
ccmp_get_trailer_len(struct ieee80211_key * k)144 ccmp_get_trailer_len(struct ieee80211_key *k)
145 {
146 return (k->wk_cipher->ic_trailer);
147 }
148
149 static int
ccmp_get_header_len(struct ieee80211_key * k)150 ccmp_get_header_len(struct ieee80211_key *k)
151 {
152 return (k->wk_cipher->ic_header);
153 }
154
155 /**
156 * @brief Return the M parameter to use for CCMP block0 initialisation.
157 *
158 * M is defined as the number of bytes in the authentication
159 * field.
160 *
161 * See RFC3610, Section 2 (CCM Mode Specification) for more
162 * information.
163 *
164 * The MIC size is defined in 802.11-2020 12.5.3
165 * (CTR with CBC-MAC Protocol (CCMP)).
166 *
167 * CCM-128 - M=8, MIC is 8 octets.
168 * CCM-256 - M=16, MIC is 16 octets.
169 *
170 * @param key ieee80211_key to calculate M for
171 * @retval the number of bytes in the authentication field
172 */
173 static int
ccmp_get_ccm_m(struct ieee80211_key * k)174 ccmp_get_ccm_m(struct ieee80211_key *k)
175 {
176 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_AES_CCM)
177 return (8);
178 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_AES_CCM_256)
179 return (16);
180 return (8); /* XXX default */
181 }
182
183 static int
ccmp_setkey(struct ieee80211_key * k)184 ccmp_setkey(struct ieee80211_key *k)
185 {
186 uint32_t keylen;
187 struct ccmp_ctx *ctx = k->wk_private;
188
189 switch (k->wk_cipher->ic_cipher) {
190 case IEEE80211_CIPHER_AES_CCM:
191 keylen = 128;
192 break;
193 case IEEE80211_CIPHER_AES_CCM_256:
194 keylen = 256;
195 break;
196 default:
197 IEEE80211_DPRINTF(ctx->cc_vap, IEEE80211_MSG_CRYPTO,
198 "%s: Unexpected cipher (%u)",
199 __func__, k->wk_cipher->ic_cipher);
200 return (0);
201 }
202
203 if (k->wk_keylen != (keylen/NBBY)) {
204 IEEE80211_DPRINTF(ctx->cc_vap, IEEE80211_MSG_CRYPTO,
205 "%s: Invalid key length %u, expecting %u\n",
206 __func__, k->wk_keylen, keylen/NBBY);
207 return 0;
208 }
209 if (k->wk_flags & IEEE80211_KEY_SWENCRYPT)
210 rijndael_set_key(&ctx->cc_aes, k->wk_key, k->wk_keylen*NBBY);
211 return 1;
212 }
213
214 static void
ccmp_setiv(struct ieee80211_key * k,uint8_t * ivp)215 ccmp_setiv(struct ieee80211_key *k, uint8_t *ivp)
216 {
217 struct ccmp_ctx *ctx = k->wk_private;
218 struct ieee80211vap *vap = ctx->cc_vap;
219 uint8_t keyid;
220
221 keyid = ieee80211_crypto_get_keyid(vap, k) << 6;
222
223 k->wk_keytsc++;
224 ivp[0] = k->wk_keytsc >> 0; /* PN0 */
225 ivp[1] = k->wk_keytsc >> 8; /* PN1 */
226 ivp[2] = 0; /* Reserved */
227 ivp[3] = keyid | IEEE80211_WEP_EXTIV; /* KeyID | ExtID */
228 ivp[4] = k->wk_keytsc >> 16; /* PN2 */
229 ivp[5] = k->wk_keytsc >> 24; /* PN3 */
230 ivp[6] = k->wk_keytsc >> 32; /* PN4 */
231 ivp[7] = k->wk_keytsc >> 40; /* PN5 */
232 }
233
234 /*
235 * Add privacy headers appropriate for the specified key.
236 */
237 static int
ccmp_encap(struct ieee80211_key * k,struct mbuf * m)238 ccmp_encap(struct ieee80211_key *k, struct mbuf *m)
239 {
240 const struct ieee80211_frame *wh;
241 struct ccmp_ctx *ctx = k->wk_private;
242 struct ieee80211com *ic = ctx->cc_ic;
243 uint8_t *ivp;
244 int hdrlen;
245 int is_mgmt;
246
247 hdrlen = ieee80211_hdrspace(ic, mtod(m, void *));
248 wh = mtod(m, const struct ieee80211_frame *);
249 is_mgmt = IEEE80211_IS_MGMT(wh);
250
251 /*
252 * Check to see if we need to insert IV/MIC.
253 *
254 * Some offload devices don't require the IV to be inserted
255 * as part of the hardware encryption.
256 */
257 if (is_mgmt && (k->wk_flags & IEEE80211_KEY_NOIVMGT))
258 return 1;
259 if ((! is_mgmt) && (k->wk_flags & IEEE80211_KEY_NOIV))
260 return 1;
261
262 /*
263 * Copy down 802.11 header and add the IV, KeyID, and ExtIV.
264 */
265 M_PREPEND(m, ccmp_get_header_len(k), IEEE80211_M_NOWAIT);
266 if (m == NULL)
267 return 0;
268 ivp = mtod(m, uint8_t *);
269 ovbcopy(ivp + ccmp_get_header_len(k), ivp, hdrlen);
270 ivp += hdrlen;
271
272 ccmp_setiv(k, ivp);
273
274 /*
275 * Finally, do software encrypt if needed.
276 */
277 if ((k->wk_flags & IEEE80211_KEY_SWENCRYPT) &&
278 !ccmp_encrypt(k, m, hdrlen))
279 return 0;
280
281 return 1;
282 }
283
284 /*
285 * Add MIC to the frame as needed.
286 */
287 static int
ccmp_enmic(struct ieee80211_key * k,struct mbuf * m,int force)288 ccmp_enmic(struct ieee80211_key *k, struct mbuf *m, int force)
289 {
290
291 return 1;
292 }
293
294 static __inline uint64_t
READ_6(uint8_t b0,uint8_t b1,uint8_t b2,uint8_t b3,uint8_t b4,uint8_t b5)295 READ_6(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5)
296 {
297 uint32_t iv32 = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
298 uint16_t iv16 = (b4 << 0) | (b5 << 8);
299 return (((uint64_t)iv16) << 32) | iv32;
300 }
301
302 /*
303 * Validate and strip privacy headers (and trailer) for a
304 * received frame. The specified key should be correct but
305 * is also verified.
306 */
307 static int
ccmp_decap(struct ieee80211_key * k,struct mbuf * m,int hdrlen)308 ccmp_decap(struct ieee80211_key *k, struct mbuf *m, int hdrlen)
309 {
310 const struct ieee80211_rx_stats *rxs;
311 struct ccmp_ctx *ctx = k->wk_private;
312 struct ieee80211vap *vap = ctx->cc_vap;
313 struct ieee80211_frame *wh;
314 uint8_t *ivp, tid;
315 uint64_t pn;
316 bool noreplaycheck;
317
318 rxs = ieee80211_get_rx_params_ptr(m);
319
320 if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) != 0)
321 goto finish;
322
323 /*
324 * Header should have extended IV and sequence number;
325 * verify the former and validate the latter.
326 */
327 wh = mtod(m, struct ieee80211_frame *);
328 ivp = mtod(m, uint8_t *) + hdrlen;
329 if ((ivp[IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV) == 0) {
330 /*
331 * No extended IV; discard frame.
332 */
333 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
334 "%s", "missing ExtIV for AES-CCM cipher");
335 vap->iv_stats.is_rx_ccmpformat++;
336 return 0;
337 }
338 tid = ieee80211_gettid(wh);
339 pn = READ_6(ivp[0], ivp[1], ivp[4], ivp[5], ivp[6], ivp[7]);
340
341 noreplaycheck = (k->wk_flags & IEEE80211_KEY_NOREPLAY) != 0;
342 noreplaycheck |= (rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_PN_VALIDATED) != 0;
343 if (pn <= k->wk_keyrsc[tid] && !noreplaycheck) {
344 /*
345 * Replay violation.
346 */
347 ieee80211_notify_replay_failure(vap, wh, k, pn, tid);
348 vap->iv_stats.is_rx_ccmpreplay++;
349 return 0;
350 }
351
352 /*
353 * Check if the device handled the decrypt in hardware.
354 * If so we just strip the header; otherwise we need to
355 * handle the decrypt in software. Note that for the
356 * latter we leave the header in place for use in the
357 * decryption work.
358 */
359 if ((k->wk_flags & IEEE80211_KEY_SWDECRYPT) &&
360 !ccmp_decrypt(k, pn, m, hdrlen))
361 return 0;
362
363 finish:
364 /*
365 * Copy up 802.11 header and strip crypto bits.
366 */
367 if (! ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP))) {
368 ovbcopy(mtod(m, void *),
369 mtod(m, uint8_t *) + ccmp_get_header_len(k),
370 hdrlen);
371 m_adj(m, ccmp_get_header_len(k));
372 }
373
374 if ((rxs == NULL) || (rxs->c_pktflags & IEEE80211_RX_F_MIC_STRIP) == 0)
375 m_adj(m, -ccmp_get_trailer_len(k));
376
377 /*
378 * Ok to update rsc now.
379 */
380 if ((rxs == NULL) || (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) == 0) {
381 /*
382 * Do not go backwards in the IEEE80211_KEY_NOREPLAY cases
383 * or in case hardware has checked but frames are arriving
384 * reordered (e.g., LinuxKPI drivers doing RSS which we are
385 * not prepared for at all).
386 */
387 if (pn > k->wk_keyrsc[tid])
388 k->wk_keyrsc[tid] = pn;
389 }
390
391 return 1;
392 }
393
394 /*
395 * Verify and strip MIC from the frame.
396 */
397 static int
ccmp_demic(struct ieee80211_key * k,struct mbuf * m,int force)398 ccmp_demic(struct ieee80211_key *k, struct mbuf *m, int force)
399 {
400 return 1;
401 }
402
403 static __inline void
xor_block(uint8_t * b,const uint8_t * a,size_t len)404 xor_block(uint8_t *b, const uint8_t *a, size_t len)
405 {
406 int i;
407 for (i = 0; i < len; i++)
408 b[i] ^= a[i];
409 }
410
411 /**
412 * @brief Initialise the AES-CCM nonce flag field in the b0 CCMP block.
413 *
414 * The B_0 block is defined in RFC 3610 section 2.2 (Authentication).
415 * b0[0] is the CCM flags field, so the nonce used for B_0 starts at
416 * b0[1]. Amusingly, b0[1] is also flags, but it's the 802.11 AES-CCM
417 * nonce flags field, NOT the CCM flags field.
418 *
419 * The AES-CCM nonce flags field is defined in 802.11-2020 12.5.3.3.4
420 * (Construct CCM nonce).
421 *
422 * TODO: net80211 currently doesn't support MFP (management frame protection)
423 * and so bit 4 is never set. This routine and ccmp_init_blocks() will
424 * need a pointer to the ieee80211_node or a flag that explicitly states
425 * the frame will be sent w/ MFP encryption / received w/ MFP decryption.
426 *
427 * @param wh the 802.11 header to populate
428 * @param b0 the CCM nonce to update (remembering b0[0] is the CCM
429 * nonce flags, and b0[1] is the AES-CCM nonce flags).
430 */
431 static void
ieee80211_crypto_ccmp_init_nonce_flags(const struct ieee80211_frame * wh,char * b0)432 ieee80211_crypto_ccmp_init_nonce_flags(const struct ieee80211_frame *wh,
433 char *b0)
434 {
435 if (IEEE80211_IS_DSTODS(wh)) {
436 /*
437 * 802.11-2020 12.5.33.3.4 (Construct CCM nonce) mentions
438 * that the low four bits of this byte are the "MPDU priority."
439 * This is defined in 5.1.1.2 (Determination of UP) and
440 * 5.1.1.3 (Interpretation of Priority Parameter in MAC
441 * service primitives).
442 *
443 * The former says "The QoS facility supports eight priority
444 * values, referred to as UPs. The values a UP may take are
445 * the integer values from 0 to 7 and are identical to the
446 * 802.11D priority tags."
447 *
448 * The latter specifically calls out that "Priority parameter
449 * and TID subfield values 0 to 7 are interpreted aas UPs for
450 * the MSDUs" .. and " .. TID subfield values 8 to 15 specify
451 * TIDs that are TS identifiers (TSIDs)" which are used for
452 * TSPEC. There's a bunch of extra work to be done with frames
453 * received in TIDs 8..15 with no TSPEC, "then the MSDU shall
454 * be sent with priority parameter set to 0."
455 *
456 * All QoS frames (not just QoS data) have TID fields and
457 * thus priorities. However, the code straight up
458 * copies the 4 bit TID field, rather than a 3 bit MPDU
459 * priority value. For now, as net80211 doesn't specifically
460 * support TSPEC negotiation, this likely never gets checked.
461 * However as part of any future TSPEC work, this will likely
462 * need to be looked at and checked with interoperability
463 * with other stacks.
464 */
465 if (IEEE80211_IS_QOS_ANY(wh)) {
466 const struct ieee80211_qosframe_addr4 *qwh4 =
467 (const struct ieee80211_qosframe_addr4 *) wh;
468 b0[1] = qwh4->i_qos[0] & 0x0f; /* prio bits */
469 } else {
470 b0[1] = 0;
471 }
472 } else {
473 if (IEEE80211_IS_QOS_ANY(wh)) {
474 const struct ieee80211_qosframe *qwh =
475 (const struct ieee80211_qosframe *) wh;
476 b0[1] = qwh->i_qos[0] & 0x0f; /* prio bits */
477 } else {
478 b0[1] = 0;
479 }
480 }
481 /* TODO: populate MFP flag */
482 }
483
484 /*
485 * Host AP crypt: host-based CCMP encryption implementation for Host AP driver
486 *
487 * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
488 *
489 * This program is free software; you can redistribute it and/or modify
490 * it under the terms of the GNU General Public License version 2 as
491 * published by the Free Software Foundation. See README and COPYING for
492 * more details.
493 *
494 * Alternatively, this software may be distributed under the terms of BSD
495 * license.
496 */
497
498 static void
ccmp_init_blocks(rijndael_ctx * ctx,struct ieee80211_frame * wh,uint32_t m,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])499 ccmp_init_blocks(rijndael_ctx *ctx, struct ieee80211_frame *wh,
500 uint32_t m, u_int64_t pn, size_t dlen,
501 uint8_t b0[AES_BLOCK_LEN], uint8_t aad[2 * AES_BLOCK_LEN],
502 uint8_t auth[AES_BLOCK_LEN], uint8_t s0[AES_BLOCK_LEN])
503 {
504 /*
505 * Map M parameter to encoding
506 * RFC3610, Section 2 (CCM Mode Specification)
507 */
508 m = (m - 2) / 2;
509
510 /* CCM Initial Block:
511 *
512 * Flag (Include authentication header,
513 * M=3 or 7 (8 or 16 octet auth field),
514 * L=1 (2-octet Dlen))
515 * Adata=1 (one or more auth blocks present)
516 * Nonce: 0x00 | A2 | PN
517 * Dlen
518 */
519 b0[0] = 0x40 | 0x01 | (m << 3);
520 /* Init b0[1] (CCM nonce flags) */
521 ieee80211_crypto_ccmp_init_nonce_flags(wh, b0);
522 IEEE80211_ADDR_COPY(b0 + 2, wh->i_addr2);
523 b0[8] = pn >> 40;
524 b0[9] = pn >> 32;
525 b0[10] = pn >> 24;
526 b0[11] = pn >> 16;
527 b0[12] = pn >> 8;
528 b0[13] = pn >> 0;
529 b0[14] = (dlen >> 8) & 0xff;
530 b0[15] = dlen & 0xff;
531
532 /* Init AAD */
533 (void) ieee80211_crypto_init_aad(wh, aad, 2 * AES_BLOCK_LEN);
534
535 /* Start with the first block and AAD */
536 rijndael_encrypt(ctx, b0, auth);
537 xor_block(auth, aad, AES_BLOCK_LEN);
538 rijndael_encrypt(ctx, auth, auth);
539 xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
540 rijndael_encrypt(ctx, auth, auth);
541 b0[0] &= 0x07;
542 b0[14] = b0[15] = 0;
543 rijndael_encrypt(ctx, b0, s0);
544 }
545
546 #define CCMP_ENCRYPT(_i, _b, _b0, _pos, _e, _len) do { \
547 /* Authentication */ \
548 xor_block(_b, _pos, _len); \
549 rijndael_encrypt(&ctx->cc_aes, _b, _b); \
550 /* Encryption, with counter */ \
551 _b0[14] = (_i >> 8) & 0xff; \
552 _b0[15] = _i & 0xff; \
553 rijndael_encrypt(&ctx->cc_aes, _b0, _e); \
554 xor_block(_pos, _e, _len); \
555 } while (0)
556
557 static int
ccmp_encrypt(struct ieee80211_key * key,struct mbuf * m0,int hdrlen)558 ccmp_encrypt(struct ieee80211_key *key, struct mbuf *m0, int hdrlen)
559 {
560 struct ccmp_ctx *ctx = key->wk_private;
561 struct ieee80211_frame *wh;
562 struct mbuf *m = m0;
563 int data_len, i, space;
564 uint8_t aad[2 * AES_BLOCK_LEN], b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN],
565 e[AES_BLOCK_LEN], s0[AES_BLOCK_LEN];
566 uint8_t *pos;
567
568 ctx->cc_vap->iv_stats.is_crypto_ccmp++;
569
570 wh = mtod(m, struct ieee80211_frame *);
571 data_len = m->m_pkthdr.len - (hdrlen + ccmp_get_header_len(key));
572 ccmp_init_blocks(&ctx->cc_aes, wh, ccmp_get_ccm_m(key),
573 key->wk_keytsc, data_len, b0, aad, b, s0);
574
575 i = 1;
576 pos = mtod(m, uint8_t *) + hdrlen + ccmp_get_header_len(key);
577 /* NB: assumes header is entirely in first mbuf */
578 space = m->m_len - (hdrlen + ccmp_get_header_len(key));
579 for (;;) {
580 if (space > data_len)
581 space = data_len;
582 /*
583 * Do full blocks.
584 */
585 while (space >= AES_BLOCK_LEN) {
586 CCMP_ENCRYPT(i, b, b0, pos, e, AES_BLOCK_LEN);
587 pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
588 data_len -= AES_BLOCK_LEN;
589 i++;
590 }
591 if (data_len <= 0) /* no more data */
592 break;
593 m = m->m_next;
594 if (m == NULL) { /* last buffer */
595 if (space != 0) {
596 /*
597 * Short last block.
598 */
599 CCMP_ENCRYPT(i, b, b0, pos, e, space);
600 }
601 break;
602 }
603 if (space != 0) {
604 uint8_t *pos_next;
605 int space_next;
606 int len, dl, sp;
607 struct mbuf *n;
608
609 /*
610 * Block straddles one or more mbufs, gather data
611 * into the block buffer b, apply the cipher, then
612 * scatter the results back into the mbuf chain.
613 * The buffer will automatically get space bytes
614 * of data at offset 0 copied in+out by the
615 * CCMP_ENCRYPT request so we must take care of
616 * the remaining data.
617 */
618 n = m;
619 dl = data_len;
620 sp = space;
621 for (;;) {
622 pos_next = mtod(n, uint8_t *);
623 len = min(dl, AES_BLOCK_LEN);
624 space_next = len > sp ? len - sp : 0;
625 if (n->m_len >= space_next) {
626 /*
627 * This mbuf has enough data; just grab
628 * what we need and stop.
629 */
630 xor_block(b+sp, pos_next, space_next);
631 break;
632 }
633 /*
634 * This mbuf's contents are insufficient,
635 * take 'em all and prepare to advance to
636 * the next mbuf.
637 */
638 xor_block(b+sp, pos_next, n->m_len);
639 sp += n->m_len, dl -= n->m_len;
640 n = n->m_next;
641 if (n == NULL)
642 break;
643 }
644
645 CCMP_ENCRYPT(i, b, b0, pos, e, space);
646
647 /* NB: just like above, but scatter data to mbufs */
648 dl = data_len;
649 sp = space;
650 for (;;) {
651 pos_next = mtod(m, uint8_t *);
652 len = min(dl, AES_BLOCK_LEN);
653 space_next = len > sp ? len - sp : 0;
654 if (m->m_len >= space_next) {
655 xor_block(pos_next, e+sp, space_next);
656 break;
657 }
658 xor_block(pos_next, e+sp, m->m_len);
659 sp += m->m_len, dl -= m->m_len;
660 m = m->m_next;
661 if (m == NULL)
662 goto done;
663 }
664 /*
665 * Do bookkeeping. m now points to the last mbuf
666 * we grabbed data from. We know we consumed a
667 * full block of data as otherwise we'd have hit
668 * the end of the mbuf chain, so deduct from data_len.
669 * Otherwise advance the block number (i) and setup
670 * pos+space to reflect contents of the new mbuf.
671 */
672 data_len -= AES_BLOCK_LEN;
673 i++;
674 pos = pos_next + space_next;
675 space = m->m_len - space_next;
676 } else {
677 /*
678 * Setup for next buffer.
679 */
680 pos = mtod(m, uint8_t *);
681 space = m->m_len;
682 }
683 }
684 done:
685 /* tack on MIC */
686 xor_block(b, s0, ccmp_get_trailer_len(key));
687 return m_append(m0, ccmp_get_trailer_len(key), b);
688 }
689 #undef CCMP_ENCRYPT
690
691 #define CCMP_DECRYPT(_i, _b, _b0, _pos, _a, _len) do { \
692 /* Decrypt, with counter */ \
693 _b0[14] = (_i >> 8) & 0xff; \
694 _b0[15] = _i & 0xff; \
695 rijndael_encrypt(&ctx->cc_aes, _b0, _b); \
696 xor_block(_pos, _b, _len); \
697 /* Authentication */ \
698 xor_block(_a, _pos, _len); \
699 rijndael_encrypt(&ctx->cc_aes, _a, _a); \
700 } while (0)
701
702 static int
ccmp_decrypt(struct ieee80211_key * key,u_int64_t pn,struct mbuf * m,int hdrlen)703 ccmp_decrypt(struct ieee80211_key *key, u_int64_t pn, struct mbuf *m, int hdrlen)
704 {
705 const struct ieee80211_rx_stats *rxs;
706 struct ccmp_ctx *ctx = key->wk_private;
707 struct ieee80211vap *vap = ctx->cc_vap;
708 struct ieee80211_frame *wh;
709 uint8_t aad[2 * AES_BLOCK_LEN];
710 uint8_t b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN], a[AES_BLOCK_LEN];
711 uint8_t mic[AES_BLOCK_LEN];
712 size_t data_len;
713 int i;
714 uint8_t *pos;
715 u_int space;
716
717 rxs = ieee80211_get_rx_params_ptr(m);
718 if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED) != 0)
719 return (1);
720
721 ctx->cc_vap->iv_stats.is_crypto_ccmp++;
722
723 wh = mtod(m, struct ieee80211_frame *);
724 data_len = m->m_pkthdr.len -
725 (hdrlen + ccmp_get_header_len(key) + ccmp_get_trailer_len(key));
726 ccmp_init_blocks(&ctx->cc_aes, wh, ccmp_get_ccm_m(key), pn,
727 data_len, b0, aad, a, b);
728 m_copydata(m, m->m_pkthdr.len - ccmp_get_trailer_len(key),
729 ccmp_get_trailer_len(key), mic);
730 xor_block(mic, b, ccmp_get_trailer_len(key));
731
732 i = 1;
733 pos = mtod(m, uint8_t *) + hdrlen + ccmp_get_header_len(key);
734 space = m->m_len - (hdrlen + ccmp_get_header_len(key));
735 for (;;) {
736 if (space > data_len)
737 space = data_len;
738 while (space >= AES_BLOCK_LEN) {
739 CCMP_DECRYPT(i, b, b0, pos, a, AES_BLOCK_LEN);
740 pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
741 data_len -= AES_BLOCK_LEN;
742 i++;
743 }
744 if (data_len <= 0) /* no more data */
745 break;
746 m = m->m_next;
747 if (m == NULL) { /* last buffer */
748 if (space != 0) /* short last block */
749 CCMP_DECRYPT(i, b, b0, pos, a, space);
750 break;
751 }
752 if (space != 0) {
753 uint8_t *pos_next;
754 u_int space_next;
755 u_int len;
756
757 /*
758 * Block straddles buffers, split references. We
759 * do not handle splits that require >2 buffers
760 * since rx'd frames are never badly fragmented
761 * because drivers typically recv in clusters.
762 */
763 pos_next = mtod(m, uint8_t *);
764 len = min(data_len, AES_BLOCK_LEN);
765 space_next = len > space ? len - space : 0;
766 KASSERT(m->m_len >= space_next,
767 ("not enough data in following buffer, "
768 "m_len %u need %u\n", m->m_len, space_next));
769
770 xor_block(b+space, pos_next, space_next);
771 CCMP_DECRYPT(i, b, b0, pos, a, space);
772 xor_block(pos_next, b+space, space_next);
773 data_len -= len;
774 i++;
775
776 pos = pos_next + space_next;
777 space = m->m_len - space_next;
778 } else {
779 /*
780 * Setup for next buffer.
781 */
782 pos = mtod(m, uint8_t *);
783 space = m->m_len;
784 }
785 }
786
787 /*
788 * If the MIC was stripped by HW/driver we are done.
789 */
790 if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_MIC_STRIP) != 0)
791 return (1);
792
793 if (memcmp(mic, a, ccmp_get_trailer_len(key)) != 0) {
794 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
795 "%s", "AES-CCM decrypt failed; MIC mismatch");
796 vap->iv_stats.is_rx_ccmpmic++;
797 return 0;
798 }
799 return 1;
800 }
801 #undef CCMP_DECRYPT
802
803 /*
804 * Module glue.
805 */
806 IEEE80211_CRYPTO_MODULE(ccmp, 1);
807 IEEE80211_CRYPTO_MODULE_ADD(ccmp_256);
808