Branch data Line data Source code
1 : : // Copyright (c) 2020-present 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.h>
7 : : #include <key_io.h>
8 : : #include <outputtype.h>
9 : : #include <policy/policy.h>
10 : : #include <pubkey.h>
11 : : #include <rpc/util.h>
12 : : #include <script/keyorigin.h>
13 : : #include <script/script.h>
14 : : #include <script/sign.h>
15 : : #include <script/signingprovider.h>
16 : : #include <script/solver.h>
17 : : #include <streams.h>
18 : : #include <test/fuzz/FuzzedDataProvider.h>
19 : : #include <test/fuzz/fuzz.h>
20 : : #include <test/fuzz/util.h>
21 : : #include <test/util/random.h>
22 : : #include <util/chaintype.h>
23 : : #include <util/strencodings.h>
24 : :
25 : : #include <array>
26 : : #include <cassert>
27 : : #include <cstddef>
28 : : #include <cstdint>
29 : : #include <numeric>
30 : : #include <optional>
31 : : #include <string>
32 : : #include <vector>
33 : :
34 : 3 : void initialize_key()
35 : : {
36 [ + - + - : 3 : static ECC_Context ecc_context{};
+ - ]
37 : 3 : SelectParams(ChainType::REGTEST);
38 : 3 : }
39 : :
40 [ + - ]: 1617 : FUZZ_TARGET(key, .init = initialize_key)
41 : : {
42 : 1175 : SeedRandomStateForTest(SeedRand::ZEROS);
43 : 2350 : const CKey key = [&] {
44 : 1175 : CKey k;
45 [ + - ]: 1175 : k.Set(buffer.begin(), buffer.end(), true);
46 : 1175 : return k;
47 : 1175 : }();
48 [ + + ]: 1175 : if (!key.IsValid()) {
49 : 4 : return;
50 : : }
51 : :
52 : 1171 : {
53 [ - + ]: 1171 : assert(key.begin() + key.size() == key.end());
54 [ - + ]: 1171 : assert(key.IsCompressed());
55 [ + - ]: 1171 : assert(key.size() == 32);
56 [ + - + - : 2342 : assert(DecodeSecret(EncodeSecret(key)) == key);
- + ]
57 : : }
58 : :
59 : 1171 : {
60 : 1171 : CKey invalid_key;
61 [ - + ]: 1171 : assert(!(invalid_key == key));
62 : 1171 : assert(!invalid_key.IsCompressed());
63 : 1171 : assert(!invalid_key.IsValid());
64 : 1171 : assert(invalid_key.size() == 0);
65 : 1171 : }
66 : :
67 : 1171 : {
68 : 1171 : CKey uncompressed_key;
69 [ + - ]: 1171 : uncompressed_key.Set(buffer.begin(), buffer.end(), false);
70 [ - + ]: 1171 : assert(!(uncompressed_key == key));
71 [ - + ]: 1171 : assert(!uncompressed_key.IsCompressed());
72 [ - + ]: 1171 : assert(key.size() == 32);
73 [ + - - + : 3513 : assert(uncompressed_key.begin() + uncompressed_key.size() == uncompressed_key.end());
+ - - + ]
74 [ - + ]: 1171 : assert(uncompressed_key.IsValid());
75 : 0 : }
76 : :
77 : 1171 : {
78 : 1171 : CKey copied_key;
79 [ + - + - : 3513 : copied_key.Set(key.begin(), key.end(), key.IsCompressed());
+ - ]
80 [ - + ]: 1171 : assert(copied_key == key);
81 : 0 : }
82 : :
83 [ + - ]: 1171 : const uint256 random_uint256 = Hash(buffer);
84 : :
85 : 1171 : {
86 : 1171 : CKey child_key;
87 : 1171 : ChainCode child_chaincode;
88 [ + - ]: 1171 : const bool ok = key.Derive(child_key, child_chaincode, 0, random_uint256);
89 [ - + ]: 1171 : assert(ok);
90 [ - + ]: 1171 : assert(child_key.IsValid());
91 [ - + ]: 1171 : assert(!(child_key == key));
92 [ - + ]: 1171 : assert(child_chaincode != random_uint256);
93 : 0 : }
94 : :
95 [ + - ]: 1171 : const CPubKey pubkey = key.GetPubKey();
96 : :
97 : 1171 : {
98 [ - + ]: 1171 : assert(pubkey.size() == 33);
99 [ + - - + ]: 1171 : assert(key.VerifyPubKey(pubkey));
100 [ + - - + ]: 1171 : assert(pubkey.GetHash() != random_uint256);
101 [ - + ]: 1171 : assert(pubkey.begin() + pubkey.size() == pubkey.end());
102 : 1171 : assert(pubkey.data() == pubkey.begin());
103 [ - + ]: 1171 : assert(pubkey.IsCompressed());
104 [ - + ]: 1171 : assert(pubkey.IsValid());
105 [ + - - + ]: 1171 : assert(pubkey.IsFullyValid());
106 [ + - + - : 1171 : assert(HexToPubKey(HexStr(pubkey)) == pubkey);
- + ]
107 : : }
108 : :
109 : 1171 : {
110 : 1171 : DataStream data_stream{};
111 [ + - ]: 1171 : pubkey.Serialize(data_stream);
112 : :
113 [ + - ]: 1171 : CPubKey pubkey_deserialized;
114 [ + - ]: 1171 : pubkey_deserialized.Unserialize(data_stream);
115 [ - + ]: 1171 : assert(pubkey_deserialized == pubkey);
116 : 0 : }
117 : :
118 : 1171 : {
119 [ + - ]: 1171 : const CScript tx_pubkey_script = GetScriptForRawPubKey(pubkey);
120 [ + - - + ]: 1171 : assert(!tx_pubkey_script.IsPayToScriptHash());
121 [ + - - + ]: 1171 : assert(!tx_pubkey_script.IsPayToWitnessScriptHash());
122 [ + - - + ]: 1171 : assert(!tx_pubkey_script.IsPushOnly());
123 [ - + ]: 1171 : assert(!tx_pubkey_script.IsUnspendable());
124 [ + - - + ]: 1171 : assert(tx_pubkey_script.HasValidOps());
125 [ + - - + ]: 1171 : assert(tx_pubkey_script.size() == 35);
126 : :
127 [ + - + - : 2342 : const CScript tx_multisig_script = GetScriptForMultisig(1, {pubkey});
+ - ]
128 [ + - - + ]: 1171 : assert(!tx_multisig_script.IsPayToScriptHash());
129 [ + - - + ]: 1171 : assert(!tx_multisig_script.IsPayToWitnessScriptHash());
130 [ + - - + ]: 1171 : assert(!tx_multisig_script.IsPushOnly());
131 [ - + ]: 1171 : assert(!tx_multisig_script.IsUnspendable());
132 [ + - - + ]: 1171 : assert(tx_multisig_script.HasValidOps());
133 [ + - - + ]: 1171 : assert(tx_multisig_script.size() == 37);
134 : :
135 : 1171 : FillableSigningProvider fillable_signing_provider;
136 [ + - - + ]: 1171 : assert(!IsSegWitOutput(fillable_signing_provider, tx_pubkey_script));
137 [ + - - + ]: 1171 : assert(!IsSegWitOutput(fillable_signing_provider, tx_multisig_script));
138 [ + - - + ]: 1171 : assert(fillable_signing_provider.GetKeys().size() == 0);
139 [ + - + - : 1171 : assert(!fillable_signing_provider.HaveKey(pubkey.GetID()));
- + ]
140 : :
141 [ + - ]: 1171 : const bool ok_add_key = fillable_signing_provider.AddKey(key);
142 [ - + ]: 1171 : assert(ok_add_key);
143 [ + - + - : 1171 : assert(fillable_signing_provider.HaveKey(pubkey.GetID()));
- + ]
144 : :
145 : 1171 : FillableSigningProvider fillable_signing_provider_pub;
146 [ + - + - : 1171 : assert(!fillable_signing_provider_pub.HaveKey(pubkey.GetID()));
- + ]
147 : :
148 [ + - ]: 1171 : const bool ok_add_key_pubkey = fillable_signing_provider_pub.AddKeyPubKey(key, pubkey);
149 [ - + ]: 1171 : assert(ok_add_key_pubkey);
150 [ + - + - : 1171 : assert(fillable_signing_provider_pub.HaveKey(pubkey.GetID()));
- + ]
151 : :
152 : 1171 : TxoutType which_type_tx_pubkey;
153 [ + - ]: 1171 : const bool is_standard_tx_pubkey = IsStandard(tx_pubkey_script, std::nullopt, which_type_tx_pubkey);
154 [ - + ]: 1171 : assert(is_standard_tx_pubkey);
155 [ - + ]: 1171 : assert(which_type_tx_pubkey == TxoutType::PUBKEY);
156 : :
157 : 1171 : TxoutType which_type_tx_multisig;
158 [ + - ]: 1171 : const bool is_standard_tx_multisig = IsStandard(tx_multisig_script, std::nullopt, which_type_tx_multisig);
159 [ - + ]: 1171 : assert(is_standard_tx_multisig);
160 [ - + ]: 1171 : assert(which_type_tx_multisig == TxoutType::MULTISIG);
161 : :
162 : 1171 : std::vector<std::vector<unsigned char>> v_solutions_ret_tx_pubkey;
163 [ + - ]: 1171 : const TxoutType outtype_tx_pubkey = Solver(tx_pubkey_script, v_solutions_ret_tx_pubkey);
164 [ - + ]: 1171 : assert(outtype_tx_pubkey == TxoutType::PUBKEY);
165 [ - + ]: 1171 : assert(v_solutions_ret_tx_pubkey.size() == 1);
166 [ - + ]: 1171 : assert(v_solutions_ret_tx_pubkey[0].size() == 33);
167 : :
168 : 1171 : std::vector<std::vector<unsigned char>> v_solutions_ret_tx_multisig;
169 [ + - ]: 1171 : const TxoutType outtype_tx_multisig = Solver(tx_multisig_script, v_solutions_ret_tx_multisig);
170 [ - + ]: 1171 : assert(outtype_tx_multisig == TxoutType::MULTISIG);
171 [ - + ]: 1171 : assert(v_solutions_ret_tx_multisig.size() == 3);
172 [ - + ]: 1171 : assert(v_solutions_ret_tx_multisig[0].size() == 1);
173 [ - + ]: 1171 : assert(v_solutions_ret_tx_multisig[1].size() == 33);
174 [ - + ]: 1171 : assert(v_solutions_ret_tx_multisig[2].size() == 1);
175 : :
176 : 1171 : OutputType output_type{};
177 [ + - ]: 1171 : const CTxDestination tx_destination{PKHash{pubkey}};
178 : 1171 : assert(output_type == OutputType::LEGACY);
179 [ + - - + ]: 1171 : assert(IsValidDestination(tx_destination));
180 [ + - + - : 2342 : assert(PKHash{pubkey} == *std::get_if<PKHash>(&tx_destination));
- + ]
181 : :
182 [ + - ]: 1171 : const CScript script_for_destination = GetScriptForDestination(tx_destination);
183 [ - + - + ]: 1171 : assert(script_for_destination.size() == 25);
184 : :
185 [ + - ]: 1171 : const std::string destination_address = EncodeDestination(tx_destination);
186 [ + - - + ]: 2342 : assert(DecodeDestination(destination_address) == tx_destination);
187 : :
188 [ + - ]: 1171 : CKeyID key_id = pubkey.GetID();
189 [ - + ]: 1171 : assert(!key_id.IsNull());
190 [ - + ]: 1171 : assert(key_id == CKeyID{key_id});
191 [ + - - + ]: 1171 : assert(key_id == GetKeyForDestination(fillable_signing_provider, tx_destination));
192 : :
193 [ + - ]: 1171 : CPubKey pubkey_out;
194 [ + - ]: 1171 : const bool ok_get_pubkey = fillable_signing_provider.GetPubKey(key_id, pubkey_out);
195 [ - + ]: 1171 : assert(ok_get_pubkey);
196 : :
197 : 1171 : CKey key_out;
198 [ + - ]: 1171 : const bool ok_get_key = fillable_signing_provider.GetKey(key_id, key_out);
199 [ - + ]: 1171 : assert(ok_get_key);
200 [ + - - + ]: 1171 : assert(fillable_signing_provider.GetKeys().size() == 1);
201 [ + - - + ]: 1171 : assert(fillable_signing_provider.HaveKey(key_id));
202 : :
203 : 1171 : KeyOriginInfo key_origin_info;
204 : 1171 : const bool ok_get_key_origin = fillable_signing_provider.GetKeyOrigin(key_id, key_origin_info);
205 : 1171 : assert(!ok_get_key_origin);
206 : 1171 : }
207 : :
208 : 1171 : {
209 [ + - ]: 1171 : const std::vector<unsigned char> vch_pubkey{pubkey.begin(), pubkey.end()};
210 [ - + ]: 1171 : assert(CPubKey::ValidSize(vch_pubkey));
211 [ + - - + ]: 2342 : assert(!CPubKey::ValidSize({pubkey.begin(), pubkey.begin() + pubkey.size() - 1}));
212 : :
213 : 2342 : const CPubKey pubkey_ctor_1{vch_pubkey};
214 [ - + ]: 1171 : assert(pubkey == pubkey_ctor_1);
215 : :
216 : 1171 : const CPubKey pubkey_ctor_2{vch_pubkey.begin(), vch_pubkey.end()};
217 [ - + ]: 1171 : assert(pubkey == pubkey_ctor_2);
218 : :
219 : 1171 : CPubKey pubkey_set;
220 : 1171 : pubkey_set.Set(vch_pubkey.begin(), vch_pubkey.end());
221 [ - + ]: 1171 : assert(pubkey == pubkey_set);
222 : 0 : }
223 : :
224 : 1171 : {
225 [ - + ]: 1171 : const CPubKey invalid_pubkey{};
226 [ - + ]: 1171 : assert(!invalid_pubkey.IsValid());
227 [ + - - + ]: 1171 : assert(!invalid_pubkey.IsFullyValid());
228 [ - + ]: 1171 : assert(!(pubkey == invalid_pubkey));
229 : 1171 : assert(pubkey != invalid_pubkey);
230 [ - + ]: 1171 : assert(pubkey < invalid_pubkey);
231 : : }
232 : :
233 : : {
234 : : // Cover CPubKey's operator[](unsigned int pos)
235 : : unsigned int sum = 0;
236 [ + + ]: 39814 : for (size_t i = 0; i < pubkey.size(); ++i) {
237 : 38643 : sum += pubkey[i];
238 : : }
239 [ - + ]: 2342 : assert(std::accumulate(pubkey.begin(), pubkey.end(), 0U) == sum);
240 : : }
241 : :
242 : 1171 : {
243 : 1171 : CPubKey decompressed_pubkey = pubkey;
244 [ - + ]: 1171 : assert(decompressed_pubkey.IsCompressed());
245 : :
246 [ + - ]: 1171 : const bool ok = decompressed_pubkey.Decompress();
247 [ - + ]: 1171 : assert(ok);
248 [ - + ]: 1171 : assert(!decompressed_pubkey.IsCompressed());
249 [ - + ]: 1171 : assert(decompressed_pubkey.size() == 65);
250 : : }
251 : :
252 : 1171 : {
253 : 1171 : std::vector<unsigned char> vch_sig;
254 [ + - ]: 1171 : const bool ok = key.Sign(random_uint256, vch_sig, false);
255 [ - + ]: 1171 : assert(ok);
256 [ + - - + ]: 1171 : assert(pubkey.Verify(random_uint256, vch_sig));
257 [ + - - + ]: 1171 : assert(CPubKey::CheckLowS(vch_sig));
258 : :
259 [ + - ]: 1171 : const std::vector<unsigned char> vch_invalid_sig{vch_sig.begin(), vch_sig.begin() + vch_sig.size() - 1};
260 [ + - - + ]: 1171 : assert(!pubkey.Verify(random_uint256, vch_invalid_sig));
261 [ + - - + ]: 1171 : assert(!CPubKey::CheckLowS(vch_invalid_sig));
262 : 1171 : }
263 : :
264 : 1171 : {
265 : 1171 : std::vector<unsigned char> vch_compact_sig;
266 [ + - ]: 1171 : const bool ok_sign_compact = key.SignCompact(random_uint256, vch_compact_sig);
267 [ - + ]: 1171 : assert(ok_sign_compact);
268 : :
269 [ + - ]: 1171 : CPubKey recover_pubkey;
270 [ + - ]: 1171 : const bool ok_recover_compact = recover_pubkey.RecoverCompact(random_uint256, vch_compact_sig);
271 [ - + ]: 1171 : assert(ok_recover_compact);
272 [ - + ]: 1171 : assert(recover_pubkey == pubkey);
273 : 0 : }
274 : :
275 : 1171 : {
276 [ + - ]: 1171 : CPubKey child_pubkey;
277 : 1171 : ChainCode child_chaincode;
278 [ + - ]: 1171 : const bool ok = pubkey.Derive(child_pubkey, child_chaincode, 0, random_uint256);
279 [ - + ]: 1171 : assert(ok);
280 [ - + ]: 1171 : assert(child_pubkey != pubkey);
281 [ - + ]: 1171 : assert(child_pubkey.IsCompressed());
282 [ + - - + ]: 1171 : assert(child_pubkey.IsFullyValid());
283 [ - + ]: 1171 : assert(child_pubkey.IsValid());
284 [ - + ]: 1171 : assert(child_pubkey.size() == 33);
285 [ - + ]: 1171 : assert(child_chaincode != random_uint256);
286 : : }
287 : :
288 [ + - ]: 1171 : const CPrivKey priv_key = key.GetPrivKey();
289 : :
290 : 1171 : {
291 [ + + ]: 3513 : for (const bool skip_check : {true, false}) {
292 : 2342 : CKey loaded_key;
293 [ + - ]: 2342 : const bool ok = loaded_key.Load(priv_key, pubkey, skip_check);
294 [ - + ]: 2342 : assert(ok);
295 [ - + ]: 2342 : assert(key == loaded_key);
296 : 2342 : }
297 : : }
298 : 1175 : }
299 : :
300 [ + - ]: 2202 : FUZZ_TARGET(ellswift_roundtrip, .init = initialize_key)
301 : : {
302 : 1760 : FuzzedDataProvider fdp{buffer.data(), buffer.size()};
303 : :
304 : 1760 : CKey key = ConsumePrivateKey(fdp, /*compressed=*/true);
305 [ + + ]: 1760 : if (!key.IsValid()) return;
306 : :
307 [ + - ]: 1757 : auto ent32 = fdp.ConsumeBytes<std::byte>(32);
308 [ + - ]: 1757 : ent32.resize(32);
309 : :
310 [ + - ]: 1757 : auto encoded_ellswift = key.EllSwiftCreate(ent32);
311 [ + - ]: 1757 : auto decoded_pubkey = encoded_ellswift.Decode();
312 : :
313 : 1757 : uint256 hash{ConsumeUInt256(fdp)};
314 : 1757 : std::vector<unsigned char> sig;
315 [ + - ]: 1757 : key.Sign(hash, sig);
316 [ + - - + ]: 1757 : assert(decoded_pubkey.Verify(hash, sig));
317 : 1760 : }
318 : :
319 [ + - ]: 1956 : FUZZ_TARGET(bip324_ecdh, .init = initialize_key)
320 : : {
321 : 1514 : FuzzedDataProvider fdp{buffer.data(), buffer.size()};
322 : :
323 : : // We generate private key, k1.
324 : 1514 : CKey k1 = ConsumePrivateKey(fdp, /*compressed=*/true);
325 [ + + ]: 1514 : if (!k1.IsValid()) return;
326 : :
327 : : // They generate private key, k2.
328 : 1513 : CKey k2 = ConsumePrivateKey(fdp, /*compressed=*/true);
329 [ + + ]: 1513 : if (!k2.IsValid()) return;
330 : :
331 : : // We construct an ellswift encoding for our key, k1_ellswift.
332 [ + - ]: 1510 : auto ent32_1 = fdp.ConsumeBytes<std::byte>(32);
333 [ + - ]: 1510 : ent32_1.resize(32);
334 [ + - ]: 1510 : auto k1_ellswift = k1.EllSwiftCreate(ent32_1);
335 : :
336 : : // They construct an ellswift encoding for their key, k2_ellswift.
337 [ + - ]: 1510 : auto ent32_2 = fdp.ConsumeBytes<std::byte>(32);
338 [ + - ]: 1510 : ent32_2.resize(32);
339 [ + - ]: 1510 : auto k2_ellswift = k2.EllSwiftCreate(ent32_2);
340 : :
341 : : // They construct another (possibly distinct) ellswift encoding for their key, k2_ellswift_bad.
342 [ + - ]: 1510 : auto ent32_2_bad = fdp.ConsumeBytes<std::byte>(32);
343 [ + - ]: 1510 : ent32_2_bad.resize(32);
344 [ + - ]: 1510 : auto k2_ellswift_bad = k2.EllSwiftCreate(ent32_2_bad);
345 [ - + ]: 3020 : assert((ent32_2_bad == ent32_2) == (k2_ellswift_bad == k2_ellswift));
346 : :
347 : : // Determine who is who.
348 : 1510 : bool initiating = fdp.ConsumeBool();
349 : :
350 : : // We compute our shared secret using our key and their public key.
351 [ + - ]: 1510 : auto ecdh_secret_1 = k1.ComputeBIP324ECDHSecret(k2_ellswift, k1_ellswift, initiating);
352 : : // They compute their shared secret using their key and our public key.
353 [ + - ]: 1510 : auto ecdh_secret_2 = k2.ComputeBIP324ECDHSecret(k1_ellswift, k2_ellswift, !initiating);
354 : : // Those must match, as everyone is behaving correctly.
355 [ - + ]: 1510 : assert(ecdh_secret_1 == ecdh_secret_2);
356 : :
357 [ + + ]: 1510 : if (k1_ellswift != k2_ellswift) {
358 : : // Unless the two keys are exactly identical, acting as the wrong party breaks things.
359 [ + - ]: 1499 : auto ecdh_secret_bad = k1.ComputeBIP324ECDHSecret(k2_ellswift, k1_ellswift, !initiating);
360 [ - + ]: 1499 : assert(ecdh_secret_bad != ecdh_secret_1);
361 : : }
362 : :
363 [ + + ]: 3020 : if (k2_ellswift_bad != k2_ellswift) {
364 : : // Unless both encodings created by them are identical, using the second one breaks things.
365 [ + - ]: 510 : auto ecdh_secret_bad = k1.ComputeBIP324ECDHSecret(k2_ellswift_bad, k1_ellswift, initiating);
366 [ - + ]: 510 : assert(ecdh_secret_bad != ecdh_secret_1);
367 : : }
368 : 1517 : }
|