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 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH GETRANDOM 2 2021-03-22 "Linux" "Linux Programmer's Manual"
9 getrandom \- obtain a series of random bytes
12 .RI ( libc ", " \-lc )
15 .B #include <sys/random.h>
17 .BI "ssize_t getrandom(void *"buf ", size_t " buflen ", unsigned int " flags );
22 system call fills the buffer pointed to by
27 These bytes can be used to seed user-space random number generators
28 or for cryptographic purposes.
32 draws entropy from the
34 source (i.e., the same source as the
37 This behavior can be changed via the
43 source has been initialized,
44 reads of up to 256 bytes will always return as many bytes as
45 requested and will not be interrupted by signals.
46 No such guarantees apply for larger buffer sizes.
47 For example, if the call is interrupted by a signal handler,
48 it may return a partially filled buffer, or fail with the error
53 source has not yet been initialized, then
62 argument is a bit mask that can contain zero or more of the following values
66 If this bit is set, then random bytes are drawn from the
69 (i.e., the same source as the
77 source is limited based on the entropy that can be obtained from environmental
79 If the number of available bytes in the
81 source is less than requested in
83 the call returns just the available random bytes.
84 If no random bytes are available, the behavior depends on the presence of
91 By default, when reading from the
95 blocks if no random bytes are available,
96 and when reading from the
98 source, it blocks if the entropy pool has not yet been initialized.
103 does not block in these cases, but instead immediately returns \-1 with
110 returns the number of bytes that were copied to the buffer
112 This may be less than the number of bytes requested via
118 and insufficient entropy was present in the
120 source or the system call was interrupted by a signal.
122 On error, \-1 is returned, and
124 is set to indicate the error.
128 The requested entropy was not available, and
130 would have blocked if the
135 The address referred to by
137 is outside the accessible address space.
140 The call was interrupted by a signal
141 handler; see the description of how interrupted
143 calls on "slow" devices are handled with and without the
150 An invalid flag was specified in
154 The glibc wrapper function for
156 determined that the underlying kernel does not implement this system call.
159 was introduced in version 3.17 of the Linux kernel.
160 Support was added to glibc in version 2.25.
162 This system call is Linux-specific.
164 For an overview and comparison of the various interfaces that
165 can be used to obtain randomness, see
173 does not involve the use of pathnames or file descriptors.
176 can be useful in cases where
181 and where an application (e.g., a daemon during start-up)
182 closes a file descriptor for one of these files
183 that was opened by a library.
185 .SS Maximum number of bytes returned
186 As of Linux 3.19 the following limits apply:
188 When reading from the
190 source, a maximum of 33554431 bytes is returned by a single call to
194 has a size of 32 bits.
196 When reading from the
198 source, a maximum of 512 bytes is returned.
199 .SS Interruption by a signal handler
200 When reading from the
206 will block until the entropy pool has been initialized
210 If a request is made to read a large number of bytes (more than 256),
212 will block until those bytes have been generated and transferred
213 from kernel memory to
215 When reading from the
221 will block until some random bytes become available
226 The behavior when a call to
228 that is blocked while reading from the
230 source is interrupted by a signal handler
231 depends on the initialization state of the entropy buffer
232 and on the request size,
234 If the entropy is not yet initialized, then the call fails with the
237 If the entropy pool has been initialized
238 and the request size is large
239 .RI ( buflen "\ >\ 256),"
240 the call either succeeds, returning a partially filled buffer,
241 or fails with the error
243 If the entropy pool has been initialized and the request size is small
244 .RI ( buflen "\ <=\ 256),"
249 Instead, it will return all of the bytes that have been requested.
251 When reading from the
253 source, blocking requests of any size can be interrupted by a signal handler
254 (the call fails with the error
259 to read small buffers (<=\ 256 bytes) from the
261 source is the preferred mode of usage.
263 The special treatment of small values of
265 was designed for compatibility with
268 which is nowadays supported by glibc.
273 always check the return value,
274 to determine whether either an error occurred
275 or fewer bytes than requested were returned.
280 is less than or equal to 256,
281 a return of fewer bytes than requested should never happen,
282 but the careful programmer will check for this anyway!
284 As of Linux 3.19, the following bug exists:
285 .\" FIXME patch proposed https://lkml.org/lkml/2014/11/29/16
287 Depending on CPU load,
289 does not react to interrupts before reading all bytes requested.