From a9aaf16de8204461096e768348344e3fca98af9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 9 Jan 2024 19:58:37 +0100 Subject: [PATCH] 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. --- ext/random/engine_pcgoneseq128xslrr64.c | 6 +++++- ext/random/engine_xoshiro256starstar.c | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ext/random/engine_pcgoneseq128xslrr64.c b/ext/random/engine_pcgoneseq128xslrr64.c index 5f318fa3f7547..39f3c2f7253f4 100644 --- a/ext/random/engine_pcgoneseq128xslrr64.c +++ b/ext/random/engine_pcgoneseq128xslrr64.c @@ -154,10 +154,14 @@ PHP_METHOD(Random_Engine_PcgOneseq128XslRr64, __construct) ZEND_PARSE_PARAMETERS_END(); if (seed_is_null) { - if (php_random_bytes_throw(&state->state, sizeof(state->state)) == FAILURE) { + php_random_uint128_t s; + + if (php_random_bytes_throw(&s, sizeof(s)) == FAILURE) { zend_throw_exception(random_ce_Random_RandomException, "Failed to generate a random seed", 0); RETURN_THROWS(); } + + seed128(state, s); } else { if (str_seed) { /* char (byte: 8 bit) * 16 = 128 bits */ diff --git a/ext/random/engine_xoshiro256starstar.c b/ext/random/engine_xoshiro256starstar.c index 7468bde3b16e7..b32daf4a5a6dd 100644 --- a/ext/random/engine_xoshiro256starstar.c +++ b/ext/random/engine_xoshiro256starstar.c @@ -216,12 +216,16 @@ PHP_METHOD(Random_Engine_Xoshiro256StarStar, __construct) ZEND_PARSE_PARAMETERS_END(); if (seed_is_null) { + uint64_t t[4]; + do { - if (php_random_bytes_throw(&state->state, sizeof(state->state)) == FAILURE) { + if (php_random_bytes_throw(&t, sizeof(t)) == FAILURE) { zend_throw_exception(random_ce_Random_RandomException, "Failed to generate a random seed", 0); RETURN_THROWS(); } - } while (UNEXPECTED(state->state[0] == 0 && state->state[1] == 0 && state->state[2] == 0 && state->state[3] == 0)); + } while (UNEXPECTED(t[0] == 0 && t[1] == 0 && t[2] == 0 && t[3] == 0)); + + seed256(state, t[0], t[1], t[2], t[3]); } else { if (str_seed) { /* char (byte: 8 bit) * 32 = 256 bits */