mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-20 13:37:28 +02:00
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it. Libsecp256k1 now has explicit context objects, which makes it completely thread-safe. In turn, keep an explicit context object in key.cpp, which is explicitly initialized destroyed. This is not really pretty now, but it's more efficient than the static initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of its calls, libsecp256k1 wasn't actually needed). This also brings in the new blinding support in libsecp256k1. By passing in a random seed, temporary variables during the elliptic curve computations are altered, in such a way that if an attacker does not know the blind, observing the internal operations leaks less information about the keys used. This was implemented by Greg Maxwell.
This commit is contained in:
parent
582b2934e6
commit
5c8529304a
7 changed files with 113 additions and 89 deletions
87
src/key.cpp
87
src/key.cpp
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include "key.h"
|
||||
|
||||
#include "crypto/common.h"
|
||||
#include "crypto/hmac_sha512.h"
|
||||
#include "crypto/rfc6979_hmac_sha256.h"
|
||||
#include "eccryptoverify.h"
|
||||
|
|
@ -13,21 +14,7 @@
|
|||
#include <secp256k1.h>
|
||||
#include "ecwrapper.h"
|
||||
|
||||
//! anonymous namespace
|
||||
namespace {
|
||||
|
||||
class CSecp256k1Init {
|
||||
public:
|
||||
CSecp256k1Init() {
|
||||
secp256k1_start(SECP256K1_START_SIGN);
|
||||
}
|
||||
~CSecp256k1Init() {
|
||||
secp256k1_stop();
|
||||
}
|
||||
};
|
||||
static CSecp256k1Init instance_of_csecp256k1;
|
||||
|
||||
} // anon namespace
|
||||
static secp256k1_context_t* secp256k1_context = NULL;
|
||||
|
||||
bool CKey::Check(const unsigned char *vch) {
|
||||
return eccrypto::Check(vch);
|
||||
|
|
@ -42,7 +29,7 @@ void CKey::MakeNewKey(bool fCompressedIn) {
|
|||
}
|
||||
|
||||
bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
|
||||
if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
|
||||
if (!secp256k1_ec_privkey_import(secp256k1_context, (unsigned char*)begin(), &privkey[0], privkey.size()))
|
||||
return false;
|
||||
fCompressed = fCompressedIn;
|
||||
fValid = true;
|
||||
|
|
@ -55,7 +42,7 @@ CPrivKey CKey::GetPrivKey() const {
|
|||
int privkeylen, ret;
|
||||
privkey.resize(279);
|
||||
privkeylen = 279;
|
||||
ret = secp256k1_ec_privkey_export(begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed);
|
||||
ret = secp256k1_ec_privkey_export(secp256k1_context, begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed);
|
||||
assert(ret);
|
||||
privkey.resize(privkeylen);
|
||||
return privkey;
|
||||
|
|
@ -65,7 +52,7 @@ CPubKey CKey::GetPubKey() const {
|
|||
assert(fValid);
|
||||
CPubKey result;
|
||||
int clen = 65;
|
||||
int ret = secp256k1_ec_pubkey_create((unsigned char*)result.begin(), &clen, begin(), fCompressed);
|
||||
int ret = secp256k1_ec_pubkey_create(secp256k1_context, (unsigned char*)result.begin(), &clen, begin(), fCompressed);
|
||||
assert((int)result.size() == clen);
|
||||
assert(ret);
|
||||
assert(result.IsValid());
|
||||
|
|
@ -76,19 +63,13 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_
|
|||
if (!fValid)
|
||||
return false;
|
||||
vchSig.resize(72);
|
||||
RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
|
||||
do {
|
||||
uint256 nonce;
|
||||
prng.Generate((unsigned char*)&nonce, 32);
|
||||
nonce += test_case;
|
||||
int nSigLen = 72;
|
||||
int ret = secp256k1_ecdsa_sign((const unsigned char*)&hash, 32, (unsigned char*)&vchSig[0], &nSigLen, begin(), (unsigned char*)&nonce);
|
||||
nonce = 0;
|
||||
if (ret) {
|
||||
vchSig.resize(nSigLen);
|
||||
return true;
|
||||
}
|
||||
} while(true);
|
||||
int nSigLen = 72;
|
||||
unsigned char extra_entropy[32] = {0};
|
||||
WriteLE32(extra_entropy, test_case);
|
||||
int ret = secp256k1_ecdsa_sign(secp256k1_context, hash.begin(), (unsigned char*)&vchSig[0], &nSigLen, begin(), secp256k1_nonce_function_rfc6979, test_case ? extra_entropy : NULL);
|
||||
assert(ret);
|
||||
vchSig.resize(nSigLen);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
|
||||
|
|
@ -110,22 +91,15 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
|
|||
return false;
|
||||
vchSig.resize(65);
|
||||
int rec = -1;
|
||||
RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
|
||||
do {
|
||||
uint256 nonce;
|
||||
prng.Generate((unsigned char*)&nonce, 32);
|
||||
int ret = secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, 32, &vchSig[1], begin(), (unsigned char*)&nonce, &rec);
|
||||
nonce = 0;
|
||||
if (ret)
|
||||
break;
|
||||
} while(true);
|
||||
int ret = secp256k1_ecdsa_sign_compact(secp256k1_context, hash.begin(), &vchSig[1], begin(), secp256k1_nonce_function_rfc6979, NULL, &rec);
|
||||
assert(ret);
|
||||
assert(rec != -1);
|
||||
vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
|
||||
if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
|
||||
if (!secp256k1_ec_privkey_import(secp256k1_context, (unsigned char*)begin(), &privkey[0], privkey.size()))
|
||||
return false;
|
||||
fCompressed = vchPubKey.IsCompressed();
|
||||
fValid = true;
|
||||
|
|
@ -151,7 +125,7 @@ bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild
|
|||
}
|
||||
memcpy(ccChild, out+32, 32);
|
||||
memcpy((unsigned char*)keyChild.begin(), begin(), 32);
|
||||
bool ret = secp256k1_ec_privkey_tweak_add((unsigned char*)keyChild.begin(), out);
|
||||
bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context, (unsigned char*)keyChild.begin(), out);
|
||||
UnlockObject(out);
|
||||
keyChild.fCompressed = true;
|
||||
keyChild.fValid = ret;
|
||||
|
|
@ -219,3 +193,32 @@ bool ECC_InitSanityCheck() {
|
|||
CPubKey pubkey = key.GetPubKey();
|
||||
return key.VerifyPubKey(pubkey);
|
||||
}
|
||||
|
||||
|
||||
void ECC_Start() {
|
||||
assert(secp256k1_context == NULL);
|
||||
|
||||
secp256k1_context_t *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
|
||||
assert(ctx != NULL);
|
||||
|
||||
{
|
||||
// Pass in a random blinding seed to the secp256k1 context.
|
||||
unsigned char seed[32];
|
||||
LockObject(seed);
|
||||
GetRandBytes(seed, 32);
|
||||
bool ret = secp256k1_context_randomize(ctx, seed);
|
||||
assert(ret);
|
||||
UnlockObject(seed);
|
||||
}
|
||||
|
||||
secp256k1_context = ctx;
|
||||
}
|
||||
|
||||
void ECC_Stop() {
|
||||
secp256k1_context_t *ctx = secp256k1_context;
|
||||
secp256k1_context = NULL;
|
||||
|
||||
if (ctx) {
|
||||
secp256k1_context_destroy(ctx);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue