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 <test/fuzz/FuzzedDataProvider.h>
8 #include <test/fuzz/fuzz.h>
9 #include <test/fuzz/util.h>
10 #include <util/message.h>
11 #include <util/strencodings.h>
12 
13 #include <cassert>
14 #include <cstdint>
15 #include <iostream>
16 #include <string>
17 #include <vector>
18 
initialize()19 void initialize()
20 {
21     static const ECCVerifyHandle ecc_verify_handle;
22     ECC_Start();
23     SelectParams(CBaseChainParams::REGTEST);
24 }
25 
test_one_input(const std::vector<uint8_t> & buffer)26 void test_one_input(const std::vector<uint8_t>& buffer)
27 {
28     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
29     const std::string random_message = fuzzed_data_provider.ConsumeRandomLengthString(1024);
30     {
31         const std::vector<uint8_t> random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
32         CKey private_key;
33         private_key.Set(random_bytes.begin(), random_bytes.end(), fuzzed_data_provider.ConsumeBool());
34         std::string signature;
35         const bool message_signed = MessageSign(private_key, random_message, signature);
36         if (private_key.IsValid()) {
37             assert(message_signed);
38             const MessageVerificationResult verification_result = MessageVerify(EncodeDestination(PKHash(private_key.GetPubKey().GetID())), signature, random_message);
39             assert(verification_result == MessageVerificationResult::OK);
40         }
41     }
42     {
43         (void)MessageHash(random_message);
44         (void)MessageVerify(fuzzed_data_provider.ConsumeRandomLengthString(1024), fuzzed_data_provider.ConsumeRandomLengthString(1024), random_message);
45         (void)SigningResultString(fuzzed_data_provider.PickValueInArray({SigningResult::OK, SigningResult::PRIVATE_KEY_NOT_AVAILABLE, SigningResult::SIGNING_FAILED}));
46     }
47 }
48