Coverage Report

Created: 2026-07-16 07:09

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
4.07k
                                size_t digest_size) {
42
4.07k
  if (!fdp.remaining_bytes()) {
43
28
    return;
44
28
  }
45
46
  // Pull a random slice of data for fuzzing
47
4.04k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
4.04k
  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
4.04k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
4.04k
  std::vector<HashType> contexts(num_contexts);
53
4.04k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
12.6k
  for (unsigned i = 0; i < num_contexts; i++) {
55
8.58k
    init_fn(&contexts[i]);
56
8.58k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
4.04k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
4.04k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
4.04k
  if (!input_bytes.empty()) {
62
3.11k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
3.11k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
4.04k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
4.04k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
4.04k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
11.0k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
6.99k
    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.99k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
6.99k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
6.99k
    size_t chunk_len = 0;
80
6.99k
    switch (pattern) {
81
1.51k
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
1.51k
        if (block_size > 1) {
84
1.51k
          chunk_len = std::min(remaining, block_size - 1);
85
1.51k
        }
86
1.51k
        break;
87
0
      }
88
382
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
382
        chunk_len = std::min(remaining, block_size);
91
382
        break;
92
0
      }
93
451
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
451
        chunk_len = std::min(remaining, block_size + 1);
96
451
        break;
97
0
      }
98
1.25k
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
1.25k
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
1.25k
        chunk_len = std::min(remaining, small_len);
102
1.25k
        break;
103
0
      }
104
1.37k
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
1.37k
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
1.37k
        break;
108
0
      }
109
387
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
387
        chunk_len = remaining;
112
387
        break;
113
0
      }
114
1.63k
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
1.63k
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
1.63k
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
4.82k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
3.19k
          size_t w = std::min(remaining, step);
120
3.19k
          update_fn(&contexts[ctx_index], w, cursor);
121
3.19k
          cursor += w;
122
3.19k
          remaining -= w;
123
3.19k
        }
124
125
        // Randomly reinitialise the hash stream
126
1.63k
        if (fdp.ConsumeBool()) {
127
727
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
727
          init_fn(&contexts[ctx_index]);
129
727
        }
130
1.63k
        continue;
131
0
      }
132
6.99k
    }
133
5.35k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
5.35k
    if (fdp.ConsumeBool()) {
139
2.15k
      init_fn(&contexts[ctx_index]);
140
2.15k
    }
141
142
    // Fuzz the update function
143
5.35k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
5.35k
    cursor += chunk_len;
145
5.35k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
5.35k
    if (fdp.ConsumeBool()) {
149
2.08k
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
2.08k
      init_fn(&contexts[ctx_index]);
151
2.08k
    }
152
5.35k
  }
153
154
  // Fuzz the finish function for all contexts
155
12.6k
  for (unsigned i = 0; i < num_contexts; i++) {
156
8.58k
    finish_fn(&contexts[i], digests[i].data());
157
8.58k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
4.04k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
1.07k
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
1.07k
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
1.07k
    if (src_idx != dst_idx) {
164
613
      init_fn(&contexts[dst_idx]);
165
613
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
613
      size_t feed_len = std::min(digest_size - offset,
167
613
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
613
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
613
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
613
    }
171
1.07k
  }
172
4.04k
}
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.54k
                                size_t digest_size) {
42
1.54k
  if (!fdp.remaining_bytes()) {
43
17
    return;
44
17
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.53k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.53k
  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.53k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.53k
  std::vector<HashType> contexts(num_contexts);
53
1.53k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
4.86k
  for (unsigned i = 0; i < num_contexts; i++) {
55
3.33k
    init_fn(&contexts[i]);
56
3.33k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.53k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.53k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.53k
  if (!input_bytes.empty()) {
62
1.20k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
1.20k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.53k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.53k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.53k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
4.24k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.71k
    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.71k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.71k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.71k
    size_t chunk_len = 0;
80
2.71k
    switch (pattern) {
81
564
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
564
        if (block_size > 1) {
84
564
          chunk_len = std::min(remaining, block_size - 1);
85
564
        }
86
564
        break;
87
0
      }
88
151
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
151
        chunk_len = std::min(remaining, block_size);
91
151
        break;
92
0
      }
93
227
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
227
        chunk_len = std::min(remaining, block_size + 1);
96
227
        break;
97
0
      }
98
503
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
503
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
503
        chunk_len = std::min(remaining, small_len);
102
503
        break;
103
0
      }
104
549
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
549
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
549
        break;
108
0
      }
109
133
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
133
        chunk_len = remaining;
112
133
        break;
113
0
      }
114
588
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
588
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
588
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.72k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.13k
          size_t w = std::min(remaining, step);
120
1.13k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.13k
          cursor += w;
122
1.13k
          remaining -= w;
123
1.13k
        }
124
125
        // Randomly reinitialise the hash stream
126
588
        if (fdp.ConsumeBool()) {
127
231
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
231
          init_fn(&contexts[ctx_index]);
129
231
        }
130
588
        continue;
131
0
      }
132
2.71k
    }
133
2.12k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
2.12k
    if (fdp.ConsumeBool()) {
139
866
      init_fn(&contexts[ctx_index]);
140
866
    }
141
142
    // Fuzz the update function
143
2.12k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
2.12k
    cursor += chunk_len;
145
2.12k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
2.12k
    if (fdp.ConsumeBool()) {
149
871
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
871
      init_fn(&contexts[ctx_index]);
151
871
    }
152
2.12k
  }
153
154
  // Fuzz the finish function for all contexts
155
4.86k
  for (unsigned i = 0; i < num_contexts; i++) {
156
3.33k
    finish_fn(&contexts[i], digests[i].data());
157
3.33k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.53k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
413
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
413
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
413
    if (src_idx != dst_idx) {
164
210
      init_fn(&contexts[dst_idx]);
165
210
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
210
      size_t feed_len = std::min(digest_size - offset,
167
210
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
210
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
210
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
210
    }
171
413
  }
172
1.53k
}
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.25k
                                size_t digest_size) {
42
1.25k
  if (!fdp.remaining_bytes()) {
43
5
    return;
44
5
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.25k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.25k
  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.25k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.25k
  std::vector<HashType> contexts(num_contexts);
53
1.25k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
3.82k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.57k
    init_fn(&contexts[i]);
56
2.57k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.25k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.25k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.25k
  if (!input_bytes.empty()) {
62
930
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
930
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.25k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.25k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.25k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.32k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.07k
    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.07k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.07k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.07k
    size_t chunk_len = 0;
80
2.07k
    switch (pattern) {
81
474
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
474
        if (block_size > 1) {
84
474
          chunk_len = std::min(remaining, block_size - 1);
85
474
        }
86
474
        break;
87
0
      }
88
107
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
107
        chunk_len = std::min(remaining, block_size);
91
107
        break;
92
0
      }
93
105
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
105
        chunk_len = std::min(remaining, block_size + 1);
96
105
        break;
97
0
      }
98
369
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
369
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
369
        chunk_len = std::min(remaining, small_len);
102
369
        break;
103
0
      }
104
391
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
391
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
391
        break;
108
0
      }
109
116
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
116
        chunk_len = remaining;
112
116
        break;
113
0
      }
114
510
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
510
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
510
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.47k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
966
          size_t w = std::min(remaining, step);
120
966
          update_fn(&contexts[ctx_index], w, cursor);
121
966
          cursor += w;
122
966
          remaining -= w;
123
966
        }
124
125
        // Randomly reinitialise the hash stream
126
510
        if (fdp.ConsumeBool()) {
127
209
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
209
          init_fn(&contexts[ctx_index]);
129
209
        }
130
510
        continue;
131
0
      }
132
2.07k
    }
133
1.56k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.56k
    if (fdp.ConsumeBool()) {
139
645
      init_fn(&contexts[ctx_index]);
140
645
    }
141
142
    // Fuzz the update function
143
1.56k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.56k
    cursor += chunk_len;
145
1.56k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.56k
    if (fdp.ConsumeBool()) {
149
542
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
542
      init_fn(&contexts[ctx_index]);
151
542
    }
152
1.56k
  }
153
154
  // Fuzz the finish function for all contexts
155
3.82k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.57k
    finish_fn(&contexts[i], digests[i].data());
157
2.57k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.25k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
328
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
328
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
328
    if (src_idx != dst_idx) {
164
202
      init_fn(&contexts[dst_idx]);
165
202
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
202
      size_t feed_len = std::min(digest_size - offset,
167
202
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
202
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
202
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
202
    }
171
328
  }
172
1.25k
}
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.27k
                                size_t digest_size) {
42
1.27k
  if (!fdp.remaining_bytes()) {
43
6
    return;
44
6
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.26k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.26k
  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.26k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.26k
  std::vector<HashType> contexts(num_contexts);
53
1.26k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
3.94k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.67k
    init_fn(&contexts[i]);
56
2.67k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.26k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.26k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.26k
  if (!input_bytes.empty()) {
62
981
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
981
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.26k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.26k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.26k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.47k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.20k
    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.20k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.20k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.20k
    size_t chunk_len = 0;
80
2.20k
    switch (pattern) {
81
475
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
475
        if (block_size > 1) {
84
475
          chunk_len = std::min(remaining, block_size - 1);
85
475
        }
86
475
        break;
87
0
      }
88
124
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
124
        chunk_len = std::min(remaining, block_size);
91
124
        break;
92
0
      }
93
119
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
119
        chunk_len = std::min(remaining, block_size + 1);
96
119
        break;
97
0
      }
98
378
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
378
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
378
        chunk_len = std::min(remaining, small_len);
102
378
        break;
103
0
      }
104
433
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
433
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
433
        break;
108
0
      }
109
138
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
138
        chunk_len = remaining;
112
138
        break;
113
0
      }
114
540
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
540
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
540
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.62k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.08k
          size_t w = std::min(remaining, step);
120
1.08k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.08k
          cursor += w;
122
1.08k
          remaining -= w;
123
1.08k
        }
124
125
        // Randomly reinitialise the hash stream
126
540
        if (fdp.ConsumeBool()) {
127
287
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
287
          init_fn(&contexts[ctx_index]);
129
287
        }
130
540
        continue;
131
0
      }
132
2.20k
    }
133
1.66k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.66k
    if (fdp.ConsumeBool()) {
139
641
      init_fn(&contexts[ctx_index]);
140
641
    }
141
142
    // Fuzz the update function
143
1.66k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.66k
    cursor += chunk_len;
145
1.66k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.66k
    if (fdp.ConsumeBool()) {
149
670
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
670
      init_fn(&contexts[ctx_index]);
151
670
    }
152
1.66k
  }
153
154
  // Fuzz the finish function for all contexts
155
3.94k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.67k
    finish_fn(&contexts[i], digests[i].data());
157
2.67k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.26k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
337
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
337
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
337
    if (src_idx != dst_idx) {
164
201
      init_fn(&contexts[dst_idx]);
165
201
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
201
      size_t feed_len = std::min(digest_size - offset,
167
201
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
201
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
201
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
201
    }
171
337
  }
172
1.26k
}
173
174
2.17k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
175
2.17k
  FuzzedDataProvider fdp(data, size);
176
177
6.25k
  for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) {
178
4.07k
    switch (fdp.ConsumeIntegralInRange<int>(0, 2)) {
179
1.54k
      case 0:
180
1.54k
        fuzz_hash_int_multi<struct mhd_Md5CtxInt>(
181
1.54k
          fdp, mhd_MD5_BLOCK_SIZE,
182
1.54k
          mhd_MD5_init, mhd_MD5_update, mhd_MD5_finish, mhd_MD5_DIGEST_SIZE);
183
1.54k
        break;
184
1.25k
      case 1:
185
1.25k
        fuzz_hash_int_multi<struct mhd_Sha256CtxInt>(
186
1.25k
          fdp, mhd_SHA256_BLOCK_SIZE,
187
1.25k
          mhd_SHA256_init, mhd_SHA256_update, mhd_SHA256_finish, mhd_SHA256_DIGEST_SIZE);
188
1.25k
        break;
189
1.27k
      case 2:
190
1.27k
      default:
191
1.27k
        fuzz_hash_int_multi<struct mhd_Sha512_256CtxInt>(
192
1.27k
          fdp, mhd_SHA512_256_BLOCK_SIZE,
193
1.27k
          mhd_SHA512_256_init, mhd_SHA512_256_update, mhd_SHA512_256_finish, mhd_SHA512_256_DIGEST_SIZE);
194
1.27k
        break;
195
4.07k
    }
196
4.07k
  }
197
2.17k
  return 0;
198
2.17k
}