Skip to content

Truly independent web browser

LibCrypto: Stop using UnsignedBigInteger outside big integer code

Shannon Booth4e584e4ad2b9
diff --git a/Libraries/LibCrypto/ASN1/DER.cpp b/Libraries/LibCrypto/ASN1/DER.cpp
index 445ce74bc8f..39d9c8d2b56 100644
--- a/Libraries/LibCrypto/ASN1/DER.cpp
+++ b/Libraries/LibCrypto/ASN1/DER.cpp
@@ -59,31 +59,27 @@ ErrorOr<void> Encoder::write_bytes(ReadonlyBytes bytes)
return {};
}
-ErrorOr<void> Encoder::write_arbitrary_sized_integer(UnsignedBigInteger const& value, Optional<Class> class_override, Optional<Kind> kind_override)
+ErrorOr<void> Encoder::write_integer(u64 value, Optional<Class> class_override, Optional<Kind> kind_override)
{
auto class_ = class_override.value_or(Class::Universal);
auto type = Type::Primitive;
auto kind = kind_override.value_or(Kind::Integer);
TRY(write_tag(class_, type, kind));
- auto max_byte_size = max(1ull, value.byte_length()); // At minimum, we need one byte to encode 0.
- ByteBuffer buffer;
- auto output = TRY(buffer.get_bytes_for_writing(max_byte_size));
- auto result = value.export_data(output);
- // DER does not allow empty integers, encode a zero if the exported size is zero.
- if (result.is_empty()) {
- output[0] = 0;
- result = output.slice(0, 1);
- }
-
- // If the MSB is set, we need to add a leading zero to indicate a positive number.
- if ((output[0] & 0x80) != 0) {
- TRY(write_length(result.size() + 1));
- TRY(write_byte(0));
- } else {
- TRY(write_length(result.size()));
- }
- return write_bytes(result);
+ // NB: The contents are the fewest big-endian bytes that hold the value, at least one, after a zero byte if the top
+ // bit is set, which keeps the integer positive.
+ u8 bytes[sizeof(u64) + 1] {};
+ for (size_t i = 0; i < sizeof(u64); ++i)
+ bytes[sizeof(bytes) - 1 - i] = static_cast<u8>(value >> (8 * i));
+ size_t start = 1;
+ while (start < sizeof(bytes) - 1 && bytes[start] == 0)
+ ++start;
+ if ((bytes[start] & 0x80) != 0)
+ --start;
+
+ ReadonlyBytes contents { bytes + start, sizeof(bytes) - start };
+ TRY(write_length(contents.size()));
+ return write_bytes(contents);
}
ErrorOr<void> Encoder::write_octet_string(ReadonlyBytes bytes, Optional<Class> class_override, Optional<Kind> kind_override)
diff --git a/Libraries/LibCrypto/ASN1/DER.h b/Libraries/LibCrypto/ASN1/DER.h
index c52f9dbaf32..9b797ad93af 100644
--- a/Libraries/LibCrypto/ASN1/DER.h
+++ b/Libraries/LibCrypto/ASN1/DER.h
@@ -7,10 +7,10 @@
#pragma once
#include <AK/Array.h>
+#include <AK/ByteBuffer.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <LibCrypto/ASN1/ASN1.h>
-#include <LibCrypto/BigInt/UnsignedBigInteger.h>
namespace Crypto::ASN1 {
@@ -51,8 +51,8 @@ public:
template<typename ValueType>
ErrorOr<void> write(ValueType const& value, Optional<Class> class_override = {}, Optional<Kind> kind_override = {})
{
- if constexpr (IsSame<ValueType, UnsignedBigInteger> || (IsIntegral<ValueType> && IsUnsigned<ValueType> && !IsSame<ValueType, bool>)) {
- return write_arbitrary_sized_integer(value, class_override, kind_override);
+ if constexpr (IsIntegral<ValueType> && IsUnsigned<ValueType> && !IsSame<ValueType, bool>) {
+ return write_integer(value, class_override, kind_override);
} else if constexpr (IsOneOf<ValueType, ReadonlyBytes, ByteBuffer>) {
return write_octet_string(value, class_override, kind_override);
} else if constexpr (IsSame<ValueType, nullptr_t>) {
@@ -98,7 +98,7 @@ private:
ErrorOr<void> write_bytes(ReadonlyBytes);
ErrorOr<void> write_byte(u8);
- ErrorOr<void> write_arbitrary_sized_integer(UnsignedBigInteger const&, Optional<Class>, Optional<Kind>);
+ ErrorOr<void> write_integer(u64, Optional<Class>, Optional<Kind>);
ErrorOr<void> write_octet_string(ReadonlyBytes, Optional<Class>, Optional<Kind>);
ErrorOr<void> write_null(Optional<Class>, Optional<Kind>);
ErrorOr<void> write_object_identifier(Span<u32 const>, Optional<Class>, Optional<Kind>);
diff --git a/Libraries/LibCrypto/Authentication/KMAC.cpp b/Libraries/LibCrypto/Authentication/KMAC.cpp
index d0dea617420..4296649d8b4 100644
--- a/Libraries/LibCrypto/Authentication/KMAC.cpp
+++ b/Libraries/LibCrypto/Authentication/KMAC.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Vector.h>
#include <LibCrypto/Authentication/KMAC.h>
#include <LibCrypto/OpenSSL.h>
diff --git a/Libraries/LibCrypto/Hash/HKDF.h b/Libraries/LibCrypto/Hash/HKDF.h
index c171179113c..15d339f2173 100644
--- a/Libraries/LibCrypto/Hash/HKDF.h
+++ b/Libraries/LibCrypto/Hash/HKDF.h
@@ -8,6 +8,8 @@
#pragma once
+#include <AK/ByteBuffer.h>
+#include <AK/Optional.h>
#include <LibCrypto/Hash/Hash.h>
namespace Crypto::Hash {
diff --git a/Libraries/LibCrypto/Hash/PBKDF2.h b/Libraries/LibCrypto/Hash/PBKDF2.h
index a0046f1491e..6ae783ea6f8 100644
--- a/Libraries/LibCrypto/Hash/PBKDF2.h
+++ b/Libraries/LibCrypto/Hash/PBKDF2.h
@@ -7,6 +7,7 @@
#pragma once
+#include <AK/ByteBuffer.h>
#include <LibCrypto/Hash/Hash.h>
namespace Crypto::Hash {
diff --git a/Libraries/LibCrypto/OpenSSL.cpp b/Libraries/LibCrypto/OpenSSL.cpp
index eeab74a51f2..da4612cf0b7 100644
--- a/Libraries/LibCrypto/OpenSSL.cpp
+++ b/Libraries/LibCrypto/OpenSSL.cpp
@@ -68,23 +68,6 @@ ErrorOr<OpenSSL_CIPHER_CTX> OpenSSL_CIPHER_CTX::create()
return OpenSSL_CIPHER_CTX(OPENSSL_TRY_PTR(EVP_CIPHER_CTX_new()));
}
-ErrorOr<OpenSSL_BN> unsigned_big_integer_to_openssl_bignum(UnsignedBigInteger const& integer)
-{
- auto bn = TRY(OpenSSL_BN::create());
- auto buf = TRY(ByteBuffer::create_uninitialized(integer.byte_length()));
- auto result = integer.export_data(buf.bytes());
- OPENSSL_TRY_PTR(BN_bin2bn(result.data(), result.size(), bn.ptr()));
- return bn;
-}
-
-ErrorOr<UnsignedBigInteger> openssl_bignum_to_unsigned_big_integer(OpenSSL_BN const& bn)
-{
- 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);
-}
-
ErrorOr<ByteBuffer> get_byte_buffer_param_from_key(EVP_PKEY* key, char const* key_name)
{
size_t size;
diff --git a/Libraries/LibCrypto/OpenSSL.h b/Libraries/LibCrypto/OpenSSL.h
index 6a9df68021a..db81dbd7e2e 100644
--- a/Libraries/LibCrypto/OpenSSL.h
+++ b/Libraries/LibCrypto/OpenSSL.h
@@ -8,7 +8,6 @@
#include <AK/Error.h>
#include <AK/Format.h>
-#include <LibCrypto/BigInt/UnsignedBigInteger.h>
#include <LibCrypto/Hash/Hash.h>
#include <LibCrypto/OpenSSLForward.h>
@@ -153,9 +152,6 @@ public:
#undef OPENSSL_WRAPPER_CLASS
-ErrorOr<OpenSSL_BN> unsigned_big_integer_to_openssl_bignum(UnsignedBigInteger const& integer);
-ErrorOr<UnsignedBigInteger> openssl_bignum_to_unsigned_big_integer(OpenSSL_BN const& bn);
-
ErrorOr<ByteBuffer> get_byte_buffer_param_from_key(EVP_PKEY* key, char const* key_name);
}
diff --git a/Libraries/LibCrypto/Rust/src/ffi.rs b/Libraries/LibCrypto/Rust/src/ffi.rs
index 8a3b51e60f4..4da711280d5 100644
--- a/Libraries/LibCrypto/Rust/src/ffi.rs
+++ b/Libraries/LibCrypto/Rust/src/ffi.rs
@@ -169,7 +169,6 @@ unsafe extern "C" {
pub(super) fn crypto_rsa_key_info_has_encryption_oid(structure: Asn1Structure, key_info: *const c_void) -> bool;
pub(super) fn crypto_rsa_key_info_read_key(structure: Asn1Structure, key_info: *const c_void) -> *mut c_void;
pub(super) fn crypto_rsa_key_is_valid(key: *const c_void, valid: &mut bool) -> bool;
- pub(super) fn crypto_rsa_validate_public_exponent(exponent: Span<'_, u8>, modulus_length: u32) -> bool;
pub(super) fn crypto_rsa_validate_modulus_length(modulus_length: u32) -> bool;
pub(super) fn crypto_rsa_generate_key_pair(
modulus_length: u32,
@@ -177,11 +176,6 @@ unsafe extern "C" {
public_key: &mut *mut c_void,
private_key: &mut *mut c_void,
) -> bool;
- pub(super) fn crypto_rsa_export_public_exponent(
- exponent: Span<'_, u8>,
- state: *mut c_void,
- receive: ReceiveBytes,
- ) -> bool;
pub(super) fn crypto_rsa_read_key_metadata(
key: *const c_void,
modulus_length: &mut u32,
diff --git a/Libraries/LibCrypto/Rust/src/lib.rs b/Libraries/LibCrypto/Rust/src/lib.rs
index cad8869fec4..edb2cd8d78a 100644
--- a/Libraries/LibCrypto/Rust/src/lib.rs
+++ b/Libraries/LibCrypto/Rust/src/lib.rs
@@ -485,9 +485,26 @@ pub fn rsa_key_is_valid(key: &KeyHandle) -> Result<bool, BackendError> {
Ok(value)
}
+/// Whether the big-endian `exponent` is at least 3, is odd, and is less than 2^modulus_length - 1.
pub fn rsa_validate_public_exponent(exponent: &[u8], modulus_length: u32) -> bool {
- // SAFETY: The input is borrowed for the call.
- unsafe { ffi::crypto_rsa_validate_public_exponent(Span::new(exponent), modulus_length) }
+ let exponent = strip_leading_zeros(exponent);
+ let (Some(&first), Some(&last)) = (exponent.first(), exponent.last()) else {
+ return false;
+ };
+ if (exponent.len() == 1 && first < 3) || last & 1 == 0 {
+ return false;
+ }
+
+ // NB: exponent + 1 has one bit more than exponent exactly when every bit of exponent is set.
+ let bit_length = (exponent.len() as u64 - 1) * 8 + u64::from(8 - first.leading_zeros());
+ let all_bits_set = (first & first.wrapping_add(1)) == 0 && exponent[1..].iter().all(|&byte| byte == 0xff);
+ let incremented_bit_length = bit_length + u64::from(all_bits_set);
+ incremented_bit_length <= u64::from(modulus_length)
+}
+
+fn strip_leading_zeros(bytes: &[u8]) -> &[u8] {
+ let leading_zeros = bytes.iter().take_while(|&&byte| byte == 0).count();
+ &bytes[leading_zeros..]
}
pub fn rsa_validate_modulus_length(modulus_length: u32) -> bool {
@@ -507,13 +524,9 @@ pub fn rsa_generate_key_pair(modulus_length: u32, exponent: &[u8]) -> Result<Key
Ok(unsafe { KeyPair::from_raw(public_key, private_key) })
}
-pub fn rsa_export_public_exponent(exponent: &[u8]) -> Result<Vec<u8>, BackendError> {
- // SAFETY: Inputs and output are borrowed for the synchronous native call.
- let (status, bytes) = receive_native_bytes(|state, receive| unsafe {
- ffi::crypto_rsa_export_public_exponent(Span::new(exponent), state, receive)
- });
- status.then_some(()).ok_or(BackendError)?;
- Ok(bytes)
+/// The big-endian `exponent` with no leading zero bytes.
+pub fn rsa_export_public_exponent(exponent: &[u8]) -> Vec<u8> {
+ strip_leading_zeros(exponent).to_vec()
}
pub fn rsa_export_der(key: &KeyHandle, is_public: bool) -> Result<Vec<u8>, BackendError> {
diff --git a/Libraries/LibCrypto/RustBindings.cpp b/Libraries/LibCrypto/RustBindings.cpp
index aa9f07ea0ae..db298e0bd81 100644
--- a/Libraries/LibCrypto/RustBindings.cpp
+++ b/Libraries/LibCrypto/RustBindings.cpp
@@ -12,6 +12,8 @@
#include <AK/Array.h>
#include <AK/Memory.h>
#include <AK/Random.h>
+#include <AK/ScopeGuard.h>
+#include <AK/Utf16String.h>
#include <LibCrypto/ASN1/ASN1.h>
#include <LibCrypto/ASN1/Constants.h>
#include <LibCrypto/ASN1/DER.h>
@@ -100,10 +102,8 @@ void crypto_asn1_delete_key_info(Asn1Structure, void*);
bool crypto_rsa_key_info_has_encryption_oid(Asn1Structure, void const*);
KeyHandle* crypto_rsa_key_info_read_key(Asn1Structure, void const*);
bool crypto_rsa_key_is_valid(KeyHandle const*, bool*);
-bool crypto_rsa_validate_public_exponent(ReadonlyBytes, u32);
bool crypto_rsa_validate_modulus_length(u32);
bool crypto_rsa_generate_key_pair(u32, ReadonlyBytes, KeyHandle**, KeyHandle**);
-bool crypto_rsa_export_public_exponent(ReadonlyBytes, void*, ReceiveEncodedBytes);
bool crypto_rsa_read_key_metadata(KeyHandle const*, u32*, void*, ReceiveEncodedBytes);
KeyHandle* crypto_rsa_key_from_jwk_components(bool, ReadonlySpan<ReadonlyBytes>);
bool crypto_rsa_export_der(KeyHandle const*, bool, void*, ReceiveEncodedBytes);
@@ -169,14 +169,6 @@ bool crypto_rsa_key_is_valid(KeyHandle const* key, bool* valid)
return true;
}
-bool crypto_rsa_validate_public_exponent(ReadonlyBytes bytes, u32 modulus_length)
-{
- auto public_exponent = ::Crypto::UnsignedBigInteger::import_data(bytes);
- return public_exponent >= ::Crypto::UnsignedBigInteger { 3 }
- && public_exponent.is_odd()
- && public_exponent.plus(1).one_based_index_of_highest_set_bit() <= modulus_length;
-}
-
bool crypto_rsa_validate_modulus_length(u32 modulus_length)
{
return !::Crypto::PK::RSA::validate_key_generation_parameters(modulus_length).is_error();
@@ -192,18 +184,6 @@ bool crypto_rsa_generate_key_pair(u32 modulus_length, ReadonlyBytes public_expon
return true;
}
-static ErrorOr<ByteBuffer> export_rsa_exponent(::Crypto::UnsignedBigInteger const& exponent)
-{
- auto bytes = TRY(ByteBuffer::create_uninitialized(exponent.byte_length()));
- return ByteBuffer::copy(exponent.export_data(bytes.span()));
-}
-
-bool crypto_rsa_export_public_exponent(ReadonlyBytes public_exponent, void* state, ReceiveEncodedBytes receive)
-{
- auto bytes = export_rsa_exponent(::Crypto::UnsignedBigInteger::import_data(public_exponent));
- return receive_byte_result(bytes, state, receive);
-}
-
bool crypto_rsa_read_key_metadata(KeyHandle const* key, u32* modulus_length, void* state, ReceiveEncodedBytes receive)
{
auto result = key->visit(
diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
index 709260cdb02..91891d05259 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs
@@ -5548,16 +5548,6 @@ fn validate_rsa_key_generation_parameters(normalized_algorithm: &RsaKeyGenParams
Ok(())
}
-fn export_public_exponent(vm: &Vm, exponent: &Bytes) -> WebIdlResult<Vec<u8>> {
- let result = libcrypto_rust::rsa_export_public_exponent(exponent.as_slice()).map(Bytes::new);
- if result.is_err() {
- return vm
- .throw_completion(ErrorKind::InternalError, ErrorType::OutOfMemory, &[])
- .map_err(Into::into);
- }
- Ok(Rc::unwrap_or_clone(result.expect("LibCrypto returns the exponent").0))
-}
-
fn rsa_key_metadata(vm: &Vm, key: Ref<CryptoKey>) -> WebIdlResult<(u32, Vec<u8>)> {
libcrypto_rust::rsa_read_key_metadata(key.handle()).map_err(|_| out_of_memory(vm))
}
@@ -5798,7 +5788,8 @@ impl AlgorithmMethods for RsaOaep {
return Err(dom_error("OperationError", b"Failed generating RSA key pair"));
};
- let public_exponent = export_public_exponent(cx.vm(), &normalized_algorithm.key_gen.public_exponent)?;
+ let public_exponent =
+ libcrypto_rust::rsa_export_public_exponent(normalized_algorithm.key_gen.public_exponent.as_slice());
let hash_name = normalized_algorithm.hash.name.clone();
// 10. Let publicKey be a new CryptoKey representing the public key of the generated key pair.
@@ -6397,7 +6388,8 @@ impl AlgorithmMethods for RsaPss {
return Err(dom_error("OperationError", b"Failed to generate RSA key pair"));
};
- let public_exponent = export_public_exponent(cx.vm(), &normalized_algorithm.key_gen.public_exponent)?;
+ let public_exponent =
+ libcrypto_rust::rsa_export_public_exponent(normalized_algorithm.key_gen.public_exponent.as_slice());
let hash_name = normalized_algorithm.hash.name.clone();
// 10. Let publicKey be a new CryptoKey representing the public key of the generated key pair.
@@ -7103,7 +7095,8 @@ impl AlgorithmMethods for RsassaPkcs1 {
return Err(dom_error("OperationError", b"Failed to generate RSA key pair"));
};
- let public_exponent = export_public_exponent(cx.vm(), &normalized_algorithm.key_gen.public_exponent)?;
+ let public_exponent =
+ libcrypto_rust::rsa_export_public_exponent(normalized_algorithm.key_gen.public_exponent.as_slice());
let hash_name = normalized_algorithm.hash.name.clone();
// 10. Let publicKey be a new CryptoKey representing the public key of the generated key pair.
diff --git a/Tests/LibCrypto/TestAES.cpp b/Tests/LibCrypto/TestAES.cpp
index b31c1dccd43..089718cf9e2 100644
--- a/Tests/LibCrypto/TestAES.cpp
+++ b/Tests/LibCrypto/TestAES.cpp
@@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <LibCrypto/BigInt/UnsignedBigInteger.h>
#include <LibCrypto/Cipher/AES.h>
#include <LibTest/TestCase.h>
#include <cstring>
diff --git a/Tests/LibCrypto/TestASN1.cpp b/Tests/LibCrypto/TestASN1.cpp
index 8b588e85c32..ea5ce8403eb 100644
--- a/Tests/LibCrypto/TestASN1.cpp
+++ b/Tests/LibCrypto/TestASN1.cpp
@@ -22,8 +22,8 @@ static void expect_encoding(ByteBuffer const& encoded, Vector<u8> const& expecte
TEST_CASE(test_encoder_integers)
{
- expect_encoding(encode([](auto& encoder) { return encoder.write(Crypto::UnsignedBigInteger { 0 }); }), { 0x02, 0x01, 0x00 });
- expect_encoding(encode([](auto& encoder) { return encoder.write(Crypto::UnsignedBigInteger { 1 }); }), { 0x02, 0x01, 0x01 });
+ expect_encoding(encode([](auto& encoder) { return encoder.write(0u); }), { 0x02, 0x01, 0x00 });
+ expect_encoding(encode([](auto& encoder) { return encoder.write(1u); }), { 0x02, 0x01, 0x01 });
expect_encoding(encode([](auto& encoder) { return encoder.write(0x1234u); }), { 0x02, 0x02, 0x12, 0x34 });
// A leading zero keeps an integer whose top bit is set positive.
diff --git a/Tests/LibCrypto/TestRSA.cpp b/Tests/LibCrypto/TestRSA.cpp
index 0a6627c3778..64435d1ac06 100644
--- a/Tests/LibCrypto/TestRSA.cpp
+++ b/Tests/LibCrypto/TestRSA.cpp
@@ -6,6 +6,7 @@
*/
#include <AK/Base64.h>
+#include <AK/Hex.h>
#include <LibCrypto/ASN1/Constants.h>
#include <LibCrypto/Hash/Hash.h>
#include <LibCrypto/PK/KeyInfo.h>
@@ -14,9 +15,10 @@
#include <LibTest/TestCase.h>
#include <cstring>
-static Crypto::UnsignedBigInteger to_integer(ByteBuffer const& bytes)
+// An RSA component in hex, as the big-endian bytes that RSAPrivateKey::component() gives.
+static ByteBuffer integer(StringView hex)
{
- return Crypto::UnsignedBigInteger::import_data(bytes);
+ return MUST(decode_hex(hex));
}
static ByteBuffer operator""_b(char const* string, size_t length)
@@ -64,10 +66,10 @@ TEST_CASE(test_RSA_ASN1_PKCS1_DER_parse)
auto private_key_info = TRY_OR_FAIL(Crypto::PK::wrap_in_private_key_info(der, Crypto::ASN1::rsa_encryption_oid, nullptr));
size_t consumed = 0;
auto private_key = TRY_OR_FAIL(Crypto::PK::parse_private_key_info(private_key_info, consumed)).rsa;
- if (to_integer(TRY_OR_FAIL(private_key.component(Crypto::PK::RSAPrivateKey::Component::PublicExponent))) != 65537) {
+ if (TRY_OR_FAIL(private_key.component(Crypto::PK::RSAPrivateKey::Component::PublicExponent)) != integer("010001"sv)) {
FAIL("Invalid public exponent");
}
- if (to_integer(TRY_OR_FAIL(private_key.component(Crypto::PK::RSAPrivateKey::Component::PrivateExponent))) != "4234603516465654167360850580101327813936403862038934287300450163438938741499875303761385527882335478349599685406941909381269804396099893549838642251053393"_bigint) {
+ if (TRY_OR_FAIL(private_key.component(Crypto::PK::RSAPrivateKey::Component::PrivateExponent)) != integer("50da4f926b441c310dc6ca100545ef5c361e5dd78f4a2201261a548e834e62847947dcf9a17d9ca5ccb2910e7c142daf28a83ec7c37ac5ccc6eea3bde6dc3951"sv)) {
FAIL("Invalid private exponent");
}
}
@@ -96,11 +98,11 @@ TEST_CASE(test_RSA_ASN1_PKCS8_DER_parse)
size_t consumed = 0;
auto private_key = TRY_OR_FAIL(Crypto::PK::parse_private_key_info(der, consumed)).rsa;
- if (to_integer(TRY_OR_FAIL(private_key.component(Crypto::PK::RSAPrivateKey::Component::PublicExponent))) != 65537) {
+ if (TRY_OR_FAIL(private_key.component(Crypto::PK::RSAPrivateKey::Component::PublicExponent)) != integer("010001"sv)) {
FAIL("Invalid public exponent");
}
- if (to_integer(TRY_OR_FAIL(private_key.component(Crypto::PK::RSAPrivateKey::Component::PrivateExponent))) != "16848664331299797559656678180469464902267415922431923391961407795209879741791261105581093539484181644099608161661780611501562625272630894063592208758992911105496755004417051031019663332258403844985328863382168329621318366311519850803972480500782200178279692319955495383119697563295214236936264406600739633470565823022975212999060908747002623721589308539473108154612454595201561671949550531384574873324370774408913092560971930541734744950937900805812300970883306404011323308000168926094053141613790857814489531436452649384151085451448183385611208320292948291211969430321231180227006521681776197974694030147965578466993"_bigint) {
+ if (TRY_OR_FAIL(private_key.component(Crypto::PK::RSAPrivateKey::Component::PrivateExponent)) != integer("857795de66b7dd19fdec6a036161f9fa1f332371cb3e4bbf677db191c02f915390a6cd0f5f13b5a20a9cc49be8697caca264efab25ca8ed595bf281d710106c9b5995d0a7d57a70251d2e10ca3a66753079633f7b551da742e0df505a47342d86411bf92cccd02163c57dac4ac58a5134e7ef39be4692ce1b4e6776b631406113026c780fc35962280f5d60a938341d37d35118d397b8673f118ea60792498e3c3ec39e315b1ea70552354a2b9137bc35a3c4d5d46e24ae74208ded62c43b278764d77e97a912015c59b82029d06dc36b99dd5e0212b5ccb88588bf7cbdd74106cda718df86c8e50f7b5d1cdaf734d60302ac5cc69b323b5342a42216dd956b1"sv)) {
FAIL("Invalid private exponent");
}
}
diff --git a/Tests/LibTLS/TestTLSCertificateParser.cpp b/Tests/LibTLS/TestTLSCertificateParser.cpp
index 43027594909..2bf38a62900 100644
--- a/Tests/LibTLS/TestTLSCertificateParser.cpp
+++ b/Tests/LibTLS/TestTLSCertificateParser.cpp
@@ -50,13 +50,16 @@ TEST_CASE(test_private_key_info_decode)
EXPECT_EQ(private_key_info.algorithm.identifier, Crypto::ASN1::rsa_encryption_oid);
auto& key = private_key_info.rsa;
using Component = Crypto::PK::RSAPrivateKey::Component;
- auto component = [&](Component component) {
+ // NB: A component has no leading zero bytes, which `openssl rsa -text` prints before a top bit that is set.
+ auto expect_component = [&](Component component, ReadonlyBytes expected) {
auto bytes = MUST(key.component(component));
- return Crypto::UnsignedBigInteger::import_data(bytes);
+ while (!expected.is_empty() && expected[0] == 0)
+ expected = expected.slice(1);
+ EXPECT_EQ(bytes.bytes(), expected);
};
- EXPECT_EQ(key.length() * 8, 512u);
- EXPECT_EQ(component(Component::PublicExponent), 65537);
+ u8 const public_exponent[] = { 0x01, 0x00, 0x01 };
+ expect_component(Component::PublicExponent, public_exponent);
// $ openssl rsa -in testcase.pem -text
@@ -67,7 +70,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(component(Component::Modulus), Crypto::UnsignedBigInteger(modulus));
+ expect_component(Component::Modulus, modulus);
u8 const private_exponent[] = {
0x69, 0x0b, 0x7e, 0xd4, 0x26, 0x48, 0xd6, 0x41, 0x11, 0xf2, 0xc4, 0xff, 0x37, 0xc6, 0xe6,
@@ -76,40 +79,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(component(Component::PrivateExponent), Crypto::UnsignedBigInteger(private_exponent));
+ expect_component(Component::PrivateExponent, 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(component(Component::Prime1), Crypto::UnsignedBigInteger(prime1));
+ expect_component(Component::Prime1, 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(component(Component::Prime2), Crypto::UnsignedBigInteger(prime2));
+ expect_component(Component::Prime2, 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(component(Component::Exponent1), Crypto::UnsignedBigInteger(exponent1));
+ expect_component(Component::Exponent1, 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(component(Component::Exponent2), Crypto::UnsignedBigInteger(exponent2));
+ expect_component(Component::Exponent2, 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(component(Component::Coefficient), Crypto::UnsignedBigInteger(coefficient));
+ expect_component(Component::Coefficient, coefficient);
}