QXmpp  Version: 1.7.1
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 
19 namespace QXmpp::Private::Encryption {
20 
21 enum Direction {
22  Encode,
23  Decode,
24 };
25 
26 QXMPP_EXPORT bool isSupported(Cipher);
27 QXMPP_EXPORT QByteArray process(const QByteArray &data, Cipher cipherConfig, Direction direction, const QByteArray &key, const QByteArray &iv);
28 QXMPP_EXPORT QByteArray generateKey(Cipher cipher);
29 QXMPP_EXPORT QByteArray generateInitializationVector(Cipher);
30 
31 // export for tests
32 class QXMPP_EXPORT EncryptionDevice : public QIODevice
33 {
34 public:
35  EncryptionDevice(std::unique_ptr<QIODevice> input, Cipher config, const QByteArray &key, const QByteArray &iv);
36  ~EncryptionDevice() override;
37 
38  bool open(QIODevice::OpenMode mode) override;
39  void close() override;
40  bool isSequential() const override;
41  qint64 size() const override;
42  qint64 readData(char *data, qint64 maxlen) override;
43  qint64 writeData(const char *data, qint64 len) override;
44  bool atEnd() const override;
45 
46 private:
47  Cipher m_cipherConfig;
48  bool m_finalized = false;
49  std::vector<char> m_outputBuffer;
50  std::unique_ptr<QIODevice> m_input;
51  std::unique_ptr<QCA::Cipher> m_cipher;
52 };
53 
54 class QXMPP_EXPORT DecryptionDevice : public QIODevice
55 {
56 public:
57  DecryptionDevice(std::unique_ptr<QIODevice> output, Cipher config, const QByteArray &key, const QByteArray &iv);
58  ~DecryptionDevice() override;
59 
60  bool open(QIODevice::OpenMode mode) override;
61  void close() override;
62  bool isSequential() const override;
63  qint64 size() const override;
64  qint64 readData(char *data, qint64 maxlen) override;
65  qint64 writeData(const char *data, qint64 len) override;
66  void finish();
67 
68 private:
69  Cipher m_cipherConfig;
70  std::vector<char> m_outputBuffer;
71  std::unique_ptr<QIODevice> m_output;
72  std::unique_ptr<QCA::Cipher> m_cipher;
73 };
74 
75 } // namespace QXmpp::Private::Encryption
76 
77 #endif // QXMPPFILEENCRYPTION_H
Cipher
Definition: QXmppGlobal.h:160