From d7c43d03a28dac1203a7563b2e279e944d8cdf55 Mon Sep 17 00:00:00 2001 From: Jim Wigginton Date: Sun, 4 Sep 2011 16:23:50 +0000 Subject: [PATCH] - fix bug with pure-PHP CTR mode (thanks, Max!) git-svn-id: http://phpseclib.svn.sourceforge.net/svnroot/phpseclib/trunk@177 21d32557-59b3-4da0-833f-c5933fad653e --- phpseclib/Crypt/DES.php | 16 ++++++++-------- phpseclib/Crypt/Rijndael.php | 16 ++++++++-------- phpseclib/Crypt/TripleDES.php | 16 ++++++++-------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/phpseclib/Crypt/DES.php b/phpseclib/Crypt/DES.php index 60cda135..bed1f9f9 100644 --- a/phpseclib/Crypt/DES.php +++ b/phpseclib/Crypt/DES.php @@ -525,11 +525,11 @@ class Crypt_DES { break; case CRYPT_DES_MODE_CTR: $xor = $this->encryptIV; - if (strlen($buffer)) { + if (strlen($buffer['encrypted'])) { for ($i = 0; $i < strlen($plaintext); $i+=8) { $block = substr($plaintext, $i, 8); - $buffer.= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT); - $key = $this->_string_shift($buffer, 8); + $buffer['encrypted'].= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT); + $key = $this->_string_shift($buffer['encrypted'], 8); $ciphertext.= $block ^ $key; } } else { @@ -542,7 +542,7 @@ class Crypt_DES { if ($this->continuousBuffer) { $this->encryptIV = $xor; if ($start = strlen($plaintext) & 7) { - $buffer = substr($key, $start) . $buffer; + $buffer['encrypted'] = substr($key, $start) . $buffer['encrypted']; } } break; @@ -702,11 +702,11 @@ class Crypt_DES { break; case CRYPT_DES_MODE_CTR: $xor = $this->decryptIV; - if (strlen($buffer)) { + if (strlen($buffer['ciphertext'])) { for ($i = 0; $i < strlen($ciphertext); $i+=8) { $block = substr($ciphertext, $i, 8); - $buffer.= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT); - $key = $this->_string_shift($buffer, 8); + $buffer['ciphertext'].= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT); + $key = $this->_string_shift($buffer['ciphertext'], 8); $plaintext.= $block ^ $key; } } else { @@ -719,7 +719,7 @@ class Crypt_DES { if ($this->continuousBuffer) { $this->decryptIV = $xor; if ($start = strlen($ciphertext) % 8) { - $buffer = substr($key, $start) . $buffer; + $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext']; } } break; diff --git a/phpseclib/Crypt/Rijndael.php b/phpseclib/Crypt/Rijndael.php index 16b3e68a..00aed4dc 100644 --- a/phpseclib/Crypt/Rijndael.php +++ b/phpseclib/Crypt/Rijndael.php @@ -687,11 +687,11 @@ class Crypt_Rijndael { break; case CRYPT_RIJNDAEL_MODE_CTR: $xor = $this->encryptIV; - if (!empty($buffer)) { + if (!empty($buffer['encrypted'])) { for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { $block = substr($plaintext, $i, $block_size); - $buffer.= $this->_encryptBlock($this->_generate_xor($block_size, $xor)); - $key = $this->_string_shift($buffer, $block_size); + $buffer['encrypted'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor)); + $key = $this->_string_shift($buffer['encrypted'], $block_size); $ciphertext.= $block ^ $key; } } else { @@ -704,7 +704,7 @@ class Crypt_Rijndael { if ($this->continuousBuffer) { $this->encryptIV = $xor; if ($start = strlen($plaintext) % $block_size) { - $buffer = substr($key, $start) . $buffer; + $buffer['encrypted'] = substr($key, $start) . $buffer['encrypted']; } } break; @@ -808,11 +808,11 @@ class Crypt_Rijndael { break; case CRYPT_RIJNDAEL_MODE_CTR: $xor = $this->decryptIV; - if (!empty($buffer)) { + if (!empty($buffer['ciphertext'])) { for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { $block = substr($ciphertext, $i, $block_size); - $buffer.= $this->_encryptBlock($this->_generate_xor($block_size, $xor)); - $key = $this->_string_shift($buffer, $block_size); + $buffer['ciphertext'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor)); + $key = $this->_string_shift($buffer['ciphertext'], $block_size); $plaintext.= $block ^ $key; } } else { @@ -825,7 +825,7 @@ class Crypt_Rijndael { if ($this->continuousBuffer) { $this->decryptIV = $xor; if ($start = strlen($ciphertext) % $block_size) { - $buffer = substr($key, $start) . $buffer; + $buffer['ciphertext'] = substr($key, $start) . $buffer['encrypted']; } } break; diff --git a/phpseclib/Crypt/TripleDES.php b/phpseclib/Crypt/TripleDES.php index f1675c9f..5f81be33 100644 --- a/phpseclib/Crypt/TripleDES.php +++ b/phpseclib/Crypt/TripleDES.php @@ -536,15 +536,15 @@ class Crypt_TripleDES { break; case CRYPT_DES_MODE_CTR: $xor = $this->encryptIV; - if (strlen($buffer)) { + if (strlen($buffer['encrypted'])) { for ($i = 0; $i < strlen($plaintext); $i+=8) { $block = substr($plaintext, $i, 8); $key = $this->_generate_xor(8, $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.= $key; - $key = $this->_string_shift($buffer, 8); + $buffer['encrypted'].= $key; + $key = $this->_string_shift($buffer['encrypted'], 8); $ciphertext.= $block ^ $key; } } else { @@ -560,7 +560,7 @@ class Crypt_TripleDES { if ($this->continuousBuffer) { $this->encryptIV = $xor; if ($start = strlen($plaintext) & 7) { - $buffer = substr($key, $start) . $buffer; + $buffer['encrypted'] = substr($key, $start) . $buffer; } } break; @@ -741,15 +741,15 @@ class Crypt_TripleDES { break; case CRYPT_DES_MODE_CTR: $xor = $this->decryptIV; - if (strlen($buffer)) { + if (strlen($buffer['ciphertext'])) { for ($i = 0; $i < strlen($ciphertext); $i+=8) { $block = substr($ciphertext, $i, 8); $key = $this->_generate_xor(8, $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.= $key; - $key = $this->_string_shift($buffer, 8); + $buffer['ciphertext'].= $key; + $key = $this->_string_shift($buffer['ciphertext'], 8); $plaintext.= $block ^ $key; } } else { @@ -765,7 +765,7 @@ class Crypt_TripleDES { if ($this->continuousBuffer) { $this->decryptIV = $xor; if ($start = strlen($plaintext) & 7) { - $buffer = substr($key, $start) . $buffer; + $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext']; } } break;