Line | Count | Source |
1 | | // Copyright 2025 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | //////////////////////////////////////////////////////////////////////////////// |
16 | | #include <stddef.h> |
17 | | #include <stdint.h> |
18 | | #include <string.h> |
19 | | #include <vector> |
20 | | #include <algorithm> |
21 | | |
22 | | #include <fuzzer/FuzzedDataProvider.h> |
23 | | extern "C" { |
24 | | #include "md5_int.h" |
25 | | #include "sha256_int.h" |
26 | | #include "sha512_256_int.h" |
27 | | } |
28 | | |
29 | | // Fuzzing target function pointer types for the internal hash APIs |
30 | | template <typename HashType> using InitFn = void (*)(HashType*); |
31 | | template <typename HashType> using UpdateFn = void (*)(HashType*, size_t, const uint8_t*); |
32 | | template <typename HashType> using FinishFn = void (*)(HashType*, uint8_t*); |
33 | | |
34 | | // Generic hashing flow that fuzz same hashing procedure for different algorithm |
35 | | template <typename HashType> |
36 | | static void fuzz_hash_int_multi(FuzzedDataProvider &fdp, |
37 | | size_t block_size, |
38 | | InitFn<HashType> init_fn, |
39 | | UpdateFn<HashType> update_fn, |
40 | | FinishFn<HashType> finish_fn, |
41 | 3.91k | size_t digest_size) { |
42 | 3.91k | if (!fdp.remaining_bytes()) { |
43 | 27 | return; |
44 | 27 | } |
45 | | |
46 | | // Pull a random slice of data for fuzzing |
47 | 3.88k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); |
48 | 3.88k | 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.88k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); |
52 | 3.88k | std::vector<HashType> contexts(num_contexts); |
53 | 3.88k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); |
54 | 12.2k | for (unsigned i = 0; i < num_contexts; i++) { |
55 | 8.34k | init_fn(&contexts[i]); |
56 | 8.34k | } |
57 | | |
58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths |
59 | 3.88k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); |
60 | 3.88k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); |
61 | 3.88k | if (!input_bytes.empty()) { |
62 | 2.92k | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); |
63 | 2.92k | } |
64 | | |
65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations |
66 | 3.88k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; |
67 | 3.88k | size_t remaining = input_bytes.size(); |
68 | | |
69 | | // Perform multiple hash update iterations on the raw data |
70 | 3.88k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); |
71 | 10.5k | while (num_iterations-- && remaining > 0) { |
72 | | // Pick which context to feed this iteration |
73 | 6.65k | 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.65k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; |
77 | 6.65k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); |
78 | | |
79 | 6.65k | size_t chunk_len = 0; |
80 | 6.65k | switch (pattern) { |
81 | 1.69k | case LESS1: { |
82 | | // Consume 1 byte less from block size from the raw data for this iteration |
83 | 1.69k | if (block_size > 1) { |
84 | 1.69k | chunk_len = std::min(remaining, block_size - 1); |
85 | 1.69k | } |
86 | 1.69k | break; |
87 | 0 | } |
88 | 339 | case EQ: { |
89 | | // Consume block size bytes from the raw data for this iteration |
90 | 339 | chunk_len = std::min(remaining, block_size); |
91 | 339 | break; |
92 | 0 | } |
93 | 262 | case PLUS1: { |
94 | | // Consume 1 byte more from block size from the raw data for this iteration |
95 | 262 | chunk_len = std::min(remaining, block_size + 1); |
96 | 262 | break; |
97 | 0 | } |
98 | 1.18k | case SMALL: { |
99 | | // Consume 1 to 32 bytes from the raw data for this iteration |
100 | 1.18k | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); |
101 | 1.18k | chunk_len = std::min(remaining, small_len); |
102 | 1.18k | break; |
103 | 0 | } |
104 | 1.20k | case RANDOM: { |
105 | | // Consume random bytes from the raw data for this iteration |
106 | 1.20k | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; |
107 | 1.20k | break; |
108 | 0 | } |
109 | 351 | case TAIL: { |
110 | | // Consume all remaining bytes from the raw data for this iteration |
111 | 351 | chunk_len = remaining; |
112 | 351 | break; |
113 | 0 | } |
114 | 1.61k | case HALT: { |
115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration |
116 | 1.61k | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); |
117 | 1.61k | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); |
118 | 4.80k | for (size_t j = 0; j < loops && remaining > 0; j++) { |
119 | 3.19k | size_t w = std::min(remaining, step); |
120 | 3.19k | update_fn(&contexts[ctx_index], w, cursor); |
121 | 3.19k | cursor += w; |
122 | 3.19k | remaining -= w; |
123 | 3.19k | } |
124 | | |
125 | | // Randomly reinitialise the hash stream |
126 | 1.61k | if (fdp.ConsumeBool()) { |
127 | 615 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); |
128 | 615 | init_fn(&contexts[ctx_index]); |
129 | 615 | } |
130 | 1.61k | continue; |
131 | 0 | } |
132 | 6.65k | } |
133 | 5.03k | if (chunk_len == 0 || chunk_len > remaining) { |
134 | 0 | continue; |
135 | 0 | } |
136 | | |
137 | | // Occasionally reinitialise a context between update iterations |
138 | 5.03k | if (fdp.ConsumeBool()) { |
139 | 2.07k | init_fn(&contexts[ctx_index]); |
140 | 2.07k | } |
141 | | |
142 | | // Fuzz the update function |
143 | 5.03k | update_fn(&contexts[ctx_index], chunk_len, cursor); |
144 | 5.03k | cursor += chunk_len; |
145 | 5.03k | remaining -= chunk_len; |
146 | | |
147 | | // Randomly halt and reinitialise the stream |
148 | 5.03k | if (fdp.ConsumeBool()) { |
149 | 2.08k | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); |
150 | 2.08k | init_fn(&contexts[ctx_index]); |
151 | 2.08k | } |
152 | 5.03k | } |
153 | | |
154 | | // Fuzz the finish function for all contexts |
155 | 12.2k | for (unsigned i = 0; i < num_contexts; i++) { |
156 | 8.34k | finish_fn(&contexts[i], digests[i].data()); |
157 | 8.34k | } |
158 | | |
159 | | // Additional fuzzing on special context chaining approach |
160 | 3.88k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { |
161 | 1.03k | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); |
162 | 1.03k | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); |
163 | 1.03k | if (src_idx != dst_idx) { |
164 | 567 | init_fn(&contexts[dst_idx]); |
165 | 567 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1); |
166 | 567 | size_t feed_len = std::min(digest_size - offset, |
167 | 567 | (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size)); |
168 | 567 | update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset); |
169 | 567 | finish_fn(&contexts[dst_idx], digests[dst_idx].data()); |
170 | 567 | } |
171 | 1.03k | } |
172 | 3.88k | } 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.44k | size_t digest_size) { | 42 | 1.44k | if (!fdp.remaining_bytes()) { | 43 | 21 | return; | 44 | 21 | } | 45 | | | 46 | | // Pull a random slice of data for fuzzing | 47 | 1.42k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); | 48 | 1.42k | 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.42k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 52 | 1.42k | std::vector<HashType> contexts(num_contexts); | 53 | 1.42k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); | 54 | 4.48k | for (unsigned i = 0; i < num_contexts; i++) { | 55 | 3.05k | init_fn(&contexts[i]); | 56 | 3.05k | } | 57 | | | 58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths | 59 | 1.42k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); | 60 | 1.42k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); | 61 | 1.42k | if (!input_bytes.empty()) { | 62 | 1.05k | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); | 63 | 1.05k | } | 64 | | | 65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations | 66 | 1.42k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; | 67 | 1.42k | size_t remaining = input_bytes.size(); | 68 | | | 69 | | // Perform multiple hash update iterations on the raw data | 70 | 1.42k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 71 | 3.84k | while (num_iterations-- && remaining > 0) { | 72 | | // Pick which context to feed this iteration | 73 | 2.42k | 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.42k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; | 77 | 2.42k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); | 78 | | | 79 | 2.42k | size_t chunk_len = 0; | 80 | 2.42k | switch (pattern) { | 81 | 614 | case LESS1: { | 82 | | // Consume 1 byte less from block size from the raw data for this iteration | 83 | 614 | if (block_size > 1) { | 84 | 614 | chunk_len = std::min(remaining, block_size - 1); | 85 | 614 | } | 86 | 614 | break; | 87 | 0 | } | 88 | 126 | case EQ: { | 89 | | // Consume block size bytes from the raw data for this iteration | 90 | 126 | chunk_len = std::min(remaining, block_size); | 91 | 126 | break; | 92 | 0 | } | 93 | 97 | case PLUS1: { | 94 | | // Consume 1 byte more from block size from the raw data for this iteration | 95 | 97 | chunk_len = std::min(remaining, block_size + 1); | 96 | 97 | break; | 97 | 0 | } | 98 | 511 | case SMALL: { | 99 | | // Consume 1 to 32 bytes from the raw data for this iteration | 100 | 511 | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); | 101 | 511 | chunk_len = std::min(remaining, small_len); | 102 | 511 | break; | 103 | 0 | } | 104 | 417 | case RANDOM: { | 105 | | // Consume random bytes from the raw data for this iteration | 106 | 417 | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; | 107 | 417 | break; | 108 | 0 | } | 109 | 102 | case TAIL: { | 110 | | // Consume all remaining bytes from the raw data for this iteration | 111 | 102 | chunk_len = remaining; | 112 | 102 | break; | 113 | 0 | } | 114 | 554 | case HALT: { | 115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration | 116 | 554 | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); | 117 | 554 | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); | 118 | 1.63k | for (size_t j = 0; j < loops && remaining > 0; j++) { | 119 | 1.07k | size_t w = std::min(remaining, step); | 120 | 1.07k | update_fn(&contexts[ctx_index], w, cursor); | 121 | 1.07k | cursor += w; | 122 | 1.07k | remaining -= w; | 123 | 1.07k | } | 124 | | | 125 | | // Randomly reinitialise the hash stream | 126 | 554 | if (fdp.ConsumeBool()) { | 127 | 199 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 128 | 199 | init_fn(&contexts[ctx_index]); | 129 | 199 | } | 130 | 554 | continue; | 131 | 0 | } | 132 | 2.42k | } | 133 | 1.86k | if (chunk_len == 0 || chunk_len > remaining) { | 134 | 0 | continue; | 135 | 0 | } | 136 | | | 137 | | // Occasionally reinitialise a context between update iterations | 138 | 1.86k | if (fdp.ConsumeBool()) { | 139 | 862 | init_fn(&contexts[ctx_index]); | 140 | 862 | } | 141 | | | 142 | | // Fuzz the update function | 143 | 1.86k | update_fn(&contexts[ctx_index], chunk_len, cursor); | 144 | 1.86k | cursor += chunk_len; | 145 | 1.86k | remaining -= chunk_len; | 146 | | | 147 | | // Randomly halt and reinitialise the stream | 148 | 1.86k | if (fdp.ConsumeBool()) { | 149 | 864 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 150 | 864 | init_fn(&contexts[ctx_index]); | 151 | 864 | } | 152 | 1.86k | } | 153 | | | 154 | | // Fuzz the finish function for all contexts | 155 | 4.48k | for (unsigned i = 0; i < num_contexts; i++) { | 156 | 3.05k | finish_fn(&contexts[i], digests[i].data()); | 157 | 3.05k | } | 158 | | | 159 | | // Additional fuzzing on special context chaining approach | 160 | 1.42k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { | 161 | 402 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 162 | 402 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 163 | 402 | if (src_idx != dst_idx) { | 164 | 222 | init_fn(&contexts[dst_idx]); | 165 | 222 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1); | 166 | 222 | size_t feed_len = std::min(digest_size - offset, | 167 | 222 | (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size)); | 168 | 222 | update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset); | 169 | 222 | finish_fn(&contexts[dst_idx], digests[dst_idx].data()); | 170 | 222 | } | 171 | 402 | } | 172 | 1.42k | } |
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.22k | size_t digest_size) { | 42 | 1.22k | if (!fdp.remaining_bytes()) { | 43 | 3 | return; | 44 | 3 | } | 45 | | | 46 | | // Pull a random slice of data for fuzzing | 47 | 1.22k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); | 48 | 1.22k | 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.22k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 52 | 1.22k | std::vector<HashType> contexts(num_contexts); | 53 | 1.22k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); | 54 | 3.77k | for (unsigned i = 0; i < num_contexts; i++) { | 55 | 2.55k | init_fn(&contexts[i]); | 56 | 2.55k | } | 57 | | | 58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths | 59 | 1.22k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); | 60 | 1.22k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); | 61 | 1.22k | if (!input_bytes.empty()) { | 62 | 907 | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); | 63 | 907 | } | 64 | | | 65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations | 66 | 1.22k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; | 67 | 1.22k | size_t remaining = input_bytes.size(); | 68 | | | 69 | | // Perform multiple hash update iterations on the raw data | 70 | 1.22k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 71 | 3.27k | while (num_iterations-- && remaining > 0) { | 72 | | // Pick which context to feed this iteration | 73 | 2.05k | 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.05k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; | 77 | 2.05k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); | 78 | | | 79 | 2.05k | size_t chunk_len = 0; | 80 | 2.05k | switch (pattern) { | 81 | 513 | case LESS1: { | 82 | | // Consume 1 byte less from block size from the raw data for this iteration | 83 | 513 | if (block_size > 1) { | 84 | 513 | chunk_len = std::min(remaining, block_size - 1); | 85 | 513 | } | 86 | 513 | break; | 87 | 0 | } | 88 | 122 | case EQ: { | 89 | | // Consume block size bytes from the raw data for this iteration | 90 | 122 | chunk_len = std::min(remaining, block_size); | 91 | 122 | break; | 92 | 0 | } | 93 | 90 | case PLUS1: { | 94 | | // Consume 1 byte more from block size from the raw data for this iteration | 95 | 90 | chunk_len = std::min(remaining, block_size + 1); | 96 | 90 | break; | 97 | 0 | } | 98 | 368 | case SMALL: { | 99 | | // Consume 1 to 32 bytes from the raw data for this iteration | 100 | 368 | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); | 101 | 368 | chunk_len = std::min(remaining, small_len); | 102 | 368 | break; | 103 | 0 | } | 104 | 324 | case RANDOM: { | 105 | | // Consume random bytes from the raw data for this iteration | 106 | 324 | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; | 107 | 324 | break; | 108 | 0 | } | 109 | 114 | case TAIL: { | 110 | | // Consume all remaining bytes from the raw data for this iteration | 111 | 114 | chunk_len = remaining; | 112 | 114 | break; | 113 | 0 | } | 114 | 521 | case HALT: { | 115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration | 116 | 521 | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); | 117 | 521 | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); | 118 | 1.51k | for (size_t j = 0; j < loops && remaining > 0; j++) { | 119 | 991 | size_t w = std::min(remaining, step); | 120 | 991 | update_fn(&contexts[ctx_index], w, cursor); | 121 | 991 | cursor += w; | 122 | 991 | remaining -= w; | 123 | 991 | } | 124 | | | 125 | | // Randomly reinitialise the hash stream | 126 | 521 | if (fdp.ConsumeBool()) { | 127 | 209 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 128 | 209 | init_fn(&contexts[ctx_index]); | 129 | 209 | } | 130 | 521 | continue; | 131 | 0 | } | 132 | 2.05k | } | 133 | 1.53k | if (chunk_len == 0 || chunk_len > remaining) { | 134 | 0 | continue; | 135 | 0 | } | 136 | | | 137 | | // Occasionally reinitialise a context between update iterations | 138 | 1.53k | if (fdp.ConsumeBool()) { | 139 | 586 | init_fn(&contexts[ctx_index]); | 140 | 586 | } | 141 | | | 142 | | // Fuzz the update function | 143 | 1.53k | update_fn(&contexts[ctx_index], chunk_len, cursor); | 144 | 1.53k | cursor += chunk_len; | 145 | 1.53k | remaining -= chunk_len; | 146 | | | 147 | | // Randomly halt and reinitialise the stream | 148 | 1.53k | if (fdp.ConsumeBool()) { | 149 | 576 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 150 | 576 | init_fn(&contexts[ctx_index]); | 151 | 576 | } | 152 | 1.53k | } | 153 | | | 154 | | // Fuzz the finish function for all contexts | 155 | 3.77k | for (unsigned i = 0; i < num_contexts; i++) { | 156 | 2.55k | finish_fn(&contexts[i], digests[i].data()); | 157 | 2.55k | } | 158 | | | 159 | | // Additional fuzzing on special context chaining approach | 160 | 1.22k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { | 161 | 317 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 162 | 317 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 163 | 317 | if (src_idx != dst_idx) { | 164 | 194 | init_fn(&contexts[dst_idx]); | 165 | 194 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1); | 166 | 194 | size_t feed_len = std::min(digest_size - offset, | 167 | 194 | (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size)); | 168 | 194 | update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset); | 169 | 194 | finish_fn(&contexts[dst_idx], digests[dst_idx].data()); | 170 | 194 | } | 171 | 317 | } | 172 | 1.22k | } |
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.23k | size_t digest_size) { | 42 | 1.23k | if (!fdp.remaining_bytes()) { | 43 | 3 | return; | 44 | 3 | } | 45 | | | 46 | | // Pull a random slice of data for fuzzing | 47 | 1.23k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); | 48 | 1.23k | 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.23k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 52 | 1.23k | std::vector<HashType> contexts(num_contexts); | 53 | 1.23k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); | 54 | 3.97k | for (unsigned i = 0; i < num_contexts; i++) { | 55 | 2.74k | init_fn(&contexts[i]); | 56 | 2.74k | } | 57 | | | 58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths | 59 | 1.23k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); | 60 | 1.23k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); | 61 | 1.23k | if (!input_bytes.empty()) { | 62 | 960 | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); | 63 | 960 | } | 64 | | | 65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations | 66 | 1.23k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; | 67 | 1.23k | size_t remaining = input_bytes.size(); | 68 | | | 69 | | // Perform multiple hash update iterations on the raw data | 70 | 1.23k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 71 | 3.41k | while (num_iterations-- && remaining > 0) { | 72 | | // Pick which context to feed this iteration | 73 | 2.17k | 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.17k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; | 77 | 2.17k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); | 78 | | | 79 | 2.17k | size_t chunk_len = 0; | 80 | 2.17k | switch (pattern) { | 81 | 566 | case LESS1: { | 82 | | // Consume 1 byte less from block size from the raw data for this iteration | 83 | 566 | if (block_size > 1) { | 84 | 566 | chunk_len = std::min(remaining, block_size - 1); | 85 | 566 | } | 86 | 566 | break; | 87 | 0 | } | 88 | 91 | case EQ: { | 89 | | // Consume block size bytes from the raw data for this iteration | 90 | 91 | chunk_len = std::min(remaining, block_size); | 91 | 91 | break; | 92 | 0 | } | 93 | 75 | case PLUS1: { | 94 | | // Consume 1 byte more from block size from the raw data for this iteration | 95 | 75 | chunk_len = std::min(remaining, block_size + 1); | 96 | 75 | break; | 97 | 0 | } | 98 | 309 | case SMALL: { | 99 | | // Consume 1 to 32 bytes from the raw data for this iteration | 100 | 309 | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); | 101 | 309 | chunk_len = std::min(remaining, small_len); | 102 | 309 | break; | 103 | 0 | } | 104 | 461 | case RANDOM: { | 105 | | // Consume random bytes from the raw data for this iteration | 106 | 461 | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; | 107 | 461 | break; | 108 | 0 | } | 109 | 135 | case TAIL: { | 110 | | // Consume all remaining bytes from the raw data for this iteration | 111 | 135 | chunk_len = remaining; | 112 | 135 | break; | 113 | 0 | } | 114 | 541 | case HALT: { | 115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration | 116 | 541 | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); | 117 | 541 | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); | 118 | 1.66k | for (size_t j = 0; j < loops && remaining > 0; j++) { | 119 | 1.12k | size_t w = std::min(remaining, step); | 120 | 1.12k | update_fn(&contexts[ctx_index], w, cursor); | 121 | 1.12k | cursor += w; | 122 | 1.12k | remaining -= w; | 123 | 1.12k | } | 124 | | | 125 | | // Randomly reinitialise the hash stream | 126 | 541 | if (fdp.ConsumeBool()) { | 127 | 207 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 128 | 207 | init_fn(&contexts[ctx_index]); | 129 | 207 | } | 130 | 541 | continue; | 131 | 0 | } | 132 | 2.17k | } | 133 | 1.63k | if (chunk_len == 0 || chunk_len > remaining) { | 134 | 0 | continue; | 135 | 0 | } | 136 | | | 137 | | // Occasionally reinitialise a context between update iterations | 138 | 1.63k | if (fdp.ConsumeBool()) { | 139 | 625 | init_fn(&contexts[ctx_index]); | 140 | 625 | } | 141 | | | 142 | | // Fuzz the update function | 143 | 1.63k | update_fn(&contexts[ctx_index], chunk_len, cursor); | 144 | 1.63k | cursor += chunk_len; | 145 | 1.63k | remaining -= chunk_len; | 146 | | | 147 | | // Randomly halt and reinitialise the stream | 148 | 1.63k | if (fdp.ConsumeBool()) { | 149 | 643 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 150 | 643 | init_fn(&contexts[ctx_index]); | 151 | 643 | } | 152 | 1.63k | } | 153 | | | 154 | | // Fuzz the finish function for all contexts | 155 | 3.97k | for (unsigned i = 0; i < num_contexts; i++) { | 156 | 2.74k | finish_fn(&contexts[i], digests[i].data()); | 157 | 2.74k | } | 158 | | | 159 | | // Additional fuzzing on special context chaining approach | 160 | 1.23k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { | 161 | 317 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 162 | 317 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 163 | 317 | if (src_idx != dst_idx) { | 164 | 151 | init_fn(&contexts[dst_idx]); | 165 | 151 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1); | 166 | 151 | size_t feed_len = std::min(digest_size - offset, | 167 | 151 | (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size)); | 168 | 151 | update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset); | 169 | 151 | finish_fn(&contexts[dst_idx], digests[dst_idx].data()); | 170 | 151 | } | 171 | 317 | } | 172 | 1.23k | } |
|
173 | | |
174 | 2.14k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
175 | 2.14k | FuzzedDataProvider fdp(data, size); |
176 | | |
177 | 6.05k | for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) { |
178 | 3.91k | switch (fdp.ConsumeIntegralInRange<int>(0, 2)) { |
179 | 1.44k | case 0: |
180 | 1.44k | fuzz_hash_int_multi<struct mhd_Md5CtxInt>( |
181 | 1.44k | fdp, mhd_MD5_BLOCK_SIZE, |
182 | 1.44k | mhd_MD5_init, mhd_MD5_update, mhd_MD5_finish, mhd_MD5_DIGEST_SIZE); |
183 | 1.44k | break; |
184 | 1.22k | case 1: |
185 | 1.22k | fuzz_hash_int_multi<struct mhd_Sha256CtxInt>( |
186 | 1.22k | fdp, mhd_SHA256_BLOCK_SIZE, |
187 | 1.22k | mhd_SHA256_init, mhd_SHA256_update, mhd_SHA256_finish, mhd_SHA256_DIGEST_SIZE); |
188 | 1.22k | break; |
189 | 1.23k | case 2: |
190 | 1.23k | default: |
191 | 1.23k | fuzz_hash_int_multi<struct mhd_Sha512_256CtxInt>( |
192 | 1.23k | fdp, mhd_SHA512_256_BLOCK_SIZE, |
193 | 1.23k | mhd_SHA512_256_init, mhd_SHA512_256_update, mhd_SHA512_256_finish, mhd_SHA512_256_DIGEST_SIZE); |
194 | 1.23k | break; |
195 | 3.91k | } |
196 | 3.91k | } |
197 | 2.14k | return 0; |
198 | 2.14k | } |