swift
Loading...
Searching...
No Matches
cryptodtochannel.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
6#include <algorithm>
7
8#include "sodium/crypto_aead_chacha20poly1305.h"
9
10#include "misc/verify.h"
11
12using namespace swift::misc;
13
14namespace swift::core::afv::crypto
15{
16 CCryptoDtoChannel::CCryptoDtoChannel(const CryptoDtoChannelConfigDto &channelConfig, int receiveSequenceHistorySize)
17 : m_aeadTransmitKey(channelConfig.aeadTransmitKey), m_aeadReceiveKey(channelConfig.aeadReceiveKey),
18 m_receiveSequenceSizeMaxSize(receiveSequenceHistorySize), m_hmacKey(channelConfig.hmacKey),
19 m_channelTag(channelConfig.channelTag)
20 {
21 if (m_aeadTransmitKey.size() != crypto_aead_chacha20poly1305_IETF_KEYBYTES)
22 {
23 SWIFT_AUDIT_X(false, Q_FUNC_INFO, "wrong transmit key size");
24 throw std::invalid_argument("wrong transmit key size");
25 }
26
27 if (m_aeadReceiveKey.size() != crypto_aead_chacha20poly1305_IETF_KEYBYTES)
28 {
29 SWIFT_AUDIT_X(false, Q_FUNC_INFO, "wrong receive key size");
30 throw std::invalid_argument("wrong receive key size");
31 }
32
33 m_receiveSequenceSizeMaxSize = std::max(m_receiveSequenceSizeMaxSize, 1);
34 m_receiveSequenceHistory.fill(0, m_receiveSequenceSizeMaxSize);
35 m_receiveSequenceHistoryDepth = 0;
36 }
37
39 {
40 switch (mode)
41 {
42 case CryptoDtoMode::AEAD_ChaCha20Poly1305: return m_aeadTransmitKey;
45 SWIFT_VERIFY_X(false, Q_FUNC_INFO, "GetTransmitKey called with wrong argument.");
46 break;
47 }
48
49 return {};
50 }
51
53 {
54 sequenceToSend = m_transmitSequence;
55 m_transmitSequence++;
56 m_LastTransmitUtc = QDateTime::currentDateTimeUtc();
57
58 switch (mode)
59 {
60 case CryptoDtoMode::AEAD_ChaCha20Poly1305: return m_aeadTransmitKey;
63 SWIFT_VERIFY_X(false, Q_FUNC_INFO, "GetTransmitKey called with wrong argument.");
64 break;
65 }
66
67 return {};
68 }
69
70 QString CCryptoDtoChannel::getChannelTag() const { return m_channelTag; }
71
73 {
74 switch (mode)
75 {
76 case CryptoDtoMode::AEAD_ChaCha20Poly1305: return m_aeadReceiveKey;
79 SWIFT_VERIFY_X(false, Q_FUNC_INFO, "GetReceiveKey called with wrong argument.");
80 break;
81 }
82
83 return {};
84 }
85
86 bool CCryptoDtoChannel::checkReceivedSequence(uint sequenceReceived)
87 {
88 if (contains(sequenceReceived))
89 {
90 // Duplication or replay attack
91 return false;
92 }
93
94 if (m_receiveSequenceHistoryDepth < m_receiveSequenceSizeMaxSize) // If the buffer has been filled...
95 {
96 m_receiveSequenceHistory[m_receiveSequenceHistoryDepth++] = sequenceReceived;
97 }
98 else
99 {
100 int minIndex {};
101 uint minValue = getMin(minIndex);
102 if (sequenceReceived < minValue)
103 {
104 return false;
105 } // Possible replay attack
106 m_receiveSequenceHistory[minIndex] = sequenceReceived;
107 }
108
109 m_lastReceiveUtc = QDateTime::currentDateTimeUtc();
110 return true;
111 }
112
113 bool CCryptoDtoChannel::contains(uint sequence) const
114 {
115 for (int i = 0; i < m_receiveSequenceHistoryDepth; i++)
116 {
117 if (m_receiveSequenceHistory[i] == sequence)
118 {
119 return true;
120 }
121 }
122 return false;
123 }
124
125 uint CCryptoDtoChannel::getMin(int &minIndex) const
126 {
127 uint minValue = std::numeric_limits<uint>::max();
128 minIndex = -1;
129 int index = -1;
130
131 for (int i = 0; i < m_receiveSequenceHistoryDepth; i++)
132 {
133 index++;
134 if (m_receiveSequenceHistory[i] <= minValue)
135 {
136 minValue = m_receiveSequenceHistory[i];
137 minIndex = index;
138 }
139 }
140 return minValue;
141 }
142} // namespace swift::core::afv::crypto
QByteArray getReceiveKey(CryptoDtoMode mode)
Receiver key.
bool checkReceivedSequence(uint sequenceReceived)
check the received sequence
QByteArray getTransmitKey(CryptoDtoMode mode)
Transmit key.
CCryptoDtoChannel(const CryptoDtoChannelConfigDto &channelConfig, int receiveSequenceHistorySize=10)
Ctor.
Free functions in swift::misc.
QDateTime currentDateTimeUtc()
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition verify.h:26