From 12df982603c69e0df85d2619f6ae2f248ae52a4c Mon Sep 17 00:00:00 2001 From: Rahul Tank Date: Wed, 20 Sep 2023 12:49:25 +0530 Subject: [PATCH] Added change to generate unique IRK for each chip --- nimble/host/src/ble_hs_pvcy.c | 50 +++++++++++++++++++++++++++--- nimble/host/src/ble_hs_pvcy_priv.h | 3 +- nimble/host/src/ble_hs_startup.c | 9 ++++-- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/nimble/host/src/ble_hs_pvcy.c b/nimble/host/src/ble_hs_pvcy.c index 158da9b60..4abbe1935 100644 --- a/nimble/host/src/ble_hs_pvcy.c +++ b/nimble/host/src/ble_hs_pvcy.c @@ -28,10 +28,7 @@ static uint8_t ble_hs_pvcy_started; static uint8_t ble_hs_pvcy_irk[16]; /** Use this as a default IRK if none gets set. */ -const uint8_t ble_hs_pvcy_default_irk[16] = { - 0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d, - 0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8, -}; +uint8_t ble_hs_pvcy_default_irk[16]; static int ble_hs_pvcy_set_addr_timeout(uint16_t timeout) @@ -203,6 +200,51 @@ ble_hs_pvcy_ensure_started(void) return 0; } +void ble_hs_pvcy_set_default_irk(void) +{ + struct ble_store_value_sec value_sec; + struct ble_store_key_sec key_sec; + uint8_t *local_id = NULL; + int rc; + + memset(&key_sec, 0, sizeof key_sec); + memset(&value_sec, 0x0, sizeof value_sec); + + ble_hs_id_addr(BLE_ADDR_PUBLIC, (const uint8_t **) &local_id, NULL); + + /* Create key / value */ + memcpy (key_sec.peer_addr.val , local_id, BLE_DEV_ADDR_LEN); + key_sec.peer_addr.type = BLE_ADDR_PUBLIC; + + /* Read NVS for local IRK */ + rc = ble_store_read_our_sec(&key_sec, &value_sec); + + if (value_sec.irk_present) { + memcpy(ble_hs_pvcy_default_irk, value_sec.irk, 16); + } else { + /* No entry for local IRK found . Generate one and load in NVS */ + memset(ble_hs_pvcy_default_irk, 0x0, 16); + rc = ble_hs_hci_util_rand(ble_hs_pvcy_default_irk, 16); + + if (rc != 0) { + BLE_HS_LOG(ERROR, "Failed to generate local IRK"); + return; + } + + memset(&value_sec, 0x0, sizeof value_sec); + + memcpy(value_sec.irk, ble_hs_pvcy_default_irk, 16); + + value_sec.irk_present = 1; + + memcpy(value_sec.peer_addr.val, local_id, BLE_DEV_ADDR_LEN); + + value_sec.peer_addr.type = BLE_ADDR_PUBLIC; + + ble_store_write_our_sec(&value_sec); + } +} + int ble_hs_pvcy_set_our_irk(const uint8_t *irk) { diff --git a/nimble/host/src/ble_hs_pvcy_priv.h b/nimble/host/src/ble_hs_pvcy_priv.h index 86157da0f..2b0f07b12 100644 --- a/nimble/host/src/ble_hs_pvcy_priv.h +++ b/nimble/host/src/ble_hs_pvcy_priv.h @@ -26,8 +26,9 @@ extern "C" { #endif -extern const uint8_t ble_hs_pvcy_default_irk[16]; +extern uint8_t ble_hs_pvcy_default_irk[16]; +void ble_hs_pvcy_set_default_irk(void); int ble_hs_pvcy_set_our_irk(const uint8_t *irk); int ble_hs_pvcy_our_irk(const uint8_t **out_irk); int ble_hs_pvcy_remove_entry(uint8_t addr_type, const uint8_t *addr); diff --git a/nimble/host/src/ble_hs_startup.c b/nimble/host/src/ble_hs_startup.c index 54c211da8..3b34e9ab8 100644 --- a/nimble/host/src/ble_hs_startup.c +++ b/nimble/host/src/ble_hs_startup.c @@ -349,8 +349,9 @@ ble_hs_startup_reset_tx(void) int ble_hs_startup_go(void) { - struct ble_store_gen_key gen_key; + //struct ble_store_gen_key gen_key; int rc; + uint8_t irk[16]; rc = ble_hs_startup_reset_tx(); if (rc != 0) { @@ -403,7 +404,7 @@ ble_hs_startup_go(void) if (rc != 0) { return rc; } - +#if 0 if (ble_hs_cfg.store_gen_key_cb) { memset(&gen_key, 0, sizeof(gen_key)); rc = ble_hs_cfg.store_gen_key_cb(BLE_STORE_GEN_KEY_IRK, &gen_key, @@ -418,6 +419,10 @@ ble_hs_startup_go(void) if (rc != 0) { ble_hs_pvcy_set_our_irk(NULL); } +#endif + ble_hs_pvcy_set_default_irk(); + + ble_hs_pvcy_set_our_irk(irk); /* If flow control is enabled, configure the controller to use it. */ ble_hs_flow_startup();