Blame man4/random.4

Packit 7cfc04
.\" Copyright (c) 1997 John S. Kallal (kallal@voicenet.com)
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(GPLv2+_DOC_ONEPARA)
Packit 7cfc04
.\" This is free documentation; you can redistribute it and/or
Packit 7cfc04
.\" modify it under the terms of the GNU General Public License as
Packit 7cfc04
.\" published by the Free Software Foundation; either version 2 of
Packit 7cfc04
.\" the License, or (at your option) any later version.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Some changes by tytso and aeb.
Packit 7cfc04
.\"
Packit 7cfc04
.\" 2004-12-16, John V. Belmonte/mtk, Updated init and quit scripts
Packit 7cfc04
.\" 2004-04-08, AEB, Improved description of read from /dev/urandom
Packit 7cfc04
.\" 2008-06-20, George Spelvin <linux@horizon.com>,
Packit 7cfc04
.\"             Matt Mackall <mpm@selenic.com>
Packit 7cfc04
.\"
Packit 7cfc04
.TH RANDOM 4 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
random, urandom \- kernel random number source devices
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
#include <linux/random.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int ioctl(" fd ", RND" request ", " param ");"
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The character special files \fI/dev/random\fP and
Packit 7cfc04
\fI/dev/urandom\fP (present since Linux 1.3.30)
Packit 7cfc04
provide an interface to the kernel's random number generator.
Packit 7cfc04
The file
Packit 7cfc04
.I /dev/random
Packit 7cfc04
has major device number 1 and minor device number 8.
Packit 7cfc04
The file
Packit 7cfc04
.I /dev/urandom
Packit 7cfc04
has major device number 1 and minor device number 9.
Packit 7cfc04
.PP
Packit 7cfc04
The random number generator gathers environmental noise
Packit 7cfc04
from device drivers and other sources into an entropy pool.
Packit 7cfc04
The generator also keeps an estimate of the
Packit 7cfc04
number of bits of noise in the entropy pool.
Packit 7cfc04
From this entropy pool, random numbers are created.
Packit 7cfc04
.PP
Packit 7cfc04
Linux 3.17 and later provides the simpler and safer
Packit 7cfc04
.BR getrandom (2)
Packit 7cfc04
interface which requires no special files;
Packit 7cfc04
see the
Packit 7cfc04
.BR getrandom (2)
Packit 7cfc04
manual page for details.
Packit 7cfc04
.PP
Packit 7cfc04
When read, the
Packit 7cfc04
.I /dev/urandom
Packit 7cfc04
device returns random bytes using a pseudorandom
Packit 7cfc04
number generator seeded from the entropy pool.
Packit 7cfc04
Reads from this device do not block (i.e., the CPU is not yielded),
Packit 7cfc04
but can incur an appreciable delay when requesting large amounts of data.
Packit 7cfc04
.PP
Packit 7cfc04
When read during early boot time,
Packit 7cfc04
.IR /dev/urandom
Packit 7cfc04
may return data prior to the entropy pool being initialized.
Packit 7cfc04
.\" This is a real problem; see
Packit 7cfc04
.\" commit 9b4d008787f864f17d008c9c15bbe8a0f7e2fc24
Packit 7cfc04
If this is of concern in your application, use
Packit 7cfc04
.BR getrandom (2)
Packit 7cfc04
or \fI/dev/random\fP instead.
Packit 7cfc04
.PP
Packit 7cfc04
The \fI/dev/random\fP device is a legacy interface which dates back to
Packit 7cfc04
a time where the cryptographic primitives used in the implementation
Packit 7cfc04
of \fI/dev/urandom\fP were not widely trusted.
Packit 7cfc04
It will return random bytes only within the estimated number of
Packit 7cfc04
bits of fresh noise in the entropy pool, blocking if necessary.
Packit 7cfc04
\fI/dev/random\fP is suitable for applications that need
Packit 7cfc04
high quality randomness, and can afford indeterminate delays.
Packit 7cfc04
.PP
Packit 7cfc04
When the entropy pool is empty, reads from \fI/dev/random\fP will block
Packit 7cfc04
until additional environmental noise is gathered.
Packit 7cfc04
If
Packit 7cfc04
.BR open (2)
Packit 7cfc04
is called for
Packit 7cfc04
.I /dev/random
Packit 7cfc04
with the
Packit 7cfc04
.BR O_NONBLOCK
Packit 7cfc04
flag, a subsequent
Packit 7cfc04
.BR read (2)
Packit 7cfc04
will not block if the requested number of bytes is not available.
Packit 7cfc04
Instead, the available bytes are returned.
Packit 7cfc04
If no byte is available,
Packit 7cfc04
.BR read (2)
Packit 7cfc04
will return -1 and
Packit 7cfc04
.I errno
Packit 7cfc04
will be set to
Packit 7cfc04
.BR EAGAIN .
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.B O_NONBLOCK
Packit 7cfc04
flag has no effect when opening
Packit 7cfc04
.IR /dev/urandom .
Packit 7cfc04
When calling
Packit 7cfc04
.BR read (2)
Packit 7cfc04
for the device
Packit 7cfc04
.IR /dev/urandom ,
Packit 7cfc04
reads of up to 256 bytes will return as many bytes as are requested
Packit 7cfc04
and will not be interrupted by a signal handler.
Packit 7cfc04
Reads with a buffer over this limit may return less than the
Packit 7cfc04
requested number of bytes or fail with the error
Packit 7cfc04
.BR EINTR ,
Packit 7cfc04
if interrupted by a signal handler.
Packit 7cfc04
.PP
Packit 7cfc04
Since Linux 3.16,
Packit 7cfc04
.\" commit 79a8468747c5f95ed3d5ce8376a3e82e0c5857fc
Packit 7cfc04
a
Packit 7cfc04
.BR read (2)
Packit 7cfc04
from
Packit 7cfc04
.IR /dev/urandom
Packit 7cfc04
will return at most 32\ MB.
Packit 7cfc04
A
Packit 7cfc04
.BR read (2)
Packit 7cfc04
from
Packit 7cfc04
.IR /dev/random
Packit 7cfc04
will return at most 512 bytes
Packit 7cfc04
.\" SEC_XFER_SIZE in drivers/char/random.c
Packit 7cfc04
(340 bytes on Linux kernels before version 2.6.12).
Packit 7cfc04
.PP
Packit 7cfc04
Writing to \fI/dev/random\fP or \fI/dev/urandom\fP will update the
Packit 7cfc04
entropy pool with the data written, but this will not result in a
Packit 7cfc04
higher entropy count.
Packit 7cfc04
This means that it will impact the contents
Packit 7cfc04
read from both files, but it will not make reads from
Packit 7cfc04
\fI/dev/random\fP faster.
Packit 7cfc04
.SS Usage
Packit 7cfc04
The
Packit 7cfc04
.IR /dev/random
Packit 7cfc04
interface is considered a legacy interface, and
Packit 7cfc04
.IR /dev/urandom
Packit 7cfc04
is preferred and sufficient in all use cases, with the exception of
Packit 7cfc04
applications which require randomness during early boot time; for
Packit 7cfc04
these applications,
Packit 7cfc04
.BR getrandom (2)
Packit 7cfc04
must be used instead,
Packit 7cfc04
because it will block until the entropy pool is initialized.
Packit 7cfc04
.PP
Packit 7cfc04
If a seed file is saved across reboots as recommended below (all major
Packit 7cfc04
Linux distributions have done this since 2000 at least), the output is
Packit 7cfc04
cryptographically secure against attackers without local root access as
Packit 7cfc04
soon as it is reloaded in the boot sequence, and perfectly adequate for
Packit 7cfc04
network encryption session keys.
Packit 7cfc04
Since reads from
Packit 7cfc04
.I /dev/random
Packit 7cfc04
may block, users will usually want to open it in nonblocking mode
Packit 7cfc04
(or perform a read with timeout),
Packit 7cfc04
and provide some sort of user notification if the desired
Packit 7cfc04
entropy is not immediately available.
Packit 7cfc04
.\"
Packit 7cfc04
.SS Configuration
Packit 7cfc04
If your system does not have
Packit 7cfc04
\fI/dev/random\fP and \fI/dev/urandom\fP created already, they
Packit 7cfc04
can be created with the following commands:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
mknod \-m 666 /dev/random c 1 8
Packit 7cfc04
mknod \-m 666 /dev/urandom c 1 9
Packit 7cfc04
chown root:root /dev/random /dev/urandom
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
When a Linux system starts up without much operator interaction,
Packit 7cfc04
the entropy pool may be in a fairly predictable state.
Packit 7cfc04
This reduces the actual amount of noise in the entropy pool
Packit 7cfc04
below the estimate.
Packit 7cfc04
In order to counteract this effect, it helps to carry
Packit 7cfc04
entropy pool information across shut-downs and start-ups.
Packit 7cfc04
To do this, add the lines to an appropriate script
Packit 7cfc04
which is run during the Linux system start-up sequence:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
echo "Initializing random number generator..."
Packit 7cfc04
random_seed=/var/run/random-seed
Packit 7cfc04
# Carry a random seed from start-up to start-up
Packit 7cfc04
# Load and then save the whole entropy pool
Packit 7cfc04
if [ \-f $random_seed ]; then
Packit 7cfc04
    cat $random_seed >/dev/urandom
Packit 7cfc04
else
Packit 7cfc04
    touch $random_seed
Packit 7cfc04
fi
Packit 7cfc04
chmod 600 $random_seed
Packit 7cfc04
poolfile=/proc/sys/kernel/random/poolsize
Packit 7cfc04
[ \-r $poolfile ] && bits=$(cat $poolfile) || bits=4096
Packit 7cfc04
bytes=$(expr $bits / 8)
Packit 7cfc04
dd if=/dev/urandom of=$random_seed count=1 bs=$bytes
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
Also, add the following lines in an appropriate script which is
Packit 7cfc04
run during the Linux system shutdown:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
# Carry a random seed from shut-down to start-up
Packit 7cfc04
# Save the whole entropy pool
Packit 7cfc04
echo "Saving random seed..."
Packit 7cfc04
random_seed=/var/run/random-seed
Packit 7cfc04
touch $random_seed
Packit 7cfc04
chmod 600 $random_seed
Packit 7cfc04
poolfile=/proc/sys/kernel/random/poolsize
Packit 7cfc04
[ \-r $poolfile ] && bits=$(cat $poolfile) || bits=4096
Packit 7cfc04
bytes=$(expr $bits / 8)
Packit 7cfc04
dd if=/dev/urandom of=$random_seed count=1 bs=$bytes
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
In the above examples, we assume Linux 2.6.0 or later, where
Packit 7cfc04
.IR /proc/sys/kernel/random/poolsize
Packit 7cfc04
returns the size of the entropy pool in bits (see below).
Packit 7cfc04
.\"
Packit 7cfc04
.SS /proc interfaces
Packit 7cfc04
The files in the directory
Packit 7cfc04
.I /proc/sys/kernel/random
Packit 7cfc04
(present since 2.3.16) provide additional information about the
Packit 7cfc04
.I /dev/random
Packit 7cfc04
device:
Packit 7cfc04
.TP
Packit 7cfc04
.I entropy_avail
Packit 7cfc04
This read-only file gives the available entropy, in bits.
Packit 7cfc04
This will be a number in the range 0 to 4096.
Packit 7cfc04
.TP
Packit 7cfc04
.I poolsize
Packit 7cfc04
This file
Packit 7cfc04
gives the size of the entropy pool.
Packit 7cfc04
The semantics of this file vary across kernel versions:
Packit 7cfc04
.RS
Packit 7cfc04
.TP
Packit 7cfc04
Linux 2.4:
Packit 7cfc04
This file gives the size of the entropy pool in
Packit 7cfc04
.IR bytes .
Packit 7cfc04
Normally, this file will have the value 512, but it is writable,
Packit 7cfc04
and can be changed to any value for which an algorithm is available.
Packit 7cfc04
The choices are 32, 64, 128, 256, 512, 1024, or 2048.
Packit 7cfc04
.TP
Packit 7cfc04
Linux 2.6 and later:
Packit 7cfc04
This file is read-only, and gives the size of the entropy pool in
Packit 7cfc04
.IR bits .
Packit 7cfc04
It contains the value 4096.
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.I read_wakeup_threshold
Packit 7cfc04
This file
Packit 7cfc04
contains the number of bits of entropy required for waking up processes
Packit 7cfc04
that sleep waiting for entropy from
Packit 7cfc04
.IR /dev/random .
Packit 7cfc04
The default is 64.
Packit 7cfc04
.TP
Packit 7cfc04
.I write_wakeup_threshold
Packit 7cfc04
This file
Packit 7cfc04
contains the number of bits of entropy below which we wake up
Packit 7cfc04
processes that do a
Packit 7cfc04
.BR select (2)
Packit 7cfc04
or
Packit 7cfc04
.BR poll (2)
Packit 7cfc04
for write access to
Packit 7cfc04
.IR /dev/random .
Packit 7cfc04
These values can be changed by writing to the files.
Packit 7cfc04
.TP
Packit 7cfc04
.IR uuid " and " boot_id
Packit 7cfc04
These read-only files
Packit 7cfc04
contain random strings like 6fd5a44b-35f4-4ad4-a9b9-6b9be13e1fe9.
Packit 7cfc04
The former is generated afresh for each read, the latter was
Packit 7cfc04
generated once.
Packit 7cfc04
.\"
Packit 7cfc04
.SS ioctl(2) interface
Packit 7cfc04
The following
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
requests are defined on file descriptors connected to either \fI/dev/random\fP
Packit 7cfc04
or \fI/dev/urandom\fP.
Packit 7cfc04
All requests performed will interact with the input
Packit 7cfc04
entropy pool impacting both \fI/dev/random\fP and \fI/dev/urandom\fP.
Packit 7cfc04
The
Packit 7cfc04
.B CAP_SYS_ADMIN
Packit 7cfc04
capability is required for all requests except
Packit 7cfc04
.BR RNDGETENTCNT .
Packit 7cfc04
.TP
Packit 7cfc04
.BR RNDGETENTCNT
Packit 7cfc04
Retrieve the entropy count of the input pool, the contents will be the same
Packit 7cfc04
as the
Packit 7cfc04
.I entropy_avail
Packit 7cfc04
file under proc.
Packit 7cfc04
The result will be stored in the int pointed to by the argument.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RNDADDTOENTCNT
Packit 7cfc04
Increment or decrement the entropy count of the input pool
Packit 7cfc04
by the value pointed to by the argument.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RNDGETPOOL
Packit 7cfc04
Removed in Linux 2.6.9.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RNDADDENTROPY
Packit 7cfc04
Add some additional entropy to the input pool,
Packit 7cfc04
incrementing the entropy count.
Packit 7cfc04
This differs from writing to \fI/dev/random\fP or \fI/dev/urandom\fP,
Packit 7cfc04
which only adds some
Packit 7cfc04
data but does not increment the entropy count.
Packit 7cfc04
The following structure is used:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct rand_pool_info {
Packit 7cfc04
    int    entropy_count;
Packit 7cfc04
    int    buf_size;
Packit 7cfc04
    __u32  buf[0];
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
Here
Packit 7cfc04
.I entropy_count
Packit 7cfc04
is the value added to (or subtracted from) the entropy count, and
Packit 7cfc04
.I buf
Packit 7cfc04
is the buffer of size
Packit 7cfc04
.I buf_size
Packit 7cfc04
which gets added to the entropy pool.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RNDZAPENTCNT ", " RNDCLEARPOOL
Packit 7cfc04
Zero the entropy count of all pools and add some system data (such as
Packit 7cfc04
wall clock) to the pools.
Packit 7cfc04
.SH FILES
Packit 7cfc04
.I /dev/random
Packit 7cfc04
.br
Packit 7cfc04
.I /dev/urandom
Packit 7cfc04
.SH NOTES
Packit 7cfc04
For an overview and comparison of the various interfaces that
Packit 7cfc04
can be used to obtain randomness, see
Packit 7cfc04
.BR random (7).
Packit 7cfc04
.SH BUGS
Packit 7cfc04
During early boot time, reads from
Packit 7cfc04
.I /dev/urandom
Packit 7cfc04
may return data prior to the entropy pool being initialized.
Packit 7cfc04
.\" .SH AUTHOR
Packit 7cfc04
.\" The kernel's random number generator was written by
Packit 7cfc04
.\" Theodore Ts'o (tytso@athena.mit.edu).
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR mknod (1),
Packit 7cfc04
.BR getrandom (2),
Packit 7cfc04
.BR random (7)
Packit 7cfc04
.PP
Packit 7cfc04
RFC\ 1750, "Randomness Recommendations for Security"
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.