Skip to content

Commit a9aaf16

Browse files
committed
random: Reuse the seed128/seed256 helpers when seeding using the CSPRNG
Instead of writing to the engine's state struct directly, use the helpers for consistency.
1 parent b8f10de commit a9aaf16

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

ext/random/engine_pcgoneseq128xslrr64.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,14 @@ PHP_METHOD(Random_Engine_PcgOneseq128XslRr64, __construct)
154154
ZEND_PARSE_PARAMETERS_END();
155155

156156
if (seed_is_null) {
157-
if (php_random_bytes_throw(&state->state, sizeof(state->state)) == FAILURE) {
157+
php_random_uint128_t s;
158+
159+
if (php_random_bytes_throw(&s, sizeof(s)) == FAILURE) {
158160
zend_throw_exception(random_ce_Random_RandomException, "Failed to generate a random seed", 0);
159161
RETURN_THROWS();
160162
}
163+
164+
seed128(state, s);
161165
} else {
162166
if (str_seed) {
163167
/* char (byte: 8 bit) * 16 = 128 bits */

ext/random/engine_xoshiro256starstar.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,16 @@ PHP_METHOD(Random_Engine_Xoshiro256StarStar, __construct)
216216
ZEND_PARSE_PARAMETERS_END();
217217

218218
if (seed_is_null) {
219+
uint64_t t[4];
220+
219221
do {
220-
if (php_random_bytes_throw(&state->state, sizeof(state->state)) == FAILURE) {
222+
if (php_random_bytes_throw(&t, sizeof(t)) == FAILURE) {
221223
zend_throw_exception(random_ce_Random_RandomException, "Failed to generate a random seed", 0);
222224
RETURN_THROWS();
223225
}
224-
} while (UNEXPECTED(state->state[0] == 0 && state->state[1] == 0 && state->state[2] == 0 && state->state[3] == 0));
226+
} while (UNEXPECTED(t[0] == 0 && t[1] == 0 && t[2] == 0 && t[3] == 0));
227+
228+
seed256(state, t[0], t[1], t[2], t[3]);
225229
} else {
226230
if (str_seed) {
227231
/* char (byte: 8 bit) * 32 = 256 bits */

0 commit comments

Comments
 (0)