Skip to content

Commit 97b3b45

Browse files
authored
random: Move CSPRNG API into php_random_csprng.h (#13290)
This allows consumers of just the CSPRNG to include a much smaller header. It also allows to verify at a glance whether a source file might use non-secure randomness. This commit includes the new header wherever the CSPRNG is used, possibly replacing the inclusion of php_random.h if nothing else is used, but also includes it in the main php_random.h header for compatibility. Somewhat related to 45f8cfa, 2b30f18, and b14dd85.
1 parent 77bc863 commit 97b3b45

17 files changed

+64
-28
lines changed

UPGRADING.INTERNALS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ PHP 8.4 INTERNALS UPGRADE NOTES
8383
the new php_random_result struct, replacing the last_generated_size
8484
member of the php_random_status struct and the generate_size member of
8585
the php_random_algo struct.
86+
- The CSPRNG API (php_random_(bytes|int)_*) is now provided by the new
87+
and much smaller php_random_csprng.h header. The new header is included
88+
in php_random.h for compatibility with existing users.
8689

8790
c. ext/xsl
8891
- The function php_xsl_create_object() was removed as it was not used

ext/gmp/gmp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
/* Needed for gmp_random() */
3333
#include "ext/random/php_random.h"
34+
#include "ext/random/php_random_csprng.h"
3435

3536
#define GMP_ROUND_ZERO 0
3637
#define GMP_ROUND_PLUSINF 1

ext/random/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ PHP_NEW_EXTENSION(random,
2929
gammasection.c \
3030
randomizer.c,
3131
no,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
32-
PHP_INSTALL_HEADERS([ext/random], [php_random.h php_random_uint128.h])
32+
PHP_INSTALL_HEADERS([ext/random], [php_random.h php_random_csprng.h php_random_uint128.h])

ext/random/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
EXTENSION("random", "random.c", false /* never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
22
PHP_RANDOM="yes";
33
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");
4-
PHP_INSTALL_HEADERS("ext/random", "php_random.h php_random_uint128.h");
4+
PHP_INSTALL_HEADERS("ext/random", "php_random.h php_random_csprng.h php_random_uint128.h");

ext/random/csprng.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "Zend/zend_exceptions.h"
2929

3030
#include "php_random.h"
31+
#include "php_random_csprng.h"
3132

3233
#if HAVE_UNISTD_H
3334
# include <unistd.h>

ext/random/engine_mt19937.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "php.h"
3131
#include "php_random.h"
32+
#include "php_random_csprng.h"
3233

3334
#include "Zend/zend_exceptions.h"
3435

ext/random/engine_pcgoneseq128xslrr64.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "php.h"
2424
#include "php_random.h"
25+
#include "php_random_csprng.h"
2526
#include "php_random_uint128.h"
2627

2728
#include "Zend/zend_exceptions.h"

ext/random/engine_secure.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "php.h"
2323
#include "php_random.h"
24+
#include "php_random_csprng.h"
2425

2526
#include "Zend/zend_exceptions.h"
2627

ext/random/engine_xoshiro256starstar.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "php.h"
2525
#include "php_random.h"
26+
#include "php_random_csprng.h"
2627

2728
#include "Zend/zend_exceptions.h"
2829

ext/random/php_random.h

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# define PHP_RANDOM_H
3333

3434
# include "php.h"
35+
# include "php_random_csprng.h"
3536
# include "php_random_uint128.h"
3637

3738
PHPAPI double php_combined_lcg(void);
@@ -65,29 +66,6 @@ PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
6566
PHPAPI void php_srand(zend_long seed);
6667
PHPAPI zend_long php_rand(void);
6768

68-
PHPAPI zend_result php_random_bytes(void *bytes, size_t size, bool should_throw);
69-
PHPAPI zend_result php_random_int(zend_long min, zend_long max, zend_long *result, bool should_throw);
70-
71-
static inline zend_result php_random_bytes_throw(void *bytes, size_t size)
72-
{
73-
return php_random_bytes(bytes, size, true);
74-
}
75-
76-
static inline zend_result php_random_bytes_silent(void *bytes, size_t size)
77-
{
78-
return php_random_bytes(bytes, size, false);
79-
}
80-
81-
static inline zend_result php_random_int_throw(zend_long min, zend_long max, zend_long *result)
82-
{
83-
return php_random_int(min, max, result, true);
84-
}
85-
86-
static inline zend_result php_random_int_silent(zend_long min, zend_long max, zend_long *result)
87-
{
88-
return php_random_int(min, max, result, false);
89-
}
90-
9169
typedef struct _php_random_status_ {
9270
void *state;
9371
} php_random_status;

0 commit comments

Comments
 (0)