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 pool (i.e., the same source as the
52 This behavior can be changed via the
58 pool 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 pool 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 pool is limited based on the entropy that can be obtained from environmental
94 If the number of available bytes in the
96 pool 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 pool, 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 pool 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.
171 This system call is Linux-specific.
178 does not involve the use of pathnames or file descriptors.
181 can be useful in cases where
186 and where an application (e.g., a daemon during start-up)
187 closes a file descriptor for one of these files
188 that was opened by a library.
190 .SS Maximum number of bytes returned
191 As of Linux 3.19 the following limits apply:
193 When reading from the
195 pool, a maximum of 33554431 bytes is returned by a single call to
199 has a size of 32 bits.
201 When reading from the
203 pool, a maximum of 512 bytes is returned.
205 .SS Initialization of the entropy pool
206 The kernel collects bits of entropy from the environment.
207 When a sufficient number of random bits has been collected, the
209 entropy pool is considered to be initialized.
210 This state is normally reached early in the system bootstrap phase.
211 .SS Interruption by a signal handler
212 When reading from the
218 will block until the entropy pool has been initialized
222 If a request is made to read a large number of bytes (more than 256),
224 will block until those bytes have been generated and transferred
225 from kernel memory to
227 When reading from the
233 will block until some random bytes become available
238 The behavior when a call to
240 that is blocked while reading from the
242 pool is interrupted by a signal handler
243 depends on the initialization state of the entropy buffer
244 and on the request size,
246 If the entropy is not yet initialized, then the call will fail with the
249 If the entropy pool has been initialized
250 and the request size is large
251 .RI ( buflen "\ >\ 256),"
252 the call either succeeds, returning a partially filled buffer,
253 or fails with the error
255 If the entropy pool has been initialized and the request size is small
256 .RI ( buflen "\ <=\ 256),"
261 Instead, it will return all of the bytes that have been requested.
263 When reading from the
265 pool, blocking requests of any size can be interrupted by a signal handler
266 (the call fails with the error
271 to read small buffers (<=\ 256 bytes) from the
273 pool is the preferred mode of usage.
275 The special treatment of small values of
277 was designed for compatibility with
285 always check the return value,
286 to determine whether either an error occurred
287 or fewer bytes than requested were returned.
292 is less than or equal to 256,
293 a return of fewer bytes than requested should never happen,
294 but the careful programmer will check for this anyway!
295 .SS Choice of random device
296 Unless you are doing long-term key generation (and perhaps not even
297 then), you probably shouldn't be using the
310 The cryptographic algorithms used for the
312 pool are quite conservative, and so should be sufficient for all purposes.
318 is that the operation can block.
319 Furthermore, dealing with the partially fulfilled
320 requests that can occur when using
324 increases code complexity.
326 .SS Usage recommendations
327 The kernel random-number generator
328 relies on entropy gathered from device drivers and other sources of
330 It is designed to produce a small
331 amount of high-quality seed material to seed a
332 cryptographic pseudorandom number generator (CPRNG).
333 It is designed for security, not speed, and is poorly
334 suited to generating large amounts of cryptographic random data.
335 Users should be very economical in the amount of seed
336 material that they consume via
341 Consuming unnecessarily large quantities of data via these interfaces
342 will have a negative impact on other consumers of randomness.
344 These interfaces should not be used to provide large quantities
345 of data for Monte Carlo simulations or other
346 programs/algorithms which are doing probabilistic sampling.
347 And indeed, such usage is unnecessary (and will be slow):
348 instead, use these interfaces to provide a small amount of
349 data used to seed a user-space pseudorandom number generator
350 for use by such applications.
352 .SS Generating cryptographic keys
353 The amount of seed material required to generate a cryptographic key
354 equals the effective key size of the key.
355 For example, a 3072-bit RSA
356 or Diffie-Hellman private key has an effective key size of 128 bits
357 (it requires about 2^128 operations to break) so a key generator
358 needs only 128 bits (16 bytes) of seed material from
361 While some safety margin above that minimum is reasonable, as a guard
362 against flaws in the CPRNG algorithm, no cryptographic primitive
363 available today can hope to promise more than 256 bits of security,
364 so if any program reads more than 256 bits (32 bytes) from the kernel
365 random pool per invocation, or per reasonable reseed interval (not less
366 than one minute), that should be taken as a sign that its cryptography is
368 skillfully implemented.
370 .SS Emulating OpenBSD's getentropy()
373 system call in OpenBSD can be emulated using the following
379 getentropy(void *buf, size_t buflen)
385 ret = getrandom(buf, buflen, 0);
397 As of Linux 3.19, the following bug exists:
398 .\" FIXME patch proposed https://lkml.org/lkml/2014/11/29/16
400 Depending on CPU load,
402 does not react to interrupts before reading all bytes requested.