• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 <stddef.h>
16 #include <stdint.h>
17 #include <stdio.h>
18 
19 #include <memory>
20 
21 #include "dice/dice.h"
22 #include "dice/known_test_values.h"
23 #include "dice/test_framework.h"
24 #include "dice/test_utils.h"
25 #include "dice/utils.h"
26 #include "pw_string/format.h"
27 
28 namespace {
29 
30 using dice::test::CertificateType_Cbor;
31 using dice::test::DeriveFakeInputValue;
32 using dice::test::DiceStateForTest;
33 using dice::test::KeyType_Ed25519;
34 
TEST(DiceOpsTest,KnownAnswerZeroInput)35 TEST(DiceOpsTest, KnownAnswerZeroInput) {
36   DiceStateForTest current_state = {};
37   DiceStateForTest next_state = {};
38   DiceInputValues input_values = {};
39   DiceResult result = DiceMainFlow(
40       NULL, current_state.cdi_attest, current_state.cdi_seal, &input_values,
41       sizeof(next_state.certificate), next_state.certificate,
42       &next_state.certificate_size, next_state.cdi_attest, next_state.cdi_seal);
43   EXPECT_EQ(kDiceResultOk, result);
44   DumpState(CertificateType_Cbor, KeyType_Ed25519, "zero_input", next_state);
45   // Both CDI values and the certificate should be deterministic.
46   EXPECT_EQ(0, memcmp(next_state.cdi_attest,
47                       dice::test::kExpectedCdiAttest_ZeroInput, DICE_CDI_SIZE));
48   EXPECT_EQ(0, memcmp(next_state.cdi_seal,
49                       dice::test::kExpectedCdiSeal_ZeroInput, DICE_CDI_SIZE));
50   ASSERT_EQ(sizeof(dice::test::kExpectedCborEd25519Cert_ZeroInput),
51             next_state.certificate_size);
52   EXPECT_EQ(0, memcmp(dice::test::kExpectedCborEd25519Cert_ZeroInput,
53                       next_state.certificate, next_state.certificate_size));
54 }
55 
TEST(DiceOpsTest,KnownAnswerHashOnlyInput)56 TEST(DiceOpsTest, KnownAnswerHashOnlyInput) {
57   DiceStateForTest current_state = {};
58   DeriveFakeInputValue("cdi_attest", DICE_CDI_SIZE, current_state.cdi_attest);
59   DeriveFakeInputValue("cdi_seal", DICE_CDI_SIZE, current_state.cdi_seal);
60   DiceStateForTest next_state = {};
61   DiceInputValues input_values = {};
62   DeriveFakeInputValue("code_hash", DICE_HASH_SIZE, input_values.code_hash);
63   DeriveFakeInputValue("authority_hash", DICE_HASH_SIZE,
64                        input_values.authority_hash);
65   input_values.config_type = kDiceConfigTypeInline;
66   DeriveFakeInputValue("inline_config", DICE_INLINE_CONFIG_SIZE,
67                        input_values.config_value);
68 
69   DiceResult result = DiceMainFlow(
70       NULL, current_state.cdi_attest, current_state.cdi_seal, &input_values,
71       sizeof(next_state.certificate), next_state.certificate,
72       &next_state.certificate_size, next_state.cdi_attest, next_state.cdi_seal);
73   EXPECT_EQ(kDiceResultOk, result);
74   DumpState(CertificateType_Cbor, KeyType_Ed25519, "hash_only_input",
75             next_state);
76   // Both CDI values and the certificate should be deterministic.
77   EXPECT_EQ(
78       0, memcmp(next_state.cdi_attest,
79                 dice::test::kExpectedCdiAttest_HashOnlyInput, DICE_CDI_SIZE));
80   EXPECT_EQ(
81       0, memcmp(next_state.cdi_seal, dice::test::kExpectedCdiSeal_HashOnlyInput,
82                 DICE_CDI_SIZE));
83   ASSERT_EQ(sizeof(dice::test::kExpectedCborEd25519Cert_HashOnlyInput),
84             next_state.certificate_size);
85   EXPECT_EQ(0, memcmp(dice::test::kExpectedCborEd25519Cert_HashOnlyInput,
86                       next_state.certificate, next_state.certificate_size));
87 }
88 
TEST(DiceOpsTest,KnownAnswerDescriptorInput)89 TEST(DiceOpsTest, KnownAnswerDescriptorInput) {
90   DiceStateForTest current_state = {};
91   DeriveFakeInputValue("cdi_attest", DICE_CDI_SIZE, current_state.cdi_attest);
92   DeriveFakeInputValue("cdi_seal", DICE_CDI_SIZE, current_state.cdi_seal);
93 
94   DiceStateForTest next_state = {};
95 
96   DiceInputValues input_values = {};
97   DeriveFakeInputValue("code_hash", DICE_HASH_SIZE, input_values.code_hash);
98   uint8_t code_descriptor[100];
99   DeriveFakeInputValue("code_desc", sizeof(code_descriptor), code_descriptor);
100   input_values.code_descriptor = code_descriptor;
101   input_values.code_descriptor_size = sizeof(code_descriptor);
102 
103   uint8_t config_descriptor[40];
104   DeriveFakeInputValue("config_desc", sizeof(config_descriptor),
105                        config_descriptor);
106   input_values.config_descriptor = config_descriptor;
107   input_values.config_descriptor_size = sizeof(config_descriptor);
108   input_values.config_type = kDiceConfigTypeDescriptor;
109 
110   DeriveFakeInputValue("authority_hash", DICE_HASH_SIZE,
111                        input_values.authority_hash);
112   uint8_t authority_descriptor[65];
113   DeriveFakeInputValue("authority_desc", sizeof(authority_descriptor),
114                        authority_descriptor);
115   input_values.authority_descriptor = authority_descriptor;
116   input_values.authority_descriptor_size = sizeof(authority_descriptor);
117 
118   DiceResult result = DiceMainFlow(
119       NULL, current_state.cdi_attest, current_state.cdi_seal, &input_values,
120       sizeof(next_state.certificate), next_state.certificate,
121       &next_state.certificate_size, next_state.cdi_attest, next_state.cdi_seal);
122   EXPECT_EQ(kDiceResultOk, result);
123   DumpState(CertificateType_Cbor, KeyType_Ed25519, "descriptor_input",
124             next_state);
125   // Both CDI values and the certificate should be deterministic.
126   EXPECT_EQ(
127       0, memcmp(next_state.cdi_attest,
128                 dice::test::kExpectedCdiAttest_DescriptorInput, DICE_CDI_SIZE));
129   EXPECT_EQ(
130       0, memcmp(next_state.cdi_seal,
131                 dice::test::kExpectedCdiSeal_DescriptorInput, DICE_CDI_SIZE));
132   ASSERT_EQ(sizeof(dice::test::kExpectedCborEd25519Cert_DescriptorInput),
133             next_state.certificate_size);
134   EXPECT_EQ(0, memcmp(dice::test::kExpectedCborEd25519Cert_DescriptorInput,
135                       next_state.certificate, next_state.certificate_size));
136 }
137 
TEST(DiceOpsTest,NonZeroMode)138 TEST(DiceOpsTest, NonZeroMode) {
139   constexpr size_t kModeOffsetInCert = 315;
140   DiceStateForTest current_state = {};
141   DiceStateForTest next_state = {};
142   DiceInputValues input_values = {};
143   input_values.mode = kDiceModeDebug;
144   DiceResult result = DiceMainFlow(
145       NULL, current_state.cdi_attest, current_state.cdi_seal, &input_values,
146       sizeof(next_state.certificate), next_state.certificate,
147       &next_state.certificate_size, next_state.cdi_attest, next_state.cdi_seal);
148   EXPECT_EQ(kDiceResultOk, result);
149   EXPECT_EQ(kDiceModeDebug, next_state.certificate[kModeOffsetInCert]);
150 }
151 
TEST(DiceOpsTest,LargeInputs)152 TEST(DiceOpsTest, LargeInputs) {
153   constexpr uint8_t kBigBuffer[1024 * 1024] = {};
154   DiceStateForTest current_state = {};
155   DiceStateForTest next_state = {};
156   DiceInputValues input_values = {};
157   input_values.code_descriptor = kBigBuffer;
158   input_values.code_descriptor_size = sizeof(kBigBuffer);
159   DiceResult result = DiceMainFlow(
160       NULL, current_state.cdi_attest, current_state.cdi_seal, &input_values,
161       sizeof(next_state.certificate), next_state.certificate,
162       &next_state.certificate_size, next_state.cdi_attest, next_state.cdi_seal);
163   EXPECT_EQ(kDiceResultBufferTooSmall, result);
164 }
165 
TEST(DiceOpsTest,InvalidConfigType)166 TEST(DiceOpsTest, InvalidConfigType) {
167   DiceStateForTest current_state = {};
168   DiceStateForTest next_state = {};
169   DiceInputValues input_values = {};
170   input_values.config_type = (DiceConfigType)55;
171   DiceResult result = DiceMainFlow(
172       NULL, current_state.cdi_attest, current_state.cdi_seal, &input_values,
173       sizeof(next_state.certificate), next_state.certificate,
174       &next_state.certificate_size, next_state.cdi_attest, next_state.cdi_seal);
175   EXPECT_EQ(kDiceResultInvalidInput, result);
176 }
177 
TEST(DiceOpsTest,PartialCertChain)178 TEST(DiceOpsTest, PartialCertChain) {
179   constexpr size_t kNumLayers = 7;
180   DiceStateForTest states[kNumLayers + 1] = {};
181   DiceInputValues inputs[kNumLayers] = {};
182   for (size_t i = 0; i < kNumLayers; ++i) {
183     char seed[40];
184     pw::string::Format(seed, "code_hash_%zu", i);
185     DeriveFakeInputValue(seed, DICE_HASH_SIZE, inputs[i].code_hash);
186     pw::string::Format(seed, "authority_hash_%zu", i);
187     DeriveFakeInputValue(seed, DICE_HASH_SIZE, inputs[i].authority_hash);
188     inputs[i].config_type = kDiceConfigTypeInline;
189     pw::string::Format(seed, "inline_config_%zu", i);
190     DeriveFakeInputValue(seed, DICE_INLINE_CONFIG_SIZE, inputs[i].config_value);
191     inputs[i].mode = kDiceModeNormal;
192     EXPECT_EQ(
193         kDiceResultOk,
194         DiceMainFlow(/*context=*/NULL, states[i].cdi_attest, states[i].cdi_seal,
195                      &inputs[i], sizeof(states[i + 1].certificate),
196                      states[i + 1].certificate, &states[i + 1].certificate_size,
197                      states[i + 1].cdi_attest, states[i + 1].cdi_seal));
198     char suffix[40];
199     pw::string::Format(suffix, "part_cert_chain_%zu", i);
200     DumpState(CertificateType_Cbor, KeyType_Ed25519, suffix, states[i + 1]);
201   }
202   // Use the first derived CDI cert as the 'root' of partial chain.
203   EXPECT_TRUE(dice::test::VerifyCertificateChain(
204       CertificateType_Cbor, states[1].certificate, states[1].certificate_size,
205       &states[2], kNumLayers - 1, /*is_partial_chain=*/true));
206 }
207 
TEST(DiceOpsTest,FullCertChain)208 TEST(DiceOpsTest, FullCertChain) {
209   constexpr size_t kNumLayers = 7;
210   DiceStateForTest states[kNumLayers + 1] = {};
211   DiceInputValues inputs[kNumLayers] = {};
212   for (size_t i = 0; i < kNumLayers; ++i) {
213     char seed[40];
214     pw::string::Format(seed, "code_hash_%zu", i);
215     DeriveFakeInputValue(seed, DICE_HASH_SIZE, inputs[i].code_hash);
216     pw::string::Format(seed, "authority_hash_%zu", i);
217     DeriveFakeInputValue(seed, DICE_HASH_SIZE, inputs[i].authority_hash);
218     inputs[i].config_type = kDiceConfigTypeInline;
219     pw::string::Format(seed, "inline_config_%zu", i);
220     DeriveFakeInputValue(seed, DICE_INLINE_CONFIG_SIZE, inputs[i].config_value);
221     inputs[i].mode = kDiceModeNormal;
222     EXPECT_EQ(
223         kDiceResultOk,
224         DiceMainFlow(/*context=*/NULL, states[i].cdi_attest, states[i].cdi_seal,
225                      &inputs[i], sizeof(states[i + 1].certificate),
226                      states[i + 1].certificate, &states[i + 1].certificate_size,
227                      states[i + 1].cdi_attest, states[i + 1].cdi_seal));
228     char suffix[40];
229     pw::string::Format(suffix, "full_cert_chain_%zu", i);
230     DumpState(CertificateType_Cbor, KeyType_Ed25519, suffix, states[i + 1]);
231   }
232   // Use a fake self-signed UDS cert as the 'root'.
233   uint8_t root_certificate[dice::test::kTestCertSize];
234   size_t root_certificate_size = 0;
235   dice::test::CreateFakeUdsCertificate(
236       NULL, states[0].cdi_attest, CertificateType_Cbor, KeyType_Ed25519,
237       root_certificate, &root_certificate_size);
238   EXPECT_TRUE(dice::test::VerifyCertificateChain(
239       CertificateType_Cbor, root_certificate, root_certificate_size, &states[1],
240       kNumLayers, /*is_partial_chain=*/false));
241 }
242 
243 }  // namespace
244