From 9da4e30c757b1b9cb70099ee67a0f0d7800c9eb2 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 25 Jun 2018 11:17:13 +0100 Subject: [PATCH] random_bytes improvements for FreeBSD (from 12.x serie) giving the possiblity to pre-fill the buffer. A new getrandom function was added for future version with a similar interface than Linux's syscall. --- ext/standard/random.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index 691fd9a394..1a394aae81 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -33,8 +33,11 @@ #ifdef __linux__ # include #endif -#if defined(__OpenBSD__) || defined(__NetBSD__) +#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) # include +# if __FreeBSD__ && __FreeBSD_version > 1200000 + #include +# endif #endif #ifdef ZTS @@ -96,8 +99,8 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, zend_bool should_throw) #else size_t read_bytes = 0; ssize_t n; -#if defined(__linux__) && defined(SYS_getrandom) - /* Linux getrandom(2) syscall */ +#if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) + /* Linux getrandom(2) syscall or FreeBSD getrandom(2) function*/ /* Keep reading until we get enough entropy */ while (read_bytes < size) { /* Below, (bytes + read_bytes) is pointer arithmetic. @@ -110,7 +113,11 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, zend_bool should_throw) */ size_t amount_to_read = size - read_bytes; +#if defined(__linux__) n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0); +#else + n = getrandom(bytes + read_bytes, amount_to_read, 0); +#endif if (n == -1) { if (errno == ENOSYS) { -- 2.49.0