Skip to content

Commit b6934fd

Browse files
committed
net: merge V2Transport constructors, move key gen
This removes the ability for BIP324Cipher to generate its own key, moving that responsibility to the caller (mostly, V2Transport). This allows us to write the random-key V2Transport constructor by delegating to the explicit-key one.
1 parent c5a63ea commit b6934fd

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

src/bip324.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@
2222
#include <iterator>
2323
#include <string>
2424

25-
BIP324Cipher::BIP324Cipher() noexcept
26-
{
27-
m_key.MakeNewKey(true);
28-
uint256 entropy = GetRandHash();
29-
m_our_pubkey = m_key.EllSwiftCreate(MakeByteSpan(entropy));
30-
}
31-
3225
BIP324Cipher::BIP324Cipher(const CKey& key, Span<const std::byte> ent32) noexcept :
3326
m_key(key)
3427
{

src/bip324.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class BIP324Cipher
4141
std::array<std::byte, GARBAGE_TERMINATOR_LEN> m_recv_garbage_terminator;
4242

4343
public:
44-
/** Initialize a BIP324 cipher with securely generated random keys. */
45-
BIP324Cipher() noexcept;
44+
/** No default constructor; keys must be provided to create a BIP324Cipher. */
45+
BIP324Cipher() = delete;
4646

4747
/** Initialize a BIP324 cipher with specified key and encoding entropy (testing only). */
4848
BIP324Cipher(const CKey& key, Span<const std::byte> ent32) noexcept;

src/net.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -979,23 +979,24 @@ class V2MessageMap
979979

980980
const V2MessageMap V2_MESSAGE_MAP;
981981

982-
} // namespace
982+
CKey GenerateRandomKey() noexcept
983+
{
984+
CKey key;
985+
key.MakeNewKey(/*fCompressed=*/true);
986+
return key;
987+
}
983988

984-
V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in) noexcept :
985-
m_cipher{}, m_initiating{initiating}, m_nodeid{nodeid},
986-
m_v1_fallback{nodeid, type_in, version_in}, m_recv_type{type_in}, m_recv_version{version_in},
987-
m_recv_state{initiating ? RecvState::KEY : RecvState::KEY_MAYBE_V1},
988-
m_send_state{initiating ? SendState::AWAITING_KEY : SendState::MAYBE_V1}
989+
std::vector<uint8_t> GenerateRandomGarbage() noexcept
989990
{
990-
// Construct garbage (including its length) using a FastRandomContext.
991+
std::vector<uint8_t> ret;
991992
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);
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+
ret.resize(rng.randrange(V2Transport::MAX_GARBAGE_LEN + 1));
994+
rng.fillrand(MakeWritableByteSpan(ret));
995+
return ret;
997996
}
998997

998+
} // namespace
999+
9991000
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 :
10001001
m_cipher{key, ent32}, m_initiating{initiating}, m_nodeid{nodeid},
10011002
m_v1_fallback{nodeid, type_in, version_in}, m_recv_type{type_in}, m_recv_version{version_in},
@@ -1009,6 +1010,10 @@ V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int versio
10091010
std::copy(garbage.begin(), garbage.end(), m_send_buffer.begin() + EllSwiftPubKey::size());
10101011
}
10111012

1013+
V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in) noexcept :
1014+
V2Transport{nodeid, initiating, type_in, version_in, GenerateRandomKey(),
1015+
MakeByteSpan(GetRandHash()), GenerateRandomGarbage()} { }
1016+
10121017
void V2Transport::SetReceiveState(RecvState recv_state) noexcept
10131018
{
10141019
AssertLockHeld(m_recv_mutex);

src/test/net_tests.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,14 @@ BOOST_AUTO_TEST_CASE(advertise_local_address)
10081008

10091009
namespace {
10101010

1011+
CKey GenerateRandomTestKey() noexcept
1012+
{
1013+
CKey key;
1014+
uint256 key_data = InsecureRand256();
1015+
key.Set(key_data.begin(), key_data.end(), true);
1016+
return key;
1017+
}
1018+
10111019
/** A class for scenario-based tests of V2Transport
10121020
*
10131021
* Each V2TransportTester encapsulates a V2Transport (the one being tested), and can be told to
@@ -1031,6 +1039,7 @@ class V2TransportTester
10311039
/** Construct a tester object. test_initiator: whether the tested transport is initiator. */
10321040
V2TransportTester(bool test_initiator) :
10331041
m_transport(0, test_initiator, SER_NETWORK, INIT_PROTO_VERSION),
1042+
m_cipher{GenerateRandomTestKey(), MakeByteSpan(InsecureRand256())},
10341043
m_test_initiator(test_initiator) {}
10351044

10361045
/** Data type returned by Interact:

0 commit comments

Comments
 (0)