eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

test-wsc.c (95594B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2015-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <stdio.h>
     28 #include <string.h>
     29 #include <assert.h>
     30 #include <stdlib.h>
     31 #include <linux/if_ether.h>
     32 #include <ell/ell.h>
     33 
     34 #include "src/wscutil.h"
     35 #include "src/eapol.h"
     36 #include "src/eap.h"
     37 #include "src/eap-wsc.h"
     38 #include "src/crypto.h"
     39 #include "src/util.h"
     40 #include "src/mpdu.h"
     41 #include "src/ie.h"
     42 #include "src/handshake.h"
     43 
     44 struct test_handshake_state {
     45 	struct handshake_state super;
     46 };
     47 
     48 static void test_handshake_state_free(struct handshake_state *hs)
     49 {
     50 	struct test_handshake_state *ths =
     51 			l_container_of(hs, struct test_handshake_state, super);
     52 
     53 	l_free(ths);
     54 }
     55 
     56 static struct handshake_state *test_handshake_state_new(uint32_t ifindex)
     57 {
     58 	struct test_handshake_state *ths;
     59 
     60 	ths = l_new(struct test_handshake_state, 1);
     61 
     62 	ths->super.ifindex = ifindex;
     63 	ths->super.free = test_handshake_state_free;
     64 	ths->super.refcount = 1;
     65 
     66 	return &ths->super;
     67 }
     68 
     69 static const unsigned char wsc_attrs1[] = {
     70 	0x10, 0x4a, 0x00, 0x01, 0x10, 0x10, 0x44, 0x00, 0x01, 0x02, 0x10, 0x41,
     71 	0x00, 0x01, 0x01, 0x10, 0x12, 0x00, 0x02, 0x00, 0x04, 0x10, 0x53, 0x00,
     72 	0x02, 0x26, 0x88, 0x10, 0x3b, 0x00, 0x01, 0x03, 0x10, 0x47, 0x00, 0x10,
     73 	0xc4, 0x4a, 0xad, 0x8d, 0x25, 0x2f, 0x52, 0xc6, 0xf9, 0x6b, 0x38, 0x5d,
     74 	0xcb, 0x23, 0x31, 0xae, 0x10, 0x21, 0x00, 0x15, 0x41, 0x53, 0x55, 0x53,
     75 	0x54, 0x65, 0x4b, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x75, 0x74, 0x65, 0x72,
     76 	0x20, 0x49, 0x6e, 0x63, 0x2e, 0x10, 0x23, 0x00, 0x1c, 0x57, 0x69, 0x2d,
     77 	0x46, 0x69, 0x20, 0x50, 0x72, 0x6f, 0x74, 0x65, 0x63, 0x74, 0x65, 0x64,
     78 	0x20, 0x53, 0x65, 0x74, 0x75, 0x70, 0x20, 0x52, 0x6f, 0x75, 0x74, 0x65,
     79 	0x72, 0x10, 0x24, 0x00, 0x08, 0x52, 0x54, 0x2d, 0x41, 0x43, 0x36, 0x38,
     80 	0x55, 0x10, 0x42, 0x00, 0x11, 0x31, 0x30, 0x3a, 0x63, 0x33, 0x3a, 0x37,
     81 	0x62, 0x3a, 0x35, 0x34, 0x3a, 0x37, 0x34, 0x3a, 0x64, 0x30, 0x10, 0x54,
     82 	0x00, 0x08, 0x00, 0x06, 0x00, 0x50, 0xf2, 0x04, 0x00, 0x01, 0x10, 0x11,
     83 	0x00, 0x08, 0x52, 0x54, 0x2d, 0x41, 0x43, 0x36, 0x38, 0x55, 0x10, 0x08,
     84 	0x00, 0x02, 0x20, 0x08, 0x10, 0x3c, 0x00, 0x01, 0x03, 0x10, 0x49, 0x00,
     85 	0x0e, 0x00, 0x37, 0x2a, 0x00, 0x01, 0x20, 0x01, 0x06, 0xff, 0xff, 0xff,
     86 	0xff, 0xff, 0xff,
     87 };
     88 
     89 static void wsc_test_iter_sanity_check(const void *data)
     90 {
     91 	struct wsc_attr_iter iter;
     92 	struct wsc_wfa_ext_iter wfa_iter;
     93 
     94 	wsc_attr_iter_init(&iter, wsc_attrs1, sizeof(wsc_attrs1));
     95 
     96 	assert(wsc_attr_iter_next(&iter));
     97 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_VERSION);
     98 	assert(wsc_attr_iter_get_length(&iter) == 1);
     99 
    100 	assert(wsc_attr_iter_next(&iter));
    101 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_WSC_STATE);
    102 	assert(wsc_attr_iter_get_length(&iter) == 1);
    103 
    104 	assert(wsc_attr_iter_next(&iter));
    105 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_SELECTED_REGISTRAR);
    106 	assert(wsc_attr_iter_get_length(&iter) == 1);
    107 
    108 	assert(wsc_attr_iter_next(&iter));
    109 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_DEVICE_PASSWORD_ID);
    110 	assert(wsc_attr_iter_get_length(&iter) == 2);
    111 
    112 	assert(wsc_attr_iter_next(&iter));
    113 	assert(wsc_attr_iter_get_type(&iter) ==
    114 			WSC_ATTR_SELECTED_REGISTRAR_CONFIGURATION_METHODS);
    115 	assert(wsc_attr_iter_get_length(&iter) == 2);
    116 
    117 	assert(wsc_attr_iter_next(&iter));
    118 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_RESPONSE_TYPE);
    119 	assert(wsc_attr_iter_get_length(&iter) == 1);
    120 
    121 	assert(wsc_attr_iter_next(&iter));
    122 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_UUID_E);
    123 	assert(wsc_attr_iter_get_length(&iter) == 16);
    124 
    125 	assert(wsc_attr_iter_next(&iter));
    126 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_MANUFACTURER);
    127 	assert(wsc_attr_iter_get_length(&iter) == 21);
    128 
    129 	assert(wsc_attr_iter_next(&iter));
    130 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_MODEL_NAME);
    131 	assert(wsc_attr_iter_get_length(&iter) == 28);
    132 
    133 	assert(wsc_attr_iter_next(&iter));
    134 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_MODEL_NUMBER);
    135 	assert(wsc_attr_iter_get_length(&iter) == 8);
    136 
    137 	assert(wsc_attr_iter_next(&iter));
    138 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_SERIAL_NUMBER);
    139 	assert(wsc_attr_iter_get_length(&iter) == 17);
    140 
    141 	assert(wsc_attr_iter_next(&iter));
    142 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_PRIMARY_DEVICE_TYPE);
    143 	assert(wsc_attr_iter_get_length(&iter) == 8);
    144 
    145 	assert(wsc_attr_iter_next(&iter));
    146 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_DEVICE_NAME);
    147 	assert(wsc_attr_iter_get_length(&iter) == 8);
    148 
    149 	assert(wsc_attr_iter_next(&iter));
    150 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_CONFIGURATION_METHODS);
    151 	assert(wsc_attr_iter_get_length(&iter) == 2);
    152 
    153 	assert(wsc_attr_iter_next(&iter));
    154 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_RF_BANDS);
    155 	assert(wsc_attr_iter_get_length(&iter) == 1);
    156 
    157 	assert(wsc_attr_iter_next(&iter));
    158 	assert(wsc_attr_iter_get_type(&iter) == WSC_ATTR_VENDOR_EXTENSION);
    159 	assert(wsc_attr_iter_get_length(&iter) == 14);
    160 
    161 	assert(wsc_attr_iter_recurse_wfa_ext(&iter, &wfa_iter));
    162 
    163 	assert(wsc_wfa_ext_iter_next(&wfa_iter));
    164 	assert(wsc_wfa_ext_iter_get_type(&wfa_iter) ==
    165 			WSC_WFA_EXTENSION_VERSION2);
    166 	assert(wsc_wfa_ext_iter_get_length(&wfa_iter) == 1);
    167 
    168 	assert(wsc_wfa_ext_iter_next(&wfa_iter));
    169 	assert(wsc_wfa_ext_iter_get_type(&wfa_iter) ==
    170 			WSC_WFA_EXTENSION_AUTHORIZED_MACS);
    171 	assert(wsc_wfa_ext_iter_get_length(&wfa_iter) == 6);
    172 
    173 	assert(!wsc_attr_iter_next(&iter));
    174 }
    175 
    176 
    177 static const unsigned char beacon1[] = {
    178 	0x10, 0x4a, 0x00, 0x01, 0x10, 0x10, 0x44, 0x00, 0x01, 0x02, 0x10, 0x49,
    179 	0x00, 0x06, 0x00, 0x37, 0x2a, 0x00, 0x01, 0x20
    180 };
    181 
    182 struct beacon_data {
    183 	struct wsc_beacon expected;
    184 	const void *pdu;
    185 	unsigned int len;
    186 };
    187 
    188 static const struct beacon_data beacon_data_1 = {
    189 	.pdu = beacon1,
    190 	.len = sizeof(beacon1),
    191 	.expected = {
    192 		.version2 = true,
    193 		.state = WSC_STATE_CONFIGURED,
    194 		.ap_setup_locked = false,
    195 		.selected_registrar = false,
    196 	},
    197 };
    198 
    199 static void wsc_test_parse_beacon(const void *data)
    200 {
    201 	const struct beacon_data *test = data;
    202 	struct wsc_beacon beacon;
    203 	const struct wsc_beacon *expected = &test->expected;
    204 	int r;
    205 
    206 	r = wsc_parse_beacon(test->pdu, test->len, &beacon);
    207 	assert(r == 0);
    208 
    209 	assert(expected->version2 == beacon.version2);
    210 	assert(expected->state == beacon.state);
    211 	assert(expected->ap_setup_locked == beacon.ap_setup_locked);
    212 	assert(expected->selected_registrar == beacon.selected_registrar);
    213 	assert(expected->device_password_id == beacon.device_password_id);
    214 	assert(expected->selected_reg_config_methods ==
    215 				beacon.selected_reg_config_methods);
    216 	assert(!memcmp(expected->uuid_e, beacon.uuid_e, 16));
    217 	assert(expected->rf_bands == beacon.rf_bands);
    218 
    219 	assert(!memcmp(expected->authorized_macs,
    220 				beacon.authorized_macs,
    221 				sizeof(beacon.authorized_macs)));
    222 	assert(expected->reg_config_methods ==
    223 				beacon.reg_config_methods);
    224 }
    225 
    226 struct probe_response_data {
    227 	struct wsc_probe_response expected;
    228 	const void *pdu;
    229 	unsigned int len;
    230 };
    231 
    232 static const struct probe_response_data probe_response_data_1 = {
    233 	.pdu = wsc_attrs1,
    234 	.len = sizeof(wsc_attrs1),
    235 	.expected = {
    236 		.version2 = true,
    237 		.state = WSC_STATE_CONFIGURED,
    238 		.ap_setup_locked = false,
    239 		.selected_registrar = true,
    240 		.device_password_id = WSC_DEVICE_PASSWORD_ID_PUSH_BUTTON,
    241 		.selected_reg_config_methods =
    242 				WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN |
    243 				WSC_CONFIGURATION_METHOD_VIRTUAL_PUSH_BUTTON |
    244 				WSC_CONFIGURATION_METHOD_PHYSICAL_PUSH_BUTTON,
    245 		.response_type = WSC_RESPONSE_TYPE_AP,
    246 		.uuid_e = { 0xc4, 0x4a, 0xad, 0x8d, 0x25, 0x2f, 0x52, 0xc6,
    247 			0xf9, 0x6b, 0x38, 0x5d, 0xcb, 0x23, 0x31, 0xae },
    248 		.manufacturer = "ASUSTeK Computer Inc.",
    249 		.model_name = "Wi-Fi Protected Setup Router",
    250 		.model_number = "RT-AC68U",
    251 		.serial_number = "10:c3:7b:54:74:d0",
    252 		.primary_device_type = {
    253 			.category = 6,
    254 			.oui = { 0x00, 0x50, 0xf2 },
    255 			.oui_type = 0x04,
    256 			.subcategory = 1, },
    257 		.device_name = "RT-AC68U",
    258 		.config_methods = WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN,
    259 		.rf_bands = WSC_RF_BAND_2_4_GHZ | WSC_RF_BAND_5_0_GHZ,
    260 		.authorized_macs = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, },
    261 	},
    262 };
    263 
    264 static void wsc_test_parse_probe_response(const void *data)
    265 {
    266 	const struct probe_response_data *test = data;
    267 	struct wsc_probe_response probe_response;
    268 	const struct wsc_probe_response *expected = &test->expected;
    269 	int r;
    270 
    271 	r = wsc_parse_probe_response(test->pdu, test->len, &probe_response);
    272 	assert(r == 0);
    273 
    274 	assert(expected->version2 == probe_response.version2);
    275 	assert(expected->state == probe_response.state);
    276 	assert(expected->ap_setup_locked == probe_response.ap_setup_locked);
    277 	assert(expected->selected_registrar ==
    278 					probe_response.selected_registrar);
    279 	assert(expected->device_password_id ==
    280 					probe_response.device_password_id);
    281 	assert(expected->selected_reg_config_methods ==
    282 				probe_response.selected_reg_config_methods);
    283 	assert(expected->response_type == probe_response.response_type);
    284 	assert(!memcmp(expected->uuid_e, probe_response.uuid_e, 16));
    285 	assert(!strcmp(expected->manufacturer, probe_response.manufacturer));
    286 	assert(!strcmp(expected->model_name, probe_response.model_name));
    287 	assert(!strcmp(expected->model_number, probe_response.model_number));
    288 	assert(!strcmp(expected->serial_number, probe_response.serial_number));
    289 
    290 	assert(expected->primary_device_type.category ==
    291 				probe_response.primary_device_type.category);
    292 	assert(!memcmp(expected->primary_device_type.oui,
    293 				probe_response.primary_device_type.oui, 3));
    294 	assert(expected->primary_device_type.oui_type ==
    295 				probe_response.primary_device_type.oui_type);
    296 	assert(expected->primary_device_type.subcategory ==
    297 				probe_response.primary_device_type.subcategory);
    298 
    299 	assert(!strcmp(expected->device_name, probe_response.device_name));
    300 	assert(expected->config_methods == probe_response.config_methods);
    301 	assert(expected->rf_bands == probe_response.rf_bands);
    302 
    303 	assert(!memcmp(expected->authorized_macs,
    304 				probe_response.authorized_macs,
    305 				sizeof(probe_response.authorized_macs)));
    306 	assert(expected->reg_config_methods ==
    307 				probe_response.reg_config_methods);
    308 }
    309 
    310 static const unsigned char probe_request1[] = {
    311 	0x10, 0x4a, 0x00, 0x01, 0x10, 0x10, 0x3a, 0x00, 0x01, 0x01, 0x10, 0x08,
    312 	0x00, 0x02, 0x21, 0x48, 0x10, 0x47, 0x00, 0x10, 0x79, 0x0c, 0x1f, 0x80,
    313 	0x4f, 0x2b, 0x52, 0xb7, 0xbe, 0x30, 0xc0, 0xe9, 0x72, 0x92, 0x08, 0x8d,
    314 	0x10, 0x54, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    315 	0x10, 0x3c, 0x00, 0x01, 0x03, 0x10, 0x02, 0x00, 0x02, 0x00, 0x00, 0x10,
    316 	0x09, 0x00, 0x02, 0x00, 0x00, 0x10, 0x12, 0x00, 0x02, 0x00, 0x04, 0x10,
    317 	0x21, 0x00, 0x01, 0x20, 0x10, 0x23, 0x00, 0x01, 0x20, 0x10, 0x24, 0x00,
    318 	0x01, 0x20, 0x10, 0x11, 0x00, 0x01, 0x20, 0x10, 0x49, 0x00, 0x09, 0x00,
    319 	0x37, 0x2a, 0x00, 0x01, 0x20, 0x03, 0x01, 0x01,
    320 };
    321 
    322 struct probe_request_data {
    323 	struct wsc_probe_request expected;
    324 	const void *pdu;
    325 	unsigned int len;
    326 };
    327 
    328 static const struct probe_request_data probe_request_data_1 = {
    329 	.pdu = probe_request1,
    330 	.len = sizeof(probe_request1),
    331 	.expected = {
    332 		.version2 = true,
    333 		.request_type = WSC_REQUEST_TYPE_ENROLLEE_OPEN_8021X,
    334 		.config_methods =
    335 				WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN |
    336 				WSC_CONFIGURATION_METHOD_NFC_INTERFACE |
    337 				WSC_CONFIGURATION_METHOD_KEYPAD,
    338 		.uuid_e = { 0x79, 0x0c, 0x1f, 0x80, 0x4f, 0x2b, 0x52, 0xb7,
    339 			0xbe, 0x30, 0xc0, 0xe9, 0x72, 0x92, 0x08, 0x8d },
    340 		.primary_device_type = {
    341 			.category = 0,
    342 			.oui = { 0x00, 0x00, 0x00 },
    343 			.oui_type = 0x00,
    344 			.subcategory = 0, },
    345 		.rf_bands = WSC_RF_BAND_2_4_GHZ | WSC_RF_BAND_5_0_GHZ,
    346 		.association_state = WSC_ASSOCIATION_STATE_NOT_ASSOCIATED,
    347 		.configuration_error = WSC_CONFIGURATION_ERROR_NO_ERROR,
    348 		.device_password_id = WSC_DEVICE_PASSWORD_ID_PUSH_BUTTON,
    349 		.manufacturer = " ",
    350 		.model_name = " ",
    351 		.model_number = " ",
    352 		.device_name = " ",
    353 		.request_to_enroll = true,
    354 	},
    355 };
    356 
    357 static void wsc_test_parse_probe_request(const void *data)
    358 {
    359 	const struct probe_request_data *test = data;
    360 	struct wsc_probe_request probe_request;
    361 	const struct wsc_probe_request *expected = &test->expected;
    362 	int r;
    363 
    364 	r = wsc_parse_probe_request(test->pdu, test->len, &probe_request);
    365 	assert(r == 0);
    366 
    367 	assert(expected->version2 == probe_request.version2);
    368 	assert(expected->request_type == probe_request.request_type);
    369 
    370 	assert(expected->config_methods == probe_request.config_methods);
    371 	assert(!memcmp(expected->uuid_e, probe_request.uuid_e, 16));
    372 
    373 	assert(expected->primary_device_type.category ==
    374 				probe_request.primary_device_type.category);
    375 	assert(!memcmp(expected->primary_device_type.oui,
    376 				probe_request.primary_device_type.oui, 3));
    377 	assert(expected->primary_device_type.oui_type ==
    378 				probe_request.primary_device_type.oui_type);
    379 	assert(expected->primary_device_type.subcategory ==
    380 				probe_request.primary_device_type.subcategory);
    381 
    382 	assert(expected->rf_bands == probe_request.rf_bands);
    383 	assert(expected->association_state == probe_request.association_state);
    384 	assert(expected->configuration_error ==
    385 				probe_request.configuration_error);
    386 	assert(expected->device_password_id ==
    387 					probe_request.device_password_id);
    388 
    389 	assert(!strcmp(expected->manufacturer, probe_request.manufacturer));
    390 	assert(!strcmp(expected->model_name, probe_request.model_name));
    391 	assert(!strcmp(expected->model_number, probe_request.model_number));
    392 	assert(!strcmp(expected->device_name, probe_request.device_name));
    393 
    394 	assert(expected->request_to_enroll == probe_request.request_to_enroll);
    395 
    396 	assert(expected->requested_device_type.category ==
    397 				probe_request.requested_device_type.category);
    398 	assert(!memcmp(expected->requested_device_type.oui,
    399 				probe_request.requested_device_type.oui, 3));
    400 	assert(expected->requested_device_type.oui_type ==
    401 				probe_request.requested_device_type.oui_type);
    402 	assert(expected->requested_device_type.subcategory ==
    403 			probe_request.requested_device_type.subcategory);
    404 }
    405 
    406 static void wsc_test_build_probe_request(const void *data)
    407 {
    408 	const struct probe_request_data *test = data;
    409 	uint8_t *pr;
    410 	size_t prlen;
    411 
    412 	pr = wsc_build_probe_request(&test->expected, &prlen);
    413 	assert(pr);
    414 
    415 	assert(!memcmp(test->pdu, pr, test->len));
    416 
    417 	l_free(pr);
    418 }
    419 
    420 struct uuid_from_addr_data {
    421 	uint8_t addr[6];
    422 	uint8_t expected_uuid[16];
    423 };
    424 
    425 static const struct uuid_from_addr_data uuid_from_addr_data_1 = {
    426 	.addr = { 0xa0, 0xa8, 0xcd, 0x1c, 0x7e, 0xc9 },
    427 	.expected_uuid = { 0x79, 0x0c, 0x1f, 0x80, 0x4f, 0x2b, 0x52, 0xb7,
    428 			0xbe, 0x30, 0xc0, 0xe9, 0x72, 0x92, 0x08, 0x8d },
    429 };
    430 
    431 static void wsc_test_uuid_from_addr(const void *data)
    432 {
    433 	const struct uuid_from_addr_data *test = data;
    434 	uint8_t uuid[16];
    435 
    436 	assert(wsc_uuid_from_addr(test->addr, uuid));
    437 
    438 	assert(!memcmp(test->expected_uuid, uuid, 16));
    439 }
    440 
    441 static const unsigned char dh_private_key_1[] = {
    442 	0x4b, 0x5e, 0x01, 0xfa, 0x2b, 0xd4, 0x91, 0xac, 0x34, 0x04, 0x28, 0xfb,
    443 	0xe2, 0x5d, 0x06, 0x2d, 0x1a, 0xe7, 0x6c, 0xd0, 0xa3, 0x9d, 0x4a, 0x27,
    444 	0x24, 0x51, 0x79, 0xdc, 0xa7, 0x47, 0x24, 0x1d, 0x19, 0x26, 0x9e, 0xb8,
    445 	0xa0, 0x6e, 0xd9, 0x7e, 0x07, 0x4e, 0x02, 0xe1, 0xb2, 0x4e, 0x31, 0x12,
    446 	0xa2, 0xc1, 0x41, 0x73, 0x6a, 0x43, 0xe2, 0x0e, 0xc8, 0xec, 0xb7, 0x41,
    447 	0xdc, 0x25, 0x4c, 0x30, 0xd0, 0x3f, 0xa7, 0x97, 0x9f, 0x2e, 0xdc, 0x59,
    448 	0x0d, 0x7a, 0x24, 0xcb, 0x1b, 0xc2, 0x8d, 0x30, 0xc5, 0x04, 0xc3, 0x50,
    449 	0x61, 0xa7, 0xd2, 0xbd, 0x79, 0xb3, 0xec, 0xd2, 0xf5, 0x10, 0x20, 0x32,
    450 	0x0c, 0x7a, 0xfd, 0x0c, 0xde, 0x07, 0x22, 0x94, 0x2d, 0x42, 0x16, 0xc8,
    451 	0xa7, 0x17, 0x70, 0x16, 0x46, 0x72, 0xaa, 0xb5, 0xa7, 0xdc, 0xc0, 0xee,
    452 	0x7d, 0xe8, 0x36, 0x0b, 0xba, 0x04, 0xd5, 0x5c, 0x5d, 0x40, 0x65, 0x58,
    453 	0xd8, 0xe8, 0x9a, 0x27, 0xd2, 0xb3, 0x39, 0x2a, 0xe7, 0xfc, 0x16, 0x59,
    454 	0xc6, 0x6c, 0x2f, 0xad, 0x6f, 0xd2, 0x0d, 0xf4, 0xb0, 0x8b, 0xdf, 0x9a,
    455 	0x88, 0x84, 0xf9, 0x2a, 0xc4, 0x64, 0xb7, 0xb5, 0xe0, 0xf7, 0x3a, 0x8a,
    456 	0xeb, 0xd0, 0xde, 0xad, 0x1b, 0x0a, 0x44, 0xf2, 0x6f, 0xf5, 0xce, 0x8c,
    457 	0x70, 0xd6, 0x67, 0x1b, 0xb2, 0xa5, 0x9f, 0xeb, 0x63, 0x7f, 0x21, 0xdc,
    458 };
    459 
    460 static const unsigned char dh_public_key_1[] = {
    461 	0xb2, 0xfc, 0xd6, 0x4f, 0xf6, 0x71, 0x5a, 0x33, 0x84, 0x60, 0x4a, 0xe8,
    462 	0x2c, 0x1e, 0x55, 0x4a, 0xdb, 0xd5, 0x18, 0x17, 0x91, 0xa6, 0xf5, 0x70,
    463 	0xcd, 0x23, 0xd7, 0x12, 0x6e, 0x4c, 0xaf, 0x27, 0x9a, 0x4e, 0xf5, 0x37,
    464 	0xea, 0x8f, 0x03, 0xc9, 0x0e, 0x79, 0xc5, 0x8d, 0x37, 0xf8, 0xfb, 0x11,
    465 	0xa1, 0x39, 0x19, 0x9b, 0x5a, 0x3a, 0x66, 0x36, 0x6d, 0xfb, 0xae, 0xed,
    466 	0xfc, 0xa5, 0x90, 0xcb, 0xb3, 0xe1, 0xd5, 0x92, 0x2e, 0xe9, 0x99, 0xbd,
    467 	0x0b, 0x93, 0x82, 0x57, 0xe1, 0xbd, 0x70, 0x17, 0xa7, 0x78, 0x7a, 0x0a,
    468 	0xff, 0x42, 0x06, 0x95, 0x2c, 0x0b, 0x6c, 0x1a, 0x6b, 0x2f, 0x6b, 0xed,
    469 	0x42, 0xa5, 0x60, 0x8a, 0xb0, 0xb5, 0x79, 0x1b, 0xa9, 0xe6, 0x15, 0x17,
    470 	0xa3, 0x6c, 0xe9, 0x84, 0xb3, 0x77, 0x48, 0x9b, 0x7a, 0x4d, 0x04, 0xf6,
    471 	0xb8, 0x27, 0xe5, 0x0c, 0xcb, 0x76, 0xfc, 0x3c, 0x65, 0x49, 0xd7, 0x28,
    472 	0x06, 0x8d, 0x99, 0x18, 0x0f, 0xa7, 0x35, 0xb2, 0x9d, 0x15, 0x35, 0x51,
    473 	0xea, 0x83, 0xb6, 0x4d, 0x14, 0xb0, 0x21, 0xa4, 0x82, 0x1f, 0xb8, 0x73,
    474 	0x2b, 0x15, 0x1d, 0x48, 0x99, 0x9f, 0x32, 0x2c, 0xe1, 0xe1, 0xab, 0x66,
    475 	0x3f, 0xb4, 0x40, 0x79, 0xe8, 0x96, 0xe1, 0x9d, 0x54, 0x8b, 0xb6, 0x7f,
    476 	0x1a, 0x5b, 0x5f, 0x09, 0x9f, 0x40, 0xa7, 0x8b, 0xc8, 0xf6, 0x27, 0x80,
    477 };
    478 
    479 static const unsigned char dh_private_key_2[] = {
    480 	0x45, 0x2d, 0x9c, 0x56, 0x48, 0xe0, 0xe2, 0x35, 0x6b, 0x47, 0x04, 0x08,
    481 	0x47, 0xf6, 0x3a, 0x8a, 0xa8, 0x65, 0xff, 0xf8, 0x7c, 0x55, 0x16, 0xf4,
    482 	0xf6, 0x11, 0xcd, 0xb8, 0xc6, 0x2c, 0x5f, 0x94, 0x04, 0xdc, 0x69, 0x47,
    483 	0xd0, 0x9c, 0x8d, 0x78, 0x18, 0xd9, 0xab, 0x51, 0xf6, 0xb4, 0x81, 0xec,
    484 	0xd8, 0x4d, 0x61, 0xfe, 0xda, 0x78, 0x9c, 0x95, 0xa1, 0x91, 0xe3, 0x52,
    485 	0x1d, 0x03, 0xdd, 0x70, 0x2f, 0x83, 0x3e, 0x9c, 0xc4, 0x7f, 0x1a, 0x6f,
    486 	0x91, 0xeb, 0xc9, 0x2c, 0xbf, 0xfa, 0x4d, 0x1e, 0xb7, 0x5f, 0x70, 0x72,
    487 	0x92, 0x73, 0xed, 0x28, 0xd5, 0xdc, 0x4b, 0x0d, 0xc2, 0xa6, 0x2b, 0xdf,
    488 	0x2f, 0xdf, 0xf7, 0x16, 0xff, 0xf6, 0x68, 0x63, 0x68, 0xde, 0x72, 0x8a,
    489 	0x9c, 0x67, 0x8e, 0xb7, 0xd9, 0xb1, 0xe9, 0x03, 0x6d, 0xb7, 0xcb, 0xcd,
    490 	0x93, 0x0c, 0x7b, 0x59, 0x26, 0x62, 0xa1, 0x04, 0x41, 0x8d, 0xc7, 0xc3,
    491 	0x02, 0xbf, 0x8c, 0xcf, 0x93, 0x7d, 0xd0, 0xd8, 0x26, 0x19, 0x41, 0x04,
    492 	0x92, 0x4e, 0x4f, 0x11, 0x88, 0x72, 0x78, 0x8b, 0x6d, 0x22, 0x62, 0xdf,
    493 	0x80, 0xd3, 0x79, 0xf5, 0xa5, 0xfa, 0xee, 0xc6, 0x9e, 0xee, 0x4b, 0x94,
    494 	0xd2, 0x77, 0x1e, 0x71, 0xf6, 0xc2, 0x5f, 0xbb, 0xb7, 0xff, 0xcd, 0x49,
    495 	0x1a, 0x80, 0xc3, 0xf8, 0x3f, 0xb4, 0x02, 0x38, 0x84, 0x06, 0xd1, 0xc0,
    496 };
    497 
    498 static const unsigned char dh_public_key_2[] = {
    499 	0x42, 0x76, 0x1f, 0xac, 0x65, 0x7b, 0x25, 0xd3, 0x59, 0xe8, 0x5d, 0xde,
    500 	0x22, 0xb1, 0x74, 0x39, 0x7d, 0x0d, 0x6c, 0x56, 0xc9, 0x4f, 0xfc, 0xf2,
    501 	0x1f, 0xd0, 0x4f, 0x70, 0xe9, 0xd5, 0x5c, 0xb6, 0x79, 0x07, 0x54, 0xb6,
    502 	0x8e, 0x67, 0xe1, 0xb5, 0x35, 0x18, 0xc0, 0xe3, 0x36, 0xf9, 0x58, 0x15,
    503 	0x3f, 0x4e, 0xed, 0x74, 0xb5, 0x46, 0xd4, 0x2f, 0x30, 0x22, 0xb2, 0xe1,
    504 	0xa9, 0x4d, 0xf6, 0x3a, 0x00, 0x22, 0x34, 0xf0, 0x14, 0xc5, 0xa3, 0x53,
    505 	0x28, 0x78, 0x91, 0x46, 0x1e, 0x80, 0x3d, 0x7e, 0x41, 0x4a, 0x44, 0x3a,
    506 	0xd0, 0x83, 0x7e, 0xcf, 0xa4, 0x31, 0x0e, 0x0c, 0xfc, 0xcd, 0x63, 0x37,
    507 	0x05, 0x8b, 0x7f, 0x0d, 0x65, 0xef, 0x52, 0x58, 0xb9, 0x9e, 0xb2, 0xfc,
    508 	0x77, 0x7b, 0xdb, 0x79, 0x17, 0x3d, 0xb2, 0x84, 0x4a, 0xaf, 0x09, 0x93,
    509 	0x1c, 0x45, 0x9d, 0x0a, 0x9f, 0xb9, 0x64, 0xec, 0x6f, 0x66, 0x01, 0x49,
    510 	0xb6, 0x00, 0xf7, 0xff, 0x85, 0x4d, 0xa8, 0x60, 0x07, 0xf6, 0x33, 0x1f,
    511 	0xe6, 0x48, 0x9e, 0x71, 0x88, 0x60, 0x88, 0x02, 0xbb, 0x1d, 0x07, 0x68,
    512 	0x7a, 0xa0, 0xfa, 0x4f, 0xfb, 0x42, 0xde, 0x6f, 0x8e, 0x4e, 0x4a, 0xa8,
    513 	0xa7, 0xd3, 0x5d, 0xcb, 0x38, 0x47, 0x16, 0xb6, 0xea, 0x99, 0x9f, 0x7d,
    514 	0xf6, 0xcd, 0xd4, 0xe4, 0x5f, 0x9f, 0xb5, 0xb6, 0x61, 0x9d, 0x42, 0x78,
    515 };
    516 
    517 struct dh_generate_pubkey_test {
    518 	const uint8_t *private_key;
    519 	uint32_t private_key_size;
    520 	const uint8_t *public_key;
    521 	uint32_t public_key_size;
    522 };
    523 
    524 static const struct dh_generate_pubkey_test dh_generate_pubkey_test_data_1 = {
    525 	.private_key = dh_private_key_1,
    526 	.private_key_size = sizeof(dh_private_key_1),
    527 	.public_key = dh_public_key_1,
    528 	.public_key_size = sizeof(dh_public_key_1),
    529 };
    530 
    531 static const struct dh_generate_pubkey_test dh_generate_pubkey_test_data_2 = {
    532 	.private_key = dh_private_key_2,
    533 	.private_key_size = sizeof(dh_private_key_2),
    534 	.public_key = dh_public_key_2,
    535 	.public_key_size = sizeof(dh_public_key_2),
    536 };
    537 
    538 static void wsc_test_dh_generate_pubkey(const void *data)
    539 {
    540 	const struct dh_generate_pubkey_test *test = data;
    541 	struct l_key *private;
    542 	struct l_key *generator;
    543 	struct l_key *prime;
    544 	uint8_t public_key[test->private_key_size];
    545 	size_t len;
    546 
    547 	memset(public_key, 0, sizeof(public_key));
    548 
    549 	generator = l_key_new(L_KEY_RAW, crypto_dh5_generator,
    550 						crypto_dh5_generator_size);
    551 	assert(generator);
    552 
    553 	prime = l_key_new(L_KEY_RAW, crypto_dh5_prime, crypto_dh5_prime_size);
    554 	assert(prime);
    555 
    556 	private = l_key_new(L_KEY_RAW, test->private_key,
    557 						test->private_key_size);
    558 	assert(private);
    559 
    560 	len = test->private_key_size;
    561 	assert(l_key_compute_dh_public(generator, private, prime,
    562 							public_key, &len));
    563 	assert(len == test->public_key_size);
    564 	assert(!memcmp(public_key, test->public_key, test->public_key_size));
    565 
    566 	l_key_free(prime);
    567 	l_key_free(generator);
    568 	l_key_free(private);
    569 }
    570 
    571 static const unsigned char eap_wsc_m1_1[] = {
    572 	0x01, 0x00, 0x01, 0x78, 0x02, 0x01, 0x01, 0x78, 0xfe, 0x00, 0x37, 0x2a,
    573 	0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10,
    574 	0x22, 0x00, 0x01, 0x04, 0x10, 0x47, 0x00, 0x10, 0x79, 0x0c, 0x1f, 0x80,
    575 	0x4f, 0x2b, 0x52, 0xb7, 0xbe, 0x30, 0xc0, 0xe9, 0x72, 0x92, 0x08, 0x8d,
    576 	0x10, 0x20, 0x00, 0x06, 0xa0, 0xa8, 0xcd, 0x1c, 0x7e, 0xc9, 0x10, 0x1a,
    577 	0x00, 0x10, 0xab, 0x84, 0x41, 0x2f, 0xe7, 0xc3, 0xc9, 0xc9, 0xd7, 0xf4,
    578 	0xe8, 0xc1, 0x4f, 0x49, 0x2b, 0x79, 0x10, 0x32, 0x00, 0xc0, 0xb2, 0xfc,
    579 	0xd6, 0x4f, 0xf6, 0x71, 0x5a, 0x33, 0x84, 0x60, 0x4a, 0xe8, 0x2c, 0x1e,
    580 	0x55, 0x4a, 0xdb, 0xd5, 0x18, 0x17, 0x91, 0xa6, 0xf5, 0x70, 0xcd, 0x23,
    581 	0xd7, 0x12, 0x6e, 0x4c, 0xaf, 0x27, 0x9a, 0x4e, 0xf5, 0x37, 0xea, 0x8f,
    582 	0x03, 0xc9, 0x0e, 0x79, 0xc5, 0x8d, 0x37, 0xf8, 0xfb, 0x11, 0xa1, 0x39,
    583 	0x19, 0x9b, 0x5a, 0x3a, 0x66, 0x36, 0x6d, 0xfb, 0xae, 0xed, 0xfc, 0xa5,
    584 	0x90, 0xcb, 0xb3, 0xe1, 0xd5, 0x92, 0x2e, 0xe9, 0x99, 0xbd, 0x0b, 0x93,
    585 	0x82, 0x57, 0xe1, 0xbd, 0x70, 0x17, 0xa7, 0x78, 0x7a, 0x0a, 0xff, 0x42,
    586 	0x06, 0x95, 0x2c, 0x0b, 0x6c, 0x1a, 0x6b, 0x2f, 0x6b, 0xed, 0x42, 0xa5,
    587 	0x60, 0x8a, 0xb0, 0xb5, 0x79, 0x1b, 0xa9, 0xe6, 0x15, 0x17, 0xa3, 0x6c,
    588 	0xe9, 0x84, 0xb3, 0x77, 0x48, 0x9b, 0x7a, 0x4d, 0x04, 0xf6, 0xb8, 0x27,
    589 	0xe5, 0x0c, 0xcb, 0x76, 0xfc, 0x3c, 0x65, 0x49, 0xd7, 0x28, 0x06, 0x8d,
    590 	0x99, 0x18, 0x0f, 0xa7, 0x35, 0xb2, 0x9d, 0x15, 0x35, 0x51, 0xea, 0x83,
    591 	0xb6, 0x4d, 0x14, 0xb0, 0x21, 0xa4, 0x82, 0x1f, 0xb8, 0x73, 0x2b, 0x15,
    592 	0x1d, 0x48, 0x99, 0x9f, 0x32, 0x2c, 0xe1, 0xe1, 0xab, 0x66, 0x3f, 0xb4,
    593 	0x40, 0x79, 0xe8, 0x96, 0xe1, 0x9d, 0x54, 0x8b, 0xb6, 0x7f, 0x1a, 0x5b,
    594 	0x5f, 0x09, 0x9f, 0x40, 0xa7, 0x8b, 0xc8, 0xf6, 0x27, 0x80, 0x10, 0x04,
    595 	0x00, 0x02, 0x00, 0x23, 0x10, 0x10, 0x00, 0x02, 0x00, 0x0d, 0x10, 0x0d,
    596 	0x00, 0x01, 0x01, 0x10, 0x08, 0x00, 0x02, 0x21, 0x48, 0x10, 0x44, 0x00,
    597 	0x01, 0x01, 0x10, 0x21, 0x00, 0x01, 0x20, 0x10, 0x23, 0x00, 0x01, 0x20,
    598 	0x10, 0x24, 0x00, 0x01, 0x20, 0x10, 0x42, 0x00, 0x01, 0x20, 0x10, 0x54,
    599 	0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11,
    600 	0x00, 0x01, 0x20, 0x10, 0x3c, 0x00, 0x01, 0x02, 0x10, 0x02, 0x00, 0x02,
    601 	0x00, 0x00, 0x10, 0x12, 0x00, 0x02, 0x00, 0x04, 0x10, 0x09, 0x00, 0x02,
    602 	0x00, 0x00, 0x10, 0x2d, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00, 0x10, 0x49,
    603 	0x00, 0x06, 0x00, 0x37, 0x2a, 0x00, 0x01, 0x20,
    604 };
    605 
    606 static const unsigned char eap_wsc_m1_2[] = {
    607 	0x01, 0x00, 0x01, 0x78, 0x02, 0x01, 0x01, 0x78, 0xfe, 0x00, 0x37, 0x2a,
    608 	0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10,
    609 	0x22, 0x00, 0x01, 0x04, 0x10, 0x47, 0x00, 0x10, 0x79, 0x0c, 0x1f, 0x80,
    610 	0x4f, 0x2b, 0x52, 0xb7, 0xbe, 0x30, 0xc0, 0xe9, 0x72, 0x92, 0x08, 0x8d,
    611 	0x10, 0x20, 0x00, 0x06, 0xa0, 0xa8, 0xcd, 0x1c, 0x7e, 0xc9, 0x10, 0x1a,
    612 	0x00, 0x10, 0x19, 0x8d, 0x0d, 0x25, 0x91, 0x2c, 0x37, 0x1c, 0xeb, 0x07,
    613 	0x89, 0x33, 0xe1, 0x25, 0xd7, 0x43, 0x10, 0x32, 0x00, 0xc0, 0x42, 0x76,
    614 	0x1f, 0xac, 0x65, 0x7b, 0x25, 0xd3, 0x59, 0xe8, 0x5d, 0xde, 0x22, 0xb1,
    615 	0x74, 0x39, 0x7d, 0x0d, 0x6c, 0x56, 0xc9, 0x4f, 0xfc, 0xf2, 0x1f, 0xd0,
    616 	0x4f, 0x70, 0xe9, 0xd5, 0x5c, 0xb6, 0x79, 0x07, 0x54, 0xb6, 0x8e, 0x67,
    617 	0xe1, 0xb5, 0x35, 0x18, 0xc0, 0xe3, 0x36, 0xf9, 0x58, 0x15, 0x3f, 0x4e,
    618 	0xed, 0x74, 0xb5, 0x46, 0xd4, 0x2f, 0x30, 0x22, 0xb2, 0xe1, 0xa9, 0x4d,
    619 	0xf6, 0x3a, 0x00, 0x22, 0x34, 0xf0, 0x14, 0xc5, 0xa3, 0x53, 0x28, 0x78,
    620 	0x91, 0x46, 0x1e, 0x80, 0x3d, 0x7e, 0x41, 0x4a, 0x44, 0x3a, 0xd0, 0x83,
    621 	0x7e, 0xcf, 0xa4, 0x31, 0x0e, 0x0c, 0xfc, 0xcd, 0x63, 0x37, 0x05, 0x8b,
    622 	0x7f, 0x0d, 0x65, 0xef, 0x52, 0x58, 0xb9, 0x9e, 0xb2, 0xfc, 0x77, 0x7b,
    623 	0xdb, 0x79, 0x17, 0x3d, 0xb2, 0x84, 0x4a, 0xaf, 0x09, 0x93, 0x1c, 0x45,
    624 	0x9d, 0x0a, 0x9f, 0xb9, 0x64, 0xec, 0x6f, 0x66, 0x01, 0x49, 0xb6, 0x00,
    625 	0xf7, 0xff, 0x85, 0x4d, 0xa8, 0x60, 0x07, 0xf6, 0x33, 0x1f, 0xe6, 0x48,
    626 	0x9e, 0x71, 0x88, 0x60, 0x88, 0x02, 0xbb, 0x1d, 0x07, 0x68, 0x7a, 0xa0,
    627 	0xfa, 0x4f, 0xfb, 0x42, 0xde, 0x6f, 0x8e, 0x4e, 0x4a, 0xa8, 0xa7, 0xd3,
    628 	0x5d, 0xcb, 0x38, 0x47, 0x16, 0xb6, 0xea, 0x99, 0x9f, 0x7d, 0xf6, 0xcd,
    629 	0xd4, 0xe4, 0x5f, 0x9f, 0xb5, 0xb6, 0x61, 0x9d, 0x42, 0x78, 0x10, 0x04,
    630 	0x00, 0x02, 0x00, 0x23, 0x10, 0x10, 0x00, 0x02, 0x00, 0x0d, 0x10, 0x0d,
    631 	0x00, 0x01, 0x01, 0x10, 0x08, 0x00, 0x02, 0x21, 0x48, 0x10, 0x44, 0x00,
    632 	0x01, 0x01, 0x10, 0x21, 0x00, 0x01, 0x20, 0x10, 0x23, 0x00, 0x01, 0x20,
    633 	0x10, 0x24, 0x00, 0x01, 0x20, 0x10, 0x42, 0x00, 0x01, 0x20, 0x10, 0x54,
    634 	0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11,
    635 	0x00, 0x01, 0x20, 0x10, 0x3c, 0x00, 0x01, 0x01, 0x10, 0x02, 0x00, 0x02,
    636 	0x00, 0x00, 0x10, 0x12, 0x00, 0x02, 0x00, 0x04, 0x10, 0x09, 0x00, 0x02,
    637 	0x00, 0x00, 0x10, 0x2d, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00, 0x10, 0x49,
    638 	0x00, 0x06, 0x00, 0x37, 0x2a, 0x00, 0x01, 0x20,
    639 };
    640 
    641 struct m1_data {
    642 	struct wsc_m1 expected;
    643 	const void *pdu;
    644 	unsigned int len;
    645 	const uint8_t *private_key;
    646 	uint32_t private_key_size;
    647 	const uint8_t *public_key;
    648 	uint32_t public_key_size;
    649 };
    650 
    651 static const struct m1_data m1_data_1 = {
    652 	.pdu = eap_wsc_m1_1 + 18,
    653 	.len = sizeof(eap_wsc_m1_1) - 18,
    654 	.expected = {
    655 		.version2 = true,
    656 		.uuid_e = { 0x79, 0x0c, 0x1f, 0x80, 0x4f, 0x2b, 0x52, 0xb7,
    657 			0xbe, 0x30, 0xc0, 0xe9, 0x72, 0x92, 0x08, 0x8d },
    658 		.addr = { 0xa0, 0xa8, 0xcd, 0x1c, 0x7e, 0xc9 },
    659 		.enrollee_nonce = { 0xab, 0x84, 0x41, 0x2f, 0xe7, 0xc3, 0xc9,
    660 					0xc9, 0xd7, 0xf4, 0xe8, 0xc1, 0x4f,
    661 					0x49, 0x2b, 0x79 },
    662 		.public_key = { }, /* Tested elsewhere */
    663 		.auth_type_flags =
    664 			WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL |
    665 			WSC_AUTHENTICATION_TYPE_WPA_PERSONAL |
    666 			WSC_AUTHENTICATION_TYPE_OPEN,
    667 		.encryption_type_flags = WSC_ENCRYPTION_TYPE_AES_TKIP |
    668 						WSC_ENCRYPTION_TYPE_NONE,
    669 		.connection_type_flags = WSC_CONNECTION_TYPE_ESS,
    670 		.config_methods = WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN |
    671 				WSC_CONFIGURATION_METHOD_KEYPAD |
    672 				WSC_CONFIGURATION_METHOD_DISPLAY |
    673 				WSC_CONFIGURATION_METHOD_NFC_INTERFACE,
    674 		.state = WSC_STATE_NOT_CONFIGURED,
    675 		.manufacturer = " ",
    676 		.model_name = " ",
    677 		.model_number = " ",
    678 		.serial_number = " ",
    679 		.primary_device_type = {
    680 			.category = 0,
    681 			.oui = { 0x00, 0x00, 0x00 },
    682 			.oui_type = 0x00,
    683 			.subcategory = 0, },
    684 		.device_name = " ",
    685 		.rf_bands = WSC_RF_BAND_5_0_GHZ,
    686 		.association_state = WSC_ASSOCIATION_STATE_NOT_ASSOCIATED,
    687 		.device_password_id = WSC_DEVICE_PASSWORD_ID_PUSH_BUTTON,
    688 		.configuration_error = WSC_CONFIGURATION_ERROR_NO_ERROR,
    689 		.os_version = 0,
    690 		.request_to_enroll = false,
    691 	},
    692 	.private_key = dh_private_key_1,
    693 	.private_key_size = sizeof(dh_private_key_1),
    694 	.public_key = dh_public_key_1,
    695 	.public_key_size = sizeof(dh_public_key_1),
    696 };
    697 
    698 static const struct m1_data m1_data_2 = {
    699 	.pdu = eap_wsc_m1_2 + 18,
    700 	.len = sizeof(eap_wsc_m1_2) - 18,
    701 	.expected = {
    702 		.version2 = true,
    703 		.uuid_e = { 0x79, 0x0c, 0x1f, 0x80, 0x4f, 0x2b, 0x52, 0xb7,
    704 			0xbe, 0x30, 0xc0, 0xe9, 0x72, 0x92, 0x08, 0x8d },
    705 		.addr = { 0xa0, 0xa8, 0xcd, 0x1c, 0x7e, 0xc9 },
    706 		.enrollee_nonce = { 0x19, 0x8d, 0x0d, 0x25, 0x91, 0x2c, 0x37,
    707 					0x1c, 0xeb, 0x07, 0x89, 0x33, 0xe1,
    708 					0x25, 0xd7, 0x43 },
    709 		.public_key = { }, /* Tested elsewhere */
    710 		.auth_type_flags =
    711 			WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL |
    712 			WSC_AUTHENTICATION_TYPE_WPA_PERSONAL |
    713 			WSC_AUTHENTICATION_TYPE_OPEN,
    714 		.encryption_type_flags = WSC_ENCRYPTION_TYPE_AES_TKIP |
    715 						WSC_ENCRYPTION_TYPE_NONE,
    716 		.connection_type_flags = WSC_CONNECTION_TYPE_ESS,
    717 		.config_methods = WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN |
    718 				WSC_CONFIGURATION_METHOD_KEYPAD |
    719 				WSC_CONFIGURATION_METHOD_DISPLAY |
    720 				WSC_CONFIGURATION_METHOD_NFC_INTERFACE,
    721 		.state = WSC_STATE_NOT_CONFIGURED,
    722 		.manufacturer = " ",
    723 		.model_name = " ",
    724 		.model_number = " ",
    725 		.serial_number = " ",
    726 		.primary_device_type = {
    727 			.category = 0,
    728 			.oui = { 0x00, 0x00, 0x00 },
    729 			.oui_type = 0x00,
    730 			.subcategory = 0, },
    731 		.device_name = " ",
    732 		.rf_bands = WSC_RF_BAND_2_4_GHZ,
    733 		.association_state = WSC_ASSOCIATION_STATE_NOT_ASSOCIATED,
    734 		.device_password_id = WSC_DEVICE_PASSWORD_ID_PUSH_BUTTON,
    735 		.configuration_error = WSC_CONFIGURATION_ERROR_NO_ERROR,
    736 		.os_version = 0,
    737 		.request_to_enroll = false,
    738 	},
    739 	.private_key = dh_private_key_2,
    740 	.private_key_size = sizeof(dh_private_key_2),
    741 	.public_key = dh_public_key_2,
    742 	.public_key_size = sizeof(dh_public_key_2),
    743 };
    744 
    745 static void wsc_test_parse_m1(const void *data)
    746 {
    747 	const struct m1_data *test = data;
    748 	struct wsc_m1 m1;
    749 	const struct wsc_m1 *expected = &test->expected;
    750 	int r;
    751 
    752 	r = wsc_parse_m1(test->pdu, test->len, &m1);
    753 	assert(r == 0);
    754 
    755 	assert(expected->version2 == m1.version2);
    756 	assert(!memcmp(expected->uuid_e, m1.uuid_e, 16));
    757 	assert(!memcmp(expected->addr, m1.addr, 6));
    758 	assert(!memcmp(expected->enrollee_nonce, m1.enrollee_nonce, 16));
    759 
    760 	/* Skip public_key testing */
    761 
    762 	assert(expected->auth_type_flags == m1.auth_type_flags);
    763 	assert(expected->encryption_type_flags == m1.encryption_type_flags);
    764 	assert(expected->connection_type_flags == m1.connection_type_flags);
    765 	assert(expected->config_methods == m1.config_methods);
    766 	assert(expected->state == m1.state);
    767 
    768 	assert(!strcmp(expected->manufacturer, m1.manufacturer));
    769 	assert(!strcmp(expected->model_name, m1.model_name));
    770 	assert(!strcmp(expected->model_number, m1.model_number));
    771 	assert(!strcmp(expected->serial_number, m1.serial_number));
    772 
    773 	assert(expected->primary_device_type.category ==
    774 				m1.primary_device_type.category);
    775 	assert(!memcmp(expected->primary_device_type.oui,
    776 				m1.primary_device_type.oui, 3));
    777 	assert(expected->primary_device_type.oui_type ==
    778 				m1.primary_device_type.oui_type);
    779 	assert(expected->primary_device_type.subcategory ==
    780 				m1.primary_device_type.subcategory);
    781 
    782 	assert(!strcmp(expected->device_name, m1.device_name));
    783 	assert(expected->rf_bands == m1.rf_bands);
    784 	assert(expected->association_state == m1.association_state);
    785 	assert(expected->device_password_id == m1.device_password_id);
    786 	assert(expected->configuration_error == m1.configuration_error);
    787 	assert(expected->os_version == m1.os_version);
    788 	assert(expected->request_to_enroll == m1.request_to_enroll);
    789 
    790 	assert(!memcmp(test->public_key, m1.public_key, 192));
    791 }
    792 
    793 static void wsc_test_build_m1(const void *data)
    794 {
    795 	const struct m1_data *test = data;
    796 	struct wsc_m1 m1;
    797 	uint8_t *out;
    798 	size_t out_len;
    799 
    800 	memcpy(&m1, &test->expected, sizeof(m1));
    801 	memcpy(m1.public_key, test->public_key, 192);
    802 
    803 	out = wsc_build_m1(&m1, &out_len);
    804 	assert(out);
    805 
    806 	assert(out_len == test->len);
    807 	assert(!memcmp(test->pdu, out, test->len));
    808 
    809 	l_free(out);
    810 }
    811 
    812 static bool wsc_compute_authenticator(struct l_key *peer_public_key,
    813 					struct l_key *private_key,
    814 					struct l_key *prime,
    815 					const uint8_t *enrollee_nonce,
    816 					const uint8_t *enrollee_mac,
    817 					const uint8_t *registrar_nonce,
    818 					const uint8_t *prev_msg,
    819 					size_t prev_msg_len,
    820 					const uint8_t *cur_msg,
    821 					size_t cur_msg_len,
    822 					uint8_t *authenticator)
    823 {
    824 	uint8_t shared_secret[192];
    825 	size_t shared_secret_len;
    826 	struct l_checksum *sha256;
    827 	uint8_t dhkey[32];
    828 	struct l_checksum *hmac_sha256;
    829 	uint8_t kdk[32];
    830 	ssize_t digest_len;
    831 	bool r;
    832 	struct iovec iov[3];
    833 	struct wsc_session_key session_key;
    834 
    835 	/* Compute shared secret */
    836 	shared_secret_len = sizeof(shared_secret);
    837 	r = l_key_compute_dh_secret(peer_public_key, private_key, prime,
    838 					shared_secret, &shared_secret_len);
    839 	assert(r);
    840 
    841 	sha256 = l_checksum_new(L_CHECKSUM_SHA256);
    842 	assert(sha256);
    843 
    844 	/* Compute DHKey */
    845 	assert(l_checksum_update(sha256, shared_secret, shared_secret_len));
    846 	digest_len = l_checksum_get_digest(sha256, dhkey, sizeof(dhkey));
    847 	assert(digest_len == 32);
    848 	l_checksum_free(sha256);
    849 
    850 	hmac_sha256 = l_checksum_new_hmac(L_CHECKSUM_SHA256,
    851 							dhkey, sizeof(dhkey));
    852 	assert(hmac_sha256);
    853 
    854 	iov[0].iov_base = (void *) enrollee_nonce;
    855 	iov[0].iov_len = 16;
    856 	iov[1].iov_base = (void *) enrollee_mac;
    857 	iov[1].iov_len = 6;
    858 	iov[2].iov_base = (void *) registrar_nonce;
    859 	iov[2].iov_len = 16;
    860 
    861 	assert(l_checksum_updatev(hmac_sha256, iov, 3));
    862 	digest_len = l_checksum_get_digest(hmac_sha256, kdk, sizeof(kdk));
    863 	assert(digest_len == 32);
    864 	l_checksum_free(hmac_sha256);
    865 
    866 	assert(wsc_kdf(kdk, &session_key, sizeof(session_key)));
    867 
    868 	hmac_sha256 = l_checksum_new_hmac(L_CHECKSUM_SHA256,
    869 						session_key.auth_key,
    870 						sizeof(session_key.auth_key));
    871 	iov[0].iov_base = (void *) prev_msg;
    872 	iov[0].iov_len = prev_msg_len;
    873 	iov[1].iov_base = (void *) cur_msg;
    874 	iov[1].iov_len = cur_msg_len - 12;
    875 
    876 	assert(l_checksum_updatev(hmac_sha256, iov, 2));
    877 	digest_len = l_checksum_get_digest(hmac_sha256, authenticator, 8);
    878 	assert(digest_len == 8);
    879 	l_checksum_free(hmac_sha256);
    880 
    881 	return true;
    882 }
    883 
    884 static const unsigned char eap_wsc_m2_1[] = {
    885 	0x01, 0x00, 0x01, 0xd6, 0x01, 0x02, 0x01, 0xd6, 0xfe, 0x00, 0x37, 0x2a,
    886 	0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10,
    887 	0x22, 0x00, 0x01, 0x05, 0x10, 0x1a, 0x00, 0x10, 0xab, 0x84, 0x41, 0x2f,
    888 	0xe7, 0xc3, 0xc9, 0xc9, 0xd7, 0xf4, 0xe8, 0xc1, 0x4f, 0x49, 0x2b, 0x79,
    889 	0x10, 0x39, 0x00, 0x10, 0x1a, 0xe1, 0xa2, 0xc1, 0x5a, 0xff, 0xa8, 0xb4,
    890 	0x3a, 0x4a, 0xe1, 0xea, 0x71, 0x3f, 0xfb, 0x49, 0x10, 0x48, 0x00, 0x10,
    891 	0xc4, 0x4a, 0xad, 0x8d, 0x25, 0x2f, 0x52, 0xc6, 0xf9, 0x6b, 0x38, 0x5d,
    892 	0xcb, 0x23, 0x31, 0xae, 0x10, 0x32, 0x00, 0xc0, 0xe8, 0x23, 0x5b, 0x62,
    893 	0x65, 0x41, 0x83, 0xfa, 0xc3, 0x23, 0x6f, 0x30, 0xc9, 0x4e, 0x32, 0x34,
    894 	0x8c, 0xfa, 0xf0, 0xfb, 0xff, 0x09, 0xc1, 0x93, 0x13, 0x7a, 0x80, 0xe2,
    895 	0xce, 0x5a, 0x5e, 0xed, 0x55, 0xcd, 0xdb, 0x87, 0x05, 0xbf, 0x06, 0x47,
    896 	0xf9, 0x96, 0xdd, 0x96, 0x41, 0xdf, 0x56, 0x6e, 0xb1, 0x20, 0xee, 0xf7,
    897 	0x97, 0xf7, 0x59, 0x9f, 0x94, 0x78, 0xe5, 0xff, 0xd7, 0xa0, 0x99, 0xcf,
    898 	0x34, 0x00, 0xb4, 0xbd, 0xaf, 0xaf, 0x8f, 0xd9, 0x7d, 0x10, 0x2e, 0x4d,
    899 	0x8c, 0xac, 0x0e, 0x8f, 0x19, 0xcf, 0x07, 0xc7, 0x24, 0x6c, 0xa8, 0xf4,
    900 	0x18, 0x56, 0x04, 0xbf, 0x80, 0x34, 0x72, 0x18, 0x65, 0x09, 0x7a, 0x85,
    901 	0x7b, 0xe2, 0xe0, 0xeb, 0x19, 0x15, 0x69, 0x7f, 0xef, 0x60, 0x78, 0x01,
    902 	0x73, 0xb8, 0xd0, 0x33, 0x76, 0x18, 0xf2, 0xf8, 0x29, 0x39, 0x11, 0x1a,
    903 	0xcd, 0x6c, 0x5c, 0x10, 0xf5, 0xbf, 0x4e, 0xf3, 0xda, 0x9a, 0x48, 0x46,
    904 	0x5c, 0x93, 0x8e, 0x18, 0x03, 0xd0, 0x42, 0x8f, 0xef, 0x5a, 0xc7, 0x9c,
    905 	0x1a, 0x10, 0x3a, 0xd3, 0x74, 0x6a, 0x3b, 0x23, 0x37, 0x7b, 0xcd, 0x4a,
    906 	0x52, 0xfd, 0x11, 0x4d, 0x62, 0xf8, 0x2e, 0xac, 0xae, 0x2e, 0xcf, 0x25,
    907 	0x5c, 0xce, 0xd4, 0xd9, 0x8b, 0x30, 0x06, 0x74, 0x87, 0xd1, 0xb8, 0x64,
    908 	0x1a, 0xf5, 0xc6, 0x28, 0x89, 0x1f, 0x68, 0xd7, 0x10, 0x04, 0x00, 0x02,
    909 	0x00, 0x23, 0x10, 0x10, 0x00, 0x02, 0x00, 0x0d, 0x10, 0x0d, 0x00, 0x01,
    910 	0x01, 0x10, 0x08, 0x00, 0x02, 0x26, 0x88, 0x10, 0x21, 0x00, 0x15, 0x41,
    911 	0x53, 0x55, 0x53, 0x54, 0x65, 0x4b, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x75,
    912 	0x74, 0x65, 0x72, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x10, 0x23, 0x00, 0x1c,
    913 	0x57, 0x69, 0x2d, 0x46, 0x69, 0x20, 0x50, 0x72, 0x6f, 0x74, 0x65, 0x63,
    914 	0x74, 0x65, 0x64, 0x20, 0x53, 0x65, 0x74, 0x75, 0x70, 0x20, 0x52, 0x6f,
    915 	0x75, 0x74, 0x65, 0x72, 0x10, 0x24, 0x00, 0x08, 0x52, 0x54, 0x2d, 0x41,
    916 	0x43, 0x36, 0x38, 0x55, 0x10, 0x42, 0x00, 0x11, 0x31, 0x30, 0x3a, 0x63,
    917 	0x33, 0x3a, 0x37, 0x62, 0x3a, 0x35, 0x34, 0x3a, 0x37, 0x34, 0x3a, 0x64,
    918 	0x30, 0x10, 0x54, 0x00, 0x08, 0x00, 0x06, 0x00, 0x50, 0xf2, 0x04, 0x00,
    919 	0x01, 0x10, 0x11, 0x00, 0x08, 0x52, 0x54, 0x2d, 0x41, 0x43, 0x36, 0x38,
    920 	0x55, 0x10, 0x3c, 0x00, 0x01, 0x02, 0x10, 0x02, 0x00, 0x02, 0x00, 0x00,
    921 	0x10, 0x09, 0x00, 0x02, 0x00, 0x00, 0x10, 0x12, 0x00, 0x02, 0x00, 0x04,
    922 	0x10, 0x2d, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00, 0x10, 0x49, 0x00, 0x06,
    923 	0x00, 0x37, 0x2a, 0x00, 0x01, 0x20, 0x10, 0x05, 0x00, 0x08, 0xec, 0xb1,
    924 	0x17, 0xac, 0xd9, 0x33, 0x84, 0x5f,
    925 };
    926 
    927 static const unsigned char eap_wsc_m2_2[] = {
    928 	0x01, 0x00, 0x01, 0xd3, 0x01, 0x02, 0x01, 0xd3, 0xfe, 0x00, 0x37, 0x2a,
    929 	0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10,
    930 	0x22, 0x00, 0x01, 0x05, 0x10, 0x1a, 0x00, 0x10, 0x19, 0x8d, 0x0d, 0x25,
    931 	0x91, 0x2c, 0x37, 0x1c, 0xeb, 0x07, 0x89, 0x33, 0xe1, 0x25, 0xd7, 0x43,
    932 	0x10, 0x39, 0x00, 0x10, 0x67, 0xa5, 0x53, 0x61, 0xee, 0x72, 0xc2, 0x21,
    933 	0x48, 0x78, 0xc5, 0x70, 0x89, 0x9f, 0xf2, 0xa0, 0x10, 0x48, 0x00, 0x10,
    934 	0x8e, 0xd8, 0x32, 0x02, 0xa3, 0x24, 0x52, 0xc0, 0xb0, 0x3e, 0x0c, 0x95,
    935 	0x18, 0x27, 0xd6, 0xe7, 0x10, 0x32, 0x00, 0xc0, 0x29, 0xdd, 0x2e, 0x36,
    936 	0x97, 0xa4, 0x25, 0x1c, 0x93, 0x56, 0xef, 0xdd, 0x8c, 0xf4, 0x9e, 0x1c,
    937 	0xab, 0xbe, 0x42, 0xd1, 0xba, 0xc4, 0xb0, 0xa4, 0x2b, 0xaf, 0x9e, 0xcc,
    938 	0xe0, 0xfb, 0x94, 0x0c, 0xf1, 0xdf, 0x5d, 0x71, 0x2b, 0xcb, 0x24, 0xd7,
    939 	0x83, 0x9f, 0xf4, 0xa2, 0xa9, 0x88, 0x86, 0xa2, 0x7f, 0x4f, 0x42, 0xe2,
    940 	0x76, 0x4a, 0xac, 0x4e, 0x5d, 0x45, 0x15, 0x01, 0x4f, 0x29, 0xbe, 0xa2,
    941 	0x44, 0xdf, 0x36, 0xb9, 0xff, 0xe3, 0xdf, 0x14, 0x30, 0x21, 0xf2, 0x74,
    942 	0x9d, 0x57, 0x5b, 0x08, 0xe9, 0x78, 0xad, 0x5c, 0x28, 0x2c, 0x15, 0xcb,
    943 	0xa2, 0x0d, 0xdb, 0x8c, 0xc5, 0x0f, 0xda, 0x6f, 0x98, 0xd0, 0x84, 0x0b,
    944 	0x4c, 0xab, 0x0b, 0x1c, 0xd9, 0xee, 0x78, 0xa4, 0xfe, 0x8e, 0x51, 0x8d,
    945 	0xa1, 0xc5, 0xf1, 0xe1, 0x5e, 0xf1, 0x9b, 0xc4, 0x17, 0xa5, 0x41, 0xb1,
    946 	0x46, 0x75, 0x9b, 0x03, 0x4e, 0x6c, 0x0b, 0x3a, 0xb9, 0x25, 0xb4, 0x76,
    947 	0x79, 0x18, 0xdd, 0xba, 0x73, 0xca, 0x32, 0x33, 0x60, 0xd5, 0xb3, 0xef,
    948 	0xfe, 0x13, 0x3f, 0xfc, 0x91, 0xf3, 0x31, 0xd6, 0x4a, 0xbf, 0xfa, 0x3e,
    949 	0x89, 0x4d, 0xe6, 0xfa, 0x09, 0x71, 0x0f, 0x8a, 0xc8, 0x67, 0x1f, 0x7e,
    950 	0xc0, 0xae, 0xb8, 0x15, 0x4d, 0xb9, 0x20, 0xec, 0x40, 0xd2, 0xdf, 0x73,
    951 	0x09, 0x13, 0x5b, 0x06, 0xe0, 0xc1, 0xfa, 0xe8, 0x10, 0x04, 0x00, 0x02,
    952 	0x00, 0x3f, 0x10, 0x10, 0x00, 0x02, 0x00, 0x0c, 0x10, 0x0d, 0x00, 0x01,
    953 	0x01, 0x10, 0x08, 0x00, 0x02, 0x00, 0x00, 0x10, 0x21, 0x00, 0x0a, 0x41,
    954 	0x70, 0x70, 0x6c, 0x65, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x10, 0x23, 0x00,
    955 	0x04, 0x4a, 0x32, 0x38, 0x45, 0x10, 0x24, 0x00, 0x19, 0x41, 0x70, 0x70,
    956 	0x6c, 0x65, 0x20, 0x42, 0x61, 0x73, 0x65, 0x20, 0x53, 0x74, 0x61, 0x74,
    957 	0x69, 0x6f, 0x6e, 0x20, 0x56, 0x37, 0x2e, 0x37, 0x2e, 0x33, 0x10, 0x42,
    958 	0x00, 0x0c, 0x43, 0x38, 0x36, 0x4d, 0x44, 0x38, 0x57, 0x48, 0x46, 0x4a,
    959 	0x31, 0x52, 0x10, 0x54, 0x00, 0x08, 0x00, 0x06, 0x00, 0x50, 0xf2, 0x04,
    960 	0x00, 0x01, 0x10, 0x11, 0x00, 0x17, 0x44, 0x65, 0x6e, 0x69, 0x73, 0x27,
    961 	0x73, 0x20, 0x41, 0x69, 0x72, 0x50, 0x6f, 0x72, 0x74, 0x20, 0x45, 0x78,
    962 	0x74, 0x72, 0x65, 0x6d, 0x65, 0x10, 0x3c, 0x00, 0x01, 0x01, 0x10, 0x02,
    963 	0x00, 0x02, 0x00, 0x00, 0x10, 0x09, 0x00, 0x02, 0x00, 0x00, 0x10, 0x12,
    964 	0x00, 0x02, 0x00, 0x04, 0x10, 0x2d, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
    965 	0x10, 0x49, 0x00, 0x0b, 0x00, 0x00, 0x3f, 0x00, 0x04, 0x00, 0x04, 0x00,
    966 	0x00, 0x00, 0x00, 0x10, 0x05, 0x00, 0x08, 0x45, 0x33, 0x07, 0x57, 0x77,
    967 	0x40, 0x50, 0x37,
    968 };
    969 
    970 struct m2_data {
    971 	struct wsc_m2 expected;
    972 	const void *pdu;
    973 	unsigned int len;
    974 	const uint8_t *public_key;
    975 	uint32_t public_key_size;
    976 	const struct m1_data *m1;
    977 };
    978 
    979 static const struct m2_data m2_data_1 = {
    980 	.pdu = eap_wsc_m2_1 + 18,
    981 	.len = sizeof(eap_wsc_m2_1) - 18,
    982 	.expected = {
    983 		.version2 = true,
    984 		.enrollee_nonce = { 0xab, 0x84, 0x41, 0x2f, 0xe7, 0xc3, 0xc9,
    985 					0xc9, 0xd7, 0xf4, 0xe8, 0xc1, 0x4f,
    986 					0x49, 0x2b, 0x79 },
    987 		.registrar_nonce = { 0x1a, 0xe1, 0xa2, 0xc1, 0x5a, 0xff, 0xa8,
    988 					0xb4, 0x3a, 0x4a, 0xe1, 0xea, 0x71,
    989 					0x3f, 0xfb, 0x49 },
    990 		.uuid_r = { 0xc4, 0x4a, 0xad, 0x8d, 0x25, 0x2f, 0x52, 0xc6,
    991 			0xf9, 0x6b, 0x38, 0x5d, 0xcb, 0x23, 0x31, 0xae },
    992 		.public_key = { }, /* Tested elsewhere */
    993 		.auth_type_flags =
    994 			WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL |
    995 			WSC_AUTHENTICATION_TYPE_WPA_PERSONAL |
    996 			WSC_AUTHENTICATION_TYPE_OPEN,
    997 		.encryption_type_flags = WSC_ENCRYPTION_TYPE_AES_TKIP |
    998 						WSC_ENCRYPTION_TYPE_NONE,
    999 		.connection_type_flags = WSC_CONNECTION_TYPE_ESS,
   1000 		.config_methods = WSC_CONFIGURATION_METHOD_VIRTUAL_PUSH_BUTTON |
   1001 				WSC_CONFIGURATION_METHOD_PHYSICAL_PUSH_BUTTON |
   1002 				WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN,
   1003 		.manufacturer = "ASUSTeK Computer Inc.",
   1004 		.model_name = "Wi-Fi Protected Setup Router",
   1005 		.model_number = "RT-AC68U",
   1006 		.serial_number = "10:c3:7b:54:74:d0",
   1007 		.primary_device_type = {
   1008 			.category = 6,
   1009 			.oui = { 0x00, 0x50, 0xf2 },
   1010 			.oui_type = 0x04,
   1011 			.subcategory = 1, },
   1012 		.device_name = "RT-AC68U",
   1013 		.rf_bands = WSC_RF_BAND_5_0_GHZ,
   1014 		.association_state = WSC_ASSOCIATION_STATE_NOT_ASSOCIATED,
   1015 		.device_password_id = WSC_DEVICE_PASSWORD_ID_PUSH_BUTTON,
   1016 		.configuration_error = WSC_CONFIGURATION_ERROR_NO_ERROR,
   1017 		.os_version = 0,
   1018 		.authenticator = { 0xec, 0xb1, 0x17, 0xac, 0xd9, 0x33, 0x84,
   1019 					0x5f },
   1020 	},
   1021 	.public_key = eap_wsc_m2_1 + 92,
   1022 	.public_key_size = 192,
   1023 	.m1 = &m1_data_1,
   1024 };
   1025 
   1026 static const struct m2_data m2_data_2 = {
   1027 	.pdu = eap_wsc_m2_2 + 18,
   1028 	.len = sizeof(eap_wsc_m2_2) - 18,
   1029 	.expected = {
   1030 		.version2 = false,
   1031 		.enrollee_nonce = { 0x19, 0x8d, 0x0d, 0x25, 0x91, 0x2c, 0x37,
   1032 					0x1c, 0xeb, 0x07, 0x89, 0x33, 0xe1,
   1033 					0x25, 0xd7, 0x43 },
   1034 		.registrar_nonce = { 0x67, 0xa5, 0x53, 0x61, 0xee, 0x72, 0xc2,
   1035 					0x21, 0x48, 0x78, 0xc5, 0x70, 0x89,
   1036 					0x9f, 0xf2, 0xa0 },
   1037 		.uuid_r = { 0x8e, 0xd8, 0x32, 0x02, 0xa3, 0x24, 0x52, 0xc0,
   1038 			0xb0, 0x3e, 0x0c, 0x95, 0x18, 0x27, 0xd6, 0xe7, },
   1039 		.public_key = { }, /* Tested elsewhere */
   1040 		.auth_type_flags =
   1041 			WSC_AUTHENTICATION_TYPE_OPEN |
   1042 			WSC_AUTHENTICATION_TYPE_WPA_PERSONAL |
   1043 			WSC_AUTHENTICATION_TYPE_SHARED |
   1044 			WSC_AUTHENTICATION_TYPE_WPA_ENTERPRISE |
   1045 			WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL |
   1046 			WSC_AUTHENTICATION_TYPE_WPA2_ENTERPRISE,
   1047 		.encryption_type_flags = WSC_ENCRYPTION_TYPE_AES_TKIP,
   1048 		.connection_type_flags = WSC_CONNECTION_TYPE_ESS,
   1049 		.config_methods = 0,
   1050 		.manufacturer = "Apple Inc.",
   1051 		.model_name = "J28E",
   1052 		.model_number = "Apple Base Station V7.7.3",
   1053 		.serial_number = "C86MD8WHFJ1R",
   1054 		.primary_device_type = {
   1055 			.category = 6,
   1056 			.oui = { 0x00, 0x50, 0xf2 },
   1057 			.oui_type = 0x04,
   1058 			.subcategory = 1, },
   1059 		.device_name = "Denis's AirPort Extreme",
   1060 		.rf_bands = WSC_RF_BAND_2_4_GHZ,
   1061 		.association_state = WSC_ASSOCIATION_STATE_NOT_ASSOCIATED,
   1062 		.device_password_id = WSC_DEVICE_PASSWORD_ID_PUSH_BUTTON,
   1063 		.configuration_error = WSC_CONFIGURATION_ERROR_NO_ERROR,
   1064 		.os_version = 0,
   1065 		.authenticator = { 0x45, 0x33, 0x07, 0x57, 0x77, 0x40, 0x50,
   1066 						0x37 },
   1067 	},
   1068 	.public_key = eap_wsc_m2_2 + 92,
   1069 	.public_key_size = 192,
   1070 	.m1 = &m1_data_2,
   1071 };
   1072 
   1073 static void wsc_test_parse_m2(const void *data)
   1074 {
   1075 	const struct m2_data *test = data;
   1076 	struct wsc_m2 m2;
   1077 	const struct wsc_m2 *expected = &test->expected;
   1078 	struct l_key *private;
   1079 	struct l_key *prime;
   1080 	struct l_key *peer_public;
   1081 	uint8_t authenticator[8];
   1082 	int r;
   1083 
   1084 	r = wsc_parse_m2(test->pdu, test->len, &m2);
   1085 	assert(r == 0);
   1086 
   1087 	assert(expected->version2 == m2.version2);
   1088 	assert(!memcmp(expected->uuid_r, m2.uuid_r, 16));
   1089 	assert(!memcmp(expected->enrollee_nonce, m2.enrollee_nonce, 16));
   1090 	assert(!memcmp(expected->registrar_nonce, m2.registrar_nonce, 16));
   1091 
   1092 	/* Skip public_key testing */
   1093 
   1094 	assert(expected->auth_type_flags == m2.auth_type_flags);
   1095 	assert(expected->encryption_type_flags == m2.encryption_type_flags);
   1096 	assert(expected->connection_type_flags == m2.connection_type_flags);
   1097 	assert(expected->config_methods == m2.config_methods);
   1098 
   1099 	assert(!strcmp(expected->manufacturer, m2.manufacturer));
   1100 	assert(!strcmp(expected->model_name, m2.model_name));
   1101 	assert(!strcmp(expected->model_number, m2.model_number));
   1102 	assert(!strcmp(expected->serial_number, m2.serial_number));
   1103 
   1104 	assert(expected->primary_device_type.category ==
   1105 				m2.primary_device_type.category);
   1106 	assert(!memcmp(expected->primary_device_type.oui,
   1107 				m2.primary_device_type.oui, 3));
   1108 	assert(expected->primary_device_type.oui_type ==
   1109 				m2.primary_device_type.oui_type);
   1110 	assert(expected->primary_device_type.subcategory ==
   1111 				m2.primary_device_type.subcategory);
   1112 
   1113 	assert(!strcmp(expected->device_name, m2.device_name));
   1114 	assert(expected->rf_bands == m2.rf_bands);
   1115 	assert(expected->association_state == m2.association_state);
   1116 	assert(expected->device_password_id == m2.device_password_id);
   1117 	assert(expected->configuration_error == m2.configuration_error);
   1118 	assert(expected->os_version == m2.os_version);
   1119 	assert(!memcmp(expected->authenticator, m2.authenticator, 8));
   1120 
   1121 	assert(!memcmp(test->public_key, m2.public_key, 192));
   1122 
   1123 	prime = l_key_new(L_KEY_RAW, crypto_dh5_prime, crypto_dh5_prime_size);
   1124 	assert(prime);
   1125 
   1126 	private = l_key_new(L_KEY_RAW, test->m1->private_key,
   1127 						test->m1->private_key_size);
   1128 	assert(private);
   1129 
   1130 	peer_public = l_key_new(L_KEY_RAW, test->public_key,
   1131 						test->public_key_size);
   1132 	assert(peer_public);
   1133 
   1134 	assert(wsc_compute_authenticator(peer_public, private, prime,
   1135 						m2.enrollee_nonce,
   1136 						test->m1->expected.addr,
   1137 						m2.registrar_nonce,
   1138 						test->m1->pdu,
   1139 						test->m1->len,
   1140 						test->pdu,
   1141 						test->len,
   1142 						authenticator));
   1143 
   1144 	assert(!memcmp(m2.authenticator, authenticator, 8));
   1145 
   1146 	l_key_free(prime);
   1147 	l_key_free(private);
   1148 	l_key_free(peer_public);
   1149 }
   1150 
   1151 static void wsc_test_build_m2(const void *data)
   1152 {
   1153 	const struct m2_data *test = data;
   1154 	struct wsc_m2 m2;
   1155 	uint8_t *out;
   1156 	size_t out_len;
   1157 
   1158 	memcpy(&m2, &test->expected, sizeof(m2));
   1159 	memcpy(m2.public_key, test->public_key, 192);
   1160 
   1161 	out = wsc_build_m2(&m2, &out_len);
   1162 	assert(out);
   1163 
   1164 	assert(out_len == test->len);
   1165 	assert(!memcmp(test->pdu, out, test->len));
   1166 
   1167 	l_free(out);
   1168 }
   1169 
   1170 static const unsigned char eap_wsc_m3[] = {
   1171 	0x01, 0x00, 0x00, 0x8a, 0x02, 0x02, 0x00, 0x8a, 0xfe, 0x00, 0x37, 0x2a,
   1172 	0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10,
   1173 	0x22, 0x00, 0x01, 0x07, 0x10, 0x39, 0x00, 0x10, 0x67, 0xa5, 0x53, 0x61,
   1174 	0xee, 0x72, 0xc2, 0x21, 0x48, 0x78, 0xc5, 0x70, 0x89, 0x9f, 0xf2, 0xa0,
   1175 	0x10, 0x14, 0x00, 0x20, 0x70, 0x9b, 0x9a, 0x2d, 0x26, 0x98, 0x0d, 0x46,
   1176 	0x8e, 0x1c, 0xb8, 0x8a, 0xbf, 0xcb, 0x83, 0xb9, 0x65, 0xba, 0x0b, 0xd1,
   1177 	0x20, 0xd9, 0x36, 0x9e, 0xc3, 0xc7, 0xb8, 0x9b, 0xe9, 0x0a, 0xa5, 0x50,
   1178 	0x10, 0x15, 0x00, 0x20, 0xfd, 0x5f, 0xfa, 0xb0, 0xa1, 0x4b, 0x78, 0xf9,
   1179 	0xa4, 0x9c, 0x84, 0x72, 0x2b, 0x08, 0x2e, 0x73, 0x73, 0x4e, 0xfb, 0x4d,
   1180 	0xbf, 0x93, 0x08, 0xa9, 0x56, 0xa5, 0x52, 0x89, 0x4c, 0x74, 0x44, 0x8f,
   1181 	0x10, 0x49, 0x00, 0x06, 0x00, 0x37, 0x2a, 0x00, 0x01, 0x20, 0x10, 0x05,
   1182 	0x00, 0x08, 0xaf, 0x03, 0x0e, 0x95, 0xd6, 0xe6, 0x72, 0xaf,
   1183 };
   1184 
   1185 struct m3_data {
   1186 	struct wsc_m3 expected;
   1187 	const void *pdu;
   1188 	unsigned int len;
   1189 };
   1190 
   1191 static const struct m3_data m3_data_1 = {
   1192 	.pdu = eap_wsc_m3 + 18,
   1193 	.len = sizeof(eap_wsc_m3) - 18,
   1194 	.expected = {
   1195 		.version2 = true,
   1196 		.registrar_nonce = { 0x67, 0xa5, 0x53, 0x61, 0xee, 0x72, 0xc2,
   1197 					0x21, 0x48, 0x78, 0xc5, 0x70, 0x89,
   1198 					0x9f, 0xf2, 0xa0 },
   1199 		.e_hash1 = { 0x70, 0x9b, 0x9a, 0x2d, 0x26, 0x98, 0x0d, 0x46,
   1200 			0x8e, 0x1c, 0xb8, 0x8a, 0xbf, 0xcb, 0x83, 0xb9, 0x65,
   1201 			0xba, 0x0b, 0xd1, 0x20, 0xd9, 0x36, 0x9e, 0xc3, 0xc7,
   1202 			0xb8, 0x9b, 0xe9, 0x0a, 0xa5, 0x50 },
   1203 		.e_hash2 = { 0xfd, 0x5f, 0xfa, 0xb0, 0xa1, 0x4b, 0x78, 0xf9,
   1204 			0xa4, 0x9c, 0x84, 0x72, 0x2b, 0x08, 0x2e, 0x73, 0x73,
   1205 			0x4e, 0xfb, 0x4d, 0xbf, 0x93, 0x08, 0xa9, 0x56, 0xa5,
   1206 			0x52, 0x89, 0x4c, 0x74, 0x44, 0x8f },
   1207 		.authenticator = { 0xaf, 0x03, 0x0e, 0x95, 0xd6, 0xe6,
   1208 					0x72, 0xaf },
   1209 	},
   1210 };
   1211 
   1212 static void wsc_test_parse_m3(const void *data)
   1213 {
   1214 	const struct m3_data *test = data;
   1215 	struct wsc_m3 m3;
   1216 	const struct wsc_m3 *expected = &test->expected;
   1217 	int r;
   1218 
   1219 	r = wsc_parse_m3(test->pdu, test->len, &m3);
   1220 	assert(r == 0);
   1221 
   1222 	assert(expected->version2 == m3.version2);
   1223 	assert(!memcmp(expected->registrar_nonce, m3.registrar_nonce, 16));
   1224 	assert(!memcmp(expected->e_hash1, m3.e_hash1, 32));
   1225 	assert(!memcmp(expected->e_hash2, m3.e_hash2, 32));
   1226 	assert(!memcmp(expected->authenticator, m3.authenticator, 8));
   1227 }
   1228 
   1229 static void wsc_test_build_m3(const void *data)
   1230 {
   1231 	const struct m3_data *test = data;
   1232 	uint8_t *out;
   1233 	size_t out_len;
   1234 
   1235 	out = wsc_build_m3(&test->expected, &out_len);
   1236 	assert(out);
   1237 
   1238 	assert(out_len == test->len);
   1239 	assert(!memcmp(test->pdu, out, test->len));
   1240 
   1241 	l_free(out);
   1242 }
   1243 
   1244 static const unsigned char eap_wsc_m4[] = {
   1245 	0x01, 0x00, 0x00, 0xc4, 0x01, 0x03, 0x00, 0xc4, 0xfe, 0x00, 0x37, 0x2a,
   1246 	0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10,
   1247 	0x22, 0x00, 0x01, 0x08, 0x10, 0x1a, 0x00, 0x10, 0x19, 0x8d, 0x0d, 0x25,
   1248 	0x91, 0x2c, 0x37, 0x1c, 0xeb, 0x07, 0x89, 0x33, 0xe1, 0x25, 0xd7, 0x43,
   1249 	0x10, 0x3d, 0x00, 0x20, 0x33, 0x81, 0x27, 0xf0, 0xb6, 0x75, 0x4c, 0x11,
   1250 	0x83, 0x05, 0x3f, 0xa6, 0x10, 0x4b, 0xb5, 0x55, 0x2d, 0x7e, 0x65, 0x29,
   1251 	0x48, 0x8c, 0xbd, 0xc4, 0x47, 0x49, 0xbd, 0xc9, 0x80, 0x67, 0x0e, 0x78,
   1252 	0x10, 0x3e, 0x00, 0x20, 0x3f, 0x7c, 0x94, 0x89, 0xf8, 0x10, 0x4e, 0x9b,
   1253 	0x37, 0xf6, 0x64, 0x28, 0xe1, 0x63, 0xdb, 0xca, 0x90, 0x42, 0xf4, 0x84,
   1254 	0x3f, 0x1f, 0x2a, 0x7a, 0x3e, 0x4b, 0x29, 0xc3, 0xd2, 0x93, 0x79, 0x37,
   1255 	0x10, 0x18, 0x00, 0x40, 0x8e, 0x02, 0xd6, 0x5f, 0xff, 0x43, 0xdf, 0x55,
   1256 	0x19, 0xe2, 0x48, 0x30, 0xce, 0x02, 0x7c, 0xd6, 0x73, 0x6e, 0x19, 0x61,
   1257 	0x38, 0x99, 0x5d, 0xb3, 0x63, 0x05, 0xba, 0xf2, 0xcc, 0xa4, 0xcf, 0x24,
   1258 	0x33, 0x26, 0xb3, 0x93, 0x92, 0xbc, 0xb6, 0x65, 0x9d, 0x94, 0xe3, 0xe4,
   1259 	0x39, 0xf3, 0xd0, 0xc5, 0x51, 0x79, 0x00, 0x67, 0xa2, 0x4e, 0xd6, 0x56,
   1260 	0xfa, 0xb3, 0x0e, 0x0d, 0xca, 0x45, 0x76, 0xdf, 0x10, 0x05, 0x00, 0x08,
   1261 	0x30, 0xf1, 0xf6, 0x4d, 0x5c, 0x55, 0x36, 0x3c,
   1262 };
   1263 
   1264 struct m4_data {
   1265 	struct wsc_m4 expected;
   1266 	const uint8_t *expected_encrypted;
   1267 	size_t expected_encrypted_size;
   1268 	const void *pdu;
   1269 	unsigned int len;
   1270 };
   1271 
   1272 static const struct m4_data m4_data_1 = {
   1273 	.pdu = eap_wsc_m4 + 18,
   1274 	.len = sizeof(eap_wsc_m4) - 18,
   1275 	.expected = {
   1276 		.version2 = false,
   1277 		.enrollee_nonce = { 0x19, 0x8d, 0x0d, 0x25, 0x91, 0x2c, 0x37,
   1278 					0x1c, 0xeb, 0x07, 0x89, 0x33, 0xe1,
   1279 					0x25, 0xd7, 0x43 },
   1280 		.r_hash1 = { 0x33, 0x81, 0x27, 0xf0, 0xb6, 0x75, 0x4c, 0x11,
   1281 			0x83, 0x05, 0x3f, 0xa6, 0x10, 0x4b, 0xb5, 0x55, 0x2d,
   1282 			0x7e, 0x65, 0x29, 0x48, 0x8c, 0xbd, 0xc4, 0x47, 0x49,
   1283 			0xbd, 0xc9, 0x80, 0x67, 0x0e, 0x78 },
   1284 		.r_hash2 = { 0x3f, 0x7c, 0x94, 0x89, 0xf8, 0x10, 0x4e, 0x9b,
   1285 			0x37, 0xf6, 0x64, 0x28, 0xe1, 0x63, 0xdb, 0xca, 0x90,
   1286 			0x42, 0xf4, 0x84, 0x3f, 0x1f, 0x2a, 0x7a, 0x3e, 0x4b,
   1287 			0x29, 0xc3, 0xd2, 0x93, 0x79, 0x37 },
   1288 		.authenticator = { 0x30, 0xf1, 0xf6, 0x4d, 0x5c, 0x55,
   1289 					0x36, 0x3c },
   1290 	},
   1291 	.expected_encrypted = eap_wsc_m4 + 124,
   1292 	.expected_encrypted_size = 64,
   1293 };
   1294 
   1295 static void wsc_test_parse_m4(const void *data)
   1296 {
   1297 	const struct m4_data *test = data;
   1298 	struct wsc_m4 m4;
   1299 	const struct wsc_m4 *expected = &test->expected;
   1300 	struct iovec encrypted;
   1301 	int r;
   1302 
   1303 	r = wsc_parse_m4(test->pdu, test->len, &m4, &encrypted);
   1304 	assert(r == 0);
   1305 
   1306 	assert(expected->version2 == m4.version2);
   1307 	assert(!memcmp(expected->enrollee_nonce, m4.enrollee_nonce, 16));
   1308 	assert(!memcmp(expected->r_hash1, m4.r_hash1, 32));
   1309 	assert(!memcmp(expected->r_hash2, m4.r_hash2, 32));
   1310 	assert(!memcmp(expected->authenticator, m4.authenticator, 8));
   1311 
   1312 	assert(test->expected_encrypted_size == encrypted.iov_len);
   1313 	assert(!memcmp(test->expected_encrypted,
   1314 					encrypted.iov_base, encrypted.iov_len));
   1315 }
   1316 
   1317 static void wsc_test_build_m4(const void *data)
   1318 {
   1319 	const struct m4_data *test = data;
   1320 	uint8_t *out;
   1321 	size_t out_len;
   1322 
   1323 	out = wsc_build_m4(&test->expected, test->expected_encrypted,
   1324 				test->expected_encrypted_size, &out_len);
   1325 	assert(out);
   1326 
   1327 	assert(out_len == test->len);
   1328 	assert(!memcmp(test->pdu, out, test->len));
   1329 
   1330 	l_free(out);
   1331 }
   1332 
   1333 static const unsigned char m4_encrypted_settings[] = {
   1334 	0x10, 0x3f, 0x00, 0x10, 0xaf, 0xbe, 0xbc, 0x19, 0x44, 0xdf, 0xf3, 0x40,
   1335 	0x3c, 0xdc, 0x70, 0xc0, 0x63, 0x63, 0xc7, 0x53, 0x10, 0x1e, 0x00, 0x08,
   1336 	0x47, 0x82, 0x81, 0xb3, 0x1e, 0xe0, 0xa4, 0xcb,
   1337 };
   1338 
   1339 struct m4_encrypted_settings_data {
   1340 	struct wsc_m4_encrypted_settings expected;
   1341 	const void *pdu;
   1342 	unsigned int len;
   1343 };
   1344 
   1345 static const struct m4_encrypted_settings_data m4_encrypted_settings_data_1 = {
   1346 	.pdu = m4_encrypted_settings,
   1347 	.len = sizeof(m4_encrypted_settings),
   1348 	.expected = {
   1349 		.r_snonce1 = { 0xaf, 0xbe, 0xbc, 0x19, 0x44, 0xdf, 0xf3, 0x40,
   1350 			0x3c, 0xdc, 0x70, 0xc0, 0x63, 0x63, 0xc7, 0x53 },
   1351 		.authenticator = { 0x47, 0x82, 0x81, 0xb3, 0x1e, 0xe0,
   1352 					0xa4, 0xcb },
   1353 	},
   1354 };
   1355 
   1356 static void wsc_test_parse_m4_encrypted_settings(const void *data)
   1357 {
   1358 	const struct m4_encrypted_settings_data *test = data;
   1359 	struct wsc_m4_encrypted_settings m4es;
   1360 	const struct wsc_m4_encrypted_settings *expected = &test->expected;
   1361 	int r;
   1362 
   1363 	r = wsc_parse_m4_encrypted_settings(test->pdu, test->len, &m4es);
   1364 	assert(r == 0);
   1365 
   1366 	assert(!memcmp(expected->r_snonce1, m4es.r_snonce1, 16));
   1367 	assert(!memcmp(expected->authenticator, m4es.authenticator, 8));
   1368 }
   1369 
   1370 static void wsc_test_build_m4_encrypted_settings(const void *data)
   1371 {
   1372 	const struct m4_encrypted_settings_data *test = data;
   1373 	uint8_t *out;
   1374 	size_t out_len;
   1375 
   1376 	out = wsc_build_m4_encrypted_settings(&test->expected, &out_len);
   1377 	assert(out);
   1378 
   1379 	assert(out_len == test->len);
   1380 	assert(!memcmp(test->pdu, out, test->len));
   1381 
   1382 	l_free(out);
   1383 }
   1384 
   1385 static const unsigned char eap_wsc_m5[] = {
   1386 	0x01, 0x00, 0x00, 0x86, 0x02, 0x03, 0x00, 0x86, 0xfe, 0x00, 0x37, 0x2a,
   1387 	0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10,
   1388 	0x22, 0x00, 0x01, 0x09, 0x10, 0x39, 0x00, 0x10, 0x67, 0xa5, 0x53, 0x61,
   1389 	0xee, 0x72, 0xc2, 0x21, 0x48, 0x78, 0xc5, 0x70, 0x89, 0x9f, 0xf2, 0xa0,
   1390 	0x10, 0x18, 0x00, 0x40, 0x9a, 0x31, 0xf8, 0x4b, 0x46, 0x72, 0xf2, 0xcc,
   1391 	0xf6, 0x3c, 0x84, 0x5e, 0xed, 0x34, 0x64, 0xec, 0xe6, 0x04, 0xe5, 0xb1,
   1392 	0xac, 0xac, 0xa5, 0x5d, 0x50, 0x5f, 0xcc, 0x5c, 0x99, 0x13, 0x3f, 0xe7,
   1393 	0x24, 0xb8, 0x10, 0x41, 0x04, 0x94, 0x2a, 0x3c, 0x56, 0xe5, 0x14, 0xee,
   1394 	0x57, 0xda, 0xdf, 0x25, 0x57, 0xbe, 0x3e, 0x71, 0x8a, 0x10, 0x3a, 0x88,
   1395 	0xd9, 0x74, 0x42, 0x2d, 0x53, 0x6a, 0xa6, 0xe5, 0x10, 0x49, 0x00, 0x06,
   1396 	0x00, 0x37, 0x2a, 0x00, 0x01, 0x20, 0x10, 0x05, 0x00, 0x08, 0x6c, 0x80,
   1397 	0x5a, 0x2f, 0xac, 0x19, 0x7c, 0x35,
   1398 };
   1399 
   1400 struct m5_data {
   1401 	struct wsc_m5 expected;
   1402 	const uint8_t *expected_encrypted;
   1403 	size_t expected_encrypted_size;
   1404 	const void *pdu;
   1405 	unsigned int len;
   1406 };
   1407 
   1408 static const struct m5_data m5_data_1 = {
   1409 	.pdu = eap_wsc_m5 + 18,
   1410 	.len = sizeof(eap_wsc_m5) - 18,
   1411 	.expected = {
   1412 		.version2 = true,
   1413 		.registrar_nonce = { 0x67, 0xa5, 0x53, 0x61, 0xee, 0x72, 0xc2,
   1414 					0x21, 0x48, 0x78, 0xc5, 0x70, 0x89,
   1415 					0x9f, 0xf2, 0xa0 },
   1416 		.authenticator = { 0x6c, 0x80, 0x5a, 0x2f, 0xac, 0x19,
   1417 						0x7c, 0x35 },
   1418 	},
   1419 	.expected_encrypted = eap_wsc_m5 + 52,
   1420 	.expected_encrypted_size = 64,
   1421 };
   1422 
   1423 static void wsc_test_parse_m5(const void *data)
   1424 {
   1425 	const struct m5_data *test = data;
   1426 	struct wsc_m5 m5;
   1427 	const struct wsc_m5 *expected = &test->expected;
   1428 	struct iovec encrypted;
   1429 	int r;
   1430 
   1431 	r = wsc_parse_m5(test->pdu, test->len, &m5, &encrypted);
   1432 	assert(r == 0);
   1433 
   1434 	assert(expected->version2 == m5.version2);
   1435 	assert(!memcmp(expected->registrar_nonce, m5.registrar_nonce, 16));
   1436 	assert(!memcmp(expected->authenticator, m5.authenticator, 8));
   1437 
   1438 	assert(test->expected_encrypted_size == encrypted.iov_len);
   1439 	assert(!memcmp(test->expected_encrypted,
   1440 					encrypted.iov_base, encrypted.iov_len));
   1441 }
   1442 
   1443 static void wsc_test_build_m5(const void *data)
   1444 {
   1445 	const struct m5_data *test = data;
   1446 	uint8_t *out;
   1447 	size_t out_len;
   1448 
   1449 	out = wsc_build_m5(&test->expected, test->expected_encrypted,
   1450 				test->expected_encrypted_size, &out_len);
   1451 	assert(out);
   1452 
   1453 	assert(out_len == test->len);
   1454 	assert(!memcmp(test->pdu, out, test->len));
   1455 
   1456 	l_free(out);
   1457 }
   1458 
   1459 static const unsigned char eap_wsc_m6[] = {
   1460 	0x01, 0x00, 0x00, 0x7c, 0x01, 0xaa, 0x00, 0x7c, 0xfe, 0x00, 0x37, 0x2a,
   1461 	0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10,
   1462 	0x22, 0x00, 0x01, 0x0a, 0x10, 0x1a, 0x00, 0x10, 0x19, 0x8d, 0x0d, 0x25,
   1463 	0x91, 0x2c, 0x37, 0x1c, 0xeb, 0x07, 0x89, 0x33, 0xe1, 0x25, 0xd7, 0x43,
   1464 	0x10, 0x18, 0x00, 0x40, 0x62, 0xfe, 0xc7, 0xf5, 0x1d, 0xa1, 0x0e, 0x5b,
   1465 	0xe9, 0x2f, 0xcb, 0x4f, 0x30, 0x7b, 0x15, 0x17, 0x9c, 0xa1, 0x92, 0x1d,
   1466 	0xd4, 0x7f, 0x06, 0x2f, 0x14, 0xcd, 0x4b, 0x8f, 0xe6, 0x76, 0xe7, 0x6c,
   1467 	0x9a, 0xc1, 0xd0, 0x1f, 0xab, 0x5e, 0x0f, 0xf5, 0xa7, 0xa9, 0x9a, 0xca,
   1468 	0xf3, 0xcc, 0xbd, 0x5f, 0xfc, 0xfb, 0x6f, 0x5b, 0x01, 0xb7, 0x05, 0x39,
   1469 	0x11, 0xc3, 0xaa, 0x92, 0x5c, 0x06, 0xdb, 0x21, 0x10, 0x05, 0x00, 0x08,
   1470 	0xc4, 0x40, 0xd2, 0x39, 0xf1, 0x07, 0x72, 0xcb,
   1471 };
   1472 
   1473 struct m6_data {
   1474 	struct wsc_m6 expected;
   1475 	const uint8_t *expected_encrypted;
   1476 	size_t expected_encrypted_size;
   1477 	const void *pdu;
   1478 	unsigned int len;
   1479 };
   1480 
   1481 static const struct m6_data m6_data_1 = {
   1482 	.pdu = eap_wsc_m6 + 18,
   1483 	.len = sizeof(eap_wsc_m6) - 18,
   1484 	.expected = {
   1485 		.version2 = false,
   1486 		.enrollee_nonce = { 0x19, 0x8d, 0x0d, 0x25, 0x91, 0x2c, 0x37,
   1487 					0x1c, 0xeb, 0x07, 0x89, 0x33, 0xe1,
   1488 					0x25, 0xd7, 0x43 },
   1489 		.authenticator = { 0xc4, 0x40, 0xd2, 0x39, 0xf1, 0x07,
   1490 								0x72, 0xcb },
   1491 	},
   1492 	.expected_encrypted = eap_wsc_m6 + 52,
   1493 	.expected_encrypted_size = 64,
   1494 };
   1495 
   1496 static void wsc_test_parse_m6(const void *data)
   1497 {
   1498 	const struct m6_data *test = data;
   1499 	struct wsc_m6 m6;
   1500 	const struct wsc_m6 *expected = &test->expected;
   1501 	struct iovec encrypted;
   1502 	int r;
   1503 
   1504 	r = wsc_parse_m6(test->pdu, test->len, &m6, &encrypted);
   1505 	assert(r == 0);
   1506 
   1507 	assert(expected->version2 == m6.version2);
   1508 	assert(!memcmp(expected->enrollee_nonce, m6.enrollee_nonce, 16));
   1509 	assert(!memcmp(expected->authenticator, m6.authenticator, 8));
   1510 
   1511 	assert(test->expected_encrypted_size == encrypted.iov_len);
   1512 	assert(!memcmp(test->expected_encrypted,
   1513 					encrypted.iov_base, encrypted.iov_len));
   1514 }
   1515 
   1516 static void wsc_test_build_m6(const void *data)
   1517 {
   1518 	const struct m6_data *test = data;
   1519 	uint8_t *out;
   1520 	size_t out_len;
   1521 
   1522 	out = wsc_build_m6(&test->expected, test->expected_encrypted,
   1523 				test->expected_encrypted_size, &out_len);
   1524 	assert(out);
   1525 
   1526 	assert(out_len == test->len);
   1527 	assert(!memcmp(test->pdu, out, test->len));
   1528 
   1529 	l_free(out);
   1530 }
   1531 
   1532 static const unsigned char m6_encrypted_settings[] = {
   1533 	0x10, 0x40, 0x00, 0x10, 0x57, 0x29, 0x13, 0xb5, 0x89, 0xf1, 0x50, 0x7e,
   1534 	0x4d, 0x5c, 0x88, 0xda, 0x25, 0x9b, 0xce, 0xf0, 0x10, 0x1e, 0x00, 0x08,
   1535 	0x72, 0x8c, 0x3c, 0xf8, 0xd4, 0xd2, 0xb1, 0xa1,
   1536 };
   1537 
   1538 struct m6_encrypted_settings_data {
   1539 	struct wsc_m6_encrypted_settings expected;
   1540 	const void *pdu;
   1541 	unsigned int len;
   1542 };
   1543 
   1544 static const struct m6_encrypted_settings_data m6_encrypted_settings_data_1 = {
   1545 	.pdu = m6_encrypted_settings,
   1546 	.len = sizeof(m6_encrypted_settings),
   1547 	.expected = {
   1548 		.r_snonce2 = { 0x57, 0x29, 0x13, 0xb5, 0x89, 0xf1, 0x50, 0x7e,
   1549 			0x4d, 0x5c, 0x88, 0xda, 0x25, 0x9b, 0xce, 0xf0 },
   1550 		.authenticator = { 0x72, 0x8c, 0x3c, 0xf8, 0xd4, 0xd2,
   1551 					0xb1, 0xa1 },
   1552 	},
   1553 };
   1554 
   1555 static void wsc_test_parse_m6_encrypted_settings(const void *data)
   1556 {
   1557 	const struct m6_encrypted_settings_data *test = data;
   1558 	struct wsc_m6_encrypted_settings m6es;
   1559 	const struct wsc_m6_encrypted_settings *expected = &test->expected;
   1560 	int r;
   1561 
   1562 	r = wsc_parse_m6_encrypted_settings(test->pdu, test->len, &m6es);
   1563 	assert(r == 0);
   1564 
   1565 	assert(!memcmp(expected->r_snonce2, m6es.r_snonce2, 16));
   1566 	assert(!memcmp(expected->authenticator, m6es.authenticator, 8));
   1567 }
   1568 
   1569 static void wsc_test_build_m6_encrypted_settings(const void *data)
   1570 {
   1571 	const struct m6_encrypted_settings_data *test = data;
   1572 	uint8_t *out;
   1573 	size_t out_len;
   1574 
   1575 	out = wsc_build_m6_encrypted_settings(&test->expected, &out_len);
   1576 	assert(out);
   1577 
   1578 	assert(out_len == test->len);
   1579 	assert(!memcmp(test->pdu, out, test->len));
   1580 
   1581 	l_free(out);
   1582 }
   1583 
   1584 static const unsigned char eap_wsc_m7[] = {
   1585 	0x01, 0x00, 0x00, 0x86, 0x02, 0xaa, 0x00, 0x86, 0xfe, 0x00, 0x37, 0x2a,
   1586 	0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10,
   1587 	0x22, 0x00, 0x01, 0x0b, 0x10, 0x39, 0x00, 0x10, 0x67, 0xa5, 0x53, 0x61,
   1588 	0xee, 0x72, 0xc2, 0x21, 0x48, 0x78, 0xc5, 0x70, 0x89, 0x9f, 0xf2, 0xa0,
   1589 	0x10, 0x18, 0x00, 0x40, 0x4e, 0x3a, 0x4c, 0xf0, 0x88, 0x17, 0x69, 0x89,
   1590 	0xe1, 0x48, 0xd4, 0xc1, 0x0b, 0x96, 0xe8, 0xfd, 0x1e, 0x9b, 0xf4, 0xc0,
   1591 	0x74, 0xcc, 0xa2, 0xeb, 0x87, 0x58, 0xc9, 0x42, 0xd1, 0xf3, 0x89, 0xe2,
   1592 	0x27, 0x13, 0xfe, 0x40, 0x10, 0x45, 0x51, 0xfd, 0xc2, 0x1f, 0xe6, 0x60,
   1593 	0x84, 0xc2, 0x8f, 0x9d, 0x43, 0xbc, 0x03, 0xf3, 0x0a, 0x64, 0x8f, 0x18,
   1594 	0xe9, 0x96, 0x0f, 0x9e, 0xeb, 0x52, 0x73, 0x70, 0x10, 0x49, 0x00, 0x06,
   1595 	0x00, 0x37, 0x2a, 0x00, 0x01, 0x20, 0x10, 0x05, 0x00, 0x08, 0x05, 0x05,
   1596 	0x2f, 0x5f, 0x3c, 0x42, 0xf8, 0xa6,
   1597 };
   1598 
   1599 struct m7_data {
   1600 	struct wsc_m7 expected;
   1601 	const uint8_t *expected_encrypted;
   1602 	size_t expected_encrypted_size;
   1603 	const void *pdu;
   1604 	unsigned int len;
   1605 };
   1606 
   1607 static const struct m7_data m7_data_1 = {
   1608 	.pdu = eap_wsc_m7 + 18,
   1609 	.len = sizeof(eap_wsc_m7) - 18,
   1610 	.expected = {
   1611 		.version2 = true,
   1612 		.registrar_nonce = { 0x67, 0xa5, 0x53, 0x61, 0xee, 0x72, 0xc2,
   1613 					0x21, 0x48, 0x78, 0xc5, 0x70, 0x89,
   1614 					0x9f, 0xf2, 0xa0 },
   1615 		.authenticator = { 0x05, 0x05, 0x2f, 0x5f, 0x3c, 0x42,
   1616 					0xf8, 0xa6 },
   1617 	},
   1618 	.expected_encrypted = eap_wsc_m7 + 52,
   1619 	.expected_encrypted_size = 64,
   1620 };
   1621 
   1622 static void wsc_test_parse_m7(const void *data)
   1623 {
   1624 	const struct m7_data *test = data;
   1625 	struct wsc_m7 m7;
   1626 	const struct wsc_m7 *expected = &test->expected;
   1627 	struct iovec encrypted;
   1628 	int r;
   1629 
   1630 	r = wsc_parse_m7(test->pdu, test->len, &m7, &encrypted);
   1631 	assert(r == 0);
   1632 
   1633 	assert(expected->version2 == m7.version2);
   1634 	assert(!memcmp(expected->registrar_nonce, m7.registrar_nonce, 16));
   1635 	assert(!memcmp(expected->authenticator, m7.authenticator, 8));
   1636 
   1637 	assert(test->expected_encrypted_size == encrypted.iov_len);
   1638 	assert(!memcmp(test->expected_encrypted,
   1639 					encrypted.iov_base, encrypted.iov_len));
   1640 }
   1641 
   1642 static void wsc_test_build_m7(const void *data)
   1643 {
   1644 	const struct m7_data *test = data;
   1645 	uint8_t *out;
   1646 	size_t out_len;
   1647 
   1648 	out = wsc_build_m7(&test->expected, test->expected_encrypted,
   1649 				test->expected_encrypted_size, &out_len);
   1650 	assert(out);
   1651 
   1652 	assert(out_len == test->len);
   1653 	assert(!memcmp(test->pdu, out, test->len));
   1654 
   1655 	l_free(out);
   1656 }
   1657 
   1658 static const unsigned char eap_wsc_m8[] = {
   1659 	0x01, 0x00, 0x00, 0xcc, 0x01, 0xab, 0x00, 0xcc, 0xfe, 0x00, 0x37, 0x2a,
   1660 	0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10,
   1661 	0x22, 0x00, 0x01, 0x0c, 0x10, 0x1a, 0x00, 0x10, 0x19, 0x8d, 0x0d, 0x25,
   1662 	0x91, 0x2c, 0x37, 0x1c, 0xeb, 0x07, 0x89, 0x33, 0xe1, 0x25, 0xd7, 0x43,
   1663 	0x10, 0x18, 0x00, 0x90, 0x7e, 0x2f, 0x4a, 0xae, 0x60, 0x1e, 0x54, 0x75,
   1664 	0x3b, 0x6e, 0xfc, 0x0a, 0x84, 0xdb, 0xae, 0x62, 0x74, 0xf4, 0xe5, 0xe0,
   1665 	0xf2, 0xc5, 0xf1, 0x78, 0xc0, 0x76, 0x4d, 0xc4, 0x08, 0x6d, 0xd5, 0xb7,
   1666 	0xa2, 0x74, 0x18, 0x26, 0xee, 0x01, 0xe4, 0x3d, 0x5f, 0x44, 0xca, 0x93,
   1667 	0xad, 0x07, 0xc7, 0xa5, 0xcf, 0xf1, 0xb9, 0xb9, 0x68, 0xf1, 0x15, 0xa9,
   1668 	0xbc, 0x66, 0x7f, 0x03, 0xa1, 0x84, 0x80, 0x78, 0x71, 0x13, 0x09, 0xaa,
   1669 	0x74, 0x60, 0xa2, 0x0f, 0x49, 0x4a, 0x8b, 0x8f, 0xd9, 0xab, 0x07, 0x5f,
   1670 	0x79, 0xc1, 0xb2, 0x22, 0x16, 0xc7, 0x05, 0xf8, 0xe8, 0x3e, 0x46, 0x26,
   1671 	0x9a, 0xc5, 0x01, 0xfe, 0xa2, 0x15, 0x1c, 0xbe, 0x97, 0x87, 0xc8, 0x83,
   1672 	0xb9, 0xf2, 0x89, 0x82, 0xb8, 0xe0, 0x46, 0x08, 0xb1, 0xbd, 0x1d, 0xe8,
   1673 	0x9f, 0x1d, 0x50, 0xbd, 0x79, 0x2c, 0xa7, 0xf7, 0xe7, 0x34, 0xf4, 0x7c,
   1674 	0x28, 0xa4, 0x9a, 0x42, 0x6e, 0x94, 0x88, 0x78, 0x28, 0xfd, 0xea, 0xdf,
   1675 	0x13, 0x5c, 0x66, 0x79, 0x10, 0x05, 0x00, 0x08, 0xba, 0x2b, 0xfc, 0x68,
   1676 	0x49, 0x8e, 0x88, 0xfa,
   1677 };
   1678 
   1679 struct m8_data {
   1680 	struct wsc_m8 expected;
   1681 	const uint8_t *expected_encrypted;
   1682 	size_t expected_encrypted_size;
   1683 	const void *pdu;
   1684 	unsigned int len;
   1685 };
   1686 
   1687 static const struct m8_data m8_data_1 = {
   1688 	.pdu = eap_wsc_m8 + 18,
   1689 	.len = sizeof(eap_wsc_m8) - 18,
   1690 	.expected = {
   1691 		.version2 = false,
   1692 		.enrollee_nonce = { 0x19, 0x8d, 0x0d, 0x25, 0x91, 0x2c, 0x37,
   1693 					0x1c, 0xeb, 0x07, 0x89, 0x33, 0xe1,
   1694 					0x25, 0xd7, 0x43 },
   1695 		.authenticator = { 0xba, 0x2b, 0xfc, 0x68, 0x49, 0x8e,
   1696 					0x88, 0xfa },
   1697 
   1698 	},
   1699 	.expected_encrypted = eap_wsc_m8 + 52,
   1700 	.expected_encrypted_size = 144,
   1701 };
   1702 
   1703 static void wsc_test_parse_m8(const void *data)
   1704 {
   1705 	const struct m8_data *test = data;
   1706 	struct wsc_m8 m8;
   1707 	const struct wsc_m8 *expected = &test->expected;
   1708 	struct iovec encrypted;
   1709 	int r;
   1710 
   1711 	r = wsc_parse_m8(test->pdu, test->len, &m8, &encrypted);
   1712 	assert(r == 0);
   1713 
   1714 	assert(expected->version2 == m8.version2);
   1715 	assert(!memcmp(expected->enrollee_nonce, m8.enrollee_nonce, 16));
   1716 	assert(!memcmp(expected->authenticator, m8.authenticator, 8));
   1717 
   1718 	assert(test->expected_encrypted_size == encrypted.iov_len);
   1719 	assert(!memcmp(test->expected_encrypted,
   1720 					encrypted.iov_base, encrypted.iov_len));
   1721 }
   1722 
   1723 static void wsc_test_build_m8(const void *data)
   1724 {
   1725 	const struct m8_data *test = data;
   1726 	uint8_t *out;
   1727 	size_t out_len;
   1728 
   1729 	out = wsc_build_m8(&test->expected, test->expected_encrypted,
   1730 				test->expected_encrypted_size, &out_len);
   1731 	assert(out);
   1732 
   1733 	assert(out_len == test->len);
   1734 	assert(!memcmp(test->pdu, out, test->len));
   1735 
   1736 	l_free(out);
   1737 }
   1738 
   1739 static const unsigned char m8_encrypted_settings[] = {
   1740 	0x10, 0x0e, 0x00, 0x6a, 0x10, 0x26, 0x00, 0x01, 0x01, 0x10, 0x45, 0x00,
   1741 	0x07, 0x54, 0x65, 0x73, 0x74, 0x57, 0x50, 0x41, 0x10, 0x03, 0x00, 0x02,
   1742 	0x00, 0x20, 0x10, 0x0f, 0x00, 0x02, 0x00, 0x08, 0x10, 0x27, 0x00, 0x40,
   1743 	0x34, 0x36, 0x30, 0x34, 0x44, 0x30, 0x31, 0x46, 0x46, 0x44, 0x42, 0x30,
   1744 	0x42, 0x32, 0x39, 0x32, 0x45, 0x33, 0x37, 0x37, 0x33, 0x32, 0x44, 0x44,
   1745 	0x34, 0x45, 0x31, 0x31, 0x43, 0x32, 0x34, 0x30, 0x31, 0x31, 0x35, 0x34,
   1746 	0x32, 0x38, 0x39, 0x41, 0x30, 0x39, 0x41, 0x33, 0x33, 0x41, 0x44, 0x37,
   1747 	0x30, 0x34, 0x31, 0x37, 0x37, 0x41, 0x42, 0x30, 0x44, 0x31, 0x42, 0x37,
   1748 	0x35, 0x38, 0x44, 0x30, 0x10, 0x20, 0x00, 0x06, 0xa0, 0xa8, 0xcd, 0x1c,
   1749 	0x7e, 0xc9, 0x10, 0x1e, 0x00, 0x08, 0xe8, 0x3b, 0x3b, 0xe7, 0x9e, 0x72,
   1750 	0x06, 0x46,
   1751 };
   1752 
   1753 struct wsc_credential creds_1[1] = {
   1754 	{
   1755 		.ssid = "TestWPA",
   1756 		.ssid_len = 7,
   1757 		.auth_type = WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL,
   1758 		.encryption_type = WSC_ENCRYPTION_TYPE_AES,
   1759 		.network_key = { 0x34, 0x36, 0x30, 0x34, 0x44, 0x30, 0x31, 0x46,
   1760 			0x46, 0x44, 0x42, 0x30, 0x42, 0x32, 0x39, 0x32, 0x45,
   1761 			0x33, 0x37, 0x37, 0x33, 0x32, 0x44, 0x44, 0x34, 0x45,
   1762 			0x31, 0x31, 0x43, 0x32, 0x34, 0x30, 0x31, 0x31, 0x35,
   1763 			0x34, 0x32, 0x38, 0x39, 0x41, 0x30, 0x39, 0x41, 0x33,
   1764 			0x33, 0x41, 0x44, 0x37, 0x30, 0x34, 0x31, 0x37, 0x37,
   1765 			0x41, 0x42, 0x30, 0x44, 0x31, 0x42, 0x37, 0x35, 0x38,
   1766 			0x44, 0x30 },
   1767 		.network_key_len = 64,
   1768 		.addr = { 0xa0, 0xa8, 0xcd, 0x1c, 0x7e, 0xc9 },
   1769 	},
   1770 };
   1771 
   1772 struct m8_encrypted_settings_data {
   1773 	struct wsc_m8_encrypted_settings expected;
   1774 	struct wsc_credential *creds;
   1775 	unsigned int n_creds;
   1776 	const void *pdu;
   1777 	unsigned int len;
   1778 };
   1779 
   1780 static const struct m8_encrypted_settings_data m8_encrypted_settings_data_1 = {
   1781 	.pdu = m8_encrypted_settings,
   1782 	.len = sizeof(m8_encrypted_settings),
   1783 	.creds = creds_1,
   1784 	.n_creds = 1,
   1785 	.expected = {
   1786 		.authenticator = { 0xe8, 0x3b, 0x3b, 0xe7, 0x9e, 0x72,
   1787 					0x06, 0x46 },
   1788 	},
   1789 };
   1790 
   1791 static void wsc_test_parse_m8_encrypted_settings(const void *data)
   1792 {
   1793 	const struct m8_encrypted_settings_data *test = data;
   1794 	struct wsc_m8_encrypted_settings m8es;
   1795 	const struct wsc_m8_encrypted_settings *expected = &test->expected;
   1796 	struct iovec creds[10];
   1797 	size_t n_creds = 10;
   1798 	size_t i;
   1799 	int r;
   1800 
   1801 	r = wsc_parse_m8_encrypted_settings(test->pdu, test->len, &m8es,
   1802 							creds, &n_creds);
   1803 	assert(r == 0);
   1804 
   1805 	assert(expected->new_password_len == m8es.new_password_len);
   1806 
   1807 	if (m8es.new_password_len > 0) {
   1808 		assert(!memcmp(expected->new_password, m8es.new_password,
   1809 					m8es.new_password_len));
   1810 		assert(expected->device_password_id == m8es.device_password_id);
   1811 	}
   1812 
   1813 	assert(n_creds == test->n_creds);
   1814 
   1815 	for (i = 0; i < n_creds; i++) {
   1816 		struct wsc_credential cred;
   1817 
   1818 		assert(!wsc_parse_credential(creds[i].iov_base,
   1819 						creds[i].iov_len,
   1820 						&cred));
   1821 
   1822 		assert(test->creds[i].ssid_len == cred.ssid_len);
   1823 		assert(!memcmp(test->creds[i].ssid, cred.ssid, cred.ssid_len));
   1824 		assert(test->creds[i].auth_type == cred.auth_type);
   1825 		assert(test->creds[i].encryption_type == cred.encryption_type);
   1826 		assert(test->creds[i].network_key_len == cred.network_key_len);
   1827 		assert(!memcmp(test->creds[i].network_key, cred.network_key,
   1828 							cred.network_key_len));
   1829 		assert(!memcmp(test->creds[i].addr, cred.addr, 6));
   1830 	}
   1831 
   1832 	assert(!memcmp(expected->authenticator, m8es.authenticator, 8));
   1833 }
   1834 
   1835 static void wsc_test_build_m8_encrypted_settings(const void *data)
   1836 {
   1837 	const struct m8_encrypted_settings_data *test = data;
   1838 	uint8_t *out;
   1839 	size_t out_len;
   1840 
   1841 	out = wsc_build_m8_encrypted_settings(&test->expected, test->creds,
   1842 						test->n_creds, &out_len);
   1843 	assert(out);
   1844 
   1845 	assert(out_len == test->len);
   1846 	assert(!memcmp(test->pdu, out, test->len));
   1847 
   1848 	l_free(out);
   1849 }
   1850 
   1851 static const unsigned char eap_wsc_done[] = {
   1852 	0x01, 0x00, 0x00, 0x4a, 0x02, 0xab, 0x00, 0x4a, 0xfe, 0x00, 0x37, 0x2a,
   1853 	0x00, 0x00, 0x00, 0x01, 0x05, 0x00, 0x10, 0x4a, 0x00, 0x01, 0x10, 0x10,
   1854 	0x22, 0x00, 0x01, 0x0f, 0x10, 0x1a, 0x00, 0x10, 0x19, 0x8d, 0x0d, 0x25,
   1855 	0x91, 0x2c, 0x37, 0x1c, 0xeb, 0x07, 0x89, 0x33, 0xe1, 0x25, 0xd7, 0x43,
   1856 	0x10, 0x39, 0x00, 0x10, 0x67, 0xa5, 0x53, 0x61, 0xee, 0x72, 0xc2, 0x21,
   1857 	0x48, 0x78, 0xc5, 0x70, 0x89, 0x9f, 0xf2, 0xa0, 0x10, 0x49, 0x00, 0x06,
   1858 	0x00, 0x37, 0x2a, 0x00, 0x01, 0x20,
   1859 };
   1860 
   1861 struct wsc_done_data {
   1862 	struct wsc_done expected;
   1863 	const void *pdu;
   1864 	unsigned int len;
   1865 };
   1866 
   1867 static const struct wsc_done_data wsc_done_data_1 = {
   1868 	.pdu = eap_wsc_done + 18,
   1869 	.len = sizeof(eap_wsc_done) - 18,
   1870 	.expected = {
   1871 		.version2 = true,
   1872 		.enrollee_nonce = { 0x19, 0x8d, 0x0d, 0x25, 0x91, 0x2c, 0x37,
   1873 					0x1c, 0xeb, 0x07, 0x89, 0x33, 0xe1,
   1874 					0x25, 0xd7, 0x43 },
   1875 		.registrar_nonce = { 0x67, 0xa5, 0x53, 0x61, 0xee, 0x72, 0xc2,
   1876 					0x21, 0x48, 0x78, 0xc5, 0x70, 0x89,
   1877 					0x9f, 0xf2, 0xa0 },
   1878 	},
   1879 };
   1880 
   1881 static void wsc_test_parse_wsc_done(const void *data)
   1882 {
   1883 	const struct wsc_done_data *test = data;
   1884 	struct wsc_done done;
   1885 	const struct wsc_done *expected = &test->expected;
   1886 	int r;
   1887 
   1888 	r = wsc_parse_wsc_done(test->pdu, test->len, &done);
   1889 	assert(r == 0);
   1890 
   1891 	assert(expected->version2 == done.version2);
   1892 	assert(!memcmp(expected->enrollee_nonce, done.enrollee_nonce, 16));
   1893 	assert(!memcmp(expected->registrar_nonce, done.registrar_nonce, 16));
   1894 }
   1895 
   1896 static void wsc_test_build_wsc_done(const void *data)
   1897 {
   1898 	const struct wsc_done_data *test = data;
   1899 	struct wsc_done done;
   1900 	uint8_t *out;
   1901 	size_t out_len;
   1902 
   1903 	memcpy(&done, &test->expected, sizeof(done));
   1904 
   1905 	out = wsc_build_wsc_done(&done, &out_len);
   1906 	assert(out);
   1907 
   1908 	assert(out_len == test->len);
   1909 	assert(!memcmp(test->pdu, out, test->len));
   1910 
   1911 	l_free(out);
   1912 }
   1913 
   1914 static const uint8_t eap_identity_req[] = {
   1915 	0x01, 0x00, 0x00, 0x05, 0x01, 0x00, 0x00, 0x05, 0x01
   1916 };
   1917 
   1918 static const uint8_t eap_identity_resp[] = {
   1919 	0x01, 0x00, 0x00, 0x22, 0x02, 0x00, 0x00, 0x22,
   1920 	0x01, 0x57, 0x46, 0x41, 0x2d, 0x53, 0x69, 0x6d,
   1921 	0x70, 0x6c, 0x65, 0x43, 0x6f, 0x6e, 0x66, 0x69,
   1922 	0x67, 0x2d, 0x45, 0x6e, 0x72, 0x6f, 0x6c, 0x6c,
   1923 	0x65, 0x65, 0x2d, 0x31, 0x2d, 0x30,
   1924 };
   1925 
   1926 static const uint8_t eap_wsc_start[] = {
   1927 	0x01, 0x00, 0x00, 0x0e, 0x01, 0x01, 0x00, 0x0e, 0xfe, 0x00, 0x37, 0x2a,
   1928 	0x00, 0x00, 0x00, 0x01, 0x01, 0x00,
   1929 };
   1930 
   1931 static const uint8_t eap_fail[] = {
   1932 	0x01, 0x00, 0x00, 0x04, 0x04, 0xab, 0x00, 0x04,
   1933 };
   1934 
   1935 struct verify_data {
   1936 	bool response_sent;
   1937 	bool eapol_failed;
   1938 	const void *expected;
   1939 	size_t expected_len;
   1940 	struct wsc_credential *expected_creds;
   1941 	unsigned int n_creds;
   1942 	unsigned int cur_cred;
   1943 };
   1944 
   1945 #define VERIFY_RESET(verify, e)				\
   1946 	verify.response_sent = false;			\
   1947 	verify.expected = e;				\
   1948 	verify.expected_len = sizeof(e)
   1949 
   1950 static void verify_handshake_event(struct handshake_state *hs,
   1951 					enum handshake_event event,
   1952 					void *user_data, ...)
   1953 {
   1954 	struct verify_data *data = user_data;
   1955 	va_list args;
   1956 
   1957 	va_start(args, user_data);
   1958 
   1959 	switch (event) {
   1960 	case HANDSHAKE_EVENT_FAILED:
   1961 		assert(va_arg(args, int) == MMPDU_REASON_CODE_IEEE8021X_FAILED);
   1962 		data->eapol_failed = true;
   1963 		break;
   1964 	case HANDSHAKE_EVENT_EAP_NOTIFY:
   1965 	{
   1966 		const struct wsc_credential *cred;
   1967 
   1968 		assert(va_arg(args, unsigned int) ==
   1969 			EAP_WSC_EVENT_CREDENTIAL_OBTAINED);
   1970 
   1971 		cred = va_arg(args, const struct wsc_credential *);
   1972 		assert(!memcmp(cred, data->expected_creds + data->cur_cred,
   1973 						sizeof(struct wsc_credential)));
   1974 
   1975 		data->cur_cred += 1;
   1976 		break;
   1977 	}
   1978 	default:
   1979 		break;
   1980 	}
   1981 
   1982 	va_end(args);
   1983 }
   1984 
   1985 static int verify_8021x(uint32_t ifindex,
   1986 			const uint8_t *aa_addr, uint16_t proto,
   1987 			const struct eapol_frame *ef, bool noencrypt,
   1988 			void *user_data)
   1989 {
   1990 	struct verify_data *data = user_data;
   1991 	size_t len = sizeof(struct eapol_header) +
   1992 		L_BE16_TO_CPU(ef->header.packet_len);
   1993 
   1994 	assert(len == data->expected_len);
   1995 	assert(!memcmp(ef, data->expected, data->expected_len));
   1996 
   1997 	data->response_sent = true;
   1998 
   1999 	return 0;
   2000 }
   2001 
   2002 static void wsc_test_pbc_handshake(const void *data)
   2003 {
   2004 	static uint8_t ap_address[] = { 0x24, 0xa2, 0xe1, 0xec, 0x17, 0x04 };
   2005 	static uint8_t sta_address[] = { 0xa0, 0xa8, 0xcd, 0x1c, 0x7e, 0xc9 };
   2006 	struct verify_data verify;
   2007 	struct handshake_state *hs;
   2008 	struct eapol_sm *sm;
   2009 	struct l_settings *settings;
   2010 
   2011 	eap_init();
   2012 	eapol_init();
   2013 
   2014 	hs = test_handshake_state_new(1);
   2015 	sm = eapol_sm_new(hs);
   2016 	eapol_register(sm);
   2017 
   2018 	handshake_state_set_authenticator_address(hs, ap_address);
   2019 	handshake_state_set_supplicant_address(hs, sta_address);
   2020 	handshake_state_set_event_func(hs, verify_handshake_event, &verify);
   2021 
   2022 	__eapol_set_tx_packet_func(verify_8021x);
   2023 	__eapol_set_tx_user_data(&verify);
   2024 
   2025 	settings = l_settings_new();
   2026 	l_settings_set_string(settings, "Security", "EAP-Identity",
   2027 					"WFA-SimpleConfig-Enrollee-1-0");
   2028 	l_settings_set_string(settings, "Security", "EAP-Method", "WSC");
   2029 
   2030 	l_settings_set_uint(settings, "WSC", "RFBand", WSC_RF_BAND_2_4_GHZ);
   2031 	l_settings_set_uint(settings, "WSC", "ConfigurationMethods",
   2032 				WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN |
   2033 				WSC_CONFIGURATION_METHOD_KEYPAD |
   2034 				WSC_CONFIGURATION_METHOD_DISPLAY |
   2035 				WSC_CONFIGURATION_METHOD_NFC_INTERFACE);
   2036 	l_settings_set_string(settings, "WSC", "PrimaryDeviceType",
   2037 					"0-00000000-0");
   2038 	l_settings_set_string(settings, "WSC", "EnrolleeMAC",
   2039 					util_address_to_string(sta_address));
   2040 	l_settings_set_bytes(settings, "WSC", "EnrolleeNonce",
   2041 				m1_data_2.expected.enrollee_nonce, 16);
   2042 	l_settings_set_bytes(settings, "WSC", "PrivateKey",
   2043 				m1_data_2.private_key,
   2044 				m1_data_2.private_key_size);
   2045 	l_settings_set_string(settings, "WSC", "E-SNonce1",
   2046 					"fdbb480ee6f572f3591cc3b364f2185b");
   2047 	l_settings_set_string(settings, "WSC", "E-SNonce2",
   2048 					"c12698739faf385920ba659d524c71c9");
   2049 	l_settings_set_string(settings, "WSC", "IV1",
   2050 					"9a31f84b4672f2ccf63c845eed3464ec");
   2051 	l_settings_set_string(settings, "WSC", "IV2",
   2052 					"4e3a4cf088176989e148d4c10b96e8fd");
   2053 
   2054 	handshake_state_set_8021x_config(hs, settings);
   2055 	eapol_start(sm);
   2056 
   2057 	l_settings_free(settings);
   2058 
   2059 	VERIFY_RESET(verify, eap_identity_resp);
   2060 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_identity_req,
   2061 					sizeof(eap_identity_req), false);
   2062 	assert(verify.response_sent);
   2063 
   2064 	VERIFY_RESET(verify, eap_wsc_m1_2);
   2065 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_start,
   2066 						sizeof(eap_wsc_start), false);
   2067 	assert(verify.response_sent);
   2068 
   2069 	VERIFY_RESET(verify, eap_wsc_m3);
   2070 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_m2_2,
   2071 						sizeof(eap_wsc_m2_2), false);
   2072 	assert(verify.response_sent);
   2073 
   2074 	VERIFY_RESET(verify, eap_wsc_m5);
   2075 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_m4,
   2076 						sizeof(eap_wsc_m4), false);
   2077 	assert(verify.response_sent);
   2078 
   2079 	VERIFY_RESET(verify, eap_wsc_m7);
   2080 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_m6,
   2081 						sizeof(eap_wsc_m6), false);
   2082 	assert(verify.response_sent);
   2083 
   2084 	VERIFY_RESET(verify, eap_wsc_done);
   2085 	verify.expected_creds = creds_1;
   2086 	verify.n_creds = 1;
   2087 	verify.cur_cred = 0;
   2088 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_m8,
   2089 						sizeof(eap_wsc_m8), false);
   2090 	assert(verify.response_sent);
   2091 	assert(verify.cur_cred == 1);
   2092 
   2093 	__eapol_rx_packet(1, ap_address, ETH_P_PAE,
   2094 					eap_fail, sizeof(eap_fail), false);
   2095 	assert(verify.eapol_failed);
   2096 
   2097 	handshake_state_unref(hs);
   2098 	eapol_exit();
   2099 	eap_exit();
   2100 }
   2101 
   2102 static void wsc_test_retransmission_no_fragmentation(const void *data)
   2103 {
   2104 	static uint8_t ap_address[] = { 0x24, 0xa2, 0xe1, 0xec, 0x17, 0x04 };
   2105 	static uint8_t sta_address[] = { 0xa0, 0xa8, 0xcd, 0x1c, 0x7e, 0xc9 };
   2106 	struct verify_data verify;
   2107 	struct handshake_state *hs;
   2108 	struct eapol_sm *sm;
   2109 	struct l_settings *settings;
   2110 
   2111 	eap_init();
   2112 	eapol_init();
   2113 
   2114 	hs = test_handshake_state_new(1);
   2115 	sm = eapol_sm_new(hs);
   2116 	eapol_register(sm);
   2117 
   2118 	handshake_state_set_authenticator_address(hs, ap_address);
   2119 	handshake_state_set_supplicant_address(hs, sta_address);
   2120 	handshake_state_set_event_func(hs, verify_handshake_event, &verify);
   2121 
   2122 	__eapol_set_tx_packet_func(verify_8021x);
   2123 	__eapol_set_tx_user_data(&verify);
   2124 
   2125 	settings = l_settings_new();
   2126 	l_settings_set_string(settings, "Security", "EAP-Identity",
   2127 					"WFA-SimpleConfig-Enrollee-1-0");
   2128 	l_settings_set_string(settings, "Security", "EAP-Method", "WSC");
   2129 
   2130 	l_settings_set_uint(settings, "WSC", "RFBand", WSC_RF_BAND_2_4_GHZ);
   2131 	l_settings_set_uint(settings, "WSC", "ConfigurationMethods",
   2132 				WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN |
   2133 				WSC_CONFIGURATION_METHOD_KEYPAD |
   2134 				WSC_CONFIGURATION_METHOD_DISPLAY |
   2135 				WSC_CONFIGURATION_METHOD_NFC_INTERFACE);
   2136 	l_settings_set_string(settings, "WSC", "PrimaryDeviceType",
   2137 					"0-00000000-0");
   2138 	l_settings_set_string(settings, "WSC", "EnrolleeMAC",
   2139 					util_address_to_string(sta_address));
   2140 	l_settings_set_bytes(settings, "WSC", "EnrolleeNonce",
   2141 				m1_data_2.expected.enrollee_nonce, 16);
   2142 	l_settings_set_bytes(settings, "WSC", "PrivateKey",
   2143 				m1_data_2.private_key,
   2144 				m1_data_2.private_key_size);
   2145 	l_settings_set_string(settings, "WSC", "E-SNonce1",
   2146 					"fdbb480ee6f572f3591cc3b364f2185b");
   2147 	l_settings_set_string(settings, "WSC", "E-SNonce2",
   2148 					"c12698739faf385920ba659d524c71c9");
   2149 	l_settings_set_string(settings, "WSC", "IV1",
   2150 					"9a31f84b4672f2ccf63c845eed3464ec");
   2151 	l_settings_set_string(settings, "WSC", "IV2",
   2152 					"4e3a4cf088176989e148d4c10b96e8fd");
   2153 
   2154 	handshake_state_set_8021x_config(hs, settings);
   2155 	eapol_start(sm);
   2156 
   2157 	l_settings_free(settings);
   2158 
   2159 	VERIFY_RESET(verify, eap_identity_resp);
   2160 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_identity_req,
   2161 					sizeof(eap_identity_req), false);
   2162 	assert(verify.response_sent);
   2163 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_identity_req,
   2164 					sizeof(eap_identity_req), false);
   2165 	assert(verify.response_sent);
   2166 
   2167 	VERIFY_RESET(verify, eap_wsc_m1_2);
   2168 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_start,
   2169 						sizeof(eap_wsc_start), false);
   2170 	assert(verify.response_sent);
   2171 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_start,
   2172 						sizeof(eap_wsc_start), false);
   2173 	assert(verify.response_sent);
   2174 
   2175 	VERIFY_RESET(verify, eap_wsc_m3);
   2176 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_m2_2,
   2177 						sizeof(eap_wsc_m2_2), false);
   2178 	assert(verify.response_sent);
   2179 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_m2_2,
   2180 						sizeof(eap_wsc_m2_2), false);
   2181 	assert(verify.response_sent);
   2182 
   2183 	VERIFY_RESET(verify, eap_wsc_m3);
   2184 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_m2_2,
   2185 						sizeof(eap_wsc_m2_2), false);
   2186 	assert(verify.response_sent);
   2187 
   2188 	VERIFY_RESET(verify, eap_wsc_m5);
   2189 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_m4,
   2190 						sizeof(eap_wsc_m4), false);
   2191 	assert(verify.response_sent);
   2192 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_m4,
   2193 						sizeof(eap_wsc_m4), false);
   2194 	assert(verify.response_sent);
   2195 
   2196 	VERIFY_RESET(verify, eap_wsc_m7);
   2197 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_m6,
   2198 						sizeof(eap_wsc_m6), false);
   2199 	assert(verify.response_sent);
   2200 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_m6,
   2201 						sizeof(eap_wsc_m6), false);
   2202 	assert(verify.response_sent);
   2203 
   2204 	VERIFY_RESET(verify, eap_wsc_done);
   2205 	verify.expected_creds = creds_1;
   2206 	verify.n_creds = 1;
   2207 	verify.cur_cred = 0;
   2208 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_wsc_m8,
   2209 						sizeof(eap_wsc_m8), false);
   2210 	assert(verify.response_sent);
   2211 	assert(verify.cur_cred == 1);
   2212 
   2213 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_fail,
   2214 						sizeof(eap_fail), false);
   2215 	assert(verify.eapol_failed);
   2216 	__eapol_rx_packet(1, ap_address, ETH_P_PAE, eap_fail,
   2217 						sizeof(eap_fail), false);
   2218 	assert(verify.eapol_failed);
   2219 
   2220 	handshake_state_unref(hs);
   2221 	eapol_exit();
   2222 	eap_exit();
   2223 }
   2224 
   2225 static void wsc_test_pin_valid(const void *data)
   2226 {
   2227 	assert(wsc_pin_is_valid("1234"));
   2228 	assert(wsc_pin_is_valid("12345678"));
   2229 	assert(!wsc_pin_is_valid(""));
   2230 	assert(!wsc_pin_is_valid("123456789"));
   2231 	assert(!wsc_pin_is_valid("abcdefgh"));
   2232 }
   2233 
   2234 static void wsc_test_pin_checksum(const void *data)
   2235 {
   2236 	assert(!wsc_pin_is_checksum_valid("12345671"));
   2237 	assert(wsc_pin_is_checksum_valid("12345670"));
   2238 	assert(wsc_pin_is_checksum_valid("24681353"));
   2239 }
   2240 
   2241 static void wsc_test_pin_generate(const void *data)
   2242 {
   2243 	int i;
   2244 	char pin[9];
   2245 
   2246 	for (i = 0; i < 1000; i++) {
   2247 		assert(wsc_pin_generate(pin));
   2248 		assert(wsc_pin_is_valid(pin));
   2249 		assert(wsc_pin_is_checksum_valid(pin));
   2250 	}
   2251 }
   2252 
   2253 struct test_ap_sta_data {
   2254 	struct handshake_state *ap_hs;
   2255 	struct handshake_state *sta_hs;
   2256 	const uint8_t ap_address[6];
   2257 	const uint8_t sta_address[6];
   2258 
   2259 	uint8_t to_sta_data[1024];
   2260 	int to_sta_data_len;
   2261 	int to_sta_msg_cnt;
   2262 	int to_ap_msg_cnt;
   2263 	struct eapol_sm *ap_sm;
   2264 	struct eapol_sm *sta_sm;
   2265 };
   2266 
   2267 static int test_ap_sta_eapol_tx(uint32_t ifindex,
   2268 					const uint8_t *dest, uint16_t proto,
   2269 					const struct eapol_frame *ef,
   2270 					bool noencrypt, void *user_data)
   2271 {
   2272 	struct test_ap_sta_data *s = user_data;
   2273 	size_t len = sizeof(struct eapol_header) +
   2274 		L_BE16_TO_CPU(ef->header.packet_len);
   2275 
   2276 	assert(ifindex == s->ap_hs->ifindex || ifindex == s->sta_hs->ifindex);
   2277 	assert(proto == ETH_P_PAE && !noencrypt);
   2278 
   2279 	if (ifindex == s->ap_hs->ifindex) {	/* From AP to STA */
   2280 		assert(!memcmp(dest, s->sta_address, 6));
   2281 		assert(len < sizeof(s->to_sta_data));
   2282 		memcpy(s->to_sta_data, ef, len);
   2283 		s->to_sta_data_len = len;
   2284 		s->to_sta_msg_cnt++;
   2285 		return 0;
   2286 	}
   2287 
   2288 	/* From STA to AP */
   2289 	assert(!memcmp(dest, s->ap_address, 6));
   2290 	s->to_ap_msg_cnt++;
   2291 	__eapol_rx_packet(1, s->sta_address, proto, (const void *) ef, len,
   2292 				noencrypt);
   2293 	return 0;
   2294 }
   2295 
   2296 static void test_ap_sta_run(struct test_ap_sta_data *s)
   2297 {
   2298 	eap_init();
   2299 	eapol_init();
   2300 	__eapol_set_tx_packet_func(test_ap_sta_eapol_tx);
   2301 	__eapol_set_tx_user_data(s);
   2302 
   2303 	s->to_sta_msg_cnt = 0;
   2304 	s->to_ap_msg_cnt = 0;
   2305 	s->to_sta_data_len = 0;
   2306 
   2307 	s->ap_sm = eapol_sm_new(s->ap_hs);
   2308 	eapol_register(s->ap_sm);
   2309 
   2310 	s->sta_sm = eapol_sm_new(s->sta_hs);
   2311 	eapol_register(s->sta_sm);
   2312 
   2313 	eapol_start(s->sta_sm);
   2314 	eapol_start(s->ap_sm);
   2315 
   2316 	while (s->to_sta_data_len) {
   2317 		int len = s->to_sta_data_len;
   2318 		s->to_sta_data_len = 0;
   2319 		__eapol_rx_packet(s->sta_hs->ifindex, s->ap_address, ETH_P_PAE,
   2320 					s->to_sta_data, len, false);
   2321 	}
   2322 
   2323 	eapol_exit();
   2324 	eap_exit();
   2325 }
   2326 
   2327 struct test_ap_sta_hs {
   2328 	struct handshake_state super;
   2329 	struct test_ap_sta_data *s;
   2330 };
   2331 
   2332 static struct handshake_state *test_ap_sta_hs_new(struct test_ap_sta_data *s,
   2333 							uint32_t ifindex)
   2334 {
   2335 	struct test_ap_sta_hs *ths = l_new(struct test_ap_sta_hs, 1);
   2336 
   2337 	ths->super.ifindex = ifindex;
   2338 	ths->super.free = (void (*)(struct handshake_state *s)) l_free;
   2339 	ths->super.refcount = 1;
   2340 	ths->s = s;
   2341 
   2342 	return &ths->super;
   2343 }
   2344 
   2345 static bool random_nonce(uint8_t nonce[])
   2346 {
   2347 	return l_getrandom(nonce, 32);
   2348 }
   2349 
   2350 static void test_ap_sta_install_tk(struct handshake_state *hs, uint8_t key_idx,
   2351 					const uint8_t *tk, uint32_t cipher)
   2352 {
   2353 	assert(false);
   2354 }
   2355 
   2356 struct wsc_r_test_success_data {
   2357 	bool credentials_obtained;
   2358 	bool credentials_sent;
   2359 	bool ap_eap_failed;
   2360 	bool sta_eap_failed;
   2361 	struct test_ap_sta_data *s;
   2362 	struct wsc_credential expected_creds;
   2363 };
   2364 
   2365 static void test_ap_sta_hs_event_ap(struct handshake_state *hs,
   2366 				enum handshake_event event,
   2367 				void *user_data, ...)
   2368 {
   2369 	struct wsc_r_test_success_data *data = user_data;
   2370 	va_list args;
   2371 
   2372 	va_start(args, user_data);
   2373 
   2374 	switch (event) {
   2375 	case HANDSHAKE_EVENT_EAP_NOTIFY:
   2376 		assert(va_arg(args, unsigned int) ==
   2377 			EAP_WSC_EVENT_CREDENTIAL_SENT);
   2378 		assert(!data->credentials_sent);
   2379 		assert(!data->ap_eap_failed);
   2380 		data->credentials_sent = true;
   2381 		break;
   2382 	case HANDSHAKE_EVENT_FAILED:
   2383 		assert(va_arg(args, int) ==
   2384 			MMPDU_REASON_CODE_IEEE8021X_FAILED);
   2385 		assert(!data->ap_eap_failed);
   2386 		data->ap_eap_failed = true;
   2387 		data->s->ap_sm = NULL;
   2388 		break;
   2389 	default:
   2390 		break;
   2391 	}
   2392 
   2393 	va_end(args);
   2394 }
   2395 
   2396 static void test_ap_sta_hs_event_sta(struct handshake_state *hs,
   2397 				enum handshake_event event,
   2398 				void *user_data, ...)
   2399 {
   2400 	struct wsc_r_test_success_data *data = user_data;
   2401 	va_list args;
   2402 
   2403 	va_start(args, user_data);
   2404 
   2405 	switch (event) {
   2406 	case HANDSHAKE_EVENT_EAP_NOTIFY:
   2407 	{
   2408 		unsigned int eap_event = va_arg(args, unsigned int);
   2409 		const struct wsc_credential *cred;
   2410 
   2411 		assert(eap_event == EAP_WSC_EVENT_CREDENTIAL_OBTAINED);
   2412 		cred = va_arg(args, const struct wsc_credential *);
   2413 		assert(!memcmp(cred, &data->expected_creds, sizeof(*cred)));
   2414 		assert(!data->credentials_obtained);
   2415 		assert(!data->sta_eap_failed);
   2416 		data->credentials_obtained = true;
   2417 		break;
   2418 	}
   2419 	case HANDSHAKE_EVENT_FAILED:
   2420 		assert(va_arg(args, int) ==
   2421 			MMPDU_REASON_CODE_IEEE8021X_FAILED);
   2422 		assert(!data->sta_eap_failed);
   2423 		data->sta_eap_failed = true;
   2424 		data->s->sta_sm = NULL;
   2425 		break;
   2426 	default:
   2427 		break;
   2428 	}
   2429 
   2430 	va_end(args);
   2431 }
   2432 
   2433 static void wsc_r_test_pbc_handshake(const void *data)
   2434 {
   2435 	static const unsigned char ap_rsne[] = {
   2436 		0x30, 0x12, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x04,
   2437 		0x01, 0x00, 0x00, 0x0f, 0xac, 0x04, 0x01, 0x00,
   2438 		0x00, 0x0f, 0xac, 0x02 };
   2439 	static const char *ssid = "TestWPA2EAP";
   2440 	static const char *ap_8021x_str = "[Security]\n"
   2441 		"EAP-Method=WSC-R\n"
   2442 		"[WSC]\n"
   2443 		"UUID-R=00112233445566778899aabbccddeeff\n"
   2444 		"RFBand=1\n"
   2445 		"ConfigurationMethods=0x680\n";
   2446 	static const char *sta_8021x_str = "[Security]\n"
   2447 		"EAP-Identity=WFA-SimpleConfig-Enrollee-1-0\n"
   2448 		"EAP-Method=WSC\n"
   2449 		"[WSC]\n"
   2450 		"RFBand=1\n"
   2451 		"ConfigurationMethods=0x680\n";
   2452 	struct l_settings *ap_8021x_settings = l_settings_new();
   2453 	struct l_settings *sta_8021x_settings = l_settings_new();
   2454 	struct test_ap_sta_data s = {
   2455 		.ap_hs = test_ap_sta_hs_new(&s, 1),
   2456 		.sta_hs = test_ap_sta_hs_new(&s, 2),
   2457 		.ap_address = { 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 },
   2458 		.sta_address = { 0x02, 0x03, 0x04, 0x05, 0x06, 0x08 },
   2459 	};
   2460 	const struct wsc_credential *expected_creds = data;
   2461 	struct wsc_r_test_success_data wsc_data = {
   2462 		.s = &s,
   2463 		.expected_creds = *expected_creds,
   2464 	};
   2465 	uint8_t uuid_e[16];
   2466 	char ssid_str[33];
   2467 
   2468 	wsc_uuid_from_addr(s.sta_address, uuid_e);
   2469 
   2470 	memcpy(wsc_data.expected_creds.addr, s.sta_address, 6);
   2471 	memset(wsc_data.expected_creds.ssid + expected_creds->ssid_len, 0,
   2472 		sizeof(expected_creds->ssid) - expected_creds->ssid_len);
   2473 	memset(wsc_data.expected_creds.network_key +
   2474 		expected_creds->network_key_len, 0,
   2475 		sizeof(expected_creds->network_key) -
   2476 		expected_creds->network_key_len);
   2477 
   2478 	memcpy(ssid_str, expected_creds->ssid, expected_creds->ssid_len);
   2479 	ssid_str[expected_creds->ssid_len] = '\0';
   2480 
   2481 	l_settings_load_from_data(ap_8021x_settings, ap_8021x_str,
   2482 					strlen(ap_8021x_str));
   2483 	l_settings_set_string(ap_8021x_settings, "WSC", "EnrolleeMAC",
   2484 				util_address_to_string(s.sta_address));
   2485 	l_settings_set_bytes(ap_8021x_settings, "WSC", "UUID-E", uuid_e, 16);
   2486 
   2487 	if (expected_creds->auth_type == WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL) {
   2488 		l_settings_set_string(ap_8021x_settings, "WSC",
   2489 					"WPA2-SSID", ssid_str);
   2490 
   2491 		if (expected_creds->network_key_len < 64)
   2492 			l_settings_set_string(ap_8021x_settings, "WSC",
   2493 					"WPA2-Passphrase", (char *)
   2494 					wsc_data.expected_creds.network_key);
   2495 		else {
   2496 			char psk_str[65];
   2497 
   2498 			/*
   2499 			 * The PSK is 32-byte long but the setting value is
   2500 			 * a hexstring, same as what we expect to receive in
   2501 			 * the raw wsc_credential.network_key data.
   2502 			 */
   2503 			memcpy(psk_str, expected_creds->network_key, 64);
   2504 			psk_str[64] = '\0';
   2505 			l_settings_set_string(ap_8021x_settings, "WSC",
   2506 						"WPA2-PSK", psk_str);
   2507 		}
   2508 	} else
   2509 		l_settings_set_string(ap_8021x_settings, "WSC",
   2510 					"Open-SSID", ssid_str);
   2511 
   2512 	__handshake_set_get_nonce_func(random_nonce);
   2513 	__handshake_set_install_tk_func(test_ap_sta_install_tk);
   2514 	__handshake_set_install_gtk_func(NULL);
   2515 
   2516 	handshake_state_set_authenticator(s.ap_hs, true);
   2517 	handshake_state_set_event_func(s.ap_hs, test_ap_sta_hs_event_ap,
   2518 					&wsc_data);
   2519 	handshake_state_set_authenticator_address(s.ap_hs, s.ap_address);
   2520 	handshake_state_set_supplicant_address(s.ap_hs, s.sta_address);
   2521 	handshake_state_set_authenticator_ie(s.ap_hs, ap_rsne);
   2522 	handshake_state_set_ssid(s.ap_hs, (void *) ssid, strlen(ssid));
   2523 	handshake_state_set_8021x_config(s.ap_hs, ap_8021x_settings);
   2524 
   2525 	handshake_state_set_authenticator(s.sta_hs, false);
   2526 	handshake_state_set_event_func(s.sta_hs, test_ap_sta_hs_event_sta,
   2527 					&wsc_data);
   2528 	handshake_state_set_authenticator_address(s.sta_hs, s.ap_address);
   2529 	handshake_state_set_supplicant_address(s.sta_hs, s.sta_address);
   2530 	handshake_state_set_authenticator_ie(s.sta_hs, ap_rsne);
   2531 	handshake_state_set_ssid(s.sta_hs, (void *) ssid, strlen(ssid));
   2532 	l_settings_load_from_data(sta_8021x_settings, sta_8021x_str,
   2533 					strlen(sta_8021x_str));
   2534 	l_settings_set_string(sta_8021x_settings, "WSC", "EnrolleeMAC",
   2535 				util_address_to_string(s.sta_address));
   2536 	handshake_state_set_8021x_config(s.sta_hs, sta_8021x_settings);
   2537 
   2538 	test_ap_sta_run(&s);
   2539 
   2540 	handshake_state_unref(s.ap_hs);
   2541 	handshake_state_unref(s.sta_hs);
   2542 	__handshake_set_install_tk_func(NULL);
   2543 	l_settings_free(ap_8021x_settings);
   2544 	l_settings_free(sta_8021x_settings);
   2545 
   2546 	assert(wsc_data.sta_eap_failed && wsc_data.credentials_obtained);
   2547 	assert(wsc_data.ap_eap_failed && wsc_data.credentials_sent);
   2548 }
   2549 
   2550 struct wsc_credential wsc_r_test_wpa2_cred_passphrase = {
   2551 	.ssid_len = 7,
   2552 	.ssid = "thessid",
   2553 	.auth_type = WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL,
   2554 	.network_key_len = 12,
   2555 	.network_key = "secretsecret",
   2556 	.encryption_type = WSC_ENCRYPTION_TYPE_AES_TKIP,
   2557 };
   2558 
   2559 struct wsc_credential wsc_r_test_wpa2_cred_psk = {
   2560 	.ssid_len = 7,
   2561 	.ssid = "thessid",
   2562 	.auth_type = WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL,
   2563 	.network_key_len = 64,
   2564 	.network_key = "2aacd631143bbc2d83fabf3febed3c45"
   2565 		"6ea9f7539a1692ffb416c41cfee0c96f",
   2566 	.encryption_type = WSC_ENCRYPTION_TYPE_AES_TKIP,
   2567 };
   2568 
   2569 struct wsc_credential wsc_r_test_open_cred = {
   2570 	.ssid_len = 7,
   2571 	.ssid = "thessid",
   2572 	.auth_type = WSC_AUTHENTICATION_TYPE_OPEN,
   2573 	.network_key_len = 0,
   2574 	.encryption_type = WSC_ENCRYPTION_TYPE_NONE,
   2575 };
   2576 
   2577 static bool getrandom_precheck(const void *data)
   2578 {
   2579 	return l_getrandom_is_supported();
   2580 }
   2581 
   2582 static bool aes_cbc_precheck(const void *data)
   2583 {
   2584 	return (l_key_is_supported(L_KEY_FEATURE_DH) &&
   2585 		l_cipher_is_supported(L_CIPHER_AES_CBC));
   2586 }
   2587 
   2588 static bool key_crypto_precheck(const void *data)
   2589 {
   2590 	return (l_key_is_supported(L_KEY_FEATURE_CRYPTO) &&
   2591 		l_checksum_is_supported(L_CHECKSUM_SHA256, true));
   2592 }
   2593 
   2594 static bool key_dh_precheck(const void *data)
   2595 {
   2596 	return (l_key_is_supported(L_KEY_FEATURE_DH) &&
   2597 		l_key_is_supported(L_KEY_FEATURE_CRYPTO) &&
   2598 		l_checksum_is_supported(L_CHECKSUM_SHA256, true));
   2599 }
   2600 
   2601 #define add_aes_cbc_test(name, func, data) l_test_add_data_func_precheck(name, \
   2602 							data, func, \
   2603 							aes_cbc_precheck, 0)
   2604 #define add_crypto_test(name, func, data) l_test_add_data_func_precheck(name, \
   2605 							data, func, \
   2606 							key_crypto_precheck, 0)
   2607 #define add_dh_test(name, func, data) l_test_add_data_func_precheck(name, \
   2608 							data, func, \
   2609 							key_dh_precheck, 0)
   2610 
   2611 int main(int argc, char *argv[])
   2612 {
   2613 	l_test_init(&argc, &argv);
   2614 
   2615 	l_test_add("/wsc/iter/sanity-check", wsc_test_iter_sanity_check, NULL);
   2616 
   2617 	l_test_add("/wsc/parse/beacon 1", wsc_test_parse_beacon,
   2618 					&beacon_data_1);
   2619 
   2620 	l_test_add("/wsc/parse/probe response 1", wsc_test_parse_probe_response,
   2621 					&probe_response_data_1);
   2622 
   2623 	l_test_add("/wsc/parse/probe request 1", wsc_test_parse_probe_request,
   2624 					&probe_request_data_1);
   2625 
   2626 	l_test_add("/wsc/build/probe request 1", wsc_test_build_probe_request,
   2627 					&probe_request_data_1);
   2628 
   2629 	l_test_add("/wsc/pin/valid pin", wsc_test_pin_valid, NULL);
   2630 	l_test_add("/wsc/pin/valid checksum", wsc_test_pin_checksum, NULL);
   2631 
   2632 	l_test_add_func_precheck("/wsc/pin/generate", wsc_test_pin_generate,
   2633 							getrandom_precheck, 0);
   2634 
   2635 	l_test_add("/wsc/gen_uuid/1", wsc_test_uuid_from_addr,
   2636 					&uuid_from_addr_data_1);
   2637 
   2638 	l_test_add("/wsc/parse/m1 1", wsc_test_parse_m1, &m1_data_1);
   2639 	l_test_add("/wsc/parse/m1 2", wsc_test_parse_m1, &m1_data_2);
   2640 
   2641 	l_test_add("/wsc/build/m1 1", wsc_test_build_m1, &m1_data_1);
   2642 	l_test_add("/wsc/build/m1 2", wsc_test_build_m1, &m1_data_2);
   2643 
   2644 	add_dh_test("/wsc/parse/m2 1", wsc_test_parse_m2, &m2_data_1);
   2645 	add_dh_test("/wsc/parse/m2 2", wsc_test_parse_m2, &m2_data_2);
   2646 
   2647 	add_crypto_test("/wsc/build/m2 1", wsc_test_build_m2, &m2_data_1);
   2648 
   2649 	add_crypto_test("/wsc/parse/m3 1", wsc_test_parse_m3, &m3_data_1);
   2650 	add_crypto_test("/wsc/build/m3 1", wsc_test_build_m3, &m3_data_1);
   2651 
   2652 	add_crypto_test("/wsc/parse/m4 1", wsc_test_parse_m4, &m4_data_1);
   2653 	add_crypto_test("/wsc/build/m4 1", wsc_test_build_m4, &m4_data_1);
   2654 
   2655 	add_crypto_test("/wsc/parse/m4 encrypted settings 1",
   2656 					wsc_test_parse_m4_encrypted_settings,
   2657 					&m4_encrypted_settings_data_1);
   2658 	add_crypto_test("/wsc/build/m4 encrypted settings 1",
   2659 					wsc_test_build_m4_encrypted_settings,
   2660 					&m4_encrypted_settings_data_1);
   2661 
   2662 	add_crypto_test("/wsc/parse/m5 1", wsc_test_parse_m5, &m5_data_1);
   2663 	add_crypto_test("/wsc/build/m5 1", wsc_test_build_m5, &m5_data_1);
   2664 
   2665 	add_crypto_test("/wsc/parse/m6 1", wsc_test_parse_m6, &m6_data_1);
   2666 	add_crypto_test("/wsc/build/m6 1", wsc_test_build_m6, &m6_data_1);
   2667 
   2668 	add_crypto_test("/wsc/parse/m6 encrypted settings 1",
   2669 					wsc_test_parse_m6_encrypted_settings,
   2670 					&m6_encrypted_settings_data_1);
   2671 	add_crypto_test("/wsc/build/m6 encrypted settings 1",
   2672 					wsc_test_build_m6_encrypted_settings,
   2673 					&m6_encrypted_settings_data_1);
   2674 
   2675 	add_crypto_test("/wsc/parse/m7 1", wsc_test_parse_m7, &m7_data_1);
   2676 	add_crypto_test("/wsc/build/m7 1", wsc_test_build_m7, &m7_data_1);
   2677 
   2678 	add_crypto_test("/wsc/parse/m8 1", wsc_test_parse_m8, &m8_data_1);
   2679 	add_crypto_test("/wsc/build/m8 1", wsc_test_build_m8, &m8_data_1);
   2680 
   2681 	add_crypto_test("/wsc/parse/m8 encrypted settings 1",
   2682 					wsc_test_parse_m8_encrypted_settings,
   2683 					&m8_encrypted_settings_data_1);
   2684 	add_crypto_test("/wsc/build/m8 encrypted settings 1",
   2685 					wsc_test_build_m8_encrypted_settings,
   2686 					&m8_encrypted_settings_data_1);
   2687 
   2688 	add_crypto_test("/wsc/parse/wsc_done 1", wsc_test_parse_wsc_done,
   2689 							&wsc_done_data_1);
   2690 	add_crypto_test("/wsc/build/wsc_done 1", wsc_test_build_wsc_done,
   2691 							&wsc_done_data_1);
   2692 
   2693 	add_dh_test("/wsc/diffie-hellman/generate pubkey 1",
   2694 					wsc_test_dh_generate_pubkey,
   2695 					&dh_generate_pubkey_test_data_1);
   2696 	add_dh_test("/wsc/diffie-hellman/generate pubkey 2",
   2697 					wsc_test_dh_generate_pubkey,
   2698 					&dh_generate_pubkey_test_data_2);
   2699 
   2700 	add_aes_cbc_test("/wsc/handshake/PBC Handshake Test",
   2701 						wsc_test_pbc_handshake, NULL);
   2702 
   2703 	add_aes_cbc_test("/wsc/retransmission/no fragmentation",
   2704 				wsc_test_retransmission_no_fragmentation, NULL);
   2705 
   2706 	add_aes_cbc_test("/wsc-r/handshake/PBC Handshake WPA2 passphrase test",
   2707 					wsc_r_test_pbc_handshake,
   2708 					&wsc_r_test_wpa2_cred_passphrase);
   2709 	add_aes_cbc_test("/wsc-r/handshake/PBC Handshake WPA2 PSK test",
   2710 					wsc_r_test_pbc_handshake,
   2711 					&wsc_r_test_wpa2_cred_psk);
   2712 	add_aes_cbc_test("/wsc-r/handshake/PBC Handshake Open test",
   2713 					wsc_r_test_pbc_handshake,
   2714 					&wsc_r_test_open_cred);
   2715 
   2716 	return l_test_run();
   2717 }