1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 14:58:09 +00:00

emulator: support /dev/{u,}random configuration at compile time; default is still /dev/urandom (#396)

This commit is contained in:
Yannick Heneault 2018-08-10 09:40:08 -04:00 committed by Pavol Rusnak
parent cf83a97c84
commit df5348fb14
2 changed files with 21 additions and 9 deletions

View File

@ -98,6 +98,10 @@ CFLAGS += -I/usr/include/SDL2 -D_REENTRANT
LDLIBS += -lSDL2 LDLIBS += -lSDL2
endif endif
ifdef RANDOM_DEV_FILE
CFLAGS += -DRANDOM_DEV_FILE=\"$(RANDOM_DEV_FILE)\"
endif
else else
ifdef APPVER ifdef APPVER
CFLAGS += -DAPPVER=$(APPVER) CFLAGS += -DAPPVER=$(APPVER)

View File

@ -34,11 +34,15 @@
#define EMULATOR_FLASH_FILE "emulator.img" #define EMULATOR_FLASH_FILE "emulator.img"
#ifndef RANDOM_DEV_FILE
#define RANDOM_DEV_FILE "/dev/urandom"
#endif
uint8_t *emulator_flash_base = NULL; uint8_t *emulator_flash_base = NULL;
uint32_t __stack_chk_guard; uint32_t __stack_chk_guard;
static int urandom = -1; static int random_fd = -1;
static void setup_urandom(void); static void setup_urandom(void);
static void setup_flash(void); static void setup_flash(void);
@ -53,17 +57,21 @@ void __attribute__((noreturn)) shutdown(void) {
} }
void emulatorRandom(void *buffer, size_t size) { void emulatorRandom(void *buffer, size_t size) {
ssize_t n = read(urandom, buffer, size); ssize_t n, len = 0;
if (n < 0 || ((size_t) n) != size) { do {
perror("Failed to read /dev/urandom"); n = read(random_fd, (char*)buffer + len, size - len);
if (n < 0) {
perror("Failed to read " RANDOM_DEV_FILE);
exit(1); exit(1);
} }
len += n;
} while (len != (ssize_t)size);
} }
static void setup_urandom(void) { static void setup_urandom(void) {
urandom = open("/dev/urandom", O_RDONLY); random_fd = open(RANDOM_DEV_FILE, O_RDONLY);
if (urandom < 0) { if (random_fd < 0) {
perror("Failed to open /dev/urandom"); perror("Failed to open " RANDOM_DEV_FILE);
exit(1); exit(1);
} }
} }