aboutsummaryrefslogtreecommitdiff
path: root/src/client/QXmppFileEncryption.h
blob: e50bf83c4990e1cb4aefe855faffe9b86122013d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// 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,
};

QXMPP_EXPORT QByteArray process(const QByteArray &data, Cipher cipherConfig, Direction direction, const QByteArray &key, const QByteArray &iv);
QXMPP_EXPORT QByteArray generateKey(Cipher cipher);
QXMPP_EXPORT 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;
    bool atEnd() const 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 QXMPP_EXPORT 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