]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[libc] Use wall clock time as seed for the (non-cryptographic) RNG 1052/head
authorMichael Brown <mcb30@ipxe.org>
Fri, 6 Oct 2023 11:43:02 +0000 (12:43 +0100)
committerMichael Brown <mcb30@ipxe.org>
Fri, 6 Oct 2023 11:50:43 +0000 (12:50 +0100)
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>
src/core/random.c

index 975a03cf588ecd8161e4fa0f253763a20d96fca4..e3251964befec3e6e3055c4b0947b93458d7de2f 100644 (file)
@@ -6,8 +6,9 @@
 
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 
+#include <stddef.h>
 #include <stdlib.h>
-#include <ipxe/timer.h>
+#include <time.h>
 
 static int32_t rnd_seed = 0;
 
@@ -30,8 +31,9 @@ void srandom ( unsigned int seed ) {
 long int random ( void ) {
        int32_t q;
 
-       if ( ! rnd_seed ) /* Initialize linear congruential generator */
-               srandom ( currticks() );
+       /* Initialize linear congruential generator */
+       if ( ! rnd_seed )
+               srandom ( time ( NULL ) );
 
        /* simplified version of the LCG given in Bruce Schneier's
           "Applied Cryptography" */