summaryrefslogtreecommitdiff
path: root/libs/libsodium/src/randombytes/salsa20/randombytes_salsa20_random.c
diff options
context:
space:
mode:
Diffstat (limited to 'libs/libsodium/src/randombytes/salsa20/randombytes_salsa20_random.c')
-rw-r--r--libs/libsodium/src/randombytes/salsa20/randombytes_salsa20_random.c39
1 files changed, 26 insertions, 13 deletions
diff --git a/libs/libsodium/src/randombytes/salsa20/randombytes_salsa20_random.c b/libs/libsodium/src/randombytes/salsa20/randombytes_salsa20_random.c
index 477fda1c39..e3ec30ff7a 100644
--- a/libs/libsodium/src/randombytes/salsa20/randombytes_salsa20_random.c
+++ b/libs/libsodium/src/randombytes/salsa20/randombytes_salsa20_random.c
@@ -19,9 +19,26 @@
#ifdef __linux__
# ifdef __dietlibc__
# define _LINUX_SOURCE
-# else
+# include <sys/random.h>
+# define HAVE_LINUX_COMPATIBLE_GETRANDOM
+# else /* __dietlibc__ */
# include <sys/syscall.h>
+# if defined(SYS_getrandom) && defined(__NR_getrandom)
+# define getrandom(B, S, F) syscall(SYS_getrandom, (B), (int) (S), (F))
+# define HAVE_LINUX_COMPATIBLE_GETRANDOM
+# endif
+# endif /* __dietlibc__ */
+#elif defined(__FreeBSD__)
+# include <sys/param.h>
+# if defined(__FreeBSD_version) && __FreeBSD_version >= 1200000
+# include <sys/random.h>
+# define HAVE_LINUX_COMPATIBLE_GETRANDOM
# endif
+#endif
+#if !defined(NO_BLOCKING_RANDOM_POLL) && defined(__linux__)
+# define BLOCK_ON_DEV_RANDOM
+#endif
+#ifdef BLOCK_ON_DEV_RANDOM
# include <poll.h>
#endif
#ifdef HAVE_RDRAND
@@ -177,7 +194,7 @@ safe_read(const int fd, void * const buf_, size_t size)
return (ssize_t) (buf - (unsigned char *) buf_);
}
-# if defined(__linux__) && !defined(USE_BLOCKING_RANDOM) && !defined(NO_BLOCKING_RANDOM_POLL)
+# ifdef BLOCK_ON_DEV_RANDOM
static int
randombytes_block_on_dev_random(void)
{
@@ -219,11 +236,11 @@ randombytes_salsa20_random_random_dev_open(void)
const char **device = devices;
int fd;
-# if defined(__linux__) && !defined(USE_BLOCKING_RANDOM) && !defined(NO_BLOCKING_RANDOM_POLL)
+# ifdef BLOCK_ON_DEV_RANDOM
if (randombytes_block_on_dev_random() != 0) {
return -1;
}
-# endif
+# endif
do {
fd = open(*device, O_RDONLY);
if (fd != -1) {
@@ -246,7 +263,7 @@ randombytes_salsa20_random_random_dev_open(void)
}
# endif
-# if defined(__dietlibc__) || (defined(SYS_getrandom) && defined(__NR_getrandom))
+# ifdef HAVE_LINUX_COMPATIBLE_GETRANDOM
static int
_randombytes_linux_getrandom(void * const buf, const size_t size)
{
@@ -254,11 +271,7 @@ _randombytes_linux_getrandom(void * const buf, const size_t size)
assert(size <= 256U);
do {
-# ifdef __dietlibc__
readnb = getrandom(buf, size, 0);
-# else
- readnb = syscall(SYS_getrandom, buf, (int) size, 0);
-# endif
} while (readnb < 0 && (errno == EINTR || errno == EAGAIN));
return (readnb == (int) size) - 1;
@@ -299,7 +312,7 @@ randombytes_salsa20_random_init(void)
errno = errno_save;
# else
-# if defined(SYS_getrandom) && defined(__NR_getrandom)
+# ifdef HAVE_LINUX_COMPATIBLE_GETRANDOM
{
unsigned char fodder[16];
@@ -310,7 +323,7 @@ randombytes_salsa20_random_init(void)
}
global.getrandom_available = 0;
}
-# endif /* SYS_getrandom */
+# endif /* HAVE_LINUX_COMPATIBLE_GETRANDOM */
if ((global.random_data_source_fd =
randombytes_salsa20_random_random_dev_open()) == -1) {
@@ -343,7 +356,7 @@ randombytes_salsa20_random_stir(void)
# ifdef HAVE_SAFE_ARC4RANDOM
arc4random_buf(stream.key, sizeof stream.key);
-# elif defined(SYS_getrandom) && defined(__NR_getrandom)
+# elif defined(HAVE_LINUX_COMPATIBLE_GETRANDOM)
if (global.getrandom_available != 0) {
if (randombytes_linux_getrandom(stream.key, sizeof stream.key) != 0) {
sodium_misuse(); /* LCOV_EXCL_LINE */
@@ -428,7 +441,7 @@ randombytes_salsa20_random_close(void)
ret = 0;
# endif
-# if defined(SYS_getrandom) && defined(__NR_getrandom)
+# ifdef HAVE_LINUX_COMPATIBLE_GETRANDOM
if (global.getrandom_available != 0) {
ret = 0;
}