- 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;
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;

View File

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

View File

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