1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

further work on making rand.{c,h} more global

This commit is contained in:
Pavol Rusnak 2018-01-13 15:07:19 +01:00
parent 65e976d70c
commit 46fa586b12
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 34 additions and 50 deletions

78
rand.c
View File

@ -21,6 +21,10 @@
* OTHER DEALINGS IN THE SOFTWARE. * OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include "rand.h"
#ifndef RAND_PLATFORM_INDEPENDENT
#include <stdio.h> #include <stdio.h>
#ifdef _WIN32 #ifdef _WIN32
#include <time.h> #include <time.h>
@ -28,64 +32,31 @@
#include <assert.h> #include <assert.h>
#endif #endif
#include "rand.h"
#ifndef RAND_PLATFORM_INDEPENDANT
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) uint32_t random32(void)
{ {
#ifdef _WIN32 #ifdef _WIN32
srand((unsigned)time(NULL)); static int initialized = 0;
if (!initialized) {
srand((unsigned)time(NULL));
initialized = 1;
}
return ((rand() % 0xFF) | ((rand() % 0xFF) << 8) | ((rand() % 0xFF) << 16) | ((rand() % 0xFF) << 24)); return ((rand() % 0xFF) | ((rand() % 0xFF) << 8) | ((rand() % 0xFF) << 16) | ((rand() % 0xFF) << 24));
#else #else
uint32_t r; static FILE *frand = NULL;
size_t len = sizeof(r);
if (!frand) { if (!frand) {
frand = fopen("/dev/urandom", "r"); frand = fopen("/dev/urandom", "r");
} }
size_t len_read = fread(&r, 1, len, frand); uint32_t r;
(void)len_read; size_t len_read = fread(&r, 1, sizeof(r), frand);
assert(len_read == len); assert(len_read == sizeof(r));
return r; return r;
#endif #endif
} }
void random_buffer(uint8_t *buf, size_t len) #endif /* RAND_PLATFORM_INDEPENDENT */
{
#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
}
#endif /* RAND_PLATFORM_INDEPENDANT */
// //
// Following code should be platform independant // The following code is platform independent
// //
uint32_t random_uniform(uint32_t n) uint32_t random_uniform(uint32_t n)
@ -95,13 +66,22 @@ uint32_t random_uniform(uint32_t n)
return x / (max / n); return x / (max / n);
} }
void random_buffer(uint8_t *buf, size_t len)
{
uint32_t r = 0;
for (size_t i = 0; i < len; i++) {
if (i % 4 == 0) {
r = random32();
}
buf[i] = (r >> ((i % 4) * 8)) & 0xFF;
}
}
void random_permute(char *str, size_t len) void random_permute(char *str, size_t len)
{ {
int i, j; for (int i = len - 1; i >= 1; i--) {
char t; int j = random_uniform(i + 1);
for (i = len - 1; i >= 1; i--) { char t = str[j];
j = random_uniform(i + 1);
t = str[j];
str[j] = str[i]; str[j] = str[i];
str[i] = t; str[i] = t;
} }

6
rand.h
View File

@ -27,8 +27,12 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
int finalize_rand(void); #ifndef RAND_PLATFORM_INDEPENDENT
uint32_t random32(void); uint32_t random32(void);
#endif /* RAND_PLATFORM_INDEPENDENT */
uint32_t random_uniform(uint32_t n); uint32_t random_uniform(uint32_t n);
void random_buffer(uint8_t *buf, size_t len); void random_buffer(uint8_t *buf, size_t len);
void random_permute(char *buf, size_t len); void random_permute(char *buf, size_t len);