diff options
Diffstat (limited to 'vendor/phpseclib/phpseclib2_compat/src/Crypt')
12 files changed, 2464 insertions, 0 deletions
diff --git a/vendor/phpseclib/phpseclib2_compat/src/Crypt/AES.php b/vendor/phpseclib/phpseclib2_compat/src/Crypt/AES.php new file mode 100644 index 000000000..0e4da3543 --- /dev/null +++ b/vendor/phpseclib/phpseclib2_compat/src/Crypt/AES.php @@ -0,0 +1,93 @@ +<?php + +/** + * Pure-PHP implementation of AES. + * + * Uses mcrypt, if available/possible, and an internal implementation, otherwise. + * + * PHP version 5 + * + * NOTE: Since AES.php is (for compatibility and phpseclib-historical reasons) virtually + * just a wrapper to Rijndael.php you may consider using Rijndael.php instead of + * to save one include_once(). + * + * If {@link self::setKeyLength() setKeyLength()} isn't called, it'll be calculated from + * {@link self::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits + * it'll be null-padded to 192-bits and 192 bits will be the key length until {@link self::setKey() setKey()} + * is called, again, at which point, it'll be recalculated. + * + * Since \phpseclib\Crypt\AES extends \phpseclib\Crypt\Rijndael, some functions are available to be called that, in the context of AES, don't + * make a whole lot of sense. {@link self::setBlockLength() setBlockLength()}, for instance. Calling that function, + * however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one). + * + * Here's a short example of how to use this library: + * <code> + * <?php + * include 'vendor/autoload.php'; + * + * $aes = new \phpseclib\Crypt\AES(); + * + * $aes->setKey('abcdefghijklmnop'); + * + * $size = 10 * 1024; + * $plaintext = ''; + * for ($i = 0; $i < $size; $i++) { + * $plaintext.= 'a'; + * } + * + * echo $aes->decrypt($aes->encrypt($plaintext)); + * ?> + * </code> + * + * @category Crypt + * @package AES + * @author Jim Wigginton <terrafrost@php.net> + * @copyright 2008 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Crypt; + +/** + * Pure-PHP implementation of AES. + * + * @package AES + * @author Jim Wigginton <terrafrost@php.net> + * @access public + */ +class AES extends Rijndael +{ + /** + * Dummy function + * + * Since \phpseclib\Crypt\AES extends \phpseclib\Crypt\Rijndael, this function is, technically, available, but it doesn't do anything. + * + * @see \phpseclib\Crypt\Rijndael::setBlockLength() + * @access public + * @param int $length + */ + function setBlockLength($length) + { + return; + } + + /** + * Turns key lengths, be they valid or invalid, to valid key lengths + * + * @param int $length + * @access private + * @return int + */ + protected function calculateNewKeyLength($length) + { + switch (true) { + case $length <= 128: + return 128; + case $length <= 192: + return 192; + default: + return 256; + } + } +}
\ No newline at end of file diff --git a/vendor/phpseclib/phpseclib2_compat/src/Crypt/Base.php b/vendor/phpseclib/phpseclib2_compat/src/Crypt/Base.php new file mode 100644 index 000000000..fad69c023 --- /dev/null +++ b/vendor/phpseclib/phpseclib2_compat/src/Crypt/Base.php @@ -0,0 +1,585 @@ +<?php + +/** + * Base Class for all \phpseclib\Crypt\* cipher classes + * + * PHP version 5 + * + * @category Crypt + * @package Base + * @author Jim Wigginton <terrafrost@php.net> + * @author Hans-Juergen Petrich <petrich@tronic-media.com> + * @copyright 2007 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Crypt; + +use phpseclib3\Exception\BadDecryptionException; +use phpseclib3\Exception\InconsistentSetupException; + +/** + * Base Class for all \phpseclib\Crypt\* cipher classes + * + * @package Base + * @author Jim Wigginton <terrafrost@php.net> + * @author Hans-Juergen Petrich <petrich@tronic-media.com> + */ +abstract class Base +{ + /**#@+ + * @access public + * @see \phpseclib\Crypt\Base::encrypt() + * @see \phpseclib\Crypt\Base::decrypt() + */ + /** + * Encrypt / decrypt using the Counter mode. + * + * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode. + * + * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29 + */ + const MODE_CTR = -1; + /** + * Encrypt / decrypt using the Electronic Code Book mode. + * + * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29 + */ + const MODE_ECB = 1; + /** + * Encrypt / decrypt using the Code Book Chaining mode. + * + * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29 + */ + const MODE_CBC = 2; + /** + * Encrypt / decrypt using the Cipher Feedback mode. + * + * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29 + */ + const MODE_CFB = 3; + /** + * Encrypt / decrypt using the Cipher Feedback mode (8bit) + */ + const MODE_CFB8 = 38; + /** + * Encrypt / decrypt using the Output Feedback mode (8bit) + */ + const MODE_OFB8 = 7; + /** + * Encrypt / decrypt using the Output Feedback mode. + * + * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29 + */ + const MODE_OFB = 4; + /** + * Encrypt / decrypt using streaming mode. + */ + const MODE_STREAM = 5; + /** + * Encrypt / decrypt using Galois/Counter mode. + * + * @link https://en.wikipedia.org/wiki/Galois/Counter_Mode + */ + const MODE_GCM = 6; + /**#@-*/ + + /**#@+ + * @access private + * @see \phpseclib3\Crypt\Common\SymmetricKey::__construct() + */ + /** + * Base value for the internal implementation $engine switch + */ + const ENGINE_INTERNAL = 1; + /** + * Base value for the eval() implementation $engine switch + */ + const ENGINE_EVAL = 4; + /** + * Base value for the mcrypt implementation $engine switch + */ + const ENGINE_MCRYPT = 2; + /** + * Base value for the openssl implementation $engine switch + */ + const ENGINE_OPENSSL = 3; + /** + * Base value for the libsodium implementation $engine switch + */ + const ENGINE_LIBSODIUM = 5; + /** + * Base value for the openssl / gcm implementation $engine switch + */ + const ENGINE_OPENSSL_GCM = 6; + /**#@-*/ + + /** + * Engine Map + * + * @access private + * @see \phpseclib3\Crypt\Common\SymmetricKey::getEngine() + */ + const ENGINE_MAP = [ + self::ENGINE_INTERNAL => 'PHP', + self::ENGINE_EVAL => 'Eval', + self::ENGINE_MCRYPT => 'mcrypt', + self::ENGINE_OPENSSL => 'OpenSSL', + self::ENGINE_LIBSODIUM => 'libsodium', + self::ENGINE_OPENSSL_GCM => 'OpenSSL (GCM)' + ]; + + /** + * The Cipher + * + * @var \phpseclib3\Crypt\Common\SymmetricKey + * @access private + */ + protected $cipher; + + /** + * The Key + * + * @see self::setKey() + * @var string + * @access private + */ + protected $key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; + + /** + * Password Parameters + * + * @see self::setPassword() + * @var array + * @access private + */ + protected $password = []; + + /** + * The Key Length (in bytes) + * + * @see self::setKeyLength() + * @var int + * @access private + */ + protected $key_length = 128; + + /** + * Does internal cipher state need to be (re)initialized? + * + * @see self::setKey() + * @var bool + * @access private + */ + private $changed = true; + + /** + * Has the IV been set? + * + * @var bool + * @access private + */ + protected $ivSet = false; + + /** + * Has the key length been explictly set? + * + * @var bool + * @access private + */ + protected $explicit_key_length = false; + + /** + * Default Constructor. + * + * Determines whether or not the mcrypt extension should be used. + * + * $mode could be: + * + * - self::MODE_ECB + * + * - self::MODE_CBC + * + * - self::MODE_CTR + * + * - self::MODE_CFB + * + * - self::MODE_OFB + * + * If not explicitly set, self::MODE_CBC will be used. + * + * @param int $mode + * @access public + */ + public function __construct($mode = self::MODE_CBC) + { + $map = [ + self::MODE_CTR => 'ctr', + self::MODE_ECB => 'ecb', + self::MODE_CBC => 'cbc', + self::MODE_CFB => 'cfb', + self::MODE_CFB8 => 'cfb8', + self::MODE_OFB => 'ofb', + self::MODE_OFB8 => 'ofb8', + self::MODE_GCM => 'gcm', + self::MODE_STREAM => 'stream' + ]; + if (!isset($map[$mode])) { + $mode = self::MODE_CBC; + } + $class = new \ReflectionClass(static::class); + $class = "phpseclib3\\Crypt\\" . $class->getShortName(); + $this->cipher = new $class($map[$mode]); + $this->key_length = $this->cipher->getKeyLength(); + } + + /** + * Sets the initialization vector. (optional) + * + * SetIV is not required when self::MODE_ECB (or ie for AES: \phpseclib\Crypt\AES::MODE_ECB) is being used. If not explicitly set, it'll be assumed + * to be all zero's. + * + * @access public + * @param string $iv + * @internal Can be overwritten by a sub class, but does not have to be + */ + public function setIV($iv) + { + $this->ivSet = true; + + if (!$this->cipher->usesIV()) { + return; + } + + $length = $this->cipher->getBlockLengthInBytes(); + $iv = str_pad(substr($iv, 0, $length), $length, "\0"); + + try { + $this->cipher->setIV($iv); + } catch (\Exception $e) {} + } + + /** + * Sets the key length. + * + * Keys with explicitly set lengths need to be treated accordingly + * + * @access public + * @param int $length + */ + public function setKeyLength($length) + { + // algorithms that have a fixed key length should override this with a method that does nothing + $this->changed = true; + $this->key_length = static::calculateNewKeyLength($length); + $this->explicit_key_length = true; + } + + /** + * Returns the current key length in bits + * + * @access public + * @return int + */ + public function getKeyLength() + { + return $this->key_length; + } + + /** + * Returns the current block length in bits + * + * @access public + * @return int + */ + public function getBlockLength() + { + return $this->cipher->getBlockLength(); + } + + /** + * Sets the key. + * + * The min/max length(s) of the key depends on the cipher which is used. + * If the key not fits the length(s) of the cipher it will paded with null bytes + * up to the closest valid key length. If the key is more than max length, + * we trim the excess bits. + * + * If the key is not explicitly set, it'll be assumed to be all null bytes. + * + * @access public + * @param string $key + * @internal Could, but not must, extend by the child Crypt_* class + */ + public function setKey($key) + { + $this->key = $key; + $this->password = []; + if (!$this->explicit_key_length) { + $this->key_length = static::calculateNewKeyLength(strlen($key) << 3); + } + $this->changed = true; + } + + /** + * Sets the password. + * + * Depending on what $method is set to, setPassword()'s (optional) parameters are as follows: + * {@link http://en.wikipedia.org/wiki/PBKDF2 pbkdf2} or pbkdf1: + * $hash, $salt, $count, $dkLen + * + * Where $hash (default = sha1) currently supports the following hashes: see: Crypt/Hash.php + * + * @see Crypt/Hash.php + * @param string $password + * @param string $method + * @return bool + * @access public + * @internal Could, but not must, extend by the child Crypt_* class + */ + public function setPassword($password, $method = 'pbkdf2') + { + $this->password = func_get_args(); + $this->cipher->setKeyLength($this->key_length); + $this->cipher->setPassword(...func_get_args()); + } + + /** + * Encrypts a message. + * + * $plaintext will be padded with additional bytes such that it's length is a multiple of the block size. Other cipher + * implementations may or may not pad in the same manner. Other common approaches to padding and the reasons why it's + * necessary are discussed in the following + * URL: + * + * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html} + * + * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does. + * strlen($plaintext) will still need to be a multiple of the block size, however, arbitrary values can be added to make it that + * length. + * + * @see self::decrypt() + * @access public + * @param string $plaintext + * @return string $ciphertext + * @internal Could, but not must, extend by the child Crypt_* class + */ + public function encrypt($plaintext) + { + if ($this->changed) { + $this->setup(); + } + + try { + return $this->cipher->encrypt($plaintext); + } catch (\LengthException $e) { + user_error($e->getMessage()); + $this->cipher->enablePadding(); + return $this->cipher->encrypt($plaintext); + } + } + + /** + * Decrypts a message. + * + * If strlen($ciphertext) is not a multiple of the block size, null bytes will be added to the end of the string until + * it is. + * + * @see self::encrypt() + * @access public + * @param string $ciphertext + * @return string $plaintext + * @internal Could, but not must, extend by the child Crypt_* class + */ + public function decrypt($ciphertext) + { + if ($this->changed) { + $this->setup(); + } + + try { + return $this->cipher->decrypt($ciphertext); + } catch (\LengthException $e) { + $len = strlen($ciphertext); + $block_size = $this->cipher->getBlockLengthInBytes(); + $ciphertext = str_pad($ciphertext, $len + ($block_size - $len % $block_size) % $block_size, chr(0)); + try { + return $this->cipher->decrypt($ciphertext); + } catch (BadDecryptionException $e) { + return false; + } + } catch (\Exception $e) { + return false; + } + } + + /** + * Setup IV and key + */ + protected function setup() + { + // we set this just in case it was already set to anything via setPassword() + $temp = $this->explicit_key_length; + $this->setKeyLength($this->key_length); + $this->explicit_key_length = $temp; + if ($this->explicit_key_length) { + $this->cipher->setKeyLength($this->key_length); + } + if (empty($this->password)) { + $key_length = $this->key_length >> 3; + $key = str_pad(substr($this->key, 0, $key_length), $key_length, "\0"); + $this->cipher->setKey($key); + } else { + $this->cipher->setPassword(...$this->password); + } + if (!$this->ivSet) { + $this->setIV(''); + } + $this->changed = false; + } + + /** + * Pad "packets". + * + * Block ciphers working by encrypting between their specified [$this->]block_size at a time + * If you ever need to encrypt or decrypt something that isn't of the proper length, it becomes necessary to + * pad the input so that it is of the proper length. + * + * Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH, + * where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping + * away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is + * transmitted separately) + * + * @see self::disablePadding() + * @access public + */ + public function enablePadding() + { + $this->cipher->enablePadding(); + } + + /** + * Do not pad packets. + * + * @see self::enablePadding() + * @access public + */ + public function disablePadding() + { + $this->cipher->disablePadding(); + } + + /** + * Treat consecutive "packets" as if they are a continuous buffer. + * + * Say you have a 32-byte plaintext $plaintext. Using the default behavior, the two following code snippets + * will yield different outputs: + * + * <code> + * echo $rijndael->encrypt(substr($plaintext, 0, 16)); + * echo $rijndael->encrypt(substr($plaintext, 16, 16)); + * </code> + * <code> + * echo $rijndael->encrypt($plaintext); + * </code> + * + * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates + * another, as demonstrated with the following: + * + * <code> + * $rijndael->encrypt(substr($plaintext, 0, 16)); + * echo $rijndael->decrypt($rijndael->encrypt(substr($plaintext, 16, 16))); + * </code> + * <code> + * echo $rijndael->decrypt($rijndael->encrypt(substr($plaintext, 16, 16))); + * </code> + * + * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different + * outputs. The reason is due to the fact that the initialization vector's change after every encryption / + * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant. + * + * Put another way, when the continuous buffer is enabled, the state of the \phpseclib\Crypt\*() object changes after each + * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that + * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them), + * however, they are also less intuitive and more likely to cause you problems. + * + * @see self::disableContinuousBuffer() + * @access public + * @internal Could, but not must, extend by the child Crypt_* class + */ + public function enableContinuousBuffer() + { + try { + $this->cipher->enableContinuousBuffer(); + } catch (\BadMethodCallException $e) { + user_error($e->getMessage()); + } + } + + /** + * Treat consecutive packets as if they are a discontinuous buffer. + * + * The default behavior. + * + * @see self::enableContinuousBuffer() + * @access public + * @internal Could, but not must, extend by the child Crypt_* class + */ + public function disableContinuousBuffer() + { + $this->cipher->disableContinuousBuffer(); + } + + /** + * Test for engine validity + * + * @see self::__construct() + * @param int $engine + * @access public + * @return bool + */ + public function isValidEngine($engine) + { + $map = self::ENGINE_MAP; + return $this->cipher->isValidEngine($map[$engine]); + } + + /** + * Sets the preferred crypt engine + * + * Currently, $engine could be: + * + * - \phpseclib\Crypt\Base::ENGINE_OPENSSL [very fast] + * + * - \phpseclib\Crypt\Base::ENGINE_MCRYPT [fast] + * + * - \phpseclib\Crypt\Base::ENGINE_INTERNAL [slow] + * + * If the preferred crypt engine is not available the fastest available one will be used + * + * @see self::__construct() + * @param int $engine + * @access public + */ + public function setPreferredEngine($engine) + { + $map = self::ENGINE_MAP; + $this->cipher->setPreferredEngine($map[$engine]); + } + + /** + * Returns the engine currently being utilized + * + * @see self::_setEngine() + * @access public + */ + public function getEngine() + { + static $reverseMap; + if (!isset($reverseMap)) { + $reverseMap = array_flip(self::ENGINE_MAP); + } + return $reverseMap[$this->cipher->getEngine()]; + } +}
\ No newline at end of file diff --git a/vendor/phpseclib/phpseclib2_compat/src/Crypt/Blowfish.php b/vendor/phpseclib/phpseclib2_compat/src/Crypt/Blowfish.php new file mode 100644 index 000000000..f2cb68ee8 --- /dev/null +++ b/vendor/phpseclib/phpseclib2_compat/src/Crypt/Blowfish.php @@ -0,0 +1,66 @@ +<?php + +/** + * Pure-PHP implementation of Blowfish. + * + * Uses mcrypt, if available, and an internal implementation, otherwise. + * + * PHP version 5 + * + * Useful resources are as follows: + * + * - {@link http://en.wikipedia.org/wiki/Blowfish_(cipher) Wikipedia description of Blowfish} + * + * Here's a short example of how to use this library: + * <code> + * <?php + * include 'vendor/autoload.php'; + * + * $blowfish = new \phpseclib\Crypt\Blowfish(); + * + * $blowfish->setKey('12345678901234567890123456789012'); + * + * $plaintext = str_repeat('a', 1024); + * + * echo $blowfish->decrypt($blowfish->encrypt($plaintext)); + * ?> + * </code> + * + * @category Crypt + * @package Blowfish + * @author Jim Wigginton <terrafrost@php.net> + * @author Hans-Juergen Petrich <petrich@tronic-media.com> + * @copyright 2007 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Crypt; + +/** + * Pure-PHP implementation of Blowfish. + * + * @package Blowfish + * @author Jim Wigginton <terrafrost@php.net> + * @access public + */ +class Blowfish extends Base +{ + /** + * Turns key lengths, be they valid or invalid, to valid key lengths + * + * @param int $length + * @access private + * @return int + */ + protected function calculateNewKeyLength($length) + { + switch (true) { + case $length < 32: + return 32; + case $length > 448: + return 448; + } + return $length; + } +}
\ No newline at end of file diff --git a/vendor/phpseclib/phpseclib2_compat/src/Crypt/DES.php b/vendor/phpseclib/phpseclib2_compat/src/Crypt/DES.php new file mode 100644 index 000000000..512c59a27 --- /dev/null +++ b/vendor/phpseclib/phpseclib2_compat/src/Crypt/DES.php @@ -0,0 +1,65 @@ +<?php + +/** + * Pure-PHP implementation of DES. + * + * Uses mcrypt, if available, and an internal implementation, otherwise. + * + * PHP version 5 + * + * Useful resources are as follows: + * + * - {@link http://en.wikipedia.org/wiki/DES_supplementary_material Wikipedia: DES supplementary material} + * - {@link http://www.itl.nist.gov/fipspubs/fip46-2.htm FIPS 46-2 - (DES), Data Encryption Standard} + * - {@link http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-DES.html JavaScript DES Example} + * + * Here's a short example of how to use this library: + * <code> + * <?php + * include 'vendor/autoload.php'; + * + * $des = new \phpseclib\Crypt\DES(); + * + * $des->setKey('abcdefgh'); + * + * $size = 10 * 1024; + * $plaintext = ''; + * for ($i = 0; $i < $size; $i++) { + * $plaintext.= 'a'; + * } + * + * echo $des->decrypt($des->encrypt($plaintext)); + * ?> + * </code> + * + * @category Crypt + * @package DES + * @author Jim Wigginton <terrafrost@php.net> + * @copyright 2007 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Crypt; + +/** + * Pure-PHP implementation of DES. + * + * @package DES + * @author Jim Wigginton <terrafrost@php.net> + * @access public + */ +class DES extends Base +{ + /** + * Turns key lengths, be they valid or invalid, to valid key lengths + * + * @param int $length + * @access private + * @return int + */ + protected function calculateNewKeyLength($length) + { + return 64; + } +}
\ No newline at end of file diff --git a/vendor/phpseclib/phpseclib2_compat/src/Crypt/Hash.php b/vendor/phpseclib/phpseclib2_compat/src/Crypt/Hash.php new file mode 100644 index 000000000..de7942705 --- /dev/null +++ b/vendor/phpseclib/phpseclib2_compat/src/Crypt/Hash.php @@ -0,0 +1,145 @@ +<?php + +/** + * Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions. + * + * Uses hash() or mhash() if available and an internal implementation, otherwise. Currently supports the following: + * + * md2, md5, md5-96, sha1, sha1-96, sha256, sha256-96, sha384, and sha512, sha512-96 + * + * If {@link self::setKey() setKey()} is called, {@link self::hash() hash()} will return the HMAC as opposed to + * the hash. If no valid algorithm is provided, sha1 will be used. + * + * PHP version 5 + * + * {@internal The variable names are the same as those in + * {@link http://tools.ietf.org/html/rfc2104#section-2 RFC2104}.}} + * + * Here's a short example of how to use this library: + * <code> + * <?php + * include 'vendor/autoload.php'; + * + * $hash = new \phpseclib\Crypt\Hash('sha1'); + * + * $hash->setKey('abcdefg'); + * + * echo base64_encode($hash->hash('abcdefg')); + * ?> + * </code> + * + * @category Crypt + * @package Hash + * @author Jim Wigginton <terrafrost@php.net> + * @copyright 2007 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Crypt; + +/** + * Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions. + * + * @package Hash + * @author Jim Wigginton <terrafrost@php.net> + * @access public + */ +class Hash +{ + /**#@+ + * @access private + * @see \phpseclib\Crypt\Hash::__construct() + */ + /** + * Toggles the internal implementation + */ + const MODE_INTERNAL = 1; + /**#@-*/ + + /** + * Hash Object + * + * @see self::setHash() + * @var null|\phpseclib3\Crypt\Hash + * @access private + */ + private $hash; + + /** + * Default Constructor. + * + * @param string $hash + * @return \phpseclib\Crypt\Hash + * @access public + */ + public function __construct($hash = 'sha1') + { + $this->setHash($hash); + } + + /** + * Sets the key for HMACs + * + * Keys can be of any length. + * + * @access public + * @param string $key + */ + public function setKey($key = false) + { + $this->hash->setKey($key); + } + + /** + * Gets the hash function. + * + * As set by the constructor or by the setHash() method. + * + * @access public + * @return string + */ + public function getHash() + { + return $this->hash->getHash(); + } + + /** + * Sets the hash function. + * + * @access public + * @param string $hash + */ + public function setHash($hash) + { + $this->hash = new \phpseclib3\Crypt\Hash; + try { + $this->hash->setHash($hash); + } catch (\phpseclib3\Exception\UnsupportedAlgorithmException $e) { + $this->hash->setHash('sha1'); + } + } + + /** + * Compute the HMAC. + * + * @access public + * @param string $text + * @return string + */ + public function hash($text) + { + return $this->hash->hash($text); + } + + /** + * Returns the hash length (in bytes) + * + * @access public + * @return int + */ + public function getLength() + { + return $this->hash->getLengthInBytes(); + } +}
\ No newline at end of file diff --git a/vendor/phpseclib/phpseclib2_compat/src/Crypt/RC2.php b/vendor/phpseclib/phpseclib2_compat/src/Crypt/RC2.php new file mode 100644 index 000000000..b23f73bca --- /dev/null +++ b/vendor/phpseclib/phpseclib2_compat/src/Crypt/RC2.php @@ -0,0 +1,132 @@ +<?php + +/** + * Pure-PHP implementation of RC2. + * + * Uses mcrypt, if available, and an internal implementation, otherwise. + * + * PHP version 5 + * + * Useful resources are as follows: + * + * - {@link http://tools.ietf.org/html/rfc2268} + * + * Here's a short example of how to use this library: + * <code> + * <?php + * include 'vendor/autoload.php'; + * + * $rc2 = new \phpseclib\Crypt\RC2(); + * + * $rc2->setKey('abcdefgh'); + * + * $plaintext = str_repeat('a', 1024); + * + * echo $rc2->decrypt($rc2->encrypt($plaintext)); + * ?> + * </code> + * + * @category Crypt + * @package RC2 + * @author Patrick Monnerat <pm@datasphere.ch> + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Crypt; + +/** + * Pure-PHP implementation of RC2. + * + * @package RC2 + * @author Jim Wigginton <terrafrost@php.net> + * @access public + */ +class RC2 extends Base +{ + /** + * Default Constructor. + * + * Determines whether or not the mcrypt extension should be used. + * + * $mode could be: + * + * - self::MODE_ECB + * + * - self::MODE_CBC + * + * - self::MODE_CTR + * + * - self::MODE_CFB + * + * - self::MODE_OFB + * + * If not explicitly set, self::MODE_CBC will be used. + * + * @param int $mode + * @access public + */ + public function __construct($mode = self::MODE_CBC) + { + parent::__construct($mode); + $this->key_length = 8; + } + + /** + * Turns key lengths, be they valid or invalid, to valid key lengths + * + * @param int $length + * @access private + * @return int + */ + protected function calculateNewKeyLength($length) + { + switch (true) { + case $length < 8: + return 8; + case $length > 1024: + return 1024; + } + + return $length; + } + + /** + * Setup IV and key + */ + protected function setup() + { + if ($this->explicit_key_length) { + $this->cipher->setKeyLength($this->key_length); + } + $this->cipher->setKey($this->key); + if (!$this->ivSet) { + $this->setIV(''); + } + $this->changed = false; + } + + /** + * Sets the key. + * + * Keys can be of any length. RC2, itself, uses 8 to 1024 bit keys (eg. + * strlen($key) <= 128), however, we only use the first 128 bytes if $key + * has more then 128 bytes in it, and set $key to a single null byte if + * it is empty. + * + * If the key is not explicitly set, it'll be assumed to be a single + * null byte. + * + * @see \phpseclib\Crypt\Base::setKey() + * @access public + * @param string $key + * @param int $t1 optional Effective key length in bits. + */ + function setKey($key, $t1 = 0) + { + parent::setKey($key); + if ($t1) { + $this->setKeyLength($t1); + } + } +}
\ No newline at end of file diff --git a/vendor/phpseclib/phpseclib2_compat/src/Crypt/RC4.php b/vendor/phpseclib/phpseclib2_compat/src/Crypt/RC4.php new file mode 100644 index 000000000..78781f553 --- /dev/null +++ b/vendor/phpseclib/phpseclib2_compat/src/Crypt/RC4.php @@ -0,0 +1,74 @@ +<?php + +/** + * Pure-PHP implementation of RC4. + * + * Uses mcrypt, if available, and an internal implementation, otherwise. + * + * PHP version 5 + * + * Useful resources are as follows: + * + * - {@link http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt ARCFOUR Algorithm} + * - {@link http://en.wikipedia.org/wiki/RC4 - Wikipedia: RC4} + * + * RC4 is also known as ARCFOUR or ARC4. The reason is elaborated upon at Wikipedia. This class is named RC4 and not + * ARCFOUR or ARC4 because RC4 is how it is referred to in the SSH1 specification. + * + * Here's a short example of how to use this library: + * <code> + * <?php + * include 'vendor/autoload.php'; + * + * $rc4 = new \phpseclib\Crypt\RC4(); + * + * $rc4->setKey('abcdefgh'); + * + * $size = 10 * 1024; + * $plaintext = ''; + * for ($i = 0; $i < $size; $i++) { + * $plaintext.= 'a'; + * } + * + * echo $rc4->decrypt($rc4->encrypt($plaintext)); + * ?> + * </code> + * + * @category Crypt + * @package RC4 + * @author Jim Wigginton <terrafrost@php.net> + * @copyright 2007 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Crypt; + +/** + * Pure-PHP implementation of RC4. + * + * @package RC4 + * @author Jim Wigginton <terrafrost@php.net> + * @access public + */ +class RC4 extends Base +{ + /** + * Turns key lengths, be they valid or invalid, to valid key lengths + * + * @param int $length + * @access private + * @return int + */ + protected function calculateNewKeyLength($length) + { + switch (true) { + case $length < 8: + return 8; + case $length > 2048: + return 2048; + } + + return $length; + } +}
\ No newline at end of file diff --git a/vendor/phpseclib/phpseclib2_compat/src/Crypt/RSA.php b/vendor/phpseclib/phpseclib2_compat/src/Crypt/RSA.php new file mode 100644 index 000000000..7a45a0bb5 --- /dev/null +++ b/vendor/phpseclib/phpseclib2_compat/src/Crypt/RSA.php @@ -0,0 +1,948 @@ +<?php + +/** + * Pure-PHP PKCS#1 (v2.1) compliant implementation of RSA. + * + * PHP version 5 + * + * Here's an example of how to encrypt and decrypt text with this library: + * <code> + * <?php + * include 'vendor/autoload.php'; + * + * $rsa = new \phpseclib\Crypt\RSA(); + * extract($rsa->createKey()); + * + * $plaintext = 'terrafrost'; + * + * $rsa->loadKey($privatekey); + * $ciphertext = $rsa->encrypt($plaintext); + * + * $rsa->loadKey($publickey); + * echo $rsa->decrypt($ciphertext); + * ?> + * </code> + * + * Here's an example of how to create signatures and verify signatures with this library: + * <code> + * <?php + * include 'vendor/autoload.php'; + * + * $rsa = new \phpseclib\Crypt\RSA(); + * extract($rsa->createKey()); + * + * $plaintext = 'terrafrost'; + * + * $rsa->loadKey($privatekey); + * $signature = $rsa->sign($plaintext); + * + * $rsa->loadKey($publickey); + * echo $rsa->verify($plaintext, $signature) ? 'verified' : 'unverified'; + * ?> + * </code> + * + * @category Crypt + * @package RSA + * @author Jim Wigginton <terrafrost@php.net> + * @copyright 2009 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Crypt; + +use phpseclib3\Crypt\RSA as RSA2; +use phpseclib3\Crypt\PublicKeyLoader; +use phpseclib3\Crypt\Common\AsymmetricKey; +use phpseclib3\Crypt\Common\PublicKey; +use phpseclib3\Crypt\Common\PrivateKey; +use phpseclib3\Exception\UnsupportedAlgorithmException; +use phpseclib3\Exception\UnsupportedFormatException; +use phpseclib3\Exception\NoKeyLoadedException; +use phpseclib3\Crypt\Common\Formats\Keys\PuTTY; +use phpseclib3\Crypt\Common\Formats\Keys\OpenSSH; +use phpseclib3\Math\BigInteger; +use phpseclib\Math\BigInteger as BigInteger2; + +/** + * Pure-PHP PKCS#1 compliant implementation of RSA. + * + * @package RSA + * @author Jim Wigginton <terrafrost@php.net> + * @access public + */ +class RSA +{ + /**#@+ + * @access public + * @see self::encrypt() + * @see self::decrypt() + */ + /** + * Use {@link http://en.wikipedia.org/wiki/Optimal_Asymmetric_Encryption_Padding Optimal Asymmetric Encryption Padding} + * (OAEP) for encryption / decryption. + * + * Uses sha1 by default. + * + * @see self::setHash() + * @see self::setMGFHash() + */ + const ENCRYPTION_OAEP = 1; + /** + * Use PKCS#1 padding. + * + * Although self::ENCRYPTION_OAEP offers more security, including PKCS#1 padding is necessary for purposes of backwards + * compatibility with protocols (like SSH-1) written before OAEP's introduction. + */ + const ENCRYPTION_PKCS1 = 2; + /** + * Do not use any padding + * + * Although this method is not recommended it can none-the-less sometimes be useful if you're trying to decrypt some legacy + * stuff, if you're trying to diagnose why an encrypted message isn't decrypting, etc. + */ + const ENCRYPTION_NONE = 3; + /**#@-*/ + + /**#@+ + * @access public + * @see self::sign() + * @see self::verify() + * @see self::setHash() + */ + /** + * Use the Probabilistic Signature Scheme for signing + * + * Uses sha1 by default. + * + * @see self::setSaltLength() + * @see self::setMGFHash() + */ + const SIGNATURE_PSS = 1; + /** + * Use the PKCS#1 scheme by default. + * + * Although self::SIGNATURE_PSS offers more security, including PKCS#1 signing is necessary for purposes of backwards + * compatibility with protocols (like SSH-2) written before PSS's introduction. + */ + const SIGNATURE_PKCS1 = 2; + /**#@-*/ + + /**#@+ + * @access public + * @see \phpseclib\Crypt\RSA::createKey() + * @see \phpseclib\Crypt\RSA::setPrivateKeyFormat() + */ + /** + * PKCS#1 formatted private key + * + * Used by OpenSSH + */ + const PRIVATE_FORMAT_PKCS1 = 0; + /** + * PuTTY formatted private key + */ + const PRIVATE_FORMAT_PUTTY = 1; + /** + * XML formatted private key + */ + const PRIVATE_FORMAT_XML = 2; + /** + * PKCS#8 formatted private key + */ + const PRIVATE_FORMAT_PKCS8 = 8; + /** + * OpenSSH formatted private key + */ + const PRIVATE_FORMAT_OPENSSH = 9; + /**#@-*/ + + /**#@+ + * @access public + * @see \phpseclib\Crypt\RSA::createKey() + * @see \phpseclib\Crypt\RSA::setPublicKeyFormat() + */ + /** + * Raw public key + * + * An array containing two \phpseclib\Math\BigInteger objects. + * + * The exponent can be indexed with any of the following: + * + * 0, e, exponent, publicExponent + * + * The modulus can be indexed with any of the following: + * + * 1, n, modulo, modulus + */ + const PUBLIC_FORMAT_RAW = 3; + /** + * PKCS#1 formatted public key (raw) + * + * Used by File/X509.php + * + * Has the following header: + * + * -----BEGIN RSA PUBLIC KEY----- + * + * Analogous to ssh-keygen's pem format (as specified by -m) + */ + const PUBLIC_FORMAT_PKCS1 = 4; + const PUBLIC_FORMAT_PKCS1_RAW = 4; + /** + * XML formatted public key + */ + const PUBLIC_FORMAT_XML = 5; + /** + * OpenSSH formatted public key + * + * Place in $HOME/.ssh/authorized_keys + */ + const PUBLIC_FORMAT_OPENSSH = 6; + /** + * PKCS#1 formatted public key (encapsulated) + * + * Used by PHP's openssl_public_encrypt() and openssl's rsautl (when -pubin is set) + * + * Has the following header: + * + * -----BEGIN PUBLIC KEY----- + * + * Analogous to ssh-keygen's pkcs8 format (as specified by -m). Although PKCS8 + * is specific to private keys it's basically creating a DER-encoded wrapper + * for keys. This just extends that same concept to public keys (much like ssh-keygen) + */ + const PUBLIC_FORMAT_PKCS8 = 7; + /**#@-*/ + + /** + * The Original Key + * + * @see self::getComment() + * @var string + * @access private + */ + private $origKey = null; + + /** + * The Key + * + * @var \phpseclib3\Crypt\Common\AsymmetricKey + * @access private + */ + private $key = null; + + /** + * Password + * + * @var string + * @access private + */ + private $password = false; + + /** + * Private Key Format + * + * @var int + * @access private + */ + private $privateKeyFormat = self::PRIVATE_FORMAT_PKCS1; + + /** + * Public Key Format + * + * @var int + * @access public + */ + private $publicKeyFormat = self::PUBLIC_FORMAT_PKCS1; + + /** + * Public key comment field. + * + * @var string + * @access private + */ + private $comment = 'phpseclib-generated-key'; + + /** + * Encryption mode + * + * @var int + * @access private + */ + private $encryptionMode = self::ENCRYPTION_OAEP; + + /** + * Signature mode + * + * @var int + * @access private + */ + private $signatureMode = self::SIGNATURE_PSS; + + /** + * Hash name + * + * @var string + * @access private + */ + private $hash = 'sha1'; + + /** + * Hash function for the Mask Generation Function + * + * @var string + * @access private + */ + private $mgfHash = 'sha1'; + + /** + * Length of salt + * + * @var int + * @access private + */ + private $sLen; + + /** + * The constructor + * + * @return \phpseclib\Crypt\RSA + * @access public + */ + public function __construct() + { + // don't do anything + } + + /** + * Create public / private key pair + * + * Returns an array with the following three elements: + * - 'privatekey': The private key. + * - 'publickey': The public key. + * - 'partialkey': A partially computed key (if the execution time exceeded $timeout). + * Will need to be passed back to \phpseclib\Crypt\RSA::createKey() as the third parameter for further processing. + * + * @access public + * @param int $bits + */ + public function createKey($bits = 1024) + { + $privatekey = RSA2::createKey($bits); + + return [ + 'privatekey' => $privatekey, + 'publickey' => $privatekey->getPublicKey(), + 'partialkey' => false + ]; + } + + /** + * Returns the key size + * + * More specifically, this returns the size of the modulo in bits. + * + * @access public + * @return int + */ + public function getSize() + { + // for EC and RSA keys this'll return an integer + // for DSA keys this'll return an array (L + N) + return isset($this->key) ? $this->key->getLength() : 0; + } + + /** + * Sets the password + * + * Private keys can be encrypted with a password. To unset the password, pass in the empty string or false. + * Or rather, pass in $password such that empty($password) && !is_string($password) is true. + * + * @see self::createKey() + * @see self::loadKey() + * @access public + * @param string $password + */ + public function setPassword($password = false) + { + $this->password = $password; + } + + /** + * Loads a public or private key + * + * Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed) + * + * @access public + * @param string|RSA|array $key + * @param bool|int $type optional + * @return bool + */ + public function loadKey($key) + { + if ($key instanceof AsymmetricKey) { + $this->key = $key; + } else if ($key instanceof RSA) { + $this->key = $key->key; + } else { + try { + if (is_array($key)) { + foreach ($key as &$value) { + if ($value instanceof BigInteger2) { + $value = new BigInteger($value->toBytes(true), -256); + } + } + } + $this->key = PublicKeyLoader::load($key, $this->password); + } catch (NoKeyLoadedException $e) { + $this->key = $this->origKey = null; + return false; + } + $this->origKey = $key; + } + + // with phpseclib 2.0 loading a key does not reset any of the following + // so we'll need to preserve the old settings whenever a new key is loaded + // with this shim + $this->setEncryptionMode($this->encryptionMode); + //$this->setSignatureMode($this->signatureMode); + $this->setHash($this->hash); + $this->setMGFHash($this->mgfHash); + $this->setSaltLength($this->sLen); + + return true; + } + + /** + * __toString() magic method + * + * @access public + * @return string + */ + public function __toString() + { + PuTTY::setComment($this->comment); + OpenSSH::setComment($this->comment); + + if ($this->key instanceof PublicKey) { + return $this->key->toString(self::const2str($this->publicKeyFormat)); + } + + if ($this->key instanceof PrivateKey) { + try { + return $this->key->toString(self::const2str($this->privateKeyFormat)); + } catch (UnsupportedFormatException $e) { + if ($this->password) { + return $this->key->withPassword()->toString(self::const2str($this->privateKeyFormat)); + } + } + } + + return ''; + } + + /** + * Defines the public key + * + * Some private key formats define the public exponent and some don't. Those that don't define it are problematic when + * used in certain contexts. For example, in SSH-2, RSA authentication works by sending the public key along with a + * message signed by the private key to the server. The SSH-2 server looks the public key up in an index of public keys + * and if it's present then proceeds to verify the signature. Problem is, if your private key doesn't include the public + * exponent this won't work unless you manually add the public exponent. phpseclib tries to guess if the key being used + * is the public key but in the event that it guesses incorrectly you might still want to explicitly set the key as being + * public. + * + * Do note that when a new key is loaded the index will be cleared. + * + * Returns true on success, false on failure + * + * @see self::getPublicKey() + * @access public + * @param string $key optional + * @param int $type optional + * @return bool + */ + public function setPublicKey() + { + return false; + } + + /** + * Defines the private key + * + * If phpseclib guessed a private key was a public key and loaded it as such it might be desirable to force + * phpseclib to treat the key as a private key. This function will do that. + * + * Do note that when a new key is loaded the index will be cleared. + * + * Returns true on success, false on failure + * + * @see self::getPublicKey() + * @access public + * @param string $key optional + * @param int $type optional + * @return bool + */ + public function setPrivateKey($key = false) + { + if ($key === false && $this->key instanceof RSA2) { + $this->key = $this->key->asPrivateKey(); + } + + try { + $key = PublicKeyLoader::load($key); + } catch (NoKeyLoadedException $e) { + return false; + } + if ($key instanceof RSA2) { + $this->key = $key instanceof PublicKey ? $key->asPrivateKey() : $key; + return true; + } + + return false; + } + + /** + * Returns the public key + * + * The public key is only returned under two circumstances - if the private key had the public key embedded within it + * or if the public key was set via setPublicKey(). If the currently loaded key is supposed to be the public key this + * function won't return it since this library, for the most part, doesn't distinguish between public and private keys. + * + * @see self::getPublicKey() + * @access public + * @param string $key + * @param int $type optional + */ + public function getPublicKey($type = self::PUBLIC_FORMAT_PKCS8) + { + PuTTY::setComment($this->comment); + OpenSSH::setComment($this->comment); + + if ($this->key instanceof PrivateKey) { + return $this->key->getPublicKey()->toString(self::const2str($type)); + } + + if ($this->key instanceof PublicKey) { + return $this->key->toString(self::const2str($type)); + } + + return false; + } + + /** + * Returns the public key's fingerprint + * + * The public key's fingerprint is returned, which is equivalent to running `ssh-keygen -lf rsa.pub`. If there is + * no public key currently loaded, false is returned. + * Example output (md5): "c1:b1:30:29:d7:b8:de:6c:97:77:10:d7:46:41:63:87" (as specified by RFC 4716) + * + * @access public + * @param string $algorithm The hashing algorithm to be used. Valid options are 'md5' and 'sha256'. False is returned + * for invalid values. + * @return mixed + */ + public function getPublicKeyFingerprint($algorithm = 'md5') + { + if ($this->key instanceof PublicKey) { + return $this->key->getFingerprint($algorithm); + } + + return false; + } + + /** + * Returns the private key + * + * The private key is only returned if the currently loaded key contains the constituent prime numbers. + * + * @see self::getPublicKey() + * @access public + * @param string $key + * @param int $type optional + * @return mixed + */ + public function getPrivateKey($type = self::PUBLIC_FORMAT_PKCS1) + { + PuTTY::setComment($this->comment); + OpenSSH::setComment($this->comment); + + if ($this->key instanceof PrivateKey) { + try { + return $this->key->toString(self::const2str($this->privateKeyFormat)); + } catch (UnsupportedFormatException $e) { + if ($this->password) { + return $this->key->withPassword()->toString(self::const2str($this->privateKeyFormat)); + } + } + + } + + return false; + } + + /** + * __clone() magic method + * + * @access public + * @return Crypt_RSA + */ + public function __clone() + { + $key = new RSA(); + $key->loadKey($this); + return $key; + } + + /** + * Convert phpseclib 2.0 style constants to phpseclib 3.0 style strings + * + * @param int $const + * @access private + * @return string + */ + private static function const2str($const) + { + switch ($const) { + case self::PRIVATE_FORMAT_PKCS1: + case self::PUBLIC_FORMAT_PKCS1: + return 'PKCS1'; + case self::PRIVATE_FORMAT_PUTTY: + return 'PuTTY'; + case self::PRIVATE_FORMAT_XML: + case self::PUBLIC_FORMAT_XML: + return 'XML'; + case self::PRIVATE_FORMAT_PKCS8: + case self::PUBLIC_FORMAT_PKCS8: + return 'PKCS8'; + case self::PRIVATE_FORMAT_OPENSSH: + case self::PUBLIC_FORMAT_OPENSSH: + return 'OpenSSH'; + } + } + + /** + * Determines the private key format + * + * @see self::createKey() + * @access public + * @param int $format + */ + public function setPrivateKeyFormat($format) + { + $this->privateKeyFormat = $format; + } + + /** + * Determines the public key format + * + * @see self::createKey() + * @access public + * @param int $format + */ + public function setPublicKeyFormat($format) + { + $this->publicKeyFormat = $format; + } + + /** + * Determines which hashing function should be used + * + * Used with signature production / verification and (if the encryption mode is self::ENCRYPTION_OAEP) encryption and + * decryption. If $hash isn't supported, sha1 is used. + * + * @access public + * @param string $hash + */ + public function setHash($hash) + { + $this->hash = $hash; + if ($this->key instanceof AsymmetricKey) { + try { + $this->key = $this->key->withHash($hash); + } catch (UnsupportedAlgorithmException $e) { + $this->key = $this->key->withHash('sha1'); + } + } + } + + /** + * Determines which hashing function should be used for the mask generation function + * + * The mask generation function is used by self::ENCRYPTION_OAEP and self::SIGNATURE_PSS and although it's + * best if Hash and MGFHash are set to the same thing this is not a requirement. + * + * @access public + * @param string $hash + */ + public function setMGFHash($hash) + { + $this->mgfHash = $hash; + if ($this->key instanceof RSA2) { + try { + $this->key = $this->key->withMGFHash($hash); + } catch (UnsupportedAlgorithmException $e) { + $this->key = $this->key->withMGFHash('sha1'); + } + } + } + + /** + * Determines the salt length + * + * To quote from {@link http://tools.ietf.org/html/rfc3447#page-38 RFC3447#page-38}: + * + * Typical salt lengths in octets are hLen (the length of the output + * of the hash function Hash) and 0. + * + * @access public + * @param int $format + */ + public function setSaltLength($sLen) + { + $this->sLen = $sLen; + if ($this->key instanceof RSA2) { + $this->key = $this->key->withSaltLength($sLen); + } + } + + /** + * Set Encryption Mode + * + * Valid values include self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1. + * + * @access public + * @param int $mode + */ + public function setEncryptionMode($mode) + { + $this->encryptionMode = $mode; + if ($this->key instanceof RSA2) { + $this->key = $this->key->withPadding( + self::enc2pad($this->encryptionMode) | + self::sig2pad($this->signatureMode) + ); + } + } + + /** + * Set Signature Mode + * + * Valid values include self::SIGNATURE_PSS and self::SIGNATURE_PKCS1 + * + * @access public + * @param int $mode + */ + public function setSignatureMode($mode) + { + $this->signatureMode = $mode; + if ($this->key instanceof RSA2) { + $this->key = $this->key->withPadding( + self::enc2pad($this->encryptionMode) | + self::sig2pad($this->signatureMode) + ); + } + } + + /** + * Convert phpseclib 2.0 style constants to phpseclib 3.0 style constants + * + * @param int $mode + * @access private + * @return int + */ + private function enc2pad($mode) + { + switch ($mode) { + case self::ENCRYPTION_PKCS1: + return RSA2::ENCRYPTION_PKCS1; + case self::ENCRYPTION_NONE: + return RSA2::ENCRYPTION_NONE; + //case self::ENCRYPTION_OAEP: + default: + return RSA2::ENCRYPTION_OAEP; + } + } + + /** + * Convert phpseclib 2.0 style constants to phpseclib 3.0 style constants + * + * @param int $mode + * @access private + * @return int + */ + private function sig2pad($mode) + { + switch ($mode) { + case self::SIGNATURE_PKCS1: + return RSA2::SIGNATURE_PKCS1; + //case self::SIGNATURE_PSS: + default: + return RSA2::SIGNATURE_PSS; + } + } + + /** + * Set public key comment. + * + * @access public + * @param string $comment + */ + public function setComment($comment) + { + $this->comment = $comment; + } + + /** + * Get public key comment. + * + * @access public + * @return string + */ + public function getComment() + { + // we'd need to make the load method in the parent PuTTY and OpenSSH classes public instead of protected + // for this to work + try { + $key = PuTTY::load($this->origKey); + return $key['comment']; + } catch (\Exception $e) {} + + try { + $key = OpenSSH::load($this->origKey); + return $key['comment']; + } catch (\Exception $e) {} + + return ''; + } + + /** + * Encryption + * + * Both self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1 both place limits on how long $plaintext can be. + * If $plaintext exceeds those limits it will be broken up so that it does and the resultant ciphertext's will + * be concatenated together. + * + * @see self::decrypt() + * @access public + * @param string $plaintext + * @return string + */ + public function encrypt($plaintext) + { + if (!$this->key instanceof RSA2) { + return false; + } + $key = $this->key; + if ($key instanceof PrivateKey) { + $key = $key->toString('Raw'); + $temp = new static(); + $temp->loadKey(['e' => $key['d'], 'n' => $key['n']]); + $key = $temp->key; + } + if ($key instanceof PublicKey) { + switch ($this->encryptionMode) { + case self::ENCRYPTION_PKCS1: + $len = ($key->getLength() - 88) >> 3; + break; + case self::ENCRYPTION_NONE: + $len = $key->getLength() >> 3; + break; + //case self::ENCRYPTION_OAEP: + default: + $len = ($key->getLength() - 2 * $key->getHash()->getLength() - 16) >> 3; + } + $plaintext = str_split($plaintext, $len); + $ciphertext = ''; + foreach ($plaintext as $m) { + $ciphertext.= $key->encrypt($m); + } + return $ciphertext; + } + + return false; + } + + /** + * Decryption + * + * @see self::encrypt() + * @access public + * @param string $plaintext + * @return string + */ + public function decrypt($ciphertext) + { + if (!$this->key instanceof RSA2) { + return false; + } + $key = $this->key; + if ($key instanceof PublicKey) { + $key = $key->asPrivateKey(); + } + if ($key instanceof PrivateKey) { + $len = $key->getLength() >> 3; + $ciphertext = str_split($ciphertext, $len); + $ciphertext[count($ciphertext) - 1] = str_pad($ciphertext[count($ciphertext) - 1], $len, chr(0), STR_PAD_LEFT); + + $plaintext = ''; + foreach ($ciphertext as $c) { + try { + $plaintext.= $key->decrypt($c); + } catch (\Exception $e) { + return false; + } + } + return $plaintext; + } + + return false; + } + + /** + * Create a signature + * + * @see self::verify() + * @access public + * @param string $message + * @return string + */ + public function sign($message) + { + if ($this->key instanceof PrivateKey) { + return $this->key->sign($message); + } + + return false; + } + + /** + * Verifies a signature + * + * @see self::sign() + * @access public + * @param string $message + * @param string $signature + * @return bool + */ + public function verify($message, $signature) + { + if ($this->key instanceof PublicKey) { + return $this->key->verify($message, $signature); + } + + return false; + } + + /** + * Returns a public key object + * + * @access public + * @return AsymmetricKey|false + */ + public function getKeyObject() + { + return $this->key; + } +}
\ No newline at end of file diff --git a/vendor/phpseclib/phpseclib2_compat/src/Crypt/Random.php b/vendor/phpseclib/phpseclib2_compat/src/Crypt/Random.php new file mode 100644 index 000000000..49a281b3d --- /dev/null +++ b/vendor/phpseclib/phpseclib2_compat/src/Crypt/Random.php @@ -0,0 +1,36 @@ +<?php + +/** + * Random Number Generator + * + * PHP version 5 + * + * Here's a short example of how to use this library: + * <code> + * <?php + * include 'vendor/autoload.php'; + * + * echo bin2hex(\phpseclib\Crypt\Random::string(8)); + * ?> + * </code> + * + * @category Crypt + * @package Random + * @author Jim Wigginton <terrafrost@php.net> + * @copyright 2007 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Crypt; + +/** + * Pure-PHP Random Number Generator + * + * @package Random + * @author Jim Wigginton <terrafrost@php.net> + * @access public + */ +class Random extends \phpseclib3\Crypt\Random +{ +}
\ No newline at end of file diff --git a/vendor/phpseclib/phpseclib2_compat/src/Crypt/Rijndael.php b/vendor/phpseclib/phpseclib2_compat/src/Crypt/Rijndael.php new file mode 100644 index 000000000..f3b9a7df8 --- /dev/null +++ b/vendor/phpseclib/phpseclib2_compat/src/Crypt/Rijndael.php @@ -0,0 +1,137 @@ +<?php + +/** + * Pure-PHP implementation of Rijndael. + * + * Uses mcrypt, if available/possible, and an internal implementation, otherwise. + * + * PHP version 5 + * + * If {@link self::setBlockLength() setBlockLength()} isn't called, it'll be assumed to be 128 bits. If + * {@link self::setKeyLength() setKeyLength()} isn't called, it'll be calculated from + * {@link self::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's + * 136-bits it'll be null-padded to 192-bits and 192 bits will be the key length until + * {@link self::setKey() setKey()} is called, again, at which point, it'll be recalculated. + * + * Not all Rijndael implementations may support 160-bits or 224-bits as the block length / key length. mcrypt, for example, + * does not. AES, itself, only supports block lengths of 128 and key lengths of 128, 192, and 256. + * {@link http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=10 Rijndael-ammended.pdf#page=10} defines the + * algorithm for block lengths of 192 and 256 but not for block lengths / key lengths of 160 and 224. Indeed, 160 and 224 + * are first defined as valid key / block lengths in + * {@link http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=44 Rijndael-ammended.pdf#page=44}: + * Extensions: Other block and Cipher Key lengths. + * Note: Use of 160/224-bit Keys must be explicitly set by setKeyLength(160) respectively setKeyLength(224). + * + * {@internal The variable names are the same as those in + * {@link http://www.csrc.nist.gov/publications/fips/fips197/fips-197.pdf#page=10 fips-197.pdf#page=10}.}} + * + * Here's a short example of how to use this library: + * <code> + * <?php + * include 'vendor/autoload.php'; + * + * $rijndael = new \phpseclib\Crypt\Rijndael(); + * + * $rijndael->setKey('abcdefghijklmnop'); + * + * $size = 10 * 1024; + * $plaintext = ''; + * for ($i = 0; $i < $size; $i++) { + * $plaintext.= 'a'; + * } + * + * echo $rijndael->decrypt($rijndael->encrypt($plaintext)); + * ?> + * </code> + * + * @category Crypt + * @package Rijndael + * @author Jim Wigginton <terrafrost@php.net> + * @copyright 2008 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Crypt; + +/** + * Pure-PHP implementation of Rijndael. + * + * @package Rijndael + * @author Jim Wigginton <terrafrost@php.net> + * @access public + */ +class Rijndael extends Base +{ + /** + * Sets the block length + * + * Valid block lengths are 128, 160, 192, 224, and 256. If the length is less than 128, it will be rounded up to + * 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount. + * + * @access public + * @param int $length + */ + public function setBlockLength($length) + { + $length >>= 5; + if ($length > 8) { + $length = 8; + } elseif ($length < 4) { + $length = 4; + } + $this->cipher->setBlockLength($length); + } + + /** + * Turns key lengths, be they valid or invalid, to valid key lengths + * + * @param int $length + * @access private + * @return int + */ + protected function calculateNewKeyLength($length) + { + switch (true) { + case $length <= 128: + return 128; + case $length <= 160: + return 160; + case $length <= 192: + return 192; + case $length <= 224: + return 224; + default: + return 256; + } + } + + /** + * Sets the password. + * + * Depending on what $method is set to, setPassword()'s (optional) parameters are as follows: + * {@link http://en.wikipedia.org/wiki/PBKDF2 pbkdf2} or pbkdf1: + * $hash, $salt, $count, $dkLen + * + * Where $hash (default = sha1) currently supports the following hashes: see: Crypt/Hash.php + * + * @see Crypt/Hash.php + * @param string $password + * @param string $method + * @return bool + * @access public + * @internal Could, but not must, extend by the child Crypt_* class + */ + public function setPassword($password, $method = 'pbkdf2') + { + $this->cipher->setKeyLength($this->key_length); + $args = func_get_args(); + if (in_array($method, ['pbkdf1', 'pbkdf2']) && !isset($args[3])) { + $args[1] = $method; + $args[2] = isset($args[2]) ? $args[2] : 'sha1'; + $args[3] = 'phpseclib'; + } + $this->password = $args; + $this->cipher->setPassword(...$args); + } +}
\ No newline at end of file diff --git a/vendor/phpseclib/phpseclib2_compat/src/Crypt/TripleDES.php b/vendor/phpseclib/phpseclib2_compat/src/Crypt/TripleDES.php new file mode 100644 index 000000000..d66dc74d6 --- /dev/null +++ b/vendor/phpseclib/phpseclib2_compat/src/Crypt/TripleDES.php @@ -0,0 +1,116 @@ +<?php + +/** + * Pure-PHP implementation of Triple DES. + * + * Uses mcrypt, if available, and an internal implementation, otherwise. Operates in the EDE3 mode (encrypt-decrypt-encrypt). + * + * PHP version 5 + * + * Here's a short example of how to use this library: + * <code> + * <?php + * include 'vendor/autoload.php'; + * + * $des = new \phpseclib\Crypt\TripleDES(); + * + * $des->setKey('abcdefghijklmnopqrstuvwx'); + * + * $size = 10 * 1024; + * $plaintext = ''; + * for ($i = 0; $i < $size; $i++) { + * $plaintext.= 'a'; + * } + * + * echo $des->decrypt($des->encrypt($plaintext)); + * ?> + * </code> + * + * @category Crypt + * @package TripleDES + * @author Jim Wigginton <terrafrost@php.net> + * @copyright 2007 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Crypt; + +/** + * Pure-PHP implementation of Triple DES. + * + * @package TripleDES + * @author Jim Wigginton <terrafrost@php.net> + * @access public + */ +class TripleDES extends Base +{ + /** + * Encrypt / decrypt using inner chaining + * + * Inner chaining is used by SSH-1 and is generally considered to be less secure then outer chaining (self::MODE_CBC3). + */ + const MODE_3CBC = -2; + + /** + * Encrypt / decrypt using outer chaining + * + * Outer chaining is used by SSH-2 and when the mode is set to \phpseclib\Crypt\Base::MODE_CBC. + */ + const MODE_CBC3 = Base::MODE_CBC; + + /** + * Default Constructor. + * + * Determines whether or not the mcrypt extension should be used. + * + * $mode could be: + * + * - \phpseclib\Crypt\Base::MODE_ECB + * + * - \phpseclib\Crypt\Base::MODE_CBC + * + * - \phpseclib\Crypt\Base::MODE_CTR + * + * - \phpseclib\Crypt\Base::MODE_CFB + * + * - \phpseclib\Crypt\Base::MODE_OFB + * + * - \phpseclib\Crypt\TripleDES::MODE_3CBC + * + * If not explicitly set, \phpseclib\Crypt\Base::MODE_CBC will be used. + * + * @see \phpseclib\Crypt\DES::__construct() + * @see \phpseclib\Crypt\Base::__construct() + * @param int $mode + * @access public + */ + public function __construct($mode = self::MODE_CBC) + { + if ($mode == self::MODE_3CBC) { + $this->cipher = new \phpseclib3\Crypt\TripleDES('3cbc'); + $this->key_length = $this->cipher->getKeyLength(); + return; + } + parent::__construct($mode); + } + + /** + * Turns key lengths, be they valid or invalid, to valid key lengths + * + * @param int $length + * @access private + * @return int + */ + protected function calculateNewKeyLength($length) + { + switch (true) { + case $length <= 64: + return 64; + case $length <= 128: + return 128; + default: + return 192; + } + } +}
\ No newline at end of file diff --git a/vendor/phpseclib/phpseclib2_compat/src/Crypt/Twofish.php b/vendor/phpseclib/phpseclib2_compat/src/Crypt/Twofish.php new file mode 100644 index 000000000..f57c54c9d --- /dev/null +++ b/vendor/phpseclib/phpseclib2_compat/src/Crypt/Twofish.php @@ -0,0 +1,67 @@ +<?php + +/** + * Pure-PHP implementation of Twofish. + * + * Uses mcrypt, if available, and an internal implementation, otherwise. + * + * PHP version 5 + * + * Useful resources are as follows: + * + * - {@link http://en.wikipedia.org/wiki/Twofish Wikipedia description of Twofish} + * + * Here's a short example of how to use this library: + * <code> + * <?php + * include 'vendor/autoload.php'; + * + * $twofish = new \phpseclib\Crypt\Twofish(); + * + * $twofish->setKey('12345678901234567890123456789012'); + * + * $plaintext = str_repeat('a', 1024); + * + * echo $twofish->decrypt($twofish->encrypt($plaintext)); + * ?> + * </code> + * + * @category Crypt + * @package Twofish + * @author Jim Wigginton <terrafrost@php.net> + * @author Hans-Juergen Petrich <petrich@tronic-media.com> + * @copyright 2007 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link http://phpseclib.sourceforge.net + */ + +namespace phpseclib\Crypt; + +/** + * Pure-PHP implementation of Twofish. + * + * @package Twofish + * @author Jim Wigginton <terrafrost@php.net> + * @access public + */ +class Twofish extends Base +{ + /** + * Turns key lengths, be they valid or invalid, to valid key lengths + * + * @param int $length + * @access private + * @return int + */ + protected function calculateNewKeyLength($length) + { + switch (true) { + case $length <= 128: + return 128; + case $length <= 192: + return 192; + default: + return 256; + } + } +}
\ No newline at end of file |