• 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 "dice/android/bcc.h"
16 
17 #include "dice/test_framework.h"
18 
19 namespace {
20 
21 extern "C" {
22 
TEST(BccConfigTest,NoInputs)23 TEST(BccConfigTest, NoInputs) {
24   BccConfigValues input_values = {};
25   uint8_t buffer[10];
26   size_t buffer_size;
27   DiceResult result = BccFormatConfigDescriptor(&input_values, sizeof(buffer),
28                                                 buffer, &buffer_size);
29   EXPECT_EQ(kDiceResultOk, result);
30   EXPECT_EQ(1u, buffer_size);
31   EXPECT_EQ(0xa0, buffer[0]);
32 }
33 
TEST(BccConfigTest,AllInputs)34 TEST(BccConfigTest, AllInputs) {
35   BccConfigValues input_values = {
36       .inputs = BCC_INPUT_COMPONENT_NAME | BCC_INPUT_COMPONENT_VERSION |
37                 BCC_INPUT_RESETTABLE,
38       .component_name = "Test Component Name",
39       .component_version = 0x232a13dec90f42b5,
40   };
41   uint8_t buffer[256];
42   size_t buffer_size;
43   DiceResult result = BccFormatConfigDescriptor(&input_values, sizeof(buffer),
44                                                 buffer, &buffer_size);
45   EXPECT_EQ(kDiceResultOk, result);
46   const uint8_t expected[] = {
47       0xa3, 0x3a, 0x00, 0x01, 0x11, 0x71, 0x73, 'T',  'e',  's',  't',  ' ',
48       'C',  'o',  'm',  'p',  'o',  'n',  'e',  'n',  't',  ' ',  'N',  'a',
49       'm',  'e',  0x3a, 0x00, 0x01, 0x11, 0x72, 0x1b, 0x23, 0x2a, 0x13, 0xde,
50       0xc9, 0x0f, 0x42, 0xb5, 0x3a, 0x00, 0x01, 0x11, 0x73, 0xf6};
51   EXPECT_EQ(sizeof(expected), buffer_size);
52   EXPECT_EQ(0, memcmp(expected, buffer, buffer_size));
53 }
54 
TEST(BccTest,PreservesPreviousEntries)55 TEST(BccTest, PreservesPreviousEntries) {
56   const uint8_t bcc[] = {
57       // Fake BCC with the root public key and two entries.
58       0x83,
59       // Fake public key.
60       0xa6, 0x01, 0x02, 0x03, 0x27, 0x04, 0x02, 0x20, 0x01, 0x21, 0x40, 0x22,
61       0x40,
62       // Fake BCC entry.
63       0x84, 0x40, 0xa0, 0x40, 0x40,
64       // Fake BCC entry.
65       0x84, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40,
66       // 8-bytes of trailing data that aren't part of the BCC.
67       0x84, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
68   const uint8_t fake_cdi_attest[DICE_CDI_SIZE] = {};
69   const uint8_t fake_cdi_seal[DICE_CDI_SIZE] = {};
70   DiceInputValues input_values = {};
71   uint8_t next_bcc[2048] = {};
72   size_t next_bcc_size;
73   uint8_t next_cdi_attest[DICE_CDI_SIZE];
74   uint8_t next_cdi_seal[DICE_CDI_SIZE];
75   DiceResult result =
76       BccMainFlow(/*context=*/NULL, fake_cdi_attest, fake_cdi_seal, bcc,
77                   sizeof(bcc), &input_values, sizeof(next_bcc), next_bcc,
78                   &next_bcc_size, next_cdi_attest, next_cdi_seal);
79   EXPECT_EQ(kDiceResultOk, result);
80   EXPECT_GT(next_bcc_size, sizeof(bcc));
81   EXPECT_EQ(0x84, next_bcc[0]);
82   EXPECT_NE(0, memcmp(next_bcc + 1, bcc + 1, sizeof(bcc) - 1));
83   EXPECT_EQ(0, memcmp(next_bcc + 1, bcc + 1, sizeof(bcc) - 8 - 1));
84 }
85 
TEST(BccHandoverTest,PreservesPreviousEntries)86 TEST(BccHandoverTest, PreservesPreviousEntries) {
87   const uint8_t bcc_handover[] = {
88       0xa3,
89       // CDI attest
90       0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93       // CDI seal
94       0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97       // BCC
98       0x03, 0x82, 0xa6, 0x01, 0x02, 0x03, 0x27, 0x04, 0x02, 0x20, 0x01, 0x21,
99       0x40, 0x22, 0x40, 0x84, 0x40, 0xa0, 0x40, 0x40,
100       // 8-bytes of trailing data that aren't part of the BCC.
101       0x84, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
102   DiceInputValues input_values = {};
103   uint8_t next_bcc_handover[2048] = {};
104   size_t next_bcc_handover_size;
105   DiceResult result = BccHandoverMainFlow(
106       /*context=*/NULL, bcc_handover, sizeof(bcc_handover), &input_values,
107       sizeof(next_bcc_handover), next_bcc_handover, &next_bcc_handover_size);
108   EXPECT_EQ(kDiceResultOk, result);
109   EXPECT_GT(next_bcc_handover_size, sizeof(bcc_handover));
110   EXPECT_EQ(0xa3, next_bcc_handover[0]);
111   EXPECT_EQ(0x83, next_bcc_handover[72]);
112   EXPECT_NE(0, memcmp(next_bcc_handover + 73, bcc_handover + 73,
113                       sizeof(bcc_handover) - 73));
114   EXPECT_EQ(0, memcmp(next_bcc_handover + 73, bcc_handover + 73,
115                       sizeof(bcc_handover) - 8 - 73));
116 }
117 
TEST(BccHandoverTest,InHandoverWithoutBccOutHandoverWithBcc)118 TEST(BccHandoverTest, InHandoverWithoutBccOutHandoverWithBcc) {
119   const uint8_t bcc_handover[] = {
120       0xa2,
121       // CDI attest
122       0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125       // CDI seal
126       0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129       // 8-bytes of trailing data that aren't part of the BCC.
130       0x00, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
131   DiceInputValues input_values = {};
132   uint8_t next_bcc_handover[1024] = {};
133   size_t next_bcc_handover_size;
134   DiceResult result = BccHandoverMainFlow(
135       /*context=*/NULL, bcc_handover, sizeof(bcc_handover), &input_values,
136       sizeof(next_bcc_handover), next_bcc_handover, &next_bcc_handover_size);
137   EXPECT_EQ(kDiceResultOk, result);
138   EXPECT_GT(next_bcc_handover_size, sizeof(bcc_handover));
139   EXPECT_EQ(0xa3, next_bcc_handover[0]);
140 }
141 
TEST(BccHandoverTest,InHandoverWithoutBccButUnknownFieldOutHandoverWithBcc)142 TEST(BccHandoverTest, InHandoverWithoutBccButUnknownFieldOutHandoverWithBcc) {
143   const uint8_t bcc_handover[] = {
144       0xa3,
145       // CDI attest
146       0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
149       // CDI seal
150       0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
151       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
152       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153       // Ignored unknown field
154       0x04, 0x01,
155       // 8-bytes of trailing data that aren't part of the BCC.
156       0x00, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
157   DiceInputValues input_values = {};
158   uint8_t next_bcc_handover[1024] = {};
159   size_t next_bcc_handover_size;
160   DiceResult result = BccHandoverMainFlow(
161       /*context=*/NULL, bcc_handover, sizeof(bcc_handover), &input_values,
162       sizeof(next_bcc_handover), next_bcc_handover, &next_bcc_handover_size);
163   EXPECT_EQ(kDiceResultOk, result);
164   EXPECT_GT(next_bcc_handover_size, sizeof(bcc_handover));
165   EXPECT_EQ(0xa3, next_bcc_handover[0]);
166 }
167 
TEST(BccHandoverTest,ParseHandover)168 TEST(BccHandoverTest, ParseHandover) {
169   const uint8_t bcc_handover[] = {
170       0xa3,
171       // CDI attest
172       0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
173       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
174       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
175       // CDI seal
176       0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
177       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
178       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179       // BCC
180       0x03, 0x82, 0xa6, 0x01, 0x02, 0x03, 0x27, 0x04, 0x02, 0x20, 0x01, 0x21,
181       0x40, 0x22, 0x40, 0x84, 0x40, 0xa0, 0x40, 0x40,
182       // 8-bytes of trailing data that aren't part of the BCC.
183       0x00, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
184   const uint8_t *cdi_attest;
185   const uint8_t *cdi_seal;
186   const uint8_t *bcc;
187   size_t bcc_size;
188   DiceResult result = BccHandoverParse(bcc_handover, sizeof(bcc_handover),
189                                        &cdi_attest, &cdi_seal, &bcc, &bcc_size);
190   EXPECT_EQ(kDiceResultOk, result);
191   EXPECT_EQ(bcc_handover + 4, cdi_attest);
192   EXPECT_EQ(bcc_handover + 39, cdi_seal);
193   EXPECT_EQ(bcc_handover + 72, bcc);
194   EXPECT_EQ(19u, bcc_size);
195 }
196 
TEST(BccHandoverTest,ParseHandoverWithoutBcc)197 TEST(BccHandoverTest, ParseHandoverWithoutBcc) {
198   const uint8_t bcc_handover[] = {
199       0xa2,
200       // CDI attest
201       0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
204       // CDI seal
205       0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
207       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208       // 8-bytes of trailing data that aren't part of the BCC.
209       0x00, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
210   const uint8_t *cdi_attest;
211   const uint8_t *cdi_seal;
212   const uint8_t *bcc;
213   size_t bcc_size;
214   DiceResult result = BccHandoverParse(bcc_handover, sizeof(bcc_handover),
215                                        &cdi_attest, &cdi_seal, &bcc, &bcc_size);
216   EXPECT_EQ(kDiceResultOk, result);
217   EXPECT_EQ(bcc_handover + 4, cdi_attest);
218   EXPECT_EQ(bcc_handover + 39, cdi_seal);
219   EXPECT_EQ(nullptr, bcc);
220   EXPECT_EQ(0u, bcc_size);
221 }
222 
TEST(BccHandoverTest,ParseHandoverWithoutBccButUnknownField)223 TEST(BccHandoverTest, ParseHandoverWithoutBccButUnknownField) {
224   const uint8_t bcc_handover[] = {
225       0xa3,
226       // CDI attest
227       0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
229       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
230       // CDI seal
231       0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
232       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
233       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
234       // Ignored unknown field
235       0x04, 0x01,
236       // 8-bytes of trailing data that aren't part of the BCC.
237       0x00, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
238   const uint8_t *cdi_attest;
239   const uint8_t *cdi_seal;
240   const uint8_t *bcc;
241   size_t bcc_size;
242   DiceResult result = BccHandoverParse(bcc_handover, sizeof(bcc_handover),
243                                        &cdi_attest, &cdi_seal, &bcc, &bcc_size);
244   EXPECT_EQ(kDiceResultOk, result);
245   EXPECT_EQ(bcc_handover + 4, cdi_attest);
246   EXPECT_EQ(bcc_handover + 39, cdi_seal);
247   EXPECT_EQ(nullptr, bcc);
248   EXPECT_EQ(0u, bcc_size);
249 }
250 
TEST(BccHandoverTest,ParseHandoverCdiTooLarge)251 TEST(BccHandoverTest, ParseHandoverCdiTooLarge) {
252   const uint8_t bcc_handover[] = {
253       0xa2,
254       // CDI attest
255       0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
256       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
257       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258       // CDI seal
259       0x02, 0x58, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
260       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
261       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
262       // 8-bytes of trailing data that aren't part of the BCC.
263       0x00, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40};
264   const uint8_t *cdi_attest;
265   const uint8_t *cdi_seal;
266   const uint8_t *bcc;
267   size_t bcc_size;
268   DiceResult result = BccHandoverParse(bcc_handover, sizeof(bcc_handover),
269                                        &cdi_attest, &cdi_seal, &bcc, &bcc_size);
270   EXPECT_EQ(kDiceResultInvalidInput, result);
271 }
272 }
273 
274 }  // namespace
275