- 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
This commit is contained in:
Jim Wigginton 2011-09-04 16:23:50 +00:00
parent 2676ca1d91
commit d7c43d03a2
3 changed files with 24 additions and 24 deletions

View File

@ -525,11 +525,11 @@ class Crypt_DES {
break; break;
case CRYPT_DES_MODE_CTR: case CRYPT_DES_MODE_CTR:
$xor = $this->encryptIV; $xor = $this->encryptIV;
if (strlen($buffer)) { if (strlen($buffer['encrypted'])) {
for ($i = 0; $i < strlen($plaintext); $i+=8) { for ($i = 0; $i < strlen($plaintext); $i+=8) {
$block = substr($plaintext, $i, 8); $block = substr($plaintext, $i, 8);
$buffer.= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT); $buffer['encrypted'].= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
$key = $this->_string_shift($buffer, 8); $key = $this->_string_shift($buffer['encrypted'], 8);
$ciphertext.= $block ^ $key; $ciphertext.= $block ^ $key;
} }
} else { } else {
@ -542,7 +542,7 @@ class Crypt_DES {
if ($this->continuousBuffer) { if ($this->continuousBuffer) {
$this->encryptIV = $xor; $this->encryptIV = $xor;
if ($start = strlen($plaintext) & 7) { if ($start = strlen($plaintext) & 7) {
$buffer = substr($key, $start) . $buffer; $buffer['encrypted'] = substr($key, $start) . $buffer['encrypted'];
} }
} }
break; break;
@ -702,11 +702,11 @@ class Crypt_DES {
break; break;
case CRYPT_DES_MODE_CTR: case CRYPT_DES_MODE_CTR:
$xor = $this->decryptIV; $xor = $this->decryptIV;
if (strlen($buffer)) { if (strlen($buffer['ciphertext'])) {
for ($i = 0; $i < strlen($ciphertext); $i+=8) { for ($i = 0; $i < strlen($ciphertext); $i+=8) {
$block = substr($ciphertext, $i, 8); $block = substr($ciphertext, $i, 8);
$buffer.= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT); $buffer['ciphertext'].= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
$key = $this->_string_shift($buffer, 8); $key = $this->_string_shift($buffer['ciphertext'], 8);
$plaintext.= $block ^ $key; $plaintext.= $block ^ $key;
} }
} else { } else {
@ -719,7 +719,7 @@ class Crypt_DES {
if ($this->continuousBuffer) { if ($this->continuousBuffer) {
$this->decryptIV = $xor; $this->decryptIV = $xor;
if ($start = strlen($ciphertext) % 8) { if ($start = strlen($ciphertext) % 8) {
$buffer = substr($key, $start) . $buffer; $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
} }
} }
break; break;

View File

@ -687,11 +687,11 @@ class Crypt_Rijndael {
break; break;
case CRYPT_RIJNDAEL_MODE_CTR: case CRYPT_RIJNDAEL_MODE_CTR:
$xor = $this->encryptIV; $xor = $this->encryptIV;
if (!empty($buffer)) { if (!empty($buffer['encrypted'])) {
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
$block = substr($plaintext, $i, $block_size); $block = substr($plaintext, $i, $block_size);
$buffer.= $this->_encryptBlock($this->_generate_xor($block_size, $xor)); $buffer['encrypted'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor));
$key = $this->_string_shift($buffer, $block_size); $key = $this->_string_shift($buffer['encrypted'], $block_size);
$ciphertext.= $block ^ $key; $ciphertext.= $block ^ $key;
} }
} else { } else {
@ -704,7 +704,7 @@ class Crypt_Rijndael {
if ($this->continuousBuffer) { if ($this->continuousBuffer) {
$this->encryptIV = $xor; $this->encryptIV = $xor;
if ($start = strlen($plaintext) % $block_size) { if ($start = strlen($plaintext) % $block_size) {
$buffer = substr($key, $start) . $buffer; $buffer['encrypted'] = substr($key, $start) . $buffer['encrypted'];
} }
} }
break; break;
@ -808,11 +808,11 @@ class Crypt_Rijndael {
break; break;
case CRYPT_RIJNDAEL_MODE_CTR: case CRYPT_RIJNDAEL_MODE_CTR:
$xor = $this->decryptIV; $xor = $this->decryptIV;
if (!empty($buffer)) { if (!empty($buffer['ciphertext'])) {
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
$block = substr($ciphertext, $i, $block_size); $block = substr($ciphertext, $i, $block_size);
$buffer.= $this->_encryptBlock($this->_generate_xor($block_size, $xor)); $buffer['ciphertext'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor));
$key = $this->_string_shift($buffer, $block_size); $key = $this->_string_shift($buffer['ciphertext'], $block_size);
$plaintext.= $block ^ $key; $plaintext.= $block ^ $key;
} }
} else { } else {
@ -825,7 +825,7 @@ class Crypt_Rijndael {
if ($this->continuousBuffer) { if ($this->continuousBuffer) {
$this->decryptIV = $xor; $this->decryptIV = $xor;
if ($start = strlen($ciphertext) % $block_size) { if ($start = strlen($ciphertext) % $block_size) {
$buffer = substr($key, $start) . $buffer; $buffer['ciphertext'] = substr($key, $start) . $buffer['encrypted'];
} }
} }
break; break;

View File

@ -536,15 +536,15 @@ class Crypt_TripleDES {
break; break;
case CRYPT_DES_MODE_CTR: case CRYPT_DES_MODE_CTR:
$xor = $this->encryptIV; $xor = $this->encryptIV;
if (strlen($buffer)) { if (strlen($buffer['encrypted'])) {
for ($i = 0; $i < strlen($plaintext); $i+=8) { for ($i = 0; $i < strlen($plaintext); $i+=8) {
$block = substr($plaintext, $i, 8); $block = substr($plaintext, $i, 8);
$key = $this->_generate_xor(8, $xor); $key = $this->_generate_xor(8, $xor);
$key = $des[0]->_processBlock($key, CRYPT_DES_ENCRYPT); $key = $des[0]->_processBlock($key, CRYPT_DES_ENCRYPT);
$key = $des[1]->_processBlock($key, CRYPT_DES_DECRYPT); $key = $des[1]->_processBlock($key, CRYPT_DES_DECRYPT);
$key = $des[2]->_processBlock($key, CRYPT_DES_ENCRYPT); $key = $des[2]->_processBlock($key, CRYPT_DES_ENCRYPT);
$buffer.= $key; $buffer['encrypted'].= $key;
$key = $this->_string_shift($buffer, 8); $key = $this->_string_shift($buffer['encrypted'], 8);
$ciphertext.= $block ^ $key; $ciphertext.= $block ^ $key;
} }
} else { } else {
@ -560,7 +560,7 @@ class Crypt_TripleDES {
if ($this->continuousBuffer) { if ($this->continuousBuffer) {
$this->encryptIV = $xor; $this->encryptIV = $xor;
if ($start = strlen($plaintext) & 7) { if ($start = strlen($plaintext) & 7) {
$buffer = substr($key, $start) . $buffer; $buffer['encrypted'] = substr($key, $start) . $buffer;
} }
} }
break; break;
@ -741,15 +741,15 @@ class Crypt_TripleDES {
break; break;
case CRYPT_DES_MODE_CTR: case CRYPT_DES_MODE_CTR:
$xor = $this->decryptIV; $xor = $this->decryptIV;
if (strlen($buffer)) { if (strlen($buffer['ciphertext'])) {
for ($i = 0; $i < strlen($ciphertext); $i+=8) { for ($i = 0; $i < strlen($ciphertext); $i+=8) {
$block = substr($ciphertext, $i, 8); $block = substr($ciphertext, $i, 8);
$key = $this->_generate_xor(8, $xor); $key = $this->_generate_xor(8, $xor);
$key = $des[0]->_processBlock($key, CRYPT_DES_ENCRYPT); $key = $des[0]->_processBlock($key, CRYPT_DES_ENCRYPT);
$key = $des[1]->_processBlock($key, CRYPT_DES_DECRYPT); $key = $des[1]->_processBlock($key, CRYPT_DES_DECRYPT);
$key = $des[2]->_processBlock($key, CRYPT_DES_ENCRYPT); $key = $des[2]->_processBlock($key, CRYPT_DES_ENCRYPT);
$buffer.= $key; $buffer['ciphertext'].= $key;
$key = $this->_string_shift($buffer, 8); $key = $this->_string_shift($buffer['ciphertext'], 8);
$plaintext.= $block ^ $key; $plaintext.= $block ^ $key;
} }
} else { } else {
@ -765,7 +765,7 @@ class Crypt_TripleDES {
if ($this->continuousBuffer) { if ($this->continuousBuffer) {
$this->decryptIV = $xor; $this->decryptIV = $xor;
if ($start = strlen($plaintext) & 7) { if ($start = strlen($plaintext) & 7) {
$buffer = substr($key, $start) . $buffer; $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
} }
} }
break; break;