1
0
mirror of https://github.com/danog/phpseclib.git synced 2024-12-03 10:08:04 +01:00

Namespaced Crypt\Random

This commit is contained in:
Clint Nelissen 2014-12-02 08:41:30 -08:00
parent bc3deb191f
commit c70702afbb
6 changed files with 29 additions and 24 deletions

View File

@ -67,12 +67,13 @@
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
use \phpseclib\Crypt\Random;
use \phpseclib\Math\BigInteger; use \phpseclib\Math\BigInteger;
/** /**
* Include Crypt_Random * Include Crypt\Random
*/ */
if (!class_exists('Crypt_Random')) { if (!class_exists('phpseclib\Crypt\Random')) {
include_once 'Random.php'; include_once 'Random.php';
} }
@ -794,7 +795,7 @@ class Crypt_RSA
$source.= pack('Na*', strlen($private), $private); $source.= pack('Na*', strlen($private), $private);
$hashkey = 'putty-private-key-file-mac-key'; $hashkey = 'putty-private-key-file-mac-key';
} else { } else {
$private.= Crypt_Random::crypt_random_string(16 - (strlen($private) & 15)); $private.= Random::crypt_random_string(16 - (strlen($private) & 15));
$source.= pack('Na*', strlen($private), $private); $source.= pack('Na*', strlen($private), $private);
if (!class_exists('Crypt_AES')) { if (!class_exists('Crypt_AES')) {
include_once 'Crypt/AES.php'; include_once 'Crypt/AES.php';
@ -860,7 +861,7 @@ class Crypt_RSA
); );
$RSAPrivateKey = pack('Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey); $RSAPrivateKey = pack('Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey);
if (!empty($this->password) || is_string($this->password)) { if (!empty($this->password) || is_string($this->password)) {
$salt = Crypt_Random::crypt_random_string(8); $salt = Random::crypt_random_string(8);
$iterationCount = 2048; $iterationCount = 2048;
if (!class_exists('Crypt_DES')) { if (!class_exists('Crypt_DES')) {
@ -900,7 +901,7 @@ class Crypt_RSA
} }
if (!empty($this->password) || is_string($this->password)) { if (!empty($this->password) || is_string($this->password)) {
$iv = Crypt_Random::crypt_random_string(8); $iv = Random::crypt_random_string(8);
$symkey = pack('H*', md5($this->password . $iv)); // symkey is short for symmetric key $symkey = pack('H*', md5($this->password . $iv)); // symkey is short for symmetric key
$symkey.= substr(pack('H*', md5($symkey . $this->password . $iv)), 0, 8); $symkey.= substr(pack('H*', md5($symkey . $this->password . $iv)), 0, 8);
if (!class_exists('Crypt_TripleDES')) { if (!class_exists('Crypt_TripleDES')) {
@ -2302,7 +2303,7 @@ class Crypt_RSA
$lHash = $this->hash->hash($l); $lHash = $this->hash->hash($l);
$ps = str_repeat(chr(0), $this->k - $mLen - 2 * $this->hLen - 2); $ps = str_repeat(chr(0), $this->k - $mLen - 2 * $this->hLen - 2);
$db = $lHash . $ps . chr(1) . $m; $db = $lHash . $ps . chr(1) . $m;
$seed = Crypt_Random::crypt_random_string($this->hLen); $seed = Random::crypt_random_string($this->hLen);
$dbMask = $this->_mgf1($seed, $this->k - $this->hLen - 1); $dbMask = $this->_mgf1($seed, $this->k - $this->hLen - 1);
$maskedDB = $db ^ $dbMask; $maskedDB = $db ^ $dbMask;
$seedMask = $this->_mgf1($maskedDB, $this->hLen); $seedMask = $this->_mgf1($maskedDB, $this->hLen);
@ -2420,7 +2421,7 @@ class Crypt_RSA
$psLen = $this->k - $mLen - 3; $psLen = $this->k - $mLen - 3;
$ps = ''; $ps = '';
while (strlen($ps) != $psLen) { while (strlen($ps) != $psLen) {
$temp = Crypt_Random::crypt_random_string($psLen - strlen($ps)); $temp = Random::crypt_random_string($psLen - strlen($ps));
$temp = str_replace("\x00", '', $temp); $temp = str_replace("\x00", '', $temp);
$ps.= $temp; $ps.= $temp;
} }
@ -2526,7 +2527,7 @@ class Crypt_RSA
return false; return false;
} }
$salt = Crypt_Random::crypt_random_string($sLen); $salt = Random::crypt_random_string($sLen);
$m2 = "\0\0\0\0\0\0\0\0" . $mHash . $salt; $m2 = "\0\0\0\0\0\0\0\0" . $mHash . $salt;
$h = $this->hash->hash($m2); $h = $this->hash->hash($m2);
$ps = str_repeat(chr(0), $emLen - $sLen - $this->hLen - 2); $ps = str_repeat(chr(0), $emLen - $sLen - $this->hLen - 2);

View File

@ -36,21 +36,23 @@
* THE SOFTWARE. * THE SOFTWARE.
* *
* @category Crypt * @category Crypt
* @package Crypt_Random * @package Random
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVII Jim Wigginton * @copyright MMVII Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License * @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
namespace phpseclib\Crypt;
/** /**
* Pure-PHP Random Number Generator * Pure-PHP Random Number Generator
* *
* @package Crypt_Random * @package Random
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @access public * @access public
*/ */
class Crypt_Random class Random
{ {
/** /**
* Generate a random string. * Generate a random string.

View File

@ -3475,7 +3475,7 @@ class File_X509
// "A challenge string that is submitted along with the public key. Defaults to an empty string if not specified." // "A challenge string that is submitted along with the public key. Defaults to an empty string if not specified."
// both Firefox and OpenSSL ("openssl spkac -key private.key") behave this way // both Firefox and OpenSSL ("openssl spkac -key private.key") behave this way
// we could alternatively do this instead if we ignored the specs: // we could alternatively do this instead if we ignored the specs:
// Crypt_Random::crypt_random_string(8) & str_repeat("\x7F", 8) // Random::crypt_random_string(8) & str_repeat("\x7F", 8)
'challenge' => !empty($this->challenge) ? $this->challenge : '' 'challenge' => !empty($this->challenge) ? $this->challenge : ''
), ),
'signatureAlgorithm' => array('algorithm' => $signatureAlgorithm), 'signatureAlgorithm' => array('algorithm' => $signatureAlgorithm),

View File

@ -68,7 +68,7 @@
namespace phpseclib\Math; namespace phpseclib\Math;
use Crypt_Random; use \phpseclib\Crypt\Random;
/**#@+ /**#@+
* Reduction constants * Reduction constants
@ -3023,7 +3023,7 @@ class BigInteger
/** /**
* Generates a random BigInteger * Generates a random BigInteger
* *
* Byte length is equal to $length. Uses Crypt_Random if it's loaded and mt_rand if it's not. * Byte length is equal to $length. Uses Crypt\Random if it's loaded and mt_rand if it's not.
* *
* @param Integer $length * @param Integer $length
* @return \phpseclib\Math\BigInteger * @return \phpseclib\Math\BigInteger
@ -3031,8 +3031,8 @@ class BigInteger
*/ */
function _random_number_helper($size) function _random_number_helper($size)
{ {
if (class_exists('Crypt_Random')) { if (class_exists('phpseclib\Crypt\Random')) {
$random = Crypt_Random::crypt_random_string($size); $random = Random::crypt_random_string($size);
} else { } else {
$random = ''; $random = '';

View File

@ -64,6 +64,7 @@
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
use \phpseclib\Crypt\Random;
use \phpseclib\Math\BigInteger; use \phpseclib\Math\BigInteger;
/**#@+ /**#@+
@ -519,8 +520,8 @@ class Net_SSH1
*/ */
function __construct($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES) function __construct($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES)
{ {
// Include Crypt_Random // Include Crypt\Random
if (!class_exists('Crypt_Random')) { if (!class_exists('phpseclib\Crypt\Random')) {
include_once 'Crypt/Random.php'; include_once 'Crypt/Random.php';
} }
@ -631,7 +632,7 @@ class Net_SSH1
$session_id = pack('H*', md5($host_key_public_modulus->toBytes() . $server_key_public_modulus->toBytes() . $anti_spoofing_cookie)); $session_id = pack('H*', md5($host_key_public_modulus->toBytes() . $server_key_public_modulus->toBytes() . $anti_spoofing_cookie));
$session_key = Crypt_Random::crypt_random_string(32); $session_key = Random::crypt_random_string(32);
$double_encrypted_session_key = $session_key ^ str_pad($session_id, 32, chr(0)); $double_encrypted_session_key = $session_key ^ str_pad($session_id, 32, chr(0));
if ($server_key_public_modulus->compare($host_key_public_modulus) < 0) { if ($server_key_public_modulus->compare($host_key_public_modulus) < 0) {
@ -1179,7 +1180,7 @@ class Net_SSH1
$length = strlen($data) + 4; $length = strlen($data) + 4;
$padding = Crypt_Random::crypt_random_string(8 - ($length & 7)); $padding = Random::crypt_random_string(8 - ($length & 7));
$orig = $data; $orig = $data;
$data = $padding . $data; $data = $padding . $data;
@ -1365,7 +1366,7 @@ class Net_SSH1
$length = strlen($modulus) - strlen($m) - 3; $length = strlen($modulus) - strlen($m) - 3;
$random = ''; $random = '';
while (strlen($random) != $length) { while (strlen($random) != $length) {
$block = Crypt_Random::crypt_random_string($length - strlen($random)); $block = Random::crypt_random_string($length - strlen($random));
$block = str_replace("\x00", '', $block); $block = str_replace("\x00", '', $block);
$random.= $block; $random.= $block;
} }

View File

@ -66,6 +66,7 @@
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
use \phpseclib\Crypt\Random;
// Used to do Diffie-Hellman key exchange and DSA/RSA signature verification. // Used to do Diffie-Hellman key exchange and DSA/RSA signature verification.
use \phpseclib\Math\BigInteger; use \phpseclib\Math\BigInteger;
@ -855,7 +856,7 @@ class Net_SSH2
*/ */
function __construct($host, $port = 22, $timeout = 10) function __construct($host, $port = 22, $timeout = 10)
{ {
if (!class_exists('Crypt_Random')) { if (!class_exists('phpseclib\Crypt\Random')) {
include_once 'Crypt/Random.php'; include_once 'Crypt/Random.php';
} }
@ -1202,7 +1203,7 @@ class Net_SSH2
$compression_algorithms_server_to_client = $compression_algorithms_client_to_server = implode(',', $compression_algorithms); $compression_algorithms_server_to_client = $compression_algorithms_client_to_server = implode(',', $compression_algorithms);
} }
$client_cookie = Crypt_Random::crypt_random_string(16); $client_cookie = Random::crypt_random_string(16);
$response = $kexinit_payload_server; $response = $kexinit_payload_server;
$this->_string_shift($response, 1); // skip past the message number (it should be SSH_MSG_KEXINIT) $this->_string_shift($response, 1); // skip past the message number (it should be SSH_MSG_KEXINIT)
@ -3154,7 +3155,7 @@ class Net_SSH2
$packet_length+= (($this->encrypt_block_size - 1) * $packet_length) % $this->encrypt_block_size; $packet_length+= (($this->encrypt_block_size - 1) * $packet_length) % $this->encrypt_block_size;
// subtracting strlen($data) is obvious - subtracting 5 is necessary because of packet_length and padding_length // subtracting strlen($data) is obvious - subtracting 5 is necessary because of packet_length and padding_length
$padding_length = $packet_length - strlen($data) - 5; $padding_length = $packet_length - strlen($data) - 5;
$padding = Crypt_Random::crypt_random_string($padding_length); $padding = Random::crypt_random_string($padding_length);
// we subtract 4 from packet_length because the packet_length field isn't supposed to include itself // we subtract 4 from packet_length because the packet_length field isn't supposed to include itself
$packet = pack('NCa*', $packet_length - 4, $padding_length, $data . $padding); $packet = pack('NCa*', $packet_length - 4, $padding_length, $data . $padding);