[riot-notifications] [RIOT-OS/RIOT] sys/random/fortune: change interval ressed to ms (#16594)

Francisco notifications at github.com
Tue Jun 29 13:10:56 CEST 2021


### Contribution description

`fortuna` suggests a 100ms reseed interval, therefore msec resolution should be enough for the reseed value which. This PR also uses a timer to set a flag when a reseed is necessary this allows using `ztimer` without the need of including `now64` and ticks an item off https://github.com/RIOT-OS/RIOT/issues/13667.

To do that I realized that Fortuna is actually relying on an xtimer call before the module is initialized, therefore I had to move timer initialization up, I'll split this part out.

### Testing procedure

I applied the following patch:

```diff
diff --git a/examples/hello-world/Makefile b/examples/hello-world/Makefile
index 258d8e9baf..f389bea7ca 100644
--- a/examples/hello-world/Makefile
+++ b/examples/hello-world/Makefile
@@ -15,4 +15,11 @@ DEVELHELP ?= 1
 # Change this to 0 show compiler invocation lines by default:
 QUIET ?= 1

+USEMODULE += xtimer
+USEMODULE += prng_fortuna
+USEMODULE += fortuna_reseed
+USEMODULE += random
+
+CFLAGS=-DFORTUNA_RESEED_INTERVAL_MS=1000
+
 include $(RIOTBASE)/Makefile.include
diff --git a/examples/hello-world/main.c b/examples/hello-world/main.c
index f51bf8c0a0..168cbdcfdc 100644
--- a/examples/hello-world/main.c
+++ b/examples/hello-world/main.c
@@ -20,6 +20,8 @@
  */

 #include <stdio.h>
+#include "xtimer.h"
+#include "random.h"

 int main(void)
 {
@@ -28,5 +30,10 @@ int main(void)
     printf("You are running RIOT on a(n) %s board.\n", RIOT_BOARD);
     printf("This board features a(n) %s MCU.\n", RIOT_MCU);

+    while(1) {
+        printf("random number: %"PRIu32"\n", random_uint32());
+        xtimer_usleep(2000000);
+    }
+
     return 0;
 }
diff --git a/sys/random/fortuna/fortuna.c b/sys/random/fortuna/fortuna.c
index ac096eb360..5f9bcc2a58 100644
--- a/sys/random/fortuna/fortuna.c
+++ b/sys/random/fortuna/fortuna.c
@@ -199,8 +199,8 @@ int fortuna_random_data(fortuna_state_t *state, uint8_t *out, size_t bytes)

     /* reseed the generator if needed, before returning data */
 #if FORTUNA_RESEED_INTERVAL_MS > 0 && IS_USED(MODULE_FORTUNA_RESEED)
-    if (state->pools[0].len >= FORTUNA_MIN_POOL_SIZE &&
-        atomic_load_u8(&state->reseed)) {
+    if (state-atomic_load_u8(&state->reseed)) {
+        puts("reseed");
 #else
     if (state->pools[0].len >= FORTUNA_MIN_POOL_SIZE) {
 #endif

```

- ` make -C examples/hello-world/ all term`

```
main(): This is RIOT! (Version: 2021.07-devel-485-g63b849-pr_sys_fortuna_reseed_ms)
Hello World!
You are running RIOT on a(n) native board.
This board features a(n) native MCU.
reseed
random number: 3113564991
reseed
random number: 984556921
reseed
random number: 44374394
reseed
random number: 4139781756
reseed
random number: 3529685450
reseed
random number: 2868101182

```

### Issues/PRs references

Ticks an item in https://github.com/RIOT-OS/RIOT/issues/13667

You can view, comment on, or merge this pull request online at:

  https://github.com/RIOT-OS/RIOT/pull/16594

-- Commit Summary --

  * sys/fortuna: remove need for now64
  * sys/auto_init: init ztimer/xtimer before random

-- File Changes --

    M makefiles/pseudomodules.inc.mk (1)
    M sys/Makefile.dep (5)
    M sys/auto_init/auto_init.c (12)
    M sys/random/Kconfig (10)
    M sys/random/fortuna.c (1)
    M sys/random/fortuna/fortuna.c (45)
    M sys/random/fortuna/fortuna.h (27)

-- Patch Links --

https://github.com/RIOT-OS/RIOT/pull/16594.patch
https://github.com/RIOT-OS/RIOT/pull/16594.diff

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/16594
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20210629/f7bb3550/attachment.htm>


More information about the notifications mailing list