diff --git a/phpseclib/Crypt/EC/Formats/Signature/IEEE.php b/phpseclib/Crypt/EC/Formats/Signature/IEEE.php index 7c34a24f..26ff7404 100644 --- a/phpseclib/Crypt/EC/Formats/Signature/IEEE.php +++ b/phpseclib/Crypt/EC/Formats/Signature/IEEE.php @@ -54,13 +54,15 @@ abstract class IEEE * * @param \phpseclib3\Math\BigInteger $r * @param \phpseclib3\Math\BigInteger $s + * @param string $curve + * @param int $length * @return string */ - public static function save(BigInteger $r, BigInteger $s, $curve) + public static function save(BigInteger $r, BigInteger $s, $curve, $length) { $r = $r->toBytes(); $s = $s->toBytes(); - $len = $curve->getLength(); - return str_pad($r, $len, "\0", STR_PAD_LEFT) . str_pad($s, $len, "\0", STR_PAD_LEFT); + $length >>= 3; + return str_pad($r, $length, "\0", STR_PAD_LEFT) . str_pad($s, $length, "\0", STR_PAD_LEFT); } } diff --git a/phpseclib/Crypt/EC/PrivateKey.php b/phpseclib/Crypt/EC/PrivateKey.php index 96884c76..611921cc 100644 --- a/phpseclib/Crypt/EC/PrivateKey.php +++ b/phpseclib/Crypt/EC/PrivateKey.php @@ -266,11 +266,10 @@ final class PrivateKey extends EC implements Common\PrivateKey $temp = new \ReflectionMethod($format, 'save'); $paramCount = $temp->getNumberOfRequiredParameters(); - if ($paramCount == 2) { - return $format::save($r, $s); - } - if ($paramCount == 3) { - return $format::save($r, $s, $this->getCurve()); + switch ($paramCount) { + case 2: return $format::save($r, $s); + case 3: return $format::save($r, $s, $this->getCurve()); + case 4: return $format::save($r, $s, $this->getCurve(), $this->getLength()); } // presumably the only way you could get to this is if you were using a custom plugin diff --git a/tests/Unit/Crypt/EC/KeyTest.php b/tests/Unit/Crypt/EC/KeyTest.php index 83ea2e8d..0a73c104 100644 --- a/tests/Unit/Crypt/EC/KeyTest.php +++ b/tests/Unit/Crypt/EC/KeyTest.php @@ -747,4 +747,18 @@ z9DYTLdGkQDqox3AtEs9nn6kE1O/vHE4bqMegjj4gbA= $key = EC::loadFormat('PKCS8', $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)); + } }