1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-17 11:58:13 +00:00

fix /dev/urandom problem

This commit is contained in:
Pavol Rusnak 2015-05-04 19:53:06 +02:00
parent ffedf8a4d0
commit 00954da5fe
4 changed files with 12 additions and 13 deletions

22
rand.c
View File

@ -26,17 +26,13 @@
#include "rand.h"
static FILE *f;
void init_rand(void)
{
f = fopen("/dev/urandom", "r");
}
static FILE *frand = NULL;
int finalize_rand(void)
{
int err = fclose(f);
f = NULL;
if (!frand) return 0;
int err = fclose(frand);
frand = NULL;
return err;
}
@ -44,7 +40,10 @@ uint32_t random32(void)
{
uint32_t r;
size_t len = sizeof(r);
size_t len_read = fread(&r, 1, len, f);
if (!frand) {
frand = fopen("/dev/urandom", "r");
}
size_t len_read = fread(&r, 1, len, frand);
(void)len_read;
assert(len_read == len);
return r;
@ -59,7 +58,10 @@ uint32_t random_uniform(uint32_t n)
void random_buffer(uint8_t *buf, size_t len)
{
size_t len_read = fread(buf, 1, len, f);
if (!frand) {
frand = fopen("/dev/urandom", "r");
}
size_t len_read = fread(buf, 1, len, frand);
(void)len_read;
assert(len_read == len);
}

1
rand.h
View File

@ -27,7 +27,6 @@
#include <stdint.h>
#include <stdlib.h>
void init_rand(void);
int finalize_rand(void);
uint32_t random32(void);
uint32_t random_uniform(uint32_t n);

View File

@ -38,7 +38,6 @@ int main(int argc, char *argv[])
EC_GROUP *ecgroup;
int cnt = 0;
init_rand();
ecgroup = EC_GROUP_new_by_curve_name(NID_secp256k1);
unsigned long max_iterations = -1;

View File

@ -1442,7 +1442,6 @@ Suite *test_suite(void)
int main(void)
{
int number_failed;
init_rand(); // needed for scalar_multiply()
Suite *s = test_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_VERBOSE);