1 // Copyright (c) 2009-2014 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.
7 #include "arith_uint256.h"
8 #include "crypto/common.h"
9 #include "crypto/hmac_sha512.h"
10 #include "eccryptoverify.h"
14 #include <secp256k1.h>
15 #include "ecwrapper.h"
17 static secp256k1_context_t* secp256k1_context = NULL;
19 bool CKey::Check(const unsigned char *vch) {
20 return eccrypto::Check(vch);
23 void CKey::MakeNewKey(bool fCompressedIn) {
25 GetRandBytes(vch, sizeof(vch));
26 } while (!Check(vch));
28 fCompressed = fCompressedIn;
31 bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
32 if (!secp256k1_ec_privkey_import(secp256k1_context, (unsigned char*)begin(), &privkey[0], privkey.size()))
34 fCompressed = fCompressedIn;
39 CPrivKey CKey::GetPrivKey() const {
45 ret = secp256k1_ec_privkey_export(secp256k1_context, begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed);
47 privkey.resize(privkeylen);
51 CPubKey CKey::GetPubKey() const {
55 int ret = secp256k1_ec_pubkey_create(secp256k1_context, (unsigned char*)result.begin(), &clen, begin(), fCompressed);
56 assert((int)result.size() == clen);
58 assert(result.IsValid());
62 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
67 unsigned char extra_entropy[32] = {0};
68 WriteLE32(extra_entropy, test_case);
69 int ret = secp256k1_ecdsa_sign(secp256k1_context, hash.begin(), (unsigned char*)&vchSig[0], &nSigLen, begin(), secp256k1_nonce_function_rfc6979, test_case ? extra_entropy : NULL);
71 vchSig.resize(nSigLen);
75 bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
76 if (pubkey.IsCompressed() != fCompressed) {
80 std::string str = "Bitcoin key verification\n";
81 GetRandBytes(rnd, sizeof(rnd));
83 CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
84 std::vector<unsigned char> vchSig;
86 return pubkey.Verify(hash, vchSig);
89 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
94 int ret = secp256k1_ecdsa_sign_compact(secp256k1_context, hash.begin(), &vchSig[1], begin(), secp256k1_nonce_function_rfc6979, NULL, &rec);
97 vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
101 bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
102 if (!secp256k1_ec_privkey_import(secp256k1_context, (unsigned char*)begin(), &privkey[0], privkey.size()))
104 fCompressed = vchPubKey.IsCompressed();
110 return VerifyPubKey(vchPubKey);
113 bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
115 assert(IsCompressed());
116 unsigned char out[64];
118 if ((nChild >> 31) == 0) {
119 CPubKey pubkey = GetPubKey();
120 assert(pubkey.begin() + 33 == pubkey.end());
121 BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out);
123 assert(begin() + 32 == end());
124 BIP32Hash(cc, nChild, 0, begin(), out);
126 memcpy(ccChild.begin(), out+32, 32);
127 memcpy((unsigned char*)keyChild.begin(), begin(), 32);
128 bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context, (unsigned char*)keyChild.begin(), out);
130 keyChild.fCompressed = true;
131 keyChild.fValid = ret;
135 bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
136 out.nDepth = nDepth + 1;
137 CKeyID id = key.GetPubKey().GetID();
138 memcpy(&out.vchFingerprint[0], &id, 4);
140 return key.Derive(out.key, out.chaincode, nChild, chaincode);
143 void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
144 static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
145 unsigned char out[64];
147 CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
148 key.Set(&out[0], &out[32], true);
149 memcpy(chaincode.begin(), &out[32], 32);
153 memset(vchFingerprint, 0, sizeof(vchFingerprint));
156 CExtPubKey CExtKey::Neuter() const {
159 memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
161 ret.pubkey = key.GetPubKey();
162 ret.chaincode = chaincode;
166 void CExtKey::Encode(unsigned char code[74]) const {
168 memcpy(code+1, vchFingerprint, 4);
169 code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
170 code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
171 memcpy(code+9, chaincode.begin(), 32);
173 assert(key.size() == 32);
174 memcpy(code+42, key.begin(), 32);
177 void CExtKey::Decode(const unsigned char code[74]) {
179 memcpy(vchFingerprint, code+1, 4);
180 nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
181 memcpy(chaincode.begin(), code+9, 32);
182 key.Set(code+42, code+74, true);
185 bool ECC_InitSanityCheck() {
186 if (!CECKey::SanityCheck()) {
190 key.MakeNewKey(true);
191 CPubKey pubkey = key.GetPubKey();
192 return key.VerifyPubKey(pubkey);
197 assert(secp256k1_context == NULL);
199 secp256k1_context_t *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
203 // Pass in a random blinding seed to the secp256k1 context.
204 unsigned char seed[32];
206 GetRandBytes(seed, 32);
207 bool ret = secp256k1_context_randomize(ctx, seed);
212 secp256k1_context = ctx;
216 secp256k1_context_t *ctx = secp256k1_context;
217 secp256k1_context = NULL;
220 secp256k1_context_destroy(ctx);