From 548e4f3de32053abe44854c35882bbc354f0538a Mon Sep 17 00:00:00 2001 From: arun-silabs <141724790+arun-silabs@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:15:50 +0530 Subject: [PATCH] [Silabs] Added code to random address generation using hardware API's. (#31277) * Added code to random address generation using hardware API's. * Made changes as per review comments * Restyled by clang-format * Made trng key array constant as per comments. --------- Co-authored-by: Restyled.io --- .../silabs/SiWx917/SiWx917/sl_wifi_if.c | 24 ++++++++++++++++ src/platform/silabs/rs911x/BLEManagerImpl.cpp | 28 ++++++++++++++++--- src/platform/silabs/rs911x/rsi_ble_config.h | 1 + third_party/silabs/SiWx917_sdk.gni | 5 ++++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.c b/examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.c index 7655f6b48a5c59..6e91f2a8c52fa7 100644 --- a/examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.c +++ b/examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.c @@ -52,6 +52,11 @@ #define ADV_MULTIPROBE 1 #define ADV_SCAN_PERIODICITY 10 +#ifdef SIWX_917 +#include "sl_si91x_trng.h" +#define TRNGKEY_SIZE 4 +#endif // SIWX_917 + struct wfx_rsi wfx_rsi; /* Declare a variable to hold the data associated with the created event group. */ @@ -301,6 +306,25 @@ static sl_status_t wfx_rsi_init(void) SILABS_LOG("sl_wifi_get_mac_address failed: %x", status); return status; } +#ifdef SIWX_917 + const uint32_t trngKey[TRNGKEY_SIZE] = { 0x16157E2B, 0xA6D2AE28, 0x8815F7AB, 0x3C4FCF09 }; + + // To check the Entropy of TRNG and verify TRNG functioning. + status = sl_si91x_trng_entropy(); + if (status != SL_STATUS_OK) + { + SILABS_LOG("TRNG Entropy Failed"); + return status; + } + + // Initiate and program the key required for TRNG hardware engine + status = sl_si91x_trng_program_key(trngKey, TRNGKEY_SIZE); + if (status != SL_STATUS_OK) + { + SILABS_LOG("TRNG Key Programming Failed"); + return status; + } +#endif // SIWX_917 wfx_rsi.events = xEventGroupCreateStatic(&rsiDriverEventGroup); wfx_rsi.dev_state |= WFX_RSI_ST_DEV_READY; osSemaphoreRelease(sl_rs_ble_init_sem); diff --git a/src/platform/silabs/rs911x/BLEManagerImpl.cpp b/src/platform/silabs/rs911x/BLEManagerImpl.cpp index 343acf6f50ac84..ca0e489616f368 100644 --- a/src/platform/silabs/rs911x/BLEManagerImpl.cpp +++ b/src/platform/silabs/rs911x/BLEManagerImpl.cpp @@ -58,6 +58,12 @@ extern "C" { #include #include +#ifdef SIWX_917 +extern "C" { +#include "sl_si91x_trng.h" +} +#endif // SIWX_917 + #if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING #include #endif @@ -78,8 +84,23 @@ using namespace ::chip::DeviceLayer::Internal; void sl_ble_init() { - uint8_t randomAddrBLE[6] = { 0 }; - uint64_t randomAddr = chip::Crypto::GetRandU64(); + uint8_t randomAddrBLE[RSI_BLE_ADDR_LENGTH] = { 0 }; +#if SIWX_917 + sl_status_t sl_status; + //! Get Random number of desired length + sl_status = sl_si91x_trng_get_random_num((uint32_t *) randomAddrBLE, RSI_BLE_ADDR_LENGTH); + if (sl_status != SL_STATUS_OK) + { + ChipLogError(DeviceLayer, " TRNG Random number generation Failed "); + return; + } + // Set the two least significant bits as the first 2 bits of the address has to be '11' to ensure the address is a random + // non-resolvable private address + randomAddrBLE[5] |= 0xC0; +#else + uint64_t randomAddr = chip::Crypto::GetRandU64(); + memcpy(randomAddrBLE, &randomAddr, RSI_BLE_ADDR_LENGTH); +#endif // SIWX_917 // registering the GAP callback functions rsi_ble_gap_register_callbacks(NULL, NULL, rsi_ble_on_disconnect_event, NULL, NULL, NULL, rsi_ble_on_enhance_conn_status_event, @@ -93,10 +114,9 @@ void sl_ble_init() // Exchange of GATT info with BLE stack rsi_ble_add_matter_service(); - // initializing the application events map rsi_ble_app_init_events(); - memcpy(randomAddrBLE, &randomAddr, 6); + rsi_ble_set_random_address_with_value(randomAddrBLE); chip::DeviceLayer::Internal::BLEMgrImpl().HandleBootEvent(); } diff --git a/src/platform/silabs/rs911x/rsi_ble_config.h b/src/platform/silabs/rs911x/rsi_ble_config.h index 9bc475a0a70131..e58cadcc77dd4c 100644 --- a/src/platform/silabs/rs911x/rsi_ble_config.h +++ b/src/platform/silabs/rs911x/rsi_ble_config.h @@ -57,6 +57,7 @@ #define RSI_BLE_DEV_NAME "CCP_DEVICE" #define RSI_BLE_SET_RAND_ADDR "00:23:A7:12:34:56" #define RSI_BLE_EVENT_GATT_RD (0x08) +#define RSI_BLE_ADDR_LENGTH 6 #define CLEAR_WHITELIST (0x00) #define ADD_DEVICE_TO_WHITELIST (0x01) diff --git a/third_party/silabs/SiWx917_sdk.gni b/third_party/silabs/SiWx917_sdk.gni index f66011f4d180e1..5e14a44ac090a3 100644 --- a/third_party/silabs/SiWx917_sdk.gni +++ b/third_party/silabs/SiWx917_sdk.gni @@ -57,6 +57,10 @@ template("siwx917_sdk") { # ble component "${wifi_sdk_root}/components/device/silabs/si91x/wireless/ble/inc", + # trng component + "${wifi_sdk_root}/components/device/silabs/si91x/wireless/crypto/inc", + "${wifi_sdk_root}/components/device/silabs/si91x/wireless/crypto/trng/inc", + # si91x component "${wifi_sdk_root}/components/device/silabs/si91x/wireless/ahb_interface/inc", "${wifi_sdk_root}/components/device/silabs/si91x/wireless/inc", @@ -337,6 +341,7 @@ template("siwx917_sdk") { "${wifi_sdk_root}/components/device/silabs/si91x/wireless/ble/src/rsi_common_apis.c", "${wifi_sdk_root}/components/device/silabs/si91x/wireless/ble/src/rsi_utils.c", "${wifi_sdk_root}/components/device/silabs/si91x/wireless/ble/src/sl_si91x_ble.c", + "${wifi_sdk_root}/components/device/silabs/si91x/wireless/crypto/trng/src/sl_si91x_trng.c", # si91x_basic_buffers component "${wifi_sdk_root}/components/board/silabs/src/rsi_board.c",