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 "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());
78 static int secp256k1_nonce_function_test_case(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, unsigned int attempt, const void *data)
80 const uint32_t *test_case = static_cast<const uint32_t*>(data);
82 secp256k1_nonce_function_rfc6979(nonce.begin(), msg32, key32, attempt, NULL);
83 nonce = ArithToUint256(UintToArith256(nonce) + *test_case);
84 memcpy(nonce32, nonce.begin(), 32);
89 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
94 int ret = secp256k1_ecdsa_sign(hash.begin(), (unsigned char*)&vchSig[0], &nSigLen, begin(), test_case == 0 ? secp256k1_nonce_function_rfc6979 : secp256k1_nonce_function_test_case, test_case == 0 ? NULL : &test_case);
96 vchSig.resize(nSigLen);
100 bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
101 if (pubkey.IsCompressed() != fCompressed) {
104 unsigned char rnd[8];
105 std::string str = "Bitcoin key verification\n";
106 GetRandBytes(rnd, sizeof(rnd));
108 CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
109 std::vector<unsigned char> vchSig;
111 return pubkey.Verify(hash, vchSig);
114 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
119 int ret = secp256k1_ecdsa_sign_compact(hash.begin(), &vchSig[1], begin(), secp256k1_nonce_function_rfc6979, NULL, &rec);
122 vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
126 bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
127 if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
129 fCompressed = vchPubKey.IsCompressed();
135 return VerifyPubKey(vchPubKey);
138 bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
140 assert(IsCompressed());
141 unsigned char out[64];
143 if ((nChild >> 31) == 0) {
144 CPubKey pubkey = GetPubKey();
145 assert(pubkey.begin() + 33 == pubkey.end());
146 BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out);
148 assert(begin() + 32 == end());
149 BIP32Hash(cc, nChild, 0, begin(), out);
151 memcpy(ccChild, out+32, 32);
152 memcpy((unsigned char*)keyChild.begin(), begin(), 32);
153 bool ret = secp256k1_ec_privkey_tweak_add((unsigned char*)keyChild.begin(), out);
155 keyChild.fCompressed = true;
156 keyChild.fValid = ret;
160 bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
161 out.nDepth = nDepth + 1;
162 CKeyID id = key.GetPubKey().GetID();
163 memcpy(&out.vchFingerprint[0], &id, 4);
165 return key.Derive(out.key, out.vchChainCode, nChild, vchChainCode);
168 void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
169 static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
170 unsigned char out[64];
172 CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
173 key.Set(&out[0], &out[32], true);
174 memcpy(vchChainCode, &out[32], 32);
178 memset(vchFingerprint, 0, sizeof(vchFingerprint));
181 CExtPubKey CExtKey::Neuter() const {
184 memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
186 ret.pubkey = key.GetPubKey();
187 memcpy(&ret.vchChainCode[0], &vchChainCode[0], 32);
191 void CExtKey::Encode(unsigned char code[74]) const {
193 memcpy(code+1, vchFingerprint, 4);
194 code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
195 code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
196 memcpy(code+9, vchChainCode, 32);
198 assert(key.size() == 32);
199 memcpy(code+42, key.begin(), 32);
202 void CExtKey::Decode(const unsigned char code[74]) {
204 memcpy(vchFingerprint, code+1, 4);
205 nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
206 memcpy(vchChainCode, code+9, 32);
207 key.Set(code+42, code+74, true);
210 bool ECC_InitSanityCheck() {
211 if (!CECKey::SanityCheck()) {
215 key.MakeNewKey(true);
216 CPubKey pubkey = key.GetPubKey();
217 return key.VerifyPubKey(pubkey);