mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-02 09:46:52 -05:00
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.
This commit is contained in:
parent
c5a63ea56f
commit
b6934fd03f
4 changed files with 30 additions and 23 deletions
|
@ -22,13 +22,6 @@
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
BIP324Cipher::BIP324Cipher() noexcept
|
|
||||||
{
|
|
||||||
m_key.MakeNewKey(true);
|
|
||||||
uint256 entropy = GetRandHash();
|
|
||||||
m_our_pubkey = m_key.EllSwiftCreate(MakeByteSpan(entropy));
|
|
||||||
}
|
|
||||||
|
|
||||||
BIP324Cipher::BIP324Cipher(const CKey& key, Span<const std::byte> ent32) noexcept :
|
BIP324Cipher::BIP324Cipher(const CKey& key, Span<const std::byte> ent32) noexcept :
|
||||||
m_key(key)
|
m_key(key)
|
||||||
{
|
{
|
||||||
|
|
|
@ -41,8 +41,8 @@ private:
|
||||||
std::array<std::byte, GARBAGE_TERMINATOR_LEN> m_recv_garbage_terminator;
|
std::array<std::byte, GARBAGE_TERMINATOR_LEN> m_recv_garbage_terminator;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/** Initialize a BIP324 cipher with securely generated random keys. */
|
/** No default constructor; keys must be provided to create a BIP324Cipher. */
|
||||||
BIP324Cipher() noexcept;
|
BIP324Cipher() = delete;
|
||||||
|
|
||||||
/** Initialize a BIP324 cipher with specified key and encoding entropy (testing only). */
|
/** Initialize a BIP324 cipher with specified key and encoding entropy (testing only). */
|
||||||
BIP324Cipher(const CKey& key, Span<const std::byte> ent32) noexcept;
|
BIP324Cipher(const CKey& key, Span<const std::byte> ent32) noexcept;
|
||||||
|
|
33
src/net.cpp
33
src/net.cpp
|
@ -979,23 +979,24 @@ public:
|
||||||
|
|
||||||
const V2MessageMap V2_MESSAGE_MAP;
|
const V2MessageMap V2_MESSAGE_MAP;
|
||||||
|
|
||||||
} // namespace
|
CKey GenerateRandomKey() noexcept
|
||||||
|
|
||||||
V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in) noexcept :
|
|
||||||
m_cipher{}, m_initiating{initiating}, m_nodeid{nodeid},
|
|
||||||
m_v1_fallback{nodeid, type_in, version_in}, m_recv_type{type_in}, m_recv_version{version_in},
|
|
||||||
m_recv_state{initiating ? RecvState::KEY : RecvState::KEY_MAYBE_V1},
|
|
||||||
m_send_state{initiating ? SendState::AWAITING_KEY : SendState::MAYBE_V1}
|
|
||||||
{
|
{
|
||||||
// Construct garbage (including its length) using a FastRandomContext.
|
CKey key;
|
||||||
FastRandomContext rng;
|
key.MakeNewKey(/*fCompressed=*/true);
|
||||||
size_t garbage_len = rng.randrange(MAX_GARBAGE_LEN + 1);
|
return key;
|
||||||
// Initialize the send buffer with ellswift pubkey + garbage.
|
|
||||||
m_send_buffer.resize(EllSwiftPubKey::size() + garbage_len);
|
|
||||||
std::copy(std::begin(m_cipher.GetOurPubKey()), std::end(m_cipher.GetOurPubKey()), MakeWritableByteSpan(m_send_buffer).begin());
|
|
||||||
rng.fillrand(MakeWritableByteSpan(m_send_buffer).subspan(EllSwiftPubKey::size()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<uint8_t> GenerateRandomGarbage() noexcept
|
||||||
|
{
|
||||||
|
std::vector<uint8_t> ret;
|
||||||
|
FastRandomContext rng;
|
||||||
|
ret.resize(rng.randrange(V2Transport::MAX_GARBAGE_LEN + 1));
|
||||||
|
rng.fillrand(MakeWritableByteSpan(ret));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
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 :
|
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 :
|
||||||
m_cipher{key, ent32}, m_initiating{initiating}, m_nodeid{nodeid},
|
m_cipher{key, ent32}, m_initiating{initiating}, m_nodeid{nodeid},
|
||||||
m_v1_fallback{nodeid, type_in, version_in}, m_recv_type{type_in}, m_recv_version{version_in},
|
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
|
||||||
std::copy(garbage.begin(), garbage.end(), m_send_buffer.begin() + EllSwiftPubKey::size());
|
std::copy(garbage.begin(), garbage.end(), m_send_buffer.begin() + EllSwiftPubKey::size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in) noexcept :
|
||||||
|
V2Transport{nodeid, initiating, type_in, version_in, GenerateRandomKey(),
|
||||||
|
MakeByteSpan(GetRandHash()), GenerateRandomGarbage()} { }
|
||||||
|
|
||||||
void V2Transport::SetReceiveState(RecvState recv_state) noexcept
|
void V2Transport::SetReceiveState(RecvState recv_state) noexcept
|
||||||
{
|
{
|
||||||
AssertLockHeld(m_recv_mutex);
|
AssertLockHeld(m_recv_mutex);
|
||||||
|
|
|
@ -1008,6 +1008,14 @@ BOOST_AUTO_TEST_CASE(advertise_local_address)
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
CKey GenerateRandomTestKey() noexcept
|
||||||
|
{
|
||||||
|
CKey key;
|
||||||
|
uint256 key_data = InsecureRand256();
|
||||||
|
key.Set(key_data.begin(), key_data.end(), true);
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
/** A class for scenario-based tests of V2Transport
|
/** A class for scenario-based tests of V2Transport
|
||||||
*
|
*
|
||||||
* Each V2TransportTester encapsulates a V2Transport (the one being tested), and can be told to
|
* Each V2TransportTester encapsulates a V2Transport (the one being tested), and can be told to
|
||||||
|
@ -1031,6 +1039,7 @@ public:
|
||||||
/** Construct a tester object. test_initiator: whether the tested transport is initiator. */
|
/** Construct a tester object. test_initiator: whether the tested transport is initiator. */
|
||||||
V2TransportTester(bool test_initiator) :
|
V2TransportTester(bool test_initiator) :
|
||||||
m_transport(0, test_initiator, SER_NETWORK, INIT_PROTO_VERSION),
|
m_transport(0, test_initiator, SER_NETWORK, INIT_PROTO_VERSION),
|
||||||
|
m_cipher{GenerateRandomTestKey(), MakeByteSpan(InsecureRand256())},
|
||||||
m_test_initiator(test_initiator) {}
|
m_test_initiator(test_initiator) {}
|
||||||
|
|
||||||
/** Data type returned by Interact:
|
/** Data type returned by Interact:
|
||||||
|
|
Loading…
Add table
Reference in a new issue