Skip to content

Commit 26171af

Browse files
committed
Preserve CRYPTOCELL_ENABLE; don't hardcode RNG FIFO depth
1 parent dd226ca commit 26171af

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

hal/nrf5340.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,11 @@ static void hal_spu_init(void)
180180
}
181181

182182
#ifdef WOLFCRYPT_SECURE_MODE
183+
static uint32_t cryptocell_enable_prev = 0;
184+
183185
void hal_trng_init(void)
184186
{
187+
cryptocell_enable_prev = CRYPTOCELL_ENABLE;
185188
CRYPTOCELL_ENABLE = 1;
186189
CC_RNG_CLK = 1;
187190
CC_RNG_SW_RESET = 1;
@@ -201,26 +204,24 @@ void hal_trng_fini(void)
201204
CC_RNG_CLK = 0;
202205
CC_RNG_SW_RESET = 1;
203206

204-
/* TODO: it might be a bad idea to disable the whole CryptoCell engine in
205-
* case other parts of it are used -- that is not the case right now */
206-
CRYPTOCELL_ENABLE = 0;
207+
CRYPTOCELL_ENABLE = cryptocell_enable_prev;
207208
}
208209

209210
int hal_trng_get_entropy(unsigned char *out, unsigned int len)
210211
{
211212
unsigned int i = 0;
212213

213214
while (i < len) {
214-
uint32_t data[6];
215+
uint32_t data[CC_RNG_EHR_DATA_LEN];
215216
uint8_t *data_bytes = (uint8_t *)data;
216217
unsigned int word, byte;
217218

218219
while (!((CC_RNG_ISR & 0x01) && (CC_RNG_TRNG_VALID & 0x01))) {}
219220

220-
for (word = 0; word < 6; word++) {
221+
for (word = 0; word < CC_RNG_EHR_DATA_LEN; word++) {
221222
data[word] = CC_RNG_EHR_DATA(word);
222223
}
223-
for (byte = 0; byte < 24 && i < len; byte++) {
224+
for (byte = 0; byte < 4 * CC_RNG_EHR_DATA_LEN && i < len; byte++) {
224225
out[i++] = (unsigned char)data_bytes[byte];
225226
}
226227
}

hal/nrf5340.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ void uart_write_sz(const char* c, unsigned int sz);
465465
#define CC_RNG_TRNG_CONFIG (*(volatile uint32_t *)(CC_RNG_BASE + 0x10C))
466466
#define CC_RNG_TRNG_VALID (*(volatile uint32_t *)(CC_RNG_BASE + 0x110))
467467
#define CC_RNG_EHR_DATA(n) (*(volatile uint32_t *)(CC_RNG_BASE + 0x114 + (n) * 0x4))
468+
#define CC_RNG_EHR_DATA_LEN 6
468469
#define CC_RNG_NOISE_SOURCE (*(volatile uint32_t *)(CC_RNG_BASE + 0x12C))
469470
#define CC_RNG_SAMPLE_CNT (*(volatile uint32_t *)(CC_RNG_BASE + 0x130))
470471
#define CC_RNG_AUTOCORR_STATISTIC (*(volatile uint32_t *)(CC_RNG_BASE + 0x134))

0 commit comments

Comments
 (0)