rng updates (#24)
parent
a381819ad1
commit
f4e72aa4cc
@ -1,15 +1,33 @@
|
||||
#include STM32_HAL_H
|
||||
|
||||
static RNG_HandleTypeDef rng_handle = {
|
||||
.Instance = RNG,
|
||||
};
|
||||
|
||||
int rng_init(void) {
|
||||
__HAL_RCC_RNG_CLK_ENABLE();
|
||||
HAL_RNG_Init(&rng_handle);
|
||||
int rng_init(void)
|
||||
{
|
||||
RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN; // enable TRNG peripheral clock
|
||||
RNG->CR = RNG_CR_RNGEN; // enable TRNG
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t rng_get(void) {
|
||||
return HAL_RNG_GetRandomNumber(&rng_handle);
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in new issue