1 .\" $NetBSD: rnd.4,v 1.14 2009/02/22 12:18:32 wiz Exp $
3 .\" Copyright (c) 1997 Michael Graff
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. The name of the author may not be used to endorse or promote products
15 .\" derived from this software without specific prior written permission.
17 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 .\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 .\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .Nd in kernel entropy collection and random number generation
40 pseudo-device uses event timing information collected from many
41 devices, and mixes this into an entropy pool.
42 This pool is stirred with a cryptographically strong hash function
43 when data is extracted from the pool.
44 .Sh INTERNAL ENTROPY POOL MANAGEMENT
45 When a hardware event occurs (such as completion of a hard drive
46 transfer or an interrupt from a network device) a timestamp is
48 This timestamp is compared to the previous timestamp
49 recorded for the device, and the first, second, and third order
50 differentials are calculated.
52 If any of these differentials is zero, no entropy is assumed to
54 If all are non-zero, one bit is assumed.
55 Next, data is mixed into the entropy pool using an LFSR (linear
56 feedback shift register).
58 To extract data from the entropy pool, a cryptographically strong hash
60 The output of this hash is mixed back into the pool using the LFSR,
61 and then folded in half before being returned to the caller.
63 Mixing the actual hash into the pool causes the next extraction to
64 return a different value, even if no timing events were added to the
66 Folding the data in half prevents the caller to derive the
67 actual hash of the pool, preventing some attacks.
69 User code can obtain random values from the kernel in two ways.
73 will only return values while sufficient entropy exists in the
75 When sufficient entropy does not exist,
77 is returned for non-blocking reads, or the read will block for
82 will return as many values as requested, even when the entropy pool is
84 This data is not as good as reading from
86 since when the pool is empty, data is still returned, degenerating to a
87 pseudo-random generator.
89 Writing to either device will mix the data written into the pool using
90 the LFSR as above, without modifying the entropy estimation for the
92 .Sh RANDOM SOURCE STRUCTURE
93 Each source has a state structure which the kernel uses to hold the
94 timing information and other state for that source.
95 .Bd -literal -offset indent
100 uint32_t last_delta2;
107 This structure holds the internal representation of a device's timing
111 field holes the device name, as known to the kernel.
114 entry is the timestamp of the last time this device generated an
116 It is for internal use only, and not in any specific representation.
121 fields hold the last first- and second-order deltas.
124 field holds a count of how many bits this device has potentially
126 This is not the same as how many bits were used from it.
129 field holds the device type.
131 Currently, these types are defined:
132 .Bl -tag -width RND_TYPE_DISK
134 The device is a physical hard drive.
136 The device is a network interface.
137 By default, timing information is
138 collected from this source type, but entropy is not estimated.
140 The device is a tape device.
142 The device is a terminal, mouse, or other user input device.
144 The device is a random number generator.
149 .Bl -tag -width RND_FLAG_NO_ESTIMATE
150 .It Dv RND_FLAG_NO_ESTIMATE
151 Do not assume any entropy is in the timing information.
152 .It Dv RND_FLAG_NO_COLLECT
153 Do not even add timing information to the pool.
158 functions are available to control device behavior, gather statistics,
159 and add data to the entropy pool.
160 These are all defined in the
162 file, along with the data types and constants.
164 .Bl -tag -width RNDADDTOENTCNT
167 Return the current entropy count (in bits).
168 .It Dv RNDGETPOOLSTAT
169 .Pq Li "rndpoolstat_t"
170 .Bd -literal -offset indent
185 Return statistics on the current state of the random collection pool.
188 .Bd -literal -offset indent
192 rndsource_t source[RND_MAXSTATCOUNT];
196 Return data for sources, starting at
198 and returning at most
202 The values returned are actual in-kernel snapshots of the entropy
204 Leaking the internal timing information will weaken security.
206 .Pq Li "rndstat_name_t"
207 .Bd -literal -offset indent
214 Return the device state for a named device.
217 .Bd -literal -offset indent
226 Change bits in the device state information.
229 is 0xff, only the device name stored in
232 If it is any other value, all devices of type
235 This allows all network interfaces to be disabled for
236 entropy collection with one call, for example.
241 work together to change flag bits.
244 field specifies which bits in
246 are to be set or cleared.
249 .Bd -literal -offset indent
253 u_char data[RND_POOLWORDS * 4];
258 .Bl -tag -width /dev/urandomx -compact
260 Returns ``good'' values only
262 Always returns data, degenerates to a pseudo-random generator
268 The random device was first made available in
271 This implementation was written by Michael Graff \*[Lt]explorer@flame.org\*[Gt]
272 using ideas and algorithms gathered from many sources, including
273 the driver written by Ted Ts'o.