]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/getrandom.2
access.2, delete_module.2, eventfd.2, fallocate.2, fcntl.2, getrandom.2, init_module...
[thirdparty/man-pages.git] / man2 / getrandom.2
CommitLineData
18647599 1.\" Copyright (C) 2014, Theodore Ts'o <tytso@mit.edu>
3907aed3 2.\" Copyright (C) 2014,2015 Heinrich Schuchardt <xypron.glpk@gmx.de>
35800487 3.\" Copyright (C) 2015, Michael Kerrisk <mtk.manpages@gmail.com>
18647599
HS
4.\"
5.\" %%%LICENSE_START(VERBATIM)
6.\" Permission is granted to make and distribute verbatim copies of this
7.\" manual provided the copyright notice and this permission notice are
8.\" preserved on all copies.
9.\"
10.\" Permission is granted to copy and distribute modified versions of
11.\" this manual under the conditions for verbatim copying, provided that
12.\" the entire resulting derived work is distributed under the terms of
13.\" a permission notice identical to this one.
14.\"
15.\" Since the Linux kernel and libraries are constantly changing, this
16.\" manual page may be incorrect or out-of-date. The author(s) assume.
17.\" no responsibility for errors or omissions, or for damages resulting.
18.\" from the use of the information contained herein. The author(s) may.
19.\" not have taken the same level of care in the production of this.
20.\" manual, which is licensed free of charge, as they might when working.
21.\" professionally.
22.\"
23.\" Formatted or processed versions of this manual, if unaccompanied by
24.\" the source, must acknowledge the copyright and authors of this work.
25.\" %%%LICENSE_END
cc5ba920 26.\"
4b8c67d9 27.TH GETRANDOM 2 2017-09-15 "Linux" "Linux Programmer's Manual"
18647599
HS
28.SH NAME
29getrandom \- obtain a series of random bytes
30.SH SYNOPSIS
2cbb6fb4 31.B #include <sys/random.h>
68e4db0a 32.PP
da874dc7 33.BI "ssize_t getrandom(void *"buf ", size_t " buflen ", unsigned int " flags );
18647599 34.SH DESCRIPTION
81d43c50 35The
18647599 36.BR getrandom ()
81d43c50 37system call fills the buffer pointed to by
18647599
HS
38.I buf
39with up to
40.I buflen
41random bytes.
077e532d 42These bytes can be used to seed user-space random number generators
473844a1 43or for cryptographic purposes.
efeece04 44.PP
7028ce50
MK
45By default,
46.BR getrandom ()
47draws entropy from the
e196d319 48.I urandom
dce6b796 49source (i.e., the same source as the
7028ce50 50.IR /dev/urandom
e196d319 51device).
7028ce50
MK
52This behavior can be changed via the
53.I flags
54argument.
efeece04 55.PP
e14ee946 56If the
e196d319 57.I urandom
dce6b796 58source has been initialized,
053e5e69
MK
59reads of up to 256 bytes will always return as many bytes as
60requested and will not be interrupted by signals.
61No such guarantees apply for larger buffer sizes.
62For example, if the call is interrupted by a signal handler,
63it may return a partially filled buffer, or fail with the error
64.BR EINTR .
efeece04 65.PP
e196d319
MK
66If the
67.I urandom
dce6b796 68source has not yet been initialized, then
e196d319
MK
69.BR getrandom ()
70will block, unless
0594459d 71.B GRND_NONBLOCK
973ff2fc
MK
72is specified in
73.IR flags .
efeece04 74.PP
18647599
HS
75The
76.I flags
77argument is a bit mask that can contain zero or more of the following values
78ORed together:
79.TP
80.B GRND_RANDOM
81If this bit is set, then random bytes are drawn from the
e196d319 82.I random
dce6b796 83source
1b9d5819 84(i.e., the same source as the
e196d319
MK
85.IR /dev/random
86device)
87instead of the
88.I urandom
dce6b796 89source.
18647599 90The
e196d319 91.I random
dce6b796 92source is limited based on the entropy that can be obtained from environmental
4f69e06a 93noise.
e196d319
MK
94If the number of available bytes in the
95.I random
dce6b796 96source is less than requested in
18647599 97.IR buflen ,
e8009a53 98the call returns just the available random bytes.
973ff2fc 99If no random bytes are available, the behavior depends on the presence of
18647599
HS
100.B GRND_NONBLOCK
101in the
102.I flags
103argument.
104.TP
105.B GRND_NONBLOCK
e196d319
MK
106By default, when reading from the
107.IR random
dce6b796 108source,
18647599 109.BR getrandom ()
0646abd6 110blocks if no random bytes are available,
e196d319
MK
111and when reading from the
112.IR urandom
dce6b796 113source, it blocks if the entropy pool has not yet been initialized.
18647599
HS
114If the
115.B GRND_NONBLOCK
d2a1b46b 116flag is set, then
18647599 117.BR getrandom ()
0646abd6 118does not block in these cases, but instead immediately returns \-1 with
d2a1b46b
MK
119.I errno
120set to
121.BR EAGAIN .
18647599
HS
122.SH RETURN VALUE
123On success,
124.BR getrandom ()
125returns the number of bytes that were copied to the buffer
126.IR buf .
01a64074 127This may be less than the number of bytes requested via
18647599 128.I buflen
e196d319 129if either
973ff2fc
MK
130.BR GRND_RANDOM
131was specified in
132.IR flags
133and insufficient entropy was present in the
e196d319 134.IR random
dce6b796 135source or the system call was interrupted by a signal.
18647599 136.PP
81d43c50 137On error, \-1 is returned, and
18647599
HS
138.I errno
139is set appropriately.
140.SH ERRORS
141.TP
18647599
HS
142.B EAGAIN
143The requested entropy was not available, and
144.BR getrandom ()
145would have blocked if the
146.B GRND_NONBLOCK
147flag was not set.
148.TP
26c04bd0
MK
149.B EFAULT
150The address referred to by
151.I buf
152is outside the accessible address space.
153.TP
18647599 154.B EINTR
3426471b 155The call was interrupted by a signal
18647599
HS
156handler; see the description of how interrupted
157.BR read (2)
158calls on "slow" devices are handled with and without the
159.B SA_RESTART
160flag in the
161.BR signal (7)
162man page.
26c04bd0
MK
163.TP
164.B EINVAL
165An invalid flag was specified in
166.IR flags .
e6bafbe6
MK
167.TP
168.B ENOSYS
169The glibc wrapper function for
170.BR getrandom ()
171determined that the underlying kernel does not implement this system call.
18647599
HS
172.SH VERSIONS
173.BR getrandom ()
174was introduced in version 3.17 of the Linux kernel.
d9e71757 175Support was added to glibc in version 2.25.
18647599
HS
176.SH CONFORMING TO
177This system call is Linux-specific.
178.SH NOTES
9eaf7500
MK
179For an overview and comparison of the various interfaces that
180can be used to obtain randomness, see
181.BR random (7).
efeece04 182.PP
c35966b1
MK
183Unlike
184.IR /dev/random
185and
3e3c1e23 186.IR /dev/urandom ,
c35966b1
MK
187.BR getrandom ()
188does not involve the use of pathnames or file descriptors.
189Thus,
190.BR getrandom ()
191can be useful in cases where
192.BR chroot (2)
193makes
194.I /dev
195pathnames invisible,
196and where an application (e.g., a daemon during start-up)
197closes a file descriptor for one of these files
198that was opened by a library.
199.\"
11353d7b
HS
200.SS Maximum number of bytes returned
201As of Linux 3.19 the following limits apply:
202.IP * 3
e196d319
MK
203When reading from the
204.IR urandom
dce6b796 205source, a maximum of 33554431 bytes is returned by a single call to
11353d7b 206.BR getrandom ()
724d21bb 207on systems where
11353d7b
HS
208.I int
209has a size of 32 bits.
210.IP *
e196d319
MK
211When reading from the
212.IR random
dce6b796 213source, a maximum of 512 bytes is returned.
18647599 214.SS Interruption by a signal handler
e196d319
MK
215When reading from the
216.I urandom
dce6b796 217source
e14ee946 218.RB ( GRND_RANDOM
3851c7aa 219is not set),
3426471b
MK
220.BR getrandom ()
221will block until the entropy pool has been initialized
222(unless the
223.BR GRND_NONBLOCK
224flag was specified).
e196d319 225If a request is made to read a large number of bytes (more than 256),
3426471b
MK
226.BR getrandom ()
227will block until those bytes have been generated and transferred
228from kernel memory to
229.IR buf .
e196d319
MK
230When reading from the
231.I random
dce6b796 232source
e14ee946 233.RB ( GRND_RANDOM
3851c7aa 234is set),
3426471b
MK
235.BR getrandom ()
236will block until some random bytes become available
237(unless the
238.BR GRND_NONBLOCK
239flag was specified).
efeece04 240.PP
978323c7 241The behavior when a call to
18647599 242.BR getrandom ()
e196d319
MK
243that is blocked while reading from the
244.I urandom
dce6b796 245source is interrupted by a signal handler
18647599 246depends on the initialization state of the entropy buffer
81d43c50 247and on the request size,
18647599 248.IR buflen .
26cd31fd 249If the entropy is not yet initialized, then the call fails with the
18647599 250.B EINTR
81d43c50 251error.
05e4d7af
MK
252If the entropy pool has been initialized
253and the request size is large
254.RI ( buflen "\ >\ 256),"
255the call either succeeds, returning a partially filled buffer,
256or fails with the error
257.BR EINTR.
01a64074
MK
258If the entropy pool has been initialized and the request size is small
259.RI ( buflen "\ <=\ 256),"
81d43c50 260then
18647599 261.BR getrandom ()
81d43c50 262will not fail with
18647599
HS
263.BR EINTR .
264Instead, it will return all of the bytes that have been requested.
efeece04 265.PP
e196d319
MK
266When reading from the
267.IR random
dce6b796 268source, blocking requests of any size can be interrupted by a signal handler
652febf0
MK
269(the call fails with the error
270.BR EINTR ).
efeece04 271.PP
e196d319 272Using
18647599 273.BR getrandom ()
e196d319
MK
274to read small buffers (<=\ 256 bytes) from the
275.I urandom
dce6b796 276source is the preferred mode of usage.
efeece04 277.PP
18647599
HS
278The special treatment of small values of
279.I buflen
280was designed for compatibility with
281OpenBSD's
15df4f6b
MK
282.BR getentropy (3),
283which is nowadays supported by glibc.
18647599
HS
284.PP
285The user of
286.BR getrandom ()
287.I must
01a64074
MK
288always check the return value,
289to determine whether either an error occurred
290or fewer bytes than requested were returned.
18647599
HS
291In the case where
292.B GRND_RANDOM
293is not specified and
294.I buflen
295is less than or equal to 256,
296a return of fewer bytes than requested should never happen,
28ef3452 297but the careful programmer will check for this anyway!
16199698
HS
298.SH BUGS
299As of Linux 3.19, the following bug exists:
16199698 300.\" FIXME patch proposed https://lkml.org/lkml/2014/11/29/16
81d43c50
MK
301.IP * 3
302Depending on CPU load,
16199698
HS
303.BR getrandom ()
304does not react to interrupts before reading all bytes requested.
18647599 305.SH SEE ALSO
15df4f6b 306.BR getentropy (3),
18647599 307.BR random (4),
2cc7c11a 308.BR urandom (4),
9eaf7500 309.BR random (7),
2cc7c11a 310.BR signal (7)