mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-15 12:08:59 +00:00
fix /dev/urandom problem
This commit is contained in:
parent
ffedf8a4d0
commit
00954da5fe
22
rand.c
22
rand.c
@ -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
1
rand.h
@ -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);
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user