random-util: rename acquire_random_bytes() → genuine_random_bytes()
authorLennart Poettering <lennart@poettering.net>
Wed, 7 Nov 2018 17:27:57 +0000 (18:27 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 8 Nov 2018 08:44:27 +0000 (09:44 +0100)
It's more descriptive, since we also have a function random_bytes()
which sounds very similar.

Also rename pseudorandom_bytes() to pseudo_random_bytes(). This way the
two functions are nicely systematic, one returning genuine random bytes
and the other pseudo random ones.

src/basic/random-util.c
src/basic/random-util.h
src/firstboot/firstboot.c
src/libsystemd/sd-id128/sd-id128.c
src/test/test-random-util.c

index 071a41f..5e4ed2d 100644 (file)
@@ -65,7 +65,7 @@ int rdrand64(uint64_t *ret) {
 #endif
 }
 
-int acquire_random_bytes(void *p, size_t n, bool high_quality_required) {
+int genuine_random_bytes(void *p, size_t n, bool high_quality_required) {
         static int have_syscall = -1;
 
         _cleanup_close_ int fd = -1;
@@ -88,7 +88,7 @@ int acquire_random_bytes(void *p, size_t n, bool high_quality_required) {
                                 return 0;
                         if (!high_quality_required) {
                                 /* Fill in the remaining bytes using pseudorandom values */
-                                pseudorandom_bytes((uint8_t*) p + r, n - r);
+                                pseudo_random_bytes((uint8_t*) p + r, n - r);
                                 return 0;
                         }
 
@@ -124,7 +124,7 @@ int acquire_random_bytes(void *p, size_t n, bool high_quality_required) {
                                 memcpy(p, &u, k);
 
                                 /* We only get 64bit out of RDRAND, the rest let's fill up with pseudo-random crap. */
-                                pseudorandom_bytes((uint8_t*) p + k, n - k);
+                                pseudo_random_bytes((uint8_t*) p + k, n - k);
                                 return 0;
                         }
                 } else
@@ -180,7 +180,7 @@ void initialize_srand(void) {
 #  define RAND_STEP 1
 #endif
 
-void pseudorandom_bytes(void *p, size_t n) {
+void pseudo_random_bytes(void *p, size_t n) {
         uint8_t *q;
 
         initialize_srand();
@@ -203,13 +203,10 @@ void pseudorandom_bytes(void *p, size_t n) {
 }
 
 void random_bytes(void *p, size_t n) {
-        int r;
 
-        r = acquire_random_bytes(p, n, false);
-        if (r >= 0)
+        if (genuine_random_bytes(p, n, false) >= 0)
                 return;
 
-        /* If some idiot made /dev/urandom unavailable to us, or the
-         * kernel has no entropy, use a PRNG instead. */
-        return pseudorandom_bytes(p, n);
+        /* If for some reason some user made /dev/urandom unavailable to us, or the kernel has no entropy, use a PRNG instead. */
+        pseudo_random_bytes(p, n);
 }
index affcc9a..0429ea5 100644 (file)
@@ -5,9 +5,10 @@
 #include <stddef.h>
 #include <stdint.h>
 
-int acquire_random_bytes(void *p, size_t n, bool high_quality_required);
-void pseudorandom_bytes(void *p, size_t n);
-void random_bytes(void *p, size_t n);
+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. */
+
 void initialize_srand(void);
 
 static inline uint64_t random_u64(void) {
index c5deb66..96260fa 100644 (file)
@@ -647,7 +647,7 @@ static int process_root_password(void) {
         if (!arg_root_password)
                 return 0;
 
-        r = acquire_random_bytes(raw, 16, true);
+        r = genuine_random_bytes(raw, 16, true);
         if (r < 0)
                 return log_error_errno(r, "Failed to get salt: %m");
 
index b7cca83..bba40a0 100644 (file)
@@ -272,7 +272,7 @@ _public_ int sd_id128_randomize(sd_id128_t *ret) {
 
         assert_return(ret, -EINVAL);
 
-        r = acquire_random_bytes(&t, sizeof t, true);
+        r = genuine_random_bytes(&t, sizeof t, true);
         if (r < 0)
                 return r;
 
index 9652a0a..82902b7 100644 (file)
@@ -5,14 +5,14 @@
 #include "log.h"
 #include "tests.h"
 
-static void test_acquire_random_bytes(bool high_quality_required) {
+static void test_genuine_random_bytes(bool high_quality_required) {
         uint8_t buf[16] = {};
         unsigned i;
 
         log_info("/* %s */", __func__);
 
         for (i = 1; i < sizeof buf; i++) {
-                assert_se(acquire_random_bytes(buf, i, high_quality_required) == 0);
+                assert_se(genuine_random_bytes(buf, i, high_quality_required) == 0);
                 if (i + 1 < sizeof buf)
                         assert_se(buf[i] == 0);
 
@@ -20,14 +20,14 @@ static void test_acquire_random_bytes(bool high_quality_required) {
         }
 }
 
-static void test_pseudorandom_bytes(void) {
+static void test_pseudo_random_bytes(void) {
         uint8_t buf[16] = {};
         unsigned i;
 
         log_info("/* %s */", __func__);
 
         for (i = 1; i < sizeof buf; i++) {
-                pseudorandom_bytes(buf, i);
+                pseudo_random_bytes(buf, i);
                 if (i + 1 < sizeof buf)
                         assert_se(buf[i] == 0);
 
@@ -54,10 +54,10 @@ static void test_rdrand64(void) {
 int main(int argc, char **argv) {
         test_setup_logging(LOG_DEBUG);
 
-        test_acquire_random_bytes(false);
-        test_acquire_random_bytes(true);
+        test_genuine_random_bytes(false);
+        test_genuine_random_bytes(true);
 
-        test_pseudorandom_bytes();
+        test_pseudo_random_bytes();
 
         test_rdrand64();