Add Protocol::SignedMessage struct (not done)

This commit is contained in:
2026-08-04 21:27:06 +03:00
parent 09ae813c76
commit 11d207d58f
6 changed files with 103 additions and 3 deletions
+8 -1
View File
@@ -4,12 +4,12 @@ project(Sabalty)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include(FetchContent)
option(BUILD_TESTING "Build tests" OFF) option(BUILD_TESTING "Build tests" OFF)
if(BUILD_TESTING) if(BUILD_TESTING)
enable_testing() enable_testing()
include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
Catch2 Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_REPOSITORY https://github.com/catchorg/Catch2.git
@@ -21,5 +21,12 @@ if(BUILD_TESTING)
include(Catch) include(Catch)
endif() endif()
FetchContent_Declare(Sodium
GIT_REPOSITORY https://github.com/robinlinden/libsodium-cmake.git
GIT_TAG cfebfd3da486d5a86c644c8b47067e5411c7599c # HEAD, last updated at 2026-02-28
)
set(SODIUM_DISABLE_TESTS ON)
FetchContent_MakeAvailable(Sodium)
add_subdirectory(Server) add_subdirectory(Server)
add_subdirectory(Protocol) add_subdirectory(Protocol)
+5
View File
@@ -8,3 +8,8 @@ target_sources(Protocol
BASE_DIRS include BASE_DIRS include
FILES include/protocol/protocol.hxx FILES include/protocol/protocol.hxx
) )
target_link_libraries(Protocol
PRIVATE
sodium
)
@@ -0,0 +1,23 @@
#pragma once
#include "sodium/crypto_hash_sha256.h"
#include "sodium/crypto_sign.h"
#include <cstdint>
#include <string>
namespace Protocol {
struct SignedMessage {
unsigned char sender_key_fingerprint[crypto_hash_sha256_BYTES];
std::string plaintext;
int64_t timestamp;
unsigned char sender_key_signature[crypto_sign_BYTES]; // signs fields 1-3
// Fills the fields and signs the plaintext
SignedMessage(unsigned char sender_pub_key[crypto_sign_PUBLICKEYBYTES],
unsigned char sender_priv_key[crypto_sign_SECRETKEYBYTES],
std::string plaintext);
// TODO: bool isMessageTrusted(std::string pub_key);
};
} // namespace Protocol
+38
View File
@@ -0,0 +1,38 @@
#include "sodium/crypto_sign_ed25519.h"
#include <chrono>
#include <protocol/protocol.hxx>
#include <sodium.h>
namespace Protocol {
SignedMessage::SignedMessage(
unsigned char sender_pub_key[crypto_sign_PUBLICKEYBYTES],
unsigned char sender_priv_key[crypto_sign_SECRETKEYBYTES],
std::string plaintext)
: plaintext(plaintext) {
if (sodium_init() < 0) {
throw std::runtime_error("Failed to init sodium!");
}
timestamp = std::chrono::system_clock::now().time_since_epoch().count();
crypto_hash_sha256(sender_key_fingerprint, sender_pub_key,
crypto_sign_PUBLICKEYBYTES);
std::vector<uint8_t> toBeSigned;
toBeSigned.reserve(sizeof(sender_key_fingerprint) + plaintext.size() +
sizeof(timestamp));
toBeSigned.insert(toBeSigned.cend(), sender_key_fingerprint,
sender_key_fingerprint +
sizeof(sender_key_fingerprint));
toBeSigned.insert(toBeSigned.cend(), plaintext.data(),
plaintext.data() + plaintext.length());
auto timestamp_bytes = reinterpret_cast<const uint8_t *>(&timestamp);
toBeSigned.insert(toBeSigned.cend(), timestamp_bytes,
timestamp_bytes + sizeof(timestamp));
crypto_sign_detached(sender_key_signature, nullptr, toBeSigned.data(),
toBeSigned.size(), sender_priv_key);
}
} // namespace Protocol
+1
View File
@@ -6,6 +6,7 @@ target_link_libraries(Protocol_tests
PRIVATE PRIVATE
Protocol Protocol
Catch2::Catch2WithMain Catch2::Catch2WithMain
sodium
) )
catch_discover_tests(Protocol_tests) catch_discover_tests(Protocol_tests)
+28 -2
View File
@@ -1,6 +1,32 @@
#include "sodium/crypto_sign.h"
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <protocol/protocol.hxx> #include <protocol/protocol.hxx>
#include <sodium.h>
TEST_CASE("something", "[stun]") { TEST_CASE("SignedMessage", "[protocol]") {
SECTION("true") { REQUIRE(true); } SECTION("sodium init") REQUIRE(sodium_init() >= 0);
unsigned char pub_key[crypto_sign_PUBLICKEYBYTES];
unsigned char priv_key[crypto_sign_SECRETKEYBYTES];
crypto_sign_keypair(pub_key, priv_key);
auto msg = Protocol::SignedMessage(pub_key, priv_key, "Hello, world!");
std::vector<uint8_t> toBeSigned;
toBeSigned.reserve(sizeof(msg.sender_key_fingerprint) +
msg.plaintext.size() + sizeof(msg.timestamp));
toBeSigned.insert(toBeSigned.cend(), msg.sender_key_fingerprint,
msg.sender_key_fingerprint +
sizeof(msg.sender_key_fingerprint));
toBeSigned.insert(toBeSigned.cend(), msg.plaintext.data(),
msg.plaintext.data() + msg.plaintext.length());
auto timestamp_bytes = reinterpret_cast<const uint8_t *>(&msg.timestamp);
toBeSigned.insert(toBeSigned.cend(), timestamp_bytes,
timestamp_bytes + sizeof(msg.timestamp));
SECTION("signed message") {
REQUIRE(crypto_sign_verify_detached(
msg.sender_key_signature, toBeSigned.data(),
toBeSigned.size(), pub_key) == 0);
}
} }