Dash Core  0.12.2.1
P2P Digital Currency
key.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2015 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.
4 
5 #include "key.h"
6 
7 #include "arith_uint256.h"
8 #include "crypto/common.h"
9 #include "crypto/hmac_sha512.h"
10 #include "pubkey.h"
11 #include "random.h"
12 
13 #include <secp256k1.h>
14 #include <secp256k1_recovery.h>
15 
17 
19 static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen) {
20  const unsigned char *end = privkey + privkeylen;
21  int lenb = 0;
22  int len = 0;
23  memset(out32, 0, 32);
24  /* sequence header */
25  if (end < privkey+1 || *privkey != 0x30) {
26  return 0;
27  }
28  privkey++;
29  /* sequence length constructor */
30  if (end < privkey+1 || !(*privkey & 0x80)) {
31  return 0;
32  }
33  lenb = *privkey & ~0x80; privkey++;
34  if (lenb < 1 || lenb > 2) {
35  return 0;
36  }
37  if (end < privkey+lenb) {
38  return 0;
39  }
40  /* sequence length */
41  len = privkey[lenb-1] | (lenb > 1 ? privkey[lenb-2] << 8 : 0);
42  privkey += lenb;
43  if (end < privkey+len) {
44  return 0;
45  }
46  /* sequence element 0: version number (=1) */
47  if (end < privkey+3 || privkey[0] != 0x02 || privkey[1] != 0x01 || privkey[2] != 0x01) {
48  return 0;
49  }
50  privkey += 3;
51  /* sequence element 1: octet string, up to 32 bytes */
52  if (end < privkey+2 || privkey[0] != 0x04 || privkey[1] > 0x20 || end < privkey+2+privkey[1]) {
53  return 0;
54  }
55  memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
56  if (!secp256k1_ec_seckey_verify(ctx, out32)) {
57  memset(out32, 0, 32);
58  return 0;
59  }
60  return 1;
61 }
62 
63 static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed) {
64  secp256k1_pubkey pubkey;
65  size_t pubkeylen = 0;
66  if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
67  *privkeylen = 0;
68  return 0;
69  }
70  if (compressed) {
71  static const unsigned char begin[] = {
72  0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20
73  };
74  static const unsigned char middle[] = {
75  0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
76  0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
77  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
78  0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
79  0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
80  0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
81  0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
82  0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
83  0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00
84  };
85  unsigned char *ptr = privkey;
86  memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
87  memcpy(ptr, key32, 32); ptr += 32;
88  memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
89  pubkeylen = 33;
90  secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
91  ptr += pubkeylen;
92  *privkeylen = ptr - privkey;
93  } else {
94  static const unsigned char begin[] = {
95  0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
96  };
97  static const unsigned char middle[] = {
98  0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
99  0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
100  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
101  0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
102  0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
103  0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
104  0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11,
105  0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10,
106  0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
107  0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
108  0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00
109  };
110  unsigned char *ptr = privkey;
111  memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
112  memcpy(ptr, key32, 32); ptr += 32;
113  memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
114  pubkeylen = 65;
115  secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
116  ptr += pubkeylen;
117  *privkeylen = ptr - privkey;
118  }
119  return 1;
120 }
121 
122 bool CKey::Check(const unsigned char *vch) {
124 }
125 
126 void CKey::MakeNewKey(bool fCompressedIn) {
128  do {
129  GetRandBytes(vch, sizeof(vch));
130  } while (!Check(vch));
131  fValid = true;
132  fCompressed = fCompressedIn;
133 }
134 
135 bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
136  if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), &privkey[0], privkey.size()))
137  return false;
138  fCompressed = fCompressedIn;
139  fValid = true;
140  return true;
141 }
142 
144  assert(fValid);
145  CPrivKey privkey;
146  int ret;
147  size_t privkeylen;
148  privkey.resize(279);
149  privkeylen = 279;
151  assert(ret);
152  privkey.resize(privkeylen);
153  return privkey;
154 }
155 
157  assert(fValid);
158  secp256k1_pubkey pubkey;
159  size_t clen = 65;
160  CPubKey result;
162  assert(ret);
164  assert(result.size() == clen);
165  assert(result.IsValid());
166  return result;
167 }
168 
169 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
170  if (!fValid)
171  return false;
172  vchSig.resize(72);
173  size_t nSigLen = 72;
174  unsigned char extra_entropy[32] = {0};
175  WriteLE32(extra_entropy, test_case);
177  int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, test_case ? extra_entropy : NULL);
178  assert(ret);
179  secp256k1_ecdsa_signature_serialize_der(secp256k1_context_sign, (unsigned char*)&vchSig[0], &nSigLen, &sig);
180  vchSig.resize(nSigLen);
181  return true;
182 }
183 
184 bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
185  if (pubkey.IsCompressed() != fCompressed) {
186  return false;
187  }
188  unsigned char rnd[8];
189  std::string str = "Bitcoin key verification\n";
190  GetRandBytes(rnd, sizeof(rnd));
191  uint256 hash;
192  CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
193  std::vector<unsigned char> vchSig;
194  Sign(hash, vchSig);
195  return pubkey.Verify(hash, vchSig);
196 }
197 
198 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
199  if (!fValid)
200  return false;
201  vchSig.resize(65);
202  int rec = -1;
205  assert(ret);
207  assert(ret);
208  assert(rec != -1);
209  vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
210  return true;
211 }
212 
213 bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
214  if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), &privkey[0], privkey.size()))
215  return false;
216  fCompressed = vchPubKey.IsCompressed();
217  fValid = true;
218 
219  if (fSkipCheck)
220  return true;
221 
222  return VerifyPubKey(vchPubKey);
223 }
224 
225 bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
226  assert(IsValid());
227  assert(IsCompressed());
228  unsigned char out[64];
229  LockObject(out);
230  if ((nChild >> 31) == 0) {
231  CPubKey pubkey = GetPubKey();
232  assert(pubkey.begin() + 33 == pubkey.end());
233  BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out);
234  } else {
235  assert(begin() + 32 == end());
236  BIP32Hash(cc, nChild, 0, begin(), out);
237  }
238  memcpy(ccChild.begin(), out+32, 32);
239  memcpy((unsigned char*)keyChild.begin(), begin(), 32);
240  bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context_sign, (unsigned char*)keyChild.begin(), out);
241  UnlockObject(out);
242  keyChild.fCompressed = true;
243  keyChild.fValid = ret;
244  return ret;
245 }
246 
247 bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
248  out.nDepth = nDepth + 1;
249  CKeyID id = key.GetPubKey().GetID();
250  memcpy(&out.vchFingerprint[0], &id, 4);
251  out.nChild = nChild;
252  return key.Derive(out.key, out.chaincode, nChild, chaincode);
253 }
254 
255 void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
256  static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
257  unsigned char out[64];
258  LockObject(out);
259  CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
260  key.Set(&out[0], &out[32], true);
261  memcpy(chaincode.begin(), &out[32], 32);
262  UnlockObject(out);
263  nDepth = 0;
264  nChild = 0;
265  memset(vchFingerprint, 0, sizeof(vchFingerprint));
266 }
267 
269  CExtPubKey ret;
270  ret.nDepth = nDepth;
271  memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
272  ret.nChild = nChild;
273  ret.pubkey = key.GetPubKey();
274  ret.chaincode = chaincode;
275  return ret;
276 }
277 
278 void CExtKey::Encode(unsigned char code[74]) const {
279  code[0] = nDepth;
280  memcpy(code+1, vchFingerprint, 4);
281  code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
282  code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
283  memcpy(code+9, chaincode.begin(), 32);
284  code[41] = 0;
285  assert(key.size() == 32);
286  memcpy(code+42, key.begin(), 32);
287 }
288 
289 void CExtKey::Decode(const unsigned char code[74]) {
290  nDepth = code[0];
291  memcpy(vchFingerprint, code+1, 4);
292  nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
293  memcpy(chaincode.begin(), code+9, 32);
294  key.Set(code+42, code+74, true);
295 }
296 
298  CKey key;
299  key.MakeNewKey(true);
300  CPubKey pubkey = key.GetPubKey();
301  return key.VerifyPubKey(pubkey);
302 }
303 
304 void ECC_Start() {
305  assert(secp256k1_context_sign == NULL);
306 
308  assert(ctx != NULL);
309 
310  {
311  // Pass in a random blinding seed to the secp256k1 context.
312  unsigned char seed[32];
313  LockObject(seed);
314  GetRandBytes(seed, 32);
315  bool ret = secp256k1_context_randomize(ctx, seed);
316  assert(ret);
317  UnlockObject(seed);
318  }
319 
321 }
322 
323 void ECC_Stop() {
325  secp256k1_context_sign = NULL;
326 
327  if (ctx) {
329  }
330 }
unsigned int nChild
Definition: pubkey.h:198
void ECC_Start()
Definition: key.cpp:304
void SetMaster(const unsigned char *seed, unsigned int nSeedLen)
Definition: key.cpp:255
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:79
bool SetPrivKey(const CPrivKey &vchPrivKey, bool fCompressed)
Initialize from a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:135
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Definition: secp256k1.c:527
void RandAddSeedPerfmon()
Definition: random.cpp:46
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:43
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:247
static void WriteLE32(unsigned char *ptr, uint32_t x)
Definition: common.h:36
bool VerifyPubKey(const CPubKey &vchPubKey) const
Definition: key.cpp:184
CHMAC_SHA512 & Write(const unsigned char *data, size_t len)
Definition: hmac_sha512.h:24
static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed)
Definition: key.cpp:63
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, uint32_t test_case=0) const
Definition: key.cpp:169
Definition: pubkey.h:27
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition: secp256k1.c:168
#define SECP256K1_CONTEXT_SIGN
Definition: secp256k1.h:161
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Definition: secp256k1.c:432
unsigned char * begin()
Definition: uint256.h:55
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:144
void UnlockObject(const T &t)
Definition: pagelocker.h:171
CPrivKey GetPrivKey() const
Definition: key.cpp:143
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
Definition: secp256k1.c:346
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Definition: secp256k1.c:94
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:225
void Encode(unsigned char code[74]) const
Definition: key.cpp:278
bool Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:213
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Definition: secp256k1.c:409
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
Definition: key.h:32
#define SECP256K1_EC_UNCOMPRESSED
Definition: secp256k1.h:166
#define SECP256K1_EC_COMPRESSED
Definition: secp256k1.h:165
CKey key
Definition: key.h:159
const unsigned char * begin() const
Definition: pubkey.h:96
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Definition: secp256k1.c:395
CExtPubKey Neuter() const
Definition: key.cpp:268
unsigned char vchFingerprint[4]
Definition: key.h:156
void ECC_Stop()
Definition: key.cpp:323
ChainCode chaincode
Definition: key.h:158
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:75
static secp256k1_context * ctx
Definition: tests.c:42
ChainCode chaincode
Definition: pubkey.h:199
SECP256K1_API int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition: secp256k1.c:349
SECP256K1_API int secp256k1_ecdsa_sign_recoverable(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition: main_impl.h:123
unsigned char vchFingerprint[4]
Definition: pubkey.h:197
SECP256K1_API int secp256k1_ecdsa_recoverable_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, int *recid, const secp256k1_ecdsa_recoverable_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition: main_impl.h:60
static int ec_privkey_import_der(const secp256k1_context *ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen)
Definition: key.cpp:19
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Definition: pubkey.cpp:167
const unsigned char * end() const
Definition: pubkey.h:97
void Decode(const unsigned char code[74])
Definition: key.cpp:289
const unsigned char * begin() const
Definition: key.h:96
static secp256k1_context * secp256k1_context_sign
Definition: key.cpp:16
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to be vch is valid keydata.
Definition: key.cpp:122
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hmac_sha512.cpp:29
CPubKey pubkey
Definition: pubkey.h:200
SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition: secp256k1.c:253
bool fValid
Definition: key.h:40
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:169
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:103
void * memcpy(void *a, const void *b, size_t c)
Definition: hash.h:74
const unsigned char * end() const
Definition: key.h:97
unsigned char nDepth
Definition: key.h:155
CPubKey GetPubKey() const
Definition: key.cpp:156
unsigned char vch[32]
The actual byte data.
Definition: key.h:46
void GetRandBytes(unsigned char *buf, int num)
Definition: random.cpp:86
Definition: pubkey.h:37
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:95
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:100
bool ECC_InitSanityCheck()
Definition: key.cpp:297
Definition: key.h:35
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Definition: key.cpp:198
unsigned int nChild
Definition: key.h:157
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Definition: secp256k1.c:60
unsigned char nDepth
Definition: pubkey.h:196
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:126
void LockObject(const T &t)
Definition: pagelocker.h:165
Definition: key.h:154
CHash256 & Write(const unsigned char *data, size_t len)
Definition: hash.h:86
result
Definition: rpcuser.py:37