From bfba3db1a70dc234b87fbca7d382607ae378fed1 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Thu, 17 Sep 2015 10:46:45 -0500 Subject: [PATCH 1/5] Crypt: make it so the key length can be explicitly set for all ciphers --- phpseclib/Crypt/Base.php | 55 +++++++++++++++++- phpseclib/Crypt/Blowfish.php | 52 ++++++++--------- phpseclib/Crypt/DES.php | 17 +----- phpseclib/Crypt/RC2.php | 32 +++++++++-- phpseclib/Crypt/RC4.php | 27 +++++---- phpseclib/Crypt/Rijndael.php | 92 +++---------------------------- phpseclib/Crypt/TripleDES.php | 36 ++++++++++-- phpseclib/Crypt/Twofish.php | 32 ++++------- tests/Unit/Crypt/AES/TestCase.php | 35 ++++++++++++ 9 files changed, 208 insertions(+), 170 deletions(-) diff --git a/phpseclib/Crypt/Base.php b/phpseclib/Crypt/Base.php index 9566f288..e3840e00 100644 --- a/phpseclib/Crypt/Base.php +++ b/phpseclib/Crypt/Base.php @@ -470,6 +470,24 @@ class Crypt_Base */ var $openssl_options; + /** + * Has the key length explicitly been set or should it be derived from the key, itself? + * + * @see setKeyLength() + * @var bool + * @access private + */ + var $explicit_key_length = false; + + /** + * Don't truncate / null pad key + * + * @see Crypt_Base::_clearBuffers + * @var bool + * @access private + */ + var $skip_key_adjustment = false; + /** * Default Constructor. * @@ -542,6 +560,32 @@ class Crypt_Base $this->changed = true; } + /** + * Sets the key length. + * + * Keys with explicitly set lengths need to be treated accordingly + * + * @access public + * @param int $length + */ + function setKeyLength($length) + { + $this->explicit_key_length = true; + $this->changed = true; + $this->_setEngine(); + } + + /** + * Returns the current key length + * + * @access public + * @return int + */ + function getKeyLength() + { + return $this->key_size << 3; + } + /** * Sets the key. * @@ -558,6 +602,11 @@ class Crypt_Base */ function setKey($key) { + if (!$this->explicit_key_length) { + $this->setKeyLength(strlen($key) << 3); + $this->explicit_key_length = false; + } + $this->key = $key; $this->changed = true; $this->_setEngine(); @@ -601,7 +650,7 @@ class Crypt_Base if (isset($func_args[5])) { $dkLen = $func_args[5]; } else { - $dkLen = $method == 'pbkdf1' ? 2 * $this->password_key_size : $this->password_key_size; + $dkLen = $method == 'pbkdf1' ? 2 * $this->key_size : $this->key_size; } switch (true) { @@ -1873,6 +1922,10 @@ class Crypt_Base // mcrypt's handling of invalid's $iv: // $this->encryptIV = $this->decryptIV = strlen($this->iv) == $this->block_size ? $this->iv : str_repeat("\0", $this->block_size); $this->encryptIV = $this->decryptIV = str_pad(substr($this->iv, 0, $this->block_size), $this->block_size, "\0"); + + if (!$this->skip_key_adjustment) { + $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, "\0"); + } } /** diff --git a/phpseclib/Crypt/Blowfish.php b/phpseclib/Crypt/Blowfish.php index 943e4476..c5db8484 100644 --- a/phpseclib/Crypt/Blowfish.php +++ b/phpseclib/Crypt/Blowfish.php @@ -120,16 +120,6 @@ class Crypt_Blowfish extends Crypt_Base */ var $block_size = 8; - /** - * The default password key_size used by setPassword() - * - * @see Crypt_Base::password_key_size - * @see Crypt_Base::setPassword() - * @var int - * @access private - */ - var $password_key_size = 56; - /** * The namespace used by the cipher for its constants. * @@ -354,31 +344,37 @@ class Crypt_Blowfish extends Crypt_Base var $kl; /** - * Sets the key. + * The Key Length * - * Keys can be of any length. Blowfish, itself, requires the use of a key between 32 and max. 448-bits long. - * If the key is less than 32-bits we NOT fill the key to 32bit but let the key as it is to be compatible - * with mcrypt because mcrypt act this way with blowfish key's < 32 bits. + * @see setKeyLength() + * @var int + * @access private + * @internal The max value is 256 / 8 = 32, the min value is 128 / 8 = 16. Exists in conjunction with $Nk + * because the encryption / decryption / key schedule creation requires this number and not $key_size. We could + * derive this from $key_size or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu + * of that, we'll just precompute it once. + */ + var $key_size = 16; + + /** + * Sets the key length. * - * If the key is more than 448-bits, we trim the excess bits. - * - * If the key is not explicitly set, or empty, it'll be assumed a 128 bits key to be all null bytes. + * Key lengths can be between 32 and 448 bits. * * @access public - * @see Crypt_Base::setKey() - * @param string $key + * @param int $length */ - function setKey($key) + function setKeyLength($length) { - $keylength = strlen($key); - - if (!$keylength) { - $key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; - } elseif ($keylength > 56) { - $key = substr($key, 0, 56); + if ($length < 32) { + $this->key_size = 7; + } elseif ($length > 448) { + $this->key_size = 56; + } else { + $this->key_size = $length >> 3; } - parent::setKey($key); + parent::setKeyLength($length); } /** @@ -394,7 +390,7 @@ class Crypt_Blowfish extends Crypt_Base function isValidEngine($engine) { if ($engine == CRYPT_ENGINE_OPENSSL) { - if (strlen($this->key) != 16) { + if ($this->key_size != 16) { return false; } $this->cipher_name_openssl_ecb = 'bf-ecb'; diff --git a/phpseclib/Crypt/DES.php b/phpseclib/Crypt/DES.php index 8ac1fe6b..98fd9394 100644 --- a/phpseclib/Crypt/DES.php +++ b/phpseclib/Crypt/DES.php @@ -140,24 +140,13 @@ class Crypt_DES extends Crypt_Base var $block_size = 8; /** - * The Key + * Key Length * - * @see Crypt_Base::key - * @see setKey() - * @var string - * @access private - */ - var $key = "\0\0\0\0\0\0\0\0"; - - /** - * The default password key_size used by setPassword() - * - * @see Crypt_Base::password_key_size - * @see Crypt_Base::setPassword() + * @see Crypt_Base::setKeyLength() * @var int * @access private */ - var $password_key_size = 8; + var $key_size = 8; /** * The namespace used by the cipher for its constants. diff --git a/phpseclib/Crypt/RC2.php b/phpseclib/Crypt/RC2.php index eb41fe8f..cda4f0d7 100644 --- a/phpseclib/Crypt/RC2.php +++ b/phpseclib/Crypt/RC2.php @@ -139,14 +139,22 @@ class Crypt_RC2 extends Crypt_Base var $orig_key; /** - * The default password key_size used by setPassword() + * Don't truncate / null pad key * - * @see Crypt_Base::password_key_size - * @see Crypt_Base::setPassword() + * @see Crypt_Base::_clearBuffers + * @var bool + * @access private + */ + var $skip_key_adjustment = true; + + /** + * Key Length + * + * @see Crypt_RC2::setKeyLength() * @var int * @access private */ - var $password_key_size = 16; // = 128 bits + var $key_size = 16; // = 128 bits /** * The namespace used by the cipher for its constants. @@ -366,7 +374,7 @@ class Crypt_RC2 extends Crypt_Base { switch ($engine) { case CRYPT_ENGINE_OPENSSL: - if ($this->current_key_length != 128 || strlen($this->orig_key) != 16) { + if ($this->current_key_length != 128 || strlen($this->orig_key) < 16) { return false; } $this->cipher_name_openssl_ecb = 'rc2-ecb'; @@ -377,7 +385,7 @@ class Crypt_RC2 extends Crypt_Base } /** - * Sets the key length + * Sets the key length. * * Valid key lengths are 1 to 1024. * Calling this function after setting the key has no effect until the next @@ -393,6 +401,17 @@ class Crypt_RC2 extends Crypt_Base } } + /** + * Returns the current key length + * + * @access public + * @return int + */ + function getKeyLength() + { + return $this->current_key_length; + } + /** * Sets the key. * @@ -448,6 +467,7 @@ class Crypt_RC2 extends Crypt_Base // Prepare the key for mcrypt. $l[0] = $this->invpitable[$l[0]]; array_unshift($l, 'C*'); + parent::setKey(call_user_func_array('pack', $l)); } diff --git a/phpseclib/Crypt/RC4.php b/phpseclib/Crypt/RC4.php index 8f21cff0..3ad16836 100644 --- a/phpseclib/Crypt/RC4.php +++ b/phpseclib/Crypt/RC4.php @@ -99,14 +99,13 @@ class Crypt_RC4 extends Crypt_Base var $block_size = 0; /** - * The default password key_size used by setPassword() + * Key Length * - * @see Crypt_Base::password_key_size - * @see Crypt_Base::setPassword() + * @see Crypt_RC4::setKeyLength() * @var int * @access private */ - var $password_key_size = 128; // = 1024 bits + var $key_size = 128; // = 1024 bits /** * The namespace used by the cipher for its constants. @@ -223,18 +222,24 @@ class Crypt_RC4 extends Crypt_Base } /** - * Sets the key. + * Sets the key length * - * Keys can be between 1 and 256 bytes long. If they are longer then 256 bytes, the first 256 bytes will - * be used. If no key is explicitly set, it'll be assumed to be a single null byte. + * Keys can be between 1 and 256 bytes long. * * @access public - * @see Crypt_Base::setKey() - * @param string $key + * @param int $length */ - function setKey($key) + function setKeyLength($length) { - parent::setKey(substr($key, 0, 256)); + if ($length < 8) { + $this->key_size = 1; + } elseif ($length > 2048) { + $this->key_size = 248; + } else { + $this->key_size = $length >> 3; + } + + parent::setKeyLength($length); } /** diff --git a/phpseclib/Crypt/Rijndael.php b/phpseclib/Crypt/Rijndael.php index 17bbb864..7bd01903 100644 --- a/phpseclib/Crypt/Rijndael.php +++ b/phpseclib/Crypt/Rijndael.php @@ -127,16 +127,6 @@ define('CRYPT_RIJNDAEL_MODE_OFB', CRYPT_MODE_OFB); */ class Crypt_Rijndael extends Crypt_Base { - /** - * The default password key_size used by setPassword() - * - * @see Crypt_Base::password_key_size - * @see Crypt_Base::setPassword() - * @var int - * @access private - */ - var $password_key_size = 16; - /** * The namespace used by the cipher for its constants. * @@ -172,15 +162,6 @@ class Crypt_Rijndael extends Crypt_Base */ var $password_default_salt = 'phpseclib'; - /** - * Has the key length explicitly been set or should it be derived from the key, itself? - * - * @see setKeyLength() - * @var bool - * @access private - */ - var $explicit_key_length = false; - /** * The Key Schedule * @@ -289,48 +270,7 @@ class Crypt_Rijndael extends Crypt_Base } /** - * Sets the key. - * - * Keys can be of any length. Rijndael, itself, requires the use of a key that's between 128-bits and 256-bits long and - * whose length is a multiple of 32. If the key is less than 256-bits and the key length isn't set, we round the length - * up to the closest valid key length, padding $key with null bytes. If the key is more than 256-bits, we trim the - * excess bits. - * - * If the key is not explicitly set, it'll be assumed to be all null bytes. - * - * Note: 160/224-bit keys must explicitly set by setKeyLength(), otherwise they will be round/pad up to 192/256 bits. - * - * @see Crypt_Base:setKey() - * @see setKeyLength() - * @access public - * @param string $key - */ - function setKey($key) - { - if (!$this->explicit_key_length) { - $length = strlen($key); - switch (true) { - case $length <= 16: - $this->key_size = 16; - break; - case $length <= 20: - $this->key_size = 20; - break; - case $length <= 24: - $this->key_size = 24; - break; - case $length <= 28: - $this->key_size = 28; - break; - default: - $this->key_size = 32; - } - } - parent::setKey($key); - } - - /** - * Sets the key length + * Sets the key length. * * Valid key lengths are 128, 160, 192, 224, and 256. If the length is less than 128, it will be rounded up to * 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount. @@ -352,25 +292,23 @@ class Crypt_Rijndael extends Crypt_Base function setKeyLength($length) { switch (true) { - case $length == 160: - $this->key_size = 20; - break; - case $length == 224: - $this->key_size = 28; - break; case $length <= 128: $this->key_size = 16; break; + case $length <= 160: + $this->key_size = 20; + break; case $length <= 192: $this->key_size = 24; break; + case $length <= 224: + $this->key_size = 28; + break; default: $this->key_size = 32; } - $this->explicit_key_length = true; - $this->changed = true; - $this->_setEngine(); + parent::setKeyLength($length); } /** @@ -427,18 +365,6 @@ class Crypt_Rijndael extends Crypt_Base return parent::isValidEngine($engine); } - /** - * Setup the CRYPT_ENGINE_MCRYPT $engine - * - * @see Crypt_Base::_setupMcrypt() - * @access private - */ - function _setupMcrypt() - { - $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, "\0"); - parent::_setupMcrypt(); - } - /** * Encrypts a block * @@ -649,8 +575,6 @@ class Crypt_Rijndael extends Crypt_Base 0x7D000000, 0xFA000000, 0xEF000000, 0xC5000000, 0x91000000 ); - $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, "\0"); - if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->key_size === $this->kl['key_size'] && $this->block_size === $this->kl['block_size']) { // already expanded return; diff --git a/phpseclib/Crypt/TripleDES.php b/phpseclib/Crypt/TripleDES.php index 99a0a5b7..0bd68361 100644 --- a/phpseclib/Crypt/TripleDES.php +++ b/phpseclib/Crypt/TripleDES.php @@ -95,15 +95,13 @@ define('CRYPT_DES_MODE_CBC3', CRYPT_MODE_CBC3); class Crypt_TripleDES extends Crypt_DES { /** - * The default password key_size used by setPassword() + * Key Length * - * @see Crypt_DES::password_key_size - * @see Crypt_Base::password_key_size - * @see Crypt_Base::setPassword() + * @see Crypt_TripleDES::setKeyLength() * @var int * @access private */ - var $password_key_size = 24; + var $key_size = 24; /** * The default salt used by setPassword() @@ -266,6 +264,32 @@ class Crypt_TripleDES extends Crypt_DES } } + /** + * Sets the key length. + * + * Valid key lengths are 64, 128 and 192 + * + * @see Crypt_Rijndael:setKeyLength() + * @access public + * @param int $length + */ + function setKeyLength($length) + { + $length >>= 3; + switch (true) { + case $length <= 8: + $this->key_size = 8; + break; + case $length <= 16: + $this->key_size = 16; + break; + default: + $this->key_size = 24; + } + + parent::setKeyLength($length); + } + /** * Sets the key. * @@ -283,7 +307,7 @@ class Crypt_TripleDES extends Crypt_DES */ function setKey($key) { - $length = strlen($key); + $length = $this->explicit_key_length ? $this->key_size : strlen($key); if ($length > 8) { $key = str_pad(substr($key, 0, 24), 24, chr(0)); // if $key is between 64 and 128-bits, use the first 64-bits as the last, per this: diff --git a/phpseclib/Crypt/Twofish.php b/phpseclib/Crypt/Twofish.php index 07b43ed5..c332a3e5 100644 --- a/phpseclib/Crypt/Twofish.php +++ b/phpseclib/Crypt/Twofish.php @@ -433,35 +433,27 @@ class Crypt_Twofish extends Crypt_Base var $kl; /** - * Sets the key. + * Sets the key length. * - * Keys can be of any length. Twofish, itself, requires the use of a key that's 128, 192 or 256-bits long. - * If the key is less than 256-bits we round the length up to the closest valid key length, - * padding $key with null bytes. If the key is more than 256-bits, we trim the excess bits. - * - * If the key is not explicitly set, it'll be assumed a 128 bits key to be all null bytes. + * Valid key lengths are 128, 192 or 256 bits * * @access public - * @see Crypt_Base::setKey() - * @param string $key + * @param int $length */ - function setKey($key) + function setKeyLength($length) { - $keylength = strlen($key); switch (true) { - case $keylength <= 16: - $key = str_pad($key, 16, "\0"); + case $length <= 128: + $this->key_size = 16; break; - case $keylength <= 24: - $key = str_pad($key, 24, "\0"); + case $length <= 192: + $this->key_size = 24; break; - case $keylength < 32: - $key = str_pad($key, 32, "\0"); - break; - case $keylength > 32: - $key = substr($key, 0, 32); + default: + $this->key_size = 32; } - parent::setKey($key); + + parent::setKeyLength($length); } /** diff --git a/tests/Unit/Crypt/AES/TestCase.php b/tests/Unit/Crypt/AES/TestCase.php index 5a277c8b..c55e0bd5 100644 --- a/tests/Unit/Crypt/AES/TestCase.php +++ b/tests/Unit/Crypt/AES/TestCase.php @@ -334,4 +334,39 @@ abstract class Unit_Crypt_AES_TestCase extends PhpseclibTestCase $result = bin2hex($aes->encrypt(pack('H*', '91fbef2d15a97816060bee1feaa49afe'))); $this->assertSame($result, '1bc704f1bce135ceb810341b216d7abe'); } + + public function testGetKeyLengthDefault() + { + $aes = new Crypt_AES(); + $this->assertSame($aes->getKeyLength(), 128); + } + + public function testGetKeyLengthWith192BitKey() + { + $aes = new Crypt_AES(); + $aes->setKey(str_repeat('a', 24)); + $this->assertSame($aes->getKeyLength(), 192); + } + + public function testSetKeyLengthWithLargerKey() + { + $aes = new Crypt_AES(); + $aes->setKeyLength(128); + $aes->setKey(str_repeat('a', 24)); + $this->assertSame($aes->getKeyLength(), 128); + $ciphertext = bin2hex($aes->encrypt('a')); + $this->assertSame($ciphertext, '82b7b068dfc60ed2a46893b69fecd6c2'); + $this->assertSame($aes->getKeyLength(), 128); + } + + public function testSetKeyLengthWithSmallerKey() + { + $aes = new Crypt_AES(); + $aes->setKeyLength(256); + $aes->setKey(str_repeat('a', 16)); + $this->assertSame($aes->getKeyLength(), 256); + $ciphertext = bin2hex($aes->encrypt('a')); + $this->assertSame($ciphertext, 'fd4250c0d234aa7e1aa592820aa8406b'); + $this->assertSame($aes->getKeyLength(), 256); + } } From 86910352288cd2bd96af170eb3c766041e1e2c47 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Thu, 24 Sep 2015 09:29:00 -0500 Subject: [PATCH 2/5] Crypt/Base: add getBlockLength() method --- phpseclib/Crypt/Base.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/phpseclib/Crypt/Base.php b/phpseclib/Crypt/Base.php index e3840e00..7087c6ce 100644 --- a/phpseclib/Crypt/Base.php +++ b/phpseclib/Crypt/Base.php @@ -576,7 +576,7 @@ class Crypt_Base } /** - * Returns the current key length + * Returns the current key length in bits * * @access public * @return int @@ -586,6 +586,17 @@ class Crypt_Base return $this->key_size << 3; } + /** + * Returns the current block length in bits + * + * @access public + * @return int + */ + function getBlockLength() + { + return $this->block_size << 3; + } + /** * Sets the key. * From 27034825f367fd1bdb155d410c479f2aebcc014e Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 4 Oct 2015 16:29:48 -0500 Subject: [PATCH 3/5] cs changes per bantu --- phpseclib/Crypt/Base.php | 4 ++-- phpseclib/Crypt/Blowfish.php | 2 +- phpseclib/Crypt/RC2.php | 2 +- phpseclib/Crypt/TripleDES.php | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/phpseclib/Crypt/Base.php b/phpseclib/Crypt/Base.php index 7087c6ce..3e99a6b7 100644 --- a/phpseclib/Crypt/Base.php +++ b/phpseclib/Crypt/Base.php @@ -473,7 +473,7 @@ class Crypt_Base /** * Has the key length explicitly been set or should it be derived from the key, itself? * - * @see setKeyLength() + * @see Crypt_Base::setKeyLength() * @var bool * @access private */ @@ -482,7 +482,7 @@ class Crypt_Base /** * Don't truncate / null pad key * - * @see Crypt_Base::_clearBuffers + * @see Crypt_Base::_clearBuffers() * @var bool * @access private */ diff --git a/phpseclib/Crypt/Blowfish.php b/phpseclib/Crypt/Blowfish.php index c5db8484..8af33d40 100644 --- a/phpseclib/Crypt/Blowfish.php +++ b/phpseclib/Crypt/Blowfish.php @@ -346,7 +346,7 @@ class Crypt_Blowfish extends Crypt_Base /** * The Key Length * - * @see setKeyLength() + * @see Crypt_Base::setKeyLength() * @var int * @access private * @internal The max value is 256 / 8 = 32, the min value is 128 / 8 = 16. Exists in conjunction with $Nk diff --git a/phpseclib/Crypt/RC2.php b/phpseclib/Crypt/RC2.php index cda4f0d7..121a426a 100644 --- a/phpseclib/Crypt/RC2.php +++ b/phpseclib/Crypt/RC2.php @@ -141,7 +141,7 @@ class Crypt_RC2 extends Crypt_Base /** * Don't truncate / null pad key * - * @see Crypt_Base::_clearBuffers + * @see Crypt_Base::_clearBuffers() * @var bool * @access private */ diff --git a/phpseclib/Crypt/TripleDES.php b/phpseclib/Crypt/TripleDES.php index 0bd68361..70c5f250 100644 --- a/phpseclib/Crypt/TripleDES.php +++ b/phpseclib/Crypt/TripleDES.php @@ -269,7 +269,7 @@ class Crypt_TripleDES extends Crypt_DES * * Valid key lengths are 64, 128 and 192 * - * @see Crypt_Rijndael:setKeyLength() + * @see Crypt_Base:setKeyLength() * @access public * @param int $length */ From d91158f6ef741f7f548091b0ee32ecac33f0207f Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 4 Oct 2015 21:06:17 -0500 Subject: [PATCH 4/5] rename key_size -> key_length --- phpseclib/Crypt/AES.php | 6 +++--- phpseclib/Crypt/Base.php | 15 +++------------ phpseclib/Crypt/Blowfish.php | 16 ++++++++-------- phpseclib/Crypt/DES.php | 12 ++++++------ phpseclib/Crypt/RC2.php | 4 ++-- phpseclib/Crypt/RC4.php | 10 +++++----- phpseclib/Crypt/Rijndael.php | 34 +++++++++++++++++----------------- phpseclib/Crypt/TripleDES.php | 14 +++++++------- phpseclib/Crypt/Twofish.php | 15 ++++++++++++--- 9 files changed, 63 insertions(+), 63 deletions(-) diff --git a/phpseclib/Crypt/AES.php b/phpseclib/Crypt/AES.php index 4d6b2794..5653bfbe 100644 --- a/phpseclib/Crypt/AES.php +++ b/phpseclib/Crypt/AES.php @@ -183,13 +183,13 @@ class Crypt_AES extends Crypt_Rijndael $length = strlen($key); switch (true) { case $length <= 16: - $this->key_size = 16; + $this->key_length = 16; break; case $length <= 24: - $this->key_size = 24; + $this->key_length = 24; break; default: - $this->key_size = 32; + $this->key_length = 32; } $this->_setEngine(); } diff --git a/phpseclib/Crypt/Base.php b/phpseclib/Crypt/Base.php index 3e99a6b7..6d0022fc 100644 --- a/phpseclib/Crypt/Base.php +++ b/phpseclib/Crypt/Base.php @@ -385,15 +385,6 @@ class Crypt_Base */ var $cipher_name_openssl_ecb; - /** - * The default password key_size used by setPassword() - * - * @see Crypt_Base::setPassword() - * @var int - * @access private - */ - var $password_key_size = 32; - /** * The default salt used by setPassword() * @@ -583,7 +574,7 @@ class Crypt_Base */ function getKeyLength() { - return $this->key_size << 3; + return $this->key_length << 3; } /** @@ -661,7 +652,7 @@ class Crypt_Base if (isset($func_args[5])) { $dkLen = $func_args[5]; } else { - $dkLen = $method == 'pbkdf1' ? 2 * $this->key_size : $this->key_size; + $dkLen = $method == 'pbkdf1' ? 2 * $this->key_length : $this->key_length; } switch (true) { @@ -1935,7 +1926,7 @@ class Crypt_Base $this->encryptIV = $this->decryptIV = str_pad(substr($this->iv, 0, $this->block_size), $this->block_size, "\0"); if (!$this->skip_key_adjustment) { - $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, "\0"); + $this->key = str_pad(substr($this->key, 0, $this->key_length), $this->key_length, "\0"); } } diff --git a/phpseclib/Crypt/Blowfish.php b/phpseclib/Crypt/Blowfish.php index 8af33d40..25907e7b 100644 --- a/phpseclib/Crypt/Blowfish.php +++ b/phpseclib/Crypt/Blowfish.php @@ -344,17 +344,17 @@ class Crypt_Blowfish extends Crypt_Base var $kl; /** - * The Key Length + * The Key Length (in bytes) * * @see Crypt_Base::setKeyLength() * @var int * @access private * @internal The max value is 256 / 8 = 32, the min value is 128 / 8 = 16. Exists in conjunction with $Nk - * because the encryption / decryption / key schedule creation requires this number and not $key_size. We could - * derive this from $key_size or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu + * because the encryption / decryption / key schedule creation requires this number and not $key_length. We could + * derive this from $key_length or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu * of that, we'll just precompute it once. */ - var $key_size = 16; + var $key_length = 16; /** * Sets the key length. @@ -367,11 +367,11 @@ class Crypt_Blowfish extends Crypt_Base function setKeyLength($length) { if ($length < 32) { - $this->key_size = 7; + $this->key_length = 7; } elseif ($length > 448) { - $this->key_size = 56; + $this->key_length = 56; } else { - $this->key_size = $length >> 3; + $this->key_length = $length >> 3; } parent::setKeyLength($length); @@ -390,7 +390,7 @@ class Crypt_Blowfish extends Crypt_Base function isValidEngine($engine) { if ($engine == CRYPT_ENGINE_OPENSSL) { - if ($this->key_size != 16) { + if ($this->key_length != 16) { return false; } $this->cipher_name_openssl_ecb = 'bf-ecb'; diff --git a/phpseclib/Crypt/DES.php b/phpseclib/Crypt/DES.php index 98fd9394..b3b6fce8 100644 --- a/phpseclib/Crypt/DES.php +++ b/phpseclib/Crypt/DES.php @@ -140,13 +140,13 @@ class Crypt_DES extends Crypt_Base var $block_size = 8; /** - * Key Length + * Key Length (in bytes) * * @see Crypt_Base::setKeyLength() * @var int * @access private */ - var $key_size = 8; + var $key_length = 8; /** * The namespace used by the cipher for its constants. @@ -209,7 +209,7 @@ class Crypt_DES extends Crypt_Base * @var string * @access private */ - var $key_size_max = 8; + var $key_length_max = 8; /** * The Key Schedule @@ -663,7 +663,7 @@ class Crypt_DES extends Crypt_Base */ function isValidEngine($engine) { - if ($this->key_size_max == 8) { + if ($this->key_length_max == 8) { if ($engine == CRYPT_ENGINE_OPENSSL) { $this->cipher_name_openssl_ecb = 'des-ecb'; $this->cipher_name_openssl = 'des-' . $this->_openssl_translate_mode(); @@ -692,8 +692,8 @@ class Crypt_DES extends Crypt_Base { // We check/cut here only up to max length of the key. // Key padding to the proper length will be done in _setupKey() - if (strlen($key) > $this->key_size_max) { - $key = substr($key, 0, $this->key_size_max); + if (strlen($key) > $this->key_length_max) { + $key = substr($key, 0, $this->key_length_max); } // Sets the key diff --git a/phpseclib/Crypt/RC2.php b/phpseclib/Crypt/RC2.php index 121a426a..42acdbc5 100644 --- a/phpseclib/Crypt/RC2.php +++ b/phpseclib/Crypt/RC2.php @@ -148,13 +148,13 @@ class Crypt_RC2 extends Crypt_Base var $skip_key_adjustment = true; /** - * Key Length + * Key Length (in bytes) * * @see Crypt_RC2::setKeyLength() * @var int * @access private */ - var $key_size = 16; // = 128 bits + var $key_length = 16; // = 128 bits /** * The namespace used by the cipher for its constants. diff --git a/phpseclib/Crypt/RC4.php b/phpseclib/Crypt/RC4.php index 3ad16836..f18a0e3a 100644 --- a/phpseclib/Crypt/RC4.php +++ b/phpseclib/Crypt/RC4.php @@ -99,13 +99,13 @@ class Crypt_RC4 extends Crypt_Base var $block_size = 0; /** - * Key Length + * Key Length (in bytes) * * @see Crypt_RC4::setKeyLength() * @var int * @access private */ - var $key_size = 128; // = 1024 bits + var $key_length = 128; // = 1024 bits /** * The namespace used by the cipher for its constants. @@ -232,11 +232,11 @@ class Crypt_RC4 extends Crypt_Base function setKeyLength($length) { if ($length < 8) { - $this->key_size = 1; + $this->key_length = 1; } elseif ($length > 2048) { - $this->key_size = 248; + $this->key_length = 248; } else { - $this->key_size = $length >> 3; + $this->key_length = $length >> 3; } parent::setKeyLength($length); diff --git a/phpseclib/Crypt/Rijndael.php b/phpseclib/Crypt/Rijndael.php index 7bd01903..5f0ac6ae 100644 --- a/phpseclib/Crypt/Rijndael.php +++ b/phpseclib/Crypt/Rijndael.php @@ -139,9 +139,9 @@ class Crypt_Rijndael extends Crypt_Base /** * The mcrypt specific name of the cipher * - * Mcrypt is useable for 128/192/256-bit $block_size/$key_size. For 160/224 not. + * Mcrypt is useable for 128/192/256-bit $block_size/$key_length. For 160/224 not. * Crypt_Rijndael determines automatically whether mcrypt is useable - * or not for the current $block_size/$key_size. + * or not for the current $block_size/$key_length. * In case of, $cipher_name_mcrypt will be set dynamically at run time accordingly. * * @see Crypt_Base::cipher_name_mcrypt @@ -194,17 +194,17 @@ class Crypt_Rijndael extends Crypt_Base var $Nb = 4; /** - * The Key Length + * The Key Length (in bytes) * * @see setKeyLength() * @var int * @access private * @internal The max value is 256 / 8 = 32, the min value is 128 / 8 = 16. Exists in conjunction with $Nk - * because the encryption / decryption / key schedule creation requires this number and not $key_size. We could - * derive this from $key_size or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu + * because the encryption / decryption / key schedule creation requires this number and not $key_length. We could + * derive this from $key_length or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu * of that, we'll just precompute it once. */ - var $key_size = 16; + var $key_length = 16; /** * The Key Length divided by 32 @@ -293,19 +293,19 @@ class Crypt_Rijndael extends Crypt_Base { switch (true) { case $length <= 128: - $this->key_size = 16; + $this->key_length = 16; break; case $length <= 160: - $this->key_size = 20; + $this->key_length = 20; break; case $length <= 192: - $this->key_size = 24; + $this->key_length = 24; break; case $length <= 224: - $this->key_size = 28; + $this->key_length = 28; break; default: - $this->key_size = 32; + $this->key_length = 32; } parent::setKeyLength($length); @@ -351,12 +351,12 @@ class Crypt_Rijndael extends Crypt_Base if ($this->block_size != 16) { return false; } - $this->cipher_name_openssl_ecb = 'aes-' . ($this->key_size << 3) . '-ecb'; - $this->cipher_name_openssl = 'aes-' . ($this->key_size << 3) . '-' . $this->_openssl_translate_mode(); + $this->cipher_name_openssl_ecb = 'aes-' . ($this->key_length << 3) . '-ecb'; + $this->cipher_name_openssl = 'aes-' . ($this->key_length << 3) . '-' . $this->_openssl_translate_mode(); break; case CRYPT_ENGINE_MCRYPT: $this->cipher_name_mcrypt = 'rijndael-' . ($this->block_size << 3); - if ($this->key_size % 8) { // is it a 160/224-bit key? + if ($this->key_length % 8) { // is it a 160/224-bit key? // mcrypt is not usable for them, only for 128/192/256-bit keys return false; } @@ -575,13 +575,13 @@ class Crypt_Rijndael extends Crypt_Base 0x7D000000, 0xFA000000, 0xEF000000, 0xC5000000, 0x91000000 ); - if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->key_size === $this->kl['key_size'] && $this->block_size === $this->kl['block_size']) { + if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->key_length === $this->kl['key_length'] && $this->block_size === $this->kl['block_size']) { // already expanded return; } - $this->kl = array('key' => $this->key, 'key_size' => $this->key_size, 'block_size' => $this->block_size); + $this->kl = array('key' => $this->key, 'key_length' => $this->key_length, 'block_size' => $this->block_size); - $this->Nk = $this->key_size >> 2; + $this->Nk = $this->key_length >> 2; // see Rijndael-ammended.pdf#page=44 $this->Nr = max($this->Nk, $this->Nb) + 6; diff --git a/phpseclib/Crypt/TripleDES.php b/phpseclib/Crypt/TripleDES.php index 70c5f250..585e2a42 100644 --- a/phpseclib/Crypt/TripleDES.php +++ b/phpseclib/Crypt/TripleDES.php @@ -95,13 +95,13 @@ define('CRYPT_DES_MODE_CBC3', CRYPT_MODE_CBC3); class Crypt_TripleDES extends Crypt_DES { /** - * Key Length + * Key Length (in bytes) * * @see Crypt_TripleDES::setKeyLength() * @var int * @access private */ - var $key_size = 24; + var $key_length = 24; /** * The default salt used by setPassword() @@ -150,7 +150,7 @@ class Crypt_TripleDES extends Crypt_DES * @var string * @access private */ - var $key_size_max = 24; + var $key_length_max = 24; /** * Internal flag whether using CRYPT_DES_MODE_3CBC or not @@ -278,13 +278,13 @@ class Crypt_TripleDES extends Crypt_DES $length >>= 3; switch (true) { case $length <= 8: - $this->key_size = 8; + $this->key_length = 8; break; case $length <= 16: - $this->key_size = 16; + $this->key_length = 16; break; default: - $this->key_size = 24; + $this->key_length = 24; } parent::setKeyLength($length); @@ -307,7 +307,7 @@ class Crypt_TripleDES extends Crypt_DES */ function setKey($key) { - $length = $this->explicit_key_length ? $this->key_size : strlen($key); + $length = $this->explicit_key_length ? $this->key_length : strlen($key); if ($length > 8) { $key = str_pad(substr($key, 0, 24), 24, chr(0)); // if $key is between 64 and 128-bits, use the first 64-bits as the last, per this: diff --git a/phpseclib/Crypt/Twofish.php b/phpseclib/Crypt/Twofish.php index c332a3e5..06cb0bb7 100644 --- a/phpseclib/Crypt/Twofish.php +++ b/phpseclib/Crypt/Twofish.php @@ -432,6 +432,15 @@ class Crypt_Twofish extends Crypt_Base */ var $kl; + /** + * The Key Length (in bytes) + * + * @see Crypt_Twofish::setKeyLength() + * @var int + * @access private + */ + var $key_length = 16; + /** * Sets the key length. * @@ -444,13 +453,13 @@ class Crypt_Twofish extends Crypt_Base { switch (true) { case $length <= 128: - $this->key_size = 16; + $this->key_length = 16; break; case $length <= 192: - $this->key_size = 24; + $this->key_length = 24; break; default: - $this->key_size = 32; + $this->key_length = 32; } parent::setKeyLength($length); From 5f1ff099dacbdd4b39179b60de045189e2625f48 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 12 Oct 2015 22:52:56 -0500 Subject: [PATCH 5/5] Crypt/Base: one more cs update --- phpseclib/Crypt/Base.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpseclib/Crypt/Base.php b/phpseclib/Crypt/Base.php index 6d0022fc..5aabc4e4 100644 --- a/phpseclib/Crypt/Base.php +++ b/phpseclib/Crypt/Base.php @@ -464,7 +464,7 @@ class Crypt_Base /** * Has the key length explicitly been set or should it be derived from the key, itself? * - * @see Crypt_Base::setKeyLength() + * @see self::setKeyLength() * @var bool * @access private */ @@ -473,7 +473,7 @@ class Crypt_Base /** * Don't truncate / null pad key * - * @see Crypt_Base::_clearBuffers() + * @see self::_clearBuffers() * @var bool * @access private */