Skip to content

Commit a3bb9c3

Browse files
[Silabs] Refactor SiWx917 random number generator (#33689)
* Refactor TRNG function to rely on hardware instead of SW on TINYCRYPT * fix pointer conversion * refactor minimal changes * Rever header change * review comments --------- Co-authored-by: Rohan S <3526930+brosahay@users.noreply.github.com>
1 parent fab01d8 commit a3bb9c3

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ extern "C" {
7070
#include "sl_wifi.h"
7171
#include "sl_wifi_callback_framework.h"
7272
#include "wfx_host_events.h"
73-
#if SLI_SI91X_MCU_INTERFACE
73+
#if TINYCRYPT_PRIMITIVES
7474
#include "sl_si91x_trng.h"
7575
#define TRNGKEY_SIZE 4
76-
#endif // SLI_SI91X_MCU_INTERFACE
77-
} // extern "C" {
76+
#endif // TINYCRYPT_PRIMITIVES
77+
}
7878

7979
WfxRsi_t wfx_rsi;
8080

@@ -477,7 +477,7 @@ static sl_status_t wfx_rsi_init(void)
477477
return status;
478478
}
479479

480-
#ifdef SLI_SI91X_MCU_INTERFACE
480+
#ifdef TINYCRYPT_PRIMITIVES
481481
const uint32_t trngKey[TRNGKEY_SIZE] = { 0x16157E2B, 0xA6D2AE28, 0x8815F7AB, 0x3C4FCF09 };
482482

483483
// To check the Entropy of TRNG and verify TRNG functioning.
@@ -495,7 +495,7 @@ static sl_status_t wfx_rsi_init(void)
495495
SILABS_LOG("TRNG Key Programming Failed");
496496
return status;
497497
}
498-
#endif // SLI_SI91X_MCU_INTERFACE
498+
#endif // TINYCRYPT_PRIMITIVES
499499

500500
wfx_rsi.events = xEventGroupCreateStatic(&rsiDriverEventGroup);
501501
wfx_rsi.dev_state |= WFX_RSI_ST_DEV_READY;

src/platform/silabs/PlatformManagerImpl.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
#if defined(TINYCRYPT_PRIMITIVES)
3535
#include "tinycrypt/ecc.h"
36-
#endif
36+
#endif // TINYCRYPT_PRIMITIVES
3737

3838
#if CHIP_SYSTEM_CONFIG_USE_LWIP
3939
#include <lwip/tcpip.h>
@@ -46,7 +46,6 @@ namespace DeviceLayer {
4646

4747
PlatformManagerImpl PlatformManagerImpl::sInstance;
4848

49-
#if SLI_SI91X_MCU_INTERFACE
5049
#if defined(TINYCRYPT_PRIMITIVES)
5150
sys_mutex_t PlatformManagerImpl::rngMutexHandle = NULL;
5251

@@ -58,8 +57,8 @@ int PlatformManagerImpl::uECC_RNG_Function(uint8_t * dest, unsigned int size)
5857

5958
return res;
6059
}
61-
#endif // TINYCRYPT_PRIMITIVES
6260

61+
#if !(SLI_SI91X_MCU_INTERFACE)
6362
static void app_get_random(uint8_t * aOutput, size_t aLen)
6463
{
6564
VerifyOrReturn(aOutput != nullptr);
@@ -76,7 +75,8 @@ static int app_entropy_source(void * data, unsigned char * output, size_t len, s
7675

7776
return 0;
7877
}
79-
#endif // SLI_SI91X_MCU_INTERFACE
78+
#endif // !SLI_SI91X_MCU_INTERFACE
79+
#endif // TINYCRYPT_PRIMITIVES
8080

8181
CHIP_ERROR PlatformManagerImpl::_InitChipStack(void)
8282
{
@@ -93,15 +93,14 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void)
9393

9494
ReturnErrorOnFailure(System::Clock::InitClock_RealTime());
9595

96-
#if SLI_SI91X_MCU_INTERFACE
97-
ReturnErrorOnFailure(chip::Crypto::add_entropy_source(app_entropy_source, NULL, 16 /*Threshold value*/));
98-
9996
#if defined(TINYCRYPT_PRIMITIVES)
97+
#if !(SLI_SI91X_MCU_INTERFACE)
98+
ReturnErrorOnFailure(chip::Crypto::add_entropy_source(app_entropy_source, NULL, 16 /*Threshold value*/));
99+
#endif // !SLI_SI91X_MCU_INTERFACE
100100
/* Set RNG function for tinycrypt operations. */
101101
VerifyOrExit(sys_mutex_new(&rngMutexHandle) == ERR_OK, err = CHIP_ERROR_NO_MEMORY);
102102
uECC_set_rng(PlatformManagerImpl::uECC_RNG_Function);
103103
#endif // TINYCRYPT_PRIMITIVES
104-
#endif // SLI_SI91X_MCU_INTERFACE
105104

106105
// Call _InitChipStack() on the generic implementation base class
107106
// to finish the initialization process.

src/platform/silabs/SiWx917/CHIPCryptoPALTinyCrypt.cpp

+21-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@
6060

6161
#include <string.h>
6262

63+
#ifdef SLI_SI91X_MCU_INTERFACE
64+
extern "C" {
65+
#include "sl_si91x_trng.h"
66+
}
67+
#endif // SLI_SI91X_MCU_INTERFACE
68+
6369
namespace chip {
6470
namespace Crypto {
6571

@@ -414,7 +420,7 @@ CHIP_ERROR PBKDF2_sha256::pbkdf2_sha256(const uint8_t * password, size_t plen, c
414420

415421
return error;
416422
}
417-
423+
#if !(SLI_SI91X_MCU_INTERFACE)
418424
static EntropyContext * get_entropy_context()
419425
{
420426
if (!gsEntropyContext.mInitialized)
@@ -448,9 +454,15 @@ static mbedtls_ctr_drbg_context * get_drbg_context()
448454

449455
return drbgCtxt;
450456
}
451-
457+
#endif // !SLI_SI91X_MCU_INTERFACE
452458
CHIP_ERROR add_entropy_source(entropy_source fn_source, void * p_source, size_t threshold)
453459
{
460+
#if SLI_SI91X_MCU_INTERFACE
461+
// SiWx917 has its hardware based generator
462+
(void) fn_source;
463+
(void) p_source;
464+
(void) threshold;
465+
#else
454466
VerifyOrReturnError(fn_source != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
455467

456468
EntropyContext * const entropy_ctxt = get_entropy_context();
@@ -459,19 +471,25 @@ CHIP_ERROR add_entropy_source(entropy_source fn_source, void * p_source, size_t
459471
const int result =
460472
mbedtls_entropy_add_source(&entropy_ctxt->mEntropy, fn_source, p_source, threshold, MBEDTLS_ENTROPY_SOURCE_STRONG);
461473
VerifyOrReturnError(result == 0, CHIP_ERROR_INTERNAL);
474+
#endif // SLI_SI91X_MCU_INTERFACE
462475
return CHIP_NO_ERROR;
463476
}
464477

465478
CHIP_ERROR DRBG_get_bytes(uint8_t * out_buffer, const size_t out_length)
466479
{
467480
VerifyOrReturnError(out_buffer != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
468481
VerifyOrReturnError(out_length > 0, CHIP_ERROR_INVALID_ARGUMENT);
469-
482+
#if SLI_SI91X_MCU_INTERFACE
483+
sl_status_t status;
484+
status = sl_si91x_trng_get_random_num(reinterpret_cast<uint32_t *>(out_buffer), out_length);
485+
VerifyOrReturnError(status == SL_STATUS_OK, CHIP_ERROR_RANDOM_DATA_UNAVAILABLE);
486+
#else
470487
mbedtls_ctr_drbg_context * const drbg_ctxt = get_drbg_context();
471488
VerifyOrReturnError(drbg_ctxt != nullptr, CHIP_ERROR_INTERNAL);
472489

473490
const int result = mbedtls_ctr_drbg_random(drbg_ctxt, Uint8::to_uchar(out_buffer), out_length);
474491
VerifyOrReturnError(result == 0, CHIP_ERROR_INTERNAL);
492+
#endif // SLI_SI91X_MCU_INTERFACE
475493

476494
return CHIP_NO_ERROR;
477495
}

src/platform/silabs/rs911x/BLEManagerImpl.cpp

+3-20
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ extern "C" {
5858
#include <platform/DeviceInstanceInfoProvider.h>
5959
#include <string.h>
6060

61-
#ifdef SLI_SI91X_MCU_INTERFACE
62-
extern "C" {
63-
#include "sl_si91x_trng.h"
64-
}
65-
#endif // SLI_SI91X_MCU_INTERFACE
66-
6761
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
6862
#include <setup_payload/AdditionalDataPayloadGenerator.h>
6963
#endif
@@ -92,22 +86,11 @@ using namespace ::chip::DeviceLayer::Internal;
9286
void sl_ble_init()
9387
{
9488
uint8_t randomAddrBLE[RSI_BLE_ADDR_LENGTH] = { 0 };
95-
#if SLI_SI91X_MCU_INTERFACE
96-
sl_status_t sl_status;
97-
//! Get Random number of desired length
98-
sl_status = sl_si91x_trng_get_random_num((uint32_t *) randomAddrBLE, RSI_BLE_ADDR_LENGTH);
99-
if (sl_status != SL_STATUS_OK)
100-
{
101-
ChipLogError(DeviceLayer, " TRNG Random number generation Failed ");
102-
return;
103-
}
89+
uint64_t randomAddr = chip::Crypto::GetRandU64();
90+
memcpy(randomAddrBLE, &randomAddr, RSI_BLE_ADDR_LENGTH);
10491
// 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
10592
// non-resolvable private address
106-
randomAddrBLE[5] |= 0xC0;
107-
#else
108-
uint64_t randomAddr = chip::Crypto::GetRandU64();
109-
memcpy(randomAddrBLE, &randomAddr, RSI_BLE_ADDR_LENGTH);
110-
#endif // SLI_SI91X_MCU_INTERFACE
93+
randomAddrBLE[(RSI_BLE_ADDR_LENGTH - 1)] |= 0xC0;
11194

11295
// registering the GAP callback functions
11396
rsi_ble_gap_register_callbacks(NULL, NULL, rsi_ble_on_disconnect_event, NULL, NULL, NULL, rsi_ble_on_enhance_conn_status_event,

0 commit comments

Comments
 (0)