1 .\" Copyright (C) 2014, Theodore Ts'o <tytso@mit.edu>
2 .\" Copyright (C) 2014,2015 Heinrich Schuchardt <xypron.glpk@gmx.de>
3 .\" Copyright (C) 2015, Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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.
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
27 .TH GETRANDOM 2 2016-10-08 "Linux" "Linux Programmer's Manual"
29 getrandom \- obtain a series of random bytes
31 .B #include <linux/random.h>
33 .BI "int getrandom(void *"buf ", size_t " buflen ", unsigned int " flags );
37 system call fills the buffer pointed to by
42 These bytes can be used to seed user-space random number generators
43 or for cryptographic purposes.
47 draws entropy from the
49 source (i.e., the same source as the
52 This behavior can be changed via the
58 source has been initialized,
59 reads of up to 256 bytes will always return as many bytes as
60 requested and will not be interrupted by signals.
61 No such guarantees apply for larger buffer sizes.
62 For example, if the call is interrupted by a signal handler,
63 it may return a partially filled buffer, or fail with the error
68 source has not yet been initialized, then
77 argument is a bit mask that can contain zero or more of the following values
81 If this bit is set, then random bytes are drawn from the
84 (i.e., the same source as the
92 source is limited based on the entropy that can be obtained from environmental
94 If the number of available bytes in the
96 source is less than requested in
98 the call returns just the available random bytes.
99 If no random bytes are available, the behavior depends on the presence of
106 By default, when reading from the
110 blocks if no random bytes are available,
111 and when reading from the
113 source, it blocks if the entropy pool has not yet been initialized.
118 does not block in these cases, but instead immediately returns \-1 with
125 returns the number of bytes that were copied to the buffer
127 This may be less than the number of bytes requested via
133 and insufficient entropy was present in the
135 source or the system call was interrupted by a signal.
137 On error, \-1 is returned, and
139 is set appropriately.
143 The requested entropy was not available, and
145 would have blocked if the
150 The address referred to by
152 is outside the accessible address space.
155 The call was interrupted by a signal
156 handler; see the description of how interrupted
158 calls on "slow" devices are handled with and without the
165 An invalid flag was specified in
169 was introduced in version 3.17 of the Linux kernel.
170 Support was added to glibc in version 2.25.
172 This system call is Linux-specific.
174 For an overview and comparison of the various interfaces that
175 can be used to obtain randomness, see
183 does not involve the use of pathnames or file descriptors.
186 can be useful in cases where
191 and where an application (e.g., a daemon during start-up)
192 closes a file descriptor for one of these files
193 that was opened by a library.
195 .SS Maximum number of bytes returned
196 As of Linux 3.19 the following limits apply:
198 When reading from the
200 source, a maximum of 33554431 bytes is returned by a single call to
204 has a size of 32 bits.
206 When reading from the
208 source, a maximum of 512 bytes is returned.
209 .SS Interruption by a signal handler
210 When reading from the
216 will block until the entropy pool has been initialized
220 If a request is made to read a large number of bytes (more than 256),
222 will block until those bytes have been generated and transferred
223 from kernel memory to
225 When reading from the
231 will block until some random bytes become available
236 The behavior when a call to
238 that is blocked while reading from the
240 source is interrupted by a signal handler
241 depends on the initialization state of the entropy buffer
242 and on the request size,
244 If the entropy is not yet initialized, then the call will fail with the
247 If the entropy pool has been initialized
248 and the request size is large
249 .RI ( buflen "\ >\ 256),"
250 the call either succeeds, returning a partially filled buffer,
251 or fails with the error
253 If the entropy pool has been initialized and the request size is small
254 .RI ( buflen "\ <=\ 256),"
259 Instead, it will return all of the bytes that have been requested.
261 When reading from the
263 source, blocking requests of any size can be interrupted by a signal handler
264 (the call fails with the error
269 to read small buffers (<=\ 256 bytes) from the
271 source is the preferred mode of usage.
273 The special treatment of small values of
275 was designed for compatibility with
278 which is nowadays supported by glibc.
283 always check the return value,
284 to determine whether either an error occurred
285 or fewer bytes than requested were returned.
290 is less than or equal to 256,
291 a return of fewer bytes than requested should never happen,
292 but the careful programmer will check for this anyway!
294 As of Linux 3.19, the following bug exists:
295 .\" FIXME patch proposed https://lkml.org/lkml/2014/11/29/16
297 Depending on CPU load,
299 does not react to interrupts before reading all bytes requested.