Coverage Report

Created: 2025-08-28 06:19

/src/fuzz_crypto_ext.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_ext.h"
25
  #include "sha256_ext.h"
26
}
27
28
// Fuzzing target function pointer types for the enternal hash APIs
29
template <typename HashType> using InitOnceFn = void (*)(HashType*);
30
template <typename HashType> using UpdateFn   = void (*)(HashType*, size_t, const uint8_t*);
31
template <typename HashType> using FinishFn   = void (*)(HashType*, uint8_t*);
32
template <typename HashType> using DeinitFn   = void (*)(HashType*);
33
34
// Generic hashing flow that fuzz same hashing procedure for different algorithm
35
template <typename HashType>
36
static void fuzz_hash_ext_multi(FuzzedDataProvider &fdp,
37
                                size_t block_size,
38
                                InitOnceFn<HashType> init_once,
39
                                UpdateFn<HashType> update_fn,
40
                                FinishFn<HashType> finish_fn,
41
                                DeinitFn<HashType> deinit_fn,
42
2.13k
                                size_t digest_size) {
43
2.13k
  if (!fdp.remaining_bytes()) {
44
21
    return;
45
21
  }
46
47
  // Pull a random slice of data for fuzzing
48
2.11k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
49
2.11k
  std::vector<uint8_t> input_bytes = fdp.ConsumeBytes<uint8_t>(take_len);
50
51
  // Create 1 to 4 independent hashing contexts with it own digest buffer
52
2.11k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
53
2.11k
  std::vector<HashType> contexts(num_contexts);
54
2.11k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
55
6.85k
  for (unsigned i = 0; i < num_contexts; i++) {
56
4.74k
    init_once(&contexts[i]);
57
4.74k
  }
58
59
  // Intentionally misalign the data pointer to stress alignment sensitive paths
60
2.11k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
61
2.11k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
62
2.11k
  if (!input_bytes.empty()) {
63
1.45k
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
64
1.45k
  }
65
66
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
67
2.11k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
68
2.11k
  size_t remaining = input_bytes.size();
69
70
  // Perform multiple hash update iterations on the raw data
71
2.11k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
72
5.26k
  while (num_iterations-- && remaining > 0) {
73
    // Pick which context to feed this iteration
74
3.14k
    const unsigned ctx_index = (num_contexts == 1) ? 0 : fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
75
76
    // Choose a chunking pattern relative to block size.
77
3.14k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
78
3.14k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
79
80
3.14k
    size_t chunk_len = 0;
81
3.14k
    switch (pattern) {
82
657
      case LESS1: {
83
        // Consume 1 byte less from block size from the raw data for this iteration
84
657
        if (block_size > 1) {
85
657
          chunk_len = std::min(remaining, block_size - 1);
86
657
        }
87
657
        break;
88
0
      }
89
199
      case EQ: {
90
        // Consume block size bytes from the raw data for this iteration
91
199
        chunk_len = std::min(remaining, block_size);
92
199
        break;
93
0
      }
94
171
      case PLUS1: {
95
        // Consume 1 byte more from block size from the raw data for this iteration
96
171
        chunk_len = std::min(remaining, block_size + 1);
97
171
        break;
98
0
      }
99
616
      case SMALL: {
100
        // Consume 1~32 bytes from the raw data for this iteration
101
616
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
102
616
        chunk_len = std::min(remaining, small_len);
103
616
        break;
104
0
      }
105
488
      case RANDOM: {
106
        // Consume random bytes from the raw data for this iteration
107
488
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
108
488
        break;
109
0
      }
110
94
      case TAIL: {
111
        // Consume all remaining bytes from the raw data for this iteration
112
94
        chunk_len = remaining;
113
94
        break;
114
0
      }
115
923
      case HALT: {
116
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
117
923
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
118
923
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
119
2.56k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
120
1.64k
          size_t w = std::min(remaining, step);
121
1.64k
          update_fn(&contexts[ctx_index], w, cursor);
122
1.64k
          cursor += w;
123
1.64k
          remaining -= w;
124
1.64k
        }
125
126
        // Randomly reinitialise the hash stream
127
923
        if (fdp.ConsumeBool()) {
128
394
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
129
394
        }
130
923
        continue;
131
0
      }
132
3.14k
    }
133
134
2.22k
    if (chunk_len == 0 || chunk_len > remaining) {
135
0
      continue;
136
0
    }
137
138
    // Fuzz the update function
139
2.22k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
140
2.22k
    cursor += chunk_len;
141
2.22k
    remaining -= chunk_len;
142
2.22k
  }
143
144
  // Finalize all active contexts (finish_reset).
145
6.85k
  for (unsigned i = 0; i < num_contexts; i++) {
146
4.74k
    finish_fn(&contexts[i], digests[i].data());
147
4.74k
  }
148
149
  // Additional fuzzing on special context chaining approach.
150
2.11k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
151
679
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
152
679
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
153
679
    if (src_idx != dst_idx) {
154
410
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
155
410
      size_t max_avail = digest_size - offset; // >= 1
156
410
      size_t feed_len = fdp.ConsumeIntegralInRange<size_t>(1, max_avail);
157
410
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
158
410
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
159
410
    }
160
679
  }
161
162
  // Deinitialise all contexts after this iteration
163
6.85k
  for (unsigned i = 0; i < num_contexts; i++) {
164
4.74k
    deinit_fn(&contexts[i]);
165
4.74k
  }
166
2.11k
}
fuzz_crypto_ext.cpp:void fuzz_hash_ext_multi<mhd_Md5CtxExt>(FuzzedDataProvider&, unsigned long, void (*)(mhd_Md5CtxExt*), void (*)(mhd_Md5CtxExt*, unsigned long, unsigned char const*), void (*)(mhd_Md5CtxExt*, unsigned char*), void (*)(mhd_Md5CtxExt*), unsigned long)
Line
Count
Source
42
1.08k
                                size_t digest_size) {
43
1.08k
  if (!fdp.remaining_bytes()) {
44
5
    return;
45
5
  }
46
47
  // Pull a random slice of data for fuzzing
48
1.08k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
49
1.08k
  std::vector<uint8_t> input_bytes = fdp.ConsumeBytes<uint8_t>(take_len);
50
51
  // Create 1 to 4 independent hashing contexts with it own digest buffer
52
1.08k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
53
1.08k
  std::vector<HashType> contexts(num_contexts);
54
1.08k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
55
3.70k
  for (unsigned i = 0; i < num_contexts; i++) {
56
2.62k
    init_once(&contexts[i]);
57
2.62k
  }
58
59
  // Intentionally misalign the data pointer to stress alignment sensitive paths
60
1.08k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
61
1.08k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
62
1.08k
  if (!input_bytes.empty()) {
63
779
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
64
779
  }
65
66
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
67
1.08k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
68
1.08k
  size_t remaining = input_bytes.size();
69
70
  // Perform multiple hash update iterations on the raw data
71
1.08k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
72
2.80k
  while (num_iterations-- && remaining > 0) {
73
    // Pick which context to feed this iteration
74
1.71k
    const unsigned ctx_index = (num_contexts == 1) ? 0 : fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
75
76
    // Choose a chunking pattern relative to block size.
77
1.71k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
78
1.71k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
79
80
1.71k
    size_t chunk_len = 0;
81
1.71k
    switch (pattern) {
82
347
      case LESS1: {
83
        // Consume 1 byte less from block size from the raw data for this iteration
84
347
        if (block_size > 1) {
85
347
          chunk_len = std::min(remaining, block_size - 1);
86
347
        }
87
347
        break;
88
0
      }
89
105
      case EQ: {
90
        // Consume block size bytes from the raw data for this iteration
91
105
        chunk_len = std::min(remaining, block_size);
92
105
        break;
93
0
      }
94
101
      case PLUS1: {
95
        // Consume 1 byte more from block size from the raw data for this iteration
96
101
        chunk_len = std::min(remaining, block_size + 1);
97
101
        break;
98
0
      }
99
368
      case SMALL: {
100
        // Consume 1~32 bytes from the raw data for this iteration
101
368
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
102
368
        chunk_len = std::min(remaining, small_len);
103
368
        break;
104
0
      }
105
263
      case RANDOM: {
106
        // Consume random bytes from the raw data for this iteration
107
263
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
108
263
        break;
109
0
      }
110
59
      case TAIL: {
111
        // Consume all remaining bytes from the raw data for this iteration
112
59
        chunk_len = remaining;
113
59
        break;
114
0
      }
115
476
      case HALT: {
116
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
117
476
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
118
476
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
119
1.36k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
120
893
          size_t w = std::min(remaining, step);
121
893
          update_fn(&contexts[ctx_index], w, cursor);
122
893
          cursor += w;
123
893
          remaining -= w;
124
893
        }
125
126
        // Randomly reinitialise the hash stream
127
476
        if (fdp.ConsumeBool()) {
128
225
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
129
225
        }
130
476
        continue;
131
0
      }
132
1.71k
    }
133
134
1.24k
    if (chunk_len == 0 || chunk_len > remaining) {
135
0
      continue;
136
0
    }
137
138
    // Fuzz the update function
139
1.24k
    update_fn(&contexts[ctx_index], chunk_len, cursor);
140
1.24k
    cursor += chunk_len;
141
1.24k
    remaining -= chunk_len;
142
1.24k
  }
143
144
  // Finalize all active contexts (finish_reset).
145
3.70k
  for (unsigned i = 0; i < num_contexts; i++) {
146
2.62k
    finish_fn(&contexts[i], digests[i].data());
147
2.62k
  }
148
149
  // Additional fuzzing on special context chaining approach.
150
1.08k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
151
396
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
152
396
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
153
396
    if (src_idx != dst_idx) {
154
205
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
155
205
      size_t max_avail = digest_size - offset; // >= 1
156
205
      size_t feed_len = fdp.ConsumeIntegralInRange<size_t>(1, max_avail);
157
205
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
158
205
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
159
205
    }
160
396
  }
161
162
  // Deinitialise all contexts after this iteration
163
3.70k
  for (unsigned i = 0; i < num_contexts; i++) {
164
2.62k
    deinit_fn(&contexts[i]);
165
2.62k
  }
166
1.08k
}
fuzz_crypto_ext.cpp:void fuzz_hash_ext_multi<mhd_Sha256CtxExt>(FuzzedDataProvider&, unsigned long, void (*)(mhd_Sha256CtxExt*), void (*)(mhd_Sha256CtxExt*, unsigned long, unsigned char const*), void (*)(mhd_Sha256CtxExt*, unsigned char*), void (*)(mhd_Sha256CtxExt*), unsigned long)
Line
Count
Source
42
1.04k
                                size_t digest_size) {
43
1.04k
  if (!fdp.remaining_bytes()) {
44
16
    return;
45
16
  }
46
47
  // Pull a random slice of data for fuzzing
48
1.03k
  size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes());
49
1.03k
  std::vector<uint8_t> input_bytes = fdp.ConsumeBytes<uint8_t>(take_len);
50
51
  // Create 1 to 4 independent hashing contexts with it own digest buffer
52
1.03k
  const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
53
1.03k
  std::vector<HashType> contexts(num_contexts);
54
1.03k
  std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size));
55
3.15k
  for (unsigned i = 0; i < num_contexts; i++) {
56
2.12k
    init_once(&contexts[i]);
57
2.12k
  }
58
59
  // Intentionally misalign the data pointer to stress alignment sensitive paths
60
1.03k
  const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64);
61
1.03k
  std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size());
62
1.03k
  if (!input_bytes.empty()) {
63
675
    memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size());
64
675
  }
65
66
  // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations
67
1.03k
  const uint8_t *cursor = scratch_buf.data() + misalign_pad;
68
1.03k
  size_t remaining = input_bytes.size();
69
70
  // Perform multiple hash update iterations on the raw data
71
1.03k
  unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4);
72
2.46k
  while (num_iterations-- && remaining > 0) {
73
    // Pick which context to feed this iteration
74
1.42k
    const unsigned ctx_index = (num_contexts == 1) ? 0 : fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
75
76
    // Choose a chunking pattern relative to block size.
77
1.42k
    enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT };
78
1.42k
    Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT});
79
80
1.42k
    size_t chunk_len = 0;
81
1.42k
    switch (pattern) {
82
310
      case LESS1: {
83
        // Consume 1 byte less from block size from the raw data for this iteration
84
310
        if (block_size > 1) {
85
310
          chunk_len = std::min(remaining, block_size - 1);
86
310
        }
87
310
        break;
88
0
      }
89
94
      case EQ: {
90
        // Consume block size bytes from the raw data for this iteration
91
94
        chunk_len = std::min(remaining, block_size);
92
94
        break;
93
0
      }
94
70
      case PLUS1: {
95
        // Consume 1 byte more from block size from the raw data for this iteration
96
70
        chunk_len = std::min(remaining, block_size + 1);
97
70
        break;
98
0
      }
99
248
      case SMALL: {
100
        // Consume 1~32 bytes from the raw data for this iteration
101
248
        size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32);
102
248
        chunk_len = std::min(remaining, small_len);
103
248
        break;
104
0
      }
105
225
      case RANDOM: {
106
        // Consume random bytes from the raw data for this iteration
107
225
        chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0;
108
225
        break;
109
0
      }
110
35
      case TAIL: {
111
        // Consume all remaining bytes from the raw data for this iteration
112
35
        chunk_len = remaining;
113
35
        break;
114
0
      }
115
447
      case HALT: {
116
        // Consume small chunk and consider reinitialisation or early halt of the hash iteration
117
447
        size_t step  = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size));
118
447
        size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4);
119
1.20k
        for (size_t j = 0; j < loops && remaining > 0; j++) {
120
753
          size_t w = std::min(remaining, step);
121
753
          update_fn(&contexts[ctx_index], w, cursor);
122
753
          cursor += w;
123
753
          remaining -= w;
124
753
        }
125
126
        // Randomly reinitialise the hash stream
127
447
        if (fdp.ConsumeBool()) {
128
169
          finish_fn(&contexts[ctx_index], digests[ctx_index].data());
129
169
        }
130
447
        continue;
131
0
      }
132
1.42k
    }
133
134
982
    if (chunk_len == 0 || chunk_len > remaining) {
135
0
      continue;
136
0
    }
137
138
    // Fuzz the update function
139
982
    update_fn(&contexts[ctx_index], chunk_len, cursor);
140
982
    cursor += chunk_len;
141
982
    remaining -= chunk_len;
142
982
  }
143
144
  // Finalize all active contexts (finish_reset).
145
3.15k
  for (unsigned i = 0; i < num_contexts; i++) {
146
2.12k
    finish_fn(&contexts[i], digests[i].data());
147
2.12k
  }
148
149
  // Additional fuzzing on special context chaining approach.
150
1.03k
  if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) {
151
283
    unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
152
283
    unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1);
153
283
    if (src_idx != dst_idx) {
154
205
      size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1);
155
205
      size_t max_avail = digest_size - offset; // >= 1
156
205
      size_t feed_len = fdp.ConsumeIntegralInRange<size_t>(1, max_avail);
157
205
      update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset);
158
205
      finish_fn(&contexts[dst_idx], digests[dst_idx].data());
159
205
    }
160
283
  }
161
162
  // Deinitialise all contexts after this iteration
163
3.15k
  for (unsigned i = 0; i < num_contexts; i++) {
164
2.12k
    deinit_fn(&contexts[i]);
165
2.12k
  }
166
1.03k
}
167
168
1.13k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
169
1.13k
  FuzzedDataProvider fdp(data, size);
170
171
3.27k
  for (unsigned i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) {
172
2.13k
    if (fdp.ConsumeBool()) {
173
1.08k
      fuzz_hash_ext_multi<struct mhd_Md5CtxExt>(
174
1.08k
        fdp, 64,
175
1.08k
        mhd_MD5_init_one_time, mhd_MD5_update, mhd_MD5_finish_reset, mhd_MD5_deinit,
176
1.08k
        mhd_MD5_DIGEST_SIZE);
177
1.08k
    } else {
178
1.04k
      fuzz_hash_ext_multi<struct mhd_Sha256CtxExt>(
179
1.04k
        fdp, 64,
180
1.04k
        mhd_SHA256_init_one_time, mhd_SHA256_update, mhd_SHA256_finish_reset, mhd_SHA256_deinit,
181
1.04k
        mhd_SHA256_DIGEST_SIZE);
182
1.04k
    }
183
2.13k
  }
184
1.13k
  return 0;
185
1.13k
}