@@ -1002,8 +1002,7 @@ void V2Transport::StartSendingHandshake() noexcept
10021002 m_send_buffer.resize (EllSwiftPubKey::size () + m_send_garbage.size ());
10031003 std::copy (std::begin (m_cipher.GetOurPubKey ()), std::end (m_cipher.GetOurPubKey ()), MakeWritableByteSpan (m_send_buffer).begin ());
10041004 std::copy (m_send_garbage.begin (), m_send_garbage.end (), m_send_buffer.begin () + EllSwiftPubKey::size ());
1005- // We cannot wipe m_send_garbage as it will still be used to construct the garbage
1006- // authentication packet.
1005+ // We cannot wipe m_send_garbage as it will still be used as AAD later in the handshake.
10071006}
10081007
10091008V2Transport::V2Transport (NodeId nodeid, bool initiating, int type_in, int version_in, const CKey& key, Span<const std::byte> ent32, std::vector<uint8_t > garbage) noexcept :
@@ -1037,9 +1036,6 @@ void V2Transport::SetReceiveState(RecvState recv_state) noexcept
10371036 Assume (recv_state == RecvState::GARB_GARBTERM);
10381037 break ;
10391038 case RecvState::GARB_GARBTERM:
1040- Assume (recv_state == RecvState::GARBAUTH);
1041- break ;
1042- case RecvState::GARBAUTH:
10431039 Assume (recv_state == RecvState::VERSION);
10441040 break ;
10451041 case RecvState::VERSION:
@@ -1171,24 +1167,15 @@ bool V2Transport::ProcessReceivedKeyBytes() noexcept
11711167 m_cipher.GetSendGarbageTerminator ().end (),
11721168 MakeWritableByteSpan (m_send_buffer).last (BIP324Cipher::GARBAGE_TERMINATOR_LEN).begin ());
11731169
1174- // Construct garbage authentication packet in the send buffer (using the garbage data which
1175- // is still there).
1176- m_send_buffer.resize (m_send_buffer.size () + BIP324Cipher::EXPANSION);
1177- m_cipher.Encrypt (
1178- /* contents=*/ {},
1179- /* aad=*/ MakeByteSpan (m_send_garbage),
1180- /* ignore=*/ false ,
1181- /* output=*/ MakeWritableByteSpan (m_send_buffer).last (BIP324Cipher::EXPANSION));
1182- // We no longer need the garbage.
1183- ClearShrink (m_send_garbage);
1184-
1185- // Construct version packet in the send buffer.
1170+ // Construct version packet in the send buffer, with the sent garbage data as AAD.
11861171 m_send_buffer.resize (m_send_buffer.size () + BIP324Cipher::EXPANSION + VERSION_CONTENTS.size ());
11871172 m_cipher.Encrypt (
11881173 /* contents=*/ VERSION_CONTENTS,
1189- /* aad=*/ {} ,
1174+ /* aad=*/ MakeByteSpan (m_send_garbage) ,
11901175 /* ignore=*/ false ,
11911176 /* output=*/ MakeWritableByteSpan (m_send_buffer).last (BIP324Cipher::EXPANSION + VERSION_CONTENTS.size ()));
1177+ // We no longer need the garbage.
1178+ ClearShrink (m_send_garbage);
11921179 } else {
11931180 // We still have to receive more key bytes.
11941181 }
@@ -1202,11 +1189,11 @@ bool V2Transport::ProcessReceivedGarbageBytes() noexcept
12021189 Assume (m_recv_buffer.size () <= MAX_GARBAGE_LEN + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
12031190 if (m_recv_buffer.size () >= BIP324Cipher::GARBAGE_TERMINATOR_LEN) {
12041191 if (MakeByteSpan (m_recv_buffer).last (BIP324Cipher::GARBAGE_TERMINATOR_LEN) == m_cipher.GetReceiveGarbageTerminator ()) {
1205- // Garbage terminator received. Switch to receiving garbage authentication packet .
1192+ // Garbage terminator received. Store garbage to authenticate it as AAD later .
12061193 m_recv_garbage = std::move (m_recv_buffer);
12071194 m_recv_garbage.resize (m_recv_garbage.size () - BIP324Cipher::GARBAGE_TERMINATOR_LEN);
12081195 m_recv_buffer.clear ();
1209- SetReceiveState (RecvState::GARBAUTH );
1196+ SetReceiveState (RecvState::VERSION );
12101197 } else if (m_recv_buffer.size () == MAX_GARBAGE_LEN + BIP324Cipher::GARBAGE_TERMINATOR_LEN) {
12111198 // We've reached the maximum length for garbage + garbage terminator, and the
12121199 // terminator still does not match. Abort.
@@ -1225,8 +1212,7 @@ bool V2Transport::ProcessReceivedGarbageBytes() noexcept
12251212bool V2Transport::ProcessReceivedPacketBytes () noexcept
12261213{
12271214 AssertLockHeld (m_recv_mutex);
1228- Assume (m_recv_state == RecvState::GARBAUTH || m_recv_state == RecvState::VERSION ||
1229- m_recv_state == RecvState::APP);
1215+ Assume (m_recv_state == RecvState::VERSION || m_recv_state == RecvState::APP);
12301216
12311217 // The maximum permitted contents length for a packet, consisting of:
12321218 // - 0x00 byte: indicating long message type encoding
@@ -1250,7 +1236,7 @@ bool V2Transport::ProcessReceivedPacketBytes() noexcept
12501236 m_recv_decode_buffer.resize (m_recv_len);
12511237 bool ignore{false };
12521238 Span<const std::byte> aad;
1253- if (m_recv_state == RecvState::GARBAUTH ) aad = MakeByteSpan (m_recv_garbage);
1239+ if (m_recv_state == RecvState::VERSION ) aad = MakeByteSpan (m_recv_garbage);
12541240 bool ret = m_cipher.Decrypt (
12551241 /* input=*/ MakeByteSpan (m_recv_buffer).subspan (BIP324Cipher::LENGTH_LEN),
12561242 /* aad=*/ aad,
@@ -1266,18 +1252,16 @@ bool V2Transport::ProcessReceivedPacketBytes() noexcept
12661252 // At this point we have a valid packet decrypted into m_recv_decode_buffer. Depending on
12671253 // the current state, decide what to do with it.
12681254 switch (m_recv_state) {
1269- case RecvState::GARBAUTH:
1270- // Ignore flag does not matter for garbage authentication. Any valid packet functions
1271- // as authentication. Receive and process the version packet next.
1272- SetReceiveState (RecvState::VERSION);
1273- ClearShrink (m_recv_garbage);
1274- break ;
12751255 case RecvState::VERSION:
12761256 if (!ignore) {
12771257 // Version message received; transition to application phase. The contents is
12781258 // ignored, but can be used for future extensions.
12791259 SetReceiveState (RecvState::APP);
12801260 }
1261+ // We have decrypted one valid packet (which may or may not have been a decoy) with the
1262+ // received garbage as AAD. We no longer need the received garbage and further packets
1263+ // are expected to use the empty string as AAD.
1264+ ClearShrink (m_recv_garbage);
12811265 break ;
12821266 case RecvState::APP:
12831267 if (!ignore) {
@@ -1323,7 +1307,6 @@ size_t V2Transport::GetMaxBytesToProcess() noexcept
13231307 case RecvState::GARB_GARBTERM:
13241308 // Process garbage bytes one by one (because terminator may appear anywhere).
13251309 return 1 ;
1326- case RecvState::GARBAUTH:
13271310 case RecvState::VERSION:
13281311 case RecvState::APP:
13291312 // These three states all involve decoding a packet. Process the length descriptor first,
@@ -1377,7 +1360,6 @@ bool V2Transport::ReceivedBytes(Span<const uint8_t>& msg_bytes) noexcept
13771360 // bytes).
13781361 m_recv_buffer.reserve (MAX_GARBAGE_LEN + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
13791362 break ;
1380- case RecvState::GARBAUTH:
13811363 case RecvState::VERSION:
13821364 case RecvState::APP: {
13831365 // During states where a packet is being received, as much as is expected but never
@@ -1421,7 +1403,6 @@ bool V2Transport::ReceivedBytes(Span<const uint8_t>& msg_bytes) noexcept
14211403 if (!ProcessReceivedGarbageBytes ()) return false ;
14221404 break ;
14231405
1424- case RecvState::GARBAUTH:
14251406 case RecvState::VERSION:
14261407 case RecvState::APP:
14271408 if (!ProcessReceivedPacketBytes ()) return false ;
0 commit comments