test-dpp.c (20175B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2021 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 <assert.h> 28 #include <stdio.h> 29 30 #include <ell/ell.h> 31 32 #include "src/dpp-util.h" 33 #include "src/util.h" 34 #include "ell/useful.h" 35 36 struct dpp_test_info { 37 const char *uri; 38 bool expect_fail; 39 uint32_t expected_freqs[10]; 40 struct dpp_uri_info result; 41 }; 42 43 struct dpp_test_info all_values = { 44 .uri = "DPP:C:81/1,115/36;I:SN=4774LH2b4044;M:5254005828e5;V:2;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;", 45 .result = { 46 .mac = { 0x52, 0x54, 0x00, 0x58, 0x28, 0xe5 }, 47 .version = 2, 48 }, 49 .expected_freqs = { 2412, 5180, 0 } 50 }; 51 52 struct dpp_test_info no_type = { 53 .uri = "C:81/1;K:shouldnotmatter;;", 54 .expect_fail = true 55 }; 56 57 struct dpp_test_info empty = { 58 .uri = "DPP:", 59 .expect_fail = true 60 }; 61 62 struct dpp_test_info no_key = { 63 .uri = "DPP:C:81/1,115/36;I:SN=4774LH2b4044;M:5254005828e5;V:2;;", 64 .expect_fail = true 65 }; 66 67 struct dpp_test_info data_after_terminator = { 68 .uri = "DPP:K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;C:81/1;;", 69 .expect_fail = true 70 }; 71 72 struct dpp_test_info single_terminator = { 73 .uri = "DPP:K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;", 74 .expect_fail = true 75 }; 76 77 struct dpp_test_info no_terminator = { 78 .uri = "DPP:K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=", 79 .expect_fail = true 80 }; 81 82 struct dpp_test_info bad_key = { 83 .uri = "DPP:K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0;;", 84 .expect_fail = true 85 }; 86 87 struct dpp_test_info unexpected_id = { 88 .uri = "DPP:Z:somedata;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;", 89 .expect_fail = true 90 }; 91 92 struct dpp_test_info bad_channels[] = { 93 { 94 .uri = "DPP:C:;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;", 95 .expect_fail = true 96 }, 97 { 98 .uri = "DPP:C:81;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;", 99 .expect_fail = true 100 }, 101 { 102 .uri = "DPP:C:81/;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;", 103 .expect_fail = true 104 }, 105 { 106 .uri = "DPP:C:81/1,;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;", 107 .expect_fail = true 108 }, 109 { 110 .uri = "DPP:C:81/1,81/;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;", 111 .expect_fail = true 112 }, 113 { 114 .uri = "DPP:C:81/1,/;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;", 115 .expect_fail = true 116 }, 117 }; 118 119 struct dpp_test_info duplicates[] = { 120 /* Duplicate key */ 121 { 122 .uri = "DPP:K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;", 123 .expect_fail = true 124 }, 125 /* Duplicate frequencies*/ 126 { 127 .uri = "DPP:C:81/1,115/36;C:81/1,115/36;;", 128 .expect_fail = true 129 }, 130 /* Duplicate MACs*/ 131 { 132 .uri = "DPP:M:5254005828e5;M:5254005828e5;;", 133 .expect_fail = true 134 }, 135 /* Duplicate versions */ 136 { 137 .uri = "DPP:V:2;V:2;;", 138 .expect_fail = true 139 }, 140 }; 141 142 static bool verify_info(const struct dpp_uri_info *parsed, 143 const struct dpp_test_info *result) 144 { 145 const struct dpp_uri_info *expected = &result->result; 146 uint32_t i; 147 148 assert(!memcmp(parsed->mac, expected->mac, 6)); 149 assert(parsed->version == expected->version); 150 assert(parsed->boot_public); 151 152 for (i = 0; result->expected_freqs[i]; i++) 153 assert(scan_freq_set_contains(parsed->freqs, 154 result->expected_freqs[i])); 155 156 return true; 157 } 158 159 static void test_uri_parse(const void *data) 160 { 161 const struct dpp_test_info *test_info = data; 162 struct dpp_uri_info *info; 163 164 info = dpp_parse_uri(test_info->uri); 165 if (test_info->expect_fail) { 166 assert(!info); 167 return; 168 } 169 170 assert(verify_info(info, test_info)); 171 172 dpp_free_uri_info(info); 173 } 174 175 176 static void test_bad_channels(const void *data) 177 { 178 unsigned int i; 179 180 for (i = 0; i < L_ARRAY_SIZE(bad_channels); i++) 181 test_uri_parse(&bad_channels[i]); 182 } 183 184 static void test_duplicate_in_uri(const void *data) 185 { 186 unsigned int i; 187 188 for (i = 0; i < L_ARRAY_SIZE(duplicates); i++) 189 test_uri_parse(&duplicates[i]); 190 } 191 192 struct dpp_test_vector { 193 /* Initiator values */ 194 const char *i_proto_public; 195 const char *i_proto_private; 196 const char *i_boot_public; 197 const char *i_boot_private; 198 const char *i_nonce; 199 const char *i_auth; 200 const char *i_asn1; 201 202 /* Responder values */ 203 const char *r_proto_public; 204 const char *r_proto_private; 205 const char *r_boot_public; 206 const char *r_boot_private; 207 const char *r_nonce; 208 const char *r_auth; 209 const char *r_asn1; 210 211 const char *k1; 212 const char *k2; 213 const char *ke; 214 const char *mx; 215 const char *nx; 216 const char *lx; 217 }; 218 219 /* 220 * B.1 Test Vectors for DPP Authentication Using P-256 for 221 * Mutual Authentication 222 */ 223 static struct dpp_test_vector mutual_p256 = { 224 .i_proto_public = "50a532ae2a07207276418d2fa630295d45569be425aa634f02014d00a7d1f61a" 225 "e14f35a5a858bccad90d126c46594c49ef82655e78888e15a32d916ac2172491", 226 .i_proto_private = "a87de9afbb406c96e5f79a3df895ecac3ad406f95da66314c8cb3165e0c61783", 227 /* 228 * The spec uses a 31 octet Y value, a zero byte was prepended to the 229 * Y value here otherwise the point cannot be created 230 */ 231 .i_boot_public = "88b37ed91938b5197097808a6244847617892046d93b9501afd48fa0f148dfde" 232 "00f73b6991287884a9c9a33f8e0691f14d44b59811e9d8242d010270b0d33ec0", 233 .i_boot_private = "15b2a83c5a0a38b61f2aa8200ee4994b8afdc01c58507d10d0a38f7eedf051bb", 234 .i_nonce = "13f4602a16daeb69712263b9c46cba31", 235 .i_auth = "d34944bb4b1f05caebda762c6e4ae034c819ec2f62a57dcfade2473876e007b2", 236 .i_asn1 = "3039301306072a8648ce3d020106082a8648ce3d0301070322000288b37ed919" 237 "38b5197097808a6244847617892046d93b9501afd48fa0f148dfde", 238 239 .r_proto_public = "5e3fb3576884887f17c3203d8a3a6c2fac722ef0e2201b61ac73bc655c709a90" 240 "2d4b030669fb9eff8b0a79fa7c1a172ac2a92c626256963f9274dc90682c81e5", 241 .r_proto_private = "f798ed2e19286f6a6efe210b1863badb99af2a14b497634dbfd2a97394fb5aa5", 242 .r_boot_public = "09c585a91b4df9fd25a045201885c39cc5cfae397ddaeda957dec57fa0e3503f" 243 "52bf05968198a2f92883e96a386d767579883302dbf292105c90a43694c2fd5c", 244 .r_boot_private = "54ce181a98525f217216f59b245f60e9df30ac7f6b26c939418cfc3c42d1afa0", 245 .r_nonce = "3d0cfb011ca916d796f7029ff0b43393", 246 .r_auth = "a725abe6dc66ccf3aa3d6d61a19932fcbb0799ed09ff78e5bc6d4ea5ef8e8670", 247 .r_asn1 = "3039301306072a8648ce3d020106082a8648ce3d0301070322000209c585a91b" 248 "4df9fd25a045201885c39cc5cfae397ddaeda957dec57fa0e3503f", 249 250 .k1 = "3d832a02ed6d7fc1dc96d2eceab738cf01c0028eb256be33d5a21a720bfcf949", 251 .k2 = "ca08bdeeef838ddf897a5f01f20bb93dc5a895cb86788ca8c00a7664899bc310", 252 .ke = "b6db65526c9a0174c3bed56f7e614f3a656233c078693249ac3516425127e5d5", 253 .mx = "dde2878117d69745be4f916a2dd14269d783d1d788c603bb8746beabbd1dbbbc", 254 .nx = "92118478b75c21c2c59340c842b5bce560a535f60bc37a75fe390d738c58d8e8", 255 .lx = "fb737234c973cc3a36e64e5170a32f12089d198c73c2fd85a53d0b282530fd02" 256 }; 257 258 /* 259 * B.2 Test Vectors for DPP Authentication Using P-256 for 260 * Responder-only Authentication 261 */ 262 static struct dpp_test_vector responder_only_p256 = { 263 .i_proto_public = "50a532ae2a07207276418d2fa630295d45569be425aa634f02014d00a7d1f61a" 264 "e14f35a5a858bccad90d126c46594c49ef82655e78888e15a32d916ac2172491", 265 .i_nonce = "13f4602a16daeb69712263b9c46cba31", 266 .i_auth = "787d1189b526448d2901e7f6c22775ce514fce52fc886c1e924f2fbb8d97b210", 267 268 .r_proto_public = "5e3fb3576884887f17c3203d8a3a6c2fac722ef0e2201b61ac73bc655c709a90" 269 "2d4b030669fb9eff8b0a79fa7c1a172ac2a92c626256963f9274dc90682c81e5", 270 .r_proto_private = "f798ed2e19286f6a6efe210b1863badb99af2a14b497634dbfd2a97394fb5aa5", 271 .r_boot_public = "09c585a91b4df9fd25a045201885c39cc5cfae397ddaeda957dec57fa0e3503f" 272 "52bf05968198a2f92883e96a386d767579883302dbf292105c90a43694c2fd5c", 273 .r_boot_private = "54ce181a98525f217216f59b245f60e9df30ac7f6b26c939418cfc3c42d1afa0", 274 .r_nonce = "3d0cfb011ca916d796f7029ff0b43393", 275 .r_auth = "43509ef7137d8c2fbe66d802ae09dedd94d41b8cbfafb4954782014ff4a3f91c", 276 .r_asn1 = "3039301306072a8648ce3d020106082a8648ce3d0301070322000209c585a91b" 277 "4df9fd25a045201885c39cc5cfae397ddaeda957dec57fa0e3503f", 278 279 .k1 = "3d832a02ed6d7fc1dc96d2eceab738cf01c0028eb256be33d5a21a720bfcf949", 280 .k2 = "ca08bdeeef838ddf897a5f01f20bb93dc5a895cb86788ca8c00a7664899bc310", 281 .ke = "c8882a8ab30c878467822534138c704ede0ab1e873fe03b601a7908463fec87a", 282 .mx = "dde2878117d69745be4f916a2dd14269d783d1d788c603bb8746beabbd1dbbbc", 283 .nx = "92118478b75c21c2c59340c842b5bce560a535f60bc37a75fe390d738c58d8e8", 284 }; 285 286 #define HEX2BUF(s, buf, _len) { \ 287 size_t _len_out; \ 288 unsigned char *_tmp = l_util_from_hexstring(s, &_len_out); \ 289 memcpy(buf, _tmp, _len_out); \ 290 l_free(_tmp); \ 291 } 292 293 #define CHECK_FROM_STR(str, bytes, __len) \ 294 ({ \ 295 uint64_t __tmp[L_ECC_MAX_DIGITS]; \ 296 HEX2BUF(str, __tmp, __len); \ 297 assert(!memcmp(bytes, __tmp, __len)); \ 298 }) 299 300 static void test_key_derivation(const void *data) 301 { 302 const struct dpp_test_vector *vector = data; 303 304 uint64_t tmp[L_ECC_MAX_DIGITS * 2]; 305 const struct l_ecc_curve *curve = l_ecc_curve_from_ike_group(19); 306 _auto_(l_ecc_point_free) struct l_ecc_point *i_boot_public = NULL; 307 _auto_(l_ecc_scalar_free) struct l_ecc_scalar *i_boot_private = NULL; 308 _auto_(l_ecc_point_free) struct l_ecc_point *i_proto_public = NULL; 309 _auto_(l_ecc_point_free) struct l_ecc_point *r_boot_public = NULL; 310 _auto_(l_ecc_scalar_free) struct l_ecc_scalar *r_boot_private = NULL; 311 _auto_(l_ecc_scalar_free) struct l_ecc_scalar *r_proto_private = NULL; 312 _auto_(l_ecc_point_free) struct l_ecc_point *r_proto_public = NULL; 313 _auto_(l_ecc_scalar_free) struct l_ecc_scalar *m = NULL; 314 _auto_(l_ecc_scalar_free) struct l_ecc_scalar *n = NULL; 315 _auto_(l_ecc_point_free) struct l_ecc_point *l = NULL; 316 _auto_(l_ecc_point_free) struct l_ecc_point *from_asn1 = NULL; 317 uint64_t k1[L_ECC_MAX_DIGITS]; 318 uint64_t k2[L_ECC_MAX_DIGITS]; 319 uint64_t ke[L_ECC_MAX_DIGITS]; 320 uint8_t i_nonce[16]; 321 uint8_t r_nonce[16]; 322 uint64_t r_auth[L_ECC_MAX_DIGITS]; 323 uint64_t i_auth[L_ECC_MAX_DIGITS]; 324 _auto_(l_free) uint8_t *asn1 = NULL; 325 size_t asn1_len; 326 327 HEX2BUF(vector->i_proto_public, tmp, 64); 328 i_proto_public = l_ecc_point_from_data(curve, 329 L_ECC_POINT_TYPE_FULL, 330 tmp, 64); 331 assert(i_proto_public); 332 333 HEX2BUF(vector->r_boot_public, tmp, 64); 334 r_boot_public = l_ecc_point_from_data(curve, 335 L_ECC_POINT_TYPE_FULL, 336 tmp, 64); 337 assert(r_boot_public); 338 339 if (vector->i_boot_public) { 340 HEX2BUF(vector->i_boot_public, tmp, 64); 341 i_boot_public = l_ecc_point_from_data(curve, 342 L_ECC_POINT_TYPE_FULL, 343 tmp, 64); 344 assert(i_boot_public); 345 } 346 347 if (vector->i_boot_private) { 348 HEX2BUF(vector->i_boot_private, tmp, 32); 349 i_boot_private = l_ecc_scalar_new(curve, tmp, 32); 350 assert(i_boot_private); 351 } 352 353 HEX2BUF(vector->r_asn1, tmp, sizeof(tmp)); 354 asn1 = dpp_point_to_asn1(r_boot_public, &asn1_len); 355 356 from_asn1 = dpp_point_from_asn1(asn1, asn1_len); 357 358 assert(l_ecc_points_are_equal(from_asn1, r_boot_public)); 359 360 assert(asn1_len == 59); 361 assert(memcmp(tmp, asn1, asn1_len) == 0); 362 363 if (vector->i_asn1) { 364 HEX2BUF(vector->i_asn1, tmp, sizeof(tmp)); 365 l_free(asn1); 366 asn1 = dpp_point_to_asn1(i_boot_public, &asn1_len); 367 368 l_free(from_asn1); 369 from_asn1 = dpp_point_from_asn1(asn1, asn1_len); 370 371 assert(l_ecc_points_are_equal(from_asn1, i_boot_public)); 372 373 assert(asn1_len == 59); 374 assert(memcmp(tmp, asn1, asn1_len) == 0); 375 } 376 377 378 HEX2BUF(vector->r_proto_public, tmp, 64); 379 r_proto_public = l_ecc_point_from_data(curve, 380 L_ECC_POINT_TYPE_FULL, 381 tmp, 64); 382 assert(r_proto_public); 383 384 HEX2BUF(vector->r_boot_private, tmp, 32); 385 r_boot_private = l_ecc_scalar_new(curve, tmp, 32); 386 assert(r_boot_private); 387 388 HEX2BUF(vector->r_proto_private, tmp, 32); 389 r_proto_private = l_ecc_scalar_new(curve, tmp, 32); 390 assert(r_proto_private); 391 392 m = dpp_derive_k1(i_proto_public, r_boot_private, k1); 393 assert(m); 394 395 CHECK_FROM_STR(vector->k1, k1, 32); 396 l_ecc_scalar_get_data(m, tmp, sizeof(tmp)); 397 CHECK_FROM_STR(vector->mx, tmp, 32); 398 399 n = dpp_derive_k2(i_proto_public, r_proto_private, k2); 400 assert(n); 401 402 CHECK_FROM_STR(vector->k2, k2, 32); 403 l_ecc_scalar_get_data(n, tmp, sizeof(tmp)); 404 CHECK_FROM_STR(vector->nx, tmp, 32); 405 406 if (vector->lx) { 407 /* Check initiator derivation */ 408 l = dpp_derive_li(r_boot_public, r_proto_public, 409 i_boot_private); 410 assert(l); 411 l_ecc_point_get_x(l, tmp, sizeof(tmp)); 412 CHECK_FROM_STR(vector->lx, tmp, 32); 413 l_ecc_point_free(l); 414 l = NULL; 415 416 /* Check responder derivation */ 417 l = dpp_derive_lr(r_boot_private, r_proto_private, 418 i_boot_public); 419 assert(l); 420 l_ecc_point_get_x(l, tmp, sizeof(tmp)); 421 CHECK_FROM_STR(vector->lx, tmp, 32); 422 423 } 424 425 HEX2BUF(vector->i_nonce, i_nonce, 16); 426 HEX2BUF(vector->r_nonce, r_nonce, 16); 427 dpp_derive_ke(i_nonce, r_nonce, m, n, l, ke); 428 429 CHECK_FROM_STR(vector->ke, ke, 32); 430 431 dpp_derive_r_auth(i_nonce, r_nonce, 16, i_proto_public, r_proto_public, 432 i_boot_public, r_boot_public, r_auth); 433 CHECK_FROM_STR(vector->r_auth, r_auth, 32); 434 435 dpp_derive_i_auth(r_nonce, i_nonce, 16, r_proto_public, i_proto_public, 436 r_boot_public, i_boot_public, i_auth); 437 CHECK_FROM_STR(vector->i_auth, i_auth, 32); 438 } 439 440 struct dpp_pkex_test_vector { 441 uint8_t mac_i[6]; 442 uint8_t mac_r[6]; 443 const char *identifier; 444 const char *key; 445 446 const char *i_boot_public; 447 const char *i_boot_private; 448 const char *qix; 449 450 const char *r_boot_public; 451 const char *r_boot_private; 452 const char *qrx; 453 454 const char *mx; 455 const char *nx; 456 457 const char *k; 458 const char *j; 459 const char *ax; 460 const char *yx; 461 const char *xx; 462 const char *bx; 463 const char *lx; 464 465 const char *z; 466 const char *u; 467 const char *v; 468 }; 469 470 /* 471 * Appendix D PKEX Test Vector for NIST p256 472 */ 473 static struct dpp_pkex_test_vector pkex_vector = { 474 .mac_i = { 0xac, 0x64, 0x91, 0xf4, 0x52, 0x07 }, 475 .mac_r = { 0x6e, 0x5e, 0xce, 0x6e, 0xf3, 0xdd }, 476 .identifier = "joes_key", 477 .key = "thisisreallysecret", 478 479 .i_boot_public = "0ad58864754c812685ff3a52a573c1d72c72c4ebed98f3915622d4dfc84a438d" 480 "7e81429aac49ddec75ad6521db9c74074e30b5eb2ba53693c9341b79be14e101", 481 .i_boot_private = "5941b51acfc702cdc1c347264beb2920db88eb1a0bf03a211868b1632233c269", 482 .qix = "2867c4e080980dbad5099a8f821e8729679c5c714888c0bd9c7e8e4048c5fa5e", 483 484 .r_boot_public = "977b7fa39779a81429febb12e1dc5e20a7e017c4bc7437090e57c966a2b0e8a3" 485 "9d2b62733947639763f64c7b6708c1e0857becb7e24fc195248b5b06036cf792", 486 .r_boot_private = "2ae8956293f49986b6d0b8169a86805d9232babb5f6813fdfe96f19d59536c60", 487 .qrx = "134af1c41c8e7d974c647cc2bfca30b036966959f9044e90f673d756706e624c", 488 489 .mx = "bcca8e23e5c05032ae6051ca6392f7c4a4b4f9fe13e8126132d070e552848176", 490 .nx = "0a91e0728809bb8191ea36d0a1d5602bf36ab6708fbfd063e2511e533b534020", 491 492 .k = "7415e1c68611f0443cc345d136984e488c6a26d3d5482fa67e9841a03a87c78f", 493 .j = "31c1b9ab31d9c2f278b35b5c29d180dfeaf76d585ede9c0dd91cb66149db572e", 494 .ax = "0ad58864754c812685ff3a52a573c1d72c72c4ebed98f3915622d4dfc84a438d", 495 .yx = "a9972a94f143740df31c7a61124d01a4e949d0fdcede61369f4c6b097aeb18b5", 496 .xx = "740ab9f0c173507b0081b475b275de6a3060cf434b6a65f0b0144a1dbf913310", 497 .bx = "977b7fa39779a81429febb12e1dc5e20a7e017c4bc7437090e57c966a2b0e8a3", 498 .lx = "bc5f3128b0b997079a23ead63cf502ef4f7526602269620377b79bce20e03d44", 499 500 .z = "5271dee915cf7b1908747d8edb8394442411c5183ee38b79ebef399c08738e0b", 501 .u = "598c3d8dcccea2d43259068d542a907442f07e8cbcfb3fb49faac12eb2fee5b6", 502 .v = "b2833ce21ab4e42c082111a5dd232334e48019f66b2e274f521fe2f7dfa11999", 503 }; 504 505 static void test_pkex_key_derivation(const void *user_data) 506 { 507 const struct dpp_pkex_test_vector *vector = user_data; 508 const struct l_ecc_curve *curve = l_ecc_curve_from_ike_group(19); 509 uint64_t tmp[L_ECC_MAX_DIGITS * 2]; 510 _auto_(l_ecc_point_free) struct l_ecc_point *qi = NULL; 511 _auto_(l_ecc_point_free) struct l_ecc_point *qr = NULL; 512 _auto_(l_ecc_point_free) struct l_ecc_point *n = NULL; 513 _auto_(l_ecc_point_free) struct l_ecc_point *m = NULL; 514 _auto_(l_ecc_point_free) struct l_ecc_point *j = NULL; 515 _auto_(l_ecc_point_free) struct l_ecc_point *k = NULL; 516 _auto_(l_ecc_point_free) struct l_ecc_point *a = NULL; 517 _auto_(l_ecc_point_free) struct l_ecc_point *y = NULL; 518 _auto_(l_ecc_point_free) struct l_ecc_point *x = NULL; 519 _auto_(l_ecc_point_free) struct l_ecc_point *b = NULL; 520 _auto_(l_ecc_point_free) struct l_ecc_point *l = NULL; 521 size_t len; 522 523 qi = dpp_derive_qi(curve, vector->key, vector->identifier, 524 vector->mac_i); 525 l_ecc_point_get_x(qi, tmp, sizeof(tmp)); 526 CHECK_FROM_STR(vector->qix, tmp, 32); 527 528 qr = dpp_derive_qr(curve, vector->key, vector->identifier, 529 vector->mac_r); 530 l_ecc_point_get_x(qr, tmp, sizeof(tmp)); 531 CHECK_FROM_STR(vector->qrx, tmp, 32); 532 533 HEX2BUF(vector->nx, tmp, 32); 534 n = l_ecc_point_from_data(curve, L_ECC_POINT_TYPE_COMPLIANT, tmp, 32); 535 assert(n); 536 537 HEX2BUF(vector->mx, tmp, 32); 538 m = l_ecc_point_from_data(curve, L_ECC_POINT_TYPE_COMPLIANT, tmp, 32); 539 assert(m); 540 541 HEX2BUF(vector->k, tmp, 32); 542 k = l_ecc_point_from_data(curve, L_ECC_POINT_TYPE_COMPLIANT, tmp, 32); 543 assert(k); 544 545 dpp_derive_z(vector->mac_i, vector->mac_r, n, m, k, vector->key, 546 vector->identifier, tmp, &len); 547 CHECK_FROM_STR(vector->z, tmp, 32); 548 549 HEX2BUF(vector->j, tmp, 32); 550 j = l_ecc_point_from_data(curve, L_ECC_POINT_TYPE_COMPLIANT, tmp, 32); 551 assert(j); 552 553 HEX2BUF(vector->ax, tmp, 32); 554 a = l_ecc_point_from_data(curve, L_ECC_POINT_TYPE_COMPLIANT, tmp, 32); 555 assert(a); 556 557 HEX2BUF(vector->yx, tmp, 32); 558 y = l_ecc_point_from_data(curve, L_ECC_POINT_TYPE_COMPLIANT, tmp, 32); 559 assert(y); 560 561 HEX2BUF(vector->xx, tmp, 32); 562 x = l_ecc_point_from_data(curve, L_ECC_POINT_TYPE_COMPLIANT, tmp, 32); 563 assert(x); 564 565 dpp_derive_u(j, vector->mac_i, a, y, x, tmp, &len); 566 CHECK_FROM_STR(vector->u, tmp, 32); 567 568 HEX2BUF(vector->bx, tmp, 32); 569 b = l_ecc_point_from_data(curve, L_ECC_POINT_TYPE_COMPLIANT, tmp, 32); 570 assert(b); 571 572 HEX2BUF(vector->lx, tmp, 32); 573 l = l_ecc_point_from_data(curve, L_ECC_POINT_TYPE_COMPLIANT, tmp, 32); 574 assert(l); 575 576 dpp_derive_v(l, vector->mac_r, b, x, y, tmp, &len); 577 CHECK_FROM_STR(vector->v, tmp, 32); 578 } 579 580 static bool test_precheck(const void *data) 581 { 582 return (l_getrandom_is_supported() && 583 l_checksum_is_supported(L_CHECKSUM_SHA256, true)); 584 } 585 586 #define add_test(name, func, data) l_test_add_data_func_precheck(name, data, \ 587 func, test_precheck, 0) 588 589 int main(int argc, char *argv[]) 590 { 591 l_test_init(&argc, &argv); 592 593 add_test("DPP test responder-only key derivation", test_key_derivation, 594 &responder_only_p256); 595 add_test("DPP test mutual key derivation", test_key_derivation, 596 &mutual_p256); 597 add_test("DPP test PKEX key derivation", test_pkex_key_derivation, 598 &pkex_vector); 599 600 l_test_add("DPP URI parse", test_uri_parse, &all_values); 601 l_test_add("DPP URI no type", test_uri_parse, &no_type); 602 l_test_add("DPP URI empty", test_uri_parse, &empty); 603 l_test_add("DPP URI no key", test_uri_parse, &no_key); 604 l_test_add("DPP URI data after terminator", test_uri_parse, 605 &data_after_terminator); 606 l_test_add("DPP URI single terminator", test_uri_parse, 607 &single_terminator); 608 l_test_add("DPP URI no terminator", test_uri_parse, 609 &no_terminator); 610 l_test_add("DPP URI bad key", test_uri_parse, &bad_key); 611 l_test_add("DPP URI bad channels", test_bad_channels, &bad_channels); 612 l_test_add("DPP URI unexpected ID", test_uri_parse, &unexpected_id); 613 l_test_add("DPP URI duplicates", test_duplicate_in_uri, &duplicates); 614 615 return l_test_run(); 616 }