Skip to content

Commit d99c816

Browse files
committed
refactor: Improve CCrypter related lines
Lines will be touched in next 2 commits.
1 parent 7e1d9a8 commit d99c816

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

src/wallet/crypter.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <crypto/aes.h>
99
#include <crypto/sha512.h>
1010

11+
#include <type_traits>
1112
#include <vector>
1213

1314
namespace wallet {
@@ -24,7 +25,7 @@ int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, cons
2425
unsigned char buf[CSHA512::OUTPUT_SIZE];
2526
CSHA512 di;
2627

27-
di.Write((const unsigned char*)strKeyData.data(), strKeyData.size());
28+
di.Write(UCharCast(strKeyData.data()), strKeyData.size());
2829
di.Write(chSalt.data(), chSalt.size());
2930
di.Finalize(buf);
3031

@@ -93,12 +94,10 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
9394
return false;
9495

9596
// plaintext will always be equal to or lesser than length of ciphertext
96-
int nLen = vchCiphertext.size();
97-
98-
vchPlaintext.resize(nLen);
97+
vchPlaintext.resize(vchCiphertext.size());
9998

10099
AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true);
101-
nLen = dec.Decrypt(vchCiphertext.data(), vchCiphertext.size(), vchPlaintext.data());
100+
int nLen = dec.Decrypt(vchCiphertext.data(), vchCiphertext.size(), vchPlaintext.data());
102101
if(nLen == 0)
103102
return false;
104103
vchPlaintext.resize(nLen);
@@ -118,8 +117,8 @@ bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vch
118117
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
119118
{
120119
CCrypter cKeyCrypter;
121-
std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
122-
memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE);
120+
static_assert(WALLET_CRYPTO_IV_SIZE <= std::remove_reference_t<decltype(nIV)>::size());
121+
std::vector<unsigned char> chIV{nIV.begin(), nIV.begin() + WALLET_CRYPTO_IV_SIZE};
123122
if(!cKeyCrypter.SetKey(vMasterKey, chIV))
124123
return false;
125124
return cKeyCrypter.Decrypt(vchCiphertext, vchPlaintext);

src/wallet/test/wallet_crypto_tests.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,55 +18,55 @@ class TestCrypter
1818
{
1919
public:
2020
static void TestPassphraseSingle(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
21-
const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
22-
const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
21+
const std::vector<unsigned char>& correctKey = {},
22+
const std::vector<unsigned char>& correctIV = {})
2323
{
2424
CCrypter crypt;
2525
crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
2626

2727
if(!correctKey.empty())
28-
BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, \
28+
BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0,
2929
HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correctKey));
3030
if(!correctIV.empty())
3131
BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0,
3232
HexStr(crypt.vchIV) + std::string(" != ") + HexStr(correctIV));
3333
}
3434

3535
static void TestPassphrase(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
36-
const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
37-
const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
36+
const std::vector<unsigned char>& correctKey = {},
37+
const std::vector<unsigned char>& correctIV = {})
3838
{
3939
TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV);
4040
for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
4141
TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds);
4242
}
4343

44-
static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext, \
45-
const std::vector<unsigned char>& vchPlaintext = std::vector<unsigned char>())
44+
static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext,
45+
const std::vector<unsigned char>& vchCorrectPlaintext = {})
4646
{
4747
CKeyingMaterial vchDecrypted;
4848
crypt.Decrypt(vchCiphertext, vchDecrypted);
4949
if (vchPlaintext.size())
50-
BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted);
50+
BOOST_CHECK_EQUAL_COLLECTIONS(vchDecrypted.begin(), vchDecrypted.end(), vchCorrectPlaintext.begin(), vchCorrectPlaintext.end());
5151
}
5252

5353
static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext,
54-
const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
54+
const std::vector<unsigned char>& vchCiphertextCorrect = {})
5555
{
5656
std::vector<unsigned char> vchCiphertext;
5757
crypt.Encrypt(vchPlaintext, vchCiphertext);
5858

5959
if (!vchCiphertextCorrect.empty())
60-
BOOST_CHECK(vchCiphertext == vchCiphertextCorrect);
60+
BOOST_CHECK_EQUAL_COLLECTIONS(vchCiphertext.begin(), vchCiphertext.end(), vchCiphertextCorrect.begin(), vchCiphertextCorrect.end());
6161

6262
const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
6363
TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
6464
}
6565

66-
static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn, \
67-
const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
66+
static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn,
67+
const std::vector<unsigned char>& vchCiphertextCorrect = {})
6868
{
69-
TestEncryptSingle(crypt, CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()), vchCiphertextCorrect);
69+
TestEncryptSingle(crypt, CKeyingMaterial{vchPlaintextIn.begin(), vchPlaintextIn.end()}, vchCiphertextCorrect);
7070
for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
7171
TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
7272
}
@@ -76,8 +76,8 @@ static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>&
7676
BOOST_AUTO_TEST_CASE(passphrase) {
7777
// These are expensive.
7878

79-
TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000, \
80-
ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \
79+
TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000,
80+
ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"),
8181
ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
8282

8383
std::string hash(GetRandHash().ToString());

0 commit comments

Comments
 (0)