Skip to content

random: Use CSPRNG for CombinedLCG seeding #13748

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

Merged
merged 1 commit into from
Mar 19, 2024
Merged
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
19 changes: 4 additions & 15 deletions ext/random/engine_combinedlcg.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,12 @@ const php_random_algo php_random_algo_combinedlcg = {
/* {{{ php_random_combinedlcg_seed_default */
PHPAPI void php_random_combinedlcg_seed_default(php_random_status_state_combinedlcg *state)
{
struct timeval tv;
uint64_t seed = 0;

if (gettimeofday(&tv, NULL) == 0) {
state->state[0] = tv.tv_usec ^ (tv.tv_usec << 11);
} else {
state->state[0] = 1;
if (php_random_bytes_silent(&seed, sizeof(seed)) == FAILURE) {
seed = php_random_generate_fallback_seed();
}

#ifdef ZTS
state->state[1] = (zend_long) tsrm_thread_id();
#else
state->state[1] = (zend_long) getpid();
#endif

/* Add entropy to s2 by calling gettimeofday() again */
if (gettimeofday(&tv, NULL) == 0) {
state->state[1] ^= (tv.tv_usec << 11);
}
php_random_combinedlcg_seed64(state, seed);
}
/* }}} */