Linux: Fix hwRandomNumberInit() (#861)

Fix #860.
Makes hwRandomNumberInit() independent of hwInit().
This commit is contained in:
Marcelo Aquino
2017-05-23 20:34:59 -03:00
committed by GitHub
parent aeceb0b683
commit 53d485828c
+11 -6
View File
@@ -25,12 +25,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <syscall.h> #include <syscall.h>
#include <unistd.h> #include <unistd.h>
#include <time.h>
#include "SoftEeprom.h" #include "SoftEeprom.h"
#include "log.h" #include "log.h"
static SoftEeprom eeprom = SoftEeprom(MY_LINUX_CONFIG_FILE, 1024); // ATMega328 has 1024 bytes static SoftEeprom eeprom = SoftEeprom(MY_LINUX_CONFIG_FILE, 1024); // ATMega328 has 1024 bytes
static FILE *randomFp; static FILE *randomFp = NULL;
bool hwInit(void) bool hwInit(void)
{ {
@@ -43,10 +42,7 @@ bool hwInit(void)
} }
#endif #endif
#endif #endif
if (!(randomFp = fopen("/dev/urandom", "r"))) {
logError("Cannot open '/dev/urandom'.\n");
exit(2);
}
return true; return true;
} }
@@ -73,6 +69,15 @@ void hwWriteConfig(int addr, uint8_t value)
void hwRandomNumberInit() void hwRandomNumberInit()
{ {
unsigned long seed=0; 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)); while (hwGetentropy(&seed, sizeof(seed)) != sizeof(seed));
randomSeed(seed); randomSeed(seed);
} }