LCOV - code coverage report
Current view: top level - src/wallet/test - wallet_crypto_tests.cpp (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 96 97 99.0 %
Date: 2026-06-25 07:23:51 Functions: 37 37 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2014-2020 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 <test/util/random.h>
       6             : #include <test/util/setup_common.h>
       7             : #include <util/strencodings.h>
       8             : #include <wallet/crypter.h>
       9             : 
      10             : #include <vector>
      11             : 
      12             : #include <boost/test/unit_test.hpp>
      13             : 
      14             : namespace wallet {
      15         146 : BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup)
      16             : 
      17           4 : void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, const std::string &hexin, const std::string &hexout)
      18             : {
      19           4 :     std::vector<unsigned char> key = ParseHex(hexkey);
      20           4 :     std::vector<unsigned char> iv = ParseHex(hexiv);
      21           4 :     std::vector<unsigned char> in = ParseHex(hexin);
      22           4 :     std::vector<unsigned char> correctout = ParseHex(hexout);
      23             : 
      24           4 :     SecureString sKey(key.begin(), key.end()), sPlaintextIn(in.begin(), in.end()), sPlaintextOut, sPlaintextOutOld;
      25           4 :     std::string sIv(iv.begin(), iv.end()), sCiphertextIn(correctout.begin(), correctout.end()), sCiphertextOut, sCiphertextOutOld;
      26           4 :     BOOST_CHECK_MESSAGE(EncryptAES256(sKey, sPlaintextIn, sIv, sCiphertextOut), "EncryptAES256: " + HexStr(sCiphertextOut) + std::string(" != ") + hexout);
      27           4 :     BOOST_CHECK_MESSAGE(DecryptAES256(sKey, sCiphertextIn, sIv, sPlaintextOut), "DecryptAES256: " + HexStr(sPlaintextOut) + std::string(" != ") + hexin);
      28           4 : }
      29             : 
      30             : class TestCrypter
      31             : {
      32             : public:
      33          70 : static void TestPassphraseSingle(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
      34             :                  const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
      35             :                  const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
      36             : {
      37          70 :     CCrypter crypt;
      38          70 :     crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
      39             : 
      40          70 :     if(!correctKey.empty())
      41           1 :         BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, \
      42             :             HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correctKey));
      43          70 :     if(!correctIV.empty())
      44           1 :         BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0,
      45             :             HexStr(crypt.vchIV) + std::string(" != ") + HexStr(correctIV));
      46          70 : }
      47             : 
      48           2 : static void TestPassphrase(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
      49             :                  const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
      50             :                  const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
      51             : {
      52           2 :     TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV);
      53          70 :     for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
      54          68 :         TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds);
      55           2 : }
      56             : 
      57        3439 : static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext, \
      58             :                         const std::vector<unsigned char>& vchPlaintext = std::vector<unsigned char>())
      59             : {
      60        3439 :     CKeyingMaterial vchDecrypted;
      61        3439 :     crypt.Decrypt(vchCiphertext, vchDecrypted);
      62        3439 :     if (vchPlaintext.size())
      63        3333 :         BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted);
      64        3439 : }
      65             : 
      66        3333 : static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext,
      67             :                        const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
      68             : {
      69        3333 :     std::vector<unsigned char> vchCiphertext;
      70        3333 :     crypt.Encrypt(vchPlaintext, vchCiphertext);
      71             : 
      72        3333 :     if (!vchCiphertextCorrect.empty())
      73           0 :         BOOST_CHECK(vchCiphertext == vchCiphertextCorrect);
      74             : 
      75        3333 :     const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
      76        3333 :     TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
      77        3333 : }
      78             : 
      79         101 : static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn, \
      80             :                        const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
      81             : {
      82         101 :     TestEncryptSingle(crypt, CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()), vchCiphertextCorrect);
      83        3333 :     for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
      84        3232 :         TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
      85         101 : }
      86             : 
      87             : };
      88             : 
      89         149 : BOOST_AUTO_TEST_CASE(passphrase) {
      90             :     // These are expensive.
      91             : 
      92           2 :     TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000, \
      93           1 :                                 ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \
      94           1 :                                 ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
      95             : 
      96           1 :     std::string hash(GetRandHash().ToString());
      97           1 :     std::vector<unsigned char> vchSalt(8);
      98           1 :     GetRandBytes(vchSalt);
      99           1 :     uint32_t rounds = InsecureRand32();
     100           1 :     if (rounds > 30000)
     101           1 :         rounds = 30000;
     102           1 :     TestCrypter::TestPassphrase(vchSalt, SecureString(hash.begin(), hash.end()), rounds);
     103           1 : }
     104             : 
     105         149 : BOOST_AUTO_TEST_CASE(encrypt) {
     106           1 :     std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
     107           1 :     BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
     108           1 :     CCrypter crypt;
     109           1 :     crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
     110           1 :     TestCrypter::TestEncrypt(crypt, ParseHex("22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"));
     111             : 
     112         101 :     for (int i = 0; i != 100; i++)
     113             :     {
     114         100 :         uint256 hash(GetRandHash());
     115         100 :         TestCrypter::TestEncrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
     116         100 :     }
     117             : 
     118           1 : }
     119             : 
     120         149 : BOOST_AUTO_TEST_CASE(decrypt) {
     121           1 :     std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
     122           1 :     BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
     123           1 :     CCrypter crypt;
     124           1 :     crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
     125             : 
     126             :     // Some corner cases the came up while testing
     127           1 :     TestCrypter::TestDecrypt(crypt,ParseHex("795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"));
     128           1 :     TestCrypter::TestDecrypt(crypt,ParseHex("de096f4a8f9bd97db012aa9d90d74de8cdea779c3ee8bc7633d8b5d6da703486"));
     129           1 :     TestCrypter::TestDecrypt(crypt,ParseHex("32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173de25947f98cf8b7ace49449c"));
     130           1 :     TestCrypter::TestDecrypt(crypt,ParseHex("e7c055cca2faa78cb9ac22c9357a90b4778ded9b2cc220a14cea49f931e596ea"));
     131           1 :     TestCrypter::TestDecrypt(crypt,ParseHex("b88efddd668a6801d19516d6830da4ae9811988ccbaf40df8fbb72f3f4d335fd"));
     132           1 :     TestCrypter::TestDecrypt(crypt,ParseHex("8cae76aa6a43694e961ebcb28c8ca8f8540b84153d72865e8561ddd93fa7bfa9"));
     133             : 
     134         101 :     for (int i = 0; i != 100; i++)
     135             :     {
     136         100 :         uint256 hash(GetRandHash());
     137         100 :         TestCrypter::TestDecrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
     138         100 :     }
     139           1 : }
     140             : 
     141         149 : BOOST_AUTO_TEST_CASE(aes_256_cbc_testvectors) {
     142             :     // NIST AES CBC 256-bit encryption test-vectors with padding enabled
     143           2 :     TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
     144           1 :                   "000102030405060708090A0B0C0D0E0F", "6bc1bee22e409f96e93d7e117393172a", \
     145           1 :                   "f58c4c04d6e5f1ba779eabfb5f7bfbd6485a5c81519cf378fa36d42b8547edc0");
     146           2 :     TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
     147           1 :                   "F58C4C04D6E5F1BA779EABFB5F7BFBD6", "ae2d8a571e03ac9c9eb76fac45af8e51", \
     148           1 :                   "9cfc4e967edb808d679f777bc6702c7d3a3aa5e0213db1a9901f9036cf5102d2");
     149           2 :     TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
     150           1 :                   "9CFC4E967EDB808D679F777BC6702C7D", "30c81c46a35ce411e5fbc1191a0a52ef",
     151           1 :                   "39f23369a9d9bacfa530e263042314612f8da707643c90a6f732b3de1d3f5cee");
     152           2 :     TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
     153           1 :                   "39F23369A9D9BACFA530E26304231461", "f69f2445df4f9b17ad2b417be66c3710", \
     154           1 :                   "b2eb05e2c39be9fcda6c19078c6a9d1b3f461796d6b0d6b2e0c2a72b4d80e644");
     155           1 : }
     156             : 
     157         146 : BOOST_AUTO_TEST_SUITE_END()
     158             : } // namespace wallet

Generated by: LCOV version 1.16