wlantest: Add replay detection for CCMP
[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);
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                 wpa_printf(MSG_INFO, "Invalid CCMP MIC in frame from " MACSTR,
206                            MAC2STR(hdr->addr2));
207                 os_free(plain);
208                 return NULL;
209         }
210
211         *decrypted_len = mlen;
212         return plain;
213 }
214
215
216 void ccmp_get_pn(u8 *pn, const u8 *data)
217 {
218         pn[0] = data[7]; /* PN5 */
219         pn[1] = data[6]; /* PN4 */
220         pn[2] = data[5]; /* PN3 */
221         pn[3] = data[4]; /* PN2 */
222         pn[4] = data[1]; /* PN1 */
223         pn[5] = data[0]; /* PN0 */
224 }