LibCrypto: Add support for raw EC keys and SECPxxxr1 signatures

This commit is contained in:
Ali Mohammad Pur 2025-05-13 12:08:29 +02:00 committed by Ali Mohammad Pur
commit 3dd246a8e1
Notes: github-actions[bot] 2025-06-11 16:18:13 +00:00
2 changed files with 35 additions and 2 deletions

View file

@ -112,6 +112,29 @@ struct SECPxxxr1Signature {
return SECPxxxr1Signature { r_big_int, s_big_int, scalar_size }; return SECPxxxr1Signature { r_big_int, s_big_int, scalar_size };
} }
static ErrorOr<SECPxxxr1Signature> from_raw(Span<int const> curve_oid, ReadonlyBytes signature)
{
size_t scalar_size;
if (curve_oid == ASN1::secp256r1_oid) {
scalar_size = ceil_div(256, 8);
} else if (curve_oid == ASN1::secp384r1_oid) {
scalar_size = ceil_div(384, 8);
} else if (curve_oid == ASN1::secp521r1_oid) {
scalar_size = ceil_div(521, 8);
} else {
return Error::from_string_literal("Unknown SECPxxxr1 curve");
}
if (signature.size() != scalar_size * 2)
return Error::from_string_literal("Invalid SECPxxxr1 signature");
return SECPxxxr1Signature {
UnsignedBigInteger::import_data(signature.slice(0, scalar_size)),
UnsignedBigInteger::import_data(signature.slice(scalar_size, scalar_size)),
scalar_size,
};
}
ErrorOr<ByteBuffer> r_bytes() const ErrorOr<ByteBuffer> r_bytes() const
{ {
return SECPxxxr1Point::scalar_to_bytes(r, size); return SECPxxxr1Point::scalar_to_bytes(r, size);

View file

@ -61,9 +61,19 @@ static ErrorOr<ECPublicKey<>> read_ec_public_key(ReadonlyBytes bytes, Vector<Str
UnsignedBigInteger::import_data(bytes.slice(1 + half_size, half_size)), UnsignedBigInteger::import_data(bytes.slice(1 + half_size, half_size)),
half_size, half_size,
}; };
} else {
ERROR_WITH_SCOPE("Unsupported public key format");
} }
if (bytes.size() % 2 == 0) {
// Raw public key, without the 0x04 prefix
auto half_size = bytes.size() / 2;
return ::Crypto::PK::ECPublicKey<> {
UnsignedBigInteger::import_data(bytes.slice(0, half_size)),
UnsignedBigInteger::import_data(bytes.slice(half_size, half_size)),
half_size,
};
}
ERROR_WITH_SCOPE("Unsupported public key format");
} }
// https://www.rfc-editor.org/rfc/rfc5915#section-3 // https://www.rfc-editor.org/rfc/rfc5915#section-3