Add missing \ to string
[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 .\" $DragonFly: src/usr.sbin/rndcontrol/random.4,v 1.2 2003/06/17 04:30:02 dillon Exp $
41 .\"
42 .Dd October 21, 1995
43 .Dt RANDOM 4 i386
44 .Os
45 .Sh NAME
46 .Nm random ,
47 .Nm urandom
48 .Nd random number devices
49 .Sh DESCRIPTION
50 This device gathers environmental noise from device drivers, etc.,
51 and returns good random numbers, suitable for cryptographic use.
52 Besides the obvious cryptographic uses, these numbers are also good
53 for seeding TCP sequence numbers, and other places where it is
54 desirable to have numbers which are not only random, but hard to
55 predict by an attacker.
56 .Ss Theory of operation
57 Computers are very predictable devices.  Hence it is extremely hard
58 to produce truly random numbers on a computer \(em as opposed to
59 pseudo-random numbers, which can easily generated by using an
60 algorithm.  Unfortunately, it is very easy for attackers to guess
61 the sequence of pseudo-random number generators, and for some
62 applications this is not acceptable.  So instead, we must try to
63 gather "environmental noise" from the computer's environment, which
64 must be hard for outside attackers to observe, and use that to
65 generate random numbers.  In a Unix environment, this is best done
66 from inside the kernel.
67 .Pp
68 Sources of randomness from the environment include inter-keyboard
69 timings, inter-interrupt timings from some interrupts, and other
70 events which are both (a) non-deterministic and (b) hard for an
71 outside observer to measure.  Randomness from these sources are
72 added to an "entropy pool", which is periodically mixed using the
73 MD5 compression function in CBC mode.  As random bytes are mixed
74 into the entropy pool, the routines keep an
75 .Em estimate
76 of how many bits of randomness have been stored into the random number
77 generator's internal state.
78 .Pp
79 When random bytes are desired, they are obtained by taking the MD5
80 hash of a counter plus the contents of the "entropy pool".  The
81 reason for the MD5 hash is so that we can avoid exposing the
82 internal state of random number generator.  Although the MD5 hash
83 does protect the pool, each random byte which is generated from
84 the pool reveals some information which was derived from the
85 internal state, and thus increases the amount of information an
86 outside attacker has available to try to make some guesses about
87 the random number generator's internal state.  For this reason,
88 the routine decreases its internal estimate of how many bits of
89 "true randomness" are contained in the entropy pool as it outputs
90 random numbers.
91 .Pp
92 If this estimate goes to zero, the routine can still generate random
93 numbers; however it may now be possible for an attacker to analyze
94 the output of the random number generator, and the MD5 algorithm,
95 and thus have some success in guessing the output of the routine.
96 Phil Karn (who devised this mechanism of using MD5 plus a counter
97 to extract random numbers from an entropy pool) calls this
98 "practical randomness", since in the worst case this is equivalent
99 to hashing MD5 with a counter and an undisclosed secret.  If MD5 is
100 a strong cryptographic hash, this should be fairly resistant to attack.
101 .Ss Exported interfaces \(em output
102 There are three exported interfaces; the first is one designed to
103 be used from within the kernel:
104 .Pp
105 .Bl -tag -width Pa -compact
106 .It Pa void get_random_bytes(void *buf, int nbytes);
107 .El
108 .Pp
109 This interface will return the requested number of random bytes,
110 and place it in the requested buffer.
111 .Pp
112 The two other interfaces are two character devices
113 .Pa /dev/random
114 and
115 .Pa /dev/urandom .
116 The
117 .Pa /dev/random
118 device is suitable for use when very high quality randomness is desired
119 (e.g. for key generation), as it will only return a maximum
120 of the number of bits of randomness (as estimated by the random number
121 generator) contained in the entropy pool.
122 .Pp
123 The
124 .Pa /dev/urandom
125 device does not have this limit, and will return as many bytes as are
126 requested.  As more and more random bytes are requested without giving
127 time for the entropy pool to recharge, this will result in lower quality
128 random numbers.  For many applications, however, this is acceptable.
129 .Ss Exported interfaces \(em input
130 The two current exported interfaces for gathering environmental
131 noise from the devices are:
132 .Pp
133 .Bl -tag -width Pa -compact
134 .It Pa void add_keyboard_randomness(unsigned char scancode);
135 .It Pa void add_interrupt_randomness(int irq);
136 .El
137 .Pp
138 The first function uses the inter-keypress timing, as well as the
139 scancode as random inputs into the "entropy pool".
140 .Pp
141 The second function uses the inter-interrupt timing as random
142 inputs to the entropy pool.  Note that not all interrupts are good
143 sources of randomness!  For example, the timer interrupts is not a
144 good choice, because the periodicity of the interrupts is too
145 regular, and hence predictable to an attacker.  Disk interrupts are
146 a better measure, since the timing of the disk interrupts are more
147 unpredictable.  The routines try to estimate how many bits of
148 randomness a particular interrupt channel offers, by keeping track
149 of the first and second order deltas in the interrupt timings.
150 .Sh ACKNOWLEDGEMENTS
151 The original core code was written by
152 .An Theodore Ts'o ,
153 and was intended
154 for the Linux platform.
155 This was ported to
156 .Fx
157 by
158 .An Mark Murray ,
159 who also wrote the
160 .Xr rndcontrol 8
161 utility.
162 .Pp
163 Ideas for constructing this random number generator were derived
164 from the Pretty Good Privacy's random number generator, and from
165 private discussions with Phil Karn.  This design has been further
166 modified by myself, so any flaws are solely my responsibility, and
167 should not be attributed to the authors of PGP or to Phil.
168 .Pp
169 The code for MD5 transform was taken from Colin Plumb's
170 implementation, which has been placed in the public domain.  The
171 MD5 cryptographic checksum was devised by Ronald Rivest, and is
172 documented in RFC 1321, "The MD5 Message Digest Algorithm".
173 .Pp
174 Further background information on this topic may be obtained from
175 RFC 1750, "Randomness Recommendations for Security", by Donald
176 Eastlake, Steve Crocker, and Jeff Schiller.
177 .Sh "SEE ALSO"
178 .Xr rndcontrol 8
179 .Sh FILES
180 .Bl -tag -width Pa -compact
181 .It Pa /dev/random
182 .It Pa /dev/urandom
183 .El
184 .Sh HISTORY
185 The
186 .Pa random ,
187 .Pa urandom
188 files appeared in
189 .Fx 2.1.5 .