Branch data Line data Source code
1 : : // Copyright (c) 2014-2022 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 : : namespace wallet {
15 : : BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup)
16 : :
17 : : class TestCrypter
18 : : {
19 : : public:
20 : 70 : static void TestPassphraseSingle(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
21 : : const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
22 : : const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
23 : : {
24 : 70 : CCrypter crypt;
25 [ + - ]: 70 : crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
26 : :
27 [ + + ]: 70 : if(!correctKey.empty())
28 [ + - + - : 2 : BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, \
+ - + - +
- + - +
- ]
29 : : HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correctKey));
30 [ + + ]: 70 : if(!correctIV.empty())
31 [ + - + - : 2 : BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0,
+ - + - +
- + - +
- ]
32 : : HexStr(crypt.vchIV) + std::string(" != ") + HexStr(correctIV));
33 : 70 : }
34 : :
35 : 2 : static void TestPassphrase(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
36 : : const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
37 : : const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
38 : : {
39 : 2 : TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV);
40 [ + + ]: 70 : for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
41 [ + - + - ]: 204 : TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds);
42 : 2 : }
43 : :
44 : 3439 : static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext, \
45 : : const std::vector<unsigned char>& vchPlaintext = std::vector<unsigned char>())
46 : : {
47 : 3439 : CKeyingMaterial vchDecrypted;
48 [ + - ]: 3439 : crypt.Decrypt(vchCiphertext, vchDecrypted);
49 [ + + ]: 3439 : if (vchPlaintext.size())
50 [ + - + - : 6666 : BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted);
+ - ]
51 : 3439 : }
52 : :
53 : 3333 : static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext,
54 : : const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
55 : : {
56 : 3333 : std::vector<unsigned char> vchCiphertext;
57 [ + - ]: 3333 : crypt.Encrypt(vchPlaintext, vchCiphertext);
58 : :
59 [ - + ]: 3333 : if (!vchCiphertextCorrect.empty())
60 [ # # # # ]: 0 : BOOST_CHECK(vchCiphertext == vchCiphertextCorrect);
61 : :
62 [ + - ]: 3333 : const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
63 [ + - ]: 3333 : TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
64 : 3333 : }
65 : :
66 : 101 : static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn, \
67 : : const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
68 : : {
69 [ + - ]: 101 : TestEncryptSingle(crypt, CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()), vchCiphertextCorrect);
70 [ + + ]: 3333 : for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
71 [ + - + - ]: 3232 : TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
72 : 101 : }
73 : :
74 : : };
75 : :
76 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(passphrase) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
77 : : // These are expensive.
78 : :
79 [ + - + - : 3 : TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000, \
+ - ]
80 [ + - ]: 2 : ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \
81 [ + - ]: 1 : ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
82 : :
83 : 1 : std::string hash(GetRandHash().ToString());
84 [ + - ]: 1 : std::vector<unsigned char> vchSalt(8);
85 : 1 : GetRandBytes(vchSalt);
86 : 1 : uint32_t rounds = InsecureRand32();
87 [ + - ]: 1 : if (rounds > 30000)
88 : 1 : rounds = 30000;
89 [ + - + - ]: 3 : TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()), rounds);
90 : 1 : }
91 : :
92 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(encrypt) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
93 : 1 : std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
94 [ + - + - : 2 : BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
+ - ]
95 [ + - ]: 1 : CCrypter crypt;
96 [ + - + - ]: 1 : crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
97 [ + - + - ]: 2 : TestCrypter::TestEncrypt(crypt, ParseHex("22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"));
98 : :
99 [ + + ]: 101 : for (int i = 0; i != 100; i++)
100 : : {
101 : 100 : uint256 hash(GetRandHash());
102 [ + - + - ]: 200 : TestCrypter::TestEncrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
103 : : }
104 : :
105 : 1 : }
106 : :
107 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(decrypt) {
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
108 : 1 : std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
109 [ + - + - : 2 : BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
+ - ]
110 [ + - ]: 1 : CCrypter crypt;
111 [ + - + - ]: 1 : crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
112 : :
113 : : // Some corner cases the came up while testing
114 [ + - + - ]: 2 : TestCrypter::TestDecrypt(crypt,ParseHex("795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"));
115 [ + - + - ]: 2 : TestCrypter::TestDecrypt(crypt,ParseHex("de096f4a8f9bd97db012aa9d90d74de8cdea779c3ee8bc7633d8b5d6da703486"));
116 [ + - + - ]: 2 : TestCrypter::TestDecrypt(crypt,ParseHex("32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173de25947f98cf8b7ace49449c"));
117 [ + - + - ]: 2 : TestCrypter::TestDecrypt(crypt,ParseHex("e7c055cca2faa78cb9ac22c9357a90b4778ded9b2cc220a14cea49f931e596ea"));
118 [ + - + - ]: 2 : TestCrypter::TestDecrypt(crypt,ParseHex("b88efddd668a6801d19516d6830da4ae9811988ccbaf40df8fbb72f3f4d335fd"));
119 [ + - + - ]: 2 : TestCrypter::TestDecrypt(crypt,ParseHex("8cae76aa6a43694e961ebcb28c8ca8f8540b84153d72865e8561ddd93fa7bfa9"));
120 : :
121 [ + + ]: 101 : for (int i = 0; i != 100; i++)
122 : : {
123 : 100 : uint256 hash(GetRandHash());
124 [ + - + - ]: 200 : TestCrypter::TestDecrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
125 : : }
126 : 1 : }
127 : :
128 : : BOOST_AUTO_TEST_SUITE_END()
129 : : } // namespace wallet
|