Initial import from FreeBSD RELENG_4:
[dragonfly.git] / secure / lib / libcrypto / man / rand.3
1 .\" Automatically generated by Pod::Man version 1.15
2 .\" Wed Feb 19 16:43:06 2003
3 .\"
4 .\" Standard preamble:
5 .\" ======================================================================
6 .de Sh \" Subsection heading
7 .br
8 .if t .Sp
9 .ne 5
10 .PP
11 \fB\\$1\fR
12 .PP
13 ..
14 .de Sp \" Vertical space (when we can't use .PP)
15 .if t .sp .5v
16 .if n .sp
17 ..
18 .de Ip \" List item
19 .br
20 .ie \\n(.$>=3 .ne \\$3
21 .el .ne 3
22 .IP "\\$1" \\$2
23 ..
24 .de Vb \" Begin verbatim text
25 .ft CW
26 .nf
27 .ne \\$1
28 ..
29 .de Ve \" End verbatim text
30 .ft R
31
32 .fi
33 ..
34 .\" Set up some character translations and predefined strings.  \*(-- will
35 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
36 .\" double quote, and \*(R" will give a right double quote.  | will give a
37 .\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used
38 .\" to do unbreakable dashes and therefore won't be available.  \*(C` and
39 .\" \*(C' expand to `' in nroff, nothing in troff, for use with C<>
40 .tr \(*W-|\(bv\*(Tr
41 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
42 .ie n \{\
43 .    ds -- \(*W-
44 .    ds PI pi
45 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
46 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
47 .    ds L" ""
48 .    ds R" ""
49 .    ds C` ""
50 .    ds C' ""
51 'br\}
52 .el\{\
53 .    ds -- \|\(em\|
54 .    ds PI \(*p
55 .    ds L" ``
56 .    ds R" ''
57 'br\}
58 .\"
59 .\" If the F register is turned on, we'll generate index entries on stderr
60 .\" for titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and
61 .\" index entries marked with X<> in POD.  Of course, you'll have to process
62 .\" the output yourself in some meaningful fashion.
63 .if \nF \{\
64 .    de IX
65 .    tm Index:\\$1\t\\n%\t"\\$2"
66 ..
67 .    nr % 0
68 .    rr F
69 .\}
70 .\"
71 .\" For nroff, turn off justification.  Always turn off hyphenation; it
72 .\" makes way too many mistakes in technical documents.
73 .hy 0
74 .if n .na
75 .\"
76 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
77 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
78 .bd B 3
79 .    \" fudge factors for nroff and troff
80 .if n \{\
81 .    ds #H 0
82 .    ds #V .8m
83 .    ds #F .3m
84 .    ds #[ \f1
85 .    ds #] \fP
86 .\}
87 .if t \{\
88 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
89 .    ds #V .6m
90 .    ds #F 0
91 .    ds #[ \&
92 .    ds #] \&
93 .\}
94 .    \" simple accents for nroff and troff
95 .if n \{\
96 .    ds ' \&
97 .    ds ` \&
98 .    ds ^ \&
99 .    ds , \&
100 .    ds ~ ~
101 .    ds /
102 .\}
103 .if t \{\
104 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
105 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
106 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
107 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
108 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
109 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
110 .\}
111 .    \" troff and (daisy-wheel) nroff accents
112 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
113 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
114 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
115 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
116 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
117 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
118 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
119 .ds ae a\h'-(\w'a'u*4/10)'e
120 .ds Ae A\h'-(\w'A'u*4/10)'E
121 .    \" corrections for vroff
122 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
123 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
124 .    \" for low resolution devices (crt and lpr)
125 .if \n(.H>23 .if \n(.V>19 \
126 \{\
127 .    ds : e
128 .    ds 8 ss
129 .    ds o a
130 .    ds d- d\h'-1'\(ga
131 .    ds D- D\h'-1'\(hy
132 .    ds th \o'bp'
133 .    ds Th \o'LP'
134 .    ds ae ae
135 .    ds Ae AE
136 .\}
137 .rm #[ #] #H #V #F C
138 .\" ======================================================================
139 .\"
140 .IX Title "rand 3"
141 .TH rand 3 "0.9.7a" "2003-02-19" "OpenSSL"
142 .UC
143 .SH "NAME"
144 rand \- pseudo-random number generator
145 .SH "SYNOPSIS"
146 .IX Header "SYNOPSIS"
147 .Vb 1
148 \& #include <openssl/rand.h>
149 .Ve
150 .Vb 1
151 \& int  RAND_set_rand_engine(ENGINE *engine);
152 .Ve
153 .Vb 2
154 \& int  RAND_bytes(unsigned char *buf, int num);
155 \& int  RAND_pseudo_bytes(unsigned char *buf, int num);
156 .Ve
157 .Vb 3
158 \& void RAND_seed(const void *buf, int num);
159 \& void RAND_add(const void *buf, int num, int entropy);
160 \& int  RAND_status(void);
161 .Ve
162 .Vb 3
163 \& int  RAND_load_file(const char *file, long max_bytes);
164 \& int  RAND_write_file(const char *file);
165 \& const char *RAND_file_name(char *file, size_t num);
166 .Ve
167 .Vb 1
168 \& int  RAND_egd(const char *path);
169 .Ve
170 .Vb 3
171 \& void RAND_set_rand_method(const RAND_METHOD *meth);
172 \& const RAND_METHOD *RAND_get_rand_method(void);
173 \& RAND_METHOD *RAND_SSLeay(void);
174 .Ve
175 .Vb 1
176 \& void RAND_cleanup(void);
177 .Ve
178 .Vb 3
179 \& /* For Win32 only */
180 \& void RAND_screen(void);
181 \& int RAND_event(UINT, WPARAM, LPARAM);
182 .Ve
183 .SH "DESCRIPTION"
184 .IX Header "DESCRIPTION"
185 Since the introduction of the \s-1ENGINE\s0 \s-1API\s0, the recommended way of controlling
186 default implementations is by using the \s-1ENGINE\s0 \s-1API\s0 functions. The default
187 \&\fB\s-1RAND_METHOD\s0\fR, as set by \fIRAND_set_rand_method()\fR and returned by
188 \&\fIRAND_get_rand_method()\fR, is only used if no \s-1ENGINE\s0 has been set as the default
189 \&\*(L"rand\*(R" implementation. Hence, these two functions are no longer the recommened
190 way to control defaults.
191 .PP
192 If an alternative \fB\s-1RAND_METHOD\s0\fR implementation is being used (either set
193 directly or as provided by an \s-1ENGINE\s0 module), then it is entirely responsible
194 for the generation and management of a cryptographically secure \s-1PRNG\s0 stream. The
195 mechanisms described below relate solely to the software \s-1PRNG\s0 implementation
196 built in to OpenSSL and used by default.
197 .PP
198 These functions implement a cryptographically secure pseudo-random
199 number generator (\s-1PRNG\s0). It is used by other library functions for
200 example to generate random keys, and applications can use it when they
201 need randomness.
202 .PP
203 A cryptographic \s-1PRNG\s0 must be seeded with unpredictable data such as
204 mouse movements or keys pressed at random by the user. This is
205 described in RAND_add(3). Its state can be saved in a seed file
206 (see RAND_load_file(3)) to avoid having to go through the
207 seeding process whenever the application is started.
208 .PP
209 RAND_bytes(3) describes how to obtain random data from the
210 \&\s-1PRNG\s0. 
211 .SH "INTERNALS"
212 .IX Header "INTERNALS"
213 The \fIRAND_SSLeay()\fR method implements a \s-1PRNG\s0 based on a cryptographic
214 hash function.
215 .PP
216 The following description of its design is based on the SSLeay
217 documentation:
218 .PP
219 First up I will state the things I believe I need for a good \s-1RNG\s0.
220 .Ip "1" 4
221 .IX Item "1"
222 A good hashing algorithm to mix things up and to convert the \s-1RNG\s0 'state'
223 to random numbers.
224 .Ip "2" 4
225 .IX Item "2"
226 An initial source of random 'state'.
227 .Ip "3" 4
228 .IX Item "3"
229 The state should be very large.  If the \s-1RNG\s0 is being used to generate
230 4096 bit \s-1RSA\s0 keys, 2 2048 bit random strings are required (at a minimum).
231 If your \s-1RNG\s0 state only has 128 bits, you are obviously limiting the
232 search space to 128 bits, not 2048.  I'm probably getting a little
233 carried away on this last point but it does indicate that it may not be
234 a bad idea to keep quite a lot of \s-1RNG\s0 state.  It should be easier to
235 break a cipher than guess the \s-1RNG\s0 seed data.
236 .Ip "4" 4
237 .IX Item "4"
238 Any \s-1RNG\s0 seed data should influence all subsequent random numbers
239 generated.  This implies that any random seed data entered will have
240 an influence on all subsequent random numbers generated.
241 .Ip "5" 4
242 .IX Item "5"
243 When using data to seed the \s-1RNG\s0 state, the data used should not be
244 extractable from the \s-1RNG\s0 state.  I believe this should be a
245 requirement because one possible source of 'secret' semi random
246 data would be a private key or a password.  This data must
247 not be disclosed by either subsequent random numbers or a
248 \&'core' dump left by a program crash.
249 .Ip "6" 4
250 .IX Item "6"
251 Given the same initial 'state', 2 systems should deviate in their \s-1RNG\s0 state
252 (and hence the random numbers generated) over time if at all possible.
253 .Ip "7" 4
254 .IX Item "7"
255 Given the random number output stream, it should not be possible to determine
256 the \s-1RNG\s0 state or the next random number.
257 .PP
258 The algorithm is as follows.
259 .PP
260 There is global state made up of a 1023 byte buffer (the 'state'), a
261 working hash value ('md'), and a counter ('count').
262 .PP
263 Whenever seed data is added, it is inserted into the 'state' as
264 follows.
265 .PP
266 The input is chopped up into units of 20 bytes (or less for
267 the last block).  Each of these blocks is run through the hash
268 function as follows:  The data passed to the hash function
269 is the current 'md', the same number of bytes from the 'state'
270 (the location determined by in incremented looping index) as
271 the current 'block', the new key data 'block', and 'count'
272 (which is incremented after each use).
273 The result of this is kept in 'md' and also xored into the
274 \&'state' at the same locations that were used as input into the
275 hash function. I
276 believe this system addresses points 1 (hash function; currently
277 \&\s-1SHA-1\s0), 3 (the 'state'), 4 (via the 'md'), 5 (by the use of a hash
278 function and xor).
279 .PP
280 When bytes are extracted from the \s-1RNG\s0, the following process is used.
281 For each group of 10 bytes (or less), we do the following:
282 .PP
283 Input into the hash function the local 'md' (which is initialized from
284 the global 'md' before any bytes are generated), the bytes that are to
285 be overwritten by the random bytes, and bytes from the 'state'
286 (incrementing looping index). From this digest output (which is kept
287 in 'md'), the top (up to) 10 bytes are returned to the caller and the
288 bottom 10 bytes are xored into the 'state'.
289 .PP
290 Finally, after we have finished 'num' random bytes for the caller,
291 \&'count' (which is incremented) and the local and global 'md' are fed
292 into the hash function and the results are kept in the global 'md'.
293 .PP
294 I believe the above addressed points 1 (use of \s-1SHA-1\s0), 6 (by hashing
295 into the 'state' the 'old' data from the caller that is about to be
296 overwritten) and 7 (by not using the 10 bytes given to the caller to
297 update the 'state', but they are used to update 'md').
298 .PP
299 So of the points raised, only 2 is not addressed (but see
300 RAND_add(3)).
301 .SH "SEE ALSO"
302 .IX Header "SEE ALSO"
303 BN_rand(3), RAND_add(3),
304 RAND_load_file(3), RAND_egd(3),
305 RAND_bytes(3),
306 RAND_set_rand_method(3),
307 RAND_cleanup(3)