From 2ffb47d0043e57707474e5ae811f97c2e5e93f25 Mon Sep 17 00:00:00 2001 From: Aleksi Lindeman <0xdec05eba@gmail.com> Date: Mon, 5 Mar 2018 22:45:56 +0100 Subject: 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. --- include/Encryption.hpp | 56 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 12 deletions(-) (limited to 'include/Encryption.hpp') 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 +#include 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; + }; } -- cgit v1.2.3