8#include <test/util/random.h>
9#include <test/util/setup_common.h>
11#include <boost/test/unit_test.hpp>
20 const std::vector<uint8_t> &vchSalt,
const SecureString &passphrase,
22 const std::vector<uint8_t> &correctKey = std::vector<uint8_t>(),
23 const std::vector<uint8_t> &correctIV = std::vector<uint8_t>()) {
27 if (!correctKey.empty()) {
28 BOOST_CHECK_MESSAGE(memcmp(crypt.
vchKey.data(), correctKey.data(),
33 if (!correctIV.empty()) {
34 BOOST_CHECK_MESSAGE(memcmp(crypt.
vchIV.data(), correctIV.data(),
35 crypt.
vchIV.size()) == 0,
42 const std::vector<uint8_t> &vchSalt,
const SecureString &passphrase,
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,
48 for (SecureString::const_iterator i(passphrase.begin());
49 i != passphrase.end(); ++i) {
50 TestPassphraseSingle(vchSalt,
SecureString(i, passphrase.end()),
56 const CCrypter &crypt,
const std::vector<uint8_t> &vchCiphertext,
57 const std::vector<uint8_t> &vchPlaintext = std::vector<uint8_t>()) {
59 crypt.
Decrypt(vchCiphertext, vchDecrypted);
60 if (vchPlaintext.size()) {
62 vchPlaintext.end()) == vchDecrypted);
69 const std::vector<uint8_t> &vchCiphertextCorrect =
70 std::vector<uint8_t>()) {
71 std::vector<uint8_t> vchCiphertext;
72 crypt.
Encrypt(vchPlaintext, vchCiphertext);
74 if (!vchCiphertextCorrect.empty()) {
78 const std::vector<uint8_t> vchPlaintext2(vchPlaintext.begin(),
80 TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
84 const std::vector<uint8_t> &vchPlaintextIn,
85 const std::vector<uint8_t> &vchCiphertextCorrect =
86 std::vector<uint8_t>()) {
90 vchCiphertextCorrect);
91 for (std::vector<uint8_t>::const_iterator i(vchPlaintextIn.begin());
92 i != vchPlaintextIn.end(); ++i) {
102 ParseHex(
"0000deadbeef0000"),
"test", 25000,
104 "fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"),
105 ParseHex(
"cf2f2691526dd1aa220896fb8bf7c369"));
108 std::vector<uint8_t> vchSalt(8);
110 uint32_t rounds = InsecureRand32();
111 if (rounds > 30000) {
119 std::vector<uint8_t> vchSalt =
ParseHex(
"0000deadbeef0000");
124 ParseHex(
"22bcade09ac03ff6386914359cfe885cfeb5f77f"
125 "f0d670f102f619687453b29d"));
127 for (
int i = 0; i != 100; i++) {
130 crypt, std::vector<uint8_t>(hash.
begin(), hash.
end()));
135 std::vector<uint8_t> vchSalt =
ParseHex(
"0000deadbeef0000");
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"));
160 for (
int i = 0; i != 100; i++) {
163 crypt, std::vector<uint8_t>(hash.
begin(), hash.
end()));
167BOOST_AUTO_TEST_SUITE_END()
Encryption/decryption context with key information.
std::vector< uint8_t, secure_allocator< uint8_t > > vchKey
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< uint8_t > &vchCiphertext) const
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< uint8_t > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
std::vector< uint8_t, secure_allocator< uint8_t > > vchIV
bool Decrypt(const std::vector< uint8_t > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
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 >())
const unsigned int WALLET_CRYPTO_SALT_SIZE
std::vector< uint8_t, secure_allocator< uint8_t > > CKeyingMaterial
#define BOOST_CHECK(expr)
void GetRandBytes(Span< uint8_t > bytes) noexcept
Overall design of the RNG and entropy sources.
uint256 GetRandHash() noexcept
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
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)