From 0a9320d84d86f4c8970895bb34ae7debf1acff7d Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Fri, 4 May 2018 13:55:44 +0200 Subject: [PATCH] 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. --- nimble/host/src/ble_sm_alg.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/nimble/host/src/ble_sm_alg.c b/nimble/host/src/ble_sm_alg.c index 91639d9e2..6698e2b0f 100644 --- a/nimble/host/src/ble_sm_alg.c +++ b/nimble/host/src/ble_sm_alg.c @@ -35,6 +35,13 @@ #if MYNEWT_VAL(BLE_SM_SC) #include "tinycrypt/cmac_mode.h" #include "tinycrypt/ecc_dh.h" +#if MYNEWT_VAL(TRNG) +#include "trng/trng.h" +#endif +#endif + +#if MYNEWT_VAL(BLE_SM_SC) && MYNEWT_VAL(TRNG) +static struct trng_dev *g_trng; #endif static void @@ -473,9 +480,24 @@ ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv) static int ble_sm_alg_rand(uint8_t *dst, unsigned int size) { +#if MYNEWT_VAL(TRNG) + size_t num; + + if (!g_trng) { + g_trng = (struct trng_dev *)os_dev_open("trng", OS_WAIT_FOREVER, NULL); + assert(g_trng); + } + + while (size) { + num = trng_read(g_trng, dst, size); + dst += num; + size -= num; + } +#else if (ble_hs_hci_util_rand(dst, size)) { return 0; } +#endif return 1; }