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;

ext/random/php_random_csprng.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| license@php.net so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: Tim Düsterhus <timwolla@php.net> |
14+
| Go Kudo <zeriyoshi@php.net> |
15+
+----------------------------------------------------------------------+
16+
*/
17+
18+
#ifndef PHP_RANDOM_CSPRNG_H
19+
# define PHP_RANDOM_CSPRNG_H
20+
21+
# include "php.h"
22+
23+
PHPAPI zend_result php_random_bytes(void *bytes, size_t size, bool should_throw);
24+
PHPAPI zend_result php_random_int(zend_long min, zend_long max, zend_long *result, bool should_throw);
25+
26+
static inline zend_result php_random_bytes_throw(void *bytes, size_t size)
27+
{
28+
return php_random_bytes(bytes, size, true);
29+
}
30+
31+
static inline zend_result php_random_bytes_silent(void *bytes, size_t size)
32+
{
33+
return php_random_bytes(bytes, size, false);
34+
}
35+
36+
static inline zend_result php_random_int_throw(zend_long min, zend_long max, zend_long *result)
37+
{
38+
return php_random_int(min, max, result, true);
39+
}
40+
41+
static inline zend_result php_random_int_silent(zend_long min, zend_long max, zend_long *result)
42+
{
43+
return php_random_int(min, max, result, false);
44+
}
45+
46+
#endif /* PHP_RANDOM_CSPRNG_H */

ext/random/random.c

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

3232
#include "php_random.h"
33+
#include "php_random_csprng.h"
3334

3435
#if HAVE_UNISTD_H
3536
# include <unistd.h>

ext/reflection/php_reflection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "php_reflection.h"
2828
#include "ext/standard/info.h"
2929
#include "ext/standard/sha1.h"
30-
#include "ext/random/php_random.h"
30+
#include "ext/random/php_random_csprng.h"
3131

3232
#include "zend.h"
3333
#include "zend_API.h"

ext/session/session.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "ext/standard/basic_functions.h"
4747
#include "ext/standard/head.h"
4848
#include "ext/random/php_random.h"
49+
#include "ext/random/php_random_csprng.h"
4950

5051
#include "mod_files.h"
5152
#include "mod_user.h"

ext/soap/php_http.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "php_soap.h"
2020
#include "ext/standard/base64.h"
2121
#include "ext/standard/md5.h"
22-
#include "ext/random/php_random.h"
22+
#include "ext/random/php_random_csprng.h"
2323
#include "ext/hash/php_hash.h"
2424

2525
static char *get_http_header_value_nodup(char *headers, char *type, size_t *len);

ext/standard/password.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "base64.h"
2626
#include "zend_interfaces.h"
2727
#include "info.h"
28-
#include "ext/random/php_random.h"
28+
#include "ext/random/php_random_csprng.h"
2929
#ifdef HAVE_ARGON2LIB
3030
#include "argon2.h"
3131
#endif

ext/standard/uniqid.c

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

3434
#include "ext/random/php_random.h"
35+
#include "ext/random/php_random_csprng.h"
3536

3637
#ifdef HAVE_GETTIMEOFDAY
3738
ZEND_TLS struct timeval prev_tv = { 0, 0 };

0 commit comments

Comments
 (0)