From dfe1edf7067385ae05b75c39b9dcc39893d51278 Mon Sep 17 00:00:00 2001 From: ImagineHaxing Date: Wed, 5 Aug 2026 10:54:05 +0300 Subject: [PATCH] Add Protocol::SignedMessage::valid + tests --- Protocol/src/include/protocol/protocol.hxx | 2 +- Protocol/src/protocol.cxx | 20 +++++++++++++++++- Protocol/tests/test_protocol.cxx | 24 ++++++++-------------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/Protocol/src/include/protocol/protocol.hxx b/Protocol/src/include/protocol/protocol.hxx index 3df2025..1494ae3 100644 --- a/Protocol/src/include/protocol/protocol.hxx +++ b/Protocol/src/include/protocol/protocol.hxx @@ -19,7 +19,7 @@ struct SignedMessage { std::string plaintext); std::vector serialize(); - // bool valid(); + bool valid(unsigned char pub_key[crypto_sign_PUBLICKEYBYTES]); }; } // namespace Protocol diff --git a/Protocol/src/protocol.cxx b/Protocol/src/protocol.cxx index bbaa748..70572c2 100644 --- a/Protocol/src/protocol.cxx +++ b/Protocol/src/protocol.cxx @@ -1,5 +1,5 @@ -#include "sodium/crypto_sign_ed25519.h" #include +#include #include #include @@ -39,4 +39,22 @@ std::vector SignedMessage::serialize() { return bytes; } +bool SignedMessage::valid(unsigned char pub_key[crypto_sign_PUBLICKEYBYTES]) { + unsigned char pub_key_fingerprint[crypto_hash_sha256_BYTES]; + crypto_hash_sha256(pub_key_fingerprint, pub_key, + crypto_sign_PUBLICKEYBYTES); + + if (std::memcmp(pub_key_fingerprint, sender_key_fingerprint, + crypto_sign_PUBLICKEYBYTES) != 0) { + return false; + } + + auto serialized_data = serialize(); + if (crypto_sign_verify_detached(signature, serialized_data.data(), + serialized_data.size(), pub_key) != 0) { + return false; + } + return true; +} + } // namespace Protocol diff --git a/Protocol/tests/test_protocol.cxx b/Protocol/tests/test_protocol.cxx index cab32d1..c5454fd 100644 --- a/Protocol/tests/test_protocol.cxx +++ b/Protocol/tests/test_protocol.cxx @@ -11,32 +11,26 @@ TEST_CASE("SignedMessage", "[protocol]") { crypto_sign_keypair(pub_key, priv_key); auto msg = Protocol::SignedMessage(pub_key, priv_key, "Hello, world!"); - auto to_be_signed = msg.serialize(); - SECTION("valid signed message") { - REQUIRE( - crypto_sign_verify_detached(msg.signature, to_be_signed.data(), - to_be_signed.size(), pub_key) == 0); - } + SECTION("valid signed message") { REQUIRE(msg.valid(pub_key)); } SECTION("tampered invalid signed message") { - // todo: use to msg.valid() SECTION("plaintext") { msg.plaintext = "Hello, world111111"; - to_be_signed = msg.serialize(); - REQUIRE_FALSE(crypto_sign_verify_detached( - msg.signature, to_be_signed.data(), - to_be_signed.size(), pub_key) == 0); + REQUIRE_FALSE(msg.valid(pub_key)); } SECTION("timestamp") { msg.timestamp = std::chrono::system_clock::now() .time_since_epoch() .count(); - to_be_signed = msg.serialize(); - REQUIRE_FALSE(crypto_sign_verify_detached( - msg.signature, to_be_signed.data(), - to_be_signed.size(), pub_key) == 0); + REQUIRE_FALSE(msg.valid(pub_key)); + } + + SECTION("fingerprint") { + std::memset(msg.sender_key_fingerprint, 'H', + crypto_hash_sha256_BYTES); + REQUIRE_FALSE(msg.valid(pub_key)); } } }