Merge branch 'callmejon'

This commit is contained in:
terrafrost 2022-08-13 09:05:30 -05:00
commit 1d04f9b1d4
22 changed files with 107 additions and 42 deletions

View File

@ -51,7 +51,7 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
- name: Composer Install
run: composer install --classmap-authoritative --no-interaction --no-cache
run: composer install --classmap-authoritative --no-interaction --no-cache --ignore-platform-req=php
- name: Setup Secure Shell Functional Tests
if: matrix.os == 'ubuntu-latest'
run: |
@ -80,4 +80,4 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
php-version: ['8.1']
php-version: ['8.1', '8.2']

View File

@ -152,6 +152,7 @@ abstract class AsymmetricKey
}
$components['format'] = $format;
$components['secret'] = $components['secret'] ?? '';
$comment = $components['comment'] ?? null;
$new = static::onLoad($components);
$new->format = $format;
@ -225,6 +226,7 @@ abstract class AsymmetricKey
}
$components['format'] = $format;
$components['secret'] = $components['secret'] ?? '';
$new = static::onLoad($components);
$new->format = $format;

View File

@ -212,6 +212,11 @@ abstract class SymmetricKey
*/
protected $key = false;
/**
* @var null|string
*/
private $hKey = null;
/**
* The Initialization Vector
*
@ -2801,7 +2806,7 @@ PHP
private function setupGCM(): void
{
// don't keep on re-calculating $this->h
if (!$this->h || $this->h->key != $this->key) {
if (!$this->h || $this->hKey != $this->key) {
$cipher = new static('ecb');
$cipher->setKey($this->key);
$cipher->disablePadding();
@ -2809,7 +2814,7 @@ PHP
$this->h = self::$gcmField->newInteger(
Strings::switchEndianness($cipher->encrypt("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"))
);
$this->h->key = $this->key;
$this->hKey = $this->key;
}
if (strlen($this->nonce) == 12) {

View File

@ -124,6 +124,11 @@ class DES extends BlockCipher
*/
private $keys;
/**
* @var array
*/
private $kl;
/**
* Shuffle table.
*

View File

@ -64,6 +64,11 @@ abstract class DH extends AsymmetricKey
*/
protected $base;
/**
* @var BigInteger
*/
protected $publicKey;
/**
* Create DH parameters
*

View File

@ -15,6 +15,7 @@ namespace phpseclib3\Crypt\DH;
use phpseclib3\Crypt\Common;
use phpseclib3\Crypt\DH;
use phpseclib3\Math\BigInteger;
/**
* DH Public Key
@ -40,7 +41,7 @@ class PublicKey extends DH
/**
* Returns the public key as a BigInteger
*/
public function toBigInteger(): \phpseclib3\Math\BigInteger
public function toBigInteger(): BigInteger
{
return $this->publicKey;
}

View File

@ -125,6 +125,11 @@ abstract class EC extends AsymmetricKey
*/
protected $context;
/**
* @var string
*/
protected $sigFormat;
/**
* Create public / private key pair.
*/
@ -169,7 +174,13 @@ abstract class EC extends AsymmetricKey
$reflect->getShortName();
$curve = new $curve();
$privatekey->dA = $dA = $curve->createRandomMultiplier();
if ($curve instanceof TwistedEdwardsCurve) {
$arr = $curve->extractSecret(Random::string($curve instanceof Ed448 ? 57 : 32));
$privatekey->dA = $dA = $arr['dA'];
$privatekey->secret = $arr['secret'];
} else {
$privatekey->dA = $dA = $curve->createRandomMultiplier();
}
if ($curve instanceof Curve25519 && self::$engines['libsodium']) {
//$r = pack('H*', '0900000000000000000000000000000000000000000000000000000000000000');
//$QA = sodium_crypto_scalarmult($dA->toBytes(), $r);
@ -219,6 +230,7 @@ abstract class EC extends AsymmetricKey
if (isset($components['dA'])) {
$new->dA = $components['dA'];
$new->secret = $components['secret'];
}
if ($new->curve instanceof TwistedEdwardsCurve) {

View File

@ -40,6 +40,15 @@ use phpseclib3\Math\PrimeField;
*/
class KoblitzPrime extends Prime
{
/**
* @var list<array{a: BigInteger, b: BigInteger}>
*/
public $basis;
/**
* @var object
*/
public $beta;
// don't overwrite setCoefficients() with one that only accepts one parameter so that
// one might be able to switch between KoblitzPrime and Prime more easily (for benchmarking
// purposes).

View File

@ -157,7 +157,7 @@ class Ed25519 extends TwistedEdwards
*
* Used by the various key handlers
*
* @return \phpseclib3\Math\PrimeField\Integer
* @return array
*/
public function extractSecret(string $str)
{
@ -180,8 +180,10 @@ class Ed25519 extends TwistedEdwards
// secret scalar s.
$dA = new BigInteger($h, 256);
$dA->secret = $str;
return $dA;
return [
'dA' => $dA,
'secret' => $str
];
}
/**
@ -205,7 +207,7 @@ class Ed25519 extends TwistedEdwards
*/
public function createRandomMultiplier(): BigInteger
{
return $this->extractSecret(Random::string(32));
return $this->extractSecret(Random::string(32))['dA'];
}
/**

View File

@ -97,7 +97,7 @@ class Ed448 extends TwistedEdwards
*
* Used by the various key handlers
*
* @return \phpseclib3\Math\PrimeField\Integer
* @return array
*/
public function extractSecret(string $str)
{
@ -121,8 +121,10 @@ class Ed448 extends TwistedEdwards
// secret scalar s.
$dA = new BigInteger($h, 256);
$dA->secret = $str;
return $dA;
return [
'dA' => $dA,
'secret' => $str
];
}
/**
@ -145,7 +147,7 @@ class Ed448 extends TwistedEdwards
*/
public function createRandomMultiplier(): BigInteger
{
return $this->extractSecret(Random::string(57));
return $this->extractSecret(Random::string(57))['dA'];
}
/**

View File

@ -176,21 +176,22 @@ abstract class OpenSSH extends Progenitor
BigInteger $privateKey,
BaseCurve $curve,
array $publicKey,
$password = '',
?string $secret = null,
?string $password = null,
array $options = []
): string {
if ($curve instanceof Ed25519) {
if (!isset($privateKey->secret)) {
if (!isset($secret)) {
throw new \RuntimeException('Private Key does not have a secret set');
}
if (strlen($privateKey->secret) != 32) {
if (strlen($secret) != 32) {
throw new \RuntimeException('Private Key secret is not of the correct length');
}
$pubKey = $curve->encodePoint($publicKey);
$publicKey = Strings::packSSH2('ss', 'ssh-ed25519', $pubKey);
$privateKey = Strings::packSSH2('sss', 'ssh-ed25519', $pubKey, $privateKey->secret . $pubKey);
$privateKey = Strings::packSSH2('sss', 'ssh-ed25519', $pubKey, $secret . $pubKey);
return self::wrapPrivateKey($publicKey, $privateKey, $password, $options);
}

View File

@ -164,7 +164,7 @@ abstract class PKCS1 extends Progenitor
*
* @param Integer[] $publicKey
*/
public static function savePrivateKey(BigInteger $privateKey, BaseCurve $curve, array $publicKey, ?string $password = null, array $options = []): string
public static function savePrivateKey(BigInteger $privateKey, BaseCurve $curve, array $publicKey, ?string $secret = null, ?string $password = null, array $options = []): string
{
self::initialize_static_variables();

View File

@ -149,7 +149,9 @@ abstract class PKCS8 extends Progenitor
if (substr($key['privateKey'], 0, 2) != "\x04\x20") {
throw new \RuntimeException('The first two bytes of the private key field should be 0x0420');
}
$components['dA'] = $components['curve']->extractSecret(substr($key['privateKey'], 2));
$arr = $components['curve']->extractSecret(substr($key['privateKey'], 2));
$components['dA'] = $arr['dA'];
$components['secret'] = $arr['secret'];
}
if (isset($key['publicKey'])) {
@ -201,7 +203,7 @@ abstract class PKCS8 extends Progenitor
*
* @param Integer[] $publicKey
*/
public static function savePrivateKey(BigInteger $privateKey, BaseCurve $curve, array $publicKey, ?string $password = null, array $options = []): string
public static function savePrivateKey(BigInteger $privateKey, BaseCurve $curve, array $publicKey, ?string $secret = null, ?string $password = null, array $options = []): string
{
self::initialize_static_variables();
@ -211,7 +213,7 @@ abstract class PKCS8 extends Progenitor
if ($curve instanceof TwistedEdwardsCurve) {
return self::wrapPrivateKey(
"\x04\x20" . $privateKey->secret,
"\x04\x20" . $secret,
[],
null,
$password,

View File

@ -73,7 +73,9 @@ abstract class PuTTY extends Progenitor
if (Strings::shift($private, 4) != "\0\0\0\x20") {
throw new \RuntimeException('Length of ssh-ed25519 key should be 32');
}
$components['dA'] = $components['curve']->extractSecret($private);
$arr = $components['curve']->extractSecret($private);
$components['dA'] = $arr['dA'];
$components['secret'] = $arr['secret'];
} else {
[$components['dA']] = Strings::unpackSSH2('i', $private);
$components['curve']->rangeCheck($components['dA']);
@ -87,7 +89,7 @@ abstract class PuTTY extends Progenitor
*
* @param \phpseclib3\Math\Common\FiniteField\Integer[] $publicKey
*/
public static function savePrivateKey(BigInteger $privateKey, BaseCurve $curve, array $publicKey, ?string $password = null, array $options = []): string
public static function savePrivateKey(BigInteger $privateKey, BaseCurve $curve, array $publicKey, ?string $secret = null, ?string $password = null, array $options = []): string
{
self::initialize_static_variables();
@ -107,7 +109,7 @@ abstract class PuTTY extends Progenitor
}
$private = $curve instanceof TwistedEdwardsCurve ?
Strings::packSSH2('s', $privateKey->secret) :
Strings::packSSH2('s', $secret) :
Strings::packSSH2('s', $private);
return self::wrapPrivateKey($public, $private, $name, $password, $options);

View File

@ -64,7 +64,9 @@ abstract class libsodium
$curve = new Ed25519();
$components = ['curve' => $curve];
if (isset($private)) {
$components['dA'] = $curve->extractSecret($private);
$arr = $curve->extractSecret($private);
$components['dA'] = $arr['dA'];
$components['secret'] = $arr['secret'];
}
$components['QA'] = isset($public) ?
self::extractPoint($public, $curve) :
@ -88,17 +90,17 @@ abstract class libsodium
*
* @param \phpseclib3\Math\Common\FiniteField\Integer[] $publicKey
*/
public static function savePrivateKey(BigInteger $privateKey, Ed25519 $curve, array $publicKey, ?string $password = null): string
public static function savePrivateKey(BigInteger $privateKey, Ed25519 $curve, array $publicKey, ?string $secret = null, ?string $password = null): string
{
if (!isset($privateKey->secret)) {
if (!isset($secret)) {
throw new \RuntimeException('Private Key does not have a secret set');
}
if (strlen($privateKey->secret) != 32) {
if (strlen($secret) != 32) {
throw new \RuntimeException('Private Key secret is not of the correct length');
}
if (!empty($password) && is_string($password)) {
throw new UnsupportedFormatException('libsodium private keys do not support encryption');
}
return $privateKey->secret . $curve->encodePoint($publicKey);
return $secret . $curve->encodePoint($publicKey);
}
}

View File

@ -46,6 +46,11 @@ class PrivateKey extends EC implements Common\PrivateKey
*/
protected $dA;
/**
* @var string
*/
protected $secret = '';
/**
* Multiplies an encoded point by the private key
*
@ -110,7 +115,7 @@ class PrivateKey extends EC implements Common\PrivateKey
$curve = $this->curve;
$hash = new Hash($curve::HASH);
$secret = substr($hash->hash($this->dA->secret), $curve::SIZE);
$secret = substr($hash->hash($this->secret), $curve::SIZE);
if ($curve instanceof Ed25519) {
$dom = !isset($this->context) ? '' :
@ -213,7 +218,7 @@ class PrivateKey extends EC implements Common\PrivateKey
{
$type = self::validatePlugin('Keys', $type, 'savePrivateKey');
return $type::savePrivateKey($this->dA, $this->curve, $this->QA, $this->password, $options);
return $type::savePrivateKey($this->dA, $this->curve, $this->QA, $this->secret, $this->password, $options);
}
/**

View File

@ -170,6 +170,11 @@ class Hash
*/
private $pad;
/**
* @var int
*/
private $blockSize;
/**#@+
* UMAC variables
*

View File

@ -251,6 +251,16 @@ abstract class RSA extends AsymmetricKey
*/
private static $smallestPrime = 4096;
/**
* Public Exponent
*/
protected $publicExponent = false;
/**
* Private Exponent
*/
protected $privateExponent = null;
/**
* Sets the public exponent for key generation
*

View File

@ -50,11 +50,6 @@ class PrivateKey extends RSA implements Common\PrivateKey
*/
protected $coefficients;
/**
* Public Exponent
*/
protected $publicExponent = false;
/**
* RSADP
*

View File

@ -57,7 +57,7 @@ abstract class EvalBarrett extends Base
$lhs->value = $x;
$rhs = new ' . $class . '();
$rhs->value = [' .
implode(',', array_map('self::float2string', $m->value)) . '];
implode(',', array_map(self::class . '::float2string', $m->value)) . '];
list(, $temp) = $lhs->divide($rhs);
return $temp->value;
';
@ -98,7 +98,7 @@ abstract class EvalBarrett extends Base
$rhs = new ' . $class . '();
$lhs->value = $n;
$rhs->value = [' .
implode(',', array_map('self::float2string', $m)) . '];
implode(',', array_map(self::class . '::float2string', $m)) . '];
list(, $temp) = $lhs->divide($rhs);
return $temp->value;
}

View File

@ -50,8 +50,6 @@ class PrimeField extends FiniteField
// throw new \UnexpectedValueException('PrimeField requires a prime number be passed to the constructor');
//}
$this->modulo = $modulo;
$this->instanceID = self::$instanceCounter++;
Integer::setModulo($this->instanceID, $modulo);
Integer::setRecurringModuloFunction($this->instanceID, $modulo->createRecurringModuloFunction());

View File

@ -16,7 +16,9 @@ class Ed448PrivateKey
}
$components = ['curve' => new Ed448()];
$components['dA'] = $components['curve']->extractSecret($key);
$arr = $components['curve']->extractSecret($key);
$components['dA'] = $arr['dA'];
$components['secret'] = $arr['secret'];
$components['QA'] = $components['curve']->multiplyPoint($components['curve']->getBasePoint(), $components['dA']);
return $components;