From 67957afb6ba01bcd85f1abd1a50ad2c1aa813c7c Mon Sep 17 00:00:00 2001 From: Aleksi Lindeman <0xdec05eba@gmail.com> Date: Wed, 14 Feb 2018 22:18:48 +0100 Subject: Sign messages/verify message signatures --- include/Database.hpp | 5 ++--- include/Signature.hpp | 28 +++++++++++++++++++++++++++- include/StagedObject.hpp | 6 +++--- 3 files changed, 32 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/Database.hpp b/include/Database.hpp index 0104a6e..e8b35bb 100644 --- a/include/Database.hpp +++ b/include/Database.hpp @@ -12,7 +12,6 @@ namespace odhtdb { - class Group; class LocalUser; class Database @@ -22,8 +21,8 @@ namespace odhtdb ~Database(); void seed(); - void create(const Key &key, Group *primaryAdminGroup); - void add(const Key &key, DataView data, LocalUser *creator); + void create(LocalUser *owner, const Key &key); + void add(LocalUser *owner, const Key &key, DataView data); void commit(); private: void commitStagedCreateObject(const StagedCreateObject &stagedObject); diff --git a/include/Signature.hpp b/include/Signature.hpp index ea776ea..aace383 100644 --- a/include/Signature.hpp +++ b/include/Signature.hpp @@ -1,11 +1,13 @@ #pragma once +#include "DataView.hpp" #include namespace odhtdb { const int PUBLIC_KEY_NUM_BYTES = 32; const int PRIVATE_KEY_NUM_BYTES = 64; + const int SIGNED_HASH_SIZE = 64; class InvalidSignatureKeySize : public std::runtime_error { @@ -25,6 +27,25 @@ namespace odhtdb DataSignException(const std::string &errMsg) : std::runtime_error(errMsg) {} }; + class UnsignException : public std::runtime_error + { + public: + UnsignException(const std::string &errMsg) : std::runtime_error(errMsg) {} + virtual ~UnsignException(){} + }; + + class UnsignInvalidSizeException : public UnsignException + { + public: + UnsignInvalidSizeException(const std::string &errMsg) : UnsignException(errMsg) {} + }; + + class UnsignWrongKeyException : public UnsignException + { + public: + UnsignWrongKeyException(const std::string &errMsg) : UnsignException(errMsg) {} + }; + namespace Signature { class PublicKey @@ -41,6 +62,11 @@ namespace odhtdb const char* getData() const { return data; } size_t getSize() const { return PUBLIC_KEY_NUM_BYTES; } + // Throws UnsignWrongKeyException if signed message was not signed using the matching private key of this public key. + // Throws UnsignInvalidSizeException if signed message is too small (< SIGNED_HASH_SIZE). + // Both exceptions are derived from UnsignException + std::string unsign(const DataView &signedMessage) const; + std::string toString() const; private: PublicKey(){} @@ -61,7 +87,7 @@ namespace odhtdb size_t getSize() const { return PRIVATE_KEY_NUM_BYTES; } // Throws DataSignException if signing data failed for whatever reason. This wont happen unless there is an issue with the private key - std::string sign(const std::string &dataToSign) const; + std::string sign(const DataView &dataToSign) const; std::string toString() const; private: PrivateKey(){} diff --git a/include/StagedObject.hpp b/include/StagedObject.hpp index dc2aaf4..fccf4f6 100644 --- a/include/StagedObject.hpp +++ b/include/StagedObject.hpp @@ -26,13 +26,13 @@ namespace odhtdb struct StagedAddObject { Key key; - DataView data; + std::unique_ptr data; u64 timestamp; // In microseconds Signature::PublicKey creatorPublicKey; StagedAddObject() : key(), data(), timestamp(0), creatorPublicKey(Signature::PublicKey::ZERO) {} - StagedAddObject(const Key &_key, const DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey) : - key(_key), data(_data), timestamp(_timestamp), creatorPublicKey(_creatorPublicKey) + StagedAddObject(const Key &_key, std::unique_ptr &&_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey) : + key(_key), data(std::move(_data)), timestamp(_timestamp), creatorPublicKey(_creatorPublicKey) { } -- cgit v1.2.3