From a6c43c64d9419dfa888b1c478e658e3e20a4af11 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Sun, 31 May 2020 20:56:05 +0000 Subject: Vendor import apr-1.7.0 --- misc/unix/rand.c | 136 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 94 insertions(+), 42 deletions(-) (limited to 'misc/unix') diff --git a/misc/unix/rand.c b/misc/unix/rand.c index c1e1e8f60dbd..c0567a667e37 100644 --- a/misc/unix/rand.c +++ b/misc/unix/rand.c @@ -43,6 +43,31 @@ #include #endif +#if defined(SYS_RANDOM) +#if defined(HAVE_SYS_RANDOM_H) && \ + defined(HAVE_GETRANDOM) + +#include +#define USE_GETRANDOM + +#elif defined(HAVE_SYS_SYSCALL_H) && \ + defined(HAVE_LINUX_RANDOM_H) && \ + defined(HAVE_DECL_SYS_GETRANDOM) && \ + HAVE_DECL_SYS_GETRANDOM + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include +#include +#include +#define getrandom(buf, buflen, flags) \ + syscall(SYS_getrandom, (buf), (buflen), (flags)) +#define USE_GETRANDOM + +#endif /* HAVE_SYS_RANDOM_H */ +#endif /* SYS_RANDOM */ + #ifndef SHUT_RDWR #define SHUT_RDWR 2 #endif @@ -87,48 +112,7 @@ APR_DECLARE(apr_status_t) apr_os_uuid_get(unsigned char *uuid_data) APR_DECLARE(apr_status_t) apr_generate_random_bytes(unsigned char *buf, apr_size_t length) { -#ifdef DEV_RANDOM - - int fd = -1; - - /* On BSD/OS 4.1, /dev/random gives out 8 bytes at a time, then - * gives EOF, so reading 'length' bytes may require opening the - * device several times. */ - do { - apr_ssize_t rc; - - if (fd == -1) - if ((fd = open(DEV_RANDOM, O_RDONLY)) == -1) - return errno; - - do { - rc = read(fd, buf, length); - } while (rc == -1 && errno == EINTR); - - if (rc < 0) { - int errnum = errno; - close(fd); - return errnum; - } - else if (rc == 0) { - close(fd); - fd = -1; /* force open() again */ - } - else { - buf += rc; - length -= rc; - } - } while (length > 0); - - close(fd); -#elif defined(OS2) - static UCHAR randbyte(); - unsigned int idx; - - for (idx=0; idx 0); + +#elif defined(SYS_RANDOM) && defined(HAVE_ARC4RANDOM_BUF) + + arc4random_buf(buf, length); + +#elif defined(DEV_RANDOM) + + int fd = -1; + + /* On BSD/OS 4.1, /dev/random gives out 8 bytes at a time, then + * gives EOF, so reading 'length' bytes may require opening the + * device several times. */ + do { + apr_ssize_t rc; + + if (fd == -1) + if ((fd = open(DEV_RANDOM, O_RDONLY)) == -1) + return errno; + + do { + rc = read(fd, buf, length); + } while (rc == -1 && errno == EINTR); + + if (rc < 0) { + int errnum = errno; + close(fd); + return errnum; + } + else if (rc == 0) { + close(fd); + fd = -1; /* force open() again */ + } + else { + buf += rc; + length -= rc; + } + } while (length > 0); + + close(fd); + +#elif defined(OS2) + + static UCHAR randbyte(); + unsigned int idx; + + for (idx=0; idx