1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-23 16:08:17 +00:00
trezor-firmware/embed/trezorhal/rng.c

38 lines
1.2 KiB
C
Raw Normal View History

2017-03-07 13:50:09 +00:00
#include STM32_HAL_H
2017-10-14 16:06:16 +00:00
#pragma GCC optimize("no-stack-protector") // applies to all functions in this file
void rng_init(void)
2017-10-06 09:11:43 +00:00
{
// enable TRNG peripheral clock
// use the HAL version due to section 2.1.6 of STM32F42xx Errata sheet
// "Delay after an RCC peripheral clock enabling"
__HAL_RCC_RNG_CLK_ENABLE();
2017-10-06 09:11:43 +00:00
RNG->CR = RNG_CR_RNGEN; // enable TRNG
2017-03-07 13:50:09 +00:00
}
2017-10-06 09:11:43 +00:00
uint32_t rng_read(const uint32_t previous, const uint32_t compare_previous)
{
uint32_t temp = previous;
do {
while ((RNG->SR & (RNG_SR_SECS | RNG_SR_CECS | RNG_SR_DRDY)) != RNG_SR_DRDY) {
; // wait until TRNG is ready
}
temp = RNG->DR; // read the data from the TRNG
} while (compare_previous && (temp == previous)); // RM0090 section 24.3.1 FIPS continuous random number generator test
return temp;
}
uint32_t rng_get(void)
{
// reason for keeping history: RM0090 section 24.3.1 FIPS continuous random number generator test
static uint32_t previous = 0, current = 0;
if (previous == current) {
previous = rng_read(previous, 0);
} else {
previous = current;
}
current = rng_read(previous, 1);
return current;
2017-03-07 13:50:09 +00:00
}