diff options
Diffstat (limited to 'include/odhtdb/Encryption.hpp')
-rw-r--r-- | include/odhtdb/Encryption.hpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/include/odhtdb/Encryption.hpp b/include/odhtdb/Encryption.hpp new file mode 100644 index 0000000..4697b35 --- /dev/null +++ b/include/odhtdb/Encryption.hpp @@ -0,0 +1,64 @@ +#pragma once + +/* + * Encrypts/decrypts data using xchacha20-poly1305 ietf + */ + +#include "DataView.hpp" +#include "utils.hpp" +#include <string> +#include <stdexcept> + +namespace odhtdb +{ + const int NONCE_BYTE_SIZE = 24; + const int KEY_BYTE_SIZE = 32; + + class EncryptionException : public std::runtime_error + { + public: + EncryptionException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class DecryptionException : public std::runtime_error + { + public: + DecryptionException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class Encryption + { + DISABLE_COPY(Encryption) + public: + // Throws EncryptionException on failure (or std::bad_alloc on failed memory allocation) + Encryption(const DataView &data, const DataView &additionalData = DataView(), const DataView &key = DataView()); + ~Encryption(); + + DataView getKey() const; + DataView getNonce() const; + DataView getCipherText() const; + private: + unsigned char key[KEY_BYTE_SIZE]; + unsigned char nonce[NONCE_BYTE_SIZE]; + unsigned char *cipherText; + unsigned long long cipherTextLength; + }; + + class Decryption + { + DISABLE_COPY(Decryption) + public: + Decryption() : decryptedText(nullptr), decryptedTextLength(0) {} + + // Throws DecryptionException on failure + Decryption(const DataView &data, const DataView &nonce, const DataView &key); + Decryption(Decryption &&other); + Decryption& operator=(Decryption &&other); + ~Decryption(); + + DataView getDecryptedText() const; + private: + unsigned char *decryptedText; + unsigned long long decryptedTextLength; + }; +} |