EC/Keys/PKCS8: code reduction

This commit is contained in:
terrafrost 2024-05-25 14:43:40 -05:00
parent 541887c33a
commit b718a63aae

View File

@ -129,21 +129,14 @@ abstract class PKCS8 extends Progenitor
$components = []; $components = [];
if (isset($key['privateKey'])) { if (isset($key['privateKey'])) {
if ($key['privateKeyAlgorithm']['algorithm'] == 'id-Ed25519') { $components['curve'] = $key['privateKeyAlgorithm']['algorithm'] == 'id-Ed25519' ? new Ed25519() : new Ed448();
$components['curve'] = new Ed25519(); $expected = chr(ASN1::TYPE_OCTET_STRING) . ASN1::encodeLength($components['curve']::SIZE);
// 0x04 == octet string if (substr($key['privateKey'], 0, 2) != $expected) {
// 0x20 == length (32 bytes) throw new \RuntimeException(
if (substr($key['privateKey'], 0, 2) != "\x04\x20") { 'The first two bytes of the ' .
throw new \RuntimeException('The first two bytes of the Ed25519 private key field should be 0x0420'); $key['privateKeyAlgorithm']['algorithm'] .
} ' private key field should be 0x' . bin2hex($expected)
} else { );
// Assume Ed448
$components['curve'] = new Ed448();
// 0x04 == octet string
// 0x39 == length (57 bytes)
if (substr($key['privateKey'], 0, 2) != "\x04\x39") {
throw new \RuntimeException('The first two bytes of the Ed448 private key field should be 0x0439');
}
} }
$arr = $components['curve']->extractSecret(substr($key['privateKey'], 2)); $arr = $components['curve']->extractSecret(substr($key['privateKey'], 2));
$components['dA'] = $arr['dA']; $components['dA'] = $arr['dA'];
@ -216,24 +209,13 @@ abstract class PKCS8 extends Progenitor
} }
if ($curve instanceof TwistedEdwardsCurve) { if ($curve instanceof TwistedEdwardsCurve) {
if ($curve instanceof Ed25519) { return self::wrapPrivateKey(
return self::wrapPrivateKey( chr(ASN1::TYPE_OCTET_STRING) . ASN1::encodeLength($curve::SIZE) . $secret,
"\x04\x20" . $secret, [],
[], null,
null, $password,
$password, $curve instanceof Ed25519 ? 'id-Ed25519' : 'id-Ed448'
'id-Ed25519' );
);
} else {
// Assume Ed448
return self::wrapPrivateKey(
"\x04\x39" . $secret,
[],
null,
$password,
'id-Ed448'
);
}
} }
$publicKey = "\4" . $publicKey[0]->toBytes() . $publicKey[1]->toBytes(); $publicKey = "\4" . $publicKey[0]->toBytes() . $publicKey[1]->toBytes();