Skip to content

Commit 403d86f

Browse files
committed
refactor: vector -> span in CCrypter
TestEncryptSingle: Remove no longer needed plaintext2-variable that existed because vectors had different allocators.
1 parent bd0830b commit 403d86f

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

src/wallet/crypter.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <vector>
1313

1414
namespace wallet {
15-
int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const
15+
int CCrypter::BytesToKeySHA512AES(const std::span<const unsigned char> salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const
1616
{
1717
// This mimics the behavior of openssl's EVP_BytesToKey with an aes256cbc
1818
// cipher and sha512 message digest. Because sha512's output size (64b) is
@@ -38,7 +38,7 @@ int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& salt, const
3838
return WALLET_CRYPTO_KEY_SIZE;
3939
}
4040

41-
bool CCrypter::SetKeyFromPassphrase(const SecureString& key_data, const std::vector<unsigned char>& salt, const unsigned int rounds, const unsigned int derivation_method)
41+
bool CCrypter::SetKeyFromPassphrase(const SecureString& key_data, const std::span<const unsigned char> salt, const unsigned int rounds, const unsigned int derivation_method)
4242
{
4343
if (rounds < 1 || salt.size() != WALLET_CRYPTO_SALT_SIZE) {
4444
return false;
@@ -60,7 +60,7 @@ bool CCrypter::SetKeyFromPassphrase(const SecureString& key_data, const std::vec
6060
return true;
6161
}
6262

63-
bool CCrypter::SetKey(const CKeyingMaterial& new_key, const std::vector<unsigned char>& new_iv)
63+
bool CCrypter::SetKey(const CKeyingMaterial& new_key, const std::span<const unsigned char> new_iv)
6464
{
6565
if (new_key.size() != WALLET_CRYPTO_KEY_SIZE || new_iv.size() != WALLET_CRYPTO_IV_SIZE) {
6666
return false;
@@ -91,7 +91,7 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
9191
return true;
9292
}
9393

94-
bool CCrypter::Decrypt(const std::vector<unsigned char>& ciphertext, CKeyingMaterial& plaintext) const
94+
bool CCrypter::Decrypt(const std::span<const unsigned char> ciphertext, CKeyingMaterial& plaintext) const
9595
{
9696
if (!fKeySet)
9797
return false;
@@ -118,18 +118,18 @@ bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vch
118118
return cKeyCrypter.Encrypt(vchPlaintext, vchCiphertext);
119119
}
120120

121-
bool DecryptSecret(const CKeyingMaterial& master_key, const std::vector<unsigned char>& ciphertext, const uint256& iv, CKeyingMaterial& plaintext)
121+
bool DecryptSecret(const CKeyingMaterial& master_key, const std::span<const unsigned char> ciphertext, const uint256& iv, CKeyingMaterial& plaintext)
122122
{
123123
CCrypter key_crypter;
124124
static_assert(WALLET_CRYPTO_IV_SIZE <= std::remove_reference_t<decltype(iv)>::size());
125-
std::vector<unsigned char> iv_prefix{iv.begin(), iv.begin() + WALLET_CRYPTO_IV_SIZE};
125+
const std::span iv_prefix{iv.data(), WALLET_CRYPTO_IV_SIZE};
126126
if (!key_crypter.SetKey(master_key, iv_prefix)) {
127127
return false;
128128
}
129129
return key_crypter.Decrypt(ciphertext, plaintext);
130130
}
131131

132-
bool DecryptKey(const CKeyingMaterial& master_key, const std::vector<unsigned char>& crypted_secret, const CPubKey& pub_key, CKey& key)
132+
bool DecryptKey(const CKeyingMaterial& master_key, const std::span<const unsigned char> crypted_secret, const CPubKey& pub_key, CKey& key)
133133
{
134134
CKeyingMaterial secret;
135135
if (!DecryptSecret(master_key, crypted_secret, pub_key.GetHash(), secret)) {

src/wallet/crypter.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ friend class wallet_crypto_tests::TestCrypter; // for test access to chKey/chIV
7575
std::vector<unsigned char, secure_allocator<unsigned char>> vchIV;
7676
bool fKeySet;
7777

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

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

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

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

111111
#endif // BITCOIN_WALLET_CRYPTER_H

src/wallet/test/wallet_crypto_tests.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup)
1717
class TestCrypter
1818
{
1919
public:
20-
static void TestPassphraseSingle(const std::vector<unsigned char>& salt, const SecureString& passphrase, uint32_t rounds,
21-
const std::vector<unsigned char>& correct_key = {},
22-
const std::vector<unsigned char>& correct_iv = {})
20+
static void TestPassphraseSingle(const std::span<const unsigned char> salt, const SecureString& passphrase, uint32_t rounds,
21+
const std::span<const unsigned char> correct_key = {},
22+
const std::span<const unsigned char> correct_iv = {})
2323
{
2424
CCrypter crypt;
2525
crypt.SetKeyFromPassphrase(passphrase, salt, rounds, 0);
@@ -34,18 +34,18 @@ static void TestPassphraseSingle(const std::vector<unsigned char>& salt, const S
3434
}
3535
}
3636

37-
static void TestPassphrase(const std::vector<unsigned char>& salt, const SecureString& passphrase, uint32_t rounds,
38-
const std::vector<unsigned char>& correct_key = {},
39-
const std::vector<unsigned char>& correct_iv = {})
37+
static void TestPassphrase(const std::span<const unsigned char> salt, const SecureString& passphrase, uint32_t rounds,
38+
const std::span<const unsigned char> correct_key = {},
39+
const std::span<const unsigned char> correct_iv = {})
4040
{
4141
TestPassphraseSingle(salt, passphrase, rounds, correct_key, correct_iv);
4242
for (SecureString::const_iterator it{passphrase.begin()}; it != passphrase.end(); ++it) {
4343
TestPassphraseSingle(salt, SecureString{it, passphrase.end()}, rounds);
4444
}
4545
}
4646

47-
static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& ciphertext,
48-
const std::vector<unsigned char>& correct_plaintext = {})
47+
static void TestDecrypt(const CCrypter& crypt, const std::span<const unsigned char> ciphertext,
48+
const std::span<const unsigned char> correct_plaintext = {})
4949
{
5050
CKeyingMaterial decrypted;
5151
crypt.Decrypt(ciphertext, decrypted);
@@ -55,7 +55,7 @@ static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>&
5555
}
5656

5757
static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& plaintext,
58-
const std::vector<unsigned char>& correct_ciphertext = {})
58+
const std::span<const unsigned char> correct_ciphertext = {})
5959
{
6060
std::vector<unsigned char> ciphertext;
6161
crypt.Encrypt(plaintext, ciphertext);
@@ -64,12 +64,11 @@ static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& plai
6464
BOOST_CHECK_EQUAL_COLLECTIONS(ciphertext.begin(), ciphertext.end(), correct_ciphertext.begin(), correct_ciphertext.end());
6565
}
6666

67-
const std::vector<unsigned char> plaintext2(plaintext.begin(), plaintext.end());
68-
TestDecrypt(crypt, ciphertext, /*correct_plaintext=*/plaintext2);
67+
TestDecrypt(crypt, ciphertext, /*correct_plaintext=*/plaintext);
6968
}
7069

71-
static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& plaintext,
72-
const std::vector<unsigned char>& correct_ciphertext = {})
70+
static void TestEncrypt(const CCrypter& crypt, const std::span<const unsigned char> plaintext,
71+
const std::span<const unsigned char> correct_ciphertext = {})
7372
{
7473
TestEncryptSingle(crypt, CKeyingMaterial{plaintext.begin(), plaintext.end()}, correct_ciphertext);
7574
for (auto it{plaintext.begin()}; it != plaintext.end(); ++it) {
@@ -105,7 +104,7 @@ BOOST_AUTO_TEST_CASE(encrypt) {
105104
for (int i = 0; i != 100; i++)
106105
{
107106
uint256 hash(GetRandHash());
108-
TestCrypter::TestEncrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
107+
TestCrypter::TestEncrypt(crypt, std::span<unsigned char>{hash.begin(), hash.end()});
109108
}
110109

111110
}

0 commit comments

Comments
 (0)