Coverage Report

Created: 2025-10-13 06: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
extern "C" {
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.83k
                                size_t digest_size) {
42
3.83k
  if (!fdp.remaining_bytes()) {
43
25
    return;
44
25
  }
45
46
  // Pull a random slice of data for fuzzing
47
3.80k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
3.80k
  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.80k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
3.80k
  std::vector<HashType> contexts(num_contexts);
53
3.80k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
12.2k
  for (unsigned i = 0; i < num_contexts; i++) {
55
8.46k
    init_fn(&contexts[i]);
56
8.46k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
3.80k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
3.80k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
3.80k
  if (!input_bytes.empty()) {
62
2.89k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
2.89k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
3.80k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
3.80k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
3.80k
  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.62k
    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.62k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
6.62k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
6.62k
    size_t chunk_len = 0;
80
6.62k
    switch (pattern) {
81
1.56k
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
1.56k
        if (block_size > 1) {
84
1.56k
          chunk_len = std::min(remaining, block_size - 1);
85
1.56k
        }
86
1.56k
        break;
87
0
      }
88
333
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
333
        chunk_len = std::min(remaining, block_size);
91
333
        break;
92
0
      }
93
369
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
369
        chunk_len = std::min(remaining, block_size + 1);
96
369
        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.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
334
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
334
        chunk_len = remaining;
112
334
        break;
113
0
      }
114
1.62k
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
1.62k
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
1.62k
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
4.89k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
3.27k
          size_t w = std::min(remaining, step);
120
3.27k
          update_fn(&contexts[ctx_index], w, cursor);
121
3.27k
          cursor += w;
122
3.27k
          remaining -= w;
123
3.27k
        }
124
125
        // Randomly reinitialise the hash stream
126
1.62k
        if (fdp.ConsumeBool()) {
127
698
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
698
          init_fn(&contexts[ctx_index]);
129
698
        }
130
1.62k
        continue;
131
0
      }
132
6.62k
    }
133
5.00k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
5.00k
    if (fdp.ConsumeBool()) {
139
2.04k
      init_fn(&contexts[ctx_index]);
140
2.04k
    }
141
142
    // Fuzz the update function
143
5.00k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
5.00k
    cursor += chunk_len;
145
5.00k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
5.00k
    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.00k
  }
153
154
  // Fuzz the finish function for all contexts
155
12.2k
  for (unsigned i = 0; i < num_contexts; i++) {
156
8.46k
    finish_fn(&contexts[i], digests[i].data());
157
8.46k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
3.80k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
1.06k
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
1.06k
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
1.06k
    if (src_idx != dst_idx) {
164
554
      init_fn(&contexts[dst_idx]);
165
554
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
554
      size_t feed_len = std::min(digest_size - offset,
167
554
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
554
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
554
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
554
    }
171
1.06k
  }
172
3.80k
}
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.38k
                                size_t digest_size) {
42
1.38k
  if (!fdp.remaining_bytes()) {
43
17
    return;
44
17
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.36k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.36k
  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.36k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.36k
  std::vector<HashType> contexts(num_contexts);
53
1.36k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
4.36k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.99k
    init_fn(&contexts[i]);
56
2.99k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.36k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.36k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.36k
  if (!input_bytes.empty()) {
62
1.01k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
1.01k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.36k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.36k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.36k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.68k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.32k
    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.32k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.32k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.32k
    size_t chunk_len = 0;
80
2.32k
    switch (pattern) {
81
537
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
537
        if (block_size > 1) {
84
537
          chunk_len = std::min(remaining, block_size - 1);
85
537
        }
86
537
        break;
87
0
      }
88
129
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
129
        chunk_len = std::min(remaining, block_size);
91
129
        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
466
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
466
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
466
        chunk_len = std::min(remaining, small_len);
102
466
        break;
103
0
      }
104
434
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
434
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
434
        break;
108
0
      }
109
95
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
95
        chunk_len = remaining;
112
95
        break;
113
0
      }
114
553
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
553
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
553
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.62k
        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
553
        if (fdp.ConsumeBool()) {
127
223
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
223
          init_fn(&contexts[ctx_index]);
129
223
        }
130
553
        continue;
131
0
      }
132
2.32k
    }
133
1.77k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.77k
    if (fdp.ConsumeBool()) {
139
784
      init_fn(&contexts[ctx_index]);
140
784
    }
141
142
    // Fuzz the update function
143
1.77k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.77k
    cursor += chunk_len;
145
1.77k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.77k
    if (fdp.ConsumeBool()) {
149
805
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
805
      init_fn(&contexts[ctx_index]);
151
805
    }
152
1.77k
  }
153
154
  // Fuzz the finish function for all contexts
155
4.36k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.99k
    finish_fn(&contexts[i], digests[i].data());
157
2.99k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.36k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
392
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
392
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
392
    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
392
  }
172
1.36k
}
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.18k
                                size_t digest_size) {
42
1.18k
  if (!fdp.remaining_bytes()) {
43
4
    return;
44
4
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.18k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.18k
  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.18k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.18k
  std::vector<HashType> contexts(num_contexts);
53
1.18k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
3.68k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.50k
    init_fn(&contexts[i]);
56
2.50k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.18k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.18k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.18k
  if (!input_bytes.empty()) {
62
876
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
876
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.18k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.18k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.18k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.14k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
1.96k
    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.96k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
1.96k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
1.96k
    size_t chunk_len = 0;
80
1.96k
    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
109
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
109
        chunk_len = std::min(remaining, block_size);
91
109
        break;
92
0
      }
93
116
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
116
        chunk_len = std::min(remaining, block_size + 1);
96
116
        break;
97
0
      }
98
332
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
332
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
332
        chunk_len = std::min(remaining, small_len);
102
332
        break;
103
0
      }
104
332
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
332
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
332
        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
495
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
495
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
495
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.46k
        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
495
        if (fdp.ConsumeBool()) {
127
200
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
200
          init_fn(&contexts[ctx_index]);
129
200
        }
130
495
        continue;
131
0
      }
132
1.96k
    }
133
1.46k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.46k
    if (fdp.ConsumeBool()) {
139
551
      init_fn(&contexts[ctx_index]);
140
551
    }
141
142
    // Fuzz the update function
143
1.46k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.46k
    cursor += chunk_len;
145
1.46k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.46k
    if (fdp.ConsumeBool()) {
149
562
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
562
      init_fn(&contexts[ctx_index]);
151
562
    }
152
1.46k
  }
153
154
  // Fuzz the finish function for all contexts
155
3.68k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.50k
    finish_fn(&contexts[i], digests[i].data());
157
2.50k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.18k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
298
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
298
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
298
    if (src_idx != dst_idx) {
164
177
      init_fn(&contexts[dst_idx]);
165
177
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
177
      size_t feed_len = std::min(digest_size - offset,
167
177
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
177
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
177
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
177
    }
171
298
  }
172
1.18k
}
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.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
4.22k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.96k
    init_fn(&contexts[i]);
56
2.96k
  }
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
1.00k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
1.00k
  }
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.59k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.33k
    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.33k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.33k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.33k
    size_t chunk_len = 0;
80
2.33k
    switch (pattern) {
81
560
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
560
        if (block_size > 1) {
84
560
          chunk_len = std::min(remaining, block_size - 1);
85
560
        }
86
560
        break;
87
0
      }
88
95
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
95
        chunk_len = std::min(remaining, block_size);
91
95
        break;
92
0
      }
93
143
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
143
        chunk_len = std::min(remaining, block_size + 1);
96
143
        break;
97
0
      }
98
362
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
362
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
362
        chunk_len = std::min(remaining, small_len);
102
362
        break;
103
0
      }
104
476
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
476
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
476
        break;
108
0
      }
109
128
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
128
        chunk_len = remaining;
112
128
        break;
113
0
      }
114
575
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
575
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
575
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.80k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.22k
          size_t w = std::min(remaining, step);
120
1.22k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.22k
          cursor += w;
122
1.22k
          remaining -= w;
123
1.22k
        }
124
125
        // Randomly reinitialise the hash stream
126
575
        if (fdp.ConsumeBool()) {
127
275
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
275
          init_fn(&contexts[ctx_index]);
129
275
        }
130
575
        continue;
131
0
      }
132
2.33k
    }
133
1.76k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.76k
    if (fdp.ConsumeBool()) {
139
713
      init_fn(&contexts[ctx_index]);
140
713
    }
141
142
    // Fuzz the update function
143
1.76k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.76k
    cursor += chunk_len;
145
1.76k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.76k
    if (fdp.ConsumeBool()) {
149
754
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
754
      init_fn(&contexts[ctx_index]);
151
754
    }
152
1.76k
  }
153
154
  // Fuzz the finish function for all contexts
155
4.22k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.96k
    finish_fn(&contexts[i], digests[i].data());
157
2.96k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.26k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
370
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
370
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
370
    if (src_idx != dst_idx) {
164
178
      init_fn(&contexts[dst_idx]);
165
178
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
178
      size_t feed_len = std::min(digest_size - offset,
167
178
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
178
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
178
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
178
    }
171
370
  }
172
1.26k
}
173
174
2.07k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
175
2.07k
  FuzzedDataProvider fdp(data, size);
176
177
5.91k
  for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) {
178
3.83k
    switch (fdp.ConsumeIntegralInRange<int>(0, 2)) {
179
1.38k
      case 0:
180
1.38k
        fuzz_hash_int_multi<struct mhd_Md5CtxInt>(
181
1.38k
          fdp, mhd_MD5_BLOCK_SIZE,
182
1.38k
          mhd_MD5_init, mhd_MD5_update, mhd_MD5_finish, mhd_MD5_DIGEST_SIZE);
183
1.38k
        break;
184
1.18k
      case 1:
185
1.18k
        fuzz_hash_int_multi<struct mhd_Sha256CtxInt>(
186
1.18k
          fdp, mhd_SHA256_BLOCK_SIZE,
187
1.18k
          mhd_SHA256_init, mhd_SHA256_update, mhd_SHA256_finish, mhd_SHA256_DIGEST_SIZE);
188
1.18k
        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.83k
    }
196
3.83k
  }
197
2.07k
  return 0;
198
2.07k
}