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.82k | size_t digest_size) { |
42 | 3.82k | if (!fdp.remaining_bytes()) { |
43 | 28 | return; |
44 | 28 | } |
45 | | |
46 | | // Pull a random slice of data for fuzzing |
47 | 3.79k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); |
48 | 3.79k | 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.79k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); |
52 | 3.79k | std::vector<HashType> contexts(num_contexts); |
53 | 3.79k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); |
54 | 12.1k | for (unsigned i = 0; i < num_contexts; i++) { |
55 | 8.37k | init_fn(&contexts[i]); |
56 | 8.37k | } |
57 | | |
58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths |
59 | 3.79k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); |
60 | 3.79k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); |
61 | 3.79k | if (!input_bytes.empty()) { |
62 | 2.88k | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); |
63 | 2.88k | } |
64 | | |
65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations |
66 | 3.79k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; |
67 | 3.79k | size_t remaining = input_bytes.size(); |
68 | | |
69 | | // Perform multiple hash update iterations on the raw data |
70 | 3.79k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); |
71 | 10.4k | while (num_iterations-- && remaining > 0) { |
72 | | // Pick which context to feed this iteration |
73 | 6.66k | 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.66k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; |
77 | 6.66k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); |
78 | | |
79 | 6.66k | size_t chunk_len = 0; |
80 | 6.66k | switch (pattern) { |
81 | 1.54k | case LESS1: { |
82 | | // Consume 1 byte less from block size from the raw data for this iteration |
83 | 1.54k | if (block_size > 1) { |
84 | 1.54k | chunk_len = std::min(remaining, block_size - 1); |
85 | 1.54k | } |
86 | 1.54k | break; |
87 | 0 | } |
88 | 353 | case EQ: { |
89 | | // Consume block size bytes from the raw data for this iteration |
90 | 353 | chunk_len = std::min(remaining, block_size); |
91 | 353 | break; |
92 | 0 | } |
93 | 341 | case PLUS1: { |
94 | | // Consume 1 byte more from block size from the raw data for this iteration |
95 | 341 | chunk_len = std::min(remaining, block_size + 1); |
96 | 341 | break; |
97 | 0 | } |
98 | 1.19k | case SMALL: { |
99 | | // Consume 1 to 32 bytes from the raw data for this iteration |
100 | 1.19k | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); |
101 | 1.19k | chunk_len = std::min(remaining, small_len); |
102 | 1.19k | break; |
103 | 0 | } |
104 | 1.25k | case RANDOM: { |
105 | | // Consume random bytes from the raw data for this iteration |
106 | 1.25k | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; |
107 | 1.25k | break; |
108 | 0 | } |
109 | 330 | case TAIL: { |
110 | | // Consume all remaining bytes from the raw data for this iteration |
111 | 330 | chunk_len = remaining; |
112 | 330 | break; |
113 | 0 | } |
114 | 1.65k | case HALT: { |
115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration |
116 | 1.65k | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); |
117 | 1.65k | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); |
118 | 5.04k | for (size_t j = 0; j < loops && remaining > 0; j++) { |
119 | 3.38k | size_t w = std::min(remaining, step); |
120 | 3.38k | update_fn(&contexts[ctx_index], w, cursor); |
121 | 3.38k | cursor += w; |
122 | 3.38k | remaining -= w; |
123 | 3.38k | } |
124 | | |
125 | | // Randomly reinitialise the hash stream |
126 | 1.65k | if (fdp.ConsumeBool()) { |
127 | 748 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); |
128 | 748 | init_fn(&contexts[ctx_index]); |
129 | 748 | } |
130 | 1.65k | continue; |
131 | 0 | } |
132 | 6.66k | } |
133 | 5.01k | if (chunk_len == 0 || chunk_len > remaining) { |
134 | 0 | continue; |
135 | 0 | } |
136 | | |
137 | | // Occasionally reinitialise a context between update iterations |
138 | 5.01k | if (fdp.ConsumeBool()) { |
139 | 2.05k | init_fn(&contexts[ctx_index]); |
140 | 2.05k | } |
141 | | |
142 | | // Fuzz the update function |
143 | 5.01k | update_fn(&contexts[ctx_index], chunk_len, cursor); |
144 | 5.01k | cursor += chunk_len; |
145 | 5.01k | remaining -= chunk_len; |
146 | | |
147 | | // Randomly halt and reinitialise the stream |
148 | 5.01k | if (fdp.ConsumeBool()) { |
149 | 2.14k | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); |
150 | 2.14k | init_fn(&contexts[ctx_index]); |
151 | 2.14k | } |
152 | 5.01k | } |
153 | | |
154 | | // Fuzz the finish function for all contexts |
155 | 12.1k | for (unsigned i = 0; i < num_contexts; i++) { |
156 | 8.37k | finish_fn(&contexts[i], digests[i].data()); |
157 | 8.37k | } |
158 | | |
159 | | // Additional fuzzing on special context chaining approach |
160 | 3.79k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { |
161 | 1.05k | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); |
162 | 1.05k | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); |
163 | 1.05k | if (src_idx != dst_idx) { |
164 | 547 | init_fn(&contexts[dst_idx]); |
165 | 547 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1); |
166 | 547 | size_t feed_len = std::min(digest_size - offset, |
167 | 547 | (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size)); |
168 | 547 | update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset); |
169 | 547 | finish_fn(&contexts[dst_idx], digests[dst_idx].data()); |
170 | 547 | } |
171 | 1.05k | } |
172 | 3.79k | } 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.42k | size_t digest_size) { | 42 | 1.42k | if (!fdp.remaining_bytes()) { | 43 | 18 | return; | 44 | 18 | } | 45 | | | 46 | | // Pull a random slice of data for fuzzing | 47 | 1.40k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); | 48 | 1.40k | 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.40k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 52 | 1.40k | std::vector<HashType> contexts(num_contexts); | 53 | 1.40k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); | 54 | 4.51k | 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.40k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); | 60 | 1.40k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); | 61 | 1.40k | 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.40k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; | 67 | 1.40k | size_t remaining = input_bytes.size(); | 68 | | | 69 | | // Perform multiple hash update iterations on the raw data | 70 | 1.40k | 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.43k | 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.43k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; | 77 | 2.43k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); | 78 | | | 79 | 2.43k | size_t chunk_len = 0; | 80 | 2.43k | switch (pattern) { | 81 | 553 | case LESS1: { | 82 | | // Consume 1 byte less from block size from the raw data for this iteration | 83 | 553 | if (block_size > 1) { | 84 | 553 | chunk_len = std::min(remaining, block_size - 1); | 85 | 553 | } | 86 | 553 | break; | 87 | 0 | } | 88 | 143 | case EQ: { | 89 | | // Consume block size bytes from the raw data for this iteration | 90 | 143 | chunk_len = std::min(remaining, block_size); | 91 | 143 | break; | 92 | 0 | } | 93 | 104 | case PLUS1: { | 94 | | // Consume 1 byte more from block size from the raw data for this iteration | 95 | 104 | chunk_len = std::min(remaining, block_size + 1); | 96 | 104 | break; | 97 | 0 | } | 98 | 496 | case SMALL: { | 99 | | // Consume 1 to 32 bytes from the raw data for this iteration | 100 | 496 | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); | 101 | 496 | chunk_len = std::min(remaining, small_len); | 102 | 496 | 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 | 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 | 578 | case HALT: { | 115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration | 116 | 578 | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); | 117 | 578 | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); | 118 | 1.70k | for (size_t j = 0; j < loops && remaining > 0; j++) { | 119 | 1.13k | size_t w = std::min(remaining, step); | 120 | 1.13k | update_fn(&contexts[ctx_index], w, cursor); | 121 | 1.13k | cursor += w; | 122 | 1.13k | remaining -= w; | 123 | 1.13k | } | 124 | | | 125 | | // Randomly reinitialise the hash stream | 126 | 578 | if (fdp.ConsumeBool()) { | 127 | 251 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 128 | 251 | init_fn(&contexts[ctx_index]); | 129 | 251 | } | 130 | 578 | continue; | 131 | 0 | } | 132 | 2.43k | } | 133 | 1.85k | if (chunk_len == 0 || chunk_len > remaining) { | 134 | 0 | continue; | 135 | 0 | } | 136 | | | 137 | | // Occasionally reinitialise a context between update iterations | 138 | 1.85k | if (fdp.ConsumeBool()) { | 139 | 808 | init_fn(&contexts[ctx_index]); | 140 | 808 | } | 141 | | | 142 | | // Fuzz the update function | 143 | 1.85k | update_fn(&contexts[ctx_index], chunk_len, cursor); | 144 | 1.85k | cursor += chunk_len; | 145 | 1.85k | remaining -= chunk_len; | 146 | | | 147 | | // Randomly halt and reinitialise the stream | 148 | 1.85k | if (fdp.ConsumeBool()) { | 149 | 835 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 150 | 835 | init_fn(&contexts[ctx_index]); | 151 | 835 | } | 152 | 1.85k | } | 153 | | | 154 | | // Fuzz the finish function for all contexts | 155 | 4.51k | 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.40k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { | 161 | 401 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 162 | 401 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 163 | 401 | if (src_idx != dst_idx) { | 164 | 192 | init_fn(&contexts[dst_idx]); | 165 | 192 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1); | 166 | 192 | size_t feed_len = std::min(digest_size - offset, | 167 | 192 | (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size)); | 168 | 192 | update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset); | 169 | 192 | finish_fn(&contexts[dst_idx], digests[dst_idx].data()); | 170 | 192 | } | 171 | 401 | } | 172 | 1.40k | } |
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.16k | size_t digest_size) { | 42 | 1.16k | if (!fdp.remaining_bytes()) { | 43 | 3 | return; | 44 | 3 | } | 45 | | | 46 | | // Pull a random slice of data for fuzzing | 47 | 1.16k | size_t take_len = fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()); | 48 | 1.16k | 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.16k | const unsigned num_contexts = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 52 | 1.16k | std::vector<HashType> contexts(num_contexts); | 53 | 1.16k | std::vector<std::vector<uint8_t>> digests(num_contexts, std::vector<uint8_t>(digest_size)); | 54 | 3.55k | for (unsigned i = 0; i < num_contexts; i++) { | 55 | 2.39k | init_fn(&contexts[i]); | 56 | 2.39k | } | 57 | | | 58 | | // Intentionally misalign the data pointer to stress alignment sensitive paths | 59 | 1.16k | const size_t misalign_pad = fdp.ConsumeIntegralInRange<size_t>(0, 64); | 60 | 1.16k | std::vector<uint8_t> scratch_buf(misalign_pad + input_bytes.size()); | 61 | 1.16k | if (!input_bytes.empty()) { | 62 | 862 | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); | 63 | 862 | } | 64 | | | 65 | | // Define cursor and remaining bytes counter to keep track of the multiple hash update iterations | 66 | 1.16k | const uint8_t *cursor = scratch_buf.data() + misalign_pad; | 67 | 1.16k | size_t remaining = input_bytes.size(); | 68 | | | 69 | | // Perform multiple hash update iterations on the raw data | 70 | 1.16k | unsigned num_iterations = fdp.ConsumeIntegralInRange<unsigned>(1, 4); | 71 | 3.11k | while (num_iterations-- && remaining > 0) { | 72 | | // Pick which context to feed this iteration | 73 | 1.95k | 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 | 1.95k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; | 77 | 1.95k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); | 78 | | | 79 | 1.95k | size_t chunk_len = 0; | 80 | 1.95k | switch (pattern) { | 81 | 444 | case LESS1: { | 82 | | // Consume 1 byte less from block size from the raw data for this iteration | 83 | 444 | if (block_size > 1) { | 84 | 444 | chunk_len = std::min(remaining, block_size - 1); | 85 | 444 | } | 86 | 444 | 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 | 102 | case PLUS1: { | 94 | | // Consume 1 byte more from block size from the raw data for this iteration | 95 | 102 | chunk_len = std::min(remaining, block_size + 1); | 96 | 102 | 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 | 330 | case RANDOM: { | 105 | | // Consume random bytes from the raw data for this iteration | 106 | 330 | chunk_len = (remaining >= 1) ? (size_t)fdp.ConsumeIntegralInRange<size_t>(1, remaining) : 0; | 107 | 330 | break; | 108 | 0 | } | 109 | 111 | case TAIL: { | 110 | | // Consume all remaining bytes from the raw data for this iteration | 111 | 111 | chunk_len = remaining; | 112 | 111 | break; | 113 | 0 | } | 114 | 501 | case HALT: { | 115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration | 116 | 501 | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); | 117 | 501 | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); | 118 | 1.49k | for (size_t j = 0; j < loops && remaining > 0; j++) { | 119 | 990 | size_t w = std::min(remaining, step); | 120 | 990 | update_fn(&contexts[ctx_index], w, cursor); | 121 | 990 | cursor += w; | 122 | 990 | remaining -= w; | 123 | 990 | } | 124 | | | 125 | | // Randomly reinitialise the hash stream | 126 | 501 | if (fdp.ConsumeBool()) { | 127 | 235 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 128 | 235 | init_fn(&contexts[ctx_index]); | 129 | 235 | } | 130 | 501 | continue; | 131 | 0 | } | 132 | 1.95k | } | 133 | 1.45k | if (chunk_len == 0 || chunk_len > remaining) { | 134 | 0 | continue; | 135 | 0 | } | 136 | | | 137 | | // Occasionally reinitialise a context between update iterations | 138 | 1.45k | if (fdp.ConsumeBool()) { | 139 | 564 | init_fn(&contexts[ctx_index]); | 140 | 564 | } | 141 | | | 142 | | // Fuzz the update function | 143 | 1.45k | update_fn(&contexts[ctx_index], chunk_len, cursor); | 144 | 1.45k | cursor += chunk_len; | 145 | 1.45k | remaining -= chunk_len; | 146 | | | 147 | | // Randomly halt and reinitialise the stream | 148 | 1.45k | if (fdp.ConsumeBool()) { | 149 | 592 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 150 | 592 | init_fn(&contexts[ctx_index]); | 151 | 592 | } | 152 | 1.45k | } | 153 | | | 154 | | // Fuzz the finish function for all contexts | 155 | 3.55k | for (unsigned i = 0; i < num_contexts; i++) { | 156 | 2.39k | finish_fn(&contexts[i], digests[i].data()); | 157 | 2.39k | } | 158 | | | 159 | | // Additional fuzzing on special context chaining approach | 160 | 1.16k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { | 161 | 298 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 162 | 298 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 163 | 298 | 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 | 298 | } | 172 | 1.16k | } |
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 | 7 | return; | 44 | 7 | } | 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 | 4.09k | for (unsigned i = 0; i < num_contexts; i++) { | 55 | 2.86k | init_fn(&contexts[i]); | 56 | 2.86k | } | 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 | 976 | memcpy(scratch_buf.data() + misalign_pad, input_bytes.data(), input_bytes.size()); | 63 | 976 | } | 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.50k | while (num_iterations-- && remaining > 0) { | 72 | | // Pick which context to feed this iteration | 73 | 2.27k | const unsigned ctx_index = (num_contexts == 1) ? 0 : fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 74 | | | 75 | | // Choose a chunking pattern for this iteration | 76 | 2.27k | enum Pattern { LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT }; | 77 | 2.27k | Pattern pattern = fdp.PickValueInArray<Pattern>({LESS1, EQ, PLUS1, SMALL, RANDOM, TAIL, HALT}); | 78 | | | 79 | 2.27k | size_t chunk_len = 0; | 80 | 2.27k | switch (pattern) { | 81 | 547 | case LESS1: { | 82 | | // Consume 1 byte less from block size from the raw data for this iteration | 83 | 547 | if (block_size > 1) { | 84 | 547 | chunk_len = std::min(remaining, block_size - 1); | 85 | 547 | } | 86 | 547 | break; | 87 | 0 | } | 88 | 102 | case EQ: { | 89 | | // Consume block size bytes from the raw data for this iteration | 90 | 102 | chunk_len = std::min(remaining, block_size); | 91 | 102 | break; | 92 | 0 | } | 93 | 135 | case PLUS1: { | 94 | | // Consume 1 byte more from block size from the raw data for this iteration | 95 | 135 | chunk_len = std::min(remaining, block_size + 1); | 96 | 135 | break; | 97 | 0 | } | 98 | 332 | case SMALL: { | 99 | | // Consume 1 to 32 bytes from the raw data for this iteration | 100 | 332 | size_t small_len = (size_t)fdp.ConsumeIntegralInRange<int>(1, 32); | 101 | 332 | chunk_len = std::min(remaining, small_len); | 102 | 332 | 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 | 122 | case TAIL: { | 110 | | // Consume all remaining bytes from the raw data for this iteration | 111 | 122 | chunk_len = remaining; | 112 | 122 | break; | 113 | 0 | } | 114 | 576 | case HALT: { | 115 | | // Consume small chunk and consider reinitialisation or early halt of the hash iteration | 116 | 576 | size_t step = std::max<size_t>(1, fdp.ConsumeIntegralInRange<size_t>(1, block_size)); | 117 | 576 | size_t loops = fdp.ConsumeIntegralInRange<size_t>(1, 4); | 118 | 1.84k | for (size_t j = 0; j < loops && remaining > 0; j++) { | 119 | 1.26k | size_t w = std::min(remaining, step); | 120 | 1.26k | update_fn(&contexts[ctx_index], w, cursor); | 121 | 1.26k | cursor += w; | 122 | 1.26k | remaining -= w; | 123 | 1.26k | } | 124 | | | 125 | | // Randomly reinitialise the hash stream | 126 | 576 | if (fdp.ConsumeBool()) { | 127 | 262 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 128 | 262 | init_fn(&contexts[ctx_index]); | 129 | 262 | } | 130 | 576 | continue; | 131 | 0 | } | 132 | 2.27k | } | 133 | 1.70k | if (chunk_len == 0 || chunk_len > remaining) { | 134 | 0 | continue; | 135 | 0 | } | 136 | | | 137 | | // Occasionally reinitialise a context between update iterations | 138 | 1.70k | if (fdp.ConsumeBool()) { | 139 | 683 | init_fn(&contexts[ctx_index]); | 140 | 683 | } | 141 | | | 142 | | // Fuzz the update function | 143 | 1.70k | update_fn(&contexts[ctx_index], chunk_len, cursor); | 144 | 1.70k | cursor += chunk_len; | 145 | 1.70k | remaining -= chunk_len; | 146 | | | 147 | | // Randomly halt and reinitialise the stream | 148 | 1.70k | if (fdp.ConsumeBool()) { | 149 | 720 | finish_fn(&contexts[ctx_index], digests[ctx_index].data()); | 150 | 720 | init_fn(&contexts[ctx_index]); | 151 | 720 | } | 152 | 1.70k | } | 153 | | | 154 | | // Fuzz the finish function for all contexts | 155 | 4.09k | for (unsigned i = 0; i < num_contexts; i++) { | 156 | 2.86k | finish_fn(&contexts[i], digests[i].data()); | 157 | 2.86k | } | 158 | | | 159 | | // Additional fuzzing on special context chaining approach | 160 | 1.22k | if (num_contexts >= 2 && digest_size && fdp.ConsumeBool()) { | 161 | 357 | unsigned src_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 162 | 357 | unsigned dst_idx = fdp.ConsumeIntegralInRange<unsigned>(0, num_contexts - 1); | 163 | 357 | if (src_idx != dst_idx) { | 164 | 165 | init_fn(&contexts[dst_idx]); | 165 | 165 | size_t offset = fdp.ConsumeIntegralInRange<size_t>(0, digest_size - 1); | 166 | 165 | size_t feed_len = std::min(digest_size - offset, | 167 | 165 | (size_t)fdp.ConsumeIntegralInRange<size_t>(1, digest_size)); | 168 | 165 | update_fn(&contexts[dst_idx], feed_len, digests[src_idx].data() + offset); | 169 | 165 | finish_fn(&contexts[dst_idx], digests[dst_idx].data()); | 170 | 165 | } | 171 | 357 | } | 172 | 1.22k | } |
|
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.90k | for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 4); i++) { |
178 | 3.82k | switch (fdp.ConsumeIntegralInRange<int>(0, 2)) { |
179 | 1.42k | case 0: |
180 | 1.42k | fuzz_hash_int_multi<struct mhd_Md5CtxInt>( |
181 | 1.42k | fdp, mhd_MD5_BLOCK_SIZE, |
182 | 1.42k | mhd_MD5_init, mhd_MD5_update, mhd_MD5_finish, mhd_MD5_DIGEST_SIZE); |
183 | 1.42k | break; |
184 | 1.16k | case 1: |
185 | 1.16k | fuzz_hash_int_multi<struct mhd_Sha256CtxInt>( |
186 | 1.16k | fdp, mhd_SHA256_BLOCK_SIZE, |
187 | 1.16k | mhd_SHA256_init, mhd_SHA256_update, mhd_SHA256_finish, mhd_SHA256_DIGEST_SIZE); |
188 | 1.16k | 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.82k | } |
196 | 3.82k | } |
197 | 2.08k | return 0; |
198 | 2.08k | } |