1
0
mirror of https://github.com/danog/phpseclib.git synced 2024-12-11 08:39:43 +01:00

CS adjustments to petrich's code and optimizations

This commit is contained in:
terrafrost 2013-01-05 17:55:30 -06:00
parent fba455dfb2
commit 1626c0a3c7

View File

@ -763,35 +763,43 @@ class Crypt_Rijndael {
} }
break; break;
case CRYPT_RIJNDAEL_MODE_CFB: case CRYPT_RIJNDAEL_MODE_CFB:
// cfb loosely routines inspired by openssl's:
// http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1
$iv = $this->encryptIV; $iv = $this->encryptIV;
$pos = $this->continuousBuffer === true ? $buffer['pos'] : 0; $pos = $this->continuousBuffer === true ? $buffer['pos'] : 0;
$len = strlen($plaintext); $len = strlen($plaintext);
if ($pos) {
for ($i=0; $pos && $len; --$len, ++$i) $orig_pos = $pos;
{ $max = $block_size - $pos;
$iv[$pos] = $iv[$pos] ^ $plaintext[$i]; if ($len >= $max) {
$ciphertext .= $iv[$pos]; $i = $max;
$pos = ($pos+1) % $block_size; $len-= $max;
$pos = 0;
} else {
$i = $len;
$pos+= $len;
$len = 0;
} }
for (; $len >= $block_size; $len-=$block_size, $i+=$block_size) // ie. $i = min($max, $len), $len-= $i, $pos+= $i
{ $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
$iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
}
while ($len >= $block_size) {
$iv = $this->_encryptBlock($iv) ^ substr($plaintext, $i, $block_size); $iv = $this->_encryptBlock($iv) ^ substr($plaintext, $i, $block_size);
$ciphertext.= $iv; $ciphertext.= $iv;
$len-= $block_size;
$i+= $block_size;
} }
if ($len) if ($len) {
{
$iv = $this->_encryptBlock($iv); $iv = $this->_encryptBlock($iv);
while ($len--) //$block = substr($iv, $pos, $len) ^ substr($plaintext, $i, $len);
{ $block = substr($iv, $pos) ^ substr($plaintext, $i);
$iv[$pos] = $iv[$pos] ^ $plaintext[$i]; $iv = substr_replace($iv, $block, $pos, $len);
$ciphertext .= $iv[$pos]; $ciphertext.= $block;
$i+= $len;
++$i; $pos+= $len;
++$pos;
} }
} if($this->continuousBuffer) {
if($this->continuousBuffer)
{
$this->encryptIV = $iv; $this->encryptIV = $iv;
$buffer['pos'] = $pos; $buffer['pos'] = $pos;
} }
@ -892,36 +900,38 @@ class Crypt_Rijndael {
$pos = $this->continuousBuffer === true ? $buffer['pos'] : 0; $pos = $this->continuousBuffer === true ? $buffer['pos'] : 0;
$len = strlen($ciphertext); $len = strlen($ciphertext);
// cfb routines inspired by: http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1 if ($pos) {
for ($i=0; $pos && $len; --$len, ++$i) $orig_pos = $pos;
{ $max = $block_size - $pos;
$plaintext .= $iv[$pos] ^ $ciphertext[$i]; if ($len >= $max) {
$iv[$pos] = $ciphertext[$i]; $i = $max;
$pos = ($pos+1) % $block_size; $len-= $max;
$pos = 0;
} else {
$i = $len;
$pos+= $len;
$len = 0;
} }
// ie. $i = min($max, $len), $len-= $i, $pos+= $i
for (; $len >= $block_size; $len-=$block_size, $i+=$block_size) $plaintext = substr($iv, $orig_pos) ^ $ciphertext;
{ $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
}
while ($len >= $block_size) {
$iv = $this->_encryptBlock($iv); $iv = $this->_encryptBlock($iv);
$cb = substr($ciphertext, $i, $block_size); $cb = substr($ciphertext, $i, $block_size);
$plaintext.= $iv ^ $cb; $plaintext.= $iv ^ $cb;
$iv = $cb; $iv = $cb;
$len-= $block_size;
$i+= $block_size;
} }
if ($len) {
if ($len)
{
$iv = $this->_encryptBlock($iv); $iv = $this->_encryptBlock($iv);
while ($len--) $plaintext.= substr($iv, $pos) ^ substr($ciphertext, $i);
{ $iv = substr_replace($iv, substr($ciphertext, $i, $len), $pos, $len);
$plaintext .= $iv[$pos] ^ $ciphertext[$i]; $i+= $len;
$iv[$pos] = $ciphertext[$i]; $pos+= $len;
++$i;
++$pos;
} }
} if ($this->continuousBuffer) {
if ($this->continuousBuffer)
{
$this->decryptIV = $iv; $this->decryptIV = $iv;
$buffer['pos'] = $pos; $buffer['pos'] = $pos;
} }