]> Git Repo - VerusCoin.git/blob - src/key.cpp
Clearly separate PUSHDATA and numeric argument MINIMALDATA tests
[VerusCoin.git] / src / key.cpp
1 // Copyright (c) 2009-2014 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 "crypto/sha2.h"
8 #include "random.h"
9
10 #ifdef USE_SECP256K1
11 #include <secp256k1.h>
12 #else
13 #include <openssl/bn.h>
14 #include <openssl/ecdsa.h>
15 #include <openssl/obj_mac.h>
16 #endif
17
18 // anonymous namespace with local implementation code (OpenSSL interaction)
19 namespace {
20
21 #ifdef USE_SECP256K1
22 #include <secp256k1.h>
23 class CSecp256k1Init {
24 public:
25     CSecp256k1Init() {
26         secp256k1_start();
27     }
28     ~CSecp256k1Init() {
29         secp256k1_stop();
30     }
31 };
32 static CSecp256k1Init instance_of_csecp256k1;
33
34 #else
35
36 // Generate a private key from just the secret parameter
37 int EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key)
38 {
39     int ok = 0;
40     BN_CTX *ctx = NULL;
41     EC_POINT *pub_key = NULL;
42
43     if (!eckey) return 0;
44
45     const EC_GROUP *group = EC_KEY_get0_group(eckey);
46
47     if ((ctx = BN_CTX_new()) == NULL)
48         goto err;
49
50     pub_key = EC_POINT_new(group);
51
52     if (pub_key == NULL)
53         goto err;
54
55     if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
56         goto err;
57
58     EC_KEY_set_private_key(eckey,priv_key);
59     EC_KEY_set_public_key(eckey,pub_key);
60
61     ok = 1;
62
63 err:
64
65     if (pub_key)
66         EC_POINT_free(pub_key);
67     if (ctx != NULL)
68         BN_CTX_free(ctx);
69
70     return(ok);
71 }
72
73 // Perform ECDSA key recovery (see SEC1 4.1.6) for curves over (mod p)-fields
74 // recid selects which key is recovered
75 // if check is non-zero, additional checks are performed
76 int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned char *msg, int msglen, int recid, int check)
77 {
78     if (!eckey) return 0;
79
80     int ret = 0;
81     BN_CTX *ctx = NULL;
82
83     BIGNUM *x = NULL;
84     BIGNUM *e = NULL;
85     BIGNUM *order = NULL;
86     BIGNUM *sor = NULL;
87     BIGNUM *eor = NULL;
88     BIGNUM *field = NULL;
89     EC_POINT *R = NULL;
90     EC_POINT *O = NULL;
91     EC_POINT *Q = NULL;
92     BIGNUM *rr = NULL;
93     BIGNUM *zero = NULL;
94     int n = 0;
95     int i = recid / 2;
96
97     const EC_GROUP *group = EC_KEY_get0_group(eckey);
98     if ((ctx = BN_CTX_new()) == NULL) { ret = -1; goto err; }
99     BN_CTX_start(ctx);
100     order = BN_CTX_get(ctx);
101     if (!EC_GROUP_get_order(group, order, ctx)) { ret = -2; goto err; }
102     x = BN_CTX_get(ctx);
103     if (!BN_copy(x, order)) { ret=-1; goto err; }
104     if (!BN_mul_word(x, i)) { ret=-1; goto err; }
105     if (!BN_add(x, x, ecsig->r)) { ret=-1; goto err; }
106     field = BN_CTX_get(ctx);
107     if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) { ret=-2; goto err; }
108     if (BN_cmp(x, field) >= 0) { ret=0; goto err; }
109     if ((R = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
110     if (!EC_POINT_set_compressed_coordinates_GFp(group, R, x, recid % 2, ctx)) { ret=0; goto err; }
111     if (check)
112     {
113         if ((O = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
114         if (!EC_POINT_mul(group, O, NULL, R, order, ctx)) { ret=-2; goto err; }
115         if (!EC_POINT_is_at_infinity(group, O)) { ret = 0; goto err; }
116     }
117     if ((Q = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
118     n = EC_GROUP_get_degree(group);
119     e = BN_CTX_get(ctx);
120     if (!BN_bin2bn(msg, msglen, e)) { ret=-1; goto err; }
121     if (8*msglen > n) BN_rshift(e, e, 8-(n & 7));
122     zero = BN_CTX_get(ctx);
123     if (!BN_zero(zero)) { ret=-1; goto err; }
124     if (!BN_mod_sub(e, zero, e, order, ctx)) { ret=-1; goto err; }
125     rr = BN_CTX_get(ctx);
126     if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) { ret=-1; goto err; }
127     sor = BN_CTX_get(ctx);
128     if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) { ret=-1; goto err; }
129     eor = BN_CTX_get(ctx);
130     if (!BN_mod_mul(eor, e, rr, order, ctx)) { ret=-1; goto err; }
131     if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) { ret=-2; goto err; }
132     if (!EC_KEY_set_public_key(eckey, Q)) { ret=-2; goto err; }
133
134     ret = 1;
135
136 err:
137     if (ctx) {
138         BN_CTX_end(ctx);
139         BN_CTX_free(ctx);
140     }
141     if (R != NULL) EC_POINT_free(R);
142     if (O != NULL) EC_POINT_free(O);
143     if (Q != NULL) EC_POINT_free(Q);
144     return ret;
145 }
146
147 // RAII Wrapper around OpenSSL's EC_KEY
148 class CECKey {
149 private:
150     EC_KEY *pkey;
151
152 public:
153     CECKey() {
154         pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
155         assert(pkey != NULL);
156     }
157
158     ~CECKey() {
159         EC_KEY_free(pkey);
160     }
161
162     void GetSecretBytes(unsigned char vch[32]) const {
163         const BIGNUM *bn = EC_KEY_get0_private_key(pkey);
164         assert(bn);
165         int nBytes = BN_num_bytes(bn);
166         int n=BN_bn2bin(bn,&vch[32 - nBytes]);
167         assert(n == nBytes);
168         memset(vch, 0, 32 - nBytes);
169     }
170
171     void SetSecretBytes(const unsigned char vch[32]) {
172         bool ret;
173         BIGNUM bn;
174         BN_init(&bn);
175         ret = BN_bin2bn(vch, 32, &bn) != NULL;
176         assert(ret);
177         ret = EC_KEY_regenerate_key(pkey, &bn) != 0;
178         assert(ret);
179         BN_clear_free(&bn);
180     }
181
182     int GetPrivKeySize(bool fCompressed) {
183         EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED);
184         return i2d_ECPrivateKey(pkey, NULL);
185     }
186     int GetPrivKey(unsigned char* privkey, bool fCompressed) {
187         EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED);
188         return i2d_ECPrivateKey(pkey, &privkey);
189     }
190
191     bool SetPrivKey(const unsigned char* privkey, size_t size, bool fSkipCheck=false) {
192         if (d2i_ECPrivateKey(&pkey, &privkey, size)) {
193             if(fSkipCheck)
194                 return true;
195
196             // d2i_ECPrivateKey returns true if parsing succeeds.
197             // This doesn't necessarily mean the key is valid.
198             if (EC_KEY_check_key(pkey))
199                 return true;
200         }
201         return false;
202     }
203
204     void GetPubKey(CPubKey &pubkey, bool fCompressed) {
205         EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED);
206         int nSize = i2o_ECPublicKey(pkey, NULL);
207         assert(nSize);
208         assert(nSize <= 65);
209         unsigned char c[65];
210         unsigned char *pbegin = c;
211         int nSize2 = i2o_ECPublicKey(pkey, &pbegin);
212         assert(nSize == nSize2);
213         pubkey.Set(&c[0], &c[nSize]);
214     }
215
216     bool SetPubKey(const CPubKey &pubkey) {
217         const unsigned char* pbegin = pubkey.begin();
218         return o2i_ECPublicKey(&pkey, &pbegin, pubkey.size()) != NULL;
219     }
220
221     bool Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool lowS) {
222         vchSig.clear();
223         ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
224         if (sig == NULL)
225             return false;
226         BN_CTX *ctx = BN_CTX_new();
227         BN_CTX_start(ctx);
228         const EC_GROUP *group = EC_KEY_get0_group(pkey);
229         BIGNUM *order = BN_CTX_get(ctx);
230         BIGNUM *halforder = BN_CTX_get(ctx);
231         EC_GROUP_get_order(group, order, ctx);
232         BN_rshift1(halforder, order);
233         if (lowS && BN_cmp(sig->s, halforder) > 0) {
234             // enforce low S values, by negating the value (modulo the order) if above order/2.
235             BN_sub(sig->s, order, sig->s);
236         }
237         BN_CTX_end(ctx);
238         BN_CTX_free(ctx);
239         unsigned int nSize = ECDSA_size(pkey);
240         vchSig.resize(nSize); // Make sure it is big enough
241         unsigned char *pos = &vchSig[0];
242         nSize = i2d_ECDSA_SIG(sig, &pos);
243         ECDSA_SIG_free(sig);
244         vchSig.resize(nSize); // Shrink to fit actual size
245         return true;
246     }
247
248     bool Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
249         // -1 = error, 0 = bad sig, 1 = good
250         if (ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], vchSig.size(), pkey) != 1)
251             return false;
252         return true;
253     }
254
255     bool SignCompact(const uint256 &hash, unsigned char *p64, int &rec) {
256         bool fOk = false;
257         ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
258         if (sig==NULL)
259             return false;
260         memset(p64, 0, 64);
261         int nBitsR = BN_num_bits(sig->r);
262         int nBitsS = BN_num_bits(sig->s);
263         if (nBitsR <= 256 && nBitsS <= 256) {
264             CPubKey pubkey;
265             GetPubKey(pubkey, true);
266             for (int i=0; i<4; i++) {
267                 CECKey keyRec;
268                 if (ECDSA_SIG_recover_key_GFp(keyRec.pkey, sig, (unsigned char*)&hash, sizeof(hash), i, 1) == 1) {
269                     CPubKey pubkeyRec;
270                     keyRec.GetPubKey(pubkeyRec, true);
271                     if (pubkeyRec == pubkey) {
272                         rec = i;
273                         fOk = true;
274                         break;
275                     }
276                 }
277             }
278             assert(fOk);
279             BN_bn2bin(sig->r,&p64[32-(nBitsR+7)/8]);
280             BN_bn2bin(sig->s,&p64[64-(nBitsS+7)/8]);
281         }
282         ECDSA_SIG_free(sig);
283         return fOk;
284     }
285
286     // reconstruct public key from a compact signature
287     // This is only slightly more CPU intensive than just verifying it.
288     // If this function succeeds, the recovered public key is guaranteed to be valid
289     // (the signature is a valid signature of the given data for that key)
290     bool Recover(const uint256 &hash, const unsigned char *p64, int rec)
291     {
292         if (rec<0 || rec>=3)
293             return false;
294         ECDSA_SIG *sig = ECDSA_SIG_new();
295         BN_bin2bn(&p64[0],  32, sig->r);
296         BN_bin2bn(&p64[32], 32, sig->s);
297         bool ret = ECDSA_SIG_recover_key_GFp(pkey, sig, (unsigned char*)&hash, sizeof(hash), rec, 0) == 1;
298         ECDSA_SIG_free(sig);
299         return ret;
300     }
301
302     static bool TweakSecret(unsigned char vchSecretOut[32], const unsigned char vchSecretIn[32], const unsigned char vchTweak[32])
303     {
304         bool ret = true;
305         BN_CTX *ctx = BN_CTX_new();
306         BN_CTX_start(ctx);
307         BIGNUM *bnSecret = BN_CTX_get(ctx);
308         BIGNUM *bnTweak = BN_CTX_get(ctx);
309         BIGNUM *bnOrder = BN_CTX_get(ctx);
310         EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);
311         EC_GROUP_get_order(group, bnOrder, ctx); // what a grossly inefficient way to get the (constant) group order...
312         BN_bin2bn(vchTweak, 32, bnTweak);
313         if (BN_cmp(bnTweak, bnOrder) >= 0)
314             ret = false; // extremely unlikely
315         BN_bin2bn(vchSecretIn, 32, bnSecret);
316         BN_add(bnSecret, bnSecret, bnTweak);
317         BN_nnmod(bnSecret, bnSecret, bnOrder, ctx);
318         if (BN_is_zero(bnSecret))
319             ret = false; // ridiculously unlikely
320         int nBits = BN_num_bits(bnSecret);
321         memset(vchSecretOut, 0, 32);
322         BN_bn2bin(bnSecret, &vchSecretOut[32-(nBits+7)/8]);
323         EC_GROUP_free(group);
324         BN_CTX_end(ctx);
325         BN_CTX_free(ctx);
326         return ret;
327     }
328
329     bool TweakPublic(const unsigned char vchTweak[32]) {
330         bool ret = true;
331         BN_CTX *ctx = BN_CTX_new();
332         BN_CTX_start(ctx);
333         BIGNUM *bnTweak = BN_CTX_get(ctx);
334         BIGNUM *bnOrder = BN_CTX_get(ctx);
335         BIGNUM *bnOne = BN_CTX_get(ctx);
336         const EC_GROUP *group = EC_KEY_get0_group(pkey);
337         EC_GROUP_get_order(group, bnOrder, ctx); // what a grossly inefficient way to get the (constant) group order...
338         BN_bin2bn(vchTweak, 32, bnTweak);
339         if (BN_cmp(bnTweak, bnOrder) >= 0)
340             ret = false; // extremely unlikely
341         EC_POINT *point = EC_POINT_dup(EC_KEY_get0_public_key(pkey), group);
342         BN_one(bnOne);
343         EC_POINT_mul(group, point, bnTweak, point, bnOne, ctx);
344         if (EC_POINT_is_at_infinity(group, point))
345             ret = false; // ridiculously unlikely
346         EC_KEY_set_public_key(pkey, point);
347         EC_POINT_free(point);
348         BN_CTX_end(ctx);
349         BN_CTX_free(ctx);
350         return ret;
351     }
352 };
353
354 #endif
355
356 int CompareBigEndian(const unsigned char *c1, size_t c1len, const unsigned char *c2, size_t c2len) {
357     while (c1len > c2len) {
358         if (*c1)
359             return 1;
360         c1++;
361         c1len--;
362     }
363     while (c2len > c1len) {
364         if (*c2)
365             return -1;
366         c2++;
367         c2len--;
368     }
369     while (c1len > 0) {
370         if (*c1 > *c2)
371             return 1;
372         if (*c2 > *c1)
373             return -1;
374         c1++;
375         c2++;
376         c1len--;
377     }
378     return 0;
379 }
380
381 // Order of secp256k1's generator minus 1.
382 const unsigned char vchMaxModOrder[32] = {
383     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
384     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
385     0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
386     0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x40
387 };
388
389 // Half of the order of secp256k1's generator minus 1.
390 const unsigned char vchMaxModHalfOrder[32] = {
391     0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
392     0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
393     0x5D,0x57,0x6E,0x73,0x57,0xA4,0x50,0x1D,
394     0xDF,0xE9,0x2F,0x46,0x68,0x1B,0x20,0xA0
395 };
396
397 const unsigned char vchZero[1] = {0};
398
399 } // anon namespace
400
401 bool CKey::Check(const unsigned char *vch) {
402     return CompareBigEndian(vch, 32, vchZero, 0) > 0 &&
403            CompareBigEndian(vch, 32, vchMaxModOrder, 32) <= 0;
404 }
405
406 bool CKey::CheckSignatureElement(const unsigned char *vch, int len, bool half) {
407     return CompareBigEndian(vch, len, vchZero, 0) > 0 &&
408            CompareBigEndian(vch, len, half ? vchMaxModHalfOrder : vchMaxModOrder, 32) <= 0;
409 }
410
411 void CKey::MakeNewKey(bool fCompressedIn) {
412     do {
413         GetRandBytes(vch, sizeof(vch));
414     } while (!Check(vch));
415     fValid = true;
416     fCompressed = fCompressedIn;
417 }
418
419 bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
420 #ifdef USE_SECP256K1
421     if (!secp256k1_ecdsa_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
422         return false;
423 #else
424     CECKey key;
425     if (!key.SetPrivKey(&privkey[0], privkey.size()))
426         return false;
427     key.GetSecretBytes(vch);
428 #endif
429     fCompressed = fCompressedIn;
430     fValid = true;
431     return true;
432 }
433
434 CPrivKey CKey::GetPrivKey() const {
435     assert(fValid);
436     CPrivKey privkey;
437     int privkeylen, ret;
438 #ifdef USE_SECP256K1
439     privkey.resize(279);
440     privkeylen = 279;
441     ret = secp256k1_ecdsa_privkey_export(begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed);
442     assert(ret);
443     privkey.resize(privkeylen);
444 #else
445     CECKey key;
446     key.SetSecretBytes(vch);
447     privkeylen = key.GetPrivKeySize(fCompressed);
448     assert(privkeylen);
449     privkey.resize(privkeylen);
450     ret = key.GetPrivKey(&privkey[0], fCompressed);
451     assert(ret == (int)privkey.size());
452 #endif
453     return privkey;
454 }
455
456 CPubKey CKey::GetPubKey() const {
457     assert(fValid);
458     CPubKey pubkey;
459 #ifdef USE_SECP256K1
460     int clen = 65;
461     int ret = secp256k1_ecdsa_pubkey_create((unsigned char*)pubkey.begin(), &clen, begin(), fCompressed);
462     assert(ret);
463     assert(pubkey.IsValid());
464     assert((int)pubkey.size() == clen);
465 #else
466     CECKey key;
467     key.SetSecretBytes(vch);
468     key.GetPubKey(pubkey, fCompressed);
469 #endif
470     return pubkey;
471 }
472
473 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool lowS) const {
474     if (!fValid)
475         return false;
476 #ifdef USE_SECP256K1
477     vchSig.resize(72);
478     int nSigLen = 72;
479     CKey nonce;
480     do {
481         nonce.MakeNewKey(true);
482         if (secp256k1_ecdsa_sign((const unsigned char*)&hash, 32, (unsigned char*)&vchSig[0], &nSigLen, begin(), nonce.begin()))
483             break;
484     } while(true);
485     vchSig.resize(nSigLen);
486     return true;
487 #else
488     CECKey key;
489     key.SetSecretBytes(vch);
490     return key.Sign(hash, vchSig, lowS);
491 #endif
492 }
493
494 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
495     if (!fValid)
496         return false;
497     vchSig.resize(65);
498     int rec = -1;
499 #ifdef USE_SECP256K1
500     CKey nonce;
501     do {
502         nonce.MakeNewKey(true);
503         if (secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, 32, &vchSig[1], begin(), nonce.begin(), &rec))
504             break;
505     } while(true);
506 #else
507     CECKey key;
508     key.SetSecretBytes(vch);
509     if (!key.SignCompact(hash, &vchSig[1], rec))
510         return false;
511 #endif
512     assert(rec != -1);
513     vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
514     return true;
515 }
516
517 bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
518 #ifdef USE_SECP256K1
519     if (!secp256k1_ecdsa_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
520         return false;
521 #else
522     CECKey key;
523     if (!key.SetPrivKey(&privkey[0], privkey.size(), fSkipCheck))
524         return false;
525     key.GetSecretBytes(vch);
526 #endif
527     fCompressed = vchPubKey.IsCompressed();
528     fValid = true;
529
530     if (fSkipCheck)
531         return true;
532
533     if (GetPubKey() != vchPubKey)
534         return false;
535
536     return true;
537 }
538
539 bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const {
540     if (!IsValid())
541         return false;
542 #ifdef USE_SECP256K1
543     if (secp256k1_ecdsa_verify((const unsigned char*)&hash, 32, &vchSig[0], vchSig.size(), begin(), size()) != 1)
544         return false;
545 #else
546     CECKey key;
547     if (!key.SetPubKey(*this))
548         return false;
549     if (!key.Verify(hash, vchSig))
550         return false;
551 #endif
552     return true;
553 }
554
555 bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
556     if (vchSig.size() != 65)
557         return false;
558     int recid = (vchSig[0] - 27) & 3;
559     bool fComp = ((vchSig[0] - 27) & 4) != 0;
560 #ifdef USE_SECP256K1
561     int pubkeylen = 65;
562     if (!secp256k1_ecdsa_recover_compact((const unsigned char*)&hash, 32, &vchSig[1], (unsigned char*)begin(), &pubkeylen, fComp, recid))
563         return false;
564     assert((int)size() == pubkeylen);
565 #else
566     CECKey key;
567     if (!key.Recover(hash, &vchSig[1], recid))
568         return false;
569     key.GetPubKey(*this, fComp);
570 #endif
571     return true;
572 }
573
574 bool CPubKey::IsFullyValid() const {
575     if (!IsValid())
576         return false;
577 #ifdef USE_SECP256K1
578     if (!secp256k1_ecdsa_pubkey_verify(begin(), size()))
579         return false;
580 #else
581     CECKey key;
582     if (!key.SetPubKey(*this))
583         return false;
584 #endif
585     return true;
586 }
587
588 bool CPubKey::Decompress() {
589     if (!IsValid())
590         return false;
591 #ifdef USE_SECP256K1
592     int clen = size();
593     int ret = secp256k1_ecdsa_pubkey_decompress((unsigned char*)begin(), &clen);
594     assert(ret);
595     assert(clen == (int)size());
596 #else
597     CECKey key;
598     if (!key.SetPubKey(*this))
599         return false;
600     key.GetPubKey(*this, false);
601 #endif
602     return true;
603 }
604
605 void static BIP32Hash(const unsigned char chainCode[32], unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]) {
606     unsigned char num[4];
607     num[0] = (nChild >> 24) & 0xFF;
608     num[1] = (nChild >> 16) & 0xFF;
609     num[2] = (nChild >>  8) & 0xFF;
610     num[3] = (nChild >>  0) & 0xFF;
611     CHMAC_SHA512(chainCode, 32).Write(&header, 1)
612                                .Write(data, 32)
613                                .Write(num, 4)
614                                .Finalize(output);
615 }
616
617 bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
618     assert(IsValid());
619     assert(IsCompressed());
620     unsigned char out[64];
621     LockObject(out);
622     if ((nChild >> 31) == 0) {
623         CPubKey pubkey = GetPubKey();
624         assert(pubkey.begin() + 33 == pubkey.end());
625         BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out);
626     } else {
627         assert(begin() + 32 == end());
628         BIP32Hash(cc, nChild, 0, begin(), out);
629     }
630     memcpy(ccChild, out+32, 32);
631 #ifdef USE_SECP256K1
632     memcpy((unsigned char*)keyChild.begin(), begin(), 32);
633     bool ret = secp256k1_ecdsa_privkey_tweak_add((unsigned char*)keyChild.begin(), out);
634 #else
635     bool ret = CECKey::TweakSecret((unsigned char*)keyChild.begin(), begin(), out);
636 #endif
637     UnlockObject(out);
638     keyChild.fCompressed = true;
639     keyChild.fValid = ret;
640     return ret;
641 }
642
643 bool CPubKey::Derive(CPubKey& pubkeyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
644     assert(IsValid());
645     assert((nChild >> 31) == 0);
646     assert(begin() + 33 == end());
647     unsigned char out[64];
648     BIP32Hash(cc, nChild, *begin(), begin()+1, out);
649     memcpy(ccChild, out+32, 32);
650 #ifdef USE_SECP256K1
651     pubkeyChild = *this;
652     bool ret = secp256k1_ecdsa_pubkey_tweak_add((unsigned char*)pubkeyChild.begin(), pubkeyChild.size(), out);
653 #else
654     CECKey key;
655     bool ret = key.SetPubKey(*this);
656     ret &= key.TweakPublic(out);
657     key.GetPubKey(pubkeyChild, true);
658 #endif
659     return ret;
660 }
661
662 bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
663     out.nDepth = nDepth + 1;
664     CKeyID id = key.GetPubKey().GetID();
665     memcpy(&out.vchFingerprint[0], &id, 4);
666     out.nChild = nChild;
667     return key.Derive(out.key, out.vchChainCode, nChild, vchChainCode);
668 }
669
670 void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
671     static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
672     unsigned char out[64];
673     LockObject(out);
674     CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
675     key.Set(&out[0], &out[32], true);
676     memcpy(vchChainCode, &out[32], 32);
677     UnlockObject(out);
678     nDepth = 0;
679     nChild = 0;
680     memset(vchFingerprint, 0, sizeof(vchFingerprint));
681 }
682
683 CExtPubKey CExtKey::Neuter() const {
684     CExtPubKey ret;
685     ret.nDepth = nDepth;
686     memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
687     ret.nChild = nChild;
688     ret.pubkey = key.GetPubKey();
689     memcpy(&ret.vchChainCode[0], &vchChainCode[0], 32);
690     return ret;
691 }
692
693 void CExtKey::Encode(unsigned char code[74]) const {
694     code[0] = nDepth;
695     memcpy(code+1, vchFingerprint, 4);
696     code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
697     code[7] = (nChild >>  8) & 0xFF; code[8] = (nChild >>  0) & 0xFF;
698     memcpy(code+9, vchChainCode, 32);
699     code[41] = 0;
700     assert(key.size() == 32);
701     memcpy(code+42, key.begin(), 32);
702 }
703
704 void CExtKey::Decode(const unsigned char code[74]) {
705     nDepth = code[0];
706     memcpy(vchFingerprint, code+1, 4);
707     nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
708     memcpy(vchChainCode, code+9, 32);
709     key.Set(code+42, code+74, true);
710 }
711
712 void CExtPubKey::Encode(unsigned char code[74]) const {
713     code[0] = nDepth;
714     memcpy(code+1, vchFingerprint, 4);
715     code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
716     code[7] = (nChild >>  8) & 0xFF; code[8] = (nChild >>  0) & 0xFF;
717     memcpy(code+9, vchChainCode, 32);
718     assert(pubkey.size() == 33);
719     memcpy(code+41, pubkey.begin(), 33);
720 }
721
722 void CExtPubKey::Decode(const unsigned char code[74]) {
723     nDepth = code[0];
724     memcpy(vchFingerprint, code+1, 4);
725     nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
726     memcpy(vchChainCode, code+9, 32);
727     pubkey.Set(code+41, code+74);
728 }
729
730 bool CExtPubKey::Derive(CExtPubKey &out, unsigned int nChild) const {
731     out.nDepth = nDepth + 1;
732     CKeyID id = pubkey.GetID();
733     memcpy(&out.vchFingerprint[0], &id, 4);
734     out.nChild = nChild;
735     return pubkey.Derive(out.pubkey, out.vchChainCode, nChild, vchChainCode);
736 }
737
738 bool ECC_InitSanityCheck() {
739 #ifdef USE_SECP256K1
740     return true;
741 #else
742     EC_KEY *pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
743     if(pkey == NULL)
744         return false;
745     EC_KEY_free(pkey);
746
747     // TODO Is there more EC functionality that could be missing?
748     return true;
749 #endif
750 }
This page took 0.063753 seconds and 4 git commands to generate.