From 53d485828ce4c0025fec73ff39931e65e9440392 Mon Sep 17 00:00:00 2001 From: Marcelo Aquino Date: Tue, 23 May 2017 20:34:59 -0300 Subject: [PATCH] Linux: Fix hwRandomNumberInit() (#861) Fix #860. Makes hwRandomNumberInit() independent of hwInit(). --- hal/architecture/MyHwLinuxGeneric.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/hal/architecture/MyHwLinuxGeneric.cpp b/hal/architecture/MyHwLinuxGeneric.cpp index c6ac805..8c6649d 100644 --- a/hal/architecture/MyHwLinuxGeneric.cpp +++ b/hal/architecture/MyHwLinuxGeneric.cpp @@ -25,12 +25,11 @@ #include #include #include -#include #include "SoftEeprom.h" #include "log.h" static SoftEeprom eeprom = SoftEeprom(MY_LINUX_CONFIG_FILE, 1024); // ATMega328 has 1024 bytes -static FILE *randomFp; +static FILE *randomFp = NULL; bool hwInit(void) { @@ -43,10 +42,7 @@ bool hwInit(void) } #endif #endif - if (!(randomFp = fopen("/dev/urandom", "r"))) { - logError("Cannot open '/dev/urandom'.\n"); - exit(2); - } + return true; } @@ -73,6 +69,15 @@ void hwWriteConfig(int addr, uint8_t value) void hwRandomNumberInit() { unsigned long seed=0; + + if (randomFp != NULL) { + fclose(randomFp); + } + if (!(randomFp = fopen("/dev/urandom", "r"))) { + logError("Cannot open '/dev/urandom'.\n"); + exit(2); + } + while (hwGetentropy(&seed, sizeof(seed)) != sizeof(seed)); randomSeed(seed); }