1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #if defined(__i386__) || defined(__x86_64__)
10 #include <linux/random.h>
18 # include <sys/auxv.h>
22 # include <sys/random.h>
24 # include <linux/random.h>
30 #include "random-util.h"
31 #include "time-util.h"
33 #if HAS_FEATURE_MEMORY_SANITIZER
34 #include <sanitizer/msan_interface.h>
37 int rdrand(unsigned long *ret
) {
39 #if defined(__i386__) || defined(__x86_64__)
40 static int have_rdrand
= -1;
43 if (have_rdrand
< 0) {
44 uint32_t eax
, ebx
, ecx
, edx
;
46 /* Check if RDRAND is supported by the CPU */
47 if (__get_cpuid(1, &eax
, &ebx
, &ecx
, &edx
) == 0) {
52 have_rdrand
= !!(ecx
& (1U << 30));
58 asm volatile("rdrand %0;"
63 #if HAS_FEATURE_MEMORY_SANITIZER
64 __msan_unpoison(&err
, sizeof(err
));
76 int genuine_random_bytes(void *p
, size_t n
, RandomFlags flags
) {
77 static int have_syscall
= -1;
78 _cleanup_close_
int fd
= -1;
79 bool got_some
= false;
82 /* Gathers some randomness from the kernel (or the CPU if the RANDOM_ALLOW_RDRAND flag is set). This call won't
83 * block, unless the RANDOM_BLOCK flag is set. If RANDOM_DONT_DRAIN is set, an error is returned if the random
84 * pool is not initialized. Otherwise it will always return some data from the kernel, regardless of whether
85 * the random pool is fully initialized or not. */
90 if (FLAGS_SET(flags
, RANDOM_ALLOW_RDRAND
))
91 /* Try x86-64' RDRAND intrinsic if we have it. We only use it if high quality randomness is not
92 * required, as we don't trust it (who does?). Note that we only do a single iteration of RDRAND here,
93 * even though the Intel docs suggest calling this in a tight loop of 10 invocations or so. That's
94 * because we don't really care about the quality here. We generally prefer using RDRAND if the caller
95 * allows us too, since this way we won't drain the kernel randomness pool if we don't need it, as the
96 * pool's entropy is scarce. */
101 if (rdrand(&u
) < 0) {
102 if (got_some
&& FLAGS_SET(flags
, RANDOM_EXTEND_WITH_PSEUDO
)) {
103 /* Fill in the remaining bytes using pseudo-random values */
104 pseudo_random_bytes(p
, n
);
108 /* OK, this didn't work, let's go to getrandom() + /dev/urandom instead */
112 m
= MIN(sizeof(u
), n
);
115 p
= (uint8_t*) p
+ m
;
119 return 0; /* Yay, success! */
124 /* Use the getrandom() syscall unless we know we don't have it. */
125 if (have_syscall
!= 0 && !HAS_FEATURE_MEMORY_SANITIZER
) {
128 r
= getrandom(p
, n
, FLAGS_SET(flags
, RANDOM_BLOCK
) ? 0 : GRND_NONBLOCK
);
133 return 0; /* Yay, success! */
135 assert((size_t) r
< n
);
136 p
= (uint8_t*) p
+ r
;
139 if (FLAGS_SET(flags
, RANDOM_EXTEND_WITH_PSEUDO
)) {
140 /* Fill in the remaining bytes using pseudo-random values */
141 pseudo_random_bytes(p
, n
);
147 /* Hmm, we didn't get enough good data but the caller insists on good data? Then try again */
148 if (FLAGS_SET(flags
, RANDOM_BLOCK
))
151 /* Fill in the rest with /dev/urandom */
158 } else if (errno
== ENOSYS
) {
159 /* We lack the syscall, continue with reading from /dev/urandom. */
160 have_syscall
= false;
163 } else if (errno
== EAGAIN
) {
164 /* The kernel has no entropy whatsoever. Let's remember to use the syscall the next
167 * If RANDOM_DONT_DRAIN is set, return an error so that random_bytes() can produce some
168 * pseudo-random bytes instead. Otherwise, fall back to /dev/urandom, which we know is empty,
169 * but the kernel will produce some bytes for us on a best-effort basis. */
172 if (got_some
&& FLAGS_SET(flags
, RANDOM_EXTEND_WITH_PSEUDO
)) {
173 /* Fill in the remaining bytes using pseudorandom values */
174 pseudo_random_bytes(p
, n
);
178 if (FLAGS_SET(flags
, RANDOM_DONT_DRAIN
))
181 /* Use /dev/urandom instead */
188 fd
= open("/dev/urandom", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
190 return errno
== ENOENT
? -ENOSYS
: -errno
;
192 return loop_read_exact(fd
, p
, n
, true);
195 void initialize_srand(void) {
196 static bool srand_called
= false;
207 /* The kernel provides us with 16 bytes of entropy in auxv, so let's
208 * try to make use of that to seed the pseudo-random generator. It's
209 * better than nothing... */
211 auxv
= (const void*) getauxval(AT_RANDOM
);
213 assert_cc(sizeof(x
) <= 16);
214 memcpy(&x
, auxv
, sizeof(x
));
219 x
^= (unsigned) now(CLOCK_REALTIME
);
220 x
^= (unsigned) gettid();
229 /* INT_MAX gives us only 31 bits, so use 24 out of that. */
230 #if RAND_MAX >= INT_MAX
233 /* SHORT_INT_MAX or lower gives at most 15 bits, we just just 8 out of that. */
237 void pseudo_random_bytes(void *p
, size_t n
) {
242 for (q
= p
; q
< (uint8_t*) p
+ n
; q
+= RAND_STEP
) {
245 rr
= (unsigned) rand();
248 if ((size_t) (q
- (uint8_t*) p
+ 2) < n
)
252 if ((size_t) (q
- (uint8_t*) p
+ 1) < n
)
259 void random_bytes(void *p
, size_t n
) {
261 if (genuine_random_bytes(p
, n
, RANDOM_EXTEND_WITH_PSEUDO
|RANDOM_DONT_DRAIN
|RANDOM_ALLOW_RDRAND
) >= 0)
264 /* If for some reason some user made /dev/urandom unavailable to us, or the kernel has no entropy, use a PRNG instead. */
265 pseudo_random_bytes(p
, n
);