1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 04:18:10 +00:00

fix build on _WIN32

This commit is contained in:
Pavol Rusnak 2015-12-14 22:50:09 +01:00
parent 6726547a88
commit 3556c74740
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

21
rand.c
View File

@ -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)