don't re-generate Eval mode stuff when only IV has changed

This commit is contained in:
terrafrost 2019-09-05 00:04:49 -05:00
parent 23ffa6452e
commit eddbdcc66c
7 changed files with 22 additions and 13 deletions

View File

@ -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');

View File

@ -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;
}
/**

View File

@ -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();
}

View File

@ -310,7 +310,6 @@ class RC4 extends StreamCipher
{
if ($this->changed) {
$this->setup();
$this->changed = false;
}
$stream = &$this->stream[$mode];

View File

@ -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();
}

View File

@ -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');

View File

@ -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) {