Bitcoin Core 28.99.0
P2P Digital Currency
signature_checker.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2022 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 <pubkey.h>
8#include <test/fuzz/fuzz.h>
9#include <test/fuzz/util.h>
10#include <test/util/script.h>
11
12#include <cstdint>
13#include <limits>
14#include <string>
15#include <vector>
16
17namespace {
18class FuzzedSignatureChecker : public BaseSignatureChecker
19{
20 FuzzedDataProvider& m_fuzzed_data_provider;
21
22public:
23 explicit FuzzedSignatureChecker(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider(fuzzed_data_provider)
24 {
25 }
26
27 bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override
28 {
29 return m_fuzzed_data_provider.ConsumeBool();
30 }
31
32 bool CheckSchnorrSignature(Span<const unsigned char> sig, Span<const unsigned char> pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override
33 {
34 return m_fuzzed_data_provider.ConsumeBool();
35 }
36
37 bool CheckLockTime(const CScriptNum& nLockTime) const override
38 {
39 return m_fuzzed_data_provider.ConsumeBool();
40 }
41
42 bool CheckSequence(const CScriptNum& nSequence) const override
43 {
44 return m_fuzzed_data_provider.ConsumeBool();
45 }
46
47 virtual ~FuzzedSignatureChecker() = default;
48};
49} // namespace
50
51FUZZ_TARGET(signature_checker)
52{
53 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
54 const unsigned int flags = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
55 const SigVersion sig_version = fuzzed_data_provider.PickValueInArray({SigVersion::BASE, SigVersion::WITNESS_V0});
56 const auto script_1{ConsumeScript(fuzzed_data_provider)};
57 const auto script_2{ConsumeScript(fuzzed_data_provider)};
58 std::vector<std::vector<unsigned char>> stack;
59 (void)EvalScript(stack, script_1, flags, FuzzedSignatureChecker(fuzzed_data_provider), sig_version, nullptr);
61 return;
62 }
63 (void)VerifyScript(script_1, script_2, nullptr, flags, FuzzedSignatureChecker(fuzzed_data_provider), nullptr);
64}
int flags
Definition: bitcoin-tx.cpp:536
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
Definition: interpreter.h:258
virtual bool CheckSchnorrSignature(Span< const unsigned char > sig, Span< const unsigned char > pubkey, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror=nullptr) const
Definition: interpreter.h:253
virtual bool CheckSequence(const CScriptNum &nSequence) const
Definition: interpreter.h:263
virtual bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
Definition: interpreter.h:248
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
T PickValueInArray(const T(&array)[size])
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
SigVersion
Definition: interpreter.h:191
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
enum ScriptError_t ScriptError
FUZZ_TARGET(signature_checker)
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) noexcept
Definition: util.cpp:93
bool IsValidFlagCombination(unsigned flags)
Flags that are not forbidden by an assert in script validation.
Definition: script.cpp:8