aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-05-20 21:32:57 +0200
committerGitHub <noreply@github.com>2022-05-20 21:32:57 +0200
commit729ba7da174eacc88bf9bd4e2e80eeab3fc92716 (patch)
treeaf15b8c6ea09cfd8a6640be4af634d32d3a78552
parent004cbef8c2fb138310a12439f4b5907862df5654 (diff)
parent59f2b60835752fc87e75f456145d21cc5f77a433 (diff)
downloadlibquotient-729ba7da174eacc88bf9bd4e2e80eeab3fc92716.tar.gz
libquotient-729ba7da174eacc88bf9bd4e2e80eeab3fc92716.zip
Merge pull request #553 from TobiasFella/work/fixencryption
Truncate ciphertext buffer to actual size during file encryption
-rw-r--r--autotests/testfilecrypto.cpp5
-rw-r--r--lib/events/encryptedfile.cpp3
2 files changed, 6 insertions, 2 deletions
diff --git a/autotests/testfilecrypto.cpp b/autotests/testfilecrypto.cpp
index e6bec1fe..f9212376 100644
--- a/autotests/testfilecrypto.cpp
+++ b/autotests/testfilecrypto.cpp
@@ -12,6 +12,9 @@ void TestFileCrypto::encryptDecryptData()
QByteArray data = "ABCDEF";
auto [file, cipherText] = EncryptedFile::encryptFile(data);
auto decrypted = file.decryptFile(cipherText);
- QCOMPARE(data, decrypted);
+ // AES CTR produces ciphertext of the same size as the original
+ QCOMPARE(cipherText.size(), data.size());
+ QCOMPARE(decrypted.size(), data.size());
+ QCOMPARE(decrypted, data);
}
QTEST_APPLESS_MAIN(TestFileCrypto)
diff --git a/lib/events/encryptedfile.cpp b/lib/events/encryptedfile.cpp
index 9cc3a0c8..33ebb514 100644
--- a/lib/events/encryptedfile.cpp
+++ b/lib/events/encryptedfile.cpp
@@ -64,8 +64,9 @@ std::pair<EncryptedFile, QByteArray> EncryptedFile::encryptFile(const QByteArray
int length;
auto* ctx = EVP_CIPHER_CTX_new();
- QByteArray cipherText(plainText.size() + EVP_MAX_BLOCK_LENGTH - 1, '\0');
EVP_EncryptInit_ex(ctx, EVP_aes_256_ctr(), nullptr, reinterpret_cast<const unsigned char*>(k.data()),reinterpret_cast<const unsigned char*>(iv.data()));
+ const auto blockSize = EVP_CIPHER_CTX_block_size(ctx);
+ QByteArray cipherText(plainText.size() + blockSize - 1, '\0');
EVP_EncryptUpdate(ctx, reinterpret_cast<unsigned char*>(cipherText.data()), &length, reinterpret_cast<const unsigned char*>(plainText.data()), plainText.size());
EVP_EncryptFinal_ex(ctx, reinterpret_cast<unsigned char*>(cipherText.data()) + length, &length);
EVP_CIPHER_CTX_free(ctx);