Bitcoin Core 31.99.0
P2P Digital Currency
wallet_crypto_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2014-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <test/util/random.h>
7#include <util/strencodings.h>
8#include <wallet/crypter.h>
9
10#include <vector>
11
12#include <boost/test/unit_test.hpp>
13
14using namespace util::hex_literals;
15
16namespace wallet {
18
20{
21public:
22static void TestPassphraseSingle(const std::span<const unsigned char> salt, const SecureString& passphrase, uint32_t rounds,
23 const std::span<const unsigned char> correct_key = {},
24 const std::span<const unsigned char> correct_iv = {})
25{
26 CCrypter crypt;
27 BOOST_REQUIRE(crypt.SetKeyFromPassphrase(passphrase, salt, rounds, /*derivation_method=*/0));
28
29 if (!correct_key.empty()) {
30 BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correct_key.data(), crypt.vchKey.size()) == 0,
31 HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correct_key));
32 }
33 if (!correct_iv.empty()) {
34 BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correct_iv.data(), crypt.vchIV.size()) == 0,
35 HexStr(crypt.vchIV) + std::string(" != ") + HexStr(correct_iv));
36 }
37}
38
39static void TestPassphrase(const std::span<const unsigned char> salt, const SecureString& passphrase, uint32_t rounds,
40 const std::span<const unsigned char> correct_key = {},
41 const std::span<const unsigned char> correct_iv = {})
42{
43 TestPassphraseSingle(salt, passphrase, rounds, correct_key, correct_iv);
44 for (SecureString::const_iterator it{passphrase.begin()}; it != passphrase.end(); ++it) {
45 TestPassphraseSingle(salt, SecureString{it, passphrase.end()}, rounds);
46 }
47}
48
49static void TestDecrypt(const CCrypter& crypt, const std::span<const unsigned char> ciphertext,
50 const std::span<const unsigned char> correct_plaintext = {})
51{
52 CKeyingMaterial decrypted;
53 const bool decrypt_ok{crypt.Decrypt(ciphertext, decrypted)};
54 if (!correct_plaintext.empty()) {
55 BOOST_REQUIRE(decrypt_ok);
56 BOOST_CHECK_EQUAL_COLLECTIONS(decrypted.begin(), decrypted.end(), correct_plaintext.begin(), correct_plaintext.end());
57 }
58}
59
60static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& plaintext,
61 const std::span<const unsigned char> correct_ciphertext = {})
62{
63 std::vector<unsigned char> ciphertext;
64 BOOST_REQUIRE(crypt.Encrypt(plaintext, ciphertext));
65
66 if (!correct_ciphertext.empty()) {
67 BOOST_CHECK_EQUAL_COLLECTIONS(ciphertext.begin(), ciphertext.end(), correct_ciphertext.begin(), correct_ciphertext.end());
68 }
69
70 TestDecrypt(crypt, ciphertext, /*correct_plaintext=*/plaintext);
71}
72
73static void TestEncrypt(const CCrypter& crypt, const std::span<const unsigned char> plaintext,
74 const std::span<const unsigned char> correct_ciphertext = {})
75{
76 TestEncryptSingle(crypt, CKeyingMaterial{plaintext.begin(), plaintext.end()}, correct_ciphertext);
77 for (auto it{plaintext.begin()}; it != plaintext.end(); ++it) {
78 TestEncryptSingle(crypt, CKeyingMaterial{it, plaintext.end()});
79 }
80}
81
82};
83
85 // These are expensive.
86
88 "fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"_hex_u8,
89 "cf2f2691526dd1aa220896fb8bf7c369"_hex_u8);
90
91 std::string hash(GetRandHash().ToString());
92 std::vector<unsigned char> vchSalt(8);
93 GetRandBytes(vchSalt);
94 uint32_t rounds = m_rng.rand32();
95 if (rounds > 30000)
96 rounds = 30000;
97 TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()), rounds);
98}
99
100BOOST_AUTO_TEST_CASE(passphrase_zero_rounds) {
101 constexpr auto salt{"0000deadbeef0000"_hex_u8};
102 CCrypter crypt;
103 BOOST_CHECK(!crypt.SetKeyFromPassphrase("passphrase", salt, /*rounds=*/0, /*derivation_method=*/0));
104 BOOST_CHECK(crypt.SetKeyFromPassphrase("passphrase", salt, /*rounds=*/1, /*derivation_method=*/0));
105}
106
108 constexpr std::array<uint8_t, WALLET_CRYPTO_SALT_SIZE> salt{"0000deadbeef0000"_hex_u8};
109 CCrypter crypt;
110 BOOST_REQUIRE(crypt.SetKeyFromPassphrase("passphrase", salt, CMasterKey::DEFAULT_DERIVE_ITERATIONS, /*derivation_method=*/0));
111 TestCrypter::TestEncrypt(crypt, "22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"_hex_u8);
112
113 for (int i = 0; i != 100; i++)
114 {
115 uint256 hash(GetRandHash());
116 TestCrypter::TestEncrypt(crypt, std::span<unsigned char>{hash.begin(), hash.end()});
117 }
118
119}
120
122 constexpr std::array<uint8_t, WALLET_CRYPTO_SALT_SIZE> salt{"0000deadbeef0000"_hex_u8};
123 CCrypter crypt;
124 BOOST_REQUIRE(crypt.SetKeyFromPassphrase("passphrase", salt, CMasterKey::DEFAULT_DERIVE_ITERATIONS, /*derivation_method=*/0));
125
126 // Some corner cases that came up while testing
127 TestCrypter::TestDecrypt(crypt,"795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"_hex_u8);
128 TestCrypter::TestDecrypt(crypt,"de096f4a8f9bd97db012aa9d90d74de8cdea779c3ee8bc7633d8b5d6da703486"_hex_u8);
129 TestCrypter::TestDecrypt(crypt,"32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173de25947f98cf8b7ace49449c"_hex_u8);
130 TestCrypter::TestDecrypt(crypt,"e7c055cca2faa78cb9ac22c9357a90b4778ded9b2cc220a14cea49f931e596ea"_hex_u8);
131 TestCrypter::TestDecrypt(crypt,"b88efddd668a6801d19516d6830da4ae9811988ccbaf40df8fbb72f3f4d335fd"_hex_u8);
132 TestCrypter::TestDecrypt(crypt,"8cae76aa6a43694e961ebcb28c8ca8f8540b84153d72865e8561ddd93fa7bfa9"_hex_u8);
133
134 for (int i = 0; i != 100; i++)
135 {
136 uint256 hash(GetRandHash());
137 TestCrypter::TestDecrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
138 }
139}
140
142} // namespace wallet
constexpr unsigned char * end()
Definition: uint256.h:102
constexpr unsigned char * begin()
Definition: uint256.h:101
256-bit opaque blob.
Definition: uint256.h:196
Encryption/decryption context with key information.
Definition: crypter.h:72
bool Decrypt(std::span< const unsigned char > ciphertext, CKeyingMaterial &plaintext) const
Definition: crypter.cpp:94
bool SetKeyFromPassphrase(const SecureString &key_data, std::span< const unsigned char > salt, unsigned int rounds, unsigned int derivation_method)
Definition: crypter.cpp:41
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
Definition: crypter.cpp:76
static constexpr unsigned int DEFAULT_DERIVE_ITERATIONS
Default/minimum number of key derivation rounds.
Definition: crypter.h:48
static void TestDecrypt(const CCrypter &crypt, const std::span< const unsigned char > ciphertext, const std::span< const unsigned char > correct_plaintext={})
static void TestPassphraseSingle(const std::span< const unsigned char > salt, const SecureString &passphrase, uint32_t rounds, const std::span< const unsigned char > correct_key={}, const std::span< const unsigned char > correct_iv={})
static void TestEncrypt(const CCrypter &crypt, const std::span< const unsigned char > plaintext, const std::span< const unsigned char > correct_ciphertext={})
static void TestEncryptSingle(const CCrypter &crypt, const CKeyingMaterial &plaintext, const std::span< const unsigned char > correct_ciphertext={})
static void TestPassphrase(const std::span< const unsigned char > salt, const SecureString &passphrase, uint32_t rounds, const std::span< const unsigned char > correct_key={}, const std::span< const unsigned char > correct_iv={})
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:30
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
Definition: strencodings.h:386
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:249
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:63
BOOST_AUTO_TEST_CASE(bnb_test)
#define BOOST_CHECK(expr)
Definition: object.cpp:16
void GetRandBytes(std::span< unsigned char > bytes) noexcept
Generate random data via the internal PRNG.
Definition: random.cpp:601
uint256 GetRandHash() noexcept
Generate a random uint256.
Definition: random.h:463
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:53
Basic testing setup.
Definition: setup_common.h:58