Bitcoin ABC  0.26.3
P2P Digital Currency
wallet_crypto_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2016 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 <util/strencodings.h>
6 #include <wallet/crypter.h>
7 
8 #include <test/util/setup_common.h>
9 
10 #include <boost/test/unit_test.hpp>
11 
12 #include <vector>
13 
15 
16 class TestCrypter {
17 public:
18  static void TestPassphraseSingle(
19  const std::vector<uint8_t> &vchSalt, const SecureString &passphrase,
20  uint32_t rounds,
21  const std::vector<uint8_t> &correctKey = std::vector<uint8_t>(),
22  const std::vector<uint8_t> &correctIV = std::vector<uint8_t>()) {
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(),
28  crypt.vchKey.size()) == 0,
29  HexStr(crypt.vchKey) + std::string(" != ") +
30  HexStr(correctKey));
31  }
32  if (!correctIV.empty()) {
33  BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(),
34  crypt.vchIV.size()) == 0,
35  HexStr(crypt.vchIV) + std::string(" != ") +
36  HexStr(correctIV));
37  }
38  }
39 
40  static void TestPassphrase(
41  const std::vector<uint8_t> &vchSalt, const SecureString &passphrase,
42  uint32_t rounds,
43  const std::vector<uint8_t> &correctKey = std::vector<uint8_t>(),
44  const std::vector<uint8_t> &correctIV = std::vector<uint8_t>()) {
45  TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey,
46  correctIV);
47  for (SecureString::const_iterator i(passphrase.begin());
48  i != passphrase.end(); ++i) {
49  TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()),
50  rounds);
51  }
52  }
53 
54  static void TestDecrypt(
55  const CCrypter &crypt, const std::vector<uint8_t> &vchCiphertext,
56  const std::vector<uint8_t> &vchPlaintext = std::vector<uint8_t>()) {
57  CKeyingMaterial vchDecrypted;
58  crypt.Decrypt(vchCiphertext, vchDecrypted);
59  if (vchPlaintext.size()) {
60  BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(),
61  vchPlaintext.end()) == vchDecrypted);
62  }
63  }
64 
65  static void
67  const CKeyingMaterial &vchPlaintext,
68  const std::vector<uint8_t> &vchCiphertextCorrect =
69  std::vector<uint8_t>()) {
70  std::vector<uint8_t> vchCiphertext;
71  crypt.Encrypt(vchPlaintext, vchCiphertext);
72 
73  if (!vchCiphertextCorrect.empty()) {
74  BOOST_CHECK(vchCiphertext == vchCiphertextCorrect);
75  }
76 
77  const std::vector<uint8_t> vchPlaintext2(vchPlaintext.begin(),
78  vchPlaintext.end());
79  TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
80  }
81 
82  static void TestEncrypt(const CCrypter &crypt,
83  const std::vector<uint8_t> &vchPlaintextIn,
84  const std::vector<uint8_t> &vchCiphertextCorrect =
85  std::vector<uint8_t>()) {
86  TestEncryptSingle(
87  crypt,
88  CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()),
89  vchCiphertextCorrect);
90  for (std::vector<uint8_t>::const_iterator i(vchPlaintextIn.begin());
91  i != vchPlaintextIn.end(); ++i) {
92  TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
93  }
94  }
95 };
96 
97 BOOST_AUTO_TEST_CASE(passphrase) {
98  // These are expensive.
99 
101  ParseHex("0000deadbeef0000"), "test", 25000,
102  ParseHex(
103  "fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"),
104  ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
105 
106  std::string hash(GetRandHash().ToString());
107  std::vector<uint8_t> vchSalt(8);
108  GetRandBytes(vchSalt);
109  uint32_t rounds = InsecureRand32();
110  if (rounds > 30000) {
111  rounds = 30000;
112  }
113  TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()),
114  rounds);
115 }
116 
118  std::vector<uint8_t> vchSalt = ParseHex("0000deadbeef0000");
119  BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
120  CCrypter crypt;
121  crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
123  ParseHex("22bcade09ac03ff6386914359cfe885cfeb5f77f"
124  "f0d670f102f619687453b29d"));
125 
126  for (int i = 0; i != 100; i++) {
127  uint256 hash(GetRandHash());
129  crypt, std::vector<uint8_t>(hash.begin(), hash.end()));
130  }
131 }
132 
134  std::vector<uint8_t> vchSalt = ParseHex("0000deadbeef0000");
135  BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
136  CCrypter crypt;
137  crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
138 
139  // Some corner cases the came up while testing
141  ParseHex("795643ce39d736088367822cdc50535ec6f10371"
142  "5e3e48f4f3b1a60a08ef59ca"));
144  ParseHex("de096f4a8f9bd97db012aa9d90d74de8cdea779c"
145  "3ee8bc7633d8b5d6da703486"));
147  ParseHex("32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173"
148  "de25947f98cf8b7ace49449c"));
150  ParseHex("e7c055cca2faa78cb9ac22c9357a90b4778ded9b"
151  "2cc220a14cea49f931e596ea"));
153  ParseHex("b88efddd668a6801d19516d6830da4ae9811988c"
154  "cbaf40df8fbb72f3f4d335fd"));
156  ParseHex("8cae76aa6a43694e961ebcb28c8ca8f8540b8415"
157  "3d72865e8561ddd93fa7bfa9"));
158 
159  for (int i = 0; i != 100; i++) {
160  uint256 hash(GetRandHash());
162  crypt, std::vector<uint8_t>(hash.begin(), hash.end()));
163  }
164 }
165 
Encryption/decryption context with key information.
Definition: crypter.h:64
std::vector< uint8_t, secure_allocator< uint8_t > > vchKey
Definition: crypter.h:69
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< uint8_t > &vchCiphertext) const
Definition: crypter.cpp:79
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< uint8_t > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:41
std::vector< uint8_t, secure_allocator< uint8_t > > vchIV
Definition: crypter.h:70
bool Decrypt(const std::vector< uint8_t > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
Definition: crypter.cpp:100
static void TestPassphrase(const std::vector< uint8_t > &vchSalt, const SecureString &passphrase, uint32_t rounds, const std::vector< uint8_t > &correctKey=std::vector< uint8_t >(), const std::vector< uint8_t > &correctIV=std::vector< uint8_t >())
static void TestEncryptSingle(const CCrypter &crypt, const CKeyingMaterial &vchPlaintext, const std::vector< uint8_t > &vchCiphertextCorrect=std::vector< uint8_t >())
static void TestPassphraseSingle(const std::vector< uint8_t > &vchSalt, const SecureString &passphrase, uint32_t rounds, const std::vector< uint8_t > &correctKey=std::vector< uint8_t >(), const std::vector< uint8_t > &correctIV=std::vector< uint8_t >())
static void TestEncrypt(const CCrypter &crypt, const std::vector< uint8_t > &vchPlaintextIn, const std::vector< uint8_t > &vchCiphertextCorrect=std::vector< uint8_t >())
static void TestDecrypt(const CCrypter &crypt, const std::vector< uint8_t > &vchCiphertext, const std::vector< uint8_t > &vchPlaintext=std::vector< uint8_t >())
uint8_t * end()
Definition: uint256.h:87
uint8_t * begin()
Definition: uint256.h:85
256-bit opaque blob.
Definition: uint256.h:129
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:13
std::vector< uint8_t, secure_allocator< uint8_t > > CKeyingMaterial
Definition: crypter.h:57
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
#define BOOST_CHECK(expr)
Definition: object.cpp:17
void GetRandBytes(Span< uint8_t > bytes) noexcept
Overall design of the RNG and entropy sources.
Definition: random.cpp:639
uint256 GetRandHash() noexcept
Definition: random.cpp:659
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:55
BOOST_FIXTURE_TEST_SUITE(stakingrewards_tests, StakingRewardsActivationTestingSetup) BOOST_AUTO_TEST_CASE(isstakingrewardsactivated)
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::vector< uint8_t > ParseHex(const char *psz)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
BOOST_AUTO_TEST_CASE(passphrase)