Bitcoin Core  22.99.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/message.h>
11 #include <util/strencodings.h>
12 
13 #include <cassert>
14 #include <cstdint>
15 #include <iostream>
16 #include <string>
17 #include <vector>
18 
20 {
21  static const ECCVerifyHandle ecc_verify_handle;
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  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);
46  }
47 }
MessageSign
bool MessageSign(const CKey &privkey, const std::string &message, std::string &signature)
Sign a message.
Definition: message.cpp:56
SigningResult::OK
@ OK
No error.
assert
assert(!tx.IsCoinBase())
key_io.h
CKey::Set
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:73
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(message, initialize_message)
Definition: message.cpp:26
util.h
FuzzedDataProvider::ConsumeRandomLengthString
std::string ConsumeRandomLengthString(size_t max_length)
Definition: FuzzedDataProvider.h:152
MessageVerify
MessageVerificationResult MessageVerify(const std::string &address, const std::string &signature, const std::string &message)
Verify a signed message.
Definition: message.cpp:24
FuzzedDataProvider::PickValueInArray
T PickValueInArray(const T(&array)[size])
Definition: FuzzedDataProvider.h:303
SigningResultString
std::string SigningResultString(const SigningResult res)
Definition: message.cpp:80
chainparams.h
strencodings.h
MessageVerificationResult
MessageVerificationResult
The result of a signed message verification.
Definition: message.h:22
initialize_message
void initialize_message()
Definition: message.cpp:19
message.h
SigningResult::PRIVATE_KEY_NOT_AVAILABLE
@ PRIVATE_KEY_NOT_AVAILABLE
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:25
FuzzedDataProvider.h
CKey::IsValid
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:92
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
SelectParams
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
Definition: chainparams.cpp:555
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
PKHash
Definition: standard.h:79
ECC_Start
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:370
ECCVerifyHandle
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:315
CKey
An encapsulated private key.
Definition: key.h:26
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
SigningResult::SIGNING_FAILED
@ SIGNING_FAILED
MessageVerificationResult::OK
@ OK
The message verification was successful.
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
EncodeDestination
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:256
CPubKey::GetID
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:160
MessageHash
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:72