diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2014-11-24 14:38:40 +0100 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2014-11-24 14:39:25 +0100 |
commit | 6582f323f084a9f758ab2958da69caeb73cd13b4 (patch) | |
tree | e2521f7a37c3304e9230d7e518985972a6aacf6e /src/key.cpp | |
parent | dfc8e1432a57c1516bd474a79d0fd1f65f02dd37 (diff) | |
parent | f321d6bfff4dbbb4c52d0f175a27d54b287e81ff (diff) | |
download | bitcoin-6582f323f084a9f758ab2958da69caeb73cd13b4.tar.xz |
Merge pull request #5224
f321d6b Add key generation/verification to ECC sanity check (Pieter Wuille)
d0c41a7 Add sanity check after key generation (Pieter Wuille)
Diffstat (limited to 'src/key.cpp')
-rw-r--r-- | src/key.cpp | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/src/key.cpp b/src/key.cpp index 76256b864c..a91ed1cc1d 100644 --- a/src/key.cpp +++ b/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 { @@ -190,5 +201,13 @@ void CExtKey::Decode(const unsigned char code[74]) { } bool ECC_InitSanityCheck() { - return CECKey::SanityCheck(); +#if !defined(USE_SECP256K1) + if (!CECKey::SanityCheck()) { + return false; + } +#endif + CKey key; + key.MakeNewKey(true); + CPubKey pubkey = key.GetPubKey(); + return key.VerifyPubKey(pubkey); } |