Merge remote-tracking branch 'origin/petrich'

This commit is contained in:
terrafrost 2013-01-06 19:10:25 -06:00
commit 5e882d57fe
3 changed files with 105 additions and 62 deletions

View File

@ -6,4 +6,4 @@ php:
- 5.4 - 5.4
script: script:
- phpunit - phpunit --verbose

View File

@ -387,7 +387,7 @@ class Crypt_Rijndael {
* @var String * @var String
* @access private * @access private
*/ */
var $enbuffer = array('encrypted' => '', 'xor' => ''); var $enbuffer = array('encrypted' => '', 'xor' => '', 'pos' => 0);
/** /**
* Decryption buffer for CTR, OFB and CFB modes * Decryption buffer for CTR, OFB and CFB modes
@ -396,7 +396,7 @@ class Crypt_Rijndael {
* @var String * @var String
* @access private * @access private
*/ */
var $debuffer = array('ciphertext' => ''); var $debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0);
/** /**
* Default Constructor. * Default Constructor.
@ -763,42 +763,54 @@ class Crypt_Rijndael {
} }
break; break;
case CRYPT_RIJNDAEL_MODE_CFB: case CRYPT_RIJNDAEL_MODE_CFB:
if (strlen($buffer['xor'])) { // cfb loosely routines inspired by openssl's:
$ciphertext = $plaintext ^ $buffer['xor']; // http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1
$iv = $buffer['encrypted'] . $ciphertext;
$start = strlen($ciphertext);
$buffer['encrypted'].= $ciphertext;
$buffer['xor'] = substr($buffer['xor'], strlen($ciphertext));
} else {
$ciphertext = '';
$iv = $this->encryptIV; $iv = $this->encryptIV;
$start = 0; $pos = $this->continuousBuffer === true ? $buffer['pos'] : 0;
$len = strlen($plaintext);
if ($pos) {
$orig_pos = $pos;
$max = $block_size - $pos;
if ($len >= $max) {
$i = $max;
$len-= $max;
$pos = 0;
} else {
$i = $len;
$pos+= $len;
$len = 0;
} }
// ie. $i = min($max, $len), $len-= $i, $pos+= $i
for ($i = $start; $i < strlen($plaintext); $i+=$block_size) { $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
$block = substr($plaintext, $i, $block_size); $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
$xor = $this->_encryptBlock($iv);
$iv = $block ^ $xor;
if ($continuousBuffer && strlen($iv) != $block_size) {
$buffer = array(
'encrypted' => $iv,
'xor' => substr($xor, strlen($iv))
);
} }
while ($len >= $block_size) {
$iv = $this->_encryptBlock($iv) ^ substr($plaintext, $i, $block_size);
$ciphertext.= $iv; $ciphertext.= $iv;
$len-= $block_size;
$i+= $block_size;
}
if ($len) {
$iv = $this->_encryptBlock($iv);
//$block = substr($iv, $pos, $len) ^ substr($plaintext, $i, $len);
$block = substr($iv, $pos) ^ substr($plaintext, $i);
$iv = substr_replace($iv, $block, $pos, $len);
$ciphertext.= $block;
$i+= $len;
$pos+= $len;
} }
if($this->continuousBuffer) { if($this->continuousBuffer) {
$this->encryptIV = $iv; $this->encryptIV = $iv;
$buffer['pos'] = $pos;
} }
break; break;
case CRYPT_RIJNDAEL_MODE_OFB: case CRYPT_RIJNDAEL_MODE_OFB:
$xor = $this->encryptIV; $xor = $this->encryptIV;
if (strlen($buffer)) { if (strlen($buffer['xor'])) {
for ($i = 0; $i < strlen($plaintext); $i+=$block_size) { for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
$xor = $this->_encryptBlock($xor); $xor = $this->_encryptBlock($xor);
$buffer.= $xor; $buffer['xor'].= $xor;
$key = $this->_string_shift($buffer, $block_size); $key = $this->_string_shift($buffer['xor'], $block_size);
$ciphertext.= substr($plaintext, $i, $block_size) ^ $key; $ciphertext.= substr($plaintext, $i, $block_size) ^ $key;
} }
} else { } else {
@ -811,7 +823,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['xor'] = substr($key, $start) . $buffer['xor'];
} }
} }
} }
@ -879,49 +891,58 @@ 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['ciphertext'] = substr($key, $start) . $buffer['encrypted']; $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
} }
} }
break; break;
case CRYPT_RIJNDAEL_MODE_CFB: case CRYPT_RIJNDAEL_MODE_CFB:
if (strlen($buffer['ciphertext'])) { $iv = $this->decryptIV;
$plaintext = $ciphertext ^ substr($this->decryptIV, strlen($buffer['ciphertext'])); $pos = $this->continuousBuffer === true ? $buffer['pos'] : 0;
$buffer['ciphertext'].= substr($ciphertext, 0, strlen($plaintext)); $len = strlen($ciphertext);
if (strlen($buffer['ciphertext']) != $block_size) {
$block = $this->decryptIV;
} else {
$block = $buffer['ciphertext'];
$xor = $this->_encryptBlock($buffer['ciphertext']);
$buffer['ciphertext'] = '';
}
$start = strlen($plaintext);
} else {
$plaintext = '';
$xor = $this->_encryptBlock($this->decryptIV);
$start = 0;
}
for ($i = $start; $i < strlen($ciphertext); $i+=$block_size) { if ($pos) {
$block = substr($ciphertext, $i, $block_size); $orig_pos = $pos;
$plaintext.= $block ^ $xor; $max = $block_size - $pos;
if ($continuousBuffer && strlen($block) != $block_size) { if ($len >= $max) {
$buffer['ciphertext'].= $block; $i = $max;
$block = $xor; $len-= $max;
} else if (strlen($block) == $block_size) { $pos = 0;
$xor = $this->_encryptBlock($block); } else {
$i = $len;
$pos+= $len;
$len = 0;
} }
// ie. $i = min($max, $len), $len-= $i, $pos+= $i
$plaintext = substr($iv, $orig_pos) ^ $ciphertext;
$iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
}
while ($len >= $block_size) {
$iv = $this->_encryptBlock($iv);
$cb = substr($ciphertext, $i, $block_size);
$plaintext.= $iv ^ $cb;
$iv = $cb;
$len-= $block_size;
$i+= $block_size;
}
if ($len) {
$iv = $this->_encryptBlock($iv);
$plaintext.= substr($iv, $pos) ^ substr($ciphertext, $i);
$iv = substr_replace($iv, substr($ciphertext, $i, $len), $pos, $len);
$i+= $len;
$pos+= $len;
} }
if ($this->continuousBuffer) { if ($this->continuousBuffer) {
$this->decryptIV = $block; $this->decryptIV = $iv;
$buffer['pos'] = $pos;
} }
break; break;
case CRYPT_RIJNDAEL_MODE_OFB: case CRYPT_RIJNDAEL_MODE_OFB:
$xor = $this->decryptIV; $xor = $this->decryptIV;
if (strlen($buffer)) { if (strlen($buffer['xor'])) {
for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) { for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
$xor = $this->_encryptBlock($xor); $xor = $this->_encryptBlock($xor);
$buffer.= $xor; $buffer['xor'].= $xor;
$key = $this->_string_shift($buffer, $block_size); $key = $this->_string_shift($buffer['xor'], $block_size);
$plaintext.= substr($ciphertext, $i, $block_size) ^ $key; $plaintext.= substr($ciphertext, $i, $block_size) ^ $key;
} }
} else { } else {
@ -934,7 +955,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['xor'] = substr($key, $start) . $buffer['xor'];
} }
} }
} }
@ -1456,6 +1477,8 @@ class Crypt_Rijndael {
$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);
$this->debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0);
} }
/** /**

View File

@ -9,14 +9,34 @@
// class files of phpseclib require() other dependencies. // class files of phpseclib require() other dependencies.
set_include_path(implode(PATH_SEPARATOR, array( set_include_path(implode(PATH_SEPARATOR, array(
dirname(__FILE__) . '/../phpseclib/', dirname(__FILE__) . '/../phpseclib/',
dirname(__FILE__) . '/',
get_include_path(), get_include_path(),
))); )));
function phpseclib_is_includable($suffix)
{
foreach (explode(PATH_SEPARATOR, get_include_path()) as $prefix)
{
$ds = substr($prefix, -1) == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR;
$file = $prefix . $ds . $suffix;
if (file_exists($file))
{
return true;
}
}
return false;
}
function phpseclib_autoload($class) function phpseclib_autoload($class)
{ {
$file = str_replace('_', '/', $class) . '.php'; $file = str_replace('_', '/', $class) . '.php';
if (phpseclib_is_includable($file))
{
require $file; require $file;
} }
}
spl_autoload_register('phpseclib_autoload'); spl_autoload_register('phpseclib_autoload');