mirror of
https://github.com/danog/phpseclib.git
synced 2024-12-12 00:59:48 +01:00
Merge branch '2.0' into 3.0
This commit is contained in:
commit
57b6942962
@ -1243,8 +1243,9 @@ abstract class SymmetricKey
|
||||
}
|
||||
|
||||
if ($this->engine === self::ENGINE_MCRYPT) {
|
||||
set_error_handler(function() {});
|
||||
if ($this->enchanged) {
|
||||
@mcrypt_generic_init($this->enmcrypt, $this->key, $this->getIV($this->encryptIV));
|
||||
mcrypt_generic_init($this->enmcrypt, $this->key, $this->getIV($this->encryptIV));
|
||||
$this->enchanged = false;
|
||||
}
|
||||
|
||||
@ -1277,15 +1278,15 @@ abstract class SymmetricKey
|
||||
if ($len >= $block_size) {
|
||||
if ($this->enbuffer['enmcrypt_init'] === false || $len > $this->cfb_init_len) {
|
||||
if ($this->enbuffer['enmcrypt_init'] === true) {
|
||||
@mcrypt_generic_init($this->enmcrypt, $this->key, $iv);
|
||||
mcrypt_generic_init($this->enmcrypt, $this->key, $iv);
|
||||
$this->enbuffer['enmcrypt_init'] = false;
|
||||
}
|
||||
$ciphertext.= @mcrypt_generic($this->enmcrypt, substr($plaintext, $i, $len - $len % $block_size));
|
||||
$ciphertext.= mcrypt_generic($this->enmcrypt, substr($plaintext, $i, $len - $len % $block_size));
|
||||
$iv = substr($ciphertext, -$block_size);
|
||||
$len%= $block_size;
|
||||
} else {
|
||||
while ($len >= $block_size) {
|
||||
$iv = @mcrypt_generic($this->ecb, $iv) ^ substr($plaintext, $i, $block_size);
|
||||
$iv = mcrypt_generic($this->ecb, $iv) ^ substr($plaintext, $i, $block_size);
|
||||
$ciphertext.= $iv;
|
||||
$len-= $block_size;
|
||||
$i+= $block_size;
|
||||
@ -1294,22 +1295,26 @@ abstract class SymmetricKey
|
||||
}
|
||||
|
||||
if ($len) {
|
||||
$iv = @mcrypt_generic($this->ecb, $iv);
|
||||
$iv = mcrypt_generic($this->ecb, $iv);
|
||||
$block = $iv ^ substr($plaintext, -$len);
|
||||
$iv = substr_replace($iv, $block, 0, $len);
|
||||
$ciphertext.= $block;
|
||||
$pos = $len;
|
||||
}
|
||||
|
||||
restore_error_handler();
|
||||
|
||||
return $ciphertext;
|
||||
}
|
||||
|
||||
$ciphertext = @mcrypt_generic($this->enmcrypt, $plaintext);
|
||||
$ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
|
||||
|
||||
if (!$this->continuousBuffer) {
|
||||
@mcrypt_generic_init($this->enmcrypt, $this->key, $this->getIV($this->encryptIV));
|
||||
mcrypt_generic_init($this->enmcrypt, $this->key, $this->getIV($this->encryptIV));
|
||||
}
|
||||
|
||||
restore_error_handler();
|
||||
|
||||
return $ciphertext;
|
||||
}
|
||||
|
||||
@ -1479,7 +1484,6 @@ abstract class SymmetricKey
|
||||
if ($this->paddable && strlen($ciphertext) % $this->block_size) {
|
||||
throw new \LengthException('The ciphertext length (' . strlen($ciphertext) . ') needs to be a multiple of the block size (' . $this->block_size . ')');
|
||||
}
|
||||
|
||||
$this->setup();
|
||||
|
||||
if ($this->mode == self::MODE_GCM || isset($this->poly1305Key)) {
|
||||
@ -1599,9 +1603,10 @@ abstract class SymmetricKey
|
||||
}
|
||||
|
||||
if ($this->engine === self::ENGINE_MCRYPT) {
|
||||
set_error_handler(function() {});
|
||||
$block_size = $this->block_size;
|
||||
if ($this->dechanged) {
|
||||
@mcrypt_generic_init($this->demcrypt, $this->key, $this->getIV($this->decryptIV));
|
||||
mcrypt_generic_init($this->demcrypt, $this->key, $this->getIV($this->decryptIV));
|
||||
$this->dechanged = false;
|
||||
}
|
||||
|
||||
@ -1629,26 +1634,30 @@ abstract class SymmetricKey
|
||||
}
|
||||
if ($len >= $block_size) {
|
||||
$cb = substr($ciphertext, $i, $len - $len % $block_size);
|
||||
$plaintext.= @mcrypt_generic($this->ecb, $iv . $cb) ^ $cb;
|
||||
$plaintext.= mcrypt_generic($this->ecb, $iv . $cb) ^ $cb;
|
||||
$iv = substr($cb, -$block_size);
|
||||
$len%= $block_size;
|
||||
}
|
||||
if ($len) {
|
||||
$iv = @mcrypt_generic($this->ecb, $iv);
|
||||
$iv = mcrypt_generic($this->ecb, $iv);
|
||||
$plaintext.= $iv ^ substr($ciphertext, -$len);
|
||||
$iv = substr_replace($iv, substr($ciphertext, -$len), 0, $len);
|
||||
$pos = $len;
|
||||
}
|
||||
|
||||
restore_error_handler();
|
||||
|
||||
return $plaintext;
|
||||
}
|
||||
|
||||
$plaintext = @mdecrypt_generic($this->demcrypt, $ciphertext);
|
||||
$plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
|
||||
|
||||
if (!$this->continuousBuffer) {
|
||||
@mcrypt_generic_init($this->demcrypt, $this->key, $this->getIV($this->decryptIV));
|
||||
mcrypt_generic_init($this->demcrypt, $this->key, $this->getIV($this->decryptIV));
|
||||
}
|
||||
|
||||
restore_error_handler();
|
||||
|
||||
return $this->paddable ? $this->unpad($plaintext) : $plaintext;
|
||||
}
|
||||
|
||||
@ -2189,9 +2198,12 @@ abstract class SymmetricKey
|
||||
}
|
||||
return false;
|
||||
case self::ENGINE_MCRYPT:
|
||||
return $this->cipher_name_mcrypt &&
|
||||
set_error_handler(function() {});
|
||||
$result = $this->cipher_name_mcrypt &&
|
||||
extension_loaded('mcrypt') &&
|
||||
in_array($this->cipher_name_mcrypt, @mcrypt_list_algorithms());
|
||||
in_array($this->cipher_name_mcrypt, mcrypt_list_algorithms());
|
||||
restore_error_handler();
|
||||
return $result;
|
||||
case self::ENGINE_EVAL:
|
||||
return method_exists($this, 'setupInlineCrypt');
|
||||
case self::ENGINE_INTERNAL:
|
||||
@ -2304,17 +2316,19 @@ abstract class SymmetricKey
|
||||
}
|
||||
|
||||
if ($this->engine != self::ENGINE_MCRYPT && $this->enmcrypt) {
|
||||
set_error_handler(function() {});
|
||||
// Closing the current mcrypt resource(s). _mcryptSetup() will, if needed,
|
||||
// (re)open them with the module named in $this->cipher_name_mcrypt
|
||||
@mcrypt_module_close($this->enmcrypt);
|
||||
@mcrypt_module_close($this->demcrypt);
|
||||
mcrypt_module_close($this->enmcrypt);
|
||||
mcrypt_module_close($this->demcrypt);
|
||||
$this->enmcrypt = null;
|
||||
$this->demcrypt = null;
|
||||
|
||||
if ($this->ecb) {
|
||||
@mcrypt_module_close($this->ecb);
|
||||
mcrypt_module_close($this->ecb);
|
||||
$this->ecb = null;
|
||||
}
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
$this->changed = $this->nonIVChanged = true;
|
||||
@ -2422,6 +2436,8 @@ abstract class SymmetricKey
|
||||
case self::ENGINE_MCRYPT:
|
||||
$this->enchanged = $this->dechanged = true;
|
||||
|
||||
set_error_handler(function() {});
|
||||
|
||||
if (!isset($this->enmcrypt)) {
|
||||
static $mcrypt_modes = [
|
||||
self::MODE_CTR => 'ctr',
|
||||
@ -2433,20 +2449,24 @@ abstract class SymmetricKey
|
||||
self::MODE_STREAM => MCRYPT_MODE_STREAM,
|
||||
];
|
||||
|
||||
$this->demcrypt = @mcrypt_module_open($this->cipher_name_mcrypt, '', $mcrypt_modes[$this->mode], '');
|
||||
$this->enmcrypt = @mcrypt_module_open($this->cipher_name_mcrypt, '', $mcrypt_modes[$this->mode], '');
|
||||
$this->demcrypt = mcrypt_module_open($this->cipher_name_mcrypt, '', $mcrypt_modes[$this->mode], '');
|
||||
$this->enmcrypt = mcrypt_module_open($this->cipher_name_mcrypt, '', $mcrypt_modes[$this->mode], '');
|
||||
|
||||
// we need the $ecb mcrypt resource (only) in MODE_CFB with enableContinuousBuffer()
|
||||
// to workaround mcrypt's broken ncfb implementation in buffered mode
|
||||
// see: {@link http://phpseclib.sourceforge.net/cfb-demo.phps}
|
||||
if ($this->mode == self::MODE_CFB) {
|
||||
$this->ecb = @mcrypt_module_open($this->cipher_name_mcrypt, '', MCRYPT_MODE_ECB, '');
|
||||
$this->ecb = mcrypt_module_open($this->cipher_name_mcrypt, '', MCRYPT_MODE_ECB, '');
|
||||
}
|
||||
|
||||
} // else should mcrypt_generic_deinit be called?
|
||||
|
||||
if ($this->mode == self::MODE_CFB) {
|
||||
@mcrypt_generic_init($this->ecb, $this->key, str_repeat("\0", $this->block_size));
|
||||
mcrypt_generic_init($this->ecb, $this->key, str_repeat("\0", $this->block_size));
|
||||
}
|
||||
|
||||
restore_error_handler();
|
||||
|
||||
break;
|
||||
case self::ENGINE_INTERNAL:
|
||||
$this->setupKey();
|
||||
|
Loading…
Reference in New Issue
Block a user