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