rename central_device_db into le_device_db

This commit is contained in:
matthias.ringwald@gmail.com 2015-03-02 21:30:12 +00:00
parent d2d764240d
commit c5d3944d90
9 changed files with 112 additions and 111 deletions

View File

@ -80,7 +80,7 @@ static bd_addr_t att_client_address;
static uint16_t att_request_size = 0;
static uint8_t att_request_buffer[HCI_ACL_PAYLOAD_SIZE];
static int att_ir_central_device_db_index = -1;
static int att_ir_le_device_db_index = -1;
static int att_ir_lookup_active = 0;
static int att_handle_value_indication_handle = 0;
@ -162,14 +162,14 @@ static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uin
break;
case SM_IDENTITY_RESOLVING_SUCCEEDED:
att_ir_lookup_active = 0;
att_ir_central_device_db_index = ((sm_event_t*) packet)->central_device_db_index;
log_info("SM_IDENTITY_RESOLVING_SUCCEEDED id %u", att_ir_central_device_db_index);
att_ir_le_device_db_index = ((sm_event_t*) packet)->central_device_db_index;
log_info("SM_IDENTITY_RESOLVING_SUCCEEDED id %u", att_ir_le_device_db_index);
att_run();
break;
case SM_IDENTITY_RESOLVING_FAILED:
log_info("SM_IDENTITY_RESOLVING_FAILED");
att_ir_lookup_active = 0;
att_ir_central_device_db_index = -1;
att_ir_le_device_db_index = -1;
att_run();
break;
@ -203,7 +203,7 @@ static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
// update sequence number
uint32_t counter_packet = READ_BT_32(att_request_buffer, att_request_size-12);
central_device_db_remote_counter_set(att_ir_central_device_db_index, counter_packet+1);
le_device_db_remote_counter_set(att_ir_le_device_db_index, counter_packet+1);
att_server_state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
att_run();
}
@ -229,7 +229,7 @@ static void att_run(void){
if (att_ir_lookup_active){
return;
}
if (att_ir_central_device_db_index < 0){
if (att_ir_le_device_db_index < 0){
log_info("ATT Signed Write, CSRK not available");
att_server_state = ATT_SERVER_IDLE;
return;
@ -237,7 +237,7 @@ static void att_run(void){
// check counter
uint32_t counter_packet = READ_BT_32(att_request_buffer, att_request_size-12);
uint32_t counter_db = central_device_db_remote_counter_get(att_ir_central_device_db_index);
uint32_t counter_db = le_device_db_remote_counter_get(att_ir_le_device_db_index);
log_info("ATT Signed Write, DB counter %u, packet counter %u", counter_db, counter_packet);
if (counter_packet < counter_db){
log_info("ATT Signed Write, db reports higher counter, abort");
@ -247,7 +247,7 @@ static void att_run(void){
// signature is { sequence counter, secure hash }
sm_key_t csrk;
central_device_db_csrk_get(att_ir_central_device_db_index, csrk);
le_device_db_csrk_get(att_ir_le_device_db_index, csrk);
att_server_state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
log_info("Orig Signature: ");
hexdump( &att_request_buffer[att_request_size-8], 8);

View File

@ -35,8 +35,8 @@
*
*/
#ifndef __CENTRAL_DEVICE_DB_H
#define __CENTRAL_DEVICE_DB_H
#ifndef __LE_DEVICE_DB_H
#define __LE_DEVICE_DB_H
#include <btstack/utils.h>
@ -46,25 +46,25 @@ extern "C" {
/**
A Central Device DB is only required for signed writes
LE Device DB for pure LE Peripherals is only required for signed writes
Per bonded device, it stores the Identity Resolving Key (IRK), the Connection Signature Resolving Key (CSRK)
and the last used counter
The IRK is necessary to identify a device that uses private addresses
The CSRK is used to generate the signatur on the remote device and is needed to verify the signature itself
The Counter is necessary to prevent reply attacks
Per bonded device, it can store
- it stores the Identity Resolving Key (IRK) and its address to resolve private addresses
- it stores the LTK + EDIV, RAND. EDIV + RAND allow a LE Perihperal to reconstruct the LTK
- it stores the Connection Signature Resolving Key (CSRK) and the last used counter.
The CSRK is used to generate the signatur on the remote device and is needed to verify the signature itself
The Counter is necessary to prevent reply attacks
*/
// Central Device db interface
// LE Device db interface
/**
* @brief init
*/
void central_device_db_init();
void le_device_db_init();
/**
* @brief add device to db
@ -72,13 +72,13 @@ void central_device_db_init();
* @param irk of the device
* @returns index if successful, -1 otherwise
*/
int central_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk);
int le_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk);
/**
* @brief get number of devices in db for enumeration
* @returns number of device in db
*/
int central_device_db_count(void);
int le_device_db_count(void);
/**
* @brief get device information: addr type and address needed to identify device
@ -86,7 +86,7 @@ int central_device_db_count(void);
* @param addr_type, address of the device as output
* @param irk of the device
*/
void central_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk);
void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk);
/**
@ -96,7 +96,7 @@ void central_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t
* @brief rand
* @brief ltk
*/
void central_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8], sm_key_t ltk);
void le_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8], sm_key_t ltk);
/**
* @brief get remote encryption info
@ -105,58 +105,58 @@ void central_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8],
* @brief rand
* @brief ltk
*/
void central_device_db_encryption_get(int index, uint16_t * ediv, uint8_t rand[8], sm_key_t ltk);
void le_device_db_encryption_get(int index, uint16_t * ediv, uint8_t rand[8], sm_key_t ltk);
/**
* @brief set signing key for this device
* @param index
* @param signing key as input
*/
void central_device_db_csrk_set(int index, sm_key_t csrk);
void le_device_db_csrk_set(int index, sm_key_t csrk);
/**
* @brief get signing key for this device
* @param index
* @param signing key as output
*/
void central_device_db_csrk_get(int index, sm_key_t csrk);
void le_device_db_csrk_get(int index, sm_key_t csrk);
/**
* @brief query last used/seen signing counter
* @param index
* @returns next expected counter, 0 after devices was added
*/
uint32_t central_device_db_remote_counter_get(int index);
uint32_t le_device_db_remote_counter_get(int index);
/**
* @brief update signing counter
* @param index
* @param counter to store
*/
void central_device_db_remote_counter_set(int index, uint32_t counter);
void le_device_db_remote_counter_set(int index, uint32_t counter);
/**
* @brief query last used/seen signing counter
* @param index
* @returns next expected counter, 0 after devices was added
*/
uint32_t central_device_db_local_counter_get(int index);
uint32_t le_device_db_local_counter_get(int index);
/**
* @brief update signing counter
* @param index
* @param counter to store
*/
void central_device_db_local_counter_set(int index, uint32_t counter);
void le_device_db_local_counter_set(int index, uint32_t counter);
/**
* @brief free device
* @param index
*/
void central_device_db_remove(int index);
void le_device_db_remove(int index);
#if defined __cplusplus
}
#endif
#endif // __CENTRAL_DEVICE_DB_H
#endif // __LE_DEVICE_DB_H

View File

@ -37,15 +37,15 @@
#include "central_device_db.h"
// Central Device db interface
void central_device_db_init(){}
void le_device_db_init(){}
// @returns index if successful, -1 otherwise
int central_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk){
int le_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk){
return -1;
}
// @returns number of device in db
int central_device_db_count(void){
int le_device_db_count(void){
return 0;
}
@ -56,7 +56,7 @@ int central_device_db_count(void){
* @brief rand
* @brief ltk
*/
void central_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8], sm_key_t ltk){}
void le_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8], sm_key_t ltk){}
/**
* @brief get remote encryption info
@ -65,30 +65,30 @@ void central_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8],
* @brief rand
* @brief ltk
*/
void central_device_db_encryption_get(int index, uint16_t * ediv, uint8_t rand[8], sm_key_t ltk){}
void le_device_db_encryption_get(int index, uint16_t * ediv, uint8_t rand[8], sm_key_t ltk){}
// get device information: addr type and address
void central_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t csrk){}
void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t csrk){}
// get signature key
void central_device_db_csrk_get(int index, sm_key_t csrk){}
void le_device_db_csrk_get(int index, sm_key_t csrk){}
// query last used/seen signing counter
uint32_t central_device_db_remote_counter_get(int index){
uint32_t le_device_db_remote_counter_get(int index){
return 0xffffffff;
}
// update signing counter
void central_device_db_local_counter_set(int index, uint32_t counter){}
void le_device_db_local_counter_set(int index, uint32_t counter){}
// query last used/seen signing counter
uint32_t central_device_db_local_counter_get(int index){
uint32_t le_device_db_local_counter_get(int index){
return 0xffffffff;
}
// update signing counter
void central_device_db_remote_counter_set(int index, uint32_t counter){}
void le_device_db_remote_counter_set(int index, uint32_t counter){}
// free device
void central_device_db_remove(int index){}
void le_device_db_remove(int index){}

View File

@ -42,7 +42,7 @@
#include "debug.h"
// Central Device db implemenation using static memory
typedef struct central_device_memory_db {
typedef struct le_device_memory_db {
// Identification
int addr_type;
@ -62,40 +62,40 @@ typedef struct central_device_memory_db {
// Signed Writes to remote (local CSRK is fixed)
uint32_t local_counter;
} central_device_memory_db_t;
} le_device_memory_db_t;
#define CENTRAL_DEVICE_MEMORY_SIZE 4
#define LE_DEVICE_MEMORY_SIZE 4
#define INVALID_ENTRY_ADDR_TYPE 0xff
static central_device_memory_db_t central_devices[CENTRAL_DEVICE_MEMORY_SIZE];
static le_device_memory_db_t le_devices[LE_DEVICE_MEMORY_SIZE];
void central_device_db_init(){
void le_device_db_init(){
int i;
for (i=0;i<CENTRAL_DEVICE_MEMORY_SIZE;i++){
central_device_db_remove(i);
for (i=0;i<LE_DEVICE_MEMORY_SIZE;i++){
le_device_db_remove(i);
}
}
// @returns number of device in db
int central_device_db_count(void){
int le_device_db_count(void){
int i;
int counter = 0;
for (i=0;i<CENTRAL_DEVICE_MEMORY_SIZE;i++){
if (central_devices[i].addr_type != INVALID_ENTRY_ADDR_TYPE) counter++;
for (i=0;i<LE_DEVICE_MEMORY_SIZE;i++){
if (le_devices[i].addr_type != INVALID_ENTRY_ADDR_TYPE) counter++;
}
return counter;
}
// free device
void central_device_db_remove(int index){
central_devices[index].addr_type = INVALID_ENTRY_ADDR_TYPE;
void le_device_db_remove(int index){
le_devices[index].addr_type = INVALID_ENTRY_ADDR_TYPE;
}
int central_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk){
int le_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk){
int i;
int index = -1;
for (i=0;i<CENTRAL_DEVICE_MEMORY_SIZE;i++){
if (central_devices[i].addr_type == INVALID_ENTRY_ADDR_TYPE){
for (i=0;i<LE_DEVICE_MEMORY_SIZE;i++){
if (le_devices[i].addr_type == INVALID_ENTRY_ADDR_TYPE){
index = i;
break;
}
@ -106,20 +106,20 @@ int central_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk){
log_info("Central Device DB adding type %u - %s", addr_type, bd_addr_to_str(addr));
log_key("irk", irk);
central_devices[index].addr_type = addr_type;
memcpy(central_devices[index].addr, addr, 6);
memcpy(central_devices[index].irk, irk, 16);
central_devices[index].remote_counter = 0;
le_devices[index].addr_type = addr_type;
memcpy(le_devices[index].addr, addr, 6);
memcpy(le_devices[index].irk, irk, 16);
le_devices[index].remote_counter = 0;
return index;
}
// get device information: addr type and address
void central_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk){
if (addr_type) *addr_type = central_devices[index].addr_type;
if (addr) memcpy(addr, central_devices[index].addr, 6);
if (irk) memcpy(irk, central_devices[index].irk, 16);
void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk){
if (addr_type) *addr_type = le_devices[index].addr_type;
if (addr) memcpy(addr, le_devices[index].addr, 6);
if (irk) memcpy(irk, le_devices[index].irk, 16);
}
/**
@ -129,10 +129,10 @@ void central_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t
* @brief rand
* @brief ltk
*/
void central_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8], sm_key_t ltk){
central_devices[index].ediv = ediv;
if (rand) memcpy(central_devices[index].rand, rand, 8);
if (ltk) memcpy(central_devices[index].ltk, ltk, 16);
void le_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8], sm_key_t ltk){
le_devices[index].ediv = ediv;
if (rand) memcpy(le_devices[index].rand, rand, 8);
if (ltk) memcpy(le_devices[index].ltk, ltk, 16);
}
/**
@ -142,48 +142,48 @@ void central_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8],
* @brief rand
* @brief ltk
*/
void central_device_db_encryption_get(int index, uint16_t * ediv, uint8_t rand[8], sm_key_t ltk){
if (ediv) *ediv = central_devices[index].ediv;
if (rand) memcpy(rand, central_devices[index].rand, 8);
if (ltk) memcpy(ltk, central_devices[index].ltk, 16);
void le_device_db_encryption_get(int index, uint16_t * ediv, uint8_t rand[8], sm_key_t ltk){
if (ediv) *ediv = le_devices[index].ediv;
if (rand) memcpy(rand, le_devices[index].rand, 8);
if (ltk) memcpy(ltk, le_devices[index].ltk, 16);
}
// get signature key
void central_device_db_csrk_get(int index, sm_key_t csrk){
if (csrk) memcpy(csrk, central_devices[index].csrk, 16);
void le_device_db_csrk_get(int index, sm_key_t csrk){
if (csrk) memcpy(csrk, le_devices[index].csrk, 16);
}
void central_device_db_csrk_set(int index, sm_key_t csrk){
if (csrk) memcpy(central_devices[index].csrk, csrk, 16);
void le_device_db_csrk_set(int index, sm_key_t csrk){
if (csrk) memcpy(le_devices[index].csrk, csrk, 16);
}
// query last used/seen signing counter
uint32_t central_device_db_remote_counter_get(int index){
return central_devices[index].remote_counter;
uint32_t le_device_db_remote_counter_get(int index){
return le_devices[index].remote_counter;
}
// update signing counter
void central_device_db_remote_counter_set(int index, uint32_t counter){
central_devices[index].remote_counter = counter;
void le_device_db_remote_counter_set(int index, uint32_t counter){
le_devices[index].remote_counter = counter;
}
// query last used/seen signing counter
uint32_t central_device_db_local_counter_get(int index){
return central_devices[index].local_counter;
uint32_t le_device_db_local_counter_get(int index){
return le_devices[index].local_counter;
}
// update signing counter
void central_device_db_local_counter_set(int index, uint32_t counter){
central_devices[index].local_counter = counter;
void le_device_db_local_counter_set(int index, uint32_t counter){
le_devices[index].local_counter = counter;
}
void central_device_db_dump(){
log_info("Central Device DB dump, devices: %u", central_device_db_count);
void le_device_db_dump(){
log_info("Central Device DB dump, devices: %u", le_device_db_count);
int i;
for (i=0;i<CENTRAL_DEVICE_MEMORY_SIZE;i++){
if (central_devices[i].addr_type == INVALID_ENTRY_ADDR_TYPE) continue;
log_info("%u: %u %s", i, central_devices[i].addr_type, bd_addr_to_str(central_devices[i].addr));
log_key("irk", central_devices[i].irk);
log_key("csrk", central_devices[i].csrk);
for (i=0;i<LE_DEVICE_MEMORY_SIZE;i++){
if (le_devices[i].addr_type == INVALID_ENTRY_ADDR_TYPE) continue;
log_info("%u: %u %s", i, le_devices[i].addr_type, bd_addr_to_str(le_devices[i].addr));
log_key("irk", le_devices[i].irk);
log_key("csrk", le_devices[i].csrk);
}
}

View File

@ -919,12 +919,12 @@ static void sm_run(void){
// -- Continue with CSRK device lookup by public or resolvable private address
if (sm_central_device_test >= 0){
log_info("Central Device Lookup: device %u/%u", sm_central_device_test, central_device_db_count());
while (sm_central_device_test < central_device_db_count()){
log_info("Central Device Lookup: device %u/%u", sm_central_device_test, le_device_db_count());
while (sm_central_device_test < le_device_db_count()){
int addr_type;
bd_addr_t addr;
sm_key_t irk;
central_device_db_info(sm_central_device_test, &addr_type, addr, irk);
le_device_db_info(sm_central_device_test, &addr_type, addr, irk);
log_info("device type %u, addr: %s", addr_type, bd_addr_to_str(addr));
if (sm_central_device_addr_type == addr_type && memcmp(addr, sm_central_device_address, 6) == 0){
@ -954,7 +954,7 @@ static void sm_run(void){
return;
}
if (sm_central_device_test >= central_device_db_count()){
if (sm_central_device_test >= le_device_db_count()){
log_info("Central Device Lookup: not found");
sm_central_device_test = -1;
sm_csrk_connection_source->sm_csrk_lookup_state = CSRK_LOOKUP_IDLE;
@ -1581,7 +1581,7 @@ static void sm_event_packet_handler (uint8_t packet_type, uint16_t channel, uint
sm_conn->sm_connection_encrypted = 0;
sm_conn->sm_connection_authenticated = 0;
sm_conn->sm_connection_authorization_state = AUTHORIZATION_UNKNOWN;
sm_conn->sm_central_db_index = -1;
sm_conn->sm_le_db_index = -1;
// prepare CSRK lookup (does not involve setup)
sm_conn->sm_csrk_lookup_state = CSRK_LOOKUP_W4_READY;
@ -1883,23 +1883,23 @@ static void sm_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *pac
// store, if: it's a public address, or, we got an IRK
if (setup->sm_peer_addr_type == 0 || (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_IDENTITY_INFORMATION)) {
sm_conn->sm_central_db_index = central_device_db_add(setup->sm_peer_addr_type, setup->sm_peer_address, setup->sm_peer_irk);
sm_conn->sm_le_db_index = le_device_db_add(setup->sm_peer_addr_type, setup->sm_peer_address, setup->sm_peer_irk);
}
if (sm_conn->sm_central_db_index >= 0){
central_device_db_local_counter_set(sm_conn->sm_central_db_index, 0);
if (sm_conn->sm_le_db_index >= 0){
le_device_db_local_counter_set(sm_conn->sm_le_db_index, 0);
// store CSRK
if (setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION){
central_device_db_csrk_set(sm_conn->sm_central_db_index, setup->sm_peer_csrk);
central_device_db_remote_counter_set(sm_conn->sm_central_db_index, 0);
le_device_db_csrk_set(sm_conn->sm_le_db_index, setup->sm_peer_csrk);
le_device_db_remote_counter_set(sm_conn->sm_le_db_index, 0);
}
// store encryption information as Central
if (sm_conn->sm_role == 0
&& setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_ENCRYPTION_INFORMATION
&& setup->sm_key_distribution_received_set & SM_KEYDIST_FLAG_MASTER_IDENTIFICATION){
central_device_db_encryption_set(sm_conn->sm_central_db_index, setup->sm_peer_ediv, setup->sm_peer_rand, setup->sm_peer_ltk);
le_device_db_encryption_set(sm_conn->sm_le_db_index, setup->sm_peer_ediv, setup->sm_peer_rand, setup->sm_peer_ltk);
}
}

View File

@ -183,8 +183,8 @@ int btstack_main(int argc, const char * argv[]){
// set up l2cap_le
l2cap_init();
// setup central device db
central_device_db_init();
// setup le device db
le_device_db_init();
// setup SM: Display only
sm_init();

View File

@ -967,8 +967,8 @@ int btstack_main(int argc, const char * argv[]){
// set up l2cap_le
l2cap_init();
// setup central device db
central_device_db_init();
// setup le device db
le_device_db_init();
// setup SM: Display only
sm_init();

View File

@ -281,8 +281,8 @@ int btstack_main(void)
hci_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
// setup central device db
central_device_db_init();
// setup le device db
le_device_db_init();
// setup SM: Display only
sm_init();

View File

@ -456,6 +456,7 @@ typedef struct sm_connection {
authorization_state_t sm_connection_authorization_state;
uint16_t sm_local_ediv;
uint8_t sm_local_rand[8];
int sm_le_db_index;
} sm_connection_t;
typedef struct {