aboutsummaryrefslogtreecommitdiff
path: root/include/Encryption.hpp
diff options
context:
space:
mode:
authorAleksi Lindeman <0xdec05eba@gmail.com>2018-03-05 22:45:56 +0100
committerAleksi Lindeman <0xdec05eba@gmail.com>2018-03-05 22:48:26 +0100
commit2ffb47d0043e57707474e5ae811f97c2e5e93f25 (patch)
treefd60b300cdf736de5adc68b395105dcfc6a43f09 /include/Encryption.hpp
parent66661e47dc826f50b690e080057f47a0ea27016c (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;
+ };
}