mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-01-18 19:21:54 +00:00
mesh: calculate identity key
This commit is contained in:
parent
3886cd806e
commit
e85388bcc4
@ -180,6 +180,7 @@ static void mesh_message_handler (uint8_t packet_type, uint16_t channel, uint8_t
|
||||
case MESH_PB_PROV_COMPLETE:
|
||||
printf("Provisioning complete\n");
|
||||
memcpy(provisioning_data.network_id, provisioning_device_data_get_network_id(), 8);
|
||||
memcpy(provisioning_data.identity_key, provisioning_device_data_get_identity_key(), 16);
|
||||
memcpy(provisioning_data.beacon_key, provisioning_device_data_get_beacon_key(), 16);
|
||||
memcpy(provisioning_data.encryption_key, provisioning_device_data_get_encryption_key(), 16);
|
||||
memcpy(provisioning_data.privacy_key, provisioning_device_data_get_privacy_key(), 16);
|
||||
|
@ -138,6 +138,7 @@ static void mesh_message_handler (uint8_t packet_type, uint16_t channel, uint8_t
|
||||
printf("Provisioning complete\n");
|
||||
memcpy(provisioning_data.network_id, provisioning_device_data_get_network_id(), 8);
|
||||
memcpy(provisioning_data.beacon_key, provisioning_device_data_get_beacon_key(), 16);
|
||||
memcpy(provisioning_data.identity_key, provisioning_device_data_get_identity_key(), 16);
|
||||
memcpy(provisioning_data.encryption_key, provisioning_device_data_get_encryption_key(), 16);
|
||||
memcpy(provisioning_data.privacy_key, provisioning_device_data_get_privacy_key(), 16);
|
||||
memcpy(provisioning_data.device_key, provisioning_device_data_get_device_key(), 16);
|
||||
|
@ -58,6 +58,12 @@ static uint16_t con_handle;
|
||||
static uint16_t adv_int_min = 0x0030;
|
||||
static uint16_t adv_int_max = 0x0030;
|
||||
|
||||
static btstack_crypto_random_t crypto_request_random;
|
||||
static btstack_crypto_aes128_t crypto_request_aes128;
|
||||
static uint8_t plaintext[16];
|
||||
static uint8_t hash[8];
|
||||
static uint8_t random_value[8];
|
||||
|
||||
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
||||
|
||||
static uint8_t adv_data_with_network_id[] = {
|
||||
@ -116,23 +122,64 @@ static void stdin_process(char cmd){
|
||||
}
|
||||
}
|
||||
|
||||
static void setup_advertising_with_network_id(uint8_t * network_id, uint16_t network_id_size){
|
||||
static void setup_advertising_with_network_id(mesh_provisioning_data_t * prov_data){
|
||||
// dynamically store network ID into adv data
|
||||
// skip flipping for now ... (check if provisioner or BlueNRG-MESH has a bug)
|
||||
// uint8_t netid_flipped[8];
|
||||
// reverse_64(provisioning_data.network_id, netid_flipped);
|
||||
// memcpy(&adv_data_with_network_id[12], netid_flipped, sizeof(netid_flipped));
|
||||
memcpy(&adv_data_with_network_id[12], network_id, network_id_size);
|
||||
memcpy(&adv_data_with_network_id[12], prov_data->network_id, sizeof(prov_data->network_id));
|
||||
// setup advertisements
|
||||
bd_addr_t null_addr;
|
||||
memset(null_addr, 0, 6);
|
||||
uint8_t adv_type = 0; // AFV_IND
|
||||
gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
|
||||
gap_advertisements_set_data(adv_data_with_network_id_len, (uint8_t*) adv_data_with_network_id);
|
||||
gap_advertisements_enable(1);
|
||||
}
|
||||
|
||||
static void setup_advertising_with_node_identity(uint16_t unicast_address){
|
||||
// Hash = e(IdentityKey, Padding | Random | Address) mod 2^64
|
||||
uint8_t hash[8];
|
||||
uint8_t random[8];
|
||||
|
||||
static void mesh_proxy_handle_get_aes128(void * arg){
|
||||
UNUSED(arg);
|
||||
memcpy(&adv_data_with_node_identity[12], hash, 8);
|
||||
memcpy(&adv_data_with_node_identity[20], random, 8);
|
||||
}
|
||||
memcpy(&adv_data_with_node_identity[20], random_value, 8);
|
||||
printf("Calculated Hash\n");
|
||||
printf_hexdump(hash, sizeof(hash));
|
||||
|
||||
printf("\nAdv\n");
|
||||
printf_hexdump(adv_data_with_node_identity, sizeof(adv_data_with_node_identity));
|
||||
// setup advertisements
|
||||
bd_addr_t null_addr;
|
||||
memset(null_addr, 0, 6);
|
||||
uint8_t adv_type = 0; // AFV_IND
|
||||
gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
|
||||
gap_advertisements_set_data(adv_data_with_network_id_len, (uint8_t*) adv_data_with_network_id);
|
||||
gap_advertisements_enable(1);
|
||||
}
|
||||
|
||||
static void setup_advertising_with_node_identity(mesh_provisioning_data_t * prov_data){
|
||||
// Hash = e(IdentityKey, Padding | Random | Address) mod 2^64
|
||||
memset(plaintext, 0, sizeof(plaintext));
|
||||
memcpy(&plaintext[6] , random_value, 8);
|
||||
big_endian_store_16(plaintext, 14, prov_data->unicast_address);
|
||||
btstack_crypto_aes128_encrypt(&crypto_request_aes128, prov_data->identity_key, plaintext, hash, mesh_proxy_handle_get_aes128, NULL);
|
||||
}
|
||||
|
||||
static void mesh_proxy_handle_get_random(void * arg){
|
||||
UNUSED(arg);
|
||||
printf("Received random value\n");
|
||||
printf_hexdump(random_value, sizeof(random_value));
|
||||
setup_advertising_with_node_identity(&provisioning_data);
|
||||
}
|
||||
|
||||
static uint8_t mesh_salt_nhbk[16];
|
||||
static btstack_crypto_aes128_cmac_t crypto_aes128_cmac_request;
|
||||
static const char salt[] = {'n','k','i','k'};
|
||||
|
||||
static void mesh_proxy_handle_get_salt_nhbk(void * arg){
|
||||
UNUSED(arg);
|
||||
printf("Received salt_nhbk\n");
|
||||
printf_hexdump(mesh_salt_nhbk, sizeof(mesh_salt_nhbk));
|
||||
}
|
||||
|
||||
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
||||
UNUSED(channel);
|
||||
@ -150,15 +197,9 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
|
||||
printf("Provisioning data available: %u\n", prov_len ? 1 : 0);
|
||||
|
||||
if (!prov_len) break;
|
||||
setup_advertising_with_network_id(provisioning_data.network_id, sizeof(provisioning_data.network_id));
|
||||
|
||||
// setup advertisements
|
||||
bd_addr_t null_addr;
|
||||
memset(null_addr, 0, 6);
|
||||
uint8_t adv_type = 0; // AFV_IND
|
||||
gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
|
||||
gap_advertisements_set_data(adv_data_with_network_id_len, (uint8_t*) adv_data_with_network_id);
|
||||
gap_advertisements_enable(1);
|
||||
// setup_advertising_with_network_id(&provisioning_data);
|
||||
btstack_crypto_random_generate(&crypto_request_random, random_value, sizeof(random_value), mesh_proxy_handle_get_random, NULL);
|
||||
// btstack_crypto_aes128_cmac_zero(&crypto_aes128_cmac_request, 4, (const uint8_t *)salt, mesh_salt_nhbk, mesh_proxy_handle_get_salt_nhbk, NULL);
|
||||
break;
|
||||
}
|
||||
case HCI_EVENT_LE_META:
|
||||
|
@ -140,6 +140,11 @@ static const uint8_t id128_tag[] = { 'i', 'd', '1', '2', '8', 0x01};
|
||||
static const uint8_t mesh_salt_nhbk[] = {
|
||||
0x2c, 0x24, 0x61, 0x9a, 0xb7, 0x93, 0xc1, 0x23, 0x3f, 0x6e, 0x22, 0x67, 0x38, 0x39, 0x3d, 0xec, };
|
||||
|
||||
// AES-CMAC_ZERO('nkik')
|
||||
static const uint8_t mesh_salt_nkik[] = {
|
||||
0xF8, 0x79, 0x5A, 0x1A, 0xAB, 0xF1, 0x82, 0xE4, 0xF1, 0x63, 0xD8, 0x6E, 0x24, 0x5E, 0x19, 0xF4};
|
||||
|
||||
|
||||
typedef enum {
|
||||
DEVICE_W4_INVITE,
|
||||
DEVICE_SEND_CAPABILITIES,
|
||||
@ -162,6 +167,7 @@ static uint16_t pb_transport_cid;
|
||||
// derived
|
||||
static uint8_t network_id[8];
|
||||
static uint8_t beacon_key[16];
|
||||
static uint8_t identity_key[16];
|
||||
static pb_type_t pb_type;
|
||||
|
||||
static void pb_send_pdu(uint16_t transport_cid, const uint8_t * buffer, uint16_t buffer_size){
|
||||
@ -691,6 +697,7 @@ static void provisioning_handle_random(uint8_t *packet, uint16_t size){
|
||||
btstack_crypto_aes128_cmac_zero(&prov_cmac_request, 48, prov_confirmation_inputs, provisioning_salt, &provisioning_handle_random_s1_calculated, NULL);
|
||||
}
|
||||
|
||||
// PROV_DATA
|
||||
static void provisioning_handle_data_k2_calculated(void * arg){
|
||||
// Dump
|
||||
printf("NID: %02x\n", k2_result[0]);
|
||||
@ -710,20 +717,28 @@ static void provisioning_handle_data_k2_calculated(void * arg){
|
||||
}
|
||||
|
||||
static void provisioning_handle_beacon_key_calculated(void *arg){
|
||||
printf("BeaconKey: ");
|
||||
printf_hexdump(beacon_key, 16);
|
||||
printf("IdentityKey: ");
|
||||
printf_hexdump(identity_key, 16);
|
||||
|
||||
// calc k2
|
||||
mesh_k2(&prov_cmac_request, net_key, k2_result, &provisioning_handle_data_k2_calculated, NULL);
|
||||
}
|
||||
|
||||
// PROV_DATA
|
||||
|
||||
static void provisioning_handle_identity_key_calculated(void *arg){
|
||||
printf("BeaconKey: ");
|
||||
printf_hexdump(beacon_key, 16);
|
||||
|
||||
// calc identity key
|
||||
mesh_k1(&prov_cmac_request, net_key, 16, mesh_salt_nkik, id128_tag, sizeof(id128_tag), identity_key, &provisioning_handle_beacon_key_calculated, NULL);
|
||||
}
|
||||
|
||||
static void provisioning_handle_data_network_id_calculated(void * arg){
|
||||
// dump
|
||||
printf("Network ID: ");
|
||||
printf_hexdump(network_id, 8);
|
||||
// calc k1 using
|
||||
mesh_k1(&prov_cmac_request, net_key, 16, mesh_salt_nhbk, id128_tag, sizeof(id128_tag), beacon_key, &provisioning_handle_beacon_key_calculated, NULL);
|
||||
mesh_k1(&prov_cmac_request, net_key, 16, mesh_salt_nhbk, id128_tag, sizeof(id128_tag), beacon_key, &provisioning_handle_identity_key_calculated, NULL);
|
||||
}
|
||||
|
||||
static void provisioning_handle_data_device_key(void * arg){
|
||||
@ -951,6 +966,9 @@ uint32_t provisioning_device_data_get_iv_index(void){
|
||||
const uint8_t * provisioning_device_data_get_beacon_key(void){
|
||||
return beacon_key;
|
||||
}
|
||||
const uint8_t * provisioning_device_data_get_identity_key(void){
|
||||
return identity_key;
|
||||
}
|
||||
uint8_t provisioning_device_data_get_nid(void){
|
||||
return k2_result[0];
|
||||
}
|
||||
|
@ -117,6 +117,7 @@ const uint8_t * provisioning_device_data_get_privacy_key(void);
|
||||
const uint8_t * provisioning_device_data_get_network_id(void);
|
||||
uint32_t provisioning_device_data_get_iv_index(void);
|
||||
const uint8_t * provisioning_device_data_get_beacon_key(void);
|
||||
const uint8_t * provisioning_device_data_get_identity_key(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* end of extern "C" */
|
||||
|
Loading…
x
Reference in New Issue
Block a user