mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 22:18:54 +01:00
wallet: Get rid of LockObject and UnlockObject calls in key.h
Replace these with vectors allocated from the secure allocator. This avoids mlock syscall churn on stack pages, as well as makes it possible to get rid of these functions. Please review this commit and the previous one carefully that no `sizeof(vectortype)` remains in the memcpys and memcmps usage (ick!), and `.data()` or `&vec[x]` is used as appropriate instead of &vec.
This commit is contained in:
34
src/key.cpp
34
src/key.cpp
@@ -125,8 +125,8 @@ bool CKey::Check(const unsigned char *vch) {
|
||||
|
||||
void CKey::MakeNewKey(bool fCompressedIn) {
|
||||
do {
|
||||
GetStrongRandBytes(vch, sizeof(vch));
|
||||
} while (!Check(vch));
|
||||
GetStrongRandBytes(keydata.data(), keydata.size());
|
||||
} while (!Check(keydata.data()));
|
||||
fValid = true;
|
||||
fCompressed = fCompressedIn;
|
||||
}
|
||||
@@ -224,20 +224,18 @@ bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
|
||||
bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
|
||||
assert(IsValid());
|
||||
assert(IsCompressed());
|
||||
unsigned char out[64];
|
||||
LockObject(out);
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
|
||||
if ((nChild >> 31) == 0) {
|
||||
CPubKey pubkey = GetPubKey();
|
||||
assert(pubkey.begin() + 33 == pubkey.end());
|
||||
BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out);
|
||||
BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, vout.data());
|
||||
} else {
|
||||
assert(begin() + 32 == end());
|
||||
BIP32Hash(cc, nChild, 0, begin(), out);
|
||||
BIP32Hash(cc, nChild, 0, begin(), vout.data());
|
||||
}
|
||||
memcpy(ccChild.begin(), out+32, 32);
|
||||
memcpy(ccChild.begin(), vout.data()+32, 32);
|
||||
memcpy((unsigned char*)keyChild.begin(), begin(), 32);
|
||||
bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context_sign, (unsigned char*)keyChild.begin(), out);
|
||||
UnlockObject(out);
|
||||
bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context_sign, (unsigned char*)keyChild.begin(), vout.data());
|
||||
keyChild.fCompressed = true;
|
||||
keyChild.fValid = ret;
|
||||
return ret;
|
||||
@@ -253,12 +251,10 @@ bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
|
||||
|
||||
void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
|
||||
static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
|
||||
unsigned char out[64];
|
||||
LockObject(out);
|
||||
CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
|
||||
key.Set(&out[0], &out[32], true);
|
||||
memcpy(chaincode.begin(), &out[32], 32);
|
||||
UnlockObject(out);
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
|
||||
CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(vout.data());
|
||||
key.Set(&vout[0], &vout[32], true);
|
||||
memcpy(chaincode.begin(), &vout[32], 32);
|
||||
nDepth = 0;
|
||||
nChild = 0;
|
||||
memset(vchFingerprint, 0, sizeof(vchFingerprint));
|
||||
@@ -308,12 +304,10 @@ void ECC_Start() {
|
||||
|
||||
{
|
||||
// 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);
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> vseed(32);
|
||||
GetRandBytes(vseed.data(), 32);
|
||||
bool ret = secp256k1_context_randomize(ctx, vseed.data());
|
||||
assert(ret);
|
||||
UnlockObject(seed);
|
||||
}
|
||||
|
||||
secp256k1_context_sign = ctx;
|
||||
|
||||
Reference in New Issue
Block a user