Bitcoin Core  22.99.0
P2P Digital Currency
wallet_crypto_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2020 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 
6 #include <util/strencodings.h>
7 #include <wallet/crypter.h>
8 
9 #include <vector>
10 
11 #include <boost/test/unit_test.hpp>
12 
13 namespace wallet {
14 BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup)
15 
17 {
18 public:
19 static void TestPassphraseSingle(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
20  const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
21  const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
22 {
23  CCrypter crypt;
24  crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
25 
26  if(!correctKey.empty())
27  BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, \
28  HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correctKey));
29  if(!correctIV.empty())
30  BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0,
31  HexStr(crypt.vchIV) + std::string(" != ") + HexStr(correctIV));
32 }
33 
34 static void TestPassphrase(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
35  const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
36  const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
37 {
38  TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV);
39  for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
40  TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds);
41 }
42 
43 static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext, \
44  const std::vector<unsigned char>& vchPlaintext = std::vector<unsigned char>())
45 {
46  CKeyingMaterial vchDecrypted;
47  crypt.Decrypt(vchCiphertext, vchDecrypted);
48  if (vchPlaintext.size())
49  BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted);
50 }
51 
52 static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext,
53  const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
54 {
55  std::vector<unsigned char> vchCiphertext;
56  crypt.Encrypt(vchPlaintext, vchCiphertext);
57 
58  if (!vchCiphertextCorrect.empty())
59  BOOST_CHECK(vchCiphertext == vchCiphertextCorrect);
60 
61  const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
62  TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
63 }
64 
65 static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn, \
66  const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
67 {
68  TestEncryptSingle(crypt, CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()), vchCiphertextCorrect);
69  for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
70  TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
71 }
72 
73 };
74 
75 BOOST_AUTO_TEST_CASE(passphrase) {
76  // These are expensive.
77 
78  TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000, \
79  ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \
80  ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
81 
82  std::string hash(GetRandHash().ToString());
83  std::vector<unsigned char> vchSalt(8);
84  GetRandBytes(vchSalt.data(), vchSalt.size());
85  uint32_t rounds = InsecureRand32();
86  if (rounds > 30000)
87  rounds = 30000;
88  TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()), rounds);
89 }
90 
92  std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
93  BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
94  CCrypter crypt;
95  crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
96  TestCrypter::TestEncrypt(crypt, ParseHex("22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"));
97 
98  for (int i = 0; i != 100; i++)
99  {
100  uint256 hash(GetRandHash());
101  TestCrypter::TestEncrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
102  }
103 
104 }
105 
107  std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
108  BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
109  CCrypter crypt;
110  crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
111 
112  // Some corner cases the came up while testing
113  TestCrypter::TestDecrypt(crypt,ParseHex("795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"));
114  TestCrypter::TestDecrypt(crypt,ParseHex("de096f4a8f9bd97db012aa9d90d74de8cdea779c3ee8bc7633d8b5d6da703486"));
115  TestCrypter::TestDecrypt(crypt,ParseHex("32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173de25947f98cf8b7ace49449c"));
116  TestCrypter::TestDecrypt(crypt,ParseHex("e7c055cca2faa78cb9ac22c9357a90b4778ded9b2cc220a14cea49f931e596ea"));
117  TestCrypter::TestDecrypt(crypt,ParseHex("b88efddd668a6801d19516d6830da4ae9811988ccbaf40df8fbb72f3f4d335fd"));
118  TestCrypter::TestDecrypt(crypt,ParseHex("8cae76aa6a43694e961ebcb28c8ca8f8540b84153d72865e8561ddd93fa7bfa9"));
119 
120  for (int i = 0; i != 100; i++)
121  {
122  uint256 hash(GetRandHash());
123  TestCrypter::TestDecrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
124  }
125 }
126 
128 } // namespace wallet
crypter.h
wallet::CCrypter::SetKeyFromPassphrase
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:40
ParseHex
std::vector< unsigned char > ParseHex(const char *psz)
Definition: strencodings.cpp:84
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
wallet::WALLET_CRYPTO_SALT_SIZE
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:15
setup_common.h
wallet::CCrypter::Encrypt
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
Definition: crypter.cpp:72
wallet::TestCrypter::TestPassphraseSingle
static void TestPassphraseSingle(const std::vector< unsigned char > &vchSalt, const SecureString &passphrase, uint32_t rounds, const std::vector< unsigned char > &correctKey=std::vector< unsigned char >(), const std::vector< unsigned char > &correctIV=std::vector< unsigned char >())
Definition: wallet_crypto_tests.cpp:19
wallet::CCrypter::vchKey
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
Definition: crypter.h:74
InsecureRand32
static uint32_t InsecureRand32()
Definition: setup_common.h:72
wallet
Definition: node.h:38
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
wallet::CKeyingMaterial
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:62
strencodings.h
wallet::CCrypter
Encryption/decryption context with key information.
Definition: crypter.h:70
SecureString
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:59
wallet::TestCrypter::TestPassphrase
static void TestPassphrase(const std::vector< unsigned char > &vchSalt, const SecureString &passphrase, uint32_t rounds, const std::vector< unsigned char > &correctKey=std::vector< unsigned char >(), const std::vector< unsigned char > &correctIV=std::vector< unsigned char >())
Definition: wallet_crypto_tests.cpp:34
wallet::CCrypter::Decrypt
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
Definition: crypter.cpp:90
wallet::TestCrypter::TestEncryptSingle
static void TestEncryptSingle(const CCrypter &crypt, const CKeyingMaterial &vchPlaintext, const std::vector< unsigned char > &vchCiphertextCorrect=std::vector< unsigned char >())
Definition: wallet_crypto_tests.cpp:52
GetRandHash
uint256 GetRandHash() noexcept
Definition: random.cpp:598
GetRandBytes
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
Definition: random.cpp:581
wallet::TestCrypter::TestEncrypt
static void TestEncrypt(const CCrypter &crypt, const std::vector< unsigned char > &vchPlaintextIn, const std::vector< unsigned char > &vchCiphertextCorrect=std::vector< unsigned char >())
Definition: wallet_crypto_tests.cpp:65
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:83
uint256
256-bit opaque blob.
Definition: uint256.h:126
wallet::BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(bnb_search_test)
Definition: coinselector_tests.cpp:177
wallet::TestCrypter
Definition: wallet_crypto_tests.cpp:16
wallet::CCrypter::vchIV
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
Definition: crypter.h:75
wallet::TestCrypter::TestDecrypt
static void TestDecrypt(const CCrypter &crypt, const std::vector< unsigned char > &vchCiphertext, const std::vector< unsigned char > &vchPlaintext=std::vector< unsigned char >())
Definition: wallet_crypto_tests.cpp:43
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:511
base_blob::begin
unsigned char * begin()
Definition: uint256.h:60
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
base_blob::end
unsigned char * end()
Definition: uint256.h:65