From 403d86f1ccf0b73f042d42a9722bb007ba8c7a31 Mon Sep 17 00:00:00 2001 From: Hodlinator <172445034+hodlinator@users.noreply.github.com> Date: Mon, 19 Aug 2024 18:21:49 +0200 Subject: [PATCH] refactor: vector -> span in CCrypter TestEncryptSingle: Remove no longer needed plaintext2-variable that existed because vectors had different allocators. --- src/wallet/crypter.cpp | 14 ++++++------- src/wallet/crypter.h | 12 +++++------ src/wallet/test/wallet_crypto_tests.cpp | 27 ++++++++++++------------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/wallet/crypter.cpp b/src/wallet/crypter.cpp index 1d6af9dda44..ffb1d29e160 100644 --- a/src/wallet/crypter.cpp +++ b/src/wallet/crypter.cpp @@ -12,7 +12,7 @@ #include namespace wallet { -int CCrypter::BytesToKeySHA512AES(const std::vector& salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const +int CCrypter::BytesToKeySHA512AES(const std::span salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const { // This mimics the behavior of openssl's EVP_BytesToKey with an aes256cbc // cipher and sha512 message digest. Because sha512's output size (64b) is @@ -38,7 +38,7 @@ int CCrypter::BytesToKeySHA512AES(const std::vector& salt, const return WALLET_CRYPTO_KEY_SIZE; } -bool CCrypter::SetKeyFromPassphrase(const SecureString& key_data, const std::vector& salt, const unsigned int rounds, const unsigned int derivation_method) +bool CCrypter::SetKeyFromPassphrase(const SecureString& key_data, const std::span salt, const unsigned int rounds, const unsigned int derivation_method) { if (rounds < 1 || salt.size() != WALLET_CRYPTO_SALT_SIZE) { return false; @@ -60,7 +60,7 @@ bool CCrypter::SetKeyFromPassphrase(const SecureString& key_data, const std::vec return true; } -bool CCrypter::SetKey(const CKeyingMaterial& new_key, const std::vector& new_iv) +bool CCrypter::SetKey(const CKeyingMaterial& new_key, const std::span new_iv) { if (new_key.size() != WALLET_CRYPTO_KEY_SIZE || new_iv.size() != WALLET_CRYPTO_IV_SIZE) { return false; @@ -91,7 +91,7 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector& ciphertext, CKeyingMaterial& plaintext) const +bool CCrypter::Decrypt(const std::span ciphertext, CKeyingMaterial& plaintext) const { if (!fKeySet) return false; @@ -118,18 +118,18 @@ bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vch return cKeyCrypter.Encrypt(vchPlaintext, vchCiphertext); } -bool DecryptSecret(const CKeyingMaterial& master_key, const std::vector& ciphertext, const uint256& iv, CKeyingMaterial& plaintext) +bool DecryptSecret(const CKeyingMaterial& master_key, const std::span ciphertext, const uint256& iv, CKeyingMaterial& plaintext) { CCrypter key_crypter; static_assert(WALLET_CRYPTO_IV_SIZE <= std::remove_reference_t::size()); - std::vector iv_prefix{iv.begin(), iv.begin() + WALLET_CRYPTO_IV_SIZE}; + const std::span iv_prefix{iv.data(), WALLET_CRYPTO_IV_SIZE}; if (!key_crypter.SetKey(master_key, iv_prefix)) { return false; } return key_crypter.Decrypt(ciphertext, plaintext); } -bool DecryptKey(const CKeyingMaterial& master_key, const std::vector& crypted_secret, const CPubKey& pub_key, CKey& key) +bool DecryptKey(const CKeyingMaterial& master_key, const std::span crypted_secret, const CPubKey& pub_key, CKey& key) { CKeyingMaterial secret; if (!DecryptSecret(master_key, crypted_secret, pub_key.GetHash(), secret)) { diff --git a/src/wallet/crypter.h b/src/wallet/crypter.h index 4c3d49175c4..944858fb3f8 100644 --- a/src/wallet/crypter.h +++ b/src/wallet/crypter.h @@ -75,13 +75,13 @@ private: std::vector> vchIV; bool fKeySet; - int BytesToKeySHA512AES(const std::vector& salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const; + int BytesToKeySHA512AES(std::span salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const; public: - bool SetKeyFromPassphrase(const SecureString& key_data, const std::vector& salt, const unsigned int rounds, const unsigned int derivation_method); + bool SetKeyFromPassphrase(const SecureString& key_data, std::span salt, const unsigned int rounds, const unsigned int derivation_method); bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector &vchCiphertext) const; - bool Decrypt(const std::vector& ciphertext, CKeyingMaterial& plaintext) const; - bool SetKey(const CKeyingMaterial& new_key, const std::vector& new_iv); + bool Decrypt(std::span ciphertext, CKeyingMaterial& plaintext) const; + bool SetKey(const CKeyingMaterial& new_key, std::span new_iv); void CleanKey() { @@ -104,8 +104,8 @@ public: }; bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector &vchCiphertext); -bool DecryptSecret(const CKeyingMaterial& master_key, const std::vector& ciphertext, const uint256& iv, CKeyingMaterial& plaintext); -bool DecryptKey(const CKeyingMaterial& master_key, const std::vector& crypted_secret, const CPubKey& pub_key, CKey& key); +bool DecryptSecret(const CKeyingMaterial& master_key, std::span ciphertext, const uint256& iv, CKeyingMaterial& plaintext); +bool DecryptKey(const CKeyingMaterial& master_key, std::span crypted_secret, const CPubKey& pub_key, CKey& key); } // namespace wallet #endif // BITCOIN_WALLET_CRYPTER_H diff --git a/src/wallet/test/wallet_crypto_tests.cpp b/src/wallet/test/wallet_crypto_tests.cpp index 34b909e85dd..6e8615c3ddd 100644 --- a/src/wallet/test/wallet_crypto_tests.cpp +++ b/src/wallet/test/wallet_crypto_tests.cpp @@ -17,9 +17,9 @@ BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup) class TestCrypter { public: -static void TestPassphraseSingle(const std::vector& salt, const SecureString& passphrase, uint32_t rounds, - const std::vector& correct_key = {}, - const std::vector& correct_iv = {}) +static void TestPassphraseSingle(const std::span salt, const SecureString& passphrase, uint32_t rounds, + const std::span correct_key = {}, + const std::span correct_iv = {}) { CCrypter crypt; crypt.SetKeyFromPassphrase(passphrase, salt, rounds, 0); @@ -34,9 +34,9 @@ static void TestPassphraseSingle(const std::vector& salt, const S } } -static void TestPassphrase(const std::vector& salt, const SecureString& passphrase, uint32_t rounds, - const std::vector& correct_key = {}, - const std::vector& correct_iv = {}) +static void TestPassphrase(const std::span salt, const SecureString& passphrase, uint32_t rounds, + const std::span correct_key = {}, + const std::span correct_iv = {}) { TestPassphraseSingle(salt, passphrase, rounds, correct_key, correct_iv); for (SecureString::const_iterator it{passphrase.begin()}; it != passphrase.end(); ++it) { @@ -44,8 +44,8 @@ static void TestPassphrase(const std::vector& salt, const SecureS } } -static void TestDecrypt(const CCrypter& crypt, const std::vector& ciphertext, - const std::vector& correct_plaintext = {}) +static void TestDecrypt(const CCrypter& crypt, const std::span ciphertext, + const std::span correct_plaintext = {}) { CKeyingMaterial decrypted; crypt.Decrypt(ciphertext, decrypted); @@ -55,7 +55,7 @@ static void TestDecrypt(const CCrypter& crypt, const std::vector& } static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& plaintext, - const std::vector& correct_ciphertext = {}) + const std::span correct_ciphertext = {}) { std::vector ciphertext; crypt.Encrypt(plaintext, ciphertext); @@ -64,12 +64,11 @@ static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& plai BOOST_CHECK_EQUAL_COLLECTIONS(ciphertext.begin(), ciphertext.end(), correct_ciphertext.begin(), correct_ciphertext.end()); } - const std::vector plaintext2(plaintext.begin(), plaintext.end()); - TestDecrypt(crypt, ciphertext, /*correct_plaintext=*/plaintext2); + TestDecrypt(crypt, ciphertext, /*correct_plaintext=*/plaintext); } -static void TestEncrypt(const CCrypter& crypt, const std::vector& plaintext, - const std::vector& correct_ciphertext = {}) +static void TestEncrypt(const CCrypter& crypt, const std::span plaintext, + const std::span correct_ciphertext = {}) { TestEncryptSingle(crypt, CKeyingMaterial{plaintext.begin(), plaintext.end()}, correct_ciphertext); for (auto it{plaintext.begin()}; it != plaintext.end(); ++it) { @@ -105,7 +104,7 @@ BOOST_AUTO_TEST_CASE(encrypt) { for (int i = 0; i != 100; i++) { uint256 hash(GetRandHash()); - TestCrypter::TestEncrypt(crypt, std::vector(hash.begin(), hash.end())); + TestCrypter::TestEncrypt(crypt, std::span{hash.begin(), hash.end()}); } }