mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 22:18:54 +01:00
Add sanity check after key generation
Add a sanity check to prevent cosmic rays from flipping a bit in the generated public key, or bugs in the elliptic curve code. This is simply done by signing a (randomized) message, and verifying the result.
This commit is contained in:
19
src/key.cpp
19
src/key.cpp
@@ -86,6 +86,20 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
|
||||
if (pubkey.IsCompressed() != fCompressed) {
|
||||
return false;
|
||||
}
|
||||
unsigned char rnd[8];
|
||||
std::string str = "Bitcoin key verification\n";
|
||||
GetRandBytes(rnd, sizeof(rnd));
|
||||
uint256 hash;
|
||||
CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize((unsigned char*)&hash);
|
||||
std::vector<unsigned char> vchSig;
|
||||
Sign(hash, vchSig);
|
||||
return pubkey.Verify(hash, vchSig);
|
||||
}
|
||||
|
||||
bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
|
||||
if (!fValid)
|
||||
return false;
|
||||
@@ -111,10 +125,7 @@ bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
|
||||
if (fSkipCheck)
|
||||
return true;
|
||||
|
||||
if (GetPubKey() != vchPubKey)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
return VerifyPubKey(vchPubKey);
|
||||
}
|
||||
|
||||
bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
|
||||
|
||||
Reference in New Issue
Block a user