xref: /aosp_15_r20/external/open-dice/src/fuzzer.cc (revision 60b67249c2e226f42f35cc6cfe66c6048e0bae6b)
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "dice/dice.h"
16 #include "dice/fuzz_utils.h"
17 #include "dice/utils.h"
18 #include "fuzzer/FuzzedDataProvider.h"
19 
20 using dice::fuzz::ConsumeRandomLengthStringAsBytesFrom;
21 using dice::fuzz::FuzzedInputValues;
22 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)23 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
24   // Exit early if there might not be enough data to fill buffers.
25   if (size < 512) {
26     return 0;
27   }
28 
29   FuzzedDataProvider fdp(data, size);
30 
31   // Prepare the fuzzed inputs.
32   auto input_values = FuzzedInputValues::ConsumeFrom(fdp);
33   uint8_t current_cdi_attest[DICE_CDI_SIZE] = {};
34   uint8_t current_cdi_seal[DICE_CDI_SIZE] = {};
35 
36   fdp.ConsumeData(&current_cdi_attest, sizeof(current_cdi_attest));
37   fdp.ConsumeData(&current_cdi_seal, sizeof(current_cdi_seal));
38 
39   // Initialize output parameters with fuzz data in case they are wrongly being
40   // read from.
41   constexpr size_t kNextCdiCertificateBufferSize = 1024;
42   auto next_cdi_certificate_actual_size = fdp.ConsumeIntegral<size_t>();
43   uint8_t next_cdi_certificate[kNextCdiCertificateBufferSize] = {};
44   uint8_t next_cdi_attest[DICE_CDI_SIZE] = {};
45   uint8_t next_cdi_seal[DICE_CDI_SIZE] = {};
46 
47   fdp.ConsumeData(&next_cdi_certificate, kNextCdiCertificateBufferSize);
48   fdp.ConsumeData(&next_cdi_attest, DICE_CDI_SIZE);
49   fdp.ConsumeData(&next_cdi_seal, DICE_CDI_SIZE);
50 
51   // Fuzz the main flow.
52   DiceMainFlow(/*context=*/NULL, current_cdi_attest, current_cdi_seal,
53                input_values, kNextCdiCertificateBufferSize,
54                next_cdi_certificate, &next_cdi_certificate_actual_size,
55                next_cdi_attest, next_cdi_seal);
56   return 0;
57 }
58