diff --git a/src/Makefile.test.include b/src/Makefile.test.include index 43862a3b60..0ac8238085 100644 --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -1,204 +1,204 @@ # Copyright (c) 2013-2016 The Bitcoin Core developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. TESTS += test/test_bitcoin LOG_DRIVER = $(srcdir)/test/test-bitcoin-driver EXTRA_DIST += test/test-bitcoin-driver bin_PROGRAMS += test/test_bitcoin noinst_PROGRAMS += test/test_bitcoin_fuzzy TEST_SRCDIR = test TEST_BINARY=test/test_bitcoin$(EXEEXT) JSON_TEST_FILES = \ test/data/script_tests.json \ test/data/base58_keys_valid.json \ test/data/base58_encode_decode.json \ test/data/base58_keys_invalid.json \ test/data/tx_invalid.json \ test/data/tx_valid.json \ test/data/sighash.json RAW_TEST_FILES = GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.raw.h) # test_bitcoin binary # BITCOIN_TESTS =\ test/scriptnum10.h \ test/activation_tests.cpp \ test/addrman_tests.cpp \ test/allocator_tests.cpp \ test/amount_tests.cpp \ test/arith_uint256_tests.cpp \ test/avalanche_tests.cpp \ test/base32_tests.cpp \ test/base58_tests.cpp \ test/base64_tests.cpp \ test/bip32_tests.cpp \ test/blockcheck_tests.cpp \ test/blockencodings_tests.cpp \ test/blockindex_tests.cpp \ test/blockstatus_tests.cpp \ test/bloom_tests.cpp \ test/bswap_tests.cpp \ test/cashaddr_tests.cpp \ test/cashaddrenc_tests.cpp \ test/checkdatasig_tests.cpp \ test/checkpoints_tests.cpp \ test/checkqueue_tests.cpp \ test/coins_tests.cpp \ test/compress_tests.cpp \ test/config_tests.cpp \ test/core_io_tests.cpp \ test/crypto_tests.cpp \ test/cuckoocache_tests.cpp \ test/dbwrapper_tests.cpp \ test/DoS_tests.cpp \ test/dstencode_tests.cpp \ test/excessiveblock_tests.cpp \ test/feerate_tests.cpp \ test/finalization_tests.cpp \ test/getarg_tests.cpp \ test/hash_tests.cpp \ test/inv_tests.cpp \ test/jsonutil.cpp \ test/jsonutil.h \ test/key_tests.cpp \ test/lcg_tests.cpp \ test/lcg.h \ test/limitedmap_tests.cpp \ test/main_tests.cpp \ test/mempool_tests.cpp \ test/merkle_tests.cpp \ test/miner_tests.cpp \ test/monolith_opcodes_tests.cpp \ test/multisig_tests.cpp \ test/net_tests.cpp \ test/netbase_tests.cpp \ test/pmt_tests.cpp \ test/policyestimator_tests.cpp \ test/pow_tests.cpp \ test/prevector_tests.cpp \ test/radix_tests.cpp \ test/raii_event_tests.cpp \ test/random_tests.cpp \ test/rcu_tests.cpp \ test/reverselock_tests.cpp \ test/rpc_tests.cpp \ test/rwcollection_tests.cpp \ test/sanity_tests.cpp \ test/scheduler_tests.cpp \ test/schnorr_tests.cpp \ test/script_commitment_tests.cpp \ test/script_P2SH_tests.cpp \ test/script_tests.cpp \ test/scriptflags.cpp \ test/scriptflags.h \ test/scriptnum_tests.cpp \ test/serialize_tests.cpp \ test/sigcache_tests.cpp \ test/sigencoding_tests.cpp \ test/sighash_tests.cpp \ test/sighashtype_tests.cpp \ test/sigopcount_tests.cpp \ test/sigutil.cpp \ test/sigutil.h \ test/skiplist_tests.cpp \ test/streams_tests.cpp \ test/sync_tests.cpp \ test/test_bitcoin.cpp \ test/test_bitcoin.h \ test/test_bitcoin_main.cpp \ test/timedata_tests.cpp \ test/transaction_tests.cpp \ test/txvalidationcache_tests.cpp \ test/versionbits_tests.cpp \ test/uint256_tests.cpp \ test/undo_tests.cpp \ test/univalue_tests.cpp \ test/util_tests.cpp \ test/validation_tests.cpp \ test/work_comparator_tests.cpp \ rpc/test/server_tests.cpp if ENABLE_WALLET BITCOIN_TESTS += \ wallet/test/wallet_test_fixture.cpp \ wallet/test/wallet_test_fixture.h \ wallet/test/accounting_tests.cpp \ wallet/test/wallet_tests.cpp \ wallet/test/walletdb_tests.cpp \ - wallet/test/crypto_tests.cpp + wallet/test/wallet_crypto_tests.cpp endif test_test_bitcoin_SOURCES = $(BITCOIN_TESTS) $(JSON_TEST_FILES) $(RAW_TEST_FILES) test_test_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -I$(builddir)/test/ $(TESTDEFS) $(EVENT_CFLAGS) test_test_bitcoin_LDADD = if ENABLE_WALLET test_test_bitcoin_LDADD += $(LIBBITCOIN_WALLET) endif test_test_bitcoin_LDADD += $(LIBBITCOIN_SERVER) $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL) $(LIBBITCOIN_CONSENSUS) $(LIBBITCOIN_CRYPTO) $(LIBUNIVALUE) \ $(LIBLEVELDB) $(LIBLEVELDB_SSE42) $(LIBMEMENV) $(BOOST_LIBS) $(BOOST_UNIT_TEST_FRAMEWORK_LIB) $(LIBSECP256K1) $(EVENT_LIBS) $(EVENT_PTHREADS_LIBS) test_test_bitcoin_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) test_test_bitcoin_LDADD += $(LIBBITCOIN_CONSENSUS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) test_test_bitcoin_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) -static if ENABLE_ZMQ test_test_bitcoin_LDADD += $(ZMQ_LIBS) endif # # test_bitcoin_fuzzy binary # test_test_bitcoin_fuzzy_SOURCES = test/test_bitcoin_fuzzy.cpp test_test_bitcoin_fuzzy_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) test_test_bitcoin_fuzzy_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) test_test_bitcoin_fuzzy_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) test_test_bitcoin_fuzzy_LDADD = \ $(LIBUNIVALUE) \ $(LIBBITCOIN_SERVER) \ $(LIBBITCOIN_COMMON) \ $(LIBBITCOIN_UTIL) \ $(LIBBITCOIN_CONSENSUS) \ $(LIBBITCOIN_CRYPTO) \ $(LIBSECP256K1) test_test_bitcoin_fuzzy_LDADD += $(BOOST_LIBS) $(CRYPTO_LIBS) # nodist_test_test_bitcoin_SOURCES = $(GENERATED_TEST_FILES) $(BITCOIN_TESTS): $(GENERATED_TEST_FILES) CLEAN_BITCOIN_TEST = test/*.gcda test/*.gcno $(GENERATED_TEST_FILES) CLEANFILES += $(CLEAN_BITCOIN_TEST) bitcoin_test: $(TEST_BINARY) bitcoin_test_check: $(TEST_BINARY) FORCE $(MAKE) check-TESTS TESTS=$^ bitcoin_test_clean : FORCE rm -f $(CLEAN_BITCOIN_TEST) $(test_test_bitcoin_OBJECTS) $(TEST_BINARY) check-local: @echo "Running test/util/bitcoin-util-test.py..." $(top_builddir)/test/util/bitcoin-util-test.py $(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C secp256k1 check if EMBEDDED_UNIVALUE $(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C univalue check endif %.json.h: %.json @$(MKDIR_P) $(@D) @{ \ echo "namespace json_tests{" && \ echo "static unsigned const char $(*F)[] = {" && \ $(HEXDUMP) -v -e '8/1 "0x%02x, "' -e '"\n"' $< | $(SED) -e 's/0x ,//g' && \ echo "};};"; \ } > "$@.new" && mv -f "$@.new" "$@" @echo "Generated $@" diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index faf3e02c15..e389f2d704 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,175 +1,175 @@ # Copyright (c) 2018 The Bitcoin developers project(bitcoin-test) # Process json files. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/data") find_program(PYTHON python) function(gen_json_header NAME) set(HEADERS "") foreach(f ${ARGN}) set(h "${CMAKE_CURRENT_BINARY_DIR}/${f}.h") # Get the proper name for the test variable. get_filename_component(TEST_NAME ${f} NAME_WE) add_custom_command(OUTPUT ${h} COMMAND ${PYTHON} ARGS "${CMAKE_CURRENT_SOURCE_DIR}/data/generate_header.py" "${TEST_NAME}" "${CMAKE_CURRENT_SOURCE_DIR}/${f}" > ${h} MAIN_DEPENDENCY ${f} DEPENDS "data/generate_header.py" VERBATIM ) list(APPEND HEADERS ${h}) endforeach(f) set(${NAME} "${HEADERS}" PARENT_SCOPE) endfunction() gen_json_header(JSON_HEADERS data/script_tests.json data/base58_keys_valid.json data/base58_encode_decode.json data/base58_keys_invalid.json data/tx_invalid.json data/tx_valid.json data/sighash.json ) include(TestSuite) create_test_suite(bitcoin) add_dependencies(check check-bitcoin) add_test_to_suite(bitcoin test_bitcoin activation_tests.cpp addrman_tests.cpp allocator_tests.cpp amount_tests.cpp arith_uint256_tests.cpp avalanche_tests.cpp base32_tests.cpp base58_tests.cpp base64_tests.cpp bip32_tests.cpp blockcheck_tests.cpp blockencodings_tests.cpp blockindex_tests.cpp blockstatus_tests.cpp bloom_tests.cpp bswap_tests.cpp cashaddr_tests.cpp cashaddrenc_tests.cpp checkdatasig_tests.cpp checkpoints_tests.cpp checkqueue_tests.cpp coins_tests.cpp compress_tests.cpp config_tests.cpp core_io_tests.cpp crypto_tests.cpp cuckoocache_tests.cpp dbwrapper_tests.cpp DoS_tests.cpp dstencode_tests.cpp excessiveblock_tests.cpp feerate_tests.cpp finalization_tests.cpp getarg_tests.cpp hash_tests.cpp inv_tests.cpp jsonutil.cpp key_tests.cpp lcg_tests.cpp limitedmap_tests.cpp main_tests.cpp mempool_tests.cpp merkle_tests.cpp miner_tests.cpp monolith_opcodes_tests.cpp multisig_tests.cpp net_tests.cpp netbase_tests.cpp pmt_tests.cpp policyestimator_tests.cpp pow_tests.cpp prevector_tests.cpp radix_tests.cpp raii_event_tests.cpp random_tests.cpp rcu_tests.cpp reverselock_tests.cpp rpc_tests.cpp rwcollection_tests.cpp sanity_tests.cpp scheduler_tests.cpp schnorr_tests.cpp script_commitment_tests.cpp script_P2SH_tests.cpp script_tests.cpp scriptflags.cpp scriptnum_tests.cpp serialize_tests.cpp sigcache_tests.cpp sigencoding_tests.cpp sighash_tests.cpp sighashtype_tests.cpp sigopcount_tests.cpp sigutil.cpp skiplist_tests.cpp streams_tests.cpp sync_tests.cpp test_bitcoin.cpp test_bitcoin_main.cpp timedata_tests.cpp transaction_tests.cpp txvalidationcache_tests.cpp versionbits_tests.cpp uint256_tests.cpp undo_tests.cpp univalue_tests.cpp util_tests.cpp validation_tests.cpp work_comparator_tests.cpp # RPC Tests ../rpc/test/server_tests.cpp # Tests generated from JSON ${JSON_HEADERS} ) target_include_directories(test_bitcoin PUBLIC # To access the generated json headers. ${CMAKE_CURRENT_BINARY_DIR} ) find_package(Boost 1.58 REQUIRED unit_test_framework) target_link_libraries(test_bitcoin Boost::unit_test_framework rpcclient server) # We need to detect if the BOOST_TEST_DYN_LINK flag is required. set(CMAKE_REQUIRED_LIBRARIES Boost::unit_test_framework) check_cxx_source_compiles(" #define BOOST_TEST_DYN_LINK #define BOOST_TEST_MAIN #include " BOOST_TEST_DYN_LINK) if(BOOST_TEST_DYN_LINK) target_compile_definitions(test_bitcoin PRIVATE BOOST_TEST_DYN_LINK) endif(BOOST_TEST_DYN_LINK) if(BUILD_BITCOIN_WALLET) target_sources(test_bitcoin PRIVATE ../wallet/test/wallet_test_fixture.cpp ../wallet/test/accounting_tests.cpp ../wallet/test/wallet_tests.cpp ../wallet/test/walletdb_tests.cpp - ../wallet/test/crypto_tests.cpp + ../wallet/test/wallet_crypto_tests.cpp ) endif() diff --git a/src/wallet/crypter.h b/src/wallet/crypter.h index 76efa6e9a1..f1d3f0eea6 100644 --- a/src/wallet/crypter.h +++ b/src/wallet/crypter.h @@ -1,162 +1,162 @@ // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_WALLET_CRYPTER_H #define BITCOIN_WALLET_CRYPTER_H #include "keystore.h" #include "serialize.h" #include "support/allocators/secure.h" #include class uint256; const unsigned int WALLET_CRYPTO_KEY_SIZE = 32; const unsigned int WALLET_CRYPTO_SALT_SIZE = 8; const unsigned int WALLET_CRYPTO_IV_SIZE = 16; /** * Private key encryption is done based on a CMasterKey, which holds a salt and * random encryption key. * * CMasterKeys are encrypted using AES-256-CBC using a key derived using * derivation method nDerivationMethod (0 == EVP_sha512()) and derivation * iterations nDeriveIterations. vchOtherDerivationParameters is provided for * alternative algorithms which may require more parameters (such as scrypt). * * Wallet Private Keys are then encrypted using AES-256-CBC with the * double-sha256 of the public key as the IV, and the master key's key as the * encryption key (see keystore.[ch]). */ /** Master key for wallet encryption */ class CMasterKey { public: std::vector vchCryptedKey; std::vector vchSalt; //! 0 = EVP_sha512() //! 1 = scrypt() unsigned int nDerivationMethod; unsigned int nDeriveIterations; //! Use this for more parameters to key derivation, such as the various //! parameters to scrypt std::vector vchOtherDerivationParameters; ADD_SERIALIZE_METHODS; template inline void SerializationOp(Stream &s, Operation ser_action) { READWRITE(vchCryptedKey); READWRITE(vchSalt); READWRITE(nDerivationMethod); READWRITE(nDeriveIterations); READWRITE(vchOtherDerivationParameters); } CMasterKey() { // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M // ie slightly lower than the lowest hardware we need bother supporting nDeriveIterations = 25000; nDerivationMethod = 0; vchOtherDerivationParameters = std::vector(0); } }; typedef std::vector> CKeyingMaterial; -namespace wallet_crypto { +namespace wallet_crypto_tests { class TestCrypter; } /** Encryption/decryption context with key information */ class CCrypter { // for test access to chKey/chIV - friend class wallet_crypto::TestCrypter; + friend class wallet_crypto_tests::TestCrypter; private: std::vector> vchKey; std::vector> vchIV; bool fKeySet; int BytesToKeySHA512AES(const std::vector &chSalt, const SecureString &strKeyData, int count, uint8_t *key, uint8_t *iv) const; public: bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod); bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector &vchCiphertext) const; bool Decrypt(const std::vector &vchCiphertext, CKeyingMaterial &vchPlaintext) const; bool SetKey(const CKeyingMaterial &chNewKey, const std::vector &chNewIV); void CleanKey() { memory_cleanse(vchKey.data(), vchKey.size()); memory_cleanse(vchIV.data(), vchIV.size()); fKeySet = false; } CCrypter() { fKeySet = false; vchKey.resize(WALLET_CRYPTO_KEY_SIZE); vchIV.resize(WALLET_CRYPTO_IV_SIZE); } ~CCrypter() { CleanKey(); } }; /** * Keystore which keeps the private keys encrypted. * It derives from the basic key store, which is used if no encryption is * active. */ class CCryptoKeyStore : public CBasicKeyStore { private: CKeyingMaterial vMasterKey; //! if fUseCrypto is true, mapKeys must be empty //! if fUseCrypto is false, vMasterKey must be empty std::atomic fUseCrypto; //! keeps track of whether Unlock has run a thorough check before bool fDecryptionThoroughlyChecked; protected: bool SetCrypted(); //! will encrypt previously unencrypted keys bool EncryptKeys(CKeyingMaterial &vMasterKeyIn); bool Unlock(const CKeyingMaterial &vMasterKeyIn); CryptedKeyMap mapCryptedKeys; public: CCryptoKeyStore() : fUseCrypto(false), fDecryptionThoroughlyChecked(false) {} bool IsCrypted() const { return fUseCrypto; } bool IsLocked() const; bool Lock(); virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret); bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override; bool HaveKey(const CKeyID &address) const override; bool GetKey(const CKeyID &address, CKey &keyOut) const override; bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override; std::set GetKeys() const override; /** * Wallet status (encrypted, locked) changed. * Note: Called without locks held. */ boost::signals2::signal NotifyStatusChanged; }; #endif // BITCOIN_WALLET_CRYPTER_H diff --git a/src/wallet/test/crypto_tests.cpp b/src/wallet/test/wallet_crypto_tests.cpp similarity index 99% rename from src/wallet/test/crypto_tests.cpp rename to src/wallet/test/wallet_crypto_tests.cpp index 7376cb9959..ff36bf179f 100644 --- a/src/wallet/test/crypto_tests.cpp +++ b/src/wallet/test/wallet_crypto_tests.cpp @@ -1,294 +1,294 @@ // Copyright (c) 2014-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "test/test_bitcoin.h" #include "utilstrencodings.h" #include "wallet/crypter.h" #include #include #include #include -BOOST_FIXTURE_TEST_SUITE(wallet_crypto, BasicTestingSetup) +BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup) bool OldSetKeyFromPassphrase(const SecureString &strKeyData, const std::vector &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod, uint8_t *chKey, uint8_t *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], (uint8_t *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV); if (i != (int)WALLET_CRYPTO_KEY_SIZE) { memory_cleanse(chKey, sizeof(chKey)); memory_cleanse(chIV, sizeof(chIV)); return false; } return true; } bool OldEncrypt(const CKeyingMaterial &vchPlaintext, std::vector &vchCiphertext, const uint8_t chKey[32], const uint8_t 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 uint8_t chKey[32], const uint8_t 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; } class TestCrypter { public: static void TestPassphraseSingle( const std::vector &vchSalt, const SecureString &passphrase, uint32_t rounds, const std::vector &correctKey = std::vector(), const std::vector &correctIV = std::vector()) { uint8_t chKey[WALLET_CRYPTO_KEY_SIZE]; uint8_t 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())); 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())); } static void TestPassphrase( const std::vector &vchSalt, const SecureString &passphrase, uint32_t rounds, const std::vector &correctKey = std::vector(), const std::vector &correctIV = std::vector()) { TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV); for (SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i) 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())); if (vchPlaintext.size()) BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted2); } 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); if (!vchCiphertextCorrect.empty()) BOOST_CHECK(vchCiphertext2 == vchCiphertextCorrect); const std::vector vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end()); if (vchCiphertext1 == vchCiphertext2) TestDecrypt(crypt, vchCiphertext1, vchPlaintext2); } static void TestEncrypt(const CCrypter &crypt, const std::vector &vchPlaintextIn, const std::vector &vchCiphertextCorrect = std::vector()) { TestEncryptSingle( crypt, CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()), vchCiphertextCorrect); for (std::vector::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i) TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end())); } }; BOOST_AUTO_TEST_CASE(passphrase) { // These are expensive. TestCrypter::TestPassphrase( ParseHex("0000deadbeef0000"), "test", 25000, ParseHex( "fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), ParseHex("cf2f2691526dd1aa220896fb8bf7c369")); std::string hash(GetRandHash().ToString()); std::vector vchSalt(8); GetRandBytes(&vchSalt[0], vchSalt.size()); uint32_t rounds = insecure_rand(); if (rounds > 30000) rounds = 30000; TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()), rounds); } BOOST_AUTO_TEST_CASE(encrypt) { std::vector vchSalt = ParseHex("0000deadbeef0000"); BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE); CCrypter crypt; crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0); TestCrypter::TestEncrypt(crypt, ParseHex("22bcade09ac03ff6386914359cfe885cfeb5f77f" "f0d670f102f619687453b29d")); for (int i = 0; i != 100; i++) { uint256 hash(GetRandHash()); TestCrypter::TestEncrypt( crypt, std::vector(hash.begin(), hash.end())); } } BOOST_AUTO_TEST_CASE(decrypt) { std::vector vchSalt = ParseHex("0000deadbeef0000"); BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE); CCrypter crypt; crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0); // Some corner cases the came up while testing TestCrypter::TestDecrypt(crypt, ParseHex("795643ce39d736088367822cdc50535ec6f10371" "5e3e48f4f3b1a60a08ef59ca")); TestCrypter::TestDecrypt(crypt, ParseHex("de096f4a8f9bd97db012aa9d90d74de8cdea779c" "3ee8bc7633d8b5d6da703486")); TestCrypter::TestDecrypt(crypt, ParseHex("32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173" "de25947f98cf8b7ace49449c")); TestCrypter::TestDecrypt(crypt, ParseHex("e7c055cca2faa78cb9ac22c9357a90b4778ded9b" "2cc220a14cea49f931e596ea")); TestCrypter::TestDecrypt(crypt, ParseHex("b88efddd668a6801d19516d6830da4ae9811988c" "cbaf40df8fbb72f3f4d335fd")); TestCrypter::TestDecrypt(crypt, ParseHex("8cae76aa6a43694e961ebcb28c8ca8f8540b8415" "3d72865e8561ddd93fa7bfa9")); for (int i = 0; i != 100; i++) { uint256 hash(GetRandHash()); TestCrypter::TestDecrypt( crypt, std::vector(hash.begin(), hash.end())); } } BOOST_AUTO_TEST_SUITE_END()