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) {
26 GetRandBytes(vch, sizeof(vch));
27 } while (!Check(vch));
29 fCompressed = fCompressedIn;
32 bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
33 if (!secp256k1_ec_privkey_import(secp256k1_context, (unsigned char*)begin(), &privkey[0], privkey.size()))
35 fCompressed = fCompressedIn;
40 CPrivKey CKey::GetPrivKey() const {
46 ret = secp256k1_ec_privkey_export(secp256k1_context, begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed);
48 privkey.resize(privkeylen);
52 CPubKey CKey::GetPubKey() const {
56 int ret = secp256k1_ec_pubkey_create(secp256k1_context, (unsigned char*)result.begin(), &clen, begin(), fCompressed);
57 assert((int)result.size() == clen);
59 assert(result.IsValid());
63 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
68 unsigned char extra_entropy[32] = {0};
69 WriteLE32(extra_entropy, test_case);
70 int ret = secp256k1_ecdsa_sign(secp256k1_context, hash.begin(), (unsigned char*)&vchSig[0], &nSigLen, begin(), secp256k1_nonce_function_rfc6979, test_case ? extra_entropy : NULL);
72 vchSig.resize(nSigLen);
76 bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
77 if (pubkey.IsCompressed() != fCompressed) {
81 std::string str = "Bitcoin key verification\n";
82 GetRandBytes(rnd, sizeof(rnd));
84 CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
85 std::vector<unsigned char> vchSig;
87 return pubkey.Verify(hash, vchSig);
90 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
95 int ret = secp256k1_ecdsa_sign_compact(secp256k1_context, hash.begin(), &vchSig[1], begin(), secp256k1_nonce_function_rfc6979, NULL, &rec);
98 vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
102 bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
103 if (!secp256k1_ec_privkey_import(secp256k1_context, (unsigned char*)begin(), &privkey[0], privkey.size()))
105 fCompressed = vchPubKey.IsCompressed();
111 return VerifyPubKey(vchPubKey);
114 bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
116 assert(IsCompressed());
117 unsigned char out[64];
119 if ((nChild >> 31) == 0) {
120 CPubKey pubkey = GetPubKey();
121 assert(pubkey.begin() + 33 == pubkey.end());
122 BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out);
124 assert(begin() + 32 == end());
125 BIP32Hash(cc, nChild, 0, begin(), out);
127 memcpy(ccChild.begin(), out+32, 32);
128 memcpy((unsigned char*)keyChild.begin(), begin(), 32);
129 bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context, (unsigned char*)keyChild.begin(), out);
131 keyChild.fCompressed = true;
132 keyChild.fValid = ret;
136 bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
137 out.nDepth = nDepth + 1;
138 CKeyID id = key.GetPubKey().GetID();
139 memcpy(&out.vchFingerprint[0], &id, 4);
141 return key.Derive(out.key, out.chaincode, nChild, chaincode);
144 void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
145 static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
146 unsigned char out[64];
148 CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
149 key.Set(&out[0], &out[32], true);
150 memcpy(chaincode.begin(), &out[32], 32);
154 memset(vchFingerprint, 0, sizeof(vchFingerprint));
157 CExtPubKey CExtKey::Neuter() const {
160 memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
162 ret.pubkey = key.GetPubKey();
163 ret.chaincode = chaincode;
167 void CExtKey::Encode(unsigned char code[74]) const {
169 memcpy(code+1, vchFingerprint, 4);
170 code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
171 code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
172 memcpy(code+9, chaincode.begin(), 32);
174 assert(key.size() == 32);
175 memcpy(code+42, key.begin(), 32);
178 void CExtKey::Decode(const unsigned char code[74]) {
180 memcpy(vchFingerprint, code+1, 4);
181 nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
182 memcpy(chaincode.begin(), code+9, 32);
183 key.Set(code+42, code+74, true);
186 bool ECC_InitSanityCheck() {
187 if (!CECKey::SanityCheck()) {
191 key.MakeNewKey(true);
192 CPubKey pubkey = key.GetPubKey();
193 return key.VerifyPubKey(pubkey);
198 assert(secp256k1_context == NULL);
200 secp256k1_context_t *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
204 // Pass in a random blinding seed to the secp256k1 context.
205 unsigned char seed[32];
207 GetRandBytes(seed, 32);
208 bool ret = secp256k1_context_randomize(ctx, seed);
213 secp256k1_context = ctx;
217 secp256k1_context_t *ctx = secp256k1_context;
218 secp256k1_context = NULL;
221 secp256k1_context_destroy(ctx);