aboutsummaryrefslogtreecommitdiff
path: root/include/Encryption.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/Encryption.hpp')
-rw-r--r--include/Encryption.hpp56
1 files changed, 44 insertions, 12 deletions
diff --git a/include/Encryption.hpp b/include/Encryption.hpp
index b70687d..b2afe49 100644
--- a/include/Encryption.hpp
+++ b/include/Encryption.hpp
@@ -1,29 +1,61 @@
#pragma once
/*
- * Encrypts/decrypts data using xchacha20
+ * 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;
- struct EncryptedData
+ class EncryptionException : public std::runtime_error
{
- char nonce[NONCE_BYTE_SIZE];
- std::string data;
+ public:
+ EncryptionException(const std::string &errMsg) : std::runtime_error(errMsg) {}
};
- using EncryptionKey = char[32];
-
- // Stores randomly generated encryption key in @output
- void generateEncryptionKey(EncryptionKey *output);
+ class DecryptionException : public std::runtime_error
+ {
+ public:
+ DecryptionException(const std::string &errMsg) : std::runtime_error(errMsg) {}
+ };
- // Returns 0 on success, storing encrypted data in @output
- int encrypt(EncryptedData *output, const EncryptionKey *key, const void *data, size_t dataSize);
+ class Encryption
+ {
+ DISABLE_COPY(Encryption)
+ public:
+ // Throws EncryptionException on failure (or std::bad_alloc on failed memory allocation)
+ Encryption(const DataView &data) : Encryption(data, DataView()) {}
+ Encryption(const DataView &data, const DataView &additionalData);
+ ~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;
+ };
- // Returns 0 on success, storing decrypted data in @output
- int decrypt(std::string *output, const EncryptionKey *key, const EncryptedData *encryptedData);
+ class Decryption
+ {
+ DISABLE_COPY(Decryption)
+ public:
+ // Throws DecryptionException on failure
+ Decryption(const DataView &data, const DataView &nonce, const DataView &key);
+ ~Decryption();
+
+ DataView getDecryptedText() const;
+ private:
+ unsigned char *decryptedText;
+ unsigned long long decryptedTextLength;
+ };
}