Skip to content

Commit 0a729b0

Browse files
author
MarcoFalke
committed
Merge #18783: tests: Add fuzzing harness for MessageSign, MessageVerify and other functions in util/message.h
38e49de tests: Add fuzzing harness for MessageSign, MessageVerify and other functions in util/message.h (practicalswift) Pull request description: Add fuzzing harness for `MessageSign`, `MessageVerify` and other functions in `util/message.h`. See [`doc/fuzzing.md`](https://github.com/bitcoin/bitcoin/blob/master/doc/fuzzing.md) for information on how to fuzz Bitcoin Core. Don't forget to contribute any coverage increasing inputs you find to the [Bitcoin Core fuzzing corpus repo](https://github.com/bitcoin-core/qa-assets). Happy fuzzing :) ACKs for top commit: vasild: utACK 38e49de Tree-SHA512: 4f83718365d9c7e772a4ccecb31817bf17117efae2bfaf6e9618ff17908def0c8b97b5fa2504d51ab38b2e6f82c046178dd751495cc37ab4779c0b1ac1a4d211
2 parents 74a1152 + 38e49de commit 0a729b0

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ FUZZ_TARGETS = \
5353
test/fuzz/locale \
5454
test/fuzz/merkle_block_deserialize \
5555
test/fuzz/merkleblock \
56+
test/fuzz/message \
5657
test/fuzz/messageheader_deserialize \
5758
test/fuzz/multiplication_overflow \
5859
test/fuzz/net_permissions \
@@ -595,6 +596,12 @@ test_fuzz_merkleblock_LDADD = $(FUZZ_SUITE_LD_COMMON)
595596
test_fuzz_merkleblock_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
596597
test_fuzz_merkleblock_SOURCES = test/fuzz/merkleblock.cpp
597598

599+
test_fuzz_message_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
600+
test_fuzz_message_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
601+
test_fuzz_message_LDADD = $(FUZZ_SUITE_LD_COMMON)
602+
test_fuzz_message_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
603+
test_fuzz_message_SOURCES = test/fuzz/message.cpp
604+
598605
test_fuzz_messageheader_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DMESSAGEHEADER_DESERIALIZE=1
599606
test_fuzz_messageheader_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
600607
test_fuzz_messageheader_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/message.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <chainparams.h>
6+
#include <key_io.h>
7+
#include <optional.h>
8+
#include <test/fuzz/FuzzedDataProvider.h>
9+
#include <test/fuzz/fuzz.h>
10+
#include <test/fuzz/util.h>
11+
#include <util/message.h>
12+
#include <util/strencodings.h>
13+
14+
#include <cassert>
15+
#include <cstdint>
16+
#include <iostream>
17+
#include <string>
18+
#include <vector>
19+
20+
void initialize()
21+
{
22+
static const ECCVerifyHandle ecc_verify_handle;
23+
ECC_Start();
24+
SelectParams(CBaseChainParams::REGTEST);
25+
}
26+
27+
void test_one_input(const std::vector<uint8_t>& buffer)
28+
{
29+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
30+
const std::string random_message = fuzzed_data_provider.ConsumeRandomLengthString(1024);
31+
{
32+
const std::vector<uint8_t> random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
33+
CKey private_key;
34+
private_key.Set(random_bytes.begin(), random_bytes.end(), fuzzed_data_provider.ConsumeBool());
35+
std::string signature;
36+
const bool message_signed = MessageSign(private_key, random_message, signature);
37+
if (private_key.IsValid()) {
38+
assert(message_signed);
39+
const MessageVerificationResult verification_result = MessageVerify(EncodeDestination(PKHash(private_key.GetPubKey().GetID())), signature, random_message);
40+
assert(verification_result == MessageVerificationResult::OK);
41+
}
42+
}
43+
{
44+
(void)MessageHash(random_message);
45+
(void)MessageVerify(fuzzed_data_provider.ConsumeRandomLengthString(1024), fuzzed_data_provider.ConsumeRandomLengthString(1024), random_message);
46+
(void)SigningResultString(fuzzed_data_provider.PickValueInArray({SigningResult::OK, SigningResult::PRIVATE_KEY_NOT_AVAILABLE, SigningResult::SIGNING_FAILED}));
47+
}
48+
}

0 commit comments

Comments
 (0)