From bf3ca6312e5318ae00a976bd11da008b4903cc07 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 3 Mar 2013 00:31:48 -0600 Subject: [PATCH] DES: Fix memory leak with CTR mode --- phpseclib/Crypt/DES.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/phpseclib/Crypt/DES.php b/phpseclib/Crypt/DES.php index 1197a50a..c8254327 100644 --- a/phpseclib/Crypt/DES.php +++ b/phpseclib/Crypt/DES.php @@ -593,7 +593,9 @@ class Crypt_DES { if (strlen($buffer['encrypted'])) { for ($i = 0; $i < strlen($plaintext); $i+=8) { $block = substr($plaintext, $i, 8); - $buffer['encrypted'].= $this->_processBlock($this->_generate_xor($xor), CRYPT_DES_ENCRYPT); + if (strlen($block) > strlen($buffer['encrypted'])) { + $buffer['encrypted'].= $this->_processBlock($this->_generate_xor($xor), CRYPT_DES_ENCRYPT); + } $key = $this->_string_shift($buffer['encrypted'], 8); $ciphertext.= $block ^ $key; } @@ -777,7 +779,9 @@ class Crypt_DES { if (strlen($buffer['ciphertext'])) { for ($i = 0; $i < strlen($ciphertext); $i+=8) { $block = substr($ciphertext, $i, 8); - $buffer['ciphertext'].= $this->_processBlock($this->_generate_xor($xor), CRYPT_DES_ENCRYPT); + if (strlen($block) > strlen($buffer['ciphertext'])) { + $buffer['ciphertext'].= $this->_processBlock($this->_generate_xor($xor), CRYPT_DES_ENCRYPT); + } $key = $this->_string_shift($buffer['ciphertext'], 8); $plaintext.= $block ^ $key; }