aboutsummaryrefslogtreecommitdiff
path: root/lib/events/encryptedfile.cpp
diff options
context:
space:
mode:
authorTobias Fella <fella@posteo.de>2022-03-08 00:06:36 +0100
committerTobias Fella <fella@posteo.de>2022-05-16 20:47:17 +0200
commitefa450920e5fc338e771e653ca0889e948d04ee7 (patch)
tree215efb53d0e06ed660a97593d56ffb4868dbc2e2 /lib/events/encryptedfile.cpp
parent6f5ac9b7315d75692960e5eac7b1eb6867c0d203 (diff)
downloadlibquotient-efa450920e5fc338e771e653ca0889e948d04ee7.tar.gz
libquotient-efa450920e5fc338e771e653ca0889e948d04ee7.zip
Implement sending encrypted files
Diffstat (limited to 'lib/events/encryptedfile.cpp')
-rw-r--r--lib/events/encryptedfile.cpp26
1 files changed, 24 insertions, 2 deletions
diff --git a/lib/events/encryptedfile.cpp b/lib/events/encryptedfile.cpp
index d4a517bd..e90be428 100644
--- a/lib/events/encryptedfile.cpp
+++ b/lib/events/encryptedfile.cpp
@@ -8,6 +8,7 @@
#ifdef Quotient_E2EE_ENABLED
#include <openssl/evp.h>
#include <QtCore/QCryptographicHash>
+#include "e2ee/qolmutils.h"
#endif
using namespace Quotient;
@@ -27,7 +28,7 @@ QByteArray EncryptedFile::decryptFile(const QByteArray& ciphertext) const
{
int length;
auto* ctx = EVP_CIPHER_CTX_new();
- QByteArray plaintext(ciphertext.size() + EVP_CIPHER_CTX_block_size(ctx)
+ QByteArray plaintext(ciphertext.size() + EVP_MAX_BLOCK_LENGTH
- 1,
'\0');
EVP_DecryptInit_ex(ctx, EVP_aes_256_ctr(), nullptr,
@@ -44,7 +45,7 @@ QByteArray EncryptedFile::decryptFile(const QByteArray& ciphertext) const
+ length,
&length);
EVP_CIPHER_CTX_free(ctx);
- return plaintext;
+ return plaintext.left(ciphertext.size());
}
#else
qWarning(MAIN) << "This build of libQuotient doesn't support E2EE, "
@@ -53,6 +54,27 @@ QByteArray EncryptedFile::decryptFile(const QByteArray& ciphertext) const
#endif
}
+std::pair<EncryptedFile, QByteArray> EncryptedFile::encryptFile(const QByteArray &plainText)
+{
+ QByteArray k = getRandom(32);
+ auto kBase64 = k.toBase64();
+ QByteArray iv = getRandom(16);
+ JWK key = {"oct"_ls, {"encrypt"_ls, "decrypt"_ls}, "A256CTR"_ls, QString(k.toBase64()).replace(u'/', u'_').replace(u'+', u'-').left(kBase64.indexOf('=')), true};
+
+ int length;
+ auto* ctx = EVP_CIPHER_CTX_new();
+ QByteArray cipherText(plainText.size(), plainText.size() + EVP_MAX_BLOCK_LENGTH - 1);
+ EVP_EncryptInit_ex(ctx, EVP_aes_256_ctr(), nullptr, reinterpret_cast<const unsigned char*>(k.data()),reinterpret_cast<const unsigned char*>(iv.data()));
+ 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);
+
+ auto hash = QCryptographicHash::hash(cipherText, QCryptographicHash::Sha256).toBase64();
+ auto ivBase64 = iv.toBase64();
+ EncryptedFile file = {{}, key, ivBase64.left(ivBase64.indexOf('=')), {{QStringLiteral("sha256"), hash.left(hash.indexOf('='))}}, "v2"_ls};
+ return {file, cipherText};
+}
+
void JsonObjectConverter<EncryptedFile>::dumpTo(QJsonObject& jo,
const EncryptedFile& pod)
{