Skip to content
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

random: Reuse the seed128/seed256 helpers when seeding using the CSPRNG #13311

Merged
merged 1 commit into from
Feb 5, 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
6 changes: 5 additions & 1 deletion ext/random/engine_pcgoneseq128xslrr64.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
8 changes: 6 additions & 2 deletions ext/random/engine_xoshiro256starstar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Loading