2 * CTR with CBC-MAC Protocol (CCMP)
3 * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "common/ieee802_11_defs.h"
19 #include "crypto/aes.h"
23 static void ccmp_aad_nonce(const struct ieee80211_hdr *hdr, const u8 *data,
24 u8 *aad, size_t *aad_len, u8 *nonce)
27 int qos = 0, addr4 = 0;
32 fc = le_to_host16(hdr->frame_control);
33 stype = WLAN_FC_GET_STYPE(fc);
34 if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
35 (WLAN_FC_TODS | WLAN_FC_FROMDS))
38 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
39 fc &= ~0x0070; /* Mask subtype bits */
44 qc = (const u8 *) (hdr + 1);
47 nonce[0] = qc[0] & 0x0f;
49 } else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
50 nonce[0] |= 0x10; /* Management */
52 fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
54 WPA_PUT_LE16(aad, fc);
56 os_memcpy(pos, hdr->addr1, 3 * ETH_ALEN);
58 seq = le_to_host16(hdr->seq_ctrl);
59 seq &= ~0xfff0; /* Mask Seq#; do not modify Frag# */
60 WPA_PUT_LE16(pos, seq);
63 os_memcpy(pos, hdr + 1, addr4 * ETH_ALEN + qos * 2);
64 pos += addr4 * ETH_ALEN;
67 if (1 /* FIX: either device has SPP A-MSDU Capab = 0 */)
75 os_memcpy(nonce + 1, hdr->addr2, ETH_ALEN);
76 nonce[7] = data[7]; /* PN5 */
77 nonce[8] = data[6]; /* PN4 */
78 nonce[9] = data[5]; /* PN3 */
79 nonce[10] = data[4]; /* PN2 */
80 nonce[11] = data[1]; /* PN1 */
81 nonce[12] = data[0]; /* PN0 */
85 static void xor_aes_block(u8 *dst, const u8 *src)
96 u8 * ccmp_decrypt(const u8 *tk, const struct ieee80211_hdr *hdr,
97 const u8 *data, size_t data_len, size_t *decrypted_len)
99 u8 aad[2 + 30], nonce[13];
101 u8 b[AES_BLOCK_SIZE], x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
103 const u8 *m, *mpos, *mic;
109 if (data_len < 8 + 8)
112 plain = os_malloc(data_len + AES_BLOCK_SIZE);
116 aes = aes_encrypt_init(tk, 16);
123 mlen = data_len - 8 - 8;
124 last = mlen % AES_BLOCK_SIZE;
126 os_memset(aad, 0, sizeof(aad));
127 ccmp_aad_nonce(hdr, data, &aad[2], &aad_len, nonce);
128 WPA_PUT_BE16(aad, aad_len);
129 wpa_hexdump(MSG_EXCESSIVE, "CCMP AAD", &aad[2], aad_len);
130 wpa_hexdump(MSG_EXCESSIVE, "CCMP nonce", nonce, 13);
132 /* CCM: M=8 L=2, Adata=1, M' = (M-2)/2 = 3, L' = L-1 = 1 */
134 /* A_i = Flags | Nonce N | Counter i */
135 a[0] = 0x01; /* Flags = L' */
136 os_memcpy(&a[1], nonce, 13);
140 mic = data + data_len - 8;
141 wpa_hexdump(MSG_EXCESSIVE, "CCMP U", mic, 8);
142 /* U = T XOR S_0; S_0 = E(K, A_0) */
143 WPA_PUT_BE16(&a[14], 0);
144 aes_encrypt(aes, a, x);
145 for (i = 0; i < 8; i++)
146 t[i] = mic[i] ^ x[i];
147 wpa_hexdump(MSG_EXCESSIVE, "CCMP T", t, 8);
149 /* plaintext = msg XOR (S_1 | S_2 | ... | S_n) */
152 for (i = 1; i <= mlen / AES_BLOCK_SIZE; i++) {
153 WPA_PUT_BE16(&a[14], i);
154 /* S_i = E(K, A_i) */
155 aes_encrypt(aes, a, ppos);
156 xor_aes_block(ppos, mpos);
157 ppos += AES_BLOCK_SIZE;
158 mpos += AES_BLOCK_SIZE;
161 WPA_PUT_BE16(&a[14], i);
162 aes_encrypt(aes, a, ppos);
163 /* XOR zero-padded last block */
164 for (i = 0; i < last; i++)
167 wpa_hexdump(MSG_EXCESSIVE, "CCMP decrypted", plain, mlen);
170 /* B_0: Flags | Nonce N | l(m) */
171 b[0] = 0x40 /* Adata */ | (3 /* M' */ << 3) | 1 /* L' */;
172 os_memcpy(&b[1], nonce, 13);
173 WPA_PUT_BE16(&b[14], mlen);
175 wpa_hexdump(MSG_EXCESSIVE, "CCMP B_0", b, AES_BLOCK_SIZE);
176 aes_encrypt(aes, b, x); /* X_1 = E(K, B_0) */
178 wpa_hexdump(MSG_EXCESSIVE, "CCMP B_1", aad, AES_BLOCK_SIZE);
179 xor_aes_block(aad, x);
180 aes_encrypt(aes, aad, x); /* X_2 = E(K, X_1 XOR B_1) */
182 wpa_hexdump(MSG_EXCESSIVE, "CCMP B_2", &aad[AES_BLOCK_SIZE],
184 xor_aes_block(&aad[AES_BLOCK_SIZE], x);
185 aes_encrypt(aes, &aad[AES_BLOCK_SIZE], x); /* X_3 = E(K, X_2 XOR B_2)
189 for (i = 0; i < mlen / AES_BLOCK_SIZE; i++) {
190 /* X_i+1 = E(K, X_i XOR B_i) */
191 xor_aes_block(x, ppos);
192 ppos += AES_BLOCK_SIZE;
193 aes_encrypt(aes, x, x);
196 /* XOR zero-padded last block */
197 for (i = 0; i < last; i++)
199 aes_encrypt(aes, x, x);
202 aes_encrypt_deinit(aes);
204 if (os_memcmp(x, t, 8) != 0) {
205 u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
206 wpa_printf(MSG_INFO, "Invalid CCMP MIC in frame: A1=" MACSTR
207 " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
208 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
210 WLAN_GET_SEQ_SEQ(seq_ctrl),
211 WLAN_GET_SEQ_FRAG(seq_ctrl));
212 wpa_hexdump(MSG_DEBUG, "CCMP decrypted", plain, mlen);
217 *decrypted_len = mlen;
222 void ccmp_get_pn(u8 *pn, const u8 *data)
224 pn[0] = data[7]; /* PN5 */
225 pn[1] = data[6]; /* PN4 */
226 pn[2] = data[5]; /* PN3 */
227 pn[3] = data[4]; /* PN2 */
228 pn[4] = data[1]; /* PN1 */
229 pn[5] = data[0]; /* PN0 */
233 u8 * ccmp_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen, u8 *qos,
234 u8 *pn, int keyid, size_t *encrypted_len)
236 u8 aad[2 + 30], nonce[13];
238 u8 b[AES_BLOCK_SIZE], x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
240 u8 *crypt, *pos, *ppos, *mpos;
242 struct ieee80211_hdr *hdr;
245 if (len < hdrlen || hdrlen < 24)
248 last = plen % AES_BLOCK_SIZE;
250 crypt = os_malloc(hdrlen + 8 + plen + 8 + AES_BLOCK_SIZE);
254 os_memcpy(crypt, frame, hdrlen);
255 hdr = (struct ieee80211_hdr *) crypt;
256 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
257 pos = crypt + hdrlen;
258 *pos++ = pn[5]; /* PN0 */
259 *pos++ = pn[4]; /* PN1 */
260 *pos++ = 0x00; /* Rsvd */
261 *pos++ = 0x20 | (keyid << 6);
262 *pos++ = pn[3]; /* PN2 */
263 *pos++ = pn[2]; /* PN3 */
264 *pos++ = pn[1]; /* PN4 */
265 *pos++ = pn[0]; /* PN5 */
267 aes = aes_encrypt_init(tk, 16);
273 os_memset(aad, 0, sizeof(aad));
274 ccmp_aad_nonce(hdr, crypt + hdrlen, &aad[2], &aad_len, nonce);
275 WPA_PUT_BE16(aad, aad_len);
276 wpa_hexdump(MSG_EXCESSIVE, "CCMP AAD", &aad[2], aad_len);
277 wpa_hexdump(MSG_EXCESSIVE, "CCMP nonce", nonce, 13);
280 /* B_0: Flags | Nonce N | l(m) */
281 b[0] = 0x40 /* Adata */ | (3 /* M' */ << 3) | 1 /* L' */;
282 os_memcpy(&b[1], nonce, 13);
283 WPA_PUT_BE16(&b[14], plen);
285 wpa_hexdump(MSG_EXCESSIVE, "CCMP B_0", b, AES_BLOCK_SIZE);
286 aes_encrypt(aes, b, x); /* X_1 = E(K, B_0) */
288 wpa_hexdump(MSG_EXCESSIVE, "CCMP B_1", aad, AES_BLOCK_SIZE);
289 xor_aes_block(aad, x);
290 aes_encrypt(aes, aad, x); /* X_2 = E(K, X_1 XOR B_1) */
292 wpa_hexdump(MSG_EXCESSIVE, "CCMP B_2", &aad[AES_BLOCK_SIZE],
294 xor_aes_block(&aad[AES_BLOCK_SIZE], x);
295 aes_encrypt(aes, &aad[AES_BLOCK_SIZE], x); /* X_3 = E(K, X_2 XOR B_2)
298 ppos = frame + hdrlen;
299 for (i = 0; i < plen / AES_BLOCK_SIZE; i++) {
300 /* X_i+1 = E(K, X_i XOR B_i) */
301 xor_aes_block(x, ppos);
302 ppos += AES_BLOCK_SIZE;
303 aes_encrypt(aes, x, x);
306 /* XOR zero-padded last block */
307 for (i = 0; i < last; i++)
309 aes_encrypt(aes, x, x);
314 /* CCM: M=8 L=2, Adata=1, M' = (M-2)/2 = 3, L' = L-1 = 1 */
316 /* A_i = Flags | Nonce N | Counter i */
317 a[0] = 0x01; /* Flags = L' */
318 os_memcpy(&a[1], nonce, 13);
320 ppos = crypt + hdrlen + 8;
322 /* crypt = msg XOR (S_1 | S_2 | ... | S_n) */
323 mpos = frame + hdrlen;
324 for (i = 1; i <= plen / AES_BLOCK_SIZE; i++) {
325 WPA_PUT_BE16(&a[14], i);
326 /* S_i = E(K, A_i) */
327 aes_encrypt(aes, a, ppos);
328 xor_aes_block(ppos, mpos);
329 ppos += AES_BLOCK_SIZE;
330 mpos += AES_BLOCK_SIZE;
333 WPA_PUT_BE16(&a[14], i);
334 aes_encrypt(aes, a, ppos);
335 /* XOR zero-padded last block */
336 for (i = 0; i < last; i++)
340 wpa_hexdump(MSG_EXCESSIVE, "CCMP T", x, 8);
341 /* U = T XOR S_0; S_0 = E(K, A_0) */
342 WPA_PUT_BE16(&a[14], 0);
343 aes_encrypt(aes, a, b);
344 for (i = 0; i < 8; i++)
345 ppos[i] = x[i] ^ b[i];
346 wpa_hexdump(MSG_EXCESSIVE, "CCMP U", ppos, 8);
348 wpa_hexdump(MSG_EXCESSIVE, "CCMP encrypted", crypt + hdrlen + 8, plen);
350 aes_encrypt_deinit(aes);
352 *encrypted_len = hdrlen + 8 + plen + 8;