iPXE
random.c
Go to the documentation of this file.
1/** @file
2 *
3 * Random number generation
4 *
5 */
6
7FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
8FILE_SECBOOT ( PERMITTED );
9
10#include <stddef.h>
11#include <stdlib.h>
12#include <time.h>
13
14static int32_t rnd_seed = 0;
15
16/**
17 * Seed the pseudo-random number generator
18 *
19 * @v seed Seed value
20 */
21void srandom ( unsigned int seed ) {
22 rnd_seed = seed;
23 if ( ! rnd_seed )
24 rnd_seed = 4; /* Chosen by fair dice roll */
25}
26
27/**
28 * Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
29 *
30 * @ret rand Pseudo-random number
31 */
32long int random ( void ) {
33 int32_t q;
34
35 /* Initialize linear congruential generator */
36 if ( ! rnd_seed )
37 srandom ( time ( NULL ) );
38
39 /* simplified version of the LCG given in Bruce Schneier's
40 "Applied Cryptography" */
41 q = ( rnd_seed / 53668 );
42 rnd_seed = ( 40014 * ( rnd_seed - 53668 * q ) - 12211 * q );
43 if ( rnd_seed < 0 )
44 rnd_seed += 2147483563L;
45 return rnd_seed;
46}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
signed int int32_t
Definition stdint.h:17
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
Date and time.
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition random.c:32
static int32_t rnd_seed
Definition random.c:14
void srandom(unsigned int seed)
Seed the pseudo-random number generator.
Definition random.c:21