Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2020 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 : 1681179 : int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
# 36 : 1681179 : size_t rpos, rlen, spos, slen;
# 37 : 1681179 : size_t pos = 0;
# 38 : 1681179 : size_t lenbyte;
# 39 : 1681179 : unsigned char tmpsig[64] = {0};
# 40 : 1681179 : int overflow = 0;
# 41 : :
# 42 : : /* Hack to initialize sig with a correctly-parsed but invalid signature. */
# 43 : 1681179 : secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
# 44 : :
# 45 : : /* Sequence tag byte */
# 46 [ + + ][ + + ]: 1681280 : if (pos == inputlen || input[pos] != 0x30) {
# 47 : 96 : return 0;
# 48 : 96 : }
# 49 : 1681083 : pos++;
# 50 : :
# 51 : : /* Sequence length bytes */
# 52 [ - + ]: 1681083 : if (pos == inputlen) {
# 53 : 0 : return 0;
# 54 : 0 : }
# 55 : 1681083 : lenbyte = input[pos++];
# 56 [ - + ]: 1681083 : 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 [ + + ][ + + ]: 1681384 : if (pos == inputlen || input[pos] != 0x02) {
# 66 : 0 : return 0;
# 67 : 0 : }
# 68 : 1681083 : pos++;
# 69 : :
# 70 : : /* Integer length for R */
# 71 [ - + ]: 1681083 : if (pos == inputlen) {
# 72 : 0 : return 0;
# 73 : 0 : }
# 74 : 1681083 : lenbyte = input[pos++];
# 75 [ - + ]: 1681083 : 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 : 1681083 : } else {
# 95 : 1681083 : rlen = lenbyte;
# 96 : 1681083 : }
# 97 [ - + ]: 1681083 : if (rlen > inputlen - pos) {
# 98 : 0 : return 0;
# 99 : 0 : }
# 100 : 1681083 : rpos = pos;
# 101 : 1681083 : pos += rlen;
# 102 : :
# 103 : : /* Integer tag byte for S */
# 104 [ + + ][ + + ]: 1681212 : if (pos == inputlen || input[pos] != 0x02) {
# 105 : 0 : return 0;
# 106 : 0 : }
# 107 : 1681083 : pos++;
# 108 : :
# 109 : : /* Integer length for S */
# 110 [ - + ]: 1681083 : if (pos == inputlen) {
# 111 : 0 : return 0;
# 112 : 0 : }
# 113 : 1681083 : lenbyte = input[pos++];
# 114 [ - + ]: 1681083 : 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 : 1681083 : } else {
# 134 : 1681083 : slen = lenbyte;
# 135 : 1681083 : }
# 136 [ - + ]: 1681083 : if (slen > inputlen - pos) {
# 137 : 0 : return 0;
# 138 : 0 : }
# 139 : 1681083 : spos = pos;
# 140 : :
# 141 : : /* Ignore leading zeroes in R */
# 142 [ + + ][ + + ]: 1715715 : while (rlen > 0 && input[rpos] == 0) {
# 143 : 34462 : rlen--;
# 144 : 34462 : rpos++;
# 145 : 34462 : }
# 146 : : /* Copy R value */
# 147 [ + + ]: 1681083 : if (rlen > 32) {
# 148 : 341 : overflow = 1;
# 149 : 1680742 : } else {
# 150 : 1680742 : memcpy(tmpsig + 32 - rlen, input + rpos, rlen);
# 151 : 1680742 : }
# 152 : :
# 153 : : /* Ignore leading zeroes in S */
# 154 [ + + ][ + + ]: 1685645 : while (slen > 0 && input[spos] == 0) {
# 155 : 4326 : slen--;
# 156 : 4326 : spos++;
# 157 : 4326 : }
# 158 : : /* Copy S value */
# 159 [ - + ]: 1681083 : if (slen > 32) {
# 160 : 0 : overflow = 1;
# 161 : 1681083 : } else {
# 162 : 1681083 : memcpy(tmpsig + 64 - slen, input + spos, slen);
# 163 : 1681083 : }
# 164 : :
# 165 [ + + ]: 1681083 : if (!overflow) {
# 166 : 1680990 : overflow = !secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
# 167 : 1680990 : }
# 168 [ + + ]: 1681083 : if (overflow) {
# 169 : : /* Overwrite the result again with a correctly-parsed but invalid
# 170 : : signature if parsing failed. */
# 171 : 341 : memset(tmpsig, 0, 64);
# 172 : 341 : secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
# 173 : 341 : }
# 174 : 1681083 : return 1;
# 175 : 1681083 : }
# 176 : :
# 177 : : XOnlyPubKey::XOnlyPubKey(Span<const unsigned char> bytes)
# 178 : 20675 : {
# 179 : 20675 : assert(bytes.size() == 32);
# 180 : 20675 : std::copy(bytes.begin(), bytes.end(), m_keydata.begin());
# 181 : 20675 : }
# 182 : :
# 183 : : bool XOnlyPubKey::VerifySchnorr(const uint256& msg, Span<const unsigned char> sigbytes) const
# 184 : 4621 : {
# 185 : 4621 : assert(sigbytes.size() == 64);
# 186 : 4621 : secp256k1_xonly_pubkey pubkey;
# 187 [ + + ]: 4621 : if (!secp256k1_xonly_pubkey_parse(secp256k1_context_verify, &pubkey, m_keydata.data())) return false;
# 188 : 4617 : return secp256k1_schnorrsig_verify(secp256k1_context_verify, sigbytes.data(), msg.begin(), &pubkey);
# 189 : 4617 : }
# 190 : :
# 191 : : bool XOnlyPubKey::CheckPayToContract(const XOnlyPubKey& base, const uint256& hash, bool parity) const
# 192 : 7253 : {
# 193 : 7253 : secp256k1_xonly_pubkey base_point;
# 194 [ + + ]: 7253 : if (!secp256k1_xonly_pubkey_parse(secp256k1_context_verify, &base_point, base.data())) return false;
# 195 : 7252 : return secp256k1_xonly_pubkey_tweak_add_check(secp256k1_context_verify, m_keydata.begin(), parity, &base_point, hash.begin());
# 196 : 7252 : }
# 197 : :
# 198 : 212565 : bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const {
# 199 [ - + ]: 212565 : if (!IsValid())
# 200 : 0 : return false;
# 201 : 212565 : secp256k1_pubkey pubkey;
# 202 : 212565 : secp256k1_ecdsa_signature sig;
# 203 : 212565 : assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
# 204 [ - + ]: 212565 : if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
# 205 : 0 : return false;
# 206 : 0 : }
# 207 [ + + ]: 212565 : if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, vchSig.data(), vchSig.size())) {
# 208 : 96 : return false;
# 209 : 96 : }
# 210 : : /* libsecp256k1's ECDSA verification requires lower-S signatures, which have
# 211 : : * not historically been enforced in Bitcoin, so normalize them first. */
# 212 : 212469 : secp256k1_ecdsa_signature_normalize(secp256k1_context_verify, &sig, &sig);
# 213 : 212469 : return secp256k1_ecdsa_verify(secp256k1_context_verify, &sig, hash.begin(), &pubkey);
# 214 : 212469 : }
# 215 : :
# 216 : 144 : bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
# 217 [ - + ]: 144 : if (vchSig.size() != COMPACT_SIGNATURE_SIZE)
# 218 : 0 : return false;
# 219 : 144 : int recid = (vchSig[0] - 27) & 3;
# 220 : 144 : bool fComp = ((vchSig[0] - 27) & 4) != 0;
# 221 : 144 : secp256k1_pubkey pubkey;
# 222 : 144 : secp256k1_ecdsa_recoverable_signature sig;
# 223 : 144 : assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
# 224 [ - + ]: 144 : if (!secp256k1_ecdsa_recoverable_signature_parse_compact(secp256k1_context_verify, &sig, &vchSig[1], recid)) {
# 225 : 0 : return false;
# 226 : 0 : }
# 227 [ + + ]: 144 : if (!secp256k1_ecdsa_recover(secp256k1_context_verify, &pubkey, &sig, hash.begin())) {
# 228 : 1 : return false;
# 229 : 1 : }
# 230 : 143 : unsigned char pub[SIZE];
# 231 : 143 : size_t publen = SIZE;
# 232 [ + + ]: 143 : secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, fComp ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
# 233 : 143 : Set(pub, pub + publen);
# 234 : 143 : return true;
# 235 : 143 : }
# 236 : :
# 237 : 3208 : bool CPubKey::IsFullyValid() const {
# 238 [ + + ]: 3208 : if (!IsValid())
# 239 : 4 : return false;
# 240 : 3204 : secp256k1_pubkey pubkey;
# 241 : 3204 : assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
# 242 : 3204 : return secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size());
# 243 : 3204 : }
# 244 : :
# 245 : 31 : bool CPubKey::Decompress() {
# 246 [ - + ]: 31 : if (!IsValid())
# 247 : 0 : return false;
# 248 : 31 : secp256k1_pubkey pubkey;
# 249 : 31 : assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
# 250 [ - + ]: 31 : if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
# 251 : 0 : return false;
# 252 : 0 : }
# 253 : 31 : unsigned char pub[SIZE];
# 254 : 31 : size_t publen = SIZE;
# 255 : 31 : secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
# 256 : 31 : Set(pub, pub + publen);
# 257 : 31 : return true;
# 258 : 31 : }
# 259 : :
# 260 : 342304 : bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
# 261 : 342304 : assert(IsValid());
# 262 : 342304 : assert((nChild >> 31) == 0);
# 263 : 342304 : assert(size() == COMPRESSED_SIZE);
# 264 : 342304 : unsigned char out[64];
# 265 : 342304 : BIP32Hash(cc, nChild, *begin(), begin()+1, out);
# 266 : 342304 : memcpy(ccChild.begin(), out+32, 32);
# 267 : 342304 : secp256k1_pubkey pubkey;
# 268 : 342304 : assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
# 269 [ - + ]: 342304 : if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
# 270 : 0 : return false;
# 271 : 0 : }
# 272 [ - + ]: 342304 : if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_verify, &pubkey, out)) {
# 273 : 0 : return false;
# 274 : 0 : }
# 275 : 342304 : unsigned char pub[COMPRESSED_SIZE];
# 276 : 342304 : size_t publen = COMPRESSED_SIZE;
# 277 : 342304 : secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_COMPRESSED);
# 278 : 342304 : pubkeyChild.Set(pub, pub + publen);
# 279 : 342304 : return true;
# 280 : 342304 : }
# 281 : :
# 282 : 110646 : void CExtPubKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
# 283 : 110646 : code[0] = nDepth;
# 284 : 110646 : memcpy(code+1, vchFingerprint, 4);
# 285 : 110646 : code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
# 286 : 110646 : code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
# 287 : 110646 : memcpy(code+9, chaincode.begin(), 32);
# 288 : 110646 : assert(pubkey.size() == CPubKey::COMPRESSED_SIZE);
# 289 : 110646 : memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_SIZE);
# 290 : 110646 : }
# 291 : :
# 292 : 2254 : void CExtPubKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
# 293 : 2254 : nDepth = code[0];
# 294 : 2254 : memcpy(vchFingerprint, code+1, 4);
# 295 : 2254 : nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
# 296 : 2254 : memcpy(chaincode.begin(), code+9, 32);
# 297 : 2254 : pubkey.Set(code+41, code+BIP32_EXTKEY_SIZE);
# 298 : 2254 : }
# 299 : :
# 300 : 342304 : bool CExtPubKey::Derive(CExtPubKey &out, unsigned int _nChild) const {
# 301 : 342304 : out.nDepth = nDepth + 1;
# 302 : 342304 : CKeyID id = pubkey.GetID();
# 303 : 342304 : memcpy(out.vchFingerprint, &id, 4);
# 304 : 342304 : out.nChild = _nChild;
# 305 : 342304 : return pubkey.Derive(out.pubkey, out.chaincode, _nChild, chaincode);
# 306 : 342304 : }
# 307 : :
# 308 : 1469003 : /* static */ bool CPubKey::CheckLowS(const std::vector<unsigned char>& vchSig) {
# 309 : 1469003 : secp256k1_ecdsa_signature sig;
# 310 : 1469003 : assert(secp256k1_context_verify && "secp256k1_context_verify must be initialized to use CPubKey.");
# 311 [ - + ]: 1469003 : if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, vchSig.data(), vchSig.size())) {
# 312 : 0 : return false;
# 313 : 0 : }
# 314 : 1469003 : return (!secp256k1_ecdsa_signature_normalize(secp256k1_context_verify, nullptr, &sig));
# 315 : 1469003 : }
# 316 : :
# 317 : : /* static */ int ECCVerifyHandle::refcount = 0;
# 318 : :
# 319 : : ECCVerifyHandle::ECCVerifyHandle()
# 320 : 1567 : {
# 321 [ + + ]: 1567 : if (refcount == 0) {
# 322 : 719 : assert(secp256k1_context_verify == nullptr);
# 323 : 719 : secp256k1_context_verify = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
# 324 : 719 : assert(secp256k1_context_verify != nullptr);
# 325 : 719 : }
# 326 : 1567 : refcount++;
# 327 : 1567 : }
# 328 : :
# 329 : : ECCVerifyHandle::~ECCVerifyHandle()
# 330 : 1563 : {
# 331 : 1563 : refcount--;
# 332 [ + + ]: 1563 : if (refcount == 0) {
# 333 : 716 : assert(secp256k1_context_verify != nullptr);
# 334 : 716 : secp256k1_context_destroy(secp256k1_context_verify);
# 335 : 716 : secp256k1_context_verify = nullptr;
# 336 : 716 : }
# 337 : 1563 : }
|