]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man4/random.4
mlock.2: tfix
[thirdparty/man-pages.git] / man4 / random.4
CommitLineData
fea681da
MK
1.\" Copyright (c) 1997 John S. Kallal (kallal@voicenet.com)
2.\"
89e3ffe9 3.\" %%%LICENSE_START(GPLv2+_DOC_ONEPARA)
fea681da
MK
4.\" This is free documentation; you can redistribute it and/or
5.\" modify it under the terms of the GNU General Public License as
6.\" published by the Free Software Foundation; either version 2 of
7.\" the License, or (at your option) any later version.
8f8359d8 8.\" %%%LICENSE_END
fea681da
MK
9.\"
10.\" Some changes by tytso and aeb.
11.\"
8deb0f0d 12.\" 2004-12-16, John V. Belmonte/mtk, Updated init and quit scripts
704a18f0 13.\" 2004-04-08, AEB, Improved description of read from /dev/urandom
9ed0f081
MK
14.\" 2008-06-20, George Spelvin <linux@horizon.com>,
15.\" Matt Mackall <mpm@selenic.com>
8deb0f0d 16.\"
4b8c67d9 17.TH RANDOM 4 2017-09-15 "Linux" "Linux Programmer's Manual"
fea681da
MK
18.SH NAME
19random, urandom \- kernel random number source devices
5a933be0
EDB
20.SH SYNOPSIS
21#include <linux/random.h>
68e4db0a 22.PP
5a933be0 23.BI "int ioctl(" fd ", RND" request ", " param ");"
fea681da 24.SH DESCRIPTION
c13182ef 25The character special files \fI/dev/random\fP and
8478ee02 26\fI/dev/urandom\fP (present since Linux 1.3.30)
c13182ef 27provide an interface to the kernel's random number generator.
af0b0990 28The file
11bda441 29.I /dev/random
af0b0990
MK
30has major device number 1 and minor device number 8.
31The file
32.I /dev/urandom
33has major device number 1 and minor device number 9.
dd3568a1 34.PP
c13182ef
MK
35The random number generator gathers environmental noise
36from device drivers and other sources into an entropy pool.
37The generator also keeps an estimate of the
fea681da 38number of bits of noise in the entropy pool.
82f0a1f9 39From this entropy pool, random numbers are created.
dd3568a1 40.PP
724d21bb 41Linux 3.17 and later provides the simpler and safer
f64f220c 42.BR getrandom (2)
724d21bb
MK
43interface which requires no special files;
44see the
45.BR getrandom (2)
46manual page for details.
2d444feb 47.PP
f64f220c
MK
48When read, the
49.I /dev/urandom
50device returns random bytes using a pseudorandom
51number generator seeded from the entropy pool.
6a0c1add 52Reads from this device do not block (i.e., the CPU is not yielded),
e6e25836 53but can incur an appreciable delay when requesting large amounts of data.
2d444feb 54.PP
daaeba95 55When read during early boot time,
bb02bcef 56.IR /dev/urandom
daaeba95 57may return data prior to the entropy pool being initialized.
8888079c
MK
58.\" This is a real problem; see
59.\" commit 9b4d008787f864f17d008c9c15bbe8a0f7e2fc24
67b7fcba 60If this is of concern in your application, use
f64f220c 61.BR getrandom (2)
67b7fcba 62or \fI/dev/random\fP instead.
2d444feb 63.PP
67b7fcba
NM
64The \fI/dev/random\fP device is a legacy interface which dates back to
65a time where the cryptographic primitives used in the implementation
f64f220c
MK
66of \fI/dev/urandom\fP were not widely trusted.
67It will return random bytes only within the estimated number of
68bits of fresh noise in the entropy pool, blocking if necessary.
c5d010eb 69\fI/dev/random\fP is suitable for applications that need
67b7fcba 70high quality randomness, and can afford indeterminate delays.
2d444feb 71.PP
c13182ef 72When the entropy pool is empty, reads from \fI/dev/random\fP will block
fea681da 73until additional environmental noise is gathered.
17344691
HS
74If
75.BR open (2)
76is called for
77.I /dev/random
724d21bb
MK
78with the
79.BR O_NONBLOCK
80flag, a subsequent
17344691
HS
81.BR read (2)
82will not block if the requested number of bytes is not available.
e8cc3c09 83Instead, the available bytes are returned.
792bb5ad 84If no byte is available,
17344691
HS
85.BR read (2)
86will return -1 and
87.I errno
88will be set to
89.BR EAGAIN .
2d444feb 90.PP
724d21bb 91The
17344691 92.B O_NONBLOCK
724d21bb 93flag has no effect when opening
17344691
HS
94.IR /dev/urandom .
95When calling
96.BR read (2)
792bb5ad
MK
97for the device
98.IR /dev/urandom ,
dbf63eed
MK
99reads of up to 256 bytes will return as many bytes as are requested
100and will not be interrupted by a signal handler.
101Reads with a buffer over this limit may return less than the
102requested number of bytes or fail with the error
4818990d
MK
103.BR EINTR ,
104if interrupted by a signal handler.
2d444feb 105.PP
866fa681
MK
106Since Linux 3.16,
107.\" commit 79a8468747c5f95ed3d5ce8376a3e82e0c5857fc
108a
109.BR read (2)
110from
111.IR /dev/urandom
c4b7e5ac 112will return at most 32\ MB.
44da0e24
MK
113A
114.BR read (2)
115from
116.IR /dev/random
117will return at most 512 bytes
118.\" SEC_XFER_SIZE in drivers/char/random.c
119(340 bytes on Linux kernels before version 2.6.12).
2d444feb 120.PP
5a933be0
EDB
121Writing to \fI/dev/random\fP or \fI/dev/urandom\fP will update the
122entropy pool with the data written, but this will not result in a
ece967c9
MK
123higher entropy count.
124This means that it will impact the contents
125read from both files, but it will not make reads from
5a933be0 126\fI/dev/random\fP faster.
9ed0f081 127.SS Usage
67b7fcba 128The
9ed0f081 129.IR /dev/random
67b7fcba 130interface is considered a legacy interface, and
724d21bb 131.IR /dev/urandom
67b7fcba
NM
132is preferred and sufficient in all use cases, with the exception of
133applications which require randomness during early boot time; for
f64f220c
MK
134these applications,
135.BR getrandom (2)
136must be used instead,
137because it will block until the entropy pool is initialized.
2d444feb 138.PP
a26a0d7b 139If a seed file is saved across reboots as recommended below (all major
9ed0f081
MK
140Linux distributions have done this since 2000 at least), the output is
141cryptographically secure against attackers without local root access as
142soon as it is reloaded in the boot sequence, and perfectly adequate for
143network encryption session keys.
e7dfa844 144Since reads from
9ed0f081 145.I /dev/random
ff40dbb3 146may block, users will usually want to open it in nonblocking mode
e7dfa844
MK
147(or perform a read with timeout),
148and provide some sort of user notification if the desired
9ed0f081 149entropy is not immediately available.
2e1f8bfa 150.\"
8eb40c9c 151.SS Configuration
fea681da 152If your system does not have
c13182ef 153\fI/dev/random\fP and \fI/dev/urandom\fP created already, they
fea681da 154can be created with the following commands:
2d444feb 155.PP
9c40f2b9
MK
156.in +4n
157.EX
158mknod \-m 666 /dev/random c 1 8
159mknod \-m 666 /dev/urandom c 1 9
160chown root:root /dev/random /dev/urandom
161.EE
162.in
2d444feb 163.PP
c13182ef 164When a Linux system starts up without much operator interaction,
fea681da 165the entropy pool may be in a fairly predictable state.
c13182ef
MK
166This reduces the actual amount of noise in the entropy pool
167below the estimate.
168In order to counteract this effect, it helps to carry
169entropy pool information across shut-downs and start-ups.
3f4a2adb 170To do this, add the lines to an appropriate script
c13182ef 171which is run during the Linux system start-up sequence:
2d444feb 172.PP
9c40f2b9
MK
173.in +4n
174.EX
175echo "Initializing random number generator..."
176random_seed=/var/run/random-seed
177# Carry a random seed from start-up to start-up
178# Load and then save the whole entropy pool
179if [ \-f $random_seed ]; then
180 cat $random_seed >/dev/urandom
181else
182 touch $random_seed
183fi
184chmod 600 $random_seed
185poolfile=/proc/sys/kernel/random/poolsize
186[ \-r $poolfile ] && bits=$(cat $poolfile) || bits=4096
187bytes=$(expr $bits / 8)
188dd if=/dev/urandom of=$random_seed count=1 bs=$bytes
189.EE
190.in
2d444feb 191.PP
c13182ef 192Also, add the following lines in an appropriate script which is
fea681da 193run during the Linux system shutdown:
2d444feb 194.PP
9c40f2b9
MK
195.in +4n
196.EX
197# Carry a random seed from shut-down to start-up
198# Save the whole entropy pool
199echo "Saving random seed..."
200random_seed=/var/run/random-seed
201touch $random_seed
202chmod 600 $random_seed
203poolfile=/proc/sys/kernel/random/poolsize
204[ \-r $poolfile ] && bits=$(cat $poolfile) || bits=4096
205bytes=$(expr $bits / 8)
206dd if=/dev/urandom of=$random_seed count=1 bs=$bytes
207.EE
208.in
2d444feb 209.PP
3f4a2adb
MK
210In the above examples, we assume Linux 2.6.0 or later, where
211.IR /proc/sys/kernel/random/poolsize
212returns the size of the entropy pool in bits (see below).
388ee0f4
MK
213.\"
214.SS /proc interfaces
fea681da
MK
215The files in the directory
216.I /proc/sys/kernel/random
388ee0f4 217(present since 2.3.16) provide additional information about the
8478ee02 218.I /dev/random
388ee0f4
MK
219device:
220.TP
fea681da 221.I entropy_avail
05babf32
MK
222This read-only file gives the available entropy, in bits.
223This will be a number in the range 0 to 4096.
388ee0f4 224.TP
fea681da 225.I poolsize
388ee0f4 226This file
c13182ef 227gives the size of the entropy pool.
da84883c
MK
228The semantics of this file vary across kernel versions:
229.RS
388ee0f4 230.TP
da84883c
MK
231Linux 2.4:
232This file gives the size of the entropy pool in
233.IR bytes .
234Normally, this file will have the value 512, but it is writable,
235and can be changed to any value for which an algorithm is available.
236The choices are 32, 64, 128, 256, 512, 1024, or 2048.
237.TP
0f1e6153 238Linux 2.6 and later:
da84883c
MK
239This file is read-only, and gives the size of the entropy pool in
240.IR bits .
241It contains the value 4096.
242.RE
388ee0f4 243.TP
fea681da 244.I read_wakeup_threshold
388ee0f4 245This file
fea681da
MK
246contains the number of bits of entropy required for waking up processes
247that sleep waiting for entropy from
31e9a9ec 248.IR /dev/random .
fea681da 249The default is 64.
388ee0f4 250.TP
fea681da 251.I write_wakeup_threshold
388ee0f4 252This file
fea681da
MK
253contains the number of bits of entropy below which we wake up
254processes that do a
5e21af3a 255.BR select (2)
fea681da 256or
5e21af3a 257.BR poll (2)
fea681da 258for write access to
31e9a9ec 259.IR /dev/random .
fea681da 260These values can be changed by writing to the files.
388ee0f4
MK
261.TP
262.IR uuid " and " boot_id
263These read-only files
fea681da
MK
264contain random strings like 6fd5a44b-35f4-4ad4-a9b9-6b9be13e1fe9.
265The former is generated afresh for each read, the latter was
266generated once.
388ee0f4 267.\"
5a933be0
EDB
268.SS ioctl(2) interface
269The following
270.BR ioctl (2)
271requests are defined on file descriptors connected to either \fI/dev/random\fP
ece967c9
MK
272or \fI/dev/urandom\fP.
273All requests performed will interact with the input
5a933be0
EDB
274entropy pool impacting both \fI/dev/random\fP and \fI/dev/urandom\fP.
275The
276.B CAP_SYS_ADMIN
277capability is required for all requests except
ece967c9 278.BR RNDGETENTCNT .
5a933be0
EDB
279.TP
280.BR RNDGETENTCNT
281Retrieve the entropy count of the input pool, the contents will be the same
282as the
283.I entropy_avail
284file under proc.
285The result will be stored in the int pointed to by the argument.
286.TP
287.BR RNDADDTOENTCNT
ece967c9
MK
288Increment or decrement the entropy count of the input pool
289by the value pointed to by the argument.
5a933be0
EDB
290.TP
291.BR RNDGETPOOL
292Removed in Linux 2.6.9.
293.TP
294.BR RNDADDENTROPY
ece967c9
MK
295Add some additional entropy to the input pool,
296incrementing the entropy count.
297This differs from writing to \fI/dev/random\fP or \fI/dev/urandom\fP,
298which only adds some
299data but does not increment the entropy count.
300The following structure is used:
5a933be0 301.IP
010c75a2
MK
302.in +4n
303.EX
304struct rand_pool_info {
305 int entropy_count;
306 int buf_size;
307 __u32 buf[0];
308};
309.EE
310.in
5a933be0
EDB
311.IP
312Here
313.I entropy_count
988bd008 314is the value added to (or subtracted from) the entropy count, and
5a933be0
EDB
315.I buf
316is the buffer of size
317.I buf_size
318which gets added to the entropy pool.
319.TP
320.BR RNDZAPENTCNT ", " RNDCLEARPOOL
321Zero the entropy count of all pools and add some system data (such as
322wall clock) to the pools.
f2910c72 323.SH FILES
323e1feb 324.I /dev/random
f2910c72 325.br
323e1feb 326.I /dev/urandom
379df7a0
MK
327.SH NOTES
328For an overview and comparison of the various interfaces that
329can be used to obtain randomness, see
330.BR random (7).
f64f220c 331.SH BUGS
debb291e 332During early boot time, reads from
f64f220c
MK
333.I /dev/urandom
334may return data prior to the entropy pool being initialized.
dc919d09
MK
335.\" .SH AUTHOR
336.\" The kernel's random number generator was written by
337.\" Theodore Ts'o (tytso@athena.mit.edu).
47297adb 338.SH SEE ALSO
59732c2a 339.BR mknod (1),
379df7a0
MK
340.BR getrandom (2),
341.BR random (7)
2d444feb 342.PP
331da7c3 343RFC\ 1750, "Randomness Recommendations for Security"