Master Core  v0.0.9 - 49a5c0d97abf09ef2911ddfe8d9551df59f9efd3-dirty
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
key.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 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 <openssl/bn.h>
8 #include <openssl/ecdsa.h>
9 #include <openssl/obj_mac.h>
10 #include <openssl/rand.h>
11 
12 // anonymous namespace with local implementation code (OpenSSL interaction)
13 namespace {
14 
15 // Generate a private key from just the secret parameter
16 int EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key)
17 {
18  int ok = 0;
19  BN_CTX *ctx = NULL;
20  EC_POINT *pub_key = NULL;
21 
22  if (!eckey) return 0;
23 
24  const EC_GROUP *group = EC_KEY_get0_group(eckey);
25 
26  if ((ctx = BN_CTX_new()) == NULL)
27  goto err;
28 
29  pub_key = EC_POINT_new(group);
30 
31  if (pub_key == NULL)
32  goto err;
33 
34  if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
35  goto err;
36 
37  EC_KEY_set_private_key(eckey,priv_key);
38  EC_KEY_set_public_key(eckey,pub_key);
39 
40  ok = 1;
41 
42 err:
43 
44  if (pub_key)
45  EC_POINT_free(pub_key);
46  if (ctx != NULL)
47  BN_CTX_free(ctx);
48 
49  return(ok);
50 }
51 
52 // Perform ECDSA key recovery (see SEC1 4.1.6) for curves over (mod p)-fields
53 // recid selects which key is recovered
54 // if check is non-zero, additional checks are performed
55 int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned char *msg, int msglen, int recid, int check)
56 {
57  if (!eckey) return 0;
58 
59  int ret = 0;
60  BN_CTX *ctx = NULL;
61 
62  BIGNUM *x = NULL;
63  BIGNUM *e = NULL;
64  BIGNUM *order = NULL;
65  BIGNUM *sor = NULL;
66  BIGNUM *eor = NULL;
67  BIGNUM *field = NULL;
68  EC_POINT *R = NULL;
69  EC_POINT *O = NULL;
70  EC_POINT *Q = NULL;
71  BIGNUM *rr = NULL;
72  BIGNUM *zero = NULL;
73  int n = 0;
74  int i = recid / 2;
75 
76  const EC_GROUP *group = EC_KEY_get0_group(eckey);
77  if ((ctx = BN_CTX_new()) == NULL) { ret = -1; goto err; }
78  BN_CTX_start(ctx);
79  order = BN_CTX_get(ctx);
80  if (!EC_GROUP_get_order(group, order, ctx)) { ret = -2; goto err; }
81  x = BN_CTX_get(ctx);
82  if (!BN_copy(x, order)) { ret=-1; goto err; }
83  if (!BN_mul_word(x, i)) { ret=-1; goto err; }
84  if (!BN_add(x, x, ecsig->r)) { ret=-1; goto err; }
85  field = BN_CTX_get(ctx);
86  if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) { ret=-2; goto err; }
87  if (BN_cmp(x, field) >= 0) { ret=0; goto err; }
88  if ((R = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
89  if (!EC_POINT_set_compressed_coordinates_GFp(group, R, x, recid % 2, ctx)) { ret=0; goto err; }
90  if (check)
91  {
92  if ((O = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
93  if (!EC_POINT_mul(group, O, NULL, R, order, ctx)) { ret=-2; goto err; }
94  if (!EC_POINT_is_at_infinity(group, O)) { ret = 0; goto err; }
95  }
96  if ((Q = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
97  n = EC_GROUP_get_degree(group);
98  e = BN_CTX_get(ctx);
99  if (!BN_bin2bn(msg, msglen, e)) { ret=-1; goto err; }
100  if (8*msglen > n) BN_rshift(e, e, 8-(n & 7));
101  zero = BN_CTX_get(ctx);
102  if (!BN_zero(zero)) { ret=-1; goto err; }
103  if (!BN_mod_sub(e, zero, e, order, ctx)) { ret=-1; goto err; }
104  rr = BN_CTX_get(ctx);
105  if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) { ret=-1; goto err; }
106  sor = BN_CTX_get(ctx);
107  if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) { ret=-1; goto err; }
108  eor = BN_CTX_get(ctx);
109  if (!BN_mod_mul(eor, e, rr, order, ctx)) { ret=-1; goto err; }
110  if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) { ret=-2; goto err; }
111  if (!EC_KEY_set_public_key(eckey, Q)) { ret=-2; goto err; }
112 
113  ret = 1;
114 
115 err:
116  if (ctx) {
117  BN_CTX_end(ctx);
118  BN_CTX_free(ctx);
119  }
120  if (R != NULL) EC_POINT_free(R);
121  if (O != NULL) EC_POINT_free(O);
122  if (Q != NULL) EC_POINT_free(Q);
123  return ret;
124 }
125 
126 // RAII Wrapper around OpenSSL's EC_KEY
127 class CECKey {
128 private:
129  EC_KEY *pkey;
130 
131 public:
132  CECKey() {
133  pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
134  assert(pkey != NULL);
135  }
136 
137  ~CECKey() {
138  EC_KEY_free(pkey);
139  }
140 
141  void GetSecretBytes(unsigned char vch[32]) const {
142  const BIGNUM *bn = EC_KEY_get0_private_key(pkey);
143  assert(bn);
144  int nBytes = BN_num_bytes(bn);
145  int n=BN_bn2bin(bn,&vch[32 - nBytes]);
146  assert(n == nBytes);
147  memset(vch, 0, 32 - nBytes);
148  }
149 
150  void SetSecretBytes(const unsigned char vch[32]) {
151  bool ret;
152  BIGNUM bn;
153  BN_init(&bn);
154  ret = BN_bin2bn(vch, 32, &bn);
155  assert(ret);
156  ret = EC_KEY_regenerate_key(pkey, &bn);
157  assert(ret);
158  BN_clear_free(&bn);
159  }
160 
161  void GetPrivKey(CPrivKey &privkey, bool fCompressed) {
162  EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED);
163  int nSize = i2d_ECPrivateKey(pkey, NULL);
164  assert(nSize);
165  privkey.resize(nSize);
166  unsigned char* pbegin = &privkey[0];
167  int nSize2 = i2d_ECPrivateKey(pkey, &pbegin);
168  assert(nSize == nSize2);
169  }
170 
171  bool SetPrivKey(const CPrivKey &privkey, bool fSkipCheck=false) {
172  const unsigned char* pbegin = &privkey[0];
173  if (d2i_ECPrivateKey(&pkey, &pbegin, privkey.size())) {
174  if(fSkipCheck)
175  return true;
176 
177  // d2i_ECPrivateKey returns true if parsing succeeds.
178  // This doesn't necessarily mean the key is valid.
179  if (EC_KEY_check_key(pkey))
180  return true;
181  }
182  return false;
183  }
184 
185  void GetPubKey(CPubKey &pubkey, bool fCompressed) {
186  EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED);
187  int nSize = i2o_ECPublicKey(pkey, NULL);
188  assert(nSize);
189  assert(nSize <= 65);
190  unsigned char c[65];
191  unsigned char *pbegin = c;
192  int nSize2 = i2o_ECPublicKey(pkey, &pbegin);
193  assert(nSize == nSize2);
194  pubkey.Set(&c[0], &c[nSize]);
195  }
196 
197  bool SetPubKey(const CPubKey &pubkey) {
198  const unsigned char* pbegin = pubkey.begin();
199  return o2i_ECPublicKey(&pkey, &pbegin, pubkey.size());
200  }
201 
202  bool Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) {
203  vchSig.clear();
204  ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
205  if (sig == NULL)
206  return false;
207  BN_CTX *ctx = BN_CTX_new();
208  BN_CTX_start(ctx);
209  const EC_GROUP *group = EC_KEY_get0_group(pkey);
210  BIGNUM *order = BN_CTX_get(ctx);
211  BIGNUM *halforder = BN_CTX_get(ctx);
212  EC_GROUP_get_order(group, order, ctx);
213  BN_rshift1(halforder, order);
214  if (BN_cmp(sig->s, halforder) > 0) {
215  // enforce low S values, by negating the value (modulo the order) if above order/2.
216  BN_sub(sig->s, order, sig->s);
217  }
218  BN_CTX_end(ctx);
219  BN_CTX_free(ctx);
220  unsigned int nSize = ECDSA_size(pkey);
221  vchSig.resize(nSize); // Make sure it is big enough
222  unsigned char *pos = &vchSig[0];
223  nSize = i2d_ECDSA_SIG(sig, &pos);
224  ECDSA_SIG_free(sig);
225  vchSig.resize(nSize); // Shrink to fit actual size
226  return true;
227  }
228 
229  bool Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
230  // -1 = error, 0 = bad sig, 1 = good
231  if (ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], vchSig.size(), pkey) != 1)
232  return false;
233  return true;
234  }
235 
236  bool SignCompact(const uint256 &hash, unsigned char *p64, int &rec) {
237  bool fOk = false;
238  ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
239  if (sig==NULL)
240  return false;
241  memset(p64, 0, 64);
242  int nBitsR = BN_num_bits(sig->r);
243  int nBitsS = BN_num_bits(sig->s);
244  if (nBitsR <= 256 && nBitsS <= 256) {
245  CPubKey pubkey;
246  GetPubKey(pubkey, true);
247  for (int i=0; i<4; i++) {
248  CECKey keyRec;
249  if (ECDSA_SIG_recover_key_GFp(keyRec.pkey, sig, (unsigned char*)&hash, sizeof(hash), i, 1) == 1) {
250  CPubKey pubkeyRec;
251  keyRec.GetPubKey(pubkeyRec, true);
252  if (pubkeyRec == pubkey) {
253  rec = i;
254  fOk = true;
255  break;
256  }
257  }
258  }
259  assert(fOk);
260  BN_bn2bin(sig->r,&p64[32-(nBitsR+7)/8]);
261  BN_bn2bin(sig->s,&p64[64-(nBitsS+7)/8]);
262  }
263  ECDSA_SIG_free(sig);
264  return fOk;
265  }
266 
267  // reconstruct public key from a compact signature
268  // This is only slightly more CPU intensive than just verifying it.
269  // If this function succeeds, the recovered public key is guaranteed to be valid
270  // (the signature is a valid signature of the given data for that key)
271  bool Recover(const uint256 &hash, const unsigned char *p64, int rec)
272  {
273  if (rec<0 || rec>=3)
274  return false;
275  ECDSA_SIG *sig = ECDSA_SIG_new();
276  BN_bin2bn(&p64[0], 32, sig->r);
277  BN_bin2bn(&p64[32], 32, sig->s);
278  bool ret = ECDSA_SIG_recover_key_GFp(pkey, sig, (unsigned char*)&hash, sizeof(hash), rec, 0) == 1;
279  ECDSA_SIG_free(sig);
280  return ret;
281  }
282 
283  static bool TweakSecret(unsigned char vchSecretOut[32], const unsigned char vchSecretIn[32], const unsigned char vchTweak[32])
284  {
285  bool ret = true;
286  BN_CTX *ctx = BN_CTX_new();
287  BN_CTX_start(ctx);
288  BIGNUM *bnSecret = BN_CTX_get(ctx);
289  BIGNUM *bnTweak = BN_CTX_get(ctx);
290  BIGNUM *bnOrder = BN_CTX_get(ctx);
291  EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);
292  EC_GROUP_get_order(group, bnOrder, ctx); // what a grossly inefficient way to get the (constant) group order...
293  BN_bin2bn(vchTweak, 32, bnTweak);
294  if (BN_cmp(bnTweak, bnOrder) >= 0)
295  ret = false; // extremely unlikely
296  BN_bin2bn(vchSecretIn, 32, bnSecret);
297  BN_add(bnSecret, bnSecret, bnTweak);
298  BN_nnmod(bnSecret, bnSecret, bnOrder, ctx);
299  if (BN_is_zero(bnSecret))
300  ret = false; // ridiculously unlikely
301  int nBits = BN_num_bits(bnSecret);
302  memset(vchSecretOut, 0, 32);
303  BN_bn2bin(bnSecret, &vchSecretOut[32-(nBits+7)/8]);
304  EC_GROUP_free(group);
305  BN_CTX_end(ctx);
306  BN_CTX_free(ctx);
307  return ret;
308  }
309 
310  bool TweakPublic(const unsigned char vchTweak[32]) {
311  bool ret = true;
312  BN_CTX *ctx = BN_CTX_new();
313  BN_CTX_start(ctx);
314  BIGNUM *bnTweak = BN_CTX_get(ctx);
315  BIGNUM *bnOrder = BN_CTX_get(ctx);
316  BIGNUM *bnOne = BN_CTX_get(ctx);
317  const EC_GROUP *group = EC_KEY_get0_group(pkey);
318  EC_GROUP_get_order(group, bnOrder, ctx); // what a grossly inefficient way to get the (constant) group order...
319  BN_bin2bn(vchTweak, 32, bnTweak);
320  if (BN_cmp(bnTweak, bnOrder) >= 0)
321  ret = false; // extremely unlikely
322  EC_POINT *point = EC_POINT_dup(EC_KEY_get0_public_key(pkey), group);
323  BN_one(bnOne);
324  EC_POINT_mul(group, point, bnTweak, point, bnOne, ctx);
325  if (EC_POINT_is_at_infinity(group, point))
326  ret = false; // ridiculously unlikely
327  EC_KEY_set_public_key(pkey, point);
328  EC_POINT_free(point);
329  BN_CTX_end(ctx);
330  BN_CTX_free(ctx);
331  return ret;
332  }
333 };
334 
335 }; // end of anonymous namespace
336 
337 bool CKey::Check(const unsigned char *vch) {
338  // Do not convert to OpenSSL's data structures for range-checking keys,
339  // it's easy enough to do directly.
340  static const unsigned char vchMax[32] = {
341  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
342  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
343  0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
344  0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x40
345  };
346  bool fIsZero = true;
347  for (int i=0; i<32 && fIsZero; i++)
348  if (vch[i] != 0)
349  fIsZero = false;
350  if (fIsZero)
351  return false;
352  for (int i=0; i<32; i++) {
353  if (vch[i] < vchMax[i])
354  return true;
355  if (vch[i] > vchMax[i])
356  return false;
357  }
358  return true;
359 }
360 
361 void CKey::MakeNewKey(bool fCompressedIn) {
362  do {
363  RAND_bytes(vch, sizeof(vch));
364  } while (!Check(vch));
365  fValid = true;
366  fCompressed = fCompressedIn;
367 }
368 
369 bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
370  CECKey key;
371  if (!key.SetPrivKey(privkey))
372  return false;
373  key.GetSecretBytes(vch);
374  fCompressed = fCompressedIn;
375  fValid = true;
376  return true;
377 }
378 
380  assert(fValid);
381  CECKey key;
382  key.SetSecretBytes(vch);
383  CPrivKey privkey;
384  key.GetPrivKey(privkey, fCompressed);
385  return privkey;
386 }
387 
389  assert(fValid);
390  CECKey key;
391  key.SetSecretBytes(vch);
392  CPubKey pubkey;
393  key.GetPubKey(pubkey, fCompressed);
394  return pubkey;
395 }
396 
397 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
398  if (!fValid)
399  return false;
400  CECKey key;
401  key.SetSecretBytes(vch);
402  return key.Sign(hash, vchSig);
403 }
404 
405 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
406  if (!fValid)
407  return false;
408  CECKey key;
409  key.SetSecretBytes(vch);
410  vchSig.resize(65);
411  int rec = -1;
412  if (!key.SignCompact(hash, &vchSig[1], rec))
413  return false;
414  assert(rec != -1);
415  vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
416  return true;
417 }
418 
419 bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
420  CECKey key;
421  if (!key.SetPrivKey(privkey, fSkipCheck))
422  return false;
423 
424  key.GetSecretBytes(vch);
425  fCompressed = vchPubKey.IsCompressed();
426  fValid = true;
427 
428  if (fSkipCheck)
429  return true;
430 
431  if (GetPubKey() != vchPubKey)
432  return false;
433 
434  return true;
435 }
436 
437 bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const {
438  if (!IsValid())
439  return false;
440  CECKey key;
441  if (!key.SetPubKey(*this))
442  return false;
443  if (!key.Verify(hash, vchSig))
444  return false;
445  return true;
446 }
447 
448 bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
449  if (vchSig.size() != 65)
450  return false;
451  CECKey key;
452  if (!key.Recover(hash, &vchSig[1], (vchSig[0] - 27) & ~4))
453  return false;
454  key.GetPubKey(*this, (vchSig[0] - 27) & 4);
455  return true;
456 }
457 
458 bool CPubKey::VerifyCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) const {
459  if (!IsValid())
460  return false;
461  if (vchSig.size() != 65)
462  return false;
463  CECKey key;
464  if (!key.Recover(hash, &vchSig[1], (vchSig[0] - 27) & ~4))
465  return false;
466  CPubKey pubkeyRec;
467  key.GetPubKey(pubkeyRec, IsCompressed());
468  if (*this != pubkeyRec)
469  return false;
470  return true;
471 }
472 
473 bool CPubKey::IsFullyValid() const {
474  if (!IsValid())
475  return false;
476  CECKey key;
477  if (!key.SetPubKey(*this))
478  return false;
479  return true;
480 }
481 
483  if (!IsValid())
484  return false;
485  CECKey key;
486  if (!key.SetPubKey(*this))
487  return false;
488  key.GetPubKey(*this, false);
489  return true;
490 }
491 
492 void static BIP32Hash(const unsigned char chainCode[32], unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]) {
493  unsigned char num[4];
494  num[0] = (nChild >> 24) & 0xFF;
495  num[1] = (nChild >> 16) & 0xFF;
496  num[2] = (nChild >> 8) & 0xFF;
497  num[3] = (nChild >> 0) & 0xFF;
498  HMAC_SHA512_CTX ctx;
499  HMAC_SHA512_Init(&ctx, chainCode, 32);
500  HMAC_SHA512_Update(&ctx, &header, 1);
501  HMAC_SHA512_Update(&ctx, data, 32);
502  HMAC_SHA512_Update(&ctx, num, 4);
503  HMAC_SHA512_Final(output, &ctx);
504 }
505 
506 bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
507  assert(IsValid());
508  assert(IsCompressed());
509  unsigned char out[64];
510  LockObject(out);
511  if ((nChild >> 31) == 0) {
512  CPubKey pubkey = GetPubKey();
513  assert(pubkey.begin() + 33 == pubkey.end());
514  BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out);
515  } else {
516  assert(begin() + 32 == end());
517  BIP32Hash(cc, nChild, 0, begin(), out);
518  }
519  memcpy(ccChild, out+32, 32);
520  bool ret = CECKey::TweakSecret((unsigned char*)keyChild.begin(), begin(), out);
521  UnlockObject(out);
522  keyChild.fCompressed = true;
523  keyChild.fValid = ret;
524  return ret;
525 }
526 
527 bool CPubKey::Derive(CPubKey& pubkeyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
528  assert(IsValid());
529  assert((nChild >> 31) == 0);
530  assert(begin() + 33 == end());
531  unsigned char out[64];
532  BIP32Hash(cc, nChild, *begin(), begin()+1, out);
533  memcpy(ccChild, out+32, 32);
534  CECKey key;
535  bool ret = key.SetPubKey(*this);
536  ret &= key.TweakPublic(out);
537  key.GetPubKey(pubkeyChild, true);
538  return ret;
539 }
540 
541 bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
542  out.nDepth = nDepth + 1;
543  CKeyID id = key.GetPubKey().GetID();
544  memcpy(&out.vchFingerprint[0], &id, 4);
545  out.nChild = nChild;
546  return key.Derive(out.key, out.vchChainCode, nChild, vchChainCode);
547 }
548 
549 void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
550  static const char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
551  HMAC_SHA512_CTX ctx;
552  HMAC_SHA512_Init(&ctx, hashkey, sizeof(hashkey));
553  HMAC_SHA512_Update(&ctx, seed, nSeedLen);
554  unsigned char out[64];
555  LockObject(out);
556  HMAC_SHA512_Final(out, &ctx);
557  key.Set(&out[0], &out[32], true);
558  memcpy(vchChainCode, &out[32], 32);
559  UnlockObject(out);
560  nDepth = 0;
561  nChild = 0;
562  memset(vchFingerprint, 0, sizeof(vchFingerprint));
563 }
564 
566  CExtPubKey ret;
567  ret.nDepth = nDepth;
568  memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
569  ret.nChild = nChild;
570  ret.pubkey = key.GetPubKey();
571  memcpy(&ret.vchChainCode[0], &vchChainCode[0], 32);
572  return ret;
573 }
574 
575 void CExtKey::Encode(unsigned char code[74]) const {
576  code[0] = nDepth;
577  memcpy(code+1, vchFingerprint, 4);
578  code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
579  code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
580  memcpy(code+9, vchChainCode, 32);
581  code[41] = 0;
582  assert(key.size() == 32);
583  memcpy(code+42, key.begin(), 32);
584 }
585 
586 void CExtKey::Decode(const unsigned char code[74]) {
587  nDepth = code[0];
588  memcpy(vchFingerprint, code+1, 4);
589  nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
590  memcpy(vchChainCode, code+9, 32);
591  key.Set(code+42, code+74, true);
592 }
593 
594 void CExtPubKey::Encode(unsigned char code[74]) const {
595  code[0] = nDepth;
596  memcpy(code+1, vchFingerprint, 4);
597  code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
598  code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
599  memcpy(code+9, vchChainCode, 32);
600  assert(pubkey.size() == 33);
601  memcpy(code+41, pubkey.begin(), 33);
602 }
603 
604 void CExtPubKey::Decode(const unsigned char code[74]) {
605  nDepth = code[0];
606  memcpy(vchFingerprint, code+1, 4);
607  nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
608  memcpy(vchChainCode, code+9, 32);
609  pubkey.Set(code+41, code+74);
610 }
611 
612 bool CExtPubKey::Derive(CExtPubKey &out, unsigned int nChild) const {
613  out.nDepth = nDepth + 1;
614  CKeyID id = pubkey.GetID();
615  memcpy(&out.vchFingerprint[0], &id, 4);
616  out.nChild = nChild;
617  return pubkey.Derive(out.pubkey, out.vchChainCode, nChild, vchChainCode);
618 }
619 
621  EC_KEY *pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
622  if(pkey == NULL)
623  return false;
624  EC_KEY_free(pkey);
625 
626  // TODO Is there more EC functionality that could be missing?
627  return true;
628 }
629 
630 
bool VerifyCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Definition: key.cpp:458
void UnlockObject(const T &t)
Definition: allocators.h:171
void Encode(unsigned char code[74]) const
Definition: key.cpp:575
unsigned char vchFingerprint[4]
Definition: key.h:276
const unsigned char * begin() const
Definition: key.h:234
unsigned char vchChainCode[32]
Definition: key.h:278
CExtPubKey Neuter() const
Definition: key.cpp:565
CKey key
Definition: key.h:296
const unsigned char * end() const
Definition: key.h:235
Definition: key.h:291
int HMAC_SHA512_Init(HMAC_SHA512_CTX *pctx, const void *pkey, size_t len)
Definition: hash.cpp:60
unsigned int size() const
Definition: key.h:90
void Set(const T pbegin, const T pend)
Definition: key.h:70
unsigned char vchFingerprint[4]
Definition: key.h:293
int HMAC_SHA512_Update(HMAC_SHA512_CTX *pctx, const void *pdata, size_t len)
Definition: hash.cpp:88
unsigned char nDepth
Definition: key.h:275
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Definition: key.cpp:397
void Decode(const unsigned char code[74])
Definition: key.cpp:586
bool fValid
Definition: key.h:183
unsigned int nChild
Definition: key.h:277
bool Derive(CExtPubKey &out, unsigned int nChild) const
Definition: key.cpp:612
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:541
bool IsValid() const
Definition: key.h:238
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
Definition: key.h:176
bool IsCompressed() const
Definition: key.h:241
void LockObject(const T &t)
Definition: allocators.h:167
CPubKey GetPubKey() const
Definition: key.cpp:388
CPrivKey GetPrivKey() const
Definition: key.cpp:379
bool Derive(CKey &keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const
Definition: key.cpp:506
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Definition: key.cpp:448
unsigned char nDepth
Definition: key.h:292
An encapsulated public key.
Definition: key.h:42
void MakeNewKey(bool fCompressed)
Definition: key.cpp:361
unsigned int nChild
Definition: key.h:294
unsigned char vchChainCode[32]
Definition: key.h:295
static const CScriptNum bnOne(1)
void Set(const T pbegin, const T pend, bool fCompressedIn)
Definition: key.h:218
int HMAC_SHA512_Final(unsigned char *pmd, HMAC_SHA512_CTX *pctx)
Definition: hash.cpp:93
const unsigned char * begin() const
Definition: key.h:91
bool Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck)
Definition: key.cpp:419
bool IsCompressed() const
Definition: key.h:151
void Decode(const unsigned char code[74])
Definition: key.cpp:604
256-bit unsigned integer
Definition: uint256.h:531
bool SetPrivKey(const CPrivKey &vchPrivKey, bool fCompressed)
Definition: key.cpp:369
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Definition: key.cpp:405
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:7
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:26
bool fCompressed
Definition: key.h:186
bool IsFullyValid() const
Definition: key.cpp:473
bool IsValid() const
Definition: key.h:143
void SetMaster(const unsigned char *seed, unsigned int nSeedLen)
Definition: key.cpp:549
CPubKey pubkey
Definition: key.h:279
static bool Check(const unsigned char *vch)
Definition: key.cpp:337
An encapsulated private key.
Definition: key.h:179
unsigned char vch[32]
Definition: key.h:189
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
Definition: key.cpp:620
static const CCheckpointData data
Definition: checkpoints.cpp:56
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Definition: key.cpp:437
void Encode(unsigned char code[74]) const
Definition: key.cpp:594
bool Derive(CPubKey &pubkeyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const
Definition: key.cpp:527
unsigned int size() const
Definition: key.h:233
CKeyID GetID() const
Definition: key.h:131
const unsigned char * end() const
Definition: key.h:92
static void BIP32Hash(const unsigned char chainCode[32], unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: key.cpp:492
bool Decompress()
Definition: key.cpp:482