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/android.h"
16
17 #include "dice/test_framework.h"
18
19 namespace {
20
21 extern "C" {
22
TEST(DiceAndroidConfigTest,NoConfigFields)23 TEST(DiceAndroidConfigTest, NoConfigFields) {
24 DiceAndroidConfigValues input_values = {};
25 uint8_t buffer[10];
26 size_t buffer_size;
27 DiceResult result = DiceAndroidFormatConfigDescriptor(
28 &input_values, sizeof(buffer), buffer, &buffer_size);
29 EXPECT_EQ(kDiceResultOk, result);
30 EXPECT_EQ(1u, buffer_size);
31 EXPECT_EQ(0xa0, buffer[0]);
32 }
33
TEST(DiceAndroidConfigTest,NoConfigFieldsMeasurement)34 TEST(DiceAndroidConfigTest, NoConfigFieldsMeasurement) {
35 DiceAndroidConfigValues config_values = {};
36 size_t buffer_size;
37 DiceResult result =
38 DiceAndroidFormatConfigDescriptor(&config_values, 0, NULL, &buffer_size);
39 EXPECT_EQ(kDiceResultBufferTooSmall, result);
40 EXPECT_EQ(1u, buffer_size);
41 }
42
TEST(DiceAndroidConfigTest,AllConfigFields)43 TEST(DiceAndroidConfigTest, AllConfigFields) {
44 DiceAndroidConfigValues config_values = {
45 .configs = DICE_ANDROID_CONFIG_COMPONENT_NAME |
46 DICE_ANDROID_CONFIG_COMPONENT_VERSION |
47 DICE_ANDROID_CONFIG_RESETTABLE |
48 DICE_ANDROID_CONFIG_SECURITY_VERSION |
49 DICE_ANDROID_CONFIG_RKP_VM_MARKER,
50 .component_name = "Test Component Name",
51 .component_version = 0x232a13dec90f42b5,
52 .security_version = 0xfab777c1,
53 };
54 size_t buffer_size;
55 DiceResult result =
56 DiceAndroidFormatConfigDescriptor(&config_values, 0, NULL, &buffer_size);
57 EXPECT_EQ(kDiceResultBufferTooSmall, result);
58 std::vector<uint8_t> buffer(buffer_size);
59 const uint8_t expected[] = {
60 0xa5, 0x3a, 0x00, 0x01, 0x11, 0x71, 0x73, 'T', 'e', 's', 't',
61 ' ', 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n', 't', ' ',
62 'N', 'a', 'm', 'e', 0x3a, 0x00, 0x01, 0x11, 0x72, 0x1b, 0x23,
63 0x2a, 0x13, 0xde, 0xc9, 0x0f, 0x42, 0xb5, 0x3a, 0x00, 0x01, 0x11,
64 0x73, 0xf6, 0x3a, 0x00, 0x01, 0x11, 0x74, 0x1a, 0xfa, 0xb7, 0x77,
65 0xc1, 0x3a, 0x00, 0x01, 0x11, 0x75, 0xf6};
66 EXPECT_EQ(sizeof(expected), buffer.size());
67 result = DiceAndroidFormatConfigDescriptor(&config_values, buffer.size(),
68 buffer.data(), &buffer_size);
69 EXPECT_EQ(sizeof(expected), buffer_size);
70 EXPECT_EQ(0, memcmp(expected, buffer.data(), sizeof(expected)));
71 }
72
TEST(DiceAndroidTest,PreservesPreviousEntries)73 TEST(DiceAndroidTest, PreservesPreviousEntries) {
74 const uint8_t chain[] = {
75 // Fake DICE chain with the root public key and two entries.
76 0x83,
77 // Fake public key.
78 0xa6, 0x01, 0x02, 0x03, 0x27, 0x04, 0x02, 0x20, 0x01, 0x21, 0x40, 0x22,
79 0x40,
80 // Fake DICE chain entry.
81 0x84, 0x40, 0xa0, 0x40, 0x40,
82 // Fake DICE chain entry.
83 0x84, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40,
84 // 8-bytes of trailing data that aren't part of the DICE chain.
85 0x84, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
86 const uint8_t fake_cdi_attest[DICE_CDI_SIZE] = {};
87 const uint8_t fake_cdi_seal[DICE_CDI_SIZE] = {};
88 DiceInputValues input_values = {};
89 size_t next_chain_size;
90 uint8_t next_cdi_attest[DICE_CDI_SIZE];
91 uint8_t next_cdi_seal[DICE_CDI_SIZE];
92 DiceResult result = DiceAndroidMainFlow(
93 /*context=*/NULL, fake_cdi_attest, fake_cdi_seal, chain, sizeof(chain),
94 &input_values, 0, NULL, &next_chain_size, next_cdi_attest, next_cdi_seal);
95 EXPECT_EQ(kDiceResultBufferTooSmall, result);
96 EXPECT_GT(next_chain_size, sizeof(chain));
97 std::vector<uint8_t> next_chain(next_chain_size);
98 result = DiceAndroidMainFlow(
99 /*context=*/NULL, fake_cdi_attest, fake_cdi_seal, chain, sizeof(chain),
100 &input_values, next_chain.size(), next_chain.data(), &next_chain_size,
101 next_cdi_attest, next_cdi_seal);
102 EXPECT_EQ(kDiceResultOk, result);
103 EXPECT_EQ(next_chain_size, next_chain.size());
104 EXPECT_EQ(0x84, next_chain[0]);
105 EXPECT_NE(0, memcmp(next_chain.data() + 1, chain + 1, sizeof(chain) - 1));
106 EXPECT_EQ(0, memcmp(next_chain.data() + 1, chain + 1, sizeof(chain) - 8 - 1));
107 }
108
TEST(DiceAndroidHandoverTest,PreservesPreviousEntries)109 TEST(DiceAndroidHandoverTest, PreservesPreviousEntries) {
110 const uint8_t handover[] = {
111 0xa3,
112 // CDI attest
113 0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
116 // CDI seal
117 0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
119 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
120 // DICE chain
121 0x03, 0x82, 0xa6, 0x01, 0x02, 0x03, 0x27, 0x04, 0x02, 0x20, 0x01, 0x21,
122 0x40, 0x22, 0x40, 0x84, 0x40, 0xa0, 0x40, 0x40,
123 // 8-bytes of trailing data that aren't part of the DICE chain.
124 0x84, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
125 DiceInputValues input_values = {};
126 size_t next_handover_size;
127 DiceResult result = DiceAndroidHandoverMainFlow(
128 /*context=*/NULL, handover, sizeof(handover), &input_values, 0, NULL,
129 &next_handover_size);
130 EXPECT_EQ(kDiceResultBufferTooSmall, result);
131 EXPECT_GT(next_handover_size, sizeof(handover));
132 std::vector<uint8_t> next_handover(next_handover_size);
133 result = DiceAndroidHandoverMainFlow(
134 /*context=*/NULL, handover, sizeof(handover), &input_values,
135 next_handover.size(), next_handover.data(), &next_handover_size);
136 EXPECT_EQ(kDiceResultOk, result);
137 EXPECT_EQ(next_handover_size, next_handover.size());
138 EXPECT_EQ(0xa3, next_handover[0]);
139 EXPECT_EQ(0x83, next_handover[72]);
140 EXPECT_NE(0, memcmp(next_handover.data() + 73, handover + 73,
141 sizeof(handover) - 73));
142 EXPECT_EQ(0, memcmp(next_handover.data() + 73, handover + 73,
143 sizeof(handover) - 8 - 73));
144 }
145
TEST(DiceAndroidHandoverTest,InHandoverWithoutDiceChainOutHandoverWithDiceChain)146 TEST(DiceAndroidHandoverTest,
147 InHandoverWithoutDiceChainOutHandoverWithDiceChain) {
148 const uint8_t handover[] = {
149 0xa2,
150 // CDI attest
151 0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154 // CDI seal
155 0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
158 // 8-bytes of trailing data that aren't part of the DICE chain.
159 0x00, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
160 DiceInputValues input_values = {};
161 size_t next_handover_size;
162 DiceResult result = DiceAndroidHandoverMainFlow(
163 /*context=*/NULL, handover, sizeof(handover), &input_values, 0, NULL,
164 &next_handover_size);
165 EXPECT_EQ(kDiceResultBufferTooSmall, result);
166 EXPECT_GT(next_handover_size, sizeof(handover));
167 std::vector<uint8_t> next_handover(next_handover_size);
168 result = DiceAndroidHandoverMainFlow(
169 /*context=*/NULL, handover, sizeof(handover), &input_values,
170 next_handover.size(), next_handover.data(), &next_handover_size);
171 EXPECT_EQ(kDiceResultOk, result);
172 EXPECT_EQ(next_handover_size, next_handover.size());
173 EXPECT_EQ(0xa3, next_handover[0]);
174 }
175
TEST(DiceAndroidHandoverTest,InHandoverWithoutDiceChainButUnknownFieldOutHandoverWithDiceChain)176 TEST(DiceAndroidHandoverTest,
177 InHandoverWithoutDiceChainButUnknownFieldOutHandoverWithDiceChain) {
178 const uint8_t handover[] = {
179 0xa3,
180 // CDI attest
181 0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
182 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
183 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
184 // CDI seal
185 0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
186 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188 // Ignored unknown field
189 0x04, 0x01,
190 // 8-bytes of trailing data that aren't part of the DICE chain.
191 0x00, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
192 DiceInputValues input_values = {};
193 size_t next_handover_size;
194 DiceResult result = DiceAndroidHandoverMainFlow(
195 /*context=*/NULL, handover, sizeof(handover), &input_values, 0, NULL,
196 &next_handover_size);
197 EXPECT_EQ(kDiceResultBufferTooSmall, result);
198 EXPECT_GT(next_handover_size, sizeof(handover));
199 std::vector<uint8_t> next_handover(next_handover_size);
200 result = DiceAndroidHandoverMainFlow(
201 /*context=*/NULL, handover, sizeof(handover), &input_values,
202 next_handover.size(), next_handover.data(), &next_handover_size);
203 EXPECT_EQ(kDiceResultOk, result);
204 EXPECT_EQ(next_handover_size, next_handover.size());
205 EXPECT_EQ(0xa3, next_handover[0]);
206 }
207
TEST(DiceAndroidHandoverTest,ParseHandover)208 TEST(DiceAndroidHandoverTest, ParseHandover) {
209 const uint8_t handover[] = {
210 0xa3,
211 // CDI attest
212 0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
213 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
214 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
215 // CDI seal
216 0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
218 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
219 // DICE chain
220 0x03, 0x82, 0xa6, 0x01, 0x02, 0x03, 0x27, 0x04, 0x02, 0x20, 0x01, 0x21,
221 0x40, 0x22, 0x40, 0x84, 0x40, 0xa0, 0x40, 0x40,
222 // 8-bytes of trailing data that aren't part of the DICE chain.
223 0x00, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
224 const uint8_t *cdi_attest;
225 const uint8_t *cdi_seal;
226 const uint8_t *chain;
227 size_t chain_size;
228 DiceResult result = DiceAndroidHandoverParse(
229 handover, sizeof(handover), &cdi_attest, &cdi_seal, &chain, &chain_size);
230 EXPECT_EQ(kDiceResultOk, result);
231 EXPECT_EQ(handover + 4, cdi_attest);
232 EXPECT_EQ(handover + 39, cdi_seal);
233 EXPECT_EQ(handover + 72, chain);
234 EXPECT_EQ(19u, chain_size);
235 }
236
TEST(DiceAndroidHandoverTest,ParseHandoverWithoutDiceChain)237 TEST(DiceAndroidHandoverTest, ParseHandoverWithoutDiceChain) {
238 const uint8_t handover[] = {
239 0xa2,
240 // CDI attest
241 0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
242 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
244 // CDI seal
245 0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
247 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
248 // 8-bytes of trailing data that aren't part of the DICE chain.
249 0x00, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
250 const uint8_t *cdi_attest;
251 const uint8_t *cdi_seal;
252 const uint8_t *chain;
253 size_t chain_size;
254 DiceResult result = DiceAndroidHandoverParse(
255 handover, sizeof(handover), &cdi_attest, &cdi_seal, &chain, &chain_size);
256 EXPECT_EQ(kDiceResultOk, result);
257 EXPECT_EQ(handover + 4, cdi_attest);
258 EXPECT_EQ(handover + 39, cdi_seal);
259 EXPECT_EQ(nullptr, chain);
260 EXPECT_EQ(0u, chain_size);
261 }
262
TEST(DiceAndroidHandoverTest,ParseHandoverWithoutDiceChainButUnknownField)263 TEST(DiceAndroidHandoverTest, ParseHandoverWithoutDiceChainButUnknownField) {
264 const uint8_t handover[] = {
265 0xa3,
266 // CDI attest
267 0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
268 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
269 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
270 // CDI seal
271 0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
272 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
274 // Ignored unknown field
275 0x04, 0x01,
276 // 8-bytes of trailing data that aren't part of the DICE chain.
277 0x00, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
278 const uint8_t *cdi_attest;
279 const uint8_t *cdi_seal;
280 const uint8_t *chain;
281 size_t chain_size;
282 DiceResult result = DiceAndroidHandoverParse(
283 handover, sizeof(handover), &cdi_attest, &cdi_seal, &chain, &chain_size);
284 EXPECT_EQ(kDiceResultOk, result);
285 EXPECT_EQ(handover + 4, cdi_attest);
286 EXPECT_EQ(handover + 39, cdi_seal);
287 EXPECT_EQ(nullptr, chain);
288 EXPECT_EQ(0u, chain_size);
289 }
290
TEST(DiceAndroidHandoverTest,ParseHandoverCdiTooLarge)291 TEST(DiceAndroidHandoverTest, ParseHandoverCdiTooLarge) {
292 const uint8_t handover[] = {
293 0xa2,
294 // CDI attest
295 0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
296 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
297 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
298 // CDI seal
299 0x02, 0x58, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
300 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
301 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
302 // 8-bytes of trailing data that aren't part of the DICE chain.
303 0x00, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
304 const uint8_t *cdi_attest;
305 const uint8_t *cdi_seal;
306 const uint8_t *chain;
307 size_t chain_size;
308 DiceResult result = DiceAndroidHandoverParse(
309 handover, sizeof(handover), &cdi_attest, &cdi_seal, &chain, &chain_size);
310 EXPECT_EQ(kDiceResultInvalidInput, result);
311 }
312 }
313
314 } // namespace
315