Add Protocol::SignedMessage::valid + tests
This commit is contained in:
@@ -19,7 +19,7 @@ struct SignedMessage {
|
||||
std::string plaintext);
|
||||
|
||||
std::vector<uint8_t> serialize();
|
||||
// bool valid();
|
||||
bool valid(unsigned char pub_key[crypto_sign_PUBLICKEYBYTES]);
|
||||
};
|
||||
|
||||
} // namespace Protocol
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "sodium/crypto_sign_ed25519.h"
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <protocol/protocol.hxx>
|
||||
#include <sodium.h>
|
||||
|
||||
@@ -39,4 +39,22 @@ std::vector<uint8_t> 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
|
||||
|
||||
Reference in New Issue
Block a user