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/crc32.h"
13 #include "utils/eloop.h"
14 #include "common/ieee802_11_defs.h"
15 #include "wlantest.h"
16 
17 
test_vector_tkip(void)18 static void test_vector_tkip(void)
19 {
20 	u8 tk[] = {
21 		0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56,
22 		0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12,
23 		0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78,
24 		0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34
25 	};
26 	u8 pn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
27 	u8 frame[] = {
28 		0x08, 0x42, 0x2c, 0x00, 0x02, 0x03, 0x04, 0x05,
29 		0x06, 0x08, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
30 		0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xd0, 0x02,
31 		/* 0x00, 0x20, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, */
32 		0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x08, 0x00,
33 		0x45, 0x00, 0x00, 0x54, 0x00, 0x00, 0x40, 0x00,
34 		0x40, 0x01, 0xa5, 0x55, 0xc0, 0xa8, 0x0a, 0x02,
35 		0xc0, 0xa8, 0x0a, 0x01, 0x08, 0x00, 0x3a, 0xb0,
36 		0x00, 0x00, 0x00, 0x00, 0xcd, 0x4c, 0x05, 0x00,
37 		0x00, 0x00, 0x00, 0x00, 0x08, 0x09, 0x0a, 0x0b,
38 		0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
39 		0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
40 		0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
41 		0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
42 		0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33,
43 		0x34, 0x35, 0x36, 0x37,
44 		/* 0x68, 0x81, 0xa3, 0xf3, 0xd6, 0x48, 0xd0, 0x3c */
45 	};
46 	u8 *enc, *plain;
47 	size_t enc_len, plain_len;
48 
49 	wpa_printf(MSG_INFO, "\nIEEE Std 802.11-2012, M.6.3 TKIP test "
50 		   "vector\n");
51 
52 	wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
53 	wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
54 	wpa_hexdump(MSG_INFO, "Plaintext MPDU", frame, sizeof(frame));
55 
56 	enc = tkip_encrypt(tk, frame, sizeof(frame), 24, NULL, pn, 0, &enc_len);
57 	if (enc == NULL) {
58 		wpa_printf(MSG_ERROR, "Failed to encrypt TKIP frame");
59 		return;
60 	}
61 
62 	wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
63 
64 	wpa_debug_level = MSG_INFO;
65 	plain = tkip_decrypt(tk, (const struct ieee80211_hdr *) enc,
66 			     enc + 24, enc_len - 24, &plain_len, NULL, NULL);
67 	wpa_debug_level = MSG_EXCESSIVE;
68 	os_free(enc);
69 
70 	if (plain == NULL) {
71 		wpa_printf(MSG_ERROR, "Failed to decrypt TKIP frame");
72 		return;
73 	}
74 
75 	if (plain_len != sizeof(frame) - 24 ||
76 	    os_memcmp(plain, frame + 24, plain_len) != 0) {
77 		wpa_hexdump(MSG_ERROR, "Decryption result did not match",
78 			    plain, plain_len);
79 	}
80 
81 	os_free(plain);
82 }
83 
84 
test_vector_ccmp(void)85 static void test_vector_ccmp(void)
86 {
87 	u8 tk[] = { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
88 		    0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f };
89 	u8 pn[] = { 0xB5, 0x03, 0x97, 0x76, 0xE7, 0x0C };
90 	u8 frame[] = {
91 		0x08, 0x48, 0xc3, 0x2c, 0x0f, 0xd2, 0xe1, 0x28,
92 		0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
93 		0xab, 0xae, 0xa5, 0xb8, 0xfc, 0xba, 0x80, 0x33,
94 		0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
95 		0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
96 		0x7e, 0x78, 0xa0, 0x50
97 	};
98 	u8 *enc, *plain;
99 	size_t enc_len, plain_len;
100 	u8 fcs[4];
101 
102 	wpa_printf(MSG_INFO, "\nIEEE Std 802.11-2012, M.6.4 CCMP test "
103 		   "vector\n");
104 
105 	wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
106 	wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
107 	wpa_hexdump(MSG_INFO, "802.11 Header", frame, 24);
108 	wpa_hexdump(MSG_INFO, "Plaintext Data", frame + 24, sizeof(frame) - 24);
109 
110 	enc = ccmp_encrypt(tk, frame, sizeof(frame), 24, NULL, pn, 0, &enc_len);
111 	if (enc == NULL) {
112 		wpa_printf(MSG_ERROR, "Failed to encrypt CCMP frame");
113 		return;
114 	}
115 
116 	wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
117 	WPA_PUT_LE32(fcs, crc32(enc, enc_len));
118 	wpa_hexdump(MSG_INFO, "FCS", fcs, sizeof(fcs));
119 
120 	wpa_debug_level = MSG_INFO;
121 	plain = ccmp_decrypt(tk, (const struct ieee80211_hdr *) enc,
122 			     enc + 24, enc_len - 24, &plain_len);
123 	wpa_debug_level = MSG_EXCESSIVE;
124 	os_free(enc);
125 
126 	if (plain == NULL) {
127 		wpa_printf(MSG_ERROR, "Failed to decrypt CCMP frame");
128 		return;
129 	}
130 
131 	if (plain_len != sizeof(frame) - 24 ||
132 	    os_memcmp(plain, frame + 24, plain_len) != 0) {
133 		wpa_hexdump(MSG_ERROR, "Decryption result did not match",
134 			    plain, plain_len);
135 	}
136 
137 	os_free(plain);
138 }
139 
140 
test_vector_ccmp_pv1(void)141 static void test_vector_ccmp_pv1(void)
142 {
143 	u8 tk[] = { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
144 		    0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f };
145 	u8 pn[8];
146 	u8 frame1[] = {
147 		0x61, 0x00, 0xa2, 0xae, 0xa5, 0xb8, 0xfc, 0xba,
148 		0x07, 0x00, 0x80, 0x33,
149 		0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
150 		0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
151 		0x7e, 0x78, 0xa0, 0x50
152 	};
153 	u8 frame2[] = {
154 		0x61, 0x00, 0xa2, 0xae, 0xa5, 0xb8, 0xfc, 0xba,
155 		0x07, 0x20, 0x80, 0x33, 0x02, 0xd2, 0xe1, 0x28,
156 		0xa5, 0x7c,
157 		0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
158 		0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
159 		0x7e, 0x78, 0xa0, 0x50
160 	};
161 	u8 frame3[] = {
162 		0x6d, 0x00, 0xa2, 0xae, 0xa5, 0xb8, 0xfc, 0xba,
163 		0x52, 0x30, 0xf1, 0x84, 0x44, 0x08, 0x80, 0x33,
164 		0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
165 		0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
166 		0x7e, 0x78, 0xa0, 0x50
167 	};
168 	u8 *enc;
169 	size_t enc_len;
170 	u8 fcs[4];
171 	u8 bssid[ETH_ALEN] = { 0xa2, 0xae, 0xa5, 0xb8, 0xfc, 0xba };
172 	u8 da[ETH_ALEN] = { 0x02, 0xd2, 0xe1, 0x28, 0xa5, 0x7c };
173 	u8 sa[ETH_ALEN] = { 0x52, 0x30, 0xf1, 0x84, 0x44, 0x08 };
174 	u16 aid = 7;
175 	u32 bpn = 123;
176 	u16 sc = 0x3380;
177 	int key_id = 0;
178 	u16 fc;
179 	int tid = 3;
180 	u16 sid;
181 
182 	wpa_printf(MSG_INFO,
183 		   "\nIEEE P802.11ah/D10.0, J.6.4 CCMP PV1 test vectors\n");
184 
185 	wpa_printf(MSG_INFO, "BSSID: " MACSTR, MAC2STR(bssid));
186 	wpa_printf(MSG_INFO, "DA: " MACSTR, MAC2STR(da));
187 	wpa_printf(MSG_INFO, "SA: " MACSTR, MAC2STR(sa));
188 	wpa_printf(MSG_INFO, "Association ID: %u", aid);
189 	wpa_printf(MSG_INFO, "Base PN: %u (0x%08x)", bpn, bpn);
190 	wpa_printf(MSG_INFO, "SC = 0x%04x (FragNum=%u SeqNum=%u)",
191 		   sc, WLAN_GET_SEQ_FRAG(sc), WLAN_GET_SEQ_SEQ(sc));
192 	wpa_printf(MSG_INFO, "TID = %u", tid);
193 	wpa_printf(MSG_INFO, "Key ID: %u", key_id);
194 	wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
195 	wpa_printf(MSG_INFO, "PN = SC||BPN");
196 	WPA_PUT_LE16(&pn[0], sc);
197 	WPA_PUT_LE32(&pn[2], bpn);
198 	wpa_hexdump(MSG_INFO, "PN (PN0..PN5)", pn, sizeof(pn));
199 
200 	wpa_printf(MSG_INFO,
201 		   "\nPV1 test vector #1:\nHeader compression used and A3 was previously stored at the receiver\n");
202 	fc = WPA_GET_LE16(frame1);
203 	wpa_printf(MSG_INFO,
204 		   "FC=0x%04x (PV=%u Type=%u PTID/Subtype=%u From_DS=%u More_Fragments=%u Power_Management=%u More_Data=%u Protected_Frame=%u End_of_SP=%u Relayed_Frame=%u Ack_Policy=%u)",
205 		   fc,
206 		   fc & WLAN_FC_PVER,
207 		   (fc & (BIT(2) | BIT(3) | BIT(4))) >> 2,
208 		   (fc & (BIT(5) | BIT(6) | BIT(7))) >> 5,
209 		   !!(fc & BIT(8)),
210 		   !!(fc & BIT(9)),
211 		   !!(fc & BIT(10)),
212 		   !!(fc & BIT(11)),
213 		   !!(fc & BIT(12)),
214 		   !!(fc & BIT(13)),
215 		   !!(fc & BIT(14)),
216 		   !!(fc & BIT(15)));
217 	wpa_printf(MSG_INFO, "A1=" MACSTR, MAC2STR(&frame1[2]));
218 	sid = WPA_GET_LE16(&frame1[8]);
219 	wpa_printf(MSG_INFO,
220 		   "A2=%02x %02x (SID: AID=%u A3_Present=%u A4_Present=%u A-MSDU=%u); corresponds to 52:30:f1:84:44:08 in uncompressed header",
221 		   frame1[8], frame1[9],
222 		   sid & ~(BIT(13) | BIT(14) | BIT(15)),
223 		   !!(sid & BIT(13)),
224 		   !!(sid & BIT(14)),
225 		   !!(sid & BIT(15)));
226 	sc = WPA_GET_LE16(&frame1[10]);
227 	wpa_printf(MSG_INFO, "Sequence Control: %02x %02x (FN=%u SN=%u)",
228 		   frame1[10], frame1[11],
229 		   WLAN_GET_SEQ_FRAG(sc), WLAN_GET_SEQ_SEQ(sc));
230 	wpa_printf(MSG_INFO, "A3 not present; corresponds to 02:d2:e1:28:a5:7c in uncompressed header");
231 	wpa_printf(MSG_INFO, "A4 not present");
232 	wpa_hexdump(MSG_INFO, "Plaintext Frame Header", frame1, 12);
233 	wpa_hexdump(MSG_INFO, "Plaintext Frame Body",
234 		    frame1 + 12, sizeof(frame1) - 12);
235 
236 	enc = ccmp_encrypt_pv1(tk, &frame1[2], sa, da, frame1, sizeof(frame1),
237 			       12, pn, key_id, &enc_len);
238 	if (enc == NULL) {
239 		wpa_printf(MSG_ERROR, "Failed to encrypt CCMP frame");
240 		return;
241 	}
242 
243 	wpa_hexdump(MSG_INFO, "Encrypted Frame Header", enc, 12);
244 	wpa_hexdump(MSG_INFO, "Encrypted Frame Frame Body",
245 		    enc + 12, enc_len - 12);
246 	WPA_PUT_LE32(fcs, crc32(enc, enc_len));
247 	wpa_hexdump(MSG_INFO, "Encrypted Frame FCS", fcs, sizeof(fcs));
248 
249 	wpa_printf(MSG_INFO,
250 		   "\nPV1 test vector #2:\nHeader compression used and A3 was not previously stored at the receiver\n");
251 	fc = WPA_GET_LE16(frame2);
252 	wpa_printf(MSG_INFO,
253 		   "FC=0x%04x (PV=%u Type=%u PTID/Subtype=%u From_DS=%u More_Fragments=%u Power_Management=%u More_Data=%u Protected_Frame=%u End_of_SP=%u Relayed_Frame=%u Ack_Policy=%u)",
254 		   fc,
255 		   fc & WLAN_FC_PVER,
256 		   (fc & (BIT(2) | BIT(3) | BIT(4))) >> 2,
257 		   (fc & (BIT(5) | BIT(6) | BIT(7))) >> 5,
258 		   !!(fc & BIT(8)),
259 		   !!(fc & BIT(9)),
260 		   !!(fc & BIT(10)),
261 		   !!(fc & BIT(11)),
262 		   !!(fc & BIT(12)),
263 		   !!(fc & BIT(13)),
264 		   !!(fc & BIT(14)),
265 		   !!(fc & BIT(15)));
266 	wpa_printf(MSG_INFO, "A1=" MACSTR, MAC2STR(&frame2[2]));
267 	sid = WPA_GET_LE16(&frame2[8]);
268 	wpa_printf(MSG_INFO,
269 		   "A2=%02x %02x (SID: AID=%u A3_Present=%u A4_Present=%u A-MSDU=%u); corresponds to 52:30:f1:84:44:08 in uncompressed header",
270 		   frame2[8], frame2[9],
271 		   sid & ~(BIT(13) | BIT(14) | BIT(15)),
272 		   !!(sid & BIT(13)),
273 		   !!(sid & BIT(14)),
274 		   !!(sid & BIT(15)));
275 	sc = WPA_GET_LE16(&frame2[10]);
276 	wpa_printf(MSG_INFO, "Sequence Control: %02x %02x (FN=%u SN=%u)",
277 		   frame2[10], frame2[11],
278 		   WLAN_GET_SEQ_FRAG(sc), WLAN_GET_SEQ_SEQ(sc));
279 	wpa_printf(MSG_INFO, "A3=" MACSTR, MAC2STR(&frame2[12]));
280 	wpa_printf(MSG_INFO, "A4 not present");
281 	wpa_hexdump(MSG_INFO, "Plaintext Frame Header", frame2, 18);
282 	wpa_hexdump(MSG_INFO, "Plaintext Frame Body",
283 		    frame2 + 18, sizeof(frame2) - 18);
284 
285 	enc = ccmp_encrypt_pv1(tk, &frame2[2], sa, &frame2[12],
286 			       frame2, sizeof(frame2), 18, pn, key_id,
287 			       &enc_len);
288 	if (enc == NULL) {
289 		wpa_printf(MSG_ERROR, "Failed to encrypt CCMP frame");
290 		return;
291 	}
292 
293 	wpa_hexdump(MSG_INFO, "Encrypted Frame Header", enc, 18);
294 	wpa_hexdump(MSG_INFO, "Encrypted Frame Frame Body",
295 		    enc + 18, enc_len - 18);
296 	WPA_PUT_LE32(fcs, crc32(enc, enc_len));
297 	wpa_hexdump(MSG_INFO, "Encrypted Frame FCS", fcs, sizeof(fcs));
298 
299 	wpa_printf(MSG_INFO,
300 		   "\nPV1 test vector #3:\nType 3 frame from SA to DA(=BSSID) (i.e., no separate DA in this example)\n");
301 	fc = WPA_GET_LE16(frame3);
302 	wpa_printf(MSG_INFO,
303 		   "FC=0x%04x (PV=%u Type=%u PTID/Subtype=%u From_DS=%u More_Fragments=%u Power_Management=%u More_Data=%u Protected_Frame=%u End_of_SP=%u Relayed_Frame=%u Ack_Policy=%u)",
304 		   fc,
305 		   fc & WLAN_FC_PVER,
306 		   (fc & (BIT(2) | BIT(3) | BIT(4))) >> 2,
307 		   (fc & (BIT(5) | BIT(6) | BIT(7))) >> 5,
308 		   !!(fc & BIT(8)),
309 		   !!(fc & BIT(9)),
310 		   !!(fc & BIT(10)),
311 		   !!(fc & BIT(11)),
312 		   !!(fc & BIT(12)),
313 		   !!(fc & BIT(13)),
314 		   !!(fc & BIT(14)),
315 		   !!(fc & BIT(15)));
316 	wpa_printf(MSG_INFO, "A1=" MACSTR, MAC2STR(&frame3[2]));
317 	wpa_printf(MSG_INFO, "A2=" MACSTR, MAC2STR(&frame3[8]));
318 	sc = WPA_GET_LE16(&frame3[14]);
319 	wpa_printf(MSG_INFO, "Sequence Control: %02x %02x (FN=%u SN=%u)",
320 		   frame3[14], frame3[15],
321 		   WLAN_GET_SEQ_FRAG(sc), WLAN_GET_SEQ_SEQ(sc));
322 	wpa_printf(MSG_INFO,
323 		   "A3 not present; corresponds to 02:d2:e1:28:a5:7c in uncompressed header");
324 	wpa_printf(MSG_INFO, "A4 not present");
325 	wpa_hexdump(MSG_INFO, "Plaintext Frame Header", frame3, 16);
326 	wpa_hexdump(MSG_INFO, "Plaintext Frame Body",
327 		    frame3 + 16, sizeof(frame3) - 16);
328 
329 	enc = ccmp_encrypt_pv1(tk, &frame3[2], &frame3[8], da,
330 			       frame3, sizeof(frame3), 16, pn, key_id,
331 			       &enc_len);
332 	if (enc == NULL) {
333 		wpa_printf(MSG_ERROR, "Failed to encrypt CCMP frame");
334 		return;
335 	}
336 
337 	wpa_hexdump(MSG_INFO, "Encrypted Frame Header", enc, 16);
338 	wpa_hexdump(MSG_INFO, "Encrypted Frame Frame Body",
339 		    enc + 16, enc_len - 16);
340 	WPA_PUT_LE32(fcs, crc32(enc, enc_len));
341 	wpa_hexdump(MSG_INFO, "Encrypted Frame FCS", fcs, sizeof(fcs));
342 
343 	wpa_debug_level = MSG_INFO;
344 }
345 
346 
test_vector_bip(void)347 static void test_vector_bip(void)
348 {
349 	u8 igtk[] = {
350 		0x4e, 0xa9, 0x54, 0x3e, 0x09, 0xcf, 0x2b, 0x1e,
351 		0xca, 0x66, 0xff, 0xc5, 0x8b, 0xde, 0xcb, 0xcf
352 	};
353 	u8 ipn[] = { 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 };
354 	u8 frame[] = {
355 		0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
356 		0xff, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
357 		0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
358 		0x02, 0x00
359 	};
360 	u8 *prot;
361 	size_t prot_len;
362 
363 	wpa_printf(MSG_INFO, "\nIEEE Std 802.11-2012, M.9.1 BIP with broadcast "
364 		   "Deauthentication frame\n");
365 
366 	wpa_hexdump(MSG_INFO, "IGTK", igtk, sizeof(igtk));
367 	wpa_hexdump(MSG_INFO, "IPN", ipn, sizeof(ipn));
368 	wpa_hexdump(MSG_INFO, "Plaintext frame", frame, sizeof(frame));
369 
370 	prot = bip_protect(igtk, sizeof(igtk), frame, sizeof(frame),
371 			   ipn, 4, &prot_len);
372 	if (prot == NULL) {
373 		wpa_printf(MSG_ERROR, "Failed to protect BIP frame");
374 		return;
375 	}
376 
377 	wpa_hexdump(MSG_INFO, "Protected MPDU (without FCS)", prot, prot_len);
378 	os_free(prot);
379 }
380 
381 
test_vector_ccmp_mgmt(void)382 static void test_vector_ccmp_mgmt(void)
383 {
384 	u8 tk[] = { 0x66, 0xed, 0x21, 0x04, 0x2f, 0x9f, 0x26, 0xd7,
385 		    0x11, 0x57, 0x06, 0xe4, 0x04, 0x14, 0xcf, 0x2e };
386 	u8 pn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
387 	u8 frame[] = {
388 		0xc0, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
389 		0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
390 		0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00,
391 		0x02, 0x00
392 	};
393 	u8 *enc, *plain;
394 	size_t enc_len, plain_len;
395 
396 	wpa_printf(MSG_INFO, "\nIEEE Std 802.11-2012, M.9.2 CCMP with unicast "
397 		   "Deauthentication frame\n");
398 
399 	wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
400 	wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
401 	wpa_hexdump(MSG_INFO, "802.11 Header", frame, 24);
402 	wpa_hexdump(MSG_INFO, "Plaintext Data", frame + 24, sizeof(frame) - 24);
403 
404 	enc = ccmp_encrypt(tk, frame, sizeof(frame), 24, NULL, pn, 0, &enc_len);
405 	if (enc == NULL) {
406 		wpa_printf(MSG_ERROR, "Failed to encrypt CCMP frame");
407 		return;
408 	}
409 
410 	wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
411 
412 	wpa_debug_level = MSG_INFO;
413 	plain = ccmp_decrypt(tk, (const struct ieee80211_hdr *) enc,
414 			     enc + 24, enc_len - 24, &plain_len);
415 	wpa_debug_level = MSG_EXCESSIVE;
416 	os_free(enc);
417 
418 	if (plain == NULL) {
419 		wpa_printf(MSG_ERROR, "Failed to decrypt CCMP frame");
420 		return;
421 	}
422 
423 	if (plain_len != sizeof(frame) - 24 ||
424 	    os_memcmp(plain, frame + 24, plain_len) != 0) {
425 		wpa_hexdump(MSG_ERROR, "Decryption result did not match",
426 			    plain, plain_len);
427 	}
428 
429 	os_free(plain);
430 }
431 
432 
433 struct gcmp_test {
434 	u8 tk[16];
435 	u8 pn[6];
436 	u8 frame[300];
437 	size_t hdr_len;
438 	size_t payload_len;
439 	u8 mic[16];
440 	u8 encr[300];
441 };
442 
443 static const struct gcmp_test gcmp_vectors[] =
444 {
445 	{
446 		.tk = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
447 			0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa },
448 		.pn = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
449 		.frame = {
450 			0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
451 			0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
452 			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
453 
454 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
455 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
456 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
457 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
458 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
459 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
460 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
461 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
462 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
463 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
464 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
465 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
466 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
467 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
468 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
469 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
470 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
471 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
472 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
473 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
474 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
475 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
476 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
477 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
478 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
479 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
480 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
481 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
482 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
483 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
484 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
485 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
486 		},
487 		.hdr_len = 24,
488 		.payload_len = 256,
489 		.mic = {
490 			0x80, 0xCB, 0x06, 0x62, 0xEA, 0x71, 0xAB, 0xFD,
491 			0x9F, 0x04, 0xC7, 0xF8, 0x72, 0xF5, 0x80, 0x90 },
492 		.encr = {
493 			0x5F, 0x55, 0x78, 0xC1, 0x8F, 0x13, 0x7A, 0xD2,
494 			0x79, 0xBF, 0x3F, 0x2B, 0x24, 0xC7, 0xBD, 0x8F,
495 			0x27, 0x7A, 0x1B, 0xE6, 0x77, 0x0D, 0xA1, 0xD9,
496 			0x8B, 0x70, 0xC6, 0xD2, 0x8A, 0xE0, 0x1C, 0x55,
497 			0x9E, 0xCB, 0xA6, 0xA0, 0x1D, 0xB0, 0x67, 0xC5,
498 			0xA2, 0x7E, 0x4D, 0xB0, 0x8C, 0xDA, 0xDC, 0x77,
499 			0x52, 0xAD, 0x63, 0x7E, 0xAF, 0x0A, 0x18, 0xED,
500 			0x13, 0xFB, 0xAA, 0x14, 0x3B, 0xAF, 0xEF, 0x18,
501 			0xF8, 0xFB, 0xCE, 0x4C, 0x65, 0xE8, 0x6B, 0xD0,
502 			0x2A, 0x87, 0xB6, 0x01, 0xB7, 0xEA, 0xB9, 0x3F,
503 			0x2B, 0xBC, 0x87, 0x4C, 0x8A, 0x71, 0x05, 0x80,
504 			0xF5, 0x02, 0x34, 0x1A, 0x6A, 0x53, 0x39, 0x31,
505 			0x43, 0xDE, 0x4C, 0x9E, 0xC6, 0xA2, 0x86, 0xF1,
506 			0x25, 0x71, 0x83, 0x78, 0xAE, 0xDC, 0x84, 0xEB,
507 			0xA2, 0xB3, 0x0F, 0x5C, 0x28, 0xBB, 0x5D, 0x75,
508 			0xC6, 0xB0, 0x25, 0x46, 0x6D, 0x06, 0x51, 0xC7,
509 			0x22, 0xDC, 0x71, 0x15, 0x1F, 0x21, 0x2D, 0x68,
510 			0x87, 0x82, 0x8A, 0x03, 0x82, 0xE9, 0x28, 0x8A,
511 			0x7F, 0x43, 0xD5, 0x2B, 0x7D, 0x25, 0x08, 0x61,
512 			0x57, 0x64, 0x69, 0x54, 0xBB, 0x43, 0xB5, 0x7E,
513 			0xA5, 0x87, 0xA0, 0x25, 0xF4, 0x0C, 0xE7, 0x45,
514 			0x11, 0xE4, 0xDD, 0x22, 0x85, 0xB4, 0x0B, 0xA3,
515 			0xF3, 0xB9, 0x62, 0x62, 0xCB, 0xC2, 0x8C, 0x6A,
516 			0xA7, 0xBE, 0x44, 0x3E, 0x7B, 0x41, 0xE1, 0xEB,
517 			0xFF, 0x52, 0x48, 0x57, 0xA6, 0x81, 0x68, 0x97,
518 			0x75, 0x01, 0x15, 0xB0, 0x23, 0x1A, 0xB7, 0xC2,
519 			0x84, 0x72, 0xC0, 0x6D, 0xD0, 0xB4, 0x9B, 0xE9,
520 			0xF3, 0x69, 0xA8, 0xC3, 0x9C, 0xCD, 0x0D, 0xB7,
521 			0x98, 0x35, 0x10, 0xE1, 0xAE, 0x8F, 0x05, 0xD7,
522 			0x75, 0x45, 0xE0, 0x23, 0x5C, 0xDB, 0xD6, 0x12,
523 			0xF3, 0x15, 0x07, 0x54, 0xCE, 0xE5, 0xCE, 0x6A,
524 			0x12, 0x25, 0xD9, 0x95, 0x25, 0x02, 0x6F, 0x74
525 		}
526 	},
527 	{
528 		.tk = { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
529 			0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f },
530 		.pn = { 0x00, 0x89, 0x5F, 0x5F, 0x2B, 0x08 },
531 		.frame = {
532 			0x88, 0x48, 0x0b, 0x00, 0x0f, 0xd2, 0xe1, 0x28,
533 			0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
534 			0x50, 0x30, 0xf1, 0x84, 0x44, 0x08, 0x80, 0x33,
535 			0x03, 0x00,
536 
537 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
538 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
539 			0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
540 			0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
541 			0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27
542 		},
543 		.hdr_len = 26,
544 		.payload_len = 40,
545 		.mic = {
546 			0xde, 0xf6, 0x19, 0xc2, 0xa3, 0x74, 0xb6, 0xdf,
547 			0x66, 0xff, 0xa5, 0x3b, 0x6c, 0x69, 0xd7, 0x9e },
548 		.encr = {
549 			0x60, 0xe9, 0x70, 0x0c, 0xc4, 0xd4, 0x0a, 0xc6,
550 			0xd2, 0x88, 0xb2, 0x01, 0xc3, 0x8f, 0x5b, 0xf0,
551 			0x8b, 0x80, 0x74, 0x42, 0x64, 0x0a, 0x15, 0x96,
552 			0xe5, 0xdb, 0xda, 0xd4, 0x1d, 0x1f, 0x36, 0x23,
553 			0xf4, 0x5d, 0x7a, 0x12, 0xdb, 0x7a, 0xfb, 0x23
554 		}
555 	}
556 };
557 
558 
run_gcmp(int idx,const struct gcmp_test * vector)559 static int run_gcmp(int idx, const struct gcmp_test *vector)
560 {
561 	u8 *enc, *plain;
562 	size_t enc_len, plain_len;
563 	u8 fcs[4];
564 	int err = 0;
565 
566 	wpa_printf(MSG_INFO,
567 		   "\nIEEE Std 802.11ad-2012, M.11.1 GCMP test mpdu #%d\n",
568 		   idx);
569 
570 	wpa_hexdump(MSG_INFO, "TK", vector->tk, sizeof(vector->tk));
571 	wpa_hexdump(MSG_INFO, "PN", vector->pn, sizeof(vector->pn));
572 	wpa_hexdump(MSG_INFO, "802.11 Header", vector->frame, vector->hdr_len);
573 	wpa_hexdump(MSG_INFO, "Plaintext Data",
574 		    vector->frame + vector->hdr_len,
575 		    vector->payload_len);
576 
577 	enc = gcmp_encrypt(vector->tk, sizeof(vector->tk),
578 			   vector->frame,
579 			   vector->hdr_len + vector->payload_len,
580 			   vector->hdr_len,
581 			   vector->hdr_len == 26 ?
582 			   vector->frame + vector->hdr_len - 2 : NULL,
583 			   vector->pn, 0, &enc_len);
584 	if (enc == NULL) {
585 		wpa_printf(MSG_ERROR, "Failed to encrypt GCMP frame");
586 		return 1;
587 	}
588 
589 	wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
590 	if (os_memcmp(vector->encr, enc + vector->hdr_len + 8,
591 		      vector->payload_len) != 0) {
592 		wpa_printf(MSG_ERROR, "GCMP test mpdu #%d enctypted data mismatch",
593 			   idx);
594 		err++;
595 	}
596 	if (os_memcmp(vector->mic, enc + enc_len - sizeof(vector->mic),
597 		      sizeof(vector->mic)) != 0) {
598 		wpa_printf(MSG_ERROR, "GCMP test mpdu #%d MIC mismatch", idx);
599 		err++;
600 	}
601 	WPA_PUT_LE32(fcs, crc32(enc, enc_len));
602 	wpa_hexdump(MSG_INFO, "FCS", fcs, sizeof(fcs));
603 
604 	wpa_debug_level = MSG_INFO;
605 	plain = gcmp_decrypt(vector->tk, sizeof(vector->tk),
606 			     (const struct ieee80211_hdr *) enc,
607 			     enc + vector->hdr_len,
608 			     enc_len - vector->hdr_len, &plain_len);
609 	wpa_debug_level = MSG_EXCESSIVE;
610 	os_free(enc);
611 
612 	if (plain == NULL) {
613 		wpa_printf(MSG_ERROR, "Failed to decrypt GCMP frame");
614 		return 1;
615 	}
616 
617 	if (plain_len != vector->payload_len ||
618 	    os_memcmp(plain, vector->frame + vector->hdr_len, plain_len) != 0) {
619 		wpa_hexdump(MSG_ERROR, "Decryption result did not match",
620 			    plain, plain_len);
621 		err++;
622 	}
623 
624 	os_free(plain);
625 
626 	return err;
627 }
628 
629 
test_vector_gcmp(void)630 static int test_vector_gcmp(void)
631 {
632 	int err = 0;
633 	int i;
634 
635 	for (i = 0; i < ARRAY_SIZE(gcmp_vectors); i++) {
636 		if (run_gcmp(i + 1, &gcmp_vectors[i]))
637 			err++;
638 
639 	}
640 
641 	return err;
642 }
643 
644 
test_vector_gcmp_256(void)645 static int test_vector_gcmp_256(void)
646 {
647 	u8 tk[] = { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
648 		    0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f,
649 		    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
650 		    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
651 	u8 pn[] = {
652 		0x00, 0x89, 0x5F, 0x5F, 0x2B, 0x08
653 	};
654 	u8 frame[] = {
655 		0x88, 0x48, 0x0b, 0x00, 0x0f, 0xd2, 0xe1, 0x28,
656 		0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
657 		0x50, 0x30, 0xf1, 0x84, 0x44, 0x08, 0x80, 0x33,
658 		0x03, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
659 		0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
660 		0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
661 		0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
662 		0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
663 		0x26, 0x27
664 	};
665 	u8 encr[] = {
666 		0x88, 0x48, 0x0b, 0x00, 0x0f, 0xd2, 0xe1, 0x28,
667 		0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
668 		0x50, 0x30, 0xf1, 0x84, 0x44, 0x08, 0x80, 0x33,
669 		0x03, 0x00, 0x08, 0x2b, 0x00, 0x20, 0x5f, 0x5f,
670 		0x89, 0x00, 0x65, 0x83, 0x43, 0xc8, 0xb1, 0x44,
671 		0x47, 0xd9, 0x21, 0x1d, 0xef, 0xd4, 0x6a, 0xd8,
672 		0x9c, 0x71, 0x0c, 0x6f, 0xc3, 0x33, 0x33, 0x23,
673 		0x6e, 0x39, 0x97, 0xb9, 0x17, 0x6a, 0x5a, 0x8b,
674 		0xe7, 0x79, 0xb2, 0x12, 0x66, 0x55, 0x5e, 0x70,
675 		0xad, 0x79, 0x11, 0x43, 0x16, 0x85, 0x90, 0x95,
676 		0x47, 0x3d, 0x5b, 0x1b, 0xd5, 0x96, 0xb3, 0xde,
677 		0xa3, 0xbf
678 	};
679 	u8 *enc, *plain;
680 	size_t enc_len, plain_len;
681 	u8 fcs[4];
682 	int err = 0;
683 
684 	wpa_printf(MSG_INFO, "\nIEEE P802.11ac/D7.0, M.11.1 GCMP-256 test vector\n");
685 
686 	wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
687 	wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
688 	wpa_hexdump(MSG_INFO, "802.11 Header", frame, 26);
689 	wpa_hexdump(MSG_INFO, "Plaintext Data", frame + 26, sizeof(frame) - 26);
690 
691 	enc = gcmp_encrypt(tk, sizeof(tk), frame, sizeof(frame), 26, frame + 24,
692 			   pn, 0, &enc_len);
693 	if (enc == NULL) {
694 		wpa_printf(MSG_ERROR, "Failed to encrypt GCMP frame");
695 		return 1;
696 	}
697 
698 	wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
699 	if (enc_len != sizeof(encr) || os_memcmp(enc, encr, enc_len) != 0) {
700 		wpa_printf(MSG_ERROR, "GCMP-256 test vector mismatch");
701 		err++;
702 	}
703 	WPA_PUT_LE32(fcs, crc32(enc, enc_len));
704 	wpa_hexdump(MSG_INFO, "FCS", fcs, sizeof(fcs));
705 
706 	wpa_debug_level = MSG_INFO;
707 	plain = gcmp_decrypt(tk, sizeof(tk), (const struct ieee80211_hdr *) enc,
708 			     enc + 26, enc_len - 26, &plain_len);
709 	wpa_debug_level = MSG_EXCESSIVE;
710 	os_free(enc);
711 
712 	if (plain == NULL) {
713 		wpa_printf(MSG_ERROR, "Failed to decrypt GCMP frame");
714 		return 1;
715 	}
716 
717 	if (plain_len != sizeof(frame) - 26 ||
718 	    os_memcmp(plain, frame + 26, plain_len) != 0) {
719 		wpa_hexdump(MSG_ERROR, "Decryption result did not match",
720 			    plain, plain_len);
721 		err++;
722 	}
723 
724 	os_free(plain);
725 
726 	return err;
727 }
728 
729 
test_vector_ccmp_256(void)730 static int test_vector_ccmp_256(void)
731 {
732 	u8 tk[] = { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
733 		    0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f,
734 		    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
735 		    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
736 	u8 pn[] = { 0xB5, 0x03, 0x97, 0x76, 0xE7, 0x0C };
737 	u8 frame[] = {
738 		0x08, 0x48, 0xc3, 0x2c, 0x0f, 0xd2, 0xe1, 0x28,
739 		0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
740 		0xab, 0xae, 0xa5, 0xb8, 0xfc, 0xba, 0x80, 0x33,
741 		0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
742 		0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
743 		0x7e, 0x78, 0xa0, 0x50
744 	};
745 	u8 encr[] = {
746 		0x08, 0x48, 0xc3, 0x2c, 0x0f, 0xd2, 0xe1, 0x28,
747 		0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
748 		0xab, 0xae, 0xa5, 0xb8, 0xfc, 0xba, 0x80, 0x33,
749 		0x0c, 0xe7, 0x00, 0x20, 0x76, 0x97, 0x03, 0xb5,
750 		0x6d, 0x15, 0x5d, 0x88, 0x32, 0x66, 0x82, 0x56,
751 		0xd6, 0xa9, 0x2b, 0x78, 0xe1, 0x1d, 0x8e, 0x54,
752 		0x49, 0x5d, 0xd1, 0x74, 0x80, 0xaa, 0x56, 0xc9,
753 		0x49, 0x2e, 0x88, 0x2b, 0x97, 0x64, 0x2f, 0x80,
754 		0xd5, 0x0f, 0xe9, 0x7b
755 
756 	};
757 	u8 *enc, *plain;
758 	size_t enc_len, plain_len;
759 	u8 fcs[4];
760 	int err = 0;
761 
762 	wpa_printf(MSG_INFO, "\nIEEE P802.11ac/D7.0, M.6.4 CCMP-256 test vector\n");
763 
764 	wpa_hexdump(MSG_INFO, "TK", tk, sizeof(tk));
765 	wpa_hexdump(MSG_INFO, "PN", pn, sizeof(pn));
766 	wpa_hexdump(MSG_INFO, "802.11 Header", frame, 24);
767 	wpa_hexdump(MSG_INFO, "Plaintext Data", frame + 24, sizeof(frame) - 24);
768 
769 	enc = ccmp_256_encrypt(tk, frame, sizeof(frame), 24, NULL, pn, 0,
770 			       &enc_len);
771 	if (enc == NULL) {
772 		wpa_printf(MSG_ERROR, "Failed to encrypt CCMP frame");
773 		return 1;
774 	}
775 
776 	wpa_hexdump(MSG_INFO, "Encrypted MPDU (without FCS)", enc, enc_len);
777 	if (enc_len != sizeof(encr) || os_memcmp(enc, encr, enc_len) != 0) {
778 		wpa_printf(MSG_ERROR, "CCMP-256 test vector mismatch");
779 		err++;
780 	}
781 	WPA_PUT_LE32(fcs, crc32(enc, enc_len));
782 	wpa_hexdump(MSG_INFO, "FCS", fcs, sizeof(fcs));
783 
784 	wpa_debug_level = MSG_INFO;
785 	plain = ccmp_256_decrypt(tk, (const struct ieee80211_hdr *) enc,
786 				 enc + 24, enc_len - 24, &plain_len);
787 	wpa_debug_level = MSG_EXCESSIVE;
788 	os_free(enc);
789 
790 	if (plain == NULL) {
791 		wpa_printf(MSG_ERROR, "Failed to decrypt CCMP-256 frame");
792 		return 1;
793 	}
794 
795 	if (plain_len != sizeof(frame) - 24 ||
796 	    os_memcmp(plain, frame + 24, plain_len) != 0) {
797 		wpa_hexdump(MSG_ERROR, "Decryption result did not match",
798 			    plain, plain_len);
799 		err++;
800 	}
801 
802 	os_free(plain);
803 
804 	return err;
805 }
806 
807 
test_vector_bip_gmac_128(void)808 static int test_vector_bip_gmac_128(void)
809 {
810 	u8 igtk[] = {
811 		0x4e, 0xa9, 0x54, 0x3e, 0x09, 0xcf, 0x2b, 0x1e,
812 		0xca, 0x66, 0xff, 0xc5, 0x8b, 0xde, 0xcb, 0xcf
813 	};
814 	u8 ipn[] = { 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 };
815 	u8 frame[] = {
816 		0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
817 		0xff, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
818 		0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
819 		0x02, 0x00
820 	};
821 	u8 res[] = {
822 		0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
823 		0xff, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
824 		0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
825 		0x02, 0x00, 0x4c, 0x18, 0x04, 0x00, 0x04, 0x00,
826 		0x00, 0x00, 0x00, 0x00, 0x3e, 0xd8, 0x62, 0xfb,
827 		0x0f, 0x33, 0x38, 0xdd, 0x33, 0x86, 0xc8, 0x97,
828 		0xe2, 0xed, 0x05, 0x3d
829 	};
830 	u8 *prot;
831 	size_t prot_len;
832 	int err = 0;
833 
834 	wpa_printf(MSG_INFO, "\nIEEE P802.11ac/D7.0, M.9.1 BIP-GMAC-128 with broadcast "
835 		   "Deauthentication frame\n");
836 
837 	wpa_hexdump(MSG_INFO, "IGTK", igtk, sizeof(igtk));
838 	wpa_hexdump(MSG_INFO, "IPN", ipn, sizeof(ipn));
839 	wpa_hexdump(MSG_INFO, "Plaintext frame", frame, sizeof(frame));
840 
841 	prot = bip_gmac_protect(igtk, sizeof(igtk), frame, sizeof(frame),
842 				ipn, 4, &prot_len);
843 	if (prot == NULL) {
844 		wpa_printf(MSG_ERROR, "Failed to protect BIP-GMAC-128 frame");
845 		return 1;
846 	}
847 
848 	wpa_hexdump(MSG_INFO, "Protected MPDU (without FCS)", prot, prot_len);
849 	if (prot_len != sizeof(res) || os_memcmp(res, prot, prot_len) != 0) {
850 		wpa_printf(MSG_ERROR, "BIP-GMAC-128 test vector mismatch");
851 		err++;
852 	}
853 	os_free(prot);
854 
855 	return err;
856 }
857 
858 
test_vector_bip_gmac_256(void)859 static int test_vector_bip_gmac_256(void)
860 {
861 	u8 igtk[] = {
862 		0x4e, 0xa9, 0x54, 0x3e, 0x09, 0xcf, 0x2b, 0x1e,
863 		0xca, 0x66, 0xff, 0xc5, 0x8b, 0xde, 0xcb, 0xcf,
864 		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
865 		0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
866 	};
867 	u8 ipn[] = { 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 };
868 	u8 frame[] = {
869 		0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
870 		0xff, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
871 		0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
872 		0x02, 0x00
873 	};
874 	u8 res[] = {
875 		0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
876 		0xff, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
877 		0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
878 		0x02, 0x00, 0x4c, 0x18, 0x04, 0x00, 0x04, 0x00,
879 		0x00, 0x00, 0x00, 0x00, 0x23, 0xbe, 0x59, 0xdc,
880 		0xc7, 0x02, 0x2e, 0xe3, 0x83, 0x62, 0x7e, 0xbb,
881 		0x10, 0x17, 0xdd, 0xfc
882 	};
883 	u8 *prot;
884 	size_t prot_len;
885 	int err = 0;
886 
887 	wpa_printf(MSG_INFO, "\nIEEE P802.11ac/D7.0, M.9.1 BIP-GMAC-256 with broadcast Deauthentication frame\n");
888 
889 	wpa_hexdump(MSG_INFO, "IGTK", igtk, sizeof(igtk));
890 	wpa_hexdump(MSG_INFO, "IPN", ipn, sizeof(ipn));
891 	wpa_hexdump(MSG_INFO, "Plaintext frame", frame, sizeof(frame));
892 
893 	prot = bip_gmac_protect(igtk, sizeof(igtk), frame, sizeof(frame),
894 				ipn, 4, &prot_len);
895 	if (prot == NULL) {
896 		wpa_printf(MSG_ERROR, "Failed to protect BIP-GMAC-256 frame");
897 		return 1;
898 	}
899 
900 	wpa_hexdump(MSG_INFO, "Protected MPDU (without FCS)", prot, prot_len);
901 	if (prot_len != sizeof(res) || os_memcmp(res, prot, prot_len) != 0) {
902 		wpa_printf(MSG_ERROR, "BIP-GMAC-128 test vector mismatch");
903 		err++;
904 	}
905 	os_free(prot);
906 
907 	return err;
908 }
909 
910 
main(int argc,char * argv[])911 int main(int argc, char *argv[])
912 {
913 	int errors = 0;
914 
915 	wpa_debug_level = MSG_EXCESSIVE;
916 	wpa_debug_show_keys = 1;
917 
918 	if (os_program_init())
919 		return -1;
920 
921 	test_vector_tkip();
922 	test_vector_ccmp();
923 	test_vector_ccmp_pv1();
924 	test_vector_bip();
925 	test_vector_ccmp_mgmt();
926 	errors += test_vector_gcmp();
927 	errors += test_vector_gcmp_256();
928 	errors += test_vector_ccmp_256();
929 	errors += test_vector_bip_gmac_128();
930 	errors += test_vector_bip_gmac_256();
931 
932 	if (errors)
933 		wpa_printf(MSG_INFO, "One or more test vectors failed");
934 	os_program_deinit();
935 
936 	return errors ? -1 : 0;
937 }
938