LCOV - code coverage report
Current view: top level - src/test/fuzz - crypto_chacha20.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 78 78
Test Date: 2026-11-21 05:38:30 Functions: 100.0 % 14 14
Branches: 62.5 % 88 55

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-present 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 <crypto/chacha20.h>
       6                 :             : #include <random.h>
       7                 :             : #include <test/fuzz/FuzzedDataProvider.h>
       8                 :             : #include <test/fuzz/fuzz.h>
       9                 :             : #include <test/fuzz/util.h>
      10                 :             : #include <util/check.h>
      11                 :             : 
      12                 :             : #include <array>
      13                 :             : #include <cstddef>
      14                 :             : #include <cstdint>
      15                 :             : #include <vector>
      16                 :             : 
      17         [ +  - ]:        1083 : FUZZ_TARGET(crypto_chacha20)
      18                 :             : {
      19                 :         607 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      20                 :             : 
      21                 :         607 :     const auto key = ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, ChaCha20::KEYLEN);
      22         [ -  + ]:         607 :     ChaCha20 chacha20{key};
      23                 :             : 
      24   [ +  +  +  + ]:      575834 :     LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {
      25         [ +  - ]:      575227 :         CallOneOf(
      26                 :             :             fuzzed_data_provider,
      27                 :       10801 :             [&] {
      28                 :       10801 :                 auto key = ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, ChaCha20::KEYLEN);
      29         [ -  + ]:       10801 :                 chacha20.SetKey(key);
      30                 :       10801 :             },
      31                 :      191986 :             [&] {
      32                 :      191986 :                 ChaCha20::Nonce96 nonce{
      33                 :      191986 :                     fuzzed_data_provider.ConsumeIntegral<uint32_t>(),
      34                 :      191986 :                     fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
      35                 :      191986 :                 chacha20.Seek(nonce, fuzzed_data_provider.ConsumeIntegral<uint32_t>());
      36                 :      191986 :             },
      37                 :      269520 :             [&] {
      38                 :      269520 :                 std::vector<uint8_t> output(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
      39                 :      269520 :                 chacha20.Keystream(MakeWritableByteSpan(output));
      40                 :      269520 :             },
      41                 :      102920 :             [&] {
      42                 :      102920 :                 std::vector<std::byte> output(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
      43         [ -  + ]:      102920 :                 const auto input = ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, output.size());
      44   [ -  +  -  + ]:      102920 :                 chacha20.Crypt(input, output);
      45                 :      102920 :             });
      46                 :             :     }
      47                 :         607 : }
      48                 :             : 
      49                 :             : namespace
      50                 :             : {
      51                 :             : 
      52                 :             : /** Fuzzer that invokes ChaCha20::Crypt() or ChaCha20::Keystream multiple times:
      53                 :             :     once for a large block at once, and then the same data in chunks, comparing
      54                 :             :     the outcome.
      55                 :             : 
      56                 :             :     If UseCrypt, seeded InsecureRandomContext output is used as input to Crypt().
      57                 :             :     If not, Keystream() is used directly, or sequences of 0x00 are encrypted.
      58                 :             : */
      59                 :             : template<bool UseCrypt>
      60                 :         745 : void ChaCha20SplitFuzz(FuzzedDataProvider& provider)
      61                 :             : {
      62                 :             :     // Determine key, iv, start position, length.
      63                 :         745 :     auto key_bytes = ConsumeFixedLengthByteVector<std::byte>(provider, ChaCha20::KEYLEN);
      64                 :         745 :     uint64_t iv = provider.ConsumeIntegral<uint64_t>();
      65                 :         745 :     uint32_t iv_prefix = provider.ConsumeIntegral<uint32_t>();
      66                 :         745 :     uint64_t total_bytes = provider.ConsumeIntegralInRange<uint64_t>(0, 1000000);
      67                 :             :     /* ~x = 2^BITS - 1 - x, so ~(total_bytes >> 6) is the maximal seek position. */
      68         [ -  + ]:         745 :     uint32_t seek = provider.ConsumeIntegralInRange<uint32_t>(0, ~(uint32_t)(total_bytes >> 6));
      69                 :             : 
      70                 :             :     // Initialize two ChaCha20 ciphers, with the same key/iv/position.
      71         [ -  + ]:         745 :     ChaCha20 crypt1(key_bytes);
      72                 :         745 :     ChaCha20 crypt2(key_bytes);
      73                 :         745 :     crypt1.Seek({iv_prefix, iv}, seek);
      74                 :         745 :     crypt2.Seek({iv_prefix, iv}, seek);
      75                 :             : 
      76                 :             :     // Construct vectors with data.
      77                 :         745 :     std::vector<std::byte> data1, data2;
      78 [ +  - ][ +  - ]:         745 :     data1.resize(total_bytes);
      79 [ +  - ][ +  - ]:         745 :     data2.resize(total_bytes);
      80                 :             : 
      81                 :             :     // If using Crypt(), initialize data1 and data2 with the same InsecureRandomContext based
      82                 :             :     // stream.
      83                 :             :     if constexpr (UseCrypt) {
      84         [ -  + ]:         347 :         InsecureRandomContext(provider.ConsumeIntegral<uint64_t>()).fillrand(data1);
      85                 :         347 :         std::copy(data1.begin(), data1.end(), data2.begin());
      86                 :             :     }
      87                 :             : 
      88                 :             :     // Whether UseCrypt is used or not, the two byte arrays must match.
      89 [ -  + ][ -  + ]:         745 :     assert(data1 == data2);
      90                 :             : 
      91                 :             :     // Encrypt data1, the whole array at once.
      92                 :             :     if constexpr (UseCrypt) {
      93                 :         347 :         crypt1.Crypt(data1, data1);
      94                 :             :     } else {
      95                 :         398 :         crypt1.Keystream(data1);
      96                 :             :     }
      97                 :             : 
      98                 :             :     // Encrypt data2, in at most 256 chunks.
      99                 :         745 :     uint64_t bytes2 = 0;
     100                 :         745 :     int iter = 0;
     101                 :             :     while (true) {
     102   [ +  +  +  + ]:      181966 :         bool is_last = (iter == 255) || (bytes2 == total_bytes) || provider.ConsumeBool();
           [ +  +  +  + ]
     103                 :       91182 :         ++iter;
     104                 :             :         // Determine how many bytes to encrypt in this chunk: a fuzzer-determined
     105                 :             :         // amount for all but the last chunk (which processes all remaining bytes).
     106                 :         745 :         uint64_t now = is_last ? total_bytes - bytes2 :
     107                 :       90437 :             provider.ConsumeIntegralInRange<uint64_t>(0, total_bytes - bytes2);
     108                 :             :         // For each chunk, consider using Crypt() even when UseCrypt is false.
     109                 :             :         // This tests that Keystream() has the same behavior as Crypt() applied
     110                 :             :         // to 0x00 input bytes.
     111         [ +  + ]:       35792 :         if (UseCrypt || provider.ConsumeBool()) {
     112 [ -  + ][ -  + ]:       60431 :             crypt2.Crypt(std::span{data2}.subspan(bytes2, now), std::span{data2}.subspan(bytes2, now));
     113                 :             :         } else {
     114         [ -  + ]:       30751 :             crypt2.Keystream(std::span{data2}.subspan(bytes2, now));
     115                 :             :         }
     116                 :       91182 :         bytes2 += now;
     117 [ +  + ][ +  + ]:       91182 :         if (is_last) break;
     118                 :             :     }
     119                 :             :     // We should have processed everything now.
     120 [ -  + ][ -  + ]:         745 :     assert(bytes2 == total_bytes);
     121                 :             :     // And the result should match.
     122 [ -  + ][ -  + ]:         745 :     assert(data1 == data2);
     123                 :         745 : }
     124                 :             : 
     125                 :             : } // namespace
     126                 :             : 
     127         [ +  - ]:         823 : FUZZ_TARGET(chacha20_split_crypt)
     128                 :             : {
     129                 :         347 :     FuzzedDataProvider provider{buffer.data(), buffer.size()};
     130                 :         347 :     ChaCha20SplitFuzz<true>(provider);
     131                 :         347 : }
     132                 :             : 
     133         [ +  - ]:         874 : FUZZ_TARGET(chacha20_split_keystream)
     134                 :             : {
     135                 :         398 :     FuzzedDataProvider provider{buffer.data(), buffer.size()};
     136                 :         398 :     ChaCha20SplitFuzz<false>(provider);
     137                 :         398 : }
     138                 :             : 
     139         [ +  - ]:         810 : FUZZ_TARGET(crypto_fschacha20)
     140                 :             : {
     141                 :         334 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     142                 :             : 
     143                 :         334 :     auto key = fuzzed_data_provider.ConsumeBytes<std::byte>(FSChaCha20::KEYLEN);
     144         [ +  - ]:         334 :     key.resize(FSChaCha20::KEYLEN);
     145                 :             : 
     146         [ -  + ]:         334 :     auto fsc20 = FSChaCha20{key, fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, 1024)};
     147                 :             : 
     148   [ +  +  +  + ]:      909792 :     LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {
     149         [ +  - ]:      909458 :         auto input = fuzzed_data_provider.ConsumeBytes<std::byte>(fuzzed_data_provider.ConsumeIntegralInRange(0, 4096));
     150                 :      909458 :         std::vector<std::byte> output;
     151   [ -  +  +  - ]:      909458 :         output.resize(input.size());
     152   [ -  +  -  + ]:      909458 :         fsc20.Crypt(input, output);
     153                 :      909458 :     }
     154                 :         334 : }
        

Generated by: LCOV version 2.5.0-full