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