Bitcoin Core 28.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>
7#include <key_io.h>
9#include <test/fuzz/fuzz.h>
10#include <test/fuzz/util.h>
11#include <util/chaintype.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 static ECC_Context ecc_context{};
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 auto address = fuzzed_data_provider.ConsumeRandomLengthString(1024);
43 auto signature = fuzzed_data_provider.ConsumeRandomLengthString(1024);
44 (void)MessageVerify(address, signature, random_message);
46 }
47}
ECC_Context ecc_context
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
An encapsulated private key.
Definition: key.h:35
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:123
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:182
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:164
RAII class initializing and deinitializing global state for elliptic curve support.
Definition: key.h:322
std::string ConsumeRandomLengthString(size_t max_length)
T PickValueInArray(const T(&array)[size])
uint256 MessageHash(const std::string &message)
Hashes a message for signing and verification in a manner that prevents inadvertently signing a trans...
Definition: signmessage.cpp:73
bool MessageSign(const CKey &privkey, const std::string &message, std::string &signature)
Sign a message.
Definition: signmessage.cpp:57
std::string SigningResultString(const SigningResult res)
Definition: signmessage.cpp:81
MessageVerificationResult MessageVerify(const std::string &address, const std::string &signature, const std::string &message)
Verify a signed message.
Definition: signmessage.cpp:26
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:294
void initialize_message()
Definition: message.cpp:20
FUZZ_TARGET(message,.init=initialize_message)
Definition: message.cpp:26
@ PRIVATE_KEY_NOT_AVAILABLE
@ OK
No error.
MessageVerificationResult
The result of a signed message verification.
Definition: signmessage.h:23
@ OK
The message verification was successful.
CKey ConsumePrivateKey(FuzzedDataProvider &fuzzed_data_provider, std::optional< bool > compressed) noexcept
Definition: util.cpp:230
assert(!tx.IsCoinBase())