2 * Copyright (c) 2002-2005 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.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * Alternatively, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL") version 2 as published by the Free
18 * Software Foundation.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_crypto_ccmp.c,v 1.7 2005/07/11 03:06:23 sam Exp $");
37 __KERNEL_RCSID(0, "$NetBSD: ieee80211_crypto_ccmp.c,v 1.7 2006/11/16 01:33:40 christos Exp $");
41 * IEEE 802.11i AES-CCMP crypto support.
43 * Part of this module is derived from similar code in the Host
44 * AP driver. The code is used with the consent of the author and
45 * it's license is included below.
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
53 #include <sys/socket.h>
56 #include <net/if_ether.h>
57 #include <net/if_media.h>
59 #include <net80211/ieee80211_var.h>
61 #include <crypto/rijndael/rijndael.h>
63 #define AES_BLOCK_LEN 16
66 struct ieee80211com
*cc_ic
; /* for diagnostics */
70 static void *ccmp_attach(struct ieee80211com
*, struct ieee80211_key
*);
71 static void ccmp_detach(struct ieee80211_key
*);
72 static int ccmp_setkey(struct ieee80211_key
*);
73 static int ccmp_encap(struct ieee80211_key
*k
, struct mbuf
*, u_int8_t keyid
);
74 static int ccmp_decap(struct ieee80211_key
*, struct mbuf
*, int);
75 static int ccmp_enmic(struct ieee80211_key
*, struct mbuf
*, int);
76 static int ccmp_demic(struct ieee80211_key
*, struct mbuf
*, int);
78 const struct ieee80211_cipher ieee80211_cipher_ccmp
= {
80 .ic_cipher
= IEEE80211_CIPHER_AES_CCM
,
81 .ic_header
= IEEE80211_WEP_IVLEN
+ IEEE80211_WEP_KIDLEN
+
82 IEEE80211_WEP_EXTIVLEN
,
83 .ic_trailer
= IEEE80211_WEP_MICLEN
,
85 .ic_attach
= ccmp_attach
,
86 .ic_detach
= ccmp_detach
,
87 .ic_setkey
= ccmp_setkey
,
88 .ic_encap
= ccmp_encap
,
89 .ic_decap
= ccmp_decap
,
90 .ic_enmic
= ccmp_enmic
,
91 .ic_demic
= ccmp_demic
,
94 #define ccmp ieee80211_cipher_ccmp
96 static int ccmp_encrypt(struct ieee80211_key
*, struct mbuf
*, int hdrlen
);
97 static int ccmp_decrypt(struct ieee80211_key
*, u_int64_t pn
,
98 struct mbuf
*, int hdrlen
);
101 ccmp_attach(struct ieee80211com
*ic
, struct ieee80211_key
*k
)
103 struct ccmp_ctx
*ctx
;
105 ctx
= malloc(sizeof(struct ccmp_ctx
),
106 M_DEVBUF
, M_NOWAIT
| M_ZERO
);
108 ic
->ic_stats
.is_crypto_nomem
++;
116 ccmp_detach(struct ieee80211_key
*k
)
118 struct ccmp_ctx
*ctx
= k
->wk_private
;
124 ccmp_setkey(struct ieee80211_key
*k
)
126 struct ccmp_ctx
*ctx
= k
->wk_private
;
128 if (k
->wk_keylen
!= (128/NBBY
)) {
129 IEEE80211_DPRINTF(ctx
->cc_ic
, IEEE80211_MSG_CRYPTO
,
130 "%s: Invalid key length %u, expecting %u\n",
131 __func__
, k
->wk_keylen
, 128/NBBY
);
134 if (k
->wk_flags
& IEEE80211_KEY_SWCRYPT
)
135 rijndael_set_key(&ctx
->cc_aes
, k
->wk_key
, k
->wk_keylen
*NBBY
);
140 * Add privacy headers appropriate for the specified key.
143 ccmp_encap(struct ieee80211_key
*k
, struct mbuf
*m
, u_int8_t keyid
)
145 struct ccmp_ctx
*ctx
= k
->wk_private
;
146 struct ieee80211com
*ic
= ctx
->cc_ic
;
150 hdrlen
= ieee80211_hdrspace(ic
, mtod(m
, void *));
153 * Copy down 802.11 header and add the IV, KeyID, and ExtIV.
155 M_PREPEND(m
, ccmp
.ic_header
, M_NOWAIT
);
158 ivp
= mtod(m
, u_int8_t
*);
159 ovbcopy(ivp
+ ccmp
.ic_header
, ivp
, hdrlen
);
162 k
->wk_keytsc
++; /* XXX wrap at 48 bits */
163 ivp
[0] = k
->wk_keytsc
>> 0; /* PN0 */
164 ivp
[1] = k
->wk_keytsc
>> 8; /* PN1 */
165 ivp
[2] = 0; /* Reserved */
166 ivp
[3] = keyid
| IEEE80211_WEP_EXTIV
; /* KeyID | ExtID */
167 ivp
[4] = k
->wk_keytsc
>> 16; /* PN2 */
168 ivp
[5] = k
->wk_keytsc
>> 24; /* PN3 */
169 ivp
[6] = k
->wk_keytsc
>> 32; /* PN4 */
170 ivp
[7] = k
->wk_keytsc
>> 40; /* PN5 */
173 * Finally, do software encrypt if neeed.
175 if ((k
->wk_flags
& IEEE80211_KEY_SWCRYPT
) &&
176 !ccmp_encrypt(k
, m
, hdrlen
))
183 * Add MIC to the frame as needed.
186 ccmp_enmic(struct ieee80211_key
*k
, struct mbuf
*m
,
193 static __inline
uint64_t
194 READ_6(uint8_t b0
, uint8_t b1
, uint8_t b2
, uint8_t b3
, uint8_t b4
, uint8_t b5
)
196 uint32_t iv32
= (b0
<< 0) | (b1
<< 8) | (b2
<< 16) | (b3
<< 24);
197 uint16_t iv16
= (b4
<< 0) | (b5
<< 8);
198 return (((uint64_t)iv16
) << 32) | iv32
;
202 * Validate and strip privacy headers (and trailer) for a
203 * received frame. The specified key should be correct but
207 ccmp_decap(struct ieee80211_key
*k
, struct mbuf
*m
, int hdrlen
)
209 struct ccmp_ctx
*ctx
= k
->wk_private
;
210 struct ieee80211_frame
*wh
;
215 * Header should have extended IV and sequence number;
216 * verify the former and validate the latter.
218 wh
= mtod(m
, struct ieee80211_frame
*);
219 ivp
= mtod(m
, uint8_t *) + hdrlen
;
220 if ((ivp
[IEEE80211_WEP_IVLEN
] & IEEE80211_WEP_EXTIV
) == 0) {
222 * No extended IV; discard frame.
224 IEEE80211_DPRINTF(ctx
->cc_ic
, IEEE80211_MSG_CRYPTO
,
225 "[%s] Missing ExtIV for AES-CCM cipher\n",
226 ether_sprintf(wh
->i_addr2
));
227 ctx
->cc_ic
->ic_stats
.is_rx_ccmpformat
++;
230 pn
= READ_6(ivp
[0], ivp
[1], ivp
[4], ivp
[5], ivp
[6], ivp
[7]);
231 if (pn
<= k
->wk_keyrsc
) {
235 ieee80211_notify_replay_failure(ctx
->cc_ic
, wh
, k
, pn
);
236 ctx
->cc_ic
->ic_stats
.is_rx_ccmpreplay
++;
241 * Check if the device handled the decrypt in hardware.
242 * If so we just strip the header; otherwise we need to
243 * handle the decrypt in software. Note that for the
244 * latter we leave the header in place for use in the
247 if ((k
->wk_flags
& IEEE80211_KEY_SWCRYPT
) &&
248 !ccmp_decrypt(k
, pn
, m
, hdrlen
))
252 * Copy up 802.11 header and strip crypto bits.
254 ovbcopy(mtod(m
, void *), mtod(m
, u_int8_t
*) + ccmp
.ic_header
, hdrlen
);
255 m_adj(m
, ccmp
.ic_header
);
256 m_adj(m
, -ccmp
.ic_trailer
);
259 * Ok to update rsc now.
267 * Verify and strip MIC from the frame.
270 ccmp_demic(struct ieee80211_key
*k
, struct mbuf
*m
,
277 xor_block(uint8_t *b
, const uint8_t *a
, size_t len
)
280 for (i
= 0; i
< len
; i
++)
285 * Host AP crypt: host-based CCMP encryption implementation for Host AP driver
287 * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
289 * This program is free software; you can redistribute it and/or modify
290 * it under the terms of the GNU General Public License version 2 as
291 * published by the Free Software Foundation. See README and COPYING for
294 * Alternatively, this software may be distributed under the terms of BSD
299 ccmp_init_blocks(rijndael_ctx
*ctx
, struct ieee80211_frame
*wh
,
300 u_int64_t pn
, size_t dlen
,
301 uint8_t b0
[AES_BLOCK_LEN
], uint8_t aad
[2 * AES_BLOCK_LEN
],
302 uint8_t auth
[AES_BLOCK_LEN
], uint8_t s0
[AES_BLOCK_LEN
])
304 #define IS_4ADDRESS(wh) \
305 ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
306 #define IS_QOS_DATA(wh) IEEE80211_QOS_HAS_SEQ(wh)
308 /* CCM Initial Block:
309 * Flag (Include authentication header, M=3 (8-octet MIC),
310 * L=1 (2-octet Dlen))
311 * Nonce: 0x00 | A2 | PN
314 /* NB: b0[1] set below */
315 IEEE80211_ADDR_COPY(b0
+ 2, wh
->i_addr2
);
322 b0
[14] = (dlen
>> 8) & 0xff;
323 b0
[15] = dlen
& 0xff;
326 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
328 * SC with bits 4..15 (seq#) masked to zero
332 aad
[0] = 0; /* AAD length >> 8 */
333 /* NB: aad[1] set below */
334 aad
[2] = wh
->i_fc
[0] & 0x8f; /* XXX magic #s */
335 aad
[3] = wh
->i_fc
[1] & 0xc7; /* XXX magic #s */
336 /* NB: we know 3 addresses are contiguous */
337 memcpy(aad
+ 4, wh
->i_addr1
, 3 * IEEE80211_ADDR_LEN
);
338 aad
[22] = wh
->i_seq
[0] & IEEE80211_SEQ_FRAG_MASK
;
339 aad
[23] = 0; /* all bits masked */
341 * Construct variable-length portion of AAD based
342 * on whether this is a 4-address frame/QOS frame.
343 * We always zero-pad to 32 bytes before running it
344 * through the cipher.
346 * We also fill in the priority bits of the CCM
347 * initial block as we know whether or not we have
350 if (IS_4ADDRESS(wh
)) {
351 IEEE80211_ADDR_COPY(aad
+ 24,
352 ((struct ieee80211_frame_addr4
*)wh
)->i_addr4
);
353 if (IS_QOS_DATA(wh
)) {
354 struct ieee80211_qosframe_addr4
*qwh4
=
355 (struct ieee80211_qosframe_addr4
*) wh
;
356 aad
[30] = qwh4
->i_qos
[0] & 0x0f;/* just priority bits */
359 aad
[1] = 22 + IEEE80211_ADDR_LEN
+ 2;
361 *(u_int16_t
*)&aad
[30] = 0;
363 aad
[1] = 22 + IEEE80211_ADDR_LEN
;
366 if (IS_QOS_DATA(wh
)) {
367 struct ieee80211_qosframe
*qwh
=
368 (struct ieee80211_qosframe
*) wh
;
369 aad
[24] = qwh
->i_qos
[0] & 0x0f; /* just priority bits */
374 *(u_int16_t
*)&aad
[24] = 0;
378 *(u_int16_t
*)&aad
[26] = 0;
379 *(u_int32_t
*)&aad
[28] = 0;
382 /* Start with the first block and AAD */
383 rijndael_encrypt(ctx
, b0
, auth
);
384 xor_block(auth
, aad
, AES_BLOCK_LEN
);
385 rijndael_encrypt(ctx
, auth
, auth
);
386 xor_block(auth
, &aad
[AES_BLOCK_LEN
], AES_BLOCK_LEN
);
387 rijndael_encrypt(ctx
, auth
, auth
);
390 rijndael_encrypt(ctx
, b0
, s0
);
395 #define CCMP_ENCRYPT(_i, _b, _b0, _pos, _e, _len) do { \
396 /* Authentication */ \
397 xor_block(_b, _pos, _len); \
398 rijndael_encrypt(&ctx->cc_aes, _b, _b); \
399 /* Encryption, with counter */ \
400 _b0[14] = (_i >> 8) & 0xff; \
401 _b0[15] = _i & 0xff; \
402 rijndael_encrypt(&ctx->cc_aes, _b0, _e); \
403 xor_block(_pos, _e, _len); \
407 ccmp_encrypt(struct ieee80211_key
*key
, struct mbuf
*m0
, int hdrlen
)
409 struct ccmp_ctx
*ctx
= key
->wk_private
;
410 struct ieee80211_frame
*wh
;
412 int data_len
, i
, space
;
413 uint8_t aad
[2 * AES_BLOCK_LEN
], b0
[AES_BLOCK_LEN
], b
[AES_BLOCK_LEN
],
414 e
[AES_BLOCK_LEN
], s0
[AES_BLOCK_LEN
];
417 ctx
->cc_ic
->ic_stats
.is_crypto_ccmp
++;
419 wh
= mtod(m
, struct ieee80211_frame
*);
420 data_len
= m
->m_pkthdr
.len
- (hdrlen
+ ccmp
.ic_header
);
421 ccmp_init_blocks(&ctx
->cc_aes
, wh
, key
->wk_keytsc
,
422 data_len
, b0
, aad
, b
, s0
);
425 pos
= mtod(m
, uint8_t *) + hdrlen
+ ccmp
.ic_header
;
426 /* NB: assumes header is entirely in first mbuf */
427 space
= m
->m_len
- (hdrlen
+ ccmp
.ic_header
);
429 if (space
> data_len
)
434 while (space
>= AES_BLOCK_LEN
) {
435 CCMP_ENCRYPT(i
, b
, b0
, pos
, e
, AES_BLOCK_LEN
);
436 pos
+= AES_BLOCK_LEN
, space
-= AES_BLOCK_LEN
;
437 data_len
-= AES_BLOCK_LEN
;
440 if (data_len
<= 0) /* no more data */
443 if (m
== NULL
) { /* last buffer */
448 CCMP_ENCRYPT(i
, b
, b0
, pos
, e
, space
);
459 * Block straddles one or more mbufs, gather data
460 * into the block buffer b, apply the cipher, then
461 * scatter the results back into the mbuf chain.
462 * The buffer will automatically get space bytes
463 * of data at offset 0 copied in+out by the
464 * CCMP_ENCRYPT request so we must take care of
465 * the remaining data.
471 pos_next
= mtod(n
, uint8_t *);
472 len
= min(dl
, AES_BLOCK_LEN
);
473 space_next
= len
> sp
? len
- sp
: 0;
474 if (n
->m_len
>= space_next
) {
476 * This mbuf has enough data; just grab
477 * what we need and stop.
479 xor_block(b
+sp
, pos_next
, space_next
);
483 * This mbuf's contents are insufficient,
484 * take 'em all and prepare to advance to
487 xor_block(b
+sp
, pos_next
, n
->m_len
);
488 sp
+= n
->m_len
, dl
-= n
->m_len
;
494 CCMP_ENCRYPT(i
, b
, b0
, pos
, e
, space
);
496 /* NB: just like above, but scatter data to mbufs */
500 pos_next
= mtod(m
, uint8_t *);
501 len
= min(dl
, AES_BLOCK_LEN
);
502 space_next
= len
> sp
? len
- sp
: 0;
503 if (m
->m_len
>= space_next
) {
504 xor_block(pos_next
, e
+sp
, space_next
);
507 xor_block(pos_next
, e
+sp
, m
->m_len
);
508 sp
+= m
->m_len
, dl
-= m
->m_len
;
514 * Do bookkeeping. m now points to the last mbuf
515 * we grabbed data from. We know we consumed a
516 * full block of data as otherwise we'd have hit
517 * the end of the mbuf chain, so deduct from data_len.
518 * Otherwise advance the block number (i) and setup
519 * pos+space to reflect contents of the new mbuf.
521 data_len
-= AES_BLOCK_LEN
;
523 pos
= pos_next
+ space_next
;
524 space
= m
->m_len
- space_next
;
527 * Setup for next buffer.
529 pos
= mtod(m
, uint8_t *);
535 xor_block(b
, s0
, ccmp
.ic_trailer
);
536 return m_append(m0
, ccmp
.ic_trailer
, b
);
540 #define CCMP_DECRYPT(_i, _b, _b0, _pos, _a, _len) do { \
541 /* Decrypt, with counter */ \
542 _b0[14] = (_i >> 8) & 0xff; \
543 _b0[15] = _i & 0xff; \
544 rijndael_encrypt(&ctx->cc_aes, _b0, _b); \
545 xor_block(_pos, _b, _len); \
546 /* Authentication */ \
547 xor_block(_a, _pos, _len); \
548 rijndael_encrypt(&ctx->cc_aes, _a, _a); \
552 ccmp_decrypt(struct ieee80211_key
*key
, u_int64_t pn
, struct mbuf
*m
, int hdrlen
)
554 struct ccmp_ctx
*ctx
= key
->wk_private
;
555 struct ieee80211_frame
*wh
;
556 uint8_t aad
[2 * AES_BLOCK_LEN
];
557 uint8_t b0
[AES_BLOCK_LEN
], b
[AES_BLOCK_LEN
], a
[AES_BLOCK_LEN
];
558 uint8_t mic
[AES_BLOCK_LEN
];
564 ctx
->cc_ic
->ic_stats
.is_crypto_ccmp
++;
566 wh
= mtod(m
, struct ieee80211_frame
*);
567 data_len
= m
->m_pkthdr
.len
- (hdrlen
+ ccmp
.ic_header
+ ccmp
.ic_trailer
);
568 ccmp_init_blocks(&ctx
->cc_aes
, wh
, pn
, data_len
, b0
, aad
, a
, b
);
569 m_copydata(m
, m
->m_pkthdr
.len
- ccmp
.ic_trailer
, ccmp
.ic_trailer
, mic
);
570 xor_block(mic
, b
, ccmp
.ic_trailer
);
573 pos
= mtod(m
, uint8_t *) + hdrlen
+ ccmp
.ic_header
;
574 space
= m
->m_len
- (hdrlen
+ ccmp
.ic_header
);
576 if (space
> data_len
)
578 while (space
>= AES_BLOCK_LEN
) {
579 CCMP_DECRYPT(i
, b
, b0
, pos
, a
, AES_BLOCK_LEN
);
580 pos
+= AES_BLOCK_LEN
, space
-= AES_BLOCK_LEN
;
581 data_len
-= AES_BLOCK_LEN
;
584 if (data_len
<= 0) /* no more data */
587 if (m
== NULL
) { /* last buffer */
588 if (space
!= 0) /* short last block */
589 CCMP_DECRYPT(i
, b
, b0
, pos
, a
, space
);
598 * Block straddles buffers, split references. We
599 * do not handle splits that require >2 buffers
600 * since rx'd frames are never badly fragmented
601 * because drivers typically recv in clusters.
603 pos_next
= mtod(m
, uint8_t *);
604 len
= min(data_len
, AES_BLOCK_LEN
);
605 space_next
= len
> space
? len
- space
: 0;
606 IASSERT(m
->m_len
>= space_next
,
607 ("not enough data in following buffer, "
608 "m_len %u need %u\n", m
->m_len
, space_next
));
610 xor_block(b
+space
, pos_next
, space_next
);
611 CCMP_DECRYPT(i
, b
, b0
, pos
, a
, space
);
612 xor_block(pos_next
, b
+space
, space_next
);
616 pos
= pos_next
+ space_next
;
617 space
= m
->m_len
- space_next
;
620 * Setup for next buffer.
622 pos
= mtod(m
, uint8_t *);
626 if (memcmp(mic
, a
, ccmp
.ic_trailer
) != 0) {
627 IEEE80211_DPRINTF(ctx
->cc_ic
, IEEE80211_MSG_CRYPTO
,
628 "[%s] AES-CCM decrypt failed; MIC mismatch\n",
629 ether_sprintf(wh
->i_addr2
));
630 ctx
->cc_ic
->ic_stats
.is_rx_ccmpmic
++;
637 IEEE80211_CRYPTO_SETUP(ccmp_register
)
639 ieee80211_crypto_register(&ccmp
);