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.
7 #include "crypto/sha2.h"
11 #include <secp256k1.h>
13 #include <openssl/bn.h>
14 #include <openssl/ecdsa.h>
15 #include <openssl/obj_mac.h>
18 // anonymous namespace with local implementation code (OpenSSL interaction)
22 #include <secp256k1.h>
23 class CSecp256k1Init {
32 static CSecp256k1Init instance_of_csecp256k1;
36 // Generate a private key from just the secret parameter
37 int EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key)
41 EC_POINT *pub_key = NULL;
45 const EC_GROUP *group = EC_KEY_get0_group(eckey);
47 if ((ctx = BN_CTX_new()) == NULL)
50 pub_key = EC_POINT_new(group);
55 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
58 EC_KEY_set_private_key(eckey,priv_key);
59 EC_KEY_set_public_key(eckey,pub_key);
66 EC_POINT_free(pub_key);
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)
97 const EC_GROUP *group = EC_KEY_get0_group(eckey);
98 if ((ctx = BN_CTX_new()) == NULL) { ret = -1; goto err; }
100 order = BN_CTX_get(ctx);
101 if (!EC_GROUP_get_order(group, order, ctx)) { ret = -2; goto err; }
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; }
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; }
117 if ((Q = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
118 n = EC_GROUP_get_degree(group);
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; }
141 if (R != NULL) EC_POINT_free(R);
142 if (O != NULL) EC_POINT_free(O);
143 if (Q != NULL) EC_POINT_free(Q);
147 // RAII Wrapper around OpenSSL's EC_KEY
154 pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
155 assert(pkey != NULL);
162 void GetSecretBytes(unsigned char vch[32]) const {
163 const BIGNUM *bn = EC_KEY_get0_private_key(pkey);
165 int nBytes = BN_num_bytes(bn);
166 int n=BN_bn2bin(bn,&vch[32 - nBytes]);
168 memset(vch, 0, 32 - nBytes);
171 void SetSecretBytes(const unsigned char vch[32]) {
175 ret = BN_bin2bn(vch, 32, &bn) != NULL;
177 ret = EC_KEY_regenerate_key(pkey, &bn) != 0;
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);
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);
191 bool SetPrivKey(const unsigned char* privkey, size_t size, bool fSkipCheck=false) {
192 if (d2i_ECPrivateKey(&pkey, &privkey, size)) {
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))
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);
210 unsigned char *pbegin = c;
211 int nSize2 = i2o_ECPublicKey(pkey, &pbegin);
212 assert(nSize == nSize2);
213 pubkey.Set(&c[0], &c[nSize]);
216 bool SetPubKey(const CPubKey &pubkey) {
217 const unsigned char* pbegin = pubkey.begin();
218 return o2i_ECPublicKey(&pkey, &pbegin, pubkey.size()) != NULL;
221 bool Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool lowS) {
223 ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
226 BN_CTX *ctx = BN_CTX_new();
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);
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);
244 vchSig.resize(nSize); // Shrink to fit actual size
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)
255 bool SignCompact(const uint256 &hash, unsigned char *p64, int &rec) {
257 ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
261 int nBitsR = BN_num_bits(sig->r);
262 int nBitsS = BN_num_bits(sig->s);
263 if (nBitsR <= 256 && nBitsS <= 256) {
265 GetPubKey(pubkey, true);
266 for (int i=0; i<4; i++) {
268 if (ECDSA_SIG_recover_key_GFp(keyRec.pkey, sig, (unsigned char*)&hash, sizeof(hash), i, 1) == 1) {
270 keyRec.GetPubKey(pubkeyRec, true);
271 if (pubkeyRec == pubkey) {
279 BN_bn2bin(sig->r,&p64[32-(nBitsR+7)/8]);
280 BN_bn2bin(sig->s,&p64[64-(nBitsS+7)/8]);
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)
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;
302 static bool TweakSecret(unsigned char vchSecretOut[32], const unsigned char vchSecretIn[32], const unsigned char vchTweak[32])
305 BN_CTX *ctx = BN_CTX_new();
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);
329 bool TweakPublic(const unsigned char vchTweak[32]) {
331 BN_CTX *ctx = BN_CTX_new();
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);
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);
356 int CompareBigEndian(const unsigned char *c1, size_t c1len, const unsigned char *c2, size_t c2len) {
357 while (c1len > c2len) {
363 while (c2len > c1len) {
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
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
397 const unsigned char vchZero[1] = {0};
401 bool CKey::Check(const unsigned char *vch) {
402 return CompareBigEndian(vch, 32, vchZero, 0) > 0 &&
403 CompareBigEndian(vch, 32, vchMaxModOrder, 32) <= 0;
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;
411 void CKey::MakeNewKey(bool fCompressedIn) {
413 GetRandBytes(vch, sizeof(vch));
414 } while (!Check(vch));
416 fCompressed = fCompressedIn;
419 bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
421 if (!secp256k1_ecdsa_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
425 if (!key.SetPrivKey(&privkey[0], privkey.size()))
427 key.GetSecretBytes(vch);
429 fCompressed = fCompressedIn;
434 CPrivKey CKey::GetPrivKey() const {
441 ret = secp256k1_ecdsa_privkey_export(begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed);
443 privkey.resize(privkeylen);
446 key.SetSecretBytes(vch);
447 privkeylen = key.GetPrivKeySize(fCompressed);
449 privkey.resize(privkeylen);
450 ret = key.GetPrivKey(&privkey[0], fCompressed);
451 assert(ret == (int)privkey.size());
456 CPubKey CKey::GetPubKey() const {
461 int ret = secp256k1_ecdsa_pubkey_create((unsigned char*)pubkey.begin(), &clen, begin(), fCompressed);
463 assert(pubkey.IsValid());
464 assert((int)pubkey.size() == clen);
467 key.SetSecretBytes(vch);
468 key.GetPubKey(pubkey, fCompressed);
473 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool lowS) const {
481 nonce.MakeNewKey(true);
482 if (secp256k1_ecdsa_sign((const unsigned char*)&hash, 32, (unsigned char*)&vchSig[0], &nSigLen, begin(), nonce.begin()))
485 vchSig.resize(nSigLen);
489 key.SetSecretBytes(vch);
490 return key.Sign(hash, vchSig, lowS);
494 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
502 nonce.MakeNewKey(true);
503 if (secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, 32, &vchSig[1], begin(), nonce.begin(), &rec))
508 key.SetSecretBytes(vch);
509 if (!key.SignCompact(hash, &vchSig[1], rec))
513 vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
517 bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
519 if (!secp256k1_ecdsa_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
523 if (!key.SetPrivKey(&privkey[0], privkey.size(), fSkipCheck))
525 key.GetSecretBytes(vch);
527 fCompressed = vchPubKey.IsCompressed();
533 if (GetPubKey() != vchPubKey)
539 bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const {
543 if (secp256k1_ecdsa_verify((const unsigned char*)&hash, 32, &vchSig[0], vchSig.size(), begin(), size()) != 1)
547 if (!key.SetPubKey(*this))
549 if (!key.Verify(hash, vchSig))
555 bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
556 if (vchSig.size() != 65)
558 int recid = (vchSig[0] - 27) & 3;
559 bool fComp = ((vchSig[0] - 27) & 4) != 0;
562 if (!secp256k1_ecdsa_recover_compact((const unsigned char*)&hash, 32, &vchSig[1], (unsigned char*)begin(), &pubkeylen, fComp, recid))
564 assert((int)size() == pubkeylen);
567 if (!key.Recover(hash, &vchSig[1], recid))
569 key.GetPubKey(*this, fComp);
574 bool CPubKey::IsFullyValid() const {
578 if (!secp256k1_ecdsa_pubkey_verify(begin(), size()))
582 if (!key.SetPubKey(*this))
588 bool CPubKey::Decompress() {
593 int ret = secp256k1_ecdsa_pubkey_decompress((unsigned char*)begin(), &clen);
595 assert(clen == (int)size());
598 if (!key.SetPubKey(*this))
600 key.GetPubKey(*this, false);
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)
617 bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
619 assert(IsCompressed());
620 unsigned char out[64];
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);
627 assert(begin() + 32 == end());
628 BIP32Hash(cc, nChild, 0, begin(), out);
630 memcpy(ccChild, out+32, 32);
632 memcpy((unsigned char*)keyChild.begin(), begin(), 32);
633 bool ret = secp256k1_ecdsa_privkey_tweak_add((unsigned char*)keyChild.begin(), out);
635 bool ret = CECKey::TweakSecret((unsigned char*)keyChild.begin(), begin(), out);
638 keyChild.fCompressed = true;
639 keyChild.fValid = ret;
643 bool CPubKey::Derive(CPubKey& pubkeyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
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);
652 bool ret = secp256k1_ecdsa_pubkey_tweak_add((unsigned char*)pubkeyChild.begin(), pubkeyChild.size(), out);
655 bool ret = key.SetPubKey(*this);
656 ret &= key.TweakPublic(out);
657 key.GetPubKey(pubkeyChild, true);
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);
667 return key.Derive(out.key, out.vchChainCode, nChild, vchChainCode);
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];
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);
680 memset(vchFingerprint, 0, sizeof(vchFingerprint));
683 CExtPubKey CExtKey::Neuter() const {
686 memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
688 ret.pubkey = key.GetPubKey();
689 memcpy(&ret.vchChainCode[0], &vchChainCode[0], 32);
693 void CExtKey::Encode(unsigned char code[74]) const {
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);
700 assert(key.size() == 32);
701 memcpy(code+42, key.begin(), 32);
704 void CExtKey::Decode(const unsigned char code[74]) {
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);
712 void CExtPubKey::Encode(unsigned char code[74]) const {
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);
722 void CExtPubKey::Decode(const unsigned char code[74]) {
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);
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);
735 return pubkey.Derive(out.pubkey, out.vchChainCode, nChild, vchChainCode);
738 bool ECC_InitSanityCheck() {
742 EC_KEY *pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
747 // TODO Is there more EC functionality that could be missing?