12#include <boost/test/unit_test.hpp>
23 const std::span<const unsigned char> correct_key = {},
24 const std::span<const unsigned char> correct_iv = {})
27 crypt.SetKeyFromPassphrase(passphrase, salt, rounds, 0);
29 if (!correct_key.empty()) {
30 BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correct_key.data(), crypt.vchKey.size()) == 0,
31 HexStr(crypt.vchKey) + std::string(
" != ") +
HexStr(correct_key));
33 if (!correct_iv.empty()) {
34 BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correct_iv.data(), crypt.vchIV.size()) == 0,
35 HexStr(crypt.vchIV) + std::string(
" != ") +
HexStr(correct_iv));
40 const std::span<const unsigned char> correct_key = {},
41 const std::span<const unsigned char> correct_iv = {})
43 TestPassphraseSingle(salt, passphrase, rounds, correct_key, correct_iv);
44 for (SecureString::const_iterator it{passphrase.begin()}; it != passphrase.end(); ++it) {
45 TestPassphraseSingle(salt,
SecureString{it, passphrase.end()}, rounds);
50 const std::span<const unsigned char> correct_plaintext = {})
53 crypt.
Decrypt(ciphertext, decrypted);
54 if (!correct_plaintext.empty()) {
55 BOOST_CHECK_EQUAL_COLLECTIONS(decrypted.begin(), decrypted.end(), correct_plaintext.begin(), correct_plaintext.end());
60 const std::span<const unsigned char> correct_ciphertext = {})
62 std::vector<unsigned char> ciphertext;
63 crypt.
Encrypt(plaintext, ciphertext);
65 if (!correct_ciphertext.empty()) {
66 BOOST_CHECK_EQUAL_COLLECTIONS(ciphertext.begin(), ciphertext.end(), correct_ciphertext.begin(), correct_ciphertext.end());
69 TestDecrypt(crypt, ciphertext, plaintext);
73 const std::span<const unsigned char> correct_ciphertext = {})
75 TestEncryptSingle(crypt,
CKeyingMaterial{plaintext.begin(), plaintext.end()}, correct_ciphertext);
76 for (
auto it{plaintext.begin()}; it != plaintext.end(); ++it) {
87 "fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"_hex_u8,
88 "cf2f2691526dd1aa220896fb8bf7c369"_hex_u8);
91 std::vector<unsigned char> vchSalt(8);
93 uint32_t rounds = m_rng.rand32();
100 constexpr std::array<uint8_t, WALLET_CRYPTO_SALT_SIZE> salt{
"0000deadbeef0000"_hex_u8};
105 for (
int i = 0; i != 100; i++)
114 constexpr std::array<uint8_t, WALLET_CRYPTO_SALT_SIZE> salt{
"0000deadbeef0000"_hex_u8};
126 for (
int i = 0; i != 100; i++)
constexpr unsigned char * end()
constexpr unsigned char * begin()
Encryption/decryption context with key information.
bool Decrypt(std::span< const unsigned char > ciphertext, CKeyingMaterial &plaintext) const
bool SetKeyFromPassphrase(const SecureString &key_data, std::span< const unsigned char > salt, const unsigned int rounds, const unsigned int derivation_method)
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
static void TestDecrypt(const CCrypter &crypt, const std::span< const unsigned char > ciphertext, const std::span< const unsigned char > correct_plaintext={})
static void TestPassphraseSingle(const std::span< const unsigned char > salt, const SecureString &passphrase, uint32_t rounds, const std::span< const unsigned char > correct_key={}, const std::span< const unsigned char > correct_iv={})
static void TestEncrypt(const CCrypter &crypt, const std::span< const unsigned char > plaintext, const std::span< const unsigned char > correct_ciphertext={})
static void TestEncryptSingle(const CCrypter &crypt, const CKeyingMaterial &plaintext, const std::span< const unsigned char > correct_ciphertext={})
static void TestPassphrase(const std::span< const unsigned char > salt, const SecureString &passphrase, uint32_t rounds, const std::span< const unsigned char > correct_key={}, const std::span< const unsigned char > correct_iv={})
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
std::string ToString(const T &t)
Locale-independent version of std::to_string.
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
BOOST_AUTO_TEST_CASE(bnb_search_test)
void GetRandBytes(Span< unsigned char > bytes) noexcept
Generate random data via the internal PRNG.
uint256 GetRandHash() noexcept
Generate a random uint256.
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString