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/hmac_sha512.h"
9 #include "crypto/rfc6979_hmac_sha256.h"
10 #include "eccryptoverify.h"
14 #include <secp256k1.h>
15 #include "ecwrapper.h"
17 //! anonymous namespace
20 class CSecp256k1Init {
23 secp256k1_start(SECP256K1_START_SIGN);
29 static CSecp256k1Init instance_of_csecp256k1;
33 bool CKey::Check(const unsigned char *vch) {
34 return eccrypto::Check(vch);
37 void CKey::MakeNewKey(bool fCompressedIn) {
40 GetRandBytes(vch, sizeof(vch));
41 } while (!Check(vch));
43 fCompressed = fCompressedIn;
46 bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
47 if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
49 fCompressed = fCompressedIn;
54 CPrivKey CKey::GetPrivKey() const {
60 ret = secp256k1_ec_privkey_export(begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed);
62 privkey.resize(privkeylen);
66 CPubKey CKey::GetPubKey() const {
70 int ret = secp256k1_ec_pubkey_create((unsigned char*)result.begin(), &clen, begin(), fCompressed);
71 assert((int)result.size() == clen);
73 assert(result.IsValid());
77 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
81 RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
84 prng.Generate((unsigned char*)&nonce, 32);
85 nonce = ArithToUint256(UintToArith256(nonce) + test_case);
87 int ret = secp256k1_ecdsa_sign((const unsigned char*)&hash, (unsigned char*)&vchSig[0], &nSigLen, begin(), (unsigned char*)&nonce);
90 vchSig.resize(nSigLen);
96 bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
97 if (pubkey.IsCompressed() != fCompressed) {
100 unsigned char rnd[8];
101 std::string str = "Bitcoin key verification\n";
102 GetRandBytes(rnd, sizeof(rnd));
104 CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize((unsigned char*)&hash);
105 std::vector<unsigned char> vchSig;
107 return pubkey.Verify(hash, vchSig);
110 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
115 RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
118 prng.Generate((unsigned char*)&nonce, 32);
119 int ret = secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, &vchSig[1], begin(), (unsigned char*)&nonce, &rec);
125 vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
129 bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
130 if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
132 fCompressed = vchPubKey.IsCompressed();
138 return VerifyPubKey(vchPubKey);
141 bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
143 assert(IsCompressed());
144 unsigned char out[64];
146 if ((nChild >> 31) == 0) {
147 CPubKey pubkey = GetPubKey();
148 assert(pubkey.begin() + 33 == pubkey.end());
149 BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out);
151 assert(begin() + 32 == end());
152 BIP32Hash(cc, nChild, 0, begin(), out);
154 memcpy(ccChild, out+32, 32);
155 memcpy((unsigned char*)keyChild.begin(), begin(), 32);
156 bool ret = secp256k1_ec_privkey_tweak_add((unsigned char*)keyChild.begin(), out);
158 keyChild.fCompressed = true;
159 keyChild.fValid = ret;
163 bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
164 out.nDepth = nDepth + 1;
165 CKeyID id = key.GetPubKey().GetID();
166 memcpy(&out.vchFingerprint[0], &id, 4);
168 return key.Derive(out.key, out.vchChainCode, nChild, vchChainCode);
171 void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
172 static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
173 unsigned char out[64];
175 CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
176 key.Set(&out[0], &out[32], true);
177 memcpy(vchChainCode, &out[32], 32);
181 memset(vchFingerprint, 0, sizeof(vchFingerprint));
184 CExtPubKey CExtKey::Neuter() const {
187 memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
189 ret.pubkey = key.GetPubKey();
190 memcpy(&ret.vchChainCode[0], &vchChainCode[0], 32);
194 void CExtKey::Encode(unsigned char code[74]) const {
196 memcpy(code+1, vchFingerprint, 4);
197 code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
198 code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
199 memcpy(code+9, vchChainCode, 32);
201 assert(key.size() == 32);
202 memcpy(code+42, key.begin(), 32);
205 void CExtKey::Decode(const unsigned char code[74]) {
207 memcpy(vchFingerprint, code+1, 4);
208 nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
209 memcpy(vchChainCode, code+9, 32);
210 key.Set(code+42, code+74, true);
213 bool ECC_InitSanityCheck() {
214 #if !defined(USE_SECP256K1)
215 if (!CECKey::SanityCheck()) {
220 key.MakeNewKey(true);
221 CPubKey pubkey = key.GetPubKey();
222 return key.VerifyPubKey(pubkey);