Coverage Report

Created: 2026-03-12 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fuzz_crypto_int.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_int.h"
25
#include "sha256_int.h"
26
#include "sha512_256_int.h"
27
28
29
// Fuzzing target function pointer types for the internal hash APIs
30
template <typename HashType> using InitFn   = void (*)(HashType*);
31
template <typename HashType> using UpdateFn = void (*)(HashType*, size_t, const uint8_t*);
32
template <typename HashType> using FinishFn = void (*)(HashType*, uint8_t*);
33
34
// Generic hashing flow that fuzz same hashing procedure for different algorithm
35
template <typename HashType>
36
static void fuzz_hash_int_multi(FuzzedDataProvider &fdp,
37
                                size_t block_size,
38
                                InitFn<HashType> init_fn,
39
                                UpdateFn<HashType> update_fn,
40
                                FinishFn<HashType> finish_fn,
41
3.82k
                                size_t digest_size) {
42
3.82k
  if (!fdp.remaining_bytes()) {
43
31
    return;
44
31
  }
45
46
  // Pull a random slice of data for fuzzing
47
3.79k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
3.79k
  std::vector<uint8_t> input_bytes = fdp.ConsumeBytes<uint8_t>(take_len);
49
50
  // Create 1 to 4 independent hashing contexts with it own digest buffer
51
3.79k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
3.79k
  std::vector<HashType> contexts(num_contexts);
53
3.79k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
11.9k
  for (unsigned i = 0; i < num_contexts; i++) {
55
8.15k
    init_fn(&contexts[i]);
56
8.15k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
3.79k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
3.79k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
3.79k
  if (!input_bytes.empty()) {
62
2.89k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
2.89k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
3.79k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
3.79k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
3.79k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
10.2k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
6.46k
    const unsigned ctx_index = (num_contexts == 1) ? 0 : fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
74
75
    // Choose a chunking pattern for this iteration
76
6.46k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
6.46k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
6.46k
    size_t chunk_len = 0;
80
6.46k
    switch (pattern) {
81
1.48k
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
1.48k
        if (block_size > 1) {
84
1.48k
          chunk_len = std::min(remaining, block_size - 1);
85
1.48k
        }
86
1.48k
        break;
87
0
      }
88
361
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
361
        chunk_len = std::min(remaining, block_size);
91
361
        break;
92
0
      }
93
347
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
347
        chunk_len = std::min(remaining, block_size + 1);
96
347
        break;
97
0
      }
98
1.21k
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
1.21k
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
1.21k
        chunk_len = std::min(remaining, small_len);
102
1.21k
        break;
103
0
      }
104
1.24k
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
1.24k
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
1.24k
        break;
108
0
      }
109
336
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
336
        chunk_len = remaining;
112
336
        break;
113
0
      }
114
1.47k
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
1.47k
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
1.47k
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
4.44k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
2.96k
          size_t w = std::min(remaining, step);
120
2.96k
          update_fn(&contexts[ctx_index], w, cursor);
121
2.96k
          cursor += w;
122
2.96k
          remaining -= w;
123
2.96k
        }
124
125
        // Randomly reinitialise the hash stream
126
1.47k
        if (fdp.ConsumeBool()) {
127
600
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
600
          init_fn(&contexts[ctx_index]);
129
600
        }
130
1.47k
        continue;
131
0
      }
132
6.46k
    }
133
4.98k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
4.98k
    if (fdp.ConsumeBool()) {
139
2.11k
      init_fn(&contexts[ctx_index]);
140
2.11k
    }
141
142
    // Fuzz the update function
143
4.98k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
4.98k
    cursor += chunk_len;
145
4.98k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
4.98k
    if (fdp.ConsumeBool()) {
149
2.10k
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
2.10k
      init_fn(&contexts[ctx_index]);
151
2.10k
    }
152
4.98k
  }
153
154
  // Fuzz the finish function for all contexts
155
11.9k
  for (unsigned i = 0; i < num_contexts; i++) {
156
8.15k
    finish_fn(&contexts[i], digests[i].data());
157
8.15k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
3.79k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
1.10k
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
1.10k
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
1.10k
    if (src_idx != dst_idx) {
164
612
      init_fn(&contexts[dst_idx]);
165
612
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
612
      size_t feed_len = std::min(digest_size - offset,
167
612
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
612
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
612
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
612
    }
171
1.10k
  }
172
3.79k
}
fuzz_crypto_int.cpp:void fuzz_hash_int_multi<mhd_Md5CtxInt>(FuzzedDataProvider&, unsigned long, void (*)(mhd_Md5CtxInt*), void (*)(mhd_Md5CtxInt*, unsigned long, unsigned char const*), void (*)(mhd_Md5CtxInt*, unsigned char*), unsigned long)
Line
Count
Source
41
1.42k
                                size_t digest_size) {
42
1.42k
  if (!fdp.remaining_bytes()) {
43
18
    return;
44
18
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.40k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.40k
  std::vector<uint8_t> input_bytes = fdp.ConsumeBytes<uint8_t>(take_len);
49
50
  // Create 1 to 4 independent hashing contexts with it own digest buffer
51
1.40k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.40k
  std::vector<HashType> contexts(num_contexts);
53
1.40k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
4.51k
  for (unsigned i = 0; i < num_contexts; i++) {
55
3.10k
    init_fn(&contexts[i]);
56
3.10k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.40k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.40k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.40k
  if (!input_bytes.empty()) {
62
1.07k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
1.07k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.40k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.40k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.40k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.83k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.42k
    const unsigned ctx_index = (num_contexts == 1) ? 0 : fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
74
75
    // Choose a chunking pattern for this iteration
76
2.42k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.42k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.42k
    size_t chunk_len = 0;
80
2.42k
    switch (pattern) {
81
525
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
525
        if (block_size > 1) {
84
525
          chunk_len = std::min(remaining, block_size - 1);
85
525
        }
86
525
        break;
87
0
      }
88
136
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
136
        chunk_len = std::min(remaining, block_size);
91
136
        break;
92
0
      }
93
177
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
177
        chunk_len = std::min(remaining, block_size + 1);
96
177
        break;
97
0
      }
98
512
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
512
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
512
        chunk_len = std::min(remaining, small_len);
102
512
        break;
103
0
      }
104
453
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
453
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
453
        break;
108
0
      }
109
108
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
108
        chunk_len = remaining;
112
108
        break;
113
0
      }
114
515
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
515
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
515
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.56k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.04k
          size_t w = std::min(remaining, step);
120
1.04k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.04k
          cursor += w;
122
1.04k
          remaining -= w;
123
1.04k
        }
124
125
        // Randomly reinitialise the hash stream
126
515
        if (fdp.ConsumeBool()) {
127
215
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
215
          init_fn(&contexts[ctx_index]);
129
215
        }
130
515
        continue;
131
0
      }
132
2.42k
    }
133
1.91k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.91k
    if (fdp.ConsumeBool()) {
139
837
      init_fn(&contexts[ctx_index]);
140
837
    }
141
142
    // Fuzz the update function
143
1.91k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.91k
    cursor += chunk_len;
145
1.91k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.91k
    if (fdp.ConsumeBool()) {
149
866
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
866
      init_fn(&contexts[ctx_index]);
151
866
    }
152
1.91k
  }
153
154
  // Fuzz the finish function for all contexts
155
4.51k
  for (unsigned i = 0; i < num_contexts; i++) {
156
3.10k
    finish_fn(&contexts[i], digests[i].data());
157
3.10k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.40k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
427
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
427
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
427
    if (src_idx != dst_idx) {
164
221
      init_fn(&contexts[dst_idx]);
165
221
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
221
      size_t feed_len = std::min(digest_size - offset,
167
221
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
221
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
221
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
221
    }
171
427
  }
172
1.40k
}
fuzz_crypto_int.cpp:void fuzz_hash_int_multi<mhd_Sha256CtxInt>(FuzzedDataProvider&, unsigned long, void (*)(mhd_Sha256CtxInt*), void (*)(mhd_Sha256CtxInt*, unsigned long, unsigned char const*), void (*)(mhd_Sha256CtxInt*, unsigned char*), unsigned long)
Line
Count
Source
41
1.22k
                                size_t digest_size) {
42
1.22k
  if (!fdp.remaining_bytes()) {
43
10
    return;
44
10
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.21k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.21k
  std::vector<uint8_t> input_bytes = fdp.ConsumeBytes<uint8_t>(take_len);
49
50
  // Create 1 to 4 independent hashing contexts with it own digest buffer
51
1.21k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.21k
  std::vector<HashType> contexts(num_contexts);
53
1.21k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
3.76k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.54k
    init_fn(&contexts[i]);
56
2.54k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.21k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.21k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.21k
  if (!input_bytes.empty()) {
62
923
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
923
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.21k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.21k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.21k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.25k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.04k
    const unsigned ctx_index = (num_contexts == 1) ? 0 : fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
74
75
    // Choose a chunking pattern for this iteration
76
2.04k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.04k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.04k
    size_t chunk_len = 0;
80
2.04k
    switch (pattern) {
81
489
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
489
        if (block_size > 1) {
84
489
          chunk_len = std::min(remaining, block_size - 1);
85
489
        }
86
489
        break;
87
0
      }
88
112
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
112
        chunk_len = std::min(remaining, block_size);
91
112
        break;
92
0
      }
93
80
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
80
        chunk_len = std::min(remaining, block_size + 1);
96
80
        break;
97
0
      }
98
362
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
362
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
362
        chunk_len = std::min(remaining, small_len);
102
362
        break;
103
0
      }
104
414
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
414
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
414
        break;
108
0
      }
109
115
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
115
        chunk_len = remaining;
112
115
        break;
113
0
      }
114
468
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
468
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
468
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.37k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
911
          size_t w = std::min(remaining, step);
120
911
          update_fn(&contexts[ctx_index], w, cursor);
121
911
          cursor += w;
122
911
          remaining -= w;
123
911
        }
124
125
        // Randomly reinitialise the hash stream
126
468
        if (fdp.ConsumeBool()) {
127
178
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
178
          init_fn(&contexts[ctx_index]);
129
178
        }
130
468
        continue;
131
0
      }
132
2.04k
    }
133
1.57k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.57k
    if (fdp.ConsumeBool()) {
139
636
      init_fn(&contexts[ctx_index]);
140
636
    }
141
142
    // Fuzz the update function
143
1.57k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.57k
    cursor += chunk_len;
145
1.57k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.57k
    if (fdp.ConsumeBool()) {
149
622
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
622
      init_fn(&contexts[ctx_index]);
151
622
    }
152
1.57k
  }
153
154
  // Fuzz the finish function for all contexts
155
3.76k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.54k
    finish_fn(&contexts[i], digests[i].data());
157
2.54k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.21k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
350
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
350
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
350
    if (src_idx != dst_idx) {
164
214
      init_fn(&contexts[dst_idx]);
165
214
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
214
      size_t feed_len = std::min(digest_size - offset,
167
214
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
214
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
214
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
214
    }
171
350
  }
172
1.21k
}
fuzz_crypto_int.cpp:void fuzz_hash_int_multi<mhd_Sha512_256CtxInt>(FuzzedDataProvider&, unsigned long, void (*)(mhd_Sha512_256CtxInt*), void (*)(mhd_Sha512_256CtxInt*, unsigned long, unsigned char const*), void (*)(mhd_Sha512_256CtxInt*, unsigned char*), unsigned long)
Line
Count
Source
41
1.17k
                                size_t digest_size) {
42
1.17k
  if (!fdp.remaining_bytes()) {
43
3
    return;
44
3
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.16k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.16k
  std::vector<uint8_t> input_bytes = fdp.ConsumeBytes<uint8_t>(take_len);
49
50
  // Create 1 to 4 independent hashing contexts with it own digest buffer
51
1.16k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.16k
  std::vector<HashType> contexts(num_contexts);
53
1.16k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
3.66k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.49k
    init_fn(&contexts[i]);
56
2.49k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.16k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.16k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.16k
  if (!input_bytes.empty()) {
62
893
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
893
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.16k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.16k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.16k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.16k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.00k
    const unsigned ctx_index = (num_contexts == 1) ? 0 : fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
74
75
    // Choose a chunking pattern for this iteration
76
2.00k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.00k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.00k
    size_t chunk_len = 0;
80
2.00k
    switch (pattern) {
81
472
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
472
        if (block_size > 1) {
84
472
          chunk_len = std::min(remaining, block_size - 1);
85
472
        }
86
472
        break;
87
0
      }
88
113
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
113
        chunk_len = std::min(remaining, block_size);
91
113
        break;
92
0
      }
93
90
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
90
        chunk_len = std::min(remaining, block_size + 1);
96
90
        break;
97
0
      }
98
336
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
336
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
336
        chunk_len = std::min(remaining, small_len);
102
336
        break;
103
0
      }
104
382
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
382
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
382
        break;
108
0
      }
109
113
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
113
        chunk_len = remaining;
112
113
        break;
113
0
      }
114
494
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
494
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
494
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.50k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.00k
          size_t w = std::min(remaining, step);
120
1.00k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.00k
          cursor += w;
122
1.00k
          remaining -= w;
123
1.00k
        }
124
125
        // Randomly reinitialise the hash stream
126
494
        if (fdp.ConsumeBool()) {
127
207
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
207
          init_fn(&contexts[ctx_index]);
129
207
        }
130
494
        continue;
131
0
      }
132
2.00k
    }
133
1.50k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.50k
    if (fdp.ConsumeBool()) {
139
645
      init_fn(&contexts[ctx_index]);
140
645
    }
141
142
    // Fuzz the update function
143
1.50k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.50k
    cursor += chunk_len;
145
1.50k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.50k
    if (fdp.ConsumeBool()) {
149
612
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
612
      init_fn(&contexts[ctx_index]);
151
612
    }
152
1.50k
  }
153
154
  // Fuzz the finish function for all contexts
155
3.66k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.49k
    finish_fn(&contexts[i], digests[i].data());
157
2.49k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.16k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
324
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
324
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
324
    if (src_idx != dst_idx) {
164
177
      init_fn(&contexts[dst_idx]);
165
177
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
177
      size_t feed_len = std::min(digest_size - offset,
167
177
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
177
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
177
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
177
    }
171
324
  }
172
1.16k
}
173
174
2.10k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
175
2.10k
  FuzzedDataProvider fdp(data, size);
176
177
5.93k
  for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) {
178
3.82k
    switch (fdp.ConsumeIntegralInRange<int>(0, 2)) {
179
1.42k
      case 0:
180
1.42k
        fuzz_hash_int_multi<struct mhd_Md5CtxInt>(
181
1.42k
          fdp, mhd_MD5_BLOCK_SIZE,
182
1.42k
          mhd_MD5_init, mhd_MD5_update, mhd_MD5_finish, mhd_MD5_DIGEST_SIZE);
183
1.42k
        break;
184
1.22k
      case 1:
185
1.22k
        fuzz_hash_int_multi<struct mhd_Sha256CtxInt>(
186
1.22k
          fdp, mhd_SHA256_BLOCK_SIZE,
187
1.22k
          mhd_SHA256_init, mhd_SHA256_update, mhd_SHA256_finish, mhd_SHA256_DIGEST_SIZE);
188
1.22k
        break;
189
1.17k
      case 2:
190
1.17k
      default:
191
1.17k
        fuzz_hash_int_multi<struct mhd_Sha512_256CtxInt>(
192
1.17k
          fdp, mhd_SHA512_256_BLOCK_SIZE,
193
1.17k
          mhd_SHA512_256_init, mhd_SHA512_256_update, mhd_SHA512_256_finish, mhd_SHA512_256_DIGEST_SIZE);
194
1.17k
        break;
195
3.82k
    }
196
3.82k
  }
197
2.10k
  return 0;
198
2.10k
}