Files
ipxe/src/core/random.c
Michael Brown ff0f860483 [libc] Use wall clock time as seed for the (non-cryptographic) RNG
We currently use the number of timer ticks since power-on as a seed
for the non-cryptographic RNG implemented by random().  Since iPXE is
often executed directly after power-on, and since the timer tick
resolution is generally low, this can often result in identical seed
values being used on each cold boot attempt.

As of commit 41f786c ("[settings] Add "unixtime" builtin setting to
expose the current time"), the current wall-clock time is always
available within the default build of iPXE.  Use this time instead, to
introduce variability between cold boot attempts on the same host.
(Note that variability between different hosts is obtained by using
the MAC address as an additional seed value.)

This has no effect on the separate DRBG used by cryptographic code.

Suggested-by: Heiko <heik0@xs4all.nl>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
2023-10-06 12:50:43 +01:00

46 lines
886 B
C

/** @file
*
* Random number generation
*
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
static int32_t rnd_seed = 0;
/**
* Seed the pseudo-random number generator
*
* @v seed Seed value
*/
void srandom ( unsigned int seed ) {
rnd_seed = seed;
if ( ! rnd_seed )
rnd_seed = 4; /* Chosen by fair dice roll */
}
/**
* Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
*
* @ret rand Pseudo-random number
*/
long int random ( void ) {
int32_t q;
/* Initialize linear congruential generator */
if ( ! rnd_seed )
srandom ( time ( NULL ) );
/* simplified version of the LCG given in Bruce Schneier's
"Applied Cryptography" */
q = ( rnd_seed / 53668 );
rnd_seed = ( 40014 * ( rnd_seed - 53668 * q ) - 12211 * q );
if ( rnd_seed < 0 )
rnd_seed += 2147483563L;
return rnd_seed;
}