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

Improve randomness #5268

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions composer.json-dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
],
"require": {
"php": ">=5.3.7",
"paragonie/random_compat": "^1|^2",
"roundcube/plugin-installer": "~0.1.6",
"pear-pear.php.net/net_socket": "~1.0.12",
"pear-pear.php.net/auth_sasl": "~1.0.6",
Expand Down
6 changes: 3 additions & 3 deletions plugins/password/password.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ static function hash_password($password, $method = '', $prefixed = true)
break;

case 'ssha':
$salt = substr(pack('h*', md5(mt_rand())), 0, 8);
$salt = random_bytes(8);

if (function_exists('mhash') && function_exists('mhash_keygen_s2k')) {
$salt = mhash_keygen_s2k(MHASH_SHA1, $password, $salt, 4);
Expand All @@ -530,7 +530,7 @@ static function hash_password($password, $method = '', $prefixed = true)
break;

case 'smd5':
$salt = substr(pack('h*', md5(mt_rand())), 0, 8);
$salt = random_bytes(8);

if (function_exists('mhash') && function_exists('mhash_keygen_s2k')) {
$salt = mhash_keygen_s2k(MHASH_MD5, $password, $salt, 4);
Expand Down Expand Up @@ -666,7 +666,7 @@ static function random_salt($length)
$str = '';

while (strlen($str) < $length) {
$str .= substr($possible, (rand() % strlen($possible)), 1);
$str .= $possible[random_int(0, 63)];
}

return $str;
Expand Down
4 changes: 2 additions & 2 deletions program/lib/Roundcube/rcube.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ public function gc_run()
$divisor = (int) ini_get('session.gc_divisor');

if ($divisor > 0 && $probability > 0) {
$random = mt_rand(1, $divisor);
$random = random_int(1, $divisor);
if ($random <= $probability) {
$this->gc();
}
Expand Down Expand Up @@ -1488,7 +1488,7 @@ public function get_user_language()
*/
public function gen_message_id()
{
$local_part = md5(uniqid('rcube'.mt_rand(), true));
$local_part = bin2hex(random_bytes(16));
$domain_part = $this->user->get_username('domain');

// Try to find FQDN, some spamfilters doesn't like 'localhost' (#1486924)
Expand Down
2 changes: 1 addition & 1 deletion program/lib/Roundcube/rcube_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ function get_hash()

// generate a random hash and store it in user prefs
if (empty($prefs['client_hash'])) {
$prefs['client_hash'] = md5($this->data['username'] . mt_rand() . $this->data['mail_host']);
$prefs['client_hash'] = bin2hex(random_bytes(16));
$this->save_prefs(array('client_hash' => $prefs['client_hash']));
}

Expand Down
1 change: 1 addition & 0 deletions program/lib/Roundcube/rcube_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,7 @@ public static function random_bytes($length, $raw = false)
$random = random_bytes($length);
}
catch (Throwable $e) {}
catch (Exception $e) {} // random_compat
}

if (!$random) {
Expand Down
2 changes: 1 addition & 1 deletion program/steps/mail/compose.inc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if (!is_array($COMPOSE)) {
}
}

$COMPOSE_ID = uniqid(mt_rand());
$COMPOSE_ID = bin2hex(random_bytes(32));
$params = rcube_utils::request2param(rcube_utils::INPUT_GET, 'task|action', true);

$_SESSION['compose_data_'.$COMPOSE_ID] = array(
Expand Down