Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2014-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <test/util/random.h>6#include <test/util/setup_common.h>7#include <util/strencodings.h>8#include <wallet/crypter.h>10#include <vector>12#include <boost/test/unit_test.hpp>14using namespace util::hex_literals;16namespace wallet {17BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup)19class TestCrypter20{21public:22static void TestPassphraseSingle(const std::span<const unsigned char> salt, const SecureString& passphrase, uint32_t rounds,23const std::span<const unsigned char> correct_key = {},24const std::span<const unsigned char> correct_iv = {})25{26CCrypter crypt;27BOOST_REQUIRE(crypt.SetKeyFromPassphrase(passphrase, salt, rounds, /*derivation_method=*/0));29if (!correct_key.empty()) {30BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correct_key.data(), crypt.vchKey.size()) == 0,31HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correct_key));32}33if (!correct_iv.empty()) {34BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correct_iv.data(), crypt.vchIV.size()) == 0,35HexStr(crypt.vchIV) + std::string(" != ") + HexStr(correct_iv));36}37}39static void TestPassphrase(const std::span<const unsigned char> salt, const SecureString& passphrase, uint32_t rounds,40const std::span<const unsigned char> correct_key = {},41const std::span<const unsigned char> correct_iv = {})42{43TestPassphraseSingle(salt, passphrase, rounds, correct_key, correct_iv);44for (SecureString::const_iterator it{passphrase.begin()}; it != passphrase.end(); ++it) {45TestPassphraseSingle(salt, SecureString{it, passphrase.end()}, rounds);46}47}49static void TestDecrypt(const CCrypter& crypt, const std::span<const unsigned char> ciphertext,50const std::span<const unsigned char> correct_plaintext = {})51{52CKeyingMaterial decrypted;53const bool decrypt_ok{crypt.Decrypt(ciphertext, decrypted)};54if (!correct_plaintext.empty()) {55BOOST_REQUIRE(decrypt_ok);56BOOST_CHECK_EQUAL_COLLECTIONS(decrypted.begin(), decrypted.end(), correct_plaintext.begin(), correct_plaintext.end());57}58}60static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& plaintext,61const std::span<const unsigned char> correct_ciphertext = {})62{63std::vector<unsigned char> ciphertext;64BOOST_REQUIRE(crypt.Encrypt(plaintext, ciphertext));66if (!correct_ciphertext.empty()) {67BOOST_CHECK_EQUAL_COLLECTIONS(ciphertext.begin(), ciphertext.end(), correct_ciphertext.begin(), correct_ciphertext.end());68}70TestDecrypt(crypt, ciphertext, /*correct_plaintext=*/plaintext);71}73static void TestEncrypt(const CCrypter& crypt, const std::span<const unsigned char> plaintext,74const std::span<const unsigned char> correct_ciphertext = {})75{76TestEncryptSingle(crypt, CKeyingMaterial{plaintext.begin(), plaintext.end()}, correct_ciphertext);77for (auto it{plaintext.begin()}; it != plaintext.end(); ++it) {78TestEncryptSingle(crypt, CKeyingMaterial{it, plaintext.end()});79}80}82};84BOOST_AUTO_TEST_CASE(passphrase) {85// These are expensive.87TestCrypter::TestPassphrase("0000deadbeef0000"_hex_u8, "test", CMasterKey::DEFAULT_DERIVE_ITERATIONS,88"fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"_hex_u8,89"cf2f2691526dd1aa220896fb8bf7c369"_hex_u8);91std::string hash(GetRandHash().ToString());92std::vector<unsigned char> vchSalt(8);93GetRandBytes(vchSalt);94uint32_t rounds = m_rng.rand32();95if (rounds > 30000)96rounds = 30000;97TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()), rounds);98}100BOOST_AUTO_TEST_CASE(passphrase_zero_rounds) {101constexpr auto salt{"0000deadbeef0000"_hex_u8};102CCrypter crypt;103BOOST_CHECK(!crypt.SetKeyFromPassphrase("passphrase", salt, /*rounds=*/0, /*derivation_method=*/0));104BOOST_CHECK(crypt.SetKeyFromPassphrase("passphrase", salt, /*rounds=*/1, /*derivation_method=*/0));105}107BOOST_AUTO_TEST_CASE(encrypt) {108constexpr std::array<uint8_t, WALLET_CRYPTO_SALT_SIZE> salt{"0000deadbeef0000"_hex_u8};109CCrypter crypt;110BOOST_REQUIRE(crypt.SetKeyFromPassphrase("passphrase", salt, CMasterKey::DEFAULT_DERIVE_ITERATIONS, /*derivation_method=*/0));111TestCrypter::TestEncrypt(crypt, "22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"_hex_u8);113for (int i = 0; i != 100; i++)114{115uint256 hash(GetRandHash());116TestCrypter::TestEncrypt(crypt, std::span<unsigned char>{hash.begin(), hash.end()});117}119}121BOOST_AUTO_TEST_CASE(decrypt) {122constexpr std::array<uint8_t, WALLET_CRYPTO_SALT_SIZE> salt{"0000deadbeef0000"_hex_u8};123CCrypter crypt;124BOOST_REQUIRE(crypt.SetKeyFromPassphrase("passphrase", salt, CMasterKey::DEFAULT_DERIVE_ITERATIONS, /*derivation_method=*/0));126// Some corner cases that came up while testing127TestCrypter::TestDecrypt(crypt,"795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"_hex_u8);128TestCrypter::TestDecrypt(crypt,"de096f4a8f9bd97db012aa9d90d74de8cdea779c3ee8bc7633d8b5d6da703486"_hex_u8);129TestCrypter::TestDecrypt(crypt,"32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173de25947f98cf8b7ace49449c"_hex_u8);130TestCrypter::TestDecrypt(crypt,"e7c055cca2faa78cb9ac22c9357a90b4778ded9b2cc220a14cea49f931e596ea"_hex_u8);131TestCrypter::TestDecrypt(crypt,"b88efddd668a6801d19516d6830da4ae9811988ccbaf40df8fbb72f3f4d335fd"_hex_u8);132TestCrypter::TestDecrypt(crypt,"8cae76aa6a43694e961ebcb28c8ca8f8540b84153d72865e8561ddd93fa7bfa9"_hex_u8);134for (int i = 0; i != 100; i++)135{136uint256 hash(GetRandHash());137TestCrypter::TestDecrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));138}139}141BOOST_AUTO_TEST_SUITE_END()142} // namespace wallet
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.