Bitcoin Core  26.1.0
P2P Digital Currency
message.cpp
Go to the documentation of this file.
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>
8 #include <test/fuzz/fuzz.h>
9 #include <test/fuzz/util.h>
10 #include <util/chaintype.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 
21 {
22  ECC_Start();
24 }
25 
27 {
28  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
29  const std::string random_message = fuzzed_data_provider.ConsumeRandomLengthString(1024);
30  {
31  CKey private_key = ConsumePrivateKey(fuzzed_data_provider);
32  std::string signature;
33  const bool message_signed = MessageSign(private_key, random_message, signature);
34  if (private_key.IsValid()) {
35  assert(message_signed);
36  const MessageVerificationResult verification_result = MessageVerify(EncodeDestination(PKHash(private_key.GetPubKey().GetID())), signature, random_message);
37  assert(verification_result == MessageVerificationResult::OK);
38  }
39  }
40  {
41  (void)MessageHash(random_message);
42  (void)MessageVerify(fuzzed_data_provider.ConsumeRandomLengthString(1024), fuzzed_data_provider.ConsumeRandomLengthString(1024), random_message);
43  (void)SigningResultString(fuzzed_data_provider.PickValueInArray({SigningResult::OK, SigningResult::PRIVATE_KEY_NOT_AVAILABLE, SigningResult::SIGNING_FAILED}));
44  }
45 }
assert(!tx.IsCoinBase())
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:188
MessageVerificationResult MessageVerify(const std::string &address, const std::string &signature, const std::string &message)
Verify a signed message.
Definition: message.cpp:26
bool MessageSign(const CKey &privkey, const std::string &message, std::string &signature)
Sign a message.
Definition: message.cpp:57
std::string SigningResultString(const SigningResult res)
Definition: message.cpp:81
The message verification was successful.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:164
MessageVerificationResult
The result of a signed message verification.
Definition: message.h:23
void initialize_message()
Definition: message.cpp:20
ECC_Start()
Definition: key.cpp:429
std::string ConsumeRandomLengthString(size_t max_length)
uint256 MessageHash(const std::string &message)
Hashes a message for signing and verification in a manner that prevents inadvertently signing a trans...
Definition: message.cpp:73
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:287
An encapsulated private key.
Definition: key.h:32
FUZZ_TARGET(message,.init=initialize_message)
Definition: message.cpp:26
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
CKey ConsumePrivateKey(FuzzedDataProvider &fuzzed_data_provider, std::optional< bool > compressed) noexcept
Definition: util.cpp:210
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:119