Coverage Report

Created: 2025-10-13 06:05

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
extern "C" {
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.47k
                                size_t digest_size) {
43
2.47k
  if (!fdp.remaining_bytes()) {
44
21
    return;
45
21
  }
46
47
  // Pull a random slice of data for fuzzing
48
2.45k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
49
2.45k
  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.45k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
53
2.45k
  std::vector<HashType> contexts(num_contexts);
54
2.45k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
55
7.98k
  for (unsigned i = 0; i < num_contexts; i++) {
56
5.53k
    init_once(&contexts[i]);
57
5.53k
  }
58
59
  // Intentionally misalign the data pointer to stress alignment sensitive paths
60
2.45k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
61
2.45k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
62
2.45k
  if (!input_bytes.empty()) {
63
1.77k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
64
1.77k
  }
65
66
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
67
2.45k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
68
2.45k
  size_t remaining = input_bytes.size();
69
70
  // Perform multiple hash update iterations on the raw data
71
2.45k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
72
6.33k
  while (num_iterations-- && remaining > 0) {
73
    // Pick which context to feed this iteration
74
3.88k
    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.88k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
78
3.88k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
79
80
3.88k
    size_t chunk_len = 0;
81
3.88k
    switch (pattern) {
82
876
      case LESS1: {
83
        // Consume 1 byte less from block size from the raw data for this iteration
84
876
        if (block_size > 1) {
85
876
          chunk_len = std::min(remaining, block_size - 1);
86
876
        }
87
876
        break;
88
0
      }
89
282
      case EQ: {
90
        // Consume block size bytes from the raw data for this iteration
91
282
        chunk_len = std::min(remaining, block_size);
92
282
        break;
93
0
      }
94
225
      case PLUS1: {
95
        // Consume 1 byte more from block size from the raw data for this iteration
96
225
        chunk_len = std::min(remaining, block_size + 1);
97
225
        break;
98
0
      }
99
771
      case SMALL: {
100
        // Consume 1~32 bytes from the raw data for this iteration
101
771
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
102
771
        chunk_len = std::min(remaining, small_len);
103
771
        break;
104
0
      }
105
661
      case RANDOM: {
106
        // Consume random bytes from the raw data for this iteration
107
661
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
108
661
        break;
109
0
      }
110
100
      case TAIL: {
111
        // Consume all remaining bytes from the raw data for this iteration
112
100
        chunk_len = remaining;
113
100
        break;
114
0
      }
115
969
      case HALT: {
116
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
117
969
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
118
969
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
119
2.66k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
120
1.69k
          size_t w = std::min(remaining, step);
121
1.69k
          update_fn(&contexts[ctx_index], w, cursor);
122
1.69k
          cursor += w;
123
1.69k
          remaining -= w;
124
1.69k
        }
125
126
        // Randomly reinitialise the hash stream
127
969
        if (fdp.ConsumeBool()) {
128
418
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
129
418
        }
130
969
        continue;
131
0
      }
132
3.88k
    }
133
134
2.91k
    if (chunk_len == 0 || chunk_len > remaining) {
135
0
      continue;
136
0
    }
137
138
    // Fuzz the update function
139
2.91k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
140
2.91k
    cursor += chunk_len;
141
2.91k
    remaining -= chunk_len;
142
2.91k
  }
143
144
  // Finalize all active contexts (finish_reset).
145
7.98k
  for (unsigned i = 0; i < num_contexts; i++) {
146
5.53k
    finish_fn(&contexts[i], digests[i].data());
147
5.53k
  }
148
149
  // Additional fuzzing on special context chaining approach.
150
2.45k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
151
773
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
152
773
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
153
773
    if (src_idx != dst_idx) {
154
441
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
155
441
      size_t max_avail = digest_size - offset; // >= 1
156
441
      size_t feed_len = fdp.ConsumeIntegralInRange<size_t>(1, max_avail);
157
441
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
158
441
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
159
441
    }
160
773
  }
161
162
  // Deinitialise all contexts after this iteration
163
7.98k
  for (unsigned i = 0; i < num_contexts; i++) {
164
5.53k
    deinit_fn(&contexts[i]);
165
5.53k
  }
166
2.45k
}
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.31k
                                size_t digest_size) {
43
1.31k
  if (!fdp.remaining_bytes()) {
44
10
    return;
45
10
  }
46
47
  // Pull a random slice of data for fuzzing
48
1.30k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
49
1.30k
  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.30k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
53
1.30k
  std::vector<HashType> contexts(num_contexts);
54
1.30k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
55
4.44k
  for (unsigned i = 0; i < num_contexts; i++) {
56
3.14k
    init_once(&contexts[i]);
57
3.14k
  }
58
59
  // Intentionally misalign the data pointer to stress alignment sensitive paths
60
1.30k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
61
1.30k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
62
1.30k
  if (!input_bytes.empty()) {
63
984
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
64
984
  }
65
66
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
67
1.30k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
68
1.30k
  size_t remaining = input_bytes.size();
69
70
  // Perform multiple hash update iterations on the raw data
71
1.30k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
72
3.51k
  while (num_iterations-- && remaining > 0) {
73
    // Pick which context to feed this iteration
74
2.20k
    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.20k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
78
2.20k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
79
80
2.20k
    size_t chunk_len = 0;
81
2.20k
    switch (pattern) {
82
467
      case LESS1: {
83
        // Consume 1 byte less from block size from the raw data for this iteration
84
467
        if (block_size > 1) {
85
467
          chunk_len = std::min(remaining, block_size - 1);
86
467
        }
87
467
        break;
88
0
      }
89
176
      case EQ: {
90
        // Consume block size bytes from the raw data for this iteration
91
176
        chunk_len = std::min(remaining, block_size);
92
176
        break;
93
0
      }
94
140
      case PLUS1: {
95
        // Consume 1 byte more from block size from the raw data for this iteration
96
140
        chunk_len = std::min(remaining, block_size + 1);
97
140
        break;
98
0
      }
99
461
      case SMALL: {
100
        // Consume 1~32 bytes from the raw data for this iteration
101
461
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
102
461
        chunk_len = std::min(remaining, small_len);
103
461
        break;
104
0
      }
105
385
      case RANDOM: {
106
        // Consume random bytes from the raw data for this iteration
107
385
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
108
385
        break;
109
0
      }
110
64
      case TAIL: {
111
        // Consume all remaining bytes from the raw data for this iteration
112
64
        chunk_len = remaining;
113
64
        break;
114
0
      }
115
515
      case HALT: {
116
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
117
515
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
118
515
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
119
1.46k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
120
947
          size_t w = std::min(remaining, step);
121
947
          update_fn(&contexts[ctx_index], w, cursor);
122
947
          cursor += w;
123
947
          remaining -= w;
124
947
        }
125
126
        // Randomly reinitialise the hash stream
127
515
        if (fdp.ConsumeBool()) {
128
235
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
129
235
        }
130
515
        continue;
131
0
      }
132
2.20k
    }
133
134
1.69k
    if (chunk_len == 0 || chunk_len > remaining) {
135
0
      continue;
136
0
    }
137
138
    // Fuzz the update function
139
1.69k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
140
1.69k
    cursor += chunk_len;
141
1.69k
    remaining -= chunk_len;
142
1.69k
  }
143
144
  // Finalize all active contexts (finish_reset).
145
4.44k
  for (unsigned i = 0; i < num_contexts; i++) {
146
3.14k
    finish_fn(&contexts[i], digests[i].data());
147
3.14k
  }
148
149
  // Additional fuzzing on special context chaining approach.
150
1.30k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
151
459
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
152
459
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
153
459
    if (src_idx != dst_idx) {
154
217
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
155
217
      size_t max_avail = digest_size - offset; // >= 1
156
217
      size_t feed_len = fdp.ConsumeIntegralInRange<size_t>(1, max_avail);
157
217
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
158
217
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
159
217
    }
160
459
  }
161
162
  // Deinitialise all contexts after this iteration
163
4.44k
  for (unsigned i = 0; i < num_contexts; i++) {
164
3.14k
    deinit_fn(&contexts[i]);
165
3.14k
  }
166
1.30k
}
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.15k
                                size_t digest_size) {
43
1.15k
  if (!fdp.remaining_bytes()) {
44
11
    return;
45
11
  }
46
47
  // Pull a random slice of data for fuzzing
48
1.14k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
49
1.14k
  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.14k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
53
1.14k
  std::vector<HashType> contexts(num_contexts);
54
1.14k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
55
3.54k
  for (unsigned i = 0; i < num_contexts; i++) {
56
2.39k
    init_once(&contexts[i]);
57
2.39k
  }
58
59
  // Intentionally misalign the data pointer to stress alignment sensitive paths
60
1.14k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
61
1.14k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
62
1.14k
  if (!input_bytes.empty()) {
63
789
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
64
789
  }
65
66
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
67
1.14k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
68
1.14k
  size_t remaining = input_bytes.size();
69
70
  // Perform multiple hash update iterations on the raw data
71
1.14k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
72
2.82k
  while (num_iterations-- && remaining > 0) {
73
    // Pick which context to feed this iteration
74
1.67k
    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.67k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
78
1.67k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
79
80
1.67k
    size_t chunk_len = 0;
81
1.67k
    switch (pattern) {
82
409
      case LESS1: {
83
        // Consume 1 byte less from block size from the raw data for this iteration
84
409
        if (block_size > 1) {
85
409
          chunk_len = std::min(remaining, block_size - 1);
86
409
        }
87
409
        break;
88
0
      }
89
106
      case EQ: {
90
        // Consume block size bytes from the raw data for this iteration
91
106
        chunk_len = std::min(remaining, block_size);
92
106
        break;
93
0
      }
94
85
      case PLUS1: {
95
        // Consume 1 byte more from block size from the raw data for this iteration
96
85
        chunk_len = std::min(remaining, block_size + 1);
97
85
        break;
98
0
      }
99
310
      case SMALL: {
100
        // Consume 1~32 bytes from the raw data for this iteration
101
310
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
102
310
        chunk_len = std::min(remaining, small_len);
103
310
        break;
104
0
      }
105
276
      case RANDOM: {
106
        // Consume random bytes from the raw data for this iteration
107
276
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
108
276
        break;
109
0
      }
110
36
      case TAIL: {
111
        // Consume all remaining bytes from the raw data for this iteration
112
36
        chunk_len = remaining;
113
36
        break;
114
0
      }
115
454
      case HALT: {
116
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
117
454
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
118
454
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
119
1.20k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
120
746
          size_t w = std::min(remaining, step);
121
746
          update_fn(&contexts[ctx_index], w, cursor);
122
746
          cursor += w;
123
746
          remaining -= w;
124
746
        }
125
126
        // Randomly reinitialise the hash stream
127
454
        if (fdp.ConsumeBool()) {
128
183
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
129
183
        }
130
454
        continue;
131
0
      }
132
1.67k
    }
133
134
1.22k
    if (chunk_len == 0 || chunk_len > remaining) {
135
0
      continue;
136
0
    }
137
138
    // Fuzz the update function
139
1.22k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
140
1.22k
    cursor += chunk_len;
141
1.22k
    remaining -= chunk_len;
142
1.22k
  }
143
144
  // Finalize all active contexts (finish_reset).
145
3.54k
  for (unsigned i = 0; i < num_contexts; i++) {
146
2.39k
    finish_fn(&contexts[i], digests[i].data());
147
2.39k
  }
148
149
  // Additional fuzzing on special context chaining approach.
150
1.14k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
151
314
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
152
314
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
153
314
    if (src_idx != dst_idx) {
154
224
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
155
224
      size_t max_avail = digest_size - offset; // >= 1
156
224
      size_t feed_len = fdp.ConsumeIntegralInRange<size_t>(1, max_avail);
157
224
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
158
224
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
159
224
    }
160
314
  }
161
162
  // Deinitialise all contexts after this iteration
163
3.54k
  for (unsigned i = 0; i < num_contexts; i++) {
164
2.39k
    deinit_fn(&contexts[i]);
165
2.39k
  }
166
1.14k
}
167
168
1.33k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
169
1.33k
  FuzzedDataProvider fdp(data, size);
170
171
3.80k
  for (unsigned i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) {
172
2.47k
    if (fdp.ConsumeBool()) {
173
1.31k
      fuzz_hash_ext_multi<struct mhd_Md5CtxExt>(
174
1.31k
        fdp, 64,
175
1.31k
        mhd_MD5_init_one_time, mhd_MD5_update, mhd_MD5_finish_reset, mhd_MD5_deinit,
176
1.31k
        mhd_MD5_DIGEST_SIZE);
177
1.31k
    } else {
178
1.15k
      fuzz_hash_ext_multi<struct mhd_Sha256CtxExt>(
179
1.15k
        fdp, 64,
180
1.15k
        mhd_SHA256_init_one_time, mhd_SHA256_update, mhd_SHA256_finish_reset, mhd_SHA256_deinit,
181
1.15k
        mhd_SHA256_DIGEST_SIZE);
182
1.15k
    }
183
2.47k
  }
184
1.33k
  return 0;
185
1.33k
}