merge bitcoin#28419: introduce and use ConsumePrivateKey helper

This commit is contained in:
Kittywhiskers Van Gogh 2023-09-05 22:38:45 +02:00
parent dccd395a4e
commit 3087275039
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
7 changed files with 23 additions and 32 deletions

View File

@ -26,19 +26,13 @@ FUZZ_TARGET_INIT(bip324_cipher_roundtrip, initialize_bip324)
// Load keys from fuzzer.
FuzzedDataProvider provider(buffer.data(), buffer.size());
// Initiator key
auto init_key_data = provider.ConsumeBytes<unsigned char>(32);
init_key_data.resize(32);
CKey init_key;
init_key.Set(init_key_data.begin(), init_key_data.end(), true);
CKey init_key = ConsumePrivateKey(provider, /*compressed=*/true);
if (!init_key.IsValid()) return;
// Initiator entropy
auto init_ent = provider.ConsumeBytes<std::byte>(32);
init_ent.resize(32);
// Responder key
auto resp_key_data = provider.ConsumeBytes<unsigned char>(32);
resp_key_data.resize(32);
CKey resp_key;
resp_key.Set(resp_key_data.begin(), resp_key_data.end(), true);
CKey resp_key = ConsumePrivateKey(provider, /*compressed=*/true);
if (!resp_key.IsValid()) return;
// Responder entropy
auto resp_ent = provider.ConsumeBytes<std::byte>(32);

View File

@ -15,6 +15,7 @@
#include <streams.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <util/strencodings.h>
#include <array>
@ -264,10 +265,7 @@ FUZZ_TARGET_INIT(ellswift_roundtrip, initialize_key)
{
FuzzedDataProvider fdp{buffer.data(), buffer.size()};
auto key_bytes = fdp.ConsumeBytes<uint8_t>(32);
key_bytes.resize(32);
CKey key;
key.Set(key_bytes.begin(), key_bytes.end(), true);
CKey key = ConsumePrivateKey(fdp, /*compressed=*/true);
if (!key.IsValid()) return;
auto ent32 = fdp.ConsumeBytes<std::byte>(32);
@ -284,17 +282,11 @@ FUZZ_TARGET_INIT(bip324_ecdh, initialize_key)
FuzzedDataProvider fdp{buffer.data(), buffer.size()};
// We generate private key, k1.
auto rnd32 = fdp.ConsumeBytes<uint8_t>(32);
rnd32.resize(32);
CKey k1;
k1.Set(rnd32.begin(), rnd32.end(), true);
CKey k1 = ConsumePrivateKey(fdp, /*compressed=*/true);
if (!k1.IsValid()) return;
// They generate private key, k2.
rnd32 = fdp.ConsumeBytes<uint8_t>(32);
rnd32.resize(32);
CKey k2;
k2.Set(rnd32.begin(), rnd32.end(), true);
CKey k2 = ConsumePrivateKey(fdp, /*compressed=*/true);
if (!k2.IsValid()) return;
// We construct an ellswift encoding for our key, k1_ellswift.

View File

@ -27,9 +27,7 @@ FUZZ_TARGET_INIT(message, initialize_message)
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
const std::string random_message = fuzzed_data_provider.ConsumeRandomLengthString(1024);
{
const std::vector<uint8_t> random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
CKey private_key;
private_key.Set(random_bytes.begin(), random_bytes.end(), fuzzed_data_provider.ConsumeBool());
CKey private_key = ConsumePrivateKey(fuzzed_data_provider);
std::string signature;
const bool message_signed = MessageSign(private_key, random_message, signature);
if (private_key.IsValid()) {

View File

@ -274,9 +274,7 @@ std::string ConsumeScalarRPCArgument(FuzzedDataProvider& fuzzed_data_provider)
},
[&] {
// base58 encoded key
const std::vector<uint8_t> random_bytes = fuzzed_data_provider.ConsumeBytes<uint8_t>(32);
CKey key;
key.Set(random_bytes.begin(), random_bytes.end(), fuzzed_data_provider.ConsumeBool());
CKey key = ConsumePrivateKey(fuzzed_data_provider);
if (!key.IsValid()) {
return;
}
@ -284,9 +282,7 @@ std::string ConsumeScalarRPCArgument(FuzzedDataProvider& fuzzed_data_provider)
},
[&] {
// hex encoded pubkey
const std::vector<uint8_t> random_bytes = fuzzed_data_provider.ConsumeBytes<uint8_t>(32);
CKey key;
key.Set(random_bytes.begin(), random_bytes.end(), fuzzed_data_provider.ConsumeBool());
CKey key = ConsumePrivateKey(fuzzed_data_provider);
if (!key.IsValid()) {
return;
}

View File

@ -78,9 +78,7 @@ FUZZ_TARGET_INIT(script_sign, initialize_script_sign)
}
FillableSigningProvider provider;
CKey k;
const std::vector<uint8_t> key_data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
k.Set(key_data.begin(), key_data.end(), fuzzed_data_provider.ConsumeBool());
CKey k = ConsumePrivateKey(fuzzed_data_provider);
if (k.IsValid()) {
provider.AddKey(k);
}

View File

@ -351,3 +351,13 @@ uint32_t ConsumeSequence(FuzzedDataProvider& fuzzed_data_provider) noexcept
}) :
fuzzed_data_provider.ConsumeIntegral<uint32_t>();
}
CKey ConsumePrivateKey(FuzzedDataProvider& fuzzed_data_provider, std::optional<bool> compressed) noexcept
{
auto key_data = fuzzed_data_provider.ConsumeBytes<uint8_t>(32);
key_data.resize(32);
CKey key;
bool compressed_value = compressed ? *compressed : fuzzed_data_provider.ConsumeBool();
key.Set(key_data.begin(), key_data.end(), compressed_value);
return key;
}

View File

@ -12,6 +12,7 @@
#include <coins.h>
#include <compat.h>
#include <consensus/consensus.h>
#include <key.h>
#include <merkleblock.h>
#include <net.h>
#include <netaddress.h>
@ -250,6 +251,8 @@ template <typename WeakEnumType, size_t size>
return tx_destination;
}
[[nodiscard]] CKey ConsumePrivateKey(FuzzedDataProvider& fuzzed_data_provider, std::optional<bool> compressed = std::nullopt) noexcept;
template <typename T>
[[nodiscard]] bool MultiplicationOverflow(const T i, const T j) noexcept
{