Skip to content

Commit a22f6e8

Browse files
Merge pull request apache#79 from andrzej-kaczmarek/ll-trng
nimble: Use TRNG
2 parents 3f8b139 + 0a9320d commit a22f6e8

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

nimble/controller/src/ble_ll_rand.c

+25-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626
#include "nimble/nimble_opt.h"
2727
#include "controller/ble_hw.h"
2828
#include "controller/ble_ll.h"
29+
#if MYNEWT_VAL(TRNG)
30+
#include "trng/trng.h"
31+
#endif
2932

33+
#if MYNEWT_VAL(TRNG)
34+
static struct trng_dev *g_trng;
35+
#else
3036
/* This is a simple circular buffer for holding N samples of random data */
3137
struct ble_ll_rnum_data
3238
{
@@ -61,11 +67,21 @@ ble_ll_rand_sample(uint8_t rnum)
6167
}
6268
OS_EXIT_CRITICAL(sr);
6369
}
70+
#endif
6471

6572
/* Get 'len' bytes of random data */
6673
int
6774
ble_ll_rand_data_get(uint8_t *buf, uint8_t len)
6875
{
76+
#if MYNEWT_VAL(TRNG)
77+
size_t num;
78+
79+
while (len) {
80+
num = trng_read(g_trng, buf, len);
81+
buf += num;
82+
len -= num;
83+
}
84+
#else
6985
uint8_t rnums;
7086
os_sr_t sr;
7187

@@ -100,7 +116,7 @@ ble_ll_rand_data_get(uint8_t *buf, uint8_t len)
100116
}
101117
}
102118
}
103-
119+
#endif
104120
return BLE_ERR_SUCCESS;
105121
}
106122

@@ -138,10 +154,14 @@ ble_ll_rand_prand_get(uint8_t *prand)
138154
int
139155
ble_ll_rand_start(void)
140156
{
157+
#if MYNEWT_VAL(TRNG)
158+
/* Nothing to do - this is handled by driver */
159+
#else
141160
/* Start the generation of numbers if we are not full */
142161
if (g_ble_ll_rnum_data.rnd_size < MYNEWT_VAL(BLE_LL_RNG_BUFSIZE)) {
143162
ble_hw_rng_start();
144163
}
164+
#endif
145165
return 0;
146166
}
147167

@@ -154,8 +174,12 @@ ble_ll_rand_start(void)
154174
int
155175
ble_ll_rand_init(void)
156176
{
177+
#if MYNEWT_VAL(TRNG)
178+
g_trng = (struct trng_dev *) os_dev_open("trng", OS_TIMEOUT_NEVER, NULL);
179+
#else
157180
g_ble_ll_rnum_data.rnd_in = g_ble_ll_rnum_buf;
158181
g_ble_ll_rnum_data.rnd_out = g_ble_ll_rnum_buf;
159182
ble_hw_rng_init(ble_ll_rand_sample, 1);
183+
#endif
160184
return 0;
161185
}

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)