Skip to content

Commit 0a9320d

Browse files
nimble/host: Use TRNG driver if available
If TRNG is enabled, let's use it directly instead of going through HCI. As a bonus, this way uECC can be used even before NimBLE is synced with controller.
1 parent 47a3ce2 commit 0a9320d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

nimble/host/src/ble_sm_alg.c

+22
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
#if MYNEWT_VAL(BLE_SM_SC)
3636
#include "tinycrypt/cmac_mode.h"
3737
#include "tinycrypt/ecc_dh.h"
38+
#if MYNEWT_VAL(TRNG)
39+
#include "trng/trng.h"
40+
#endif
41+
#endif
42+
43+
#if MYNEWT_VAL(BLE_SM_SC) && MYNEWT_VAL(TRNG)
44+
static struct trng_dev *g_trng;
3845
#endif
3946

4047
static void
@@ -473,9 +480,24 @@ ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv)
473480
static int
474481
ble_sm_alg_rand(uint8_t *dst, unsigned int size)
475482
{
483+
#if MYNEWT_VAL(TRNG)
484+
size_t num;
485+
486+
if (!g_trng) {
487+
g_trng = (struct trng_dev *)os_dev_open("trng", OS_WAIT_FOREVER, NULL);
488+
assert(g_trng);
489+
}
490+
491+
while (size) {
492+
num = trng_read(g_trng, dst, size);
493+
dst += num;
494+
size -= num;
495+
}
496+
#else
476497
if (ble_hs_hci_util_rand(dst, size)) {
477498
return 0;
478499
}
500+
#endif
479501

480502
return 1;
481503
}

0 commit comments

Comments
 (0)