Add Protocol::SignedMessage::serialize
This commit is contained in:
@@ -4,20 +4,21 @@
|
|||||||
#include "sodium/crypto_sign.h"
|
#include "sodium/crypto_sign.h"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
namespace Protocol {
|
namespace Protocol {
|
||||||
|
|
||||||
struct SignedMessage {
|
struct SignedMessage {
|
||||||
unsigned char sender_key_fingerprint[crypto_hash_sha256_BYTES];
|
unsigned char sender_key_fingerprint[crypto_hash_sha256_BYTES];
|
||||||
std::string plaintext;
|
std::string plaintext;
|
||||||
int64_t timestamp;
|
int64_t timestamp;
|
||||||
unsigned char sender_key_signature[crypto_sign_BYTES]; // signs fields 1-3
|
unsigned char signature[crypto_sign_BYTES]; // signs fields 1-3
|
||||||
|
|
||||||
// Fills the fields and signs the plaintext
|
// Fills the fields and signs the plaintext
|
||||||
SignedMessage(unsigned char sender_pub_key[crypto_sign_PUBLICKEYBYTES],
|
SignedMessage(unsigned char sender_pub_key[crypto_sign_PUBLICKEYBYTES],
|
||||||
unsigned char sender_priv_key[crypto_sign_SECRETKEYBYTES],
|
unsigned char sender_priv_key[crypto_sign_SECRETKEYBYTES],
|
||||||
std::string plaintext);
|
std::string plaintext);
|
||||||
|
|
||||||
// TODO: bool isMessageTrusted(std::string pub_key);
|
std::vector<uint8_t> serialize();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Protocol
|
} // namespace Protocol
|
||||||
|
|||||||
+14
-10
@@ -18,21 +18,25 @@ SignedMessage::SignedMessage(
|
|||||||
crypto_hash_sha256(sender_key_fingerprint, sender_pub_key,
|
crypto_hash_sha256(sender_key_fingerprint, sender_pub_key,
|
||||||
crypto_sign_PUBLICKEYBYTES);
|
crypto_sign_PUBLICKEYBYTES);
|
||||||
|
|
||||||
std::vector<uint8_t> toBeSigned;
|
auto to_be_signed = serialize();
|
||||||
toBeSigned.reserve(sizeof(sender_key_fingerprint) + plaintext.size() +
|
|
||||||
sizeof(timestamp));
|
|
||||||
|
|
||||||
toBeSigned.insert(toBeSigned.cend(), sender_key_fingerprint,
|
crypto_sign_detached(signature, nullptr, to_be_signed.data(),
|
||||||
sender_key_fingerprint +
|
to_be_signed.size(), sender_priv_key);
|
||||||
sizeof(sender_key_fingerprint));
|
}
|
||||||
toBeSigned.insert(toBeSigned.cend(), plaintext.data(),
|
|
||||||
|
std::vector<uint8_t> SignedMessage::serialize() {
|
||||||
|
std::vector<uint8_t> bytes;
|
||||||
|
bytes.reserve(sizeof(sender_key_fingerprint) + plaintext.size() +
|
||||||
|
sizeof(timestamp));
|
||||||
|
bytes.insert(bytes.cend(), sender_key_fingerprint,
|
||||||
|
sender_key_fingerprint + sizeof(sender_key_fingerprint));
|
||||||
|
bytes.insert(bytes.cend(), plaintext.data(),
|
||||||
plaintext.data() + plaintext.length());
|
plaintext.data() + plaintext.length());
|
||||||
auto timestamp_bytes = reinterpret_cast<const uint8_t *>(×tamp);
|
auto timestamp_bytes = reinterpret_cast<const uint8_t *>(×tamp);
|
||||||
toBeSigned.insert(toBeSigned.cend(), timestamp_bytes,
|
bytes.insert(bytes.cend(), timestamp_bytes,
|
||||||
timestamp_bytes + sizeof(timestamp));
|
timestamp_bytes + sizeof(timestamp));
|
||||||
|
|
||||||
crypto_sign_detached(sender_key_signature, nullptr, toBeSigned.data(),
|
return bytes;
|
||||||
toBeSigned.size(), sender_priv_key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Protocol
|
} // namespace Protocol
|
||||||
|
|||||||
@@ -11,22 +11,11 @@ TEST_CASE("SignedMessage", "[protocol]") {
|
|||||||
crypto_sign_keypair(pub_key, priv_key);
|
crypto_sign_keypair(pub_key, priv_key);
|
||||||
|
|
||||||
auto msg = Protocol::SignedMessage(pub_key, priv_key, "Hello, world!");
|
auto msg = Protocol::SignedMessage(pub_key, priv_key, "Hello, world!");
|
||||||
std::vector<uint8_t> toBeSigned;
|
auto to_be_signed = msg.serialize();
|
||||||
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") {
|
SECTION("signed message") {
|
||||||
REQUIRE(crypto_sign_verify_detached(
|
REQUIRE(
|
||||||
msg.sender_key_signature, toBeSigned.data(),
|
crypto_sign_verify_detached(msg.signature, to_be_signed.data(),
|
||||||
toBeSigned.size(), pub_key) == 0);
|
to_be_signed.size(), pub_key) == 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user