xref: /netbsd-src/share/man/man4/rnd.4 (revision a4ddc2c8fb9af816efe3b1c375a5530aef0e89e9)
1.\"	$NetBSD: rnd.4,v 1.20 2012/04/17 08:28:20 wiz Exp $
2.\"
3.\" Copyright (c) 1997 Michael Graff
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
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.
16.\"
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
27.\" SUCH DAMAGE.
28.\"
29.Dd April 17, 2012
30.Dt RND 4
31.Os
32.Sh NAME
33.Nm rnd
34.Nd in kernel entropy collection and random number generation
35.Sh SYNOPSIS
36.Cd pseudo-device rnd
37.Sh DESCRIPTION
38The
39.Nm
40pseudo-device has three purposes.
41On read, it returns cryptographically
42strong random data from a generator keyed from the kernel entropy pool.
43On write, data may be added to the entropy pool.
44By ioctl, the behavior of the entropy pool (which sources are used;
45how their entropy is estimated, etc.) may be controlled.
46.Pp
47The kernel uses event timing information collected from many
48devices, and mixes this into an entropy pool.
49This pool is used to
50key a stream generator (the CTR_DRBG generator specified by NIST
51SP 800-90) which is used to generate values returned to userspace when
52the pseudo-device is read.
53.Pp
54The pseudodevice is cloning, which means that each time it is opened,
55a new instance of the stream generator may be created.
56Interposing a stream
57generator between the entropy pool and readers in this manner protects
58readers from each other (each reader's random stream is generated from a
59unique key) and protects all users of the entropy pool from any attack
60which might correlate its successive outputs to each other, such as
61iterative guessing attacks.
62.Pp
63Certain programs make very short reads from
64.Pa /dev/urandom
65each time they begin execution.
66One program with this behavior is
67.Xr perl 1 .
68If such a program is run repeatedly (for example from a network
69service or shell script), the resulting repeated keying of the stream
70generator can quickly drain the entropy pool dry.
71As an optimization for such cases, a separate per-CPU instance of
72the stream generator is used to handle reads from
73.Pa /dev/urandom
74which are smaller than the key length of the underlying cipher.
75Any read of a larger size causes an immediate allocation of a
76private instance of the stream generator for the reader.
77Since all stream
78generators are automatically rekeyed upon use when sufficient entropy
79is available, the shared short-request generators do still offer
80some protection against other consumers of
81.Pa /dev/urandom ,
82though less than is provided for consumers making larger requests.
83.Sh USER ACCESS
84User code can obtain random values from the kernel in two ways.
85.Pp
86Reading from
87.Pa /dev/random
88provides information-theoretic properties desirable for some callers:
89it will guarantee that the stream generator never outputs more bits
90than the length of its key, which may in some sense mean that all the
91entropy provided to it by the entropy pool is "preserved" in its output.
92.Pp
93Reading from
94.Pa /dev/random
95may return
96.Er EAGAIN
97(for non-blocking reads), block, or return less data than requested, if
98the pool does not have sufficient entropy
99to provide a new key for the stream generator when sufficient bits have
100been read to require rekeying.
101.Pp
102Reading from
103.Pa /dev/urandom
104will return as many values as requested.
105The stream generator may be
106initially keyed from the entropy pool even if the pool's estimate of
107its own entropy is less than the number of bits in the stream generator's
108key.
109If this occurs, the generator will be rekeyed with fresh entropy
110from the pool as soon as sufficient entropy becomes available.
111The generator will also be rekeyed whenever the pool's entropy estimate
112exceeds the size of the pool's internal state (when the pool "is full").
113.Pp
114In some sense, this data is not as good as reading from
115.Pa /dev/random ,
116for at least two reasons.
117First, the generator may initially be keyed
118from a pool that has never had as many bits of entropy mixed into it as
119there are bits in the generator's key.
120Second, the generator may produce
121many more bits of output than are contained in its own key, though it
122will never produce more output on one key than is allowed by the
123CTR_DRBG specification.
124.Pp
125However, reading large amounts of data from a single opened instance of
126.Pa /dev/urandom
127will
128.Em not
129deplete the kernel entropy pool, as it would with some other
130implementations.
131This preserves entropy for other callers and will
132produce a more fair distribution of the available entropy over many
133potential readers on the same system.
134.Pp
135Users of these interfaces must carefully consider their application's
136actual security requirements and the characteristics of the system
137on which they are reading from the pseudodevice.
138For many applications, the depletion of the entropy pool caused by the
139.Pa /dev/random
140pseudodevice's continual rekeying of the stream generator will cause
141application behavior (or, perhaps more precisely, nonbehavior) which
142is less secure than relying on the
143.Pa /dev/urandom
144interface, which is guaranteed to rekey the stream generator as often
145as it can.
146.Pp
147Excessive use of
148.Pa /dev/random
149can deplete the entropy pool (or, at least, its estimate of how many
150bits of entropy it "contains") and reduce security for other consumers
151of randomness both in userspace
152.Em and within the kernel .
153Some system administrators may wish therefore to remove the
154.Pa /dev/random
155device node and replace it with a second copy of the node for the nonblocking
156.Pa /dev/urandom
157device.
158.Pp
159In any event, as the Linux manual page notes, one should
160be very suspicious of any application which attempts to read more than
16132 bytes (256 bits) from the blocking
162.Pa /dev/random
163pseudodevice, since no practical cryptographic algorithm in current
164use is believed to have a security strength greater than 256 bits.
165.Pp
166Writing to either device node will mix the data written into the
167entropy pool, but will have no effect on the pool's entropy estimate.
168The
169.Xr ioctl 2
170interface to the device may be used -- once only, and only when the
171system is in insecure mode at security level 0 or lower -- to add
172data with an explicit entropy estimate.
173.Sh IOCTL INTERFACE
174Various
175.Xr ioctl 2
176functions are available to control device behavior, gather statistics,
177and add data to the entropy pool.
178These are all defined in the
179.In sys/rnd.h
180file, along with the data types and constants.
181The structures and ioctl functions are also listed below.
182.Sh DATA TYPES
183Each source has a state structure which summarizes the kernel's state
184for that entropy source.
185.Bd -literal -offset indent
186typedef struct {
187        char            name[16];
188        uint32_t	total;
189        uint32_t	type;
190	uint32_t	flags;
191} rndsource_t;
192.Ed
193The
194.Va name
195field holds the device name, as known to the kernel.
196The
197.Va type
198field holds the device type.
199.Pp
200Currently, these types are defined:
201.Bl -tag -width RND_TYPE_DISK
202.It Dv RND_TYPE_DISK
203The device is a physical hard drive.
204.It Dv RND_TYPE_ENV
205The device is an environmental sensor such as a temperature sensor or
206a fan speed sensor.
207.It Dv RND_TYPE_NET
208The device is a network interface.
209By default, timing information is
210collected from this source type, but entropy is not estimated.
211.It Dv RND_TYPE_POWER
212The device is a sensor returning changes in the power state of the
213system, such as battery charge state or A/C adapter state.
214.It Dv RND_TYPE_RNG
215The device is a random number generator.
216.It Dv RND_TYPE_SKEW
217The "device" is a measurement of the skew between two clocks, such as a
218periodic device interrupt and the system timecounter, a timecounter and
219an audio codec, or some other source of pairs of events where each
220member of each pair is derived from a different instance of some
221recurring physical process.
222.It Dv RND_TYPE_TAPE
223The device is a tape device.
224.It Dv RND_TYPE_TTY
225The device is a terminal, mouse, or other user input device.
226.It Dv RND_TYPE_VM
227The "device" consists of timings of virtual memory system events.
228.El
229.Pp
230.Va flags
231is a bitfield.
232.Bl -tag -width RND_FLAG_NO_ESTIMATE
233.It Dv RND_FLAG_NO_COLLECT
234Do not even add timing information to the pool.
235.It Dv RND_FLAG_NO_ESTIMATE
236Do not assume any entropy is in the timing information.
237.El
238.Pp
239.Bl -tag -width RNDADDTOENTCNT
240.It Dv RNDGETENTCNT
241.Pq Li "uint32_t"
242Return the current entropy count (in bits).
243.It Dv RNDGETPOOLSTAT
244.Pq Li "rndpoolstat_t"
245.Bd -literal -offset indent
246typedef struct
247{
248	uint32_t	poolsize;
249	uint32_t 	threshold;
250	uint32_t	maxentropy;
251
252	uint32_t	added;
253	uint32_t	curentropy;
254	uint32_t	removed;
255	uint32_t	discarded;
256	uint32_t	generated;
257} rndpoolstat_t;
258.Ed
259.Pp
260Return statistics on the current state of the random collection pool.
261.It Dv RNDGETSRCNUM
262.Pq Li "rndstat_t"
263.Bd -literal -offset indent
264typedef struct {
265        uint32_t       start;
266        uint32_t       count;
267        rndsource_t     source[RND_MAXSTATCOUNT];
268} rndstat_t;
269.Ed
270.Pp
271Return data for sources, starting at
272.Va start
273and returning at most
274.Va count
275sources.
276.Pp
277The values returned are actual in-kernel snapshots of the entropy
278status for devices.
279Leaking the internal timing information will weaken security.
280.It Dv RNDGETSRCNAME
281.Pq Li "rndstat_name_t"
282.Bd -literal -offset indent
283typedef struct {
284        char            name[16];
285        rndsource_t     source;
286} rndstat_name_t;
287.Ed
288.Pp
289Return the device state for a named device.
290.It Dv RNDCTL
291.Pq Li "rndctl_t"
292.Bd -literal -offset indent
293typedef struct {
294        char            name[16];
295        uint32_t       type;
296        uint32_t       flags;
297        uint32_t       mask;
298} rndctl_t;
299.Ed
300.Pp
301Change bits in the device state information.
302If
303.Va type
304is 0xff, only the device name stored in
305.Va name
306is used.
307If it is any other value, all devices of type
308.Va type
309are altered.
310This allows all network interfaces to be disabled for
311entropy collection with one call, for example.
312The
313.Va flags
314and
315.Va mask
316work together to change flag bits.
317The
318.Va mask
319field specifies which bits in
320.Va flags
321are to be set or cleared.
322.It Dv RNDADDDATA
323.Pq Li "rnddata_t"
324.Bd -literal -offset indent
325typedef struct {
326        uint32_t	len;
327        uint32_t	entropy;
328        u_char		data[RND_SAVEWORDS * sizeof(uint32_t)];
329} rnddata_t;
330.Ed
331.El
332.Sh FILES
333.Bl -tag -width /dev/urandomx -compact
334.It Pa /dev/random
335Returns ``good'' values only
336.It Pa /dev/urandom
337Always returns data.
338.El
339.Sh SEE ALSO
340.Xr rndctl 8 ,
341.Xr rnd 9
342.Sh HISTORY
343The random device was first made available in
344.Nx 1.3 .
345.Sh AUTHORS
346This implementation was written by Thor Lancelot Simon.
347It retains
348some code (particularly for the ioctl interface) from the earlier
349implementation by Michael Graff
350.Aq explorer@flame.org .
351