Bitcoin ABC 0.30.5
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/random.h>
9#include <test/util/setup_common.h>
10
11#include <boost/test/unit_test.hpp>
12
13#include <vector>
14
15BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup)
16
18public:
20 const std::vector<uint8_t> &vchSalt, const SecureString &passphrase,
21 uint32_t rounds,
22 const std::vector<uint8_t> &correctKey = std::vector<uint8_t>(),
23 const std::vector<uint8_t> &correctIV = std::vector<uint8_t>()) {
24 CCrypter crypt;
25 crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
26
27 if (!correctKey.empty()) {
28 BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(),
29 crypt.vchKey.size()) == 0,
30 HexStr(crypt.vchKey) + std::string(" != ") +
31 HexStr(correctKey));
32 }
33 if (!correctIV.empty()) {
34 BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(),
35 crypt.vchIV.size()) == 0,
36 HexStr(crypt.vchIV) + std::string(" != ") +
37 HexStr(correctIV));
38 }
39 }
40
41 static void TestPassphrase(
42 const std::vector<uint8_t> &vchSalt, const SecureString &passphrase,
43 uint32_t rounds,
44 const std::vector<uint8_t> &correctKey = std::vector<uint8_t>(),
45 const std::vector<uint8_t> &correctIV = std::vector<uint8_t>()) {
46 TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey,
47 correctIV);
48 for (SecureString::const_iterator i(passphrase.begin());
49 i != passphrase.end(); ++i) {
50 TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()),
51 rounds);
52 }
53 }
54
55 static void TestDecrypt(
56 const CCrypter &crypt, const std::vector<uint8_t> &vchCiphertext,
57 const std::vector<uint8_t> &vchPlaintext = std::vector<uint8_t>()) {
58 CKeyingMaterial vchDecrypted;
59 crypt.Decrypt(vchCiphertext, vchDecrypted);
60 if (vchPlaintext.size()) {
61 BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(),
62 vchPlaintext.end()) == vchDecrypted);
63 }
64 }
65
66 static void
68 const CKeyingMaterial &vchPlaintext,
69 const std::vector<uint8_t> &vchCiphertextCorrect =
70 std::vector<uint8_t>()) {
71 std::vector<uint8_t> vchCiphertext;
72 crypt.Encrypt(vchPlaintext, vchCiphertext);
73
74 if (!vchCiphertextCorrect.empty()) {
75 BOOST_CHECK(vchCiphertext == vchCiphertextCorrect);
76 }
77
78 const std::vector<uint8_t> vchPlaintext2(vchPlaintext.begin(),
79 vchPlaintext.end());
80 TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
81 }
82
83 static void TestEncrypt(const CCrypter &crypt,
84 const std::vector<uint8_t> &vchPlaintextIn,
85 const std::vector<uint8_t> &vchCiphertextCorrect =
86 std::vector<uint8_t>()) {
87 TestEncryptSingle(
88 crypt,
89 CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()),
90 vchCiphertextCorrect);
91 for (std::vector<uint8_t>::const_iterator i(vchPlaintextIn.begin());
92 i != vchPlaintextIn.end(); ++i) {
93 TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
94 }
95 }
96};
97
99 // These are expensive.
100
102 ParseHex("0000deadbeef0000"), "test", 25000,
103 ParseHex(
104 "fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"),
105 ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
106
107 std::string hash(GetRandHash().ToString());
108 std::vector<uint8_t> vchSalt(8);
109 GetRandBytes(vchSalt);
110 uint32_t rounds = InsecureRand32();
111 if (rounds > 30000) {
112 rounds = 30000;
113 }
114 TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()),
115 rounds);
116}
117
119 std::vector<uint8_t> vchSalt = ParseHex("0000deadbeef0000");
120 BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
121 CCrypter crypt;
122 crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
124 ParseHex("22bcade09ac03ff6386914359cfe885cfeb5f77f"
125 "f0d670f102f619687453b29d"));
126
127 for (int i = 0; i != 100; i++) {
128 uint256 hash(GetRandHash());
130 crypt, std::vector<uint8_t>(hash.begin(), hash.end()));
131 }
132}
133
135 std::vector<uint8_t> vchSalt = ParseHex("0000deadbeef0000");
136 BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
137 CCrypter crypt;
138 crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
139
140 // Some corner cases the came up while testing
142 ParseHex("795643ce39d736088367822cdc50535ec6f10371"
143 "5e3e48f4f3b1a60a08ef59ca"));
145 ParseHex("de096f4a8f9bd97db012aa9d90d74de8cdea779c"
146 "3ee8bc7633d8b5d6da703486"));
148 ParseHex("32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173"
149 "de25947f98cf8b7ace49449c"));
151 ParseHex("e7c055cca2faa78cb9ac22c9357a90b4778ded9b"
152 "2cc220a14cea49f931e596ea"));
154 ParseHex("b88efddd668a6801d19516d6830da4ae9811988c"
155 "cbaf40df8fbb72f3f4d335fd"));
157 ParseHex("8cae76aa6a43694e961ebcb28c8ca8f8540b8415"
158 "3d72865e8561ddd93fa7bfa9"));
159
160 for (int i = 0; i != 100; i++) {
161 uint256 hash(GetRandHash());
163 crypt, std::vector<uint8_t>(hash.begin(), hash.end()));
164 }
165}
166
167BOOST_AUTO_TEST_SUITE_END()
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_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
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:100
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
template std::vector< std::byte > ParseHex(std::string_view)
BOOST_AUTO_TEST_CASE(passphrase)