diff --git a/Makefile.dep b/Makefile.dep index 783119f4d3..eb4e8a138d 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -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 diff --git a/sys/include/random.h b/sys/include/random.h index 252a8379cf..dfd35a15fa 100644 --- a/sys/include/random.h +++ b/sys/include/random.h @@ -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 diff --git a/sys/random/hwrng.c b/sys/random/hwrng.c new file mode 100644 index 0000000000..8bde81b68c --- /dev/null +++ b/sys/random/hwrng.c @@ -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 + * @} + */ + +#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(); + } +}