@@ -987,20 +987,26 @@ V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int versio
987
987
m_recv_state{initiating ? RecvState::KEY : RecvState::KEY_MAYBE_V1},
988
988
m_send_state{initiating ? SendState::AWAITING_KEY : SendState::MAYBE_V1}
989
989
{
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);
992
995
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 ()));
993
997
}
994
998
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 :
996
1000
m_cipher{key, ent32}, m_initiating{initiating}, m_nodeid{nodeid},
997
1001
m_v1_fallback{nodeid, type_in, version_in}, m_recv_type{type_in}, m_recv_version{version_in},
998
1002
m_recv_state{initiating ? RecvState::KEY : RecvState::KEY_MAYBE_V1},
999
1003
m_send_state{initiating ? SendState::AWAITING_KEY : SendState::MAYBE_V1}
1000
1004
{
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 ());
1003
1008
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 ());
1004
1010
}
1005
1011
1006
1012
void V2Transport::SetReceiveState (RecvState recv_state) noexcept
@@ -1126,16 +1132,18 @@ void V2Transport::ProcessReceivedKeyBytes() noexcept
1126
1132
SetSendState (SendState::READY);
1127
1133
1128
1134
// Append the garbage terminator to the send buffer.
1135
+ size_t garbage_len = m_send_buffer.size () - EllSwiftPubKey::size ();
1129
1136
m_send_buffer.resize (m_send_buffer.size () + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
1130
1137
std::copy (m_cipher.GetSendGarbageTerminator ().begin (),
1131
1138
m_cipher.GetSendGarbageTerminator ().end (),
1132
1139
MakeWritableByteSpan (m_send_buffer).last (BIP324Cipher::GARBAGE_TERMINATOR_LEN).begin ());
1133
1140
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).
1135
1143
m_send_buffer.resize (m_send_buffer.size () + BIP324Cipher::EXPANSION);
1136
1144
m_cipher.Encrypt (
1137
1145
/* contents=*/ {},
1138
- /* aad=*/ {}, /* empty garbage for now */
1146
+ /* aad=*/ MakeByteSpan (m_send_buffer). subspan ( EllSwiftPubKey::size (), garbage_len),
1139
1147
/* ignore=*/ false ,
1140
1148
/* output=*/ MakeWritableByteSpan (m_send_buffer).last (BIP324Cipher::EXPANSION));
1141
1149
@@ -1490,7 +1498,10 @@ void V2Transport::MarkBytesSent(size_t bytes_sent) noexcept
1490
1498
1491
1499
m_send_pos += bytes_sent;
1492
1500
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 ()) {
1494
1505
m_send_pos = 0 ;
1495
1506
m_send_buffer = {};
1496
1507
}
0 commit comments