diff --git a/Zend/Zend.m4 b/Zend/Zend.m4 index 111426638ac3a..868a04d91efe8 100644 --- a/Zend/Zend.m4 +++ b/Zend/Zend.m4 @@ -401,13 +401,14 @@ if test -r "/dev/urandom" && test -c "/dev/urandom"; then AC_MSG_RESULT(yes) else AC_MSG_RESULT(no) - AC_MSG_CHECKING(whether /dev/arandom exists) - if test -r "/dev/arandom" && test -c "/dev/arandom"; then - AC_DEFINE([HAVE_DEV_ARANDOM], 1, [Define if the target system has /dev/arandom device]) - AC_MSG_RESULT(yes) - else - AC_MSG_RESULT(no) - fi +fi + +AC_MSG_CHECKING(whether /dev/arandom exists) +if test -r "/dev/arandom" && test -c "/dev/arandom"; then + AC_DEFINE([HAVE_DEV_ARANDOM], 1, [Define if the target system has /dev/arandom device]) + AC_MSG_RESULT(yes) +else + AC_MSG_RESULT(no) fi AC_ARG_ENABLE(gcc-global-regs, diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 36d530d47b74d..35c8f163b94d5 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1904,6 +1904,16 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_mt_getrandmax, 0) ZEND_END_ARG_INFO() /* }}} */ +/* {{{ random.c */ +ZEND_BEGIN_ARG_INFO_EX(arginfo_random_bytes, 0, 0, 0) + ZEND_ARG_INFO(0, length) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_random_int, 0, 0, 0) + ZEND_ARG_INFO(0, min) + ZEND_ARG_INFO(0, max) +ZEND_END_ARG_INFO() +/* }}} */ /* {{{ sha1.c */ ZEND_BEGIN_ARG_INFO_EX(arginfo_sha1, 0, 0, 1) ZEND_ARG_INFO(0, str) @@ -2827,6 +2837,9 @@ const zend_function_entry basic_functions[] = { /* {{{ */ PHP_FE(mt_srand, arginfo_mt_srand) PHP_FE(mt_getrandmax, arginfo_mt_getrandmax) + PHP_FE(random_bytes, arginfo_random_bytes) + PHP_FE(random_int, arginfo_random_int) + #if HAVE_GETSERVBYNAME PHP_FE(getservbyname, arginfo_getservbyname) #endif @@ -3657,6 +3670,8 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */ # endif #endif + BASIC_MINIT_SUBMODULE(random) + return SUCCESS; } /* }}} */ @@ -3695,6 +3710,8 @@ PHP_MSHUTDOWN_FUNCTION(basic) /* {{{ */ BASIC_MSHUTDOWN_SUBMODULE(crypt) #endif + BASIC_MSHUTDOWN_SUBMODULE(random) + zend_hash_destroy(&basic_submodules); return SUCCESS; } diff --git a/ext/standard/config.m4 b/ext/standard/config.m4 index 3263389d00d52..c75b141c80dcc 100644 --- a/ext/standard/config.m4 +++ b/ext/standard/config.m4 @@ -592,6 +592,11 @@ dnl Check for atomic operation API availability in Solaris dnl AC_CHECK_HEADERS([atomic.h]) +dnl +dnl Check for arc4random on BSD systems +dnl +AC_CHECK_DECLS([arc4random_buf]) + dnl dnl Setup extension sources dnl @@ -605,7 +610,8 @@ PHP_NEW_EXTENSION(standard, array.c base64.c basic_functions.c browscap.c crc32. incomplete_class.c url_scanner_ex.c ftp_fopen_wrapper.c \ http_fopen_wrapper.c php_fopen_wrapper.c credits.c css.c \ var_unserializer.c ftok.c sha1.c user_filters.c uuencode.c \ - filters.c proc_open.c streamsfuncs.c http.c password.c,,, + filters.c proc_open.c streamsfuncs.c http.c password.c \ + random.c,,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) PHP_ADD_MAKEFILE_FRAGMENT diff --git a/ext/standard/config.w32 b/ext/standard/config.w32 index e8b50efc7e9e9..adff3d8c878af 100644 --- a/ext/standard/config.w32 +++ b/ext/standard/config.w32 @@ -20,7 +20,7 @@ EXTENSION("standard", "array.c base64.c basic_functions.c browscap.c \ url_scanner_ex.c ftp_fopen_wrapper.c http_fopen_wrapper.c \ php_fopen_wrapper.c credits.c css.c var_unserializer.c ftok.c sha1.c \ user_filters.c uuencode.c filters.c proc_open.c password.c \ - streamsfuncs.c http.c flock_compat.c", false /* never shared */, + streamsfuncs.c http.c flock_compat.c random.c", false /* never shared */, '/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1'); PHP_INSTALL_HEADERS("", "ext/standard"); if (PHP_MBREGEX != "no") { diff --git a/ext/standard/php_random.h b/ext/standard/php_random.h new file mode 100644 index 0000000000000..ecf9c7135ba60 --- /dev/null +++ b/ext/standard/php_random.h @@ -0,0 +1,49 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 7 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2015 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Sammy Kaye Powers | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#ifndef PHP_RANDOM_H +#define PHP_RANDOM_H + +PHP_FUNCTION(random_bytes); +PHP_FUNCTION(random_int); + +PHP_MINIT_FUNCTION(random); +PHP_MSHUTDOWN_FUNCTION(random); + +typedef struct { + int fd; +} php_random_globals; + +#ifdef ZTS +# define RANDOM_G(v) ZEND_TSRMG(random_globals_id, php_random_globals *, v) +extern PHPAPI int random_globals_id; +#else +# define RANDOM_G(v) random_globals.v +extern PHPAPI php_random_globals random_globals; +#endif + +#endif + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + */ diff --git a/ext/standard/php_standard.h b/ext/standard/php_standard.h index 545406bef7223..418350738a6f9 100644 --- a/ext/standard/php_standard.h +++ b/ext/standard/php_standard.h @@ -59,6 +59,7 @@ #include "php_ftok.h" #include "php_type.h" #include "php_password.h" +#include "php_random.h" #include "php_version.h" #define PHP_STANDARD_VERSION PHP_VERSION diff --git a/ext/standard/random.c b/ext/standard/random.c new file mode 100644 index 0000000000000..ab6e2e5e9acda --- /dev/null +++ b/ext/standard/random.c @@ -0,0 +1,211 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 7 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2015 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Sammy Kaye Powers | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#include +#include +#include +#include + +#include "php.h" +#include "php_random.h" + +#if PHP_WIN32 +# include "win32/winutil.h" +#endif + +#ifdef ZTS +int random_globals_id; +#else +php_random_globals random_globals; +#endif + +static void random_globals_ctor(php_random_globals *random_globals_p) +{ + random_globals_p->fd = -1; +} + +static void random_globals_dtor(php_random_globals *random_globals_p) +{ + if (random_globals_p->fd > 0) { + close(random_globals_p->fd); + random_globals_p->fd = -1; + } +} + +/* {{{ */ +PHP_MINIT_FUNCTION(random) +{ +#ifdef ZTS + ts_allocate_id(&random_globals_id, sizeof(php_random_globals), (ts_allocate_ctor)random_globals_ctor, (ts_allocate_dtor)random_globals_dtor); +#else + random_globals_ctor(&random_globals); +#endif + + return SUCCESS; +} +/* }}} */ + +/* {{{ */ +PHP_MSHUTDOWN_FUNCTION(random) +{ +#ifndef ZTS + random_globals_dtor(&random_globals); +#endif +} +/* }}} */ + +/* {{{ */ +static int php_random_bytes(void *bytes, size_t size) +{ +#if PHP_WIN32 + /* Defer to CryptGenRandom on Windows */ + if (php_win32_get_random_bytes(bytes, size) == FAILURE) { + php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data"); + return FAILURE; + } +#else +#if HAVE_DECL_ARC4RANDOM_BUF + arc4random_buf(bytes, size); +#else + int fd = RANDOM_G(fd); + size_t read_bytes = 0; + + if (fd < 0) { +#if HAVE_DEV_ARANDOM + fd = open("/dev/arandom", O_RDONLY); +#else +#if HAVE_DEV_URANDOM + fd = open("/dev/urandom", O_RDONLY); +#endif // URANDOM +#endif // ARANDOM + if (fd < 0) { + php_error_docref(NULL, E_WARNING, "Cannot open source device"); + return FAILURE; + } + + RANDOM_G(fd) = fd; + } + + while (read_bytes < size) { + ssize_t n = read(fd, bytes + read_bytes, size - read_bytes); + if (n < 0) { + break; + } + read_bytes += n; + } + + if (read_bytes < size) { + php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data"); + return FAILURE; + } +#endif // !ARC4RANDOM_BUF +#endif // !WIN32 + + return SUCCESS; +} +/* }}} */ + +/* {{{ proto string random_bytes(int length) +Return an arbitrary length of pseudo-random bytes as binary string */ +PHP_FUNCTION(random_bytes) +{ + zend_long size; + zend_string *bytes; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) { + RETURN_FALSE; + } + + if (size < 1) { + php_error_docref(NULL, E_WARNING, "Length must be greater than 0"); + RETURN_FALSE; + } + + bytes = zend_string_alloc(size, 0); + + if (php_random_bytes(bytes->val, size) == FAILURE) { + zend_string_release(bytes); + RETURN_FALSE; + } + + bytes->val[size] = '\0'; + + RETURN_STR(bytes); +} +/* }}} */ + +/* {{{ proto int random_int(int min, int max) +Return an arbitrary pseudo-random integer */ +PHP_FUNCTION(random_int) +{ + zend_long min; + zend_long max; + zend_ulong limit; + zend_ulong umax; + zend_ulong result; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &min, &max) == FAILURE) { + RETURN_FALSE; + } + + if (min >= max) { + php_error_docref(NULL, E_WARNING, "Minimum value must be less than the maximum value"); + RETURN_FALSE; + } + + umax = max - min; + + if (php_random_bytes(&result, sizeof(result)) == FAILURE) { + RETURN_FALSE; + } + + // Special case where no modulus is required + if (umax == ZEND_ULONG_MAX) { + RETURN_LONG((zend_long)result); + } + + // Increment the max so the range is inclusive of max + umax++; + + // Powers of two are not biased + if ((umax & ~umax) != umax) { + // Ceiling under which ZEND_LONG_MAX % max == 0 + limit = ZEND_ULONG_MAX - (ZEND_ULONG_MAX % umax) - 1; + + // Discard numbers over the limit to avoid modulo bias + while (result > limit) { + if (php_random_bytes(&result, sizeof(result)) == FAILURE) { + return; + } + } + } + + RETURN_LONG((zend_long)((result % umax) + min)); +} +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/ext/standard/tests/random/random_bytes.phpt b/ext/standard/tests/random/random_bytes.phpt new file mode 100644 index 0000000000000..86391383e443f --- /dev/null +++ b/ext/standard/tests/random/random_bytes.phpt @@ -0,0 +1,14 @@ +--TEST-- +Test normal operation of random_bytes() +--FILE-- + +--EXPECT-- +int(32) +bool(true) diff --git a/ext/standard/tests/random/random_bytes_error.phpt b/ext/standard/tests/random/random_bytes_error.phpt new file mode 100644 index 0000000000000..b5a0c473c2044 --- /dev/null +++ b/ext/standard/tests/random/random_bytes_error.phpt @@ -0,0 +1,17 @@ +--TEST-- +Test error operation of random_bytes() +--FILE-- + +--EXPECTF-- +Warning: random_bytes() expects exactly 1 parameter, 0 given in %s on line %d +bool(false) + +Warning: random_bytes(): Length must be greater than 0 in %s on line %d +bool(false) diff --git a/ext/standard/tests/random/random_int.phpt b/ext/standard/tests/random/random_int.phpt new file mode 100644 index 0000000000000..0c3081452c5d8 --- /dev/null +++ b/ext/standard/tests/random/random_int.phpt @@ -0,0 +1,18 @@ +--TEST-- +Test normal operation of random_int() +--FILE-- += 10 && $x <= 100); + +var_dump(random_int(-1000, -1) < 0); + +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) diff --git a/ext/standard/tests/random/random_int_error.phpt b/ext/standard/tests/random/random_int_error.phpt new file mode 100644 index 0000000000000..27ae411063d90 --- /dev/null +++ b/ext/standard/tests/random/random_int_error.phpt @@ -0,0 +1,22 @@ +--TEST-- +Test error operation of random_int() +--FILE-- + +--EXPECTF-- +Warning: random_int() expects exactly 2 parameters, 0 given in %s on line %d +bool(false) + +Warning: random_int() expects exactly 2 parameters, 1 given in %s on line %d +bool(false) + +Warning: random_int(): Minimum value must be less than the maximum value in %s on line %d +bool(false)