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.28k | size_t digest_size) { |
42 | 4.28k | if (!fdp.remaining_bytes()) { |
43 | 41 | return; |
44 | 41 | } |
45 | | |
46 | | // Pull a random slice of data for fuzzing |
47 | 4.24k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); |
48 | 4.24k | 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.24k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); |
52 | 4.24k | std::vector<HashType> contexts(num_contexts); |
53 | 4.24k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); |
54 | 14.5k | for (unsigned i = 0; i < num_contexts; i++) { |
55 | 10.3k | init_fn(&contexts[i]); |
56 | 10.3k | } |
57 | | |
58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths |
59 | 4.24k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); |
60 | 4.24k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); |
61 | 4.24k | if (!input_bytes.empty()) { |
62 | 3.29k | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); |
63 | 3.29k | } |
64 | | |
65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations |
66 | 4.24k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; |
67 | 4.24k | size_t remaining = input_bytes.size(); |
68 | | |
69 | | // Perform multiple hash update iterations on the raw data |
70 | 4.24k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); |
71 | 11.8k | while (num_iterations-- && remaining > 0) { |
72 | | // Pick which context to feed this iteration |
73 | 7.63k | 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.63k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; |
77 | 7.63k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); |
78 | | |
79 | 7.63k | size_t chunk_len = 0; |
80 | 7.63k | switch (pattern) { |
81 | 1.68k | case LESS1: { |
82 | | // Consume 1 byte less from block size from the raw data for this iteration |
83 | 1.68k | if (block_size > 1) { |
84 | 1.68k | chunk_len = std::min(remaining, block_size - 1); |
85 | 1.68k | } |
86 | 1.68k | break; |
87 | 0 | } |
88 | 418 | case EQ: { |
89 | | // Consume block size bytes from the raw data for this iteration |
90 | 418 | chunk_len = std::min(remaining, block_size); |
91 | 418 | break; |
92 | 0 | } |
93 | 381 | case PLUS1: { |
94 | | // Consume 1 byte more from block size from the raw data for this iteration |
95 | 381 | chunk_len = std::min(remaining, block_size + 1); |
96 | 381 | break; |
97 | 0 | } |
98 | 1.28k | case SMALL: { |
99 | | // Consume 1 to 32 bytes from the raw data for this iteration |
100 | 1.28k | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); |
101 | 1.28k | chunk_len = std::min(remaining, small_len); |
102 | 1.28k | break; |
103 | 0 | } |
104 | 1.55k | case RANDOM: { |
105 | | // Consume random bytes from the raw data for this iteration |
106 | 1.55k | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; |
107 | 1.55k | break; |
108 | 0 | } |
109 | 291 | case TAIL: { |
110 | | // Consume all remaining bytes from the raw data for this iteration |
111 | 291 | chunk_len = remaining; |
112 | 291 | break; |
113 | 0 | } |
114 | 2.02k | case HALT: { |
115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration |
116 | 2.02k | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); |
117 | 2.02k | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); |
118 | 6.56k | for (size_t j = 0; j < loops && remaining > 0; j++) { |
119 | 4.54k | size_t w = std::min(remaining, step); |
120 | 4.54k | update_fn(&contexts[ctx_index], w, cursor); |
121 | 4.54k | cursor += w; |
122 | 4.54k | remaining -= w; |
123 | 4.54k | } |
124 | | |
125 | | // Randomly reinitialise the hash stream |
126 | 2.02k | if (fdp.ConsumeBool()) { |
127 | 1.21k | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); |
128 | 1.21k | init_fn(&contexts[ctx_index]); |
129 | 1.21k | } |
130 | 2.02k | continue; |
131 | 0 | } |
132 | 7.63k | } |
133 | 5.61k | if (chunk_len == 0 || chunk_len > remaining) { |
134 | 0 | continue; |
135 | 0 | } |
136 | | |
137 | | // Occasionally reinitialise a context between update iterations |
138 | 5.61k | if (fdp.ConsumeBool()) { |
139 | 2.72k | init_fn(&contexts[ctx_index]); |
140 | 2.72k | } |
141 | | |
142 | | // Fuzz the update function |
143 | 5.61k | update_fn(&contexts[ctx_index], chunk_len, cursor); |
144 | 5.61k | cursor += chunk_len; |
145 | 5.61k | remaining -= chunk_len; |
146 | | |
147 | | // Randomly halt and reinitialise the stream |
148 | 5.61k | if (fdp.ConsumeBool()) { |
149 | 2.75k | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); |
150 | 2.75k | init_fn(&contexts[ctx_index]); |
151 | 2.75k | } |
152 | 5.61k | } |
153 | | |
154 | | // Fuzz the finish function for all contexts |
155 | 14.5k | for (unsigned i = 0; i < num_contexts; i++) { |
156 | 10.3k | finish_fn(&contexts[i], digests[i].data()); |
157 | 10.3k | } |
158 | | |
159 | | // Additional fuzzing on special context chaining approach |
160 | 4.24k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { |
161 | 1.35k | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); |
162 | 1.35k | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); |
163 | 1.35k | if (src_idx != dst_idx) { |
164 | 509 | init_fn(&contexts[dst_idx]); |
165 | 509 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1); |
166 | 509 | size_t feed_len = std::min(digest_size - offset, |
167 | 509 | (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size)); |
168 | 509 | update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset); |
169 | 509 | finish_fn(&contexts[dst_idx], digests[dst_idx].data()); |
170 | 509 | } |
171 | 1.35k | } |
172 | 4.24k | } 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.58k | size_t digest_size) { | 42 | 1.58k | if (!fdp.remaining_bytes()) { | 43 | 32 | return; | 44 | 32 | } | 45 | | | 46 | | // Pull a random slice of data for fuzzing | 47 | 1.55k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); | 48 | 1.55k | 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.55k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 52 | 1.55k | std::vector<HashType> contexts(num_contexts); | 53 | 1.55k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); | 54 | 5.37k | for (unsigned i = 0; i < num_contexts; i++) { | 55 | 3.81k | init_fn(&contexts[i]); | 56 | 3.81k | } | 57 | | | 58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths | 59 | 1.55k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); | 60 | 1.55k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); | 61 | 1.55k | if (!input_bytes.empty()) { | 62 | 1.21k | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); | 63 | 1.21k | } | 64 | | | 65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations | 66 | 1.55k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; | 67 | 1.55k | size_t remaining = input_bytes.size(); | 68 | | | 69 | | // Perform multiple hash update iterations on the raw data | 70 | 1.55k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 71 | 4.40k | while (num_iterations-- && remaining > 0) { | 72 | | // Pick which context to feed this iteration | 73 | 2.85k | 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.85k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; | 77 | 2.85k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); | 78 | | | 79 | 2.85k | size_t chunk_len = 0; | 80 | 2.85k | switch (pattern) { | 81 | 628 | case LESS1: { | 82 | | // Consume 1 byte less from block size from the raw data for this iteration | 83 | 628 | if (block_size > 1) { | 84 | 628 | chunk_len = std::min(remaining, block_size - 1); | 85 | 628 | } | 86 | 628 | break; | 87 | 0 | } | 88 | 144 | case EQ: { | 89 | | // Consume block size bytes from the raw data for this iteration | 90 | 144 | chunk_len = std::min(remaining, block_size); | 91 | 144 | break; | 92 | 0 | } | 93 | 162 | case PLUS1: { | 94 | | // Consume 1 byte more from block size from the raw data for this iteration | 95 | 162 | chunk_len = std::min(remaining, block_size + 1); | 96 | 162 | break; | 97 | 0 | } | 98 | 601 | case SMALL: { | 99 | | // Consume 1 to 32 bytes from the raw data for this iteration | 100 | 601 | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); | 101 | 601 | chunk_len = std::min(remaining, small_len); | 102 | 601 | break; | 103 | 0 | } | 104 | 464 | case RANDOM: { | 105 | | // Consume random bytes from the raw data for this iteration | 106 | 464 | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; | 107 | 464 | break; | 108 | 0 | } | 109 | 97 | case TAIL: { | 110 | | // Consume all remaining bytes from the raw data for this iteration | 111 | 97 | chunk_len = remaining; | 112 | 97 | break; | 113 | 0 | } | 114 | 755 | case HALT: { | 115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration | 116 | 755 | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); | 117 | 755 | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); | 118 | 2.55k | for (size_t j = 0; j < loops && remaining > 0; j++) { | 119 | 1.80k | size_t w = std::min(remaining, step); | 120 | 1.80k | update_fn(&contexts[ctx_index], w, cursor); | 121 | 1.80k | cursor += w; | 122 | 1.80k | remaining -= w; | 123 | 1.80k | } | 124 | | | 125 | | // Randomly reinitialise the hash stream | 126 | 755 | if (fdp.ConsumeBool()) { | 127 | 471 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 128 | 471 | init_fn(&contexts[ctx_index]); | 129 | 471 | } | 130 | 755 | continue; | 131 | 0 | } | 132 | 2.85k | } | 133 | 2.09k | if (chunk_len == 0 || chunk_len > remaining) { | 134 | 0 | continue; | 135 | 0 | } | 136 | | | 137 | | // Occasionally reinitialise a context between update iterations | 138 | 2.09k | if (fdp.ConsumeBool()) { | 139 | 1.11k | init_fn(&contexts[ctx_index]); | 140 | 1.11k | } | 141 | | | 142 | | // Fuzz the update function | 143 | 2.09k | update_fn(&contexts[ctx_index], chunk_len, cursor); | 144 | 2.09k | cursor += chunk_len; | 145 | 2.09k | remaining -= chunk_len; | 146 | | | 147 | | // Randomly halt and reinitialise the stream | 148 | 2.09k | if (fdp.ConsumeBool()) { | 149 | 1.12k | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 150 | 1.12k | init_fn(&contexts[ctx_index]); | 151 | 1.12k | } | 152 | 2.09k | } | 153 | | | 154 | | // Fuzz the finish function for all contexts | 155 | 5.37k | for (unsigned i = 0; i < num_contexts; i++) { | 156 | 3.81k | finish_fn(&contexts[i], digests[i].data()); | 157 | 3.81k | } | 158 | | | 159 | | // Additional fuzzing on special context chaining approach | 160 | 1.55k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { | 161 | 542 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 162 | 542 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 163 | 542 | if (src_idx != dst_idx) { | 164 | 166 | init_fn(&contexts[dst_idx]); | 165 | 166 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1); | 166 | 166 | size_t feed_len = std::min(digest_size - offset, | 167 | 166 | (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size)); | 168 | 166 | update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset); | 169 | 166 | finish_fn(&contexts[dst_idx], digests[dst_idx].data()); | 170 | 166 | } | 171 | 542 | } | 172 | 1.55k | } |
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.31k | size_t digest_size) { | 42 | 1.31k | if (!fdp.remaining_bytes()) { | 43 | 5 | return; | 44 | 5 | } | 45 | | | 46 | | // Pull a random slice of data for fuzzing | 47 | 1.30k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); | 48 | 1.30k | 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.30k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 52 | 1.30k | std::vector<HashType> contexts(num_contexts); | 53 | 1.30k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); | 54 | 4.42k | for (unsigned i = 0; i < num_contexts; i++) { | 55 | 3.11k | init_fn(&contexts[i]); | 56 | 3.11k | } | 57 | | | 58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths | 59 | 1.30k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); | 60 | 1.30k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); | 61 | 1.30k | if (!input_bytes.empty()) { | 62 | 995 | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); | 63 | 995 | } | 64 | | | 65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations | 66 | 1.30k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; | 67 | 1.30k | size_t remaining = input_bytes.size(); | 68 | | | 69 | | // Perform multiple hash update iterations on the raw data | 70 | 1.30k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 71 | 3.63k | 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 | 509 | case LESS1: { | 82 | | // Consume 1 byte less from block size from the raw data for this iteration | 83 | 509 | if (block_size > 1) { | 84 | 509 | chunk_len = std::min(remaining, block_size - 1); | 85 | 509 | } | 86 | 509 | break; | 87 | 0 | } | 88 | 147 | case EQ: { | 89 | | // Consume block size bytes from the raw data for this iteration | 90 | 147 | chunk_len = std::min(remaining, block_size); | 91 | 147 | break; | 92 | 0 | } | 93 | 107 | case PLUS1: { | 94 | | // Consume 1 byte more from block size from the raw data for this iteration | 95 | 107 | chunk_len = std::min(remaining, block_size + 1); | 96 | 107 | break; | 97 | 0 | } | 98 | 372 | case SMALL: { | 99 | | // Consume 1 to 32 bytes from the raw data for this iteration | 100 | 372 | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); | 101 | 372 | chunk_len = std::min(remaining, small_len); | 102 | 372 | break; | 103 | 0 | } | 104 | 449 | case RANDOM: { | 105 | | // Consume random bytes from the raw data for this iteration | 106 | 449 | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; | 107 | 449 | 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 | 652 | case HALT: { | 115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration | 116 | 652 | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); | 117 | 652 | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); | 118 | 2.17k | for (size_t j = 0; j < loops && remaining > 0; j++) { | 119 | 1.52k | size_t w = std::min(remaining, step); | 120 | 1.52k | update_fn(&contexts[ctx_index], w, cursor); | 121 | 1.52k | cursor += w; | 122 | 1.52k | remaining -= w; | 123 | 1.52k | } | 124 | | | 125 | | // Randomly reinitialise the hash stream | 126 | 652 | if (fdp.ConsumeBool()) { | 127 | 410 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 128 | 410 | init_fn(&contexts[ctx_index]); | 129 | 410 | } | 130 | 652 | continue; | 131 | 0 | } | 132 | 2.33k | } | 133 | 1.67k | if (chunk_len == 0 || chunk_len > remaining) { | 134 | 0 | continue; | 135 | 0 | } | 136 | | | 137 | | // Occasionally reinitialise a context between update iterations | 138 | 1.67k | if (fdp.ConsumeBool()) { | 139 | 809 | init_fn(&contexts[ctx_index]); | 140 | 809 | } | 141 | | | 142 | | // Fuzz the update function | 143 | 1.67k | update_fn(&contexts[ctx_index], chunk_len, cursor); | 144 | 1.67k | cursor += chunk_len; | 145 | 1.67k | remaining -= chunk_len; | 146 | | | 147 | | // Randomly halt and reinitialise the stream | 148 | 1.67k | if (fdp.ConsumeBool()) { | 149 | 825 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 150 | 825 | init_fn(&contexts[ctx_index]); | 151 | 825 | } | 152 | 1.67k | } | 153 | | | 154 | | // Fuzz the finish function for all contexts | 155 | 4.42k | for (unsigned i = 0; i < num_contexts; i++) { | 156 | 3.11k | finish_fn(&contexts[i], digests[i].data()); | 157 | 3.11k | } | 158 | | | 159 | | // Additional fuzzing on special context chaining approach | 160 | 1.30k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { | 161 | 394 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 162 | 394 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 163 | 394 | if (src_idx != dst_idx) { | 164 | 174 | init_fn(&contexts[dst_idx]); | 165 | 174 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1); | 166 | 174 | size_t feed_len = std::min(digest_size - offset, | 167 | 174 | (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size)); | 168 | 174 | update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset); | 169 | 174 | finish_fn(&contexts[dst_idx], digests[dst_idx].data()); | 170 | 174 | } | 171 | 394 | } | 172 | 1.30k | } |
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.39k | size_t digest_size) { | 42 | 1.39k | if (!fdp.remaining_bytes()) { | 43 | 4 | return; | 44 | 4 | } | 45 | | | 46 | | // Pull a random slice of data for fuzzing | 47 | 1.38k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); | 48 | 1.38k | 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.38k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 52 | 1.38k | std::vector<HashType> contexts(num_contexts); | 53 | 1.38k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); | 54 | 4.76k | for (unsigned i = 0; i < num_contexts; i++) { | 55 | 3.37k | init_fn(&contexts[i]); | 56 | 3.37k | } | 57 | | | 58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths | 59 | 1.38k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); | 60 | 1.38k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); | 61 | 1.38k | if (!input_bytes.empty()) { | 62 | 1.08k | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); | 63 | 1.08k | } | 64 | | | 65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations | 66 | 1.38k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; | 67 | 1.38k | size_t remaining = input_bytes.size(); | 68 | | | 69 | | // Perform multiple hash update iterations on the raw data | 70 | 1.38k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 71 | 3.83k | while (num_iterations-- && remaining > 0) { | 72 | | // Pick which context to feed this iteration | 73 | 2.45k | 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.45k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; | 77 | 2.45k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); | 78 | | | 79 | 2.45k | size_t chunk_len = 0; | 80 | 2.45k | switch (pattern) { | 81 | 551 | case LESS1: { | 82 | | // Consume 1 byte less from block size from the raw data for this iteration | 83 | 551 | if (block_size > 1) { | 84 | 551 | chunk_len = std::min(remaining, block_size - 1); | 85 | 551 | } | 86 | 551 | break; | 87 | 0 | } | 88 | 127 | case EQ: { | 89 | | // Consume block size bytes from the raw data for this iteration | 90 | 127 | chunk_len = std::min(remaining, block_size); | 91 | 127 | break; | 92 | 0 | } | 93 | 112 | case PLUS1: { | 94 | | // Consume 1 byte more from block size from the raw data for this iteration | 95 | 112 | chunk_len = std::min(remaining, block_size + 1); | 96 | 112 | break; | 97 | 0 | } | 98 | 312 | case SMALL: { | 99 | | // Consume 1 to 32 bytes from the raw data for this iteration | 100 | 312 | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); | 101 | 312 | chunk_len = std::min(remaining, small_len); | 102 | 312 | break; | 103 | 0 | } | 104 | 638 | case RANDOM: { | 105 | | // Consume random bytes from the raw data for this iteration | 106 | 638 | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; | 107 | 638 | break; | 108 | 0 | } | 109 | 99 | case TAIL: { | 110 | | // Consume all remaining bytes from the raw data for this iteration | 111 | 99 | chunk_len = remaining; | 112 | 99 | break; | 113 | 0 | } | 114 | 613 | case HALT: { | 115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration | 116 | 613 | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); | 117 | 613 | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); | 118 | 1.83k | for (size_t j = 0; j < loops && remaining > 0; j++) { | 119 | 1.21k | size_t w = std::min(remaining, step); | 120 | 1.21k | update_fn(&contexts[ctx_index], w, cursor); | 121 | 1.21k | cursor += w; | 122 | 1.21k | remaining -= w; | 123 | 1.21k | } | 124 | | | 125 | | // Randomly reinitialise the hash stream | 126 | 613 | if (fdp.ConsumeBool()) { | 127 | 331 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 128 | 331 | init_fn(&contexts[ctx_index]); | 129 | 331 | } | 130 | 613 | continue; | 131 | 0 | } | 132 | 2.45k | } | 133 | 1.83k | if (chunk_len == 0 || chunk_len > remaining) { | 134 | 0 | continue; | 135 | 0 | } | 136 | | | 137 | | // Occasionally reinitialise a context between update iterations | 138 | 1.83k | if (fdp.ConsumeBool()) { | 139 | 806 | init_fn(&contexts[ctx_index]); | 140 | 806 | } | 141 | | | 142 | | // Fuzz the update function | 143 | 1.83k | update_fn(&contexts[ctx_index], chunk_len, cursor); | 144 | 1.83k | cursor += chunk_len; | 145 | 1.83k | remaining -= chunk_len; | 146 | | | 147 | | // Randomly halt and reinitialise the stream | 148 | 1.83k | if (fdp.ConsumeBool()) { | 149 | 803 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 150 | 803 | init_fn(&contexts[ctx_index]); | 151 | 803 | } | 152 | 1.83k | } | 153 | | | 154 | | // Fuzz the finish function for all contexts | 155 | 4.76k | for (unsigned i = 0; i < num_contexts; i++) { | 156 | 3.37k | finish_fn(&contexts[i], digests[i].data()); | 157 | 3.37k | } | 158 | | | 159 | | // Additional fuzzing on special context chaining approach | 160 | 1.38k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { | 161 | 420 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 162 | 420 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 163 | 420 | 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 | 420 | } | 172 | 1.38k | } |
|
173 | | |
174 | 2.23k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
175 | 2.23k | FuzzedDataProvider fdp(data, size); |
176 | | |
177 | 6.52k | for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) { |
178 | 4.28k | switch (fdp.ConsumeIntegralInRange<int>(0, 2)) { |
179 | 1.58k | case 0: |
180 | 1.58k | fuzz_hash_int_multi<struct mhd_Md5CtxInt>( |
181 | 1.58k | fdp, mhd_MD5_BLOCK_SIZE, |
182 | 1.58k | mhd_MD5_init, mhd_MD5_update, mhd_MD5_finish, mhd_MD5_DIGEST_SIZE); |
183 | 1.58k | break; |
184 | 1.31k | case 1: |
185 | 1.31k | fuzz_hash_int_multi<struct mhd_Sha256CtxInt>( |
186 | 1.31k | fdp, mhd_SHA256_BLOCK_SIZE, |
187 | 1.31k | mhd_SHA256_init, mhd_SHA256_update, mhd_SHA256_finish, mhd_SHA256_DIGEST_SIZE); |
188 | 1.31k | break; |
189 | 1.39k | case 2: |
190 | 1.39k | default: |
191 | 1.39k | fuzz_hash_int_multi<struct mhd_Sha512_256CtxInt>( |
192 | 1.39k | fdp, mhd_SHA512_256_BLOCK_SIZE, |
193 | 1.39k | mhd_SHA512_256_init, mhd_SHA512_256_update, mhd_SHA512_256_finish, mhd_SHA512_256_DIGEST_SIZE); |
194 | 1.39k | break; |
195 | 4.28k | } |
196 | 4.28k | } |
197 | 2.23k | return 0; |
198 | 2.23k | } |