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(BccHandoverNoCertTest,InHandoverWithoutBccOutHandoverWithBcc)118 TEST(BccHandoverNoCertTest, 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 };
130 DiceInputValues input_values = {};
131 uint8_t next_bcc_handover[1024] = {};
132 size_t next_bcc_handover_size;
133 DiceResult result = BccHandoverMainFlow(
134 /*context=*/NULL, bcc_handover, sizeof(bcc_handover), &input_values,
135 sizeof(next_bcc_handover), next_bcc_handover, &next_bcc_handover_size);
136 EXPECT_EQ(kDiceResultOk, result);
137 EXPECT_GT(next_bcc_handover_size, sizeof(bcc_handover));
138 EXPECT_EQ(0xa3, next_bcc_handover[0]);
139 }
140 }
141
142 } // namespace
143