From c2d1138472669fdd201273f4d1640362c54440db Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Tue, 22 Aug 2017 17:00:20 +0200 Subject: [PATCH] Merge #11024: tests: Remove OldSetKeyFromPassphrase/OldEncrypt/OldDecrypt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 > const&, std::vector >&, unsigned char const*, unsigned char const*) (crypto_tests.cpp:81) ==19023== by 0x3AF892: wallet_crypto::TestCrypter::TestDecrypt(CCrypter const&, std::vector > const&, std::vector > 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 (callback.hpp:56) ==19023== by 0x182596: boost::unit_test::ut_detail::callback0_impl_t::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 > const&, std::vector >&, unsigned char const*, unsigned char const*) (crypto_tests.cpp:81) ==19023== by 0x3AF892: wallet_crypto::TestCrypter::TestDecrypt(CCrypter const&, std::vector > const&, std::vector > 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 > const&, std::vector >&, unsigned char const*, unsigned char const*) (crypto_tests.cpp:81) ==19023== by 0x3AF892: wallet_crypto::TestCrypter::TestDecrypt(CCrypter const&, std::vector > const&, std::vector > 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 > const&, std::vector >&, unsigned char const*, unsigned char const*) (crypto_tests.cpp:81) ==19023== by 0x3AF892: wallet_crypto::TestCrypter::TestDecrypt(CCrypter const&, std::vector > const&, std::vector > 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 (callback.hpp:56) ==19023== by 0x182596: boost::unit_test::ut_detail::callback0_impl_t::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 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 --- src/wallet/test/crypto_tests.cpp | 205 ++----------------------------- 1 file changed, 11 insertions(+), 194 deletions(-) diff --git a/src/wallet/test/crypto_tests.cpp b/src/wallet/test/crypto_tests.cpp index f9008c37678edf..876a0caf960fdf 100644 --- a/src/wallet/test/crypto_tests.cpp +++ b/src/wallet/test/crypto_tests.cpp @@ -9,158 +9,9 @@ #include #include -#include -#include BOOST_FIXTURE_TEST_SUITE(wallet_crypto, BasicTestingSetup) -bool OldSetKeyFromPassphrase(const SecureString& strKeyData, const std::vector& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod, unsigned char* chKey, unsigned char* chIV) -{ - if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE) - return false; - - int i = 0; - if (nDerivationMethod == 0) - i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0], - (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV); - - if (i != (int)WALLET_CRYPTO_KEY_SIZE) - { - memory_cleanse(chKey, WALLET_CRYPTO_KEY_SIZE); - memory_cleanse(chIV, WALLET_CRYPTO_IV_SIZE); - return false; - } - return true; -} - -bool OldEncrypt(const CKeyingMaterial& vchPlaintext, std::vector &vchCiphertext, const unsigned char chKey[32], const unsigned char chIV[16]) -{ - // max ciphertext len for a n bytes of plaintext is - // n + AES_BLOCK_SIZE - 1 bytes - int nLen = vchPlaintext.size(); - int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0; - vchCiphertext = std::vector (nCLen); - - EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); - - if (!ctx) return false; - - bool fOk = true; - - EVP_CIPHER_CTX_init(ctx); - if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, chKey, chIV) != 0; - if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0; - if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0; - EVP_CIPHER_CTX_cleanup(ctx); - - EVP_CIPHER_CTX_free(ctx); - - if (!fOk) return false; - - vchCiphertext.resize(nCLen + nFLen); - return true; -} - -bool OldDecrypt(const std::vector& vchCiphertext, CKeyingMaterial& vchPlaintext, const unsigned char chKey[32], const unsigned char chIV[16]) -{ - // plaintext will always be equal to or lesser than length of ciphertext - int nLen = vchCiphertext.size(); - int nPLen = nLen, nFLen = 0; - - vchPlaintext = CKeyingMaterial(nPLen); - - EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); - - if (!ctx) return false; - - bool fOk = true; - - EVP_CIPHER_CTX_init(ctx); - if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, chKey, chIV) != 0; - if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0; - if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0; - EVP_CIPHER_CTX_cleanup(ctx); - - EVP_CIPHER_CTX_free(ctx); - - if (!fOk) return false; - - vchPlaintext.resize(nPLen + nFLen); - return true; -} - -// General secure AES 256 CBC encryption routine -bool OldEncryptAES256(const SecureString& sKey, const SecureString& sPlaintext, const std::string& sIV, std::string& sCiphertext) -{ - // max ciphertext len for a n bytes of plaintext is - // n + AES_BLOCK_SIZE - 1 bytes - int nLen = sPlaintext.size(); - int nCLen = nLen + AES_BLOCK_SIZE; - int nFLen = 0; - - // Verify key sizes - if(sKey.size() != 32 || sIV.size() != AES_BLOCK_SIZE) { - LogPrintf("crypter EncryptAES256 - Invalid key or block size: Key: %d sIV:%d\n", sKey.size(), sIV.size()); - return false; - } - - // Prepare output buffer - sCiphertext.resize(nCLen); - - // Perform the encryption - EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); - - if (!ctx) return false; - - bool fOk = true; - - EVP_CIPHER_CTX_init(ctx); - if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, (const unsigned char*) &sKey[0], (const unsigned char*) &sIV[0]); - if (fOk) fOk = EVP_EncryptUpdate(ctx, (unsigned char*) &sCiphertext[0], &nCLen, (const unsigned char*) &sPlaintext[0], nLen); - if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (unsigned char*) (&sCiphertext[0])+nCLen, &nFLen); - EVP_CIPHER_CTX_cleanup(ctx); - - EVP_CIPHER_CTX_free(ctx); - - if (!fOk) return false; - - sCiphertext.resize(nCLen + nFLen); - return true; -} - -bool OldDecryptAES256(const SecureString& sKey, const std::string& sCiphertext, const std::string& sIV, SecureString& sPlaintext) -{ - // plaintext will always be equal to or lesser than length of ciphertext - int nLen = sCiphertext.size(); - int nPLen = nLen, nFLen = 0; - - // Verify key sizes - if(sKey.size() != 32 || sIV.size() != AES_BLOCK_SIZE) { - LogPrintf("crypter DecryptAES256 - Invalid key or block size\n"); - return false; - } - - sPlaintext.resize(nPLen); - - EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); - - if (!ctx) return false; - - bool fOk = true; - - EVP_CIPHER_CTX_init(ctx); - if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, (const unsigned char*) &sKey[0], (const unsigned char*) &sIV[0]); - if (fOk) fOk = EVP_DecryptUpdate(ctx, (unsigned char *) &sPlaintext[0], &nPLen, (const unsigned char *) &sCiphertext[0], nLen); - if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (unsigned char *) (&sPlaintext[0])+nPLen, &nFLen); - EVP_CIPHER_CTX_cleanup(ctx); - - EVP_CIPHER_CTX_free(ctx); - - if (!fOk) return false; - - sPlaintext.resize(nPLen + nFLen); - return true; -} void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, const std::string &hexin, const std::string &hexout) { @@ -186,25 +37,15 @@ static void TestPassphraseSingle(const std::vector& vchSalt, cons const std::vector& correctKey = std::vector(), const std::vector& correctIV=std::vector()) { - unsigned char chKey[WALLET_CRYPTO_KEY_SIZE]; - unsigned char chIV[WALLET_CRYPTO_IV_SIZE]; - CCrypter crypt; crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0); - OldSetKeyFromPassphrase(passphrase, vchSalt, rounds, 0, chKey, chIV); - - BOOST_CHECK_MESSAGE(memcmp(chKey, crypt.vchKey.data(), crypt.vchKey.size()) == 0, \ - HexStr(chKey, chKey+sizeof(chKey)) + std::string(" != ") + HexStr(crypt.vchKey)); - BOOST_CHECK_MESSAGE(memcmp(chIV, crypt.vchIV.data(), crypt.vchIV.size()) == 0, \ - HexStr(chIV, chIV+sizeof(chIV)) + std::string(" != ") + HexStr(crypt.vchIV)); - if(!correctKey.empty()) - BOOST_CHECK_MESSAGE(memcmp(chKey, &correctKey[0], sizeof(chKey)) == 0, \ - HexStr(chKey, chKey+sizeof(chKey)) + std::string(" != ") + HexStr(correctKey.begin(), correctKey.end())); + BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, \ + HexStr(crypt.vchKey.begin(), crypt.vchKey.end()) + std::string(" != ") + HexStr(correctKey.begin(), correctKey.end())); if(!correctIV.empty()) - BOOST_CHECK_MESSAGE(memcmp(chIV, &correctIV[0], sizeof(chIV)) == 0, - HexStr(chIV, chIV+sizeof(chIV)) + std::string(" != ") + HexStr(correctIV.begin(), correctIV.end())); + BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0, + HexStr(crypt.vchIV.begin(), crypt.vchIV.end()) + std::string(" != ") + HexStr(correctIV.begin(), correctIV.end())); } static void TestPassphrase(const std::vector& vchSalt, const SecureString& passphrase, uint32_t rounds, @@ -216,50 +57,26 @@ static void TestPassphrase(const std::vector& vchSalt, const Secu TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds); } - static void TestDecrypt(const CCrypter& crypt, const std::vector& vchCiphertext, \ const std::vector& vchPlaintext = std::vector()) { - CKeyingMaterial vchDecrypted1; - CKeyingMaterial vchDecrypted2; - int result1, result2; - result1 = crypt.Decrypt(vchCiphertext, vchDecrypted1); - result2 = OldDecrypt(vchCiphertext, vchDecrypted2, crypt.vchKey.data(), crypt.vchIV.data()); - BOOST_CHECK(result1 == result2); - - // These two should be equal. However, OpenSSL 1.0.1j introduced a change - // that would zero all padding except for the last byte for failed decrypts. - // This behavior was reverted for 1.0.1k. - if (vchDecrypted1 != vchDecrypted2 && vchDecrypted1.size() >= AES_BLOCK_SIZE && SSLeay() == 0x100010afL) - { - for(CKeyingMaterial::iterator it = vchDecrypted1.end() - AES_BLOCK_SIZE; it != vchDecrypted1.end() - 1; it++) - *it = 0; - } - - BOOST_CHECK_MESSAGE(vchDecrypted1 == vchDecrypted2, HexStr(vchDecrypted1.begin(), vchDecrypted1.end()) + " != " + HexStr(vchDecrypted2.begin(), vchDecrypted2.end())); - + CKeyingMaterial vchDecrypted; + crypt.Decrypt(vchCiphertext, vchDecrypted); if (vchPlaintext.size()) - BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted2); + BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted); } static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext, const std::vector& vchCiphertextCorrect = std::vector()) { - std::vector vchCiphertext1; - std::vector vchCiphertext2; - int result1 = crypt.Encrypt(vchPlaintext, vchCiphertext1); - - int result2 = OldEncrypt(vchPlaintext, vchCiphertext2, crypt.vchKey.data(), crypt.vchIV.data()); - BOOST_CHECK(result1 == result2); - BOOST_CHECK(vchCiphertext1 == vchCiphertext2); + std::vector vchCiphertext; + crypt.Encrypt(vchPlaintext, vchCiphertext); if (!vchCiphertextCorrect.empty()) - BOOST_CHECK(vchCiphertext2 == vchCiphertextCorrect); + BOOST_CHECK(vchCiphertext == vchCiphertextCorrect); const std::vector vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end()); - - if(vchCiphertext1 == vchCiphertext2) - TestDecrypt(crypt, vchCiphertext1, vchPlaintext2); + TestDecrypt(crypt, vchCiphertext, vchPlaintext2); } static void TestEncrypt(const CCrypter& crypt, const std::vector& vchPlaintextIn, \