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 "crypto/hmac_sha512.h"
8 #include "crypto/rfc6979_hmac_sha256.h"
9 #include "eccryptoverify.h"
13 #include <secp256k1.h>
14 #include "ecwrapper.h"
16 //! anonymous namespace
19 class CSecp256k1Init {
22 secp256k1_start(SECP256K1_START_SIGN);
28 static CSecp256k1Init instance_of_csecp256k1;
32 bool CKey::Check(const unsigned char *vch) {
33 return eccrypto::Check(vch);
36 void CKey::MakeNewKey(bool fCompressedIn) {
38 GetRandBytes(vch, sizeof(vch));
39 } while (!Check(vch));
41 fCompressed = fCompressedIn;
44 bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
45 if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
47 fCompressed = fCompressedIn;
52 CPrivKey CKey::GetPrivKey() const {
58 ret = secp256k1_ec_privkey_export(begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed);
60 privkey.resize(privkeylen);
64 CPubKey CKey::GetPubKey() const {
68 int ret = secp256k1_ec_pubkey_create((unsigned char*)result.begin(), &clen, begin(), fCompressed);
69 assert((int)result.size() == clen);
71 assert(result.IsValid());
75 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
79 RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
82 prng.Generate((unsigned char*)&nonce, 32);
85 int ret = secp256k1_ecdsa_sign((const unsigned char*)&hash, (unsigned char*)&vchSig[0], &nSigLen, begin(), (unsigned char*)&nonce);
88 vchSig.resize(nSigLen);
94 bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
95 if (pubkey.IsCompressed() != fCompressed) {
99 std::string str = "Bitcoin key verification\n";
100 GetRandBytes(rnd, sizeof(rnd));
102 CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize((unsigned char*)&hash);
103 std::vector<unsigned char> vchSig;
105 return pubkey.Verify(hash, vchSig);
108 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
113 RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
116 prng.Generate((unsigned char*)&nonce, 32);
117 int ret = secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, &vchSig[1], begin(), (unsigned char*)&nonce, &rec);
123 vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
127 bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
128 if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
130 fCompressed = vchPubKey.IsCompressed();
136 return VerifyPubKey(vchPubKey);
139 bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
141 assert(IsCompressed());
142 unsigned char out[64];
144 if ((nChild >> 31) == 0) {
145 CPubKey pubkey = GetPubKey();
146 assert(pubkey.begin() + 33 == pubkey.end());
147 BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out);
149 assert(begin() + 32 == end());
150 BIP32Hash(cc, nChild, 0, begin(), out);
152 memcpy(ccChild, out+32, 32);
153 memcpy((unsigned char*)keyChild.begin(), begin(), 32);
154 bool ret = secp256k1_ec_privkey_tweak_add((unsigned char*)keyChild.begin(), out);
156 keyChild.fCompressed = true;
157 keyChild.fValid = ret;
161 bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
162 out.nDepth = nDepth + 1;
163 CKeyID id = key.GetPubKey().GetID();
164 memcpy(&out.vchFingerprint[0], &id, 4);
166 return key.Derive(out.key, out.vchChainCode, nChild, vchChainCode);
169 void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
170 static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
171 unsigned char out[64];
173 CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
174 key.Set(&out[0], &out[32], true);
175 memcpy(vchChainCode, &out[32], 32);
179 memset(vchFingerprint, 0, sizeof(vchFingerprint));
182 CExtPubKey CExtKey::Neuter() const {
185 memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
187 ret.pubkey = key.GetPubKey();
188 memcpy(&ret.vchChainCode[0], &vchChainCode[0], 32);
192 void CExtKey::Encode(unsigned char code[74]) const {
194 memcpy(code+1, vchFingerprint, 4);
195 code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
196 code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
197 memcpy(code+9, vchChainCode, 32);
199 assert(key.size() == 32);
200 memcpy(code+42, key.begin(), 32);
203 void CExtKey::Decode(const unsigned char code[74]) {
205 memcpy(vchFingerprint, code+1, 4);
206 nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
207 memcpy(vchChainCode, code+9, 32);
208 key.Set(code+42, code+74, true);
211 bool ECC_InitSanityCheck() {
212 #if !defined(USE_SECP256K1)
213 if (!CECKey::SanityCheck()) {
218 key.MakeNewKey(true);
219 CPubKey pubkey = key.GetPubKey();
220 return key.VerifyPubKey(pubkey);