Skip to content

random: Move CSPRNG API into php_random_csprng.h #13290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ PHP 8.4 INTERNALS UPGRADE NOTES
the new php_random_result struct, replacing the last_generated_size
member of the php_random_status struct and the generate_size member of
the php_random_algo struct.
- The CSPRNG API (php_random_(bytes|int)_*) is now provided by the new
and much smaller php_random_csprng.h header. The new header is included
in php_random.h for compatibility with existing users.

c. ext/xsl
- The function php_xsl_create_object() was removed as it was not used
Expand Down
1 change: 1 addition & 0 deletions ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

/* Needed for gmp_random() */
#include "ext/random/php_random.h"
#include "ext/random/php_random_csprng.h"

#define GMP_ROUND_ZERO 0
#define GMP_ROUND_PLUSINF 1
Expand Down
2 changes: 1 addition & 1 deletion ext/random/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ PHP_NEW_EXTENSION(random,
gammasection.c \
randomizer.c,
no,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
PHP_INSTALL_HEADERS([ext/random], [php_random.h php_random_uint128.h])
PHP_INSTALL_HEADERS([ext/random], [php_random.h php_random_csprng.h php_random_uint128.h])
2 changes: 1 addition & 1 deletion ext/random/config.w32
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
EXTENSION("random", "random.c", false /* never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
PHP_RANDOM="yes";
ADD_SOURCES(configure_module_dirname, "csprng.c engine_combinedlcg.c engine_mt19937.c engine_pcgoneseq128xslrr64.c engine_xoshiro256starstar.c engine_secure.c engine_user.c gammasection.c randomizer.c", "random");
PHP_INSTALL_HEADERS("ext/random", "php_random.h php_random_uint128.h");
PHP_INSTALL_HEADERS("ext/random", "php_random.h php_random_csprng.h php_random_uint128.h");
1 change: 1 addition & 0 deletions ext/random/csprng.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "Zend/zend_exceptions.h"

#include "php_random.h"
#include "php_random_csprng.h"

#if HAVE_UNISTD_H
# include <unistd.h>
Expand Down
1 change: 1 addition & 0 deletions ext/random/engine_mt19937.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "php.h"
#include "php_random.h"
#include "php_random_csprng.h"

#include "Zend/zend_exceptions.h"

Expand Down
1 change: 1 addition & 0 deletions ext/random/engine_pcgoneseq128xslrr64.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "php.h"
#include "php_random.h"
#include "php_random_csprng.h"
#include "php_random_uint128.h"

#include "Zend/zend_exceptions.h"
Expand Down
1 change: 1 addition & 0 deletions ext/random/engine_secure.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "php.h"
#include "php_random.h"
#include "php_random_csprng.h"

#include "Zend/zend_exceptions.h"

Expand Down
1 change: 1 addition & 0 deletions ext/random/engine_xoshiro256starstar.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "php.h"
#include "php_random.h"
#include "php_random_csprng.h"

#include "Zend/zend_exceptions.h"

Expand Down
24 changes: 1 addition & 23 deletions ext/random/php_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
# define PHP_RANDOM_H

# include "php.h"
# include "php_random_csprng.h"
# include "php_random_uint128.h"

PHPAPI double php_combined_lcg(void);
Expand Down Expand Up @@ -65,29 +66,6 @@ PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
PHPAPI void php_srand(zend_long seed);
PHPAPI zend_long php_rand(void);

PHPAPI zend_result php_random_bytes(void *bytes, size_t size, bool should_throw);
PHPAPI zend_result php_random_int(zend_long min, zend_long max, zend_long *result, bool should_throw);

static inline zend_result php_random_bytes_throw(void *bytes, size_t size)
{
return php_random_bytes(bytes, size, true);
}

static inline zend_result php_random_bytes_silent(void *bytes, size_t size)
{
return php_random_bytes(bytes, size, false);
}

static inline zend_result php_random_int_throw(zend_long min, zend_long max, zend_long *result)
{
return php_random_int(min, max, result, true);
}

static inline zend_result php_random_int_silent(zend_long min, zend_long max, zend_long *result)
{
return php_random_int(min, max, result, false);
}

typedef struct _php_random_status_ {
void *state;
} php_random_status;
Expand Down
46 changes: 46 additions & 0 deletions ext/random/php_random_csprng.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
+----------------------------------------------------------------------+
| Copyright (c) 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: |
| https://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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Tim Düsterhus <[email protected]> |
| Go Kudo <[email protected]> |
+----------------------------------------------------------------------+
*/

#ifndef PHP_RANDOM_CSPRNG_H
# define PHP_RANDOM_CSPRNG_H

# include "php.h"

PHPAPI zend_result php_random_bytes(void *bytes, size_t size, bool should_throw);
PHPAPI zend_result php_random_int(zend_long min, zend_long max, zend_long *result, bool should_throw);

static inline zend_result php_random_bytes_throw(void *bytes, size_t size)
{
return php_random_bytes(bytes, size, true);
}

static inline zend_result php_random_bytes_silent(void *bytes, size_t size)
{
return php_random_bytes(bytes, size, false);
}

static inline zend_result php_random_int_throw(zend_long min, zend_long max, zend_long *result)
{
return php_random_int(min, max, result, true);
}

static inline zend_result php_random_int_silent(zend_long min, zend_long max, zend_long *result)
{
return php_random_int(min, max, result, false);
}

#endif /* PHP_RANDOM_CSPRNG_H */
1 change: 1 addition & 0 deletions ext/random/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "Zend/zend_exceptions.h"

#include "php_random.h"
#include "php_random_csprng.h"

#if HAVE_UNISTD_H
# include <unistd.h>
Expand Down
2 changes: 1 addition & 1 deletion ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "php_reflection.h"
#include "ext/standard/info.h"
#include "ext/standard/sha1.h"
#include "ext/random/php_random.h"
#include "ext/random/php_random_csprng.h"

#include "zend.h"
#include "zend_API.h"
Expand Down
1 change: 1 addition & 0 deletions ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "ext/standard/basic_functions.h"
#include "ext/standard/head.h"
#include "ext/random/php_random.h"
#include "ext/random/php_random_csprng.h"

#include "mod_files.h"
#include "mod_user.h"
Expand Down
2 changes: 1 addition & 1 deletion ext/soap/php_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "php_soap.h"
#include "ext/standard/base64.h"
#include "ext/standard/md5.h"
#include "ext/random/php_random.h"
#include "ext/random/php_random_csprng.h"
#include "ext/hash/php_hash.h"

static char *get_http_header_value_nodup(char *headers, char *type, size_t *len);
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/password.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "base64.h"
#include "zend_interfaces.h"
#include "info.h"
#include "ext/random/php_random.h"
#include "ext/random/php_random_csprng.h"
#ifdef HAVE_ARGON2LIB
#include "argon2.h"
#endif
Expand Down
1 change: 1 addition & 0 deletions ext/standard/uniqid.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#endif

#include "ext/random/php_random.h"
#include "ext/random/php_random_csprng.h"

#ifdef HAVE_GETTIMEOFDAY
ZEND_TLS struct timeval prev_tv = { 0, 0 };
Expand Down