From d99c81697148a9695c0fba614dff9fbe728a3acd Mon Sep 17 00:00:00 2001 From: Hodlinator <172445034+hodlinator@users.noreply.github.com> Date: Tue, 20 Aug 2024 23:41:02 +0200 Subject: [PATCH] refactor: Improve CCrypter related lines Lines will be touched in next 2 commits. --- src/wallet/crypter.cpp | 13 +++++------ src/wallet/test/wallet_crypto_tests.cpp | 30 ++++++++++++------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/wallet/crypter.cpp b/src/wallet/crypter.cpp index 57a19fb5f2b..5ee755452d2 100644 --- a/src/wallet/crypter.cpp +++ b/src/wallet/crypter.cpp @@ -8,6 +8,7 @@ #include #include +#include #include namespace wallet { @@ -24,7 +25,7 @@ int CCrypter::BytesToKeySHA512AES(const std::vector& chSalt, cons unsigned char buf[CSHA512::OUTPUT_SIZE]; CSHA512 di; - di.Write((const unsigned char*)strKeyData.data(), strKeyData.size()); + di.Write(UCharCast(strKeyData.data()), strKeyData.size()); di.Write(chSalt.data(), chSalt.size()); di.Finalize(buf); @@ -93,12 +94,10 @@ bool CCrypter::Decrypt(const std::vector& vchCiphertext, CKeyingM return false; // plaintext will always be equal to or lesser than length of ciphertext - int nLen = vchCiphertext.size(); - - vchPlaintext.resize(nLen); + vchPlaintext.resize(vchCiphertext.size()); AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true); - nLen = dec.Decrypt(vchCiphertext.data(), vchCiphertext.size(), vchPlaintext.data()); + int nLen = dec.Decrypt(vchCiphertext.data(), vchCiphertext.size(), vchPlaintext.data()); if(nLen == 0) return false; vchPlaintext.resize(nLen); @@ -118,8 +117,8 @@ bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vch bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext) { CCrypter cKeyCrypter; - std::vector chIV(WALLET_CRYPTO_IV_SIZE); - memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE); + static_assert(WALLET_CRYPTO_IV_SIZE <= std::remove_reference_t::size()); + std::vector chIV{nIV.begin(), nIV.begin() + WALLET_CRYPTO_IV_SIZE}; if(!cKeyCrypter.SetKey(vMasterKey, chIV)) return false; return cKeyCrypter.Decrypt(vchCiphertext, vchPlaintext); diff --git a/src/wallet/test/wallet_crypto_tests.cpp b/src/wallet/test/wallet_crypto_tests.cpp index e413bc1fa0b..09b08fa4337 100644 --- a/src/wallet/test/wallet_crypto_tests.cpp +++ b/src/wallet/test/wallet_crypto_tests.cpp @@ -18,14 +18,14 @@ class TestCrypter { public: static void TestPassphraseSingle(const std::vector& vchSalt, const SecureString& passphrase, uint32_t rounds, - const std::vector& correctKey = std::vector(), - const std::vector& correctIV=std::vector()) + const std::vector& correctKey = {}, + const std::vector& correctIV = {}) { CCrypter crypt; crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0); if(!correctKey.empty()) - BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, \ + BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correctKey)); if(!correctIV.empty()) BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0, @@ -33,40 +33,40 @@ static void TestPassphraseSingle(const std::vector& vchSalt, cons } static void TestPassphrase(const std::vector& vchSalt, const SecureString& passphrase, uint32_t rounds, - const std::vector& correctKey = std::vector(), - const std::vector& correctIV=std::vector()) + const std::vector& correctKey = {}, + const std::vector& correctIV = {}) { TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV); for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i) TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds); } -static void TestDecrypt(const CCrypter& crypt, const std::vector& vchCiphertext, \ - const std::vector& vchPlaintext = std::vector()) +static void TestDecrypt(const CCrypter& crypt, const std::vector& vchCiphertext, + const std::vector& vchCorrectPlaintext = {}) { CKeyingMaterial vchDecrypted; crypt.Decrypt(vchCiphertext, vchDecrypted); if (vchPlaintext.size()) - BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted); + BOOST_CHECK_EQUAL_COLLECTIONS(vchDecrypted.begin(), vchDecrypted.end(), vchCorrectPlaintext.begin(), vchCorrectPlaintext.end()); } static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext, - const std::vector& vchCiphertextCorrect = std::vector()) + const std::vector& vchCiphertextCorrect = {}) { std::vector vchCiphertext; crypt.Encrypt(vchPlaintext, vchCiphertext); if (!vchCiphertextCorrect.empty()) - BOOST_CHECK(vchCiphertext == vchCiphertextCorrect); + BOOST_CHECK_EQUAL_COLLECTIONS(vchCiphertext.begin(), vchCiphertext.end(), vchCiphertextCorrect.begin(), vchCiphertextCorrect.end()); const std::vector vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end()); TestDecrypt(crypt, vchCiphertext, vchPlaintext2); } -static void TestEncrypt(const CCrypter& crypt, const std::vector& vchPlaintextIn, \ - const std::vector& vchCiphertextCorrect = std::vector()) +static void TestEncrypt(const CCrypter& crypt, const std::vector& vchPlaintextIn, + const std::vector& vchCiphertextCorrect = {}) { - TestEncryptSingle(crypt, CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()), vchCiphertextCorrect); + TestEncryptSingle(crypt, CKeyingMaterial{vchPlaintextIn.begin(), vchPlaintextIn.end()}, vchCiphertextCorrect); for(std::vector::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i) TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end())); } @@ -76,8 +76,8 @@ static void TestEncrypt(const CCrypter& crypt, const std::vector& BOOST_AUTO_TEST_CASE(passphrase) { // These are expensive. - TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000, \ - ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \ + TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000, + ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), ParseHex("cf2f2691526dd1aa220896fb8bf7c369")); std::string hash(GetRandHash().ToString());