swift
Loading...
Searching...
No Matches
cryptodtoserializer.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6namespace swift::core::afv::crypto
7{
9 const QByteArray &bytes, bool loopback)
10 {
11 return { channel, bytes, loopback };
12 }
13
15 {
16 QByteArray data(bytes);
17 QBuffer buffer(&data);
19
20 buffer.read(reinterpret_cast<char *>(&m_headerLength), sizeof(m_headerLength));
21
22 QByteArray headerBuffer = buffer.read(m_headerLength);
23
24 msgpack::object_handle oh = msgpack::unpack(headerBuffer.data(), headerBuffer.size());
25 m_header = oh.get().as<CryptoDtoHeaderDto>();
26
28 {
29 int aeLength = buffer.size() - (2 + m_headerLength);
30 const QByteArray aePayloadBuffer = buffer.read(aeLength);
31
32 const QByteArray adBuffer = data.left(2 + m_headerLength);
33
34 QByteArray nonce;
35 nonce.fill(0, crypto_aead_chacha20poly1305_IETF_NPUBBYTES);
36 QBuffer nonceBuffer(&nonce);
37 nonceBuffer.open(QIODevice::WriteOnly);
38 uint32_t id = 0;
39 nonceBuffer.write(reinterpret_cast<const char *>(&id), sizeof(id));
40 nonceBuffer.write(reinterpret_cast<const char *>(&m_header.Sequence), sizeof(m_header.Sequence));
41 nonceBuffer.close();
42
43 QByteArray decryptedPayload;
44 unsigned long long mlen = 500;
45 decryptedPayload.fill(0, mlen);
46
47 QByteArray key;
48 if (loopback)
49 {
51 }
52 else
53 {
55 }
56 Q_ASSERT_X(key.size() == crypto_aead_chacha20poly1305_IETF_KEYBYTES, Q_FUNC_INFO, "");
57 int result = crypto_aead_chacha20poly1305_ietf_decrypt(
58 reinterpret_cast<unsigned char *>(decryptedPayload.data()), &mlen, nullptr,
59 reinterpret_cast<const unsigned char *>(aePayloadBuffer.constData()), aePayloadBuffer.size(),
60 reinterpret_cast<const unsigned char *>(adBuffer.constData()), adBuffer.size(),
61 reinterpret_cast<const unsigned char *>(nonce.constData()),
62 reinterpret_cast<const unsigned char *>(key.constData()));
63
64 if (result == 0)
65 {
66 decryptedPayload.resize(mlen);
67
68 // FIXME:
69 // if (! channel.checkReceivedSequence(header.Sequence)) { }
70
71 QBuffer decryptedPayloadBuffer(&decryptedPayload);
72 decryptedPayloadBuffer.open(QIODevice::ReadOnly);
73 decryptedPayloadBuffer.read(reinterpret_cast<char *>(&m_dtoNameLength), sizeof(m_dtoNameLength));
74 m_dtoNameBuffer = decryptedPayloadBuffer.read(m_dtoNameLength);
75
76 decryptedPayloadBuffer.read(reinterpret_cast<char *>(&m_dataLength), sizeof(m_dataLength));
77 m_dataBuffer = decryptedPayloadBuffer.read(m_dataLength);
78 m_verified = true;
79 }
80 }
81 }
82} // namespace swift::core::afv::crypto
QByteArray getReceiveKey(CryptoDtoMode mode)
Receiver key.
QByteArray getTransmitKey(CryptoDtoMode mode)
Transmit key.
static Deserializer deserialize(CCryptoDtoChannel &channel, const QByteArray &bytes, bool loopback)
Deserialize.
Core data traits (aka cached values) and classes.
virtual void close() override
virtual bool open(QIODeviceBase::OpenMode mode) override
virtual qint64 size() const const override
const char * constData() const const
char * data()
QByteArray & fill(char ch, qsizetype size)
void resize(qsizetype newSize, char c)
qsizetype size() const const
QByteArray read(qint64 maxSize)
qint64 write(const QByteArray &data)
Deserializer(CCryptoDtoChannel &channel, const QByteArray &bytes, bool loopback)
Ctor.