use libsodium's hex (en|de)coding if available

This commit is contained in:
terrafrost 2022-08-19 10:09:38 -05:00
parent e5396968c5
commit 13b241e3e9
10 changed files with 49 additions and 27 deletions

View File

@ -15,6 +15,7 @@ namespace phpseclib3\Common\Functions;
use ParagonIE\ConstantTime\Base64;
use ParagonIE\ConstantTime\Base64UrlSafe;
use ParagonIE\ConstantTime\Hex;
use phpseclib3\Math\BigInteger;
use phpseclib3\Math\Common\FiniteField;
@ -475,4 +476,30 @@ abstract class Strings
sodium_bin2base64($data, SODIUM_BASE64_VARIANT_URLSAFE) :
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);
}
}

View File

@ -13,7 +13,6 @@
namespace phpseclib3\Crypt\Common\Formats\Keys;
use ParagonIE\ConstantTime\Hex;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Crypt\AES;
use phpseclib3\Crypt\DES;
@ -137,7 +136,7 @@ abstract class PKCS1 extends PKCS
* OpenSSL is the de facto standard. It's utilized by OpenSSH and other projects */
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
$key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $key);
$ciphertext = ASN1::extractBER($key);
@ -185,7 +184,7 @@ abstract class PKCS1 extends PKCS
$iv = Random::string($cipher->getBlockLength() >> 3);
$cipher->setKey(self::generateSymmetricKey($password, $iv, $cipher->getKeyLength() >> 3));
$cipher->setIV($iv);
$iv = strtoupper(Hex::encode($iv));
$iv = strtoupper(Strings::bin2hex($iv));
return "-----BEGIN $type PRIVATE KEY-----\r\n" .
"Proc-Type: 4,ENCRYPTED\r\n" .
"DEK-Info: " . $encryptionAlgorithm . ",$iv\r\n" .

View File

@ -15,7 +15,6 @@
namespace phpseclib3\Crypt\Common\Formats\Keys;
use ParagonIE\ConstantTime\Hex;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Crypt\AES;
use phpseclib3\Crypt\Hash;
@ -79,7 +78,7 @@ abstract class PuTTY
$sequence = 0;
while (strlen($symkey) < $length) {
$temp = pack('Na*', $sequence++, $password);
$symkey .= Hex::decode(sha1($temp));
$symkey .= Strings::hex2bin(sha1($temp));
}
return substr($symkey, 0, $length);
}
@ -226,7 +225,7 @@ abstract class PuTTY
$memory = trim(preg_replace('#Argon2-Memory: (\d+)#', '$1', $key[$offset++]));
$passes = trim(preg_replace('#Argon2-Passes: (\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, $memory, $passes, $salt));
@ -261,7 +260,7 @@ abstract class PuTTY
$source .= Strings::packSSH2('s', $private);
$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)) {
throw new \UnexpectedValueException('MAC validation error');
@ -323,7 +322,7 @@ abstract class PuTTY
$key .= "Argon2-Memory: 8192\r\n";
$key .= "Argon2-Passes: 13\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));
$hash = new Hash('sha256');
@ -349,7 +348,7 @@ abstract class PuTTY
$private = Strings::base64_encode($private);
$key .= 'Private-Lines: ' . ((strlen($private) + 63) >> 6) . "\r\n";
$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;
}

View File

@ -13,7 +13,6 @@
namespace phpseclib3\Crypt\EC\Formats\Keys;
use ParagonIE\ConstantTime\Hex;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Crypt\EC\BaseCurves\Base as BaseCurve;
use phpseclib3\Crypt\EC\BaseCurves\Binary as BinaryCurve;
@ -250,8 +249,8 @@ trait Common
$curve->setModulo(...$modulo);
$len = ceil($modulo[0] / 8);
$curve->setCoefficients(
Hex::encode($data['curve']['a']),
Hex::encode($data['curve']['b'])
Strings::bin2hex($data['curve']['a']),
Strings::bin2hex($data['curve']['b'])
);
$point = self::extractPoint("\0" . $data['base'], $curve);
$curve->setBasePoint(...$point);
@ -297,7 +296,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,
// currently, bit strings wanting a non-zero amount of bits trimmed are not supported
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") {
return [];
@ -315,7 +314,7 @@ trait Common
preg_match("#(.)(.{{$order}})(.{{$order}})#s", $str, $matches);
list(, $w, $x, $y) = $matches;
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 = [
$curve->convertInteger(new BigInteger($x, 256)),

View File

@ -24,7 +24,6 @@
namespace phpseclib3\File;
use ParagonIE\ConstantTime\Hex;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Crypt\Common\PrivateKey;
use phpseclib3\Crypt\Common\PublicKey;
@ -1844,7 +1843,7 @@ class X509
$hash = new Hash('sha1');
$hash = $hash->hash($dn);
extract(unpack('Vhash', $hash));
return strtolower(Hex::encode(pack('N', $hash)));
return strtolower(Strings::bin2hex(pack('N', $hash)));
}
// Default is to return a string.

View File

@ -13,7 +13,7 @@
namespace phpseclib3\Math\BigInteger\Engines;
use ParagonIE\ConstantTime\Hex;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Exception\BadConfigurationException;
/**
@ -103,7 +103,7 @@ class BCMath extends Engine
break;
case 16:
$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->is_negative = false;
break;

View File

@ -13,7 +13,6 @@
namespace phpseclib3\Math\BigInteger\Engines;
use ParagonIE\ConstantTime\Hex;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Crypt\Random;
use phpseclib3\Exception\BadConfigurationException;
@ -172,7 +171,7 @@ abstract class Engine implements \JsonSerializable
$is_negative = false;
if ($base < 0 && hexdec($x[0]) >= 8) {
$this->is_negative = $is_negative = true;
$x = Hex::encode(~Hex::decode($x));
$x = Strings::bin2hex(~Strings::hex2bin($x));
}
$this->value = $x;
@ -270,7 +269,7 @@ abstract class Engine implements \JsonSerializable
*/
public function toHex($twos_compliment = false)
{
return Hex::encode($this->toBytes($twos_compliment));
return Strings::bin2hex($this->toBytes($twos_compliment));
}
/**

View File

@ -13,7 +13,7 @@
namespace phpseclib3\Math\BigInteger\Engines;
use ParagonIE\ConstantTime\Hex;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Exception\BadConfigurationException;
/**
@ -95,7 +95,7 @@ abstract class PHP extends Engine
switch (abs($base)) {
case 16:
$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;
break;
case 10:

View File

@ -20,7 +20,7 @@
namespace phpseclib3\Math\BinaryField;
use ParagonIE\ConstantTime\Hex;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Math\BigInteger;
use phpseclib3\Math\BinaryField;
use phpseclib3\Math\Common\FiniteField\Integer as Base;
@ -472,7 +472,7 @@ class Integer extends Base
*/
public function toHex()
{
return Hex::encode($this->toBytes());
return Strings::bin2hex($this->toBytes());
}
/**

View File

@ -12,7 +12,7 @@
namespace phpseclib3\Math\PrimeField;
use ParagonIE\ConstantTime\Hex;
use phpseclib3\Common\Functions\Strings;
use phpseclib3\Math\BigInteger;
use phpseclib3\Math\Common\FiniteField\Integer as Base;
@ -323,7 +323,7 @@ class Integer extends Base
*/
public function toHex()
{
return Hex::encode($this->toBytes());
return Strings::bin2hex($this->toBytes());
}
/**