]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
3df3e884 | 2 | |
11c3a366 | 3 | #include <elf.h> |
3df3e884 | 4 | #include <errno.h> |
3df3e884 | 5 | #include <fcntl.h> |
dccca82b | 6 | #include <linux/random.h> |
11c3a366 | 7 | #include <stdbool.h> |
dccca82b | 8 | #include <stdint.h> |
11c3a366 | 9 | #include <stdlib.h> |
dccca82b | 10 | #include <string.h> |
11c3a366 | 11 | #include <sys/time.h> |
11c3a366 | 12 | |
349cc4a5 | 13 | #if HAVE_SYS_AUXV_H |
5224c2c7 ZJS |
14 | # include <sys/auxv.h> |
15 | #endif | |
16 | ||
349cc4a5 | 17 | #if USE_SYS_RANDOM_H |
5224c2c7 ZJS |
18 | # include <sys/random.h> |
19 | #else | |
20 | # include <linux/random.h> | |
a67dab34 | 21 | #endif |
3df3e884 | 22 | |
3ffd4af2 | 23 | #include "fd-util.h" |
c004493c | 24 | #include "io-util.h" |
3ffd4af2 | 25 | #include "missing.h" |
3df3e884 RC |
26 | #include "random-util.h" |
27 | #include "time-util.h" | |
3df3e884 | 28 | |
f0d09059 | 29 | int acquire_random_bytes(void *p, size_t n, bool high_quality_required) { |
3df3e884 RC |
30 | static int have_syscall = -1; |
31 | ||
32 | _cleanup_close_ int fd = -1; | |
da6053d0 | 33 | size_t already_done = 0; |
7b54715d | 34 | int r; |
3df3e884 | 35 | |
f0d09059 ZJS |
36 | /* Gathers some randomness from the kernel. This call will never block. If |
37 | * high_quality_required, it will always return some data from the kernel, | |
38 | * regardless of whether the random pool is fully initialized or not. | |
39 | * Otherwise, it will return success if at least some random bytes were | |
40 | * successfully acquired, and an error if the kernel has no entropy whatsover | |
41 | * for us. */ | |
3df3e884 | 42 | |
f0d09059 | 43 | /* Use the getrandom() syscall unless we know we don't have it. */ |
2e69f411 | 44 | if (have_syscall != 0 && !HAS_FEATURE_MEMORY_SANITIZER) { |
3df3e884 | 45 | r = getrandom(p, n, GRND_NONBLOCK); |
f0d09059 | 46 | if (r > 0) { |
3df3e884 | 47 | have_syscall = true; |
7b54715d | 48 | if ((size_t) r == n) |
f0d09059 ZJS |
49 | return 0; |
50 | if (!high_quality_required) { | |
ed88a900 | 51 | /* Fill in the remaining bytes using pseudorandom values */ |
f0d09059 ZJS |
52 | pseudorandom_bytes((uint8_t*) p + r, n - r); |
53 | return 0; | |
54 | } | |
55 | ||
56 | already_done = r; | |
57 | } else if (errno == ENOSYS) | |
58 | /* We lack the syscall, continue with reading from /dev/urandom. */ | |
59 | have_syscall = false; | |
60 | else if (errno == EAGAIN) { | |
61 | /* The kernel has no entropy whatsoever. Let's remember to | |
62 | * use the syscall the next time again though. | |
63 | * | |
64 | * If high_quality_required is false, return an error so that | |
65 | * random_bytes() can produce some pseudorandom | |
66 | * bytes. Otherwise, fall back to /dev/urandom, which we know | |
67 | * is empty, but the kernel will produce some bytes for us on | |
68 | * a best-effort basis. */ | |
69 | have_syscall = true; | |
70 | ||
71 | if (!high_quality_required) | |
72 | return -ENODATA; | |
3df3e884 | 73 | } else |
f0d09059 | 74 | return -errno; |
3df3e884 RC |
75 | } |
76 | ||
77 | fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
78 | if (fd < 0) | |
79 | return errno == ENOENT ? -ENOSYS : -errno; | |
80 | ||
f0d09059 | 81 | return loop_read_exact(fd, (uint8_t*) p + already_done, n - already_done, true); |
3df3e884 RC |
82 | } |
83 | ||
84 | void initialize_srand(void) { | |
85 | static bool srand_called = false; | |
86 | unsigned x; | |
349cc4a5 | 87 | #if HAVE_SYS_AUXV_H |
3df3e884 RC |
88 | void *auxv; |
89 | #endif | |
90 | ||
91 | if (srand_called) | |
92 | return; | |
93 | ||
349cc4a5 | 94 | #if HAVE_SYS_AUXV_H |
f0d09059 ZJS |
95 | /* The kernel provides us with 16 bytes of entropy in auxv, so let's |
96 | * try to make use of that to seed the pseudo-random generator. It's | |
97 | * better than nothing... */ | |
3df3e884 RC |
98 | |
99 | auxv = (void*) getauxval(AT_RANDOM); | |
ad6b1fa2 | 100 | if (auxv) { |
b5fa4c77 | 101 | assert_cc(sizeof(x) <= 16); |
ad6b1fa2 LP |
102 | memcpy(&x, auxv, sizeof(x)); |
103 | } else | |
3df3e884 | 104 | #endif |
ad6b1fa2 LP |
105 | x = 0; |
106 | ||
3df3e884 RC |
107 | x ^= (unsigned) now(CLOCK_REALTIME); |
108 | x ^= (unsigned) gettid(); | |
109 | ||
110 | srand(x); | |
111 | srand_called = true; | |
112 | } | |
113 | ||
6a06b1a5 ZJS |
114 | /* INT_MAX gives us only 31 bits, so use 24 out of that. */ |
115 | #if RAND_MAX >= INT_MAX | |
116 | # define RAND_STEP 3 | |
117 | #else | |
118 | /* SHORT_INT_MAX or lower gives at most 15 bits, we just just 8 out of that. */ | |
119 | # define RAND_STEP 1 | |
120 | #endif | |
121 | ||
122 | void pseudorandom_bytes(void *p, size_t n) { | |
3df3e884 | 123 | uint8_t *q; |
6a06b1a5 ZJS |
124 | |
125 | initialize_srand(); | |
126 | ||
127 | for (q = p; q < (uint8_t*) p + n; q += RAND_STEP) { | |
128 | unsigned rr; | |
129 | ||
130 | rr = (unsigned) rand(); | |
131 | ||
132 | #if RAND_STEP >= 3 | |
7b54715d | 133 | if ((size_t) (q - (uint8_t*) p + 2) < n) |
6a06b1a5 ZJS |
134 | q[2] = rr >> 16; |
135 | #endif | |
136 | #if RAND_STEP >= 2 | |
7b54715d | 137 | if ((size_t) (q - (uint8_t*) p + 1) < n) |
6a06b1a5 ZJS |
138 | q[1] = rr >> 8; |
139 | #endif | |
140 | q[0] = rr; | |
141 | } | |
142 | } | |
143 | ||
144 | void random_bytes(void *p, size_t n) { | |
3df3e884 RC |
145 | int r; |
146 | ||
f0d09059 | 147 | r = acquire_random_bytes(p, n, false); |
3df3e884 RC |
148 | if (r >= 0) |
149 | return; | |
150 | ||
6a06b1a5 ZJS |
151 | /* If some idiot made /dev/urandom unavailable to us, or the |
152 | * kernel has no entropy, use a PRNG instead. */ | |
153 | return pseudorandom_bytes(p, n); | |
3df3e884 | 154 | } |