Skip to content

Commit

Permalink
refactor: vector -> span in CCrypter
Browse files Browse the repository at this point in the history
TestEncryptSingle: Remove no longer needed plaintext2-variable that existed because vectors had different allocators.
  • Loading branch information
hodlinator committed Aug 28, 2024
1 parent bd0830b commit 403d86f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
14 changes: 7 additions & 7 deletions src/wallet/crypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <vector>

namespace wallet {
int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const
int CCrypter::BytesToKeySHA512AES(const std::span<const unsigned char> 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
Expand All @@ -38,7 +38,7 @@ int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& salt, const
return WALLET_CRYPTO_KEY_SIZE;
}

bool CCrypter::SetKeyFromPassphrase(const SecureString& key_data, const std::vector<unsigned char>& salt, const unsigned int rounds, const unsigned int derivation_method)
bool CCrypter::SetKeyFromPassphrase(const SecureString& key_data, const std::span<const unsigned char> salt, const unsigned int rounds, const unsigned int derivation_method)
{
if (rounds < 1 || salt.size() != WALLET_CRYPTO_SALT_SIZE) {
return false;
Expand All @@ -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<unsigned char>& new_iv)
bool CCrypter::SetKey(const CKeyingMaterial& new_key, const std::span<const unsigned char> new_iv)
{
if (new_key.size() != WALLET_CRYPTO_KEY_SIZE || new_iv.size() != WALLET_CRYPTO_IV_SIZE) {
return false;
Expand Down Expand Up @@ -91,7 +91,7 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
return true;
}

bool CCrypter::Decrypt(const std::vector<unsigned char>& ciphertext, CKeyingMaterial& plaintext) const
bool CCrypter::Decrypt(const std::span<const unsigned char> ciphertext, CKeyingMaterial& plaintext) const
{
if (!fKeySet)
return false;
Expand All @@ -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<unsigned char>& ciphertext, const uint256& iv, CKeyingMaterial& plaintext)
bool DecryptSecret(const CKeyingMaterial& master_key, const std::span<const unsigned char> ciphertext, const uint256& iv, CKeyingMaterial& plaintext)
{
CCrypter key_crypter;
static_assert(WALLET_CRYPTO_IV_SIZE <= std::remove_reference_t<decltype(iv)>::size());
std::vector<unsigned char> 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<unsigned char>& crypted_secret, const CPubKey& pub_key, CKey& key)
bool DecryptKey(const CKeyingMaterial& master_key, const std::span<const unsigned char> crypted_secret, const CPubKey& pub_key, CKey& key)
{
CKeyingMaterial secret;
if (!DecryptSecret(master_key, crypted_secret, pub_key.GetHash(), secret)) {
Expand Down
12 changes: 6 additions & 6 deletions src/wallet/crypter.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ friend class wallet_crypto_tests::TestCrypter; // for test access to chKey/chIV
std::vector<unsigned char, secure_allocator<unsigned char>> vchIV;
bool fKeySet;

int BytesToKeySHA512AES(const std::vector<unsigned char>& salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const;
int BytesToKeySHA512AES(std::span<const unsigned char> salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const;

public:
bool SetKeyFromPassphrase(const SecureString& key_data, const std::vector<unsigned char>& salt, const unsigned int rounds, const unsigned int derivation_method);
bool SetKeyFromPassphrase(const SecureString& key_data, std::span<const unsigned char> salt, const unsigned int rounds, const unsigned int derivation_method);
bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const;
bool Decrypt(const std::vector<unsigned char>& ciphertext, CKeyingMaterial& plaintext) const;
bool SetKey(const CKeyingMaterial& new_key, const std::vector<unsigned char>& new_iv);
bool Decrypt(std::span<const unsigned char> ciphertext, CKeyingMaterial& plaintext) const;
bool SetKey(const CKeyingMaterial& new_key, std::span<const unsigned char> new_iv);

void CleanKey()
{
Expand All @@ -104,8 +104,8 @@ friend class wallet_crypto_tests::TestCrypter; // for test access to chKey/chIV
};

bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
bool DecryptSecret(const CKeyingMaterial& master_key, const std::vector<unsigned char>& ciphertext, const uint256& iv, CKeyingMaterial& plaintext);
bool DecryptKey(const CKeyingMaterial& master_key, const std::vector<unsigned char>& crypted_secret, const CPubKey& pub_key, CKey& key);
bool DecryptSecret(const CKeyingMaterial& master_key, std::span<const unsigned char> ciphertext, const uint256& iv, CKeyingMaterial& plaintext);
bool DecryptKey(const CKeyingMaterial& master_key, std::span<const unsigned char> crypted_secret, const CPubKey& pub_key, CKey& key);
} // namespace wallet

#endif // BITCOIN_WALLET_CRYPTER_H
27 changes: 13 additions & 14 deletions src/wallet/test/wallet_crypto_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup)
class TestCrypter
{
public:
static void TestPassphraseSingle(const std::vector<unsigned char>& salt, const SecureString& passphrase, uint32_t rounds,
const std::vector<unsigned char>& correct_key = {},
const std::vector<unsigned char>& correct_iv = {})
static void TestPassphraseSingle(const std::span<const unsigned char> salt, const SecureString& passphrase, uint32_t rounds,
const std::span<const unsigned char> correct_key = {},
const std::span<const unsigned char> correct_iv = {})
{
CCrypter crypt;
crypt.SetKeyFromPassphrase(passphrase, salt, rounds, 0);
Expand All @@ -34,18 +34,18 @@ static void TestPassphraseSingle(const std::vector<unsigned char>& salt, const S
}
}

static void TestPassphrase(const std::vector<unsigned char>& salt, const SecureString& passphrase, uint32_t rounds,
const std::vector<unsigned char>& correct_key = {},
const std::vector<unsigned char>& correct_iv = {})
static void TestPassphrase(const std::span<const unsigned char> salt, const SecureString& passphrase, uint32_t rounds,
const std::span<const unsigned char> correct_key = {},
const std::span<const unsigned char> correct_iv = {})
{
TestPassphraseSingle(salt, passphrase, rounds, correct_key, correct_iv);
for (SecureString::const_iterator it{passphrase.begin()}; it != passphrase.end(); ++it) {
TestPassphraseSingle(salt, SecureString{it, passphrase.end()}, rounds);
}
}

static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& ciphertext,
const std::vector<unsigned char>& correct_plaintext = {})
static void TestDecrypt(const CCrypter& crypt, const std::span<const unsigned char> ciphertext,
const std::span<const unsigned char> correct_plaintext = {})
{
CKeyingMaterial decrypted;
crypt.Decrypt(ciphertext, decrypted);
Expand All @@ -55,7 +55,7 @@ static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>&
}

static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& plaintext,
const std::vector<unsigned char>& correct_ciphertext = {})
const std::span<const unsigned char> correct_ciphertext = {})
{
std::vector<unsigned char> ciphertext;
crypt.Encrypt(plaintext, ciphertext);
Expand All @@ -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<unsigned char> 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<unsigned char>& plaintext,
const std::vector<unsigned char>& correct_ciphertext = {})
static void TestEncrypt(const CCrypter& crypt, const std::span<const unsigned char> plaintext,
const std::span<const unsigned char> correct_ciphertext = {})
{
TestEncryptSingle(crypt, CKeyingMaterial{plaintext.begin(), plaintext.end()}, correct_ciphertext);
for (auto it{plaintext.begin()}; it != plaintext.end(); ++it) {
Expand Down Expand Up @@ -105,7 +104,7 @@ BOOST_AUTO_TEST_CASE(encrypt) {
for (int i = 0; i != 100; i++)
{
uint256 hash(GetRandHash());
TestCrypter::TestEncrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
TestCrypter::TestEncrypt(crypt, std::span<unsigned char>{hash.begin(), hash.end()});
}

}
Expand Down

0 comments on commit 403d86f

Please sign in to comment.