1
0
mirror of https://github.com/danog/phpseclib.git synced 2024-12-11 16:49:42 +01:00

Rijndael: little code optimizing

little code optimizing
This commit is contained in:
Hans-Jürgen Petrich 2013-01-21 15:46:44 +07:00
parent 45976002c7
commit d81563d6f3

View File

@ -765,8 +765,13 @@ class Crypt_Rijndael {
case CRYPT_RIJNDAEL_MODE_CFB: case CRYPT_RIJNDAEL_MODE_CFB:
// cfb loosely routines inspired by openssl's: // 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 // http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1
if ($this->continuousBuffer) {
$iv = &$this->encryptIV;
$pos = &$buffer['pos'];
} else {
$iv = $this->encryptIV; $iv = $this->encryptIV;
$pos = $this->continuousBuffer === true ? $buffer['pos'] : 0; $pos = 0;
}
$len = strlen($plaintext); $len = strlen($plaintext);
$i = 0; $i = 0;
if ($pos) { if ($pos) {
@ -793,15 +798,10 @@ class Crypt_Rijndael {
} }
if ($len) { if ($len) {
$iv = $this->_encryptBlock($iv); $iv = $this->_encryptBlock($iv);
//$block = substr($iv, $pos, $len) ^ substr($plaintext, $i, $len); $block = $iv ^ substr($plaintext, $i);
$block = substr($iv, $pos) ^ substr($plaintext, $i);
$iv = substr_replace($iv, $block, 0, $len); $iv = substr_replace($iv, $block, 0, $len);
$ciphertext.= $block; $ciphertext.= $block;
$pos+= $len; $pos = $len;
}
if($this->continuousBuffer) {
$this->encryptIV = $iv;
$buffer['pos'] = $pos;
} }
break; break;
case CRYPT_RIJNDAEL_MODE_OFB: case CRYPT_RIJNDAEL_MODE_OFB:
@ -896,8 +896,13 @@ class Crypt_Rijndael {
} }
break; break;
case CRYPT_RIJNDAEL_MODE_CFB: case CRYPT_RIJNDAEL_MODE_CFB:
if ($this->continuousBuffer) {
$iv = &$this->decryptIV;
$pos = &$buffer['pos'];
} else {
$iv = $this->decryptIV; $iv = $this->decryptIV;
$pos = $this->continuousBuffer === true ? $buffer['pos'] : 0; $pos = 0;
}
$len = strlen($ciphertext); $len = strlen($ciphertext);
$i = 0; $i = 0;
if ($pos) { if ($pos) {
@ -926,13 +931,9 @@ class Crypt_Rijndael {
} }
if ($len) { if ($len) {
$iv = $this->_encryptBlock($iv); $iv = $this->_encryptBlock($iv);
$plaintext.= substr($iv, $pos) ^ substr($ciphertext, $i); $plaintext.= $iv ^ substr($ciphertext, $i);
$iv = substr_replace($iv, substr($ciphertext, $i, $len), 0, $len); $iv = substr_replace($iv, substr($ciphertext, $i), 0, $len);
$pos+= $len; $pos = $len;
}
if ($this->continuousBuffer) {
$this->decryptIV = $iv;
$buffer['pos'] = $pos;
} }
break; break;
case CRYPT_RIJNDAEL_MODE_OFB: case CRYPT_RIJNDAEL_MODE_OFB: