]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - secure/lib/libcrypto/man/rand.3
This commit was generated by cvs2svn to compensate for changes in r98567,
[FreeBSD/FreeBSD.git] / secure / lib / libcrypto / man / rand.3
1 .\" Automatically generated by Pod::Man version 1.15
2 .\" Thu May  9 13:19:34 2002
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 1"
141 .TH RAND 1 "perl v5.6.1" "2001-07-19" "User Contributed Perl Documentation"
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 2
151 \& int  RAND_bytes(unsigned char *buf, int num);
152 \& int  RAND_pseudo_bytes(unsigned char *buf, int num);
153 .Ve
154 .Vb 4
155 \& void RAND_seed(const void *buf, int num);
156 \& void RAND_add(const void *buf, int num, int entropy);
157 \& int  RAND_status(void);
158 \& void RAND_screen(void);
159 .Ve
160 .Vb 3
161 \& int  RAND_load_file(const char *file, long max_bytes);
162 \& int  RAND_write_file(const char *file);
163 \& const char *RAND_file_name(char *file, size_t num);
164 .Ve
165 .Vb 1
166 \& int  RAND_egd(const char *path);
167 .Ve
168 .Vb 3
169 \& void RAND_set_rand_method(RAND_METHOD *meth);
170 \& RAND_METHOD *RAND_get_rand_method(void);
171 \& RAND_METHOD *RAND_SSLeay(void);
172 .Ve
173 .Vb 1
174 \& void RAND_cleanup(void);
175 .Ve
176 .SH "DESCRIPTION"
177 .IX Header "DESCRIPTION"
178 These functions implement a cryptographically secure pseudo-random
179 number generator (\s-1PRNG\s0). It is used by other library functions for
180 example to generate random keys, and applications can use it when they
181 need randomness.
182 .PP
183 A cryptographic \s-1PRNG\s0 must be seeded with unpredictable data such as
184 mouse movements or keys pressed at random by the user. This is
185 described in RAND_add(3). Its state can be saved in a seed file
186 (see RAND_load_file(3)) to avoid having to go through the
187 seeding process whenever the application is started.
188 .PP
189 RAND_bytes(3) describes how to obtain random data from the
190 \&\s-1PRNG\s0. 
191 .SH "INTERNALS"
192 .IX Header "INTERNALS"
193 The \fIRAND_SSLeay()\fR method implements a \s-1PRNG\s0 based on a cryptographic
194 hash function.
195 .PP
196 The following description of its design is based on the SSLeay
197 documentation:
198 .PP
199 First up I will state the things I believe I need for a good \s-1RNG\s0.
200 .Ip "1" 4
201 .IX Item "1"
202 A good hashing algorithm to mix things up and to convert the \s-1RNG\s0 'state'
203 to random numbers.
204 .Ip "2" 4
205 .IX Item "2"
206 An initial source of random 'state'.
207 .Ip "3" 4
208 .IX Item "3"
209 The state should be very large.  If the \s-1RNG\s0 is being used to generate
210 4096 bit \s-1RSA\s0 keys, 2 2048 bit random strings are required (at a minimum).
211 If your \s-1RNG\s0 state only has 128 bits, you are obviously limiting the
212 search space to 128 bits, not 2048.  I'm probably getting a little
213 carried away on this last point but it does indicate that it may not be
214 a bad idea to keep quite a lot of \s-1RNG\s0 state.  It should be easier to
215 break a cipher than guess the \s-1RNG\s0 seed data.
216 .Ip "4" 4
217 .IX Item "4"
218 Any \s-1RNG\s0 seed data should influence all subsequent random numbers
219 generated.  This implies that any random seed data entered will have
220 an influence on all subsequent random numbers generated.
221 .Ip "5" 4
222 .IX Item "5"
223 When using data to seed the \s-1RNG\s0 state, the data used should not be
224 extractable from the \s-1RNG\s0 state.  I believe this should be a
225 requirement because one possible source of 'secret' semi random
226 data would be a private key or a password.  This data must
227 not be disclosed by either subsequent random numbers or a
228 \&'core' dump left by a program crash.
229 .Ip "6" 4
230 .IX Item "6"
231 Given the same initial 'state', 2 systems should deviate in their \s-1RNG\s0 state
232 (and hence the random numbers generated) over time if at all possible.
233 .Ip "7" 4
234 .IX Item "7"
235 Given the random number output stream, it should not be possible to determine
236 the \s-1RNG\s0 state or the next random number.
237 .PP
238 The algorithm is as follows.
239 .PP
240 There is global state made up of a 1023 byte buffer (the 'state'), a
241 working hash value ('md'), and a counter ('count').
242 .PP
243 Whenever seed data is added, it is inserted into the 'state' as
244 follows.
245 .PP
246 The input is chopped up into units of 20 bytes (or less for
247 the last block).  Each of these blocks is run through the hash
248 function as follows:  The data passed to the hash function
249 is the current 'md', the same number of bytes from the 'state'
250 (the location determined by in incremented looping index) as
251 the current 'block', the new key data 'block', and 'count'
252 (which is incremented after each use).
253 The result of this is kept in 'md' and also xored into the
254 \&'state' at the same locations that were used as input into the
255 hash function. I
256 believe this system addresses points 1 (hash function; currently
257 \&\s-1SHA-1\s0), 3 (the 'state'), 4 (via the 'md'), 5 (by the use of a hash
258 function and xor).
259 .PP
260 When bytes are extracted from the \s-1RNG\s0, the following process is used.
261 For each group of 10 bytes (or less), we do the following:
262 .PP
263 Input into the hash function the local 'md' (which is initialized from
264 the global 'md' before any bytes are generated), the bytes that are to
265 be overwritten by the random bytes, and bytes from the 'state'
266 (incrementing looping index). From this digest output (which is kept
267 in 'md'), the top (up to) 10 bytes are returned to the caller and the
268 bottom 10 bytes are xored into the 'state'.
269 .PP
270 Finally, after we have finished 'num' random bytes for the caller,
271 \&'count' (which is incremented) and the local and global 'md' are fed
272 into the hash function and the results are kept in the global 'md'.
273 .PP
274 I believe the above addressed points 1 (use of \s-1SHA-1\s0), 6 (by hashing
275 into the 'state' the 'old' data from the caller that is about to be
276 overwritten) and 7 (by not using the 10 bytes given to the caller to
277 update the 'state', but they are used to update 'md').
278 .PP
279 So of the points raised, only 2 is not addressed (but see
280 RAND_add(3)).
281 .SH "SEE ALSO"
282 .IX Header "SEE ALSO"
283 BN_rand(3), RAND_add(3),
284 RAND_load_file(3), RAND_egd(3),
285 RAND_bytes(3),
286 RAND_set_rand_method(3),
287 RAND_cleanup(3)