diff options
author | Mike Gilbert <floppym@gentoo.org> | 2018-11-08 09:47:16 -0500 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-11-10 14:56:53 +0100 |
commit | 33dbab6fde5423ce61b5544ca49afa13495477a8 (patch) | |
tree | be46001261b2f1786d0706589a36df5031296980 | |
parent | e29a9fcb29a0509ccc30bcfe07ecf5dc2b4de9ea (diff) | |
download | systemd-33dbab6fde5423ce61b5544ca49afa13495477a8.tar.gz systemd-33dbab6fde5423ce61b5544ca49afa13495477a8.tar.bz2 systemd-33dbab6fde5423ce61b5544ca49afa13495477a8.zip |
random-util: allow RDRAND to be used in 32-bit x86 binaries
Rename rdrand64 to rdrand, and switch from uint64_t to unsigned long.
This produces code that will compile/assemble on both x86-64 and x86-32.
This could be useful when running a 32-bit copy of systemd on a modern
Intel processor.
RDRAND is inherently arch-specific, so relying on the compiler-defined
'long' type seems reasonable.
-rw-r--r-- | src/basic/random-util.c | 14 | ||||
-rw-r--r-- | src/basic/random-util.h | 2 | ||||
-rw-r--r-- | src/test/test-random-util.c | 10 |
3 files changed, 13 insertions, 13 deletions
diff --git a/src/basic/random-util.c b/src/basic/random-util.c index c17c09765b..4a36ad5119 100644 --- a/src/basic/random-util.c +++ b/src/basic/random-util.c @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -#ifdef __x86_64__ +#if defined(__i386__) || defined(__x86_64__) #include <cpuid.h> #endif @@ -34,9 +34,9 @@ #include <sanitizer/msan_interface.h> #endif -int rdrand64(uint64_t *ret) { +int rdrand(unsigned long *ret) { -#ifdef __x86_64__ +#if defined(__i386__) || defined(__x86_64__) static int have_rdrand = -1; unsigned char err; @@ -95,10 +95,10 @@ int genuine_random_bytes(void *p, size_t n, RandomFlags flags) { * allows us too, since this way we won't drain the kernel randomness pool if we don't need it, as the * pool's entropy is scarce. */ for (;;) { - uint64_t u; + unsigned long u; size_t m; - if (rdrand64(&u) < 0) { + if (rdrand(&u) < 0) { if (got_some && FLAGS_SET(flags, RANDOM_EXTEND_WITH_PSEUDO)) { /* Fill in the remaining bytes using pseudo-random values */ pseudo_random_bytes(p, n); @@ -198,7 +198,7 @@ void initialize_srand(void) { #if HAVE_SYS_AUXV_H const void *auxv; #endif - uint64_t k; + unsigned long k; if (srand_called) return; @@ -219,7 +219,7 @@ void initialize_srand(void) { x ^= (unsigned) now(CLOCK_REALTIME); x ^= (unsigned) gettid(); - if (rdrand64(&k) >= 0) + if (rdrand(&k) >= 0) x ^= (unsigned) k; srand(x); diff --git a/src/basic/random-util.h b/src/basic/random-util.h index 7a960cf9e7..3e8c288d3d 100644 --- a/src/basic/random-util.h +++ b/src/basic/random-util.h @@ -30,4 +30,4 @@ static inline uint32_t random_u32(void) { return u; } -int rdrand64(uint64_t *ret); +int rdrand(unsigned long *ret); diff --git a/src/test/test-random-util.c b/src/test/test-random-util.c index a59c6374b2..94c431f7e6 100644 --- a/src/test/test-random-util.c +++ b/src/test/test-random-util.c @@ -35,19 +35,19 @@ static void test_pseudo_random_bytes(void) { } } -static void test_rdrand64(void) { +static void test_rdrand(void) { int r, i; for (i = 0; i < 10; i++) { - uint64_t x = 0; + unsigned long x = 0; - r = rdrand64(&x); + r = rdrand(&x); if (r < 0) { log_error_errno(r, "RDRAND failed: %m"); return; } - printf("%" PRIx64 "\n", x); + printf("%lx\n", x); } } @@ -61,7 +61,7 @@ int main(int argc, char **argv) { test_pseudo_random_bytes(); - test_rdrand64(); + test_rdrand(); return 0; } |