eiwd

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

test-sae.c (27269B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2018-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 <string.h>
     28 #include <alloca.h>
     29 #include <assert.h>
     30 #include <ell/ell.h>
     31 
     32 #include "src/util.h"
     33 #include "src/ie.h"
     34 #include "src/handshake.h"
     35 #include "src/mpdu.h"
     36 #include "src/sae.h"
     37 #include "src/auth-proto.h"
     38 #include "src/crypto.h"
     39 
     40 const unsigned int *__wrap_l_ecc_supported_ike_groups(void);
     41 
     42 const unsigned int *__wrap_l_ecc_supported_ike_groups(void)
     43 {
     44 	static unsigned int supported_ike_groups[2] = { 19, 0 };
     45 	return supported_ike_groups;
     46 }
     47 
     48 struct test_handshake_state {
     49 	struct handshake_state super;
     50 };
     51 
     52 struct test_data {
     53 	/* flag for testing anti clogging */
     54 	bool test_anti_clogging;
     55 	/* true if a commit message was sent and verified */
     56 	bool commit_success;
     57 	/* true if a confirm message was sent and verified */
     58 	bool confirm_success;
     59 	/* true if a rejection packet was sent */
     60 	bool tx_reject_occurred;
     61 	/* true if the tx function was called */
     62 	bool tx_auth_called;
     63 	bool tx_assoc_called;
     64 	/* copy of clogging token (if present) */
     65 	uint8_t test_clogging_token[32];
     66 	/* copy of last packet sent */
     67 	uint8_t tx_packet[512];
     68 	size_t tx_packet_len;
     69 	/* status in complete callback */
     70 	uint16_t status;
     71 
     72 	struct handshake_state *handshake;
     73 };
     74 
     75 struct authenticate_frame {
     76 	struct mmpdu_header hdr;
     77 	struct mmpdu_authentication auth;
     78 } __attribute__ ((packed));
     79 
     80 struct associate_frame {
     81 	struct mmpdu_header hdr;
     82 	struct mmpdu_association_response assoc;
     83 } __attribute__ ((packed));
     84 
     85 static uint8_t spa[] = {2, 0, 0, 0, 0, 0};
     86 static uint8_t aa[] = {2, 0, 0, 0, 0, 1};
     87 static char *passphrase = "secret123";
     88 
     89 static void test_handshake_state_free(struct handshake_state *hs)
     90 {
     91 	struct test_handshake_state *ths =
     92 			l_container_of(hs, struct test_handshake_state, super);
     93 
     94 	l_free(ths);
     95 }
     96 
     97 static struct handshake_state *test_handshake_state_new(uint32_t ifindex)
     98 {
     99 	struct test_handshake_state *ths;
    100 
    101 	ths = l_new(struct test_handshake_state, 1);
    102 
    103 	ths->super.ifindex = ifindex;
    104 	ths->super.free = test_handshake_state_free;
    105 	ths->super.refcount = 1;
    106 
    107 	return &ths->super;
    108 }
    109 
    110 static void test_tx_auth_func(const uint8_t *frame, size_t len, void *user_data)
    111 {
    112 	struct test_data *td = user_data;
    113 	uint16_t trans;
    114 
    115 	td->tx_auth_called = true;
    116 
    117 	memset(td->tx_packet, 0, sizeof(td->tx_packet));
    118 	memcpy(td->tx_packet, frame, len);
    119 	td->tx_packet_len = len;
    120 
    121 	if (len <= 6 && l_get_le16(frame + 2) != 0) {
    122 		td->tx_reject_occurred = true;
    123 		return;
    124 	}
    125 
    126 	trans = l_get_le16(frame);	/* transaction */
    127 
    128 	switch (trans) {
    129 	case 1:
    130 		assert(l_get_le16(frame + 2) == 0);	/* status */
    131 		assert(l_get_le16(frame + 4) == 19);	/* group */
    132 
    133 		if (len > 102) {
    134 			/* clogging token */
    135 			assert(len == 134);
    136 			assert(!memcmp(frame + 6, td->test_clogging_token, 32));
    137 		} else {
    138 			assert(len == 102);
    139 		}
    140 
    141 		td->commit_success = true;
    142 
    143 		return;
    144 	case 2:
    145 		assert(l_get_le16(frame + 2) == 0);
    146 		assert(len == 38);
    147 
    148 		td->confirm_success = true;
    149 
    150 		return;
    151 	}
    152 
    153 	assert(false);
    154 }
    155 
    156 static void test_tx_assoc_func(void *user_data)
    157 {
    158 	struct test_data *td = user_data;
    159 
    160 	td->tx_assoc_called = true;
    161 }
    162 
    163 static struct auth_proto *test_initialize(struct test_data *td)
    164 {
    165 	struct auth_proto *ap;
    166 	struct handshake_state *hs = test_handshake_state_new(1);
    167 
    168 	td->handshake = hs;
    169 
    170 	handshake_state_set_supplicant_address(hs, spa);
    171 	handshake_state_set_authenticator_address(hs, aa);
    172 	handshake_state_set_passphrase(hs, passphrase);
    173 
    174 	memset(td->test_clogging_token, 0xde, 32);
    175 
    176 	ap = sae_sm_new(hs, test_tx_auth_func, test_tx_assoc_func, td);
    177 
    178 	td->commit_success = false;
    179 	auth_proto_start(ap);
    180 
    181 	assert(td->commit_success == true);
    182 
    183 	return ap;
    184 }
    185 
    186 static void test_destruct(struct test_data *td)
    187 {
    188 	handshake_state_unref(td->handshake);
    189 	l_free(td);
    190 }
    191 
    192 static uint8_t aa_commit[] = {
    193 	0x13, 0x00, 0x50, 0x5b, 0xb2, 0x1f, 0xaf, 0x7d,
    194 	0xaf, 0x14, 0x7c, 0x7b, 0x19, 0xc9, 0x72, 0x82, 0xbc, 0x1a, 0xdb, 0xa1,
    195 	0xbd, 0x6e, 0x5a, 0xc7, 0x58, 0x0a, 0x65, 0x1f, 0xd2, 0xde, 0xb0, 0x66,
    196 	0xa5, 0xf9, 0x3e, 0x95, 0x4a, 0xe1, 0x83, 0xdb, 0x8a, 0xf5, 0x47, 0x8a,
    197 	0x9d, 0x88, 0x5b, 0x58, 0xb9, 0x5b, 0xfb, 0x99, 0xff, 0xbe, 0xa0, 0xe8,
    198 	0x70, 0x9d, 0x99, 0x2e, 0x8f, 0xa3, 0x53, 0x57, 0x3c, 0x49, 0x81, 0x0e,
    199 	0xbc, 0x8f, 0xbc, 0xe7, 0x77, 0x8a, 0x5b, 0xf0, 0xae, 0x4a, 0xfb, 0xcd,
    200 	0x81, 0xc0, 0x97, 0xb2, 0xf8, 0xb9, 0x12, 0xed, 0x3b, 0xd5, 0x3c, 0x5c,
    201 	0xb2, 0x3a, 0xcc, 0x22, 0xe3, 0x9e
    202 };
    203 
    204 static uint8_t aa_confirm[] = {
    205 	0x00, 0x00, 0x03, 0x0e, 0xf7, 0x5c, 0x1c, 0xab,
    206 	0x7c, 0x29, 0xa1, 0x79, 0x22, 0xe4, 0x3b, 0x64, 0xb8, 0xf0, 0x70, 0x25,
    207 	0x40, 0xcc, 0x78, 0x81, 0x27, 0x12, 0xca, 0xa9, 0xf5, 0xe5, 0x0f, 0xa7,
    208 	0x73, 0x6d
    209 };
    210 
    211 static size_t setup_auth_frame(struct authenticate_frame *frame,
    212 				const uint8_t *addr,
    213 				uint16_t trans, uint16_t status,
    214 				const uint8_t *data, size_t len)
    215 {
    216 	memset(frame, 0, sizeof(struct authenticate_frame));
    217 	memcpy(frame->hdr.address_2, addr, 6);
    218 
    219 	frame->hdr.fc.type = MPDU_TYPE_MANAGEMENT;
    220 	frame->hdr.fc.subtype = MPDU_MANAGEMENT_SUBTYPE_AUTHENTICATION;
    221 	frame->hdr.fc.order = 1;
    222 
    223 	l_put_le16(MMPDU_AUTH_ALGO_SAE, &frame->auth.algorithm);
    224 	l_put_le16(trans, &frame->auth.transaction_sequence);
    225 	l_put_le16(status, &frame->auth.status);
    226 
    227 	if (data)
    228 		memcpy(frame->auth.ies, data, len);
    229 
    230 	return sizeof(frame->hdr) + sizeof(frame->auth) + len;
    231 }
    232 
    233 static size_t setup_assoc_frame(struct associate_frame *frame, uint16_t status)
    234 {
    235 	/*
    236 	 * Only need the frame to verify with mpdu_validate and have status
    237 	 * code set.
    238 	 */
    239 	memset(frame, 0, sizeof(struct associate_frame));
    240 
    241 	frame->hdr.fc.type = MPDU_TYPE_MANAGEMENT;
    242 	frame->hdr.fc.subtype = MPDU_MANAGEMENT_SUBTYPE_ASSOCIATION_REQUEST;
    243 	frame->hdr.fc.order = 1;
    244 
    245 	l_put_le16(status, &frame->assoc.status_code);
    246 
    247 	return sizeof(frame->hdr) + sizeof(frame->assoc);
    248 }
    249 
    250 static void test_clogging(const void *arg)
    251 {
    252 	struct test_data *td = l_new(struct test_data, 1);
    253 	struct auth_proto *ap = test_initialize(td);
    254 	struct authenticate_frame *frame = alloca(
    255 					sizeof(struct authenticate_frame) + 34);
    256 	uint8_t extra[34];
    257 	size_t len;
    258 
    259 	l_put_le16(19, extra);
    260 	memcpy(extra + 2, td->test_clogging_token, 32);
    261 
    262 	len = setup_auth_frame(frame, aa, 1,
    263 				MMPDU_STATUS_CODE_ANTI_CLOGGING_TOKEN_REQ,
    264 				extra, sizeof(extra));
    265 
    266 	td->test_anti_clogging = true;
    267 	td->commit_success = false;
    268 
    269 	assert(auth_proto_rx_authenticate(ap, (uint8_t *)frame, len) ==
    270 						-EAGAIN);
    271 
    272 	assert(td->commit_success == true);
    273 
    274 	test_destruct(td);
    275 	auth_proto_free(ap);
    276 }
    277 
    278 static void test_early_confirm(const void *arg)
    279 {
    280 	struct test_data *td = l_new(struct test_data, 1);
    281 	struct auth_proto *ap = test_initialize(td);
    282 	uint8_t first_commit[102];
    283 	struct authenticate_frame *frame = alloca(
    284 					sizeof(struct authenticate_frame) + 32);
    285 	size_t len;
    286 
    287 	/* save the initial commit */
    288 	memcpy(first_commit, td->tx_packet, td->tx_packet_len);
    289 
    290 	len = setup_auth_frame(frame, aa, 2, 0, NULL, 32);
    291 	memset(frame->auth.ies, 0xfe, 32);
    292 
    293 	td->test_anti_clogging = false;
    294 
    295 	assert(auth_proto_rx_authenticate(ap, (uint8_t *)frame, len) ==
    296 						-EAGAIN);
    297 
    298 	/* verify earlier commit matched most recent */
    299 	assert(!memcmp(td->tx_packet, first_commit, td->tx_packet_len));
    300 
    301 	test_destruct(td);
    302 	auth_proto_free(ap);
    303 }
    304 
    305 static void test_reflection(const void *arg)
    306 {
    307 	struct test_data *td = l_new(struct test_data, 1);
    308 	struct auth_proto *ap = test_initialize(td);
    309 
    310 	td->tx_auth_called = false;
    311 	/* send reflect same commit */
    312 	ap->rx_authenticate(ap, td->tx_packet, td->tx_packet_len);
    313 
    314 	assert(td->tx_auth_called == false);
    315 
    316 	test_destruct(td);
    317 	auth_proto_free(ap);
    318 }
    319 
    320 static void test_malformed_commit(const void *arg)
    321 {
    322 	struct test_data *td = l_new(struct test_data, 1);
    323 	struct auth_proto *ap = test_initialize(td);
    324 	struct authenticate_frame *frame = alloca(
    325 					sizeof(struct authenticate_frame) +
    326 					sizeof(aa_commit));
    327 	size_t len;
    328 
    329 	len = setup_auth_frame(frame, aa, 1, 0, aa_commit, sizeof(aa_commit));
    330 
    331 	/* don't send entire commit */
    332 	assert(auth_proto_rx_authenticate(ap, (uint8_t *)frame, len - 20) != 0);
    333 
    334 	test_destruct(td);
    335 	auth_proto_free(ap);
    336 }
    337 
    338 static void test_malformed_confirm(const void *arg)
    339 {
    340 	struct test_data *td = l_new(struct test_data, 1);
    341 	struct auth_proto *ap = test_initialize(td);
    342 	struct authenticate_frame *frame = alloca(
    343 					sizeof(struct authenticate_frame) +
    344 					sizeof(aa_commit));
    345 	size_t len;
    346 
    347 	len = setup_auth_frame(frame, aa, 1, 0, aa_commit, sizeof(aa_commit));
    348 
    349 	assert(auth_proto_rx_authenticate(ap, (uint8_t *)frame, len) == 0);
    350 
    351 	assert(td->commit_success);
    352 
    353 	frame = alloca(sizeof(struct authenticate_frame) + sizeof(aa_confirm));
    354 	len = setup_auth_frame(frame, aa, 2, 0, aa_confirm, sizeof(aa_confirm));
    355 
    356 	/* don't send entire confirm */
    357 	assert(auth_proto_rx_authenticate(ap, (uint8_t *)frame, len - 10) != 0);
    358 
    359 	test_destruct(td);
    360 	auth_proto_free(ap);
    361 }
    362 
    363 static uint8_t aa_commit_bad_group[] = {
    364 	0xff, 0x00, 0x50, 0x5b, 0xb2, 0x1f, 0xaf, 0x7d,
    365 	0xaf, 0x14, 0x7c, 0x7b, 0x19, 0xc9, 0x72, 0x82, 0xbc, 0x1a, 0xdb, 0xa1,
    366 	0xbd, 0x6e, 0x5a, 0xc7, 0x58, 0x0a, 0x65, 0x1f, 0xd2, 0xde, 0xb0, 0x66,
    367 	0xa5, 0xf9, 0x3e, 0x95, 0x4a, 0xe1, 0x83, 0xdb, 0x8a, 0xf5, 0x47, 0x8a,
    368 	0x9d, 0x88, 0x5b, 0x58, 0xb9, 0x5b, 0xfb, 0x99, 0xff, 0xbe, 0xa0, 0xe8,
    369 	0x70, 0x9d, 0x99, 0x2e, 0x8f, 0xa3, 0x53, 0x57, 0x3c, 0x49, 0x81, 0x0e,
    370 	0xbc, 0x8f, 0xbc, 0xe7, 0x77, 0x8a, 0x5b, 0xf0, 0xae, 0x4a, 0xfb, 0xcd,
    371 	0x81, 0xc0, 0x97, 0xb2, 0xf8, 0xb9, 0x12, 0xed, 0x3b, 0xd5, 0x3c, 0x5c,
    372 	0xb2, 0x3a, 0xcc, 0x22, 0xe3, 0x9e
    373 };
    374 
    375 static void test_bad_group(const void *arg)
    376 {
    377 	struct test_data *td = l_new(struct test_data, 1);
    378 	struct auth_proto *ap = test_initialize(td);
    379 	struct authenticate_frame *frame = alloca(
    380 					sizeof(struct authenticate_frame) +
    381 					sizeof(aa_commit_bad_group));
    382 	size_t len;
    383 
    384 	len = setup_auth_frame(frame, aa, 1, 0, aa_commit_bad_group,
    385 				sizeof(aa_commit_bad_group));
    386 
    387 	assert(auth_proto_rx_authenticate(ap, (uint8_t *)frame, len) ==
    388 			-EPROTO);
    389 
    390 	test_destruct(td);
    391 	auth_proto_free(ap);
    392 }
    393 
    394 static void end_to_end_tx_func(const uint8_t *frame, size_t len, void *user_data)
    395 {
    396 	struct test_data *td = user_data;
    397 
    398 	memcpy(td->tx_packet, frame, len);
    399 	td->tx_packet_len = len;
    400 }
    401 
    402 static void test_bad_confirm(const void *arg)
    403 {
    404 	struct auth_proto *ap1;
    405 	struct auth_proto *ap2;
    406 	struct test_data *td1 = l_new(struct test_data, 1);
    407 	struct test_data *td2 = l_new(struct test_data, 1);
    408 	struct handshake_state *hs1 = test_handshake_state_new(1);
    409 	struct handshake_state *hs2 = test_handshake_state_new(2);
    410 	struct authenticate_frame *frame = alloca(
    411 				sizeof(struct authenticate_frame) + 512);
    412 	size_t frame_len;
    413 	uint8_t tmp_commit[512];
    414 	size_t tmp_commit_len;
    415 
    416 	td1->status = 0xffff;
    417 	td2->status = 0xffff;
    418 
    419 	handshake_state_set_supplicant_address(hs1, spa);
    420 	handshake_state_set_authenticator_address(hs1, aa);
    421 	handshake_state_set_passphrase(hs1, passphrase);
    422 
    423 	handshake_state_set_supplicant_address(hs2, aa);
    424 	handshake_state_set_authenticator_address(hs2, spa);
    425 	handshake_state_set_passphrase(hs2, passphrase);
    426 
    427 	ap1 = sae_sm_new(hs1, end_to_end_tx_func, test_tx_assoc_func, td1);
    428 	ap2 = sae_sm_new(hs2, end_to_end_tx_func, test_tx_assoc_func, td2);
    429 
    430 	/* both peers send out commit */
    431 	ap1->start(ap1);
    432 	ap2->start(ap2);
    433 
    434 	/* save sm1 commit, tx_packet will get overwritten with confirm */
    435 	memcpy(tmp_commit, td1->tx_packet, td1->tx_packet_len);
    436 	tmp_commit_len = td1->tx_packet_len;
    437 
    438 	/* Setup whole frame */
    439 	frame_len = setup_auth_frame(frame, aa, 1, 0, td2->tx_packet + 4,
    440 					td2->tx_packet_len - 4);
    441 
    442 	/* rx commit for both peers */
    443 	ap1->rx_authenticate(ap1, (uint8_t *) frame, frame_len);
    444 
    445 	frame_len = setup_auth_frame(frame, spa, 1, 0, tmp_commit + 4,
    446 					tmp_commit_len - 4);
    447 	ap2->rx_authenticate(ap2, (uint8_t *)frame, frame_len);
    448 	/* both peers should now have sent confirm */
    449 
    450 	/* rx confirm for both peers */
    451 	frame_len = setup_auth_frame(frame, aa, 2, 0, td2->tx_packet + 4,
    452 					td2->tx_packet_len - 4);
    453 	ap1->rx_authenticate(ap1, (uint8_t *)frame, frame_len);
    454 
    455 	/* muck with a byte in the confirm */
    456 	td1->tx_packet[10] = ~td1->tx_packet[10];
    457 	frame_len = setup_auth_frame(frame, spa, 2, 0, td1->tx_packet + 4,
    458 					td1->tx_packet_len - 4);
    459 	ap2->rx_authenticate(ap2, (uint8_t *)frame, frame_len);
    460 
    461 	assert(td1->tx_assoc_called);
    462 	assert(td2->status != 0);
    463 
    464 	handshake_state_unref(hs1);
    465 	handshake_state_unref(hs2);
    466 
    467 	ap1->free(ap1);
    468 	ap2->free(ap2);
    469 
    470 	/* sm2 gets freed by sae since it failed */
    471 	l_free(td1);
    472 	l_free(td2);
    473 }
    474 
    475 static void test_confirm_after_accept(const void *arg)
    476 {
    477 	struct auth_proto *ap1;
    478 	struct auth_proto *ap2;
    479 	struct test_data *td1 = l_new(struct test_data, 1);
    480 	struct test_data *td2 = l_new(struct test_data, 1);
    481 	struct handshake_state *hs1 = test_handshake_state_new(1);
    482 	struct handshake_state *hs2 = test_handshake_state_new(2);
    483 	struct authenticate_frame *frame = alloca(
    484 				sizeof(struct authenticate_frame) + 512);
    485 	struct associate_frame *assoc = alloca(sizeof(struct associate_frame));
    486 	size_t frame_len;
    487 	uint8_t tmp_commit[512];
    488 	size_t tmp_commit_len;
    489 
    490 	td1->status = 0xffff;
    491 	td2->status = 0xffff;
    492 
    493 	handshake_state_set_supplicant_address(hs1, spa);
    494 	handshake_state_set_authenticator_address(hs1, aa);
    495 	handshake_state_set_passphrase(hs1, passphrase);
    496 
    497 	handshake_state_set_supplicant_address(hs2, aa);
    498 	handshake_state_set_authenticator_address(hs2, spa);
    499 	handshake_state_set_passphrase(hs2, passphrase);
    500 
    501 	ap1 = sae_sm_new(hs1, end_to_end_tx_func, test_tx_assoc_func, td1);
    502 	ap2 = sae_sm_new(hs2, end_to_end_tx_func, test_tx_assoc_func, td2);
    503 
    504 	/* both peers send out commit */
    505 	auth_proto_start(ap1);
    506 	auth_proto_start(ap2);
    507 
    508 	/* save sm1 commit, tx_packet will get overwritten with confirm */
    509 	memcpy(tmp_commit, td1->tx_packet, td1->tx_packet_len);
    510 	tmp_commit_len = td1->tx_packet_len;
    511 
    512 	/* rx commit for both peers */
    513 	frame_len = setup_auth_frame(frame, aa, 1, 0, td2->tx_packet + 4,
    514 					td2->tx_packet_len - 4);
    515 	assert(auth_proto_rx_authenticate(ap1, (uint8_t *)frame,
    516 						frame_len) == 0);
    517 
    518 	frame_len = setup_auth_frame(frame, spa, 1, 0, tmp_commit + 4,
    519 					tmp_commit_len - 4);
    520 	assert(auth_proto_rx_authenticate(ap2, (uint8_t *)frame,
    521 						frame_len) == 0);
    522 	/* both peers should now have sent confirm */
    523 
    524 	/* rx confirm for one peer, sm1 should accept confirm */
    525 	frame_len = setup_auth_frame(frame, aa, 2, 0, td2->tx_packet + 4,
    526 					td2->tx_packet_len - 4);
    527 	assert(auth_proto_rx_authenticate(ap1, (uint8_t *)frame,
    528 						frame_len) == 0);
    529 
    530 	assert(td1->tx_assoc_called);
    531 
    532 	/* sm1 should respond with a confirm, and accept */
    533 	frame_len = setup_auth_frame(frame, spa, 2, 0, td1->tx_packet + 4,
    534 					td1->tx_packet_len - 4);
    535 	assert(auth_proto_rx_authenticate(ap2, (uint8_t *)frame,
    536 						frame_len) == 0);
    537 
    538 	assert(td1->tx_assoc_called);
    539 
    540 	frame_len = setup_assoc_frame(assoc, 0);
    541 
    542 	/*
    543 	 * This is just to complete the connection, SAE just verifies status
    544 	 * so the same frame can be used for both SMs
    545 	 */
    546 	assert(auth_proto_rx_associate(ap1, (uint8_t *)assoc, frame_len) == 0);
    547 	assert(auth_proto_rx_associate(ap2, (uint8_t *)assoc, frame_len) == 0);
    548 
    549 	handshake_state_unref(hs1);
    550 	handshake_state_unref(hs2);
    551 
    552 	auth_proto_free(ap1);
    553 	auth_proto_free(ap2);
    554 
    555 	l_free(td1);
    556 	l_free(td2);
    557 }
    558 
    559 static void test_end_to_end(const void *arg)
    560 {
    561 	struct auth_proto *ap1;
    562 	struct auth_proto *ap2;
    563 	struct test_data *td1 = l_new(struct test_data, 1);
    564 	struct test_data *td2 = l_new(struct test_data, 1);
    565 	struct handshake_state *hs1 = test_handshake_state_new(1);
    566 	struct handshake_state *hs2 = test_handshake_state_new(2);
    567 	struct authenticate_frame *frame = alloca(
    568 				sizeof(struct authenticate_frame) + 512);
    569 	struct associate_frame *assoc = alloca(sizeof(struct associate_frame));
    570 	size_t frame_len;
    571 	uint8_t tmp_commit[512];
    572 	size_t tmp_commit_len;
    573 
    574 	td1->status = 0xffff;
    575 	td2->status = 0xffff;
    576 
    577 	handshake_state_set_supplicant_address(hs1, spa);
    578 	handshake_state_set_authenticator_address(hs1, aa);
    579 	handshake_state_set_passphrase(hs1, passphrase);
    580 
    581 	handshake_state_set_supplicant_address(hs2, aa);
    582 	handshake_state_set_authenticator_address(hs2, spa);
    583 	handshake_state_set_passphrase(hs2, passphrase);
    584 
    585 	ap1 = sae_sm_new(hs1, end_to_end_tx_func, test_tx_assoc_func, td1);
    586 	ap2 = sae_sm_new(hs2, end_to_end_tx_func, test_tx_assoc_func, td2);
    587 
    588 	/* both peers send out commit */
    589 	auth_proto_start(ap1);
    590 	auth_proto_start(ap2);
    591 
    592 	/* save sm1 commit, tx_packet will get overwritten with confirm */
    593 	memcpy(tmp_commit, td1->tx_packet, td1->tx_packet_len);
    594 	tmp_commit_len = td1->tx_packet_len;
    595 
    596 	/* rx commit for both peers */
    597 	frame_len = setup_auth_frame(frame, aa, 1, 0, td2->tx_packet + 4,
    598 					td2->tx_packet_len - 4);
    599 	assert(auth_proto_rx_authenticate(ap1, (uint8_t *)frame,
    600 						frame_len) == 0);
    601 
    602 	/* both peers should now have sent confirm */
    603 	frame_len = setup_auth_frame(frame, spa, 1, 0, tmp_commit + 4,
    604 					tmp_commit_len - 4);
    605 	assert(auth_proto_rx_authenticate(ap2, (uint8_t *)frame,
    606 						frame_len) == 0);
    607 
    608 	/* rx confirm for both peers */
    609 	frame_len = setup_auth_frame(frame, aa, 2, 0, td2->tx_packet + 4,
    610 					td2->tx_packet_len - 4);
    611 	assert(auth_proto_rx_authenticate(ap1, (uint8_t *)frame,
    612 						frame_len) == 0);
    613 
    614 	frame_len = setup_auth_frame(frame, spa, 2, 0, td1->tx_packet + 4,
    615 					td1->tx_packet_len - 4);
    616 	assert(auth_proto_rx_authenticate(ap2, (uint8_t *)frame,
    617 						frame_len) == 0);
    618 
    619 	assert(td1->tx_assoc_called);
    620 	assert(td2->tx_assoc_called);
    621 
    622 	frame_len = setup_assoc_frame(assoc, 0);
    623 	assert(auth_proto_rx_associate(ap1, (uint8_t *)assoc, frame_len) == 0);
    624 	assert(auth_proto_rx_associate(ap2, (uint8_t *)assoc, frame_len) == 0);
    625 
    626 	handshake_state_unref(hs1);
    627 	handshake_state_unref(hs2);
    628 
    629 	auth_proto_free(ap1);
    630 	auth_proto_free(ap2);
    631 
    632 	l_free(td1);
    633 	l_free(td2);
    634 }
    635 
    636 static void test_pt_pwe(const void *data)
    637 {
    638 	static const char *ssid = "byteme";
    639 	static const char *identifier = "psk4internet";
    640 	static const char *password = "mekmitasdigoat";
    641 	static const uint8_t mac1[] = { 0x00, 0x09, 0x5b, 0x66, 0xec, 0x1e };
    642 	static const uint8_t mac2[] = { 0x00, 0x0b, 0x6b, 0xd9, 0x02, 0x46 };
    643 	static const uint8_t prk[] = {
    644 		0x3b, 0xd5, 0x3f, 0xe9, 0x22, 0x3d, 0xc0, 0x28,
    645 		0x0f, 0xbf, 0xce, 0x17, 0xd7, 0xa3, 0x56, 0x40,
    646 		0x64, 0xe2, 0x0f, 0x48, 0xc6, 0xec, 0x72, 0x24,
    647 		0x6c, 0xe3, 0x67, 0xb5, 0x56, 0x9a, 0x22, 0xaf,
    648 	};
    649 	static const uint8_t okm1[] = {
    650 		0xa5, 0x04, 0x44, 0x69, 0xab, 0x16, 0xf2, 0x5b,
    651 		0x6a, 0xbf, 0x1e, 0x0e, 0x37, 0xa3, 0x6b, 0x56,
    652 		0xf5, 0x0b, 0xe7, 0x33, 0x69, 0x05, 0x3d, 0xf8,
    653 		0xdb, 0x87, 0x98, 0x9a, 0x6b, 0x66, 0xfd, 0x1a,
    654 		0x49, 0x1f, 0x1c, 0xda, 0xcb, 0xd0, 0x79, 0x31,
    655 		0x62, 0x0f, 0x83, 0x00, 0x8f, 0xfc, 0x0e, 0xcc,
    656 	};
    657 	static const uint8_t u1_data[] = {
    658 		0xdc, 0x94, 0x1b, 0xc3, 0xc6, 0xa2, 0xb4, 0x94,
    659 		0x8b, 0x6c, 0x61, 0xd5, 0x55, 0x90, 0xec, 0xb1,
    660 		0xf0, 0xc5, 0x1c, 0x4b, 0x1b, 0xeb, 0xaf, 0xf6,
    661 		0x77, 0xe5, 0x93, 0x69, 0x8d, 0x5a, 0x53, 0xc6,
    662 	};
    663 	static const uint8_t okm2[] = {
    664 		0x9b, 0x4e, 0x0d, 0x5b, 0x18, 0x79, 0xf2, 0x53,
    665 		0xc5, 0x31, 0x96, 0x15, 0x09, 0x9b, 0x05, 0xae,
    666 		0xc5, 0xb0, 0x6f, 0xa5, 0xe7, 0x88, 0xbc, 0xfd,
    667 		0x1e, 0x9e, 0xa6, 0x0d, 0x33, 0x43, 0x69, 0x27,
    668 		0x19, 0x08, 0x14, 0xc3, 0x22, 0xa6, 0x25, 0x85,
    669 		0xc9, 0x3c, 0x57, 0x7b, 0xba, 0xa3, 0xd3, 0x07,
    670 	};
    671 	static const uint8_t u2_data[] = {
    672 		0x1b, 0x83, 0x75, 0xa5, 0x18, 0xbc, 0x21, 0x39,
    673 		0x6a, 0xd6, 0xa6, 0x5e, 0x55, 0x97, 0xe0, 0xbf,
    674 		0x80, 0xd7, 0x93, 0xb6, 0xd6, 0x6e, 0x25, 0x34,
    675 		0xa6, 0xe7, 0xdf, 0xe3, 0xee, 0x22, 0x61, 0x6f,
    676 	};
    677 	static const uint8_t p1x_data[] = {
    678 		0xa0, 0x7c, 0x26, 0x07, 0x64, 0xa1, 0x34, 0x45,
    679 		0xff, 0x8c, 0xd9, 0x7c, 0x5a, 0xcc, 0x64, 0x4e,
    680 		0x71, 0x19, 0xbd, 0xe5, 0x1b, 0xad, 0x42, 0x58,
    681 		0x3e, 0xed, 0x6f, 0x41, 0x09, 0x63, 0x9e, 0x6b,
    682 	};
    683 	static const uint8_t p1y_data[] = {
    684 		0x3b, 0xdc, 0x8d, 0xf0, 0xd3, 0x23, 0x37, 0x93,
    685 		0x6c, 0x74, 0xdf, 0x60, 0x49, 0x33, 0xa4, 0x54,
    686 		0x14, 0x22, 0x51, 0xc5, 0x3c, 0x57, 0x6c, 0x03,
    687 		0x51, 0xb2, 0x8d, 0xea, 0xf9, 0x42, 0x8d, 0x7e,
    688 	};
    689 	static const uint8_t p2x_data[] = {
    690 		0x72, 0xcd, 0x2a, 0x96, 0x7a, 0x83, 0x7f, 0xea,
    691 		0x50, 0x51, 0xf0, 0x13, 0x3d, 0xb4, 0x62, 0x27,
    692 		0x77, 0x5b, 0xa0, 0x9f, 0x7b, 0x6d, 0xfb, 0x99,
    693 		0xae, 0x7a, 0x8e, 0xf2, 0x2c, 0x7d, 0x34, 0xa0,
    694 	};
    695 	static const uint8_t p2y_data[] = {
    696 		0x86, 0x43, 0x90, 0xd7, 0x97, 0xd3, 0x52, 0xb3,
    697 		0x68, 0xd3, 0x11, 0xaf, 0x51, 0x5b, 0xde, 0x11,
    698 		0x6f, 0xe5, 0x44, 0x59, 0xfe, 0xc8, 0x67, 0xee,
    699 		0x18, 0xa8, 0xa1, 0x61, 0x9c, 0xa3, 0xff, 0x59,
    700 	};
    701 	static const uint8_t ptx_data[] = {
    702 		0xb6, 0xe3, 0x8c, 0x98, 0x75, 0x0c, 0x68, 0x4b,
    703 		0x5d, 0x17, 0xc3, 0xd8, 0xc9, 0xa4, 0x10, 0x0b,
    704 		0x39, 0x93, 0x12, 0x79, 0x18, 0x7c, 0xa6, 0xcc,
    705 		0xed, 0x5f, 0x37, 0xef, 0x46, 0xdd, 0xfa, 0x97,
    706 	};
    707 	static const uint8_t pty_data[] = {
    708 		0x56, 0x87, 0xe9, 0x72, 0xe5, 0x0f, 0x73, 0xe3,
    709 		0x89, 0x88, 0x61, 0xe7, 0xed, 0xad, 0x21, 0xbe,
    710 		0xa7, 0xd5, 0xf6, 0x22, 0xdf, 0x88, 0x24, 0x3b,
    711 		0xb8, 0x04, 0x92, 0x0a, 0xe8, 0xe6, 0x47, 0xfa,
    712 	};
    713 	static const uint8_t val_data[] = {
    714 		0xbb, 0x7f, 0x9c, 0xac, 0x5a, 0xa8, 0xb7, 0x2c,
    715 		0x02, 0xb5, 0xda, 0xac, 0xc2, 0x77, 0x1a, 0xbe,
    716 		0x74, 0xe7, 0x26, 0x04, 0x61, 0x22, 0x95, 0xec,
    717 		0xa2, 0xce, 0x18, 0x36, 0x3a, 0xe9, 0xa9, 0x27,
    718 	};
    719 	static const uint8_t pwex_data[] = {
    720 		0xc9, 0x30, 0x49, 0xb9, 0xe6, 0x40, 0x00, 0xf8,
    721 		0x48, 0x20, 0x16, 0x49, 0xe9, 0x99, 0xf2, 0xb5,
    722 		0xc2, 0x2d, 0xea, 0x69, 0xb5, 0x63, 0x2c, 0x9d,
    723 		0xf4, 0xd6, 0x33, 0xb8, 0xaa, 0x1f, 0x6c, 0x1e,
    724 	};
    725 	static const uint8_t pwey_data[] = {
    726 		0x73, 0x63, 0x4e, 0x94, 0xb5, 0x3d, 0x82, 0xe7,
    727 		0x38, 0x3a, 0x8d, 0x25, 0x81, 0x99, 0xd9, 0xdc,
    728 		0x1a, 0x5e, 0xe8, 0x26, 0x9d, 0x06, 0x03, 0x82,
    729 		0xcc, 0xbf, 0x33, 0xe6, 0x14, 0xff, 0x59, 0xa0,
    730 	};
    731 
    732 	const struct l_ecc_curve *curve;
    733 	struct l_ecc_scalar *u1;
    734 	struct l_ecc_scalar *u2;
    735 	struct l_ecc_scalar *val;
    736 	struct l_ecc_point *p1;
    737 	struct l_ecc_point *p2;
    738 	struct l_ecc_point *pt;
    739 	struct l_ecc_point *pwe;
    740 	enum l_checksum_type hash;
    741 	size_t hash_len;
    742 	uint8_t pwd_seed[64]; /* SHA512 is the biggest possible right now */
    743 	uint8_t pwd_value[128];
    744 	size_t pwd_value_len;
    745 	uint8_t ubuf[64];
    746 	uint64_t num[L_ECC_MAX_DIGITS];
    747 	uint8_t zero[64] = { 0 };
    748 	uint8_t val_buf[32];
    749 	uint8_t sorted_macs[12];
    750 
    751 	curve = l_ecc_curve_from_ike_group(19);
    752 	assert(curve);
    753 
    754 	hash = crypto_sae_hash_from_ecc_prime_len(CRYPTO_SAE_HASH_TO_ELEMENT,
    755 					l_ecc_curve_get_scalar_bytes(curve));
    756 	hash_len = l_checksum_digest_length(hash);
    757 
    758 	assert(hash_len == 32);
    759 
    760 	/* pwd-seed = HKDF-Extract(ssid, password [|| identifier]) */
    761 	assert(hkdf_extract(hash, ssid, strlen(ssid), 2, pwd_seed,
    762 				password, strlen(password),
    763 				identifier, strlen(identifier)));
    764 	assert(!memcmp(pwd_seed, prk, sizeof(prk)));
    765 
    766 	/* len = olen(p) + floor(olen(p)/2) */
    767 	pwd_value_len = l_ecc_curve_get_scalar_bytes(curve);
    768 	pwd_value_len += pwd_value_len / 2;
    769 
    770 	/*
    771 	 * pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u1 P1", len)
    772 	 */
    773 	assert(hkdf_expand(hash, pwd_seed, hash_len,
    774 				"SAE Hash to Element u1 P1",
    775 				pwd_value, pwd_value_len));
    776 	assert(!memcmp(pwd_value, okm1, sizeof(okm1)));
    777 
    778 	u1 = l_ecc_scalar_new_modp(curve, pwd_value, pwd_value_len);
    779 	assert(u1);
    780 
    781 	assert(l_ecc_scalar_get_data(u1, ubuf, sizeof(ubuf)) ==
    782 			(ssize_t) sizeof(u1_data));
    783 	assert(!memcmp(ubuf, u1_data, sizeof(u1_data)));
    784 
    785 	/*
    786 	 * pwd-value = HKDF-Expand(pwd-seed, "SAE Hash to Element u2 P2", len)
    787 	 */
    788 	assert(hkdf_expand(hash, pwd_seed, hash_len,
    789 				"SAE Hash to Element u2 P2",
    790 				pwd_value, pwd_value_len));
    791 	assert(!memcmp(pwd_value, okm2, sizeof(okm2)));
    792 
    793 	u2 = l_ecc_scalar_new_modp(curve, pwd_value, pwd_value_len);
    794 	assert(u2);
    795 
    796 	assert(l_ecc_scalar_get_data(u2, ubuf, sizeof(ubuf)) ==
    797 			(ssize_t) sizeof(u2_data));
    798 	assert(!memcmp(ubuf, u2_data, sizeof(u2_data)));
    799 
    800 	p1 = l_ecc_point_from_sswu(u1);
    801 	assert(p1);
    802 
    803 	assert(l_ecc_point_get_x(p1, num, sizeof(num)) > 0);
    804 	assert(!memcmp(num, p1x_data, sizeof(p1x_data)));
    805 
    806 	assert(l_ecc_point_get_y(p1, num, sizeof(num)) > 0);
    807 	assert(!memcmp(num, p1y_data, sizeof(p1y_data)));
    808 
    809 	p2 = l_ecc_point_from_sswu(u2);
    810 	assert(p2);
    811 
    812 	assert(l_ecc_point_get_x(p2, num, sizeof(num)) > 0);
    813 	assert(!memcmp(num, p2x_data, sizeof(p2x_data)));
    814 
    815 	assert(l_ecc_point_get_y(p2, num, sizeof(num)) > 0);
    816 	assert(!memcmp(num, p2y_data, sizeof(p2y_data)));
    817 
    818 	pt = l_ecc_point_new(curve);
    819 	assert(pt);
    820 	assert(l_ecc_point_add(pt, p1, p2));
    821 
    822 	assert(l_ecc_point_get_x(pt, num, sizeof(num)) > 0);
    823 	assert(!memcmp(num, ptx_data, sizeof(ptx_data)));
    824 
    825 	assert(l_ecc_point_get_y(pt, num, sizeof(num)) > 0);
    826 	assert(!memcmp(num, pty_data, sizeof(pty_data)));
    827 
    828 	l_ecc_point_free(p1);
    829 	l_ecc_point_free(p2);
    830 	l_ecc_scalar_free(u1);
    831 	l_ecc_scalar_free(u2);
    832 
    833 	p1 = crypto_derive_sae_pt_ecc(19, ssid, password, identifier);
    834 	assert(p1);
    835 	assert(l_ecc_points_are_equal(p1, pt));
    836 	l_ecc_point_free(p1);
    837 
    838 	if (memcmp(mac1, mac2, 6) > 0) {
    839 		memcpy(sorted_macs, mac1, 6);
    840 		memcpy(sorted_macs + 6, mac2, 6);
    841 	} else {
    842 		memcpy(sorted_macs, mac2, 6);
    843 		memcpy(sorted_macs + 6, mac1, 6);
    844 	}
    845 
    846 	assert(hkdf_extract(hash, zero, hash_len, 1, val_buf,
    847 				sorted_macs, sizeof(sorted_macs)));
    848 	val = l_ecc_scalar_new_reduced_1_to_n(curve, val_buf, sizeof(val_buf));
    849 	assert(val);
    850 	assert(l_ecc_scalar_get_data(val, ubuf, sizeof(ubuf)) ==
    851 			(ssize_t) sizeof(val_data));
    852 	assert(!memcmp(ubuf, val_data, sizeof(val_data)));
    853 
    854 	pwe = l_ecc_point_new(curve);
    855 	assert(pwe);
    856 	assert(l_ecc_point_multiply(pwe, val, pt));
    857 
    858 	assert(l_ecc_point_get_x(pwe, num, sizeof(num)) > 0);
    859 	assert(!memcmp(num, pwex_data, sizeof(pwex_data)));
    860 
    861 	assert(l_ecc_point_get_y(pwe, num, sizeof(num)) > 0);
    862 	assert(!memcmp(num, pwey_data, sizeof(pwey_data)));
    863 
    864 	l_ecc_scalar_free(val);
    865 
    866 	p1 = crypto_derive_sae_pwe_from_pt_ecc(mac1, mac2, pt);
    867 	assert(p1);
    868 	assert(l_ecc_points_are_equal(p1, pwe));
    869 	l_ecc_point_free(p1);
    870 
    871 	l_ecc_point_free(pwe);
    872 	l_ecc_point_free(pt);
    873 }
    874 
    875 static bool test_precheck(const void *data)
    876 {
    877 	return (l_getrandom_is_supported() &&
    878 		l_checksum_is_supported(L_CHECKSUM_SHA256, true));
    879 }
    880 
    881 #define add_test(name, func) l_test_add_func_precheck(name, func, \
    882 							test_precheck, 0)
    883 
    884 int main(int argc, char *argv[])
    885 {
    886 	l_test_init(&argc, &argv);
    887 
    888 	add_test("SAE anti-clogging", test_clogging);
    889 	add_test("SAE early confirm", test_early_confirm);
    890 	add_test("SAE reflection", test_reflection);
    891 	add_test("SAE malformed commit", test_malformed_commit);
    892 	add_test("SAE malformed confirm", test_malformed_confirm);
    893 	add_test("SAE bad group", test_bad_group);
    894 	add_test("SAE bad confirm", test_bad_confirm);
    895 	add_test("SAE confirm after accept", test_confirm_after_accept);
    896 	add_test("SAE end-to-end", test_end_to_end);
    897 	add_test("SAE pt-pwe", test_pt_pwe);
    898 
    899 	return l_test_run();
    900 }