Coverage Report

Created: 2026-02-26 07:05

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.90k
                                size_t digest_size) {
42
3.90k
  if (!fdp.remaining_bytes()) {
43
32
    return;
44
32
  }
45
46
  // Pull a random slice of data for fuzzing
47
3.87k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
3.87k
  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.87k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
3.87k
  std::vector<HashType> contexts(num_contexts);
53
3.87k
  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.27k
    init_fn(&contexts[i]);
56
8.27k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
3.87k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
3.87k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
3.87k
  if (!input_bytes.empty()) {
62
2.96k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
2.96k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
3.87k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
3.87k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
3.87k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
10.4k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
6.58k
    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.58k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
6.58k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
6.58k
    size_t chunk_len = 0;
80
6.58k
    switch (pattern) {
81
1.45k
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
1.45k
        if (block_size > 1) {
84
1.45k
          chunk_len = std::min(remaining, block_size - 1);
85
1.45k
        }
86
1.45k
        break;
87
0
      }
88
378
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
378
        chunk_len = std::min(remaining, block_size);
91
378
        break;
92
0
      }
93
389
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
389
        chunk_len = std::min(remaining, block_size + 1);
96
389
        break;
97
0
      }
98
1.19k
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
1.19k
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
1.19k
        chunk_len = std::min(remaining, small_len);
102
1.19k
        break;
103
0
      }
104
1.23k
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
1.23k
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
1.23k
        break;
108
0
      }
109
347
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
347
        chunk_len = remaining;
112
347
        break;
113
0
      }
114
1.59k
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
1.59k
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
1.59k
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
4.73k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
3.14k
          size_t w = std::min(remaining, step);
120
3.14k
          update_fn(&contexts[ctx_index], w, cursor);
121
3.14k
          cursor += w;
122
3.14k
          remaining -= w;
123
3.14k
        }
124
125
        // Randomly reinitialise the hash stream
126
1.59k
        if (fdp.ConsumeBool()) {
127
643
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
643
          init_fn(&contexts[ctx_index]);
129
643
        }
130
1.59k
        continue;
131
0
      }
132
6.58k
    }
133
4.99k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
4.99k
    if (fdp.ConsumeBool()) {
139
2.06k
      init_fn(&contexts[ctx_index]);
140
2.06k
    }
141
142
    // Fuzz the update function
143
4.99k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
4.99k
    cursor += chunk_len;
145
4.99k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
4.99k
    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
4.99k
  }
153
154
  // Fuzz the finish function for all contexts
155
12.1k
  for (unsigned i = 0; i < num_contexts; i++) {
156
8.27k
    finish_fn(&contexts[i], digests[i].data());
157
8.27k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
3.87k
  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
617
      init_fn(&contexts[dst_idx]);
165
617
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
617
      size_t feed_len = std::min(digest_size - offset,
167
617
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
617
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
617
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
617
    }
171
1.09k
  }
172
3.87k
}
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.46k
                                size_t digest_size) {
42
1.46k
  if (!fdp.remaining_bytes()) {
43
19
    return;
44
19
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.44k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.44k
  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.44k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.44k
  std::vector<HashType> contexts(num_contexts);
53
1.44k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
4.57k
  for (unsigned i = 0; i < num_contexts; i++) {
55
3.13k
    init_fn(&contexts[i]);
56
3.13k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.44k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.44k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.44k
  if (!input_bytes.empty()) {
62
1.08k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
1.08k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.44k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.44k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.44k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.88k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.44k
    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.44k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.44k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.44k
    size_t chunk_len = 0;
80
2.44k
    switch (pattern) {
81
515
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
515
        if (block_size > 1) {
84
515
          chunk_len = std::min(remaining, block_size - 1);
85
515
        }
86
515
        break;
87
0
      }
88
141
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
141
        chunk_len = std::min(remaining, block_size);
91
141
        break;
92
0
      }
93
198
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
198
        chunk_len = std::min(remaining, block_size + 1);
96
198
        break;
97
0
      }
98
481
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
481
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
481
        chunk_len = std::min(remaining, small_len);
102
481
        break;
103
0
      }
104
455
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
455
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
455
        break;
108
0
      }
109
109
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
109
        chunk_len = remaining;
112
109
        break;
113
0
      }
114
541
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
541
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
541
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.61k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.07k
          size_t w = std::min(remaining, step);
120
1.07k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.07k
          cursor += w;
122
1.07k
          remaining -= w;
123
1.07k
        }
124
125
        // Randomly reinitialise the hash stream
126
541
        if (fdp.ConsumeBool()) {
127
212
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
212
          init_fn(&contexts[ctx_index]);
129
212
        }
130
541
        continue;
131
0
      }
132
2.44k
    }
133
1.89k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.89k
    if (fdp.ConsumeBool()) {
139
806
      init_fn(&contexts[ctx_index]);
140
806
    }
141
142
    // Fuzz the update function
143
1.89k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.89k
    cursor += chunk_len;
145
1.89k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.89k
    if (fdp.ConsumeBool()) {
149
834
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
834
      init_fn(&contexts[ctx_index]);
151
834
    }
152
1.89k
  }
153
154
  // Fuzz the finish function for all contexts
155
4.57k
  for (unsigned i = 0; i < num_contexts; i++) {
156
3.13k
    finish_fn(&contexts[i], digests[i].data());
157
3.13k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.44k
  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
217
      init_fn(&contexts[dst_idx]);
165
217
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
217
      size_t feed_len = std::min(digest_size - offset,
167
217
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
217
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
217
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
217
    }
171
414
  }
172
1.44k
}
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.24k
                                size_t digest_size) {
42
1.24k
  if (!fdp.remaining_bytes()) {
43
7
    return;
44
7
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.23k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.23k
  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.23k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.23k
  std::vector<HashType> contexts(num_contexts);
53
1.23k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
3.81k
  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.23k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.23k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.23k
  if (!input_bytes.empty()) {
62
943
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
943
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.23k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.23k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.23k
  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.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
477
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
477
        if (block_size > 1) {
84
477
          chunk_len = std::min(remaining, block_size - 1);
85
477
        }
86
477
        break;
87
0
      }
88
115
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
115
        chunk_len = std::min(remaining, block_size);
91
115
        break;
92
0
      }
93
87
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
87
        chunk_len = std::min(remaining, block_size + 1);
96
87
        break;
97
0
      }
98
380
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
380
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
380
        chunk_len = std::min(remaining, small_len);
102
380
        break;
103
0
      }
104
396
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
396
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
396
        break;
108
0
      }
109
118
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
118
        chunk_len = remaining;
112
118
        break;
113
0
      }
114
519
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
519
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
519
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.53k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.02k
          size_t w = std::min(remaining, step);
120
1.02k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.02k
          cursor += w;
122
1.02k
          remaining -= w;
123
1.02k
        }
124
125
        // Randomly reinitialise the hash stream
126
519
        if (fdp.ConsumeBool()) {
127
212
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
212
          init_fn(&contexts[ctx_index]);
129
212
        }
130
519
        continue;
131
0
      }
132
2.09k
    }
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
616
      init_fn(&contexts[ctx_index]);
140
616
    }
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
634
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
634
      init_fn(&contexts[ctx_index]);
151
634
    }
152
1.57k
  }
153
154
  // Fuzz the finish function for all contexts
155
3.81k
  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.23k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
335
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
335
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
335
    if (src_idx != dst_idx) {
164
212
      init_fn(&contexts[dst_idx]);
165
212
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
212
      size_t feed_len = std::min(digest_size - offset,
167
212
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
212
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
212
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
212
    }
171
335
  }
172
1.23k
}
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.20k
                                size_t digest_size) {
42
1.20k
  if (!fdp.remaining_bytes()) {
43
6
    return;
44
6
  }
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.75k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.56k
    init_fn(&contexts[i]);
56
2.56k
  }
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
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.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.24k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.05k
    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.05k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.05k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.05k
    size_t chunk_len = 0;
80
2.05k
    switch (pattern) {
81
460
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
460
        if (block_size > 1) {
84
460
          chunk_len = std::min(remaining, block_size - 1);
85
460
        }
86
460
        break;
87
0
      }
88
122
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
122
        chunk_len = std::min(remaining, block_size);
91
122
        break;
92
0
      }
93
104
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
104
        chunk_len = std::min(remaining, block_size + 1);
96
104
        break;
97
0
      }
98
330
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
330
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
330
        chunk_len = std::min(remaining, small_len);
102
330
        break;
103
0
      }
104
387
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
387
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
387
        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
530
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
530
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
530
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.57k
        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
530
        if (fdp.ConsumeBool()) {
127
219
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
219
          init_fn(&contexts[ctx_index]);
129
219
        }
130
530
        continue;
131
0
      }
132
2.05k
    }
133
1.52k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.52k
    if (fdp.ConsumeBool()) {
139
642
      init_fn(&contexts[ctx_index]);
140
642
    }
141
142
    // Fuzz the update function
143
1.52k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.52k
    cursor += chunk_len;
145
1.52k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.52k
    if (fdp.ConsumeBool()) {
149
616
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
616
      init_fn(&contexts[ctx_index]);
151
616
    }
152
1.52k
  }
153
154
  // Fuzz the finish function for all contexts
155
3.75k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.56k
    finish_fn(&contexts[i], digests[i].data());
157
2.56k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.19k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
344
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
344
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
344
    if (src_idx != dst_idx) {
164
188
      init_fn(&contexts[dst_idx]);
165
188
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
188
      size_t feed_len = std::min(digest_size - offset,
167
188
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
188
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
188
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
188
    }
171
344
  }
172
1.19k
}
173
174
2.11k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
175
2.11k
  FuzzedDataProvider fdp(data, size);
176
177
6.02k
  for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) {
178
3.90k
    switch (fdp.ConsumeIntegralInRange<int>(0, 2)) {
179
1.46k
      case 0:
180
1.46k
        fuzz_hash_int_multi<struct mhd_Md5CtxInt>(
181
1.46k
          fdp, mhd_MD5_BLOCK_SIZE,
182
1.46k
          mhd_MD5_init, mhd_MD5_update, mhd_MD5_finish, mhd_MD5_DIGEST_SIZE);
183
1.46k
        break;
184
1.24k
      case 1:
185
1.24k
        fuzz_hash_int_multi<struct mhd_Sha256CtxInt>(
186
1.24k
          fdp, mhd_SHA256_BLOCK_SIZE,
187
1.24k
          mhd_SHA256_init, mhd_SHA256_update, mhd_SHA256_finish, mhd_SHA256_DIGEST_SIZE);
188
1.24k
        break;
189
1.20k
      case 2:
190
1.20k
      default:
191
1.20k
        fuzz_hash_int_multi<struct mhd_Sha512_256CtxInt>(
192
1.20k
          fdp, mhd_SHA512_256_BLOCK_SIZE,
193
1.20k
          mhd_SHA512_256_init, mhd_SHA512_256_update, mhd_SHA512_256_finish, mhd_SHA512_256_DIGEST_SIZE);
194
1.20k
        break;
195
3.90k
    }
196
3.90k
  }
197
2.11k
  return 0;
198
2.11k
}