3DES/Rijndael/AES: Fix memory leak with CTR mode

Related to: https://github.com/phpseclib/phpseclib/issues/77
This commit is contained in:
Hans-Jürgen Petrich 2013-03-03 17:26:41 +07:00
parent 1e4fcf77fa
commit eff0bb21c5
2 changed files with 30 additions and 18 deletions

View File

@ -834,7 +834,9 @@ class Crypt_Rijndael {
if (strlen($buffer['encrypted'])) {
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
$block = substr($plaintext, $i, $block_size);
$buffer['encrypted'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor));
if (strlen($block) > strlen($buffer['encrypted'])) {
$buffer['encrypted'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor));
}
$key = $this->_string_shift($buffer['encrypted'], $block_size);
$ciphertext.= $block ^ $key;
}
@ -971,7 +973,9 @@ class Crypt_Rijndael {
if (strlen($buffer['ciphertext'])) {
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
$block = substr($ciphertext, $i, $block_size);
$buffer['ciphertext'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor));
if (strlen($block) > strlen($buffer['ciphertext'])) {
$buffer['ciphertext'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor));
}
$key = $this->_string_shift($buffer['ciphertext'], $block_size);
$plaintext.= $block ^ $key;
}
@ -1794,9 +1798,11 @@ class Crypt_Rijndael {
if (strlen($buffer["encrypted"])) {
for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') {
$block = substr($text, $i, '.$block_size.');
$in = $self->_generate_xor('.$block_size.', $xor);
'.$_encryptBlock.'
$buffer["encrypted"].= $in;
if (strlen($block) > strlen($buffer["encrypted"])) {
$in = $self->_generate_xor('.$block_size.', $xor);
'.$_encryptBlock.'
$buffer["encrypted"].= $in;
}
$key = $self->_string_shift($buffer["encrypted"], '.$block_size.');
$ciphertext.= $block ^ $key;
}
@ -1828,9 +1834,11 @@ class Crypt_Rijndael {
if (strlen($buffer["ciphertext"])) {
for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') {
$block = substr($text, $i, '.$block_size.');
$in = $self->_generate_xor('.$block_size.', $xor);
'.$_encryptBlock.'
$buffer["ciphertext"].= $in;
if (strlen($block) > strlen($buffer["ciphertext"])) {
$in = $self->_generate_xor('.$block_size.', $xor);
'.$_encryptBlock.'
$buffer["ciphertext"].= $in;
}
$key = $self->_string_shift($buffer["ciphertext"], '.$block_size.');
$plaintext.= $block ^ $key;
}

View File

@ -605,11 +605,13 @@ class Crypt_TripleDES {
if (strlen($buffer['encrypted'])) {
for ($i = 0; $i < strlen($plaintext); $i+=8) {
$block = substr($plaintext, $i, 8);
$key = $this->_generate_xor($xor);
$key = $des[0]->_processBlock($key, CRYPT_DES_ENCRYPT);
$key = $des[1]->_processBlock($key, CRYPT_DES_DECRYPT);
$key = $des[2]->_processBlock($key, CRYPT_DES_ENCRYPT);
$buffer['encrypted'].= $key;
if (strlen($block) > strlen($buffer['encrypted'])) {
$key = $this->_generate_xor($xor);
$key = $des[0]->_processBlock($key, CRYPT_DES_ENCRYPT);
$key = $des[1]->_processBlock($key, CRYPT_DES_DECRYPT);
$key = $des[2]->_processBlock($key, CRYPT_DES_ENCRYPT);
$buffer['encrypted'].= $key;
}
$key = $this->_string_shift($buffer['encrypted'], 8);
$ciphertext.= $block ^ $key;
}
@ -809,11 +811,13 @@ class Crypt_TripleDES {
if (strlen($buffer['ciphertext'])) {
for ($i = 0; $i < strlen($ciphertext); $i+=8) {
$block = substr($ciphertext, $i, 8);
$key = $this->_generate_xor($xor);
$key = $des[0]->_processBlock($key, CRYPT_DES_ENCRYPT);
$key = $des[1]->_processBlock($key, CRYPT_DES_DECRYPT);
$key = $des[2]->_processBlock($key, CRYPT_DES_ENCRYPT);
$buffer['ciphertext'].= $key;
if (strlen($block) > strlen($buffer['ciphertext'])) {
$key = $this->_generate_xor($xor);
$key = $des[0]->_processBlock($key, CRYPT_DES_ENCRYPT);
$key = $des[1]->_processBlock($key, CRYPT_DES_DECRYPT);
$key = $des[2]->_processBlock($key, CRYPT_DES_ENCRYPT);
$buffer['ciphertext'].= $key;
}
$key = $this->_string_shift($buffer['ciphertext'], 8);
$plaintext.= $block ^ $key;
}