random-util: change high_quality_required bool parameter into a flags parameter

No change in behaviour, just some refactoring.
This commit is contained in:
Lennart Poettering 2018-11-07 18:40:26 +01:00
parent afff8f16ae
commit 94d457e8d9
5 changed files with 21 additions and 19 deletions

View file

@ -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. */

View file

@ -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);

View file

@ -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");

View file

@ -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;

View file

@ -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();