Skip to content

Commit 3335dc2

Browse files
committed
random-util: rename acquire_random_bytes() → genuine_random_bytes()
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.
1 parent 8d2411f commit 3335dc2

File tree

5 files changed

+20
-22
lines changed

5 files changed

+20
-22
lines changed

src/basic/random-util.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int rdrand64(uint64_t *ret) {
6565
#endif
6666
}
6767

68-
int acquire_random_bytes(void *p, size_t n, bool high_quality_required) {
68+
int genuine_random_bytes(void *p, size_t n, bool high_quality_required) {
6969
static int have_syscall = -1;
7070

7171
_cleanup_close_ int fd = -1;
@@ -88,7 +88,7 @@ int acquire_random_bytes(void *p, size_t n, bool high_quality_required) {
8888
return 0;
8989
if (!high_quality_required) {
9090
/* Fill in the remaining bytes using pseudorandom values */
91-
pseudorandom_bytes((uint8_t*) p + r, n - r);
91+
pseudo_random_bytes((uint8_t*) p + r, n - r);
9292
return 0;
9393
}
9494

@@ -124,7 +124,7 @@ int acquire_random_bytes(void *p, size_t n, bool high_quality_required) {
124124
memcpy(p, &u, k);
125125

126126
/* We only get 64bit out of RDRAND, the rest let's fill up with pseudo-random crap. */
127-
pseudorandom_bytes((uint8_t*) p + k, n - k);
127+
pseudo_random_bytes((uint8_t*) p + k, n - k);
128128
return 0;
129129
}
130130
} else
@@ -180,7 +180,7 @@ void initialize_srand(void) {
180180
# define RAND_STEP 1
181181
#endif
182182

183-
void pseudorandom_bytes(void *p, size_t n) {
183+
void pseudo_random_bytes(void *p, size_t n) {
184184
uint8_t *q;
185185

186186
initialize_srand();
@@ -203,13 +203,10 @@ void pseudorandom_bytes(void *p, size_t n) {
203203
}
204204

205205
void random_bytes(void *p, size_t n) {
206-
int r;
207206

208-
r = acquire_random_bytes(p, n, false);
209-
if (r >= 0)
207+
if (genuine_random_bytes(p, n, false) >= 0)
210208
return;
211209

212-
/* If some idiot made /dev/urandom unavailable to us, or the
213-
* kernel has no entropy, use a PRNG instead. */
214-
return pseudorandom_bytes(p, n);
210+
/* If for some reason some user made /dev/urandom unavailable to us, or the kernel has no entropy, use a PRNG instead. */
211+
pseudo_random_bytes(p, n);
215212
}

src/basic/random-util.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
#include <stddef.h>
66
#include <stdint.h>
77

8-
int acquire_random_bytes(void *p, size_t n, bool high_quality_required);
9-
void pseudorandom_bytes(void *p, size_t n);
10-
void random_bytes(void *p, size_t n);
8+
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 */
9+
void pseudo_random_bytes(void *p, size_t n); /* returns only pseudo-randommess (but possibly seeded from something better) */
10+
void random_bytes(void *p, size_t n); /* returns genuine randomness if cheaply available, and pseudo randomness if not. */
11+
1112
void initialize_srand(void);
1213

1314
static inline uint64_t random_u64(void) {

src/firstboot/firstboot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ static int process_root_password(void) {
647647
if (!arg_root_password)
648648
return 0;
649649

650-
r = acquire_random_bytes(raw, 16, true);
650+
r = genuine_random_bytes(raw, 16, true);
651651
if (r < 0)
652652
return log_error_errno(r, "Failed to get salt: %m");
653653

src/libsystemd/sd-id128/sd-id128.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ _public_ int sd_id128_randomize(sd_id128_t *ret) {
272272

273273
assert_return(ret, -EINVAL);
274274

275-
r = acquire_random_bytes(&t, sizeof t, true);
275+
r = genuine_random_bytes(&t, sizeof t, true);
276276
if (r < 0)
277277
return r;
278278

src/test/test-random-util.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@
55
#include "log.h"
66
#include "tests.h"
77

8-
static void test_acquire_random_bytes(bool high_quality_required) {
8+
static void test_genuine_random_bytes(bool high_quality_required) {
99
uint8_t buf[16] = {};
1010
unsigned i;
1111

1212
log_info("/* %s */", __func__);
1313

1414
for (i = 1; i < sizeof buf; i++) {
15-
assert_se(acquire_random_bytes(buf, i, high_quality_required) == 0);
15+
assert_se(genuine_random_bytes(buf, i, high_quality_required) == 0);
1616
if (i + 1 < sizeof buf)
1717
assert_se(buf[i] == 0);
1818

1919
hexdump(stdout, buf, i);
2020
}
2121
}
2222

23-
static void test_pseudorandom_bytes(void) {
23+
static void test_pseudo_random_bytes(void) {
2424
uint8_t buf[16] = {};
2525
unsigned i;
2626

2727
log_info("/* %s */", __func__);
2828

2929
for (i = 1; i < sizeof buf; i++) {
30-
pseudorandom_bytes(buf, i);
30+
pseudo_random_bytes(buf, i);
3131
if (i + 1 < sizeof buf)
3232
assert_se(buf[i] == 0);
3333

@@ -54,10 +54,10 @@ static void test_rdrand64(void) {
5454
int main(int argc, char **argv) {
5555
test_setup_logging(LOG_DEBUG);
5656

57-
test_acquire_random_bytes(false);
58-
test_acquire_random_bytes(true);
57+
test_genuine_random_bytes(false);
58+
test_genuine_random_bytes(true);
5959

60-
test_pseudorandom_bytes();
60+
test_pseudo_random_bytes();
6161

6262
test_rdrand64();
6363

0 commit comments

Comments
 (0)