diff --git a/src/wallet/crypter.cpp b/src/wallet/crypter.cpp --- a/src/wallet/crypter.cpp +++ b/src/wallet/crypter.cpp @@ -20,17 +20,22 @@ // cipher and sha512 message digest. Because sha512's output size (64b) is // greater than the aes256 block size (16b) + aes256 key size (32b), there's // no need to process more than once (D_0). - if (!count || !key || !iv) return 0; + if (!count || !key || !iv) { + return 0; + } uint8_t buf[CSHA512::OUTPUT_SIZE]; CSHA512 di; di.Write((const uint8_t *)strKeyData.c_str(), strKeyData.size()); - if (chSalt.size()) di.Write(&chSalt[0], chSalt.size()); + if (chSalt.size()) { + di.Write(&chSalt[0], chSalt.size()); + } di.Finalize(buf); - for (int i = 0; i != count - 1; i++) + for (int i = 0; i != count - 1; i++) { di.Reset().Write(buf, sizeof(buf)).Finalize(buf); + } memcpy(key, buf, WALLET_CRYPTO_KEY_SIZE); memcpy(iv, buf + WALLET_CRYPTO_KEY_SIZE, WALLET_CRYPTO_IV_SIZE); @@ -42,12 +47,15 @@ const std::vector &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod) { - if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE) return false; + if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE) { + return false; + } int i = 0; - if (nDerivationMethod == 0) + if (nDerivationMethod == 0) { i = BytesToKeySHA512AES(chSalt, strKeyData, nRounds, vchKey.data(), vchIV.data()); + } if (i != (int)WALLET_CRYPTO_KEY_SIZE) { memory_cleanse(vchKey.data(), vchKey.size()); @@ -62,8 +70,9 @@ bool CCrypter::SetKey(const CKeyingMaterial &chNewKey, const std::vector &chNewIV) { if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || - chNewIV.size() != WALLET_CRYPTO_IV_SIZE) + chNewIV.size() != WALLET_CRYPTO_IV_SIZE) { return false; + } memcpy(vchKey.data(), chNewKey.data(), chNewKey.size()); memcpy(vchIV.data(), chNewIV.data(), chNewIV.size()); @@ -74,7 +83,9 @@ bool CCrypter::Encrypt(const CKeyingMaterial &vchPlaintext, std::vector &vchCiphertext) const { - if (!fKeySet) return false; + if (!fKeySet) { + return false; + } // max ciphertext len for a n bytes of plaintext is // n + AES_BLOCKSIZE bytes @@ -83,7 +94,9 @@ AES256CBCEncrypt enc(vchKey.data(), vchIV.data(), true); size_t nLen = enc.Encrypt(&vchPlaintext[0], vchPlaintext.size(), &vchCiphertext[0]); - if (nLen < vchPlaintext.size()) return false; + if (nLen < vchPlaintext.size()) { + return false; + } vchCiphertext.resize(nLen); return true; @@ -91,7 +104,9 @@ bool CCrypter::Decrypt(const std::vector &vchCiphertext, CKeyingMaterial &vchPlaintext) const { - if (!fKeySet) return false; + if (!fKeySet) { + return false; + } // plaintext will always be equal to or lesser than length of ciphertext int nLen = vchCiphertext.size(); @@ -101,7 +116,9 @@ AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true); nLen = dec.Decrypt(&vchCiphertext[0], vchCiphertext.size(), &vchPlaintext[0]); - if (nLen == 0) return false; + if (nLen == 0) { + return false; + } vchPlaintext.resize(nLen); return true; } @@ -113,7 +130,9 @@ CCrypter cKeyCrypter; std::vector chIV(WALLET_CRYPTO_IV_SIZE); memcpy(&chIV[0], &nIV, WALLET_CRYPTO_IV_SIZE); - if (!cKeyCrypter.SetKey(vMasterKey, chIV)) return false; + if (!cKeyCrypter.SetKey(vMasterKey, chIV)) { + return false; + } return cKeyCrypter.Encrypt(*((const CKeyingMaterial *)&vchPlaintext), vchCiphertext); } @@ -124,7 +143,9 @@ CCrypter cKeyCrypter; std::vector chIV(WALLET_CRYPTO_IV_SIZE); memcpy(&chIV[0], &nIV, WALLET_CRYPTO_IV_SIZE); - if (!cKeyCrypter.SetKey(vMasterKey, chIV)) return false; + if (!cKeyCrypter.SetKey(vMasterKey, chIV)) { + return false; + } return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial *)&vchPlaintext)); } @@ -134,10 +155,13 @@ const CPubKey &vchPubKey, CKey &key) { CKeyingMaterial vchSecret; if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), - vchSecret)) + vchSecret)) { return false; + } - if (vchSecret.size() != 32) return false; + if (vchSecret.size() != 32) { + return false; + } key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed()); return key.VerifyPubKey(vchPubKey); @@ -145,8 +169,12 @@ bool CCryptoKeyStore::SetCrypted() { LOCK(cs_KeyStore); - if (fUseCrypto) return true; - if (!mapKeys.empty()) return false; + if (fUseCrypto) { + return true; + } + if (!mapKeys.empty()) { + return false; + } fUseCrypto = true; return true; } @@ -164,7 +192,9 @@ } bool CCryptoKeyStore::Lock() { - if (!SetCrypted()) return false; + if (!SetCrypted()) { + return false; + } { LOCK(cs_KeyStore); @@ -178,7 +208,9 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial &vMasterKeyIn) { { LOCK(cs_KeyStore); - if (!SetCrypted()) return false; + if (!SetCrypted()) { + return false; + } bool keyPass = false; bool keyFail = false; @@ -192,14 +224,18 @@ break; } keyPass = true; - if (fDecryptionThoroughlyChecked) break; + if (fDecryptionThoroughlyChecked) { + break; + } } if (keyPass && keyFail) { LogPrintf("The wallet is probably corrupted: Some keys decrypt but " "not all.\n"); assert(false); } - if (keyFail || !keyPass) return false; + if (keyFail || !keyPass) { + return false; + } vMasterKey = vMasterKeyIn; fDecryptionThoroughlyChecked = true; } @@ -210,17 +246,24 @@ bool CCryptoKeyStore::AddKeyPubKey(const CKey &key, const CPubKey &pubkey) { { LOCK(cs_KeyStore); - if (!IsCrypted()) return CBasicKeyStore::AddKeyPubKey(key, pubkey); + if (!IsCrypted()) { + return CBasicKeyStore::AddKeyPubKey(key, pubkey); + } - if (IsLocked()) return false; + if (IsLocked()) { + return false; + } std::vector vchCryptedSecret; CKeyingMaterial vchSecret(key.begin(), key.end()); if (!EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), - vchCryptedSecret)) + vchCryptedSecret)) { return false; + } - if (!AddCryptedKey(pubkey, vchCryptedSecret)) return false; + if (!AddCryptedKey(pubkey, vchCryptedSecret)) { + return false; + } } return true; } @@ -229,7 +272,9 @@ const CPubKey &vchPubKey, const std::vector &vchCryptedSecret) { { LOCK(cs_KeyStore); - if (!SetCrypted()) return false; + if (!SetCrypted()) { + return false; + } mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret); @@ -248,7 +293,9 @@ bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey &keyOut) const { { LOCK(cs_KeyStore); - if (!IsCrypted()) return CBasicKeyStore::GetKey(address, keyOut); + if (!IsCrypted()) { + return CBasicKeyStore::GetKey(address, keyOut); + } CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); if (mi != mapCryptedKeys.end()) { @@ -264,8 +311,9 @@ CPubKey &vchPubKeyOut) const { { LOCK(cs_KeyStore); - if (!IsCrypted()) + if (!IsCrypted()) { return CBasicKeyStore::GetPubKey(address, vchPubKeyOut); + } CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); if (mi != mapCryptedKeys.end()) { @@ -293,7 +341,9 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial &vMasterKeyIn) { { LOCK(cs_KeyStore); - if (!mapCryptedKeys.empty() || IsCrypted()) return false; + if (!mapCryptedKeys.empty() || IsCrypted()) { + return false; + } fUseCrypto = true; for (KeyMap::value_type &mKey : mapKeys) { @@ -302,9 +352,12 @@ CKeyingMaterial vchSecret(key.begin(), key.end()); std::vector vchCryptedSecret; if (!EncryptSecret(vMasterKeyIn, vchSecret, vchPubKey.GetHash(), - vchCryptedSecret)) + vchCryptedSecret)) { return false; - if (!AddCryptedKey(vchPubKey, vchCryptedSecret)) return false; + } + if (!AddCryptedKey(vchPubKey, vchCryptedSecret)) { + return false; + } } mapKeys.clear(); }