2 * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * IEEE 802.11i AES-CCMP crypto support.
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.
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/socket.h>
46 #include <net/if_media.h>
47 #include <net/ethernet.h>
49 #include <net80211/ieee80211_var.h>
51 #include <crypto/rijndael/rijndael.h>
53 #define AES_BLOCK_LEN 16
56 struct ieee80211com *cc_ic; /* for diagnostics */
60 static void *ccmp_attach(struct ieee80211com *, struct ieee80211_key *);
61 static void ccmp_detach(struct ieee80211_key *);
62 static int ccmp_setkey(struct ieee80211_key *);
63 static int ccmp_encap(struct ieee80211_key *k, struct mbuf *, uint8_t keyid);
64 static int ccmp_decap(struct ieee80211_key *, struct mbuf *, int);
65 static int ccmp_enmic(struct ieee80211_key *, struct mbuf *, int);
66 static int ccmp_demic(struct ieee80211_key *, struct mbuf *, int);
68 static const struct ieee80211_cipher ccmp = {
70 .ic_cipher = IEEE80211_CIPHER_AES_CCM,
71 .ic_header = IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
72 IEEE80211_WEP_EXTIVLEN,
73 .ic_trailer = IEEE80211_WEP_MICLEN,
75 .ic_attach = ccmp_attach,
76 .ic_detach = ccmp_detach,
77 .ic_setkey = ccmp_setkey,
78 .ic_encap = ccmp_encap,
79 .ic_decap = ccmp_decap,
80 .ic_enmic = ccmp_enmic,
81 .ic_demic = ccmp_demic,
84 static int ccmp_encrypt(struct ieee80211_key *, struct mbuf *, int hdrlen);
85 static int ccmp_decrypt(struct ieee80211_key *, u_int64_t pn,
86 struct mbuf *, int hdrlen);
88 /* number of references from net80211 layer */
92 ccmp_attach(struct ieee80211com *ic, struct ieee80211_key *k)
96 MALLOC(ctx, struct ccmp_ctx *, sizeof(struct ccmp_ctx),
97 M_DEVBUF, M_NOWAIT | M_ZERO);
99 ic->ic_stats.is_crypto_nomem++;
103 nrefs++; /* NB: we assume caller locking */
108 ccmp_detach(struct ieee80211_key *k)
110 struct ccmp_ctx *ctx = k->wk_private;
113 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
114 nrefs--; /* NB: we assume caller locking */
118 ccmp_setkey(struct ieee80211_key *k)
120 struct ccmp_ctx *ctx = k->wk_private;
122 if (k->wk_keylen != (128/NBBY)) {
123 IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
124 "%s: Invalid key length %u, expecting %u\n",
125 __func__, k->wk_keylen, 128/NBBY);
128 if (k->wk_flags & IEEE80211_KEY_SWCRYPT)
129 rijndael_set_key(&ctx->cc_aes, k->wk_key, k->wk_keylen*NBBY);
134 * Add privacy headers appropriate for the specified key.
137 ccmp_encap(struct ieee80211_key *k, struct mbuf *m, uint8_t keyid)
139 struct ccmp_ctx *ctx = k->wk_private;
140 struct ieee80211com *ic = ctx->cc_ic;
144 hdrlen = ieee80211_hdrspace(ic, mtod(m, void *));
147 * Copy down 802.11 header and add the IV, KeyID, and ExtIV.
149 M_PREPEND(m, ccmp.ic_header, M_NOWAIT);
152 ivp = mtod(m, uint8_t *);
153 ovbcopy(ivp + ccmp.ic_header, ivp, hdrlen);
156 k->wk_keytsc++; /* XXX wrap at 48 bits */
157 ivp[0] = k->wk_keytsc >> 0; /* PN0 */
158 ivp[1] = k->wk_keytsc >> 8; /* PN1 */
159 ivp[2] = 0; /* Reserved */
160 ivp[3] = keyid | IEEE80211_WEP_EXTIV; /* KeyID | ExtID */
161 ivp[4] = k->wk_keytsc >> 16; /* PN2 */
162 ivp[5] = k->wk_keytsc >> 24; /* PN3 */
163 ivp[6] = k->wk_keytsc >> 32; /* PN4 */
164 ivp[7] = k->wk_keytsc >> 40; /* PN5 */
167 * Finally, do software encrypt if neeed.
169 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
170 !ccmp_encrypt(k, m, hdrlen))
177 * Add MIC to the frame as needed.
180 ccmp_enmic(struct ieee80211_key *k, struct mbuf *m, int force)
186 static __inline uint64_t
187 READ_6(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5)
189 uint32_t iv32 = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
190 uint16_t iv16 = (b4 << 0) | (b5 << 8);
191 return (((uint64_t)iv16) << 32) | iv32;
195 * Validate and strip privacy headers (and trailer) for a
196 * received frame. The specified key should be correct but
200 ccmp_decap(struct ieee80211_key *k, struct mbuf *m, int hdrlen)
202 struct ccmp_ctx *ctx = k->wk_private;
203 struct ieee80211_frame *wh;
208 * Header should have extended IV and sequence number;
209 * verify the former and validate the latter.
211 wh = mtod(m, struct ieee80211_frame *);
212 ivp = mtod(m, uint8_t *) + hdrlen;
213 if ((ivp[IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV) == 0) {
215 * No extended IV; discard frame.
217 IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
218 "[%s] Missing ExtIV for AES-CCM cipher\n",
219 ether_sprintf(wh->i_addr2));
220 ctx->cc_ic->ic_stats.is_rx_ccmpformat++;
223 pn = READ_6(ivp[0], ivp[1], ivp[4], ivp[5], ivp[6], ivp[7]);
224 if (pn <= k->wk_keyrsc) {
228 ieee80211_notify_replay_failure(ctx->cc_ic, wh, k, pn);
229 ctx->cc_ic->ic_stats.is_rx_ccmpreplay++;
234 * Check if the device handled the decrypt in hardware.
235 * If so we just strip the header; otherwise we need to
236 * handle the decrypt in software. Note that for the
237 * latter we leave the header in place for use in the
240 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
241 !ccmp_decrypt(k, pn, m, hdrlen))
245 * Copy up 802.11 header and strip crypto bits.
247 ovbcopy(mtod(m, void *), mtod(m, uint8_t *) + ccmp.ic_header, hdrlen);
248 m_adj(m, ccmp.ic_header);
249 m_adj(m, -ccmp.ic_trailer);
252 * Ok to update rsc now.
260 * Verify and strip MIC from the frame.
263 ccmp_demic(struct ieee80211_key *k, struct mbuf *m, int force)
269 xor_block(uint8_t *b, const uint8_t *a, size_t len)
272 for (i = 0; i < len; i++)
277 * Host AP crypt: host-based CCMP encryption implementation for Host AP driver
279 * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
281 * This program is free software; you can redistribute it and/or modify
282 * it under the terms of the GNU General Public License version 2 as
283 * published by the Free Software Foundation. See README and COPYING for
286 * Alternatively, this software may be distributed under the terms of BSD
291 ccmp_init_blocks(rijndael_ctx *ctx, struct ieee80211_frame *wh,
292 u_int64_t pn, size_t dlen,
293 uint8_t b0[AES_BLOCK_LEN], uint8_t aad[2 * AES_BLOCK_LEN],
294 uint8_t auth[AES_BLOCK_LEN], uint8_t s0[AES_BLOCK_LEN])
296 #define IS_4ADDRESS(wh) \
297 ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
298 #define IS_QOS_DATA(wh) IEEE80211_QOS_HAS_SEQ(wh)
300 /* CCM Initial Block:
301 * Flag (Include authentication header, M=3 (8-octet MIC),
302 * L=1 (2-octet Dlen))
303 * Nonce: 0x00 | A2 | PN
306 /* NB: b0[1] set below */
307 IEEE80211_ADDR_COPY(b0 + 2, wh->i_addr2);
314 b0[14] = (dlen >> 8) & 0xff;
315 b0[15] = dlen & 0xff;
318 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
320 * SC with bits 4..15 (seq#) masked to zero
324 aad[0] = 0; /* AAD length >> 8 */
325 /* NB: aad[1] set below */
326 aad[2] = wh->i_fc[0] & 0x8f; /* XXX magic #s */
327 aad[3] = wh->i_fc[1] & 0xc7; /* XXX magic #s */
328 /* NB: we know 3 addresses are contiguous */
329 memcpy(aad + 4, wh->i_addr1, 3 * IEEE80211_ADDR_LEN);
330 aad[22] = wh->i_seq[0] & IEEE80211_SEQ_FRAG_MASK;
331 aad[23] = 0; /* all bits masked */
333 * Construct variable-length portion of AAD based
334 * on whether this is a 4-address frame/QOS frame.
335 * We always zero-pad to 32 bytes before running it
336 * through the cipher.
338 * We also fill in the priority bits of the CCM
339 * initial block as we know whether or not we have
342 if (IS_4ADDRESS(wh)) {
343 IEEE80211_ADDR_COPY(aad + 24,
344 ((struct ieee80211_frame_addr4 *)wh)->i_addr4);
345 if (IS_QOS_DATA(wh)) {
346 struct ieee80211_qosframe_addr4 *qwh4 =
347 (struct ieee80211_qosframe_addr4 *) wh;
348 aad[30] = qwh4->i_qos[0] & 0x0f;/* just priority bits */
351 aad[1] = 22 + IEEE80211_ADDR_LEN + 2;
353 *(uint16_t *)&aad[30] = 0;
355 aad[1] = 22 + IEEE80211_ADDR_LEN;
358 if (IS_QOS_DATA(wh)) {
359 struct ieee80211_qosframe *qwh =
360 (struct ieee80211_qosframe*) wh;
361 aad[24] = qwh->i_qos[0] & 0x0f; /* just priority bits */
366 *(uint16_t *)&aad[24] = 0;
370 *(uint16_t *)&aad[26] = 0;
371 *(uint32_t *)&aad[28] = 0;
374 /* Start with the first block and AAD */
375 rijndael_encrypt(ctx, b0, auth);
376 xor_block(auth, aad, AES_BLOCK_LEN);
377 rijndael_encrypt(ctx, auth, auth);
378 xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
379 rijndael_encrypt(ctx, auth, auth);
382 rijndael_encrypt(ctx, b0, s0);
387 #define CCMP_ENCRYPT(_i, _b, _b0, _pos, _e, _len) do { \
388 /* Authentication */ \
389 xor_block(_b, _pos, _len); \
390 rijndael_encrypt(&ctx->cc_aes, _b, _b); \
391 /* Encryption, with counter */ \
392 _b0[14] = (_i >> 8) & 0xff; \
393 _b0[15] = _i & 0xff; \
394 rijndael_encrypt(&ctx->cc_aes, _b0, _e); \
395 xor_block(_pos, _e, _len); \
399 ccmp_encrypt(struct ieee80211_key *key, struct mbuf *m0, int hdrlen)
401 struct ccmp_ctx *ctx = key->wk_private;
402 struct ieee80211_frame *wh;
404 int data_len, i, space;
405 uint8_t aad[2 * AES_BLOCK_LEN], b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN],
406 e[AES_BLOCK_LEN], s0[AES_BLOCK_LEN];
409 ctx->cc_ic->ic_stats.is_crypto_ccmp++;
411 wh = mtod(m, struct ieee80211_frame *);
412 data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header);
413 ccmp_init_blocks(&ctx->cc_aes, wh, key->wk_keytsc,
414 data_len, b0, aad, b, s0);
417 pos = mtod(m, uint8_t *) + hdrlen + ccmp.ic_header;
418 /* NB: assumes header is entirely in first mbuf */
419 space = m->m_len - (hdrlen + ccmp.ic_header);
421 if (space > data_len)
426 while (space >= AES_BLOCK_LEN) {
427 CCMP_ENCRYPT(i, b, b0, pos, e, AES_BLOCK_LEN);
428 pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
429 data_len -= AES_BLOCK_LEN;
432 if (data_len <= 0) /* no more data */
435 if (m == NULL) { /* last buffer */
440 CCMP_ENCRYPT(i, b, b0, pos, e, space);
451 * Block straddles one or more mbufs, gather data
452 * into the block buffer b, apply the cipher, then
453 * scatter the results back into the mbuf chain.
454 * The buffer will automatically get space bytes
455 * of data at offset 0 copied in+out by the
456 * CCMP_ENCRYPT request so we must take care of
457 * the remaining data.
463 pos_next = mtod(n, uint8_t *);
464 len = min(dl, AES_BLOCK_LEN);
465 space_next = len > sp ? len - sp : 0;
466 if (n->m_len >= space_next) {
468 * This mbuf has enough data; just grab
469 * what we need and stop.
471 xor_block(b+sp, pos_next, space_next);
475 * This mbuf's contents are insufficient,
476 * take 'em all and prepare to advance to
479 xor_block(b+sp, pos_next, n->m_len);
480 sp += n->m_len, dl -= n->m_len;
486 CCMP_ENCRYPT(i, b, b0, pos, e, space);
488 /* NB: just like above, but scatter data to mbufs */
492 pos_next = mtod(m, uint8_t *);
493 len = min(dl, AES_BLOCK_LEN);
494 space_next = len > sp ? len - sp : 0;
495 if (m->m_len >= space_next) {
496 xor_block(pos_next, e+sp, space_next);
499 xor_block(pos_next, e+sp, m->m_len);
500 sp += m->m_len, dl -= m->m_len;
506 * Do bookkeeping. m now points to the last mbuf
507 * we grabbed data from. We know we consumed a
508 * full block of data as otherwise we'd have hit
509 * the end of the mbuf chain, so deduct from data_len.
510 * Otherwise advance the block number (i) and setup
511 * pos+space to reflect contents of the new mbuf.
513 data_len -= AES_BLOCK_LEN;
515 pos = pos_next + space_next;
516 space = m->m_len - space_next;
519 * Setup for next buffer.
521 pos = mtod(m, uint8_t *);
527 xor_block(b, s0, ccmp.ic_trailer);
528 return m_append(m0, ccmp.ic_trailer, b);
532 #define CCMP_DECRYPT(_i, _b, _b0, _pos, _a, _len) do { \
533 /* Decrypt, with counter */ \
534 _b0[14] = (_i >> 8) & 0xff; \
535 _b0[15] = _i & 0xff; \
536 rijndael_encrypt(&ctx->cc_aes, _b0, _b); \
537 xor_block(_pos, _b, _len); \
538 /* Authentication */ \
539 xor_block(_a, _pos, _len); \
540 rijndael_encrypt(&ctx->cc_aes, _a, _a); \
544 ccmp_decrypt(struct ieee80211_key *key, u_int64_t pn, struct mbuf *m, int hdrlen)
546 struct ccmp_ctx *ctx = key->wk_private;
547 struct ieee80211_frame *wh;
548 uint8_t aad[2 * AES_BLOCK_LEN];
549 uint8_t b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN], a[AES_BLOCK_LEN];
550 uint8_t mic[AES_BLOCK_LEN];
556 ctx->cc_ic->ic_stats.is_crypto_ccmp++;
558 wh = mtod(m, struct ieee80211_frame *);
559 data_len = m->m_pkthdr.len - (hdrlen + ccmp.ic_header + ccmp.ic_trailer);
560 ccmp_init_blocks(&ctx->cc_aes, wh, pn, data_len, b0, aad, a, b);
561 m_copydata(m, m->m_pkthdr.len - ccmp.ic_trailer, ccmp.ic_trailer, mic);
562 xor_block(mic, b, ccmp.ic_trailer);
565 pos = mtod(m, uint8_t *) + hdrlen + ccmp.ic_header;
566 space = m->m_len - (hdrlen + ccmp.ic_header);
568 if (space > data_len)
570 while (space >= AES_BLOCK_LEN) {
571 CCMP_DECRYPT(i, b, b0, pos, a, AES_BLOCK_LEN);
572 pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
573 data_len -= AES_BLOCK_LEN;
576 if (data_len <= 0) /* no more data */
579 if (m == NULL) { /* last buffer */
580 if (space != 0) /* short last block */
581 CCMP_DECRYPT(i, b, b0, pos, a, space);
590 * Block straddles buffers, split references. We
591 * do not handle splits that require >2 buffers
592 * since rx'd frames are never badly fragmented
593 * because drivers typically recv in clusters.
595 pos_next = mtod(m, uint8_t *);
596 len = min(data_len, AES_BLOCK_LEN);
597 space_next = len > space ? len - space : 0;
598 KASSERT(m->m_len >= space_next,
599 ("not enough data in following buffer, "
600 "m_len %u need %u\n", m->m_len, space_next));
602 xor_block(b+space, pos_next, space_next);
603 CCMP_DECRYPT(i, b, b0, pos, a, space);
604 xor_block(pos_next, b+space, space_next);
608 pos = pos_next + space_next;
609 space = m->m_len - space_next;
612 * Setup for next buffer.
614 pos = mtod(m, uint8_t *);
618 if (memcmp(mic, a, ccmp.ic_trailer) != 0) {
619 IEEE80211_DPRINTF(ctx->cc_ic, IEEE80211_MSG_CRYPTO,
620 "[%s] AES-CCM decrypt failed; MIC mismatch\n",
621 ether_sprintf(wh->i_addr2));
622 ctx->cc_ic->ic_stats.is_rx_ccmpmic++;
632 IEEE80211_CRYPTO_MODULE(ccmp, 1);