SymmetricCiphers: add public / private / protected as appropriate

This commit is contained in:
terrafrost 2016-12-03 14:39:11 -06:00
parent 496fb80020
commit 2a1177b256
11 changed files with 552 additions and 542 deletions

View File

@ -33,10 +33,27 @@ abstract class Strings
* @access public * @access public
* @return string * @return string
*/ */
static function shift(&$string, $index = 1) public static function shift(&$string, $index = 1)
{ {
$substr = substr($string, 0, $index); $substr = substr($string, 0, $index);
$string = substr($string, $index); $string = substr($string, $index);
return $substr; return $substr;
} }
/**
* String Pop
*
* Inspired by array_pop
*
* @param string $string
* @param int $index
* @access public
* @return string
*/
public static function pop(&$string, $index = 1)
{
$substr = substr($string, -$index);
$string = substr($string, 0, -$index);
return $substr;
}
} }

View File

@ -68,7 +68,7 @@ class AES extends Rijndael
* @param int $length * @param int $length
* @throws \BadMethodCallException anytime it's called * @throws \BadMethodCallException anytime it's called
*/ */
function setBlockLength($length) public function setBlockLength($length)
{ {
throw new \BadMethodCallException('The block length cannot be set for AES.'); throw new \BadMethodCallException('The block length cannot be set for AES.');
} }
@ -83,7 +83,7 @@ class AES extends Rijndael
* @param int $length * @param int $length
* @throws \LengthException if the key length isn't supported * @throws \LengthException if the key length isn't supported
*/ */
function setKeyLength($length) public function setKeyLength($length)
{ {
switch ($length) { switch ($length) {
case 128: case 128:
@ -107,7 +107,7 @@ class AES extends Rijndael
* @param string $key * @param string $key
* @throws \LengthException if the key length isn't supported * @throws \LengthException if the key length isn't supported
*/ */
function setKey($key) public function setKey($key)
{ {
switch (strlen($key)) { switch (strlen($key)) {
case 16: case 16:
@ -120,4 +120,15 @@ class AES extends Rijndael
parent::setKey($key); parent::setKey($key);
} }
/**
* Returns the class that defines the private methods
*
* @access private
* @return string
*/
protected function getClassContext()
{
return 'phpseclib\Crypt\Rijndael';
}
} }

View File

@ -56,7 +56,7 @@ class Blowfish extends BlockCipher
* @var int * @var int
* @access private * @access private
*/ */
var $block_size = 8; protected $block_size = 8;
/** /**
* The mcrypt specific name of the cipher * The mcrypt specific name of the cipher
@ -65,7 +65,7 @@ class Blowfish extends BlockCipher
* @var string * @var string
* @access private * @access private
*/ */
var $cipher_name_mcrypt = 'blowfish'; protected $cipher_name_mcrypt = 'blowfish';
/** /**
* Optimizing value while CFB-encrypting * Optimizing value while CFB-encrypting
@ -74,7 +74,7 @@ class Blowfish extends BlockCipher
* @var int * @var int
* @access private * @access private
*/ */
var $cfb_init_len = 500; protected $cfb_init_len = 500;
/** /**
* The fixed subkeys boxes ($sbox0 - $sbox3) with 256 entries each * The fixed subkeys boxes ($sbox0 - $sbox3) with 256 entries each
@ -84,7 +84,7 @@ class Blowfish extends BlockCipher
* @access private * @access private
* @var array * @var array
*/ */
var $sbox0 = [ private $sbox0 = [
0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99,
0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e,
0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013,
@ -125,7 +125,7 @@ class Blowfish extends BlockCipher
* @access private * @access private
* @var array * @var array
*/ */
var $sbox1 = [ private $sbox1 = [
0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266,
0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e,
0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1,
@ -166,7 +166,7 @@ class Blowfish extends BlockCipher
* @access private * @access private
* @var array * @var array
*/ */
var $sbox2 = [ private $sbox2 = [
0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068, 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068,
0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840,
0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504,
@ -207,7 +207,7 @@ class Blowfish extends BlockCipher
* @access private * @access private
* @var array * @var array
*/ */
var $sbox3 = [ private $sbox3 = [
0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe,
0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4,
0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6,
@ -248,7 +248,7 @@ class Blowfish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $parray = [ private $parray = [
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0,
0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b
@ -262,7 +262,7 @@ class Blowfish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $bctx; private $bctx;
/** /**
* Holds the last used key * Holds the last used key
@ -270,7 +270,7 @@ class Blowfish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $kl; private $kl;
/** /**
* The Key Length (in bytes) * The Key Length (in bytes)
@ -283,7 +283,7 @@ class Blowfish extends BlockCipher
* derive this from $key_length or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu * derive this from $key_length or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu
* of that, we'll just precompute it once. * of that, we'll just precompute it once.
*/ */
var $key_length = 16; protected $key_length = 16;
/** /**
* Default Constructor. * Default Constructor.
@ -292,7 +292,7 @@ class Blowfish extends BlockCipher
* @access public * @access public
* @throws \InvalidArgumentException if an invalid / unsupported mode is provided * @throws \InvalidArgumentException if an invalid / unsupported mode is provided
*/ */
function __construct($mode) public function __construct($mode)
{ {
if ($mode == self::MODE_STREAM) { if ($mode == self::MODE_STREAM) {
throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode'); throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode');
@ -309,7 +309,7 @@ class Blowfish extends BlockCipher
* @access public * @access public
* @param int $length * @param int $length
*/ */
function setKeyLength($length) public function setKeyLength($length)
{ {
if ($length < 32 || $length > 448) { if ($length < 32 || $length > 448) {
throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys of sizes between 32 and 448 bits are supported'); throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys of sizes between 32 and 448 bits are supported');
@ -330,14 +330,14 @@ class Blowfish extends BlockCipher
* @access public * @access public
* @return bool * @return bool
*/ */
function isValidEngine($engine) public function isValidEngine($engine)
{ {
if ($engine == self::ENGINE_OPENSSL) { if ($engine == self::ENGINE_OPENSSL) {
if ($this->key_length != 16) { if ($this->key_length != 16) {
return false; return false;
} }
$this->cipher_name_openssl_ecb = 'bf-ecb'; $this->cipher_name_openssl_ecb = 'bf-ecb';
$this->cipher_name_openssl = 'bf-' . $this->_openssl_translate_mode(); $this->cipher_name_openssl = 'bf-' . $this->openssl_translate_mode();
} }
return parent::isValidEngine($engine); return parent::isValidEngine($engine);
@ -349,7 +349,7 @@ class Blowfish extends BlockCipher
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupKey() * @see \phpseclib\Crypt\Common\SymmetricKey::_setupKey()
* @access private * @access private
*/ */
function _setupKey() protected function setupKey()
{ {
if (isset($this->kl['key']) && $this->key === $this->kl['key']) { if (isset($this->kl['key']) && $this->key === $this->kl['key']) {
// already expanded // already expanded
@ -386,13 +386,13 @@ class Blowfish extends BlockCipher
// encrypt P3 and P4 with the new P1 and P2, do it with all P-array and subkeys // encrypt P3 and P4 with the new P1 and P2, do it with all P-array and subkeys
$data = "\0\0\0\0\0\0\0\0"; $data = "\0\0\0\0\0\0\0\0";
for ($i = 0; $i < 18; $i += 2) { for ($i = 0; $i < 18; $i += 2) {
list($l, $r) = array_values(unpack('N*', $data = $this->_encryptBlock($data))); list($l, $r) = array_values(unpack('N*', $data = $this->encryptBlock($data)));
$this->bctx['p'][$i ] = $l; $this->bctx['p'][$i ] = $l;
$this->bctx['p'][$i + 1] = $r; $this->bctx['p'][$i + 1] = $r;
} }
for ($i = 0; $i < 4; ++$i) { for ($i = 0; $i < 4; ++$i) {
for ($j = 0; $j < 256; $j += 2) { for ($j = 0; $j < 256; $j += 2) {
list($l, $r) = array_values(unpack('N*', $data = $this->_encryptBlock($data))); list($l, $r) = array_values(unpack('N*', $data = $this->encryptBlock($data)));
$this->bctx['sb'][$i][$j ] = $l; $this->bctx['sb'][$i][$j ] = $l;
$this->bctx['sb'][$i][$j + 1] = $r; $this->bctx['sb'][$i][$j + 1] = $r;
} }
@ -406,7 +406,7 @@ class Blowfish extends BlockCipher
* @param string $in * @param string $in
* @return string * @return string
*/ */
function _encryptBlock($in) protected function encryptBlock($in)
{ {
$p = $this->bctx["p"]; $p = $this->bctx["p"];
// extract($this->bctx["sb"], EXTR_PREFIX_ALL, "sb"); // slower // extract($this->bctx["sb"], EXTR_PREFIX_ALL, "sb"); // slower
@ -442,7 +442,7 @@ class Blowfish extends BlockCipher
* @param string $in * @param string $in
* @return string * @return string
*/ */
function _decryptBlock($in) protected function decryptBlock($in)
{ {
$p = $this->bctx["p"]; $p = $this->bctx["p"];
$sb_0 = $this->bctx["sb"][0]; $sb_0 = $this->bctx["sb"][0];
@ -476,9 +476,9 @@ class Blowfish extends BlockCipher
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupInlineCrypt() * @see \phpseclib\Crypt\Common\SymmetricKey::_setupInlineCrypt()
* @access private * @access private
*/ */
function _setupInlineCrypt() protected function setupInlineCrypt()
{ {
$lambda_functions =& self::_getLambdaFunctions(); $lambda_functions =& self::getLambdaFunctions();
// We create max. 10 hi-optimized code for memory reason. Means: For each $key one ultra fast inline-crypt function. // We create max. 10 hi-optimized code for memory reason. Means: For each $key one ultra fast inline-crypt function.
// (Currently, for Blowfish, one generated $lambda_function cost on php5.5@32bit ~100kb unfreeable mem and ~180kb on php5.5@64bit) // (Currently, for Blowfish, one generated $lambda_function cost on php5.5@32bit ~100kb unfreeable mem and ~180kb on php5.5@64bit)
@ -488,7 +488,7 @@ class Blowfish extends BlockCipher
// Generation of a unique hash for our generated code // Generation of a unique hash for our generated code
$code_hash = "Crypt_Blowfish, {$this->mode}"; $code_hash = "Crypt_Blowfish, {$this->mode}";
if ($gen_hi_opt_code) { if ($gen_hi_opt_code) {
$code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key); $code_hash = str_pad($code_hash, 32) . $this->hashInlineCryptFunction($this->key);
} }
if (!isset($lambda_functions[$code_hash])) { if (!isset($lambda_functions[$code_hash])) {
@ -498,10 +498,10 @@ class Blowfish extends BlockCipher
$init_crypt = ' $init_crypt = '
static $sb_0, $sb_1, $sb_2, $sb_3; static $sb_0, $sb_1, $sb_2, $sb_3;
if (!$sb_0) { if (!$sb_0) {
$sb_0 = $self->bctx["sb"][0]; $sb_0 = $this->bctx["sb"][0];
$sb_1 = $self->bctx["sb"][1]; $sb_1 = $this->bctx["sb"][1];
$sb_2 = $self->bctx["sb"][2]; $sb_2 = $this->bctx["sb"][2];
$sb_3 = $self->bctx["sb"][3]; $sb_3 = $this->bctx["sb"][3];
} }
'; ';
break; break;
@ -511,8 +511,8 @@ class Blowfish extends BlockCipher
$p[] = '$p_' . $i; $p[] = '$p_' . $i;
} }
$init_crypt = ' $init_crypt = '
list($sb_0, $sb_1, $sb_2, $sb_3) = $self->bctx["sb"]; list($sb_0, $sb_1, $sb_2, $sb_3) = $this->bctx["sb"];
list(' . implode(',', $p) . ') = $self->bctx["p"]; list(' . implode(',', $p) . ') = $this->bctx["p"];
'; ';
} }
@ -575,7 +575,7 @@ class Blowfish extends BlockCipher
); );
'; ';
$lambda_functions[$code_hash] = $this->_createInlineCryptFunction( $lambda_functions[$code_hash] = $this->createInlineCryptFunction(
[ [
'init_crypt' => $init_crypt, 'init_crypt' => $init_crypt,
'init_encrypt' => '', 'init_encrypt' => '',
@ -585,6 +585,6 @@ class Blowfish extends BlockCipher
] ]
); );
} }
$this->inline_crypt = $lambda_functions[$code_hash]; $this->inline_crypt = \Closure::bind($lambda_functions[$code_hash], $this, $this->getClassContext());
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -55,8 +55,8 @@ class DES extends BlockCipher
{ {
/**#@+ /**#@+
* @access private * @access private
* @see \phpseclib\Crypt\DES::_setupKey() * @see \phpseclib\Crypt\DES::setupKey()
* @see \phpseclib\Crypt\DES::_processBlock() * @see \phpseclib\Crypt\DES::processBlock()
*/ */
/** /**
* Contains $keys[self::ENCRYPT] * Contains $keys[self::ENCRYPT]
@ -75,7 +75,7 @@ class DES extends BlockCipher
* @var int * @var int
* @access private * @access private
*/ */
var $block_size = 8; protected $block_size = 8;
/** /**
* Key Length (in bytes) * Key Length (in bytes)
@ -84,7 +84,7 @@ class DES extends BlockCipher
* @var int * @var int
* @access private * @access private
*/ */
var $key_length = 8; protected $key_length = 8;
/** /**
* The mcrypt specific name of the cipher * The mcrypt specific name of the cipher
@ -93,7 +93,7 @@ class DES extends BlockCipher
* @var string * @var string
* @access private * @access private
*/ */
var $cipher_name_mcrypt = 'des'; protected $cipher_name_mcrypt = 'des';
/** /**
* The OpenSSL names of the cipher / modes * The OpenSSL names of the cipher / modes
@ -102,7 +102,7 @@ class DES extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $openssl_mode_names = [ protected $openssl_mode_names = [
self::MODE_ECB => 'des-ecb', self::MODE_ECB => 'des-ecb',
self::MODE_CBC => 'des-cbc', self::MODE_CBC => 'des-cbc',
self::MODE_CFB => 'des-cfb', self::MODE_CFB => 'des-cfb',
@ -117,19 +117,19 @@ class DES extends BlockCipher
* @var int * @var int
* @access private * @access private
*/ */
var $cfb_init_len = 500; protected $cfb_init_len = 500;
/** /**
* Switch for DES/3DES encryption * Switch for DES/3DES encryption
* *
* Used only if $engine == self::ENGINE_INTERNAL * Used only if $engine == self::ENGINE_INTERNAL
* *
* @see self::_setupKey() * @see self::setupKey()
* @see self::_processBlock() * @see self::processBlock()
* @var int * @var int
* @access private * @access private
*/ */
var $des_rounds = 1; protected $des_rounds = 1;
/** /**
* max possible size of $key * max possible size of $key
@ -138,16 +138,16 @@ class DES extends BlockCipher
* @var string * @var string
* @access private * @access private
*/ */
var $key_length_max = 8; protected $key_length_max = 8;
/** /**
* The Key Schedule * The Key Schedule
* *
* @see self::_setupKey() * @see self::setupKey()
* @var array * @var array
* @access private * @access private
*/ */
var $keys; private $keys;
/** /**
* Shuffle table. * Shuffle table.
@ -156,12 +156,12 @@ class DES extends BlockCipher
* with each byte containing all bits in the same state as the * with each byte containing all bits in the same state as the
* corresponding bit in the index value. * corresponding bit in the index value.
* *
* @see self::_processBlock() * @see self::processBlock()
* @see self::_setupKey() * @see self::setupKey()
* @var array * @var array
* @access private * @access private
*/ */
var $shuffle = [ private $shuffle = [
"\x00\x00\x00\x00\x00\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\xFF", "\x00\x00\x00\x00\x00\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\xFF",
"\x00\x00\x00\x00\x00\x00\xFF\x00", "\x00\x00\x00\x00\x00\x00\xFF\xFF", "\x00\x00\x00\x00\x00\x00\xFF\x00", "\x00\x00\x00\x00\x00\x00\xFF\xFF",
"\x00\x00\x00\x00\x00\xFF\x00\x00", "\x00\x00\x00\x00\x00\xFF\x00\xFF", "\x00\x00\x00\x00\x00\xFF\x00\x00", "\x00\x00\x00\x00\x00\xFF\x00\xFF",
@ -300,7 +300,7 @@ class DES extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $ipmap = [ private $ipmap = [
0x00, 0x10, 0x01, 0x11, 0x20, 0x30, 0x21, 0x31, 0x00, 0x10, 0x01, 0x11, 0x20, 0x30, 0x21, 0x31,
0x02, 0x12, 0x03, 0x13, 0x22, 0x32, 0x23, 0x33, 0x02, 0x12, 0x03, 0x13, 0x22, 0x32, 0x23, 0x33,
0x40, 0x50, 0x41, 0x51, 0x60, 0x70, 0x61, 0x71, 0x40, 0x50, 0x41, 0x51, 0x60, 0x70, 0x61, 0x71,
@ -342,7 +342,7 @@ class DES extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $invipmap = [ private $invipmap = [
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
@ -386,7 +386,7 @@ class DES extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $sbox1 = [ private $sbox1 = [
0x00808200, 0x00000000, 0x00008000, 0x00808202, 0x00808200, 0x00000000, 0x00008000, 0x00808202,
0x00808002, 0x00008202, 0x00000002, 0x00008000, 0x00808002, 0x00008202, 0x00000002, 0x00008000,
0x00000200, 0x00808200, 0x00808202, 0x00000200, 0x00000200, 0x00808200, 0x00808202, 0x00000200,
@ -411,7 +411,7 @@ class DES extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $sbox2 = [ private $sbox2 = [
0x40084010, 0x40004000, 0x00004000, 0x00084010, 0x40084010, 0x40004000, 0x00004000, 0x00084010,
0x00080000, 0x00000010, 0x40080010, 0x40004010, 0x00080000, 0x00000010, 0x40080010, 0x40004010,
0x40000010, 0x40084010, 0x40084000, 0x40000000, 0x40000010, 0x40084010, 0x40084000, 0x40000000,
@ -436,7 +436,7 @@ class DES extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $sbox3 = [ private $sbox3 = [
0x00000104, 0x04010100, 0x00000000, 0x04010004, 0x00000104, 0x04010100, 0x00000000, 0x04010004,
0x04000100, 0x00000000, 0x00010104, 0x04000100, 0x04000100, 0x00000000, 0x00010104, 0x04000100,
0x00010004, 0x04000004, 0x04000004, 0x00010000, 0x00010004, 0x04000004, 0x04000004, 0x00010000,
@ -461,7 +461,7 @@ class DES extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $sbox4 = [ private $sbox4 = [
0x80401000, 0x80001040, 0x80001040, 0x00000040, 0x80401000, 0x80001040, 0x80001040, 0x00000040,
0x00401040, 0x80400040, 0x80400000, 0x80001000, 0x00401040, 0x80400040, 0x80400000, 0x80001000,
0x00000000, 0x00401000, 0x00401000, 0x80401040, 0x00000000, 0x00401000, 0x00401000, 0x80401040,
@ -486,7 +486,7 @@ class DES extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $sbox5 = [ private $sbox5 = [
0x00000080, 0x01040080, 0x01040000, 0x21000080, 0x00000080, 0x01040080, 0x01040000, 0x21000080,
0x00040000, 0x00000080, 0x20000000, 0x01040000, 0x00040000, 0x00000080, 0x20000000, 0x01040000,
0x20040080, 0x00040000, 0x01000080, 0x20040080, 0x20040080, 0x00040000, 0x01000080, 0x20040080,
@ -511,7 +511,7 @@ class DES extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $sbox6 = [ private $sbox6 = [
0x10000008, 0x10200000, 0x00002000, 0x10202008, 0x10000008, 0x10200000, 0x00002000, 0x10202008,
0x10200000, 0x00000008, 0x10202008, 0x00200000, 0x10200000, 0x00000008, 0x10202008, 0x00200000,
0x10002000, 0x00202008, 0x00200000, 0x10000008, 0x10002000, 0x00202008, 0x00200000, 0x10000008,
@ -536,7 +536,7 @@ class DES extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $sbox7 = [ private $sbox7 = [
0x00100000, 0x02100001, 0x02000401, 0x00000000, 0x00100000, 0x02100001, 0x02000401, 0x00000000,
0x00000400, 0x02000401, 0x00100401, 0x02100400, 0x00000400, 0x02000401, 0x00100401, 0x02100400,
0x02100401, 0x00100000, 0x00000000, 0x02000001, 0x02100401, 0x00100000, 0x00000000, 0x02000001,
@ -561,7 +561,7 @@ class DES extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $sbox8 = [ private $sbox8 = [
0x08000820, 0x00000800, 0x00020000, 0x08020820, 0x08000820, 0x00000800, 0x00020000, 0x08020820,
0x08000000, 0x08000820, 0x00000020, 0x08000000, 0x08000000, 0x08000820, 0x00000020, 0x08000000,
0x00020020, 0x08020000, 0x08020820, 0x00020800, 0x00020020, 0x08020000, 0x08020820, 0x00020800,
@ -587,7 +587,7 @@ class DES extends BlockCipher
* @access public * @access public
* @throws \InvalidArgumentException if an invalid / unsupported mode is provided * @throws \InvalidArgumentException if an invalid / unsupported mode is provided
*/ */
function __construct($mode) public function __construct($mode)
{ {
if ($mode == self::MODE_STREAM) { if ($mode == self::MODE_STREAM) {
throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode'); throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode');
@ -606,12 +606,12 @@ class DES extends BlockCipher
* @access public * @access public
* @return bool * @return bool
*/ */
function isValidEngine($engine) public function isValidEngine($engine)
{ {
if ($this->key_length_max == 8) { if ($this->key_length_max == 8) {
if ($engine == self::ENGINE_OPENSSL) { if ($engine == self::ENGINE_OPENSSL) {
$this->cipher_name_openssl_ecb = 'des-ecb'; $this->cipher_name_openssl_ecb = 'des-ecb';
$this->cipher_name_openssl = 'des-' . $this->_openssl_translate_mode(); $this->cipher_name_openssl = 'des-' . $this->openssl_translate_mode();
} }
} }
@ -629,7 +629,7 @@ class DES extends BlockCipher
* @access public * @access public
* @param string $key * @param string $key
*/ */
function setKey($key) public function setKey($key)
{ {
if (!($this instanceof TripleDES) && strlen($key) != 8) { if (!($this instanceof TripleDES) && strlen($key) != 8) {
throw new \LengthException('Key of size ' . strlen($key) . ' not supported by this algorithm. Only keys of size 8 are supported'); throw new \LengthException('Key of size ' . strlen($key) . ' not supported by this algorithm. Only keys of size 8 are supported');
@ -642,31 +642,31 @@ class DES extends BlockCipher
/** /**
* Encrypts a block * Encrypts a block
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::_encryptBlock() * @see \phpseclib\Crypt\Common\SymmetricKey::encryptBlock()
* @see \phpseclib\Crypt\Common\SymmetricKey::encrypt() * @see \phpseclib\Crypt\Common\SymmetricKey::encrypt()
* @see self::encrypt() * @see self::encrypt()
* @access private * @access private
* @param string $in * @param string $in
* @return string * @return string
*/ */
function _encryptBlock($in) protected function encryptBlock($in)
{ {
return $this->_processBlock($in, self::ENCRYPT); return $this->processBlock($in, self::ENCRYPT);
} }
/** /**
* Decrypts a block * Decrypts a block
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::_decryptBlock() * @see \phpseclib\Crypt\Common\SymmetricKey::decryptBlock()
* @see \phpseclib\Crypt\Common\SymmetricKey::decrypt() * @see \phpseclib\Crypt\Common\SymmetricKey::decrypt()
* @see self::decrypt() * @see self::decrypt()
* @access private * @access private
* @param string $in * @param string $in
* @return string * @return string
*/ */
function _decryptBlock($in) protected function decryptBlock($in)
{ {
return $this->_processBlock($in, self::DECRYPT); return $this->processBlock($in, self::DECRYPT);
} }
/** /**
@ -676,14 +676,14 @@ class DES extends BlockCipher
* {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general * {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general
* idea of what this function does. * idea of what this function does.
* *
* @see self::_encryptBlock() * @see self::encryptBlock()
* @see self::_decryptBlock() * @see self::decryptBlock()
* @access private * @access private
* @param string $block * @param string $block
* @param int $mode * @param int $mode
* @return string * @return string
*/ */
function _processBlock($block, $mode) private function processBlock($block, $mode)
{ {
static $sbox1, $sbox2, $sbox3, $sbox4, $sbox5, $sbox6, $sbox7, $sbox8, $shuffleip, $shuffleinvip; static $sbox1, $sbox2, $sbox3, $sbox4, $sbox5, $sbox6, $sbox7, $sbox8, $shuffleip, $shuffleinvip;
if (!$sbox1) { if (!$sbox1) {
@ -761,10 +761,10 @@ class DES extends BlockCipher
/** /**
* Creates the key schedule * Creates the key schedule
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupKey() * @see \phpseclib\Crypt\Common\SymmetricKey::setupKey()
* @access private * @access private
*/ */
function _setupKey() protected function setupKey()
{ {
if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->des_rounds === $this->kl['des_rounds']) { if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->des_rounds === $this->kl['des_rounds']) {
// already expanded // already expanded
@ -1296,12 +1296,12 @@ class DES extends BlockCipher
/** /**
* Setup the performance-optimized function for de/encrypt() * Setup the performance-optimized function for de/encrypt()
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupInlineCrypt() * @see \phpseclib\Crypt\Common\SymmetricKey::setupInlineCrypt()
* @access private * @access private
*/ */
function _setupInlineCrypt() protected function setupInlineCrypt()
{ {
$lambda_functions =& self::_getLambdaFunctions(); $lambda_functions =& self::getLambdaFunctions();
// Engine configuration for: // Engine configuration for:
// - DES ($des_rounds == 1) or // - DES ($des_rounds == 1) or
@ -1322,7 +1322,7 @@ class DES extends BlockCipher
// After max 10 hi-optimized functions, we create generic // After max 10 hi-optimized functions, we create generic
// (still very fast.. but not ultra) functions for each $mode/$des_rounds // (still very fast.. but not ultra) functions for each $mode/$des_rounds
// Currently 2 * 5 generic functions will be then max. possible. // Currently 2 * 5 generic functions will be then max. possible.
$code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key); $code_hash = str_pad($code_hash, 32) . $this->hashInlineCryptFunction($this->key);
} }
// Is there a re-usable $lambda_functions in there? If not, we have to create it. // Is there a re-usable $lambda_functions in there? If not, we have to create it.
@ -1330,18 +1330,18 @@ class DES extends BlockCipher
// Init code for both, encrypt and decrypt. // Init code for both, encrypt and decrypt.
$init_crypt = 'static $sbox1, $sbox2, $sbox3, $sbox4, $sbox5, $sbox6, $sbox7, $sbox8, $shuffleip, $shuffleinvip; $init_crypt = 'static $sbox1, $sbox2, $sbox3, $sbox4, $sbox5, $sbox6, $sbox7, $sbox8, $shuffleip, $shuffleinvip;
if (!$sbox1) { if (!$sbox1) {
$sbox1 = array_map("intval", $self->sbox1); $sbox1 = array_map("intval", $this->sbox1);
$sbox2 = array_map("intval", $self->sbox2); $sbox2 = array_map("intval", $this->sbox2);
$sbox3 = array_map("intval", $self->sbox3); $sbox3 = array_map("intval", $this->sbox3);
$sbox4 = array_map("intval", $self->sbox4); $sbox4 = array_map("intval", $this->sbox4);
$sbox5 = array_map("intval", $self->sbox5); $sbox5 = array_map("intval", $this->sbox5);
$sbox6 = array_map("intval", $self->sbox6); $sbox6 = array_map("intval", $this->sbox6);
$sbox7 = array_map("intval", $self->sbox7); $sbox7 = array_map("intval", $this->sbox7);
$sbox8 = array_map("intval", $self->sbox8);' $sbox8 = array_map("intval", $this->sbox8);'
/* Merge $shuffle with $[inv]ipmap */ . ' /* Merge $shuffle with $[inv]ipmap */ . '
for ($i = 0; $i < 256; ++$i) { for ($i = 0; $i < 256; ++$i) {
$shuffleip[] = $self->shuffle[$self->ipmap[$i]]; $shuffleip[] = $this->shuffle[$this->ipmap[$i]];
$shuffleinvip[] = $self->shuffle[$self->invipmap[$i]]; $shuffleinvip[] = $this->shuffle[$this->invipmap[$i]];
} }
} }
'; ';
@ -1369,8 +1369,8 @@ class DES extends BlockCipher
$k[self::ENCRYPT][$i] = '$ke[' . $i . ']'; $k[self::ENCRYPT][$i] = '$ke[' . $i . ']';
$k[self::DECRYPT][$i] = '$kd[' . $i . ']'; $k[self::DECRYPT][$i] = '$kd[' . $i . ']';
} }
$init_encrypt = '$ke = $self->keys[self::ENCRYPT];'; $init_encrypt = '$ke = $this->keys[self::ENCRYPT];';
$init_decrypt = '$kd = $self->keys[self::DECRYPT];'; $init_decrypt = '$kd = $this->keys[self::DECRYPT];';
break; break;
} }
@ -1438,7 +1438,7 @@ class DES extends BlockCipher
} }
// Creates the inline-crypt function // Creates the inline-crypt function
$lambda_functions[$code_hash] = $this->_createInlineCryptFunction( $lambda_functions[$code_hash] = $this->createInlineCryptFunction(
[ [
'init_crypt' => $init_crypt, 'init_crypt' => $init_crypt,
'init_encrypt' => $init_encrypt, 'init_encrypt' => $init_encrypt,
@ -1450,6 +1450,6 @@ class DES extends BlockCipher
} }
// Set the inline-crypt function as callback in: $this->inline_crypt // Set the inline-crypt function as callback in: $this->inline_crypt
$this->inline_crypt = $lambda_functions[$code_hash]; $this->inline_crypt = \Closure::bind($lambda_functions[$code_hash], $this, $this->getClassContext());
} }
} }

View File

@ -52,7 +52,7 @@ class RC2 extends BlockCipher
* @var int * @var int
* @access private * @access private
*/ */
var $block_size = 8; protected $block_size = 8;
/** /**
* The Key * The Key
@ -62,7 +62,7 @@ class RC2 extends BlockCipher
* @var string * @var string
* @access private * @access private
*/ */
var $key; protected $key;
/** /**
* The Original (unpadded) Key * The Original (unpadded) Key
@ -74,16 +74,16 @@ class RC2 extends BlockCipher
* @var string * @var string
* @access private * @access private
*/ */
var $orig_key; private $orig_key;
/** /**
* Don't truncate / null pad key * Don't truncate / null pad key
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::_clearBuffers() * @see \phpseclib\Crypt\Common\SymmetricKey::clearBuffers()
* @var bool * @var bool
* @access private * @access private
*/ */
var $skip_key_adjustment = true; private $skip_key_adjustment = true;
/** /**
* Key Length (in bytes) * Key Length (in bytes)
@ -92,7 +92,7 @@ class RC2 extends BlockCipher
* @var int * @var int
* @access private * @access private
*/ */
var $key_length = 16; // = 128 bits protected $key_length = 16; // = 128 bits
/** /**
* The mcrypt specific name of the cipher * The mcrypt specific name of the cipher
@ -101,7 +101,7 @@ class RC2 extends BlockCipher
* @var string * @var string
* @access private * @access private
*/ */
var $cipher_name_mcrypt = 'rc2'; protected $cipher_name_mcrypt = 'rc2';
/** /**
* Optimizing value while CFB-encrypting * Optimizing value while CFB-encrypting
@ -110,7 +110,7 @@ class RC2 extends BlockCipher
* @var int * @var int
* @access private * @access private
*/ */
var $cfb_init_len = 500; protected $cfb_init_len = 500;
/** /**
* The key length in bits. * The key length in bits.
@ -122,7 +122,7 @@ class RC2 extends BlockCipher
* @internal Should be in range [1..1024]. * @internal Should be in range [1..1024].
* @internal Changing this value after setting the key has no effect. * @internal Changing this value after setting the key has no effect.
*/ */
var $default_key_length = 1024; private $default_key_length = 1024;
/** /**
* The key length in bits. * The key length in bits.
@ -133,16 +133,16 @@ class RC2 extends BlockCipher
* @access private * @access private
* @internal Should be in range [1..1024]. * @internal Should be in range [1..1024].
*/ */
var $current_key_length; private $current_key_length;
/** /**
* The Key Schedule * The Key Schedule
* *
* @see self::_setupKey() * @see self::setupKey()
* @var array * @var array
* @access private * @access private
*/ */
var $keys; private $keys;
/** /**
* Key expansion randomization table. * Key expansion randomization table.
@ -152,7 +152,7 @@ class RC2 extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $pitable = [ private $pitable = [
0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED, 0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED,
0x28, 0xE9, 0xFD, 0x79, 0x4A, 0xA0, 0xD8, 0x9D, 0x28, 0xE9, 0xFD, 0x79, 0x4A, 0xA0, 0xD8, 0x9D,
0xC6, 0x7E, 0x37, 0x83, 0x2B, 0x76, 0x53, 0x8E, 0xC6, 0x7E, 0x37, 0x83, 0x2B, 0x76, 0x53, 0x8E,
@ -226,7 +226,7 @@ class RC2 extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $invpitable = [ private $invpitable = [
0xD1, 0xDA, 0xB9, 0x6F, 0x9C, 0xC8, 0x78, 0x66, 0xD1, 0xDA, 0xB9, 0x6F, 0x9C, 0xC8, 0x78, 0x66,
0x80, 0x2C, 0xF8, 0x37, 0xEA, 0xE0, 0x62, 0xA4, 0x80, 0x2C, 0xF8, 0x37, 0xEA, 0xE0, 0x62, 0xA4,
0xCB, 0x71, 0x50, 0x27, 0x4B, 0x95, 0xD9, 0x20, 0xCB, 0x71, 0x50, 0x27, 0x4B, 0x95, 0xD9, 0x20,
@ -268,7 +268,7 @@ class RC2 extends BlockCipher
* @access public * @access public
* @throws \InvalidArgumentException if an invalid / unsupported mode is provided * @throws \InvalidArgumentException if an invalid / unsupported mode is provided
*/ */
function __construct($mode) public function __construct($mode)
{ {
if ($mode == self::MODE_STREAM) { if ($mode == self::MODE_STREAM) {
throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode'); throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode');
@ -287,7 +287,7 @@ class RC2 extends BlockCipher
* @access public * @access public
* @return bool * @return bool
*/ */
function isValidEngine($engine) public function isValidEngine($engine)
{ {
switch ($engine) { switch ($engine) {
case self::ENGINE_OPENSSL: case self::ENGINE_OPENSSL:
@ -295,7 +295,7 @@ class RC2 extends BlockCipher
return false; return false;
} }
$this->cipher_name_openssl_ecb = 'rc2-ecb'; $this->cipher_name_openssl_ecb = 'rc2-ecb';
$this->cipher_name_openssl = 'rc2-' . $this->_openssl_translate_mode(); $this->cipher_name_openssl = 'rc2-' . $this->openssl_translate_mode();
} }
return parent::isValidEngine($engine); return parent::isValidEngine($engine);
@ -312,7 +312,7 @@ class RC2 extends BlockCipher
* @param int $length in bits * @param int $length in bits
* @throws \LengthException if the key length isn't supported * @throws \LengthException if the key length isn't supported
*/ */
function setKeyLength($length) public function setKeyLength($length)
{ {
if ($length < 8 || $length > 1024) { if ($length < 8 || $length > 1024) {
throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys between 1 and 1024 bits, inclusive, are supported'); throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys between 1 and 1024 bits, inclusive, are supported');
@ -328,7 +328,7 @@ class RC2 extends BlockCipher
* @access public * @access public
* @return int * @return int
*/ */
function getKeyLength() public function getKeyLength()
{ {
return $this->current_key_length; return $this->current_key_length;
} }
@ -347,7 +347,7 @@ class RC2 extends BlockCipher
* @param int $t1 optional Effective key length in bits. * @param int $t1 optional Effective key length in bits.
* @throws \LengthException if the key length isn't supported * @throws \LengthException if the key length isn't supported
*/ */
function setKey($key, $t1 = false) public function setKey($key, $t1 = false)
{ {
$this->orig_key = $key; $this->orig_key = $key;
@ -395,7 +395,7 @@ class RC2 extends BlockCipher
$this->key = call_user_func_array('pack', $l); $this->key = call_user_func_array('pack', $l);
$this->key_length = strlen($this->key); $this->key_length = strlen($this->key);
$this->changed = true; $this->changed = true;
$this->_setEngine(); $this->setEngine();
} }
/** /**
@ -408,7 +408,7 @@ class RC2 extends BlockCipher
* @param string $plaintext * @param string $plaintext
* @return string $ciphertext * @return string $ciphertext
*/ */
function encrypt($plaintext) public function encrypt($plaintext)
{ {
if ($this->engine == self::ENGINE_OPENSSL) { if ($this->engine == self::ENGINE_OPENSSL) {
$temp = $this->key; $temp = $this->key;
@ -431,7 +431,7 @@ class RC2 extends BlockCipher
* @param string $ciphertext * @param string $ciphertext
* @return string $plaintext * @return string $plaintext
*/ */
function decrypt($ciphertext) public function decrypt($ciphertext)
{ {
if ($this->engine == self::ENGINE_OPENSSL) { if ($this->engine == self::ENGINE_OPENSSL) {
$temp = $this->key; $temp = $this->key;
@ -447,13 +447,13 @@ class RC2 extends BlockCipher
/** /**
* Encrypts a block * Encrypts a block
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::_encryptBlock() * @see \phpseclib\Crypt\Common\SymmetricKey::encryptBlock()
* @see \phpseclib\Crypt\Common\SymmetricKey::encrypt() * @see \phpseclib\Crypt\Common\SymmetricKey::encrypt()
* @access private * @access private
* @param string $in * @param string $in
* @return string * @return string
*/ */
function _encryptBlock($in) protected function encryptBlock($in)
{ {
list($r0, $r1, $r2, $r3) = array_values(unpack('v*', $in)); list($r0, $r1, $r2, $r3) = array_values(unpack('v*', $in));
$keys = $this->keys; $keys = $this->keys;
@ -492,13 +492,13 @@ class RC2 extends BlockCipher
/** /**
* Decrypts a block * Decrypts a block
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::_decryptBlock() * @see \phpseclib\Crypt\Common\SymmetricKey::decryptBlock()
* @see \phpseclib\Crypt\Common\SymmetricKey::decrypt() * @see \phpseclib\Crypt\Common\SymmetricKey::decrypt()
* @access private * @access private
* @param string $in * @param string $in
* @return string * @return string
*/ */
function _decryptBlock($in) protected function decryptBlock($in)
{ {
list($r0, $r1, $r2, $r3) = array_values(unpack('v*', $in)); list($r0, $r1, $r2, $r3) = array_values(unpack('v*', $in));
$keys = $this->keys; $keys = $this->keys;
@ -537,25 +537,25 @@ class RC2 extends BlockCipher
/** /**
* Setup the \phpseclib\Crypt\Common\SymmetricKey::ENGINE_MCRYPT $engine * Setup the \phpseclib\Crypt\Common\SymmetricKey::ENGINE_MCRYPT $engine
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupMcrypt() * @see \phpseclib\Crypt\Common\SymmetricKey::setupMcrypt()
* @access private * @access private
*/ */
function _setupMcrypt() protected function setupMcrypt()
{ {
if (!isset($this->key)) { if (!isset($this->key)) {
$this->setKey(''); $this->setKey('');
} }
parent::_setupMcrypt(); parent::setupMcrypt();
} }
/** /**
* Creates the key schedule * Creates the key schedule
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupKey() * @see \phpseclib\Crypt\Common\SymmetricKey::setupKey()
* @access private * @access private
*/ */
function _setupKey() protected function setupKey()
{ {
if (!isset($this->key)) { if (!isset($this->key)) {
$this->setKey(''); $this->setKey('');
@ -573,12 +573,12 @@ class RC2 extends BlockCipher
/** /**
* Setup the performance-optimized function for de/encrypt() * Setup the performance-optimized function for de/encrypt()
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupInlineCrypt() * @see \phpseclib\Crypt\Common\SymmetricKey::setupInlineCrypt()
* @access private * @access private
*/ */
function _setupInlineCrypt() protected function setupInlineCrypt()
{ {
$lambda_functions =& self::_getLambdaFunctions(); $lambda_functions =& self::getLambdaFunctions();
// The first 10 generated $lambda_functions will use the $keys hardcoded as integers // The first 10 generated $lambda_functions will use the $keys hardcoded as integers
// for the mixing rounds, for better inline crypt performance [~20% faster]. // for the mixing rounds, for better inline crypt performance [~20% faster].
@ -589,14 +589,14 @@ class RC2 extends BlockCipher
// Generation of a unique hash for our generated code // Generation of a unique hash for our generated code
$code_hash = "Crypt_RC2, {$this->mode}"; $code_hash = "Crypt_RC2, {$this->mode}";
if ($gen_hi_opt_code) { if ($gen_hi_opt_code) {
$code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key); $code_hash = str_pad($code_hash, 32) . $this->hashInlineCryptFunction($this->key);
} }
// Is there a re-usable $lambda_functions in there? // Is there a re-usable $lambda_functions in there?
// If not, we have to create it. // If not, we have to create it.
if (!isset($lambda_functions[$code_hash])) { if (!isset($lambda_functions[$code_hash])) {
// Init code for both, encrypt and decrypt. // Init code for both, encrypt and decrypt.
$init_crypt = '$keys = $self->keys;'; $init_crypt = '$keys = $this->keys;';
switch (true) { switch (true) {
case $gen_hi_opt_code: case $gen_hi_opt_code:
@ -694,7 +694,7 @@ class RC2 extends BlockCipher
$decrypt_block .= '$in = pack("v4", $r0, $r1, $r2, $r3);'; $decrypt_block .= '$in = pack("v4", $r0, $r1, $r2, $r3);';
// Creates the inline-crypt function // Creates the inline-crypt function
$lambda_functions[$code_hash] = $this->_createInlineCryptFunction( $lambda_functions[$code_hash] = $this->createInlineCryptFunction(
[ [
'init_crypt' => $init_crypt, 'init_crypt' => $init_crypt,
'encrypt_block' => $encrypt_block, 'encrypt_block' => $encrypt_block,
@ -704,6 +704,6 @@ class RC2 extends BlockCipher
} }
// Set the inline-crypt function as callback in: $this->inline_crypt // Set the inline-crypt function as callback in: $this->inline_crypt
$this->inline_crypt = $lambda_functions[$code_hash]; $this->inline_crypt = \Closure::bind($lambda_functions[$code_hash], $this, $this->getClassContext());
} }
} }

View File

@ -73,7 +73,7 @@ class RC4 extends StreamCipher
* @var int * @var int
* @access private * @access private
*/ */
var $block_size = 0; protected $block_size = 0;
/** /**
* Key Length (in bytes) * Key Length (in bytes)
@ -82,7 +82,7 @@ class RC4 extends StreamCipher
* @var int * @var int
* @access private * @access private
*/ */
var $key_length = 128; // = 1024 bits protected $key_length = 128; // = 1024 bits
/** /**
* The mcrypt specific name of the cipher * The mcrypt specific name of the cipher
@ -91,7 +91,7 @@ class RC4 extends StreamCipher
* @var string * @var string
* @access private * @access private
*/ */
var $cipher_name_mcrypt = 'arcfour'; protected $cipher_name_mcrypt = 'arcfour';
/** /**
* Holds whether performance-optimized $inline_crypt() can/should be used. * Holds whether performance-optimized $inline_crypt() can/should be used.
@ -100,7 +100,7 @@ class RC4 extends StreamCipher
* @var mixed * @var mixed
* @access private * @access private
*/ */
var $use_inline_crypt = false; // currently not available protected $use_inline_crypt = false; // currently not available
/** /**
* The Key * The Key
@ -109,7 +109,7 @@ class RC4 extends StreamCipher
* @var string * @var string
* @access private * @access private
*/ */
var $key; protected $key;
/** /**
* The Key Stream for decryption and encryption * The Key Stream for decryption and encryption
@ -118,7 +118,7 @@ class RC4 extends StreamCipher
* @var array * @var array
* @access private * @access private
*/ */
var $stream; private $stream;
/** /**
* Default Constructor. * Default Constructor.
@ -127,7 +127,7 @@ class RC4 extends StreamCipher
* @return \phpseclib\Crypt\RC4 * @return \phpseclib\Crypt\RC4
* @access public * @access public
*/ */
function __construct() public function __construct()
{ {
parent::__construct(self::MODE_STREAM); parent::__construct(self::MODE_STREAM);
} }
@ -142,7 +142,7 @@ class RC4 extends StreamCipher
* @access public * @access public
* @return bool * @return bool
*/ */
function isValidEngine($engine) public function isValidEngine($engine)
{ {
switch ($engine) { switch ($engine) {
case self::ENGINE_OPENSSL: case self::ENGINE_OPENSSL:
@ -170,7 +170,7 @@ class RC4 extends StreamCipher
* @access public * @access public
* @return bool * @return bool
*/ */
function usesIV() public function usesIV()
{ {
return false; return false;
} }
@ -184,7 +184,7 @@ class RC4 extends StreamCipher
* @param int $length * @param int $length
* @throws \LengthException if the key length is invalid * @throws \LengthException if the key length is invalid
*/ */
function setKeyLength($length) public function setKeyLength($length)
{ {
if ($length < 8 || $length > 2048) { if ($length < 8 || $length > 2048) {
throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys between 1 and 256 bytes are supported'); throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys between 1 and 256 bytes are supported');
@ -204,7 +204,7 @@ class RC4 extends StreamCipher
* @param int $length * @param int $length
* @throws \LengthException if the key length is invalid * @throws \LengthException if the key length is invalid
*/ */
function setKey($key) public function setKey($key)
{ {
$length = strlen($key); $length = strlen($key);
if ($length < 1 || $length > 256) { if ($length < 1 || $length > 256) {
@ -218,17 +218,17 @@ class RC4 extends StreamCipher
* Encrypts a message. * Encrypts a message.
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::decrypt() * @see \phpseclib\Crypt\Common\SymmetricKey::decrypt()
* @see self::_crypt() * @see self::crypt()
* @access public * @access public
* @param string $plaintext * @param string $plaintext
* @return string $ciphertext * @return string $ciphertext
*/ */
function encrypt($plaintext) public function encrypt($plaintext)
{ {
if ($this->engine != self::ENGINE_INTERNAL) { if ($this->engine != self::ENGINE_INTERNAL) {
return parent::encrypt($plaintext); return parent::encrypt($plaintext);
} }
return $this->_crypt($plaintext, self::ENCRYPT); return $this->crypt($plaintext, self::ENCRYPT);
} }
/** /**
@ -238,17 +238,17 @@ class RC4 extends StreamCipher
* At least if the continuous buffer is disabled. * At least if the continuous buffer is disabled.
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::encrypt() * @see \phpseclib\Crypt\Common\SymmetricKey::encrypt()
* @see self::_crypt() * @see self::crypt()
* @access public * @access public
* @param string $ciphertext * @param string $ciphertext
* @return string $plaintext * @return string $plaintext
*/ */
function decrypt($ciphertext) public function decrypt($ciphertext)
{ {
if ($this->engine != self::ENGINE_INTERNAL) { if ($this->engine != self::ENGINE_INTERNAL) {
return parent::decrypt($ciphertext); return parent::decrypt($ciphertext);
} }
return $this->_crypt($ciphertext, self::DECRYPT); return $this->crypt($ciphertext, self::DECRYPT);
} }
/** /**
@ -257,7 +257,7 @@ class RC4 extends StreamCipher
* @access private * @access private
* @param string $in * @param string $in
*/ */
function _encryptBlock($in) protected function encryptBlock($in)
{ {
// RC4 does not utilize this method // RC4 does not utilize this method
} }
@ -268,7 +268,7 @@ class RC4 extends StreamCipher
* @access private * @access private
* @param string $in * @param string $in
*/ */
function _decryptBlock($in) protected function decryptBlock($in)
{ {
// RC4 does not utilize this method // RC4 does not utilize this method
} }
@ -279,7 +279,7 @@ class RC4 extends StreamCipher
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupKey() * @see \phpseclib\Crypt\Common\SymmetricKey::_setupKey()
* @access private * @access private
*/ */
function _setupKey() protected function setupKey()
{ {
$key = $this->key; $key = $this->key;
$keyLength = strlen($key); $keyLength = strlen($key);
@ -310,10 +310,10 @@ class RC4 extends StreamCipher
* @param int $mode * @param int $mode
* @return string $text * @return string $text
*/ */
function _crypt($text, $mode) private function crypt($text, $mode)
{ {
if ($this->changed) { if ($this->changed) {
$this->_setup(); $this->setup();
$this->changed = false; $this->changed = false;
} }

View File

@ -79,7 +79,7 @@ class Rijndael extends BlockCipher
* @var string * @var string
* @access private * @access private
*/ */
var $cipher_name_mcrypt = 'rijndael-128'; protected $cipher_name_mcrypt = 'rijndael-128';
/** /**
* The default salt used by setPassword() * The default salt used by setPassword()
@ -89,25 +89,25 @@ class Rijndael extends BlockCipher
* @var string * @var string
* @access private * @access private
*/ */
var $password_default_salt = 'phpseclib'; protected $password_default_salt = 'phpseclib';
/** /**
* The Key Schedule * The Key Schedule
* *
* @see self::_setup() * @see self::setup()
* @var array * @var array
* @access private * @access private
*/ */
var $w; private $w;
/** /**
* The Inverse Key Schedule * The Inverse Key Schedule
* *
* @see self::_setup() * @see self::setup()
* @var array * @var array
* @access private * @access private
*/ */
var $dw; private $dw;
/** /**
* The Block Length divided by 32 * The Block Length divided by 32
@ -120,7 +120,7 @@ class Rijndael extends BlockCipher
* derive this from $block_size or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu * derive this from $block_size or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu
* of that, we'll just precompute it once. * of that, we'll just precompute it once.
*/ */
var $Nb = 4; private $Nb = 4;
/** /**
* The Key Length (in bytes) * The Key Length (in bytes)
@ -133,7 +133,7 @@ class Rijndael extends BlockCipher
* derive this from $key_length or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu * derive this from $key_length or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu
* of that, we'll just precompute it once. * of that, we'll just precompute it once.
*/ */
var $key_length = 16; protected $key_length = 16;
/** /**
* The Key Length divided by 32 * The Key Length divided by 32
@ -143,7 +143,7 @@ class Rijndael extends BlockCipher
* @access private * @access private
* @internal The max value is 256 / 32 = 8, the min value is 128 / 32 = 4 * @internal The max value is 256 / 32 = 8, the min value is 128 / 32 = 4
*/ */
var $Nk = 4; private $Nk = 4;
/** /**
* The Number of Rounds * The Number of Rounds
@ -152,7 +152,7 @@ class Rijndael extends BlockCipher
* @access private * @access private
* @internal The max value is 14, the min value is 10. * @internal The max value is 14, the min value is 10.
*/ */
var $Nr; private $Nr;
/** /**
* Shift offsets * Shift offsets
@ -160,7 +160,7 @@ class Rijndael extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $c; private $c;
/** /**
* Holds the last used key- and block_size information * Holds the last used key- and block_size information
@ -168,7 +168,7 @@ class Rijndael extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $kl; private $kl;
/** /**
* Default Constructor. * Default Constructor.
@ -177,7 +177,7 @@ class Rijndael extends BlockCipher
* @access public * @access public
* @throws \InvalidArgumentException if an invalid / unsupported mode is provided * @throws \InvalidArgumentException if an invalid / unsupported mode is provided
*/ */
function __construct($mode) public function __construct($mode)
{ {
if ($mode == self::MODE_STREAM) { if ($mode == self::MODE_STREAM) {
throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode'); throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode');
@ -206,7 +206,7 @@ class Rijndael extends BlockCipher
* @throws \LengthException if the key length is invalid * @throws \LengthException if the key length is invalid
* @param int $length * @param int $length
*/ */
function setKeyLength($length) public function setKeyLength($length)
{ {
switch ($length) { switch ($length) {
case 128: case 128:
@ -233,7 +233,7 @@ class Rijndael extends BlockCipher
* @param string $key * @param string $key
* @throws \LengthException if the key length isn't supported * @throws \LengthException if the key length isn't supported
*/ */
function setKey($key) public function setKey($key)
{ {
switch (strlen($key)) { switch (strlen($key)) {
case 16: case 16:
@ -257,7 +257,7 @@ class Rijndael extends BlockCipher
* @access public * @access public
* @param int $length * @param int $length
*/ */
function setBlockLength($length) public function setBlockLength($length)
{ {
switch ($length) { switch ($length) {
case 128: case 128:
@ -273,7 +273,7 @@ class Rijndael extends BlockCipher
$this->Nb = $length >> 5; $this->Nb = $length >> 5;
$this->block_size = $length >> 3; $this->block_size = $length >> 3;
$this->changed = true; $this->changed = true;
$this->_setEngine(); $this->setEngine();
} }
/** /**
@ -286,7 +286,7 @@ class Rijndael extends BlockCipher
* @access public * @access public
* @return bool * @return bool
*/ */
function isValidEngine($engine) public function isValidEngine($engine)
{ {
switch ($engine) { switch ($engine) {
case self::ENGINE_OPENSSL: case self::ENGINE_OPENSSL:
@ -294,7 +294,7 @@ class Rijndael extends BlockCipher
return false; return false;
} }
$this->cipher_name_openssl_ecb = 'aes-' . ($this->key_length << 3) . '-ecb'; $this->cipher_name_openssl_ecb = 'aes-' . ($this->key_length << 3) . '-ecb';
$this->cipher_name_openssl = 'aes-' . ($this->key_length << 3) . '-' . $this->_openssl_translate_mode(); $this->cipher_name_openssl = 'aes-' . ($this->key_length << 3) . '-' . $this->openssl_translate_mode();
break; break;
case self::ENGINE_MCRYPT: case self::ENGINE_MCRYPT:
$this->cipher_name_mcrypt = 'rijndael-' . ($this->block_size << 3); $this->cipher_name_mcrypt = 'rijndael-' . ($this->block_size << 3);
@ -314,11 +314,11 @@ class Rijndael extends BlockCipher
* @param string $in * @param string $in
* @return string * @return string
*/ */
function _encryptBlock($in) protected function encryptBlock($in)
{ {
static $tables; static $tables;
if (empty($tables)) { if (empty($tables)) {
$tables = &$this->_getTables(); $tables = &$this->getTables();
} }
$t0 = $tables[0]; $t0 = $tables[0];
$t1 = $tables[1]; $t1 = $tables[1];
@ -415,11 +415,11 @@ class Rijndael extends BlockCipher
* @param string $in * @param string $in
* @return string * @return string
*/ */
function _decryptBlock($in) protected function decryptBlock($in)
{ {
static $invtables; static $invtables;
if (empty($invtables)) { if (empty($invtables)) {
$invtables = &$this->_getInvTables(); $invtables = &$this->getInvTables();
} }
$dt0 = $invtables[0]; $dt0 = $invtables[0];
$dt1 = $invtables[1]; $dt1 = $invtables[1];
@ -501,10 +501,10 @@ class Rijndael extends BlockCipher
/** /**
* Setup the key (expansion) * Setup the key (expansion)
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupKey() * @see \phpseclib\Crypt\Common\SymmetricKey::setupKey()
* @access private * @access private
*/ */
function _setupKey() protected function setupKey()
{ {
// Each number in $rcon is equal to the previous number multiplied by two in Rijndael's finite field. // Each number in $rcon is equal to the previous number multiplied by two in Rijndael's finite field.
// See http://en.wikipedia.org/wiki/Finite_field_arithmetic#Multiplicative_inverse // See http://en.wikipedia.org/wiki/Finite_field_arithmetic#Multiplicative_inverse
@ -555,9 +555,9 @@ class Rijndael extends BlockCipher
// 0xFFFFFFFF << 8 == 0xFFFFFF00, but on a 64-bit machine, it equals 0xFFFFFFFF00. as such, doing 'and' // 0xFFFFFFFF << 8 == 0xFFFFFF00, but on a 64-bit machine, it equals 0xFFFFFFFF00. as such, doing 'and'
// with 0xFFFFFFFF (or 0xFFFFFF00) on a 32-bit machine is unnecessary, but on a 64-bit machine, it is. // with 0xFFFFFFFF (or 0xFFFFFF00) on a 32-bit machine is unnecessary, but on a 64-bit machine, it is.
$temp = (($temp << 8) & 0xFFFFFF00) | (($temp >> 24) & 0x000000FF); // rotWord $temp = (($temp << 8) & 0xFFFFFF00) | (($temp >> 24) & 0x000000FF); // rotWord
$temp = $this->_subWord($temp) ^ $rcon[$i / $this->Nk]; $temp = $this->subWord($temp) ^ $rcon[$i / $this->Nk];
} elseif ($this->Nk > 6 && $i % $this->Nk == 4) { } elseif ($this->Nk > 6 && $i % $this->Nk == 4) {
$temp = $this->_subWord($temp); $temp = $this->subWord($temp);
} }
$w[$i] = $w[$i - $this->Nk] ^ $temp; $w[$i] = $w[$i - $this->Nk] ^ $temp;
} }
@ -569,7 +569,7 @@ class Rijndael extends BlockCipher
// 1. Apply the Key Expansion. // 1. Apply the Key Expansion.
// 2. Apply InvMixColumn to all Round Keys except the first and the last one." // 2. Apply InvMixColumn to all Round Keys except the first and the last one."
// also, see fips-197.pdf#page=27, "5.3.5 Equivalent Inverse Cipher" // also, see fips-197.pdf#page=27, "5.3.5 Equivalent Inverse Cipher"
list($dt0, $dt1, $dt2, $dt3) = $this->_getInvTables(); list($dt0, $dt1, $dt2, $dt3) = $this->getInvTables();
$temp = $this->w = $this->dw = []; $temp = $this->w = $this->dw = [];
for ($i = $row = $col = 0; $i < $length; $i++, $col++) { for ($i = $row = $col = 0; $i < $length; $i++, $col++) {
if ($col == $this->Nb) { if ($col == $this->Nb) {
@ -579,7 +579,7 @@ class Rijndael extends BlockCipher
// subWord + invMixColumn + invSubWord = invMixColumn // subWord + invMixColumn + invSubWord = invMixColumn
$j = 0; $j = 0;
while ($j < $this->Nb) { while ($j < $this->Nb) {
$dw = $this->_subWord($this->w[$row][$j]); $dw = $this->subWord($this->w[$row][$j]);
$temp[$j] = $dt0[$dw >> 24 & 0x000000FF] ^ $temp[$j] = $dt0[$dw >> 24 & 0x000000FF] ^
$dt1[$dw >> 16 & 0x000000FF] ^ $dt1[$dw >> 16 & 0x000000FF] ^
$dt2[$dw >> 8 & 0x000000FF] ^ $dt2[$dw >> 8 & 0x000000FF] ^
@ -617,11 +617,11 @@ class Rijndael extends BlockCipher
* @access private * @access private
* @param int $word * @param int $word
*/ */
function _subWord($word) private function subWord($word)
{ {
static $sbox; static $sbox;
if (empty($sbox)) { if (empty($sbox)) {
list(, , , , $sbox) = $this->_getTables(); list(, , , , $sbox) = self::getTables();
} }
return $sbox[$word & 0x000000FF] | return $sbox[$word & 0x000000FF] |
@ -633,13 +633,13 @@ class Rijndael extends BlockCipher
/** /**
* Provides the mixColumns and sboxes tables * Provides the mixColumns and sboxes tables
* *
* @see self::_encryptBlock() * @see self::encryptBlock()
* @see self::_setupInlineCrypt() * @see self::setupInlineCrypt()
* @see self::_subWord() * @see self::subWord()
* @access private * @access private
* @return array &$tables * @return array &$tables
*/ */
function &_getTables() private function &getTables()
{ {
static $tables; static $tables;
if (empty($tables)) { if (empty($tables)) {
@ -722,13 +722,13 @@ class Rijndael extends BlockCipher
/** /**
* Provides the inverse mixColumns and inverse sboxes tables * Provides the inverse mixColumns and inverse sboxes tables
* *
* @see self::_decryptBlock() * @see self::decryptBlock()
* @see self::_setupInlineCrypt() * @see self::setupInlineCrypt()
* @see self::_setupKey() * @see self::setupKey()
* @access private * @access private
* @return array &$tables * @return array &$tables
*/ */
function &_getInvTables() private function &getInvTables()
{ {
static $tables; static $tables;
if (empty($tables)) { if (empty($tables)) {
@ -806,16 +806,16 @@ class Rijndael extends BlockCipher
/** /**
* Setup the performance-optimized function for de/encrypt() * Setup the performance-optimized function for de/encrypt()
* *
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupInlineCrypt() * @see \phpseclib\Crypt\Common\SymmetricKey::setupInlineCrypt()
* @access private * @access private
*/ */
function _setupInlineCrypt() protected function setupInlineCrypt()
{ {
// Note: _setupInlineCrypt() will be called only if $this->changed === true // Note: _setupInlineCrypt() will be called only if $this->changed === true
// So here we are'nt under the same heavy timing-stress as we are in _de/encryptBlock() or de/encrypt(). // So here we are'nt under the same heavy timing-stress as we are in _de/encryptBlock() or de/encrypt().
// However...the here generated function- $code, stored as php callback in $this->inline_crypt, must work as fast as even possible. // However...the here generated function- $code, stored as php callback in $this->inline_crypt, must work as fast as even possible.
$lambda_functions =& self::_getLambdaFunctions(); $lambda_functions =& self::getLambdaFunctions();
// We create max. 10 hi-optimized code for memory reason. Means: For each $key one ultra fast inline-crypt function. // We create max. 10 hi-optimized code for memory reason. Means: For each $key one ultra fast inline-crypt function.
// (Currently, for Crypt_Rijndael/AES, one generated $lambda_function cost on php5.5@32bit ~80kb unfreeable mem and ~130kb on php5.5@64bit) // (Currently, for Crypt_Rijndael/AES, one generated $lambda_function cost on php5.5@32bit ~80kb unfreeable mem and ~130kb on php5.5@64bit)
@ -825,7 +825,7 @@ class Rijndael extends BlockCipher
// Generation of a uniqe hash for our generated code // Generation of a uniqe hash for our generated code
$code_hash = "Crypt_Rijndael, {$this->mode}, {$this->Nr}, {$this->Nb}"; $code_hash = "Crypt_Rijndael, {$this->mode}, {$this->Nr}, {$this->Nb}";
if ($gen_hi_opt_code) { if ($gen_hi_opt_code) {
$code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key); $code_hash = str_pad($code_hash, 32) . $this->hashInlineCryptFunction($this->key);
} }
if (!isset($lambda_functions[$code_hash])) { if (!isset($lambda_functions[$code_hash])) {
@ -842,8 +842,8 @@ class Rijndael extends BlockCipher
$w[] = '$w[' . $i . ']'; $w[] = '$w[' . $i . ']';
$dw[] = '$dw[' . $i . ']'; $dw[] = '$dw[' . $i . ']';
} }
$init_encrypt = '$w = $self->w;'; $init_encrypt = '$w = $this->w;';
$init_decrypt = '$dw = $self->dw;'; $init_decrypt = '$dw = $this->dw;';
} }
$Nr = $this->Nr; $Nr = $this->Nr;
@ -854,7 +854,7 @@ class Rijndael extends BlockCipher
$init_encrypt.= ' $init_encrypt.= '
static $tables; static $tables;
if (empty($tables)) { if (empty($tables)) {
$tables = &$self->_getTables(); $tables = &$this->getTables();
} }
$t0 = $tables[0]; $t0 = $tables[0];
$t1 = $tables[1]; $t1 = $tables[1];
@ -911,7 +911,7 @@ class Rijndael extends BlockCipher
$init_decrypt.= ' $init_decrypt.= '
static $invtables; static $invtables;
if (empty($invtables)) { if (empty($invtables)) {
$invtables = &$self->_getInvTables(); $invtables = &$this->getInvTables();
} }
$dt0 = $invtables[0]; $dt0 = $invtables[0];
$dt1 = $invtables[1]; $dt1 = $invtables[1];
@ -964,7 +964,7 @@ class Rijndael extends BlockCipher
} }
$decrypt_block .= ');'; $decrypt_block .= ');';
$lambda_functions[$code_hash] = $this->_createInlineCryptFunction( $lambda_functions[$code_hash] = $this->createInlineCryptFunction(
[ [
'init_crypt' => '', 'init_crypt' => '',
'init_encrypt' => $init_encrypt, 'init_encrypt' => $init_encrypt,
@ -974,6 +974,7 @@ class Rijndael extends BlockCipher
] ]
); );
} }
$this->inline_crypt = $lambda_functions[$code_hash];
$this->inline_crypt = \Closure::bind($lambda_functions[$code_hash], $this, $this->getClassContext());
} }
} }

View File

@ -66,7 +66,7 @@ class TripleDES extends DES
* @var int * @var int
* @access private * @access private
*/ */
var $key_length = 24; protected $key_length = 24;
/** /**
* The default salt used by setPassword() * The default salt used by setPassword()
@ -76,7 +76,7 @@ class TripleDES extends DES
* @var string * @var string
* @access private * @access private
*/ */
var $password_default_salt = 'phpseclib'; protected $password_default_salt = 'phpseclib';
/** /**
* The mcrypt specific name of the cipher * The mcrypt specific name of the cipher
@ -86,7 +86,7 @@ class TripleDES extends DES
* @var string * @var string
* @access private * @access private
*/ */
var $cipher_name_mcrypt = 'tripledes'; protected $cipher_name_mcrypt = 'tripledes';
/** /**
* Optimizing value while CFB-encrypting * Optimizing value while CFB-encrypting
@ -95,7 +95,7 @@ class TripleDES extends DES
* @var int * @var int
* @access private * @access private
*/ */
var $cfb_init_len = 750; protected $cfb_init_len = 750;
/** /**
* max possible size of $key * max possible size of $key
@ -105,7 +105,7 @@ class TripleDES extends DES
* @var string * @var string
* @access private * @access private
*/ */
var $key_length_max = 24; protected $key_length_max = 24;
/** /**
* Internal flag whether using self::MODE_3CBC or not * Internal flag whether using self::MODE_3CBC or not
@ -113,7 +113,7 @@ class TripleDES extends DES
* @var bool * @var bool
* @access private * @access private
*/ */
var $mode_3cbc; private $mode_3cbc;
/** /**
* The \phpseclib\Crypt\DES objects * The \phpseclib\Crypt\DES objects
@ -123,7 +123,7 @@ class TripleDES extends DES
* @var array * @var array
* @access private * @access private
*/ */
var $des; private $des;
/** /**
* Default Constructor. * Default Constructor.
@ -149,7 +149,7 @@ class TripleDES extends DES
* @param int $mode * @param int $mode
* @access public * @access public
*/ */
function __construct($mode) public function __construct($mode)
{ {
switch ($mode) { switch ($mode) {
// In case of self::MODE_3CBC, we init as CRYPT_DES_MODE_CBC // In case of self::MODE_3CBC, we init as CRYPT_DES_MODE_CBC
@ -186,11 +186,11 @@ class TripleDES extends DES
* @access public * @access public
* @return bool * @return bool
*/ */
function isValidEngine($engine) public function isValidEngine($engine)
{ {
if ($engine == self::ENGINE_OPENSSL) { if ($engine == self::ENGINE_OPENSSL) {
$this->cipher_name_openssl_ecb = 'des-ede3'; $this->cipher_name_openssl_ecb = 'des-ede3';
$mode = $this->_openssl_translate_mode(); $mode = $this->openssl_translate_mode();
$this->cipher_name_openssl = $mode == 'ecb' ? 'des-ede3' : 'des-ede3-' . $mode; $this->cipher_name_openssl = $mode == 'ecb' ? 'des-ede3' : 'des-ede3-' . $mode;
} }
@ -206,7 +206,7 @@ class TripleDES extends DES
* @access public * @access public
* @param string $iv * @param string $iv
*/ */
function setIV($iv) public function setIV($iv)
{ {
parent::setIV($iv); parent::setIV($iv);
if ($this->mode_3cbc) { if ($this->mode_3cbc) {
@ -228,7 +228,7 @@ class TripleDES extends DES
* @throws \LengthException if the key length is invalid * @throws \LengthException if the key length is invalid
* @param int $length * @param int $length
*/ */
function setKeyLength($length) public function setKeyLength($length)
{ {
switch ($length) { switch ($length) {
case 128: case 128:
@ -254,7 +254,7 @@ class TripleDES extends DES
* @throws \LengthException if the key length is invalid * @throws \LengthException if the key length is invalid
* @param string $key * @param string $key
*/ */
function setKey($key) public function setKey($key)
{ {
if ($this->explicit_key_length !== false && strlen($key) != $this->explicit_key_length) { if ($this->explicit_key_length !== false && strlen($key) != $this->explicit_key_length) {
throw new \LengthException('Key length has already been set to ' . $this->explicit_key_length . ' bytes and this key is ' . strlen($key) . ' bytes'); throw new \LengthException('Key length has already been set to ' . $this->explicit_key_length . ' bytes and this key is ' . strlen($key) . ' bytes');
@ -273,7 +273,7 @@ class TripleDES extends DES
$this->key = $key; $this->key = $key;
$this->key_length = strlen($key); $this->key_length = strlen($key);
$this->changed = true; $this->changed = true;
$this->_setEngine(); $this->setEngine();
if ($this->mode_3cbc) { if ($this->mode_3cbc) {
$this->des[0]->setKey(substr($key, 0, 8)); $this->des[0]->setKey(substr($key, 0, 8));
@ -290,7 +290,7 @@ class TripleDES extends DES
* @param string $plaintext * @param string $plaintext
* @return string $cipertext * @return string $cipertext
*/ */
function encrypt($plaintext) public function encrypt($plaintext)
{ {
// parent::en/decrypt() is able to do all the work for all modes and keylengths, // parent::en/decrypt() is able to do all the work for all modes and keylengths,
// except for: self::MODE_3CBC (inner chaining CBC) with a key > 64bits // except for: self::MODE_3CBC (inner chaining CBC) with a key > 64bits
@ -300,7 +300,7 @@ class TripleDES extends DES
return $this->des[2]->encrypt( return $this->des[2]->encrypt(
$this->des[1]->decrypt( $this->des[1]->decrypt(
$this->des[0]->encrypt( $this->des[0]->encrypt(
$this->_pad($plaintext) $this->pad($plaintext)
) )
) )
); );
@ -317,10 +317,10 @@ class TripleDES extends DES
* @param string $ciphertext * @param string $ciphertext
* @return string $plaintext * @return string $plaintext
*/ */
function decrypt($ciphertext) public function decrypt($ciphertext)
{ {
if ($this->mode_3cbc && strlen($this->key) > 8) { if ($this->mode_3cbc && strlen($this->key) > 8) {
return $this->_unpad( return $this->unpad(
$this->des[0]->decrypt( $this->des[0]->decrypt(
$this->des[1]->encrypt( $this->des[1]->encrypt(
$this->des[2]->decrypt( $this->des[2]->decrypt(
@ -372,7 +372,7 @@ class TripleDES extends DES
* @see self::disableContinuousBuffer() * @see self::disableContinuousBuffer()
* @access public * @access public
*/ */
function enableContinuousBuffer() public function enableContinuousBuffer()
{ {
parent::enableContinuousBuffer(); parent::enableContinuousBuffer();
if ($this->mode_3cbc) { if ($this->mode_3cbc) {
@ -391,7 +391,7 @@ class TripleDES extends DES
* @see self::enableContinuousBuffer() * @see self::enableContinuousBuffer()
* @access public * @access public
*/ */
function disableContinuousBuffer() public function disableContinuousBuffer()
{ {
parent::disableContinuousBuffer(); parent::disableContinuousBuffer();
if ($this->mode_3cbc) { if ($this->mode_3cbc) {
@ -404,11 +404,11 @@ class TripleDES extends DES
/** /**
* Creates the key schedule * Creates the key schedule
* *
* @see \phpseclib\Crypt\DES::_setupKey() * @see \phpseclib\Crypt\DES::setupKey()
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupKey() * @see \phpseclib\Crypt\Common\SymmetricKey::setupKey()
* @access private * @access private
*/ */
function _setupKey() protected function setupKey()
{ {
switch (true) { switch (true) {
// if $key <= 64bits we configure our internal pure-php cipher engine // if $key <= 64bits we configure our internal pure-php cipher engine
@ -423,17 +423,17 @@ class TripleDES extends DES
// (only) if 3CBC is used we have, of course, to setup the $des[0-2] keys also separately. // (only) if 3CBC is used we have, of course, to setup the $des[0-2] keys also separately.
if ($this->mode_3cbc) { if ($this->mode_3cbc) {
$this->des[0]->_setupKey(); $this->des[0]->setupKey();
$this->des[1]->_setupKey(); $this->des[1]->setupKey();
$this->des[2]->_setupKey(); $this->des[2]->setupKey();
// because $des[0-2] will, now, do all the work we can return here // because $des[0-2] will, now, do all the work we can return here
// not need unnecessary stress parent::_setupKey() with our, now unused, $key. // not need unnecessary stress parent::setupKey() with our, now unused, $key.
return; return;
} }
} }
// setup our key // setup our key
parent::_setupKey(); parent::setupKey();
} }
/** /**
@ -445,7 +445,7 @@ class TripleDES extends DES
* @access public * @access public
* @return int * @return int
*/ */
function setPreferredEngine($engine) public function setPreferredEngine($engine)
{ {
if ($this->mode_3cbc) { if ($this->mode_3cbc) {
$this->des[0]->setPreferredEngine($engine); $this->des[0]->setPreferredEngine($engine);
@ -455,4 +455,15 @@ class TripleDES extends DES
return parent::setPreferredEngine($engine); return parent::setPreferredEngine($engine);
} }
/**
* Returns the class that defines the private methods
*
* @access private
* @return string
*/
protected function getClassContext()
{
return 'phpseclib\Crypt\DES';
}
} }

View File

@ -56,7 +56,7 @@ class Twofish extends BlockCipher
* @var string * @var string
* @access private * @access private
*/ */
var $cipher_name_mcrypt = 'twofish'; protected $cipher_name_mcrypt = 'twofish';
/** /**
* Optimizing value while CFB-encrypting * Optimizing value while CFB-encrypting
@ -65,7 +65,7 @@ class Twofish extends BlockCipher
* @var int * @var int
* @access private * @access private
*/ */
var $cfb_init_len = 800; protected $cfb_init_len = 800;
/** /**
* Q-Table * Q-Table
@ -73,7 +73,7 @@ class Twofish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $q0 = [ private $q0 = [
0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76,
0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38, 0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38,
0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C, 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C,
@ -114,7 +114,7 @@ class Twofish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $q1 = [ private $q1 = [
0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8,
0x4A, 0xD3, 0xE6, 0x6B, 0x45, 0x7D, 0xE8, 0x4B, 0x4A, 0xD3, 0xE6, 0x6B, 0x45, 0x7D, 0xE8, 0x4B,
0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1, 0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1,
@ -155,7 +155,7 @@ class Twofish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $m0 = [ private $m0 = [
0xBCBC3275, 0xECEC21F3, 0x202043C6, 0xB3B3C9F4, 0xDADA03DB, 0x02028B7B, 0xE2E22BFB, 0x9E9EFAC8, 0xBCBC3275, 0xECEC21F3, 0x202043C6, 0xB3B3C9F4, 0xDADA03DB, 0x02028B7B, 0xE2E22BFB, 0x9E9EFAC8,
0xC9C9EC4A, 0xD4D409D3, 0x18186BE6, 0x1E1E9F6B, 0x98980E45, 0xB2B2387D, 0xA6A6D2E8, 0x2626B74B, 0xC9C9EC4A, 0xD4D409D3, 0x18186BE6, 0x1E1E9F6B, 0x98980E45, 0xB2B2387D, 0xA6A6D2E8, 0x2626B74B,
0x3C3C57D6, 0x93938A32, 0x8282EED8, 0x525298FD, 0x7B7BD437, 0xBBBB3771, 0x5B5B97F1, 0x474783E1, 0x3C3C57D6, 0x93938A32, 0x8282EED8, 0x525298FD, 0x7B7BD437, 0xBBBB3771, 0x5B5B97F1, 0x474783E1,
@ -196,7 +196,7 @@ class Twofish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $m1 = [ private $m1 = [
0xA9D93939, 0x67901717, 0xB3719C9C, 0xE8D2A6A6, 0x04050707, 0xFD985252, 0xA3658080, 0x76DFE4E4, 0xA9D93939, 0x67901717, 0xB3719C9C, 0xE8D2A6A6, 0x04050707, 0xFD985252, 0xA3658080, 0x76DFE4E4,
0x9A084545, 0x92024B4B, 0x80A0E0E0, 0x78665A5A, 0xE4DDAFAF, 0xDDB06A6A, 0xD1BF6363, 0x38362A2A, 0x9A084545, 0x92024B4B, 0x80A0E0E0, 0x78665A5A, 0xE4DDAFAF, 0xDDB06A6A, 0xD1BF6363, 0x38362A2A,
0x0D54E6E6, 0xC6432020, 0x3562CCCC, 0x98BEF2F2, 0x181E1212, 0xF724EBEB, 0xECD7A1A1, 0x6C774141, 0x0D54E6E6, 0xC6432020, 0x3562CCCC, 0x98BEF2F2, 0x181E1212, 0xF724EBEB, 0xECD7A1A1, 0x6C774141,
@ -237,7 +237,7 @@ class Twofish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $m2 = [ private $m2 = [
0xBC75BC32, 0xECF3EC21, 0x20C62043, 0xB3F4B3C9, 0xDADBDA03, 0x027B028B, 0xE2FBE22B, 0x9EC89EFA, 0xBC75BC32, 0xECF3EC21, 0x20C62043, 0xB3F4B3C9, 0xDADBDA03, 0x027B028B, 0xE2FBE22B, 0x9EC89EFA,
0xC94AC9EC, 0xD4D3D409, 0x18E6186B, 0x1E6B1E9F, 0x9845980E, 0xB27DB238, 0xA6E8A6D2, 0x264B26B7, 0xC94AC9EC, 0xD4D3D409, 0x18E6186B, 0x1E6B1E9F, 0x9845980E, 0xB27DB238, 0xA6E8A6D2, 0x264B26B7,
0x3CD63C57, 0x9332938A, 0x82D882EE, 0x52FD5298, 0x7B377BD4, 0xBB71BB37, 0x5BF15B97, 0x47E14783, 0x3CD63C57, 0x9332938A, 0x82D882EE, 0x52FD5298, 0x7B377BD4, 0xBB71BB37, 0x5BF15B97, 0x47E14783,
@ -278,7 +278,7 @@ class Twofish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $m3 = [ private $m3 = [
0xD939A9D9, 0x90176790, 0x719CB371, 0xD2A6E8D2, 0x05070405, 0x9852FD98, 0x6580A365, 0xDFE476DF, 0xD939A9D9, 0x90176790, 0x719CB371, 0xD2A6E8D2, 0x05070405, 0x9852FD98, 0x6580A365, 0xDFE476DF,
0x08459A08, 0x024B9202, 0xA0E080A0, 0x665A7866, 0xDDAFE4DD, 0xB06ADDB0, 0xBF63D1BF, 0x362A3836, 0x08459A08, 0x024B9202, 0xA0E080A0, 0x665A7866, 0xDDAFE4DD, 0xB06ADDB0, 0xBF63D1BF, 0x362A3836,
0x54E60D54, 0x4320C643, 0x62CC3562, 0xBEF298BE, 0x1E12181E, 0x24EBF724, 0xD7A1ECD7, 0x77416C77, 0x54E60D54, 0x4320C643, 0x62CC3562, 0xBEF298BE, 0x1E12181E, 0x24EBF724, 0xD7A1ECD7, 0x77416C77,
@ -319,7 +319,7 @@ class Twofish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $K = []; private $K = [];
/** /**
* The Key depended S-Table 0 * The Key depended S-Table 0
@ -327,7 +327,7 @@ class Twofish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $S0 = []; private $S0 = [];
/** /**
* The Key depended S-Table 1 * The Key depended S-Table 1
@ -335,7 +335,7 @@ class Twofish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $S1 = []; private $S1 = [];
/** /**
* The Key depended S-Table 2 * The Key depended S-Table 2
@ -343,7 +343,7 @@ class Twofish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $S2 = []; private $S2 = [];
/** /**
* The Key depended S-Table 3 * The Key depended S-Table 3
@ -351,7 +351,7 @@ class Twofish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $S3 = []; private $S3 = [];
/** /**
* Holds the last used key * Holds the last used key
@ -359,7 +359,7 @@ class Twofish extends BlockCipher
* @var array * @var array
* @access private * @access private
*/ */
var $kl; private $kl;
/** /**
* The Key Length (in bytes) * The Key Length (in bytes)
@ -368,7 +368,7 @@ class Twofish extends BlockCipher
* @var int * @var int
* @access private * @access private
*/ */
var $key_length = 16; protected $key_length = 16;
/** /**
* Default Constructor. * Default Constructor.
@ -377,7 +377,7 @@ class Twofish extends BlockCipher
* @access public * @access public
* @throws \InvalidArgumentException if an invalid / unsupported mode is provided * @throws \InvalidArgumentException if an invalid / unsupported mode is provided
*/ */
function __construct($mode) public function __construct($mode)
{ {
if ($mode == self::MODE_STREAM) { if ($mode == self::MODE_STREAM) {
throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode'); throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode');
@ -394,7 +394,7 @@ class Twofish extends BlockCipher
* @access public * @access public
* @param int $length * @param int $length
*/ */
function setKeyLength($length) public function setKeyLength($length)
{ {
switch ($length) { switch ($length) {
case 128: case 128:
@ -418,7 +418,7 @@ class Twofish extends BlockCipher
* @param string $key * @param string $key
* @throws \LengthException if the key length isn't supported * @throws \LengthException if the key length isn't supported
*/ */
function setKey($key) public function setKey($key)
{ {
switch (strlen($key)) { switch (strlen($key)) {
case 16: case 16:
@ -438,7 +438,7 @@ class Twofish extends BlockCipher
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupKey() * @see \phpseclib\Crypt\Common\SymmetricKey::_setupKey()
* @access private * @access private
*/ */
function _setupKey() protected function setupKey()
{ {
if (isset($this->kl['key']) && $this->key === $this->kl['key']) { if (isset($this->kl['key']) && $this->key === $this->kl['key']) {
// already expanded // already expanded
@ -460,8 +460,8 @@ class Twofish extends BlockCipher
switch (strlen($this->key)) { switch (strlen($this->key)) {
case 16: case 16:
list($s7, $s6, $s5, $s4) = $this->_mdsrem($le_longs[1], $le_longs[2]); list($s7, $s6, $s5, $s4) = $this->mdsrem($le_longs[1], $le_longs[2]);
list($s3, $s2, $s1, $s0) = $this->_mdsrem($le_longs[3], $le_longs[4]); list($s3, $s2, $s1, $s0) = $this->mdsrem($le_longs[3], $le_longs[4]);
for ($i = 0, $j = 1; $i < 40; $i+= 2, $j+= 2) { for ($i = 0, $j = 1; $i < 40; $i+= 2, $j+= 2) {
$A = $m0[$q0[$q0[$i] ^ $key[ 9]] ^ $key[1]] ^ $A = $m0[$q0[$q0[$i] ^ $key[ 9]] ^ $key[1]] ^
$m1[$q0[$q1[$i] ^ $key[10]] ^ $key[2]] ^ $m1[$q0[$q1[$i] ^ $key[10]] ^ $key[2]] ^
@ -483,9 +483,9 @@ class Twofish extends BlockCipher
} }
break; break;
case 24: case 24:
list($sb, $sa, $s9, $s8) = $this->_mdsrem($le_longs[1], $le_longs[2]); list($sb, $sa, $s9, $s8) = $this->mdsrem($le_longs[1], $le_longs[2]);
list($s7, $s6, $s5, $s4) = $this->_mdsrem($le_longs[3], $le_longs[4]); list($s7, $s6, $s5, $s4) = $this->mdsrem($le_longs[3], $le_longs[4]);
list($s3, $s2, $s1, $s0) = $this->_mdsrem($le_longs[5], $le_longs[6]); list($s3, $s2, $s1, $s0) = $this->mdsrem($le_longs[5], $le_longs[6]);
for ($i = 0, $j = 1; $i < 40; $i+= 2, $j+= 2) { for ($i = 0, $j = 1; $i < 40; $i+= 2, $j+= 2) {
$A = $m0[$q0[$q0[$q1[$i] ^ $key[17]] ^ $key[ 9]] ^ $key[1]] ^ $A = $m0[$q0[$q0[$q1[$i] ^ $key[17]] ^ $key[ 9]] ^ $key[1]] ^
$m1[$q0[$q1[$q1[$i] ^ $key[18]] ^ $key[10]] ^ $key[2]] ^ $m1[$q0[$q1[$q1[$i] ^ $key[18]] ^ $key[10]] ^ $key[2]] ^
@ -507,10 +507,10 @@ class Twofish extends BlockCipher
} }
break; break;
default: // 32 default: // 32
list($sf, $se, $sd, $sc) = $this->_mdsrem($le_longs[1], $le_longs[2]); list($sf, $se, $sd, $sc) = $this->mdsrem($le_longs[1], $le_longs[2]);
list($sb, $sa, $s9, $s8) = $this->_mdsrem($le_longs[3], $le_longs[4]); list($sb, $sa, $s9, $s8) = $this->mdsrem($le_longs[3], $le_longs[4]);
list($s7, $s6, $s5, $s4) = $this->_mdsrem($le_longs[5], $le_longs[6]); list($s7, $s6, $s5, $s4) = $this->mdsrem($le_longs[5], $le_longs[6]);
list($s3, $s2, $s1, $s0) = $this->_mdsrem($le_longs[7], $le_longs[8]); list($s3, $s2, $s1, $s0) = $this->mdsrem($le_longs[7], $le_longs[8]);
for ($i = 0, $j = 1; $i < 40; $i+= 2, $j+= 2) { for ($i = 0, $j = 1; $i < 40; $i+= 2, $j+= 2) {
$A = $m0[$q0[$q0[$q1[$q1[$i] ^ $key[25]] ^ $key[17]] ^ $key[ 9]] ^ $key[1]] ^ $A = $m0[$q0[$q0[$q1[$q1[$i] ^ $key[25]] ^ $key[17]] ^ $key[ 9]] ^ $key[1]] ^
$m1[$q0[$q1[$q1[$q0[$i] ^ $key[26]] ^ $key[18]] ^ $key[10]] ^ $key[2]] ^ $m1[$q0[$q1[$q1[$q0[$i] ^ $key[26]] ^ $key[18]] ^ $key[10]] ^ $key[2]] ^
@ -547,7 +547,7 @@ class Twofish extends BlockCipher
* @param string $B * @param string $B
* @return array * @return array
*/ */
function _mdsrem($A, $B) private function mdsrem($A, $B)
{ {
// No gain by unrolling this loop. // No gain by unrolling this loop.
for ($i = 0; $i < 8; ++$i) { for ($i = 0; $i < 8; ++$i) {
@ -594,7 +594,7 @@ class Twofish extends BlockCipher
* @param string $in * @param string $in
* @return string * @return string
*/ */
function _encryptBlock($in) protected function encryptBlock($in)
{ {
$S0 = $this->S0; $S0 = $this->S0;
$S1 = $this->S1; $S1 = $this->S1;
@ -650,7 +650,7 @@ class Twofish extends BlockCipher
* @param string $in * @param string $in
* @return string * @return string
*/ */
function _decryptBlock($in) protected function decryptBlock($in)
{ {
$S0 = $this->S0; $S0 = $this->S0;
$S1 = $this->S1; $S1 = $this->S1;
@ -705,9 +705,9 @@ class Twofish extends BlockCipher
* @see \phpseclib\Crypt\Common\SymmetricKey::_setupInlineCrypt() * @see \phpseclib\Crypt\Common\SymmetricKey::_setupInlineCrypt()
* @access private * @access private
*/ */
function _setupInlineCrypt() protected function setupInlineCrypt()
{ {
$lambda_functions =& self::_getLambdaFunctions(); $lambda_functions =& self::getLambdaFunctions();
// Max. 10 Ultra-Hi-optimized inline-crypt functions. After that, we'll (still) create very fast code, but not the ultimate fast one. // Max. 10 Ultra-Hi-optimized inline-crypt functions. After that, we'll (still) create very fast code, but not the ultimate fast one.
// (Currently, for Crypt_Twofish, one generated $lambda_function cost on php5.5@32bit ~140kb unfreeable mem and ~240kb on php5.5@64bit) // (Currently, for Crypt_Twofish, one generated $lambda_function cost on php5.5@32bit ~140kb unfreeable mem and ~240kb on php5.5@64bit)
@ -716,7 +716,7 @@ class Twofish extends BlockCipher
// Generation of a unique hash for our generated code // Generation of a unique hash for our generated code
$code_hash = "Crypt_Twofish, {$this->mode}"; $code_hash = "Crypt_Twofish, {$this->mode}";
if ($gen_hi_opt_code) { if ($gen_hi_opt_code) {
$code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key); $code_hash = str_pad($code_hash, 32) . $this->hashInlineCryptFunction($this->key);
} }
if (!isset($lambda_functions[$code_hash])) { if (!isset($lambda_functions[$code_hash])) {
@ -727,10 +727,10 @@ class Twofish extends BlockCipher
static $S0, $S1, $S2, $S3; static $S0, $S1, $S2, $S3;
if (!$S0) { if (!$S0) {
for ($i = 0; $i < 256; ++$i) { for ($i = 0; $i < 256; ++$i) {
$S0[] = (int)$self->S0[$i]; $S0[] = (int)$this->S0[$i];
$S1[] = (int)$self->S1[$i]; $S1[] = (int)$this->S1[$i];
$S2[] = (int)$self->S2[$i]; $S2[] = (int)$this->S2[$i];
$S3[] = (int)$self->S3[$i]; $S3[] = (int)$this->S3[$i];
} }
} }
'; ';
@ -741,11 +741,11 @@ class Twofish extends BlockCipher
$K[] = '$K_' . $i; $K[] = '$K_' . $i;
} }
$init_crypt = ' $init_crypt = '
$S0 = $self->S0; $S0 = $this->S0;
$S1 = $self->S1; $S1 = $this->S1;
$S2 = $self->S2; $S2 = $this->S2;
$S3 = $self->S3; $S3 = $this->S3;
list(' . implode(',', $K) . ') = $self->K; list(' . implode(',', $K) . ') = $this->K;
'; ';
} }
@ -833,7 +833,7 @@ class Twofish extends BlockCipher
'.$K[3].' ^ $R1); '.$K[3].' ^ $R1);
'; ';
$lambda_functions[$code_hash] = $this->_createInlineCryptFunction( $lambda_functions[$code_hash] = $this->createInlineCryptFunction(
[ [
'init_crypt' => $init_crypt, 'init_crypt' => $init_crypt,
'init_encrypt' => '', 'init_encrypt' => '',
@ -843,6 +843,6 @@ class Twofish extends BlockCipher
] ]
); );
} }
$this->inline_crypt = $lambda_functions[$code_hash]; $this->inline_crypt = \Closure::bind($lambda_functions[$code_hash], $this, $this->getClassContext());
} }
} }

View File

@ -1630,8 +1630,8 @@ class SSH2
if ($this->crypto_engine) { if ($this->crypto_engine) {
$this->encrypt->setEngine($this->crypto_engine); $this->encrypt->setEngine($this->crypto_engine);
} }
if ($this->encrypt->block_size) { if ($this->encrypt->getBlockLengthInBytes()) {
$this->encrypt_block_size = $this->encrypt->block_size; $this->encrypt_block_size = $this->encrypt->getBlockLengthInBytes();
} }
$this->encrypt->enableContinuousBuffer(); $this->encrypt->enableContinuousBuffer();
$this->encrypt->disablePadding(); $this->encrypt->disablePadding();
@ -1656,8 +1656,8 @@ class SSH2
if ($this->crypto_engine) { if ($this->crypto_engine) {
$this->decrypt->setEngine($this->crypto_engine); $this->decrypt->setEngine($this->crypto_engine);
} }
if ($this->decrypt->block_size) { if ($this->decrypt->getBlockLengthInBytes()) {
$this->decrypt_block_size = $this->decrypt->block_size; $this->decrypt_block_size = $this->decrypt->getBlockLengthInBytes();
} }
$this->decrypt->enableContinuousBuffer(); $this->decrypt->enableContinuousBuffer();
$this->decrypt->disablePadding(); $this->decrypt->disablePadding();