rand (3)
Leading comments
Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) Standard preamble: ========================================================================
NAME
rand - pseudo-random number generatorSYNOPSIS
#include <openssl/rand.h> int RAND_set_rand_engine(ENGINE *engine); int RAND_bytes(unsigned char *buf, int num); int RAND_pseudo_bytes(unsigned char *buf, int num); void RAND_seed(const void *buf, int num); void RAND_add(const void *buf, int num, int entropy); int RAND_status(void); int RAND_load_file(const char *file, long max_bytes); int RAND_write_file(const char *file); const char *RAND_file_name(char *file, size_t num); int RAND_egd(const char *path); void RAND_set_rand_method(const RAND_METHOD *meth); const RAND_METHOD *RAND_get_rand_method(void); RAND_METHOD *RAND_SSLeay(void); void RAND_cleanup(void); /* For Win32 only */ void RAND_screen(void); int RAND_event(UINT, WPARAM, LPARAM);
DESCRIPTION
Since the introduction of theIf an alternative
These functions implement a cryptographically secure pseudo-random number generator (
A cryptographic
RAND_bytes(3) describes how to obtain random data from the
INTERNALS
The RAND_SSLeay() method implements aThe following description of its design is based on the SSLeay documentation:
First up I will state the things I believe I need for a good
- 1.
-
A good hashing algorithm to mix things up and to convert the RNG'state' to random numbers.
- 2.
- An initial source of random 'state'.
- 3.
-
The state should be very large. If the RNGis being used to generate 4096 bitRSAkeys, 2 2048 bit random strings are required (at a minimum). If yourRNGstate only has 128 bits, you are obviously limiting the search space to 128 bits, not 2048. I'm probably getting a little carried away on this last point but it does indicate that it may not be a bad idea to keep quite a lot ofRNGstate. It should be easier to break a cipher than guess theRNGseed data.
- 4.
-
Any RNGseed data should influence all subsequent random numbers generated. This implies that any random seed data entered will have an influence on all subsequent random numbers generated.
- 5.
-
When using data to seed the RNGstate, the data used should not be extractable from theRNGstate. I believe this should be a requirement because one possible source of 'secret' semi random data would be a private key or a password. This data must not be disclosed by either subsequent random numbers or a 'core' dump left by a program crash.
- 6.
-
Given the same initial 'state', 2 systems should deviate in their RNGstate (and hence the random numbers generated) over time if at all possible.
- 7.
-
Given the random number output stream, it should not be possible to determine
the RNGstate or the next random number.
The algorithm is as follows.
There is global state made up of a 1023 byte buffer (the 'state'), a working hash value ('md'), and a counter ('count').
Whenever seed data is added, it is inserted into the 'state' as follows.
The input is chopped up into units of 20 bytes (or less for the last block). Each of these blocks is run through the hash function as follows: The data passed to the hash function is the current 'md', the same number of bytes from the 'state' (the location determined by in incremented looping index) as the current 'block', the new key data 'block', and 'count' (which is incremented after each use). The result of this is kept in 'md' and also xored into the 'state' at the same locations that were used as input into the hash function. I believe this system addresses points 1 (hash function; currently
When bytes are extracted from the
Input into the hash function the local 'md' (which is initialized from the global 'md' before any bytes are generated), the bytes that are to be overwritten by the random bytes, and bytes from the 'state' (incrementing looping index). From this digest output (which is kept in 'md'), the top (up to) 10 bytes are returned to the caller and the bottom 10 bytes are xored into the 'state'.
Finally, after we have finished 'num' random bytes for the caller, 'count' (which is incremented) and the local and global 'md' are fed into the hash function and the results are kept in the global 'md'.
I believe the above addressed points 1 (use of
So of the points raised, only 2 is not addressed (but see RAND_add(3)).