2 ''' $RCSfile$$Revision$$Date$
20 .ie \\n(.$>=3 .ne \\$3
36 ''' Set up \*(-- to give an unbreakable dash;
37 ''' string Tr holds user defined translation string.
38 ''' Bell System Logo is used as a dummy character.
44 .if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
45 .if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
48 ''' \*(M", \*(S", \*(N" and \*(T" are the equivalent of
49 ''' \*(L" and \*(R", except that they are used on ".xx" lines,
50 ''' such as .IP and .SH, which do another additional levels of
51 ''' double-quote interpretation
80 .\" If the F register is turned on, we'll generate
81 .\" index entries out stderr for the following things:
86 .\" X<> Xref (embedded
87 .\" Of course, you have to process the output yourself
88 .\" in some meaninful fashion.
91 .tm Index:\\$1\t\\n%\t"\\$2"
96 .TH rand 3 "0.9.7d" "2/Sep/2004" "OpenSSL"
100 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
101 .de CQ \" put $1 in typewriter font
107 \\&\\$2 \\$3 \\$4 \\$5 \\$6 \\$7
110 .\" @(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2
111 . \" AM - accent mark definitions
113 . \" fudge factors for nroff and troff
122 . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
128 . \" simple accents for nroff and troff
141 . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
142 . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
143 . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
144 . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
145 . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
146 . ds ? \s-2c\h'-\w'c'u*7/10'\u\h'\*(#H'\zi\d\s+2\h'\w'c'u*8/10'
147 . ds ! \s-2\(or\s+2\h'-\w'\(or'u'\v'-.8m'.\v'.8m'
148 . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
149 . ds q o\h'-\w'o'u*8/10'\s-4\v'.4m'\z\(*i\v'-.4m'\s+4\h'\w'o'u*8/10'
151 . \" troff and (daisy-wheel) nroff accents
152 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
153 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
154 .ds v \\k:\h'-(\\n(.wu*9/10-\*(#H)'\v'-\*(#V'\*(#[\s-4v\s0\v'\*(#V'\h'|\\n:u'\*(#]
155 .ds _ \\k:\h'-(\\n(.wu*9/10-\*(#H+(\*(#F*2/3))'\v'-.4m'\z\(hy\v'.4m'\h'|\\n:u'
156 .ds . \\k:\h'-(\\n(.wu*8/10)'\v'\*(#V*4/10'\z.\v'-\*(#V*4/10'\h'|\\n:u'
157 .ds 3 \*(#[\v'.2m'\s-2\&3\s0\v'-.2m'\*(#]
158 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
159 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
160 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
161 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
162 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
163 .ds ae a\h'-(\w'a'u*4/10)'e
164 .ds Ae A\h'-(\w'A'u*4/10)'E
165 .ds oe o\h'-(\w'o'u*4/10)'e
166 .ds Oe O\h'-(\w'O'u*4/10)'E
167 . \" corrections for vroff
168 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
169 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
170 . \" for low resolution devices (crt and lpr)
171 .if \n(.H>23 .if \n(.V>19 \
175 . ds v \h'-1'\o'\(aa\(ga'
191 rand \- pseudo-random number generator
195 \& #include <openssl/rand.h>
198 \& int RAND_set_rand_engine(ENGINE *engine);
201 \& int RAND_bytes(unsigned char *buf, int num);
202 \& int RAND_pseudo_bytes(unsigned char *buf, int num);
205 \& void RAND_seed(const void *buf, int num);
206 \& void RAND_add(const void *buf, int num, int entropy);
207 \& int RAND_status(void);
210 \& int RAND_load_file(const char *file, long max_bytes);
211 \& int RAND_write_file(const char *file);
212 \& const char *RAND_file_name(char *file, size_t num);
215 \& int RAND_egd(const char *path);
218 \& void RAND_set_rand_method(const RAND_METHOD *meth);
219 \& const RAND_METHOD *RAND_get_rand_method(void);
220 \& RAND_METHOD *RAND_SSLeay(void);
223 \& void RAND_cleanup(void);
226 \& /* For Win32 only */
227 \& void RAND_screen(void);
228 \& int RAND_event(UINT, WPARAM, LPARAM);
231 Since the introduction of the ENGINE API, the recommended way of controlling
232 default implementations is by using the ENGINE API functions. The default
233 \fBRAND_METHOD\fR, as set by \fIRAND_set_rand_method()\fR and returned by
234 \fIRAND_get_rand_method()\fR, is only used if no ENGINE has been set as the default
235 \*(L"rand\*(R" implementation. Hence, these two functions are no longer the recommened
236 way to control defaults.
238 If an alternative \fBRAND_METHOD\fR implementation is being used (either set
239 directly or as provided by an ENGINE module), then it is entirely responsible
240 for the generation and management of a cryptographically secure PRNG stream. The
241 mechanisms described below relate solely to the software PRNG implementation
242 built in to OpenSSL and used by default.
244 These functions implement a cryptographically secure pseudo-random
245 number generator (PRNG). It is used by other library functions for
246 example to generate random keys, and applications can use it when they
249 A cryptographic PRNG must be seeded with unpredictable data such as
250 mouse movements or keys pressed at random by the user. This is
251 described in RAND_add(3). Its state can be saved in a seed file
252 (see RAND_load_file(3)) to avoid having to go through the
253 seeding process whenever the application is started.
255 RAND_bytes(3) describes how to obtain random data from the
258 The \fIRAND_SSLeay()\fR method implements a PRNG based on a cryptographic
261 The following description of its design is based on the SSLeay
264 First up I will state the things I believe I need for a good RNG.
266 A good hashing algorithm to mix things up and to convert the \s-1RNG\s0 \*(L'state\*(R'
269 An initial source of random \*(L'state\*(R'.
271 The state should be very large. If the \s-1RNG\s0 is being used to generate
272 4096 bit \s-1RSA\s0 keys, 2 2048 bit random strings are required (at a minimum).
273 If your \s-1RNG\s0 state only has 128 bits, you are obviously limiting the
274 search space to 128 bits, not 2048. I'm probably getting a little
275 carried away on this last point but it does indicate that it may not be
276 a bad idea to keep quite a lot of \s-1RNG\s0 state. It should be easier to
277 break a cipher than guess the \s-1RNG\s0 seed data.
279 Any \s-1RNG\s0 seed data should influence all subsequent random numbers
280 generated. This implies that any random seed data entered will have
281 an influence on all subsequent random numbers generated.
283 When using data to seed the \s-1RNG\s0 state, the data used should not be
284 extractable from the \s-1RNG\s0 state. I believe this should be a
285 requirement because one possible source of \*(L'secret\*(R' semi random
286 data would be a private key or a password. This data must
287 not be disclosed by either subsequent random numbers or a
288 \&'core\*(R' dump left by a program crash.
290 Given the same initial \*(L'state\*(R', 2 systems should deviate in their \s-1RNG\s0 state
291 (and hence the random numbers generated) over time if at all possible.
293 Given the random number output stream, it should not be possible to determine
294 the \s-1RNG\s0 state or the next random number.
296 The algorithm is as follows.
298 There is global state made up of a 1023 byte buffer (the \*(L'state'), a
299 working hash value ('md'), and a counter ('count').
301 Whenever seed data is added, it is inserted into the \*(L'state\*(R' as
304 The input is chopped up into units of 20 bytes (or less for
305 the last block). Each of these blocks is run through the hash
306 function as follows: The data passed to the hash function
307 is the current \*(L'md\*(R', the same number of bytes from the \*(L'state\*(R'
308 (the location determined by in incremented looping index) as
309 the current \*(L'block\*(R', the new key data \*(L'block\*(R', and \*(L'count\*(R'
310 (which is incremented after each use).
311 The result of this is kept in \*(L'md\*(R' and also xored into the
312 \&'state\*(R' at the same locations that were used as input into the
314 believe this system addresses points 1 (hash function; currently
315 \s-1SHA\s0\-1), 3 (the \*(L'state'), 4 (via the \*(L'md'), 5 (by the use of a hash
318 When bytes are extracted from the \s-1RNG\s0, the following process is used.
319 For each group of 10 bytes (or less), we do the following:
321 Input into the hash function the local \*(L'md\*(R' (which is initialized from
322 the global \*(L'md\*(R' before any bytes are generated), the bytes that are to
323 be overwritten by the random bytes, and bytes from the \*(L'state\*(R'
324 (incrementing looping index). From this digest output (which is kept
325 in \*(L'md'), the top (up to) 10 bytes are returned to the caller and the
326 bottom 10 bytes are xored into the \*(L'state\*(R'.
328 Finally, after we have finished \*(L'num\*(R' random bytes for the caller,
329 \&'count\*(R' (which is incremented) and the local and global \*(L'md\*(R' are fed
330 into the hash function and the results are kept in the global \*(L'md\*(R'.
332 I believe the above addressed points 1 (use of \s-1SHA\s0\-1), 6 (by hashing
333 into the \*(L'state\*(R' the \*(L'old\*(R' data from the caller that is about to be
334 overwritten) and 7 (by not using the 10 bytes given to the caller to
335 update the \*(L'state\*(R', but they are used to update \*(L'md').
337 So of the points raised, only 2 is not addressed (but see
340 BN_rand(3), RAND_add(3),
341 RAND_load_file(3), RAND_egd(3),
343 RAND_set_rand_method(3),
348 .IX Name "rand - pseudo-random number generator"
352 .IX Header "SYNOPSIS"
354 .IX Header "DESCRIPTION"
356 .IX Header "INTERNALS"
372 .IX Header "SEE ALSO"