Branch data Line data Source code
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>
6 : : #include <test/util/setup_common.h>
7 : : #include <util/strencodings.h>
8 : : #include <wallet/crypter.h>
9 : :
10 : : #include <vector>
11 : :
12 : : #include <boost/test/unit_test.hpp>
13 : :
14 : : using namespace util::hex_literals;
15 : :
16 : : namespace wallet {
17 : : BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup)
18 : :
19 : : class TestCrypter
20 : : {
21 : : public:
22 : 70 : static 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 : 70 : CCrypter crypt;
27 [ + - + - : 140 : BOOST_REQUIRE(crypt.SetKeyFromPassphrase(passphrase, salt, rounds, /*derivation_method=*/0));
+ - + + ]
28 : :
29 [ + + ]: 70 : if (!correct_key.empty()) {
30 [ + - + - : 3 : 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 [ + + ]: 70 : if (!correct_iv.empty()) {
34 [ + - + - : 3 : 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 : 70 : }
38 : :
39 : 2 : static 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 : 2 : TestPassphraseSingle(salt, passphrase, rounds, correct_key, correct_iv);
44 [ - + + + ]: 70 : for (SecureString::const_iterator it{passphrase.begin()}; it != passphrase.end(); ++it) {
45 [ + - ]: 136 : TestPassphraseSingle(salt, SecureString{it, passphrase.end()}, rounds);
46 : : }
47 : 2 : }
48 : :
49 : 3439 : static void TestDecrypt(const CCrypter& crypt, const std::span<const unsigned char> ciphertext,
50 : : const std::span<const unsigned char> correct_plaintext = {})
51 : : {
52 : 3439 : CKeyingMaterial decrypted;
53 [ + - ]: 3439 : const bool decrypt_ok{crypt.Decrypt(ciphertext, decrypted)};
54 [ + + ]: 3439 : if (!correct_plaintext.empty()) {
55 [ + - + - : 6666 : BOOST_REQUIRE(decrypt_ok);
+ - ]
56 [ + - + - : 6666 : BOOST_CHECK_EQUAL_COLLECTIONS(decrypted.begin(), decrypted.end(), correct_plaintext.begin(), correct_plaintext.end());
+ - ]
57 : : }
58 : 3439 : }
59 : :
60 : 3333 : static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& plaintext,
61 : : const std::span<const unsigned char> correct_ciphertext = {})
62 : : {
63 : 3333 : std::vector<unsigned char> ciphertext;
64 [ + - + - : 6666 : BOOST_REQUIRE(crypt.Encrypt(plaintext, ciphertext));
+ - - + ]
65 : :
66 [ - + ]: 3333 : if (!correct_ciphertext.empty()) {
67 [ # # # # : 0 : BOOST_CHECK_EQUAL_COLLECTIONS(ciphertext.begin(), ciphertext.end(), correct_ciphertext.begin(), correct_ciphertext.end());
# # ]
68 : : }
69 : :
70 [ - + - + : 3333 : TestDecrypt(crypt, ciphertext, /*correct_plaintext=*/plaintext);
+ - ]
71 : 3333 : }
72 : :
73 : 101 : static void TestEncrypt(const CCrypter& crypt, const std::span<const unsigned char> plaintext,
74 : : const std::span<const unsigned char> correct_ciphertext = {})
75 : : {
76 [ + - ]: 101 : TestEncryptSingle(crypt, CKeyingMaterial{plaintext.begin(), plaintext.end()}, correct_ciphertext);
77 [ + + ]: 3333 : for (auto it{plaintext.begin()}; it != plaintext.end(); ++it) {
78 [ + - ]: 3232 : TestEncryptSingle(crypt, CKeyingMaterial{it, plaintext.end()});
79 : : }
80 : 101 : }
81 : :
82 : : };
83 : :
84 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(passphrase) {
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
85 : : // These are expensive.
86 : :
87 [ + - ]: 1 : TestCrypter::TestPassphrase("0000deadbeef0000"_hex_u8, "test", CMasterKey::DEFAULT_DERIVE_ITERATIONS,
88 : 1 : "fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"_hex_u8,
89 : 1 : "cf2f2691526dd1aa220896fb8bf7c369"_hex_u8);
90 : :
91 : 1 : std::string hash(GetRandHash().ToString());
92 [ + - ]: 1 : std::vector<unsigned char> vchSalt(8);
93 [ - + ]: 1 : GetRandBytes(vchSalt);
94 : 1 : uint32_t rounds = m_rng.rand32();
95 [ + - ]: 1 : if (rounds > 30000)
96 : 1 : rounds = 30000;
97 [ - + + - : 3 : TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()), rounds);
- + + - ]
98 : 1 : }
99 : :
100 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(passphrase_zero_rounds) {
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
101 : 1 : constexpr auto salt{"0000deadbeef0000"_hex_u8};
102 : 1 : CCrypter crypt;
103 [ + - + - : 2 : BOOST_CHECK(!crypt.SetKeyFromPassphrase("passphrase", salt, /*rounds=*/0, /*derivation_method=*/0));
+ - + - +
- ]
104 [ + - + - : 2 : BOOST_CHECK(crypt.SetKeyFromPassphrase("passphrase", salt, /*rounds=*/1, /*derivation_method=*/0));
+ - + - ]
105 : 1 : }
106 : :
107 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(encrypt) {
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
108 : 1 : constexpr std::array<uint8_t, WALLET_CRYPTO_SALT_SIZE> salt{"0000deadbeef0000"_hex_u8};
109 : 1 : CCrypter crypt;
110 [ + - + - : 2 : BOOST_REQUIRE(crypt.SetKeyFromPassphrase("passphrase", salt, CMasterKey::DEFAULT_DERIVE_ITERATIONS, /*derivation_method=*/0));
+ - + - +
- ]
111 [ + - ]: 1 : TestCrypter::TestEncrypt(crypt, "22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"_hex_u8);
112 : :
113 [ + + ]: 101 : for (int i = 0; i != 100; i++)
114 : : {
115 : 100 : uint256 hash(GetRandHash());
116 [ + - ]: 100 : TestCrypter::TestEncrypt(crypt, std::span<unsigned char>{hash.begin(), hash.end()});
117 : : }
118 : :
119 : 1 : }
120 : :
121 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(decrypt) {
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
122 : 1 : constexpr std::array<uint8_t, WALLET_CRYPTO_SALT_SIZE> salt{"0000deadbeef0000"_hex_u8};
123 : 1 : CCrypter crypt;
124 [ + - + - : 2 : BOOST_REQUIRE(crypt.SetKeyFromPassphrase("passphrase", salt, CMasterKey::DEFAULT_DERIVE_ITERATIONS, /*derivation_method=*/0));
+ - + - +
- ]
125 : :
126 : : // Some corner cases that came up while testing
127 [ + - ]: 1 : TestCrypter::TestDecrypt(crypt,"795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"_hex_u8);
128 [ + - ]: 1 : TestCrypter::TestDecrypt(crypt,"de096f4a8f9bd97db012aa9d90d74de8cdea779c3ee8bc7633d8b5d6da703486"_hex_u8);
129 [ + - ]: 1 : TestCrypter::TestDecrypt(crypt,"32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173de25947f98cf8b7ace49449c"_hex_u8);
130 [ + - ]: 1 : TestCrypter::TestDecrypt(crypt,"e7c055cca2faa78cb9ac22c9357a90b4778ded9b2cc220a14cea49f931e596ea"_hex_u8);
131 [ + - ]: 1 : TestCrypter::TestDecrypt(crypt,"b88efddd668a6801d19516d6830da4ae9811988ccbaf40df8fbb72f3f4d335fd"_hex_u8);
132 [ + - ]: 1 : TestCrypter::TestDecrypt(crypt,"8cae76aa6a43694e961ebcb28c8ca8f8540b84153d72865e8561ddd93fa7bfa9"_hex_u8);
133 : :
134 [ + + ]: 101 : for (int i = 0; i != 100; i++)
135 : : {
136 : 100 : uint256 hash(GetRandHash());
137 [ + - + - ]: 300 : TestCrypter::TestDecrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
138 : : }
139 : 1 : }
140 : :
141 : : BOOST_AUTO_TEST_SUITE_END()
142 : : } // namespace wallet
|