Skip to content

Commit 3e55f13

Browse files
committed
Merge #11024: tests: Remove OldSetKeyFromPassphrase/OldEncrypt/OldDecrypt
a897d0e tests: Remove OldSetKeyFromPassphrase/OldEncrypt/OldDecrypt (practicalswift) Pull request description: Reduces the number of non-free:d allocs with four (Δ in use at exit = -928 bytes). With this patch applied: ``` $ valgrind --leak-check=full --show-leak-kinds=all src/test/test_bitcoin --log_level=test_suite --run_test=wallet_crypto … ==20243== HEAP SUMMARY: ==20243== in use at exit: 72,704 bytes in 1 blocks ==20243== total heap usage: 53,138 allocs, 53,137 frees, 49,600,420 bytes allocated ==20243== ==20243== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1 ==20243== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==20243== by 0x6AA5EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21) ==20243== by 0x40106B9: call_init.part.0 (dl-init.c:72) ==20243== by 0x40107CA: call_init (dl-init.c:30) ==20243== by 0x40107CA: _dl_init (dl-init.c:120) ==20243== by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so) ==20243== by 0x2: ??? ==20243== by 0xFFF0006A2: ??? ==20243== by 0xFFF0006B8: ??? ==20243== by 0xFFF0006CF: ??? ==20243== ==20243== LEAK SUMMARY: ==20243== definitely lost: 0 bytes in 0 blocks ==20243== indirectly lost: 0 bytes in 0 blocks ==20243== possibly lost: 0 bytes in 0 blocks ==20243== still reachable: 72,704 bytes in 1 blocks ==20243== suppressed: 0 bytes in 0 blocks ``` Without this patch applied: ``` $ valgrind --leak-check=full --show-leak-kinds=all src/test/test_bitcoin --log_level=test_suite --run_test=wallet_crypto … ==19023== HEAP SUMMARY: ==19023== in use at exit: 73,632 bytes in 5 blocks ==19023== total heap usage: 52,718 allocs, 52,713 frees, 49,502,962 bytes allocated ==19023== ==19023== 24 bytes in 1 blocks are still reachable in loss record 1 of 5 ==19023== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==19023== by 0x642DE77: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E5665: lh_insert (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E7BB3: ??? (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E87AD: ERR_get_state (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E883D: ERR_put_error (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64EAAE4: EVP_DecryptFinal_ex (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x3AD150: wallet_crypto::OldDecrypt(std::vector<unsigned char, std::allocator<unsigned char> > const&, std::vector<unsigned char, secure_allocator<unsigned char> >&, unsigned char const*, unsigned char const*) (crypto_tests.cpp:81) ==19023== by 0x3AF892: wallet_crypto::TestCrypter::TestDecrypt(CCrypter const&, std::vector<unsigned char, std::allocator<unsigned char> > const&, std::vector<unsigned char, std::allocator<unsigned char> > const&) (crypto_tests.cpp:137) ==19023== by 0x3AD5E9: wallet_crypto::decrypt::test_method() (crypto_tests.cpp:223) ==19023== by 0x3ADC11: wallet_crypto::decrypt_invoker() (crypto_tests.cpp:216) ==19023== by 0x182596: invoke<void (*)()> (callback.hpp:56) ==19023== by 0x182596: boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()>::invoke() (callback.hpp:89) ==19023== ==19023== 128 bytes in 1 blocks are still reachable in loss record 2 of 5 ==19023== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==19023== by 0x642DE77: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E5331: lh_new (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E7862: ??? (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E7B7F: ??? (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E87AD: ERR_get_state (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E883D: ERR_put_error (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64EAAE4: EVP_DecryptFinal_ex (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x3AD150: wallet_crypto::OldDecrypt(std::vector<unsigned char, std::allocator<unsigned char> > const&, std::vector<unsigned char, secure_allocator<unsigned char> >&, unsigned char const*, unsigned char const*) (crypto_tests.cpp:81) ==19023== by 0x3AF892: wallet_crypto::TestCrypter::TestDecrypt(CCrypter const&, std::vector<unsigned char, std::allocator<unsigned char> > const&, std::vector<unsigned char, std::allocator<unsigned char> > const&) (crypto_tests.cpp:137) ==19023== by 0x3AD5E9: wallet_crypto::decrypt::test_method() (crypto_tests.cpp:223) ==19023== by 0x3ADC11: wallet_crypto::decrypt_invoker() (crypto_tests.cpp:216) ==19023== ==19023== 176 bytes in 1 blocks are still reachable in loss record 3 of 5 ==19023== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==19023== by 0x642DE77: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E530F: lh_new (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E7862: ??? (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E7B7F: ??? (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E87AD: ERR_get_state (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E883D: ERR_put_error (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64EAAE4: EVP_DecryptFinal_ex (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x3AD150: wallet_crypto::OldDecrypt(std::vector<unsigned char, std::allocator<unsigned char> > const&, std::vector<unsigned char, secure_allocator<unsigned char> >&, unsigned char const*, unsigned char const*) (crypto_tests.cpp:81) ==19023== by 0x3AF892: wallet_crypto::TestCrypter::TestDecrypt(CCrypter const&, std::vector<unsigned char, std::allocator<unsigned char> > const&, std::vector<unsigned char, std::allocator<unsigned char> > const&) (crypto_tests.cpp:137) ==19023== by 0x3AD5E9: wallet_crypto::decrypt::test_method() (crypto_tests.cpp:223) ==19023== by 0x3ADC11: wallet_crypto::decrypt_invoker() (crypto_tests.cpp:216) ==19023== ==19023== 600 bytes in 1 blocks are still reachable in loss record 4 of 5 ==19023== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==19023== by 0x642DE77: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E8745: ERR_get_state (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64E883D: ERR_put_error (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x64EAAE4: EVP_DecryptFinal_ex (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0) ==19023== by 0x3AD150: wallet_crypto::OldDecrypt(std::vector<unsigned char, std::allocator<unsigned char> > const&, std::vector<unsigned char, secure_allocator<unsigned char> >&, unsigned char const*, unsigned char const*) (crypto_tests.cpp:81) ==19023== by 0x3AF892: wallet_crypto::TestCrypter::TestDecrypt(CCrypter const&, std::vector<unsigned char, std::allocator<unsigned char> > const&, std::vector<unsigned char, std::allocator<unsigned char> > const&) (crypto_tests.cpp:137) ==19023== by 0x3AD5E9: wallet_crypto::decrypt::test_method() (crypto_tests.cpp:223) ==19023== by 0x3ADC11: wallet_crypto::decrypt_invoker() (crypto_tests.cpp:216) ==19023== by 0x182596: invoke<void (*)()> (callback.hpp:56) ==19023== by 0x182596: boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()>::invoke() (callback.hpp:89) ==19023== by 0x596CCB0: ??? (in /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.58.0) ==19023== by 0x594C995: boost::execution_monitor::catch_signals(boost::unit_test::callback0<int> const&) (in /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.58.0) ==19023== ==19023== 72,704 bytes in 1 blocks are still reachable in loss record 5 of 5 ==19023== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==19023== by 0x6AA5EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21) ==19023== by 0x40106B9: call_init.part.0 (dl-init.c:72) ==19023== by 0x40107CA: call_init (dl-init.c:30) ==19023== by 0x40107CA: _dl_init (dl-init.c:120) ==19023== by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so) ==19023== by 0x2: ??? ==19023== by 0xFFF0006A2: ??? ==19023== by 0xFFF0006B8: ??? ==19023== by 0xFFF0006CF: ??? ==19023== ==19023== LEAK SUMMARY: ==19023== definitely lost: 0 bytes in 0 blocks ==19023== indirectly lost: 0 bytes in 0 blocks ==19023== possibly lost: 0 bytes in 0 blocks ==19023== still reachable: 73,632 bytes in 5 blocks ==19023== suppressed: 0 bytes in 0 blocks ==19023== ==19023== For counts of detected and suppressed errors, rerun with: -v ==19023== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) ``` Tree-SHA512: 38b6552736a5710a42dbad770c490583cfc762acbec716f5db4cf38314f494ea99430713ea407c73b49d867676ced221a282437f3fcfd8346f8f68386f4fc74d
2 parents fc5c237 + a897d0e commit 3e55f13

File tree

1 file changed

+11
-122
lines changed

1 file changed

+11
-122
lines changed

src/wallet/test/crypto_tests.cpp

Lines changed: 11 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -9,112 +9,25 @@
99
#include <vector>
1010

1111
#include <boost/test/unit_test.hpp>
12-
#include <openssl/aes.h>
13-
#include <openssl/evp.h>
1412

1513
BOOST_FIXTURE_TEST_SUITE(wallet_crypto, BasicTestingSetup)
1614

17-
bool OldSetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod, unsigned char* chKey, unsigned char* chIV)
18-
{
19-
if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
20-
return false;
21-
22-
int i = 0;
23-
if (nDerivationMethod == 0)
24-
i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
25-
(unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
26-
27-
if (i != (int)WALLET_CRYPTO_KEY_SIZE)
28-
{
29-
memory_cleanse(chKey, WALLET_CRYPTO_KEY_SIZE);
30-
memory_cleanse(chIV, WALLET_CRYPTO_IV_SIZE);
31-
return false;
32-
}
33-
return true;
34-
}
35-
36-
bool OldEncrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext, const unsigned char chKey[32], const unsigned char chIV[16])
37-
{
38-
// max ciphertext len for a n bytes of plaintext is
39-
// n + AES_BLOCK_SIZE - 1 bytes
40-
int nLen = vchPlaintext.size();
41-
int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
42-
vchCiphertext = std::vector<unsigned char> (nCLen);
43-
44-
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
45-
46-
if (!ctx) return false;
47-
48-
bool fOk = true;
49-
50-
EVP_CIPHER_CTX_init(ctx);
51-
if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, chKey, chIV) != 0;
52-
if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
53-
if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
54-
EVP_CIPHER_CTX_cleanup(ctx);
55-
56-
EVP_CIPHER_CTX_free(ctx);
57-
58-
if (!fOk) return false;
59-
60-
vchCiphertext.resize(nCLen + nFLen);
61-
return true;
62-
}
63-
64-
bool OldDecrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext, const unsigned char chKey[32], const unsigned char chIV[16])
65-
{
66-
// plaintext will always be equal to or lesser than length of ciphertext
67-
int nLen = vchCiphertext.size();
68-
int nPLen = nLen, nFLen = 0;
69-
70-
vchPlaintext = CKeyingMaterial(nPLen);
71-
72-
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
73-
74-
if (!ctx) return false;
75-
76-
bool fOk = true;
77-
78-
EVP_CIPHER_CTX_init(ctx);
79-
if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, chKey, chIV) != 0;
80-
if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
81-
if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
82-
EVP_CIPHER_CTX_cleanup(ctx);
83-
84-
EVP_CIPHER_CTX_free(ctx);
85-
86-
if (!fOk) return false;
87-
88-
vchPlaintext.resize(nPLen + nFLen);
89-
return true;
90-
}
91-
9215
class TestCrypter
9316
{
9417
public:
9518
static void TestPassphraseSingle(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
9619
const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
9720
const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
9821
{
99-
unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
100-
unsigned char chIV[WALLET_CRYPTO_IV_SIZE];
101-
10222
CCrypter crypt;
10323
crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
10424

105-
OldSetKeyFromPassphrase(passphrase, vchSalt, rounds, 0, chKey, chIV);
106-
107-
BOOST_CHECK_MESSAGE(memcmp(chKey, crypt.vchKey.data(), crypt.vchKey.size()) == 0, \
108-
HexStr(chKey, chKey+sizeof(chKey)) + std::string(" != ") + HexStr(crypt.vchKey));
109-
BOOST_CHECK_MESSAGE(memcmp(chIV, crypt.vchIV.data(), crypt.vchIV.size()) == 0, \
110-
HexStr(chIV, chIV+sizeof(chIV)) + std::string(" != ") + HexStr(crypt.vchIV));
111-
11225
if(!correctKey.empty())
113-
BOOST_CHECK_MESSAGE(memcmp(chKey, &correctKey[0], sizeof(chKey)) == 0, \
114-
HexStr(chKey, chKey+sizeof(chKey)) + std::string(" != ") + HexStr(correctKey.begin(), correctKey.end()));
26+
BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, \
27+
HexStr(crypt.vchKey.begin(), crypt.vchKey.end()) + std::string(" != ") + HexStr(correctKey.begin(), correctKey.end()));
11528
if(!correctIV.empty())
116-
BOOST_CHECK_MESSAGE(memcmp(chIV, &correctIV[0], sizeof(chIV)) == 0,
117-
HexStr(chIV, chIV+sizeof(chIV)) + std::string(" != ") + HexStr(correctIV.begin(), correctIV.end()));
29+
BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0,
30+
HexStr(crypt.vchIV.begin(), crypt.vchIV.end()) + std::string(" != ") + HexStr(correctIV.begin(), correctIV.end()));
11831
}
11932

12033
static void TestPassphrase(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
@@ -126,50 +39,26 @@ static void TestPassphrase(const std::vector<unsigned char>& vchSalt, const Secu
12639
TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds);
12740
}
12841

129-
13042
static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext, \
13143
const std::vector<unsigned char>& vchPlaintext = std::vector<unsigned char>())
13244
{
133-
CKeyingMaterial vchDecrypted1;
134-
CKeyingMaterial vchDecrypted2;
135-
int result1, result2;
136-
result1 = crypt.Decrypt(vchCiphertext, vchDecrypted1);
137-
result2 = OldDecrypt(vchCiphertext, vchDecrypted2, crypt.vchKey.data(), crypt.vchIV.data());
138-
BOOST_CHECK(result1 == result2);
139-
140-
// These two should be equal. However, OpenSSL 1.0.1j introduced a change
141-
// that would zero all padding except for the last byte for failed decrypts.
142-
// This behavior was reverted for 1.0.1k.
143-
if (vchDecrypted1 != vchDecrypted2 && vchDecrypted1.size() >= AES_BLOCK_SIZE && SSLeay() == 0x100010afL)
144-
{
145-
for(CKeyingMaterial::iterator it = vchDecrypted1.end() - AES_BLOCK_SIZE; it != vchDecrypted1.end() - 1; it++)
146-
*it = 0;
147-
}
148-
149-
BOOST_CHECK_MESSAGE(vchDecrypted1 == vchDecrypted2, HexStr(vchDecrypted1.begin(), vchDecrypted1.end()) + " != " + HexStr(vchDecrypted2.begin(), vchDecrypted2.end()));
150-
45+
CKeyingMaterial vchDecrypted;
46+
crypt.Decrypt(vchCiphertext, vchDecrypted);
15147
if (vchPlaintext.size())
152-
BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted2);
48+
BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted);
15349
}
15450

15551
static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext,
15652
const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
15753
{
158-
std::vector<unsigned char> vchCiphertext1;
159-
std::vector<unsigned char> vchCiphertext2;
160-
int result1 = crypt.Encrypt(vchPlaintext, vchCiphertext1);
161-
162-
int result2 = OldEncrypt(vchPlaintext, vchCiphertext2, crypt.vchKey.data(), crypt.vchIV.data());
163-
BOOST_CHECK(result1 == result2);
164-
BOOST_CHECK(vchCiphertext1 == vchCiphertext2);
54+
std::vector<unsigned char> vchCiphertext;
55+
crypt.Encrypt(vchPlaintext, vchCiphertext);
16556

16657
if (!vchCiphertextCorrect.empty())
167-
BOOST_CHECK(vchCiphertext2 == vchCiphertextCorrect);
58+
BOOST_CHECK(vchCiphertext == vchCiphertextCorrect);
16859

16960
const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
170-
171-
if(vchCiphertext1 == vchCiphertext2)
172-
TestDecrypt(crypt, vchCiphertext1, vchPlaintext2);
61+
TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
17362
}
17463

17564
static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn, \

0 commit comments

Comments
 (0)