• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdint>
16 #include <vector>
17 
18 #include "dice/dice.h"
19 #include "fuzzer/FuzzedDataProvider.h"
20 
21 namespace dice {
22 namespace fuzz {
23 
ConsumeRandomLengthStringAsBytesFrom(FuzzedDataProvider & fdp)24 static inline std::vector<uint8_t> ConsumeRandomLengthStringAsBytesFrom(
25     FuzzedDataProvider& fdp) {
26   auto s = fdp.ConsumeRandomLengthString();
27   return std::vector<uint8_t>(s.begin(), s.end());
28 }
29 
30 struct FuzzedInputValues {
ConsumeFromFuzzedInputValues31   static FuzzedInputValues ConsumeFrom(FuzzedDataProvider& fdp) {
32     FuzzedInputValues fiv = {};
33     DiceInputValues& input_values = fiv.input_values_;
34 
35     fdp.ConsumeData(&input_values.code_hash, DICE_HASH_SIZE);
36 
37     fiv.code_descriptor_ = ConsumeRandomLengthStringAsBytesFrom(fdp);
38     input_values.code_descriptor = fiv.code_descriptor_.data();
39     input_values.code_descriptor_size = fiv.code_descriptor_.size();
40 
41     input_values.config_type = (DiceConfigType)fdp.ConsumeIntegralInRange(0, 1);
42 
43     fdp.ConsumeData(&input_values.config_value, DICE_INLINE_CONFIG_SIZE);
44 
45     fiv.config_descriptor_ = ConsumeRandomLengthStringAsBytesFrom(fdp);
46     input_values.config_descriptor = fiv.config_descriptor_.data();
47     input_values.config_descriptor_size = fiv.config_descriptor_.size();
48 
49     fdp.ConsumeData(&input_values.authority_hash, DICE_HASH_SIZE);
50 
51     fiv.authority_descriptor_ = ConsumeRandomLengthStringAsBytesFrom(fdp);
52     input_values.authority_descriptor = fiv.authority_descriptor_.data();
53     input_values.authority_descriptor_size = fiv.authority_descriptor_.size();
54 
55     input_values.mode = (DiceMode)fdp.ConsumeIntegralInRange(0, 3);
56 
57     fdp.ConsumeData(&input_values.hidden, DICE_HIDDEN_SIZE);
58 
59     return fiv;
60   }
61 
62   operator const DiceInputValues*() const { return &input_values_; }
63 
64   std::vector<uint8_t> code_descriptor_;
65   std::vector<uint8_t> config_descriptor_;
66   std::vector<uint8_t> authority_descriptor_;
67 
68   DiceInputValues input_values_;
69 };
70 
71 }  // namespace fuzz
72 }  // namespace dice
73