From 94d457e8d936555b3b1ae25592d0624746994166 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 7 Nov 2018 18:40:26 +0100 Subject: [PATCH] random-util: change high_quality_required bool parameter into a flags parameter No change in behaviour, just some refactoring. --- src/basic/random-util.c | 18 ++++++++---------- src/basic/random-util.h | 10 +++++++--- src/firstboot/firstboot.c | 2 +- src/libsystemd/sd-id128/sd-id128.c | 2 +- src/test/test-random-util.c | 8 ++++---- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/basic/random-util.c b/src/basic/random-util.c index b6d1cfd..d7543d3 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 0429ea5..6328f66 100644 --- a/src/basic/random-util.h +++ b/src/basic/random-util.h @@ -5,9 +5,13 @@ #include #include -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); diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index 96260fa..ee267dc 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -647,7 +647,7 @@ static int process_root_password(void) { if (!arg_root_password) return 0; - r = genuine_random_bytes(raw, 16, true); + r = genuine_random_bytes(raw, 16, 0); if (r < 0) return log_error_errno(r, "Failed to get salt: %m"); diff --git a/src/libsystemd/sd-id128/sd-id128.c b/src/libsystemd/sd-id128/sd-id128.c index bba40a0..2180700 100644 --- a/src/libsystemd/sd-id128/sd-id128.c +++ b/src/libsystemd/sd-id128/sd-id128.c @@ -272,7 +272,7 @@ _public_ int sd_id128_randomize(sd_id128_t *ret) { assert_return(ret, -EINVAL); - r = genuine_random_bytes(&t, sizeof t, true); + r = genuine_random_bytes(&t, sizeof t, 0); if (r < 0) return r; diff --git a/src/test/test-random-util.c b/src/test/test-random-util.c index 82902b7..adb2588 100644 --- a/src/test/test-random-util.c +++ b/src/test/test-random-util.c @@ -5,14 +5,14 @@ #include "log.h" #include "tests.h" -static void test_genuine_random_bytes(bool high_quality_required) { +static void test_genuine_random_bytes(RandomFlags flags) { uint8_t buf[16] = {}; unsigned i; log_info("/* %s */", __func__); for (i = 1; i < sizeof buf; i++) { - assert_se(genuine_random_bytes(buf, i, high_quality_required) == 0); + assert_se(genuine_random_bytes(buf, i, flags) == 0); if (i + 1 < sizeof buf) assert_se(buf[i] == 0); @@ -54,8 +54,8 @@ static void test_rdrand64(void) { int main(int argc, char **argv) { test_setup_logging(LOG_DEBUG); - test_genuine_random_bytes(false); - test_genuine_random_bytes(true); + test_genuine_random_bytes(RANDOM_EXTEND_WITH_PSEUDO); + test_genuine_random_bytes(0); test_pseudo_random_bytes(); -- 2.7.4