Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2021 The Bitcoin Core developers
# 2 : : // Copyright (c) 2017 The Zcash developers
# 3 : : // Distributed under the MIT software license, see the accompanying
# 4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 5 : :
# 6 : : #include <pubkey.h>
# 7 : :
# 8 : : #include <hash.h>
# 9 : : #include <secp256k1.h>
# 10 : : #include <secp256k1_extrakeys.h>
# 11 : : #include <secp256k1_recovery.h>
# 12 : : #include <secp256k1_schnorrsig.h>
# 13 : : #include <span.h>
# 14 : : #include <uint256.h>
# 15 : :
# 16 : : #include <algorithm>
# 17 : : #include <cassert>
# 18 : :
# 19 : : namespace
# 20 : : {
# 21 : : /* Global secp256k1_context object used for verification. */
# 22 : : secp256k1_context* secp256k1_context_verify = nullptr;
# 23 : : } // namespace
# 24 : :
# 25 : : /** This function is taken from the libsecp256k1 distribution and implements
# 26 : : * DER parsing for ECDSA signatures, while supporting an arbitrary subset of
# 27 : : * format violations.
# 28 : : *
# 29 : : * Supported violations include negative integers, excessive padding, garbage
# 30 : : * at the end, and overly long length descriptors. This is safe to use in
# 31 : : * Bitcoin because since the activation of BIP66, signatures are verified to be
# 32 : : * strict DER before being passed to this module, and we know it supports all
# 33 : : * violations present in the blockchain before that point.
# 34 : : */
# 35 : 1442376 : int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
# 36 : 1442376 : size_t rpos, rlen, spos, slen;
# 37 : 1442376 : size_t pos = 0;
# 38 : 1442376 : size_t lenbyte;
# 39 : 1442376 : unsigned char tmpsig[64] = {0};
# 40 : 1442376 : int overflow = 0;
# 41 : :
# 42 : : /* Hack to initialize sig with a correctly-parsed but invalid signature. */
# 43 : 1442376 : secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
# 44 : :
# 45 : : /* Sequence tag byte */
# 46 [ + + ][ + + ]: 1442426 : if (pos == inputlen || input[pos] != 0x30) {
# 47 : 92 : return 0;
# 48 : 92 : }
# 49 : 1442284 : pos++;
# 50 : :
# 51 : : /* Sequence length bytes */
# 52 [ - + ]: 1442284 : if (pos == inputlen) {
# 53 : 0 : return 0;
# 54 : 0 : }
# 55 : 1442284 : lenbyte = input[pos++];
# 56 [ - + ]: 1442284 : if (lenbyte & 0x80) {
# 57 : 0 : lenbyte -= 0x80;
# 58 [ # # ]: 0 : if (lenbyte > inputlen - pos) {
# 59 : 0 : return 0;
# 60 : 0 : }
# 61 : 0 : pos += lenbyte;
# 62 : 0 : }
# 63 : :
# 64 : : /* Integer tag byte for R */
# 65 [ + + ][ + + ]: 1442494 : if (pos == inputlen || input[pos] != 0x02) {
# 66 : 0 : return 0;
# 67 : 0 : }
# 68 : 1442284 : pos++;
# 69 : :
# 70 : : /* Integer length for R */
# 71 [ - + ]: 1442284 : if (pos == inputlen) {
# 72 : 0 : return 0;
# 73 : 0 : }
# 74 : 1442284 : lenbyte = input[pos++];
# 75 [ - + ]: 1442284 : if (lenbyte & 0x80) {
# 76 : 0 : lenbyte -= 0x80;
# 77 [ # # ]: 0 : if (lenbyte > inputlen - pos) {
# 78 : 0 : return 0;
# 79 : 0 : }
# 80 [ # # ][ # # ]: 0 : while (lenbyte > 0 && input[pos] == 0) {
# 81 : 0 : pos++;
# 82 : 0 : lenbyte--;
# 83 : 0 : }
# 84 : 0 : static_assert(sizeof(size_t) >= 4, "size_t too small");
# 85 [ # # ]: 0 : if (lenbyte >= 4) {
# 86 : 0 : return 0;
# 87 : 0 : }
# 88 : 0 : rlen = 0;
# 89 [ # # ]: 0 : while (lenbyte > 0) {
# 90 : 0 : rlen = (rlen << 8) + input[pos];
# 91 : 0 : pos++;
# 92 : 0 : lenbyte--;
# 93 : 0 : }
# 94 : 1442284 : } else {
# 95 : 1442284 : rlen = lenbyte;
# 96 : 1442284 : }
# 97 [ - + ]: 1442284 : if (rlen > inputlen - pos) {
# 98 : 0 : return 0;
# 99 : 0 : }
# 100 : 1442284 : rpos = pos;
# 101 : 1442284 : pos += rlen;
# 102 : :
# 103 : : /* Integer tag byte for S */
# 104 [ + + ][ + + ]: 1442389 : if (pos == inputlen || input[pos] != 0x02) {
# 105 : 0 : return 0;
# 106 : 0 : }
# 107 : 1442284 : pos++;
# 108 : :
# 109 : : /* Integer length for S */
# 110 [ - + ]: 1442284 : if (pos == inputlen) {
# 111 : 0 : return 0;
# 112 : 0 : }
# 113 : 1442284 : lenbyte = input[pos++];
# 114 [ - + ]: 1442284 : if (lenbyte & 0x80) {
# 115 : 0 : lenbyte -= 0x80;
# 116 [ # # ]: 0 : if (lenbyte > inputlen - pos) {
# 117 : 0 : return 0;
# 118 : 0 : }
# 119 [ # # ][ # # ]: 0 : while (lenbyte > 0 && input[pos] == 0) {
# 120 : 0 : pos++;
# 121 : 0 : lenbyte--;
# 122 : 0 : }
# 123 : 0 : static_assert(sizeof(size_t) >= 4, "size_t too small");
# 124 [ # # ]: 0 : if (lenbyte >= 4) {
# 125 : 0 : return 0;
# 126 : 0 : }
# 127 : 0 : slen = 0;
# 128 [ # # ]: 0 : while (lenbyte > 0) {
# 129 : 0 : slen = (slen << 8) + input[pos];
# 130 : 0 : pos++;
# 131 : 0 : lenbyte--;
# 132 : 0 : }
# 133 : 1442284 : } else {
# 134 : 1442284 : slen = lenbyte;
# 135 : 1442284 : }
# 136 [ - + ]: 1442284 : if (slen > inputlen - pos) {
# 137 : 0 : return 0;
# 138 : 0 : }
# 139 : 1442284 : spos = pos;
# 140 : :
# 141 : : /* Ignore leading zeroes in R */
# 142 [ + + ][ + + ]: 1475314 : while (rlen > 0 && input[rpos] == 0) {
# 143 : 32963 : rlen--;
# 144 : 32963 : rpos++;
# 145 : 32963 : }
# 146 : : /* Copy R value */
# 147 [ + + ]: 1442284 : if (rlen > 32) {
# 148 : 1467 : overflow = 1;
# 149 : 1440817 : } else {
# 150 : 1440817 : memcpy(tmpsig + 32 - rlen, input + rpos, rlen);
# 151 : 1440817 : }
# 152 : :
# 153 : : /* Ignore leading zeroes in S */
# 154 [ + + ][ + + ]: 1446493 : while (slen > 0 && input[spos] == 0) {
# 155 : 4122 : slen--;
# 156 : 4122 : spos++;
# 157 : 4122 : }
# 158 : : /* Copy S value */
# 159 [ - + ]: 1442284 : if (slen > 32) {
# 160 : 0 : overflow = 1;
# 161 : 1442284 : } else {
# 162 : 1442284 : memcpy(tmpsig + 64 - slen, input + spos, slen);
# 163 : 1442284 : }
# 164 : :
# 165 [ + + ]: 1442284 : if (!overflow) {
# 166 : 1440908 : overflow = !secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
# 167 : 1440908 : }
# 168 [ + + ]: 1442284 : if (overflow) {
# 169 : : /* Overwrite the result again with a correctly-parsed but invalid
# 170 : : signature if parsing failed. */
# 171 : 1467 : memset(tmpsig, 0, 64);
# 172 : 1467 : secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
# 173 : 1467 : }
# 174 : 1442284 : return 1;
# 175 : 1442284 : }
# 176 : :
# 177 : : XOnlyPubKey::XOnlyPubKey(Span<const unsigned char> bytes)
# 178 : 1310636 : {
# 179 : 1310636 : assert(bytes.size() == 32);
# 180 : 0 : std::copy(bytes.begin(), bytes.end(), m_keydata.begin());
# 181 : 1310636 : }
# 182 : :
# 183 : : std::vector<CKeyID> XOnlyPubKey::GetKeyIDs() const
# 184 : 24466 : {
# 185 : 24466 : std::vector<CKeyID> out;
# 186 : : // For now, use the old full pubkey-based key derivation logic. As it is indexed by
# 187 : : // Hash160(full pubkey), we need to return both a version prefixed with 0x02, and one
# 188 : : // with 0x03.
# 189 : 24466 : unsigned char b[33] = {0x02};
# 190 : 24466 : std::copy(m_keydata.begin(), m_keydata.end(), b + 1);
# 191 : 24466 : CPubKey fullpubkey;
# 192 : 24466 : fullpubkey.Set(b, b + 33);
# 193 : 24466 : out.push_back(fullpubkey.GetID());
# 194 : 24466 : b[0] = 0x03;
# 195 : 24466 : fullpubkey.Set(b, b + 33);
# 196 : 24466 : out.push_back(fullpubkey.GetID());
# 197 : 24466 : return out;
# 198 : 24466 : }
# 199 : :
# 200 : : bool XOnlyPubKey::IsFullyValid() const
# 201 : 61268 : {
# 202 : 61268 : secp256k1_xonly_pubkey pubkey;
# 203 : 61268 : return secp256k1_xonly_pubkey_parse(secp256k1_context_verify, &pubkey, m_keydata.data());
# 204 : 61268 : }
# 205 : :
# 206 : : bool XOnlyPubKey::VerifySchnorr(const uint256& msg, Span<const unsigned char> sigbytes) const
# 207 : 8830 : {
# 208 : 8830 : assert(sigbytes.size() == 64);
# 209 : 0 : secp256k1_xonly_pubkey pubkey;
# 210 [ + + ]: 8830 : if (!secp256k1_xonly_pubkey_parse(secp256k1_context_verify, &pubkey, m_keydata.data())) return false;
# 211 : 8826 : return secp256k1_schnorrsig_verify(secp256k1_context_verify, sigbytes.data(), msg.begin(), 32, &pubkey);
# 212 : 8830 : }
# 213 : :
# 214 : : static const CHashWriter HASHER_TAPTWEAK = TaggedHash("TapTweak");
# 215 : :
# 216 : : uint256 XOnlyPubKey::ComputeTapTweakHash(const uint256* merkle_root) const
# 217 : 108567 : {
# 218 [ + + ]: 108567 : if (merkle_root == nullptr) {
# 219 : : // We have no scripts. The actual tweak does not matter, but follow BIP341 here to
# 220 : : // allow for reproducible tweaking.
# 221 : 46585 : return (CHashWriter(HASHER_TAPTWEAK) << m_keydata).GetSHA256();
# 222 : 61982 : } else {
# 223 : 61982 : return (CHashWriter(HASHER_TAPTWEAK) << m_keydata << *merkle_root).GetSHA256();
# 224 : 61982 : }
# 225 : 108567 : }
# 226 : :
# 227 : : bool XOnlyPubKey::CheckTapTweak(const XOnlyPubKey& internal, const uint256& merkle_root, bool parity) const
# 228 : 46874 : {
# 229 : 46874 : secp256k1_xonly_pubkey internal_key;
# 230 [ + + ]: 46874 : if (!secp256k1_xonly_pubkey_parse(secp256k1_context_verify, &internal_key, internal.data())) return false;
# 231 : 46871 : uint256 tweak = internal.ComputeTapTweakHash(&merkle_root);
# 232 : 46871 : return secp256k1_xonly_pubkey_tweak_add_check(secp256k1_context_verify, m_keydata.begin(), parity, &internal_key, tweak.begin());
# 233 : 46874 : }
# 234 : :
# 235 : : std::optional<std::pair<XOnlyPubKey, bool>> XOnlyPubKey::CreateTapTweak(const uint256* merkle_root) const
# 236 : 61481 : {
# 237 : 61481 : secp256k1_xonly_pubkey base_point;
# 238 [ - + ]: 61481 : if (!secp256k1_xonly_pubkey_parse(secp256k1_context_verify, &base_point, data())) return std::nullopt;
# 239 : 61481 : secp256k1_pubkey out;
# 240 : 61481 : uint256 tweak = ComputeTapTweakHash(merkle_root);
# 241 [ - + ]: 61481 : if (!secp256k1_xonly_pubkey_tweak_add(secp256k1_context_verify, &out, &base_point, tweak.data())) return std::nullopt;
# 242 : 61481 : int parity = -1;
# 243 : 61481 : std::pair<XOnlyPubKey, bool> ret;
# 244 : 61481 : secp256k1_xonly_pubkey out_xonly;
# 245 [ - + ]: 61481 : if (!secp256k1_xonly_pubkey_from_pubkey(secp256k1_context_verify, &out_xonly, &parity, &out)) return std::nullopt;
# 246 : 61481 : secp256k1_xonly_pubkey_serialize(secp256k1_context_verify, ret.first.begin(), &out_xonly);
# 247 : 61481 : assert(parity == 0 || parity == 1);
# 248 : 0 : ret.second = parity;
# 249 : 61481 : return ret;
# 250 : 61481 : }
# 251 : :
# 252 : :
# 253 : 195589 : bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const {
# 254 [ - + ]: 195589 : if (!IsValid())
# 255 : 0 : return false;
# 256 : 195589 : secp256k1_pubkey pubkey;
# 257 : 195589 : secp256k1_ecdsa_signature sig;
# 258 : 195589 : assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
# 259 [ - + ]: 195589 : if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
# 260 : 0 : return false;
# 261 : 0 : }
# 262 [ + + ]: 195589 : if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, vchSig.data(), vchSig.size())) {
# 263 : 92 : return false;
# 264 : 92 : }
# 265 : : /* libsecp256k1's ECDSA verification requires lower-S signatures, which have
# 266 : : * not historically been enforced in Bitcoin, so normalize them first. */
# 267 : 195497 : secp256k1_ecdsa_signature_normalize(secp256k1_context_verify, &sig, &sig);
# 268 : 195497 : return secp256k1_ecdsa_verify(secp256k1_context_verify, &sig, hash.begin(), &pubkey);
# 269 : 195589 : }
# 270 : :
# 271 : 144 : bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
# 272 [ - + ]: 144 : if (vchSig.size() != COMPACT_SIGNATURE_SIZE)
# 273 : 0 : return false;
# 274 : 144 : int recid = (vchSig[0] - 27) & 3;
# 275 : 144 : bool fComp = ((vchSig[0] - 27) & 4) != 0;
# 276 : 144 : secp256k1_pubkey pubkey;
# 277 : 144 : secp256k1_ecdsa_recoverable_signature sig;
# 278 : 144 : assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
# 279 [ - + ]: 144 : if (!secp256k1_ecdsa_recoverable_signature_parse_compact(secp256k1_context_verify, &sig, &vchSig[1], recid)) {
# 280 : 0 : return false;
# 281 : 0 : }
# 282 [ + + ]: 144 : if (!secp256k1_ecdsa_recover(secp256k1_context_verify, &pubkey, &sig, hash.begin())) {
# 283 : 1 : return false;
# 284 : 1 : }
# 285 : 143 : unsigned char pub[SIZE];
# 286 : 143 : size_t publen = SIZE;
# 287 [ + + ]: 143 : secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, fComp ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
# 288 : 143 : Set(pub, pub + publen);
# 289 : 143 : return true;
# 290 : 144 : }
# 291 : :
# 292 : 49305 : bool CPubKey::IsFullyValid() const {
# 293 [ + + ]: 49305 : if (!IsValid())
# 294 : 20560 : return false;
# 295 : 28745 : secp256k1_pubkey pubkey;
# 296 : 28745 : assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
# 297 : 0 : return secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size());
# 298 : 49305 : }
# 299 : :
# 300 : 26 : bool CPubKey::Decompress() {
# 301 [ - + ]: 26 : if (!IsValid())
# 302 : 0 : return false;
# 303 : 26 : secp256k1_pubkey pubkey;
# 304 : 26 : assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
# 305 [ - + ]: 26 : if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
# 306 : 0 : return false;
# 307 : 0 : }
# 308 : 26 : unsigned char pub[SIZE];
# 309 : 26 : size_t publen = SIZE;
# 310 : 26 : secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
# 311 : 26 : Set(pub, pub + publen);
# 312 : 26 : return true;
# 313 : 26 : }
# 314 : :
# 315 : 574101 : bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
# 316 : 574101 : assert(IsValid());
# 317 : 0 : assert((nChild >> 31) == 0);
# 318 : 0 : assert(size() == COMPRESSED_SIZE);
# 319 : 0 : unsigned char out[64];
# 320 : 574101 : BIP32Hash(cc, nChild, *begin(), begin()+1, out);
# 321 : 574101 : memcpy(ccChild.begin(), out+32, 32);
# 322 : 574101 : secp256k1_pubkey pubkey;
# 323 : 574101 : assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
# 324 [ - + ]: 574101 : if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
# 325 : 0 : return false;
# 326 : 0 : }
# 327 [ - + ]: 574101 : if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_verify, &pubkey, out)) {
# 328 : 0 : return false;
# 329 : 0 : }
# 330 : 574101 : unsigned char pub[COMPRESSED_SIZE];
# 331 : 574101 : size_t publen = COMPRESSED_SIZE;
# 332 : 574101 : secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_COMPRESSED);
# 333 : 574101 : pubkeyChild.Set(pub, pub + publen);
# 334 : 574101 : return true;
# 335 : 574101 : }
# 336 : :
# 337 : 197077 : void CExtPubKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
# 338 : 197077 : code[0] = nDepth;
# 339 : 197077 : memcpy(code+1, vchFingerprint, 4);
# 340 : 197077 : WriteBE32(code+5, nChild);
# 341 : 197077 : memcpy(code+9, chaincode.begin(), 32);
# 342 : 197077 : assert(pubkey.size() == CPubKey::COMPRESSED_SIZE);
# 343 : 0 : memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_SIZE);
# 344 : 197077 : }
# 345 : :
# 346 : 5058 : void CExtPubKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
# 347 : 5058 : nDepth = code[0];
# 348 : 5058 : memcpy(vchFingerprint, code+1, 4);
# 349 : 5058 : nChild = ReadBE32(code+5);
# 350 : 5058 : memcpy(chaincode.begin(), code+9, 32);
# 351 : 5058 : pubkey.Set(code+41, code+BIP32_EXTKEY_SIZE);
# 352 [ + + ][ + + ]: 5058 : if ((nDepth == 0 && (nChild != 0 || ReadLE32(vchFingerprint) != 0)) || !pubkey.IsFullyValid()) pubkey = CPubKey();
# [ + + ][ + + ]
# 353 : 5058 : }
# 354 : :
# 355 : : void CExtPubKey::EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const
# 356 : 6 : {
# 357 : 6 : memcpy(code, version, 4);
# 358 : 6 : Encode(&code[4]);
# 359 : 6 : }
# 360 : :
# 361 : : void CExtPubKey::DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE])
# 362 : 6 : {
# 363 : 6 : memcpy(version, code, 4);
# 364 : 6 : Decode(&code[4]);
# 365 : 6 : }
# 366 : :
# 367 : 574101 : bool CExtPubKey::Derive(CExtPubKey &out, unsigned int _nChild) const {
# 368 : 574101 : out.nDepth = nDepth + 1;
# 369 : 574101 : CKeyID id = pubkey.GetID();
# 370 : 574101 : memcpy(out.vchFingerprint, &id, 4);
# 371 : 574101 : out.nChild = _nChild;
# 372 : 574101 : return pubkey.Derive(out.pubkey, out.chaincode, _nChild, chaincode);
# 373 : 574101 : }
# 374 : :
# 375 : 1247030 : /* static */ bool CPubKey::CheckLowS(const std::vector<unsigned char>& vchSig) {
# 376 : 1247030 : secp256k1_ecdsa_signature sig;
# 377 : 1247030 : assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
# 378 [ - + ]: 1247030 : if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, vchSig.data(), vchSig.size())) {
# 379 : 0 : return false;
# 380 : 0 : }
# 381 : 1247030 : return (!secp256k1_ecdsa_signature_normalize(secp256k1_context_verify, nullptr, &sig));
# 382 : 1247030 : }
# 383 : :
# 384 : : /* static */ int ECCVerifyHandle::refcount = 0;
# 385 : :
# 386 : : ECCVerifyHandle::ECCVerifyHandle()
# 387 : 1689 : {
# 388 [ + + ]: 1689 : if (refcount == 0) {
# 389 : 856 : assert(secp256k1_context_verify == nullptr);
# 390 : 856 : secp256k1_context_verify = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
# 391 : 856 : assert(secp256k1_context_verify != nullptr);
# 392 : 856 : }
# 393 : 0 : refcount++;
# 394 : 1689 : }
# 395 : :
# 396 : : ECCVerifyHandle::~ECCVerifyHandle()
# 397 : 1685 : {
# 398 : 1685 : refcount--;
# 399 [ + + ]: 1685 : if (refcount == 0) {
# 400 : 853 : assert(secp256k1_context_verify != nullptr);
# 401 : 0 : secp256k1_context_destroy(secp256k1_context_verify);
# 402 : 853 : secp256k1_context_verify = nullptr;
# 403 : 853 : }
# 404 : 1685 : }
# 405 : :
# 406 : 69360 : const secp256k1_context* GetVerifyContext() {
# 407 : 69360 : return secp256k1_context_verify;
# 408 : 69360 : }
|