| 1 |
'\" t |
| 2 |
.\" Copyright 2008, George Spelvin <[email protected]> |
| 3 |
.\" Copyright 2008, Matt Mackall <[email protected]> |
| 4 |
.\" Copyright 2016, Laurent Georget <[email protected]> |
| 5 |
.\" Copyright 2016, Nikos Mavrogiannopoulos <[email protected]> |
| 6 |
.\" Copyright, the authors of the Linux man-pages project |
| 7 |
.\" |
| 8 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 9 |
.\" |
| 10 |
.TH random 7 2026-02-08 "Linux man-pages 6.18" |
| 11 |
.SH NAME |
| 12 |
random \- overview of interfaces for obtaining randomness |
| 13 |
.SH DESCRIPTION |
| 14 |
The kernel random-number generator relies on entropy gathered from |
| 15 |
device drivers and other sources of environmental noise to seed |
| 16 |
a cryptographically secure pseudorandom number generator (CSPRNG). |
| 17 |
It is designed for security, rather than speed. |
| 18 |
.P |
| 19 |
The following interfaces provide access to output from the kernel CSPRNG: |
| 20 |
.IP \[bu] 3 |
| 21 |
The |
| 22 |
.I /dev/urandom |
| 23 |
and |
| 24 |
.I /dev/random |
| 25 |
devices, both described in |
| 26 |
.BR random (4). |
| 27 |
These devices have been present on Linux since early times, |
| 28 |
and are also available on many other systems. |
| 29 |
.IP \[bu] |
| 30 |
The Linux-specific |
| 31 |
.BR getrandom (2) |
| 32 |
system call, available since Linux 3.17. |
| 33 |
This system call provides access either to the same source as |
| 34 |
.I /dev/urandom |
| 35 |
(called the |
| 36 |
.I urandom |
| 37 |
source in this page) |
| 38 |
or to the same source as |
| 39 |
.I /dev/random |
| 40 |
(called the |
| 41 |
.I random |
| 42 |
source in this page). |
| 43 |
The default is the |
| 44 |
.I urandom |
| 45 |
source; |
| 46 |
the |
| 47 |
.I random |
| 48 |
source is selected by specifying the |
| 49 |
.B GRND_RANDOM |
| 50 |
flag to the system call. |
| 51 |
(The |
| 52 |
.BR getentropy (3) |
| 53 |
function provides a slightly more portable interface on top of |
| 54 |
.BR getrandom (2).) |
| 55 |
.\" |
| 56 |
.SS Initialization of the entropy pool |
| 57 |
The kernel collects bits of entropy from the environment. |
| 58 |
When a sufficient number of random bits has been collected, the |
| 59 |
entropy pool is considered to be initialized. |
| 60 |
.SS Choice of random source |
| 61 |
Unless you are doing long-term key generation (and most likely not even |
| 62 |
then), you probably shouldn't be reading from the |
| 63 |
.I /dev/random |
| 64 |
device or employing |
| 65 |
.BR getrandom (2) |
| 66 |
with the |
| 67 |
.B GRND_RANDOM |
| 68 |
flag. |
| 69 |
Instead, either read from the |
| 70 |
.I /dev/urandom |
| 71 |
device or employ |
| 72 |
.BR getrandom (2) |
| 73 |
without the |
| 74 |
.B GRND_RANDOM |
| 75 |
flag. |
| 76 |
The cryptographic algorithms used for the |
| 77 |
.I urandom |
| 78 |
source are quite conservative, and so should be sufficient for all purposes. |
| 79 |
.P |
| 80 |
The disadvantage of |
| 81 |
.B GRND_RANDOM |
| 82 |
and reads from |
| 83 |
.I /dev/random |
| 84 |
is that the operation can block for an indefinite period of time. |
| 85 |
Furthermore, dealing with the partially fulfilled |
| 86 |
requests that can occur when using |
| 87 |
.B GRND_RANDOM |
| 88 |
or when reading from |
| 89 |
.I /dev/random |
| 90 |
increases code complexity. |
| 91 |
.\" |
| 92 |
.SS Monte Carlo and other probabilistic sampling applications |
| 93 |
Using these interfaces to provide large quantities of data for |
| 94 |
Monte Carlo simulations or other programs/algorithms which are |
| 95 |
doing probabilistic sampling will be slow. |
| 96 |
Furthermore, it is unnecessary, because such applications do not |
| 97 |
need cryptographically secure random numbers. |
| 98 |
Instead, use the interfaces described in this page to obtain |
| 99 |
a small amount of data to seed a user-space pseudorandom |
| 100 |
number generator for use by such applications. |
| 101 |
.\" |
| 102 |
.SS Comparison between getrandom, /dev/urandom, and /dev/random |
| 103 |
The following table summarizes the behavior of the various |
| 104 |
interfaces that can be used to obtain randomness. |
| 105 |
.B GRND_NONBLOCK |
| 106 |
is a flag that can be used to control the blocking behavior of |
| 107 |
.BR getrandom (2). |
| 108 |
The final column of the table considers the case that can occur |
| 109 |
in early boot time when the entropy pool is not yet initialized. |
| 110 |
.ad l |
| 111 |
.TS |
| 112 |
allbox; |
| 113 |
lbw13 lbw12 lbw14 lbw18 |
| 114 |
l l l l. |
| 115 |
Interface Pool T{ |
| 116 |
Blocking |
| 117 |
\%behavior |
| 118 |
T} T{ |
| 119 |
Behavior when pool is not yet ready |
| 120 |
T} |
| 121 |
T{ |
| 122 |
.I /dev/random |
| 123 |
T} T{ |
| 124 |
Blocking pool |
| 125 |
T} T{ |
| 126 |
If entropy too low, blocks until there is enough entropy again |
| 127 |
T} T{ |
| 128 |
Blocks until enough entropy gathered |
| 129 |
T} |
| 130 |
T{ |
| 131 |
.I /dev/urandom |
| 132 |
T} T{ |
| 133 |
CSPRNG output |
| 134 |
T} T{ |
| 135 |
Never blocks |
| 136 |
T} T{ |
| 137 |
Returns output from uninitialized CSPRNG (may be low entropy and unsuitable for cryptography) |
| 138 |
T} |
| 139 |
T{ |
| 140 |
.BR getrandom () |
| 141 |
T} T{ |
| 142 |
Same as |
| 143 |
.I /dev/urandom |
| 144 |
T} T{ |
| 145 |
Does not block once pool is ready |
| 146 |
T} T{ |
| 147 |
Blocks until pool ready |
| 148 |
T} |
| 149 |
T{ |
| 150 |
.BR getrandom () |
| 151 |
.B GRND_RANDOM |
| 152 |
T} T{ |
| 153 |
Same as |
| 154 |
.I /dev/random |
| 155 |
T} T{ |
| 156 |
If entropy too low, blocks until there is enough entropy again |
| 157 |
T} T{ |
| 158 |
Blocks until pool ready |
| 159 |
T} |
| 160 |
T{ |
| 161 |
.BR getrandom () |
| 162 |
.B GRND_NONBLOCK |
| 163 |
T} T{ |
| 164 |
Same as |
| 165 |
.I /dev/urandom |
| 166 |
T} T{ |
| 167 |
Does not block once pool is ready |
| 168 |
T} T{ |
| 169 |
.B EAGAIN |
| 170 |
T} |
| 171 |
T{ |
| 172 |
.BR getrandom () |
| 173 |
.B GRND_RANDOM |
| 174 |
+ |
| 175 |
.B GRND_NONBLOCK |
| 176 |
T} T{ |
| 177 |
Same as |
| 178 |
.I /dev/random |
| 179 |
T} T{ |
| 180 |
.B EAGAIN |
| 181 |
if not enough entropy available |
| 182 |
T} T{ |
| 183 |
.B EAGAIN |
| 184 |
T} |
| 185 |
.TE |
| 186 |
.ad |
| 187 |
.\" |
| 188 |
.SS Generating cryptographic keys |
| 189 |
The amount of seed material required to generate a cryptographic key |
| 190 |
equals the effective key size of the key. |
| 191 |
For example, a 3072-bit RSA |
| 192 |
or Diffie-Hellman private key has an effective key size of 128 bits |
| 193 |
(it requires about 2\[ha]128 operations to break) so a key generator |
| 194 |
needs only 128 bits (16 bytes) of seed material from |
| 195 |
.IR /dev/random . |
| 196 |
.P |
| 197 |
While some safety margin above that minimum is reasonable, as a guard |
| 198 |
against flaws in the CSPRNG algorithm, no cryptographic primitive |
| 199 |
available today can hope to promise more than 256 bits of security, |
| 200 |
so if any program reads more than 256 bits (32 bytes) from the kernel |
| 201 |
random pool per invocation, or per reasonable reseed interval (not less |
| 202 |
than one minute), that should be taken as a sign that its cryptography is |
| 203 |
.I not |
| 204 |
skillfully implemented. |
| 205 |
.\" |
| 206 |
.SH SEE ALSO |
| 207 |
.BR getrandom (2), |
| 208 |
.BR getauxval (3), |
| 209 |
.BR getentropy (3), |
| 210 |
.BR random (4), |
| 211 |
.BR urandom (4), |
| 212 |
.BR signal (7) |
| 213 |
.P |
| 214 |
.UR http:\://www.2uo.de/myths-about-urandom/ |
| 215 |
.UE |