summaryrefslogtreecommitdiffstats
path: root/system
diff options
context:
space:
mode:
authorAndrey Andreev <narf@devilix.net>2016-03-22 12:42:03 +0100
committerAndrey Andreev <narf@devilix.net>2016-03-22 12:42:03 +0100
commit4d2628e8aab6d0673ac0a010acbfaa9d76b7d568 (patch)
treeb3cac7e3e06b8ac4c56d771cc83b5df53eca1231 /system
parent86758e1003e6ce44b205d2eb104318a309fd92ab (diff)
random_bytes()-related improvements
See #4260
Diffstat (limited to 'system')
-rw-r--r--system/core/compat/password.php26
-rw-r--r--system/libraries/Encryption.php26
2 files changed, 42 insertions, 10 deletions
diff --git a/system/core/compat/password.php b/system/core/compat/password.php
index f0c22c780..76dd2cf0a 100644
--- a/system/core/compat/password.php
+++ b/system/core/compat/password.php
@@ -116,13 +116,21 @@ if ( ! function_exists('password_hash'))
}
elseif ( ! isset($options['salt']))
{
- if (defined('MCRYPT_DEV_URANDOM'))
+ if (function_exists('random_bytes'))
{
- $options['salt'] = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
+ try
+ {
+ $options['salt'] = random_bytes(16);
+ }
+ catch (Exception $e)
+ {
+ log_message('error', 'compat/password: Error while trying to use random_bytes(): '.$e->getMessage());
+ return FALSE;
+ }
}
- elseif (function_exists('openssl_random_pseudo_bytes'))
+ elseif (defined('MCRYPT_DEV_URANDOM'))
{
- $options['salt'] = openssl_random_pseudo_bytes(16);
+ $options['salt'] = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
}
elseif (DIRECTORY_SEPARATOR === '/' && (is_readable($dev = '/dev/arandom') OR is_readable($dev = '/dev/urandom')))
{
@@ -148,6 +156,16 @@ if ( ! function_exists('password_hash'))
fclose($fp);
}
+ elseif (function_exists('openssl_random_pseudo_bytes'))
+ {
+ $is_secure = NULL;
+ $options['salt'] = openssl_random_pseudo_bytes(16, $is_secure);
+ if ($is_secure !== TRUE)
+ {
+ log_message('error', 'compat/password: openssl_random_pseudo_bytes() set the $cryto_strong flag to FALSE');
+ return FALSE;
+ }
+ }
else
{
log_message('error', 'compat/password: No CSPRNG available.');
diff --git a/system/libraries/Encryption.php b/system/libraries/Encryption.php
index 92c38a0ed..a10a5c20c 100644
--- a/system/libraries/Encryption.php
+++ b/system/libraries/Encryption.php
@@ -339,12 +339,26 @@ class CI_Encryption {
{
if (function_exists('random_bytes'))
{
- return random_bytes((int) $length);
+ try
+ {
+ return random_bytes((int) $length);
+ }
+ catch (Exception $e)
+ {
+ log_message('error', $e->getMessage());
+ return FALSE;
+ }
+ }
+ elseif (defined('MCRYPT_DEV_URANDOM'))
+ {
+ return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
}
- return ($this->_driver === 'mcrypt')
- ? mcrypt_create_iv($length, MCRYPT_DEV_URANDOM)
- : openssl_random_pseudo_bytes($length);
+ $is_secure = NULL;
+ $key = openssl_random_pseudo_bytes($length, $is_secure);
+ return ($is_secure === TRUE)
+ ? $key
+ : FALSE;
}
// --------------------------------------------------------------------
@@ -400,7 +414,7 @@ class CI_Encryption {
// The greater-than-1 comparison is mostly a work-around for a bug,
// where 1 is returned for ARCFour instead of 0.
$iv = (($iv_size = mcrypt_enc_get_iv_size($params['handle'])) > 1)
- ? mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM)
+ ? $this->create_key($iv_size)
: NULL;
if (mcrypt_generic_init($params['handle'], $params['key'], $iv) < 0)
@@ -463,7 +477,7 @@ class CI_Encryption {
}
$iv = ($iv_size = openssl_cipher_iv_length($params['handle']))
- ? openssl_random_pseudo_bytes($iv_size)
+ ? $this->create_key($iv_size)
: NULL;
$data = openssl_encrypt(