@@ -987,20 +987,26 @@ V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int versio
987987 m_recv_state{initiating ? RecvState::KEY : RecvState::KEY_MAYBE_V1},
988988 m_send_state{initiating ? SendState::AWAITING_KEY : SendState::MAYBE_V1}
989989{
990- // Initialize the send buffer with ellswift pubkey.
991- m_send_buffer.resize (EllSwiftPubKey::size ());
990+ // Construct garbage (including its length) using a FastRandomContext.
991+ FastRandomContext rng;
992+ size_t garbage_len = rng.randrange (MAX_GARBAGE_LEN + 1 );
993+ // Initialize the send buffer with ellswift pubkey + garbage.
994+ m_send_buffer.resize (EllSwiftPubKey::size () + garbage_len);
992995 std::copy (std::begin (m_cipher.GetOurPubKey ()), std::end (m_cipher.GetOurPubKey ()), MakeWritableByteSpan (m_send_buffer).begin ());
996+ rng.fillrand (MakeWritableByteSpan (m_send_buffer).subspan (EllSwiftPubKey::size ()));
993997}
994998
995- V2Transport::V2Transport (NodeId nodeid, bool initiating, int type_in, int version_in, const CKey& key, Span<const std::byte> ent32) noexcept :
999+ V2Transport::V2Transport (NodeId nodeid, bool initiating, int type_in, int version_in, const CKey& key, Span<const std::byte> ent32, Span< const uint8_t > garbage ) noexcept :
9961000 m_cipher{key, ent32}, m_initiating{initiating}, m_nodeid{nodeid},
9971001 m_v1_fallback{nodeid, type_in, version_in}, m_recv_type{type_in}, m_recv_version{version_in},
9981002 m_recv_state{initiating ? RecvState::KEY : RecvState::KEY_MAYBE_V1},
9991003 m_send_state{initiating ? SendState::AWAITING_KEY : SendState::MAYBE_V1}
10001004{
1001- // Initialize the send buffer with ellswift pubkey.
1002- m_send_buffer.resize (EllSwiftPubKey::size ());
1005+ assert (garbage.size () <= MAX_GARBAGE_LEN);
1006+ // Initialize the send buffer with ellswift pubkey + provided garbage.
1007+ m_send_buffer.resize (EllSwiftPubKey::size () + garbage.size ());
10031008 std::copy (std::begin (m_cipher.GetOurPubKey ()), std::end (m_cipher.GetOurPubKey ()), MakeWritableByteSpan (m_send_buffer).begin ());
1009+ std::copy (garbage.begin (), garbage.end (), m_send_buffer.begin () + EllSwiftPubKey::size ());
10041010}
10051011
10061012void V2Transport::SetReceiveState (RecvState recv_state) noexcept
@@ -1126,16 +1132,18 @@ void V2Transport::ProcessReceivedKeyBytes() noexcept
11261132 SetSendState (SendState::READY);
11271133
11281134 // Append the garbage terminator to the send buffer.
1135+ size_t garbage_len = m_send_buffer.size () - EllSwiftPubKey::size ();
11291136 m_send_buffer.resize (m_send_buffer.size () + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
11301137 std::copy (m_cipher.GetSendGarbageTerminator ().begin (),
11311138 m_cipher.GetSendGarbageTerminator ().end (),
11321139 MakeWritableByteSpan (m_send_buffer).last (BIP324Cipher::GARBAGE_TERMINATOR_LEN).begin ());
11331140
1134- // Construct garbage authentication packet in the send buffer.
1141+ // Construct garbage authentication packet in the send buffer (using the garbage data which
1142+ // is still there).
11351143 m_send_buffer.resize (m_send_buffer.size () + BIP324Cipher::EXPANSION);
11361144 m_cipher.Encrypt (
11371145 /* contents=*/ {},
1138- /* aad=*/ {}, /* empty garbage for now */
1146+ /* aad=*/ MakeByteSpan (m_send_buffer). subspan ( EllSwiftPubKey::size (), garbage_len),
11391147 /* ignore=*/ false ,
11401148 /* output=*/ MakeWritableByteSpan (m_send_buffer).last (BIP324Cipher::EXPANSION));
11411149
@@ -1490,7 +1498,10 @@ void V2Transport::MarkBytesSent(size_t bytes_sent) noexcept
14901498
14911499 m_send_pos += bytes_sent;
14921500 Assume (m_send_pos <= m_send_buffer.size ());
1493- if (m_send_pos == m_send_buffer.size ()) {
1501+ // Only wipe the buffer when everything is sent in the READY state. In the AWAITING_KEY state
1502+ // we still need the garbage that's in the send buffer to construct the garbage authentication
1503+ // packet.
1504+ if (m_send_state == SendState::READY && m_send_pos == m_send_buffer.size ()) {
14941505 m_send_pos = 0 ;
14951506 m_send_buffer = {};
14961507 }
0 commit comments