From f8d7aec431397e008fb63302e361444b0e0ea737 Mon Sep 17 00:00:00 2001 From: SammyK Date: Thu, 12 Feb 2015 20:53:55 -0600 Subject: [PATCH 01/20] Got initial API added --- ext/standard/basic_functions.c | 18 ++++ ext/standard/config.m4 | 3 +- ext/standard/config.w32 | 2 +- ext/standard/php_random.h | 34 +++++++ ext/standard/php_standard.h | 1 + ext/standard/random.c | 158 +++++++++++++++++++++++++++++++++ 6 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 ext/standard/php_random.h create mode 100644 ext/standard/random.c diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 940c9e4141750..c68651925d141 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1897,6 +1897,20 @@ 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, bytes) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_random_hex, 0, 0, 0) + ZEND_ARG_INFO(0, bytes) +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) @@ -2820,6 +2834,10 @@ 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_hex, arginfo_random_hex) + PHP_FE(random_int, arginfo_random_int) + #if HAVE_GETSERVBYNAME PHP_FE(getservbyname, arginfo_getservbyname) #endif diff --git a/ext/standard/config.m4 b/ext/standard/config.m4 index c502c5426892a..be23d6df8021b 100644 --- a/ext/standard/config.m4 +++ b/ext/standard/config.m4 @@ -605,7 +605,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..56709e53177f2 --- /dev/null +++ b/ext/standard/php_random.h @@ -0,0 +1,34 @@ +/* + +----------------------------------------------------------------------+ + | 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_hex); +PHP_FUNCTION(random_int); +#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 9038c9de45f13..5a0b6e7525b6f 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" #define phpext_standard_ptr basic_functions_module_ptr PHP_MINIT_FUNCTION(standard_filters); diff --git a/ext/standard/random.c b/ext/standard/random.c new file mode 100644 index 0000000000000..dd4ccd8ffb776 --- /dev/null +++ b/ext/standard/random.c @@ -0,0 +1,158 @@ +/* + +----------------------------------------------------------------------+ + | 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 "php.h" + +// Copy/pasted from string.c +static char hexconvtab[] = "0123456789abcdef"; + +// Copy/pasted from string.c +static zend_string *php_bin_to_hex(const unsigned char *old, const size_t oldlen) +{ + zend_string *result; + size_t i, j; + + result = zend_string_safe_alloc(oldlen, 2 * sizeof(char), 0, 0); + + for (i = j = 0; i < oldlen; i++) { + result->val[j++] = hexconvtab[old[i] >> 4]; + result->val[j++] = hexconvtab[old[i] & 15]; + } + result->val[j] = '\0'; + + return result; +} + +static int *php_random_bytes(zend_string *bytes, size_t length) +{ + FILE *fp; + +#ifdef PHP_WIN32 + // @todo Need to add Windows support + fp = NULL; +#else + fp = fopen("/dev/urandom" , "rb"); +#endif + + if (fp == NULL) { + php_error_docref(NULL, E_WARNING, "Unable to open /dev/urandom"); + return FAILURE; + } + + // @todo I think this has to be char & int! + if (fgets(bytes, 10, fp) == NULL) { + fclose(fp); + php_error_docref(NULL, E_WARNING, "Unable to read from /dev/urandom"); + return FAILURE; + } + fclose(fp); + + return bytes; +} + +/* {{{ proto string random_bytes(int bytes) +Return an arbitrary length of pseudo-random bytes as binary string */ +PHP_FUNCTION(random_bytes) +{ + size_t length; + zend_string *bytes; + + int argc = ZEND_NUM_ARGS(); + + if (argc != 0) { + if (zend_parse_parameters(argc, "l", &length) == FAILURE) { + return; + } else if (length <= 0) { + php_error_docref(NULL, E_WARNING, "length(" ZEND_LONG_FMT ") must be greater than 0", length); + RETURN_FALSE; + } + } + + if (php_random_bytes(bytes, length) == FAILURE) { + return; + } + + RETURN_STR(bytes); +} +/* }}} */ + +/* {{{ proto string random_hex(int bytes) +Return an arbitrary length of pseudo-random bytes as hexadecimal string */ +PHP_FUNCTION(random_hex) +{ + size_t length; + zend_string *bytes; + zend_string *hex; + + int argc = ZEND_NUM_ARGS(); + + if (argc != 0) { + if (zend_parse_parameters(argc, "l", &length) == FAILURE) { + return; + } else if (length <= 0) { + php_error_docref(NULL, E_WARNING, "length(" ZEND_LONG_FMT ") must be greater than 0", length); + RETURN_FALSE; + } + } + + if (php_random_bytes(bytes, length) == FAILURE) { + return; + } + + hex = php_bin_to_hex(bytes, length); + + RETURN_STR(hex); +} +/* }}} */ + +/* {{{ 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_long number; + int argc = ZEND_NUM_ARGS(); + + if (argc != 0) { + if (zend_parse_parameters(argc, "ll", &min, &max) == FAILURE) { + return; + } else if (max < min) { + php_error_docref(NULL, E_WARNING, "max(" ZEND_LONG_FMT ") is smaller than min(" ZEND_LONG_FMT ")", max, min); + RETURN_FALSE; + } + } + + number = min + max; + + RETURN_LONG(number); +} +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ From 26e4ed2a215afe8f5d218c6886351e7bb17785d5 Mon Sep 17 00:00:00 2001 From: SammyK Date: Fri, 13 Feb 2015 03:58:47 -0600 Subject: [PATCH 02/20] Got mostly working. I think Hex is broken. Need to add int. --- ext/standard/random.c | 159 +++++++++++++++++++++++++----------------- 1 file changed, 95 insertions(+), 64 deletions(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index dd4ccd8ffb776..8dd9688616363 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -19,79 +19,98 @@ /* $Id$ */ #include +#include +#include #include "php.h" +#if PHP_WIN32 +# include "win32/winutil.h" +#endif + // Copy/pasted from string.c static char hexconvtab[] = "0123456789abcdef"; // Copy/pasted from string.c -static zend_string *php_bin_to_hex(const unsigned char *old, const size_t oldlen) +static void php_bin_to_hex(char *old, const zend_long old_len, char *hex) { - zend_string *result; - size_t i, j; + zend_long i, j; - result = zend_string_safe_alloc(oldlen, 2 * sizeof(char), 0, 0); - - for (i = j = 0; i < oldlen; i++) { - result->val[j++] = hexconvtab[old[i] >> 4]; - result->val[j++] = hexconvtab[old[i] & 15]; + // @todo I don't think this is doing it right + for (i = j = 0; i < old_len; i++) { + hex[j++] = hexconvtab[old[i] >> 4]; + hex[j++] = hexconvtab[old[i] & 15]; } - result->val[j] = '\0'; - return result; + hex[j] = '\0'; } -static int *php_random_bytes(zend_string *bytes, size_t length) +// Copy/pasted from mcrypt.c +static int php_random_bytes(char *bytes, zend_long size) { - FILE *fp; + int n = 0; -#ifdef PHP_WIN32 - // @todo Need to add Windows support - fp = NULL; +#if PHP_WIN32 + /* random/urandom equivalent on Windows */ + BYTE *win_bytes = (BYTE *) bytes; + if (php_win32_get_random_bytes(win_bytes, (size_t) size) == FAILURE){ + php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data"); + return FAILURE; + } + n = (int)size; #else - fp = fopen("/dev/urandom" , "rb"); -#endif + int fd; + size_t read_bytes = 0; - if (fp == NULL) { - php_error_docref(NULL, E_WARNING, "Unable to open /dev/urandom"); + fd = open("/dev/urandom", O_RDONLY); + if (fd < 0) { + php_error_docref(NULL, E_WARNING, "Cannot open source device"); return FAILURE; } - - // @todo I think this has to be char & int! - if (fgets(bytes, 10, fp) == NULL) { - fclose(fp); - php_error_docref(NULL, E_WARNING, "Unable to read from /dev/urandom"); + while (read_bytes < size) { + n = read(fd, bytes + read_bytes, size - read_bytes); + if (n < 0) { + break; + } + read_bytes += n; + } + n = read_bytes; + close(fd); + if (n < size) { + php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data"); return FAILURE; } - fclose(fp); +#endif - return bytes; + return SUCCESS; } /* {{{ proto string random_bytes(int bytes) Return an arbitrary length of pseudo-random bytes as binary string */ PHP_FUNCTION(random_bytes) { - size_t length; - zend_string *bytes; + zend_long size; + char *bytes; - int argc = ZEND_NUM_ARGS(); + if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) { + return; + } - if (argc != 0) { - if (zend_parse_parameters(argc, "l", &length) == FAILURE) { - return; - } else if (length <= 0) { - php_error_docref(NULL, E_WARNING, "length(" ZEND_LONG_FMT ") must be greater than 0", length); - RETURN_FALSE; - } + if (size <= 0 || size >= INT_MAX) { + php_error_docref(NULL, E_WARNING, "Cannot genrate a random string with a size of less than 1 or greater than %d", INT_MAX); + RETURN_FALSE; } - if (php_random_bytes(bytes, length) == FAILURE) { + bytes = ecalloc(size + 1, 1); + + if (php_random_bytes(bytes, size) == FAILURE) { + efree(bytes); return; } - RETURN_STR(bytes); + RETVAL_STRINGL(bytes, size); + + efree(bytes); } /* }}} */ @@ -99,28 +118,35 @@ PHP_FUNCTION(random_bytes) Return an arbitrary length of pseudo-random bytes as hexadecimal string */ PHP_FUNCTION(random_hex) { - size_t length; - zend_string *bytes; - zend_string *hex; - - int argc = ZEND_NUM_ARGS(); - - if (argc != 0) { - if (zend_parse_parameters(argc, "l", &length) == FAILURE) { - return; - } else if (length <= 0) { - php_error_docref(NULL, E_WARNING, "length(" ZEND_LONG_FMT ") must be greater than 0", length); - RETURN_FALSE; - } + zend_long size; + char *bytes; + char *hex; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) { + return; } - if (php_random_bytes(bytes, length) == FAILURE) { + if (size <= 0 || size >= INT_MAX) { + php_error_docref(NULL, E_WARNING, "Cannot genrate a random string with a size of less than 1 or greater than %d", INT_MAX); + RETURN_FALSE; + } + + // @todo should we half the size for hex? How for odd num of chars? + bytes = ecalloc(size + 1, 1); + + if (php_random_bytes(bytes, size) == FAILURE) { + efree(bytes); return; } - hex = php_bin_to_hex(bytes, length); + int hex_size = size * 2; + hex = ecalloc(hex_size + 1, 1); + php_bin_to_hex(bytes, hex_size, hex); + + RETVAL_STRINGL(hex, hex_size); - RETURN_STR(hex); + efree(bytes); + efree(hex); } /* }}} */ @@ -131,18 +157,23 @@ PHP_FUNCTION(random_int) zend_long min; zend_long max; zend_long number; - int argc = ZEND_NUM_ARGS(); - - if (argc != 0) { - if (zend_parse_parameters(argc, "ll", &min, &max) == FAILURE) { - return; - } else if (max < min) { - php_error_docref(NULL, E_WARNING, "max(" ZEND_LONG_FMT ") is smaller than min(" ZEND_LONG_FMT ")", max, min); - RETURN_FALSE; - } + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &min, &max) == FAILURE) { + return; + } + + if (min >= INT_MAX || max >= INT_MAX) { + php_error_docref(NULL, E_WARNING, "Cannot use range greater than %d", INT_MAX); + RETURN_FALSE; + } + + if (max < min) { + php_error_docref(NULL, E_WARNING, "Max value (%d) is less than min value (%d)", max, min); + RETURN_FALSE; } - number = min + max; + // @todo Insert bin-to-int stuff here + number = min + max * 100; RETURN_LONG(number); } From 3c5fcac453a0def6c754ab2a7f83b977e90a9b84 Mon Sep 17 00:00:00 2001 From: SammyK Date: Thu, 19 Feb 2015 20:40:52 -0600 Subject: [PATCH 03/20] Updated to use zend_string but getting memory leaks. Doh! --- ext/standard/random.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index 8dd9688616363..74b662b82224b 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -46,7 +46,7 @@ static void php_bin_to_hex(char *old, const zend_long old_len, char *hex) } // Copy/pasted from mcrypt.c -static int php_random_bytes(char *bytes, zend_long size) +static int php_random_bytes(zend_string *bytes, zend_long size) { int n = 0; @@ -90,7 +90,7 @@ Return an arbitrary length of pseudo-random bytes as binary string */ PHP_FUNCTION(random_bytes) { zend_long size; - char *bytes; + zend_string *bytes; if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) { return; @@ -101,16 +101,16 @@ PHP_FUNCTION(random_bytes) RETURN_FALSE; } - bytes = ecalloc(size + 1, 1); + bytes = zend_string_alloc(size + 1, 0); if (php_random_bytes(bytes, size) == FAILURE) { - efree(bytes); + zend_string_release(bytes); return; } RETVAL_STRINGL(bytes, size); - efree(bytes); + zend_string_release(bytes); } /* }}} */ @@ -119,8 +119,8 @@ Return an arbitrary length of pseudo-random bytes as hexadecimal string */ PHP_FUNCTION(random_hex) { zend_long size; - char *bytes; - char *hex; + zend_string *bytes; + zend_string *hex; if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) { return; @@ -132,21 +132,21 @@ PHP_FUNCTION(random_hex) } // @todo should we half the size for hex? How for odd num of chars? - bytes = ecalloc(size + 1, 1); + bytes = zend_string_alloc(size + 1, 0); if (php_random_bytes(bytes, size) == FAILURE) { - efree(bytes); + zend_string_release(bytes); return; } int hex_size = size * 2; - hex = ecalloc(hex_size + 1, 1); + hex = zend_string_alloc(hex_size + 1, 0); php_bin_to_hex(bytes, hex_size, hex); RETVAL_STRINGL(hex, hex_size); - efree(bytes); - efree(hex); + zend_string_release(bytes); + zend_string_release(hex); } /* }}} */ From e96b077fe58004b0451dab7083b5674e9d8a3279 Mon Sep 17 00:00:00 2001 From: SammyK Date: Fri, 20 Feb 2015 11:25:04 -0600 Subject: [PATCH 04/20] Got random_bytes() working again. First attempt at random_int(). Commented-out random_hex() until the other functions are rocking. --- ext/standard/random.c | 88 ++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 31 deletions(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index 74b662b82224b..a202b889edaac 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -28,32 +28,35 @@ # include "win32/winutil.h" #endif +/* // Copy/pasted from string.c static char hexconvtab[] = "0123456789abcdef"; // Copy/pasted from string.c -static void php_bin_to_hex(char *old, const zend_long old_len, char *hex) +static void php_bin_to_hex(zend_string *old, const zend_long old_len, zend_string *hex) { zend_long i, j; - // @todo I don't think this is doing it right + hex = zend_string_alloc(old_len * 2, 0); // @todo is this right? + for (i = j = 0; i < old_len; i++) { - hex[j++] = hexconvtab[old[i] >> 4]; - hex[j++] = hexconvtab[old[i] & 15]; + hex->val[j++] = hexconvtab[old->val[i] >> 4]; + hex->val[j++] = hexconvtab[old->val[i] & 15]; } - hex[j] = '\0'; + hex->val[j] = '\0'; } +*/ // Copy/pasted from mcrypt.c -static int php_random_bytes(zend_string *bytes, zend_long size) +static int php_random_bytes(char *bytes, zend_long size) { int n = 0; #if PHP_WIN32 /* random/urandom equivalent on Windows */ BYTE *win_bytes = (BYTE *) bytes; - if (php_win32_get_random_bytes(win_bytes, (size_t) size) == FAILURE){ + if (php_win32_get_random_bytes(win_bytes, (size_t) size) == FAILURE) { php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data"); return FAILURE; } @@ -75,6 +78,7 @@ static int php_random_bytes(zend_string *bytes, zend_long size) read_bytes += n; } n = read_bytes; + close(fd); if (n < size) { php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data"); @@ -82,6 +86,9 @@ static int php_random_bytes(zend_string *bytes, zend_long size) } #endif + // @todo - Do we need to do this? + bytes[size] = '\0'; + return SUCCESS; } @@ -101,16 +108,14 @@ PHP_FUNCTION(random_bytes) RETURN_FALSE; } - bytes = zend_string_alloc(size + 1, 0); + bytes = zend_string_alloc(size, 0); - if (php_random_bytes(bytes, size) == FAILURE) { + if (php_random_bytes(bytes->val, size) == FAILURE) { zend_string_release(bytes); return; } - RETVAL_STRINGL(bytes, size); - - zend_string_release(bytes); + RETURN_STR(bytes); } /* }}} */ @@ -118,6 +123,7 @@ PHP_FUNCTION(random_bytes) Return an arbitrary length of pseudo-random bytes as hexadecimal string */ PHP_FUNCTION(random_hex) { + /* zend_long size; zend_string *bytes; zend_string *hex; @@ -131,49 +137,69 @@ PHP_FUNCTION(random_hex) RETURN_FALSE; } - // @todo should we half the size for hex? How for odd num of chars? - bytes = zend_string_alloc(size + 1, 0); - if (php_random_bytes(bytes, size) == FAILURE) { - zend_string_release(bytes); return; } int hex_size = size * 2; - hex = zend_string_alloc(hex_size + 1, 0); - php_bin_to_hex(bytes, hex_size, hex); - RETVAL_STRINGL(hex, hex_size); + php_bin_to_hex(bytes, hex_size, hex); zend_string_release(bytes); - zend_string_release(hex); + */ + + RETURN_STR("Foo!"); } /* }}} */ -/* {{{ proto int random_int(int min, int max) +/* {{{ proto int random_int(int max) Return an arbitrary pseudo-random integer */ PHP_FUNCTION(random_int) { - zend_long min; zend_long max; - zend_long number; + zend_long size; + zend_long number = 0; + zend_string *bytes; + size_t i; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &min, &max) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &max) == FAILURE) { return; } - if (min >= INT_MAX || max >= INT_MAX) { - php_error_docref(NULL, E_WARNING, "Cannot use range greater than %d", INT_MAX); + if (max >= INT_MAX) { + php_error_docref(NULL, E_WARNING, "Cannot use max greater than %d", INT_MAX); RETURN_FALSE; } - if (max < min) { - php_error_docref(NULL, E_WARNING, "Max value (%d) is less than min value (%d)", max, min); - RETURN_FALSE; + size = sizeof(number); + + bytes = zend_string_alloc(size, 0); + + if (php_random_bytes(bytes->val, size) == FAILURE) { + zend_string_release(bytes); + return; + } + + // @todo bin-to-int: I know this is wrong but don't know how to fix + for (i = 0; i < size; i++) { + unsigned char c = bytes->val[i++]; + unsigned char d; + + if (c >= '0' && c <= '9') { + d = c - '0'; + } else if (c >= 'a' && c <= 'f') { + d = c - 'a' - 10; + } else if (c >= 'A' && c <= 'F') { + d = c - 'A' - 10; + } else { + continue; + } + + // Binary = base-2 + number = number * 2 + d; } - // @todo Insert bin-to-int stuff here - number = min + max * 100; + zend_string_release(bytes); RETURN_LONG(number); } From aa0ca696a068e5650e2ddf8a0c489960406d76f7 Mon Sep 17 00:00:00 2001 From: SammyK Date: Fri, 20 Feb 2015 11:30:23 -0600 Subject: [PATCH 05/20] Fix random_int() checking size of wrong var. --- ext/standard/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index a202b889edaac..2ae74cd8d0cb4 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -171,7 +171,7 @@ PHP_FUNCTION(random_int) RETURN_FALSE; } - size = sizeof(number); + size = sizeof(max); bytes = zend_string_alloc(size, 0); From b32e0d07cf863e1718109ed0968d5d2c6f82d790 Mon Sep 17 00:00:00 2001 From: SammyK Date: Fri, 20 Feb 2015 12:29:49 -0600 Subject: [PATCH 06/20] Got random_int() seemingly working thanks to @ircmaxell --- ext/standard/random.c | 60 +++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index 2ae74cd8d0cb4..01197b6951faa 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "php.h" @@ -28,6 +29,12 @@ # include "win32/winutil.h" #endif +// Big thanks to @ircmaxell for the help on this bit +union rand_long_buffer { + char buffer[8]; + long number; +}; + /* // Copy/pasted from string.c static char hexconvtab[] = "0123456789abcdef"; @@ -62,6 +69,7 @@ static int php_random_bytes(char *bytes, zend_long size) } n = (int)size; #else + // @todo Need to cache the fd for random_int() call within loop int fd; size_t read_bytes = 0; @@ -152,56 +160,40 @@ PHP_FUNCTION(random_hex) } /* }}} */ -/* {{{ proto int random_int(int max) +/* {{{ proto int random_int(int maximum) Return an arbitrary pseudo-random integer */ PHP_FUNCTION(random_int) { - zend_long max; + zend_long maximum; zend_long size; - zend_long number = 0; - zend_string *bytes; size_t i; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &max) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &maximum) == FAILURE) { return; } - if (max >= INT_MAX) { - php_error_docref(NULL, E_WARNING, "Cannot use max greater than %d", INT_MAX); + if (maximum <= 0 || maximum >= INT_MAX) { + php_error_docref(NULL, E_WARNING, "Cannot use maximum less than 1 or greater than %d", INT_MAX); RETURN_FALSE; } - size = sizeof(max); - - bytes = zend_string_alloc(size, 0); + long range = (long) maximum; // @todo Support min? - if (php_random_bytes(bytes->val, size) == FAILURE) { - zend_string_release(bytes); - return; - } + // Big thanks to @ircmaxell for the help on this bit + union rand_long_buffer value; + long result; + int bits = (int) (log((double) range) / log(2.0)) + 1; + int bytes = MAX(ceil(bits / 8), 1); + long mask = (long) pow(2.0, (double) bits) - 1; - // @todo bin-to-int: I know this is wrong but don't know how to fix - for (i = 0; i < size; i++) { - unsigned char c = bytes->val[i++]; - unsigned char d; - - if (c >= '0' && c <= '9') { - d = c - '0'; - } else if (c >= 'a' && c <= 'f') { - d = c - 'a' - 10; - } else if (c >= 'A' && c <= 'F') { - d = c - 'A' - 10; - } else { - continue; + do { + if (php_random_bytes(&value.buffer, 8) == FAILURE) { + return; } + result = value.number & mask; + } while (result > maximum); - // Binary = base-2 - number = number * 2 + d; - } - - zend_string_release(bytes); - - RETURN_LONG(number); + RETURN_LONG(result); } /* }}} */ From 2c659ed784228f9d92c9358c862eb4602a1f1269 Mon Sep 17 00:00:00 2001 From: SammyK Date: Fri, 20 Feb 2015 12:44:27 -0600 Subject: [PATCH 07/20] Make maximum argument to random_int() optional with default to INT_MAX. --- ext/standard/random.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index 01197b6951faa..24d1830233bc5 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -168,11 +168,15 @@ PHP_FUNCTION(random_int) zend_long size; size_t i; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &maximum) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &maximum) == FAILURE) { return; } - if (maximum <= 0 || maximum >= INT_MAX) { + if (ZEND_NUM_ARGS() == 0) { + maximum = INT_MAX; + } + + if (maximum <= 0 || maximum > INT_MAX) { php_error_docref(NULL, E_WARNING, "Cannot use maximum less than 1 or greater than %d", INT_MAX); RETURN_FALSE; } From a1e62293b12f4440da55846fd50cae37b4c53265 Mon Sep 17 00:00:00 2001 From: SammyK Date: Fri, 20 Feb 2015 15:10:42 -0600 Subject: [PATCH 08/20] Remove random_hex(). *sadface* --- ext/standard/basic_functions.c | 6 ---- ext/standard/php_random.h | 1 - ext/standard/random.c | 53 ---------------------------------- 3 files changed, 60 deletions(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index c68651925d141..110bbbe2abf7f 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1902,12 +1902,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_random_bytes, 0, 0, 0) ZEND_ARG_INFO(0, bytes) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_INFO_EX(arginfo_random_hex, 0, 0, 0) - ZEND_ARG_INFO(0, bytes) -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() /* }}} */ @@ -2835,7 +2830,6 @@ const zend_function_entry basic_functions[] = { /* {{{ */ PHP_FE(mt_getrandmax, arginfo_mt_getrandmax) PHP_FE(random_bytes, arginfo_random_bytes) - PHP_FE(random_hex, arginfo_random_hex) PHP_FE(random_int, arginfo_random_int) #if HAVE_GETSERVBYNAME diff --git a/ext/standard/php_random.h b/ext/standard/php_random.h index 56709e53177f2..1be5894a636be 100644 --- a/ext/standard/php_random.h +++ b/ext/standard/php_random.h @@ -22,7 +22,6 @@ #define PHP_RANDOM_H PHP_FUNCTION(random_bytes); -PHP_FUNCTION(random_hex); PHP_FUNCTION(random_int); #endif diff --git a/ext/standard/random.c b/ext/standard/random.c index 24d1830233bc5..72377fec880f0 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -35,26 +35,6 @@ union rand_long_buffer { long number; }; -/* -// Copy/pasted from string.c -static char hexconvtab[] = "0123456789abcdef"; - -// Copy/pasted from string.c -static void php_bin_to_hex(zend_string *old, const zend_long old_len, zend_string *hex) -{ - zend_long i, j; - - hex = zend_string_alloc(old_len * 2, 0); // @todo is this right? - - for (i = j = 0; i < old_len; i++) { - hex->val[j++] = hexconvtab[old->val[i] >> 4]; - hex->val[j++] = hexconvtab[old->val[i] & 15]; - } - - hex->val[j] = '\0'; -} -*/ - // Copy/pasted from mcrypt.c static int php_random_bytes(char *bytes, zend_long size) { @@ -127,39 +107,6 @@ PHP_FUNCTION(random_bytes) } /* }}} */ -/* {{{ proto string random_hex(int bytes) -Return an arbitrary length of pseudo-random bytes as hexadecimal string */ -PHP_FUNCTION(random_hex) -{ - /* - zend_long size; - zend_string *bytes; - zend_string *hex; - - if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) { - return; - } - - if (size <= 0 || size >= INT_MAX) { - php_error_docref(NULL, E_WARNING, "Cannot genrate a random string with a size of less than 1 or greater than %d", INT_MAX); - RETURN_FALSE; - } - - if (php_random_bytes(bytes, size) == FAILURE) { - return; - } - - int hex_size = size * 2; - - php_bin_to_hex(bytes, hex_size, hex); - - zend_string_release(bytes); - */ - - RETURN_STR("Foo!"); -} -/* }}} */ - /* {{{ proto int random_int(int maximum) Return an arbitrary pseudo-random integer */ PHP_FUNCTION(random_int) From bbc9198d014f9acf8374706df7021ad9f95ffecf Mon Sep 17 00:00:00 2001 From: Leigh Date: Sat, 21 Feb 2015 15:30:43 +0000 Subject: [PATCH 09/20] Detect presence of /dev/arandom --- Zend/Zend.m4 | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Zend/Zend.m4 b/Zend/Zend.m4 index 945409eb1e135..a8f66406eee19 100644 --- a/Zend/Zend.m4 +++ b/Zend/Zend.m4 @@ -425,11 +425,13 @@ 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 + From 7a99db67c22f0911618abf61e1b2c2cad13a41e6 Mon Sep 17 00:00:00 2001 From: Leigh Date: Sat, 21 Feb 2015 18:51:08 +0000 Subject: [PATCH 10/20] Tidy up `php_random_bytes` and add /dev/arandom --- ext/standard/random.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index 72377fec880f0..5c2a95c8dff6c 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -36,28 +36,31 @@ union rand_long_buffer { }; // Copy/pasted from mcrypt.c -static int php_random_bytes(char *bytes, zend_long size) +static int php_random_bytes(void *bytes, size_t size) { int n = 0; #if PHP_WIN32 - /* random/urandom equivalent on Windows */ - BYTE *win_bytes = (BYTE *) bytes; - if (php_win32_get_random_bytes(win_bytes, (size_t) size) == FAILURE) { + /* 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; } - n = (int)size; #else - // @todo Need to cache the fd for random_int() call within loop - int fd; + int fd = -1; size_t read_bytes = 0; - +#if HAVE_DEV_ARANDOM + fd = open("/dev/arandom", O_RDONLY); +#else +#if HAVE_DEV_URANDOM fd = open("/dev/urandom", O_RDONLY); +#endif +#endif if (fd < 0) { php_error_docref(NULL, E_WARNING, "Cannot open source device"); return FAILURE; } + while (read_bytes < size) { n = read(fd, bytes + read_bytes, size - read_bytes); if (n < 0) { @@ -65,18 +68,14 @@ static int php_random_bytes(char *bytes, zend_long size) } read_bytes += n; } - n = read_bytes; close(fd); - if (n < size) { + if (read_bytes < size) { php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data"); return FAILURE; } #endif - // @todo - Do we need to do this? - bytes[size] = '\0'; - return SUCCESS; } @@ -103,6 +102,8 @@ PHP_FUNCTION(random_bytes) return; } + bytes->val[size] = '\0'; + RETURN_STR(bytes); } /* }}} */ From 3d413ad04411864c1345ef8d2e440be85cac9e1f Mon Sep 17 00:00:00 2001 From: Leigh Date: Sun, 22 Feb 2015 22:45:13 +0000 Subject: [PATCH 11/20] Ensure random_int() uses a uniform distribution --- ext/standard/random.c | 57 ++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index 5c2a95c8dff6c..5f02be9f9be3b 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -29,13 +29,6 @@ # include "win32/winutil.h" #endif -// Big thanks to @ircmaxell for the help on this bit -union rand_long_buffer { - char buffer[8]; - long number; -}; - -// Copy/pasted from mcrypt.c static int php_random_bytes(void *bytes, size_t size) { int n = 0; @@ -79,7 +72,7 @@ static int php_random_bytes(void *bytes, size_t size) return SUCCESS; } -/* {{{ proto string random_bytes(int bytes) +/* {{{ proto string random_bytes(int length) Return an arbitrary length of pseudo-random bytes as binary string */ PHP_FUNCTION(random_bytes) { @@ -90,8 +83,8 @@ PHP_FUNCTION(random_bytes) return; } - if (size <= 0 || size >= INT_MAX) { - php_error_docref(NULL, E_WARNING, "Cannot genrate a random string with a size of less than 1 or greater than %d", INT_MAX); + if (size < 1) { + php_error_docref(NULL, E_WARNING, "Length must be greater than 0"); RETURN_FALSE; } @@ -108,44 +101,46 @@ PHP_FUNCTION(random_bytes) } /* }}} */ -/* {{{ proto int random_int(int maximum) +/* {{{ proto int random_int(int max) Return an arbitrary pseudo-random integer */ PHP_FUNCTION(random_int) { - zend_long maximum; - zend_long size; - size_t i; + zend_long max = ZEND_LONG_MAX; + zend_long limit; + zend_long result; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &maximum) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &max) == FAILURE) { return; } - if (ZEND_NUM_ARGS() == 0) { - maximum = INT_MAX; + if (max <= 0 || max > ZEND_LONG_MAX) { + php_error_docref(NULL, E_WARNING, "Cannot use maximum less than 1 or greater than %d", ZEND_LONG_MAX); + RETURN_FALSE; } - if (maximum <= 0 || maximum > INT_MAX) { - php_error_docref(NULL, E_WARNING, "Cannot use maximum less than 1 or greater than %d", INT_MAX); - RETURN_FALSE; + // Special case so we can return a range inclusive of the upper bound + if (max == ZEND_LONG_MAX) { + if (php_random_bytes(&result, sizeof(result)) == FAILURE) { + return; + } + RETURN_LONG(result & ZEND_LONG_MAX); } - long range = (long) maximum; // @todo Support min? + // Increment the max so the range is inclusive of max + max++; - // Big thanks to @ircmaxell for the help on this bit - union rand_long_buffer value; - long result; - int bits = (int) (log((double) range) / log(2.0)) + 1; - int bytes = MAX(ceil(bits / 8), 1); - long mask = (long) pow(2.0, (double) bits) - 1; + // Ceiling under which ZEND_LONG_MAX % max == 0 + limit = ZEND_LONG_MAX - (ZEND_LONG_MAX % max) - 1; + // Discard numbers over the limit to avoid modulo bias do { - if (php_random_bytes(&value.buffer, 8) == FAILURE) { + if (php_random_bytes(&result, sizeof(result)) == FAILURE) { return; } - result = value.number & mask; - } while (result > maximum); + result &= ZEND_LONG_MAX; + } while (result > limit); - RETURN_LONG(result); + RETURN_LONG(result % max); } /* }}} */ From 513d5c9c6fc409395c93c84bd9f847ee0845b817 Mon Sep 17 00:00:00 2001 From: Leigh Date: Tue, 24 Feb 2015 01:23:55 +0000 Subject: [PATCH 12/20] Allow full integer range from random_int() --- ext/standard/basic_functions.c | 3 +- ext/standard/random.c | 53 +++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 110bbbe2abf7f..d66e743c90d47 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1899,10 +1899,11 @@ ZEND_END_ARG_INFO() /* }}} */ /* {{{ random.c */ ZEND_BEGIN_ARG_INFO_EX(arginfo_random_bytes, 0, 0, 0) - ZEND_ARG_INFO(0, bytes) + 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() /* }}} */ diff --git a/ext/standard/random.c b/ext/standard/random.c index 5f02be9f9be3b..5a5105c3561b0 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -105,42 +105,49 @@ PHP_FUNCTION(random_bytes) Return an arbitrary pseudo-random integer */ PHP_FUNCTION(random_int) { + zend_long min = ZEND_LONG_MIN; zend_long max = ZEND_LONG_MAX; - zend_long limit; - zend_long result; + zend_ulong limit; + zend_ulong umax; + zend_ulong result; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &max) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &min, &max) == FAILURE) { return; } - if (max <= 0 || max > ZEND_LONG_MAX) { - php_error_docref(NULL, E_WARNING, "Cannot use maximum less than 1 or greater than %d", ZEND_LONG_MAX); + if (min >= max) { + php_error_docref(NULL, E_WARNING, "Minimum value must be greater than the maximum value"); RETURN_FALSE; } - // Special case so we can return a range inclusive of the upper bound - if (max == ZEND_LONG_MAX) { - if (php_random_bytes(&result, sizeof(result)) == FAILURE) { - return; - } - RETURN_LONG(result & ZEND_LONG_MAX); - } + umax = max - min; - // Increment the max so the range is inclusive of max - max++; + if (php_random_bytes(&result, sizeof(result)) == FAILURE) { + return; + } - // Ceiling under which ZEND_LONG_MAX % max == 0 - limit = ZEND_LONG_MAX - (ZEND_LONG_MAX % max) - 1; + // Special case where no modulus is required + if (umax == ZEND_ULONG_MAX) { + RETURN_LONG((zend_long)result); + } - // Discard numbers over the limit to avoid modulo bias - do { - if (php_random_bytes(&result, sizeof(result)) == FAILURE) { - return; + // 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; + } } - result &= ZEND_LONG_MAX; - } while (result > limit); + } - RETURN_LONG(result % max); + RETURN_LONG((zend_long)((result % umax) + min)); } /* }}} */ From 77f99cc4da72fc80c3e5d71bf9c76275b5066bfe Mon Sep 17 00:00:00 2001 From: Leigh Date: Tue, 24 Feb 2015 01:30:57 +0000 Subject: [PATCH 13/20] Use arc4random where present --- ext/standard/config.m4 | 5 +++++ ext/standard/random.c | 10 +++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/ext/standard/config.m4 b/ext/standard/config.m4 index be23d6df8021b..2680a67f4e571 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 diff --git a/ext/standard/random.c b/ext/standard/random.c index 5a5105c3561b0..d3b6e545a685f 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -39,6 +39,9 @@ static int php_random_bytes(void *bytes, size_t size) 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 = -1; size_t read_bytes = 0; @@ -47,8 +50,8 @@ static int php_random_bytes(void *bytes, size_t size) #else #if HAVE_DEV_URANDOM fd = open("/dev/urandom", O_RDONLY); -#endif -#endif +#endif // URANDOM +#endif // ARANDOM if (fd < 0) { php_error_docref(NULL, E_WARNING, "Cannot open source device"); return FAILURE; @@ -67,7 +70,8 @@ static int php_random_bytes(void *bytes, size_t size) php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data"); return FAILURE; } -#endif +#endif // !ARC4RANDOM_BUF +#endif // !WIN32 return SUCCESS; } From 766ce0cad2d5711869b6b87f39d3fd986d46c979 Mon Sep 17 00:00:00 2001 From: SammyK Date: Tue, 24 Feb 2015 16:23:39 -0600 Subject: [PATCH 14/20] Add tests --- ext/standard/tests/random/random_bytes.phpt | 14 +++++++++++++ .../tests/random/random_bytes_error.phpt | 17 +++++++++++++++ ext/standard/tests/random/random_int.phpt | 21 +++++++++++++++++++ .../tests/random/random_int_error.phpt | 12 +++++++++++ 4 files changed, 64 insertions(+) create mode 100644 ext/standard/tests/random/random_bytes.phpt create mode 100644 ext/standard/tests/random/random_bytes_error.phpt create mode 100644 ext/standard/tests/random/random_int.phpt create mode 100644 ext/standard/tests/random/random_int_error.phpt 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..466a3ac3bf759 --- /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 +NULL + +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..8598282309d9e --- /dev/null +++ b/ext/standard/tests/random/random_int.phpt @@ -0,0 +1,21 @@ +--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) +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..e9ebfe0051b6d --- /dev/null +++ b/ext/standard/tests/random/random_int_error.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test error operation of random_int() +--FILE-- + +--EXPECTF-- +Warning: random_int(): Minimum value must be greater than the maximum value in %s on line %d +bool(false) From 99e36d60c1148cb76d7b1ce3cad680fa0e8526f3 Mon Sep 17 00:00:00 2001 From: SammyK Date: Tue, 24 Feb 2015 16:45:26 -0600 Subject: [PATCH 15/20] Fix wording in error message. Add check for max value. --- ext/standard/random.c | 9 +++++++-- ext/standard/tests/random/random_int_error.phpt | 7 ++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index d3b6e545a685f..598d32884c2a0 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -105,7 +105,7 @@ PHP_FUNCTION(random_bytes) } /* }}} */ -/* {{{ proto int random_int(int max) +/* {{{ proto int random_int(int min, int max) Return an arbitrary pseudo-random integer */ PHP_FUNCTION(random_int) { @@ -115,12 +115,17 @@ PHP_FUNCTION(random_int) zend_ulong umax; zend_ulong result; + if (ZEND_NUM_ARGS() == 1) { + php_error_docref(NULL, E_WARNING, "A minimum and maximum value are expected, only minimum given"); + RETURN_FALSE; + } + if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &min, &max) == FAILURE) { return; } if (min >= max) { - php_error_docref(NULL, E_WARNING, "Minimum value must be greater than the maximum value"); + php_error_docref(NULL, E_WARNING, "Minimum value must be less than the maximum value"); RETURN_FALSE; } diff --git a/ext/standard/tests/random/random_int_error.phpt b/ext/standard/tests/random/random_int_error.phpt index e9ebfe0051b6d..6a7f69c2cdc99 100644 --- a/ext/standard/tests/random/random_int_error.phpt +++ b/ext/standard/tests/random/random_int_error.phpt @@ -4,9 +4,14 @@ Test error operation of random_int() --EXPECTF-- -Warning: random_int(): Minimum value must be greater than the maximum value in %s on line %d +Warning: random_int(): A minimum and maximum value are expected, only minimum 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) From c6fc3915c450a94ffeec40d66b720f80598299f0 Mon Sep 17 00:00:00 2001 From: SammyK Date: Tue, 24 Feb 2015 18:04:18 -0600 Subject: [PATCH 16/20] Fix return types on error. Avoid a warning on BSD systems. --- ext/standard/random.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index 598d32884c2a0..397cc754651e5 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -31,8 +31,6 @@ static int php_random_bytes(void *bytes, size_t size) { - int n = 0; - #if PHP_WIN32 /* Defer to CryptGenRandom on Windows */ if (php_win32_get_random_bytes(bytes, size) == FAILURE) { @@ -57,6 +55,7 @@ static int php_random_bytes(void *bytes, size_t size) return FAILURE; } + size_t n = 0; while (read_bytes < size) { n = read(fd, bytes + read_bytes, size - read_bytes); if (n < 0) { @@ -96,7 +95,7 @@ PHP_FUNCTION(random_bytes) if (php_random_bytes(bytes->val, size) == FAILURE) { zend_string_release(bytes); - return; + RETURN_FALSE; } bytes->val[size] = '\0'; @@ -132,7 +131,7 @@ PHP_FUNCTION(random_int) umax = max - min; if (php_random_bytes(&result, sizeof(result)) == FAILURE) { - return; + RETURN_FALSE; } // Special case where no modulus is required From ab02b7bb0391624371ca6c1374ae9740b7fc66ec Mon Sep 17 00:00:00 2001 From: Leigh Date: Wed, 4 Mar 2015 17:37:40 +0000 Subject: [PATCH 17/20] Add fd caching --- ext/standard/basic_functions.c | 2 ++ ext/standard/php_random.h | 13 +++++++++++++ ext/standard/random.c | 33 +++++++++++++++++++++++++-------- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index d66e743c90d47..9b5057f865524 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -3659,6 +3659,8 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */ # endif #endif + BASIC_MINIT_SUBMODULE(random) + return SUCCESS; } /* }}} */ diff --git a/ext/standard/php_random.h b/ext/standard/php_random.h index 1be5894a636be..4ca74549b0ad6 100644 --- a/ext/standard/php_random.h +++ b/ext/standard/php_random.h @@ -23,6 +23,19 @@ PHP_FUNCTION(random_bytes); PHP_FUNCTION(random_int); + +PHP_MINIT_FUNCTION(lcg); + +ZEND_BEGIN_MODULE_GLOBALS(random) + int fd; +ZEND_END_MODULE_GLOBALS(random) + +#ifdef ZTS +# define RANDOM_G(v) TSRMG(random_globals_id, zend_random_globals *, v); +#else +# define RANDOM_G(v) random_globals.v +#endif + #endif /* diff --git a/ext/standard/random.c b/ext/standard/random.c index d3b6e545a685f..7bec554d3f739 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -24,11 +24,23 @@ #include #include "php.h" +#include "php_random.h" #if PHP_WIN32 # include "win32/winutil.h" #endif +ZEND_DECLARE_MODULE_GLOBALS(random); + +/* {{{ */ +PHP_MINIT_FUNCTION(random) +{ + RANDOM_G(fd) = -1; + return SUCCESS; +} +/* }}} */ + +/* {{{ */ static int php_random_bytes(void *bytes, size_t size) { int n = 0; @@ -43,18 +55,23 @@ static int php_random_bytes(void *bytes, size_t size) #if HAVE_DECL_ARC4RANDOM_BUF arc4random_buf(bytes, size); #else - int fd = -1; + int fd = RANDOM_G(fd); size_t read_bytes = 0; + + if (fd < 0) { #if HAVE_DEV_ARANDOM - fd = open("/dev/arandom", O_RDONLY); + fd = open("/dev/arandom", O_RDONLY); #else #if HAVE_DEV_URANDOM - fd = open("/dev/urandom", O_RDONLY); + 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; + if (fd < 0) { + php_error_docref(NULL, E_WARNING, "Cannot open source device"); + return FAILURE; + } + + RANDOM_G(fd) = fd; } while (read_bytes < size) { @@ -65,7 +82,6 @@ static int php_random_bytes(void *bytes, size_t size) read_bytes += n; } - close(fd); if (read_bytes < size) { php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data"); return FAILURE; @@ -75,6 +91,7 @@ static int php_random_bytes(void *bytes, size_t size) return SUCCESS; } +/* }}} */ /* {{{ proto string random_bytes(int length) Return an arbitrary length of pseudo-random bytes as binary string */ @@ -139,7 +156,7 @@ PHP_FUNCTION(random_int) umax++; // Powers of two are not biased - if (umax & ~umax != umax) { + if ((umax & ~umax) != umax) { // Ceiling under which ZEND_LONG_MAX % max == 0 limit = ZEND_ULONG_MAX - (ZEND_ULONG_MAX % umax) - 1; From 7ae49171643ddd77251b9c6bfcc12d492d60a60a Mon Sep 17 00:00:00 2001 From: Leigh Date: Fri, 27 Mar 2015 20:05:22 +0100 Subject: [PATCH 18/20] Fixes based on PR feedback --- ext/standard/basic_functions.c | 2 ++ ext/standard/php_random.h | 11 ++++++---- ext/standard/random.c | 37 +++++++++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 9b5057f865524..4040bafaac1fd 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -3699,6 +3699,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/php_random.h b/ext/standard/php_random.h index 4ca74549b0ad6..ecf9c7135ba60 100644 --- a/ext/standard/php_random.h +++ b/ext/standard/php_random.h @@ -24,16 +24,19 @@ PHP_FUNCTION(random_bytes); PHP_FUNCTION(random_int); -PHP_MINIT_FUNCTION(lcg); +PHP_MINIT_FUNCTION(random); +PHP_MSHUTDOWN_FUNCTION(random); -ZEND_BEGIN_MODULE_GLOBALS(random) +typedef struct { int fd; -ZEND_END_MODULE_GLOBALS(random) +} php_random_globals; #ifdef ZTS -# define RANDOM_G(v) TSRMG(random_globals_id, zend_random_globals *, v); +# 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 diff --git a/ext/standard/random.c b/ext/standard/random.c index 2548ab75774a5..52fa8fa658d89 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -30,16 +30,47 @@ # include "win32/winutil.h" #endif -ZEND_DECLARE_MODULE_GLOBALS(random); +#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) { - RANDOM_G(fd) = -1; +#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) { @@ -72,7 +103,7 @@ static int php_random_bytes(void *bytes, size_t size) RANDOM_G(fd) = fd; } - size_t n = 0; + ssize_t n = 0; while (read_bytes < size) { n = read(fd, bytes + read_bytes, size - read_bytes); if (n < 0) { From a67e42f3d9300725fa4fb9783e159bfd479c1390 Mon Sep 17 00:00:00 2001 From: Leigh Date: Thu, 9 Apr 2015 21:29:29 +0200 Subject: [PATCH 19/20] Changes based on feedback --- ext/standard/random.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index 52fa8fa658d89..22531cf24c1bd 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -103,9 +103,8 @@ static int php_random_bytes(void *bytes, size_t size) RANDOM_G(fd) = fd; } - ssize_t n = 0; while (read_bytes < size) { - n = read(fd, bytes + read_bytes, size - read_bytes); + ssize_t n = read(fd, bytes + read_bytes, size - read_bytes); if (n < 0) { break; } @@ -156,18 +155,13 @@ PHP_FUNCTION(random_bytes) Return an arbitrary pseudo-random integer */ PHP_FUNCTION(random_int) { - zend_long min = ZEND_LONG_MIN; - zend_long max = ZEND_LONG_MAX; + zend_long min; + zend_long max; zend_ulong limit; zend_ulong umax; zend_ulong result; - if (ZEND_NUM_ARGS() == 1) { - php_error_docref(NULL, E_WARNING, "A minimum and maximum value are expected, only minimum given"); - RETURN_FALSE; - } - - if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &min, &max) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &min, &max) == FAILURE) { return; } From f8a6d382b0133841ad2161abd6c5f1bc938f8aab Mon Sep 17 00:00:00 2001 From: SammyK Date: Fri, 10 Apr 2015 13:54:10 -0500 Subject: [PATCH 20/20] Normalized the return value for errors & updated tests. --- ext/standard/random.c | 4 ++-- ext/standard/tests/random/random_bytes_error.phpt | 2 +- ext/standard/tests/random/random_int.phpt | 3 --- ext/standard/tests/random/random_int_error.phpt | 7 ++++++- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/ext/standard/random.c b/ext/standard/random.c index 22531cf24c1bd..ab6e2e5e9acda 100644 --- a/ext/standard/random.c +++ b/ext/standard/random.c @@ -130,7 +130,7 @@ PHP_FUNCTION(random_bytes) zend_string *bytes; if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) { - return; + RETURN_FALSE; } if (size < 1) { @@ -162,7 +162,7 @@ PHP_FUNCTION(random_int) zend_ulong result; if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &min, &max) == FAILURE) { - return; + RETURN_FALSE; } if (min >= max) { diff --git a/ext/standard/tests/random/random_bytes_error.phpt b/ext/standard/tests/random/random_bytes_error.phpt index 466a3ac3bf759..b5a0c473c2044 100644 --- a/ext/standard/tests/random/random_bytes_error.phpt +++ b/ext/standard/tests/random/random_bytes_error.phpt @@ -11,7 +11,7 @@ var_dump(random_bytes(-1)); ?> --EXPECTF-- Warning: random_bytes() expects exactly 1 parameter, 0 given in %s on line %d -NULL +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 index 8598282309d9e..0c3081452c5d8 100644 --- a/ext/standard/tests/random/random_int.phpt +++ b/ext/standard/tests/random/random_int.phpt @@ -4,8 +4,6 @@ Test normal operation of random_int() --EXPECTF-- -Warning: random_int(): A minimum and maximum value are expected, only minimum given in %s on line %d +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