Add Protocol::SignedMessage struct (not done)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 *>(×tamp);
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user