xref: /minix3/crypto/external/bsd/openssl/dist/doc/crypto/rand.pod (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc=pod
2*ebfedea0SLionel Sambuc
3*ebfedea0SLionel Sambuc=head1 NAME
4*ebfedea0SLionel Sambuc
5*ebfedea0SLionel Sambucrand - pseudo-random number generator
6*ebfedea0SLionel Sambuc
7*ebfedea0SLionel Sambuc=head1 SYNOPSIS
8*ebfedea0SLionel Sambuc
9*ebfedea0SLionel Sambuc #include <openssl/rand.h>
10*ebfedea0SLionel Sambuc
11*ebfedea0SLionel Sambuc int  RAND_set_rand_engine(ENGINE *engine);
12*ebfedea0SLionel Sambuc
13*ebfedea0SLionel Sambuc int  RAND_bytes(unsigned char *buf, int num);
14*ebfedea0SLionel Sambuc int  RAND_pseudo_bytes(unsigned char *buf, int num);
15*ebfedea0SLionel Sambuc
16*ebfedea0SLionel Sambuc void RAND_seed(const void *buf, int num);
17*ebfedea0SLionel Sambuc void RAND_add(const void *buf, int num, int entropy);
18*ebfedea0SLionel Sambuc int  RAND_status(void);
19*ebfedea0SLionel Sambuc
20*ebfedea0SLionel Sambuc int  RAND_load_file(const char *file, long max_bytes);
21*ebfedea0SLionel Sambuc int  RAND_write_file(const char *file);
22*ebfedea0SLionel Sambuc const char *RAND_file_name(char *file, size_t num);
23*ebfedea0SLionel Sambuc
24*ebfedea0SLionel Sambuc int  RAND_egd(const char *path);
25*ebfedea0SLionel Sambuc
26*ebfedea0SLionel Sambuc void RAND_set_rand_method(const RAND_METHOD *meth);
27*ebfedea0SLionel Sambuc const RAND_METHOD *RAND_get_rand_method(void);
28*ebfedea0SLionel Sambuc RAND_METHOD *RAND_SSLeay(void);
29*ebfedea0SLionel Sambuc
30*ebfedea0SLionel Sambuc void RAND_cleanup(void);
31*ebfedea0SLionel Sambuc
32*ebfedea0SLionel Sambuc /* For Win32 only */
33*ebfedea0SLionel Sambuc void RAND_screen(void);
34*ebfedea0SLionel Sambuc int RAND_event(UINT, WPARAM, LPARAM);
35*ebfedea0SLionel Sambuc
36*ebfedea0SLionel Sambuc=head1 DESCRIPTION
37*ebfedea0SLionel Sambuc
38*ebfedea0SLionel SambucSince the introduction of the ENGINE API, the recommended way of controlling
39*ebfedea0SLionel Sambucdefault implementations is by using the ENGINE API functions. The default
40*ebfedea0SLionel SambucB<RAND_METHOD>, as set by RAND_set_rand_method() and returned by
41*ebfedea0SLionel SambucRAND_get_rand_method(), is only used if no ENGINE has been set as the default
42*ebfedea0SLionel Sambuc"rand" implementation. Hence, these two functions are no longer the recommened
43*ebfedea0SLionel Sambucway to control defaults.
44*ebfedea0SLionel Sambuc
45*ebfedea0SLionel SambucIf an alternative B<RAND_METHOD> implementation is being used (either set
46*ebfedea0SLionel Sambucdirectly or as provided by an ENGINE module), then it is entirely responsible
47*ebfedea0SLionel Sambucfor the generation and management of a cryptographically secure PRNG stream. The
48*ebfedea0SLionel Sambucmechanisms described below relate solely to the software PRNG implementation
49*ebfedea0SLionel Sambucbuilt in to OpenSSL and used by default.
50*ebfedea0SLionel Sambuc
51*ebfedea0SLionel SambucThese functions implement a cryptographically secure pseudo-random
52*ebfedea0SLionel Sambucnumber generator (PRNG). It is used by other library functions for
53*ebfedea0SLionel Sambucexample to generate random keys, and applications can use it when they
54*ebfedea0SLionel Sambucneed randomness.
55*ebfedea0SLionel Sambuc
56*ebfedea0SLionel SambucA cryptographic PRNG must be seeded with unpredictable data such as
57*ebfedea0SLionel Sambucmouse movements or keys pressed at random by the user. This is
58*ebfedea0SLionel Sambucdescribed in L<RAND_add(3)|RAND_add(3)>. Its state can be saved in a seed file
59*ebfedea0SLionel Sambuc(see L<RAND_load_file(3)|RAND_load_file(3)>) to avoid having to go through the
60*ebfedea0SLionel Sambucseeding process whenever the application is started.
61*ebfedea0SLionel Sambuc
62*ebfedea0SLionel SambucL<RAND_bytes(3)|RAND_bytes(3)> describes how to obtain random data from the
63*ebfedea0SLionel SambucPRNG.
64*ebfedea0SLionel Sambuc
65*ebfedea0SLionel Sambuc=head1 INTERNALS
66*ebfedea0SLionel Sambuc
67*ebfedea0SLionel SambucThe RAND_SSLeay() method implements a PRNG based on a cryptographic
68*ebfedea0SLionel Sambuchash function.
69*ebfedea0SLionel Sambuc
70*ebfedea0SLionel SambucThe following description of its design is based on the SSLeay
71*ebfedea0SLionel Sambucdocumentation:
72*ebfedea0SLionel Sambuc
73*ebfedea0SLionel SambucFirst up I will state the things I believe I need for a good RNG.
74*ebfedea0SLionel Sambuc
75*ebfedea0SLionel Sambuc=over 4
76*ebfedea0SLionel Sambuc
77*ebfedea0SLionel Sambuc=item 1
78*ebfedea0SLionel Sambuc
79*ebfedea0SLionel SambucA good hashing algorithm to mix things up and to convert the RNG 'state'
80*ebfedea0SLionel Sambucto random numbers.
81*ebfedea0SLionel Sambuc
82*ebfedea0SLionel Sambuc=item 2
83*ebfedea0SLionel Sambuc
84*ebfedea0SLionel SambucAn initial source of random 'state'.
85*ebfedea0SLionel Sambuc
86*ebfedea0SLionel Sambuc=item 3
87*ebfedea0SLionel Sambuc
88*ebfedea0SLionel SambucThe state should be very large.  If the RNG is being used to generate
89*ebfedea0SLionel Sambuc4096 bit RSA keys, 2 2048 bit random strings are required (at a minimum).
90*ebfedea0SLionel SambucIf your RNG state only has 128 bits, you are obviously limiting the
91*ebfedea0SLionel Sambucsearch space to 128 bits, not 2048.  I'm probably getting a little
92*ebfedea0SLionel Sambuccarried away on this last point but it does indicate that it may not be
93*ebfedea0SLionel Sambuca bad idea to keep quite a lot of RNG state.  It should be easier to
94*ebfedea0SLionel Sambucbreak a cipher than guess the RNG seed data.
95*ebfedea0SLionel Sambuc
96*ebfedea0SLionel Sambuc=item 4
97*ebfedea0SLionel Sambuc
98*ebfedea0SLionel SambucAny RNG seed data should influence all subsequent random numbers
99*ebfedea0SLionel Sambucgenerated.  This implies that any random seed data entered will have
100*ebfedea0SLionel Sambucan influence on all subsequent random numbers generated.
101*ebfedea0SLionel Sambuc
102*ebfedea0SLionel Sambuc=item 5
103*ebfedea0SLionel Sambuc
104*ebfedea0SLionel SambucWhen using data to seed the RNG state, the data used should not be
105*ebfedea0SLionel Sambucextractable from the RNG state.  I believe this should be a
106*ebfedea0SLionel Sambucrequirement because one possible source of 'secret' semi random
107*ebfedea0SLionel Sambucdata would be a private key or a password.  This data must
108*ebfedea0SLionel Sambucnot be disclosed by either subsequent random numbers or a
109*ebfedea0SLionel Sambuc'core' dump left by a program crash.
110*ebfedea0SLionel Sambuc
111*ebfedea0SLionel Sambuc=item 6
112*ebfedea0SLionel Sambuc
113*ebfedea0SLionel SambucGiven the same initial 'state', 2 systems should deviate in their RNG state
114*ebfedea0SLionel Sambuc(and hence the random numbers generated) over time if at all possible.
115*ebfedea0SLionel Sambuc
116*ebfedea0SLionel Sambuc=item 7
117*ebfedea0SLionel Sambuc
118*ebfedea0SLionel SambucGiven the random number output stream, it should not be possible to determine
119*ebfedea0SLionel Sambucthe RNG state or the next random number.
120*ebfedea0SLionel Sambuc
121*ebfedea0SLionel Sambuc=back
122*ebfedea0SLionel Sambuc
123*ebfedea0SLionel SambucThe algorithm is as follows.
124*ebfedea0SLionel Sambuc
125*ebfedea0SLionel SambucThere is global state made up of a 1023 byte buffer (the 'state'), a
126*ebfedea0SLionel Sambucworking hash value ('md'), and a counter ('count').
127*ebfedea0SLionel Sambuc
128*ebfedea0SLionel SambucWhenever seed data is added, it is inserted into the 'state' as
129*ebfedea0SLionel Sambucfollows.
130*ebfedea0SLionel Sambuc
131*ebfedea0SLionel SambucThe input is chopped up into units of 20 bytes (or less for
132*ebfedea0SLionel Sambucthe last block).  Each of these blocks is run through the hash
133*ebfedea0SLionel Sambucfunction as follows:  The data passed to the hash function
134*ebfedea0SLionel Sambucis the current 'md', the same number of bytes from the 'state'
135*ebfedea0SLionel Sambuc(the location determined by in incremented looping index) as
136*ebfedea0SLionel Sambucthe current 'block', the new key data 'block', and 'count'
137*ebfedea0SLionel Sambuc(which is incremented after each use).
138*ebfedea0SLionel SambucThe result of this is kept in 'md' and also xored into the
139*ebfedea0SLionel Sambuc'state' at the same locations that were used as input into the
140*ebfedea0SLionel Sambuchash function. I
141*ebfedea0SLionel Sambucbelieve this system addresses points 1 (hash function; currently
142*ebfedea0SLionel SambucSHA-1), 3 (the 'state'), 4 (via the 'md'), 5 (by the use of a hash
143*ebfedea0SLionel Sambucfunction and xor).
144*ebfedea0SLionel Sambuc
145*ebfedea0SLionel SambucWhen bytes are extracted from the RNG, the following process is used.
146*ebfedea0SLionel SambucFor each group of 10 bytes (or less), we do the following:
147*ebfedea0SLionel Sambuc
148*ebfedea0SLionel SambucInput into the hash function the local 'md' (which is initialized from
149*ebfedea0SLionel Sambucthe global 'md' before any bytes are generated), the bytes that are to
150*ebfedea0SLionel Sambucbe overwritten by the random bytes, and bytes from the 'state'
151*ebfedea0SLionel Sambuc(incrementing looping index). From this digest output (which is kept
152*ebfedea0SLionel Sambucin 'md'), the top (up to) 10 bytes are returned to the caller and the
153*ebfedea0SLionel Sambucbottom 10 bytes are xored into the 'state'.
154*ebfedea0SLionel Sambuc
155*ebfedea0SLionel SambucFinally, after we have finished 'num' random bytes for the caller,
156*ebfedea0SLionel Sambuc'count' (which is incremented) and the local and global 'md' are fed
157*ebfedea0SLionel Sambucinto the hash function and the results are kept in the global 'md'.
158*ebfedea0SLionel Sambuc
159*ebfedea0SLionel SambucI believe the above addressed points 1 (use of SHA-1), 6 (by hashing
160*ebfedea0SLionel Sambucinto the 'state' the 'old' data from the caller that is about to be
161*ebfedea0SLionel Sambucoverwritten) and 7 (by not using the 10 bytes given to the caller to
162*ebfedea0SLionel Sambucupdate the 'state', but they are used to update 'md').
163*ebfedea0SLionel Sambuc
164*ebfedea0SLionel SambucSo of the points raised, only 2 is not addressed (but see
165*ebfedea0SLionel SambucL<RAND_add(3)|RAND_add(3)>).
166*ebfedea0SLionel Sambuc
167*ebfedea0SLionel Sambuc=head1 SEE ALSO
168*ebfedea0SLionel Sambuc
169*ebfedea0SLionel SambucL<BN_rand(3)|BN_rand(3)>, L<RAND_add(3)|RAND_add(3)>,
170*ebfedea0SLionel SambucL<RAND_load_file(3)|RAND_load_file(3)>, L<RAND_egd(3)|RAND_egd(3)>,
171*ebfedea0SLionel SambucL<RAND_bytes(3)|RAND_bytes(3)>,
172*ebfedea0SLionel SambucL<RAND_set_rand_method(3)|RAND_set_rand_method(3)>,
173*ebfedea0SLionel SambucL<RAND_cleanup(3)|RAND_cleanup(3)>
174*ebfedea0SLionel Sambuc
175*ebfedea0SLionel Sambuc=cut
176