Browse Source

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.
0.13
Pieter Wuille 10 years ago
parent
commit
a56054be65
  1. 13
      src/bitcoin-tx.cpp
  2. 4
      src/init.cpp
  3. 59
      src/key.cpp
  4. 8
      src/key.h
  5. 3
      src/test/test_bitcoin.cpp

13
src/bitcoin-tx.cpp

@ -444,9 +444,18 @@ static void MutateTxSign(CMutableTransaction& tx, const string& flagStr)
tx = mergedTx; tx = mergedTx;
} }
class Secp256k1Init
{
public:
Secp256k1Init() { ECC_Start(); }
~Secp256k1Init() { ECC_Stop(); }
};
static void MutateTx(CMutableTransaction& tx, const string& command, static void MutateTx(CMutableTransaction& tx, const string& command,
const string& commandVal) const string& commandVal)
{ {
boost::scoped_ptr<Secp256k1Init> ecc;
if (command == "nversion") if (command == "nversion")
MutateTxVersion(tx, commandVal); MutateTxVersion(tx, commandVal);
else if (command == "locktime") else if (command == "locktime")
@ -464,8 +473,10 @@ static void MutateTx(CMutableTransaction& tx, const string& command,
else if (command == "outscript") else if (command == "outscript")
MutateTxAddOutScript(tx, commandVal); MutateTxAddOutScript(tx, commandVal);
else if (command == "sign") else if (command == "sign") {
if (!ecc) { ecc.reset(new Secp256k1Init()); }
MutateTxSign(tx, commandVal); MutateTxSign(tx, commandVal);
}
else if (command == "load") else if (command == "load")
RegisterLoad(commandVal); RegisterLoad(commandVal);

4
src/init.cpp

@ -194,6 +194,7 @@ void Shutdown()
delete pwalletMain; delete pwalletMain;
pwalletMain = NULL; pwalletMain = NULL;
#endif #endif
ECC_Stop();
LogPrintf("%s: done\n", __func__); LogPrintf("%s: done\n", __func__);
} }
@ -788,6 +789,9 @@ bool AppInit2(boost::thread_group& threadGroup)
// ********************************************************* Step 4: application initialization: dir lock, daemonize, pidfile, debug log // ********************************************************* Step 4: application initialization: dir lock, daemonize, pidfile, debug log
// Initialize elliptic curve code
ECC_Start();
// Sanity check // Sanity check
if (!InitSanityCheck()) if (!InitSanityCheck())
return InitError(_("Initialization sanity check failed. Bitcoin Core is shutting down.")); return InitError(_("Initialization sanity check failed. Bitcoin Core is shutting down."));

59
src/key.cpp

@ -14,21 +14,7 @@
#include <secp256k1.h> #include <secp256k1.h>
#include "ecwrapper.h" #include "ecwrapper.h"
//! anonymous namespace static secp256k1_context_t* secp256k1_context = NULL;
namespace {
class CSecp256k1Init {
public:
CSecp256k1Init() {
secp256k1_start(SECP256K1_START_SIGN);
}
~CSecp256k1Init() {
secp256k1_stop();
}
};
static CSecp256k1Init instance_of_csecp256k1;
} // anon namespace
bool CKey::Check(const unsigned char *vch) { bool CKey::Check(const unsigned char *vch) {
return eccrypto::Check(vch); return eccrypto::Check(vch);
@ -44,7 +30,7 @@ void CKey::MakeNewKey(bool fCompressedIn) {
} }
bool CKey::SetPrivKey(const CPrivKey &privkey, 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; return false;
fCompressed = fCompressedIn; fCompressed = fCompressedIn;
fValid = true; fValid = true;
@ -57,7 +43,7 @@ CPrivKey CKey::GetPrivKey() const {
int privkeylen, ret; int privkeylen, ret;
privkey.resize(279); privkey.resize(279);
privkeylen = 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); assert(ret);
privkey.resize(privkeylen); privkey.resize(privkeylen);
return privkey; return privkey;
@ -67,7 +53,7 @@ CPubKey CKey::GetPubKey() const {
assert(fValid); assert(fValid);
CPubKey result; CPubKey result;
int clen = 65; 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((int)result.size() == clen);
assert(ret); assert(ret);
assert(result.IsValid()); assert(result.IsValid());
@ -81,7 +67,7 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_
int nSigLen = 72; int nSigLen = 72;
unsigned char extra_entropy[32] = {0}; unsigned char extra_entropy[32] = {0};
WriteLE32(extra_entropy, test_case); WriteLE32(extra_entropy, test_case);
int ret = secp256k1_ecdsa_sign(hash.begin(), (unsigned char*)&vchSig[0], &nSigLen, begin(), secp256k1_nonce_function_rfc6979, test_case ? extra_entropy : NULL); 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); assert(ret);
vchSig.resize(nSigLen); vchSig.resize(nSigLen);
return true; return true;
@ -106,7 +92,7 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
return false; return false;
vchSig.resize(65); vchSig.resize(65);
int rec = -1; int rec = -1;
int ret = secp256k1_ecdsa_sign_compact(hash.begin(), &vchSig[1], begin(), secp256k1_nonce_function_rfc6979, NULL, &rec); int ret = secp256k1_ecdsa_sign_compact(secp256k1_context, hash.begin(), &vchSig[1], begin(), secp256k1_nonce_function_rfc6979, NULL, &rec);
assert(ret); assert(ret);
assert(rec != -1); assert(rec != -1);
vchSig[0] = 27 + rec + (fCompressed ? 4 : 0); vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
@ -114,7 +100,7 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
} }
bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) { 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; return false;
fCompressed = vchPubKey.IsCompressed(); fCompressed = vchPubKey.IsCompressed();
fValid = true; fValid = true;
@ -140,7 +126,7 @@ bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild
} }
memcpy(ccChild, out+32, 32); memcpy(ccChild, out+32, 32);
memcpy((unsigned char*)keyChild.begin(), begin(), 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); UnlockObject(out);
keyChild.fCompressed = true; keyChild.fCompressed = true;
keyChild.fValid = ret; keyChild.fValid = ret;
@ -206,3 +192,32 @@ bool ECC_InitSanityCheck() {
CPubKey pubkey = key.GetPubKey(); CPubKey pubkey = key.GetPubKey();
return key.VerifyPubKey(pubkey); 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);
}
}

8
src/key.h

@ -173,7 +173,13 @@ struct CExtKey {
void SetMaster(const unsigned char* seed, unsigned int nSeedLen); void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
}; };
/** Check that required EC support is available at runtime */ /** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */
void ECC_Start(void);
/** Deinitialize the elliptic curve support. No-op if ECC_Start wasn't called first. */
void ECC_Stop(void);
/** Check that required EC support is available at runtime. */
bool ECC_InitSanityCheck(void); bool ECC_InitSanityCheck(void);
#endif // BITCOIN_KEY_H #endif // BITCOIN_KEY_H

3
src/test/test_bitcoin.cpp

@ -6,6 +6,7 @@
#include "test_bitcoin.h" #include "test_bitcoin.h"
#include "key.h"
#include "main.h" #include "main.h"
#include "random.h" #include "random.h"
#include "txdb.h" #include "txdb.h"
@ -28,6 +29,7 @@ extern void noui_connect();
BasicTestingSetup::BasicTestingSetup() BasicTestingSetup::BasicTestingSetup()
{ {
ECC_Start();
SetupEnvironment(); SetupEnvironment();
fPrintToDebugLog = false; // don't want to write to debug.log file fPrintToDebugLog = false; // don't want to write to debug.log file
fCheckBlockIndex = true; fCheckBlockIndex = true;
@ -35,6 +37,7 @@ BasicTestingSetup::BasicTestingSetup()
} }
BasicTestingSetup::~BasicTestingSetup() BasicTestingSetup::~BasicTestingSetup()
{ {
ECC_Stop();
} }
TestingSetup::TestingSetup() TestingSetup::TestingSetup()

Loading…
Cancel
Save