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 | | |
24 | | #include "md5_builtin.h" |
25 | | #include "sha256_builtin.h" |
26 | | #include "sha512_256_builtin.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 void*); |
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.85k | size_t digest_size) { |
42 | 3.85k | if (!fdp.remaining_bytes()) { |
43 | 23 | return; |
44 | 23 | } |
45 | | |
46 | | // Pull a random slice of data for fuzzing |
47 | 3.82k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); |
48 | 3.82k | 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.82k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); |
52 | 3.82k | std::vector<HashType> contexts(num_contexts); |
53 | 3.82k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); |
54 | 11.8k | for (unsigned i = 0; i < num_contexts; i++) { |
55 | 8.07k | init_fn(&contexts[i]); |
56 | 8.07k | } |
57 | | |
58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths |
59 | 3.82k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); |
60 | 3.82k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); |
61 | 3.82k | if (!input_bytes.empty()) { |
62 | 2.98k | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); |
63 | 2.98k | } |
64 | | |
65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations |
66 | 3.82k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; |
67 | 3.82k | size_t remaining = input_bytes.size(); |
68 | | |
69 | | // Perform multiple hash update iterations on the raw data |
70 | 3.82k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); |
71 | 10.6k | while (num_iterations-- && remaining > 0) { |
72 | | // Pick which context to feed this iteration |
73 | 6.79k | 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.79k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; |
77 | 6.79k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); |
78 | | |
79 | 6.79k | size_t chunk_len = 0; |
80 | 6.79k | switch (pattern) { |
81 | 1.59k | case LESS1: { |
82 | | // Consume 1 byte less from block size from the raw data for this iteration |
83 | 1.59k | if (block_size > 1) { |
84 | 1.59k | chunk_len = std::min(remaining, block_size - 1); |
85 | 1.59k | } |
86 | 1.59k | break; |
87 | 0 | } |
88 | 427 | case EQ: { |
89 | | // Consume block size bytes from the raw data for this iteration |
90 | 427 | chunk_len = std::min(remaining, block_size); |
91 | 427 | break; |
92 | 0 | } |
93 | 379 | case PLUS1: { |
94 | | // Consume 1 byte more from block size from the raw data for this iteration |
95 | 379 | chunk_len = std::min(remaining, block_size + 1); |
96 | 379 | break; |
97 | 0 | } |
98 | 1.22k | case SMALL: { |
99 | | // Consume 1 to 32 bytes from the raw data for this iteration |
100 | 1.22k | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); |
101 | 1.22k | chunk_len = std::min(remaining, small_len); |
102 | 1.22k | break; |
103 | 0 | } |
104 | 1.40k | case RANDOM: { |
105 | | // Consume random bytes from the raw data for this iteration |
106 | 1.40k | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; |
107 | 1.40k | break; |
108 | 0 | } |
109 | 368 | case TAIL: { |
110 | | // Consume all remaining bytes from the raw data for this iteration |
111 | 368 | chunk_len = remaining; |
112 | 368 | break; |
113 | 0 | } |
114 | 1.39k | case HALT: { |
115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration |
116 | 1.39k | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); |
117 | 1.39k | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); |
118 | 4.07k | for (size_t j = 0; j < loops && remaining > 0; j++) { |
119 | 2.68k | size_t w = std::min(remaining, step); |
120 | 2.68k | update_fn(&contexts[ctx_index], w, cursor); |
121 | 2.68k | cursor += w; |
122 | 2.68k | remaining -= w; |
123 | 2.68k | } |
124 | | |
125 | | // Randomly reinitialise the hash stream |
126 | 1.39k | if (fdp.ConsumeBool()) { |
127 | 649 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); |
128 | 649 | init_fn(&contexts[ctx_index]); |
129 | 649 | } |
130 | 1.39k | continue; |
131 | 0 | } |
132 | 6.79k | } |
133 | 5.40k | if (chunk_len == 0 || chunk_len > remaining) { |
134 | 0 | continue; |
135 | 0 | } |
136 | | |
137 | | // Occasionally reinitialise a context between update iterations |
138 | 5.40k | if (fdp.ConsumeBool()) { |
139 | 2.20k | init_fn(&contexts[ctx_index]); |
140 | 2.20k | } |
141 | | |
142 | | // Fuzz the update function |
143 | 5.40k | update_fn(&contexts[ctx_index], chunk_len, cursor); |
144 | 5.40k | cursor += chunk_len; |
145 | 5.40k | remaining -= chunk_len; |
146 | | |
147 | | // Randomly halt and reinitialise the stream |
148 | 5.40k | if (fdp.ConsumeBool()) { |
149 | 2.09k | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); |
150 | 2.09k | init_fn(&contexts[ctx_index]); |
151 | 2.09k | } |
152 | 5.40k | } |
153 | | |
154 | | // Fuzz the finish function for all contexts |
155 | 11.8k | for (unsigned i = 0; i < num_contexts; i++) { |
156 | 8.07k | finish_fn(&contexts[i], digests[i].data()); |
157 | 8.07k | } |
158 | | |
159 | | // Additional fuzzing on special context chaining approach |
160 | 3.82k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { |
161 | 978 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); |
162 | 978 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); |
163 | 978 | if (src_idx != dst_idx) { |
164 | 525 | init_fn(&contexts[dst_idx]); |
165 | 525 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1); |
166 | 525 | size_t feed_len = std::min(digest_size - offset, |
167 | 525 | (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size)); |
168 | 525 | update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset); |
169 | 525 | finish_fn(&contexts[dst_idx], digests[dst_idx].data()); |
170 | 525 | } |
171 | 978 | } |
172 | 3.82k | } fuzz_crypto_int.cpp:void fuzz_hash_int_multi<mhd_Md5CtxBlti>(FuzzedDataProvider&, unsigned long, void (*)(mhd_Md5CtxBlti*), void (*)(mhd_Md5CtxBlti*, unsigned long, void const*), void (*)(mhd_Md5CtxBlti*, unsigned char*), unsigned long) Line | Count | Source | 41 | 1.40k | size_t digest_size) { | 42 | 1.40k | if (!fdp.remaining_bytes()) { | 43 | 15 | return; | 44 | 15 | } | 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.32k | for (unsigned i = 0; i < num_contexts; i++) { | 55 | 2.93k | init_fn(&contexts[i]); | 56 | 2.93k | } | 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.07k | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); | 63 | 1.07k | } | 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.86k | while (num_iterations-- && remaining > 0) { | 72 | | // Pick which context to feed this iteration | 73 | 2.47k | 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.47k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; | 77 | 2.47k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); | 78 | | | 79 | 2.47k | size_t chunk_len = 0; | 80 | 2.47k | switch (pattern) { | 81 | 623 | case LESS1: { | 82 | | // Consume 1 byte less from block size from the raw data for this iteration | 83 | 623 | if (block_size > 1) { | 84 | 623 | chunk_len = std::min(remaining, block_size - 1); | 85 | 623 | } | 86 | 623 | break; | 87 | 0 | } | 88 | 206 | case EQ: { | 89 | | // Consume block size bytes from the raw data for this iteration | 90 | 206 | chunk_len = std::min(remaining, block_size); | 91 | 206 | break; | 92 | 0 | } | 93 | 133 | case PLUS1: { | 94 | | // Consume 1 byte more from block size from the raw data for this iteration | 95 | 133 | chunk_len = std::min(remaining, block_size + 1); | 96 | 133 | break; | 97 | 0 | } | 98 | 481 | case SMALL: { | 99 | | // Consume 1 to 32 bytes from the raw data for this iteration | 100 | 481 | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); | 101 | 481 | chunk_len = std::min(remaining, small_len); | 102 | 481 | break; | 103 | 0 | } | 104 | 434 | case RANDOM: { | 105 | | // Consume random bytes from the raw data for this iteration | 106 | 434 | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; | 107 | 434 | break; | 108 | 0 | } | 109 | 98 | case TAIL: { | 110 | | // Consume all remaining bytes from the raw data for this iteration | 111 | 98 | chunk_len = remaining; | 112 | 98 | break; | 113 | 0 | } | 114 | 498 | case HALT: { | 115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration | 116 | 498 | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); | 117 | 498 | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); | 118 | 1.43k | for (size_t j = 0; j < loops && remaining > 0; j++) { | 119 | 939 | size_t w = std::min(remaining, step); | 120 | 939 | update_fn(&contexts[ctx_index], w, cursor); | 121 | 939 | cursor += w; | 122 | 939 | remaining -= w; | 123 | 939 | } | 124 | | | 125 | | // Randomly reinitialise the hash stream | 126 | 498 | if (fdp.ConsumeBool()) { | 127 | 231 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 128 | 231 | init_fn(&contexts[ctx_index]); | 129 | 231 | } | 130 | 498 | continue; | 131 | 0 | } | 132 | 2.47k | } | 133 | 1.97k | if (chunk_len == 0 || chunk_len > remaining) { | 134 | 0 | continue; | 135 | 0 | } | 136 | | | 137 | | // Occasionally reinitialise a context between update iterations | 138 | 1.97k | if (fdp.ConsumeBool()) { | 139 | 872 | init_fn(&contexts[ctx_index]); | 140 | 872 | } | 141 | | | 142 | | // Fuzz the update function | 143 | 1.97k | update_fn(&contexts[ctx_index], chunk_len, cursor); | 144 | 1.97k | cursor += chunk_len; | 145 | 1.97k | remaining -= chunk_len; | 146 | | | 147 | | // Randomly halt and reinitialise the stream | 148 | 1.97k | if (fdp.ConsumeBool()) { | 149 | 789 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 150 | 789 | init_fn(&contexts[ctx_index]); | 151 | 789 | } | 152 | 1.97k | } | 153 | | | 154 | | // Fuzz the finish function for all contexts | 155 | 4.32k | for (unsigned i = 0; i < num_contexts; i++) { | 156 | 2.93k | finish_fn(&contexts[i], digests[i].data()); | 157 | 2.93k | } | 158 | | | 159 | | // Additional fuzzing on special context chaining approach | 160 | 1.38k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { | 161 | 377 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 162 | 377 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 163 | 377 | if (src_idx != dst_idx) { | 164 | 190 | init_fn(&contexts[dst_idx]); | 165 | 190 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1); | 166 | 190 | size_t feed_len = std::min(digest_size - offset, | 167 | 190 | (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size)); | 168 | 190 | update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset); | 169 | 190 | finish_fn(&contexts[dst_idx], digests[dst_idx].data()); | 170 | 190 | } | 171 | 377 | } | 172 | 1.38k | } |
fuzz_crypto_int.cpp:void fuzz_hash_int_multi<mhd_Sha256CtxBlti>(FuzzedDataProvider&, unsigned long, void (*)(mhd_Sha256CtxBlti*), void (*)(mhd_Sha256CtxBlti*, unsigned long, void const*), void (*)(mhd_Sha256CtxBlti*, unsigned char*), unsigned long) Line | Count | Source | 41 | 1.18k | size_t digest_size) { | 42 | 1.18k | if (!fdp.remaining_bytes()) { | 43 | 3 | return; | 44 | 3 | } | 45 | | | 46 | | // Pull a random slice of data for fuzzing | 47 | 1.18k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); | 48 | 1.18k | std::vector<uint8_t> input_bytes = fdp.ConsumeBytes<uint8_t>(take_len); | 49 | | | 50 | | // Create 1 to 4 independent hashing contexts with it own digest buffer | 51 | 1.18k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 52 | 1.18k | std::vector<HashType> contexts(num_contexts); | 53 | 1.18k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); | 54 | 3.60k | for (unsigned i = 0; i < num_contexts; i++) { | 55 | 2.42k | init_fn(&contexts[i]); | 56 | 2.42k | } | 57 | | | 58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths | 59 | 1.18k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); | 60 | 1.18k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); | 61 | 1.18k | if (!input_bytes.empty()) { | 62 | 914 | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); | 63 | 914 | } | 64 | | | 65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations | 66 | 1.18k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; | 67 | 1.18k | size_t remaining = input_bytes.size(); | 68 | | | 69 | | // Perform multiple hash update iterations on the raw data | 70 | 1.18k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 71 | 3.30k | while (num_iterations-- && remaining > 0) { | 72 | | // Pick which context to feed this iteration | 73 | 2.11k | 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.11k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; | 77 | 2.11k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); | 78 | | | 79 | 2.11k | size_t chunk_len = 0; | 80 | 2.11k | switch (pattern) { | 81 | 451 | case LESS1: { | 82 | | // Consume 1 byte less from block size from the raw data for this iteration | 83 | 451 | if (block_size > 1) { | 84 | 451 | chunk_len = std::min(remaining, block_size - 1); | 85 | 451 | } | 86 | 451 | break; | 87 | 0 | } | 88 | 113 | case EQ: { | 89 | | // Consume block size bytes from the raw data for this iteration | 90 | 113 | chunk_len = std::min(remaining, block_size); | 91 | 113 | break; | 92 | 0 | } | 93 | 130 | case PLUS1: { | 94 | | // Consume 1 byte more from block size from the raw data for this iteration | 95 | 130 | chunk_len = std::min(remaining, block_size + 1); | 96 | 130 | break; | 97 | 0 | } | 98 | 363 | case SMALL: { | 99 | | // Consume 1 to 32 bytes from the raw data for this iteration | 100 | 363 | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); | 101 | 363 | chunk_len = std::min(remaining, small_len); | 102 | 363 | break; | 103 | 0 | } | 104 | 495 | case RANDOM: { | 105 | | // Consume random bytes from the raw data for this iteration | 106 | 495 | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; | 107 | 495 | break; | 108 | 0 | } | 109 | 119 | case TAIL: { | 110 | | // Consume all remaining bytes from the raw data for this iteration | 111 | 119 | chunk_len = remaining; | 112 | 119 | break; | 113 | 0 | } | 114 | 447 | case HALT: { | 115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration | 116 | 447 | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); | 117 | 447 | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); | 118 | 1.30k | for (size_t j = 0; j < loops && remaining > 0; j++) { | 119 | 854 | size_t w = std::min(remaining, step); | 120 | 854 | update_fn(&contexts[ctx_index], w, cursor); | 121 | 854 | cursor += w; | 122 | 854 | remaining -= w; | 123 | 854 | } | 124 | | | 125 | | // Randomly reinitialise the hash stream | 126 | 447 | if (fdp.ConsumeBool()) { | 127 | 195 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 128 | 195 | init_fn(&contexts[ctx_index]); | 129 | 195 | } | 130 | 447 | continue; | 131 | 0 | } | 132 | 2.11k | } | 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 | 627 | init_fn(&contexts[ctx_index]); | 140 | 627 | } | 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 | 572 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 150 | 572 | init_fn(&contexts[ctx_index]); | 151 | 572 | } | 152 | 1.67k | } | 153 | | | 154 | | // Fuzz the finish function for all contexts | 155 | 3.60k | for (unsigned i = 0; i < num_contexts; i++) { | 156 | 2.42k | finish_fn(&contexts[i], digests[i].data()); | 157 | 2.42k | } | 158 | | | 159 | | // Additional fuzzing on special context chaining approach | 160 | 1.18k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { | 161 | 262 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 162 | 262 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 163 | 262 | 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 | 262 | } | 172 | 1.18k | } |
fuzz_crypto_int.cpp:void fuzz_hash_int_multi<mhd_Sha512_256CtxBlti>(FuzzedDataProvider&, unsigned long, void (*)(mhd_Sha512_256CtxBlti*), void (*)(mhd_Sha512_256CtxBlti*, unsigned long, void const*), void (*)(mhd_Sha512_256CtxBlti*, unsigned char*), unsigned long) Line | Count | Source | 41 | 1.25k | size_t digest_size) { | 42 | 1.25k | if (!fdp.remaining_bytes()) { | 43 | 5 | return; | 44 | 5 | } | 45 | | | 46 | | // Pull a random slice of data for fuzzing | 47 | 1.25k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); | 48 | 1.25k | 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.25k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 52 | 1.25k | std::vector<HashType> contexts(num_contexts); | 53 | 1.25k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); | 54 | 3.96k | for (unsigned i = 0; i < num_contexts; i++) { | 55 | 2.71k | init_fn(&contexts[i]); | 56 | 2.71k | } | 57 | | | 58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths | 59 | 1.25k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); | 60 | 1.25k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); | 61 | 1.25k | if (!input_bytes.empty()) { | 62 | 997 | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); | 63 | 997 | } | 64 | | | 65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations | 66 | 1.25k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; | 67 | 1.25k | size_t remaining = input_bytes.size(); | 68 | | | 69 | | // Perform multiple hash update iterations on the raw data | 70 | 1.25k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 71 | 3.45k | while (num_iterations-- && remaining > 0) { | 72 | | // Pick which context to feed this iteration | 73 | 2.20k | 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.20k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; | 77 | 2.20k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); | 78 | | | 79 | 2.20k | size_t chunk_len = 0; | 80 | 2.20k | switch (pattern) { | 81 | 522 | case LESS1: { | 82 | | // Consume 1 byte less from block size from the raw data for this iteration | 83 | 522 | if (block_size > 1) { | 84 | 522 | chunk_len = std::min(remaining, block_size - 1); | 85 | 522 | } | 86 | 522 | break; | 87 | 0 | } | 88 | 108 | case EQ: { | 89 | | // Consume block size bytes from the raw data for this iteration | 90 | 108 | chunk_len = std::min(remaining, block_size); | 91 | 108 | break; | 92 | 0 | } | 93 | 116 | case PLUS1: { | 94 | | // Consume 1 byte more from block size from the raw data for this iteration | 95 | 116 | chunk_len = std::min(remaining, block_size + 1); | 96 | 116 | break; | 97 | 0 | } | 98 | 383 | case SMALL: { | 99 | | // Consume 1 to 32 bytes from the raw data for this iteration | 100 | 383 | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); | 101 | 383 | chunk_len = std::min(remaining, small_len); | 102 | 383 | break; | 103 | 0 | } | 104 | 476 | case RANDOM: { | 105 | | // Consume random bytes from the raw data for this iteration | 106 | 476 | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; | 107 | 476 | break; | 108 | 0 | } | 109 | 151 | case TAIL: { | 110 | | // Consume all remaining bytes from the raw data for this iteration | 111 | 151 | chunk_len = remaining; | 112 | 151 | break; | 113 | 0 | } | 114 | 445 | case HALT: { | 115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration | 116 | 445 | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); | 117 | 445 | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); | 118 | 1.33k | for (size_t j = 0; j < loops && remaining > 0; j++) { | 119 | 889 | size_t w = std::min(remaining, step); | 120 | 889 | update_fn(&contexts[ctx_index], w, cursor); | 121 | 889 | cursor += w; | 122 | 889 | remaining -= w; | 123 | 889 | } | 124 | | | 125 | | // Randomly reinitialise the hash stream | 126 | 445 | if (fdp.ConsumeBool()) { | 127 | 223 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 128 | 223 | init_fn(&contexts[ctx_index]); | 129 | 223 | } | 130 | 445 | continue; | 131 | 0 | } | 132 | 2.20k | } | 133 | 1.75k | if (chunk_len == 0 || chunk_len > remaining) { | 134 | 0 | continue; | 135 | 0 | } | 136 | | | 137 | | // Occasionally reinitialise a context between update iterations | 138 | 1.75k | if (fdp.ConsumeBool()) { | 139 | 708 | init_fn(&contexts[ctx_index]); | 140 | 708 | } | 141 | | | 142 | | // Fuzz the update function | 143 | 1.75k | update_fn(&contexts[ctx_index], chunk_len, cursor); | 144 | 1.75k | cursor += chunk_len; | 145 | 1.75k | remaining -= chunk_len; | 146 | | | 147 | | // Randomly halt and reinitialise the stream | 148 | 1.75k | if (fdp.ConsumeBool()) { | 149 | 736 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 150 | 736 | init_fn(&contexts[ctx_index]); | 151 | 736 | } | 152 | 1.75k | } | 153 | | | 154 | | // Fuzz the finish function for all contexts | 155 | 3.96k | for (unsigned i = 0; i < num_contexts; i++) { | 156 | 2.71k | finish_fn(&contexts[i], digests[i].data()); | 157 | 2.71k | } | 158 | | | 159 | | // Additional fuzzing on special context chaining approach | 160 | 1.25k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { | 161 | 339 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 162 | 339 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 163 | 339 | 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 | 339 | } | 172 | 1.25k | } |
|
173 | | |
174 | 2.08k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
175 | 2.08k | FuzzedDataProvider fdp(data, size); |
176 | | |
177 | 5.93k | for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) { |
178 | 3.85k | switch (fdp.ConsumeIntegralInRange<int>(0, 2)) { |
179 | 1.40k | case 0: |
180 | 1.40k | fuzz_hash_int_multi<struct mhd_Md5CtxBlti>( |
181 | 1.40k | fdp, mhd_MD5_BLOCK_SIZE, |
182 | 1.40k | mhd_MD5_blti_init, mhd_MD5_blti_update, mhd_MD5_blti_finish, mhd_MD5_DIGEST_SIZE); |
183 | 1.40k | break; |
184 | 1.18k | case 1: |
185 | 1.18k | fuzz_hash_int_multi<struct mhd_Sha256CtxBlti>( |
186 | 1.18k | fdp, mhd_SHA256_BLOCK_SIZE, |
187 | 1.18k | mhd_SHA256_blti_init, mhd_SHA256_blti_update, mhd_SHA256_blti_finish, mhd_SHA256_DIGEST_SIZE); |
188 | 1.18k | break; |
189 | 1.25k | case 2: |
190 | 1.25k | default: |
191 | 1.25k | fuzz_hash_int_multi<struct mhd_Sha512_256CtxBlti>( |
192 | 1.25k | fdp, mhd_SHA512_256_BLOCK_SIZE, |
193 | 1.25k | mhd_SHA512_256_blti_init, mhd_SHA512_256_blti_update, mhd_SHA512_256_blti_finish, mhd_SHA512_256_DIGEST_SIZE); |
194 | 1.25k | break; |
195 | 3.85k | } |
196 | 3.85k | } |
197 | 2.08k | return 0; |
198 | 2.08k | } |