Bitcoin Core  22.99.0
P2P Digital Currency
signature_checker.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-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 <pubkey.h>
6 #include <script/interpreter.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 
18 {
19  static const auto verify_handle = std::make_unique<ECCVerifyHandle>();
20 }
21 
22 namespace {
23 class FuzzedSignatureChecker : public BaseSignatureChecker
24 {
25  FuzzedDataProvider& m_fuzzed_data_provider;
26 
27 public:
28  explicit FuzzedSignatureChecker(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider(fuzzed_data_provider)
29  {
30  }
31 
32  bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override
33  {
34  return m_fuzzed_data_provider.ConsumeBool();
35  }
36 
37  bool CheckSchnorrSignature(Span<const unsigned char> sig, Span<const unsigned char> pubkey, SigVersion sigversion, const ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override
38  {
39  return m_fuzzed_data_provider.ConsumeBool();
40  }
41 
42  bool CheckLockTime(const CScriptNum& nLockTime) const override
43  {
44  return m_fuzzed_data_provider.ConsumeBool();
45  }
46 
47  bool CheckSequence(const CScriptNum& nSequence) const override
48  {
49  return m_fuzzed_data_provider.ConsumeBool();
50  }
51 
52  virtual ~FuzzedSignatureChecker() {}
53 };
54 } // namespace
55 
57 {
58  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
59  const unsigned int flags = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
60  const SigVersion sig_version = fuzzed_data_provider.PickValueInArray({SigVersion::BASE, SigVersion::WITNESS_V0});
61  const auto script_1 = ConsumeScript(fuzzed_data_provider, 65536);
62  const auto script_2 = ConsumeScript(fuzzed_data_provider, 65536);
63  std::vector<std::vector<unsigned char>> stack;
64  (void)EvalScript(stack, script_1, flags, FuzzedSignatureChecker(fuzzed_data_provider), sig_version, nullptr);
66  return;
67  }
68  (void)VerifyScript(script_1, script_2, nullptr, flags, FuzzedSignatureChecker(fuzzed_data_provider), nullptr);
69 }
SigVersion
SigVersion
Definition: interpreter.h:187
SigVersion::BASE
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
flags
int flags
Definition: bitcoin-tx.cpp:513
IsValidFlagCombination
bool IsValidFlagCombination(unsigned flags)
Flags that are not forbidden by an assert in script validation.
Definition: script.cpp:8
BaseSignatureChecker::CheckECDSASignature
virtual bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
Definition: interpreter.h:241
util.h
ScriptExecutionData
Definition: interpreter.h:195
FuzzedDataProvider::PickValueInArray
T PickValueInArray(const T(&array)[size])
Definition: FuzzedDataProvider.h:303
interpreter.h
BaseSignatureChecker
Definition: interpreter.h:238
pubkey.h
VerifyScript
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
Definition: interpreter.cpp:1969
ScriptError
enum ScriptError_t ScriptError
ConsumeScript
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length, const bool maybe_p2wsh) noexcept
Definition: util.cpp:285
CScriptNum
Definition: script.h:219
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
script.h
FuzzedDataProvider.h
SigVersion::WITNESS_V0
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(signature_checker, initialize_signature_checker)
Definition: signature_checker.cpp:56
BaseSignatureChecker::CheckSchnorrSignature
virtual bool CheckSchnorrSignature(Span< const unsigned char > sig, Span< const unsigned char > pubkey, SigVersion sigversion, const ScriptExecutionData &execdata, ScriptError *serror=nullptr) const
Definition: interpreter.h:246
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
EvalScript
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror)
Definition: interpreter.cpp:431
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
BaseSignatureChecker::CheckSequence
virtual bool CheckSequence(const CScriptNum &nSequence) const
Definition: interpreter.h:256
initialize_signature_checker
void initialize_signature_checker()
Definition: signature_checker.cpp:17
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
BaseSignatureChecker::CheckLockTime
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
Definition: interpreter.h:251