refactor: de-Hungarianize CCrypter

Beyond renaming it also adjusts whitespace and adds braces to conform to current doc/developer-notes.md.

TestEncrypt: Change iterator type to auto in ahead of vector -> span conversion.

Only touches functions that will be modified in next commit.
This commit is contained in:
Hodlinator 2024-08-20 23:44:57 +02:00
parent d99c816971
commit bd0830bbd4
No known key found for this signature in database
4 changed files with 87 additions and 74 deletions

View file

@ -12,7 +12,7 @@
#include <vector> #include <vector>
namespace wallet { namespace wallet {
int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const int CCrypter::BytesToKeySHA512AES(const std::vector<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 // This mimics the behavior of openssl's EVP_BytesToKey with an aes256cbc
// cipher and sha512 message digest. Because sha512's output size (64b) is // cipher and sha512 message digest. Because sha512's output size (64b) is
@ -25,8 +25,8 @@ int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, cons
unsigned char buf[CSHA512::OUTPUT_SIZE]; unsigned char buf[CSHA512::OUTPUT_SIZE];
CSHA512 di; CSHA512 di;
di.Write(UCharCast(strKeyData.data()), strKeyData.size()); di.Write(UCharCast(key_data.data()), key_data.size());
di.Write(chSalt.data(), chSalt.size()); di.Write(salt.data(), salt.size());
di.Finalize(buf); di.Finalize(buf);
for(int i = 0; i != count - 1; i++) for(int i = 0; i != count - 1; i++)
@ -38,14 +38,16 @@ int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, cons
return WALLET_CRYPTO_KEY_SIZE; return WALLET_CRYPTO_KEY_SIZE;
} }
bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod) bool CCrypter::SetKeyFromPassphrase(const SecureString& key_data, const std::vector<unsigned char>& salt, const unsigned int rounds, const unsigned int derivation_method)
{ {
if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE) if (rounds < 1 || salt.size() != WALLET_CRYPTO_SALT_SIZE) {
return false; return false;
}
int i = 0; int i = 0;
if (nDerivationMethod == 0) if (derivation_method == 0) {
i = BytesToKeySHA512AES(chSalt, strKeyData, nRounds, vchKey.data(), vchIV.data()); i = BytesToKeySHA512AES(salt, key_data, rounds, vchKey.data(), vchIV.data());
}
if (i != (int)WALLET_CRYPTO_KEY_SIZE) if (i != (int)WALLET_CRYPTO_KEY_SIZE)
{ {
@ -58,13 +60,14 @@ bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::v
return true; return true;
} }
bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV) bool CCrypter::SetKey(const CKeyingMaterial& new_key, const std::vector<unsigned char>& new_iv)
{ {
if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_IV_SIZE) if (new_key.size() != WALLET_CRYPTO_KEY_SIZE || new_iv.size() != WALLET_CRYPTO_IV_SIZE) {
return false; return false;
}
memcpy(vchKey.data(), chNewKey.data(), chNewKey.size()); memcpy(vchKey.data(), new_key.data(), new_key.size());
memcpy(vchIV.data(), chNewIV.data(), chNewIV.size()); memcpy(vchIV.data(), new_iv.data(), new_iv.size());
fKeySet = true; fKeySet = true;
return true; return true;
@ -88,19 +91,20 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
return true; return true;
} }
bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) const bool CCrypter::Decrypt(const std::vector<unsigned char>& ciphertext, CKeyingMaterial& plaintext) const
{ {
if (!fKeySet) if (!fKeySet)
return false; return false;
// plaintext will always be equal to or lesser than length of ciphertext // plaintext will always be equal to or lesser than length of ciphertext
vchPlaintext.resize(vchCiphertext.size()); plaintext.resize(ciphertext.size());
AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true); AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true);
int nLen = dec.Decrypt(vchCiphertext.data(), vchCiphertext.size(), vchPlaintext.data()); int len = dec.Decrypt(ciphertext.data(), ciphertext.size(), plaintext.data());
if(nLen == 0) if (len == 0) {
return false; return false;
vchPlaintext.resize(nLen); }
plaintext.resize(len);
return true; return true;
} }
@ -114,26 +118,29 @@ bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vch
return cKeyCrypter.Encrypt(vchPlaintext, vchCiphertext); return cKeyCrypter.Encrypt(vchPlaintext, vchCiphertext);
} }
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext) bool DecryptSecret(const CKeyingMaterial& master_key, const std::vector<unsigned char>& ciphertext, const uint256& iv, CKeyingMaterial& plaintext)
{ {
CCrypter cKeyCrypter; CCrypter key_crypter;
static_assert(WALLET_CRYPTO_IV_SIZE <= std::remove_reference_t<decltype(nIV)>::size()); static_assert(WALLET_CRYPTO_IV_SIZE <= std::remove_reference_t<decltype(iv)>::size());
std::vector<unsigned char> chIV{nIV.begin(), nIV.begin() + WALLET_CRYPTO_IV_SIZE}; std::vector<unsigned char> iv_prefix{iv.begin(), iv.begin() + WALLET_CRYPTO_IV_SIZE};
if(!cKeyCrypter.SetKey(vMasterKey, chIV)) if (!key_crypter.SetKey(master_key, iv_prefix)) {
return false; return false;
return cKeyCrypter.Decrypt(vchCiphertext, vchPlaintext); }
return key_crypter.Decrypt(ciphertext, plaintext);
} }
bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key) bool DecryptKey(const CKeyingMaterial& master_key, const std::vector<unsigned char>& crypted_secret, const CPubKey& pub_key, CKey& key)
{ {
CKeyingMaterial vchSecret; CKeyingMaterial secret;
if(!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) if (!DecryptSecret(master_key, crypted_secret, pub_key.GetHash(), secret)) {
return false; return false;
}
if (vchSecret.size() != 32) if (secret.size() != 32) {
return false; return false;
}
key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed()); key.Set(secret.begin(), secret.end(), pub_key.IsCompressed());
return key.VerifyPubKey(vchPubKey); return key.VerifyPubKey(pub_key);
} }
} // namespace wallet } // namespace wallet

View file

@ -75,13 +75,13 @@ private:
std::vector<unsigned char, secure_allocator<unsigned char>> vchIV; std::vector<unsigned char, secure_allocator<unsigned char>> vchIV;
bool fKeySet; bool fKeySet;
int BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const; int BytesToKeySHA512AES(const std::vector<unsigned char>& salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const;
public: public:
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod); bool SetKeyFromPassphrase(const SecureString& key_data, const std::vector<unsigned char>& salt, const unsigned int rounds, const unsigned int derivation_method);
bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const; bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const;
bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) const; bool Decrypt(const std::vector<unsigned char>& ciphertext, CKeyingMaterial& plaintext) const;
bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV); bool SetKey(const CKeyingMaterial& new_key, const std::vector<unsigned char>& new_iv);
void CleanKey() void CleanKey()
{ {
@ -104,8 +104,8 @@ public:
}; };
bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext); bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext); bool DecryptSecret(const CKeyingMaterial& master_key, const std::vector<unsigned char>& ciphertext, const uint256& iv, CKeyingMaterial& plaintext);
bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key); bool DecryptKey(const CKeyingMaterial& master_key, const std::vector<unsigned char>& crypted_secret, const CPubKey& pub_key, CKey& key);
} // namespace wallet } // namespace wallet
#endif // BITCOIN_WALLET_CRYPTER_H #endif // BITCOIN_WALLET_CRYPTER_H

View file

@ -35,11 +35,11 @@ FUZZ_TARGET(crypter, .init = initialize_crypter)
const unsigned int derivation_method = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<unsigned int>(); const unsigned int derivation_method = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<unsigned int>();
// Limiting the value of nRounds since it is otherwise uselessly expensive and causes a timeout when fuzzing. // Limiting the value of rounds since it is otherwise uselessly expensive and causes a timeout when fuzzing.
crypt.SetKeyFromPassphrase(/*strKeyData=*/secure_string, crypt.SetKeyFromPassphrase(/*key_data=*/secure_string,
/*chSalt=*/ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_SALT_SIZE), /*salt=*/ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_SALT_SIZE),
/*nRounds=*/fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 25000), /*rounds=*/fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 25000),
/*nDerivationMethod=*/derivation_method); /*derivation_method=*/derivation_method);
} }
LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 100) LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 100)

View file

@ -17,58 +17,64 @@ BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup)
class TestCrypter class TestCrypter
{ {
public: public:
static void TestPassphraseSingle(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds, static void TestPassphraseSingle(const std::vector<unsigned char>& salt, const SecureString& passphrase, uint32_t rounds,
const std::vector<unsigned char>& correctKey = {}, const std::vector<unsigned char>& correct_key = {},
const std::vector<unsigned char>& correctIV = {}) const std::vector<unsigned char>& correct_iv = {})
{ {
CCrypter crypt; CCrypter crypt;
crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0); crypt.SetKeyFromPassphrase(passphrase, salt, rounds, 0);
if(!correctKey.empty()) if (!correct_key.empty()) {
BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correct_key.data(), crypt.vchKey.size()) == 0,
HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correctKey)); HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correct_key));
if(!correctIV.empty()) }
BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0, if (!correct_iv.empty()) {
HexStr(crypt.vchIV) + std::string(" != ") + HexStr(correctIV)); BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correct_iv.data(), crypt.vchIV.size()) == 0,
HexStr(crypt.vchIV) + std::string(" != ") + HexStr(correct_iv));
}
} }
static void TestPassphrase(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds, static void TestPassphrase(const std::vector<unsigned char>& salt, const SecureString& passphrase, uint32_t rounds,
const std::vector<unsigned char>& correctKey = {}, const std::vector<unsigned char>& correct_key = {},
const std::vector<unsigned char>& correctIV = {}) const std::vector<unsigned char>& correct_iv = {})
{ {
TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV); TestPassphraseSingle(salt, passphrase, rounds, correct_key, correct_iv);
for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i) for (SecureString::const_iterator it{passphrase.begin()}; it != passphrase.end(); ++it) {
TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds); TestPassphraseSingle(salt, SecureString{it, passphrase.end()}, rounds);
}
} }
static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext, static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& ciphertext,
const std::vector<unsigned char>& vchCorrectPlaintext = {}) const std::vector<unsigned char>& correct_plaintext = {})
{ {
CKeyingMaterial vchDecrypted; CKeyingMaterial decrypted;
crypt.Decrypt(vchCiphertext, vchDecrypted); crypt.Decrypt(ciphertext, decrypted);
if (vchPlaintext.size()) if (!correct_plaintext.empty()) {
BOOST_CHECK_EQUAL_COLLECTIONS(vchDecrypted.begin(), vchDecrypted.end(), vchCorrectPlaintext.begin(), vchCorrectPlaintext.end()); BOOST_CHECK_EQUAL_COLLECTIONS(decrypted.begin(), decrypted.end(), correct_plaintext.begin(), correct_plaintext.end());
}
} }
static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext, static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& plaintext,
const std::vector<unsigned char>& vchCiphertextCorrect = {}) const std::vector<unsigned char>& correct_ciphertext = {})
{ {
std::vector<unsigned char> vchCiphertext; std::vector<unsigned char> ciphertext;
crypt.Encrypt(vchPlaintext, vchCiphertext); crypt.Encrypt(plaintext, ciphertext);
if (!vchCiphertextCorrect.empty()) if (!correct_ciphertext.empty()) {
BOOST_CHECK_EQUAL_COLLECTIONS(vchCiphertext.begin(), vchCiphertext.end(), vchCiphertextCorrect.begin(), vchCiphertextCorrect.end()); BOOST_CHECK_EQUAL_COLLECTIONS(ciphertext.begin(), ciphertext.end(), correct_ciphertext.begin(), correct_ciphertext.end());
}
const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end()); const std::vector<unsigned char> plaintext2(plaintext.begin(), plaintext.end());
TestDecrypt(crypt, vchCiphertext, vchPlaintext2); TestDecrypt(crypt, ciphertext, /*correct_plaintext=*/plaintext2);
} }
static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn, static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& plaintext,
const std::vector<unsigned char>& vchCiphertextCorrect = {}) const std::vector<unsigned char>& correct_ciphertext = {})
{ {
TestEncryptSingle(crypt, CKeyingMaterial{vchPlaintextIn.begin(), vchPlaintextIn.end()}, vchCiphertextCorrect); TestEncryptSingle(crypt, CKeyingMaterial{plaintext.begin(), plaintext.end()}, correct_ciphertext);
for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i) for (auto it{plaintext.begin()}; it != plaintext.end(); ++it) {
TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end())); TestEncryptSingle(crypt, CKeyingMaterial{it, plaintext.end()});
}
} }
}; };