mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-17 20:08:12 +00:00
fix build on _WIN32
This commit is contained in:
parent
6726547a88
commit
3556c74740
21
rand.c
21
rand.c
@ -22,7 +22,11 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#ifdef _WIN32
|
||||
#include <time.h>
|
||||
#else
|
||||
#include <assert.h>
|
||||
#endif
|
||||
|
||||
#include "rand.h"
|
||||
|
||||
@ -30,14 +34,22 @@ static FILE *frand = NULL;
|
||||
|
||||
int finalize_rand(void)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return 0;
|
||||
#else
|
||||
if (!frand) return 0;
|
||||
int err = fclose(frand);
|
||||
frand = NULL;
|
||||
return err;
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t random32(void)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
srand((unsigned)time(NULL));
|
||||
return ((rand() % 0xFF) | ((rand() % 0xFF) << 8) | ((rand() % 0xFF) << 16) | ((rand() % 0xFF) << 24));
|
||||
#else
|
||||
uint32_t r;
|
||||
size_t len = sizeof(r);
|
||||
if (!frand) {
|
||||
@ -47,6 +59,7 @@ uint32_t random32(void)
|
||||
(void)len_read;
|
||||
assert(len_read == len);
|
||||
return r;
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t random_uniform(uint32_t n)
|
||||
@ -58,12 +71,20 @@ uint32_t random_uniform(uint32_t n)
|
||||
|
||||
void random_buffer(uint8_t *buf, size_t len)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
srand((unsigned)time(NULL));
|
||||
size_t i;
|
||||
for (i = 0; i < len; i++) {
|
||||
buf[i] = rand() % 0xFF;
|
||||
}
|
||||
#else
|
||||
if (!frand) {
|
||||
frand = fopen("/dev/urandom", "r");
|
||||
}
|
||||
size_t len_read = fread(buf, 1, len, frand);
|
||||
(void)len_read;
|
||||
assert(len_read == len);
|
||||
#endif
|
||||
}
|
||||
|
||||
void random_permute(char *str, size_t len)
|
||||
|
Loading…
Reference in New Issue
Block a user