sys/random: add option to use HWRNG as source of randomness

Add the `prng_hwrng` module to enable the HWRNG as source of all randomness, not just
for seeding a PRNG.

saves ~260 bytes compared to using tinymt32.
This commit is contained in:
Benjamin Valentin 2020-07-07 15:34:52 +02:00 committed by Benjamin Valentin
parent db5070c772
commit 2f423473fc
3 changed files with 45 additions and 0 deletions

View File

@ -786,6 +786,10 @@ ifneq (,$(filter random,$(USEMODULE)))
USEMODULE += hashes
endif
ifneq (,$(filter prng_hwrng,$(USEMODULE)))
FEATURES_REQUIRED += periph_hwrng
endif
ifeq (,$(filter puf_sram,$(USEMODULE)))
FEATURES_OPTIONAL += periph_hwrng
endif

View File

@ -22,6 +22,9 @@
* - Simple Park-Miller PRNG
* - Musl C PRNG
* - Fortuna (CS)PRNG
* - Hardware Random Number Generator (non-seedable)
* HWRNG differ in how they generate random numbers and may not use a PRNG internally.
* Refer to the manual of your MCU for details.
*/
#ifndef RANDOM_H

38
sys/random/hwrng.c Normal file
View File

@ -0,0 +1,38 @@
/**
* Copyright (C) 2020 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys_random
* @{
* @file
*
* @brief Use the HWRNG as source of randomness
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
* @}
*/
#include "kernel_defines.h"
#include "periph/hwrng.h"
#include "random.h"
uint32_t random_uint32(void)
{
uint32_t rnd;
hwrng_read(&rnd, sizeof(rnd));
return rnd;
}
void random_init(uint32_t val)
{
(void) val;
if (!IS_ACTIVE(MODULE_PERIPH_INIT_HWRNG)) {
hwrng_init();
}
}