Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / rndcontrol / random.4
1 .\"
2 .\" random.c -- A strong random number generator
3 .\"
4 .\" Version 0.92, last modified 21-Sep-95
5 .\" 
6 .\" Copyright Theodore Ts'o, 1994, 1995.  All rights reserved.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\"    notice, and the entire permission notice in its entirety,
13 .\"    including the disclaimer of warranties.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\"    notice, this list of conditions and the following disclaimer in the
16 .\"    documentation and/or other materials provided with the distribution.
17 .\" 3. The name of the author may not be used to endorse or promote
18 .\"    products derived from this software without specific prior
19 .\"    written permission.
20 .\" 
21 .\" ALTERNATIVELY, this product may be distributed under the terms of
22 .\" the GNU Public License, in which case the provisions of the GPL are
23 .\" required INSTEAD OF the above restrictions.  (This clause is
24 .\" necessary due to a potential bad interaction between the GPL and
25 .\" the restrictions contained in a BSD-style copyright.)
26 .\" 
27 .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
28 .\" WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 .\" DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
31 .\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 .\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
37 .\" OF THE POSSIBILITY OF SUCH DAMAGE.
38 .\"
39 .\" $FreeBSD: src/usr.sbin/rndcontrol/random.4,v 1.9.2.2 2001/11/24 16:14:18 dd Exp $
40 .\"
41 .Dd October 21, 1995
42 .Dt RANDOM 4 i386
43 .Os
44 .Sh NAME
45 .Nm random ,
46 .Nm urandom
47 .Nd random number devices
48 .Sh DESCRIPTION
49 This device gathers environmental noise from device drivers, etc.,
50 and returns good random numbers, suitable for cryptographic use.
51 Besides the obvious cryptographic uses, these numbers are also good
52 for seeding TCP sequence numbers, and other places where it is
53 desirable to have numbers which are not only random, but hard to
54 predict by an attacker.
55 .Ss Theory of operation
56 Computers are very predictable devices.  Hence it is extremely hard
57 to produce truly random numbers on a computer \(em as opposed to
58 pseudo-random numbers, which can easily generated by using an
59 algorithm.  Unfortunately, it is very easy for attackers to guess
60 the sequence of pseudo-random number generators, and for some
61 applications this is not acceptable.  So instead, we must try to
62 gather "environmental noise" from the computer's environment, which
63 must be hard for outside attackers to observe, and use that to
64 generate random numbers.  In a Unix environment, this is best done
65 from inside the kernel.
66 .Pp
67 Sources of randomness from the environment include inter-keyboard
68 timings, inter-interrupt timings from some interrupts, and other
69 events which are both (a) non-deterministic and (b) hard for an
70 outside observer to measure.  Randomness from these sources are
71 added to an "entropy pool", which is periodically mixed using the
72 MD5 compression function in CBC mode.  As random bytes are mixed
73 into the entropy pool, the routines keep an
74 .Em estimate
75 of how many bits of randomness have been stored into the random number
76 generator's internal state.
77 .Pp
78 When random bytes are desired, they are obtained by taking the MD5
79 hash of a counter plus the contents of the "entropy pool".  The
80 reason for the MD5 hash is so that we can avoid exposing the
81 internal state of random number generator.  Although the MD5 hash
82 does protect the pool, each random byte which is generated from
83 the pool reveals some information which was derived from the
84 internal state, and thus increases the amount of information an
85 outside attacker has available to try to make some guesses about
86 the random number generator's internal state.  For this reason,
87 the routine decreases its internal estimate of how many bits of
88 "true randomness" are contained in the entropy pool as it outputs
89 random numbers.
90 .Pp
91 If this estimate goes to zero, the routine can still generate random
92 numbers; however it may now be possible for an attacker to analyze
93 the output of the random number generator, and the MD5 algorithm,
94 and thus have some success in guessing the output of the routine.
95 Phil Karn (who devised this mechanism of using MD5 plus a counter
96 to extract random numbers from an entropy pool) calls this
97 "practical randomness", since in the worst case this is equivalent
98 to hashing MD5 with a counter and an undisclosed secret.  If MD5 is
99 a strong cryptographic hash, this should be fairly resistant to attack.
100 .Ss Exported interfaces \(em output
101 There are three exported interfaces; the first is one designed to
102 be used from within the kernel:
103 .Pp
104 .Bl -tag -width Pa -compact
105 .It Pa void get_random_bytes(void *buf, int nbytes);
106 .El
107 .Pp
108 This interface will return the requested number of random bytes,
109 and place it in the requested buffer.
110 .Pp
111 The two other interfaces are two character devices
112 .Pa /dev/random
113 and
114 .Pa /dev/urandom .
115 The
116 .Pa /dev/random
117 device is suitable for use when very high quality randomness is desired
118 (e.g. for key generation), as it will only return a maximum
119 of the number of bits of randomness (as estimated by the random number
120 generator) contained in the entropy pool.
121 .Pp
122 The
123 .Pa /dev/urandom
124 device does not have this limit, and will return as many bytes as are
125 requested.  As more and more random bytes are requested without giving
126 time for the entropy pool to recharge, this will result in lower quality
127 random numbers.  For many applications, however, this is acceptable.
128 .Ss Exported interfaces \(em input
129 The two current exported interfaces for gathering environmental
130 noise from the devices are:
131 .Pp
132 .Bl -tag -width Pa -compact
133 .It Pa void add_keyboard_randomness(unsigned char scancode);
134 .It Pa void add_interrupt_randomness(int irq);
135 .El
136 .Pp
137 The first function uses the inter-keypress timing, as well as the
138 scancode as random inputs into the "entropy pool".
139 .Pp
140 The second function uses the inter-interrupt timing as random
141 inputs to the entropy pool.  Note that not all interrupts are good
142 sources of randomness!  For example, the timer interrupts is not a
143 good choice, because the periodicity of the interrupts is too
144 regular, and hence predictable to an attacker.  Disk interrupts are
145 a better measure, since the timing of the disk interrupts are more
146 unpredictable.  The routines try to estimate how many bits of
147 randomness a particular interrupt channel offers, by keeping track
148 of the first and second order deltas in the interrupt timings.
149 .Sh ACKNOWLEDGEMENTS
150 The original core code was written by
151 .An Theodore Ts'o ,
152 and was intended
153 for the Linux platform.
154 This was ported to
155 .Fx
156 by
157 .An Mark Murray ,
158 who also wrote the
159 .Xr rndcontrol 8
160 utility.
161 .Pp
162 Ideas for constructing this random number generator were derived
163 from the Pretty Good Privacy's random number generator, and from
164 private discussions with Phil Karn.  This design has been further
165 modified by myself, so any flaws are solely my responsibility, and
166 should not be attributed to the authors of PGP or to Phil.
167 .Pp
168 The code for MD5 transform was taken from Colin Plumb's
169 implementation, which has been placed in the public domain.  The
170 MD5 cryptographic checksum was devised by Ronald Rivest, and is
171 documented in RFC 1321, "The MD5 Message Digest Algorithm".
172 .Pp
173 Further background information on this topic may be obtained from
174 RFC 1750, "Randomness Recommendations for Security", by Donald
175 Eastlake, Steve Crocker, and Jeff Schiller.
176 .Sh "SEE ALSO"
177 .Xr rndcontrol 8
178 .Sh FILES
179 .Bl -tag -width Pa -compact
180 .It Pa /dev/random
181 .It Pa /dev/urandom
182 .El
183 .Sh HISTORY
184 The
185 .Pa random ,
186 .Pa urandom
187 files appeared in
188 .Fx 2.1.5 .