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