aboutsummaryrefslogtreecommitdiff
path: root/src/client/QXmppFileEncryption.h
diff options
context:
space:
mode:
authorLinus Jahn <lnj@kaidan.im>2022-09-14 22:36:11 +0200
committerLinus Jahn <lnj@kaidan.im>2022-09-24 18:11:58 +0200
commitc8bc1db682c165853ad51e2806f932e4fd0b0597 (patch)
tree8bd06a8b72ef038ee2ffaca8881656df950d84b8 /src/client/QXmppFileEncryption.h
parent9d9d0b22664c6860a005818e9e787670aec389ff (diff)
downloadqxmpp-c8bc1db682c165853ad51e2806f932e4fd0b0597.tar.gz
Add file encryption functions and Encryption/DecryptionDevice
The devices allow it to encrypt or decrypt data on the fly when reading or writing data.
Diffstat (limited to 'src/client/QXmppFileEncryption.h')
-rw-r--r--src/client/QXmppFileEncryption.h74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/client/QXmppFileEncryption.h b/src/client/QXmppFileEncryption.h
new file mode 100644
index 00000000..b1108b22
--- /dev/null
+++ b/src/client/QXmppFileEncryption.h
@@ -0,0 +1,74 @@
+// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
+//
+// SPDX-License-Identifier: LGPL-2.1-or-later
+
+#ifndef QXMPPFILEENCRYPTION_H
+#define QXMPPFILEENCRYPTION_H
+
+#include "QXmppGlobal.h"
+
+#include <memory>
+
+#include <QIODevice>
+
+namespace QCA {
+class Cipher;
+class Initializer;
+} // namespace QCA
+
+namespace QXmpp::Private::Encryption {
+
+enum Direction {
+ Encode,
+ Decode,
+};
+
+QByteArray process(const QByteArray &data, Cipher cipherConfig, Direction direction, const QByteArray &key, const QByteArray &iv);
+QByteArray generateKey(Cipher cipher);
+QByteArray generateInitializationVector(Cipher);
+
+// export for tests
+class QXMPP_EXPORT EncryptionDevice : public QIODevice
+{
+public:
+ EncryptionDevice(std::unique_ptr<QIODevice> input, Cipher config, const QByteArray &key, const QByteArray &iv);
+ ~EncryptionDevice() override;
+
+ bool open(QIODevice::OpenMode mode) override;
+ void close() override;
+ bool isSequential() const override;
+ qint64 size() const override;
+ qint64 readData(char *data, qint64 maxlen) override;
+ qint64 writeData(const char *data, qint64 len) override;
+
+private:
+ Cipher m_cipherConfig;
+ bool m_finalized = false;
+ std::vector<char> m_outputBuffer;
+ std::unique_ptr<QIODevice> m_input;
+ std::unique_ptr<QCA::Cipher> m_cipher;
+};
+
+class DecryptionDevice : public QIODevice
+{
+public:
+ DecryptionDevice(std::unique_ptr<QIODevice> output, Cipher config, const QByteArray &key, const QByteArray &iv);
+ ~DecryptionDevice() override;
+
+ bool open(QIODevice::OpenMode mode) override;
+ void close() override;
+ bool isSequential() const override;
+ qint64 size() const override;
+ qint64 readData(char *data, qint64 maxlen) override;
+ qint64 writeData(const char *data, qint64 len) override;
+
+private:
+ Cipher m_cipherConfig;
+ std::vector<char> m_outputBuffer;
+ std::unique_ptr<QIODevice> m_output;
+ std::unique_ptr<QCA::Cipher> m_cipher;
+};
+
+} // namespace QXmpp::Private::Encryption
+
+#endif // QXMPPFILEENCRYPTION_H