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
+5
View File
@@ -8,3 +8,8 @@ target_sources(Protocol
BASE_DIRS include
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
Protocol
Catch2::Catch2WithMain
sodium
)
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 <protocol/protocol.hxx>
#include <sodium.h>
TEST_CASE("something", "[stun]") {
SECTION("true") { REQUIRE(true); }
TEST_CASE("SignedMessage", "[protocol]") {
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);
}
}