eiwd

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

test-eap-sim.c (12300B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2017-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/eap.h"
     33 #include "src/eap-private.h"
     34 #include "src/simutil.h"
     35 
     36 static uint8_t attr_data[] = {
     37 		EAP_SIM_AT_RAND,		/* attribute type */
     38 		0x02,				/* length (4 * 2) == 8 bytes */
     39 		0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
     40 		EAP_SIM_AT_AUTN,		/* next attribute */
     41 		0x01,
     42 		0x0f, 0x0f,
     43 		EAP_SIM_AT_RES,			/* next attribute */
     44 		0x03,
     45 		0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
     46 };
     47 
     48 static void test_next_attribute(const void *data)
     49 {
     50 	struct eap_sim_tlv_iter iter;
     51 	/* basic attribute iteration */
     52 	uint8_t rand = 0, autn = 0, res = 0;
     53 
     54 	eap_sim_tlv_iter_init(&iter, attr_data, sizeof(attr_data));
     55 
     56 	while (eap_sim_tlv_iter_next(&iter)) {
     57 		switch (iter.tag) {
     58 		case EAP_SIM_AT_RAND:
     59 			rand = 1;
     60 			break;
     61 
     62 		case EAP_SIM_AT_AUTN:
     63 			autn = 1;
     64 			break;
     65 
     66 		case EAP_SIM_AT_RES:
     67 			res = 1;
     68 			break;
     69 
     70 		default:
     71 			assert(0);
     72 		}
     73 	}
     74 
     75 	assert(rand && autn && res);
     76 }
     77 
     78 static void test_add_attribute(const void *data)
     79 {
     80 	uint8_t buf[100];
     81 	char test[] = "test data";
     82 
     83 	/* test EAP_SIM_PAD_NONE */
     84 	eap_sim_add_attribute(buf, EAP_SIM_AT_RAND, EAP_SIM_PAD_NONE,
     85 			(uint8_t *)test, strlen(test));
     86 	/*
     87 	 * Attribute should look like:
     88 	 *
     89 	 * buf[0] = AT_RAND
     90 	 * buf[1] = 0x03
     91 	 * buf[2 - 10] = "test data"
     92 	 * buf[11] = 0x00	(padding)
     93 	 */
     94 
     95 	assert(buf[0] == EAP_SIM_AT_RAND);
     96 	assert(buf[1] == 3);
     97 	assert(!memcmp(buf + 2, test, 9));
     98 	assert(buf[11] == 0);
     99 
    100 	/* test EAP_SIM_PAD_ZERO */
    101 	memset(buf, 0, sizeof(buf));
    102 	eap_sim_add_attribute(buf, EAP_SIM_AT_RAND, EAP_SIM_PAD_ZERO,
    103 			(uint8_t *)test, strlen(test));
    104 	/*
    105 	 * Attribute should look like:
    106 	 *
    107 	 * buf[0] = AT_RAND
    108 	 * buf[1] = 0x04
    109 	 * buf[2-3] = 0x0000
    110 	 * buf[4-13] = "test data"
    111 	 * buf[14-16] = 0x000000
    112 	 */
    113 	assert(buf[0] == EAP_SIM_AT_RAND);
    114 	assert(buf[1] == 4);
    115 	assert(buf[2] == 0 && buf[3] == 0);
    116 	assert(!memcmp(buf + 4, test, strlen(test)));
    117 	assert(buf[14] == 0 && buf[15] == 0 && buf[16] == 0);
    118 
    119 	/* test EAP_SIM_PAD_LENGTH */
    120 	memset(buf, 0, sizeof(buf));
    121 	eap_sim_add_attribute(buf, EAP_SIM_AT_RAND, EAP_SIM_PAD_LENGTH,
    122 			(uint8_t *)test, strlen(test));
    123 	/*
    124 	 * Attribute should look like:
    125 	 *
    126 	 * buf[0] = AT_RAND
    127 	 * buf[1] = 0x04
    128 	 * buf[2-3] = 0x0009
    129 	 * buf[4-13] = "test data"
    130 	 * buf[14-16] = 0x000000
    131 	 */
    132 	assert(buf[0] == EAP_SIM_AT_RAND);
    133 	assert(buf[1] == 4);
    134 	assert(buf[2] == 0x00 && buf[3] == 0x09);
    135 	assert(!memcmp(buf + 4, test, strlen(test)));
    136 	assert(buf[14] == 0 && buf[15] == 0 && buf[16] == 0);
    137 
    138 	/* test EAP_SIM_PAD_LENGTH_BITS */
    139 	memset(buf, 0, sizeof(buf));
    140 	eap_sim_add_attribute(buf, EAP_SIM_AT_RAND, EAP_SIM_PAD_LENGTH_BITS,
    141 			(uint8_t *)test, strlen(test));
    142 	/*
    143 	 * Attribute should look like:
    144 	 *
    145 	 * buf[0] = AT_RAND
    146 	 * buf[1] = 0x04
    147 	 * buf[2-3] = 0x0048
    148 	 * buf[4-13] = "test data"
    149 	 * buf[14-16] = 0x000000
    150 	 */
    151 	assert(buf[0] == EAP_SIM_AT_RAND);
    152 	assert(buf[1] == 4);
    153 	assert(buf[2] == 0x00 && buf[3] == 0x48);
    154 	assert(!memcmp(buf + 4, test, strlen(test)));
    155 	assert(buf[14] == 0 && buf[15] == 0 && buf[16] == 0);
    156 }
    157 
    158 static uint8_t ex_pkt[] = {
    159 		0x02, 0x02, 0x00, 0x1c, 0x12, 0x0b, 0x00, 0x00, 0x0b, 0x05,
    160 		0x00, 0x00, 0xf5, 0x6d, 0x64, 0x33, 0xe6, 0x8e, 0xd2, 0x97,
    161 		0x6a, 0xc1, 0x19, 0x37, 0xfc, 0x3d, 0x11, 0x54 };
    162 
    163 static uint8_t ex_mac[] = {
    164 		0xf5, 0x6d, 0x64, 0x33, 0xe6, 0x8e, 0xd2, 0x97,
    165 		0x6a, 0xc1, 0x19, 0x37, 0xfc, 0x3d, 0x11, 0x54 };
    166 
    167 static uint8_t ex_sres[] = {
    168 		0xd1, 0xd2, 0xd3, 0xd4,
    169 		0xe1, 0xe2, 0xe3, 0xe4,
    170 		0xf1, 0xf2, 0xf3, 0xf4 };
    171 
    172 static uint8_t ex_k_aut[] = {
    173 		0x25, 0xaf, 0x19, 0x42, 0xef, 0xcb, 0xf4, 0xbc,
    174 		0x72, 0xb3, 0x94, 0x34, 0x21, 0xf2, 0xa9, 0x74 };
    175 
    176 static void test_calc_mac(const void *data)
    177 {
    178 	uint8_t pkt[100];
    179 	uint8_t pos = 0;
    180 
    181 	/* header */
    182 	memcpy(pkt, ex_pkt, 8);
    183 	pos += 8;
    184 	/* add MAC attribute */
    185 	pos += eap_sim_add_attribute(pkt + 8, EAP_SIM_AT_MAC, EAP_SIM_PAD_ZERO,
    186 			NULL, EAP_SIM_MAC_LEN);
    187 
    188 	memcpy(pkt + pos, ex_sres, 12);
    189 
    190 	eap_sim_derive_mac(EAP_TYPE_SIM, pkt, sizeof(ex_pkt) + 12, ex_k_aut,
    191 			pkt + pos - EAP_SIM_MAC_LEN);
    192 
    193 	assert(!memcmp(ex_mac, pkt + pos - EAP_SIM_MAC_LEN, EAP_SIM_MAC_LEN));
    194 	assert(!memcmp(ex_pkt, pkt, sizeof(ex_pkt)));
    195 }
    196 
    197 static uint8_t ex_mk[] = {
    198 		0xe5, 0x76, 0xd5, 0xca, 0x33, 0x2e, 0x99, 0x30, 0x01, 0x8b,
    199 		0xf1, 0xba, 0xee, 0x27, 0x63, 0xc7, 0x95, 0xb3, 0xc7, 0x12 };
    200 
    201 static uint8_t ex_keys[] = {
    202 		0x53, 0x6e, 0x5e, 0xbc, 0x44, 0x65, 0x58, 0x2a, 0xa6, 0xa8,
    203 		0xec, 0x99, 0x86, 0xeb, 0xb6, 0x20, 0x25, 0xaf, 0x19, 0x42,
    204 		0xef, 0xcb, 0xf4, 0xbc, 0x72, 0xb3, 0x94, 0x34, 0x21, 0xf2,
    205 		0xa9, 0x74, 0x39, 0xd4, 0x5a, 0xea, 0xf4, 0xe3, 0x06, 0x01,
    206 		0x98, 0x3e, 0x97, 0x2b, 0x6c, 0xfd, 0x46, 0xd1, 0xc3, 0x63,
    207 		0x77, 0x33, 0x65, 0x69, 0x0d, 0x09, 0xcd, 0x44, 0x97, 0x6b,
    208 		0x52, 0x5f, 0x47, 0xd3, 0xa6, 0x0a, 0x98, 0x5e, 0x95, 0x5c,
    209 		0x53, 0xb0, 0x90, 0xb2, 0xe4, 0xb7, 0x37, 0x19, 0x19, 0x6a,
    210 		0x40, 0x25, 0x42, 0x96, 0x8f, 0xd1, 0x4a, 0x88, 0x8f, 0x46,
    211 		0xb9, 0xa7, 0x88, 0x6e, 0x44, 0x88, 0x59, 0x49, 0xea, 0xb0,
    212 		0xff, 0xf6, 0x9d, 0x52, 0x31, 0x5c, 0x6c, 0x63, 0x4f, 0xd1,
    213 		0x4a, 0x7f, 0x0d, 0x52, 0x02, 0x3d, 0x56, 0xf7, 0x96, 0x98,
    214 		0xfa, 0x65, 0x96, 0xab, 0xee, 0xd4, 0xf9, 0x3f, 0xbb, 0x48,
    215 		0xeb, 0x53, 0x4d, 0x98, 0x54, 0x14, 0xce, 0xed, 0x0d, 0x9a,
    216 		0x8e, 0xd3, 0x3c, 0x38, 0x7c, 0x9d, 0xfd, 0xab, 0x92, 0xff,
    217 		0xbd, 0xf2, 0x40, 0xfc, 0xec, 0xf6, 0x5a, 0x2c, 0x93, 0xb9 };
    218 
    219 static void test_prng(const void *data)
    220 {
    221 	uint8_t prng_buf[160];
    222 
    223 	eap_sim_fips_prf((uint8_t *)ex_mk, 20, prng_buf, sizeof(ex_keys));
    224 
    225 	assert(!memcmp(prng_buf, (uint8_t *)ex_keys, sizeof(ex_keys)));
    226 }
    227 
    228 struct aka_prime_data {
    229 	const char *identity;
    230 	const char *network;
    231 	uint8_t ik[EAP_AKA_IK_LEN];
    232 	uint8_t ck[EAP_AKA_CK_LEN];
    233 	uint8_t autn[16];
    234 	uint8_t ik_p[EAP_AKA_IK_LEN];
    235 	uint8_t ck_p[EAP_AKA_CK_LEN];
    236 	uint8_t k_encr[EAP_SIM_K_ENCR_LEN];
    237 	uint8_t k_aut[EAP_AKA_PRIME_K_AUT_LEN];
    238 	uint8_t k_re[32];
    239 	uint8_t msk[EAP_SIM_MSK_LEN];
    240 	uint8_t emsk[EAP_SIM_EMSK_LEN];
    241 };
    242 
    243 /*
    244  * RFC 5448, Appendix C: Case 1
    245  */
    246 struct aka_prime_data test_case_1 = {
    247 	.identity = "0555444333222111",
    248 	.network = "WLAN",
    249 	.autn = { 0xbb, 0x52, 0xe9, 0x1c, 0x74, 0x7a, 0xc3, 0xab,
    250 			0x2a, 0x5c, 0x23, 0xd1, 0x5e, 0xe3, 0x51, 0xd5 },
    251 	.ik = { 0x97, 0x44, 0x87, 0x1a, 0xd3, 0x2b, 0xf9, 0xbb,
    252 			0xd1, 0xdd, 0x5c, 0xe5, 0x4e, 0x3e, 0x2e, 0x5a },
    253 	.ck = { 0x53, 0x49, 0xfb, 0xe0, 0x98, 0x64, 0x9f, 0x94,
    254 			0x8f, 0x5d, 0x2e, 0x97, 0x3a, 0x81, 0xc0, 0x0f },
    255 	.ik_p = { 0xcc, 0xfc, 0x23, 0x0c, 0xa7, 0x4f, 0xcc, 0x96,
    256 			0xc0, 0xa5, 0xd6, 0x11, 0x64, 0xf5, 0xa7, 0x6c },
    257 	.ck_p = { 0x00, 0x93, 0x96, 0x2d, 0x0d, 0xd8, 0x4a, 0xa5,
    258 			0x68, 0x4b, 0x04, 0x5c, 0x9e, 0xdf, 0xfa, 0x04 },
    259 	.k_encr = { 0x76, 0x6f, 0xa0, 0xa6, 0xc3, 0x17, 0x17, 0x4b,
    260 			0x81, 0x2d, 0x52, 0xfb, 0xcd, 0x11, 0xa1, 0x79 },
    261 	.k_aut = { 0x08, 0x42, 0xea, 0x72, 0x2f, 0xf6, 0x83, 0x5b, 0xfa, 0x20,
    262 			0x32, 0x49, 0x9f, 0xc3, 0xec, 0x23, 0xc2, 0xf0, 0xe3,
    263 			0x88, 0xb4, 0xf0, 0x75, 0x43, 0xff, 0xc6, 0x77, 0xf1,
    264 			0x69, 0x6d, 0x71, 0xea },
    265 	.k_re = { 0xcf, 0x83, 0xaa, 0x8b, 0xc7, 0xe0, 0xac, 0xed, 0x89, 0x2a,
    266 			0xcc, 0x98, 0xe7, 0x6a, 0x9b, 0x20, 0x95, 0xb5, 0x58,
    267 			0xc7, 0x79, 0x5c, 0x70, 0x94, 0x71, 0x5c, 0xb3, 0x39,
    268 			0x3a, 0xa7, 0xd1, 0x7a },
    269 	.msk = { 0x67, 0xc4, 0x2d, 0x9a, 0xa5, 0x6c, 0x1b, 0x79, 0xe2, 0x95,
    270 			0xe3, 0x45, 0x9f, 0xc3, 0xd1, 0x87, 0xd4, 0x2b, 0xe0,
    271 			0xbf, 0x81, 0x8d, 0x30, 0x70, 0xe3, 0x62, 0xc5, 0xe9,
    272 			0x67, 0xa4, 0xd5, 0x44, 0xe8, 0xec, 0xfe, 0x19, 0x35,
    273 			0x8a, 0xb3, 0x03, 0x9a, 0xff, 0x03, 0xb7, 0xc9, 0x30,
    274 			0x58, 0x8c, 0x05, 0x5b, 0xab, 0xee, 0x58, 0xa0, 0x26,
    275 			0x50, 0xb0, 0x67, 0xec, 0x4e, 0x93, 0x47, 0xc7, 0x5a },
    276 	.emsk = { 0xf8, 0x61, 0x70, 0x3c, 0xd7, 0x75, 0x59, 0x0e, 0x16, 0xc7,
    277 			0x67, 0x9e, 0xa3, 0x87, 0x4a, 0xda, 0x86, 0x63, 0x11,
    278 			0xde, 0x29, 0x07, 0x64, 0xd7, 0x60, 0xcf, 0x76, 0xdf,
    279 			0x64, 0x7e, 0xa0, 0x1c, 0x31, 0x3f, 0x69, 0x92, 0x4b,
    280 			0xdd, 0x76, 0x50, 0xca, 0x9b, 0xac, 0x14, 0x1e, 0xa0,
    281 			0x75, 0xc4, 0xef, 0x9e, 0x80, 0x29, 0xc0, 0xe2, 0x90,
    282 			0xcd, 0xba, 0xd5, 0x63, 0x8b, 0x63, 0xbc, 0x23, 0xfb }
    283 
    284 };
    285 
    286 /*
    287  * RFC 5448, Appendix C: Case 2
    288  */
    289 struct aka_prime_data test_case_2 = {
    290 	.identity = "0555444333222111",
    291 	.network = "HRPD",
    292 	.autn = { 0xbb, 0x52, 0xe9, 0x1c, 0x74, 0x7a, 0xc3, 0xab,
    293 			0x2a, 0x5c, 0x23, 0xd1, 0x5e, 0xe3, 0x51, 0xd5 },
    294 	.ik = { 0x97, 0x44, 0x87, 0x1a, 0xd3, 0x2b, 0xf9, 0xbb,
    295 			0xd1, 0xdd, 0x5c, 0xe5, 0x4e, 0x3e, 0x2e, 0x5a },
    296 	.ck = { 0x53, 0x49, 0xfb, 0xe0, 0x98, 0x64, 0x9f, 0x94,
    297 			0x8f, 0x5d, 0x2e, 0x97, 0x3a, 0x81, 0xc0, 0x0f },
    298 	.ik_p = { 0xdb, 0x94, 0xa0, 0xab, 0x55, 0x7e, 0xf6, 0xc9,
    299 			0xab, 0x48, 0x61, 0x9c, 0xa0, 0x5b, 0x9a, 0x9f },
    300 	.ck_p = { 0x38, 0x20, 0xf0, 0x27, 0x7f, 0xa5, 0xf7, 0x77,
    301 			0x32, 0xb1, 0xfb, 0x1d, 0x90, 0xc1, 0xa0, 0xda },
    302 	.k_encr = { 0x05, 0xad, 0x73, 0xac, 0x91, 0x5f, 0xce, 0x89,
    303 			0xac, 0x77, 0xe1, 0x52, 0x0d, 0x82, 0x18, 0x7b },
    304 	.k_aut = { 0x5b, 0x4a, 0xca, 0xef, 0x62, 0xc6, 0xeb, 0xb8, 0x88, 0x2b,
    305 			0x2f, 0x3d, 0x53, 0x4c, 0x4b, 0x35, 0x27, 0x73, 0x37,
    306 			0xa0, 0x01, 0x84, 0xf2, 0x0f, 0xf2, 0x5d, 0x22, 0x4c,
    307 			0x04, 0xbe, 0x2a, 0xfd },
    308 	.k_re = { 0x3f, 0x90, 0xbf, 0x5c, 0x6e, 0x5e, 0xf3, 0x25, 0xff, 0x04,
    309 			0xeb, 0x5e, 0xf6, 0x53, 0x9f, 0xa8, 0xcc, 0xa8, 0x39,
    310 			0x81, 0x94, 0xfb, 0xd0, 0x0b, 0xe4, 0x25, 0xb3, 0xf4,
    311 			0x0d, 0xba, 0x10, 0xac },
    312 	.msk = { 0x87, 0xb3, 0x21, 0x57, 0x01, 0x17, 0xcd, 0x6c, 0x95, 0xab,
    313 			0x6c, 0x43, 0x6f, 0xb5, 0x07, 0x3f, 0xf1, 0x5c, 0xf8,
    314 			0x55, 0x05, 0xd2, 0xbc, 0x5b, 0xb7, 0x35, 0x5f, 0xc2,
    315 			0x1e, 0xa8, 0xa7, 0x57, 0x57, 0xe8, 0xf8, 0x6a, 0x2b,
    316 			0x13, 0x80, 0x02, 0xe0, 0x57, 0x52, 0x91, 0x3b, 0xb4,
    317 			0x3b, 0x82, 0xf8, 0x68, 0xa9, 0x61, 0x17, 0xe9, 0x1a,
    318 			0x2d, 0x95, 0xf5, 0x26, 0x67, 0x7d, 0x57, 0x29, 0x00 },
    319 	.emsk = { 0xc8, 0x91, 0xd5, 0xf2, 0x0f, 0x14, 0x8a, 0x10, 0x07, 0x55,
    320 			0x3e, 0x2d, 0xea, 0x55, 0x5c, 0x9c, 0xb6, 0x72, 0xe9,
    321 			0x67, 0x5f, 0x4a, 0x66, 0xb4, 0xba, 0xfa, 0x02, 0x73,
    322 			0x79, 0xf9, 0x3a, 0xee, 0x53, 0x9a, 0x59, 0x79, 0xd0,
    323 			0xa0, 0x04, 0x2b, 0x9d, 0x2a, 0xe2, 0x8b, 0xed, 0x3b,
    324 			0x17, 0xa3, 0x1d, 0xc8, 0xab, 0x75, 0x07, 0x2b, 0x80,
    325 			0xbd, 0x0c, 0x1d, 0xa6, 0x12, 0x46, 0x6e, 0x40, 0x2c }
    326 
    327 };
    328 
    329 static void test_aka_prf_prime(const void *data)
    330 {
    331 	struct aka_prime_data *vals = (struct aka_prime_data *)data;
    332 	uint8_t k_encr[16];
    333 	uint8_t k_aut[32];
    334 	uint8_t k_re[32];
    335 	uint8_t msk[64];
    336 	uint8_t emsk[64];
    337 	uint8_t ik_p[16];
    338 	uint8_t ck_p[16];
    339 
    340 	eap_aka_derive_primes(vals->ck, vals->ik, vals->autn,
    341 			(const uint8_t *)vals->network, strlen(vals->network),
    342 			ck_p, ik_p);
    343 
    344 	assert(memcmp(ik_p, vals->ik_p, EAP_AKA_IK_LEN) == 0);
    345 	assert(memcmp(ck_p, vals->ck_p, EAP_AKA_CK_LEN) == 0);
    346 
    347 	eap_aka_prf_prime(ik_p, ck_p, vals->identity, k_encr, k_aut, k_re,
    348 			msk, emsk);
    349 
    350 	assert(memcmp(k_encr, vals->k_encr, EAP_SIM_K_ENCR_LEN) == 0);
    351 	assert(memcmp(k_aut, vals->k_aut, EAP_AKA_PRIME_K_AUT_LEN) == 0);
    352 	assert(memcmp(k_re, vals->k_re, EAP_AKA_K_RE_LEN) == 0);
    353 	assert(memcmp(msk, vals->msk, EAP_SIM_MSK_LEN) == 0);
    354 	assert(memcmp(emsk, vals->emsk, EAP_SIM_EMSK_LEN) == 0);
    355 }
    356 
    357 int main(int argc, char *argv[])
    358 {
    359 	l_test_init(&argc, &argv);
    360 
    361 	l_test_add("EAP-SIM next attribute test", test_next_attribute, NULL);
    362 	l_test_add("EAP-SIM add attribute test", test_add_attribute, NULL);
    363 	l_test_add("EAP-SIM calculate MAC test", test_calc_mac, NULL);
    364 	l_test_add("EAP-SIM PRNG test", test_prng, NULL);
    365 	l_test_add("EAP-AKA' Test Case 1", test_aka_prf_prime, &test_case_1);
    366 	l_test_add("EAP-AKA' Test Case 2", test_aka_prf_prime, &test_case_2);
    367 
    368 	return l_test_run();
    369 }