LibCrypto: Convert UnsignedBigInteger::import_data to accept Bytes

All the callers are already using Bytes and manually converting to the
old style pointer + length when calling this API.
This commit is contained in:
Idan Horowitz 2025-08-05 02:55:17 +03:00 committed by Ali Mohammad Pur
commit b0fdbe3756
Notes: github-actions[bot] 2025-08-05 07:10:25 +00:00
9 changed files with 31 additions and 44 deletions

View file

@ -117,7 +117,7 @@ ErrorOr<UnsignedBigInteger> Decoder::decode_arbitrary_sized_integer(ReadonlyByte
if (is_negative)
return Error::from_string_literal("ASN1::Decoder: Decoding a negative unsigned arbitrary sized integer");
return UnsignedBigInteger::import_data(data.data(), data.size());
return UnsignedBigInteger::import_data(data);
}
ErrorOr<StringView> Decoder::decode_octet_string(ReadonlyBytes bytes)

View file

@ -18,10 +18,10 @@
namespace Crypto {
UnsignedBigInteger::UnsignedBigInteger(u8 const* ptr, size_t length)
UnsignedBigInteger::UnsignedBigInteger(ReadonlyBytes data)
{
MP_MUST(mp_init(&m_mp));
MP_MUST(mp_from_ubin(&m_mp, ptr, length));
MP_MUST(mp_from_ubin(&m_mp, data.data(), data.size()));
}
UnsignedBigInteger::UnsignedBigInteger(Vector<u32> const& words)

View file

@ -24,7 +24,7 @@ public:
: UnsignedBigInteger(static_cast<u64>(value))
{
}
UnsignedBigInteger(u8 const* ptr, size_t length);
UnsignedBigInteger(ReadonlyBytes);
explicit UnsignedBigInteger(Vector<u32> const& words);
explicit UnsignedBigInteger(double value);
@ -39,8 +39,7 @@ public:
UnsignedBigInteger();
~UnsignedBigInteger();
[[nodiscard]] static UnsignedBigInteger import_data(StringView data) { return import_data(reinterpret_cast<u8 const*>(data.characters_without_null_termination()), data.length()); }
[[nodiscard]] static UnsignedBigInteger import_data(u8 const* ptr, size_t length) { return UnsignedBigInteger(ptr, length); }
[[nodiscard]] static UnsignedBigInteger import_data(ReadonlyBytes data) { return UnsignedBigInteger(data); }
// Exports in big-endian (msb stored first), trimmed (no leading zeros) format
[[nodiscard]] Bytes export_data(Bytes) const;

View file

@ -46,7 +46,7 @@ ErrorOr<UnsignedBigInteger> openssl_bignum_to_unsigned_big_integer(OpenSSL_BN co
auto size = BN_num_bytes(bn.ptr());
auto buf = TRY(ByteBuffer::create_uninitialized(size));
BN_bn2bin(bn.ptr(), buf.bytes().data());
return UnsignedBigInteger::import_data(buf.bytes().data(), size);
return UnsignedBigInteger::import_data(buf);
}
ErrorOr<StringView> hash_kind_to_openssl_digest_name(Hash::HashKind hash)

View file

@ -104,7 +104,7 @@ ErrorOr<EC::KeyPairType> EC::parse_ec_key(ReadonlyBytes der, bool is_private, Ve
READ_OBJECT(OctetString, StringView, private_key_bytes);
POP_SCOPE();
auto private_key = UnsignedBigInteger::import_data(private_key_bytes);
auto private_key = UnsignedBigInteger::import_data(private_key_bytes.bytes());
Optional<Vector<int>> parameters;
if (!decoder.eof()) {

View file

@ -88,20 +88,8 @@ static ::Crypto::UnsignedBigInteger big_integer_from_api_big_integer(GC::Ptr<JS:
auto const& buffer = big_integer->viewed_array_buffer()->buffer();
if (buffer.size() > 0) {
if constexpr (AK::HostIsLittleEndian) {
// We need to reverse the buffer to get it into little-endian order
Vector<u8, 32> reversed_buffer;
reversed_buffer.resize(buffer.size());
for (size_t i = 0; i < buffer.size(); ++i) {
reversed_buffer[buffer.size() - i - 1] = buffer[i];
}
return ::Crypto::UnsignedBigInteger::import_data(reversed_buffer.data(), reversed_buffer.size());
} else {
return ::Crypto::UnsignedBigInteger::import_data(buffer.data(), buffer.size());
}
}
if (buffer.size() > 0)
return ::Crypto::UnsignedBigInteger::import_data(buffer);
return ::Crypto::UnsignedBigInteger(0);
}
@ -136,7 +124,7 @@ WebIDL::ExceptionOr<ByteBuffer> base64_url_bytes_decode(JS::Realm& realm, String
WebIDL::ExceptionOr<::Crypto::UnsignedBigInteger> base64_url_uint_decode(JS::Realm& realm, String const& base64_url_string)
{
auto base64_bytes_be = TRY(base64_url_bytes_decode(realm, base64_url_string));
return ::Crypto::UnsignedBigInteger::import_data(base64_bytes_be.data(), base64_bytes_be.size());
return ::Crypto::UnsignedBigInteger::import_data(base64_bytes_be);
}
// https://w3c.github.io/webcrypto/#concept-parse-an-asn1-structure
@ -3937,8 +3925,8 @@ WebIDL::ExceptionOr<JS::Value> ECDSA::verify(AlgorithmParams const& params, GC::
// and using params as the EC domain parameters, and Q as the public key.
auto half_size = signature.size() / 2;
auto r = ::Crypto::UnsignedBigInteger::import_data(signature.data(), half_size);
auto s = ::Crypto::UnsignedBigInteger::import_data(signature.data() + half_size, half_size);
auto r = ::Crypto::UnsignedBigInteger::import_data(signature.bytes().slice(0, half_size));
auto s = ::Crypto::UnsignedBigInteger::import_data(signature.bytes().slice(half_size, half_size));
auto maybe_result = curve.visit(
[](Empty const&) -> ErrorOr<bool> { VERIFY_NOT_REACHED(); },

View file

@ -226,7 +226,7 @@ TEST_CASE(test_bigint_import_big_endian_decode_encode_roundtrip)
u8 random_bytes[128];
u8 target_buffer[128];
fill_with_random(random_bytes);
auto encoded = Crypto::UnsignedBigInteger::import_data(random_bytes, sizeof(random_bytes));
auto encoded = Crypto::UnsignedBigInteger::import_data(random_bytes);
auto result = encoded.export_data(target_buffer);
EXPECT_EQ(result.size(), sizeof(target_buffer) - count_leading_zeros(random_bytes));
EXPECT(Bytes { random_bytes }.ends_with(result));
@ -237,13 +237,13 @@ TEST_CASE(test_bigint_import_big_endian_encode_decode_roundtrip)
u8 target_buffer[128];
auto encoded = "12345678901234567890"_bigint;
auto result = encoded.export_data({ target_buffer, 128 });
auto decoded = Crypto::UnsignedBigInteger::import_data(result.data(), result.size());
auto decoded = Crypto::UnsignedBigInteger::import_data(result);
EXPECT_EQ(encoded, decoded);
}
TEST_CASE(test_bigint_big_endian_import)
{
auto number = Crypto::UnsignedBigInteger::import_data("hello"sv);
auto number = Crypto::UnsignedBigInteger::import_data("hello"sv.bytes());
EXPECT_EQ(number, "448378203247"_bigint);
}

View file

@ -197,12 +197,12 @@ TEST_CASE(test_secp256r1)
};
// clang-format on
auto alice_private_key = Crypto::UnsignedBigInteger::import_data(alice_private_key_data, 32);
auto alice_private_key = Crypto::UnsignedBigInteger::import_data(alice_private_key_data);
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_private_key = Crypto::UnsignedBigInteger::import_data(bob_private_key_data);
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 private_key = Crypto::UnsignedBigInteger::import_data(private_key_data);
auto expected_public_key = TRY_OR_FAIL(Crypto::Curves::SECPxxxr1Point::from_uncompressed({ expected_public_key_data, 65 }));
Crypto::Curves::SECP256r1 curve;
@ -293,12 +293,12 @@ 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_private_key = Crypto::UnsignedBigInteger::import_data(alice_private_key_data);
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_private_key = Crypto::UnsignedBigInteger::import_data(bob_private_key_data);
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 private_key = Crypto::UnsignedBigInteger::import_data(private_key_data);
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;
@ -406,12 +406,12 @@ TEST_CASE(test_secp521r1)
};
// clang-format on
auto alice_private_key = Crypto::UnsignedBigInteger::import_data(alice_private_key_data.data(), alice_private_key_data.size());
auto alice_private_key = Crypto::UnsignedBigInteger::import_data(alice_private_key_data);
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_private_key = Crypto::UnsignedBigInteger::import_data(bob_private_key_data);
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 private_key = Crypto::UnsignedBigInteger::import_data(private_key_data);
auto expected_public_key = TRY_OR_FAIL(Crypto::Curves::SECPxxxr1Point::from_uncompressed({ expected_public_key_data.data(), expected_public_key_data.size() }));
Crypto::Curves::SECP521r1 curve;

View file

@ -41,7 +41,7 @@ TEST_CASE(test_private_key_info_decode)
0x63, 0x2a, 0x21, 0x56, 0x3e, 0x81, 0xf1, 0x67, 0xe7, 0xe8, 0x47, 0x1a, 0xcb, 0xbf, 0x87,
0x7d, 0xb1, 0x5f, 0xc4, 0x4f
};
EXPECT_EQ(key.modulus(), Crypto::UnsignedBigInteger(modulus, sizeof(modulus)));
EXPECT_EQ(key.modulus(), Crypto::UnsignedBigInteger(modulus));
u8 const private_exponent[] = {
0x69, 0x0b, 0x7e, 0xd4, 0x26, 0x48, 0xd6, 0x41, 0x11, 0xf2, 0xc4, 0xff, 0x37, 0xc6, 0xe6,
@ -50,40 +50,40 @@ TEST_CASE(test_private_key_info_decode)
0x25, 0xbb, 0x8a, 0xb1, 0xe3, 0xcf, 0xdf, 0x62, 0xdf, 0x02, 0xbf, 0xac, 0xfa, 0x26, 0xe4,
0x34, 0x65, 0x01, 0x31
};
EXPECT_EQ(key.private_exponent(), Crypto::UnsignedBigInteger(private_exponent, sizeof(private_exponent)));
EXPECT_EQ(key.private_exponent(), Crypto::UnsignedBigInteger(private_exponent));
u8 const prime1[] = {
0x00, 0xf5, 0x74, 0x59, 0x55, 0x49, 0xc2, 0x27, 0x6c, 0x4e, 0x70, 0x21, 0x42, 0x6f, 0x22,
0x05, 0xf9, 0xdd, 0xc5, 0xf4, 0xa8, 0x6d, 0xe6, 0xb4, 0x7b, 0xec, 0x5f, 0x4a, 0xb1, 0x19,
0x60, 0x51, 0xb9
};
EXPECT_EQ(key.prime1(), Crypto::UnsignedBigInteger(prime1, sizeof(prime1)));
EXPECT_EQ(key.prime1(), Crypto::UnsignedBigInteger(prime1));
u8 const prime2[] = {
0x00, 0xee, 0x43, 0xb6, 0x33, 0x2e, 0x94, 0x3c, 0x57, 0xc0, 0x49, 0xc0, 0xa6, 0xc3, 0x79,
0x7c, 0x35, 0xbd, 0x0b, 0xdc, 0x3c, 0x49, 0xf2, 0x2a, 0x74, 0xce, 0xa0, 0x02, 0xe9, 0x17,
0xa6, 0xea, 0x47
};
EXPECT_EQ(key.prime2(), Crypto::UnsignedBigInteger(prime2, sizeof(prime2)));
EXPECT_EQ(key.prime2(), Crypto::UnsignedBigInteger(prime2));
u8 const exponent1[] = {
0x00, 0x80, 0xd2, 0x9b, 0xc0, 0x23, 0x81, 0xfe, 0xe6, 0xdd, 0x14, 0x04, 0xa0, 0xb5, 0x6b,
0x09, 0xef, 0xe5, 0xf1, 0x6b, 0x42, 0xaa, 0xcb, 0x96, 0x96, 0x23, 0xac, 0xaf, 0xaa, 0xdb,
0x42, 0xae, 0x21
};
EXPECT_EQ(key.exponent1(), Crypto::UnsignedBigInteger(exponent1, sizeof(exponent1)));
EXPECT_EQ(key.exponent1(), Crypto::UnsignedBigInteger(exponent1));
u8 const exponent2[] = {
0x00, 0xc7, 0xc4, 0x75, 0xeb, 0x0b, 0xce, 0xb5, 0x99, 0x4d, 0x5b, 0x88, 0xef, 0x49, 0x4d,
0x7e, 0x5b, 0x00, 0x1a, 0x05, 0x99, 0x76, 0xd6, 0x57, 0xca, 0x7f, 0xc3, 0xa1, 0x2d, 0x15,
0xeb, 0x98, 0xd9
};
EXPECT_EQ(key.exponent2(), Crypto::UnsignedBigInteger(exponent2, sizeof(exponent2)));
EXPECT_EQ(key.exponent2(), Crypto::UnsignedBigInteger(exponent2));
u8 const coefficient[] = {
0x6f, 0x48, 0x45, 0xd7, 0x12, 0x88, 0xd4, 0x7f, 0x52, 0xdf, 0x10, 0x7c, 0x3d, 0xcd, 0xf4,
0x16, 0xf5, 0x1e, 0xc5, 0x26, 0xf8, 0x1b, 0xf3, 0x73, 0xcc, 0x86, 0xce, 0x5f, 0x3d, 0xa5,
0x84, 0x93
};
EXPECT_EQ(key.coefficient(), Crypto::UnsignedBigInteger(coefficient, sizeof(coefficient)));
EXPECT_EQ(key.coefficient(), Crypto::UnsignedBigInteger(coefficient));
}