Coverage Report

Created: 2025-10-27 06:12

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.81k
                                size_t digest_size) {
42
3.81k
  if (!fdp.remaining_bytes()) {
43
27
    return;
44
27
  }
45
46
  // Pull a random slice of data for fuzzing
47
3.78k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
3.78k
  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.78k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
3.78k
  std::vector<HashType> contexts(num_contexts);
53
3.78k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
12.0k
  for (unsigned i = 0; i < num_contexts; i++) {
55
8.26k
    init_fn(&contexts[i]);
56
8.26k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
3.78k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
3.78k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
3.78k
  if (!input_bytes.empty()) {
62
2.88k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
2.88k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
3.78k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
3.78k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
3.78k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
10.3k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
6.51k
    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.51k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
6.51k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
6.51k
    size_t chunk_len = 0;
80
6.51k
    switch (pattern) {
81
1.65k
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
1.65k
        if (block_size > 1) {
84
1.65k
          chunk_len = std::min(remaining, block_size - 1);
85
1.65k
        }
86
1.65k
        break;
87
0
      }
88
289
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
289
        chunk_len = std::min(remaining, block_size);
91
289
        break;
92
0
      }
93
296
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
296
        chunk_len = std::min(remaining, block_size + 1);
96
296
        break;
97
0
      }
98
1.13k
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
1.13k
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
1.13k
        chunk_len = std::min(remaining, small_len);
102
1.13k
        break;
103
0
      }
104
1.21k
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
1.21k
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
1.21k
        break;
108
0
      }
109
349
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
349
        chunk_len = remaining;
112
349
        break;
113
0
      }
114
1.58k
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
1.58k
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
1.58k
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
4.73k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
3.15k
          size_t w = std::min(remaining, step);
120
3.15k
          update_fn(&contexts[ctx_index], w, cursor);
121
3.15k
          cursor += w;
122
3.15k
          remaining -= w;
123
3.15k
        }
124
125
        // Randomly reinitialise the hash stream
126
1.58k
        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.58k
        continue;
131
0
      }
132
6.51k
    }
133
4.93k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
4.93k
    if (fdp.ConsumeBool()) {
139
1.95k
      init_fn(&contexts[ctx_index]);
140
1.95k
    }
141
142
    // Fuzz the update function
143
4.93k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
4.93k
    cursor += chunk_len;
145
4.93k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
4.93k
    if (fdp.ConsumeBool()) {
149
2.03k
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
2.03k
      init_fn(&contexts[ctx_index]);
151
2.03k
    }
152
4.93k
  }
153
154
  // Fuzz the finish function for all contexts
155
12.0k
  for (unsigned i = 0; i < num_contexts; i++) {
156
8.26k
    finish_fn(&contexts[i], digests[i].data());
157
8.26k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
3.78k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
1.03k
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
1.03k
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
1.03k
    if (src_idx != dst_idx) {
164
572
      init_fn(&contexts[dst_idx]);
165
572
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
572
      size_t feed_len = std::min(digest_size - offset,
167
572
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
572
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
572
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
572
    }
171
1.03k
  }
172
3.78k
}
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.36k
                                size_t digest_size) {
42
1.36k
  if (!fdp.remaining_bytes()) {
43
16
    return;
44
16
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.35k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.35k
  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.35k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.35k
  std::vector<HashType> contexts(num_contexts);
53
1.35k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
4.30k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.94k
    init_fn(&contexts[i]);
56
2.94k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.35k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.35k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.35k
  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.35k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.35k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.35k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.64k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.29k
    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.29k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.29k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.29k
    size_t chunk_len = 0;
80
2.29k
    switch (pattern) {
81
590
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
590
        if (block_size > 1) {
84
590
          chunk_len = std::min(remaining, block_size - 1);
85
590
        }
86
590
        break;
87
0
      }
88
100
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
100
        chunk_len = std::min(remaining, block_size);
91
100
        break;
92
0
      }
93
91
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
91
        chunk_len = std::min(remaining, block_size + 1);
96
91
        break;
97
0
      }
98
451
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
451
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
451
        chunk_len = std::min(remaining, small_len);
102
451
        break;
103
0
      }
104
433
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
433
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
433
        break;
108
0
      }
109
106
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
106
        chunk_len = remaining;
112
106
        break;
113
0
      }
114
523
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
523
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
523
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.56k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.03k
          size_t w = std::min(remaining, step);
120
1.03k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.03k
          cursor += w;
122
1.03k
          remaining -= w;
123
1.03k
        }
124
125
        // Randomly reinitialise the hash stream
126
523
        if (fdp.ConsumeBool()) {
127
205
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
205
          init_fn(&contexts[ctx_index]);
129
205
        }
130
523
        continue;
131
0
      }
132
2.29k
    }
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
786
      init_fn(&contexts[ctx_index]);
140
786
    }
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
807
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
807
      init_fn(&contexts[ctx_index]);
151
807
    }
152
1.77k
  }
153
154
  // Fuzz the finish function for all contexts
155
4.30k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.94k
    finish_fn(&contexts[i], digests[i].data());
157
2.94k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.35k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
380
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
380
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
380
    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
380
  }
172
1.35k
}
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
6
    return;
44
6
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.17k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.17k
  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.17k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.17k
  std::vector<HashType> contexts(num_contexts);
53
1.17k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
3.62k
  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.17k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.17k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.17k
  if (!input_bytes.empty()) {
62
875
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
875
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.17k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.17k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.17k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.12k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
1.94k
    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.94k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
1.94k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
1.94k
    size_t chunk_len = 0;
80
1.94k
    switch (pattern) {
81
474
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
474
        if (block_size > 1) {
84
474
          chunk_len = std::min(remaining, block_size - 1);
85
474
        }
86
474
        break;
87
0
      }
88
104
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
104
        chunk_len = std::min(remaining, block_size);
91
104
        break;
92
0
      }
93
83
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
83
        chunk_len = std::min(remaining, block_size + 1);
96
83
        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
362
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
362
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
362
        break;
108
0
      }
109
106
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
106
        chunk_len = remaining;
112
106
        break;
113
0
      }
114
486
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
486
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
486
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.39k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
913
          size_t w = std::min(remaining, step);
120
913
          update_fn(&contexts[ctx_index], w, cursor);
121
913
          cursor += w;
122
913
          remaining -= w;
123
913
        }
124
125
        // Randomly reinitialise the hash stream
126
486
        if (fdp.ConsumeBool()) {
127
188
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
188
          init_fn(&contexts[ctx_index]);
129
188
        }
130
486
        continue;
131
0
      }
132
1.94k
    }
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
500
      init_fn(&contexts[ctx_index]);
140
500
    }
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
555
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
555
      init_fn(&contexts[ctx_index]);
151
555
    }
152
1.46k
  }
153
154
  // Fuzz the finish function for all contexts
155
3.62k
  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.17k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
309
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
309
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
309
    if (src_idx != dst_idx) {
164
200
      init_fn(&contexts[dst_idx]);
165
200
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
200
      size_t feed_len = std::min(digest_size - offset,
167
200
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
200
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
200
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
200
    }
171
309
  }
172
1.17k
}
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
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
4.12k
  for (unsigned i = 0; i < num_contexts; i++) {
55
2.86k
    init_fn(&contexts[i]);
56
2.86k
  }
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.53k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.27k
    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.27k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.27k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.27k
    size_t chunk_len = 0;
80
2.27k
    switch (pattern) {
81
586
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
586
        if (block_size > 1) {
84
586
          chunk_len = std::min(remaining, block_size - 1);
85
586
        }
86
586
        break;
87
0
      }
88
85
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
85
        chunk_len = std::min(remaining, block_size);
91
85
        break;
92
0
      }
93
122
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
122
        chunk_len = std::min(remaining, block_size + 1);
96
122
        break;
97
0
      }
98
350
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
350
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
350
        chunk_len = std::min(remaining, small_len);
102
350
        break;
103
0
      }
104
423
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
423
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
423
        break;
108
0
      }
109
137
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
137
        chunk_len = remaining;
112
137
        break;
113
0
      }
114
574
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
574
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
574
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.77k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.20k
          size_t w = std::min(remaining, step);
120
1.20k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.20k
          cursor += w;
122
1.20k
          remaining -= w;
123
1.20k
        }
124
125
        // Randomly reinitialise the hash stream
126
574
        if (fdp.ConsumeBool()) {
127
250
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
250
          init_fn(&contexts[ctx_index]);
129
250
        }
130
574
        continue;
131
0
      }
132
2.27k
    }
133
1.70k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.70k
    if (fdp.ConsumeBool()) {
139
669
      init_fn(&contexts[ctx_index]);
140
669
    }
141
142
    // Fuzz the update function
143
1.70k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.70k
    cursor += chunk_len;
145
1.70k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.70k
    if (fdp.ConsumeBool()) {
149
672
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
672
      init_fn(&contexts[ctx_index]);
151
672
    }
152
1.70k
  }
153
154
  // Fuzz the finish function for all contexts
155
4.12k
  for (unsigned i = 0; i < num_contexts; i++) {
156
2.86k
    finish_fn(&contexts[i], digests[i].data());
157
2.86k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.26k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
350
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
350
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
350
    if (src_idx != dst_idx) {
164
173
      init_fn(&contexts[dst_idx]);
165
173
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
173
      size_t feed_len = std::min(digest_size - offset,
167
173
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
173
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
173
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
173
    }
171
350
  }
172
1.26k
}
173
174
2.10k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
175
2.10k
  FuzzedDataProvider fdp(data, size);
176
177
5.91k
  for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) {
178
3.81k
    switch (fdp.ConsumeIntegralInRange<int>(0, 2)) {
179
1.36k
      case 0:
180
1.36k
        fuzz_hash_int_multi<struct mhd_Md5CtxInt>(
181
1.36k
          fdp, mhd_MD5_BLOCK_SIZE,
182
1.36k
          mhd_MD5_init, mhd_MD5_update, mhd_MD5_finish, mhd_MD5_DIGEST_SIZE);
183
1.36k
        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.81k
    }
196
3.81k
  }
197
2.10k
  return 0;
198
2.10k
}