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.
This commit is contained in:
Andrzej Kaczmarek
2018-05-04 14:00:03 +02:00
parent 47a3ce26f2
commit 0a9320d84d
+22
View File
@@ -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;
}