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) {
39 GetRandBytes(vch, sizeof(vch));
40 } while (!Check(vch));
42 fCompressed = fCompressedIn;
45 bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
46 if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
48 fCompressed = fCompressedIn;
53 CPrivKey CKey::GetPrivKey() const {
59 ret = secp256k1_ec_privkey_export(begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed);
61 privkey.resize(privkeylen);
65 CPubKey CKey::GetPubKey() const {
69 int ret = secp256k1_ec_pubkey_create((unsigned char*)result.begin(), &clen, begin(), fCompressed);
70 assert((int)result.size() == clen);
72 assert(result.IsValid());
76 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
80 RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
83 prng.Generate((unsigned char*)&nonce, 32);
86 int ret = secp256k1_ecdsa_sign((const unsigned char*)&hash, (unsigned char*)&vchSig[0], &nSigLen, begin(), (unsigned char*)&nonce);
89 vchSig.resize(nSigLen);
95 bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
96 if (pubkey.IsCompressed() != fCompressed) {
100 std::string str = "Bitcoin key verification\n";
101 GetRandBytes(rnd, sizeof(rnd));
103 CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize((unsigned char*)&hash);
104 std::vector<unsigned char> vchSig;
106 return pubkey.Verify(hash, vchSig);
109 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
114 RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
117 prng.Generate((unsigned char*)&nonce, 32);
118 int ret = secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, &vchSig[1], begin(), (unsigned char*)&nonce, &rec);
124 vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
128 bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
129 if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
131 fCompressed = vchPubKey.IsCompressed();
137 return VerifyPubKey(vchPubKey);
140 bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
142 assert(IsCompressed());
143 unsigned char out[64];
145 if ((nChild >> 31) == 0) {
146 CPubKey pubkey = GetPubKey();
147 assert(pubkey.begin() + 33 == pubkey.end());
148 BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out);
150 assert(begin() + 32 == end());
151 BIP32Hash(cc, nChild, 0, begin(), out);
153 memcpy(ccChild, out+32, 32);
154 memcpy((unsigned char*)keyChild.begin(), begin(), 32);
155 bool ret = secp256k1_ec_privkey_tweak_add((unsigned char*)keyChild.begin(), out);
157 keyChild.fCompressed = true;
158 keyChild.fValid = ret;
162 bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
163 out.nDepth = nDepth + 1;
164 CKeyID id = key.GetPubKey().GetID();
165 memcpy(&out.vchFingerprint[0], &id, 4);
167 return key.Derive(out.key, out.vchChainCode, nChild, vchChainCode);
170 void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
171 static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
172 unsigned char out[64];
174 CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
175 key.Set(&out[0], &out[32], true);
176 memcpy(vchChainCode, &out[32], 32);
180 memset(vchFingerprint, 0, sizeof(vchFingerprint));
183 CExtPubKey CExtKey::Neuter() const {
186 memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
188 ret.pubkey = key.GetPubKey();
189 memcpy(&ret.vchChainCode[0], &vchChainCode[0], 32);
193 void CExtKey::Encode(unsigned char code[74]) const {
195 memcpy(code+1, vchFingerprint, 4);
196 code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
197 code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
198 memcpy(code+9, vchChainCode, 32);
200 assert(key.size() == 32);
201 memcpy(code+42, key.begin(), 32);
204 void CExtKey::Decode(const unsigned char code[74]) {
206 memcpy(vchFingerprint, code+1, 4);
207 nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
208 memcpy(vchChainCode, code+9, 32);
209 key.Set(code+42, code+74, true);
212 bool ECC_InitSanityCheck() {
213 #if !defined(USE_SECP256K1)
214 if (!CECKey::SanityCheck()) {
219 key.MakeNewKey(true);
220 CPubKey pubkey = key.GetPubKey();
221 return key.VerifyPubKey(pubkey);