wlantest: Add more details for CCMP MIC failures
[mech_eap.git] / wlantest / ccmp.c
1 /*
2  * CTR with CBC-MAC Protocol (CCMP)
3  * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "common/ieee802_11_defs.h"
19 #include "crypto/aes.h"
20 #include "wlantest.h"
21
22
23 static void ccmp_aad_nonce(const struct ieee80211_hdr *hdr, const u8 *data,
24                            u8 *aad, size_t *aad_len, u8 *nonce)
25 {
26         u16 fc, stype, seq;
27         int qos = 0, addr4 = 0;
28         u8 *pos;
29
30         nonce[0] = 0;
31
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))
36                 addr4 = 1;
37
38         if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
39                 fc &= ~0x0070; /* Mask subtype bits */
40                 if (stype & 0x08) {
41                         const u8 *qc;
42                         qos = 1;
43                         fc &= ~WLAN_FC_ORDER;
44                         qc = (const u8 *) (hdr + 1);
45                         if (addr4)
46                                 qc += ETH_ALEN;
47                         nonce[0] = qc[0] & 0x0f;
48                 }
49         } else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
50                 nonce[0] |= 0x10; /* Management */
51
52         fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
53         fc |= WLAN_FC_ISWEP;
54         WPA_PUT_LE16(aad, fc);
55         pos = aad + 2;
56         os_memcpy(pos, hdr->addr1, 3 * ETH_ALEN);
57         pos += 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);
61         pos += 2;
62
63         os_memcpy(pos, hdr + 1, addr4 * ETH_ALEN + qos * 2);
64         pos += addr4 * ETH_ALEN;
65         if (qos) {
66                 pos[0] &= ~0x70;
67                 if (1 /* FIX: either device has SPP A-MSDU Capab = 0 */)
68                         pos[0] &= ~0x80;
69                 pos++;
70                 *pos++ = 0x00;
71         }
72
73         *aad_len = pos - aad;
74
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 */
82 }
83
84
85 static void xor_aes_block(u8 *dst, const u8 *src)
86 {
87         u32 *d = (u32 *) dst;
88         u32 *s = (u32 *) src;
89         *d++ ^= *s++;
90         *d++ ^= *s++;
91         *d++ ^= *s++;
92         *d++ ^= *s++;
93 }
94
95
96 u8 * ccmp_decrypt(const u8 *tk, const struct ieee80211_hdr *hdr,
97                   const u8 *data, size_t data_len, size_t *decrypted_len)
98 {
99         u8 aad[2 + 30], nonce[13];
100         size_t aad_len;
101         u8 b[AES_BLOCK_SIZE], x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
102         void *aes;
103         const u8 *m, *mpos, *mic;
104         size_t mlen, last;
105         int i;
106         u8 *plain, *ppos;
107         u8 t[8];
108
109         if (data_len < 8 + 8)
110                 return NULL;
111
112         plain = os_malloc(data_len + AES_BLOCK_SIZE);
113         if (plain == NULL)
114                 return NULL;
115
116         aes = aes_encrypt_init(tk, 16);
117         if (aes == NULL) {
118                 os_free(plain);
119                 return NULL;
120         }
121
122         m = data + 8;
123         mlen = data_len - 8 - 8;
124         last = mlen % AES_BLOCK_SIZE;
125
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);
131
132         /* CCM: M=8 L=2, Adata=1, M' = (M-2)/2 = 3, L' = L-1 = 1 */
133
134         /* A_i = Flags | Nonce N | Counter i */
135         a[0] = 0x01; /* Flags = L' */
136         os_memcpy(&a[1], nonce, 13);
137
138         /* Decryption */
139
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);
148
149         /* plaintext = msg XOR (S_1 | S_2 | ... | S_n) */
150         ppos = plain;
151         mpos = m;
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;
159         }
160         if (last) {
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++)
165                         *ppos++ ^= *mpos++;
166         }
167         wpa_hexdump(MSG_EXCESSIVE, "CCMP decrypted", plain, mlen);
168
169         /* Authentication */
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);
174
175         wpa_hexdump(MSG_EXCESSIVE, "CCMP B_0", b, AES_BLOCK_SIZE);
176         aes_encrypt(aes, b, x); /* X_1 = E(K, B_0) */
177
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) */
181
182         wpa_hexdump(MSG_EXCESSIVE, "CCMP B_2", &aad[AES_BLOCK_SIZE],
183                     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)
186                                                     */
187
188         ppos = plain;
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);
194         }
195         if (last) {
196                 /* XOR zero-padded last block */
197                 for (i = 0; i < last; i++)
198                         x[i] ^= *ppos++;
199                 aes_encrypt(aes, x, x);
200         }
201
202         aes_encrypt_deinit(aes);
203
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),
209                            MAC2STR(hdr->addr3),
210                            WLAN_GET_SEQ_SEQ(seq_ctrl),
211                            WLAN_GET_SEQ_FRAG(seq_ctrl));
212                 wpa_hexdump(MSG_DEBUG, "CCMP decrypted", plain, mlen);
213                 os_free(plain);
214                 return NULL;
215         }
216
217         *decrypted_len = mlen;
218         return plain;
219 }
220
221
222 void ccmp_get_pn(u8 *pn, const u8 *data)
223 {
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 */
230 }
231
232
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)
235 {
236         u8 aad[2 + 30], nonce[13];
237         size_t aad_len;
238         u8 b[AES_BLOCK_SIZE], x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
239         void *aes;
240         u8 *crypt, *pos, *ppos, *mpos;
241         size_t plen, last;
242         struct ieee80211_hdr *hdr;
243         int i;
244
245         if (len < hdrlen || hdrlen < 24)
246                 return NULL;
247         plen = len - hdrlen;
248         last = plen % AES_BLOCK_SIZE;
249
250         crypt = os_malloc(hdrlen + 8 + plen + 8 + AES_BLOCK_SIZE);
251         if (crypt == NULL)
252                 return NULL;
253
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 */
266
267         aes = aes_encrypt_init(tk, 16);
268         if (aes == NULL) {
269                 os_free(crypt);
270                 return NULL;
271         }
272
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);
278
279         /* Authentication */
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);
284
285         wpa_hexdump(MSG_EXCESSIVE, "CCMP B_0", b, AES_BLOCK_SIZE);
286         aes_encrypt(aes, b, x); /* X_1 = E(K, B_0) */
287
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) */
291
292         wpa_hexdump(MSG_EXCESSIVE, "CCMP B_2", &aad[AES_BLOCK_SIZE],
293                     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)
296                                                     */
297
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);
304         }
305         if (last) {
306                 /* XOR zero-padded last block */
307                 for (i = 0; i < last; i++)
308                         x[i] ^= *ppos++;
309                 aes_encrypt(aes, x, x);
310         }
311
312         /* Encryption */
313
314         /* CCM: M=8 L=2, Adata=1, M' = (M-2)/2 = 3, L' = L-1 = 1 */
315
316         /* A_i = Flags | Nonce N | Counter i */
317         a[0] = 0x01; /* Flags = L' */
318         os_memcpy(&a[1], nonce, 13);
319
320         ppos = crypt + hdrlen + 8;
321
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;
331         }
332         if (last) {
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++)
337                         *ppos++ ^= *mpos++;
338         }
339
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);
347
348         wpa_hexdump(MSG_EXCESSIVE, "CCMP encrypted", crypt + hdrlen + 8, plen);
349
350         aes_encrypt_deinit(aes);
351
352         *encrypted_len = hdrlen + 8 + plen + 8;
353
354         return crypt;
355 }