1*2175Sjp161948=pod 2*2175Sjp161948 3*2175Sjp161948=head1 NAME 4*2175Sjp161948 5*2175Sjp161948rand - pseudo-random number generator 6*2175Sjp161948 7*2175Sjp161948=head1 SYNOPSIS 8*2175Sjp161948 9*2175Sjp161948 #include <openssl/rand.h> 10*2175Sjp161948 11*2175Sjp161948 int RAND_set_rand_engine(ENGINE *engine); 12*2175Sjp161948 13*2175Sjp161948 int RAND_bytes(unsigned char *buf, int num); 14*2175Sjp161948 int RAND_pseudo_bytes(unsigned char *buf, int num); 15*2175Sjp161948 16*2175Sjp161948 void RAND_seed(const void *buf, int num); 17*2175Sjp161948 void RAND_add(const void *buf, int num, int entropy); 18*2175Sjp161948 int RAND_status(void); 19*2175Sjp161948 20*2175Sjp161948 int RAND_load_file(const char *file, long max_bytes); 21*2175Sjp161948 int RAND_write_file(const char *file); 22*2175Sjp161948 const char *RAND_file_name(char *file, size_t num); 23*2175Sjp161948 24*2175Sjp161948 int RAND_egd(const char *path); 25*2175Sjp161948 26*2175Sjp161948 void RAND_set_rand_method(const RAND_METHOD *meth); 27*2175Sjp161948 const RAND_METHOD *RAND_get_rand_method(void); 28*2175Sjp161948 RAND_METHOD *RAND_SSLeay(void); 29*2175Sjp161948 30*2175Sjp161948 void RAND_cleanup(void); 31*2175Sjp161948 32*2175Sjp161948 /* For Win32 only */ 33*2175Sjp161948 void RAND_screen(void); 34*2175Sjp161948 int RAND_event(UINT, WPARAM, LPARAM); 35*2175Sjp161948 36*2175Sjp161948=head1 DESCRIPTION 37*2175Sjp161948 38*2175Sjp161948Since the introduction of the ENGINE API, the recommended way of controlling 39*2175Sjp161948default implementations is by using the ENGINE API functions. The default 40*2175Sjp161948B<RAND_METHOD>, as set by RAND_set_rand_method() and returned by 41*2175Sjp161948RAND_get_rand_method(), is only used if no ENGINE has been set as the default 42*2175Sjp161948"rand" implementation. Hence, these two functions are no longer the recommened 43*2175Sjp161948way to control defaults. 44*2175Sjp161948 45*2175Sjp161948If an alternative B<RAND_METHOD> implementation is being used (either set 46*2175Sjp161948directly or as provided by an ENGINE module), then it is entirely responsible 47*2175Sjp161948for the generation and management of a cryptographically secure PRNG stream. The 48*2175Sjp161948mechanisms described below relate solely to the software PRNG implementation 49*2175Sjp161948built in to OpenSSL and used by default. 50*2175Sjp161948 51*2175Sjp161948These functions implement a cryptographically secure pseudo-random 52*2175Sjp161948number generator (PRNG). It is used by other library functions for 53*2175Sjp161948example to generate random keys, and applications can use it when they 54*2175Sjp161948need randomness. 55*2175Sjp161948 56*2175Sjp161948A cryptographic PRNG must be seeded with unpredictable data such as 57*2175Sjp161948mouse movements or keys pressed at random by the user. This is 58*2175Sjp161948described in L<RAND_add(3)|RAND_add(3)>. Its state can be saved in a seed file 59*2175Sjp161948(see L<RAND_load_file(3)|RAND_load_file(3)>) to avoid having to go through the 60*2175Sjp161948seeding process whenever the application is started. 61*2175Sjp161948 62*2175Sjp161948L<RAND_bytes(3)|RAND_bytes(3)> describes how to obtain random data from the 63*2175Sjp161948PRNG. 64*2175Sjp161948 65*2175Sjp161948=head1 INTERNALS 66*2175Sjp161948 67*2175Sjp161948The RAND_SSLeay() method implements a PRNG based on a cryptographic 68*2175Sjp161948hash function. 69*2175Sjp161948 70*2175Sjp161948The following description of its design is based on the SSLeay 71*2175Sjp161948documentation: 72*2175Sjp161948 73*2175Sjp161948First up I will state the things I believe I need for a good RNG. 74*2175Sjp161948 75*2175Sjp161948=over 4 76*2175Sjp161948 77*2175Sjp161948=item 1 78*2175Sjp161948 79*2175Sjp161948A good hashing algorithm to mix things up and to convert the RNG 'state' 80*2175Sjp161948to random numbers. 81*2175Sjp161948 82*2175Sjp161948=item 2 83*2175Sjp161948 84*2175Sjp161948An initial source of random 'state'. 85*2175Sjp161948 86*2175Sjp161948=item 3 87*2175Sjp161948 88*2175Sjp161948The state should be very large. If the RNG is being used to generate 89*2175Sjp1619484096 bit RSA keys, 2 2048 bit random strings are required (at a minimum). 90*2175Sjp161948If your RNG state only has 128 bits, you are obviously limiting the 91*2175Sjp161948search space to 128 bits, not 2048. I'm probably getting a little 92*2175Sjp161948carried away on this last point but it does indicate that it may not be 93*2175Sjp161948a bad idea to keep quite a lot of RNG state. It should be easier to 94*2175Sjp161948break a cipher than guess the RNG seed data. 95*2175Sjp161948 96*2175Sjp161948=item 4 97*2175Sjp161948 98*2175Sjp161948Any RNG seed data should influence all subsequent random numbers 99*2175Sjp161948generated. This implies that any random seed data entered will have 100*2175Sjp161948an influence on all subsequent random numbers generated. 101*2175Sjp161948 102*2175Sjp161948=item 5 103*2175Sjp161948 104*2175Sjp161948When using data to seed the RNG state, the data used should not be 105*2175Sjp161948extractable from the RNG state. I believe this should be a 106*2175Sjp161948requirement because one possible source of 'secret' semi random 107*2175Sjp161948data would be a private key or a password. This data must 108*2175Sjp161948not be disclosed by either subsequent random numbers or a 109*2175Sjp161948'core' dump left by a program crash. 110*2175Sjp161948 111*2175Sjp161948=item 6 112*2175Sjp161948 113*2175Sjp161948Given the same initial 'state', 2 systems should deviate in their RNG state 114*2175Sjp161948(and hence the random numbers generated) over time if at all possible. 115*2175Sjp161948 116*2175Sjp161948=item 7 117*2175Sjp161948 118*2175Sjp161948Given the random number output stream, it should not be possible to determine 119*2175Sjp161948the RNG state or the next random number. 120*2175Sjp161948 121*2175Sjp161948=back 122*2175Sjp161948 123*2175Sjp161948The algorithm is as follows. 124*2175Sjp161948 125*2175Sjp161948There is global state made up of a 1023 byte buffer (the 'state'), a 126*2175Sjp161948working hash value ('md'), and a counter ('count'). 127*2175Sjp161948 128*2175Sjp161948Whenever seed data is added, it is inserted into the 'state' as 129*2175Sjp161948follows. 130*2175Sjp161948 131*2175Sjp161948The input is chopped up into units of 20 bytes (or less for 132*2175Sjp161948the last block). Each of these blocks is run through the hash 133*2175Sjp161948function as follows: The data passed to the hash function 134*2175Sjp161948is the current 'md', the same number of bytes from the 'state' 135*2175Sjp161948(the location determined by in incremented looping index) as 136*2175Sjp161948the current 'block', the new key data 'block', and 'count' 137*2175Sjp161948(which is incremented after each use). 138*2175Sjp161948The result of this is kept in 'md' and also xored into the 139*2175Sjp161948'state' at the same locations that were used as input into the 140*2175Sjp161948hash function. I 141*2175Sjp161948believe this system addresses points 1 (hash function; currently 142*2175Sjp161948SHA-1), 3 (the 'state'), 4 (via the 'md'), 5 (by the use of a hash 143*2175Sjp161948function and xor). 144*2175Sjp161948 145*2175Sjp161948When bytes are extracted from the RNG, the following process is used. 146*2175Sjp161948For each group of 10 bytes (or less), we do the following: 147*2175Sjp161948 148*2175Sjp161948Input into the hash function the local 'md' (which is initialized from 149*2175Sjp161948the global 'md' before any bytes are generated), the bytes that are to 150*2175Sjp161948be overwritten by the random bytes, and bytes from the 'state' 151*2175Sjp161948(incrementing looping index). From this digest output (which is kept 152*2175Sjp161948in 'md'), the top (up to) 10 bytes are returned to the caller and the 153*2175Sjp161948bottom 10 bytes are xored into the 'state'. 154*2175Sjp161948 155*2175Sjp161948Finally, after we have finished 'num' random bytes for the caller, 156*2175Sjp161948'count' (which is incremented) and the local and global 'md' are fed 157*2175Sjp161948into the hash function and the results are kept in the global 'md'. 158*2175Sjp161948 159*2175Sjp161948I believe the above addressed points 1 (use of SHA-1), 6 (by hashing 160*2175Sjp161948into the 'state' the 'old' data from the caller that is about to be 161*2175Sjp161948overwritten) and 7 (by not using the 10 bytes given to the caller to 162*2175Sjp161948update the 'state', but they are used to update 'md'). 163*2175Sjp161948 164*2175Sjp161948So of the points raised, only 2 is not addressed (but see 165*2175Sjp161948L<RAND_add(3)|RAND_add(3)>). 166*2175Sjp161948 167*2175Sjp161948=head1 SEE ALSO 168*2175Sjp161948 169*2175Sjp161948L<BN_rand(3)|BN_rand(3)>, L<RAND_add(3)|RAND_add(3)>, 170*2175Sjp161948L<RAND_load_file(3)|RAND_load_file(3)>, L<RAND_egd(3)|RAND_egd(3)>, 171*2175Sjp161948L<RAND_bytes(3)|RAND_bytes(3)>, 172*2175Sjp161948L<RAND_set_rand_method(3)|RAND_set_rand_method(3)>, 173*2175Sjp161948L<RAND_cleanup(3)|RAND_cleanup(3)> 174*2175Sjp161948 175*2175Sjp161948=cut 176