Coverage Report

Created: 2026-05-12 06:24

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.99k
                                size_t digest_size) {
42
3.99k
  if (!fdp.remaining_bytes()) {
43
31
    return;
44
31
  }
45
46
  // Pull a random slice of data for fuzzing
47
3.96k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
3.96k
  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.96k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
3.96k
  std::vector<HashType> contexts(num_contexts);
53
3.96k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
12.4k
  for (unsigned i = 0; i < num_contexts; i++) {
55
8.45k
    init_fn(&contexts[i]);
56
8.45k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
3.96k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
3.96k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
3.96k
  if (!input_bytes.empty()) {
62
3.04k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
3.04k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
3.96k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
3.96k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
3.96k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
10.7k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
6.76k
    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.76k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
6.76k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
6.76k
    size_t chunk_len = 0;
80
6.76k
    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
405
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
405
        chunk_len = std::min(remaining, block_size);
91
405
        break;
92
0
      }
93
396
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
396
        chunk_len = std::min(remaining, block_size + 1);
96
396
        break;
97
0
      }
98
1.16k
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
1.16k
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
1.16k
        chunk_len = std::min(remaining, small_len);
102
1.16k
        break;
103
0
      }
104
1.34k
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
1.34k
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
1.34k
        break;
108
0
      }
109
358
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
358
        chunk_len = remaining;
112
358
        break;
113
0
      }
114
1.60k
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
1.60k
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
1.60k
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
4.69k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
3.08k
          size_t w = std::min(remaining, step);
120
3.08k
          update_fn(&contexts[ctx_index], w, cursor);
121
3.08k
          cursor += w;
122
3.08k
          remaining -= w;
123
3.08k
        }
124
125
        // Randomly reinitialise the hash stream
126
1.60k
        if (fdp.ConsumeBool()) {
127
637
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
637
          init_fn(&contexts[ctx_index]);
129
637
        }
130
1.60k
        continue;
131
0
      }
132
6.76k
    }
133
5.15k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
5.15k
    if (fdp.ConsumeBool()) {
139
2.11k
      init_fn(&contexts[ctx_index]);
140
2.11k
    }
141
142
    // Fuzz the update function
143
5.15k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
5.15k
    cursor += chunk_len;
145
5.15k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
5.15k
    if (fdp.ConsumeBool()) {
149
2.12k
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
2.12k
      init_fn(&contexts[ctx_index]);
151
2.12k
    }
152
5.15k
  }
153
154
  // Fuzz the finish function for all contexts
155
12.4k
  for (unsigned i = 0; i < num_contexts; i++) {
156
8.45k
    finish_fn(&contexts[i], digests[i].data());
157
8.45k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
3.96k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
1.09k
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
1.09k
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
1.09k
    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.09k
  }
172
3.96k
}
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
22
    return;
44
22
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.52k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.52k
  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.52k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.52k
  std::vector<HashType> contexts(num_contexts);
53
1.52k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
4.82k
  for (unsigned i = 0; i < num_contexts; i++) {
55
3.30k
    init_fn(&contexts[i]);
56
3.30k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.52k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.52k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.52k
  if (!input_bytes.empty()) {
62
1.16k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
1.16k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.52k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.52k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.52k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
4.11k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.59k
    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.59k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.59k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.59k
    size_t chunk_len = 0;
80
2.59k
    switch (pattern) {
81
545
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
545
        if (block_size > 1) {
84
545
          chunk_len = std::min(remaining, block_size - 1);
85
545
        }
86
545
        break;
87
0
      }
88
168
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
168
        chunk_len = std::min(remaining, block_size);
91
168
        break;
92
0
      }
93
200
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
200
        chunk_len = std::min(remaining, block_size + 1);
96
200
        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
480
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
480
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
480
        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
582
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
582
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
582
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.67k
        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
582
        if (fdp.ConsumeBool()) {
127
217
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
217
          init_fn(&contexts[ctx_index]);
129
217
        }
130
582
        continue;
131
0
      }
132
2.59k
    }
133
2.01k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
2.01k
    if (fdp.ConsumeBool()) {
139
803
      init_fn(&contexts[ctx_index]);
140
803
    }
141
142
    // Fuzz the update function
143
2.01k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
2.01k
    cursor += chunk_len;
145
2.01k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
2.01k
    if (fdp.ConsumeBool()) {
149
806
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
806
      init_fn(&contexts[ctx_index]);
151
806
    }
152
2.01k
  }
153
154
  // Fuzz the finish function for all contexts
155
4.82k
  for (unsigned i = 0; i < num_contexts; i++) {
156
3.30k
    finish_fn(&contexts[i], digests[i].data());
157
3.30k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.52k
  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
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
427
  }
172
1.52k
}
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.19k
                                size_t digest_size) {
42
1.19k
  if (!fdp.remaining_bytes()) {
43
5
    return;
44
5
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.19k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.19k
  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.19k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.19k
  std::vector<HashType> contexts(num_contexts);
53
1.19k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
3.64k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.45k
    init_fn(&contexts[i]);
56
2.45k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.19k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.19k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.19k
  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.19k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.19k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.19k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.13k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
1.93k
    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
1.93k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
1.93k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
1.93k
    size_t chunk_len = 0;
80
1.93k
    switch (pattern) {
81
466
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
466
        if (block_size > 1) {
84
466
          chunk_len = std::min(remaining, block_size - 1);
85
466
        }
86
466
        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
86
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
86
        chunk_len = std::min(remaining, block_size + 1);
96
86
        break;
97
0
      }
98
314
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
314
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
314
        chunk_len = std::min(remaining, small_len);
102
314
        break;
103
0
      }
104
361
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
361
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
361
        break;
108
0
      }
109
111
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
111
        chunk_len = remaining;
112
111
        break;
113
0
      }
114
487
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
487
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
487
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.41k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
931
          size_t w = std::min(remaining, step);
120
931
          update_fn(&contexts[ctx_index], w, cursor);
121
931
          cursor += w;
122
931
          remaining -= w;
123
931
        }
124
125
        // Randomly reinitialise the hash stream
126
487
        if (fdp.ConsumeBool()) {
127
197
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
197
          init_fn(&contexts[ctx_index]);
129
197
        }
130
487
        continue;
131
0
      }
132
1.93k
    }
133
1.45k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.45k
    if (fdp.ConsumeBool()) {
139
611
      init_fn(&contexts[ctx_index]);
140
611
    }
141
142
    // Fuzz the update function
143
1.45k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.45k
    cursor += chunk_len;
145
1.45k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.45k
    if (fdp.ConsumeBool()) {
149
564
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
564
      init_fn(&contexts[ctx_index]);
151
564
    }
152
1.45k
  }
153
154
  // Fuzz the finish function for all contexts
155
3.64k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.45k
    finish_fn(&contexts[i], digests[i].data());
157
2.45k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.19k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
320
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
320
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
320
    if (src_idx != dst_idx) {
164
199
      init_fn(&contexts[dst_idx]);
165
199
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
199
      size_t feed_len = std::min(digest_size - offset,
167
199
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
199
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
199
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
199
    }
171
320
  }
172
1.19k
}
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.26k
                                size_t digest_size) {
42
1.26k
  if (!fdp.remaining_bytes()) {
43
4
    return;
44
4
  }
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.95k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.70k
    init_fn(&contexts[i]);
56
2.70k
  }
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
989
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
989
  }
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.48k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.22k
    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.22k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.22k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.22k
    size_t chunk_len = 0;
80
2.22k
    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
110
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
110
        chunk_len = std::min(remaining, block_size + 1);
96
110
        break;
97
0
      }
98
347
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
347
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
347
        chunk_len = std::min(remaining, small_len);
102
347
        break;
103
0
      }
104
505
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
505
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
505
        break;
108
0
      }
109
127
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
127
        chunk_len = remaining;
112
127
        break;
113
0
      }
114
536
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
536
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
536
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.60k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.06k
          size_t w = std::min(remaining, step);
120
1.06k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.06k
          cursor += w;
122
1.06k
          remaining -= w;
123
1.06k
        }
124
125
        // Randomly reinitialise the hash stream
126
536
        if (fdp.ConsumeBool()) {
127
223
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
223
          init_fn(&contexts[ctx_index]);
129
223
        }
130
536
        continue;
131
0
      }
132
2.22k
    }
133
1.68k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.68k
    if (fdp.ConsumeBool()) {
139
705
      init_fn(&contexts[ctx_index]);
140
705
    }
141
142
    // Fuzz the update function
143
1.68k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.68k
    cursor += chunk_len;
145
1.68k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.68k
    if (fdp.ConsumeBool()) {
149
755
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
755
      init_fn(&contexts[ctx_index]);
151
755
    }
152
1.68k
  }
153
154
  // Fuzz the finish function for all contexts
155
3.95k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.70k
    finish_fn(&contexts[i], digests[i].data());
157
2.70k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.25k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
346
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
346
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
346
    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
346
  }
172
1.25k
}
173
174
2.16k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
175
2.16k
  FuzzedDataProvider fdp(data, size);
176
177
6.16k
  for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) {
178
3.99k
    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.19k
      case 1:
185
1.19k
        fuzz_hash_int_multi<struct mhd_Sha256CtxInt>(
186
1.19k
          fdp, mhd_SHA256_BLOCK_SIZE,
187
1.19k
          mhd_SHA256_init, mhd_SHA256_update, mhd_SHA256_finish, mhd_SHA256_DIGEST_SIZE);
188
1.19k
        break;
189
1.26k
      case 2:
190
1.26k
      default:
191
1.26k
        fuzz_hash_int_multi<struct mhd_Sha512_256CtxInt>(
192
1.26k
          fdp, mhd_SHA512_256_BLOCK_SIZE,
193
1.26k
          mhd_SHA512_256_init, mhd_SHA512_256_update, mhd_SHA512_256_finish, mhd_SHA512_256_DIGEST_SIZE);
194
1.26k
        break;
195
3.99k
    }
196
3.99k
  }
197
2.16k
  return 0;
198
2.16k
}