wlantest: Add CCMP-256 test vector
[mech_eap.git] / wlantest / test_vectors.c
1 /*
2  * test_vectors - IEEE 802.11 test vector generator
3  * Copyright (c) 2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "wlantest.h"
14
15
16 extern int wpa_debug_level;
17 extern int wpa_debug_show_keys;
18
19
20 static void test_vector_tkip(void)
21 {
22         u8 tk[] = {
23                 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56,
24                 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12,
25                 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78,
26                 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34
27         };
28         u8 pn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
29         u8 frame[] = {
30                 0x08, 0x42, 0x2c, 0x00, 0x02, 0x03, 0x04, 0x05,
31                 0x06, 0x08, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
32                 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xd0, 0x02,
33                 /* 0x00, 0x20, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, */
34                 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x08, 0x00,
35                 0x45, 0x00, 0x00, 0x54, 0x00, 0x00, 0x40, 0x00,
36                 0x40, 0x01, 0xa5, 0x55, 0xc0, 0xa8, 0x0a, 0x02,
37                 0xc0, 0xa8, 0x0a, 0x01, 0x08, 0x00, 0x3a, 0xb0,
38                 0x00, 0x00, 0x00, 0x00, 0xcd, 0x4c, 0x05, 0x00,
39                 0x00, 0x00, 0x00, 0x00, 0x08, 0x09, 0x0a, 0x0b,
40                 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
41                 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
42                 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
43                 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
44                 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33,
45                 0x34, 0x35, 0x36, 0x37,
46                 /* 0x68, 0x81, 0xa3, 0xf3, 0xd6, 0x48, 0xd0, 0x3c */
47         };
48         u8 *enc, *plain;
49         size_t enc_len, plain_len;
50
51         wpa_printf(MSG_INFO, "\nIEEE Std 802.11-2012, M.6.3 TKIP test "
52                    "vector\n");
53
54         wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
55         wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
56         wpa_hexdump(MSG_INFO, "Plaintext MPDU", frame, sizeof(frame));
57
58         enc = tkip_encrypt(tk, frame, sizeof(frame), 24, NULL, pn, 0, &enc_len);
59         if (enc == NULL) {
60                 wpa_printf(MSG_ERROR, "Failed to encrypt TKIP frame");
61                 return;
62         }
63
64         wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
65
66         wpa_debug_level = MSG_INFO;
67         plain = tkip_decrypt(tk, (const struct ieee80211_hdr *) enc,
68                              enc + 24, enc_len - 24, &plain_len);
69         wpa_debug_level = MSG_EXCESSIVE;
70         os_free(enc);
71
72         if (plain == NULL) {
73                 wpa_printf(MSG_ERROR, "Failed to decrypt TKIP frame");
74                 return;
75         }
76
77         if (plain_len != sizeof(frame) - 24 ||
78             os_memcmp(plain, frame + 24, plain_len) != 0) {
79                 wpa_hexdump(MSG_ERROR, "Decryption result did not match",
80                             plain, plain_len);
81         }
82
83         os_free(plain);
84 }
85
86
87 static void test_vector_ccmp(void)
88 {
89         u8 tk[] = { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
90                     0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f };
91         u8 pn[] = { 0xB5, 0x03, 0x97, 0x76, 0xE7, 0x0C };
92         u8 frame[] = {
93                 0x08, 0x48, 0xc3, 0x2c, 0x0f, 0xd2, 0xe1, 0x28,
94                 0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
95                 0xab, 0xae, 0xa5, 0xb8, 0xfc, 0xba, 0x80, 0x33,
96                 0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
97                 0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
98                 0x7e, 0x78, 0xa0, 0x50
99         };
100         u8 *enc, *plain;
101         size_t enc_len, plain_len;
102         u8 fcs[4];
103
104         wpa_printf(MSG_INFO, "\nIEEE Std 802.11-2012, M.6.4 CCMP test "
105                    "vector\n");
106
107         wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
108         wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
109         wpa_hexdump(MSG_INFO, "802.11 Header", frame, 24);
110         wpa_hexdump(MSG_INFO, "Plaintext Data", frame + 24, sizeof(frame) - 24);
111
112         enc = ccmp_encrypt(tk, frame, sizeof(frame), 24, NULL, pn, 0, &enc_len);
113         if (enc == NULL) {
114                 wpa_printf(MSG_ERROR, "Failed to encrypt CCMP frame");
115                 return;
116         }
117
118         wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
119         WPA_PUT_LE32(fcs, crc32(enc, enc_len));
120         wpa_hexdump(MSG_INFO, "FCS", fcs, sizeof(fcs));
121
122         wpa_debug_level = MSG_INFO;
123         plain = ccmp_decrypt(tk, (const struct ieee80211_hdr *) enc,
124                              enc + 24, enc_len - 24, &plain_len);
125         wpa_debug_level = MSG_EXCESSIVE;
126         os_free(enc);
127
128         if (plain == NULL) {
129                 wpa_printf(MSG_ERROR, "Failed to decrypt CCMP frame");
130                 return;
131         }
132
133         if (plain_len != sizeof(frame) - 24 ||
134             os_memcmp(plain, frame + 24, plain_len) != 0) {
135                 wpa_hexdump(MSG_ERROR, "Decryption result did not match",
136                             plain, plain_len);
137         }
138
139         os_free(plain);
140 }
141
142
143 static void test_vector_bip(void)
144 {
145         u8 igtk[] = {
146                 0x4e, 0xa9, 0x54, 0x3e, 0x09, 0xcf, 0x2b, 0x1e,
147                 0xca, 0x66, 0xff, 0xc5, 0x8b, 0xde, 0xcb, 0xcf
148         };
149         u8 ipn[] = { 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 };
150         u8 frame[] = {
151                 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
152                 0xff, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
153                 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
154                 0x02, 0x00
155         };
156         u8 *prot;
157         size_t prot_len;
158
159         wpa_printf(MSG_INFO, "\nIEEE Std 802.11-2012, M.9.1 BIP with broadcast "
160                    "Deauthentication frame\n");
161
162         wpa_hexdump(MSG_INFO, "IGTK", igtk, sizeof(igtk));
163         wpa_hexdump(MSG_INFO, "IPN", ipn, sizeof(ipn));
164         wpa_hexdump(MSG_INFO, "Plaintext frame", frame, sizeof(frame));
165
166         prot = bip_protect(igtk, frame, sizeof(frame), ipn, 4, &prot_len);
167         if (prot == NULL) {
168                 wpa_printf(MSG_ERROR, "Failed to protect BIP frame");
169                 return;
170         }
171
172         wpa_hexdump(MSG_INFO, "Protected MPDU (without FCS)", prot, prot_len);
173         os_free(prot);
174 }
175
176
177 static void test_vector_ccmp_mgmt(void)
178 {
179         u8 tk[] = { 0x66, 0xed, 0x21, 0x04, 0x2f, 0x9f, 0x26, 0xd7,
180                     0x11, 0x57, 0x06, 0xe4, 0x04, 0x14, 0xcf, 0x2e };
181         u8 pn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
182         u8 frame[] = {
183                 0xc0, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
184                 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
185                 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00,
186                 0x02, 0x00
187         };
188         u8 *enc, *plain;
189         size_t enc_len, plain_len;
190
191         wpa_printf(MSG_INFO, "\nIEEE Std 802.11-2012, M.9.2 CCMP with unicast "
192                    "Deauthentication frame\n");
193
194         wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
195         wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
196         wpa_hexdump(MSG_INFO, "802.11 Header", frame, 24);
197         wpa_hexdump(MSG_INFO, "Plaintext Data", frame + 24, sizeof(frame) - 24);
198
199         enc = ccmp_encrypt(tk, frame, sizeof(frame), 24, NULL, pn, 0, &enc_len);
200         if (enc == NULL) {
201                 wpa_printf(MSG_ERROR, "Failed to encrypt CCMP frame");
202                 return;
203         }
204
205         wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
206
207         wpa_debug_level = MSG_INFO;
208         plain = ccmp_decrypt(tk, (const struct ieee80211_hdr *) enc,
209                              enc + 24, enc_len - 24, &plain_len);
210         wpa_debug_level = MSG_EXCESSIVE;
211         os_free(enc);
212
213         if (plain == NULL) {
214                 wpa_printf(MSG_ERROR, "Failed to decrypt CCMP frame");
215                 return;
216         }
217
218         if (plain_len != sizeof(frame) - 24 ||
219             os_memcmp(plain, frame + 24, plain_len) != 0) {
220                 wpa_hexdump(MSG_ERROR, "Decryption result did not match",
221                             plain, plain_len);
222         }
223
224         os_free(plain);
225 }
226
227
228 static void test_vector_gcmp(void)
229 {
230         u8 tk[] = { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
231                     0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f };
232         u8 pn[] = {
233                 0x00, 0x89, 0x5F, 0x5F, 0x2B, 0x08
234         };
235         u8 frame[] = {
236                 0x88, 0x48, 0x0b, 0x00, 0x0f, 0xd2, 0xe1, 0x28,
237                 0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
238                 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08, 0x80, 0x33,
239                 0x03, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
240                 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
241                 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
242                 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
243                 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
244                 0x26, 0x27
245         };
246         u8 *enc, *plain;
247         size_t enc_len, plain_len;
248         u8 fcs[4];
249
250         wpa_printf(MSG_INFO, "\nIEEE P802.11ad/D9.0, M.11.1 GCMP test "
251                    "vector #2\n");
252
253         wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
254         wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
255         wpa_hexdump(MSG_INFO, "802.11 Header", frame, 26);
256         wpa_hexdump(MSG_INFO, "Plaintext Data", frame + 26, sizeof(frame) - 26);
257
258         enc = gcmp_encrypt(tk, sizeof(tk), frame, sizeof(frame), 26, frame + 24,
259                            pn, 0, &enc_len);
260         if (enc == NULL) {
261                 wpa_printf(MSG_ERROR, "Failed to encrypt GCMP frame");
262                 return;
263         }
264
265         wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
266         WPA_PUT_LE32(fcs, crc32(enc, enc_len));
267         wpa_hexdump(MSG_INFO, "FCS", fcs, sizeof(fcs));
268
269         wpa_debug_level = MSG_INFO;
270         plain = gcmp_decrypt(tk, sizeof(tk), (const struct ieee80211_hdr *) enc,
271                              enc + 26, enc_len - 26, &plain_len);
272         wpa_debug_level = MSG_EXCESSIVE;
273         os_free(enc);
274
275         if (plain == NULL) {
276                 wpa_printf(MSG_ERROR, "Failed to decrypt GCMP frame");
277                 return;
278         }
279
280         if (plain_len != sizeof(frame) - 26 ||
281             os_memcmp(plain, frame + 26, plain_len) != 0) {
282                 wpa_hexdump(MSG_ERROR, "Decryption result did not match",
283                             plain, plain_len);
284         }
285
286         os_free(plain);
287 }
288
289
290 static void test_vector_gcmp_256(void)
291 {
292         u8 tk[] = { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
293                     0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f,
294                     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
295                     0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
296         u8 pn[] = {
297                 0x00, 0x89, 0x5F, 0x5F, 0x2B, 0x08
298         };
299         u8 frame[] = {
300                 0x88, 0x48, 0x0b, 0x00, 0x0f, 0xd2, 0xe1, 0x28,
301                 0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
302                 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08, 0x80, 0x33,
303                 0x03, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
304                 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
305                 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
306                 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
307                 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
308                 0x26, 0x27
309         };
310         u8 *enc, *plain;
311         size_t enc_len, plain_len;
312         u8 fcs[4];
313
314         wpa_printf(MSG_INFO, "\nGCMP-256 test vector\n");
315
316         wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
317         wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
318         wpa_hexdump(MSG_INFO, "802.11 Header", frame, 26);
319         wpa_hexdump(MSG_INFO, "Plaintext Data", frame + 26, sizeof(frame) - 26);
320
321         enc = gcmp_encrypt(tk, sizeof(tk), frame, sizeof(frame), 26, frame + 24,
322                            pn, 0, &enc_len);
323         if (enc == NULL) {
324                 wpa_printf(MSG_ERROR, "Failed to encrypt GCMP frame");
325                 return;
326         }
327
328         wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
329         WPA_PUT_LE32(fcs, crc32(enc, enc_len));
330         wpa_hexdump(MSG_INFO, "FCS", fcs, sizeof(fcs));
331
332         wpa_debug_level = MSG_INFO;
333         plain = gcmp_decrypt(tk, sizeof(tk), (const struct ieee80211_hdr *) enc,
334                              enc + 26, enc_len - 26, &plain_len);
335         wpa_debug_level = MSG_EXCESSIVE;
336         os_free(enc);
337
338         if (plain == NULL) {
339                 wpa_printf(MSG_ERROR, "Failed to decrypt GCMP frame");
340                 return;
341         }
342
343         if (plain_len != sizeof(frame) - 26 ||
344             os_memcmp(plain, frame + 26, plain_len) != 0) {
345                 wpa_hexdump(MSG_ERROR, "Decryption result did not match",
346                             plain, plain_len);
347         }
348
349         os_free(plain);
350 }
351
352
353 static void test_vector_ccmp_256(void)
354 {
355         u8 tk[] = { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
356                     0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f,
357                     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
358                     0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
359         u8 pn[] = { 0xB5, 0x03, 0x97, 0x76, 0xE7, 0x0C };
360         u8 frame[] = {
361                 0x08, 0x48, 0xc3, 0x2c, 0x0f, 0xd2, 0xe1, 0x28,
362                 0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
363                 0xab, 0xae, 0xa5, 0xb8, 0xfc, 0xba, 0x80, 0x33,
364                 0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
365                 0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
366                 0x7e, 0x78, 0xa0, 0x50
367         };
368         u8 *enc, *plain;
369         size_t enc_len, plain_len;
370         u8 fcs[4];
371
372         wpa_printf(MSG_INFO, "\nCCMP-256 test vector\n");
373
374         wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
375         wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
376         wpa_hexdump(MSG_INFO, "802.11 Header", frame, 24);
377         wpa_hexdump(MSG_INFO, "Plaintext Data", frame + 24, sizeof(frame) - 24);
378
379         enc = ccmp_256_encrypt(tk, frame, sizeof(frame), 24, NULL, pn, 0,
380                                &enc_len);
381         if (enc == NULL) {
382                 wpa_printf(MSG_ERROR, "Failed to encrypt CCMP frame");
383                 return;
384         }
385
386         wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
387         WPA_PUT_LE32(fcs, crc32(enc, enc_len));
388         wpa_hexdump(MSG_INFO, "FCS", fcs, sizeof(fcs));
389
390         wpa_debug_level = MSG_INFO;
391         plain = ccmp_256_decrypt(tk, (const struct ieee80211_hdr *) enc,
392                                  enc + 24, enc_len - 24, &plain_len);
393         wpa_debug_level = MSG_EXCESSIVE;
394         os_free(enc);
395
396         if (plain == NULL) {
397                 wpa_printf(MSG_ERROR, "Failed to decrypt CCMP-256 frame");
398                 return;
399         }
400
401         if (plain_len != sizeof(frame) - 24 ||
402             os_memcmp(plain, frame + 24, plain_len) != 0) {
403                 wpa_hexdump(MSG_ERROR, "Decryption result did not match",
404                             plain, plain_len);
405         }
406
407         os_free(plain);
408 }
409
410
411 int main(int argc, char *argv[])
412 {
413         wpa_debug_level = MSG_EXCESSIVE;
414         wpa_debug_show_keys = 1;
415
416         if (os_program_init())
417                 return -1;
418
419         test_vector_tkip();
420         test_vector_ccmp();
421         test_vector_bip();
422         test_vector_ccmp_mgmt();
423         test_vector_gcmp();
424         test_vector_gcmp_256();
425         test_vector_ccmp_256();
426
427         os_program_deinit();
428
429         return 0;
430 }