Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2020-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.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>11#include <array>12#include <cstddef>13#include <cstdint>14#include <vector>16FUZZ_TARGET(crypto_chacha20)17{18FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};20const auto key = ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, ChaCha20::KEYLEN);21ChaCha20 chacha20{key};23LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {24CallOneOf(25fuzzed_data_provider,26[&] {27auto key = ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, ChaCha20::KEYLEN);28chacha20.SetKey(key);29},30[&] {31ChaCha20::Nonce96 nonce{32fuzzed_data_provider.ConsumeIntegral<uint32_t>(),33fuzzed_data_provider.ConsumeIntegral<uint64_t>()};34chacha20.Seek(nonce, fuzzed_data_provider.ConsumeIntegral<uint32_t>());35},36[&] {37std::vector<uint8_t> output(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));38chacha20.Keystream(MakeWritableByteSpan(output));39},40[&] {41std::vector<std::byte> output(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));42const auto input = ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, output.size());43chacha20.Crypt(input, output);44});45}46}48namespace49{51/** Fuzzer that invokes ChaCha20::Crypt() or ChaCha20::Keystream multiple times:52once for a large block at once, and then the same data in chunks, comparing53the outcome.55If UseCrypt, seeded InsecureRandomContext output is used as input to Crypt().56If not, Keystream() is used directly, or sequences of 0x00 are encrypted.57*/58template<bool UseCrypt>59void ChaCha20SplitFuzz(FuzzedDataProvider& provider)60{61// Determine key, iv, start position, length.62auto key_bytes = ConsumeFixedLengthByteVector<std::byte>(provider, ChaCha20::KEYLEN);63uint64_t iv = provider.ConsumeIntegral<uint64_t>();64uint32_t iv_prefix = provider.ConsumeIntegral<uint32_t>();65uint64_t total_bytes = provider.ConsumeIntegralInRange<uint64_t>(0, 1000000);66/* ~x = 2^BITS - 1 - x, so ~(total_bytes >> 6) is the maximal seek position. */67uint32_t seek = provider.ConsumeIntegralInRange<uint32_t>(0, ~(uint32_t)(total_bytes >> 6));69// Initialize two ChaCha20 ciphers, with the same key/iv/position.70ChaCha20 crypt1(key_bytes);71ChaCha20 crypt2(key_bytes);72crypt1.Seek({iv_prefix, iv}, seek);73crypt2.Seek({iv_prefix, iv}, seek);75// Construct vectors with data.76std::vector<std::byte> data1, data2;77data1.resize(total_bytes);78data2.resize(total_bytes);80// If using Crypt(), initialize data1 and data2 with the same InsecureRandomContext based81// stream.82if constexpr (UseCrypt) {83InsecureRandomContext(provider.ConsumeIntegral<uint64_t>()).fillrand(data1);84std::copy(data1.begin(), data1.end(), data2.begin());85}87// Whether UseCrypt is used or not, the two byte arrays must match.88assert(data1 == data2);90// Encrypt data1, the whole array at once.91if constexpr (UseCrypt) {92crypt1.Crypt(data1, data1);93} else {94crypt1.Keystream(data1);95}97// Encrypt data2, in at most 256 chunks.98uint64_t bytes2 = 0;99int iter = 0;100while (true) {101bool is_last = (iter == 255) || (bytes2 == total_bytes) || provider.ConsumeBool();102++iter;103// Determine how many bytes to encrypt in this chunk: a fuzzer-determined104// amount for all but the last chunk (which processes all remaining bytes).105uint64_t now = is_last ? total_bytes - bytes2 :106provider.ConsumeIntegralInRange<uint64_t>(0, total_bytes - bytes2);107// For each chunk, consider using Crypt() even when UseCrypt is false.108// This tests that Keystream() has the same behavior as Crypt() applied109// to 0x00 input bytes.110if (UseCrypt || provider.ConsumeBool()) {111crypt2.Crypt(std::span{data2}.subspan(bytes2, now), std::span{data2}.subspan(bytes2, now));112} else {113crypt2.Keystream(std::span{data2}.subspan(bytes2, now));114}115bytes2 += now;116if (is_last) break;117}118// We should have processed everything now.119assert(bytes2 == total_bytes);120// And the result should match.121assert(data1 == data2);122}124} // namespace126FUZZ_TARGET(chacha20_split_crypt)127{128FuzzedDataProvider provider{buffer.data(), buffer.size()};129ChaCha20SplitFuzz<true>(provider);130}132FUZZ_TARGET(chacha20_split_keystream)133{134FuzzedDataProvider provider{buffer.data(), buffer.size()};135ChaCha20SplitFuzz<false>(provider);136}138FUZZ_TARGET(crypto_fschacha20)139{140FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};142auto key = fuzzed_data_provider.ConsumeBytes<std::byte>(FSChaCha20::KEYLEN);143key.resize(FSChaCha20::KEYLEN);145auto fsc20 = FSChaCha20{key, fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, 1024)};147LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {148auto input = fuzzed_data_provider.ConsumeBytes<std::byte>(fuzzed_data_provider.ConsumeIntegralInRange(0, 4096));149std::vector<std::byte> output;150output.resize(input.size());151fsc20.Crypt(input, output);152}153}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.