mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-12 19:19:30 +00:00
LibCrypto: Add support for raw EC keys and SECPxxxr1 signatures
This commit is contained in:
parent
b374322e38
commit
3dd246a8e1
Notes:
github-actions[bot]
2025-06-11 16:18:13 +00:00
Author: https://github.com/alimpfard
Commit: 3dd246a8e1
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4709
Reviewed-by: https://github.com/ADKaster ✅
2 changed files with 35 additions and 2 deletions
|
@ -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);
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue