Coverage Report

Created: 2026-12-01 06:45

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_gnutls.h"
25
#include "sha256_gnutls.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 void*);
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.53k
                                size_t digest_size) {
43
2.53k
  if (!fdp.remaining_bytes()) {
44
25
    return;
45
25
  }
46
47
  // Pull a random slice of data for fuzzing
48
2.50k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
49
2.50k
  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.50k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
53
2.50k
  std::vector<HashType> contexts(num_contexts);
54
2.50k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
55
7.82k
  for (unsigned i = 0; i < num_contexts; i++) {
56
5.32k
    init_once(&contexts[i]);
57
5.32k
  }
58
59
  // Intentionally misalign the data pointer to stress alignment sensitive paths
60
2.50k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
61
2.50k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
62
2.50k
  if (!input_bytes.empty()) {
63
1.85k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
64
1.85k
  }
65
66
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
67
2.50k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
68
2.50k
  size_t remaining = input_bytes.size();
69
70
  // Perform multiple hash update iterations on the raw data
71
2.50k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
72
6.53k
  while (num_iterations-- && remaining > 0) {
73
    // Pick which context to feed this iteration
74
4.03k
    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
4.03k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
78
4.03k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
79
80
4.03k
    size_t chunk_len = 0;
81
4.03k
    switch (pattern) {
82
852
      case LESS1: {
83
        // Consume 1 byte less from block size from the raw data for this iteration
84
852
        if (block_size > 1) {
85
852
          chunk_len = std::min(remaining, block_size - 1);
86
852
        }
87
852
        break;
88
0
      }
89
218
      case EQ: {
90
        // Consume block size bytes from the raw data for this iteration
91
218
        chunk_len = std::min(remaining, block_size);
92
218
        break;
93
0
      }
94
262
      case PLUS1: {
95
        // Consume 1 byte more from block size from the raw data for this iteration
96
262
        chunk_len = std::min(remaining, block_size + 1);
97
262
        break;
98
0
      }
99
913
      case SMALL: {
100
        // Consume 1~32 bytes from the raw data for this iteration
101
913
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
102
913
        chunk_len = std::min(remaining, small_len);
103
913
        break;
104
0
      }
105
694
      case RANDOM: {
106
        // Consume random bytes from the raw data for this iteration
107
694
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
108
694
        break;
109
0
      }
110
120
      case TAIL: {
111
        // Consume all remaining bytes from the raw data for this iteration
112
120
        chunk_len = remaining;
113
120
        break;
114
0
      }
115
971
      case HALT: {
116
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
117
971
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
118
971
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
119
2.53k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
120
1.56k
          size_t w = std::min(remaining, step);
121
1.56k
          update_fn(&contexts[ctx_index], w, cursor);
122
1.56k
          cursor += w;
123
1.56k
          remaining -= w;
124
1.56k
        }
125
126
        // Randomly reinitialise the hash stream
127
971
        if (fdp.ConsumeBool()) {
128
393
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
129
393
        }
130
971
        continue;
131
0
      }
132
4.03k
    }
133
134
3.05k
    if (chunk_len == 0 || chunk_len > remaining) {
135
0
      continue;
136
0
    }
137
138
    // Fuzz the update function
139
3.05k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
140
3.05k
    cursor += chunk_len;
141
3.05k
    remaining -= chunk_len;
142
3.05k
  }
143
144
  // Finalize all active contexts (finish_reset).
145
7.82k
  for (unsigned i = 0; i < num_contexts; i++) {
146
5.32k
    finish_fn(&contexts[i], digests[i].data());
147
5.32k
  }
148
149
  // Additional fuzzing on special context chaining approach.
150
2.50k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
151
752
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
152
752
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
153
752
    if (src_idx != dst_idx) {
154
419
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
155
419
      size_t max_avail = digest_size - offset; // >= 1
156
419
      size_t feed_len = fdp.ConsumeIntegralInRange<size_t>(1, max_avail);
157
419
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
158
419
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
159
419
    }
160
752
  }
161
162
  // Deinitialise all contexts after this iteration
163
7.82k
  for (unsigned i = 0; i < num_contexts; i++) {
164
5.32k
    deinit_fn(&contexts[i]);
165
5.32k
  }
166
2.50k
}
fuzz_crypto_ext.cpp:void fuzz_hash_ext_multi<mhd_Md5CtxGtls>(FuzzedDataProvider&, unsigned long, void (*)(mhd_Md5CtxGtls*), void (*)(mhd_Md5CtxGtls*, unsigned long, void const*), void (*)(mhd_Md5CtxGtls*, unsigned char*), void (*)(mhd_Md5CtxGtls*), unsigned long)
Line
Count
Source
42
1.35k
                                size_t digest_size) {
43
1.35k
  if (!fdp.remaining_bytes()) {
44
5
    return;
45
5
  }
46
47
  // Pull a random slice of data for fuzzing
48
1.35k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
49
1.35k
  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.35k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
53
1.35k
  std::vector<HashType> contexts(num_contexts);
54
1.35k
  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.09k
    init_once(&contexts[i]);
57
3.09k
  }
58
59
  // Intentionally misalign the data pointer to stress alignment sensitive paths
60
1.35k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
61
1.35k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
62
1.35k
  if (!input_bytes.empty()) {
63
1.04k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
64
1.04k
  }
65
66
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
67
1.35k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
68
1.35k
  size_t remaining = input_bytes.size();
69
70
  // Perform multiple hash update iterations on the raw data
71
1.35k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
72
3.68k
  while (num_iterations-- && remaining > 0) {
73
    // Pick which context to feed this iteration
74
2.33k
    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.33k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
78
2.33k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
79
80
2.33k
    size_t chunk_len = 0;
81
2.33k
    switch (pattern) {
82
468
      case LESS1: {
83
        // Consume 1 byte less from block size from the raw data for this iteration
84
468
        if (block_size > 1) {
85
468
          chunk_len = std::min(remaining, block_size - 1);
86
468
        }
87
468
        break;
88
0
      }
89
131
      case EQ: {
90
        // Consume block size bytes from the raw data for this iteration
91
131
        chunk_len = std::min(remaining, block_size);
92
131
        break;
93
0
      }
94
164
      case PLUS1: {
95
        // Consume 1 byte more from block size from the raw data for this iteration
96
164
        chunk_len = std::min(remaining, block_size + 1);
97
164
        break;
98
0
      }
99
649
      case SMALL: {
100
        // Consume 1~32 bytes from the raw data for this iteration
101
649
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
102
649
        chunk_len = std::min(remaining, small_len);
103
649
        break;
104
0
      }
105
382
      case RANDOM: {
106
        // Consume random bytes from the raw data for this iteration
107
382
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
108
382
        break;
109
0
      }
110
58
      case TAIL: {
111
        // Consume all remaining bytes from the raw data for this iteration
112
58
        chunk_len = remaining;
113
58
        break;
114
0
      }
115
483
      case HALT: {
116
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
117
483
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
118
483
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
119
1.27k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
120
788
          size_t w = std::min(remaining, step);
121
788
          update_fn(&contexts[ctx_index], w, cursor);
122
788
          cursor += w;
123
788
          remaining -= w;
124
788
        }
125
126
        // Randomly reinitialise the hash stream
127
483
        if (fdp.ConsumeBool()) {
128
197
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
129
197
        }
130
483
        continue;
131
0
      }
132
2.33k
    }
133
134
1.85k
    if (chunk_len == 0 || chunk_len > remaining) {
135
0
      continue;
136
0
    }
137
138
    // Fuzz the update function
139
1.85k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
140
1.85k
    cursor += chunk_len;
141
1.85k
    remaining -= chunk_len;
142
1.85k
  }
143
144
  // Finalize all active contexts (finish_reset).
145
4.44k
  for (unsigned i = 0; i < num_contexts; i++) {
146
3.09k
    finish_fn(&contexts[i], digests[i].data());
147
3.09k
  }
148
149
  // Additional fuzzing on special context chaining approach.
150
1.35k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
151
468
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
152
468
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
153
468
    if (src_idx != dst_idx) {
154
223
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
155
223
      size_t max_avail = digest_size - offset; // >= 1
156
223
      size_t feed_len = fdp.ConsumeIntegralInRange<size_t>(1, max_avail);
157
223
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
158
223
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
159
223
    }
160
468
  }
161
162
  // Deinitialise all contexts after this iteration
163
4.44k
  for (unsigned i = 0; i < num_contexts; i++) {
164
3.09k
    deinit_fn(&contexts[i]);
165
3.09k
  }
166
1.35k
}
fuzz_crypto_ext.cpp:void fuzz_hash_ext_multi<mhd_Sha256CtxGtls>(FuzzedDataProvider&, unsigned long, void (*)(mhd_Sha256CtxGtls*), void (*)(mhd_Sha256CtxGtls*, unsigned long, void const*), void (*)(mhd_Sha256CtxGtls*, unsigned char*), void (*)(mhd_Sha256CtxGtls*), unsigned long)
Line
Count
Source
42
1.17k
                                size_t digest_size) {
43
1.17k
  if (!fdp.remaining_bytes()) {
44
20
    return;
45
20
  }
46
47
  // Pull a random slice of data for fuzzing
48
1.15k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
49
1.15k
  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.15k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
53
1.15k
  std::vector<HashType> contexts(num_contexts);
54
1.15k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
55
3.38k
  for (unsigned i = 0; i < num_contexts; i++) {
56
2.22k
    init_once(&contexts[i]);
57
2.22k
  }
58
59
  // Intentionally misalign the data pointer to stress alignment sensitive paths
60
1.15k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
61
1.15k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
62
1.15k
  if (!input_bytes.empty()) {
63
808
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
64
808
  }
65
66
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
67
1.15k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
68
1.15k
  size_t remaining = input_bytes.size();
69
70
  // Perform multiple hash update iterations on the raw data
71
1.15k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
72
2.85k
  while (num_iterations-- && remaining > 0) {
73
    // Pick which context to feed this iteration
74
1.69k
    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.69k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
78
1.69k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
79
80
1.69k
    size_t chunk_len = 0;
81
1.69k
    switch (pattern) {
82
384
      case LESS1: {
83
        // Consume 1 byte less from block size from the raw data for this iteration
84
384
        if (block_size > 1) {
85
384
          chunk_len = std::min(remaining, block_size - 1);
86
384
        }
87
384
        break;
88
0
      }
89
87
      case EQ: {
90
        // Consume block size bytes from the raw data for this iteration
91
87
        chunk_len = std::min(remaining, block_size);
92
87
        break;
93
0
      }
94
98
      case PLUS1: {
95
        // Consume 1 byte more from block size from the raw data for this iteration
96
98
        chunk_len = std::min(remaining, block_size + 1);
97
98
        break;
98
0
      }
99
264
      case SMALL: {
100
        // Consume 1~32 bytes from the raw data for this iteration
101
264
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
102
264
        chunk_len = std::min(remaining, small_len);
103
264
        break;
104
0
      }
105
312
      case RANDOM: {
106
        // Consume random bytes from the raw data for this iteration
107
312
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
108
312
        break;
109
0
      }
110
62
      case TAIL: {
111
        // Consume all remaining bytes from the raw data for this iteration
112
62
        chunk_len = remaining;
113
62
        break;
114
0
      }
115
488
      case HALT: {
116
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
117
488
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
118
488
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
119
1.26k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
120
777
          size_t w = std::min(remaining, step);
121
777
          update_fn(&contexts[ctx_index], w, cursor);
122
777
          cursor += w;
123
777
          remaining -= w;
124
777
        }
125
126
        // Randomly reinitialise the hash stream
127
488
        if (fdp.ConsumeBool()) {
128
196
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
129
196
        }
130
488
        continue;
131
0
      }
132
1.69k
    }
133
134
1.20k
    if (chunk_len == 0 || chunk_len > remaining) {
135
0
      continue;
136
0
    }
137
138
    // Fuzz the update function
139
1.20k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
140
1.20k
    cursor += chunk_len;
141
1.20k
    remaining -= chunk_len;
142
1.20k
  }
143
144
  // Finalize all active contexts (finish_reset).
145
3.38k
  for (unsigned i = 0; i < num_contexts; i++) {
146
2.22k
    finish_fn(&contexts[i], digests[i].data());
147
2.22k
  }
148
149
  // Additional fuzzing on special context chaining approach.
150
1.15k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
151
284
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
152
284
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
153
284
    if (src_idx != dst_idx) {
154
196
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
155
196
      size_t max_avail = digest_size - offset; // >= 1
156
196
      size_t feed_len = fdp.ConsumeIntegralInRange<size_t>(1, max_avail);
157
196
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
158
196
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
159
196
    }
160
284
  }
161
162
  // Deinitialise all contexts after this iteration
163
3.38k
  for (unsigned i = 0; i < num_contexts; i++) {
164
2.22k
    deinit_fn(&contexts[i]);
165
2.22k
  }
166
1.15k
}
167
168
1.34k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
169
1.34k
  FuzzedDataProvider fdp(data, size);
170
171
3.87k
  for (unsigned i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) {
172
2.53k
    if (fdp.ConsumeBool()) {
173
1.35k
      fuzz_hash_ext_multi<struct mhd_Md5CtxGtls>(
174
1.35k
        fdp, 64,
175
1.35k
        mhd_MD5_gtls_init, mhd_MD5_gtls_update, mhd_MD5_gtls_finish, mhd_MD5_gtls_deinit,
176
1.35k
        mhd_MD5_DIGEST_SIZE);
177
1.35k
    } else {
178
1.17k
      fuzz_hash_ext_multi<struct mhd_Sha256CtxGtls>(
179
1.17k
        fdp, 64,
180
1.17k
        mhd_SHA256_gtls_init, mhd_SHA256_gtls_update, mhd_SHA256_gtls_finish, mhd_SHA256_gtls_deinit,
181
1.17k
        mhd_SHA256_DIGEST_SIZE);
182
1.17k
    }
183
2.53k
  }
184
1.34k
  return 0;
185
1.34k
}