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
50 This behavior can be changed via the
56 pool has been initialized,
57 reads of up to 256 bytes will always return as many bytes as
58 requested and will not be interrupted by signals.
59 No such guarantees apply for larger buffer sizes.
60 For example, if the call is interrupted by a signal handler,
61 it may return a partially filled buffer, or fail with the error
63 If the entropy pool has not yet been initialized, then the call blocks, unless
70 argument is a bit mask that can contain zero or more of the following values
74 If this bit is set, then random bytes are drawn from the
81 pool is limited based on the entropy that can be obtained from environmental
83 If the number of available bytes in
85 is less than requested in
87 the call returns just the available random bytes.
88 If no random bytes are available, the behavior depends on the presence of
95 By default, when reading from
98 blocks if no random bytes are available,
101 it blocks if the entropy pool has not yet been initialized.
106 does not block in these cases, but instead immediately returns \-1 with
113 returns the number of bytes that were copied to the buffer
115 This may be less than the number of bytes requested via
121 and insufficient entropy was present in the
123 pool, or if the system call was interrupted by a signal.
125 On error, \-1 is returned, and
127 is set appropriately.
131 The requested entropy was not available, and
133 would have blocked if the
138 The address referred to by
140 is outside the accessible address space.
143 The call was interrupted by a signal
144 handler; see the description of how interrupted
146 calls on "slow" devices are handled with and without the
153 An invalid flag was specified in
157 was introduced in version 3.17 of the Linux kernel.
159 This system call is Linux-specific.
166 does not involve the use of pathnames or file descriptors.
169 can be useful in cases where
174 and where an application (e.g., a daemon during start-up)
175 closes a file descriptor for one of these files
176 that was opened by a library.
178 .SS Maximum number of bytes returned
179 As of Linux 3.19 the following limits apply:
183 a maximum of 33554431 bytes is returned by a single call to
187 has a size of 32 bits.
191 a maximum of 512 bytes is returned.
192 .SS Initialization of the entropy pool
193 The kernel collects bits of entropy from the environment.
194 When a sufficient number of random bits has been collected, the
196 entropy pool is considered to be initialized.
197 This state is normally reached early in the system bootstrap phase.
198 .SS Interruption by a signal handler
204 will block until the entropy pool has been initialized
208 If a request is made to read a large number (more than 256) of bytes,
210 will block until those bytes have been generated and transferred
211 from kernel memory to
218 will block until some random bytes become available
223 The behavior when a call to
225 that is blocked while reading from
227 is interrupted by a signal handler
228 depends on the initialization state of the entropy buffer
229 and on the request size,
231 If the entropy is not yet initialized, then the call will fail with the
234 If the entropy pool has been initialized
235 and the request size is large
236 .RI ( buflen "\ >\ 256),"
237 the call either succeeds, returning a partially filled buffer,
238 or fails with the error
240 If the entropy pool has been initialized and the request size is small
241 .RI ( buflen "\ <=\ 256),"
246 Instead, it will return all of the bytes that have been requested.
250 blocking requests of any size can be interrupted by a signal
251 (the call fails with the error
258 for small values (<=\ 256) of
260 is the preferred mode of usage.
262 The special treatment of small values of
264 was designed for compatibility with
272 always check the return value,
273 to determine whether either an error occurred
274 or fewer bytes than requested were returned.
279 is less than or equal to 256,
280 a return of fewer bytes than requested should never happen,
281 but the careful programmer will check for this anyway!
282 .SS Choice of random device
283 Unless you are doing long-term key generation (and perhaps not even
284 then), you probably shouldn't be using the
293 the cryptographic algorithms used for
295 are quite conservative, and so should be sufficient for all purposes.
301 is that the operation can block.
302 Furthermore, dealing with the partially fulfilled
303 requests that can occur when using
307 increases code complexity.
309 .SS Usage recommendations
310 The kernel random-number generator
311 relies on entropy gathered from device drivers and other sources of
313 It is designed to produce a small
314 amount of high-quality seed material to seed a
315 cryptographic pseudorandom number generator (CPRNG).
316 It is designed for security, not speed, and is poorly
317 suited to generating large amounts of cryptographic random data.
318 Users should be very economical in the amount of seed
319 material that they consume via
324 Consuming unnecessarily large quantities of data via these interfaces
325 will have a negative impact on other consumers of randomness.
327 These interfaces should not be used to provide large quantities
328 of data for Monte Carlo simulations or other
329 programs/algorithms which are doing probabilistic sampling.
330 And indeed, such usage is unnecessary (and will be slow):
331 instead, use these interfaces to provide a small amount of
332 data used to seed a user-space pseudorandom number generator
333 for use by such applications.
335 .SS Generating cryptographic keys
336 The amount of seed material required to generate a cryptographic key
337 equals the effective key size of the key.
338 For example, a 3072-bit RSA
339 or Diffie-Hellman private key has an effective key size of 128 bits
340 (it requires about 2^128 operations to break) so a key generator
341 needs only 128 bits (16 bytes) of seed material from
344 While some safety margin above that minimum is reasonable, as a guard
345 against flaws in the CPRNG algorithm, no cryptographic primitive
346 available today can hope to promise more than 256 bits of security,
347 so if any program reads more than 256 bits (32 bytes) from the kernel
348 random pool per invocation, or per reasonable reseed interval (not less
349 than one minute), that should be taken as a sign that its cryptography is
351 skillfully implemented.
353 .SS Emulating OpenBSD's getentropy()
356 system call in OpenBSD can be emulated using the following
362 getentropy(void *buf, size_t buflen)
368 ret = getrandom(buf, buflen, 0);
380 As of Linux 3.19, the following bug exists:
381 .\" FIXME patch proposed https://lkml.org/lkml/2014/11/29/16
383 Depending on CPU load,
385 does not react to interrupts before reading all bytes requested.