2007-07-02 06:19:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Random Number Generator
|
|
|
|
*
|
2015-04-02 12:57:52 +02:00
|
|
|
* PHP version 5
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
|
|
|
* Here's a short example of how to use this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
2014-12-17 01:16:54 +01:00
|
|
|
* include 'vendor/autoload.php';
|
2007-07-02 06:19:55 +02:00
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* echo bin2hex(\phpseclib\Crypt\Random::string(8));
|
2007-07-02 06:19:55 +02:00
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
2013-12-10 20:10:37 +01:00
|
|
|
* @category Crypt
|
2014-12-02 17:41:30 +01:00
|
|
|
* @package Random
|
2013-12-10 20:10:37 +01:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2014-12-10 00:02:44 +01:00
|
|
|
* @copyright 2007 Jim Wigginton
|
2013-12-10 20:10:37 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
2007-07-02 06:19:55 +02:00
|
|
|
*/
|
|
|
|
|
2014-12-02 17:41:30 +01:00
|
|
|
namespace phpseclib\Crypt;
|
|
|
|
|
2014-12-02 04:01:00 +01:00
|
|
|
/**
|
|
|
|
* Pure-PHP Random Number Generator
|
|
|
|
*
|
2014-12-02 17:41:30 +01:00
|
|
|
* @package Random
|
2014-12-02 04:01:00 +01:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2014-12-02 17:41:30 +01:00
|
|
|
class Random
|
2014-12-02 03:54:44 +01:00
|
|
|
{
|
2014-05-27 01:32:36 +02:00
|
|
|
/**
|
|
|
|
* Generate a random string.
|
|
|
|
*
|
|
|
|
* Although microoptimizations are generally discouraged as they impair readability this function is ripe with
|
|
|
|
* microoptimizations because this function has the potential of being called a huge number of times.
|
|
|
|
* eg. for RSA key generation.
|
|
|
|
*
|
2016-04-10 18:30:59 +02:00
|
|
|
* @param int $length
|
|
|
|
* @throws \RuntimeException if a symmetric cipher is needed but not loaded
|
|
|
|
* @return string
|
2014-05-27 01:32:36 +02:00
|
|
|
*/
|
2016-04-10 18:30:59 +02:00
|
|
|
static function string($length)
|
2014-05-27 01:32:36 +02:00
|
|
|
{
|
2016-04-10 18:30:59 +02:00
|
|
|
try {
|
|
|
|
return \random_bytes($length);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// random_compat will throw an Exception, which in PHP 5 does not implement Throwable
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// If a sufficient source of randomness is unavailable, random_bytes() will throw an
|
|
|
|
// object that implements the Throwable interface (Exception, TypeError, Error).
|
|
|
|
// We don't actually need to do anything here. The string() method should just continue
|
|
|
|
// as normal. Note, however, that if we don't have a sufficient source of randomness for
|
|
|
|
// random_bytes(), most of the other calls here will fail too, so we'll end up using
|
|
|
|
// the PHP implementation.
|
2011-11-24 19:32:49 +01:00
|
|
|
}
|
2014-05-27 01:32:36 +02:00
|
|
|
// at this point we have no choice but to use a pure-PHP CSPRNG
|
2011-11-24 19:32:49 +01:00
|
|
|
|
2014-05-27 01:32:36 +02:00
|
|
|
// cascade entropy across multiple PHP instances by fixing the session and collecting all
|
|
|
|
// environmental variables, including the previous session data and the current session
|
|
|
|
// data.
|
|
|
|
//
|
|
|
|
// mt_rand seeds itself by looking at the PID and the time, both of which are (relatively)
|
|
|
|
// easy to guess at. linux uses mouse clicks, keyboard timings, etc, as entropy sources, but
|
|
|
|
// PHP isn't low level to be able to use those as sources and on a web server there's not likely
|
|
|
|
// going to be a ton of keyboard or mouse action. web servers do have one thing that we can use
|
2014-06-16 16:40:27 +02:00
|
|
|
// however, a ton of people visiting the website. obviously you don't want to base your seeding
|
2014-05-27 01:32:36 +02:00
|
|
|
// soley on parameters a potential attacker sends but (1) not everything in $_SERVER is controlled
|
|
|
|
// by the user and (2) this isn't just looking at the data sent by the current user - it's based
|
|
|
|
// on the data sent by all users. one user requests the page and a hash of their info is saved.
|
|
|
|
// another user visits the page and the serialization of their data is utilized along with the
|
|
|
|
// server envirnment stuff and a hash of the previous http request data (which itself utilizes
|
|
|
|
// a hash of the session data before that). certainly an attacker should be assumed to have
|
|
|
|
// full control over his own http requests. he, however, is not going to have control over
|
|
|
|
// everyone's http requests.
|
|
|
|
static $crypto = false, $v;
|
|
|
|
if ($crypto === false) {
|
|
|
|
// save old session data
|
|
|
|
$old_session_id = session_id();
|
|
|
|
$old_use_cookies = ini_get('session.use_cookies');
|
|
|
|
$old_session_cache_limiter = session_cache_limiter();
|
|
|
|
$_OLD_SESSION = isset($_SESSION) ? $_SESSION : false;
|
|
|
|
if ($old_session_id != '') {
|
|
|
|
session_write_close();
|
|
|
|
}
|
2010-02-19 23:39:44 +01:00
|
|
|
|
2014-05-27 01:32:36 +02:00
|
|
|
session_id(1);
|
|
|
|
ini_set('session.use_cookies', 0);
|
|
|
|
session_cache_limiter('');
|
|
|
|
session_start();
|
2010-02-19 23:39:44 +01:00
|
|
|
|
2014-05-27 01:32:36 +02:00
|
|
|
$v = $seed = $_SESSION['seed'] = pack('H*', sha1(
|
2016-04-10 18:30:59 +02:00
|
|
|
(isset($_SERVER) ? self::safe_serialize($_SERVER) : '') .
|
|
|
|
(isset($_POST) ? self::safe_serialize($_POST) : '') .
|
|
|
|
(isset($_GET) ? self::safe_serialize($_GET) : '') .
|
|
|
|
(isset($_COOKIE) ? self::safe_serialize($_COOKIE) : '') .
|
|
|
|
self::safe_serialize($GLOBALS) .
|
|
|
|
self::safe_serialize($_SESSION) .
|
|
|
|
self::safe_serialize($_OLD_SESSION)
|
2014-05-27 01:32:36 +02:00
|
|
|
));
|
|
|
|
if (!isset($_SESSION['count'])) {
|
|
|
|
$_SESSION['count'] = 0;
|
|
|
|
}
|
|
|
|
$_SESSION['count']++;
|
2010-02-19 23:39:44 +01:00
|
|
|
|
2014-05-27 01:32:36 +02:00
|
|
|
session_write_close();
|
2010-02-19 23:39:44 +01:00
|
|
|
|
2014-05-27 01:32:36 +02:00
|
|
|
// restore old session data
|
|
|
|
if ($old_session_id != '') {
|
|
|
|
session_id($old_session_id);
|
|
|
|
session_start();
|
|
|
|
ini_set('session.use_cookies', $old_use_cookies);
|
|
|
|
session_cache_limiter($old_session_cache_limiter);
|
2012-12-16 09:20:16 +01:00
|
|
|
} else {
|
2015-07-15 03:52:31 +02:00
|
|
|
if ($_OLD_SESSION !== false) {
|
|
|
|
$_SESSION = $_OLD_SESSION;
|
|
|
|
unset($_OLD_SESSION);
|
2014-05-27 01:32:36 +02:00
|
|
|
} else {
|
|
|
|
unset($_SESSION);
|
|
|
|
}
|
2012-12-16 09:20:16 +01:00
|
|
|
}
|
2010-02-19 23:39:44 +01:00
|
|
|
|
2014-05-27 01:32:36 +02:00
|
|
|
// in SSH2 a shared secret and an exchange hash are generated through the key exchange process.
|
|
|
|
// the IV client to server is the hash of that "nonce" with the letter A and for the encryption key it's the letter C.
|
|
|
|
// if the hash doesn't produce enough a key or an IV that's long enough concat successive hashes of the
|
|
|
|
// original hash and the current hash. we'll be emulating that. for more info see the following URL:
|
|
|
|
//
|
|
|
|
// http://tools.ietf.org/html/rfc4253#section-7.2
|
|
|
|
//
|
|
|
|
// see the is_string($crypto) part for an example of how to expand the keys
|
|
|
|
$key = pack('H*', sha1($seed . 'A'));
|
|
|
|
$iv = pack('H*', sha1($seed . 'C'));
|
2007-07-02 06:19:55 +02:00
|
|
|
|
2014-05-27 01:32:36 +02:00
|
|
|
// ciphers are used as per the nist.gov link below. also, see this link:
|
|
|
|
//
|
|
|
|
// http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator#Designs_based_on_cryptographic_primitives
|
|
|
|
switch (true) {
|
2014-12-17 01:16:54 +01:00
|
|
|
case class_exists('\phpseclib\Crypt\AES'):
|
|
|
|
$crypto = new AES(Base::MODE_CTR);
|
2014-05-27 01:32:36 +02:00
|
|
|
break;
|
2014-12-17 01:16:54 +01:00
|
|
|
case class_exists('\phpseclib\Crypt\Twofish'):
|
|
|
|
$crypto = new Twofish(Base::MODE_CTR);
|
2014-06-16 16:40:27 +02:00
|
|
|
break;
|
2014-12-17 01:16:54 +01:00
|
|
|
case class_exists('\phpseclib\Crypt\Blowfish'):
|
|
|
|
$crypto = new Blowfish(Base::MODE_CTR);
|
2014-06-16 16:40:27 +02:00
|
|
|
break;
|
2014-12-17 01:16:54 +01:00
|
|
|
case class_exists('\phpseclib\Crypt\TripleDES'):
|
|
|
|
$crypto = new TripleDES(Base::MODE_CTR);
|
2014-05-27 01:32:36 +02:00
|
|
|
break;
|
2014-12-17 01:16:54 +01:00
|
|
|
case class_exists('\phpseclib\Crypt\DES'):
|
|
|
|
$crypto = new DES(Base::MODE_CTR);
|
2014-05-27 01:32:36 +02:00
|
|
|
break;
|
2014-12-17 01:16:54 +01:00
|
|
|
case class_exists('\phpseclib\Crypt\RC4'):
|
|
|
|
$crypto = new RC4();
|
2014-05-27 01:32:36 +02:00
|
|
|
break;
|
|
|
|
default:
|
2016-04-10 18:30:59 +02:00
|
|
|
throw new \RuntimeException(__CLASS__ . ' requires at least one symmetric cipher be loaded');
|
2014-05-27 01:32:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$crypto->setKey($key);
|
|
|
|
$crypto->setIV($iv);
|
|
|
|
$crypto->enableContinuousBuffer();
|
2007-07-02 06:19:55 +02:00
|
|
|
}
|
2012-12-16 09:20:16 +01:00
|
|
|
|
2014-05-27 01:32:36 +02:00
|
|
|
//return $crypto->encrypt(str_repeat("\0", $length));
|
2007-07-02 06:19:55 +02:00
|
|
|
|
2014-06-16 16:40:27 +02:00
|
|
|
// the following is based off of ANSI X9.31:
|
|
|
|
//
|
|
|
|
// http://csrc.nist.gov/groups/STM/cavp/documents/rng/931rngext.pdf
|
|
|
|
//
|
|
|
|
// OpenSSL uses that same standard for it's random numbers:
|
|
|
|
//
|
|
|
|
// http://www.opensource.apple.com/source/OpenSSL/OpenSSL-38/openssl/fips-1.0/rand/fips_rand.c
|
|
|
|
// (do a search for "ANS X9.31 A.2.4")
|
2012-12-16 09:20:16 +01:00
|
|
|
$result = '';
|
2014-05-27 01:32:36 +02:00
|
|
|
while (strlen($result) < $length) {
|
2014-06-16 16:40:27 +02:00
|
|
|
$i = $crypto->encrypt(microtime()); // strlen(microtime()) == 21
|
|
|
|
$r = $crypto->encrypt($i ^ $v); // strlen($v) == 20
|
|
|
|
$v = $crypto->encrypt($r ^ $i); // strlen($r) == 20
|
2012-12-16 09:20:16 +01:00
|
|
|
$result.= $r;
|
|
|
|
}
|
|
|
|
return substr($result, 0, $length);
|
|
|
|
}
|
2016-04-10 18:30:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Safely serialize variables
|
|
|
|
*
|
|
|
|
* If a class has a private __sleep() it'll emit a warning
|
|
|
|
*
|
|
|
|
* @param mixed $arr
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function safe_serialize(&$arr)
|
|
|
|
{
|
|
|
|
if (is_object($arr)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if (!is_array($arr)) {
|
|
|
|
return serialize($arr);
|
|
|
|
}
|
|
|
|
// prevent circular array recursion
|
|
|
|
if (isset($arr['__phpseclib_marker'])) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$safearr = array();
|
|
|
|
$arr['__phpseclib_marker'] = true;
|
|
|
|
foreach (array_keys($arr) as $key) {
|
|
|
|
// do not recurse on the '__phpseclib_marker' key itself, for smaller memory usage
|
|
|
|
if ($key !== '__phpseclib_marker') {
|
|
|
|
$safearr[$key] = self::safe_serialize($arr[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($arr['__phpseclib_marker']);
|
|
|
|
return serialize($safearr);
|
|
|
|
}
|
2013-01-10 20:19:31 +01:00
|
|
|
}
|