OpenSSL random pseudo bytes
| From: | Scott MacVicar | Date: | Wed, 03 Sep 2008 01:08:40 +0000 |
| Subject: | OpenSSL random pseudo bytes | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-40225@lists.php.net to get a copy of this message | ||
Hi All,
Attached and uploaded [1] is a patch to add the OpenSSL random pseudo byte function, at the moment it will return FALSE if the bytes aren't considered cryptographically strong, I am however considering making this parameter controlled.
Any objections to me applying this to 5.3?
Scott
--
[1] - http://whisky.macvicar.net/patches/openssl_prg.patch.txt
Index: ext/openssl/openssl.c =================================================================== RCS file: /repository/php-src/ext/openssl/openssl.c,v retrieving revision 1.98.2.5.2.41.2.16 diff -u -r1.98.2.5.2.41.2.16 openssl.c --- ext/openssl/openssl.c 30 Jul 2008 11:59:05 -0000 1.98.2.5.2.41.2.16 +++ ext/openssl/openssl.c 3 Sep 2008 01:06:19 -0000 @@ -94,6 +94,7 @@ PHP_FUNCTION(openssl_decrypt); PHP_FUNCTION(openssl_dh_compute_key); +PHP_FUNCTION(openssl_random_pseudo_bytes); /* {{{ arginfo */ static @@ -394,6 +395,11 @@ ZEND_ARG_INFO(0, pub_key) ZEND_ARG_INFO(0, dh_key) ZEND_END_ARG_INFO() + +static +ZEND_BEGIN_ARG_INFO(arginfo_openssl_random_psuedo_bytes, 0) + ZEND_ARG_INFO(0, length) +ZEND_END_ARG_INFO() /* }}} */ /* {{{ openssl_functions[] @@ -458,6 +464,7 @@ PHP_FE(openssl_dh_compute_key, arginfo_openssl_dh_compute_key) + PHP_FE(openssl_random_pseudo_bytes, arginfo_openssl_random_psuedo_bytes) PHP_FE(openssl_error_string, arginfo_openssl_error_string) {NULL, NULL, NULL} }; @@ -4742,6 +4749,30 @@ } /* }}} */ +/* {{{ proto string openssl_random_pseudo_bytes(integer length) + Returns a string of the length specified filled with random pseudo bytes */ +PHP_FUNCTION(openssl_random_pseudo_bytes) +{ + long buffer_length; + unsigned char *buffer = NULL; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &buffer_length) == FAILURE) { + return; + } + + if (buffer_length <= 0) { + RETURN_FALSE; + } + + buffer = emalloc(buffer_length); + if (RAND_pseudo_bytes(buffer, buffer_length) < 0) { + RETVAL_FALSE; + } else { + RETVAL_STRINGL((char *)buffer, buffer_length, 1); + } + efree(buffer); +} +/* }}} */ + /* * Local variables: * tab-width: 8
Index: ext/openssl/openssl.c =================================================================== RCS file: /repository/php-src/ext/openssl/openssl.c,v retrieving revision 1.98.2.5.2.41.2.16 diff -u -r1.98.2.5.2.41.2.16 openssl.c --- ext/openssl/openssl.c 30 Jul 2008 11:59:05 -0000 1.98.2.5.2.41.2.16 +++ ext/openssl/openssl.c 3 Sep 2008 01:06:19 -0000 @@ -94,6 +94,7 @@ PHP_FUNCTION(openssl_decrypt); PHP_FUNCTION(openssl_dh_compute_key); +PHP_FUNCTION(openssl_random_pseudo_bytes); /* {{{ arginfo */ static @@ -394,6 +395,11 @@ ZEND_ARG_INFO(0, pub_key) ZEND_ARG_INFO(0, dh_key) ZEND_END_ARG_INFO() + +static +ZEND_BEGIN_ARG_INFO(arginfo_openssl_random_psuedo_bytes, 0) + ZEND_ARG_INFO(0, length) +ZEND_END_ARG_INFO() /* }}} */ /* {{{ openssl_functions[] @@ -458,6 +464,7 @@ PHP_FE(openssl_dh_compute_key, arginfo_openssl_dh_compute_key) + PHP_FE(openssl_random_pseudo_bytes, arginfo_openssl_random_psuedo_bytes) PHP_FE(openssl_error_string, arginfo_openssl_error_string) {NULL, NULL, NULL} }; @@ -4742,6 +4749,30 @@ } /* }}} */ +/* {{{ proto string openssl_random_pseudo_bytes(integer length) + Returns a string of the length specified filled with random pseudo bytes */ +PHP_FUNCTION(openssl_random_pseudo_bytes) +{ + long buffer_length; + unsigned char *buffer = NULL; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &buffer_length) == FAILURE) { + return; + } + + if (buffer_length <= 0) { + RETURN_FALSE; + } + + buffer = emalloc(buffer_length); + if (RAND_pseudo_bytes(buffer, buffer_length) < 0) { + RETVAL_FALSE; + } else { + RETVAL_STRINGL((char *)buffer, buffer_length, 1); + } + efree(buffer); +} +/* }}} */ + /* * Local variables: * tab-width: 8