Merge from vendor branch FILE:
[dragonfly.git] / secure / lib / libcrypto / man / rand.3
1 .rn '' }`
2 ''' $RCSfile$$Revision$$Date$
3 '''
4 ''' $Log$
5 '''
6 .de Sh
7 .br
8 .if t .Sp
9 .ne 5
10 .PP
11 \fB\\$1\fR
12 .PP
13 ..
14 .de Sp
15 .if t .sp .5v
16 .if n .sp
17 ..
18 .de Ip
19 .br
20 .ie \\n(.$>=3 .ne \\$3
21 .el .ne 3
22 .IP "\\$1" \\$2
23 ..
24 .de Vb
25 .ft CW
26 .nf
27 .ne \\$1
28 ..
29 .de Ve
30 .ft R
31
32 .fi
33 ..
34 '''
35 '''
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.
39 '''
40 .tr \(*W-|\(bv\*(Tr
41 .ie n \{\
42 .ds -- \(*W-
43 .ds PI pi
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
46 .ds L" ""
47 .ds R" ""
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
52 .ds M" """
53 .ds S" """
54 .ds N" """""
55 .ds T" """""
56 .ds L' '
57 .ds R' '
58 .ds M' '
59 .ds S' '
60 .ds N' '
61 .ds T' '
62 'br\}
63 .el\{\
64 .ds -- \(em\|
65 .tr \*(Tr
66 .ds L" ``
67 .ds R" ''
68 .ds M" ``
69 .ds S" ''
70 .ds N" ``
71 .ds T" ''
72 .ds L' `
73 .ds R' '
74 .ds M' `
75 .ds S' '
76 .ds N' `
77 .ds T' '
78 .ds PI \(*p
79 'br\}
80 .\"     If the F register is turned on, we'll generate
81 .\"     index entries out stderr for the following things:
82 .\"             TH      Title 
83 .\"             SH      Header
84 .\"             Sh      Subsection 
85 .\"             Ip      Item
86 .\"             X<>     Xref  (embedded
87 .\"     Of course, you have to process the output yourself
88 .\"     in some meaninful fashion.
89 .if \nF \{
90 .de IX
91 .tm Index:\\$1\t\\n%\t"\\$2"
92 ..
93 .nr % 0
94 .rr F
95 .\}
96 .TH rand 3 "0.9.7d" "2/Sep/2004" "OpenSSL"
97 .UC
98 .if n .hy 0
99 .if n .na
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
102 .ft CW
103 'if n "\c
104 'if t \\&\\$1\c
105 'if n \\&\\$1\c
106 'if n \&"
107 \\&\\$2 \\$3 \\$4 \\$5 \\$6 \\$7
108 '.ft R
109 ..
110 .\" @(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2
111 .       \" AM - accent mark definitions
112 .bd B 3
113 .       \" fudge factors for nroff and troff
114 .if n \{\
115 .       ds #H 0
116 .       ds #V .8m
117 .       ds #F .3m
118 .       ds #[ \f1
119 .       ds #] \fP
120 .\}
121 .if t \{\
122 .       ds #H ((1u-(\\\\n(.fu%2u))*.13m)
123 .       ds #V .6m
124 .       ds #F 0
125 .       ds #[ \&
126 .       ds #] \&
127 .\}
128 .       \" simple accents for nroff and troff
129 .if n \{\
130 .       ds ' \&
131 .       ds ` \&
132 .       ds ^ \&
133 .       ds , \&
134 .       ds ~ ~
135 .       ds ? ?
136 .       ds ! !
137 .       ds /
138 .       ds q
139 .\}
140 .if t \{\
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'
150 .\}
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 \
172 \{\
173 .       ds : e
174 .       ds 8 ss
175 .       ds v \h'-1'\o'\(aa\(ga'
176 .       ds _ \h'-1'^
177 .       ds . \h'-1'.
178 .       ds 3 3
179 .       ds o a
180 .       ds d- d\h'-1'\(ga
181 .       ds D- D\h'-1'\(hy
182 .       ds th \o'bp'
183 .       ds Th \o'LP'
184 .       ds ae ae
185 .       ds Ae AE
186 .       ds oe oe
187 .       ds Oe OE
188 .\}
189 .rm #[ #] #H #V #F C
190 .SH "NAME"
191 rand \- pseudo-random number generator
192 .SH "SYNOPSIS"
193 .PP
194 .Vb 1
195 \& #include <openssl/rand.h>
196 .Ve
197 .Vb 1
198 \& int  RAND_set_rand_engine(ENGINE *engine);
199 .Ve
200 .Vb 2
201 \& int  RAND_bytes(unsigned char *buf, int num);
202 \& int  RAND_pseudo_bytes(unsigned char *buf, int num);
203 .Ve
204 .Vb 3
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);
208 .Ve
209 .Vb 3
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);
213 .Ve
214 .Vb 1
215 \& int  RAND_egd(const char *path);
216 .Ve
217 .Vb 3
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);
221 .Ve
222 .Vb 1
223 \& void RAND_cleanup(void);
224 .Ve
225 .Vb 3
226 \& /* For Win32 only */
227 \& void RAND_screen(void);
228 \& int RAND_event(UINT, WPARAM, LPARAM);
229 .Ve
230 .SH "DESCRIPTION"
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.
237 .PP
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.
243 .PP
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
247 need randomness.
248 .PP
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.
254 .PP
255 RAND_bytes(3) describes how to obtain random data from the
256 PRNG. 
257 .SH "INTERNALS"
258 The \fIRAND_SSLeay()\fR method implements a PRNG based on a cryptographic
259 hash function.
260 .PP
261 The following description of its design is based on the SSLeay
262 documentation:
263 .PP
264 First up I will state the things I believe I need for a good RNG.
265 .Ip "1" 4
266 A good hashing algorithm to mix things up and to convert the \s-1RNG\s0 \*(L'state\*(R'
267 to random numbers.
268 .Ip "2" 4
269 An initial source of random \*(L'state\*(R'.
270 .Ip "3" 4
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.
278 .Ip "4" 4
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.
282 .Ip "5" 4
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.
289 .Ip "6" 4
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.
292 .Ip "7" 4
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.
295 .PP
296 The algorithm is as follows.
297 .PP
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').
300 .PP
301 Whenever seed data is added, it is inserted into the \*(L'state\*(R' as
302 follows.
303 .PP
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
313 hash function. I
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
316 function and xor).
317 .PP
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:
320 .PP
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'.
327 .PP
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'.
331 .PP
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').
336 .PP
337 So of the points raised, only 2 is not addressed (but see
338 RAND_add(3)).
339 .SH "SEE ALSO"
340 BN_rand(3), RAND_add(3),
341 RAND_load_file(3), RAND_egd(3),
342 RAND_bytes(3),
343 RAND_set_rand_method(3),
344 RAND_cleanup(3) 
345
346 .rn }` ''
347 .IX Title "rand 3"
348 .IX Name "rand - pseudo-random number generator"
349
350 .IX Header "NAME"
351
352 .IX Header "SYNOPSIS"
353
354 .IX Header "DESCRIPTION"
355
356 .IX Header "INTERNALS"
357
358 .IX Item "1"
359
360 .IX Item "2"
361
362 .IX Item "3"
363
364 .IX Item "4"
365
366 .IX Item "5"
367
368 .IX Item "6"
369
370 .IX Item "7"
371
372 .IX Header "SEE ALSO"
373