Coverage Report

Created: 2025-08-28 06:19

/src/fuzz_crypto_int.cpp
Line
Count
Source (jump to first uncovered line)
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
4.30k
                                size_t digest_size) {
42
4.30k
  if (!fdp.remaining_bytes()) {
43
39
    return;
44
39
  }
45
46
  // Pull a random slice of data for fuzzing
47
4.26k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
4.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
4.26k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
4.26k
  std::vector<HashType> contexts(num_contexts);
53
4.26k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
14.2k
  for (unsigned i = 0; i < num_contexts; i++) {
55
10.0k
    init_fn(&contexts[i]);
56
10.0k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
4.26k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
4.26k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
4.26k
  if (!input_bytes.empty()) {
62
3.28k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
3.28k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
4.26k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
4.26k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
4.26k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
11.7k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
7.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
7.51k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
7.51k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
7.51k
    size_t chunk_len = 0;
80
7.51k
    switch (pattern) {
81
1.78k
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
1.78k
        if (block_size > 1) {
84
1.78k
          chunk_len = std::min(remaining, block_size - 1);
85
1.78k
        }
86
1.78k
        break;
87
0
      }
88
359
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
359
        chunk_len = std::min(remaining, block_size);
91
359
        break;
92
0
      }
93
354
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
354
        chunk_len = std::min(remaining, block_size + 1);
96
354
        break;
97
0
      }
98
1.24k
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
1.24k
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
1.24k
        chunk_len = std::min(remaining, small_len);
102
1.24k
        break;
103
0
      }
104
1.51k
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
1.51k
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
1.51k
        break;
108
0
      }
109
310
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
310
        chunk_len = remaining;
112
310
        break;
113
0
      }
114
1.95k
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
1.95k
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
1.95k
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
5.98k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
4.03k
          size_t w = std::min(remaining, step);
120
4.03k
          update_fn(&contexts[ctx_index], w, cursor);
121
4.03k
          cursor += w;
122
4.03k
          remaining -= w;
123
4.03k
        }
124
125
        // Randomly reinitialise the hash stream
126
1.95k
        if (fdp.ConsumeBool()) {
127
1.06k
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
1.06k
          init_fn(&contexts[ctx_index]);
129
1.06k
        }
130
1.95k
        continue;
131
0
      }
132
7.51k
    }
133
5.56k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
5.56k
    if (fdp.ConsumeBool()) {
139
2.55k
      init_fn(&contexts[ctx_index]);
140
2.55k
    }
141
142
    // Fuzz the update function
143
5.56k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
5.56k
    cursor += chunk_len;
145
5.56k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
5.56k
    if (fdp.ConsumeBool()) {
149
2.56k
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
2.56k
      init_fn(&contexts[ctx_index]);
151
2.56k
    }
152
5.56k
  }
153
154
  // Fuzz the finish function for all contexts
155
14.2k
  for (unsigned i = 0; i < num_contexts; i++) {
156
10.0k
    finish_fn(&contexts[i], digests[i].data());
157
10.0k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
4.26k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
1.28k
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
1.28k
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
1.28k
    if (src_idx != dst_idx) {
164
564
      init_fn(&contexts[dst_idx]);
165
564
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
564
      size_t feed_len = std::min(digest_size - offset,
167
564
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
564
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
564
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
564
    }
171
1.28k
  }
172
4.26k
}
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.56k
                                size_t digest_size) {
42
1.56k
  if (!fdp.remaining_bytes()) {
43
26
    return;
44
26
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.53k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.53k
  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.53k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.53k
  std::vector<HashType> contexts(num_contexts);
53
1.53k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
5.11k
  for (unsigned i = 0; i < num_contexts; i++) {
55
3.58k
    init_fn(&contexts[i]);
56
3.58k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.53k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.53k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.53k
  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.53k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.53k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.53k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
4.18k
  while (num_iterations-- && remaining > 0) {
72
    // Pick which context to feed this iteration
73
2.64k
    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.64k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
77
2.64k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
78
79
2.64k
    size_t chunk_len = 0;
80
2.64k
    switch (pattern) {
81
640
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
640
        if (block_size > 1) {
84
640
          chunk_len = std::min(remaining, block_size - 1);
85
640
        }
86
640
        break;
87
0
      }
88
130
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
130
        chunk_len = std::min(remaining, block_size);
91
130
        break;
92
0
      }
93
148
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
148
        chunk_len = std::min(remaining, block_size + 1);
96
148
        break;
97
0
      }
98
535
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
535
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
535
        chunk_len = std::min(remaining, small_len);
102
535
        break;
103
0
      }
104
465
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
465
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
465
        break;
108
0
      }
109
94
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
94
        chunk_len = remaining;
112
94
        break;
113
0
      }
114
637
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
637
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
637
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
1.92k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.28k
          size_t w = std::min(remaining, step);
120
1.28k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.28k
          cursor += w;
122
1.28k
          remaining -= w;
123
1.28k
        }
124
125
        // Randomly reinitialise the hash stream
126
637
        if (fdp.ConsumeBool()) {
127
327
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
327
          init_fn(&contexts[ctx_index]);
129
327
        }
130
637
        continue;
131
0
      }
132
2.64k
    }
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
976
      init_fn(&contexts[ctx_index]);
140
976
    }
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
954
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
954
      init_fn(&contexts[ctx_index]);
151
954
    }
152
2.01k
  }
153
154
  // Fuzz the finish function for all contexts
155
5.11k
  for (unsigned i = 0; i < num_contexts; i++) {
156
3.58k
    finish_fn(&contexts[i], digests[i].data());
157
3.58k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.53k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
471
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
471
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
471
    if (src_idx != dst_idx) {
164
209
      init_fn(&contexts[dst_idx]);
165
209
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
209
      size_t feed_len = std::min(digest_size - offset,
167
209
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
209
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
209
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
209
    }
171
471
  }
172
1.53k
}
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.29k
                                size_t digest_size) {
42
1.29k
  if (!fdp.remaining_bytes()) {
43
4
    return;
44
4
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.28k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.28k
  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.28k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.28k
  std::vector<HashType> contexts(num_contexts);
53
1.28k
  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
3.01k
    init_fn(&contexts[i]);
56
3.01k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.28k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.28k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.28k
  if (!input_bytes.empty()) {
62
984
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
984
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.28k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.28k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.28k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
3.56k
  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
530
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
530
        if (block_size > 1) {
84
530
          chunk_len = std::min(remaining, block_size - 1);
85
530
        }
86
530
        break;
87
0
      }
88
117
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
117
        chunk_len = std::min(remaining, block_size);
91
117
        break;
92
0
      }
93
103
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
103
        chunk_len = std::min(remaining, block_size + 1);
96
103
        break;
97
0
      }
98
375
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
375
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
375
        chunk_len = std::min(remaining, small_len);
102
375
        break;
103
0
      }
104
404
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
404
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
404
        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
623
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
623
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
623
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
2.01k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.39k
          size_t w = std::min(remaining, step);
120
1.39k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.39k
          cursor += w;
122
1.39k
          remaining -= w;
123
1.39k
        }
124
125
        // Randomly reinitialise the hash stream
126
623
        if (fdp.ConsumeBool()) {
127
358
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
358
          init_fn(&contexts[ctx_index]);
129
358
        }
130
623
        continue;
131
0
      }
132
2.27k
    }
133
1.64k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.64k
    if (fdp.ConsumeBool()) {
139
780
      init_fn(&contexts[ctx_index]);
140
780
    }
141
142
    // Fuzz the update function
143
1.64k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.64k
    cursor += chunk_len;
145
1.64k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.64k
    if (fdp.ConsumeBool()) {
149
776
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
776
      init_fn(&contexts[ctx_index]);
151
776
    }
152
1.64k
  }
153
154
  // Fuzz the finish function for all contexts
155
4.30k
  for (unsigned i = 0; i < num_contexts; i++) {
156
3.01k
    finish_fn(&contexts[i], digests[i].data());
157
3.01k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.28k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
388
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
388
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
388
    if (src_idx != dst_idx) {
164
186
      init_fn(&contexts[dst_idx]);
165
186
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
186
      size_t feed_len = std::min(digest_size - offset,
167
186
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
186
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
186
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
186
    }
171
388
  }
172
1.28k
}
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.44k
                                size_t digest_size) {
42
1.44k
  if (!fdp.remaining_bytes()) {
43
9
    return;
44
9
  }
45
46
  // Pull a random slice of data for fuzzing
47
1.43k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
48
1.43k
  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.43k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
52
1.43k
  std::vector<HashType> contexts(num_contexts);
53
1.43k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
54
4.85k
  for (unsigned i = 0; i < num_contexts; i++) {
55
3.41k
    init_fn(&contexts[i]);
56
3.41k
  }
57
58
  // Intentionally misalign the data pointer to stress alignment sensitive paths
59
1.43k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
60
1.43k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
61
1.43k
  if (!input_bytes.empty()) {
62
1.13k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
63
1.13k
  }
64
65
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
66
1.43k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
67
1.43k
  size_t remaining = input_bytes.size();
68
69
  // Perform multiple hash update iterations on the raw data
70
1.43k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
71
4.02k
  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
610
      case LESS1: {
82
        // Consume 1 byte less from block size from the raw data for this iteration
83
610
        if (block_size > 1) {
84
610
          chunk_len = std::min(remaining, block_size - 1);
85
610
        }
86
610
        break;
87
0
      }
88
112
      case EQ: {
89
        // Consume block size bytes from the raw data for this iteration
90
112
        chunk_len = std::min(remaining, block_size);
91
112
        break;
92
0
      }
93
103
      case PLUS1: {
94
        // Consume 1 byte more from block size from the raw data for this iteration
95
103
        chunk_len = std::min(remaining, block_size + 1);
96
103
        break;
97
0
      }
98
331
      case SMALL: {
99
        // Consume 1 to 32 bytes from the raw data for this iteration
100
331
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
101
331
        chunk_len = std::min(remaining, small_len);
102
331
        break;
103
0
      }
104
648
      case RANDOM: {
105
        // Consume random bytes from the raw data for this iteration
106
648
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
107
648
        break;
108
0
      }
109
96
      case TAIL: {
110
        // Consume all remaining bytes from the raw data for this iteration
111
96
        chunk_len = remaining;
112
96
        break;
113
0
      }
114
692
      case HALT: {
115
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
116
692
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
117
692
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
118
2.05k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
119
1.36k
          size_t w = std::min(remaining, step);
120
1.36k
          update_fn(&contexts[ctx_index], w, cursor);
121
1.36k
          cursor += w;
122
1.36k
          remaining -= w;
123
1.36k
        }
124
125
        // Randomly reinitialise the hash stream
126
692
        if (fdp.ConsumeBool()) {
127
379
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
128
379
          init_fn(&contexts[ctx_index]);
129
379
        }
130
692
        continue;
131
0
      }
132
2.59k
    }
133
1.90k
    if (chunk_len == 0 || chunk_len > remaining) {
134
0
      continue;
135
0
    }
136
137
    // Occasionally reinitialise a context between update iterations
138
1.90k
    if (fdp.ConsumeBool()) {
139
799
      init_fn(&contexts[ctx_index]);
140
799
    }
141
142
    // Fuzz the update function
143
1.90k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
144
1.90k
    cursor += chunk_len;
145
1.90k
    remaining -= chunk_len;
146
147
    // Randomly halt and reinitialise the stream
148
1.90k
    if (fdp.ConsumeBool()) {
149
831
      finish_fn(&contexts[ctx_index], digests[ctx_index].data());
150
831
      init_fn(&contexts[ctx_index]);
151
831
    }
152
1.90k
  }
153
154
  // Fuzz the finish function for all contexts
155
4.85k
  for (unsigned i = 0; i < num_contexts; i++) {
156
3.41k
    finish_fn(&contexts[i], digests[i].data());
157
3.41k
  }
158
159
  // Additional fuzzing on special context chaining approach
160
1.43k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
161
430
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
162
430
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
163
430
    if (src_idx != dst_idx) {
164
169
      init_fn(&contexts[dst_idx]);
165
169
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
166
169
      size_t feed_len = std::min(digest_size - offset,
167
169
                                 (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size));
168
169
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
169
169
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
170
169
    }
171
430
  }
172
1.43k
}
173
174
2.29k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
175
2.29k
  FuzzedDataProvider fdp(data, size);
176
177
6.60k
  for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) {
178
4.30k
    switch (fdp.ConsumeIntegralInRange<int>(0, 2)) {
179
1.56k
      case 0:
180
1.56k
        fuzz_hash_int_multi<struct mhd_Md5CtxInt>(
181
1.56k
          fdp, mhd_MD5_BLOCK_SIZE,
182
1.56k
          mhd_MD5_init, mhd_MD5_update, mhd_MD5_finish, mhd_MD5_DIGEST_SIZE);
183
1.56k
        break;
184
1.29k
      case 1:
185
1.29k
        fuzz_hash_int_multi<struct mhd_Sha256CtxInt>(
186
1.29k
          fdp, mhd_SHA256_BLOCK_SIZE,
187
1.29k
          mhd_SHA256_init, mhd_SHA256_update, mhd_SHA256_finish, mhd_SHA256_DIGEST_SIZE);
188
1.29k
        break;
189
1.44k
      case 2:
190
1.44k
      default:
191
1.44k
        fuzz_hash_int_multi<struct mhd_Sha512_256CtxInt>(
192
1.44k
          fdp, mhd_SHA512_256_BLOCK_SIZE,
193
1.44k
          mhd_SHA512_256_init, mhd_SHA512_256_update, mhd_SHA512_256_finish, mhd_SHA512_256_DIGEST_SIZE);
194
1.44k
        break;
195
4.30k
    }
196
4.30k
  }
197
2.29k
  return 0;
198
2.29k
}