1
0
mirror of https://github.com/danog/tgseclib.git synced 2025-01-22 05:51:20 +01:00

SymmetricKey: isValidKey took ints instead of strings

This commit is contained in:
terrafrost 2017-08-07 23:20:23 -05:00
parent e7e30cd239
commit 9d8f014748
9 changed files with 50 additions and 27 deletions

View File

@ -327,10 +327,10 @@ class Blowfish extends BlockCipher
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::isValidEngine() * @see \phpseclib\Crypt\Common\SymmetricKey::isValidEngine()
* @param int $engine * @param int $engine
* @access public * @access protected
* @return bool * @return bool
*/ */
public function isValidEngine($engine) protected function isValidEngineHelper($engine)
{ {
if ($engine == self::ENGINE_OPENSSL) { if ($engine == self::ENGINE_OPENSSL) {
if (version_compare(PHP_VERSION, '5.3.7') < 0 && $this->key_length != 16) { if (version_compare(PHP_VERSION, '5.3.7') < 0 && $this->key_length != 16) {
@ -343,7 +343,7 @@ class Blowfish extends BlockCipher
$this->cipher_name_openssl = 'bf-' . $this->openssl_translate_mode(); $this->cipher_name_openssl = 'bf-' . $this->openssl_translate_mode();
} }
return parent::isValidEngine($engine); return parent::isValidEngineHelper($engine);
} }
/** /**

View File

@ -1712,10 +1712,10 @@ abstract class SymmetricKey
* *
* @see self::__construct() * @see self::__construct()
* @param int $engine * @param int $engine
* @access public * @access private
* @return bool * @return bool
*/ */
public function isValidEngine($engine) protected function isValidEngineHelper($engine)
{ {
switch ($engine) { switch ($engine) {
case self::ENGINE_OPENSSL: case self::ENGINE_OPENSSL:
@ -1756,6 +1756,29 @@ abstract class SymmetricKey
return false; return false;
} }
/**
* Test for engine validity
*
* @see self::__construct()
* @param string $engine
* @access public
* @return bool
*/
public function isValidEngine($engine)
{
static $reverseMap;
if (!isset($reverseMap)) {
$reverseMap = array_map('strtolower', self::ENGINE_MAP);
$reverseMap = array_flip($reverseMap);
}
$engine = strtolower($engine);
if (!isset($reverseMap[$engine])) {
return false;
}
return $this->isValidEngineHelper($reverseMap[$engine]);
}
/** /**
* Sets the preferred crypt engine * Sets the preferred crypt engine
* *
@ -1816,7 +1839,7 @@ abstract class SymmetricKey
self::ENGINE_EVAL self::ENGINE_EVAL
]; ];
foreach ($candidateEngines as $engine) { foreach ($candidateEngines as $engine) {
if ($this->isValidEngine($engine)) { if ($this->isValidEngineHelper($engine)) {
$this->engine = $engine; $this->engine = $engine;
break; break;
} }

View File

@ -603,10 +603,10 @@ class DES extends BlockCipher
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::isValidEngine() * @see \phpseclib\Crypt\Common\SymmetricKey::isValidEngine()
* @param int $engine * @param int $engine
* @access public * @access protected
* @return bool * @return bool
*/ */
public function isValidEngine($engine) protected function isValidEngineHelper($engine)
{ {
if ($this->key_length_max == 8) { if ($this->key_length_max == 8) {
if ($engine == self::ENGINE_OPENSSL) { if ($engine == self::ENGINE_OPENSSL) {
@ -615,7 +615,7 @@ class DES extends BlockCipher
} }
} }
return parent::isValidEngine($engine); return parent::isValidEngineHelper($engine);
} }
/** /**

View File

@ -284,10 +284,10 @@ class RC2 extends BlockCipher
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::__construct() * @see \phpseclib\Crypt\Common\SymmetricKey::__construct()
* @param int $engine * @param int $engine
* @access public * @access protected
* @return bool * @return bool
*/ */
public function isValidEngine($engine) protected function isValidEngineHelper($engine)
{ {
switch ($engine) { switch ($engine) {
case self::ENGINE_OPENSSL: case self::ENGINE_OPENSSL:
@ -298,7 +298,7 @@ class RC2 extends BlockCipher
$this->cipher_name_openssl = 'rc2-' . $this->openssl_translate_mode(); $this->cipher_name_openssl = 'rc2-' . $this->openssl_translate_mode();
} }
return parent::isValidEngine($engine); return parent::isValidEngineHelper($engine);
} }
/** /**

View File

@ -139,10 +139,10 @@ class RC4 extends StreamCipher
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::__construct() * @see \phpseclib\Crypt\Common\SymmetricKey::__construct()
* @param int $engine * @param int $engine
* @access public * @access protected
* @return bool * @return bool
*/ */
public function isValidEngine($engine) protected function isValidEngineHelper($engine)
{ {
if ($engine == self::ENGINE_OPENSSL) { if ($engine == self::ENGINE_OPENSSL) {
if (version_compare(PHP_VERSION, '5.3.7') >= 0) { if (version_compare(PHP_VERSION, '5.3.7') >= 0) {
@ -164,7 +164,7 @@ class RC4 extends StreamCipher
} }
} }
return parent::isValidEngine($engine); return parent::isValidEngineHelper($engine);
} }
/** /**

View File

@ -273,10 +273,10 @@ class Rijndael extends BlockCipher
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::__construct() * @see \phpseclib\Crypt\Common\SymmetricKey::__construct()
* @param int $engine * @param int $engine
* @access public * @access protected
* @return bool * @return bool
*/ */
public function isValidEngine($engine) protected function isValidEngineHelper($engine)
{ {
switch ($engine) { switch ($engine) {
case self::ENGINE_OPENSSL: case self::ENGINE_OPENSSL:
@ -294,7 +294,7 @@ class Rijndael extends BlockCipher
} }
} }
return parent::isValidEngine($engine); return parent::isValidEngineHelper($engine);
} }
/** /**

View File

@ -178,10 +178,10 @@ class TripleDES extends DES
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::__construct() * @see \phpseclib\Crypt\Common\SymmetricKey::__construct()
* @param int $engine * @param int $engine
* @access public * @access protected
* @return bool * @return bool
*/ */
public function isValidEngine($engine) protected function isValidEngineHelper($engine)
{ {
if ($engine == self::ENGINE_OPENSSL) { if ($engine == self::ENGINE_OPENSSL) {
$this->cipher_name_openssl_ecb = 'des-ede3'; $this->cipher_name_openssl_ecb = 'des-ede3';
@ -189,7 +189,7 @@ class TripleDES extends DES
$this->cipher_name_openssl = $mode == 'ecb' ? 'des-ede3' : 'des-ede3-' . $mode; $this->cipher_name_openssl = $mode == 'ecb' ? 'des-ede3' : 'des-ede3-' . $mode;
} }
return parent::isValidEngine($engine); return parent::isValidEngineHelper($engine);
} }
/** /**

View File

@ -215,20 +215,20 @@ class Unit_Crypt_RC4Test extends PhpseclibTestCase
{ {
$objects = $engines = array(); $objects = $engines = array();
$temp = new RC4(RC4::MODE_CTR); $temp = new RC4(RC4::MODE_CTR);
$temp->setPreferredEngine(RC4::ENGINE_INTERNAL); $temp->setPreferredEngine('internal');
$objects[] = $temp; $objects[] = $temp;
$engines[] = 'internal'; $engines[] = 'internal';
if ($temp->isValidEngine(RC4::ENGINE_MCRYPT)) { if ($temp->isValidEngine('mcrypt')) {
$temp = new RC4(RC4::MODE_CTR); $temp = new RC4(RC4::MODE_CTR);
$temp->setPreferredEngine(RC4::ENGINE_MCRYPT); $temp->setPreferredEngine('mcrypt');
$objects[] = $temp; $objects[] = $temp;
$engines[] = 'mcrypt'; $engines[] = 'mcrypt';
} }
if ($temp->isValidEngine(RC4::ENGINE_OPENSSL)) { if ($temp->isValidEngine('openssl')) {
$temp = new RC4(RC4::MODE_CTR); $temp = new RC4(RC4::MODE_CTR);
$temp->setPreferredEngine(RC4::ENGINE_OPENSSL); $temp->setPreferredEngine('openssl');
$objects[] = $temp; $objects[] = $temp;
$engines[] = 'OpenSSL'; $engines[] = 'OpenSSL';
} }

View File

@ -167,7 +167,7 @@ class Unit_Crypt_TripleDESTest extends PhpseclibTestCase
$des->disablePadding(); $des->disablePadding();
$result = $des->encrypt($plaintext); $result = $des->encrypt($plaintext);
$plaintext = bin2hex($plaintext); $plaintext = bin2hex($plaintext);
$this->assertEquals($result, $expected, "Failed asserting that $plaintext yielded expected output in $engin engine"); $this->assertEquals($result, $expected, "Failed asserting that $plaintext yielded expected output in $engine engine");
} }
public function testInnerChaining() public function testInnerChaining()