fix issue creating IEEE signatures

This commit is contained in:
terrafrost 2024-07-31 08:38:04 -05:00
parent 04a559debb
commit a015cded00
3 changed files with 23 additions and 8 deletions

View File

@ -54,13 +54,15 @@ abstract class IEEE
* *
* @param \phpseclib3\Math\BigInteger $r * @param \phpseclib3\Math\BigInteger $r
* @param \phpseclib3\Math\BigInteger $s * @param \phpseclib3\Math\BigInteger $s
* @param string $curve
* @param int $length
* @return string * @return string
*/ */
public static function save(BigInteger $r, BigInteger $s, $curve) public static function save(BigInteger $r, BigInteger $s, $curve, $length)
{ {
$r = $r->toBytes(); $r = $r->toBytes();
$s = $s->toBytes(); $s = $s->toBytes();
$len = $curve->getLength(); $length >>= 3;
return str_pad($r, $len, "\0", STR_PAD_LEFT) . str_pad($s, $len, "\0", STR_PAD_LEFT); return str_pad($r, $length, "\0", STR_PAD_LEFT) . str_pad($s, $length, "\0", STR_PAD_LEFT);
} }
} }

View File

@ -266,11 +266,10 @@ final class PrivateKey extends EC implements Common\PrivateKey
$temp = new \ReflectionMethod($format, 'save'); $temp = new \ReflectionMethod($format, 'save');
$paramCount = $temp->getNumberOfRequiredParameters(); $paramCount = $temp->getNumberOfRequiredParameters();
if ($paramCount == 2) { switch ($paramCount) {
return $format::save($r, $s); case 2: return $format::save($r, $s);
} case 3: return $format::save($r, $s, $this->getCurve());
if ($paramCount == 3) { case 4: return $format::save($r, $s, $this->getCurve(), $this->getLength());
return $format::save($r, $s, $this->getCurve());
} }
// presumably the only way you could get to this is if you were using a custom plugin // presumably the only way you could get to this is if you were using a custom plugin

View File

@ -747,4 +747,18 @@ z9DYTLdGkQDqox3AtEs9nn6kE1O/vHE4bqMegjj4gbA=
$key = EC::loadFormat('PKCS8', $key); $key = EC::loadFormat('PKCS8', $key);
$this->assertInstanceOf(PublicKey::class, $key); $this->assertInstanceOf(PublicKey::class, $key);
} }
public function testIEEESignatureCreate()
{
$key = PublicKeyLoader::load('-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg7+qVCtyt+tV2hTou
kbZNIHu+PaE0osExnxdlkiC+VYqhRANCAAS8yEueJvIAnCk++0rsD8X9dk3hAmyb
4lv6WkjCQU5iksxIG/E60L8IeDZX8+oNzHPjNN5/6MBk0ISrGKyFhlH1
-----END PRIVATE KEY-----');
$priv = $key->withSignatureFormat('IEEE');
$sig = $priv->sign('ddd');
$this->assertTrue($key->getPublicKey()->withSignatureFormat('IEEE')->verify('ddd', $sig));
}
} }