QXmpp  Version: 1.5.3
QXmppFileEncryption.h
1 // SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 
5 #ifndef QXMPPFILEENCRYPTION_H
6 #define QXMPPFILEENCRYPTION_H
7 
8 #include "QXmppGlobal.h"
9 
10 #include <memory>
11 
12 #include <QIODevice>
13 
14 namespace QCA {
15 class Cipher;
16 class Initializer;
17 } // namespace QCA
18 
20 
21 enum Direction {
22  Encode,
23  Decode,
24 };
25 
26 QXMPP_EXPORT QByteArray process(const QByteArray &data, Cipher cipherConfig, Direction direction, const QByteArray &key, const QByteArray &iv);
27 QXMPP_EXPORT QByteArray generateKey(Cipher cipher);
28 QXMPP_EXPORT QByteArray generateInitializationVector(Cipher);
29 
30 // export for tests
31 class QXMPP_EXPORT EncryptionDevice : public QIODevice
32 {
33 public:
34  EncryptionDevice(std::unique_ptr<QIODevice> input, Cipher config, const QByteArray &key, const QByteArray &iv);
35  ~EncryptionDevice() override;
36 
37  bool open(QIODevice::OpenMode mode) override;
38  void close() override;
39  bool isSequential() const override;
40  qint64 size() const override;
41  qint64 readData(char *data, qint64 maxlen) override;
42  qint64 writeData(const char *data, qint64 len) override;
43  bool atEnd() const override;
44 
45 private:
46  Cipher m_cipherConfig;
47  bool m_finalized = false;
48  std::vector<char> m_outputBuffer;
49  std::unique_ptr<QIODevice> m_input;
50  std::unique_ptr<QCA::Cipher> m_cipher;
51 };
52 
53 class QXMPP_EXPORT DecryptionDevice : public QIODevice
54 {
55 public:
56  DecryptionDevice(std::unique_ptr<QIODevice> output, Cipher config, const QByteArray &key, const QByteArray &iv);
57  ~DecryptionDevice() override;
58 
59  bool open(QIODevice::OpenMode mode) override;
60  void close() override;
61  bool isSequential() const override;
62  qint64 size() const override;
63  qint64 readData(char *data, qint64 maxlen) override;
64  qint64 writeData(const char *data, qint64 len) override;
65 
66 private:
67  Cipher m_cipherConfig;
68  std::vector<char> m_outputBuffer;
69  std::unique_ptr<QIODevice> m_output;
70  std::unique_ptr<QCA::Cipher> m_cipher;
71 };
72 
73 } // namespace QXmpp::Private::Encryption
74 
75 #endif // QXMPPFILEENCRYPTION_H
Cipher
Definition: QXmppGlobal.h:160
Definition: QXmppFileEncryption.h:14
Definition: QXmppFileEncryption.cpp:18