Skip to content

Truly independent web browser

LibCrypto: Remove unpadded RSA and RSA PKCS#1 v1.5 encryption

Shannon Booth5926ee7d1d04
diff --git a/Libraries/LibCrypto/PK/MLDSA.h b/Libraries/LibCrypto/PK/MLDSA.h
index 75cc3fae162..b2abae1b7ae 100644
--- a/Libraries/LibCrypto/PK/MLDSA.h
+++ b/Libraries/LibCrypto/PK/MLDSA.h
@@ -90,7 +90,6 @@ public:
ErrorOr<ByteBuffer> encrypt(ReadonlyBytes) override { return Error::from_string_literal("Operation not supported"); }
ErrorOr<ByteBuffer> decrypt(ReadonlyBytes) override { return Error::from_string_literal("Operation not supported"); }
ErrorOr<bool> verify(ReadonlyBytes, ReadonlyBytes) override;
- ByteString class_name() const override { return "ML-DSA"; }
private:
MLDSASize m_size;
diff --git a/Libraries/LibCrypto/PK/PK.h b/Libraries/LibCrypto/PK/PK.h
index d9481fb4335..7c073fe2e72 100644
--- a/Libraries/LibCrypto/PK/PK.h
+++ b/Libraries/LibCrypto/PK/PK.h
@@ -237,8 +237,6 @@ public:
virtual ErrorOr<bool> verify(ReadonlyBytes message, ReadonlyBytes signature) = 0;
virtual ErrorOr<ByteBuffer> sign(ReadonlyBytes message) = 0;
- virtual ByteString class_name() const = 0;
-
protected:
virtual ~PKSystem() = default;
diff --git a/Libraries/LibCrypto/PK/RSA.cpp b/Libraries/LibCrypto/PK/RSA.cpp
index 2d629a02816..19aea8c8567 100644
--- a/Libraries/LibCrypto/PK/RSA.cpp
+++ b/Libraries/LibCrypto/PK/RSA.cpp
@@ -361,12 +361,6 @@ ErrorOr<bool> RSAPrivateKey::is_valid() const
return false;
}
-ErrorOr<void> RSA::configure(OpenSSL_PKEY_CTX& ctx)
-{
- OPENSSL_TRY(EVP_PKEY_CTX_set_rsa_padding(ctx.ptr(), RSA_NO_PADDING));
- return {};
-}
-
ErrorOr<ByteBuffer> RSA::encrypt(ReadonlyBytes in)
{
auto key = TRY(public_key_to_openssl_pkey(m_public_key));
@@ -404,44 +398,6 @@ ErrorOr<ByteBuffer> RSA::decrypt(ReadonlyBytes in)
return out.slice(0, out_size);
}
-ErrorOr<ByteBuffer> RSA::sign(ReadonlyBytes message)
-{
- auto key = TRY(private_key_to_openssl_pkey(m_private_key));
-
- auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_pkey(nullptr, key.ptr(), nullptr)));
-
- OPENSSL_TRY(EVP_PKEY_sign_init(ctx.ptr()));
- TRY(configure(ctx));
-
- size_t signature_size = 0;
- OPENSSL_TRY(EVP_PKEY_sign(ctx.ptr(), nullptr, &signature_size, message.data(), message.size()));
-
- auto signature = TRY(ByteBuffer::create_uninitialized(signature_size));
- OPENSSL_TRY(EVP_PKEY_sign(ctx.ptr(), signature.data(), &signature_size, message.data(), message.size()));
- return signature.slice(0, signature_size);
-}
-
-ErrorOr<bool> RSA::verify(ReadonlyBytes message, ReadonlyBytes signature)
-{
- if (!TRY(m_public_key.is_valid()))
- return false;
-
- auto key = TRY(public_key_to_openssl_pkey(m_public_key));
-
- auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_pkey(nullptr, key.ptr(), nullptr)));
-
- OPENSSL_TRY(EVP_PKEY_verify_init(ctx.ptr()));
- TRY(configure(ctx));
-
- auto ret = EVP_PKEY_verify(ctx.ptr(), signature.data(), signature.size(), message.data(), message.size());
- if (ret == 1)
- return true;
- if (ret == 0)
- return false;
- OPENSSL_TRY(ret);
- VERIFY_NOT_REACHED();
-}
-
ErrorOr<EVP_MD const*> hash_kind_to_hash_type(Hash::HashKind hash_kind)
{
switch (hash_kind) {
@@ -512,12 +468,6 @@ ErrorOr<ByteBuffer> RSA_EMSA::sign(ReadonlyBytes message)
return signature.slice(0, signature_size);
}
-ErrorOr<void> RSA_PKCS1_EME::configure(OpenSSL_PKEY_CTX& ctx)
-{
- OPENSSL_TRY(EVP_PKEY_CTX_set_rsa_padding(ctx.ptr(), RSA_PKCS1_PADDING));
- return {};
-}
-
ErrorOr<void> RSA_PKCS1_EMSA::configure(OpenSSL_PKEY_CTX& ctx)
{
OPENSSL_TRY(EVP_PKEY_CTX_set_rsa_padding(ctx.ptr(), RSA_PKCS1_PADDING));
diff --git a/Libraries/LibCrypto/PK/RSA.h b/Libraries/LibCrypto/PK/RSA.h
index 313bf421851..bf91ae7ecb0 100644
--- a/Libraries/LibCrypto/PK/RSA.h
+++ b/Libraries/LibCrypto/PK/RSA.h
@@ -167,22 +167,11 @@ public:
virtual ErrorOr<ByteBuffer> encrypt(ReadonlyBytes in) override;
virtual ErrorOr<ByteBuffer> decrypt(ReadonlyBytes in) override;
- virtual ErrorOr<ByteBuffer> sign(ReadonlyBytes message) override;
- virtual ErrorOr<bool> verify(ReadonlyBytes message, ReadonlyBytes signature) override;
-
- virtual ByteString class_name() const override
- {
- return "RSA";
- }
-
PrivateKeyType const& private_key() const { return m_private_key; }
PublicKeyType const& public_key() const { return m_public_key; }
- void set_public_key(PublicKeyType const& key) { m_public_key = key; }
- void set_private_key(PrivateKeyType const& key) { m_private_key = key; }
-
protected:
- virtual ErrorOr<void> configure(OpenSSL_PKEY_CTX& ctx);
+ virtual ErrorOr<void> configure(OpenSSL_PKEY_CTX& ctx) = 0;
};
ErrorOr<EVP_MD const*> hash_kind_to_hash_type(Hash::HashKind hash_kind);
@@ -238,25 +227,6 @@ protected:
Hash::HashKind m_hash_kind { Hash::HashKind::Unknown };
};
-class RSA_PKCS1_EME : public RSA_EME {
-public:
- template<typename... Args>
- RSA_PKCS1_EME(Args... args)
- : RSA_EME(Hash::HashKind::None, args...)
- {
- }
-
- ~RSA_PKCS1_EME() = default;
-
- virtual ByteString class_name() const override
- {
- return "RSA_PKCS1-EME";
- }
-
-protected:
- ErrorOr<void> configure(OpenSSL_PKEY_CTX& ctx) override;
-};
-
class RSA_PKCS1_EMSA : public RSA_EMSA {
public:
template<typename... Args>
@@ -267,11 +237,6 @@ public:
~RSA_PKCS1_EMSA() = default;
- virtual ByteString class_name() const override
- {
- return "RSA_PKCS1-EMSA";
- }
-
protected:
ErrorOr<void> configure(OpenSSL_PKEY_CTX& ctx) override;
};
@@ -286,11 +251,6 @@ public:
~RSA_OAEP_EME() = default;
- virtual ByteString class_name() const override
- {
- return "RSA_OAEP-EME";
- }
-
void set_label(ReadonlyBytes label) { m_label = label; }
protected:
@@ -310,11 +270,6 @@ public:
~RSA_PSS_EMSA() = default;
- virtual ByteString class_name() const override
- {
- return "RSA_PSS-EMSA";
- }
-
void set_salt_length(u32 value) { m_salt_length = value; }
virtual ErrorOr<bool> verify(ReadonlyBytes message, ReadonlyBytes signature) override;
diff --git a/Tests/LibCrypto/TestRSA.cpp b/Tests/LibCrypto/TestRSA.cpp
index 7ec047299d9..334d4c9d2c0 100644
--- a/Tests/LibCrypto/TestRSA.cpp
+++ b/Tests/LibCrypto/TestRSA.cpp
@@ -43,31 +43,7 @@ TEST_CASE(test_RSA_rejects_invalid_public_key)
EXPECT(!TRY_OR_FAIL(verifier.verify(message, encoded_message)));
}
-TEST_CASE(test_RSA_raw_encrypt)
-{
- ByteBuffer data { "hellohellohellohellohellohellohellohellohellohellohellohello123-"_b };
- u8 result[] { 0x6f, 0x7b, 0xe2, 0xd3, 0x95, 0xf8, 0x8d, 0x87, 0x6d, 0x10, 0x5e, 0xc3, 0xcd, 0xf7, 0xbb, 0xa6, 0x62, 0x8e, 0x45, 0xa0, 0xf1, 0xe5, 0x0f, 0xdf, 0x69, 0xcb, 0xb6, 0xd5, 0x42, 0x06, 0x7d, 0x72, 0xa9, 0x5e, 0xae, 0xbf, 0xbf, 0x0f, 0xe0, 0xeb, 0x31, 0x31, 0xca, 0x8a, 0x81, 0x1e, 0xb9, 0xec, 0x6d, 0xcc, 0xb8, 0xa4, 0xac, 0xa3, 0x31, 0x05, 0xa9, 0xac, 0xc9, 0xd3, 0xe6, 0x2a, 0x18, 0xfe };
- Crypto::PK::RSA rsa(Crypto::PK::RSAPublicKey {
- "8126832723025844890518845777858816391166654950553329127845898924164623511718747856014227624997335860970996746552094406240834082304784428582653994490504519"_bigint,
- "65537"_bigint,
- });
- auto buf = TRY_OR_FAIL(rsa.encrypt(data));
- EXPECT(memcmp(result, buf.data(), buf.size()) == 0);
-}
-
// RSA PKCS #1 1.5
-TEST_CASE(test_RSA_PKCS_1_encrypt)
-{
- ByteBuffer data { "hellohellohellohellohellohellohellohellohello123-"_b };
-
- auto keypair = TRY_OR_FAIL(Crypto::PK::RSA::generate_key_pair(1024));
- Crypto::PK::RSA_PKCS1_EME rsa(keypair);
- auto enc = TRY_OR_FAIL(rsa.encrypt(data));
- auto dec = TRY_OR_FAIL(rsa.decrypt(enc));
-
- EXPECT(memcmp(dec.data(), "hellohellohellohellohellohellohellohellohello123-", 49) == 0);
-}
-
// RSA | ASN1 PKCS1 DER encoded Key import
TEST_CASE(test_RSA_ASN1_PKCS1_DER_parse)
{
@@ -133,38 +109,15 @@ TEST_CASE(test_RSA_keygen_enc)
auto keypair = MUST(Crypto::PK::RSA::parse_rsa_key(priv_key_info.raw_key, true, {}));
auto rsa_encryption_oid = Array<u32, 7> { 1, 2, 840, 113549, 1, 1, 1 };
auto wrapped_priv_der = MUST(Crypto::PK::wrap_in_private_key_info(priv_key_info.raw_key, rsa_encryption_oid, nullptr));
- auto rsa_from_pair = Crypto::PK::RSA(keypair.public_key, keypair.private_key);
- auto rsa_from_der = Crypto::PK::RSA(priv_key_info.rsa);
+ Crypto::PK::RSA_OAEP_EME rsa_from_pair { Crypto::Hash::HashKind::SHA1, keypair.public_key, keypair.private_key };
+ Crypto::PK::RSA_OAEP_EME rsa_from_der { Crypto::Hash::HashKind::SHA1, priv_key_info.rsa };
EXPECT_EQ(der.bytes(), wrapped_priv_der.bytes());
- ByteBuffer msg_buffer = {};
- msg_buffer.resize(rsa_from_pair.public_key().length());
-
- auto msg = msg_buffer.bytes();
- msg.overwrite(0, "WellHelloFriends", 16);
-
- auto enc = TRY_OR_FAIL(rsa_from_pair.encrypt(msg));
+ auto enc = TRY_OR_FAIL(rsa_from_pair.encrypt("WellHelloFriends"sv.bytes()));
auto dec = TRY_OR_FAIL(rsa_from_der.decrypt(enc));
- EXPECT_EQ(memcmp(dec.data(), "WellHelloFriends", 16), 0);
-}
-
-TEST_CASE(test_RSA_encrypt_decrypt)
-{
- auto keypair = TRY_OR_FAIL(Crypto::PK::RSA::generate_key_pair(1024));
- Crypto::PK::RSA rsa(keypair);
-
- ByteBuffer msg_buffer = {};
- msg_buffer.resize(rsa.public_key().length());
-
- auto msg = msg_buffer.bytes();
- msg.overwrite(0, "WellHelloFriendsWellHelloFriendsWellHelloFriendsWellHelloFriends", 64);
-
- auto enc = TRY_OR_FAIL(rsa.encrypt(msg));
- auto dec = TRY_OR_FAIL(rsa.decrypt(enc));
-
- EXPECT(memcmp(dec.data(), "WellHelloFriendsWellHelloFriendsWellHelloFriendsWellHelloFriends", 64) == 0);
+ EXPECT_EQ(StringView { dec }, "WellHelloFriends"sv);
}
TEST_CASE(test_RSA_OAEP_rejects_short_ciphertext)
@@ -188,22 +141,6 @@ TEST_CASE(test_RSA_OAEP_rejects_short_ciphertext)
FAIL("Could not generate an RSA ciphertext with a leading zero");
}
-TEST_CASE(test_RSA_sign_verify)
-{
- auto keypair = TRY_OR_FAIL(Crypto::PK::RSA::generate_key_pair(1024));
- Crypto::PK::RSA rsa(keypair);
-
- ByteBuffer msg_buffer = {};
- msg_buffer.resize(rsa.public_key().length());
-
- auto msg = msg_buffer.bytes();
- msg.overwrite(0, "WellHelloFriendsWellHelloFriendsWellHelloFriendsWellHelloFriends", 64);
-
- auto sig = TRY_OR_FAIL(rsa.sign(msg));
- auto ok = TRY_OR_FAIL(rsa.verify(msg, sig));
- EXPECT_EQ(ok, true);
-}
-
TEST_CASE(test_RSA_PSS_rejects_short_signature)
{
auto keypair = TRY_OR_FAIL(Crypto::PK::RSA::generate_key_pair(1024));