| 1 | // Copyright (c) 2009-2014 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 https://www.opensource.org/licenses/mit-license.php . |
| 5 | |
| 6 | #include "key.h" |
| 7 | |
| 8 | #include "arith_uint256.h" |
| 9 | #include "crypto/common.h" |
| 10 | #include "crypto/hmac_sha512.h" |
| 11 | #include "pubkey.h" |
| 12 | #include "random.h" |
| 13 | |
| 14 | #include <secp256k1.h> |
| 15 | #include <secp256k1_recovery.h> |
| 16 | |
| 17 | static secp256k1_context* secp256k1_context_sign = NULL; |
| 18 | |
| 19 | /** These functions are taken from the libsecp256k1 distribution and are very ugly. */ |
| 20 | |
| 21 | /** |
| 22 | * This parses a format loosely based on a DER encoding of the ECPrivateKey type from |
| 23 | * section C.4 of SEC 1 <http://www.secg.org/sec1-v2.pdf>, with the following caveats: |
| 24 | * |
| 25 | * * The octet-length of the SEQUENCE must be encoded as 1 or 2 octets. It is not |
| 26 | * required to be encoded as one octet if it is less than 256, as DER would require. |
| 27 | * * The octet-length of the SEQUENCE must not be greater than the remaining |
| 28 | * length of the key encoding, but need not match it (i.e. the encoding may contain |
| 29 | * junk after the encoded SEQUENCE). |
| 30 | * * The privateKey OCTET STRING is zero-filled on the left to 32 octets. |
| 31 | * * Anything after the encoding of the privateKey OCTET STRING is ignored, whether |
| 32 | * or not it is validly encoded DER. |
| 33 | * |
| 34 | * out32 must point to an output buffer of length at least 32 bytes. |
| 35 | */ |
| 36 | static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen) { |
| 37 | const unsigned char *end = privkey + privkeylen; |
| 38 | memset(out32, 0, 32); |
| 39 | /* sequence header */ |
| 40 | if (end - privkey < 1 || *privkey != 0x30u) { |
| 41 | return 0; |
| 42 | } |
| 43 | privkey++; |
| 44 | /* sequence length constructor */ |
| 45 | if (end - privkey < 1 || !(*privkey & 0x80u)) { |
| 46 | return 0; |
| 47 | } |
| 48 | size_t lenb = *privkey & ~0x80u; privkey++; |
| 49 | if (lenb < 1 || lenb > 2) { |
| 50 | return 0; |
| 51 | } |
| 52 | if (end - privkey < lenb) { |
| 53 | return 0; |
| 54 | } |
| 55 | /* sequence length */ |
| 56 | size_t len = privkey[lenb-1] | (lenb > 1 ? privkey[lenb-2] << 8 : 0u); |
| 57 | privkey += lenb; |
| 58 | if (end - privkey < len) { |
| 59 | return 0; |
| 60 | } |
| 61 | /* sequence element 0: version number (=1) */ |
| 62 | if (end - privkey < 3 || privkey[0] != 0x02u || privkey[1] != 0x01u || privkey[2] != 0x01u) { |
| 63 | return 0; |
| 64 | } |
| 65 | privkey += 3; |
| 66 | /* sequence element 1: octet string, up to 32 bytes */ |
| 67 | if (end - privkey < 2 || privkey[0] != 0x04u) { |
| 68 | return 0; |
| 69 | } |
| 70 | size_t oslen = privkey[1]; |
| 71 | privkey += 2; |
| 72 | if (oslen > 32 || end - privkey < oslen) { |
| 73 | return 0; |
| 74 | } |
| 75 | memcpy(out32 + (32 - oslen), privkey, oslen); |
| 76 | if (!secp256k1_ec_seckey_verify(ctx, out32)) { |
| 77 | memset(out32, 0, 32); |
| 78 | return 0; |
| 79 | } |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * This serializes to a DER encoding of the ECPrivateKey type from section C.4 of SEC 1 |
| 85 | * <http://www.secg.org/sec1-v2.pdf>. The optional parameters and publicKey fields are |
| 86 | * included. |
| 87 | * |
| 88 | * privkey must point to an output buffer of length at least CKey::PRIVATE_KEY_SIZE bytes. |
| 89 | * privkeylen must initially be set to the size of the privkey buffer. Upon return it |
| 90 | * will be set to the number of bytes used in the buffer. |
| 91 | * key32 must point to a 32-byte raw private key. |
| 92 | */ |
| 93 | static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed) { |
| 94 | assert(*privkeylen >= CKey::PRIVATE_KEY_SIZE); |
| 95 | secp256k1_pubkey pubkey; |
| 96 | size_t pubkeylen = 0; |
| 97 | if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) { |
| 98 | *privkeylen = 0; |
| 99 | return 0; |
| 100 | } |
| 101 | if (compressed) { |
| 102 | static const unsigned char begin[] = { |
| 103 | 0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20 |
| 104 | }; |
| 105 | static const unsigned char middle[] = { |
| 106 | 0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48, |
| 107 | 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 108 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 109 | 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04, |
| 110 | 0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87, |
| 111 | 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8, |
| 112 | 0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 113 | 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E, |
| 114 | 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00 |
| 115 | }; |
| 116 | unsigned char *ptr = privkey; |
| 117 | memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin); |
| 118 | memcpy(ptr, key32, 32); ptr += 32; |
| 119 | memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle); |
| 120 | pubkeylen = CPubKey::COMPRESSED_PUBLIC_KEY_SIZE; |
| 121 | secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED); |
| 122 | ptr += pubkeylen; |
| 123 | *privkeylen = ptr - privkey; |
| 124 | assert(*privkeylen == CKey::COMPRESSED_PRIVATE_KEY_SIZE); |
| 125 | } else { |
| 126 | static const unsigned char begin[] = { |
| 127 | 0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20 |
| 128 | }; |
| 129 | static const unsigned char middle[] = { |
| 130 | 0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48, |
| 131 | 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 132 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 133 | 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04, |
| 134 | 0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87, |
| 135 | 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8, |
| 136 | 0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11, |
| 137 | 0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10, |
| 138 | 0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 139 | 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E, |
| 140 | 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00 |
| 141 | }; |
| 142 | unsigned char *ptr = privkey; |
| 143 | memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin); |
| 144 | memcpy(ptr, key32, 32); ptr += 32; |
| 145 | memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle); |
| 146 | pubkeylen = CPubKey::PUBLIC_KEY_SIZE; |
| 147 | secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED); |
| 148 | ptr += pubkeylen; |
| 149 | *privkeylen = ptr - privkey; |
| 150 | assert(*privkeylen == CKey::PRIVATE_KEY_SIZE); |
| 151 | } |
| 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | bool CKey::Check(const unsigned char *vch) { |
| 156 | return secp256k1_ec_seckey_verify(secp256k1_context_sign, vch); |
| 157 | } |
| 158 | |
| 159 | void CKey::MakeNewKey(bool fCompressedIn) { |
| 160 | do { |
| 161 | GetRandBytes(vch, sizeof(vch)); |
| 162 | } while (!Check(vch)); |
| 163 | fValid = true; |
| 164 | fCompressed = fCompressedIn; |
| 165 | } |
| 166 | |
| 167 | bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) { |
| 168 | if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), &privkey[0], privkey.size())) |
| 169 | return false; |
| 170 | fCompressed = fCompressedIn; |
| 171 | fValid = true; |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | CPrivKey CKey::GetPrivKey() const { |
| 176 | assert(fValid); |
| 177 | CPrivKey privkey; |
| 178 | int ret; |
| 179 | size_t privkeylen; |
| 180 | privkey.resize(PRIVATE_KEY_SIZE); |
| 181 | privkeylen = PRIVATE_KEY_SIZE; |
| 182 | ret = ec_privkey_export_der(secp256k1_context_sign, (unsigned char*)&privkey[0], &privkeylen, begin(), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); |
| 183 | assert(ret); |
| 184 | privkey.resize(privkeylen); |
| 185 | return privkey; |
| 186 | } |
| 187 | |
| 188 | CPubKey CKey::GetPubKey() const { |
| 189 | assert(fValid); |
| 190 | secp256k1_pubkey pubkey; |
| 191 | size_t clen = CPubKey::PUBLIC_KEY_SIZE; |
| 192 | CPubKey result; |
| 193 | int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin()); |
| 194 | assert(ret); |
| 195 | secp256k1_ec_pubkey_serialize(secp256k1_context_sign, (unsigned char*)result.begin(), &clen, &pubkey, fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); |
| 196 | assert(result.size() == clen); |
| 197 | assert(result.IsValid()); |
| 198 | return result; |
| 199 | } |
| 200 | |
| 201 | bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const { |
| 202 | if (!fValid) |
| 203 | return false; |
| 204 | vchSig.resize(CPubKey::SIGNATURE_SIZE); |
| 205 | size_t nSigLen = CPubKey::SIGNATURE_SIZE; |
| 206 | unsigned char extra_entropy[32] = {0}; |
| 207 | WriteLE32(extra_entropy, test_case); |
| 208 | secp256k1_ecdsa_signature sig; |
| 209 | int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, test_case ? extra_entropy : NULL); |
| 210 | assert(ret); |
| 211 | secp256k1_ecdsa_signature_serialize_der(secp256k1_context_sign, (unsigned char*)&vchSig[0], &nSigLen, &sig); |
| 212 | vchSig.resize(nSigLen); |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | bool CKey::VerifyPubKey(const CPubKey& pubkey) const { |
| 217 | if (pubkey.IsCompressed() != fCompressed) { |
| 218 | return false; |
| 219 | } |
| 220 | unsigned char rnd[8]; |
| 221 | std::string str = "Zcash key verification\n"; |
| 222 | GetRandBytes(rnd, sizeof(rnd)); |
| 223 | uint256 hash; |
| 224 | CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin()); |
| 225 | std::vector<unsigned char> vchSig; |
| 226 | Sign(hash, vchSig); |
| 227 | return pubkey.Verify(hash, vchSig); |
| 228 | } |
| 229 | |
| 230 | bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const { |
| 231 | if (!fValid) |
| 232 | return false; |
| 233 | vchSig.resize(CPubKey::COMPACT_SIGNATURE_SIZE); |
| 234 | int rec = -1; |
| 235 | secp256k1_ecdsa_recoverable_signature sig; |
| 236 | int ret = secp256k1_ecdsa_sign_recoverable(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, NULL); |
| 237 | assert(ret); |
| 238 | secp256k1_ecdsa_recoverable_signature_serialize_compact(secp256k1_context_sign, (unsigned char*)&vchSig[1], &rec, &sig); |
| 239 | assert(ret); |
| 240 | assert(rec != -1); |
| 241 | vchSig[0] = 27 + rec + (fCompressed ? 4 : 0); |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) { |
| 246 | if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), &privkey[0], privkey.size())) |
| 247 | return false; |
| 248 | fCompressed = vchPubKey.IsCompressed(); |
| 249 | fValid = true; |
| 250 | |
| 251 | if (fSkipCheck) |
| 252 | return true; |
| 253 | |
| 254 | return VerifyPubKey(vchPubKey); |
| 255 | } |
| 256 | |
| 257 | bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const { |
| 258 | assert(IsValid()); |
| 259 | assert(IsCompressed()); |
| 260 | unsigned char out[64]; |
| 261 | LockObject(out); |
| 262 | if ((nChild >> 31) == 0) { |
| 263 | CPubKey pubkey = GetPubKey(); |
| 264 | assert(pubkey.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE); |
| 265 | BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out); |
| 266 | } else { |
| 267 | assert(size() == 32); |
| 268 | BIP32Hash(cc, nChild, 0, begin(), out); |
| 269 | } |
| 270 | memcpy(ccChild.begin(), out+32, 32); |
| 271 | memcpy((unsigned char*)keyChild.begin(), begin(), 32); |
| 272 | bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context_sign, (unsigned char*)keyChild.begin(), out); |
| 273 | UnlockObject(out); |
| 274 | keyChild.fCompressed = true; |
| 275 | keyChild.fValid = ret; |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const { |
| 280 | out.nDepth = nDepth + 1; |
| 281 | CKeyID id = key.GetPubKey().GetID(); |
| 282 | memcpy(&out.vchFingerprint[0], &id, 4); |
| 283 | out.nChild = nChild; |
| 284 | return key.Derive(out.key, out.chaincode, nChild, chaincode); |
| 285 | } |
| 286 | |
| 287 | void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) { |
| 288 | static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'}; |
| 289 | unsigned char out[64]; |
| 290 | LockObject(out); |
| 291 | CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out); |
| 292 | key.Set(&out[0], &out[32], true); |
| 293 | memcpy(chaincode.begin(), &out[32], 32); |
| 294 | UnlockObject(out); |
| 295 | nDepth = 0; |
| 296 | nChild = 0; |
| 297 | memset(vchFingerprint, 0, sizeof(vchFingerprint)); |
| 298 | } |
| 299 | |
| 300 | CExtPubKey CExtKey::Neuter() const { |
| 301 | CExtPubKey ret; |
| 302 | ret.nDepth = nDepth; |
| 303 | memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4); |
| 304 | ret.nChild = nChild; |
| 305 | ret.pubkey = key.GetPubKey(); |
| 306 | ret.chaincode = chaincode; |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | void CExtKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const { |
| 311 | code[0] = nDepth; |
| 312 | memcpy(code+1, vchFingerprint, 4); |
| 313 | code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF; |
| 314 | code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF; |
| 315 | memcpy(code+9, chaincode.begin(), 32); |
| 316 | code[41] = 0; |
| 317 | assert(key.size() == 32); |
| 318 | memcpy(code+42, key.begin(), 32); |
| 319 | } |
| 320 | |
| 321 | void CExtKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) { |
| 322 | nDepth = code[0]; |
| 323 | memcpy(vchFingerprint, code+1, 4); |
| 324 | nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8]; |
| 325 | memcpy(chaincode.begin(), code+9, 32); |
| 326 | key.Set(code+42, code+BIP32_EXTKEY_SIZE, true); |
| 327 | } |
| 328 | |
| 329 | bool ECC_InitSanityCheck() { |
| 330 | CKey key; |
| 331 | key.MakeNewKey(true); |
| 332 | CPubKey pubkey = key.GetPubKey(); |
| 333 | return key.VerifyPubKey(pubkey); |
| 334 | } |
| 335 | |
| 336 | void ECC_Start() { |
| 337 | assert(secp256k1_context_sign == NULL); |
| 338 | |
| 339 | secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); |
| 340 | assert(ctx != NULL); |
| 341 | |
| 342 | { |
| 343 | // Pass in a random blinding seed to the secp256k1 context. |
| 344 | unsigned char seed[32]; |
| 345 | LockObject(seed); |
| 346 | GetRandBytes(seed, 32); |
| 347 | bool ret = secp256k1_context_randomize(ctx, seed); |
| 348 | assert(ret); |
| 349 | UnlockObject(seed); |
| 350 | } |
| 351 | |
| 352 | secp256k1_context_sign = ctx; |
| 353 | } |
| 354 | |
| 355 | void ECC_Stop() { |
| 356 | secp256k1_context *ctx = secp256k1_context_sign; |
| 357 | secp256k1_context_sign = NULL; |
| 358 | |
| 359 | if (ctx) { |
| 360 | secp256k1_context_destroy(ctx); |
| 361 | } |
| 362 | } |