diff --git a/phpseclib/Crypt/ChaCha20.php b/phpseclib/Crypt/ChaCha20.php index 0f9e90c1..6e1d3919 100644 --- a/phpseclib/Crypt/ChaCha20.php +++ b/phpseclib/Crypt/ChaCha20.php @@ -186,7 +186,7 @@ class ChaCha20 extends Salsa20 * * @param string $nonce */ - public function setNonce($nonce) + public function setNonce($nonce) { if (!is_string($nonce)) { throw new \UnexpectedValueException('The nonce should be a string'); @@ -238,7 +238,7 @@ class ChaCha20 extends Salsa20 $this->enbuffer = $this->debuffer = ['ciphertext' => '', 'counter' => $this->counter]; - $this->changed = false; + $this->changed = $this->nonIVChanged = false; if ($this->nonce === false) { throw new InsufficientSetupException('No nonce has been defined'); diff --git a/phpseclib/Crypt/Common/SymmetricKey.php b/phpseclib/Crypt/Common/SymmetricKey.php index 28041d3d..8c16a784 100644 --- a/phpseclib/Crypt/Common/SymmetricKey.php +++ b/phpseclib/Crypt/Common/SymmetricKey.php @@ -351,6 +351,15 @@ abstract class SymmetricKey */ protected $changed = true; + /** + * Does Eval engie need to be (re)initialized? + * + * @see self::setup() + * @var bool + * @access private + */ + protected $nonIVChanged = true; + /** * Padding status * @@ -742,7 +751,6 @@ abstract class SymmetricKey } $this->nonce = $nonce; - $this->changed = true; $this->setEngine(); } @@ -859,7 +867,6 @@ abstract class SymmetricKey $this->key = $key; $this->key_length = strlen($key); - $this->changed = true; $this->setEngine(); } @@ -2104,7 +2111,6 @@ abstract class SymmetricKey } $this->continuousBuffer = false; - $this->changed = true; $this->setEngine(); } @@ -2271,7 +2277,7 @@ abstract class SymmetricKey } } - $this->changed = true; + $this->changed = $this->nonIVChanged = true; } /** @@ -2404,9 +2410,13 @@ abstract class SymmetricKey $this->setupKey(); break; case self::ENGINE_EVAL: - $this->setupKey(); - $this->setupInlineCrypt(); + if ($this->nonIVChanged) { + $this->setupKey(); + $this->setupInlineCrypt(); + } } + + $this->nonIVChanged = false; } /** diff --git a/phpseclib/Crypt/RC2.php b/phpseclib/Crypt/RC2.php index 317d4380..9f4acfce 100644 --- a/phpseclib/Crypt/RC2.php +++ b/phpseclib/Crypt/RC2.php @@ -395,7 +395,7 @@ class RC2 extends BlockCipher $this->key = call_user_func_array('pack', $l); $this->key_length = strlen($this->key); - $this->changed = true; + $this->changed = $this->nonIVChanged = true; $this->setEngine(); } diff --git a/phpseclib/Crypt/RC4.php b/phpseclib/Crypt/RC4.php index 9922a268..c658144a 100644 --- a/phpseclib/Crypt/RC4.php +++ b/phpseclib/Crypt/RC4.php @@ -310,7 +310,6 @@ class RC4 extends StreamCipher { if ($this->changed) { $this->setup(); - $this->changed = false; } $stream = &$this->stream[$mode]; diff --git a/phpseclib/Crypt/Rijndael.php b/phpseclib/Crypt/Rijndael.php index 58aa80e0..0fe13c90 100644 --- a/phpseclib/Crypt/Rijndael.php +++ b/phpseclib/Crypt/Rijndael.php @@ -267,7 +267,7 @@ class Rijndael extends BlockCipher $this->Nb = $length >> 5; $this->block_size = $length >> 3; - $this->changed = true; + $this->changed = $this->nonIVChanged = true; $this->setEngine(); } diff --git a/phpseclib/Crypt/Salsa20.php b/phpseclib/Crypt/Salsa20.php index 0d59c808..7f51a93c 100644 --- a/phpseclib/Crypt/Salsa20.php +++ b/phpseclib/Crypt/Salsa20.php @@ -225,7 +225,7 @@ class Salsa20 extends StreamCipher $this->enbuffer = $this->debuffer = ['ciphertext' => '', 'counter' => $this->counter]; - $this->changed = false; + $this->changed = $this->nonIVChanged = false; if ($this->nonce === false) { throw new InsufficientSetupException('No nonce has been defined'); diff --git a/phpseclib/Crypt/TripleDES.php b/phpseclib/Crypt/TripleDES.php index aa1f5216..f530758a 100644 --- a/phpseclib/Crypt/TripleDES.php +++ b/phpseclib/Crypt/TripleDES.php @@ -271,7 +271,7 @@ class TripleDES extends DES // copied from self::setKey() $this->key = $key; $this->key_length = strlen($key); - $this->changed = true; + $this->changed = $this->nonIVChanged = true; $this->setEngine(); if ($this->mode_3cbc) {