dpp-util.c (35816B)
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 <stdio.h> 28 29 #include <errno.h> 30 31 #include <ell/ell.h> 32 33 #include "src/dpp-util.h" 34 #include "src/util.h" 35 #include "src/band.h" 36 #include "src/crypto.h" 37 #include "src/json.h" 38 #include "ell/useful.h" 39 #include "ell/asn1-private.h" 40 #include "src/ie.h" 41 42 /* WFA Easy Connect v3.0 C.1 Role-specific Elements for NIST p256 */ 43 static const uint8_t dpp_pkex_initiator_p256[64] = { 44 /* X */ 45 0x56, 0x26, 0x12, 0xcf, 0x36, 0x48, 0xfe, 0x0b, 46 0x07, 0x04, 0xbb, 0x12, 0x22, 0x50, 0xb2, 0x54, 47 0xb1, 0x94, 0x64, 0x7e, 0x54, 0xce, 0x08, 0x07, 48 0x2e, 0xec, 0xca, 0x74, 0x5b, 0x61, 0x2d, 0x25, 49 /* Y */ 50 0x3e, 0x44, 0xc7, 0xc9, 0x8c, 0x1c, 0xa1, 0x0b, 51 0x20, 0x09, 0x93, 0xb2, 0xfd, 0xe5, 0x69, 0xdc, 52 0x75, 0xbc, 0xad, 0x33, 0xc1, 0xe7, 0xc6, 0x45, 53 0x4d, 0x10, 0x1e, 0x6a, 0x3d, 0x84, 0x3c, 0xa4 54 }; 55 56 static const uint8_t dpp_pkex_responder_p256[64] = { 57 /* X */ 58 0x1e, 0xa4, 0x8a, 0xb1, 0xa4, 0xe8, 0x42, 0x39, 59 0xad, 0x73, 0x07, 0xf2, 0x34, 0xdf, 0x57, 0x4f, 60 0xc0, 0x9d, 0x54, 0xbe, 0x36, 0x1b, 0x31, 0x0f, 61 0x59, 0x91, 0x52, 0x33, 0xac, 0x19, 0x9d, 0x76, 62 /* Y */ 63 0xd9, 0xfb, 0xf6, 0xb9, 0xf5, 0xfa, 0xdf, 0x19, 64 0x58, 0xd8, 0x3e, 0xc9, 0x89, 0x7a, 0x35, 0xc1, 65 0xbd, 0xe9, 0x0b, 0x77, 0x7a, 0xcb, 0x91, 0x2a, 66 0xe8, 0x21, 0x3f, 0x47, 0x52, 0x02, 0x4d, 0x67 67 }; 68 69 static void append_freqs(struct l_string *uri, 70 const uint32_t *freqs, size_t len) 71 { 72 size_t i; 73 enum band_freq band; 74 75 l_string_append_printf(uri, "C:"); 76 77 for (i = 0; i < len; i++) { 78 uint8_t oper_class; 79 uint8_t channel = band_freq_to_channel(freqs[i], &band); 80 81 /* For now use global operating classes */ 82 if (band == BAND_FREQ_2_4_GHZ) 83 oper_class = 81; 84 else 85 oper_class = 115; 86 87 l_string_append_printf(uri, "%u/%u", oper_class, channel); 88 89 if (i != len - 1) 90 l_string_append_c(uri, ','); 91 } 92 93 l_string_append_c(uri, ';'); 94 } 95 96 char *dpp_generate_uri(const uint8_t *asn1, size_t asn1_len, uint8_t version, 97 const uint8_t *mac, const uint32_t *freqs, 98 size_t freqs_len, const char *info, const char *host) 99 { 100 struct l_string *uri = l_string_new(256); 101 char *base64; 102 103 base64 = l_base64_encode(asn1, asn1_len, 0); 104 105 l_string_append_printf(uri, "DPP:K:%s;", base64); 106 l_free(base64); 107 108 if (mac) 109 l_string_append_printf(uri, "M:%02x%02x%02x%02x%02x%02x;", 110 MAC_STR(mac)); 111 112 if (freqs) 113 append_freqs(uri, freqs, freqs_len); 114 115 if (info) 116 l_string_append_printf(uri, "I:%s;", info); 117 118 if (host) 119 l_string_append_printf(uri, "H:%s;", host); 120 121 if (version) 122 l_string_append_printf(uri, "V:%u;", version); 123 124 l_string_append_c(uri, ';'); 125 126 return l_string_unwrap(uri); 127 } 128 129 static uint32_t dpp_parse_akm(char *akms) 130 { 131 _auto_(l_strv_free) char **split = l_strsplit(akms, '+'); 132 char **i = split; 133 uint32_t akm_out = 0; 134 135 while (*i) { 136 if (!strncmp(*i, "psk", 3)) 137 akm_out |= IE_RSN_AKM_SUITE_PSK; 138 else if (!strncmp(*i, "sae", 3)) 139 akm_out |= IE_RSN_AKM_SUITE_SAE_SHA256; 140 141 i++; 142 } 143 144 return akm_out; 145 } 146 147 static bool dpp_parse_extra_options(struct dpp_configuration *config, 148 struct json_iter *extra) 149 { 150 struct json_iter host_val; 151 struct json_iter hidden_val; 152 bool hostname = false; 153 bool hidden = false; 154 155 if (!json_iter_parse(extra, 156 JSON_OPTIONAL("send_hostname", JSON_PRIMITIVE, 157 &host_val), 158 JSON_OPTIONAL("hidden", JSON_PRIMITIVE, &hidden_val), 159 JSON_UNDEFINED)) 160 return false; 161 162 /* 163 * The values are optional in order to support backwards compatibility 164 * if more are added, but if the key does exist require the type 165 * matches and fail otherwise. 166 */ 167 if (json_iter_is_valid(&host_val) && 168 !json_iter_get_boolean(&host_val, &hostname)) 169 return false; 170 171 if (json_iter_is_valid(&hidden_val) && 172 !json_iter_get_boolean(&hidden_val, &hidden)) 173 return false; 174 175 config->send_hostname = hostname; 176 config->hidden = hidden; 177 178 return true; 179 } 180 181 /* 182 * TODO: This handles the most basic configuration. i.e. a configuration object 183 * with ssid/passphrase/akm. 184 */ 185 struct dpp_configuration *dpp_parse_configuration_object(const char *json, 186 size_t json_len) 187 { 188 struct dpp_configuration *config; 189 struct json_contents *c; 190 struct json_iter iter; 191 struct json_iter discovery; 192 struct json_iter cred; 193 struct json_iter extra; 194 _auto_(l_free) char *tech = NULL; 195 _auto_(l_free) char *ssid = NULL; 196 _auto_(l_free) char *akm = NULL; 197 _auto_(l_free) char *pass = NULL; 198 _auto_(l_free) char *psk = NULL; 199 200 c = json_contents_new(json, json_len); 201 if (!c) 202 return NULL; 203 204 json_iter_init(&iter, c); 205 206 if (!json_iter_parse(&iter, 207 JSON_MANDATORY("wi-fi_tech", JSON_STRING, &tech), 208 JSON_MANDATORY("discovery", JSON_OBJECT, &discovery), 209 JSON_MANDATORY("cred", JSON_OBJECT, &cred), 210 JSON_OPTIONAL("/net/connman/iwd", JSON_OBJECT, &extra), 211 JSON_UNDEFINED)) 212 goto free_contents; 213 214 if (!tech || strncmp(tech, "infra", 5)) 215 goto free_contents; 216 217 if (!json_iter_parse(&discovery, 218 JSON_MANDATORY("ssid", JSON_STRING, &ssid), 219 JSON_UNDEFINED)) 220 goto free_contents; 221 222 if (!ssid || !util_ssid_is_utf8(strlen(ssid), (const uint8_t *) ssid)) 223 goto free_contents; 224 225 if (!json_iter_parse(&cred, 226 JSON_MANDATORY("akm", JSON_STRING, &akm), 227 JSON_OPTIONAL("pass", JSON_STRING, &pass), 228 JSON_OPTIONAL("psk", JSON_STRING, &psk), 229 JSON_UNDEFINED)) 230 goto free_contents; 231 232 if (!pass && (!psk || strlen(psk) != 64)) 233 goto free_contents; 234 235 config = l_new(struct dpp_configuration, 1); 236 237 if (pass) 238 config->passphrase = l_steal_ptr(pass); 239 else 240 config->psk = l_steal_ptr(psk); 241 242 memcpy(config->ssid, ssid, strlen(ssid)); 243 config->ssid_len = strlen(ssid); 244 245 config->akm_suites = dpp_parse_akm(akm); 246 if (!config->akm_suites) 247 goto free_config; 248 249 if (json_iter_is_valid(&extra)) { 250 if (!dpp_parse_extra_options(config, &extra)) 251 l_warn("Extra settings failed to parse!"); 252 } 253 254 json_contents_free(c); 255 256 return config; 257 258 free_config: 259 dpp_configuration_free(config); 260 free_contents: 261 json_contents_free(c); 262 return NULL; 263 } 264 265 /* 266 * The DPP spec does not specify a difference between FT AKMs and their normal 267 * counterpart. Because of this any FT AKM will just result in the standard 268 * 'psk' or 'sae' AKM. 269 */ 270 static const char *dpp_akm_to_string(enum ie_rsn_akm_suite akm_suite) 271 { 272 switch (akm_suite) { 273 case IE_RSN_AKM_SUITE_PSK: 274 case IE_RSN_AKM_SUITE_FT_USING_PSK: 275 case IE_RSN_AKM_SUITE_PSK_SHA256: 276 return "psk"; 277 case IE_RSN_AKM_SUITE_SAE_SHA256: 278 case IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256: 279 return "sae"; 280 default: 281 return NULL; 282 } 283 } 284 285 char *dpp_configuration_to_json(struct dpp_configuration *config) 286 { 287 _auto_(l_free) char *pass_or_psk; 288 _auto_(l_free) char *ssid; 289 290 ssid = l_malloc(config->ssid_len + 1); 291 memcpy(ssid, config->ssid, config->ssid_len); 292 ssid[config->ssid_len] = '\0'; 293 294 if (config->passphrase) 295 pass_or_psk = l_strdup_printf("\"pass\":\"%s\"", 296 config->passphrase); 297 else 298 pass_or_psk = l_strdup_printf("\"psk\":\"%s\"", 299 config->psk); 300 301 return l_strdup_printf("{\"wi-fi_tech\":\"infra\"," 302 "\"discovery\":{" 303 "\"ssid\":\"%s\"" 304 "}," 305 "\"cred\":{" 306 "\"akm\":\"%s\",%s" 307 "}," 308 "\"/net/connman/iwd\":{" 309 "\"send_hostname\":%s," 310 "\"hidden\":%s}" 311 "}", 312 ssid, dpp_akm_to_string(config->akm_suites), 313 pass_or_psk, 314 config->send_hostname ? "true" : "false", 315 config->hidden ? "true" : "false"); 316 } 317 318 struct dpp_configuration *dpp_configuration_new( 319 const struct l_settings *settings, 320 const char *ssid, 321 enum ie_rsn_akm_suite akm_suite) 322 { 323 struct dpp_configuration *config; 324 _auto_(l_free) char *passphrase = NULL; 325 _auto_(l_free) char *psk = NULL; 326 size_t ssid_len = strlen(ssid); 327 bool send_hostname; 328 bool hidden; 329 330 if (!l_settings_has_group(settings, "Security")) 331 return NULL; 332 333 passphrase = l_settings_get_string(settings, "Security", "Passphrase"); 334 if (!passphrase) { 335 psk = l_settings_get_string(settings, "Security", 336 "PreSharedKey"); 337 if (!psk) 338 return NULL; 339 } 340 341 config = l_new(struct dpp_configuration, 1); 342 343 memcpy(config->ssid, ssid, ssid_len); 344 config->ssid[ssid_len] = '\0'; 345 config->ssid_len = ssid_len; 346 347 if (passphrase) 348 config->passphrase = l_steal_ptr(passphrase); 349 else 350 config->psk = l_steal_ptr(psk); 351 352 353 config->akm_suites = akm_suite; 354 355 if (!l_settings_get_bool(settings, "IPv4", "SendHostname", 356 &send_hostname)) 357 send_hostname = false; 358 359 if (!l_settings_get_bool(settings, "Settings", "Hidden", &hidden)) 360 hidden = false; 361 362 config->send_hostname = send_hostname; 363 config->hidden = hidden; 364 365 return config; 366 } 367 368 void dpp_configuration_free(struct dpp_configuration *config) 369 { 370 if (config->passphrase) 371 l_free(config->passphrase); 372 373 if (config->psk) 374 l_free(config->psk); 375 376 l_free(config); 377 } 378 379 void dpp_attr_iter_init(struct dpp_attr_iter *iter, const uint8_t *pdu, 380 size_t len) 381 { 382 iter->pos = pdu; 383 iter->end = pdu + len; 384 } 385 386 bool dpp_attr_iter_next(struct dpp_attr_iter *iter, 387 enum dpp_attribute_type *type_out, size_t *len_out, 388 const uint8_t **data_out) 389 { 390 enum dpp_attribute_type type; 391 uint16_t len; 392 393 if (iter->pos + 4 > iter->end) 394 return false; 395 396 type = l_get_le16(iter->pos); 397 len = l_get_le16(iter->pos + 2); 398 399 iter->pos += 4; 400 401 if (iter->end - iter->pos < len) 402 return false; 403 404 *type_out = type; 405 *len_out = len; 406 *data_out = iter->pos; 407 408 iter->pos += len; 409 410 return true; 411 } 412 413 size_t dpp_append_attr(uint8_t *to, enum dpp_attribute_type type, 414 void *attr, size_t attr_len) 415 { 416 l_put_le16(type, to); 417 l_put_le16(attr_len, to + 2); 418 memcpy(to + 4, attr, attr_len); 419 420 return attr_len + 4; 421 } 422 423 /* 424 * The use of ad0/ad1 differs with different protocol frame types, which is why 425 * this is left up to the caller to pass the correct AD bytes. The usage is 426 * defined in: 427 * 428 * 6.3.1.4 Protocol Conventions (for authentication) 429 * 6.4.1 Overview (for configuration) 430 * 431 */ 432 uint8_t *dpp_unwrap_attr(const void *ad0, size_t ad0_len, const void *ad1, 433 size_t ad1_len, const void *key, size_t key_len, 434 const void *wrapped, size_t wrapped_len, 435 size_t *unwrapped_len) 436 { 437 struct iovec ad[2]; 438 uint8_t *unwrapped; 439 size_t ad_size = 0; 440 441 if (ad0) { 442 ad[ad_size].iov_base = (void *) ad0; 443 ad[ad_size].iov_len = ad0_len; 444 ad_size++; 445 } 446 447 if (ad1) { 448 ad[ad_size].iov_base = (void *) ad1; 449 ad[ad_size].iov_len = ad1_len; 450 ad_size++; 451 } 452 453 unwrapped = l_malloc(wrapped_len - 16); 454 455 if (!aes_siv_decrypt(key, key_len, wrapped, wrapped_len, ad, ad_size, 456 unwrapped)) { 457 l_free(unwrapped); 458 return NULL; 459 } 460 461 *unwrapped_len = wrapped_len - 16; 462 463 return unwrapped; 464 } 465 466 /* 467 * Encrypt DPP attributes encapsulated in DPP wrapped data. 468 * 469 * ad0/ad0_len - frame specific AD0 component 470 * ad1/ad0_len - frame specific AD1 component 471 * to - buffer to encrypt data. 472 * to_len - size of 'to' 473 * key - key used to encrypt 474 * key_len - size of 'key' 475 * num_attrs - number of attributes listed (type, length, data triplets) 476 * ... - List of attributes, Type, Length, and data 477 */ 478 size_t dpp_append_wrapped_data(const void *ad0, size_t ad0_len, 479 const void *ad1, size_t ad1_len, 480 uint8_t *to, size_t to_len, 481 const void *key, size_t key_len, 482 size_t num_attrs, ...) 483 { 484 size_t i; 485 size_t attrs_len = 0; 486 _auto_(l_free) uint8_t *plaintext = NULL; 487 uint8_t *ptr; 488 struct iovec ad[2]; 489 size_t ad_size = 0; 490 va_list va; 491 492 va_start(va, num_attrs); 493 494 /* Count up total attributes length */ 495 for (i = 0; i < num_attrs; i++) { 496 va_arg(va, enum dpp_attribute_type); 497 attrs_len += va_arg(va, size_t) + 4; 498 va_arg(va, void*); 499 } 500 501 va_end(va); 502 503 if (to_len < attrs_len + 4 + 16) 504 return false; 505 506 plaintext = l_malloc(attrs_len); 507 508 ptr = plaintext; 509 510 va_start(va, num_attrs); 511 512 /* Build up plaintext attributes */ 513 for (i = 0; i < num_attrs; i++) { 514 enum dpp_attribute_type type = va_arg(va, 515 enum dpp_attribute_type); 516 size_t l = va_arg(va, size_t); 517 void *p = va_arg(va, void *); 518 519 l_put_le16(type, ptr); 520 ptr += 2; 521 l_put_le16(l, ptr); 522 ptr += 2; 523 memcpy(ptr, p, l); 524 ptr += l; 525 } 526 527 va_end(va); 528 529 ptr = to; 530 531 l_put_le16(DPP_ATTR_WRAPPED_DATA, ptr); 532 ptr += 2; 533 l_put_le16(attrs_len + 16, ptr); 534 ptr += 2; 535 536 if (ad0) { 537 ad[ad_size].iov_base = (void *) ad0; 538 ad[ad_size].iov_len = ad0_len; 539 ad_size++; 540 } 541 542 if (ad1) { 543 ad[ad_size].iov_base = (void *) ad1; 544 ad[ad_size].iov_len = ad1_len; 545 ad_size++; 546 } 547 548 if (!aes_siv_encrypt(key, key_len, plaintext, attrs_len, 549 ad, ad_size, ptr)) 550 return 0; 551 552 return attrs_len + 4 + 16; 553 } 554 555 /* 556 * EasyConnect 2.0 Table 3. Key and Nonce Length Dependency on Prime Length 557 */ 558 static enum l_checksum_type dpp_sha_from_key_len(size_t len) 559 { 560 if (len == 32) 561 return L_CHECKSUM_SHA256; 562 else if (len == 48) 563 return L_CHECKSUM_SHA384; 564 else if (len == 64) 565 return L_CHECKSUM_SHA512; 566 else 567 return L_CHECKSUM_NONE; 568 569 } 570 571 size_t dpp_nonce_len_from_key_len(size_t len) 572 { 573 if (len == 32) 574 return 16; 575 else if (len == 48) 576 return 24; 577 else if (len == 64) 578 return 32; 579 else 580 return 0; 581 } 582 583 /* 584 * 3.2.2 585 * H() 586 */ 587 bool dpp_hash(enum l_checksum_type type, uint8_t *out, unsigned int num, ...) 588 { 589 struct l_checksum *sha = l_checksum_new(type); 590 size_t hsize = l_checksum_digest_length(type); 591 unsigned int i; 592 593 va_list va; 594 595 va_start(va, num); 596 597 for (i = 0; i < num; i++) { 598 void *data = va_arg(va, void *); 599 size_t len = va_arg(va, size_t); 600 601 l_checksum_update(sha, data, len); 602 } 603 604 va_end(va); 605 606 l_checksum_get_digest(sha, out, hsize); 607 l_checksum_free(sha); 608 609 return true; 610 } 611 612 /* 613 * 3.2.2 614 * 615 * HKDF is defined as: 616 * 617 * key = HKDF(salt, info, ikm) 618 * = HKDF-Expand(HKDF-Extract(salt, ikm), info, len) 619 * 620 * Note: A NULL 'salt' means a zero'ed buffer and 'salt_len' should still be 621 * set for this zero'ed buffer length. 622 */ 623 static bool dpp_hkdf(enum l_checksum_type sha, const void *salt, 624 size_t salt_len, const char *info, const void *ikm, 625 size_t ikm_len, void *out, size_t out_len) 626 { 627 uint8_t tmp[64]; 628 uint8_t zero_salt[64] = { 0 }; 629 size_t hash_len = l_checksum_digest_length(sha); 630 631 if (!salt) 632 salt = zero_salt; 633 634 if (!hkdf_extract(sha, salt, salt_len, 1, tmp, 635 ikm, ikm_len)) 636 return false; 637 638 return hkdf_expand(sha, tmp, hash_len, info, out, out_len); 639 } 640 641 bool dpp_derive_r_auth(const void *i_nonce, const void *r_nonce, 642 size_t nonce_len, struct l_ecc_point *i_proto, 643 struct l_ecc_point *r_proto, 644 struct l_ecc_point *i_boot, 645 struct l_ecc_point *r_boot, 646 void *r_auth) 647 { 648 uint64_t pix[L_ECC_MAX_DIGITS]; 649 uint64_t prx[L_ECC_MAX_DIGITS]; 650 uint64_t brx[L_ECC_MAX_DIGITS]; 651 uint64_t bix[L_ECC_MAX_DIGITS]; 652 size_t keys_len; 653 uint8_t zero = 0; 654 enum l_checksum_type type; 655 656 keys_len = l_ecc_point_get_x(i_proto, pix, sizeof(pix)); 657 l_ecc_point_get_x(r_proto, prx, sizeof(prx)); 658 l_ecc_point_get_x(r_boot, brx, sizeof(brx)); 659 660 if (i_boot) 661 l_ecc_point_get_x(i_boot, bix, sizeof(bix)); 662 663 type = dpp_sha_from_key_len(keys_len); 664 665 /* 666 * R-auth = H(I-nonce | R-nonce | PI.x | PR.x | [ BI.x | ] BR.x | 0) 667 */ 668 return dpp_hash(type, r_auth, 7, i_nonce, nonce_len, r_nonce, nonce_len, 669 pix, keys_len, prx, keys_len, 670 bix, i_boot ? keys_len : 0, brx, keys_len, 671 &zero, (size_t) 1); 672 } 673 674 bool dpp_derive_i_auth(const void *r_nonce, const void *i_nonce, 675 size_t nonce_len, struct l_ecc_point *r_proto, 676 struct l_ecc_point *i_proto, 677 struct l_ecc_point *r_boot, 678 struct l_ecc_point *i_boot, void *i_auth) 679 { 680 uint64_t prx[L_ECC_MAX_DIGITS]; 681 uint64_t pix[L_ECC_MAX_DIGITS]; 682 uint64_t brx[L_ECC_MAX_DIGITS]; 683 uint64_t bix[L_ECC_MAX_DIGITS]; 684 size_t keys_len; 685 uint8_t one = 1; 686 enum l_checksum_type type; 687 688 keys_len = l_ecc_point_get_x(r_proto, prx, sizeof(prx)); 689 l_ecc_point_get_x(i_proto, pix, sizeof(pix)); 690 l_ecc_point_get_x(r_boot, brx, sizeof(brx)); 691 692 if (i_boot) 693 l_ecc_point_get_x(i_boot, bix, sizeof(bix)); 694 695 type = dpp_sha_from_key_len(keys_len); 696 697 /* 698 * I-auth = H(R-nonce | I-nonce | PR.x | PI.x | BR.x | [ BI.x | ] 1) 699 */ 700 return dpp_hash(type, i_auth, 7, r_nonce, nonce_len, i_nonce, nonce_len, 701 prx, keys_len, pix, keys_len, brx, keys_len, 702 bix, i_boot ? keys_len : 0, 703 &one, (size_t) 1); 704 } 705 706 /* 707 * Derives key k1. This returns the intermediate secret M.x used in deriving 708 * key ke. 709 */ 710 struct l_ecc_scalar *dpp_derive_k1(const struct l_ecc_point *i_proto_public, 711 const struct l_ecc_scalar *boot_private, 712 void *k1) 713 { 714 struct l_ecc_scalar *m; 715 uint64_t mx_bytes[L_ECC_MAX_DIGITS]; 716 ssize_t key_len; 717 enum l_checksum_type sha; 718 719 if (!l_ecdh_generate_shared_secret(boot_private, i_proto_public, &m)) 720 return NULL; 721 722 key_len = l_ecc_scalar_get_data(m, mx_bytes, sizeof(mx_bytes)); 723 if (key_len < 0) 724 goto free_m; 725 726 sha = dpp_sha_from_key_len(key_len); 727 728 if (!dpp_hkdf(sha, NULL, key_len, "first intermediate key", mx_bytes, 729 key_len, k1, key_len)) 730 goto free_m; 731 732 return m; 733 734 free_m: 735 l_ecc_scalar_free(m); 736 return NULL; 737 } 738 739 /* 740 * Derives key k2. This returns the intermediate secret N.x used in deriving 741 * key ke. 742 */ 743 struct l_ecc_scalar *dpp_derive_k2(const struct l_ecc_point *i_proto_public, 744 const struct l_ecc_scalar *proto_private, 745 void *k2) 746 { 747 struct l_ecc_scalar *n; 748 uint64_t nx_bytes[L_ECC_MAX_DIGITS]; 749 ssize_t key_len; 750 enum l_checksum_type sha; 751 752 if (!l_ecdh_generate_shared_secret(proto_private, i_proto_public, &n)) 753 return NULL; 754 755 key_len = l_ecc_scalar_get_data(n, nx_bytes, sizeof(nx_bytes)); 756 if (key_len < 0) 757 goto free_n; 758 759 sha = dpp_sha_from_key_len(key_len); 760 761 if (!dpp_hkdf(sha, NULL, key_len, "second intermediate key", nx_bytes, 762 key_len, k2, key_len)) 763 goto free_n; 764 765 return n; 766 767 free_n: 768 l_ecc_scalar_free(n); 769 return NULL; 770 } 771 772 bool dpp_derive_ke(const uint8_t *i_nonce, const uint8_t *r_nonce, 773 struct l_ecc_scalar *m, struct l_ecc_scalar *n, 774 struct l_ecc_point *l, void *ke) 775 { 776 uint8_t nonces[32 + 32]; 777 size_t nonce_len; 778 uint64_t mx_bytes[L_ECC_MAX_DIGITS]; 779 uint64_t nx_bytes[L_ECC_MAX_DIGITS]; 780 uint64_t lx_bytes[L_ECC_MAX_DIGITS]; 781 uint64_t bk[L_ECC_MAX_DIGITS]; 782 ssize_t key_len; 783 enum l_checksum_type sha; 784 785 key_len = l_ecc_scalar_get_data(m, mx_bytes, sizeof(mx_bytes)); 786 l_ecc_scalar_get_data(n, nx_bytes, sizeof(nx_bytes)); 787 788 nonce_len = dpp_nonce_len_from_key_len(key_len); 789 sha = dpp_sha_from_key_len(key_len); 790 791 if (l) 792 l_ecc_point_get_x(l, lx_bytes, key_len * 2); 793 794 memcpy(nonces, i_nonce, nonce_len); 795 memcpy(nonces + nonce_len, r_nonce, nonce_len); 796 797 /* bk = HKDF-Extract(I-nonce | R-nonce, M.x | N.x [ | L.x]) */ 798 if (!hkdf_extract(sha, nonces, nonce_len * 2, 3, bk, mx_bytes, 799 key_len, nx_bytes, key_len, lx_bytes, l ? key_len : 0)) 800 return false; 801 802 /* ke = HKDF-Expand(bk, "DPP Key", length) */ 803 return hkdf_expand(sha, bk, key_len, "DPP Key", ke, key_len); 804 } 805 806 /* 807 * Values derived from OID definitions in https://www.secg.org/sec2-v2.pdf 808 * Appendix A.2.1 809 * 810 * 1.2.840.10045.2.1 (ecPublicKey) 811 */ 812 static struct asn1_oid ec_oid = { 813 .asn1_len = 7, 814 .asn1 = { 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01 } 815 }; 816 817 /* 1.2.840.10045.3.1.7 (prime256v1) */ 818 static struct asn1_oid ec_p256_oid = { 819 .asn1_len = 8, 820 .asn1 = { 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 } 821 }; 822 823 /* 1.3.132.0.34 (secp384r1) */ 824 static struct asn1_oid ec_p384_oid = { 825 .asn1_len = 5, 826 .asn1 = { 0x2B, 0x81, 0x04, 0x00, 0x22 } 827 }; 828 829 uint8_t *dpp_point_to_asn1(const struct l_ecc_point *p, size_t *len_out) 830 { 831 uint8_t *asn1; 832 uint8_t *ptr; 833 struct asn1_oid *key_type; 834 const struct l_ecc_curve *curve = l_ecc_point_get_curve(p); 835 ssize_t key_size = l_ecc_curve_get_scalar_bytes(curve); 836 uint64_t x[L_ECC_MAX_DIGITS]; 837 ssize_t ret; 838 size_t len; 839 uint8_t point_type; 840 841 switch (key_size) { 842 case 32: 843 key_type = &ec_p256_oid; 844 break; 845 case 48: 846 key_type = &ec_p384_oid; 847 break; 848 default: 849 return NULL; 850 } 851 852 ret = l_ecc_point_get_x(p, x, sizeof(x)); 853 if (ret < 0 || ret != key_size) 854 return NULL; 855 856 len = 2 + ec_oid.asn1_len + 2 + key_type->asn1_len + 2 + key_size + 4; 857 858 /* 859 * Set the type to whatever avoids doing p - y when reading in the 860 * key. Working backwards from l_ecc_point_from_data if Y is even and 861 * the type is BIT0 there is no subtraction. Similarly if Y is odd 862 * and the type is BIT1. 863 */ 864 if (!l_ecc_point_y_isodd(p)) 865 point_type = L_ECC_POINT_TYPE_COMPRESSED_BIT0; 866 else 867 point_type = L_ECC_POINT_TYPE_COMPRESSED_BIT1; 868 869 if (L_WARN_ON(len > 128)) 870 return NULL; 871 872 asn1 = l_malloc(len + 2); 873 ptr = asn1; 874 875 *ptr++ = ASN1_ID_SEQUENCE; 876 /* Length of both OIDs and key, plus tag/len bytes */ 877 *ptr++ = len; 878 879 *ptr++ = ASN1_ID_SEQUENCE; 880 881 len = ec_oid.asn1_len + key_type->asn1_len + 4; 882 883 *ptr++ = len; 884 885 *ptr++ = ASN1_ID_OID; 886 *ptr++ = ec_oid.asn1_len; 887 memcpy(ptr, ec_oid.asn1, ec_oid.asn1_len); 888 ptr += ec_oid.asn1_len; 889 890 *ptr++ = ASN1_ID_OID; 891 *ptr++ = key_type->asn1_len; 892 memcpy(ptr, key_type->asn1, key_type->asn1_len); 893 ptr += key_type->asn1_len; 894 895 *ptr++ = ASN1_ID_BIT_STRING; 896 *ptr++ = key_size + 2; 897 *ptr++ = 0x00; 898 *ptr++ = point_type; 899 memcpy(ptr, x, key_size); 900 ptr += key_size; 901 902 if (len_out) 903 *len_out = ptr - asn1; 904 905 return asn1; 906 } 907 908 /* 909 * Only checking for the ASN.1 form: 910 * 911 * SEQUENCE { 912 * SEQUENCE { 913 * OBJECT IDENTIFIER ecPublicKey 914 * OBJECT IDENTIFIER key type (p256/p384) 915 * } 916 * BITSTRING (key data) 917 * } 918 */ 919 struct l_ecc_point *dpp_point_from_asn1(const uint8_t *asn1, size_t len) 920 { 921 922 const uint8_t *outer_seq; 923 size_t outer_len; 924 const uint8_t *inner_seq; 925 size_t inner_len; 926 const uint8_t *elem; 927 const uint8_t *key_data; 928 size_t elen = 0; 929 uint8_t tag; 930 unsigned int curve_num; 931 const struct l_ecc_curve *curve; 932 933 /* SEQUENCE */ 934 outer_seq = asn1_der_find_elem(asn1, len, 0, &tag, &outer_len); 935 if (!outer_seq || tag != ASN1_ID_SEQUENCE) 936 return NULL; 937 938 /* SEQUENCE */ 939 inner_seq = asn1_der_find_elem(outer_seq, outer_len, 0, &tag, 940 &inner_len); 941 if (!inner_seq || tag != ASN1_ID_SEQUENCE) 942 return NULL; 943 944 /* OBJECT IDENTIFIER (ecPublicKey) */ 945 elem = asn1_der_find_elem(inner_seq, inner_len, 0, &tag, &elen); 946 if (!elem || tag != ASN1_ID_OID) 947 return NULL; 948 949 /* Check that this OID is ecPublicKey */ 950 if (!asn1_oid_eq(&ec_oid, elen, elem)) 951 return NULL; 952 953 elem = asn1_der_find_elem(inner_seq, inner_len, 1, &tag, &elen); 954 if (!elem || tag != ASN1_ID_OID) 955 return NULL; 956 957 /* Check if ELL supports this curve */ 958 if (asn1_oid_eq(&ec_p256_oid, elen, elem)) 959 curve_num = 19; 960 else if (asn1_oid_eq(&ec_p384_oid, elen, elem)) 961 curve_num = 20; 962 else 963 return NULL; 964 965 curve = l_ecc_curve_from_ike_group(curve_num); 966 if (!curve) 967 return NULL; 968 969 /* BITSTRING */ 970 key_data = asn1_der_find_elem(outer_seq, outer_len, 1, &tag, &elen); 971 if (!key_data || tag != ASN1_ID_BIT_STRING || elen < 2) 972 return NULL; 973 974 return l_ecc_point_from_data(curve, key_data[1], 975 key_data + 2, elen - 2); 976 } 977 978 /* 979 * Advances 'p' to the next character 'sep' plus one. strchr can be trusted to 980 * find the next character, but we do need to check that the next character + 1 981 * isn't the NULL terminator, i.e. that data actually exists past this point. 982 */ 983 #define TOKEN_NEXT(p, sep) \ 984 ({ \ 985 const char *_next = strchr((p), (sep)); \ 986 if (_next) { \ 987 if (*(_next + 1) == '\0') \ 988 _next = NULL; \ 989 else \ 990 _next++; \ 991 } \ 992 _next; \ 993 }) 994 995 /* 996 * Finds the length of the current token (characters until next 'sep'). If no 997 * 'sep' is found zero is returned. 998 */ 999 #define TOKEN_LEN(p, sep) \ 1000 ({ \ 1001 const char *_next = strchr((p), (sep)); \ 1002 if (!_next) \ 1003 _next = (p); \ 1004 (_next - (p)); \ 1005 }) 1006 1007 /* 1008 * Ensures 'p' points to something resembling a single character followed by 1009 * ':' followed by at least one non-null byte of data. This allows the parse 1010 * loop to safely advance the pointer to each tokens data (pos + 2) 1011 */ 1012 #define TOKEN_OK(p) \ 1013 ((p) && (p)[0] != '\0' && (p)[1] == ':' && (p)[2] != '\0') \ 1014 1015 static struct scan_freq_set *dpp_parse_class_and_channel(const char *token, 1016 unsigned int len) 1017 { 1018 const char *pos = token; 1019 char *end; 1020 struct scan_freq_set *freqs = scan_freq_set_new(); 1021 1022 /* Checking for <operclass>/<channel>,<operclass>/<channel>,... */ 1023 for (; pos && pos < token + len; pos = TOKEN_NEXT(pos, ',')) { 1024 unsigned long r; 1025 uint8_t channel; 1026 uint8_t oper_class; 1027 uint32_t freq; 1028 1029 /* strtoul accepts minus and plus signs before value */ 1030 if (*pos == '-' || *pos == '+') 1031 goto free_set; 1032 1033 /* to check uint8_t overflow */ 1034 errno = 0; 1035 r = oper_class = strtoul(pos, &end, 10); 1036 1037 if (errno == ERANGE || errno == EINVAL) 1038 goto free_set; 1039 /* 1040 * Did strtoul not advance pointer, not reach the next 1041 * token, or overflow? 1042 */ 1043 if (end == pos || *end != '/' || r != oper_class) 1044 goto free_set; 1045 1046 pos = end + 1; 1047 1048 if (*pos == '-' || *pos == '+') 1049 goto free_set; 1050 1051 errno = 0; 1052 r = channel = strtoul(pos, &end, 10); 1053 1054 if (errno == ERANGE || errno == EINVAL) 1055 goto free_set; 1056 /* 1057 * Same verification as above, but also checks either for 1058 * another pair (,) or end of this token (;) 1059 */ 1060 if (end == pos || (*end != ',' && *end != ';') || r != channel) 1061 goto free_set; 1062 1063 freq = oci_to_frequency(oper_class, channel); 1064 if (!freq) 1065 goto free_set; 1066 1067 scan_freq_set_add(freqs, freq); 1068 } 1069 1070 if (token + len != end) 1071 goto free_set; 1072 1073 if (scan_freq_set_isempty(freqs)) { 1074 free_set: 1075 scan_freq_set_free(freqs); 1076 return NULL; 1077 } 1078 1079 return freqs; 1080 } 1081 1082 static int dpp_parse_mac(const char *str, unsigned int len, uint8_t *mac_out) 1083 { 1084 uint8_t mac[6]; 1085 unsigned int i; 1086 1087 if (len != 12) 1088 return -EINVAL; 1089 1090 for (i = 0; i < 12; i += 2) { 1091 if (!l_ascii_isxdigit(str[i])) 1092 return -EINVAL; 1093 1094 if (!l_ascii_isxdigit(str[i + 1])) 1095 return -EINVAL; 1096 } 1097 1098 if (sscanf(str, "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", 1099 &mac[0], &mac[1], &mac[2], 1100 &mac[3], &mac[4], &mac[5]) != 6) 1101 return -EINVAL; 1102 1103 if (!util_is_valid_sta_address(mac)) 1104 return -EINVAL; 1105 1106 memcpy(mac_out, mac, 6); 1107 1108 return 0; 1109 } 1110 1111 static int dpp_parse_version(const char *str, unsigned int len, 1112 uint8_t *version_out) 1113 { 1114 if (len != 1) 1115 return -EINVAL; 1116 1117 if (str[0] != '1' && str[0] != '2') 1118 return -EINVAL; 1119 1120 *version_out = str[0] - '0'; 1121 1122 return 0; 1123 } 1124 1125 static struct l_ecc_point *dpp_parse_key(const char *str, unsigned int len) 1126 { 1127 _auto_(l_free) uint8_t *decoded = NULL; 1128 size_t decoded_len; 1129 1130 decoded = l_base64_decode(str, len, &decoded_len); 1131 if (!decoded) 1132 return NULL; 1133 1134 return dpp_point_from_asn1(decoded, decoded_len); 1135 } 1136 1137 /* 1138 * Parse a bootstrapping URI. This parses the tokens defined in the Easy Connect 1139 * spec, and verifies they are the correct syntax. Some values have extra 1140 * verification: 1141 * - The bootstrapping key is base64 decoded and converted to an l_ecc_point 1142 * - The operating class and channels are checked against the OCI table. 1143 * - The version is checked to be either 1 or 2, as defined by the spec. 1144 * - The MAC is verified to be a valid station address. 1145 */ 1146 struct dpp_uri_info *dpp_parse_uri(const char *uri) 1147 { 1148 struct dpp_uri_info *info; 1149 const char *pos = uri; 1150 const char *end = uri + strlen(uri) - 1; 1151 int ret = 0; 1152 1153 if (!l_str_has_prefix(pos, "DPP:")) 1154 return NULL; 1155 1156 info = l_new(struct dpp_uri_info, 1); 1157 1158 pos += 4; 1159 1160 /* EasyConnect 5.2.1 - Bootstrapping information format */ 1161 for (; TOKEN_OK(pos); pos = TOKEN_NEXT(pos, ';')) { 1162 unsigned int len = TOKEN_LEN(pos + 2, ';'); 1163 1164 if (!len) 1165 goto free_info; 1166 1167 switch (*pos) { 1168 case 'C': 1169 if (L_WARN_ON(info->freqs)) 1170 goto free_info; 1171 1172 info->freqs = dpp_parse_class_and_channel(pos + 2, len); 1173 if (!info->freqs) 1174 goto free_info; 1175 break; 1176 case 'M': 1177 if (L_WARN_ON(!l_memeqzero(info->mac, 1178 sizeof(info->mac)))) 1179 goto free_info; 1180 1181 ret = dpp_parse_mac(pos + 2, len, info->mac); 1182 if (ret < 0) 1183 goto free_info; 1184 break; 1185 case 'V': 1186 if (L_WARN_ON(info->version != 0)) 1187 goto free_info; 1188 1189 ret = dpp_parse_version(pos + 2, len, &info->version); 1190 if (ret < 0) 1191 goto free_info; 1192 break; 1193 case 'K': 1194 if (L_WARN_ON(info->boot_public)) 1195 goto free_info; 1196 1197 info->boot_public = dpp_parse_key(pos + 2, len); 1198 if (!info->boot_public) 1199 goto free_info; 1200 break; 1201 case 'H': 1202 case 'I': 1203 break; 1204 default: 1205 goto free_info; 1206 } 1207 } 1208 1209 /* Extra data found after last token */ 1210 if (pos != end) 1211 goto free_info; 1212 1213 /* The public bootstrapping key is the only required token */ 1214 if (!info->boot_public) 1215 goto free_info; 1216 1217 return info; 1218 1219 free_info: 1220 dpp_free_uri_info(info); 1221 return NULL; 1222 } 1223 1224 void dpp_free_uri_info(struct dpp_uri_info *info) 1225 { 1226 if (info->freqs) 1227 scan_freq_set_free(info->freqs); 1228 1229 if (info->boot_public) 1230 l_ecc_point_free(info->boot_public); 1231 1232 if (info->information) 1233 l_free(info->information); 1234 1235 if (info->host) 1236 l_free(info->host); 1237 1238 l_free(info); 1239 } 1240 1241 /* 1242 * 6.3.4 DPP Authentication Confirm 1243 * 1244 * L = bI * (BR + PR) 1245 */ 1246 struct l_ecc_point *dpp_derive_li(const struct l_ecc_point *boot_public, 1247 const struct l_ecc_point *proto_public, 1248 const struct l_ecc_scalar *boot_private) 1249 { 1250 const struct l_ecc_curve *curve = l_ecc_point_get_curve(boot_public); 1251 struct l_ecc_point *ret = l_ecc_point_new(curve); 1252 1253 l_ecc_point_add(ret, boot_public, proto_public); 1254 l_ecc_point_multiply(ret, boot_private, ret); 1255 1256 return ret; 1257 } 1258 1259 /* 1260 * 6.3.3 DPP Authentication Response 1261 * 1262 * L = ((bR + pR) modulo q) * BI 1263 */ 1264 struct l_ecc_point *dpp_derive_lr(const struct l_ecc_scalar *boot_private, 1265 const struct l_ecc_scalar *proto_private, 1266 const struct l_ecc_point *peer_public) 1267 { 1268 const struct l_ecc_curve *curve = l_ecc_point_get_curve(peer_public); 1269 _auto_(l_ecc_scalar_free) struct l_ecc_scalar *order = 1270 l_ecc_curve_get_order(curve); 1271 _auto_(l_ecc_scalar_free) struct l_ecc_scalar *sum = 1272 l_ecc_scalar_new(curve, NULL, 0); 1273 _auto_(l_ecc_point_free) struct l_ecc_point *ret = 1274 l_ecc_point_new(curve); 1275 1276 if (!l_ecc_scalar_add(sum, boot_private, proto_private, order)) 1277 return NULL; 1278 1279 if (!l_ecc_point_multiply(ret, sum, peer_public)) 1280 return NULL; 1281 1282 return l_steal_ptr(ret); 1283 } 1284 1285 1286 static struct l_ecc_point *dpp_derive_q(const struct l_ecc_curve *curve, 1287 const uint8_t *p_data, 1288 const char *key, 1289 const char *identifier, 1290 const uint8_t *mac) 1291 { 1292 _auto_(l_ecc_scalar_free) struct l_ecc_scalar *scalar = NULL; 1293 _auto_(l_ecc_point_free) struct l_ecc_point *ret = NULL; 1294 uint8_t hash[L_ECC_SCALAR_MAX_BYTES]; 1295 unsigned int bytes = l_ecc_curve_get_scalar_bytes(curve); 1296 enum l_checksum_type type = dpp_sha_from_key_len(bytes); 1297 _auto_(l_ecc_point_free) struct l_ecc_point *p = NULL; 1298 struct l_checksum *sha = l_checksum_new(type); 1299 1300 /* 1301 * "If the Initiator indicates PKEX with a Protocol Version of 1, 1302 * MAC-Initiator shall be the MAC address of the Initiator and the 1303 * Protocol Version shall not be present. Otherwise, MAC-Initiator is 1304 * not present" 1305 * 1306 * (This goes for MAC-Responder as well) 1307 */ 1308 if (mac) 1309 l_checksum_update(sha, mac, 6); 1310 1311 if (identifier) 1312 l_checksum_update(sha, identifier, strlen(identifier)); 1313 1314 l_checksum_update(sha, key, strlen(key)); 1315 l_checksum_get_digest(sha, hash, bytes); 1316 l_checksum_free(sha); 1317 1318 /* Unlikely but can happen */ 1319 scalar = l_ecc_scalar_new(curve, hash, bytes); 1320 if (!scalar) 1321 return NULL; 1322 1323 p = l_ecc_point_from_data(curve, L_ECC_POINT_TYPE_FULL, 1324 p_data, bytes * 2); 1325 if (!p) 1326 return NULL; 1327 1328 ret = l_ecc_point_new(curve); 1329 1330 if (!l_ecc_point_multiply(ret, scalar, p)) 1331 return NULL; 1332 1333 return l_steal_ptr(ret); 1334 } 1335 1336 /* 1337 * 5.6.2 PKEX Exchange Phase 1338 * 1339 * Qi = H([MAC-Initiator |] [identifier | ] code) * Pi 1340 */ 1341 struct l_ecc_point *dpp_derive_qi(const struct l_ecc_curve *curve, 1342 const char *key, 1343 const char *identifier, 1344 const uint8_t *mac_initiator) 1345 { 1346 return dpp_derive_q(curve, dpp_pkex_initiator_p256, key, identifier, 1347 mac_initiator); 1348 } 1349 1350 /* 1351 * 5.6.2 PKEX Exchange Phase 1352 * 1353 * Qr = H([MAC-Responder |] [identifier | ] code) * Pr 1354 */ 1355 struct l_ecc_point *dpp_derive_qr(const struct l_ecc_curve *curve, 1356 const char *key, 1357 const char *identifier, 1358 const uint8_t *mac_responder) 1359 { 1360 return dpp_derive_q(curve, dpp_pkex_responder_p256, key, identifier, 1361 mac_responder); 1362 } 1363 1364 /* 1365 * 5.6.2 PKEX Exchange Phase 1366 * 1367 * z = HKDF(<>, info | M.x | N.x | code, K.x) 1368 */ 1369 bool dpp_derive_z(const uint8_t *mac_i, const uint8_t *mac_r, 1370 const struct l_ecc_point *n, 1371 const struct l_ecc_point *m, 1372 const struct l_ecc_point *k, 1373 const char *key, 1374 const char *identifier, 1375 void *z_out, size_t *z_len) 1376 { 1377 const struct l_ecc_curve *curve = l_ecc_point_get_curve(n); 1378 size_t bytes = l_ecc_curve_get_scalar_bytes(curve); 1379 enum l_checksum_type sha = dpp_sha_from_key_len(bytes); 1380 uint8_t k_x[L_ECC_SCALAR_MAX_BYTES]; 1381 uint8_t m_x[L_ECC_SCALAR_MAX_BYTES]; 1382 uint8_t n_x[L_ECC_SCALAR_MAX_BYTES]; 1383 uint8_t prk[L_ECC_SCALAR_MAX_BYTES]; 1384 1385 l_ecc_point_get_x(k, k_x, sizeof(k_x)); 1386 l_ecc_point_get_x(m, m_x, sizeof(m_x)); 1387 l_ecc_point_get_x(n, n_x, sizeof(n_x)); 1388 1389 hkdf_extract(sha, NULL, 0, 1, prk, k_x, bytes); 1390 1391 /* HKDF-Extract (since it doesn't take non-string arguments)*/ 1392 prf_plus(sha, prk, bytes, z_out, bytes, 5, 1393 mac_i, (size_t) 6, mac_r, (size_t) 6, m_x, bytes, 1394 n_x, bytes, key, strlen(key)); 1395 1396 *z_len = bytes; 1397 1398 return true; 1399 } 1400 1401 /* 1402 * 5.6.3 PKEX Commit-Reveal Phase 1403 * 1404 * Initiator derivation: 1405 * u = HMAC(J.x, [MAC-Initiator |] A.x | Y'.x | X.x ) 1406 * 1407 * Responder derivation: 1408 * u' = HMAC(J'.x, [MAC-Initiator |] A'.x | Y.x | X'.x) 1409 */ 1410 bool dpp_derive_u(const struct l_ecc_point *j, 1411 const uint8_t *mac_i, 1412 const struct l_ecc_point *a, 1413 const struct l_ecc_point *y, 1414 const struct l_ecc_point *x, 1415 void *u_out, size_t *u_len) 1416 { 1417 const struct l_ecc_curve *curve = l_ecc_point_get_curve(y); 1418 uint8_t j_x[L_ECC_SCALAR_MAX_BYTES]; 1419 uint8_t a_x[L_ECC_SCALAR_MAX_BYTES]; 1420 uint8_t y_x[L_ECC_SCALAR_MAX_BYTES]; 1421 uint8_t x_x[L_ECC_SCALAR_MAX_BYTES]; 1422 size_t bytes = l_ecc_curve_get_scalar_bytes(curve); 1423 enum l_checksum_type sha = dpp_sha_from_key_len(bytes); 1424 struct l_checksum *hmac; 1425 1426 l_ecc_point_get_x(j, j_x, bytes); 1427 l_ecc_point_get_x(a, a_x, bytes); 1428 l_ecc_point_get_x(y, y_x, bytes); 1429 l_ecc_point_get_x(x, x_x, bytes); 1430 1431 /* u = HMAC(J.x, MAC-Initiator | A.x | Y'.x | X.x)*/ 1432 hmac = l_checksum_new_hmac(sha, j_x, bytes); 1433 l_checksum_update(hmac, mac_i, 6); 1434 l_checksum_update(hmac, a_x, bytes); 1435 l_checksum_update(hmac, y_x, bytes); 1436 l_checksum_update(hmac, x_x, bytes); 1437 l_checksum_get_digest(hmac, u_out, bytes); 1438 l_checksum_free(hmac); 1439 1440 *u_len = bytes; 1441 1442 return true; 1443 } 1444 1445 /* 1446 * 5.6.3 PKEX Commit-Reveal Phase 1447 * 1448 * Initiator derivation: 1449 * v = HMAC(L.x, [MAC-Responder |] B.x | X'.x |Y.x ) 1450 * 1451 * Responder derivation: 1452 * v' = HMAC(L.x, [MAC-Responder |] B'.x | X.x | Y'.x ) 1453 */ 1454 bool dpp_derive_v(const struct l_ecc_point *l, const uint8_t *mac, 1455 const struct l_ecc_point *b, 1456 const struct l_ecc_point *x, 1457 const struct l_ecc_point *y, 1458 void *v_out, size_t *v_len) 1459 { 1460 const struct l_ecc_curve *curve = l_ecc_point_get_curve(l); 1461 uint8_t l_x[L_ECC_SCALAR_MAX_BYTES]; 1462 uint8_t b_x[L_ECC_SCALAR_MAX_BYTES]; 1463 uint8_t x_x[L_ECC_SCALAR_MAX_BYTES]; 1464 uint8_t y_x[L_ECC_SCALAR_MAX_BYTES]; 1465 size_t bytes = l_ecc_curve_get_scalar_bytes(curve); 1466 enum l_checksum_type sha = dpp_sha_from_key_len(bytes); 1467 struct l_checksum *hmac; 1468 1469 l_ecc_point_get_x(l, l_x, sizeof(l_x)); 1470 l_ecc_point_get_x(b, b_x, sizeof(b_x)); 1471 l_ecc_point_get_x(x, x_x, sizeof(x_x)); 1472 l_ecc_point_get_x(y, y_x, sizeof(y_x)); 1473 1474 hmac = l_checksum_new_hmac(sha, l_x, bytes); 1475 1476 if (mac) 1477 l_checksum_update(hmac, mac, 6); 1478 1479 l_checksum_update(hmac, b_x, bytes); 1480 l_checksum_update(hmac, x_x, bytes); 1481 l_checksum_update(hmac, y_x, bytes); 1482 l_checksum_get_digest(hmac, v_out, bytes); 1483 l_checksum_free(hmac); 1484 1485 *v_len = bytes; 1486 1487 return true; 1488 }