rand.c: for testing purposes seed the pseudorandom number generator with 0

instead of the current time.

This is needed to ensure identical pseudorandom outputs when running tests.
pull/25/head
andrew 6 years ago committed by Pavol Rusnak
parent 7079277fb0
commit c5227fdb96
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

@ -26,26 +26,31 @@
#ifndef RAND_PLATFORM_INDEPENDENT #ifndef RAND_PLATFORM_INDEPENDENT
#pragma message("NOT SUITABLE FOR PRODUCTION USE!") #pragma message("NOT SUITABLE FOR PRODUCTION USE! Replace random8() and random32() functions with your own secure code.")
// The following code is not supposed to be used in a production environment. // The following code is not supposed to be used in a production environment.
// It's included only to make the library testable. // It's included only to make the library testable.
// The message above tries to prevent any accidental use outside of the test environment. // The message above tries to prevent any accidental use outside of the test environment.
// //
// You are supposed to replace the random32() function with your own secure code. // You are supposed to replace the random8() and random32() function with your own secure code.
// There is also a possibility to replace the random_buffer() function as it is defined as a weak symbol. // There is also a possibility to replace the random_buffer() function as it is defined as a weak symbol.
#include <stdio.h> static uint8_t random8(void)
#include <time.h> {
// Linear congruential generator used in glibc
// https://en.wikipedia.org/wiki/Linear_congruential_generator
static int seed = 0;
seed = (1103515245 * seed + 12345) & 0x7FFFFFFF;
return seed & 0xFF;
}
uint32_t random32(void) uint32_t random32(void)
{ {
static int initialized = 0; uint32_t r1 = random8();
if (!initialized) { uint32_t r2 = random8();
srand((unsigned)time(NULL)); uint32_t r3 = random8();
initialized = 1; uint32_t r4 = random8();
} return ((r1 << 24) | (r2 << 16) | (r3 << 8) | r4);
return ((rand() & 0xFF) | ((rand() & 0xFF) << 8) | ((rand() & 0xFF) << 16) | ((uint32_t) (rand() & 0xFF) << 24));
} }
#endif /* RAND_PLATFORM_INDEPENDENT */ #endif /* RAND_PLATFORM_INDEPENDENT */

Loading…
Cancel
Save