diff --git a/Libraries/LibCrypto/Curves/SECPxxxr1.h b/Libraries/LibCrypto/Curves/SECPxxxr1.h index fa22280ac1b..9502a494773 100644 --- a/Libraries/LibCrypto/Curves/SECPxxxr1.h +++ b/Libraries/LibCrypto/Curves/SECPxxxr1.h @@ -112,6 +112,29 @@ struct SECPxxxr1Signature { return SECPxxxr1Signature { r_big_int, s_big_int, scalar_size }; } + static ErrorOr from_raw(Span 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 r_bytes() const { return SECPxxxr1Point::scalar_to_bytes(r, size); diff --git a/Libraries/LibCrypto/PK/EC.cpp b/Libraries/LibCrypto/PK/EC.cpp index 6563f1ba870..16912b3cb36 100644 --- a/Libraries/LibCrypto/PK/EC.cpp +++ b/Libraries/LibCrypto/PK/EC.cpp @@ -61,9 +61,19 @@ static ErrorOr> read_ec_public_key(ReadonlyBytes bytes, Vector { + 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