Coverage Report

Created: 2026-03-08 06:25

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.96k
                                size_t digest_size) {
42
3.96k
  if (!fdp.remaining_bytes()) {
43
30
    return;
44
30
  }
45
46
  // Pull a random slice of data for fuzzing
47
3.93k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
3.93k
  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.93k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
3.93k
  std::vector<HashType> contexts(num_contexts);
53
3.93k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
12.1k
  for (unsigned i = 0; i < num_contexts; i++) {
55
8.25k
    init_fn(&contexts[i]);
56
8.25k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
3.93k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
3.93k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
3.93k
  if (!input_bytes.empty()) {
62
3.00k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
3.00k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
3.93k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
3.93k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
3.93k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
10.6k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
6.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
6.71k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
6.71k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
6.71k
    size_t chunk_len = 0;
80
6.71k
    switch (pattern) {
81
1.53k
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
1.53k
        if (block_size > 1) {
84
1.53k
          chunk_len = std::min(remaining, block_size - 1);
85
1.53k
        }
86
1.53k
        break;
87
0
      }
88
350
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
350
        chunk_len = std::min(remaining, block_size);
91
350
        break;
92
0
      }
93
349
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
349
        chunk_len = std::min(remaining, block_size + 1);
96
349
        break;
97
0
      }
98
1.22k
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
1.22k
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
1.22k
        chunk_len = std::min(remaining, small_len);
102
1.22k
        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
357
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
357
        chunk_len = remaining;
112
357
        break;
113
0
      }
114
1.65k
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
1.65k
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
1.65k
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
4.86k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
3.20k
          size_t w = std::min(remaining, step);
120
3.20k
          update_fn(&contexts[ctx_index], w, cursor);
121
3.20k
          cursor += w;
122
3.20k
          remaining -= w;
123
3.20k
        }
124
125
        // Randomly reinitialise the hash stream
126
1.65k
        if (fdp.ConsumeBool()) {
127
668
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
668
          init_fn(&contexts[ctx_index]);
129
668
        }
130
1.65k
        continue;
131
0
      }
132
6.71k
    }
133
5.06k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
5.06k
    if (fdp.ConsumeBool()) {
139
2.09k
      init_fn(&contexts[ctx_index]);
140
2.09k
    }
141
142
    // Fuzz the update function
143
5.06k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
5.06k
    cursor += chunk_len;
145
5.06k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
5.06k
    if (fdp.ConsumeBool()) {
149
2.07k
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
2.07k
      init_fn(&contexts[ctx_index]);
151
2.07k
    }
152
5.06k
  }
153
154
  // Fuzz the finish function for all contexts
155
12.1k
  for (unsigned i = 0; i < num_contexts; i++) {
156
8.25k
    finish_fn(&contexts[i], digests[i].data());
157
8.25k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
3.93k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
1.08k
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
1.08k
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
1.08k
    if (src_idx != dst_idx) {
164
630
      init_fn(&contexts[dst_idx]);
165
630
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
630
      size_t feed_len = std::min(digest_size - offset,
167
630
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
630
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
630
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
630
    }
171
1.08k
  }
172
3.93k
}
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.47k
                                size_t digest_size) {
42
1.47k
  if (!fdp.remaining_bytes()) {
43
21
    return;
44
21
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.45k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.45k
  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.45k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.45k
  std::vector<HashType> contexts(num_contexts);
53
1.45k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
4.60k
  for (unsigned i = 0; i < num_contexts; i++) {
55
3.15k
    init_fn(&contexts[i]);
56
3.15k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.45k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.45k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.45k
  if (!input_bytes.empty()) {
62
1.10k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
1.10k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.45k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.45k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.45k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.98k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.52k
    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.52k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.52k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.52k
    size_t chunk_len = 0;
80
2.52k
    switch (pattern) {
81
550
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
550
        if (block_size > 1) {
84
550
          chunk_len = std::min(remaining, block_size - 1);
85
550
        }
86
550
        break;
87
0
      }
88
128
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
128
        chunk_len = std::min(remaining, block_size);
91
128
        break;
92
0
      }
93
187
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
187
        chunk_len = std::min(remaining, block_size + 1);
96
187
        break;
97
0
      }
98
480
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
480
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
480
        chunk_len = std::min(remaining, small_len);
102
480
        break;
103
0
      }
104
478
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
478
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
478
        break;
108
0
      }
109
112
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
112
        chunk_len = remaining;
112
112
        break;
113
0
      }
114
593
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
593
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
593
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.77k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.18k
          size_t w = std::min(remaining, step);
120
1.18k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.18k
          cursor += w;
122
1.18k
          remaining -= w;
123
1.18k
        }
124
125
        // Randomly reinitialise the hash stream
126
593
        if (fdp.ConsumeBool()) {
127
233
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
233
          init_fn(&contexts[ctx_index]);
129
233
        }
130
593
        continue;
131
0
      }
132
2.52k
    }
133
1.93k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.93k
    if (fdp.ConsumeBool()) {
139
825
      init_fn(&contexts[ctx_index]);
140
825
    }
141
142
    // Fuzz the update function
143
1.93k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.93k
    cursor += chunk_len;
145
1.93k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.93k
    if (fdp.ConsumeBool()) {
149
839
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
839
      init_fn(&contexts[ctx_index]);
151
839
    }
152
1.93k
  }
153
154
  // Fuzz the finish function for all contexts
155
4.60k
  for (unsigned i = 0; i < num_contexts; i++) {
156
3.15k
    finish_fn(&contexts[i], digests[i].data());
157
3.15k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.45k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
414
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
414
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
414
    if (src_idx != dst_idx) {
164
222
      init_fn(&contexts[dst_idx]);
165
222
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
222
      size_t feed_len = std::min(digest_size - offset,
167
222
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
222
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
222
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
222
    }
171
414
  }
172
1.45k
}
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.26k
                                size_t digest_size) {
42
1.26k
  if (!fdp.remaining_bytes()) {
43
5
    return;
44
5
  }
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.84k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.58k
    init_fn(&contexts[i]);
56
2.58k
  }
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
959
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
959
  }
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.35k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.09k
    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.09k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.09k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.09k
    size_t chunk_len = 0;
80
2.09k
    switch (pattern) {
81
502
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
502
        if (block_size > 1) {
84
502
          chunk_len = std::min(remaining, block_size - 1);
85
502
        }
86
502
        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
78
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
78
        chunk_len = std::min(remaining, block_size + 1);
96
78
        break;
97
0
      }
98
383
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
383
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
383
        chunk_len = std::min(remaining, small_len);
102
383
        break;
103
0
      }
104
397
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
397
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
397
        break;
108
0
      }
109
120
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
120
        chunk_len = remaining;
112
120
        break;
113
0
      }
114
504
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
504
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
504
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.44k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
939
          size_t w = std::min(remaining, step);
120
939
          update_fn(&contexts[ctx_index], w, cursor);
121
939
          cursor += w;
122
939
          remaining -= w;
123
939
        }
124
125
        // Randomly reinitialise the hash stream
126
504
        if (fdp.ConsumeBool()) {
127
195
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
195
          init_fn(&contexts[ctx_index]);
129
195
        }
130
504
        continue;
131
0
      }
132
2.09k
    }
133
1.59k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.59k
    if (fdp.ConsumeBool()) {
139
616
      init_fn(&contexts[ctx_index]);
140
616
    }
141
142
    // Fuzz the update function
143
1.59k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.59k
    cursor += chunk_len;
145
1.59k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.59k
    if (fdp.ConsumeBool()) {
149
620
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
620
      init_fn(&contexts[ctx_index]);
151
620
    }
152
1.59k
  }
153
154
  // Fuzz the finish function for all contexts
155
3.84k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.58k
    finish_fn(&contexts[i], digests[i].data());
157
2.58k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.26k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
340
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
340
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
340
    if (src_idx != dst_idx) {
164
216
      init_fn(&contexts[dst_idx]);
165
216
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
216
      size_t feed_len = std::min(digest_size - offset,
167
216
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
216
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
216
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
216
    }
171
340
  }
172
1.26k
}
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.22k
                                size_t digest_size) {
42
1.22k
  if (!fdp.remaining_bytes()) {
43
4
    return;
44
4
  }
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.74k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.52k
    init_fn(&contexts[i]);
56
2.52k
  }
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
933
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
933
  }
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.30k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.09k
    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.09k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.09k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.09k
    size_t chunk_len = 0;
80
2.09k
    switch (pattern) {
81
481
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
481
        if (block_size > 1) {
84
481
          chunk_len = std::min(remaining, block_size - 1);
85
481
        }
86
481
        break;
87
0
      }
88
110
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
110
        chunk_len = std::min(remaining, block_size);
91
110
        break;
92
0
      }
93
84
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
84
        chunk_len = std::min(remaining, block_size + 1);
96
84
        break;
97
0
      }
98
359
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
359
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
359
        chunk_len = std::min(remaining, small_len);
102
359
        break;
103
0
      }
104
374
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
374
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
374
        break;
108
0
      }
109
125
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
125
        chunk_len = remaining;
112
125
        break;
113
0
      }
114
559
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
559
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
559
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.64k
        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
559
        if (fdp.ConsumeBool()) {
127
240
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
240
          init_fn(&contexts[ctx_index]);
129
240
        }
130
559
        continue;
131
0
      }
132
2.09k
    }
133
1.53k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.53k
    if (fdp.ConsumeBool()) {
139
653
      init_fn(&contexts[ctx_index]);
140
653
    }
141
142
    // Fuzz the update function
143
1.53k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.53k
    cursor += chunk_len;
145
1.53k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.53k
    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.53k
  }
153
154
  // Fuzz the finish function for all contexts
155
3.74k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.52k
    finish_fn(&contexts[i], digests[i].data());
157
2.52k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.21k
  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
192
      init_fn(&contexts[dst_idx]);
165
192
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
192
      size_t feed_len = std::min(digest_size - offset,
167
192
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
192
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
192
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
192
    }
171
328
  }
172
1.21k
}
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.14k
  for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) {
178
3.96k
    switch (fdp.ConsumeIntegralInRange<int>(0, 2)) {
179
1.47k
      case 0:
180
1.47k
        fuzz_hash_int_multi<struct mhd_Md5CtxInt>(
181
1.47k
          fdp, mhd_MD5_BLOCK_SIZE,
182
1.47k
          mhd_MD5_init, mhd_MD5_update, mhd_MD5_finish, mhd_MD5_DIGEST_SIZE);
183
1.47k
        break;
184
1.26k
      case 1:
185
1.26k
        fuzz_hash_int_multi<struct mhd_Sha256CtxInt>(
186
1.26k
          fdp, mhd_SHA256_BLOCK_SIZE,
187
1.26k
          mhd_SHA256_init, mhd_SHA256_update, mhd_SHA256_finish, mhd_SHA256_DIGEST_SIZE);
188
1.26k
        break;
189
1.22k
      case 2:
190
1.22k
      default:
191
1.22k
        fuzz_hash_int_multi<struct mhd_Sha512_256CtxInt>(
192
1.22k
          fdp, mhd_SHA512_256_BLOCK_SIZE,
193
1.22k
          mhd_SHA512_256_init, mhd_SHA512_256_update, mhd_SHA512_256_finish, mhd_SHA512_256_DIGEST_SIZE);
194
1.22k
        break;
195
3.96k
    }
196
3.96k
  }
197
2.17k
  return 0;
198
2.17k
}