aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/encryptionmanager.cpp24
-rw-r--r--lib/encryptionmanager.h2
2 files changed, 26 insertions, 0 deletions
diff --git a/lib/encryptionmanager.cpp b/lib/encryptionmanager.cpp
index b9bd6646..48e6701c 100644
--- a/lib/encryptionmanager.cpp
+++ b/lib/encryptionmanager.cpp
@@ -7,11 +7,13 @@
#include "connection.h"
#include "crypto/e2ee.h"
+#include "events/encryptedfile.h"
#include "csapi/keys.h"
#include <QtCore/QHash>
#include <QtCore/QStringBuilder>
+#include <QtCore/QCryptographicHash>
#include "crypto/qolmaccount.h"
#include "crypto/qolmsession.h"
@@ -21,6 +23,8 @@
#include <functional>
#include <memory>
+#include <openssl/evp.h>
+
using namespace Quotient;
using std::move;
@@ -119,4 +123,24 @@ QString EncryptionManager::sessionDecryptMessage(
}
return decrypted;
}
+
+QByteArray EncryptionManager::decryptFile(const QByteArray &ciphertext, EncryptedFile* file)
+{
+ const auto key = QByteArray::fromBase64(file->key.k.replace(QLatin1Char('_'), QLatin1Char('/')).replace(QLatin1Char('-'), QLatin1Char('+')).toLatin1());
+ const auto iv = QByteArray::fromBase64(file->iv.toLatin1());
+ const auto sha256 = QByteArray::fromBase64(file->hashes["sha256"].toLatin1());
+ if(sha256 != QCryptographicHash::hash(ciphertext, QCryptographicHash::Sha256)) {
+ qCWarning(E2EE) << "Hash verification failed for file";
+ return QByteArray();
+ }
+ QByteArray plaintext(ciphertext.size(), 0);
+ EVP_CIPHER_CTX *ctx;
+ int length;
+ ctx = EVP_CIPHER_CTX_new();
+ EVP_DecryptInit_ex(ctx, EVP_aes_256_ctr(), NULL, (const unsigned char *)key.data(), (const unsigned char *)iv.data());
+ EVP_DecryptUpdate(ctx, (unsigned char *)plaintext.data(), &length, (const unsigned char *)ciphertext.data(), ciphertext.size());
+ EVP_DecryptFinal_ex(ctx, (unsigned char *)plaintext.data() + length, &length);
+ EVP_CIPHER_CTX_free(ctx);
+ return plaintext;
+}
#endif // Quotient_E2EE_ENABLED
diff --git a/lib/encryptionmanager.h b/lib/encryptionmanager.h
index 17f4f853..96569980 100644
--- a/lib/encryptionmanager.h
+++ b/lib/encryptionmanager.h
@@ -12,6 +12,7 @@
namespace Quotient {
class Connection;
class QOlmAccount;
+struct EncryptedFile;
class EncryptionManager : public QObject {
Q_OBJECT
@@ -21,6 +22,7 @@ public:
~EncryptionManager();
QString sessionDecryptMessage(const QJsonObject& personalCipherObject,
const QByteArray& senderKey, std::unique_ptr<QOlmAccount>& account);
+ static QByteArray decryptFile(const QByteArray &ciphertext, EncryptedFile* encryptedFile);
private:
class Private;