LCOV - code coverage report
Current view: top level - src/test/fuzz - bip324.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 63 63
Test Date: 2026-11-29 06:21:53 Functions: 100.0 % 3 3
Branches: 61.4 % 88 54

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2023-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 <bip324.h>
       6                 :             : 
       7                 :             : #include <chainparams.h>
       8                 :             : #include <random.h>
       9                 :             : #include <span.h>
      10                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      11                 :             : #include <test/fuzz/fuzz.h>
      12                 :             : #include <test/fuzz/util.h>
      13                 :             : #include <util/check.h>
      14                 :             : 
      15                 :             : #include <algorithm>
      16                 :             : #include <cstdint>
      17                 :             : #include <vector>
      18                 :             : 
      19                 :             : namespace {
      20                 :             : 
      21                 :           1 : void Initialize()
      22                 :             : {
      23   [ +  -  +  -  :           1 :     static ECC_Context ecc_context{};
                   +  - ]
      24                 :           1 :     SelectParams(ChainType::MAIN);
      25                 :           1 : }
      26                 :             : 
      27                 :             : }  // namespace
      28                 :             : 
      29         [ +  - ]:        1951 : FUZZ_TARGET(bip324_cipher_roundtrip, .init=Initialize)
      30                 :             : {
      31                 :             :     // Test that BIP324Cipher's encryption and decryption agree.
      32                 :             : 
      33                 :             :     // Load keys from fuzzer.
      34                 :        1473 :     FuzzedDataProvider provider(buffer.data(), buffer.size());
      35                 :             :     // Initiator key
      36                 :        1473 :     CKey init_key = ConsumePrivateKey(provider, /*compressed=*/true);
      37         [ +  + ]:        1473 :     if (!init_key.IsValid()) return;
      38                 :             :     // Initiator entropy
      39         [ +  - ]:        1471 :     auto init_ent = provider.ConsumeBytes<std::byte>(32);
      40         [ +  - ]:        1471 :     init_ent.resize(32);
      41                 :             :     // Responder key
      42                 :        1471 :     CKey resp_key = ConsumePrivateKey(provider, /*compressed=*/true);
      43         [ +  + ]:        1471 :     if (!resp_key.IsValid()) return;
      44                 :             :     // Responder entropy
      45         [ +  - ]:        1464 :     auto resp_ent = provider.ConsumeBytes<std::byte>(32);
      46         [ +  - ]:        1464 :     resp_ent.resize(32);
      47                 :             : 
      48                 :             :     // Initialize ciphers by exchanging public keys.
      49         [ -  + ]:        1464 :     BIP324Cipher initiator(init_key, init_ent);
      50         [ -  + ]:        1464 :     assert(!initiator);
      51         [ -  + ]:        1464 :     BIP324Cipher responder(resp_key, resp_ent);
      52         [ -  + ]:        1464 :     assert(!responder);
      53                 :        1464 :     initiator.Initialize(responder.GetOurPubKey(), true);
      54         [ -  + ]:        1464 :     assert(initiator);
      55                 :        1464 :     responder.Initialize(initiator.GetOurPubKey(), false);
      56         [ -  + ]:        1464 :     assert(responder);
      57                 :             : 
      58                 :             :     // Initialize RNG deterministically, to generate contents and AAD. We assume that there are no
      59                 :             :     // (potentially buggy) edge cases triggered by specific values of contents/AAD, so we can avoid
      60                 :             :     // reading the actual data for those from the fuzzer input (which would need large amounts of
      61                 :             :     // data).
      62                 :        1464 :     InsecureRandomContext rng(provider.ConsumeIntegral<uint64_t>());
      63                 :             : 
      64                 :             :     // Compare session IDs and garbage terminators.
      65         [ -  + ]:        1464 :     assert(std::ranges::equal(initiator.GetSessionID(), responder.GetSessionID()));
      66         [ -  + ]:        1464 :     assert(std::ranges::equal(initiator.GetSendGarbageTerminator(), responder.GetReceiveGarbageTerminator()));
      67         [ -  + ]:        1464 :     assert(std::ranges::equal(initiator.GetReceiveGarbageTerminator(), responder.GetSendGarbageTerminator()));
      68                 :             : 
      69   [ +  +  +  + ]:      125592 :     LIMITED_WHILE (provider.remaining_bytes(), 1000) {
      70                 :             :         // Mode:
      71                 :             :         // - Bit 0: whether the ignore bit is set in message
      72                 :             :         // - Bit 1: whether the responder (0) or initiator (1) sends
      73                 :             :         // - Bit 2: whether this ciphertext will be corrupted (making it the last sent one)
      74                 :             :         // - Bit 3-4: controls the maximum aad length (max 4095 bytes)
      75                 :             :         // - Bit 5-7: controls the maximum content length (max 16383 bytes, for performance reasons)
      76                 :      125046 :         unsigned mode = provider.ConsumeIntegral<uint8_t>();
      77                 :      125046 :         bool ignore = mode & 1;
      78                 :      125046 :         bool from_init = mode & 2;
      79                 :      125046 :         bool damage = mode & 4;
      80                 :      125046 :         unsigned aad_length_bits = 4 * ((mode >> 3) & 3);
      81                 :      125046 :         unsigned aad_length = provider.ConsumeIntegralInRange<unsigned>(0, (1 << aad_length_bits) - 1);
      82                 :      125046 :         unsigned length_bits = 2 * ((mode >> 5) & 7);
      83                 :      125046 :         unsigned length = provider.ConsumeIntegralInRange<unsigned>(0, (1 << length_bits) - 1);
      84                 :             :         // Generate aad and content.
      85                 :      125046 :         auto aad = rng.randbytes<std::byte>(aad_length);
      86                 :      125046 :         auto contents = rng.randbytes<std::byte>(length);
      87                 :             : 
      88                 :             :         // Pick sides.
      89         [ +  + ]:      125046 :         auto& sender{from_init ? initiator : responder};
      90                 :       90411 :         auto& receiver{from_init ? responder : initiator};
      91                 :             : 
      92                 :             :         // Encrypt
      93         [ +  - ]:      125046 :         std::vector<std::byte> ciphertext(length + initiator.EXPANSION);
      94   [ -  +  -  +  :      125046 :         sender.Encrypt(contents, aad, ignore, ciphertext);
                   -  + ]
      95                 :             : 
      96                 :             :         // Optionally damage 1 bit in either the ciphertext (corresponding to a change in transit)
      97                 :             :         // or the aad (to make sure that decryption will fail if the AAD mismatches).
      98         [ +  + ]:      125046 :         if (damage) {
      99         [ -  + ]:         918 :             unsigned damage_bit = provider.ConsumeIntegralInRange<unsigned>(0,
     100         [ -  + ]:         918 :                 (ciphertext.size() + aad.size()) * 8U - 1U);
     101                 :         918 :             unsigned damage_pos = damage_bit >> 3;
     102                 :         918 :             std::byte damage_val{(uint8_t)(1U << (damage_bit & 7))};
     103   [ -  +  +  + ]:         918 :             if (damage_pos >= ciphertext.size()) {
     104                 :         169 :                 aad[damage_pos - ciphertext.size()] ^= damage_val;
     105                 :             :             } else {
     106                 :         749 :                 ciphertext[damage_pos] ^= damage_val;
     107                 :             :             }
     108                 :             :         }
     109                 :             : 
     110                 :             :         // Decrypt length
     111         [ -  + ]:      125046 :         uint32_t dec_length = receiver.DecryptLength(std::span{ciphertext}.first(initiator.LENGTH_LEN));
     112         [ +  + ]:      125046 :         if (!damage) {
     113         [ -  + ]:      124128 :             assert(dec_length == length);
     114                 :             :         } else {
     115                 :             :             // For performance reasons, don't try to decode if length got increased too much.
     116         [ +  + ]:         918 :             if (dec_length > 16384 + length) break;
     117                 :             :             // Otherwise, just append zeros if dec_length > length.
     118         [ +  - ]:         877 :             ciphertext.resize(dec_length + initiator.EXPANSION);
     119                 :             :         }
     120                 :             : 
     121                 :             :         // Decrypt
     122         [ +  - ]:      125005 :         std::vector<std::byte> decrypt(dec_length);
     123                 :      125005 :         bool dec_ignore{false};
     124   [ -  +  -  +  :      125005 :         bool ok = receiver.Decrypt(std::span{ciphertext}.subspan(initiator.LENGTH_LEN), aad, dec_ignore, decrypt);
                   -  + ]
     125                 :             :         // Decryption *must* fail if the packet was damaged, and succeed if it wasn't.
     126         [ -  + ]:      125005 :         assert(!ok == damage);
     127         [ +  + ]:      125005 :         if (!ok) break;
     128         [ -  + ]:      124128 :         assert(ignore == dec_ignore);
     129         [ -  + ]:      124128 :         assert(decrypt == contents);
     130                 :      125923 :     }
     131                 :        1480 : }
        

Generated by: LCOV version 2.5.0-full