fuzz: introduce and use ConsumePrivateKey helper

This commit is contained in:
Sebastian Falbesoner 2023-09-05 22:38:45 +02:00
parent 9d3b216e00
commit 583af18fd1
7 changed files with 23 additions and 32 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -193,6 +193,16 @@ CTxDestination ConsumeTxDestination(FuzzedDataProvider& fuzzed_data_provider) no
return tx_destination; return tx_destination;
} }
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;
}
bool ContainsSpentInput(const CTransaction& tx, const CCoinsViewCache& inputs) noexcept bool ContainsSpentInput(const CTransaction& tx, const CCoinsViewCache& inputs) noexcept
{ {
for (const CTxIn& tx_in : tx.vin) { for (const CTxIn& tx_in : tx.vin) {

View File

@ -11,6 +11,7 @@
#include <compat/compat.h> #include <compat/compat.h>
#include <consensus/amount.h> #include <consensus/amount.h>
#include <consensus/consensus.h> #include <consensus/consensus.h>
#include <key.h>
#include <merkleblock.h> #include <merkleblock.h>
#include <primitives/transaction.h> #include <primitives/transaction.h>
#include <script/script.h> #include <script/script.h>
@ -165,6 +166,8 @@ template <typename WeakEnumType, size_t size>
[[nodiscard]] CTxDestination ConsumeTxDestination(FuzzedDataProvider& fuzzed_data_provider) noexcept; [[nodiscard]] CTxDestination ConsumeTxDestination(FuzzedDataProvider& fuzzed_data_provider) noexcept;
[[nodiscard]] CKey ConsumePrivateKey(FuzzedDataProvider& fuzzed_data_provider, std::optional<bool> compressed = std::nullopt) noexcept;
template <typename T> template <typename T>
[[nodiscard]] bool MultiplicationOverflow(const T i, const T j) noexcept [[nodiscard]] bool MultiplicationOverflow(const T i, const T j) noexcept
{ {