Merge branch 'openssl-support' of https://github.com/terrafrost/phpseclib into openssl-support

This commit is contained in:
terrafrost 2015-04-02 05:58:58 -05:00
commit 87e0f05ada
6 changed files with 28 additions and 31 deletions

View File

@ -53,10 +53,9 @@
*/ */
/**#@+ /**#@+
* @access private * @access public
* @see Crypt_Base::encrypt() * @see Crypt_Base::encrypt()
* @see Crypt_Base::decrypt() * @see Crypt_Base::decrypt()
* @internal This constants are for internal use only
*/ */
/** /**
* Encrypt / decrypt using the Counter mode. * Encrypt / decrypt using the Counter mode.
@ -100,7 +99,7 @@ define('CRYPT_MODE_STREAM', 5);
/**#@+ /**#@+
* @access private * @access private
* @see Crypt_Base::Crypt_Base() * @see Crypt_Base::Crypt_Base()
* @internal This constants are for internal use only * @internal These constants are for internal use only
*/ */
/** /**
* Base value for the internal implementation $engine switch * Base value for the internal implementation $engine switch
@ -979,7 +978,8 @@ class Crypt_Base
function decrypt($ciphertext) function decrypt($ciphertext)
{ {
if ($this->paddable) { if ($this->paddable) {
// we pad with chr(0) since that's what mcrypt_generic does [...] // we pad with chr(0) since that's what mcrypt_generic does. to quote from {@link http://www.php.net/function.mcrypt-generic}:
// "The data is padded with "\0" to make sure the length of the data is n * blocksize."
$ciphertext = str_pad($ciphertext, strlen($ciphertext) + ($this->block_size - strlen($ciphertext) % $this->block_size) % $this->block_size, chr(0)); $ciphertext = str_pad($ciphertext, strlen($ciphertext) + ($this->block_size - strlen($ciphertext) % $this->block_size) % $this->block_size, chr(0));
} }
@ -1112,12 +1112,6 @@ class Crypt_Base
return $plaintext; return $plaintext;
} }
if ($this->paddable) {
// we pad with chr(0) since that's what mcrypt_generic does. to quote from {@link http://www.php.net/function.mcrypt-generic}:
// "The data is padded with "\0" to make sure the length of the data is n * blocksize."
$ciphertext = str_pad($ciphertext, strlen($ciphertext) + ($block_size - strlen($ciphertext) % $block_size) % $block_size, chr(0));
}
$plaintext = mdecrypt_generic($this->demcrypt, $ciphertext); $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
if (!$this->continuousBuffer) { if (!$this->continuousBuffer) {
@ -1587,6 +1581,8 @@ class Crypt_Base
case CRYPT_ENGINE_INTERNAL: case CRYPT_ENGINE_INTERNAL:
return true; return true;
} }
return false;
} }
/** /**
@ -1640,18 +1636,21 @@ class Crypt_Base
*/ */
function _setEngine() function _setEngine()
{ {
switch (true) { $this->engine = null;
case $this->isValidEngine($this->preferredEngine):
$this->engine = $this->preferredEngine; $candidateEngines = array(
$this->preferredEngine,
CRYPT_ENGINE_OPENSSL,
CRYPT_ENGINE_MCRYPT
);
foreach ($candidateEngines as $engine) {
if ($this->isValidEngine($engine)) {
$this->engine = $engine;
break; break;
case $this->isValidEngine(CRYPT_ENGINE_OPENSSL): }
$this->engine = CRYPT_ENGINE_OPENSSL; }
break; if (!$this->engine) {
case $this->isValidEngine(CRYPT_ENGINE_MCRYPT): $this->engine = CRYPT_ENGINE_INTERNAL;
$this->engine = CRYPT_ENGINE_MCRYPT;
break;
default:
$this->engine = CRYPT_ENGINE_INTERNAL;
} }
if ($this->engine != CRYPT_ENGINE_MCRYPT && $this->enmcrypt) { if ($this->engine != CRYPT_ENGINE_MCRYPT && $this->enmcrypt) {
@ -1732,7 +1731,7 @@ class Crypt_Base
* @see setIV() * @see setIV()
* @see disableContinuousBuffer() * @see disableContinuousBuffer()
* @access private * @access private
* @internal _setup() is called always before(!) en/decryption. * @internal _setup() is always called before en/decryption.
* @internal Could, but not must, extend by the child Crypt_* class * @internal Could, but not must, extend by the child Crypt_* class
*/ */
function _setup() function _setup()
@ -2512,14 +2511,12 @@ class Crypt_Base
* @param $bytes * @param $bytes
* @return String * @return String
*/ */
function _trapdoor($bytes) function _hashInlineCryptFunction($bytes)
{ {
if (!defined('CRYPT_BASE_WHIRLPOOL_AVAILABLE')) { if (!defined('CRYPT_BASE_WHIRLPOOL_AVAILABLE')) {
define('CRYPT_BASE_WHIRLPOOL_AVAILABLE', (bool)(extension_loaded('hash') && in_array('whirlpool', hash_algos()))); define('CRYPT_BASE_WHIRLPOOL_AVAILABLE', (bool)(extension_loaded('hash') && in_array('whirlpool', hash_algos())));
} }
// return pack('H*', md5($bytes) . sha1($bytes) . (CRYPT_BASE_WHIRLPOOL_AVAILABLE ? hash('whirlpool', $bytes) : '')); // Alternative
$result = ''; $result = '';
$hash = $bytes; $hash = $bytes;

View File

@ -546,10 +546,10 @@ class Crypt_Blowfish extends Crypt_Base
// After that, we'll still create very fast optimized code but not the hi-ultimative code, for each $mode one. // After that, we'll still create very fast optimized code but not the hi-ultimative code, for each $mode one.
$gen_hi_opt_code = (bool)( count($lambda_functions) < 10 ); $gen_hi_opt_code = (bool)( count($lambda_functions) < 10 );
// Generation of a uniqe hash for our generated code // Generation of a unique hash for our generated code
$code_hash = "Crypt_Blowfish, {$this->mode}"; $code_hash = "Crypt_Blowfish, {$this->mode}";
if ($gen_hi_opt_code) { if ($gen_hi_opt_code) {
$code_hash = str_pad($code_hash, 32) . $this->_trapdoor($this->key); $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key);
} }
if (!isset($lambda_functions[$code_hash])) { if (!isset($lambda_functions[$code_hash])) {

View File

@ -1394,7 +1394,7 @@ class Crypt_DES extends Crypt_Base
// After max 10 hi-optimized functions, we create generic // After max 10 hi-optimized functions, we create generic
// (still very fast.. but not ultra) functions for each $mode/$des_rounds // (still very fast.. but not ultra) functions for each $mode/$des_rounds
// Currently 2 * 5 generic functions will be then max. possible. // Currently 2 * 5 generic functions will be then max. possible.
$code_hash = str_pad($code_hash, 32) . $this->_trapdoor($this->key); $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key);
} }
// Is there a re-usable $lambda_functions in there? If not, we have to create it. // Is there a re-usable $lambda_functions in there? If not, we have to create it.

View File

@ -642,7 +642,7 @@ class Crypt_RC2 extends Crypt_Base
// Generation of a uniqe hash for our generated code // Generation of a uniqe hash for our generated code
$code_hash = "Crypt_RC2, {$this->mode}"; $code_hash = "Crypt_RC2, {$this->mode}";
if ($gen_hi_opt_code) { if ($gen_hi_opt_code) {
$code_hash = str_pad($code_hash, 32) . $this->_trapdoor($this->key); $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key);
} }
// Is there a re-usable $lambda_functions in there? // Is there a re-usable $lambda_functions in there?

View File

@ -960,7 +960,7 @@ class Crypt_Rijndael extends Crypt_Base
// Generation of a uniqe hash for our generated code // Generation of a uniqe hash for our generated code
$code_hash = "Crypt_Rijndael, {$this->mode}, {$this->Nr}, {$this->Nb}"; $code_hash = "Crypt_Rijndael, {$this->mode}, {$this->Nr}, {$this->Nb}";
if ($gen_hi_opt_code) { if ($gen_hi_opt_code) {
$code_hash = str_pad($code_hash, 32) . $this->_trapdoor($this->key); $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key);
} }
if (!isset($lambda_functions[$code_hash])) { if (!isset($lambda_functions[$code_hash])) {

View File

@ -746,7 +746,7 @@ class Crypt_Twofish extends Crypt_Base
// Generation of a uniqe hash for our generated code // Generation of a uniqe hash for our generated code
$code_hash = "Crypt_Twofish, {$this->mode}"; $code_hash = "Crypt_Twofish, {$this->mode}";
if ($gen_hi_opt_code) { if ($gen_hi_opt_code) {
$code_hash = str_pad($code_hash, 32) . $this->_trapdoor($this->key); $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key);
} }
if (!isset($lambda_functions[$code_hash])) { if (!isset($lambda_functions[$code_hash])) {