aboutsummaryrefslogtreecommitdiff
path: root/include/Encryption.hpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-03-05 22:45:56 +0100
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:46 +0200
commiteda9a7bbefc5587bf1ff895a9214f450e64575fa (patch)
tree0f968fb7373a29cf116b4b6d473a966e28e62825 /include/Encryption.hpp
parent33e823ddddddd4a13b1a05b90ae5b419b89bcb1d (diff)
Implement 'create' operation, add seeding
Seeding is currently only done on the key you specify, in the future the user should request data that it can seed.
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;
+ };
}