aboutsummaryrefslogtreecommitdiff
path: root/src/Encryption.cpp
blob: ff37270ed95e6cb4feaa9e7c4cec29126fc8d9e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "../include/odhtdb/Encryption.hpp"
#include <sodium/crypto_aead_xchacha20poly1305.h>
#include <sodium/randombytes.h>
#include <cstring>

namespace odhtdb
{
    static_assert(ENCRYPTION_CHECKSUM_BYTE_SIZE == crypto_aead_xchacha20poly1305_ietf_ABYTES, "Encryption checksum key size has changed for some reason, oops...");
    
    Encryption::Encryption(const DataView &data, const DataView &additionalData, const DataView &_key)
    {
        cipherTextLength = crypto_aead_xchacha20poly1305_ietf_ABYTES + data.size;
        cipherText = new unsigned char[cipherTextLength];   
        
        if(_key.data)
        {
            if(_key.size != ENCRYPTION_KEY_BYTE_SIZE)
                throw EncryptionException("Encryption key is wrong size");
            memcpy(key, _key.data, _key.size);
        }
        else
            generateKey(key);
        
        randombytes_buf(nonce, ENCRYPTION_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");
    }
    
    Encryption::~Encryption()
    {
        delete[](cipherText);
    }
    
    DataView Encryption::getKey() const
    {
        return DataView((void*)key, ENCRYPTION_KEY_BYTE_SIZE);
    }
    
    DataView Encryption::getNonce() const
    {
        return DataView((void*)nonce, ENCRYPTION_NONCE_BYTE_SIZE);
    }
    
    DataView Encryption::getCipherText() const
    {
        return DataView((void*)cipherText, cipherTextLength);
    }
    
    void Encryption::generateKey(unsigned char *output)
    {
        crypto_aead_xchacha20poly1305_ietf_keygen(output);
    }
    
    Decryption::Decryption(const DataView &data, const DataView &nonce, const DataView &key)
    {
        decryptedTextLength = data.size;
        decryptedText = new unsigned char[decryptedTextLength];
        
        if(nonce.size < ENCRYPTION_NONCE_BYTE_SIZE)
            throw DecryptionException("Nonce is not big enough");
        
        if(key.size < ENCRYPTION_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(Decryption &&other)
    {
        decryptedText = other.decryptedText;
        decryptedTextLength = other.decryptedTextLength;
        
        other.decryptedText = nullptr;
        other.decryptedTextLength = 0;
    }
    
    Decryption& Decryption::operator=(Decryption &&other)
    {
        decryptedText = other.decryptedText;
        decryptedTextLength = other.decryptedTextLength;
        
        other.decryptedText = nullptr;
        other.decryptedTextLength = 0;
        return *this;
    }
    
    Decryption::~Decryption()
    {
        delete[](decryptedText);
    }
    
    DataView Decryption::getDecryptedText() const
    {
        return DataView((void*)decryptedText, decryptedTextLength);
    }
}