summaryrefslogtreecommitdiffstats
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-11-07 18:40:26 +0100
committerLennart Poettering <lennart@poettering.net>2018-11-08 09:44:27 +0100
commit94d457e8d936555b3b1ae25592d0624746994166 (patch)
tree747d7158616103180c7c93ac4e0fed6a0c68f1cf /src/basic
parentrandom-util: fix indentation (diff)
downloadsystemd-94d457e8d936555b3b1ae25592d0624746994166.tar.xz
systemd-94d457e8d936555b3b1ae25592d0624746994166.zip
random-util: change high_quality_required bool parameter into a flags parameter
No change in behaviour, just some refactoring.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/random-util.c18
-rw-r--r--src/basic/random-util.h10
2 files changed, 15 insertions, 13 deletions
diff --git a/src/basic/random-util.c b/src/basic/random-util.c
index b6d1cfd1ca..d7543d388a 100644
--- a/src/basic/random-util.c
+++ b/src/basic/random-util.c
@@ -65,19 +65,17 @@ int rdrand64(uint64_t *ret) {
#endif
}
-int genuine_random_bytes(void *p, size_t n, bool high_quality_required) {
+int genuine_random_bytes(void *p, size_t n, RandomFlags flags) {
static int have_syscall = -1;
_cleanup_close_ int fd = -1;
size_t already_done = 0;
int r;
- /* Gathers some randomness from the kernel. This call will never block. If
- * high_quality_required, it will always return some data from the kernel,
- * regardless of whether the random pool is fully initialized or not.
- * Otherwise, it will return success if at least some random bytes were
- * successfully acquired, and an error if the kernel has no entropy whatsover
- * for us. */
+ /* Gathers some randomness from the kernel. This call will never block. If RANDOM_EXTEND_WITH_PSEUDO is unset,
+ * it will always return some data from the kernel, regardless of whether the random pool is fully initialized
+ * or not. Otherwise, it will return success if at least some random bytes were successfully acquired, and an
+ * error if the kernel has no entropy whatsover for us. */
/* Use the getrandom() syscall unless we know we don't have it. */
if (have_syscall != 0 && !HAS_FEATURE_MEMORY_SANITIZER) {
@@ -86,7 +84,7 @@ int genuine_random_bytes(void *p, size_t n, bool high_quality_required) {
have_syscall = true;
if ((size_t) r == n)
return 0;
- if (!high_quality_required) {
+ if (FLAGS_SET(flags, RANDOM_EXTEND_WITH_PSEUDO)) {
/* Fill in the remaining bytes using pseudorandom values */
pseudo_random_bytes((uint8_t*) p + r, n - r);
return 0;
@@ -110,7 +108,7 @@ int genuine_random_bytes(void *p, size_t n, bool high_quality_required) {
* a best-effort basis. */
have_syscall = true;
- if (!high_quality_required) {
+ if (FLAGS_SET(flags, RANDOM_EXTEND_WITH_PSEUDO)) {
uint64_t u;
size_t k;
@@ -207,7 +205,7 @@ void pseudo_random_bytes(void *p, size_t n) {
void random_bytes(void *p, size_t n) {
- if (genuine_random_bytes(p, n, false) >= 0)
+ if (genuine_random_bytes(p, n, RANDOM_EXTEND_WITH_PSEUDO) >= 0)
return;
/* If for some reason some user made /dev/urandom unavailable to us, or the kernel has no entropy, use a PRNG instead. */
diff --git a/src/basic/random-util.h b/src/basic/random-util.h
index 0429ea5e52..6328f661d2 100644
--- a/src/basic/random-util.h
+++ b/src/basic/random-util.h
@@ -5,9 +5,13 @@
#include <stddef.h>
#include <stdint.h>
-int genuine_random_bytes(void *p, size_t n, bool high_quality_required); /* returns "genuine" randomness, optionally filled upwith pseudo random, if not enough is available */
-void pseudo_random_bytes(void *p, size_t n); /* returns only pseudo-randommess (but possibly seeded from something better) */
-void random_bytes(void *p, size_t n); /* returns genuine randomness if cheaply available, and pseudo randomness if not. */
+typedef enum RandomFlags {
+ RANDOM_EXTEND_WITH_PSEUDO = 1 << 0, /* If we can't get enough genuine randomness, but some, fill up the rest with pseudo-randomness */
+} RandomFlags;
+
+int genuine_random_bytes(void *p, size_t n, RandomFlags flags); /* returns "genuine" randomness, optionally filled upwith pseudo random, if not enough is available */
+void pseudo_random_bytes(void *p, size_t n); /* returns only pseudo-randommess (but possibly seeded from something better) */
+void random_bytes(void *p, size_t n); /* returns genuine randomness if cheaply available, and pseudo randomness if not. */
void initialize_srand(void);