eiwd

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

test-crypto.c (12463B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2013-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 <ell/ell.h>
     31 
     32 #include "src/crypto.h"
     33 
     34 struct psk_data {
     35 	const char *passphrase;
     36 	const unsigned char *ssid;
     37 	size_t ssid_len;
     38 	const char *psk;
     39 };
     40 
     41 static const unsigned char psk_test_case_1_ssid[] = { 'I', 'E', 'E', 'E' };
     42 
     43 static const struct psk_data psk_test_case_1 = {
     44 	.passphrase =	"password",
     45 	.ssid =		psk_test_case_1_ssid,
     46 	.ssid_len =	sizeof(psk_test_case_1_ssid),
     47 	.psk =		"f42c6fc52df0ebef9ebb4b90b38a5f90"
     48 			"2e83fe1b135a70e23aed762e9710a12e",
     49 };
     50 
     51 static const unsigned char psk_test_case_2_ssid[] = { 'T', 'h', 'i', 's',
     52 					'I', 's', 'A', 'S', 'S', 'I', 'D' };
     53 
     54 static const struct psk_data psk_test_case_2 = {
     55 	.passphrase =	"ThisIsAPassword",
     56 	.ssid =		psk_test_case_2_ssid,
     57 	.ssid_len =	sizeof(psk_test_case_2_ssid),
     58 	.psk =		"0dc0d6eb90555ed6419756b9a15ec3e3"
     59 			"209b63df707dd508d14581f8982721af",
     60 };
     61 
     62 static const unsigned char psk_test_case_3_ssid[] = {
     63 				'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z',
     64 				'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z',
     65 				'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z',
     66 				'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z' };
     67 
     68 static const struct psk_data psk_test_case_3 = {
     69 	.passphrase =	"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
     70 	.ssid =		psk_test_case_3_ssid,
     71 	.ssid_len =	sizeof(psk_test_case_3_ssid),
     72 	.psk =		"becb93866bb8c3832cb777c2f559807c"
     73 			"8c59afcb6eae734885001300a981cc62",
     74 };
     75 
     76 static void psk_test(const void *data)
     77 {
     78 	const struct psk_data *test = data;
     79 	unsigned char output[32];
     80 	char psk[65];
     81 	unsigned int i;
     82 	int result;
     83 
     84 	printf("Passphrase  = \"%s\"\n", test->passphrase);
     85 	printf("SSID        = {");
     86 	for (i = 0; i < test->ssid_len; i++)
     87 		printf("%s'%c'", i == 0 ? " " : ", ", test->ssid[i]);
     88 	printf(" }\n");
     89 	printf("SSID Length = %zd\n", test->ssid_len);
     90 	printf("PSK         = %s\n", test->psk);
     91 
     92 	result = crypto_psk_from_passphrase(test->passphrase,
     93 						test->ssid, test->ssid_len,
     94 						output);
     95 	assert(result == 0);
     96 
     97 	for (i = 0; i < sizeof(output); i++)
     98 		sprintf(psk + (i * 2), "%02x", output[i]);
     99 
    100 	printf("Result      = %s\n", psk);
    101 
    102 	assert(strcmp(test->psk, psk) == 0);
    103 }
    104 
    105 struct ptk_data {
    106 	const unsigned char *pmk;
    107 	const unsigned char *aa;
    108 	const unsigned char *spa;
    109 	const unsigned char *snonce;
    110 	const unsigned char *anonce;
    111 	enum crypto_cipher cipher;
    112 	const unsigned char *kck;
    113 	const unsigned char *kek;
    114 	const unsigned char *tk;
    115 };
    116 
    117 static unsigned char pmk_data_1[32] = {
    118 	0x0d, 0xc0, 0xd6, 0xeb, 0x90, 0x55, 0x5e, 0xd6,
    119 	0x41, 0x97, 0x56, 0xb9, 0xa1, 0x5e, 0xc3, 0xe3,
    120 	0x20, 0x9b, 0x63, 0xdf, 0x70, 0x7d, 0xd5, 0x08,
    121 	0xd1, 0x45, 0x81, 0xf8, 0x98, 0x27, 0x21, 0xaf,
    122 };
    123 
    124 static unsigned char aa_data_1[6] = {
    125 	0xa0, 0xa1, 0xa1, 0xa3, 0xa4, 0xa5,
    126 };
    127 
    128 static unsigned char spa_data_1[6] = {
    129 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5,
    130 };
    131 
    132 static unsigned char snonce_data_1[32] = {
    133 	0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
    134 	0xc8, 0xc9, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5,
    135 	0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd,
    136 	0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5,
    137 };
    138 
    139 static unsigned char anonce_data_1[32] = {
    140 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
    141 	0xe8, 0xe9, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5,
    142 	0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
    143 	0xfe, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
    144 };
    145 
    146 static unsigned char tk_data_1[] = {
    147 	0xb2, 0x36, 0x0c, 0x79, 0xe9, 0x71, 0x0f, 0xdd,
    148 	0x58, 0xbe, 0xa9, 0x3d, 0xea, 0xf0, 0x65, 0x99,
    149 };
    150 
    151 static unsigned char kck_data_2[] = {
    152 	0x37, 0x9f, 0x98, 0x52, 0xd0, 0x19, 0x92, 0x36,
    153 	0xb9, 0x4e, 0x40, 0x7c, 0xe4, 0xc0, 0x0e, 0xc8
    154 };
    155 
    156 static unsigned char kek_data_2[] = {
    157 	0x47, 0xc9, 0xed, 0xc0, 0x1c, 0x2c, 0x6e, 0x5b,
    158 	0x49, 0x10, 0xca, 0xdd, 0xfb, 0x3e, 0x51, 0xa7,
    159 };
    160 
    161 static unsigned char tk_data_2[] = {
    162 	0xb2, 0x36, 0x0c, 0x79, 0xe9, 0x71, 0x0f, 0xdd,
    163 	0x58, 0xbe, 0xa9, 0x3d, 0xea, 0xf0, 0x65, 0x99,
    164 	0xdb, 0x98, 0x0a, 0xfb, 0xc2, 0x9c, 0x15, 0x28,
    165 	0x55, 0x74, 0x0a, 0x6c, 0xe5, 0xae, 0x38, 0x27,
    166 };
    167 
    168 static const struct ptk_data ptk_test_1 = {
    169 	.pmk = pmk_data_1,
    170 	.aa = aa_data_1,
    171 	.spa = spa_data_1,
    172 	.snonce = snonce_data_1,
    173 	.anonce = anonce_data_1,
    174 	.cipher = CRYPTO_CIPHER_CCMP,
    175 	.tk = tk_data_1,
    176 };
    177 
    178 static const struct ptk_data ptk_test_2 = {
    179 	.pmk = pmk_data_1,
    180 	.aa = aa_data_1,
    181 	.spa = spa_data_1,
    182 	.snonce = snonce_data_1,
    183 	.anonce = anonce_data_1,
    184 	.cipher = CRYPTO_CIPHER_TKIP,
    185 	.kck = kck_data_2,
    186 	.kek = kek_data_2,
    187 	.tk = tk_data_2,
    188 };
    189 
    190 static unsigned char pmk_data_3[32] = {
    191 	0x9e, 0x99, 0x88, 0xbd, 0xe2, 0xcb, 0xa7, 0x43,
    192 	0x95, 0xc0, 0x28, 0x9f, 0xfd, 0xa0, 0x7b, 0xc4,
    193 	0x1f, 0xfa, 0x88, 0x9a, 0x33, 0x09, 0x23, 0x7a,
    194 	0x22, 0x40, 0xc9, 0x34, 0xbc, 0xdc, 0x7d, 0xdb,
    195 };
    196 
    197 static unsigned char aa_data_3[6] = {
    198 	0x00, 0x0c, 0x41, 0xd2, 0x94, 0xfb,
    199 };
    200 
    201 static unsigned char spa_data_3[6] = {
    202 	0x00, 0x0d, 0x3a, 0x26, 0x10, 0xfb,
    203 };
    204 
    205 static unsigned char snonce_data_3[32] = {
    206 	0x89, 0x3e, 0xe5, 0x51, 0x21, 0x45, 0x57, 0xff,
    207 	0xf3, 0xc0, 0x76, 0xac, 0x97, 0x79, 0x15, 0xa2,
    208 	0x06, 0x07, 0x27, 0x03, 0x8e, 0x9b, 0xea, 0x9b,
    209 	0x66, 0x19, 0xa5, 0xba, 0xb4, 0x0f, 0x89, 0xc1,
    210 };
    211 
    212 static unsigned char anonce_data_3[32] = {
    213 	0xda, 0xbd, 0xc1, 0x04, 0xd4, 0x57, 0x41, 0x1a,
    214 	0xee, 0x33, 0x8c, 0x00, 0xfa, 0x8a, 0x1f, 0x32,
    215 	0xab, 0xfc, 0x6c, 0xfb, 0x79, 0x43, 0x60, 0xad,
    216 	0xce, 0x3a, 0xfb, 0x5d, 0x15, 0x9a, 0x51, 0xf6,
    217 };
    218 
    219 static unsigned char kck_data_3[] = {
    220 	0xcc, 0xbf, 0x97, 0xa8, 0x2b, 0x5c, 0x51, 0xa4,
    221 	0x43, 0x25, 0xa7, 0x7e, 0x9b, 0xc5, 0x70, 0x50,
    222 };
    223 
    224 static unsigned char kek_data_3[] = {
    225 	0xda, 0xec, 0x54, 0x38, 0x43, 0x0f, 0x00, 0xeb,
    226 	0x89, 0x3d, 0x84, 0xd8, 0xb4, 0xb4, 0xb5, 0xe8,
    227 };
    228 
    229 static unsigned char tk_data_3[] = {
    230 	0x19, 0xf4, 0xdc, 0xe0, 0xcc, 0x5f, 0x21, 0x66,
    231 	0xe9, 0x4f, 0xdb, 0x3e, 0xaf, 0x68, 0xeb, 0x76,
    232 	0x80, 0xf4, 0xe2, 0x64, 0x6e, 0x6d, 0x9e, 0x36,
    233 	0x26, 0x0d, 0x89, 0xff, 0xbf, 0x24, 0xee, 0x7e,
    234 };
    235 
    236 static const struct ptk_data ptk_test_3 = {
    237 	.pmk = pmk_data_3,
    238 	.aa = aa_data_3,
    239 	.spa = spa_data_3,
    240 	.snonce = snonce_data_3,
    241 	.anonce = anonce_data_3,
    242 	.cipher = CRYPTO_CIPHER_TKIP,
    243 	.kck = kck_data_3,
    244 	.kek = kek_data_3,
    245 	.tk = tk_data_3,
    246 };
    247 
    248 
    249 static unsigned char pmk_data_4[32] = {
    250 	0x6f, 0xe8, 0x57, 0xc0, 0xb7, 0x42, 0xdf, 0xc2,
    251 	0xda, 0x8a, 0x1f, 0xe8, 0xb1, 0xb4, 0xb4, 0x62,
    252         0x8d, 0x9f, 0xbb, 0xb0, 0x60, 0x82, 0x6b, 0x83,
    253 	0xcb, 0x43, 0xb6, 0x4b, 0x13, 0xe1, 0x03, 0xe8,
    254 };
    255 
    256 static unsigned char aa_data_4[6] = {
    257 	0x00, 0x14, 0x6c, 0x7e, 0x40, 0x80,
    258 };
    259 
    260 static unsigned char spa_data_4[6] = {
    261 	0x00, 0x13, 0x46, 0xfe, 0x32, 0x0c,
    262 };
    263 
    264 static unsigned char snonce_data_4[32] = {
    265 	0x59, 0x16, 0x8b, 0xc3, 0xa5, 0xdf, 0x18, 0xd7,
    266 	0x1e, 0xfb, 0x64, 0x23, 0xf3, 0x40, 0x08, 0x8d,
    267 	0xab, 0x9e, 0x1b, 0xa2, 0xbb, 0xc5, 0x86, 0x59,
    268 	0xe0, 0x7b, 0x37, 0x64, 0xb0, 0xde, 0x85, 0x70,
    269 };
    270 
    271 static unsigned char anonce_data_4[32] = {
    272 	0x22, 0x58, 0x54, 0xb0, 0x44, 0x4d, 0xe3, 0xaf,
    273 	0x06, 0xd1, 0x49, 0x2b, 0x85, 0x29, 0x84, 0xf0,
    274 	0x4c, 0xf6, 0x27, 0x4c, 0x0e, 0x32, 0x18, 0xb8,
    275 	0x68, 0x17, 0x56, 0x86, 0x4d, 0xb7, 0xa0, 0x55,
    276 };
    277 
    278 static unsigned char kck_data_4[] = {
    279 	0x9a, 0x75, 0xef, 0x0b, 0xde, 0x7c, 0x20, 0x9c,
    280 	0xca, 0xe1, 0x3f, 0x54, 0xb1, 0xb3, 0x3e, 0xa3,
    281 };
    282 
    283 static unsigned char kek_data_4[] = {
    284 	0xfa, 0x01, 0x66, 0x16, 0x6e, 0x30, 0x81, 0x1f,
    285 	0x1b, 0xaa, 0xff, 0x8d, 0x1c, 0x1a, 0xc1, 0x63,
    286 };
    287 
    288 static unsigned char tk_data_4[] = {
    289 	0x82, 0xa4, 0x9b, 0x50, 0xb8, 0x60, 0x88, 0xc5,
    290 	0xcb, 0x79, 0x3b, 0x54, 0x26, 0x61, 0xa2, 0x8a,
    291         0x64, 0x69, 0x3c, 0x72, 0xb8, 0x66, 0x73, 0xe0,
    292 	0xa8, 0x2d, 0x48, 0xc7, 0x47, 0x68, 0xcb, 0xf9,
    293 };
    294 
    295 static const struct ptk_data ptk_test_4 = {
    296 	.pmk = pmk_data_4,
    297 	.aa = aa_data_4,
    298 	.spa = spa_data_4,
    299 	.snonce = snonce_data_4,
    300 	.anonce = anonce_data_4,
    301 	.cipher = CRYPTO_CIPHER_TKIP,
    302 	.kck = kck_data_4,
    303 	.kek = kek_data_4,
    304 	.tk = tk_data_4,
    305 };
    306 
    307 static void ptk_test(const void *data)
    308 {
    309 	const struct ptk_data *test = data;
    310 	uint8_t *ptk;
    311 	size_t ptk_len;
    312 	bool ret;
    313 
    314 	ptk_len = 32 + crypto_cipher_key_len(test->cipher);
    315 
    316 	ptk = l_malloc(ptk_len);
    317 
    318 	ret = crypto_derive_pairwise_ptk(test->pmk, 32, test->aa,
    319 					test->spa, test->anonce, test->snonce,
    320 					ptk, ptk_len, L_CHECKSUM_SHA1);
    321 
    322 	assert(ret);
    323 
    324 	if (test->kck)
    325 		assert(!memcmp(test->kck, ptk, 16));
    326 
    327 	if (test->kek)
    328 		assert(!memcmp(test->kek, ptk + 16, 16));
    329 
    330 	if (test->tk)
    331 		assert(!memcmp(test->tk, ptk + 32,
    332 				crypto_cipher_key_len(test->cipher)));
    333 
    334 	l_free(ptk);
    335 }
    336 
    337 static void aes_wrap_test(const void *data)
    338 {
    339 	/* RFC3394 section 4.1 test vector */
    340 	static const uint8_t kek[16] = {
    341 		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    342 		0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    343 	};
    344 	static const uint8_t key_data[16] = {
    345 		0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    346 		0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
    347 	};
    348 	static const uint8_t ciphertext[24] = {
    349 		0x1f, 0xa6, 0x8b, 0x0a, 0x81, 0x12, 0xb4, 0x47,
    350 		0xae, 0xf3, 0x4b, 0xd8, 0xfb, 0x5a, 0x7b, 0x82,
    351 		0x9d, 0x3e, 0x86, 0x23, 0x71, 0xd2, 0xcf, 0xe5,
    352 	};
    353 	uint8_t buf[24];
    354 
    355 	memcpy(buf, key_data, sizeof(key_data));
    356 	assert(aes_wrap(kek, buf, sizeof(key_data), buf));
    357 	assert(!memcmp(buf, ciphertext, sizeof(ciphertext)));
    358 
    359 	buf[10] = 10;
    360 	assert(!aes_unwrap(kek, 16, buf, sizeof(ciphertext), buf));
    361 
    362 	memcpy(buf, ciphertext, sizeof(ciphertext));
    363 	assert(aes_unwrap(kek, 16, buf, sizeof(ciphertext), buf));
    364 	assert(!memcmp(buf, key_data, sizeof(key_data)));
    365 }
    366 
    367 static void aes_siv_test(const void *data)
    368 {
    369 	/* RFC5297 Appendix A.1 Test Vectors */
    370 	const uint8_t key[] = {
    371 		0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6,
    372 		0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0, 0xf0, 0xf1, 0xf2, 0xf3,
    373 		0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
    374 		0xfe, 0xff
    375 	};
    376 	const uint8_t ad[] = {
    377 		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
    378 		0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
    379 		0x24, 0x25, 0x26, 0x27
    380 	};
    381 	const uint8_t plaintext[] = {
    382 		0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa,
    383 		0xbb, 0xcc, 0xdd, 0xee
    384 	};
    385 	const uint8_t encrypted[] = {
    386 		0x85, 0x63, 0x2d, 0x07, 0xc6, 0xe8, 0xf3, 0x7f, 0x95, 0x0a,
    387 		0xcd, 0x32, 0x0a, 0x2e, 0xcc, 0x93, 0x40, 0xc0, 0x2b, 0x96,
    388 		0x90, 0xc4, 0xdc, 0x04, 0xda, 0xef, 0x7f, 0x6a, 0xfe, 0x5c
    389 	};
    390 	struct iovec iov[1];
    391 	uint8_t outbuf[sizeof(plaintext) + 16];
    392 	uint8_t decrypted[sizeof(plaintext)];
    393 
    394 	iov[0].iov_base = (void *)ad;
    395 	iov[0].iov_len = sizeof(ad);
    396 
    397 	assert(aes_siv_encrypt(key, sizeof(key), plaintext, sizeof(plaintext),
    398 				iov, 1, outbuf));
    399 	assert(memcmp(outbuf, encrypted, sizeof(outbuf)) == 0);
    400 
    401 	assert(aes_siv_decrypt(key, sizeof(key), encrypted, sizeof(encrypted), iov, 1,
    402 				decrypted));
    403 	assert(memcmp(decrypted, plaintext, sizeof(decrypted)) == 0);
    404 }
    405 
    406 static bool test_precheck(const void *data)
    407 {
    408 	return (l_cipher_is_supported(L_CIPHER_AES) &&
    409 		l_checksum_is_supported(L_CHECKSUM_SHA1, true));
    410 }
    411 
    412 #define add_test(name, func, data) l_test_add_data_func_precheck(name, data, \
    413 							func, test_precheck, 0)
    414 int main(int argc, char *argv[])
    415 {
    416 	l_test_init(&argc, &argv);
    417 
    418 	add_test("/Passphrase Generator/PSK Test Case 1",
    419 						psk_test, &psk_test_case_1);
    420 	add_test("/Passphrase Generator/PSK Test Case 2",
    421 						psk_test, &psk_test_case_2);
    422 	add_test("/Passphrase Generator/PSK Test Case 3",
    423 						psk_test, &psk_test_case_3);
    424 
    425 	add_test("/PTK Derivation/PTK Test Case 1", ptk_test, &ptk_test_1);
    426 	add_test("/PTK Derivation/PTK Test Case 2", ptk_test, &ptk_test_2);
    427 	add_test("/PTK Derivation/PTK Test Case 3", ptk_test, &ptk_test_3);
    428 	add_test("/PTK Derivation/PTK Test Case 4", ptk_test, &ptk_test_4);
    429 
    430 	add_test("/AES Key-wrap/Wrap & unwrap", aes_wrap_test, NULL);
    431 	add_test("/AES-SIV", aes_siv_test, NULL);
    432 
    433 	return l_test_run();
    434 }