EC/Formats/Signature/IEEE: make key length more consistent

This commit is contained in:
terrafrost 2024-07-31 06:52:15 -05:00
parent 38b617210d
commit 04a559debb
2 changed files with 27 additions and 4 deletions

View File

@ -56,11 +56,11 @@ abstract class IEEE
* @param \phpseclib3\Math\BigInteger $s
* @return string
*/
public static function save(BigInteger $r, BigInteger $s)
public static function save(BigInteger $r, BigInteger $s, $curve)
{
$r = $r->toBytes();
$s = $s->toBytes();
$len = max(strlen($r), strlen($s));
$len = $curve->getLength();
return str_pad($r, $len, "\0", STR_PAD_LEFT) . str_pad($s, $len, "\0", STR_PAD_LEFT);
}
}

View File

@ -159,7 +159,7 @@ final class PrivateKey extends EC implements Common\PrivateKey
extract(ASN1Signature::load($signature));
return $shortFormat == 'SSH2' ? $format::save($r, $s, $this->getCurve()) : $format::save($r, $s);
return $this->formatSignature($r, $s);
}
}
@ -208,7 +208,7 @@ final class PrivateKey extends EC implements Common\PrivateKey
list(, $s) = $temp->divide($this->q);
*/
return $shortFormat == 'SSH2' ? $format::save($r, $s, $this->getCurve()) : $format::save($r, $s);
return $this->formatSignature($r, $s);
}
/**
@ -253,4 +253,27 @@ final class PrivateKey extends EC implements Common\PrivateKey
}
return $key;
}
/**
* Returns a signature in the appropriate format
*
* @return string
*/
private function formatSignature(BigInteger $r, BigInteger $s)
{
$format = $this->sigFormat;
$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());
}
// presumably the only way you could get to this is if you were using a custom plugin
throw new UnsupportedOperationException("$format::save() has $paramCount parameters - the only valid parameter counts are 2 or 3");
}
}