From 9a848e2cae1fc51c8b76a5d76a69d8fe2d87e078 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Sat, 2 Apr 2016 23:55:12 +0300 Subject: [PATCH] Preserve key compression attribute while initializing a new instance of CKey. THis bug was originally reported by Alex AXe: https://bitcointalk.org/index.php?topic=704756.msg14401298#msg14401298 --- src/key.cpp | 15 ++++++--------- src/key.h | 4 ++-- src/keystore.cpp | 9 +++------ src/walletdb.cpp | 8 ++------ 4 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/key.cpp b/src/key.cpp index 1adeab2..d5caca9 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -165,10 +165,10 @@ const unsigned char vchMaxModHalfOrder[32] = { const unsigned char *vchZero = NULL; -void CKey::SetCompressedPubKey() +void CKey::SetCompressedPubKey(bool fCompressed) { - EC_KEY_set_conv_form(pkey, POINT_CONVERSION_COMPRESSED); - fCompressedPubKey = true; + EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED); + fCompressedPubKey = fCompressed; } void CKey::Reset() @@ -264,8 +264,7 @@ void CKey::MakeNewKey(bool fCompressed) { if (!EC_KEY_generate_key(pkey)) throw key_error("CKey::MakeNewKey() : EC_KEY_generate_key failed"); - if (fCompressed) - SetCompressedPubKey(); + SetCompressedPubKey(fCompressed); fSet = true; } @@ -311,8 +310,7 @@ bool CKey::SetSecret(const CSecret& vchSecret, bool fCompressed) } BN_clear_free(bn); fSet = true; - if (fCompressed) - SetCompressedPubKey(); + SetCompressedPubKey(fCompressed); return true; } @@ -429,8 +427,7 @@ bool CKey::SignCompact(uint256 hash, std::vector& vchSig) { CKey keyRec; keyRec.fSet = true; - if (fCompressedPubKey) - keyRec.SetCompressedPubKey(); + keyRec.SetCompressedPubKey(fCompressedPubKey); if (ECDSA_SIG_recover_key_GFp(keyRec.pkey, sig, (unsigned char*)&hash, sizeof(hash), i, 1) == 1) if (keyRec.GetPubKey() == this->GetPubKey()) { diff --git a/src/key.h b/src/key.h index 2489dfc..45ce39b 100644 --- a/src/key.h +++ b/src/key.h @@ -240,10 +240,10 @@ public: bool IsNull() const; bool IsCompressed() const; - void SetCompressedPubKey(); + void SetCompressedPubKey(bool fCompressed=true); void MakeNewKey(bool fCompressed=true); bool SetPrivKey(const CPrivKey& vchPrivKey); - bool SetSecret(const CSecret& vchSecret, bool fCompressed = true); + bool SetSecret(const CSecret& vchSecret, bool fCompressed = false); CSecret GetSecret(bool &fCompressed) const; CSecret GetSecret() const; CPrivKey GetPrivKey() const; diff --git a/src/keystore.cpp b/src/keystore.cpp index 33b8eb1..0aca325 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -158,8 +158,7 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn) return false; CKey key; key.SetSecret(vchSecret); - if (vchPubKey.size() == 33) - key.SetCompressedPubKey(); + key.SetCompressedPubKey(vchPubKey.IsCompressed()); if (key.GetPubKey() == vchPubKey) break; return false; @@ -318,8 +317,7 @@ bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const if (vchSecret.size() != 32) return false; keyOut.SetSecret(vchSecret); - if (vchPubKey.size() == 33) - keyOut.SetCompressedPubKey(); + keyOut.SetCompressedPubKey(vchPubKey.IsCompressed()); return true; } } @@ -399,8 +397,7 @@ bool CCryptoKeyStore::DecryptKeys(const CKeyingMaterial& vMasterKeyIn) return false; CKey key; key.SetSecret(vchSecret); - if (vchPubKey.size() == 33) - key.SetCompressedPubKey(); + key.SetCompressedPubKey(vchPubKey.IsCompressed()); if (!CBasicKeyStore::AddKey(key)) return false; } diff --git a/src/walletdb.cpp b/src/walletdb.cpp index 3880a93..0f39cdf 100644 --- a/src/walletdb.cpp +++ b/src/walletdb.cpp @@ -352,9 +352,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, strErr = "Error reading wallet database: CPrivKey pubkey inconsistency"; return false; } - if (vchPubKey.size() == 33) { - key.SetCompressedPubKey(); - } + key.SetCompressedPubKey(vchPubKey.IsCompressed()); if (!key.IsValid()) { strErr = "Error reading wallet database: invalid CPrivKey"; @@ -375,9 +373,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, strErr = "Error reading wallet database: CWalletKey pubkey inconsistency"; return false; } - if (vchPubKey.size() == 33) { - key.SetCompressedPubKey(); - } + key.SetCompressedPubKey(vchPubKey.IsCompressed()); if (!key.IsValid()) { strErr = "Error reading wallet database: invalid CWalletKey"; -- 1.7.1