Coverage Report

Created: 2026-03-12 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fuzz_crypto_ext.cpp
Line
Count
Source
1
// Copyright 2025 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
////////////////////////////////////////////////////////////////////////////////
16
#include <stddef.h>
17
#include <stdint.h>
18
#include <string.h>
19
#include <vector>
20
#include <algorithm>
21
22
#include <fuzzer/FuzzedDataProvider.h>
23
24
#include "md5_ext.h"
25
#include "sha256_ext.h"
26
27
28
// Fuzzing target function pointer types for the enternal hash APIs
29
template <typename HashType> using InitOnceFn = void (*)(HashType*);
30
template <typename HashType> using UpdateFn   = void (*)(HashType*, size_t, const uint8_t*);
31
template <typename HashType> using FinishFn   = void (*)(HashType*, uint8_t*);
32
template <typename HashType> using DeinitFn   = void (*)(HashType*);
33
34
// Generic hashing flow that fuzz same hashing procedure for different algorithm
35
template <typename HashType>
36
static void fuzz_hash_ext_multi(FuzzedDataProvider &fdp,
37
                                size_t block_size,
38
                                InitOnceFn<HashType> init_once,
39
                                UpdateFn<HashType> update_fn,
40
                                FinishFn<HashType> finish_fn,
41
                                DeinitFn<HashType> deinit_fn,
42
2.45k
                                size_t digest_size) {
43
2.45k
  if (!fdp.remaining_bytes()) {
44
22
    return;
45
22
  }
46
47
  // Pull a random slice of data for fuzzing
48
2.43k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
49
2.43k
  std::vector<uint8_t> input_bytes = fdp.ConsumeBytes<uint8_t>(take_len);
50
51
  // Create 1 to 4 independent hashing contexts with it own digest buffer
52
2.43k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
53
2.43k
  std::vector<HashType> contexts(num_contexts);
54
2.43k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
55
7.60k
  for (unsigned i = 0; i < num_contexts; i++) {
56
5.16k
    init_once(&contexts[i]);
57
5.16k
  }
58
59
  // Intentionally misalign the data pointer to stress alignment sensitive paths
60
2.43k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
61
2.43k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
62
2.43k
  if (!input_bytes.empty()) {
63
1.79k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
64
1.79k
  }
65
66
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
67
2.43k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
68
2.43k
  size_t remaining = input_bytes.size();
69
70
  // Perform multiple hash update iterations on the raw data
71
2.43k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
72
6.41k
  while (num_iterations-- && remaining > 0) {
73
    // Pick which context to feed this iteration
74
3.97k
    const unsigned ctx_index = (num_contexts == 1) ? 0 : fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
75
76
    // Choose a chunking pattern relative to block size.
77
3.97k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
78
3.97k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
79
80
3.97k
    size_t chunk_len = 0;
81
3.97k
    switch (pattern) {
82
901
      case LESS1: {
83
        // Consume 1 byte less from block size from the raw data for this iteration
84
901
        if (block_size > 1) {
85
901
          chunk_len = std::min(remaining, block_size - 1);
86
901
        }
87
901
        break;
88
0
      }
89
272
      case EQ: {
90
        // Consume block size bytes from the raw data for this iteration
91
272
        chunk_len = std::min(remaining, block_size);
92
272
        break;
93
0
      }
94
278
      case PLUS1: {
95
        // Consume 1 byte more from block size from the raw data for this iteration
96
278
        chunk_len = std::min(remaining, block_size + 1);
97
278
        break;
98
0
      }
99
788
      case SMALL: {
100
        // Consume 1~32 bytes from the raw data for this iteration
101
788
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
102
788
        chunk_len = std::min(remaining, small_len);
103
788
        break;
104
0
      }
105
682
      case RANDOM: {
106
        // Consume random bytes from the raw data for this iteration
107
682
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
108
682
        break;
109
0
      }
110
103
      case TAIL: {
111
        // Consume all remaining bytes from the raw data for this iteration
112
103
        chunk_len = remaining;
113
103
        break;
114
0
      }
115
951
      case HALT: {
116
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
117
951
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
118
951
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
119
2.41k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
120
1.45k
          size_t w = std::min(remaining, step);
121
1.45k
          update_fn(&contexts[ctx_index], w, cursor);
122
1.45k
          cursor += w;
123
1.45k
          remaining -= w;
124
1.45k
        }
125
126
        // Randomly reinitialise the hash stream
127
951
        if (fdp.ConsumeBool()) {
128
350
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
129
350
        }
130
951
        continue;
131
0
      }
132
3.97k
    }
133
134
3.02k
    if (chunk_len == 0 || chunk_len > remaining) {
135
0
      continue;
136
0
    }
137
138
    // Fuzz the update function
139
3.02k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
140
3.02k
    cursor += chunk_len;
141
3.02k
    remaining -= chunk_len;
142
3.02k
  }
143
144
  // Finalize all active contexts (finish_reset).
145
7.60k
  for (unsigned i = 0; i < num_contexts; i++) {
146
5.16k
    finish_fn(&contexts[i], digests[i].data());
147
5.16k
  }
148
149
  // Additional fuzzing on special context chaining approach.
150
2.43k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
151
734
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
152
734
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
153
734
    if (src_idx != dst_idx) {
154
473
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
155
473
      size_t max_avail = digest_size - offset; // >= 1
156
473
      size_t feed_len = fdp.ConsumeIntegralInRange<size_t>(1, max_avail);
157
473
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
158
473
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
159
473
    }
160
734
  }
161
162
  // Deinitialise all contexts after this iteration
163
7.60k
  for (unsigned i = 0; i < num_contexts; i++) {
164
5.16k
    deinit_fn(&contexts[i]);
165
5.16k
  }
166
2.43k
}
fuzz_crypto_ext.cpp:void fuzz_hash_ext_multi<mhd_Md5CtxExt>(FuzzedDataProvider&, unsigned long, void (*)(mhd_Md5CtxExt*), void (*)(mhd_Md5CtxExt*, unsigned long, unsigned char const*), void (*)(mhd_Md5CtxExt*, unsigned char*), void (*)(mhd_Md5CtxExt*), unsigned long)
Line
Count
Source
42
1.27k
                                size_t digest_size) {
43
1.27k
  if (!fdp.remaining_bytes()) {
44
10
    return;
45
10
  }
46
47
  // Pull a random slice of data for fuzzing
48
1.26k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
49
1.26k
  std::vector<uint8_t> input_bytes = fdp.ConsumeBytes<uint8_t>(take_len);
50
51
  // Create 1 to 4 independent hashing contexts with it own digest buffer
52
1.26k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
53
1.26k
  std::vector<HashType> contexts(num_contexts);
54
1.26k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
55
4.14k
  for (unsigned i = 0; i < num_contexts; i++) {
56
2.87k
    init_once(&contexts[i]);
57
2.87k
  }
58
59
  // Intentionally misalign the data pointer to stress alignment sensitive paths
60
1.26k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
61
1.26k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
62
1.26k
  if (!input_bytes.empty()) {
63
969
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
64
969
  }
65
66
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
67
1.26k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
68
1.26k
  size_t remaining = input_bytes.size();
69
70
  // Perform multiple hash update iterations on the raw data
71
1.26k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
72
3.49k
  while (num_iterations-- && remaining > 0) {
73
    // Pick which context to feed this iteration
74
2.22k
    const unsigned ctx_index = (num_contexts == 1) ? 0 : fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
75
76
    // Choose a chunking pattern relative to block size.
77
2.22k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
78
2.22k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
79
80
2.22k
    size_t chunk_len = 0;
81
2.22k
    switch (pattern) {
82
475
      case LESS1: {
83
        // Consume 1 byte less from block size from the raw data for this iteration
84
475
        if (block_size > 1) {
85
475
          chunk_len = std::min(remaining, block_size - 1);
86
475
        }
87
475
        break;
88
0
      }
89
172
      case EQ: {
90
        // Consume block size bytes from the raw data for this iteration
91
172
        chunk_len = std::min(remaining, block_size);
92
172
        break;
93
0
      }
94
188
      case PLUS1: {
95
        // Consume 1 byte more from block size from the raw data for this iteration
96
188
        chunk_len = std::min(remaining, block_size + 1);
97
188
        break;
98
0
      }
99
452
      case SMALL: {
100
        // Consume 1~32 bytes from the raw data for this iteration
101
452
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
102
452
        chunk_len = std::min(remaining, small_len);
103
452
        break;
104
0
      }
105
402
      case RANDOM: {
106
        // Consume random bytes from the raw data for this iteration
107
402
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
108
402
        break;
109
0
      }
110
55
      case TAIL: {
111
        // Consume all remaining bytes from the raw data for this iteration
112
55
        chunk_len = remaining;
113
55
        break;
114
0
      }
115
479
      case HALT: {
116
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
117
479
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
118
479
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
119
1.26k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
120
784
          size_t w = std::min(remaining, step);
121
784
          update_fn(&contexts[ctx_index], w, cursor);
122
784
          cursor += w;
123
784
          remaining -= w;
124
784
        }
125
126
        // Randomly reinitialise the hash stream
127
479
        if (fdp.ConsumeBool()) {
128
167
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
129
167
        }
130
479
        continue;
131
0
      }
132
2.22k
    }
133
134
1.74k
    if (chunk_len == 0 || chunk_len > remaining) {
135
0
      continue;
136
0
    }
137
138
    // Fuzz the update function
139
1.74k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
140
1.74k
    cursor += chunk_len;
141
1.74k
    remaining -= chunk_len;
142
1.74k
  }
143
144
  // Finalize all active contexts (finish_reset).
145
4.14k
  for (unsigned i = 0; i < num_contexts; i++) {
146
2.87k
    finish_fn(&contexts[i], digests[i].data());
147
2.87k
  }
148
149
  // Additional fuzzing on special context chaining approach.
150
1.26k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
151
433
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
152
433
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
153
433
    if (src_idx != dst_idx) {
154
257
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
155
257
      size_t max_avail = digest_size - offset; // >= 1
156
257
      size_t feed_len = fdp.ConsumeIntegralInRange<size_t>(1, max_avail);
157
257
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
158
257
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
159
257
    }
160
433
  }
161
162
  // Deinitialise all contexts after this iteration
163
4.14k
  for (unsigned i = 0; i < num_contexts; i++) {
164
2.87k
    deinit_fn(&contexts[i]);
165
2.87k
  }
166
1.26k
}
fuzz_crypto_ext.cpp:void fuzz_hash_ext_multi<mhd_Sha256CtxExt>(FuzzedDataProvider&, unsigned long, void (*)(mhd_Sha256CtxExt*), void (*)(mhd_Sha256CtxExt*, unsigned long, unsigned char const*), void (*)(mhd_Sha256CtxExt*, unsigned char*), void (*)(mhd_Sha256CtxExt*), unsigned long)
Line
Count
Source
42
1.17k
                                size_t digest_size) {
43
1.17k
  if (!fdp.remaining_bytes()) {
44
12
    return;
45
12
  }
46
47
  // Pull a random slice of data for fuzzing
48
1.16k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
49
1.16k
  std::vector<uint8_t> input_bytes = fdp.ConsumeBytes<uint8_t>(take_len);
50
51
  // Create 1 to 4 independent hashing contexts with it own digest buffer
52
1.16k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
53
1.16k
  std::vector<HashType> contexts(num_contexts);
54
1.16k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
55
3.45k
  for (unsigned i = 0; i < num_contexts; i++) {
56
2.29k
    init_once(&contexts[i]);
57
2.29k
  }
58
59
  // Intentionally misalign the data pointer to stress alignment sensitive paths
60
1.16k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
61
1.16k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
62
1.16k
  if (!input_bytes.empty()) {
63
824
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
64
824
  }
65
66
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
67
1.16k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
68
1.16k
  size_t remaining = input_bytes.size();
69
70
  // Perform multiple hash update iterations on the raw data
71
1.16k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
72
2.91k
  while (num_iterations-- && remaining > 0) {
73
    // Pick which context to feed this iteration
74
1.75k
    const unsigned ctx_index = (num_contexts == 1) ? 0 : fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
75
76
    // Choose a chunking pattern relative to block size.
77
1.75k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
78
1.75k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
79
80
1.75k
    size_t chunk_len = 0;
81
1.75k
    switch (pattern) {
82
426
      case LESS1: {
83
        // Consume 1 byte less from block size from the raw data for this iteration
84
426
        if (block_size > 1) {
85
426
          chunk_len = std::min(remaining, block_size - 1);
86
426
        }
87
426
        break;
88
0
      }
89
100
      case EQ: {
90
        // Consume block size bytes from the raw data for this iteration
91
100
        chunk_len = std::min(remaining, block_size);
92
100
        break;
93
0
      }
94
90
      case PLUS1: {
95
        // Consume 1 byte more from block size from the raw data for this iteration
96
90
        chunk_len = std::min(remaining, block_size + 1);
97
90
        break;
98
0
      }
99
336
      case SMALL: {
100
        // Consume 1~32 bytes from the raw data for this iteration
101
336
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
102
336
        chunk_len = std::min(remaining, small_len);
103
336
        break;
104
0
      }
105
280
      case RANDOM: {
106
        // Consume random bytes from the raw data for this iteration
107
280
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
108
280
        break;
109
0
      }
110
48
      case TAIL: {
111
        // Consume all remaining bytes from the raw data for this iteration
112
48
        chunk_len = remaining;
113
48
        break;
114
0
      }
115
472
      case HALT: {
116
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
117
472
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
118
472
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
119
1.14k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
120
675
          size_t w = std::min(remaining, step);
121
675
          update_fn(&contexts[ctx_index], w, cursor);
122
675
          cursor += w;
123
675
          remaining -= w;
124
675
        }
125
126
        // Randomly reinitialise the hash stream
127
472
        if (fdp.ConsumeBool()) {
128
183
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
129
183
        }
130
472
        continue;
131
0
      }
132
1.75k
    }
133
134
1.28k
    if (chunk_len == 0 || chunk_len > remaining) {
135
0
      continue;
136
0
    }
137
138
    // Fuzz the update function
139
1.28k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
140
1.28k
    cursor += chunk_len;
141
1.28k
    remaining -= chunk_len;
142
1.28k
  }
143
144
  // Finalize all active contexts (finish_reset).
145
3.45k
  for (unsigned i = 0; i < num_contexts; i++) {
146
2.29k
    finish_fn(&contexts[i], digests[i].data());
147
2.29k
  }
148
149
  // Additional fuzzing on special context chaining approach.
150
1.16k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
151
301
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
152
301
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
153
301
    if (src_idx != dst_idx) {
154
216
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
155
216
      size_t max_avail = digest_size - offset; // >= 1
156
216
      size_t feed_len = fdp.ConsumeIntegralInRange<size_t>(1, max_avail);
157
216
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
158
216
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
159
216
    }
160
301
  }
161
162
  // Deinitialise all contexts after this iteration
163
3.45k
  for (unsigned i = 0; i < num_contexts; i++) {
164
2.29k
    deinit_fn(&contexts[i]);
165
2.29k
  }
166
1.16k
}
167
168
1.32k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
169
1.32k
  FuzzedDataProvider fdp(data, size);
170
171
3.78k
  for (unsigned i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) {
172
2.45k
    if (fdp.ConsumeBool()) {
173
1.27k
      fuzz_hash_ext_multi<struct mhd_Md5CtxExt>(
174
1.27k
        fdp, 64,
175
1.27k
        mhd_MD5_init_one_time, mhd_MD5_update, mhd_MD5_finish_reset, mhd_MD5_deinit,
176
1.27k
        mhd_MD5_DIGEST_SIZE);
177
1.27k
    } else {
178
1.17k
      fuzz_hash_ext_multi<struct mhd_Sha256CtxExt>(
179
1.17k
        fdp, 64,
180
1.17k
        mhd_SHA256_init_one_time, mhd_SHA256_update, mhd_SHA256_finish_reset, mhd_SHA256_deinit,
181
1.17k
        mhd_SHA256_DIGEST_SIZE);
182
1.17k
    }
183
2.45k
  }
184
1.32k
  return 0;
185
1.32k
}