aboutsummaryrefslogtreecommitdiff
path: root/src/Encryption.cpp
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 /src/Encryption.cpp
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 'src/Encryption.cpp')
-rw-r--r--src/Encryption.cpp60
1 files changed, 45 insertions, 15 deletions
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 <sodium/crypto_stream_xchacha20.h>
+#include <sodium/crypto_aead_xchacha20poly1305.h>
#include <sodium/randombytes.h>
#include <string>
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);
}
}