From c8bc1db682c165853ad51e2806f932e4fd0b0597 Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Wed, 14 Sep 2022 22:36:11 +0200 Subject: Add file encryption functions and Encryption/DecryptionDevice The devices allow it to encrypt or decrypt data on the fly when reading or writing data. --- src/client/QXmppFileEncryption.h | 74 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/client/QXmppFileEncryption.h (limited to 'src/client/QXmppFileEncryption.h') 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 +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#ifndef QXMPPFILEENCRYPTION_H +#define QXMPPFILEENCRYPTION_H + +#include "QXmppGlobal.h" + +#include + +#include + +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 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 m_outputBuffer; + std::unique_ptr m_input; + std::unique_ptr m_cipher; +}; + +class DecryptionDevice : public QIODevice +{ +public: + DecryptionDevice(std::unique_ptr 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 m_outputBuffer; + std::unique_ptr m_output; + std::unique_ptr m_cipher; +}; + +} // namespace QXmpp::Private::Encryption + +#endif // QXMPPFILEENCRYPTION_H -- cgit v1.2.3