mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-21 14:34:49 +01:00
refactor: vector -> span in CCrypter
TestEncryptSingle: Remove no longer needed plaintext2-variable that existed because vectors had different allocators.
This commit is contained in:
parent
bd0830bbd4
commit
403d86f1cc
3 changed files with 26 additions and 27 deletions
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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)) {
|
||||
|
|
|
@ -75,13 +75,13 @@ private:
|
|||
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()
|
||||
{
|
||||
|
@ -104,8 +104,8 @@ public:
|
|||
};
|
||||
|
||||
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
|
||||
|
|
|
@ -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);
|
||||
|
@ -34,9 +34,9 @@ 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) {
|
||||
|
@ -44,8 +44,8 @@ static void TestPassphrase(const std::vector<unsigned char>& salt, const SecureS
|
|||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -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);
|
||||
|
@ -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) {
|
||||
|
@ -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()});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue