Skip to content

Truly independent web browser

LibCrypto: Sign and verify ML-DSA with free functions

Shannon Booth096fba684760
diff --git a/Libraries/LibCrypto/PK/EC.h b/Libraries/LibCrypto/PK/EC.h
index 3885d5e93ae..929148939f9 100644
--- a/Libraries/LibCrypto/PK/EC.h
+++ b/Libraries/LibCrypto/PK/EC.h
@@ -104,9 +104,9 @@ struct ECKeyPair {
PrivKey private_key;
};
-class EC : public PKSystem<ECPrivateKey, ECPublicKey> {
+class EC {
public:
- using KeyPairType = ECKeyPair<PublicKeyType, PrivateKeyType>;
+ using KeyPairType = ECKeyPair<ECPublicKey, ECPrivateKey>;
static ErrorOr<KeyPairType> parse_ec_key(ReadonlyBytes der, bool is_private, Vector<StringView> current_scope);
};
diff --git a/Libraries/LibCrypto/PK/MLDSA.cpp b/Libraries/LibCrypto/PK/MLDSA.cpp
index 6f874810cf7..f94c9f48ac1 100644
--- a/Libraries/LibCrypto/PK/MLDSA.cpp
+++ b/Libraries/LibCrypto/PK/MLDSA.cpp
@@ -134,23 +134,22 @@ static ErrorOr<OpenSSL_PKEY> private_key_to_openssl_pkey(MLDSASize size, MLDSAPr
return key;
}
-ErrorOr<ByteBuffer> MLDSA::sign(ReadonlyBytes message)
+ErrorOr<ByteBuffer> mldsa_sign(MLDSASize size, MLDSAPrivateKey const& private_key, ReadonlyBytes context, ReadonlyBytes message)
{
OSSL_PARAM params[2] = {
OSSL_PARAM_END,
OSSL_PARAM_END
};
- if (!m_context.is_empty()) {
- params[0] = OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, m_context.data(), m_context.size());
+ if (!context.is_empty()) {
+ params[0] = OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, const_cast<u8*>(context.data()), context.size());
}
- auto key = TRY(private_key_to_openssl_pkey(m_size, m_private_key));
- auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_name(nullptr, mldsa_size_to_openssl_name(m_size), nullptr)));
+ auto key = TRY(private_key_to_openssl_pkey(size, private_key));
EVP_PKEY_CTX* sign_ctx = OPENSSL_TRY_PTR(EVP_PKEY_CTX_new_from_pkey(nullptr, key.ptr(), nullptr));
ScopeGuard const free_sign_ctx = [&] { EVP_PKEY_CTX_free(sign_ctx); };
- auto* sign_algorithm = OPENSSL_TRY_PTR(EVP_SIGNATURE_fetch(nullptr, mldsa_size_to_openssl_name(m_size), nullptr));
+ auto* sign_algorithm = OPENSSL_TRY_PTR(EVP_SIGNATURE_fetch(nullptr, mldsa_size_to_openssl_name(size), nullptr));
ScopeGuard const free_sign_algorithm = [&] { EVP_SIGNATURE_free(sign_algorithm); };
size_t sign_size;
@@ -184,20 +183,20 @@ static ErrorOr<OpenSSL_PKEY> public_key_to_openssl_pkey(MLDSASize size, MLDSAPub
return key;
}
-ErrorOr<bool> MLDSA::verify(ReadonlyBytes message, ReadonlyBytes signature)
+ErrorOr<bool> mldsa_verify(MLDSASize size, MLDSAPublicKey const& public_key, ReadonlyBytes context, ReadonlyBytes message, ReadonlyBytes signature)
{
OSSL_PARAM params[2] = {
OSSL_PARAM_END,
OSSL_PARAM_END
};
- if (!m_context.is_empty()) {
- params[0] = OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, m_context.data(), m_context.size());
+ if (!context.is_empty()) {
+ params[0] = OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, const_cast<u8*>(context.data()), context.size());
}
- auto key = TRY(public_key_to_openssl_pkey(m_size, m_public_key));
+ auto key = TRY(public_key_to_openssl_pkey(size, public_key));
auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new(key.ptr(), nullptr)));
- auto* sign_algorithm = OPENSSL_TRY_PTR(EVP_SIGNATURE_fetch(nullptr, mldsa_size_to_openssl_name(m_size), nullptr));
+ auto* sign_algorithm = OPENSSL_TRY_PTR(EVP_SIGNATURE_fetch(nullptr, mldsa_size_to_openssl_name(size), nullptr));
ScopeGuard const free_sign_algorithm = [&] { EVP_SIGNATURE_free(sign_algorithm); };
OPENSSL_TRY(EVP_PKEY_verify_message_init(ctx.ptr(), sign_algorithm, params));
diff --git a/Libraries/LibCrypto/PK/MLDSA.h b/Libraries/LibCrypto/PK/MLDSA.h
index b2abae1b7ae..e0d6bda36cb 100644
--- a/Libraries/LibCrypto/PK/MLDSA.h
+++ b/Libraries/LibCrypto/PK/MLDSA.h
@@ -63,37 +63,15 @@ struct MLDSAKeyPair {
PrivKey private_key;
};
-class MLDSA : public PKSystem<MLDSAPrivateKey, MLDSAPublicKey> {
+class MLDSA {
public:
- using KeyPairType = MLDSAKeyPair<PublicKeyType, PrivateKeyType>;
+ using KeyPairType = MLDSAKeyPair<MLDSAPublicKey, MLDSAPrivateKey>;
static ErrorOr<KeyPairType> parse_mldsa_key(MLDSASize, ReadonlyBytes der);
static ErrorOr<KeyPairType> generate_key_pair(MLDSASize, ByteBuffer seed = {});
-
- MLDSA(MLDSASize size, PrivateKeyType const& priv_key, ByteBuffer context)
- : m_size(size)
- , m_context(move(context))
- {
- m_private_key = priv_key;
- m_public_key = { priv_key.public_key() };
- }
-
- MLDSA(MLDSASize size, PublicKeyType const& pub_key, ByteBuffer context)
- : m_size(size)
- , m_context(move(context))
- {
- m_public_key = pub_key;
- }
-
- ErrorOr<ByteBuffer> sign(ReadonlyBytes message) override;
-
- 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;
-
-private:
- MLDSASize m_size;
- ByteBuffer m_context;
};
+ErrorOr<ByteBuffer> mldsa_sign(MLDSASize, MLDSAPrivateKey const&, ReadonlyBytes context, ReadonlyBytes message);
+ErrorOr<bool> mldsa_verify(MLDSASize, MLDSAPublicKey const&, ReadonlyBytes context, ReadonlyBytes message, ReadonlyBytes signature);
+
}
diff --git a/Libraries/LibCrypto/PK/PK.h b/Libraries/LibCrypto/PK/PK.h
index 7c073fe2e72..a7d1c98071c 100644
--- a/Libraries/LibCrypto/PK/PK.h
+++ b/Libraries/LibCrypto/PK/PK.h
@@ -217,31 +217,4 @@ requires requires(ExportableKey k) {
return encoder.finish();
}
-template<typename PrivKeyT, typename PubKeyT>
-class PKSystem {
-public:
- using PublicKeyType = PubKeyT;
- using PrivateKeyType = PrivKeyT;
-
- PKSystem(PublicKeyType const& pubkey, PrivateKeyType const& privkey)
- : m_public_key(pubkey)
- , m_private_key(privkey)
- {
- }
-
- PKSystem() = default;
-
- virtual ErrorOr<ByteBuffer> encrypt(ReadonlyBytes in) = 0;
- virtual ErrorOr<ByteBuffer> decrypt(ReadonlyBytes in) = 0;
-
- virtual ErrorOr<bool> verify(ReadonlyBytes message, ReadonlyBytes signature) = 0;
- virtual ErrorOr<ByteBuffer> sign(ReadonlyBytes message) = 0;
-
-protected:
- virtual ~PKSystem() = default;
-
- PublicKeyType m_public_key;
- PrivateKeyType m_private_key;
-};
-
}
diff --git a/Libraries/LibCrypto/RustBindings.cpp b/Libraries/LibCrypto/RustBindings.cpp
index 937517dc04f..0f23f713e75 100644
--- a/Libraries/LibCrypto/RustBindings.cpp
+++ b/Libraries/LibCrypto/RustBindings.cpp
@@ -800,15 +800,13 @@ static_assert(to_underlying(::Crypto::PK::MLDSA87) == 2);
bool crypto_mldsa_sign(::Crypto::PK::MLDSASize size, KeyHandle const* key, ReadonlyBytes message, ReadonlyBytes context, void* state, ReceiveEncodedBytes receive)
{
- ::Crypto::PK::MLDSA lattice(size, key->get<::Crypto::PK::MLDSAPrivateKey>(), MUST(ByteBuffer::copy(context)));
- auto result = lattice.sign(message);
+ auto result = ::Crypto::PK::mldsa_sign(size, key->get<::Crypto::PK::MLDSAPrivateKey>(), context, message);
return receive_byte_result(result, state, receive);
}
bool crypto_mldsa_verify(::Crypto::PK::MLDSASize size, KeyHandle const* key, ReadonlyBytes message, ReadonlyBytes signature, ReadonlyBytes context, bool* verified)
{
- ::Crypto::PK::MLDSA lattice(size, key->get<::Crypto::PK::MLDSAPublicKey>(), MUST(ByteBuffer::copy(context)));
- auto result = lattice.verify(message, signature);
+ auto result = ::Crypto::PK::mldsa_verify(size, key->get<::Crypto::PK::MLDSAPublicKey>(), context, message, signature);
if (result.is_error())
return false;
*verified = result.value();
diff --git a/Tests/LibCrypto/TestMLDSA.cpp b/Tests/LibCrypto/TestMLDSA.cpp
index eee4cf70315..c471a43a12c 100644
--- a/Tests/LibCrypto/TestMLDSA.cpp
+++ b/Tests/LibCrypto/TestMLDSA.cpp
@@ -16,17 +16,17 @@ TEST_CASE(TEST_SIGN_AND_VERIFY)
auto [_, private_key] = TRY_OR_FAIL(Crypto::PK::MLDSA::generate_key_pair(Crypto::PK::MLDSA65));
- Crypto::PK::MLDSA lattice { Crypto::PK::MLDSA65, private_key, {} };
- Crypto::PK::MLDSA lattice_with_context { Crypto::PK::MLDSA65, private_key, TRY_OR_FAIL(ByteBuffer::copy(message_bytes)) };
+ Crypto::PK::MLDSAPublicKey public_key { private_key.public_key() };
+ auto size = Crypto::PK::MLDSA65;
- auto const signed_message = TRY_OR_FAIL(lattice.sign(message));
+ auto const signed_message = TRY_OR_FAIL(Crypto::PK::mldsa_sign(size, private_key, {}, message));
EXPECT(signed_message.size() == 3309);
- EXPECT(TRY_OR_FAIL(lattice.verify(message, signed_message)));
+ EXPECT(TRY_OR_FAIL(Crypto::PK::mldsa_verify(size, public_key, {}, message, signed_message)));
- auto const signed_message_with_context = TRY_OR_FAIL(lattice_with_context.sign(message));
+ auto const signed_message_with_context = TRY_OR_FAIL(Crypto::PK::mldsa_sign(size, private_key, message, message));
EXPECT(signed_message_with_context.size() == 3309);
EXPECT_NE(signed_message, signed_message_with_context);
- EXPECT(TRY_OR_FAIL(lattice_with_context.verify(message, signed_message_with_context)));
+ EXPECT(TRY_OR_FAIL(Crypto::PK::mldsa_verify(size, public_key, message, message, signed_message_with_context)));
}
static Crypto::PK::MLDSA::KeyPairType generate_key_pair_from_seed(u8 first_seed_byte)