mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-28 08:37:22 +00:00
mesh: extract network key managment from mesh_network into mesh_keys
This commit is contained in:
parent
8e94013fd8
commit
1c4e808439
@ -46,6 +46,7 @@
|
||||
#include "ble/mesh/beacon.h"
|
||||
#include "provisioning.h"
|
||||
#include "provisioning_device.h"
|
||||
#include "mesh_keys.h"
|
||||
#include "btstack.h"
|
||||
|
||||
// configuration
|
||||
@ -57,11 +58,6 @@
|
||||
|
||||
// structs
|
||||
|
||||
typedef struct {
|
||||
uint8_t nid;
|
||||
uint8_t first;
|
||||
} mesh_network_key_iterator_t;
|
||||
|
||||
// globals
|
||||
|
||||
static uint32_t global_iv_index;
|
||||
@ -105,10 +101,6 @@ static btstack_linked_list_t network_pdus_queued;
|
||||
// Network PDUs ready to send via adv bearer
|
||||
static btstack_linked_list_t network_pdus_outgoing;
|
||||
|
||||
|
||||
// mesh network key list
|
||||
static mesh_network_key_t mesh_network_primary_key;
|
||||
|
||||
// mesh network cache - we use 32-bit 'hashes'
|
||||
static uint32_t mesh_network_cache[MESH_NETWORK_CACHE_SIZE];
|
||||
static int mesh_network_cache_index;
|
||||
@ -147,55 +139,6 @@ static void mesh_network_cache_add(uint32_t hash){
|
||||
}
|
||||
}
|
||||
|
||||
// network key list
|
||||
|
||||
const mesh_network_key_t * mesh_network_key_list_get(uint16_t netkey_index){
|
||||
if (netkey_index) return NULL;
|
||||
return &mesh_network_primary_key;
|
||||
}
|
||||
|
||||
void mesh_network_key_list_add_from_provisioning_data(const mesh_provisioning_data_t * provisioning_data){
|
||||
// get single instance
|
||||
mesh_network_key_t * network_key = &mesh_network_primary_key;
|
||||
memset(network_key, 0, sizeof(mesh_network_key_t));
|
||||
|
||||
// NetKey
|
||||
// memcpy(network_key->net_key, provisioning_data, net_key);
|
||||
|
||||
// IdentityKey
|
||||
// memcpy(network_key->identity_key, provisioning_data->identity_key, 16);
|
||||
|
||||
// BeaconKey
|
||||
memcpy(network_key->beacon_key, provisioning_data->beacon_key, 16);
|
||||
|
||||
// NID
|
||||
network_key->nid = provisioning_data->nid;
|
||||
|
||||
// EncryptionKey
|
||||
memcpy(network_key->encryption_key, provisioning_data->encryption_key, 16);
|
||||
|
||||
// PrivacyKey
|
||||
memcpy(network_key->privacy_key, provisioning_data->privacy_key, 16);
|
||||
|
||||
// NetworkID
|
||||
memcpy(network_key->network_id, provisioning_data->network_id, 8);
|
||||
}
|
||||
|
||||
// mesh network key iterator
|
||||
static void mesh_network_key_iterator_init(mesh_network_key_iterator_t * it, uint8_t nid){
|
||||
it->nid = nid;
|
||||
it->first = 1;
|
||||
}
|
||||
|
||||
static int mesh_network_key_iterator_has_more(mesh_network_key_iterator_t * it){
|
||||
return it->first && it->nid == mesh_network_primary_key.nid;
|
||||
}
|
||||
|
||||
static const mesh_network_key_t * mesh_network_key_iterator_get_next(mesh_network_key_iterator_t * it){
|
||||
it->first = 0;
|
||||
return &mesh_network_primary_key;
|
||||
}
|
||||
|
||||
// common helper
|
||||
int mesh_network_address_unicast(uint16_t addr){
|
||||
return addr < 0x8000;
|
||||
|
@ -120,30 +120,6 @@ typedef struct {
|
||||
uint8_t data[MESH_ACCESS_PAYLOAD_MAX];
|
||||
} mesh_transport_pdu_t;
|
||||
|
||||
//
|
||||
typedef struct {
|
||||
btstack_linked_item_t item;
|
||||
|
||||
// index into shared global key list
|
||||
uint16_t netkey_index;
|
||||
|
||||
// random net_key
|
||||
uint8_t net_key[16];
|
||||
|
||||
// derivative data
|
||||
|
||||
// k1
|
||||
uint8_t identity_key[16];
|
||||
uint8_t beacon_key[16];
|
||||
|
||||
// k2
|
||||
uint8_t nid;
|
||||
uint8_t encryption_key[16];
|
||||
uint8_t privacy_key[16];
|
||||
|
||||
// k3
|
||||
uint8_t network_id[8];
|
||||
} mesh_network_key_t;
|
||||
|
||||
/**
|
||||
* @brief Init Mesh Network Layer
|
||||
@ -173,19 +149,6 @@ void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network
|
||||
*/
|
||||
void mesh_network_set_primary_element_address(uint16_t addr);
|
||||
|
||||
/**
|
||||
* @brief Initialize network key list from provisioning data
|
||||
* @param provisioning_data
|
||||
*/
|
||||
void mesh_network_key_list_add_from_provisioning_data(const mesh_provisioning_data_t * provisioning_data);
|
||||
|
||||
/**
|
||||
* @brief Get network_key for netkey_index
|
||||
* @param netkey_index
|
||||
* @returns mesh_network_key_t or NULL
|
||||
*/
|
||||
const mesh_network_key_t * mesh_network_key_list_get(uint16_t netkey_index);
|
||||
|
||||
/**
|
||||
* @brief Send network_pdu after encryption
|
||||
* @param network_pdu
|
||||
|
@ -914,7 +914,11 @@ void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
|
||||
static mesh_network_pdu_t mesh_network_pdu_storage[MAX_NR_MESH_NETWORK_PDUS];
|
||||
static btstack_memory_pool_t mesh_network_pdu_pool;
|
||||
mesh_network_pdu_t * btstack_memory_mesh_network_pdu_get(void){
|
||||
return (mesh_network_pdu_t *) btstack_memory_pool_get(&mesh_network_pdu_pool);
|
||||
void * buffer = btstack_memory_pool_get(&mesh_network_pdu_pool);
|
||||
if (buffer){
|
||||
memset(buffer, 0, sizeof(mesh_network_pdu_t));
|
||||
}
|
||||
return (mesh_network_pdu_t *) buffer;
|
||||
}
|
||||
void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
|
||||
btstack_memory_pool_free(&mesh_network_pdu_pool, mesh_network_pdu);
|
||||
@ -930,7 +934,11 @@ void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
|
||||
#endif
|
||||
#elif defined(HAVE_MALLOC)
|
||||
mesh_network_pdu_t * btstack_memory_mesh_network_pdu_get(void){
|
||||
return (mesh_network_pdu_t*) malloc(sizeof(mesh_network_pdu_t));
|
||||
void * buffer = malloc(sizeof(mesh_network_pdu_t));
|
||||
if (buffer){
|
||||
memset(buffer, 0, sizeof(mesh_network_pdu_t));
|
||||
}
|
||||
return (mesh_network_pdu_t *) buffer;
|
||||
}
|
||||
void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
|
||||
free(mesh_network_pdu);
|
||||
@ -952,7 +960,11 @@ void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
|
||||
static mesh_transport_pdu_t mesh_transport_pdu_storage[MAX_NR_MESH_TRANSPORT_PDUS];
|
||||
static btstack_memory_pool_t mesh_transport_pdu_pool;
|
||||
mesh_transport_pdu_t * btstack_memory_mesh_transport_pdu_get(void){
|
||||
return (mesh_transport_pdu_t *) btstack_memory_pool_get(&mesh_transport_pdu_pool);
|
||||
void * buffer = btstack_memory_pool_get(&mesh_transport_pdu_pool);
|
||||
if (buffer){
|
||||
memset(buffer, 0, sizeof(mesh_transport_pdu_t));
|
||||
}
|
||||
return (mesh_transport_pdu_t *) buffer;
|
||||
}
|
||||
void btstack_memory_mesh_transport_pdu_free(mesh_transport_pdu_t *mesh_transport_pdu){
|
||||
btstack_memory_pool_free(&mesh_transport_pdu_pool, mesh_transport_pdu);
|
||||
@ -968,7 +980,11 @@ void btstack_memory_mesh_transport_pdu_free(mesh_transport_pdu_t *mesh_transport
|
||||
#endif
|
||||
#elif defined(HAVE_MALLOC)
|
||||
mesh_transport_pdu_t * btstack_memory_mesh_transport_pdu_get(void){
|
||||
return (mesh_transport_pdu_t*) malloc(sizeof(mesh_transport_pdu_t));
|
||||
void * buffer = malloc(sizeof(mesh_transport_pdu_t));
|
||||
if (buffer){
|
||||
memset(buffer, 0, sizeof(mesh_transport_pdu_t));
|
||||
}
|
||||
return (mesh_transport_pdu_t *) buffer;
|
||||
}
|
||||
void btstack_memory_mesh_transport_pdu_free(mesh_transport_pdu_t *mesh_transport_pdu){
|
||||
free(mesh_transport_pdu);
|
||||
@ -990,7 +1006,11 @@ void btstack_memory_mesh_transport_pdu_free(mesh_transport_pdu_t *mesh_transport
|
||||
static mesh_network_key_t mesh_network_key_storage[MAX_NR_MESH_NETWORK_KEYS];
|
||||
static btstack_memory_pool_t mesh_network_key_pool;
|
||||
mesh_network_key_t * btstack_memory_mesh_network_key_get(void){
|
||||
return (mesh_network_key_t *) btstack_memory_pool_get(&mesh_network_key_pool);
|
||||
void * buffer = btstack_memory_pool_get(&mesh_network_key_pool);
|
||||
if (buffer){
|
||||
memset(buffer, 0, sizeof(mesh_network_key_t));
|
||||
}
|
||||
return (mesh_network_key_t *) buffer;
|
||||
}
|
||||
void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){
|
||||
btstack_memory_pool_free(&mesh_network_key_pool, mesh_network_key);
|
||||
@ -1006,7 +1026,11 @@ void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){
|
||||
#endif
|
||||
#elif defined(HAVE_MALLOC)
|
||||
mesh_network_key_t * btstack_memory_mesh_network_key_get(void){
|
||||
return (mesh_network_key_t*) malloc(sizeof(mesh_network_key_t));
|
||||
void * buffer = malloc(sizeof(mesh_network_key_t));
|
||||
if (buffer){
|
||||
memset(buffer, 0, sizeof(mesh_network_key_t));
|
||||
}
|
||||
return (mesh_network_key_t *) buffer;
|
||||
}
|
||||
void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){
|
||||
free(mesh_network_key);
|
||||
|
@ -76,6 +76,7 @@ extern "C" {
|
||||
|
||||
#ifdef ENABLE_MESH
|
||||
#include "ble/mesh/mesh_network.h"
|
||||
#include "mesh_keys.h"
|
||||
#endif
|
||||
|
||||
/* API_START */
|
||||
|
@ -47,6 +47,59 @@ static void mesh_print_hex(const char * name, const uint8_t * data, uint16_t len
|
||||
printf_hexdump(data, len);
|
||||
}
|
||||
|
||||
// network key list
|
||||
|
||||
// mesh network key list
|
||||
static mesh_network_key_t mesh_network_primary_key;
|
||||
|
||||
const mesh_network_key_t * mesh_network_key_list_get(uint16_t netkey_index){
|
||||
if (netkey_index) return NULL;
|
||||
return &mesh_network_primary_key;
|
||||
}
|
||||
|
||||
void mesh_network_key_list_add_from_provisioning_data(const mesh_provisioning_data_t * provisioning_data){
|
||||
// get single instance
|
||||
mesh_network_key_t * network_key = &mesh_network_primary_key;
|
||||
memset(network_key, 0, sizeof(mesh_network_key_t));
|
||||
|
||||
// NetKey
|
||||
// memcpy(network_key->net_key, provisioning_data, net_key);
|
||||
|
||||
// IdentityKey
|
||||
// memcpy(network_key->identity_key, provisioning_data->identity_key, 16);
|
||||
|
||||
// BeaconKey
|
||||
memcpy(network_key->beacon_key, provisioning_data->beacon_key, 16);
|
||||
|
||||
// NID
|
||||
network_key->nid = provisioning_data->nid;
|
||||
|
||||
// EncryptionKey
|
||||
memcpy(network_key->encryption_key, provisioning_data->encryption_key, 16);
|
||||
|
||||
// PrivacyKey
|
||||
memcpy(network_key->privacy_key, provisioning_data->privacy_key, 16);
|
||||
|
||||
// NetworkID
|
||||
memcpy(network_key->network_id, provisioning_data->network_id, 8);
|
||||
}
|
||||
|
||||
// mesh network key iterator
|
||||
void mesh_network_key_iterator_init(mesh_network_key_iterator_t * it, uint8_t nid){
|
||||
it->nid = nid;
|
||||
it->first = 1;
|
||||
}
|
||||
|
||||
int mesh_network_key_iterator_has_more(mesh_network_key_iterator_t * it){
|
||||
return it->first && it->nid == mesh_network_primary_key.nid;
|
||||
}
|
||||
|
||||
const mesh_network_key_t * mesh_network_key_iterator_get_next(mesh_network_key_iterator_t * it){
|
||||
it->first = 0;
|
||||
return &mesh_network_primary_key;
|
||||
}
|
||||
|
||||
|
||||
// application key list
|
||||
|
||||
// key management
|
||||
|
@ -49,6 +49,35 @@ extern "C"
|
||||
#include <stdint.h>
|
||||
#include "ble/mesh/mesh_network.h"
|
||||
|
||||
typedef struct {
|
||||
btstack_linked_item_t item;
|
||||
|
||||
// index into shared global key list
|
||||
uint16_t netkey_index;
|
||||
|
||||
// random net_key
|
||||
uint8_t net_key[16];
|
||||
|
||||
// derivative data
|
||||
|
||||
// k1
|
||||
uint8_t identity_key[16];
|
||||
uint8_t beacon_key[16];
|
||||
|
||||
// k2
|
||||
uint8_t nid;
|
||||
uint8_t encryption_key[16];
|
||||
uint8_t privacy_key[16];
|
||||
|
||||
// k3
|
||||
uint8_t network_id[8];
|
||||
} mesh_network_key_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t nid;
|
||||
uint8_t first;
|
||||
} mesh_network_key_iterator_t;
|
||||
|
||||
typedef struct {
|
||||
btstack_linked_item_t item;
|
||||
|
||||
@ -71,6 +100,40 @@ typedef struct {
|
||||
uint8_t first;
|
||||
} mesh_transport_key_iterator_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize network key list from provisioning data
|
||||
* @param provisioning_data
|
||||
*/
|
||||
void mesh_network_key_list_add_from_provisioning_data(const mesh_provisioning_data_t * provisioning_data);
|
||||
|
||||
/**
|
||||
* @brief Get network_key for netkey_index
|
||||
* @param netkey_index
|
||||
* @returns mesh_network_key_t or NULL
|
||||
*/
|
||||
const mesh_network_key_t * mesh_network_key_list_get(uint16_t netkey_index);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param it
|
||||
* @param nid
|
||||
*/
|
||||
void mesh_network_key_iterator_init(mesh_network_key_iterator_t * it, uint8_t nid);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param it
|
||||
* @return
|
||||
*/
|
||||
int mesh_network_key_iterator_has_more(mesh_network_key_iterator_t * it);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param it
|
||||
* @return
|
||||
*/
|
||||
const mesh_network_key_t * mesh_network_key_iterator_get_next(mesh_network_key_iterator_t * it);
|
||||
|
||||
/**
|
||||
* Set device key
|
||||
* @param device_key
|
||||
|
@ -84,6 +84,7 @@ extern "C" {
|
||||
|
||||
#ifdef ENABLE_MESH
|
||||
#include "ble/mesh/mesh_network.h"
|
||||
#include "mesh_keys.h"
|
||||
#endif
|
||||
|
||||
/* API_START */
|
||||
|
Loading…
x
Reference in New Issue
Block a user