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. --- src/Encryption.cpp | 60 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 15 deletions(-) (limited to 'src/Encryption.cpp') diff --git a/src/Encryption.cpp b/src/Encryption.cpp index 8e87a8d..c4e6a2c 100644 --- a/src/Encryption.cpp +++ b/src/Encryption.cpp @@ -1,30 +1,60 @@ #include "../include/Encryption.hpp" -#include +#include #include #include namespace odhtdb { - void generateEncryptionKey(EncryptionKey *output) + Encryption::Encryption(const DataView &data, const DataView &additionalData) { - if(!output) return; - crypto_stream_xchacha20_keygen((unsigned char*)output); + cipherText = new unsigned char[crypto_aead_xchacha20poly1305_ietf_ABYTES + data.size]; + crypto_aead_xchacha20poly1305_ietf_keygen(key); + randombytes_buf(nonce, NONCE_BYTE_SIZE); + if(crypto_aead_xchacha20poly1305_ietf_encrypt(cipherText, &cipherTextLength, (const unsigned char*)data.data, data.size, (const unsigned char*)additionalData.data, additionalData.size, nullptr, nonce, key) != 0) + throw EncryptionException("Failed to encrypt data"); } - int encrypt(EncryptedData *output, const EncryptionKey *key, const void *data, size_t dataSize) + Encryption::~Encryption() { - if(!output || !key) return -1; - if(dataSize == 0) return 0; - output->data.resize(dataSize); - randombytes_buf(output->nonce, NONCE_BYTE_SIZE); - return crypto_stream_xchacha20_xor((unsigned char*)&output->data[0], (const unsigned char*)data, dataSize, (const unsigned char*)output->nonce, (const unsigned char*)key); + delete[](cipherText); } - int decrypt(std::string *output, const EncryptionKey *key, const EncryptedData *encryptedData) + DataView Encryption::getKey() const { - if(!encryptedData || !key || !output) return -1; - if(encryptedData->data.empty()) return 0; - output->resize(encryptedData->data.size()); - return crypto_stream_xchacha20_xor((unsigned char*)&(*output)[0], (const unsigned char*)&encryptedData->data[0], encryptedData->data.size(), (const unsigned char*)encryptedData->nonce, (const unsigned char*)key); + return DataView((void*)key, KEY_BYTE_SIZE); + } + + DataView Encryption::getNonce() const + { + return DataView((void*)nonce, NONCE_BYTE_SIZE); + } + + DataView Encryption::getCipherText() const + { + return DataView((void*)cipherText, cipherTextLength); + } + + Decryption::Decryption(const DataView &data, const DataView &nonce, const DataView &key) + { + decryptedText = new unsigned char[data.size]; + + if(nonce.size < NONCE_BYTE_SIZE) + throw DecryptionException("Nonce is not big enough"); + + if(key.size < KEY_BYTE_SIZE) + throw DecryptionException("Key is not big enough"); + + if(crypto_aead_xchacha20poly1305_ietf_decrypt(decryptedText, &decryptedTextLength, nullptr, (const unsigned char*)data.data, data.size, nullptr, 0, (const unsigned char*)nonce.data, (const unsigned char*)key.data) != 0) + throw DecryptionException("Failed to decrypt data"); + } + + Decryption::~Decryption() + { + delete[](decryptedText); + } + + DataView Decryption::getDecryptedText() const + { + return DataView((void*)decryptedText, decryptedTextLength); } } -- cgit v1.2.3