From b761c4251379938bb2a97c901cc8b384ab9c3e37 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 14 Jun 2019 15:43:15 +0200 Subject: [PATCH] mesh: use identity key from network key in mesh proxy --- test/mesh/mesh.c | 6 +++--- test/mesh/mesh_access.h | 4 ++-- test/mesh/mesh_proxy.c | 19 +++++++++++++------ test/mesh/mesh_proxy.h | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/test/mesh/mesh.c b/test/mesh/mesh.c index 461cae9f6..766ec0a08 100644 --- a/test/mesh/mesh.c +++ b/test/mesh/mesh.c @@ -104,7 +104,6 @@ static const btstack_tlv_t * btstack_tlv_singleton_impl; static void * btstack_tlv_singleton_context; static uint8_t beacon_key[16]; -static uint8_t identity_key[16]; static uint8_t network_id[8]; static uint16_t primary_element_address; @@ -135,6 +134,7 @@ static void mesh_provisioning_dump(const mesh_provisioning_data_t * data){ printf("IV Index: 0x%08x\n", data->iv_index); printf("DevKey: "); printf_hexdump(data->device_key, 16); printf("NetKey: "); printf_hexdump(data->net_key, 16); + printf("-- Derived from NetKey --\n"); printf("NID: 0x%02x\n", data->nid); printf("NetworkID: "); printf_hexdump(data->network_id, 8); printf("BeaconKey: "); printf_hexdump(data->beacon_key, 16); @@ -198,7 +198,7 @@ static void mesh_setup_from_provisioning_data(const mesh_provisioning_data_t * p // set device_key mesh_transport_set_device_key(provisioning_data->device_key); // copy beacon key and network id - memcpy(identity_key, provisioning_data->identity_key, 16); + // memcpy(identity_key, provisioning_data->identity_key, 16); memcpy(beacon_key, provisioning_data->beacon_key, 16); memcpy(network_id, provisioning_data->network_id, 8); // for secure beacon @@ -209,7 +209,7 @@ static void mesh_setup_from_provisioning_data(const mesh_provisioning_data_t * p // Mesh Proxy #ifdef ENABLE_MESH_PROXY_SERVER // Setup Proxy - mesh_proxy_init(provisioning_data->unicast_address, provisioning_data->identity_key); + mesh_proxy_init(provisioning_data->unicast_address); printf("Advertise Mesh Proxy Service with Network ID\n"); mesh_proxy_start_advertising_with_network_id(); diff --git a/test/mesh/mesh_access.h b/test/mesh/mesh_access.h index 4bc6db4f2..d0131c37d 100644 --- a/test/mesh/mesh_access.h +++ b/test/mesh/mesh_access.h @@ -225,8 +225,8 @@ uint8_t * mesh_pdu_data(mesh_pdu_t * pdu); // Mesh NetKey List void mesh_store_network_key(mesh_network_key_t * network_key); void mesh_delete_network_key(uint16_t internal_index); -void mesh_delete_net_keys(void); -void mesh_load_net_keys(void); +void mesh_delete_networ_keys(void); +void mesh_load_network_keys(void); // Mesh Appkeys void mesh_store_app_key(uint16_t internal_index, uint16_t netkey_index, uint16_t appkey_index, uint8_t aid, const uint8_t * application_key); diff --git a/test/mesh/mesh_proxy.c b/test/mesh/mesh_proxy.c index cb4eaac5f..8efb42aca 100644 --- a/test/mesh/mesh_proxy.c +++ b/test/mesh/mesh_proxy.c @@ -65,7 +65,6 @@ static uint8_t mesh_proxy_node_id_plain static uint8_t mesh_proxy_node_id_hash[16]; static uint8_t mesh_proxy_node_id_random_value[8]; -static uint8_t proxy_identity_key[16]; static uint16_t primary_element_address; // Mesh Proxy, advertise with node id @@ -103,7 +102,8 @@ static void mesh_proxy_node_id_timeout_handler(btstack_timer_source_t * ts){ } static void mesh_proxy_node_id_handle_get_aes128(void * arg){ - UNUSED(arg); + mesh_network_key_t * network_key = (mesh_network_key_t *) arg; + memcpy(connectable_advertisement_item.adv_data, adv_data_with_node_identity_template, 12); memcpy(&connectable_advertisement_item.adv_data[12], &mesh_proxy_node_id_hash[8], 8); memcpy(&connectable_advertisement_item.adv_data[20], mesh_proxy_node_id_random_value, 8); @@ -116,21 +116,29 @@ static void mesh_proxy_node_id_handle_get_aes128(void * arg){ btstack_run_loop_set_timer_handler(&mesh_proxy_node_id_timer, mesh_proxy_node_id_timeout_handler); btstack_run_loop_set_timer(&mesh_proxy_node_id_timer, MESH_PROXY_NODE_ID_ADVERTISEMENT_TIMEOUT_MS); btstack_run_loop_add_timer(&mesh_proxy_node_id_timer); + + // mark as active + network_key->node_id_advertisement_running = 1; } static void mesh_proxy_node_id_handle_random(void * arg){ + mesh_network_key_t * network_key = (mesh_network_key_t *) arg; + // Hash = e(IdentityKey, Padding | Random | Address) mod 2^64 memset(mesh_proxy_node_id_plaintext, 0, sizeof(mesh_proxy_node_id_plaintext)); memcpy(&mesh_proxy_node_id_plaintext[6] , mesh_proxy_node_id_random_value, 8); big_endian_store_16(mesh_proxy_node_id_plaintext, 14, primary_element_address); - btstack_crypto_aes128_encrypt(&mesh_proxy_node_id_crypto_request_aes128, proxy_identity_key, mesh_proxy_node_id_plaintext, mesh_proxy_node_id_hash, mesh_proxy_node_id_handle_get_aes128, NULL); + btstack_crypto_aes128_encrypt(&mesh_proxy_node_id_crypto_request_aes128, network_key->identity_key, mesh_proxy_node_id_plaintext, mesh_proxy_node_id_hash, mesh_proxy_node_id_handle_get_aes128, network_key); } static void mesh_proxy_start_advertising_with_node_id(uint16_t netkey_index){ mesh_proxy_stop_all_advertising_with_node_id(); + // get network key + mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); + if (network_key == NULL) return; log_info("Proxy start advertising with node id, netkey index %04x", netkey_index); // setup node id - btstack_crypto_random_generate(&mesh_proxy_node_id_crypto_request_random, mesh_proxy_node_id_random_value, sizeof(mesh_proxy_node_id_random_value), mesh_proxy_node_id_handle_random, NULL); + btstack_crypto_random_generate(&mesh_proxy_node_id_crypto_request_random, mesh_proxy_node_id_random_value, sizeof(mesh_proxy_node_id_random_value), mesh_proxy_node_id_handle_random, network_key); } static void mesh_proxy_stop_advertising_with_node_id(uint16_t netkey_index){ @@ -205,9 +213,8 @@ void mesh_proxy_stop_advertising_with_network_id(void){ } } -void mesh_proxy_init(uint16_t primary_unicast_address, const uint8_t * identity_key){ +void mesh_proxy_init(uint16_t primary_unicast_address){ primary_element_address = primary_unicast_address; - memcpy(proxy_identity_key, identity_key, 16); } #endif diff --git a/test/mesh/mesh_proxy.h b/test/mesh/mesh_proxy.h index 65a0b3075..abdbb6946 100644 --- a/test/mesh/mesh_proxy.h +++ b/test/mesh/mesh_proxy.h @@ -54,7 +54,7 @@ typedef enum { /** * @brief Init Mesh Proxy */ -void mesh_proxy_init(uint16_t primary_unicast_address, const uint8_t * identity_key); +void mesh_proxy_init(uint16_t primary_unicast_address); /** * @brief Set Advertising with Node ID on given subnet