LCOV - code coverage report
Current view: top level - src/wallet/test/fuzz - crypter.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 93.1 % 58 54
Test Date: 2024-09-01 05:20:30 Functions: 90.0 % 10 9
Branches: 56.2 % 48 27

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2022 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/fuzz/FuzzedDataProvider.h>
       6                 :             : #include <test/fuzz/fuzz.h>
       7                 :             : #include <test/fuzz/util.h>
       8                 :             : #include <test/util/setup_common.h>
       9                 :             : #include <wallet/crypter.h>
      10                 :             : 
      11                 :             : namespace wallet {
      12                 :             : namespace {
      13                 :             : 
      14                 :             : const TestingSetup* g_setup;
      15                 :           0 : void initialize_crypter()
      16                 :             : {
      17   [ #  #  #  #  :           0 :     static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
                   #  # ]
      18                 :           0 :     g_setup = testing_setup.get();
      19                 :           0 : }
      20                 :             : 
      21         [ +  - ]:         557 : FUZZ_TARGET(crypter, .init = initialize_crypter)
      22                 :             : {
      23                 :         555 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      24                 :         555 :     bool good_data{true};
      25                 :             : 
      26                 :         555 :     CCrypter crypt;
      27                 :             :     // These values are regularly updated within `CallOneOf`
      28                 :         555 :     std::vector<unsigned char> cipher_text_ed;
      29                 :         555 :     CKeyingMaterial plain_text_ed;
      30                 :         555 :     const std::vector<unsigned char> random_key = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
      31                 :             : 
      32   [ +  -  +  + ]:         555 :     if (fuzzed_data_provider.ConsumeBool()) {
      33         [ +  - ]:         539 :         const std::string random_string = fuzzed_data_provider.ConsumeRandomLengthString(100);
      34         [ +  - ]:         539 :         SecureString secure_string(random_string.begin(), random_string.end());
      35                 :             : 
      36   [ +  -  +  +  :         539 :         const unsigned int derivation_method = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<unsigned int>();
                   +  - ]
      37                 :             : 
      38                 :             :         // Limiting the value of rounds since it is otherwise uselessly expensive and causes a timeout when fuzzing.
      39         [ +  - ]:         539 :         crypt.SetKeyFromPassphrase(/*key_data=*/secure_string,
      40                 :         539 :                                    /*salt=*/ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_SALT_SIZE),
      41         [ +  - ]:         539 :                                    /*rounds=*/fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 25000),
      42                 :         539 :                                    /*derivation_method=*/derivation_method);
      43                 :         539 :     }
      44                 :             : 
      45   [ +  +  +  -  :       18690 :     LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 100)
             +  +  +  + ]
      46                 :             :     {
      47         [ +  - ]:       18135 :         CallOneOf(
      48                 :             :             fuzzed_data_provider,
      49                 :       18865 :             [&] {
      50                 :         730 :                 const std::vector<unsigned char> random_vector = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
      51         [ +  - ]:         730 :                 plain_text_ed = CKeyingMaterial(random_vector.begin(), random_vector.end());
      52                 :         730 :             },
      53                 :       19843 :             [&] {
      54                 :        1708 :                 cipher_text_ed = ConsumeRandomLengthByteVector(fuzzed_data_provider, 64);
      55                 :        1708 :             },
      56                 :       22157 :             [&] {
      57                 :        4022 :                 (void)crypt.Encrypt(plain_text_ed, cipher_text_ed);
      58                 :        4022 :             },
      59                 :       22934 :             [&] {
      60                 :        4799 :                 (void)crypt.Decrypt(cipher_text_ed, plain_text_ed);
      61                 :        4799 :             },
      62                 :       20865 :             [&] {
      63                 :        2730 :                 const CKeyingMaterial master_key(random_key.begin(), random_key.end());
      64                 :        2730 :                 const uint256 iv = ConsumeUInt256(fuzzed_data_provider);
      65         [ +  - ]:        2730 :                 (void)EncryptSecret(master_key, plain_text_ed, iv, cipher_text_ed);
      66                 :        2730 :             },
      67                 :       21058 :             [&] {
      68                 :        2923 :                 const CKeyingMaterial master_key(random_key.begin(), random_key.end());
      69                 :        2923 :                 const uint256 iv = ConsumeUInt256(fuzzed_data_provider);
      70         [ +  - ]:        2923 :                 (void)DecryptSecret(master_key, cipher_text_ed, iv, plain_text_ed);
      71                 :        2923 :             },
      72                 :       19358 :             [&] {
      73                 :        1223 :                 std::optional<CPubKey> random_pub_key = ConsumeDeserializable<CPubKey>(fuzzed_data_provider);
      74         [ +  + ]:        1223 :                 if (!random_pub_key) {
      75                 :          79 :                     good_data = false;
      76                 :          79 :                     return;
      77                 :             :                 }
      78                 :        1144 :                 const CPubKey pub_key = *random_pub_key;
      79                 :        1144 :                 const CKeyingMaterial master_key(random_key.begin(), random_key.end());
      80                 :        1144 :                 const std::vector<unsigned char> crypted_secret = ConsumeRandomLengthByteVector(fuzzed_data_provider, 64);
      81                 :        1144 :                 CKey key;
      82         [ +  - ]:        1144 :                 (void)DecryptKey(master_key, crypted_secret, pub_key, key);
      83         [ -  + ]:        1223 :             });
      84                 :       18135 :     }
      85                 :         555 : }
      86                 :             : } // namespace
      87                 :             : } // namespace wallet
        

Generated by: LCOV version 2.0-1