mirror of
https://github.com/phpseclib/phpseclib.git
synced 2025-01-17 03:39:17 +00:00
Merge branch '3.0'
This commit is contained in:
commit
a01c3915ce
@ -17,6 +17,7 @@ namespace phpseclib3\Common\Functions;
|
|||||||
|
|
||||||
use ParagonIE\ConstantTime\Base64;
|
use ParagonIE\ConstantTime\Base64;
|
||||||
use ParagonIE\ConstantTime\Base64UrlSafe;
|
use ParagonIE\ConstantTime\Base64UrlSafe;
|
||||||
|
use ParagonIE\ConstantTime\Hex;
|
||||||
use phpseclib3\Math\BigInteger;
|
use phpseclib3\Math\BigInteger;
|
||||||
use phpseclib3\Math\Common\FiniteField;
|
use phpseclib3\Math\Common\FiniteField;
|
||||||
|
|
||||||
@ -446,4 +447,30 @@ abstract class Strings
|
|||||||
sodium_bin2base64($data, SODIUM_BASE64_VARIANT_URLSAFE) :
|
sodium_bin2base64($data, SODIUM_BASE64_VARIANT_URLSAFE) :
|
||||||
Base64::encode($data);
|
Base64::encode($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant Time Hex Decoder
|
||||||
|
*
|
||||||
|
* @param string $data
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function hex2bin($data)
|
||||||
|
{
|
||||||
|
return function_exists('sodium_hex2bin') ?
|
||||||
|
sodium_hex2bin($data) :
|
||||||
|
Hex::decode($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant Time Hex Encoder
|
||||||
|
*
|
||||||
|
* @param string $data
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function bin2hex($data)
|
||||||
|
{
|
||||||
|
return function_exists('sodium_bin2hex') ?
|
||||||
|
sodium_bin2hex($data) :
|
||||||
|
Hex::encode($data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace phpseclib3\Crypt\Common\Formats\Keys;
|
namespace phpseclib3\Crypt\Common\Formats\Keys;
|
||||||
|
|
||||||
use ParagonIE\ConstantTime\Hex;
|
|
||||||
use phpseclib3\Common\Functions\Strings;
|
use phpseclib3\Common\Functions\Strings;
|
||||||
use phpseclib3\Crypt\AES;
|
use phpseclib3\Crypt\AES;
|
||||||
use phpseclib3\Crypt\DES;
|
use phpseclib3\Crypt\DES;
|
||||||
@ -129,7 +128,7 @@ abstract class PKCS1 extends PKCS
|
|||||||
|
|
||||||
* OpenSSL is the de facto standard. It's utilized by OpenSSH and other projects */
|
* OpenSSL is the de facto standard. It's utilized by OpenSSH and other projects */
|
||||||
if (preg_match('#DEK-Info: (.+),(.+)#', $key, $matches)) {
|
if (preg_match('#DEK-Info: (.+),(.+)#', $key, $matches)) {
|
||||||
$iv = Hex::decode(trim($matches[2]));
|
$iv = Strings::hex2bin(trim($matches[2]));
|
||||||
// remove the Proc-Type / DEK-Info sections as they're no longer needed
|
// remove the Proc-Type / DEK-Info sections as they're no longer needed
|
||||||
$key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $key);
|
$key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $key);
|
||||||
$ciphertext = ASN1::extractBER($key);
|
$ciphertext = ASN1::extractBER($key);
|
||||||
@ -174,7 +173,7 @@ abstract class PKCS1 extends PKCS
|
|||||||
$iv = Random::string($cipher->getBlockLength() >> 3);
|
$iv = Random::string($cipher->getBlockLength() >> 3);
|
||||||
$cipher->setKey(self::generateSymmetricKey($password, $iv, $cipher->getKeyLength() >> 3));
|
$cipher->setKey(self::generateSymmetricKey($password, $iv, $cipher->getKeyLength() >> 3));
|
||||||
$cipher->setIV($iv);
|
$cipher->setIV($iv);
|
||||||
$iv = strtoupper(Hex::encode($iv));
|
$iv = strtoupper(Strings::bin2hex($iv));
|
||||||
return "-----BEGIN $type PRIVATE KEY-----\r\n" .
|
return "-----BEGIN $type PRIVATE KEY-----\r\n" .
|
||||||
"Proc-Type: 4,ENCRYPTED\r\n" .
|
"Proc-Type: 4,ENCRYPTED\r\n" .
|
||||||
"DEK-Info: " . $encryptionAlgorithm . ",$iv\r\n" .
|
"DEK-Info: " . $encryptionAlgorithm . ",$iv\r\n" .
|
||||||
|
@ -17,7 +17,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace phpseclib3\Crypt\Common\Formats\Keys;
|
namespace phpseclib3\Crypt\Common\Formats\Keys;
|
||||||
|
|
||||||
use ParagonIE\ConstantTime\Hex;
|
|
||||||
use phpseclib3\Common\Functions\Strings;
|
use phpseclib3\Common\Functions\Strings;
|
||||||
use phpseclib3\Crypt\AES;
|
use phpseclib3\Crypt\AES;
|
||||||
use phpseclib3\Crypt\Hash;
|
use phpseclib3\Crypt\Hash;
|
||||||
@ -73,7 +72,7 @@ abstract class PuTTY
|
|||||||
$sequence = 0;
|
$sequence = 0;
|
||||||
while (strlen($symkey) < $length) {
|
while (strlen($symkey) < $length) {
|
||||||
$temp = pack('Na*', $sequence++, $password);
|
$temp = pack('Na*', $sequence++, $password);
|
||||||
$symkey .= Hex::decode(sha1($temp));
|
$symkey .= Strings::hex2bin(sha1($temp));
|
||||||
}
|
}
|
||||||
return substr($symkey, 0, $length);
|
return substr($symkey, 0, $length);
|
||||||
}
|
}
|
||||||
@ -213,7 +212,7 @@ abstract class PuTTY
|
|||||||
$memory = trim(preg_replace('#Argon2-Memory: (\d+)#', '$1', $key[$offset++]));
|
$memory = trim(preg_replace('#Argon2-Memory: (\d+)#', '$1', $key[$offset++]));
|
||||||
$passes = trim(preg_replace('#Argon2-Passes: (\d+)#', '$1', $key[$offset++]));
|
$passes = trim(preg_replace('#Argon2-Passes: (\d+)#', '$1', $key[$offset++]));
|
||||||
$parallelism = trim(preg_replace('#Argon2-Parallelism: (\d+)#', '$1', $key[$offset++]));
|
$parallelism = trim(preg_replace('#Argon2-Parallelism: (\d+)#', '$1', $key[$offset++]));
|
||||||
$salt = Hex::decode(trim(preg_replace('#Argon2-Salt: ([0-9a-f]+)#', '$1', $key[$offset++])));
|
$salt = Strings::hex2bin(trim(preg_replace('#Argon2-Salt: ([0-9a-f]+)#', '$1', $key[$offset++])));
|
||||||
|
|
||||||
extract(self::generateV3Key($password, $flavour, (int)$memory, (int)$passes, $salt));
|
extract(self::generateV3Key($password, $flavour, (int)$memory, (int)$passes, $salt));
|
||||||
|
|
||||||
@ -248,7 +247,7 @@ abstract class PuTTY
|
|||||||
$source .= Strings::packSSH2('s', $private);
|
$source .= Strings::packSSH2('s', $private);
|
||||||
|
|
||||||
$hmac = trim(preg_replace('#Private-MAC: (.+)#', '$1', $key[$offset + $privateLength]));
|
$hmac = trim(preg_replace('#Private-MAC: (.+)#', '$1', $key[$offset + $privateLength]));
|
||||||
$hmac = Hex::decode($hmac);
|
$hmac = Strings::hex2bin($hmac);
|
||||||
|
|
||||||
if (!hash_equals($hash->hash($source), $hmac)) {
|
if (!hash_equals($hash->hash($source), $hmac)) {
|
||||||
throw new \UnexpectedValueException('MAC validation error');
|
throw new \UnexpectedValueException('MAC validation error');
|
||||||
@ -306,7 +305,7 @@ abstract class PuTTY
|
|||||||
$key .= "Argon2-Memory: 8192\r\n";
|
$key .= "Argon2-Memory: 8192\r\n";
|
||||||
$key .= "Argon2-Passes: 13\r\n";
|
$key .= "Argon2-Passes: 13\r\n";
|
||||||
$key .= "Argon2-Parallelism: 1\r\n";
|
$key .= "Argon2-Parallelism: 1\r\n";
|
||||||
$key .= "Argon2-Salt: " . Hex::encode($salt) . "\r\n";
|
$key .= "Argon2-Salt: " . Strings::bin2hex($salt) . "\r\n";
|
||||||
extract(self::generateV3Key($password, 'Argon2id', 8192, 13, $salt));
|
extract(self::generateV3Key($password, 'Argon2id', 8192, 13, $salt));
|
||||||
|
|
||||||
$hash = new Hash('sha256');
|
$hash = new Hash('sha256');
|
||||||
@ -332,7 +331,7 @@ abstract class PuTTY
|
|||||||
$private = Strings::base64_encode($private);
|
$private = Strings::base64_encode($private);
|
||||||
$key .= 'Private-Lines: ' . ((strlen($private) + 63) >> 6) . "\r\n";
|
$key .= 'Private-Lines: ' . ((strlen($private) + 63) >> 6) . "\r\n";
|
||||||
$key .= chunk_split($private, 64);
|
$key .= chunk_split($private, 64);
|
||||||
$key .= 'Private-MAC: ' . Hex::encode($hash->hash($source)) . "\r\n";
|
$key .= 'Private-MAC: ' . Strings::bin2hex($hash->hash($source)) . "\r\n";
|
||||||
|
|
||||||
return $key;
|
return $key;
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace phpseclib3\Crypt\EC\Formats\Keys;
|
namespace phpseclib3\Crypt\EC\Formats\Keys;
|
||||||
|
|
||||||
use ParagonIE\ConstantTime\Hex;
|
|
||||||
use phpseclib3\Common\Functions\Strings;
|
use phpseclib3\Common\Functions\Strings;
|
||||||
use phpseclib3\Crypt\EC\BaseCurves\Base as BaseCurve;
|
use phpseclib3\Crypt\EC\BaseCurves\Base as BaseCurve;
|
||||||
use phpseclib3\Crypt\EC\BaseCurves\Binary as BinaryCurve;
|
use phpseclib3\Crypt\EC\BaseCurves\Binary as BinaryCurve;
|
||||||
@ -249,8 +248,8 @@ trait Common
|
|||||||
$curve->setModulo(...$modulo);
|
$curve->setModulo(...$modulo);
|
||||||
$len = ceil($modulo[0] / 8);
|
$len = ceil($modulo[0] / 8);
|
||||||
$curve->setCoefficients(
|
$curve->setCoefficients(
|
||||||
Hex::encode($data['curve']['a']),
|
Strings::bin2hex($data['curve']['a']),
|
||||||
Hex::encode($data['curve']['b'])
|
Strings::bin2hex($data['curve']['b'])
|
||||||
);
|
);
|
||||||
$point = self::extractPoint("\0" . $data['base'], $curve);
|
$point = self::extractPoint("\0" . $data['base'], $curve);
|
||||||
$curve->setBasePoint(...$point);
|
$curve->setBasePoint(...$point);
|
||||||
@ -294,7 +293,7 @@ trait Common
|
|||||||
// the first byte of a bit string represents the number of bits in the last byte that are to be ignored but,
|
// the first byte of a bit string represents the number of bits in the last byte that are to be ignored but,
|
||||||
// currently, bit strings wanting a non-zero amount of bits trimmed are not supported
|
// currently, bit strings wanting a non-zero amount of bits trimmed are not supported
|
||||||
if (($val = Strings::shift($str)) != "\0") {
|
if (($val = Strings::shift($str)) != "\0") {
|
||||||
throw new \UnexpectedValueException('extractPoint expects the first byte to be null - not ' . Hex::encode($val));
|
throw new \UnexpectedValueException('extractPoint expects the first byte to be null - not ' . Strings::bin2hex($val));
|
||||||
}
|
}
|
||||||
if ($str == "\0") {
|
if ($str == "\0") {
|
||||||
return [];
|
return [];
|
||||||
@ -312,7 +311,7 @@ trait Common
|
|||||||
preg_match("#(.)(.{{$order}})(.{{$order}})#s", $str, $matches);
|
preg_match("#(.)(.{{$order}})(.{{$order}})#s", $str, $matches);
|
||||||
[, $w, $x, $y] = $matches;
|
[, $w, $x, $y] = $matches;
|
||||||
if ($w != "\4") {
|
if ($w != "\4") {
|
||||||
throw new \UnexpectedValueException('The first byte of an uncompressed point should be 04 - not ' . Hex::encode($val));
|
throw new \UnexpectedValueException('The first byte of an uncompressed point should be 04 - not ' . Strings::bin2hex($val));
|
||||||
}
|
}
|
||||||
$point = [
|
$point = [
|
||||||
$curve->convertInteger(new BigInteger($x, 256)),
|
$curve->convertInteger(new BigInteger($x, 256)),
|
||||||
|
@ -26,7 +26,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace phpseclib3\File;
|
namespace phpseclib3\File;
|
||||||
|
|
||||||
use ParagonIE\ConstantTime\Hex;
|
|
||||||
use phpseclib3\Common\Functions\Strings;
|
use phpseclib3\Common\Functions\Strings;
|
||||||
use phpseclib3\Crypt\Common\PrivateKey;
|
use phpseclib3\Crypt\Common\PrivateKey;
|
||||||
use phpseclib3\Crypt\Common\PublicKey;
|
use phpseclib3\Crypt\Common\PublicKey;
|
||||||
@ -1772,7 +1771,7 @@ class X509
|
|||||||
$hash = new Hash('sha1');
|
$hash = new Hash('sha1');
|
||||||
$hash = $hash->hash($dn);
|
$hash = $hash->hash($dn);
|
||||||
extract(unpack('Vhash', $hash));
|
extract(unpack('Vhash', $hash));
|
||||||
return strtolower(Hex::encode(pack('N', $hash)));
|
return strtolower(Strings::bin2hex(pack('N', $hash)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default is to return a string.
|
// Default is to return a string.
|
||||||
|
@ -15,7 +15,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace phpseclib3\Math\BigInteger\Engines;
|
namespace phpseclib3\Math\BigInteger\Engines;
|
||||||
|
|
||||||
use ParagonIE\ConstantTime\Hex;
|
use phpseclib3\Common\Functions\Strings;
|
||||||
use phpseclib3\Exception\BadConfigurationException;
|
use phpseclib3\Exception\BadConfigurationException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -100,7 +100,7 @@ class BCMath extends Engine
|
|||||||
break;
|
break;
|
||||||
case 16:
|
case 16:
|
||||||
$x = (strlen($this->value) & 1) ? '0' . $this->value : $this->value;
|
$x = (strlen($this->value) & 1) ? '0' . $this->value : $this->value;
|
||||||
$temp = new self(Hex::decode($x), 256);
|
$temp = new self(Strings::hex2bin($x), 256);
|
||||||
$this->value = $this->is_negative ? '-' . $temp->value : $temp->value;
|
$this->value = $this->is_negative ? '-' . $temp->value : $temp->value;
|
||||||
$this->is_negative = false;
|
$this->is_negative = false;
|
||||||
break;
|
break;
|
||||||
|
@ -15,7 +15,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace phpseclib3\Math\BigInteger\Engines;
|
namespace phpseclib3\Math\BigInteger\Engines;
|
||||||
|
|
||||||
use ParagonIE\ConstantTime\Hex;
|
|
||||||
use phpseclib3\Common\Functions\Strings;
|
use phpseclib3\Common\Functions\Strings;
|
||||||
use phpseclib3\Crypt\Random;
|
use phpseclib3\Crypt\Random;
|
||||||
use phpseclib3\Exception\BadConfigurationException;
|
use phpseclib3\Exception\BadConfigurationException;
|
||||||
@ -173,7 +172,7 @@ abstract class Engine implements \JsonSerializable
|
|||||||
$is_negative = false;
|
$is_negative = false;
|
||||||
if ($base < 0 && hexdec($x[0]) >= 8) {
|
if ($base < 0 && hexdec($x[0]) >= 8) {
|
||||||
$this->is_negative = $is_negative = true;
|
$this->is_negative = $is_negative = true;
|
||||||
$x = Hex::encode(~Hex::decode($x));
|
$x = Strings::bin2hex(~Strings::hex2bin($x));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->value = $x;
|
$this->value = $x;
|
||||||
@ -267,7 +266,7 @@ abstract class Engine implements \JsonSerializable
|
|||||||
*/
|
*/
|
||||||
public function toHex(bool $twos_compliment = false): string
|
public function toHex(bool $twos_compliment = false): string
|
||||||
{
|
{
|
||||||
return Hex::encode($this->toBytes($twos_compliment));
|
return Strings::bin2hex($this->toBytes($twos_compliment));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,7 +15,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace phpseclib3\Math\BigInteger\Engines;
|
namespace phpseclib3\Math\BigInteger\Engines;
|
||||||
|
|
||||||
use ParagonIE\ConstantTime\Hex;
|
use phpseclib3\Common\Functions\Strings;
|
||||||
use phpseclib3\Exception\BadConfigurationException;
|
use phpseclib3\Exception\BadConfigurationException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,7 +94,7 @@ abstract class PHP extends Engine
|
|||||||
switch (abs($base)) {
|
switch (abs($base)) {
|
||||||
case 16:
|
case 16:
|
||||||
$x = (strlen($this->value) & 1) ? '0' . $this->value : $this->value;
|
$x = (strlen($this->value) & 1) ? '0' . $this->value : $this->value;
|
||||||
$temp = new static(Hex::decode($x), 256);
|
$temp = new static(Strings::hex2bin($x), 256);
|
||||||
$this->value = $temp->value;
|
$this->value = $temp->value;
|
||||||
break;
|
break;
|
||||||
case 10:
|
case 10:
|
||||||
|
@ -22,7 +22,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace phpseclib3\Math\BinaryField;
|
namespace phpseclib3\Math\BinaryField;
|
||||||
|
|
||||||
use ParagonIE\ConstantTime\Hex;
|
use phpseclib3\Common\Functions\Strings;
|
||||||
use phpseclib3\Math\BigInteger;
|
use phpseclib3\Math\BigInteger;
|
||||||
use phpseclib3\Math\BinaryField;
|
use phpseclib3\Math\BinaryField;
|
||||||
use phpseclib3\Math\Common\FiniteField\Integer as Base;
|
use phpseclib3\Math\Common\FiniteField\Integer as Base;
|
||||||
@ -448,7 +448,7 @@ class Integer extends Base
|
|||||||
*/
|
*/
|
||||||
public function toHex(): string
|
public function toHex(): string
|
||||||
{
|
{
|
||||||
return Hex::encode($this->toBytes());
|
return Strings::bin2hex($this->toBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -14,7 +14,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace phpseclib3\Math\PrimeField;
|
namespace phpseclib3\Math\PrimeField;
|
||||||
|
|
||||||
use ParagonIE\ConstantTime\Hex;
|
use phpseclib3\Common\Functions\Strings;
|
||||||
use phpseclib3\Math\BigInteger;
|
use phpseclib3\Math\BigInteger;
|
||||||
use phpseclib3\Math\Common\FiniteField\Integer as Base;
|
use phpseclib3\Math\Common\FiniteField\Integer as Base;
|
||||||
|
|
||||||
@ -302,7 +302,7 @@ class Integer extends Base
|
|||||||
*/
|
*/
|
||||||
public function toHex(): string
|
public function toHex(): string
|
||||||
{
|
{
|
||||||
return Hex::encode($this->toBytes());
|
return Strings::bin2hex($this->toBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user