DES: Fixed CFB/OFB and continuousBuffer

FIXED: multiple calls to enable/disableCMode() will work now
FIXED: CFB/OFB modes
FIXED: "Illegal string offset" and strlen(array()) Warning/Notice
This commit is contained in:
Hans-Jürgen Petrich 2013-01-13 01:48:48 +07:00
parent bccce5802c
commit c624c785ee

View File

@ -257,19 +257,19 @@ class Crypt_DES {
* Encryption buffer for CTR, OFB and CFB modes * Encryption buffer for CTR, OFB and CFB modes
* *
* @see Crypt_DES::encrypt() * @see Crypt_DES::encrypt()
* @var String * @var Array
* @access private * @access private
*/ */
var $enbuffer = ''; var $enbuffer = array('encrypted' => '', 'xor' => '', 'pos' => 0, 'enmcrypt_init' => true);
/** /**
* Decryption buffer for CTR, OFB and CFB modes * Decryption buffer for CTR, OFB and CFB modes
* *
* @see Crypt_DES::decrypt() * @see Crypt_DES::decrypt()
* @var String * @var Array
* @access private * @access private
*/ */
var $debuffer = ''; var $debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0, 'demcrypt_init' => true);
/** /**
* mcrypt resource for CFB mode * mcrypt resource for CFB mode
@ -316,6 +316,7 @@ class Crypt_DES {
break; break;
case CRYPT_DES_MODE_CFB: case CRYPT_DES_MODE_CFB:
$this->mode = 'ncfb'; $this->mode = 'ncfb';
$this->ecb = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
break; break;
case CRYPT_DES_MODE_OFB: case CRYPT_DES_MODE_OFB:
$this->mode = MCRYPT_MODE_NOFB; $this->mode = MCRYPT_MODE_NOFB;
@ -325,6 +326,8 @@ class Crypt_DES {
$this->paddable = true; $this->paddable = true;
$this->mode = MCRYPT_MODE_CBC; $this->mode = MCRYPT_MODE_CBC;
} }
$this->enmcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');
$this->demcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');
break; break;
default: default:
@ -363,7 +366,8 @@ class Crypt_DES {
function setKey($key) function setKey($key)
{ {
$this->keys = ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) ? str_pad(substr($key, 0, 8), 8, chr(0)) : $this->_prepareKey($key); $this->keys = ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) ? str_pad(substr($key, 0, 8), 8, chr(0)) : $this->_prepareKey($key);
$this->changed = true; $this->enchanged = true;
$this->dechanged = true;
} }
/** /**
@ -431,7 +435,8 @@ class Crypt_DES {
function setIV($iv) function setIV($iv)
{ {
$this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, 8), 8, chr(0)); $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, 8), 8, chr(0));
$this->changed = true; $this->enchanged = true;
$this->dechanged = true;
} }
/** /**
@ -497,48 +502,64 @@ class Crypt_DES {
if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) { if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
if ($this->enchanged) { if ($this->enchanged) {
if (!isset($this->enmcrypt)) {
$this->enmcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');
}
mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV); mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);
if ($this->mode != 'ncfb') { if ($this->mode == 'ncfb') {
$this->enchanged = false; mcrypt_generic_init($this->ecb, $this->keys, "\0\0\0\0\0\0\0\0");
} }
$this->enchanged = false;
} }
if ($this->mode != 'ncfb') { if ($this->mode != 'ncfb' || !$this->continuousBuffer) {
$ciphertext = mcrypt_generic($this->enmcrypt, $plaintext); $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
} else { } else {
if ($this->enchanged) { $iv = &$this->encryptIV;
$this->ecb = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, ''); $pos = &$this->enbuffer['pos'];
mcrypt_generic_init($this->ecb, $this->keys, "\0\0\0\0\0\0\0\0"); $len = strlen($plaintext);
$this->enchanged = false; $ciphertext = '';
} $i = 0;
if ($pos) {
if (strlen($this->enbuffer)) { $orig_pos = $pos;
$ciphertext = $plaintext ^ substr($this->encryptIV, strlen($this->enbuffer)); $max = 8 - $pos;
$this->enbuffer.= $ciphertext; if ($len >= $max) {
if (strlen($this->enbuffer) == 8) { $i = $max;
$this->encryptIV = $this->enbuffer; $len-= $max;
$this->enbuffer = ''; $pos = 0;
mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV); } else {
$i = $len;
$pos+= $len;
$len = 0;
} }
$plaintext = substr($plaintext, strlen($ciphertext)); $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
} else { $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
$ciphertext = ''; $this->enbuffer['enmcrypt_init'] = true;
} }
if ($len >= 8) {
$last_pos = strlen($plaintext) & 0xFFFFFFF8; if ($this->enbuffer['enmcrypt_init'] === false || $len > 600) {
$ciphertext.= $last_pos ? mcrypt_generic($this->enmcrypt, substr($plaintext, 0, $last_pos)) : ''; if ($this->enbuffer['enmcrypt_init'] === true) {
mcrypt_generic_init($this->enmcrypt, $this->keys, $iv);
if (strlen($plaintext) & 0x7) { $this->enbuffer['enmcrypt_init'] = false;
if (strlen($ciphertext)) { }
$this->encryptIV = substr($ciphertext, -8); $ciphertext.= mcrypt_generic($this->enmcrypt, substr($plaintext, $i, $len - $len % 8));
$iv = substr($ciphertext, -8);
$i = strlen($ciphertext);
$len%= 8;
} else {
while ($len >= 8) {
$iv = mcrypt_generic($this->ecb, $iv) ^ substr($plaintext, $i, 8);
$ciphertext.= $iv;
$len-= 8;
$i+= 8;
}
} }
$this->encryptIV = mcrypt_generic($this->ecb, $this->encryptIV); }
$this->enbuffer = substr($plaintext, $last_pos) ^ $this->encryptIV; if ($len) {
$ciphertext.= $this->enbuffer; $iv = mcrypt_generic($this->ecb, $iv);
$block = $iv ^ substr($plaintext, $i);
$iv = substr_replace($iv, $block, 0, $len);
$ciphertext.= $block;
$pos = $len;
} }
return $ciphertext;
} }
if (!$this->continuousBuffer) { if (!$this->continuousBuffer) {
@ -597,42 +618,51 @@ class Crypt_DES {
} }
break; break;
case CRYPT_DES_MODE_CFB: case CRYPT_DES_MODE_CFB:
if (strlen($buffer['xor'])) {
$ciphertext = $plaintext ^ $buffer['xor'];
$iv = $buffer['encrypted'] . $ciphertext;
$start = strlen($ciphertext);
$buffer['encrypted'].= $ciphertext;
$buffer['xor'] = substr($buffer['xor'], strlen($ciphertext));
} else {
$ciphertext = '';
$iv = $this->encryptIV;
$start = 0;
}
for ($i = $start; $i < strlen($plaintext); $i+=8) {
$block = substr($plaintext, $i, 8);
$xor = $this->_processBlock($iv, CRYPT_DES_ENCRYPT);
$iv = $block ^ $xor;
if ($continuousBuffer && strlen($iv) != 8) {
$buffer = array(
'encrypted' => $iv,
'xor' => substr($xor, strlen($iv))
);
}
$ciphertext.= $iv;
}
if ($this->continuousBuffer) { if ($this->continuousBuffer) {
$this->encryptIV = $iv; $iv = &$this->encryptIV;
$pos = &$buffer['pos'];
} else {
$iv = $this->encryptIV;
$pos = 0;
} }
break; $len = strlen($plaintext);
$i = 0;
if ($pos) {
$orig_pos = $pos;
$max = 8 - $pos;
if ($len >= $max) {
$i = $max;
$len-= $max;
$pos = 0;
} else {
$i = $len;
$pos+= $len;
$len = 0;
}
$ciphertext = substr($iv, $orig_pos) ^ $plaintext;
$iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
}
while ($len >= 8) {
$iv = $this->_processBlock($iv, CRYPT_DES_ENCRYPT) ^ substr($plaintext, $i, 8);
$ciphertext.= $iv;
$len-= 8;
$i+= 8;
}
if ($len) {
$iv = $this->_processBlock($iv, CRYPT_DES_ENCRYPT);
$block = $iv ^ substr($plaintext, $i);
$iv = substr_replace($iv, $block, 0, $len);
$ciphertext.= $block;
$pos = $len;
}
return $ciphertext;
case CRYPT_DES_MODE_OFB: case CRYPT_DES_MODE_OFB:
$xor = $this->encryptIV; $xor = $this->encryptIV;
if (strlen($buffer)) { if (strlen($buffer['xor'])) {
for ($i = 0; $i < strlen($plaintext); $i+=8) { for ($i = 0; $i < strlen($plaintext); $i+=8) {
$xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT); $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
$buffer.= $xor; $buffer['xor'].= $xor;
$key = $this->_string_shift($buffer, 8); $key = $this->_string_shift($buffer['xor'], 8);
$ciphertext.= substr($plaintext, $i, 8) ^ $key; $ciphertext.= substr($plaintext, $i, 8) ^ $key;
} }
} else { } else {
@ -645,7 +675,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['xor'] = substr($key, $start) . $buffer['xor'];
} }
} }
} }
@ -672,50 +702,65 @@ class Crypt_DES {
if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) { if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
if ($this->dechanged) { if ($this->dechanged) {
if (!isset($this->demcrypt)) {
$this->demcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');
}
mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV); mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);
if ($this->mode != 'ncfb') { if ($this->mode == 'ncfb') {
$this->dechanged = false; mcrypt_generic_init($this->ecb, $this->keys, "\0\0\0\0\0\0\0\0");
} }
$this->dechanged = false;
} }
if ($this->mode != 'ncfb') { if ($this->mode != 'ncfb' || !$this->continuousBuffer) {
$plaintext = mdecrypt_generic($this->demcrypt, $ciphertext); $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
} else { } else {
if ($this->dechanged) { $iv = &$this->decryptIV;
$this->ecb = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, ''); $pos = &$this->debuffer['pos'];
mcrypt_generic_init($this->ecb, $this->keys, "\0\0\0\0\0\0\0\0"); $len = strlen($ciphertext);
$this->dechanged = false; $plaintext = '';
} $i = 0;
if ($pos) {
if (strlen($this->debuffer)) { $orig_pos = $pos;
$plaintext = $ciphertext ^ substr($this->decryptIV, strlen($this->debuffer)); $max = 8 - $pos;
if ($len >= $max) {
$this->debuffer.= substr($ciphertext, 0, strlen($plaintext)); $i = $max;
if (strlen($this->debuffer) == 8) { $len-= $max;
$this->decryptIV = $this->debuffer; $pos = 0;
$this->debuffer = ''; } else {
mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV); $i = $len;
$pos+= $len;
$len = 0;
} }
$ciphertext = substr($ciphertext, strlen($plaintext)); $plaintext = substr($iv, $orig_pos) ^ $ciphertext;
} else { $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
$plaintext = ''; $this->debuffer['demcrypt_init'] = true;
} }
if ($len >= 8) {
$last_pos = strlen($ciphertext) & 0xFFFFFFF8; if ($this->debuffer['demcrypt_init'] === false || $len > 600) {
$plaintext.= $last_pos ? mdecrypt_generic($this->demcrypt, substr($ciphertext, 0, $last_pos)) : ''; if ($this->debuffer['demcrypt_init'] === true) {
mcrypt_generic_init($this->demcrypt, $this->keys, $iv);
if (strlen($ciphertext) & 0x7) { $this->debuffer['demcrypt_init'] = false;
if (strlen($plaintext)) { }
$this->decryptIV = substr($ciphertext, $last_pos - 8, 8); $cb = substr($ciphertext, $i, $len - $len % 8);
$plaintext.= mdecrypt_generic($this->demcrypt, $cb);
$iv = substr($cb, -8);
$i = strlen($plaintext);
$len%= 8;
} else {
while ($len >= 8) {
$iv = mcrypt_generic($this->ecb,$iv);
$cb = substr($ciphertext, $i, 8);
$plaintext.= $iv ^ $cb;
$iv = $cb;
$len-= 8;
$i+= 8;
}
} }
$this->decryptIV = mcrypt_generic($this->ecb, $this->decryptIV);
$this->debuffer = substr($ciphertext, $last_pos);
$plaintext.= $this->debuffer ^ $this->decryptIV;
} }
if ($len) {
$iv = mcrypt_generic($this->ecb, $iv);
$plaintext.= $iv ^ substr($ciphertext, $i);
$iv = substr_replace($iv, substr($ciphertext, $i, $len), 0, $len);
$pos = $len;
}
return $plaintext; return $plaintext;
} }
@ -774,44 +819,52 @@ class Crypt_DES {
} }
break; break;
case CRYPT_DES_MODE_CFB: case CRYPT_DES_MODE_CFB:
if (strlen($buffer['ciphertext'])) {
$plaintext = $ciphertext ^ substr($this->decryptIV, strlen($buffer['ciphertext']));
$buffer['ciphertext'].= substr($ciphertext, 0, strlen($plaintext));
if (strlen($buffer['ciphertext']) != 8) {
$block = $this->decryptIV;
} else {
$block = $buffer['ciphertext'];
$xor = $this->_processBlock($buffer['ciphertext'], CRYPT_DES_ENCRYPT);
$buffer['ciphertext'] = '';
}
$start = strlen($plaintext);
} else {
$plaintext = '';
$xor = $this->_processBlock($this->decryptIV, CRYPT_DES_ENCRYPT);
$start = 0;
}
for ($i = $start; $i < strlen($ciphertext); $i+=8) {
$block = substr($ciphertext, $i, 8);
$plaintext.= $block ^ $xor;
if ($continuousBuffer && strlen($block) != 8) {
$buffer['ciphertext'].= $block;
$block = $xor;
} else if (strlen($block) == 8) {
$xor = $this->_processBlock($block, CRYPT_DES_ENCRYPT);
}
}
if ($this->continuousBuffer) { if ($this->continuousBuffer) {
$this->decryptIV = $block; $iv = &$this->decryptIV;
$pos = &$buffer['pos'];
} else {
$iv = $this->decryptIV;
$pos = 0;
} }
break; $len = strlen($ciphertext);
$i = 0;
if ($pos) {
$orig_pos = $pos;
$max = 8 - $pos;
if ($len >= $max) {
$i = $max;
$len-= $max;
$pos = 0;
} else {
$i = $len;
$pos+= $len;
$len = 0;
}
$plaintext = substr($iv, $orig_pos) ^ $ciphertext;
$iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
}
while ($len >= 8) {
$iv = $this->_processBlock($iv, CRYPT_DES_ENCRYPT);
$cb = substr($ciphertext, $i, 8);
$plaintext.= $iv ^ $cb;
$iv = $cb;
$len-= 8;
$i+= 8;
}
if ($len) {
$iv = $this->_processBlock($iv, CRYPT_DES_ENCRYPT);
$plaintext.= $iv ^ substr($ciphertext, $i);
$iv = substr_replace($iv, substr($ciphertext, $i, $len), 0, $len);
$pos = $len;
}
return $plaintext;
case CRYPT_DES_MODE_OFB: case CRYPT_DES_MODE_OFB:
$xor = $this->decryptIV; $xor = $this->decryptIV;
if (strlen($buffer)) { if (strlen($buffer['xor'])) {
for ($i = 0; $i < strlen($ciphertext); $i+=8) { for ($i = 0; $i < strlen($ciphertext); $i+=8) {
$xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT); $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
$buffer.= $xor; $buffer['xor'].= $xor;
$key = $this->_string_shift($buffer, 8); $key = $this->_string_shift($buffer['xor'], 8);
$plaintext.= substr($ciphertext, $i, 8) ^ $key; $plaintext.= substr($ciphertext, $i, 8) ^ $key;
} }
} else { } else {
@ -824,7 +877,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['xor'] = substr($key, $start) . $buffer['xor'];
} }
} }
} }
@ -887,6 +940,13 @@ class Crypt_DES {
$this->continuousBuffer = false; $this->continuousBuffer = false;
$this->encryptIV = $this->iv; $this->encryptIV = $this->iv;
$this->decryptIV = $this->iv; $this->decryptIV = $this->iv;
$this->enbuffer = array('encrypted' => '', 'xor' => '', 'pos' => 0, 'enmcrypt_init' => true);
$this->debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0, 'demcrypt_init' => true);
if (CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT) {
mcrypt_generic_init($this->enmcrypt, $this->keys, $this->iv);
mcrypt_generic_init($this->demcrypt, $this->keys, $this->iv);
}
} }
/** /**
@ -1294,4 +1354,4 @@ class Crypt_DES {
} }
// vim: ts=4:sw=4:et: // vim: ts=4:sw=4:et:
// vim6: fdl=1: // vim6: fdl=1: