LibCrypto: Rename and remove unused methods from SECPxxxr1 class

This commit is contained in:
devgianlu 2025-02-16 12:22:22 +01:00 committed by Alexander Kalenik
parent 048d6b8012
commit f630ca7cd0
Notes: github-actions[bot] 2025-02-17 23:03:23 +00:00
4 changed files with 66 additions and 96 deletions

View file

@ -13,7 +13,7 @@
namespace Crypto::Curves {
ErrorOr<UnsignedBigInteger> SECPxxxr1::generate_private_key_scalar()
ErrorOr<UnsignedBigInteger> SECPxxxr1::generate_private_key()
{
auto key = TRY(OpenSSL_PKEY::wrap(EVP_PKEY_Q_keygen(nullptr, nullptr, "EC", m_curve_name)));
@ -24,7 +24,7 @@ ErrorOr<UnsignedBigInteger> SECPxxxr1::generate_private_key_scalar()
return TRY(openssl_bignum_to_unsigned_big_integer(priv_bn));
}
ErrorOr<SECPxxxr1Point> SECPxxxr1::generate_public_key_point(UnsignedBigInteger scalar)
ErrorOr<SECPxxxr1Point> SECPxxxr1::generate_public_key(UnsignedBigInteger scalar)
{
auto* group = EC_GROUP_new_by_curve_name(EC_curve_nist2nid(m_curve_name));
ScopeGuard const free_group = [&] { EC_GROUP_free(group); };
@ -48,7 +48,7 @@ ErrorOr<SECPxxxr1Point> SECPxxxr1::generate_public_key_point(UnsignedBigInteger
};
}
ErrorOr<SECPxxxr1Point> SECPxxxr1::compute_coordinate_point(UnsignedBigInteger scalar, SECPxxxr1Point point)
ErrorOr<SECPxxxr1Point> SECPxxxr1::compute_coordinate(UnsignedBigInteger scalar, SECPxxxr1Point point)
{
auto* group = EC_GROUP_new_by_curve_name(EC_curve_nist2nid(m_curve_name));
ScopeGuard const free_group = [&] { EC_GROUP_free(group); };
@ -80,7 +80,7 @@ ErrorOr<SECPxxxr1Point> SECPxxxr1::compute_coordinate_point(UnsignedBigInteger s
};
}
ErrorOr<bool> SECPxxxr1::verify_point(ReadonlyBytes hash, SECPxxxr1Point pubkey, SECPxxxr1Signature signature)
ErrorOr<bool> SECPxxxr1::verify(ReadonlyBytes hash, SECPxxxr1Point pubkey, SECPxxxr1Signature signature)
{
auto ctx_import = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_name(nullptr, "EC", nullptr)));
@ -135,7 +135,7 @@ ErrorOr<bool> SECPxxxr1::verify_point(ReadonlyBytes hash, SECPxxxr1Point pubkey,
VERIFY_NOT_REACHED();
}
ErrorOr<SECPxxxr1Signature> SECPxxxr1::sign_scalar(ReadonlyBytes hash, UnsignedBigInteger private_key)
ErrorOr<SECPxxxr1Signature> SECPxxxr1::sign(ReadonlyBytes hash, UnsignedBigInteger private_key)
{
auto ctx_import = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_name(nullptr, "EC", nullptr)));

View file

@ -135,62 +135,11 @@ struct SECPxxxr1Signature {
class SECPxxxr1 {
public:
size_t key_size() { return 1 + (2 * m_scalar_size); }
ErrorOr<UnsignedBigInteger> generate_private_key_scalar();
ErrorOr<SECPxxxr1Point> generate_public_key_point(UnsignedBigInteger scalar);
ErrorOr<SECPxxxr1Point> compute_coordinate_point(UnsignedBigInteger scalar, SECPxxxr1Point point);
ErrorOr<bool> verify_point(ReadonlyBytes hash, SECPxxxr1Point pubkey, SECPxxxr1Signature signature);
ErrorOr<SECPxxxr1Signature> sign_scalar(ReadonlyBytes hash, UnsignedBigInteger private_key);
ErrorOr<SECPxxxr1Point> derive_premaster_key_point(SECPxxxr1Point shared_point)
{
return shared_point;
}
ErrorOr<ByteBuffer> generate_private_key()
{
auto key = TRY(generate_private_key_scalar());
auto buffer = TRY(ByteBuffer::create_uninitialized(m_scalar_size));
auto buffer_bytes = buffer.bytes();
auto size = key.export_data(buffer_bytes);
return buffer.slice(0, size);
}
ErrorOr<ByteBuffer> generate_public_key(ReadonlyBytes a)
{
auto a_int = UnsignedBigInteger::import_data(a);
auto point = TRY(generate_public_key_point(a_int));
return point.to_uncompressed();
}
ErrorOr<ByteBuffer> compute_coordinate(ReadonlyBytes scalar_bytes, ReadonlyBytes point_bytes)
{
auto scalar = UnsignedBigInteger::import_data(scalar_bytes);
auto point = TRY(SECPxxxr1Point::from_uncompressed(point_bytes));
auto result = TRY(compute_coordinate_point(scalar, { point.x, point.y, m_scalar_size }));
return result.to_uncompressed();
}
ErrorOr<ByteBuffer> derive_premaster_key(ReadonlyBytes shared_point_bytes)
{
auto shared_point = TRY(SECPxxxr1Point::from_uncompressed(shared_point_bytes));
auto premaster_key_point = TRY(derive_premaster_key_point(shared_point));
return premaster_key_point.to_uncompressed();
}
ErrorOr<bool> verify(ReadonlyBytes hash, ReadonlyBytes pubkey, SECPxxxr1Signature signature)
{
auto pubkey_point = TRY(SECPxxxr1Point::from_uncompressed(pubkey));
return verify_point(hash, pubkey_point, signature);
}
ErrorOr<SECPxxxr1Signature> sign(ReadonlyBytes hash, ReadonlyBytes private_key_bytes)
{
auto signature = TRY(sign_scalar(hash, UnsignedBigInteger::import_data(private_key_bytes.data(), private_key_bytes.size())));
return signature;
}
ErrorOr<UnsignedBigInteger> generate_private_key();
ErrorOr<SECPxxxr1Point> generate_public_key(UnsignedBigInteger scalar);
ErrorOr<SECPxxxr1Point> compute_coordinate(UnsignedBigInteger scalar, SECPxxxr1Point point);
ErrorOr<bool> verify(ReadonlyBytes hash, SECPxxxr1Point pubkey, SECPxxxr1Signature signature);
ErrorOr<SECPxxxr1Signature> sign(ReadonlyBytes hash, UnsignedBigInteger private_key);
protected:
SECPxxxr1(char const* curve_name, size_t scalar_size)

View file

@ -3777,7 +3777,7 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, GC::Ref<CryptoKeyPair>>> ECDSA::
// 6. If performing the key generation operation results in an error, then throw an OperationError.
auto maybe_private_key_data = curve.visit(
[](Empty const&) -> ErrorOr<::Crypto::UnsignedBigInteger> { return Error::from_string_literal("noop error"); },
[](auto instance) { return instance.generate_private_key_scalar(); });
[](auto instance) { return instance.generate_private_key(); });
if (maybe_private_key_data.is_error())
return WebIDL::OperationError::create(m_realm, "Failed to create valid crypto instance"_string);
@ -3786,7 +3786,7 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, GC::Ref<CryptoKeyPair>>> ECDSA::
auto maybe_public_key_data = curve.visit(
[](Empty const&) -> ErrorOr<::Crypto::Curves::SECPxxxr1Point> { return Error::from_string_literal("noop error"); },
[&](auto instance) { return instance.generate_public_key_point(private_key_data); });
[&](auto instance) { return instance.generate_public_key(private_key_data); });
if (maybe_public_key_data.is_error())
return WebIDL::OperationError::create(m_realm, "Failed to create valid crypto instance"_string);
@ -3906,7 +3906,7 @@ WebIDL::ExceptionOr<GC::Ref<JS::ArrayBuffer>> ECDSA::sign(AlgorithmParams const&
// 2. Let r and s be the pair of integers resulting from performing the ECDSA signing process.
auto maybe_signature = curve.visit(
[](Empty const&) -> ErrorOr<::Crypto::Curves::SECPxxxr1Signature> { return Error::from_string_literal("Failed to create valid crypto instance"); },
[&](auto instance) { return instance.sign_scalar(M, d.d()); });
[&](auto instance) { return instance.sign(M, d.d()); });
if (maybe_signature.is_error()) {
auto error_message = MUST(String::from_utf8(maybe_signature.error().string_literal()));
@ -4002,7 +4002,7 @@ WebIDL::ExceptionOr<JS::Value> ECDSA::verify(AlgorithmParams const& params, GC::
auto maybe_result = curve.visit(
[](Empty const&) -> ErrorOr<bool> { return Error::from_string_literal("Failed to create valid crypto instance"); },
[&](auto instance) { return instance.verify_point(M, Q.to_secpxxxr1_point(), ::Crypto::Curves::SECPxxxr1Signature { r, s, half_size }); });
[&](auto instance) { return instance.verify(M, Q.to_secpxxxr1_point(), ::Crypto::Curves::SECPxxxr1Signature { r, s, half_size }); });
if (maybe_result.is_error()) {
auto error_message = MUST(String::from_utf8(maybe_result.error().string_literal()));
@ -4644,7 +4644,7 @@ WebIDL::ExceptionOr<GC::Ref<JS::Object>> ECDSA::export_key(Bindings::KeyFormat f
auto maybe_public_key = curve.visit(
[](Empty const&) -> ErrorOr<::Crypto::Curves::SECPxxxr1Point> { return Error::from_string_literal("noop error"); },
[&](auto instance) { return instance.generate_public_key_point(private_key.d()); });
[&](auto instance) { return instance.generate_public_key(private_key.d()); });
auto public_key = TRY(maybe_public_key);
auto x_bytes = TRY(public_key.x_bytes());
@ -4789,7 +4789,7 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, GC::Ref<CryptoKeyPair>>> ECDH::g
// 3. If performing the operation results in an error, then throw a OperationError.
auto maybe_private_key_data = curve.visit(
[](Empty const&) -> ErrorOr<::Crypto::UnsignedBigInteger> { return Error::from_string_literal("noop error"); },
[](auto instance) { return instance.generate_private_key_scalar(); });
[](auto instance) { return instance.generate_private_key(); });
if (maybe_private_key_data.is_error())
return WebIDL::OperationError::create(m_realm, "Failed to create valid crypto instance"_string);
@ -4798,7 +4798,7 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, GC::Ref<CryptoKeyPair>>> ECDH::g
auto maybe_public_key_data = curve.visit(
[](Empty const&) -> ErrorOr<::Crypto::Curves::SECPxxxr1Point> { return Error::from_string_literal("noop error"); },
[&](auto instance) { return instance.generate_public_key_point(private_key_data); });
[&](auto instance) { return instance.generate_public_key(private_key_data); });
if (maybe_public_key_data.is_error())
return WebIDL::OperationError::create(m_realm, "Failed to create valid crypto instance"_string);
@ -4908,7 +4908,7 @@ WebIDL::ExceptionOr<GC::Ref<JS::ArrayBuffer>> ECDH::derive_bits(AlgorithmParams
auto maybe_secret = curve.visit(
[](Empty const&) -> ErrorOr<::Crypto::Curves::SECPxxxr1Point> { return Error::from_string_literal("noop error"); },
[&private_key_data, &public_key_data](auto instance) { return instance.compute_coordinate_point(private_key_data.d(), public_key_data.to_secpxxxr1_point()); });
[&private_key_data, &public_key_data](auto instance) { return instance.compute_coordinate(private_key_data.d(), public_key_data.to_secpxxxr1_point()); });
if (maybe_secret.is_error()) {
auto message = TRY_OR_THROW_OOM(realm.vm(), String::formatted("Failed to compute secret: {}", maybe_secret.error()));
@ -5538,7 +5538,7 @@ WebIDL::ExceptionOr<GC::Ref<JS::Object>> ECDH::export_key(Bindings::KeyFormat fo
auto maybe_public_key = curve.visit(
[](Empty const&) -> ErrorOr<::Crypto::Curves::SECPxxxr1Point> { return Error::from_string_literal("noop error"); },
[&](auto instance) { return instance.generate_public_key_point(private_key.d()); });
[&](auto instance) { return instance.generate_public_key(private_key.d()); });
auto public_key = TRY(maybe_public_key);
auto x_bytes = TRY(public_key.x_bytes());

View file

@ -198,43 +198,50 @@ TEST_CASE(test_secp256r1)
};
// clang-format on
ReadonlyBytes alice_private_key { alice_private_key_data, 32 };
ReadonlyBytes alice_public_key { alice_public_key_data, 65 };
ReadonlyBytes bob_private_key { bob_private_key_data, 32 };
ReadonlyBytes bob_public_key { bob_public_key_data, 65 };
ReadonlyBytes shared_secret { shared_secret_data, 65 };
auto alice_private_key = Crypto::UnsignedBigInteger::import_data(alice_private_key_data, 32);
auto alice_public_key = TRY_OR_FAIL(Crypto::Curves::SECPxxxr1Point::from_uncompressed({ alice_public_key_data, 65 }));
auto bob_private_key = Crypto::UnsignedBigInteger::import_data(bob_private_key_data, 32);
auto bob_public_key = TRY_OR_FAIL(Crypto::Curves::SECPxxxr1Point::from_uncompressed({ bob_public_key_data, 65 }));
auto shared_secret = TRY_OR_FAIL(Crypto::Curves::SECPxxxr1Point::from_uncompressed({ shared_secret_data, 65 }));
auto private_key = Crypto::UnsignedBigInteger::import_data(private_key_data, 32);
auto expected_public_key = TRY_OR_FAIL(Crypto::Curves::SECPxxxr1Point::from_uncompressed({ expected_public_key_data, 65 }));
Crypto::Curves::SECP256r1 curve;
auto generated_alice_public = MUST(curve.generate_public_key(alice_private_key));
EXPECT_EQ(alice_public_key, generated_alice_public);
EXPECT_EQ(alice_public_key.x, generated_alice_public.x);
EXPECT_EQ(alice_public_key.y, generated_alice_public.y);
auto generated_bob_public = MUST(curve.generate_public_key(bob_private_key));
EXPECT_EQ(bob_public_key, generated_bob_public);
EXPECT_EQ(bob_public_key.x, generated_bob_public.x);
EXPECT_EQ(bob_public_key.y, generated_bob_public.y);
auto shared_alice = MUST(curve.compute_coordinate(alice_private_key, bob_public_key));
EXPECT_EQ(shared_alice, shared_secret);
EXPECT_EQ(shared_alice.x, shared_secret.x);
EXPECT_EQ(shared_alice.y, shared_secret.y);
auto shared_bob = MUST(curve.compute_coordinate(bob_private_key, alice_public_key));
EXPECT_EQ(shared_bob, shared_secret);
EXPECT_EQ(shared_bob.x, shared_secret.x);
EXPECT_EQ(shared_bob.y, shared_secret.y);
EXPECT_EQ(shared_alice, shared_bob);
EXPECT_EQ(shared_alice.x, shared_bob.x);
EXPECT_EQ(shared_alice.y, shared_bob.y);
auto generated_public = MUST(curve.generate_public_key({ private_key_data, 32 }));
ReadonlyBytes expected_public_key { expected_public_key_data, 65 };
EXPECT_EQ(expected_public_key, generated_public);
auto generated_public = MUST(curve.generate_public_key(private_key));
EXPECT_EQ(expected_public_key.x, generated_public.x);
EXPECT_EQ(expected_public_key.y, generated_public.y);
}
TEST_CASE(test_secp384r1)
{
// clang-format off
Array<u8, 48> alice_private_key {
Array<u8, 48> alice_private_key_data {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
};
Array<u8, 97> alice_public_key {
Array<u8, 97> alice_public_key_data {
0x04,
0xAA, 0x87, 0xCA, 0x22, 0xBE, 0x8B, 0x05, 0x37, 0x8E, 0xB1, 0xC7, 0x1E, 0xF3, 0x20, 0xAD, 0x74,
0x6E, 0x1D, 0x3B, 0x62, 0x8B, 0xA7, 0x9B, 0x98, 0x59, 0xF7, 0x41, 0xE0, 0x82, 0x54, 0x2A, 0x38,
@ -244,13 +251,13 @@ TEST_CASE(test_secp384r1)
0x0A, 0x60, 0xB1, 0xCE, 0x1D, 0x7E, 0x81, 0x9D, 0x7A, 0x43, 0x1D, 0x7C, 0x90, 0xEA, 0x0E, 0x5F,
};
Array<u8, 48> bob_private_key {
Array<u8, 48> bob_private_key_data {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
};
Array<u8, 97> bob_public_key {
Array<u8, 97> bob_public_key_data {
0x04,
0x08, 0xD9, 0x99, 0x05, 0x7B, 0xA3, 0xD2, 0xD9, 0x69, 0x26, 0x00, 0x45, 0xC5, 0x5B, 0x97, 0xF0,
0x89, 0x02, 0x59, 0x59, 0xA6, 0xF4, 0x34, 0xD6, 0x51, 0xD2, 0x07, 0xD1, 0x9F, 0xB9, 0x6E, 0x9E,
@ -260,7 +267,7 @@ TEST_CASE(test_secp384r1)
0x5F, 0xFD, 0x43, 0xE9, 0x4D, 0x39, 0xE2, 0x2D, 0x61, 0x50, 0x1E, 0x70, 0x0A, 0x94, 0x0E, 0x80,
};
Array<u8, 97> shared_secret {
Array<u8, 97> shared_secret_data {
0x04,
0x08, 0xd9, 0x99, 0x05, 0x7b, 0xa3, 0xd2, 0xd9, 0x69, 0x26, 0x00, 0x45, 0xc5, 0x5b, 0x97, 0xf0,
0x89, 0x02, 0x59, 0x59, 0xa6, 0xf4, 0x34, 0xd6, 0x51, 0xd2, 0x07, 0xd1, 0x9f, 0xb9, 0x6e, 0x9e,
@ -270,13 +277,13 @@ TEST_CASE(test_secp384r1)
0x5f, 0xfd, 0x43, 0xe9, 0x4d, 0x39, 0xe2, 0x2d, 0x61, 0x50, 0x1e, 0x70, 0x0a, 0x94, 0x0e, 0x80,
};
Array<u8, 48> private_key {
Array<u8, 48> private_key_data {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0x63, 0x4d, 0x81, 0xf4, 0x37, 0x2d, 0xdf,
0x58, 0x1a, 0x0d, 0xb2, 0x48, 0xb0, 0xa7, 0x7a, 0xec, 0xec, 0x19, 0x6a, 0xcc, 0xc5, 0x29, 0x62,
};
Array<u8, 97> expected_public_key {
Array<u8, 97> expected_public_key_data {
0x04,
0x40, 0x99, 0x95, 0x22, 0x08, 0xB4, 0x88, 0x96, 0x00, 0xA5, 0xEB, 0xBC, 0xB1, 0x3E, 0x1A, 0x32,
0x69, 0x2B, 0xEF, 0xB0, 0x73, 0x3B, 0x41, 0xE6, 0xDC, 0xC6, 0x14, 0xE4, 0x2E, 0x58, 0x05, 0xF8,
@ -287,22 +294,36 @@ TEST_CASE(test_secp384r1)
};
// clang-format on
auto alice_private_key = Crypto::UnsignedBigInteger::import_data(alice_private_key_data.data(), alice_private_key_data.size());
auto alice_public_key = TRY_OR_FAIL(Crypto::Curves::SECPxxxr1Point::from_uncompressed({ alice_public_key_data.data(), alice_public_key_data.size() }));
auto bob_private_key = Crypto::UnsignedBigInteger::import_data(bob_private_key_data.data(), bob_private_key_data.size());
auto bob_public_key = TRY_OR_FAIL(Crypto::Curves::SECPxxxr1Point::from_uncompressed({ bob_public_key_data.data(), bob_public_key_data.size() }));
auto shared_secret = TRY_OR_FAIL(Crypto::Curves::SECPxxxr1Point::from_uncompressed({ shared_secret_data.data(), shared_secret_data.size() }));
auto private_key = Crypto::UnsignedBigInteger::import_data(private_key_data.data(), private_key_data.size());
auto expected_public_key = TRY_OR_FAIL(Crypto::Curves::SECPxxxr1Point::from_uncompressed({ expected_public_key_data.data(), expected_public_key_data.size() }));
Crypto::Curves::SECP384r1 curve;
auto generated_alice_public = MUST(curve.generate_public_key(alice_private_key));
EXPECT_EQ(alice_public_key.span(), generated_alice_public);
EXPECT_EQ(alice_public_key.x, generated_alice_public.x);
EXPECT_EQ(alice_public_key.y, generated_alice_public.y);
auto generated_bob_public = MUST(curve.generate_public_key(bob_private_key));
EXPECT_EQ(bob_public_key.span(), generated_bob_public);
EXPECT_EQ(bob_public_key.x, generated_bob_public.x);
EXPECT_EQ(bob_public_key.y, generated_bob_public.y);
auto shared_alice = MUST(curve.compute_coordinate(alice_private_key, bob_public_key));
EXPECT_EQ(shared_alice, shared_secret.span());
EXPECT_EQ(shared_alice.x, shared_secret.x);
EXPECT_EQ(shared_alice.y, shared_secret.y);
auto shared_bob = MUST(curve.compute_coordinate(bob_private_key, alice_public_key));
EXPECT_EQ(shared_bob, shared_secret.span());
EXPECT_EQ(shared_bob.x, shared_secret.x);
EXPECT_EQ(shared_bob.y, shared_secret.y);
EXPECT_EQ(shared_alice, shared_bob);
EXPECT_EQ(shared_alice.x, shared_bob.x);
EXPECT_EQ(shared_alice.y, shared_bob.y);
auto generated_public = MUST(curve.generate_public_key(private_key));
EXPECT_EQ(expected_public_key.span(), generated_public);
EXPECT_EQ(expected_public_key.x, generated_public.x);
EXPECT_EQ(expected_public_key.y, generated_public.y);
}