1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "fs_avb_test_util.h"
18 
19 #include <stdlib.h>
20 
21 #include <android-base/file.h>
22 #include <base/files/file_util.h>
23 #include <base/strings/string_util.h>
24 
25 namespace fs_avb_host_test {
26 
TEST_F(BaseFsAvbTest,GenerateImage)27 TEST_F(BaseFsAvbTest, GenerateImage) {
28     const size_t image_size = 5 * 1024 * 1024;
29     base::FilePath boot_path = GenerateImage("boot.img", image_size);
30     EXPECT_NE(0U, boot_path.value().size());
31 
32     // Checks file size is as expected.
33     int64_t file_size;
34     ASSERT_TRUE(base::GetFileSize(boot_path, &file_size));
35     EXPECT_EQ(file_size, image_size);
36 
37     // Checks file content is as expected.
38     std::vector<uint8_t> expected_content;
39     expected_content.resize(image_size);
40     for (size_t n = 0; n < image_size; n++) {
41         expected_content[n] = uint8_t(n);
42     }
43     std::vector<uint8_t> actual_content;
44     actual_content.resize(image_size);
45     EXPECT_TRUE(
46             base::ReadFile(boot_path, reinterpret_cast<char*>(actual_content.data()), image_size));
47     EXPECT_EQ(expected_content, actual_content);
48 }
49 
TEST_F(BaseFsAvbTest,GenerateVBMetaImage)50 TEST_F(BaseFsAvbTest, GenerateVBMetaImage) {
51     GenerateVBMetaImage("vbmeta.img", "SHA256_RSA2048", 0, data_dir_.Append("testkey_rsa2048.pem"),
52                         {}, /* include_descriptor_image_paths */
53                         {}, /* chain_partitions */
54                         "--internal_release_string \"unit test\"");
55     EXPECT_EQ("5eba9ad4e775645e7eac441a563c200681ae868158d06f6a6cd36d06c07bd781",
56               CalcVBMetaDigest("vbmeta.img", "sha256"));
57     EXPECT_EQ(
58             "Minimum libavb version:   1.0\n"
59             "Header Block:             256 bytes\n"
60             "Authentication Block:     320 bytes\n"
61             "Auxiliary Block:          576 bytes\n"
62             "Public key (sha1):        cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
63             "Algorithm:                SHA256_RSA2048\n"
64             "Rollback Index:           0\n"
65             "Flags:                    0\n"
66             "Rollback Index Location:  0\n"
67             "Release String:           'unit test'\n"
68             "Descriptors:\n"
69             "    (none)\n",
70             InfoImage("vbmeta.img"));
71 }
72 
TEST_F(BaseFsAvbTest,AddHashFooter)73 TEST_F(BaseFsAvbTest, AddHashFooter) {
74     // Generates a raw boot.img
75     const size_t image_size = 5 * 1024 * 1024;
76     const size_t partition_size = 10 * 1024 * 1024;
77     base::FilePath boot_path = GenerateImage("boot.img", image_size);
78     EXPECT_NE(0U, boot_path.value().size());
79     // Checks file size is as expected.
80     int64_t file_size;
81     ASSERT_TRUE(base::GetFileSize(boot_path, &file_size));
82     EXPECT_EQ(file_size, image_size);
83     // Appends AVB Hash Footer.
84     AddAvbFooter(boot_path, "hash", "boot", partition_size, "SHA256_RSA4096", 10,
85                  data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
86                  "--internal_release_string \"unit test\"");
87     // Extracts boot vbmeta from boot.img into boot-vbmeta.img.
88     ExtractVBMetaImage(boot_path, "boot-vbmeta.img");
89     EXPECT_EQ(
90             "Minimum libavb version:   1.0\n"
91             "Header Block:             256 bytes\n"
92             "Authentication Block:     576 bytes\n"
93             "Auxiliary Block:          1216 bytes\n"
94             "Public key (sha1):        2597c218aae470a130f61162feaae70afd97f011\n"
95             "Algorithm:                SHA256_RSA4096\n"
96             "Rollback Index:           10\n"
97             "Flags:                    0\n"
98             "Rollback Index Location:  0\n"
99             "Release String:           'unit test'\n"
100             "Descriptors:\n"
101             "    Hash descriptor:\n"
102             "      Image Size:            5242880 bytes\n"
103             "      Hash Algorithm:        sha256\n"
104             "      Partition Name:        boot\n"
105             "      Salt:                  d00df00d\n"
106             "      Digest:                "
107             "222dd01e98284a1fcd7781f85d1392e43a530511a64eff96db197db90ebc4df1\n"
108             "      Flags:                 0\n",
109             InfoImage("boot-vbmeta.img"));
110 }
111 
TEST_F(BaseFsAvbTest,AddHashtreeFooter)112 TEST_F(BaseFsAvbTest, AddHashtreeFooter) {
113     // Generates a raw system.img
114     const size_t image_size = 50 * 1024 * 1024;
115     const size_t partition_size = 60 * 1024 * 1024;
116     base::FilePath system_path = GenerateImage("system.img", image_size);
117     EXPECT_NE(0U, system_path.value().size());
118     // Checks file size is as expected.
119     int64_t file_size;
120     ASSERT_TRUE(base::GetFileSize(system_path, &file_size));
121     EXPECT_EQ(file_size, image_size);
122     // Appends AVB Hashtree Footer.
123     AddAvbFooter(system_path, "hashtree", "system", partition_size, "SHA512_RSA8192", 20,
124                  data_dir_.Append("testkey_rsa8192.pem"), "d00df00d",
125                  "--internal_release_string \"unit test\"");
126     // Extracts system vbmeta from system.img into system-vbmeta.img.
127     ExtractVBMetaImage(system_path, "system-vbmeta.img");
128     EXPECT_EQ(
129             "Minimum libavb version:   1.0\n"
130             "Header Block:             256 bytes\n"
131             "Authentication Block:     1088 bytes\n"
132             "Auxiliary Block:          2304 bytes\n"
133             "Public key (sha1):        5227b569de003adc7f8ec3fc03e05dfbd969abad\n"
134             "Algorithm:                SHA512_RSA8192\n"
135             "Rollback Index:           20\n"
136             "Flags:                    0\n"
137             "Rollback Index Location:  0\n"
138             "Release String:           'unit test'\n"
139             "Descriptors:\n"
140             "    Hashtree descriptor:\n"
141             "      Version of dm-verity:  1\n"
142             "      Image Size:            52428800 bytes\n"
143             "      Tree Offset:           52428800\n"
144             "      Tree Size:             413696 bytes\n"
145             "      Data Block Size:       4096 bytes\n"
146             "      Hash Block Size:       4096 bytes\n"
147             "      FEC num roots:         2\n"
148             "      FEC offset:            52842496\n"
149             "      FEC size:              417792 bytes\n"
150             "      Hash Algorithm:        sha1\n"
151             "      Partition Name:        system\n"
152             "      Salt:                  d00df00d\n"
153             "      Root Digest:           d20d40c02298e385ab6d398a61a3b91dc9947d99\n"
154             "      Flags:                 0\n",
155             InfoImage("system-vbmeta.img"));
156 }
157 
TEST_F(BaseFsAvbTest,GenerateVBMetaImageWithDescriptors)158 TEST_F(BaseFsAvbTest, GenerateVBMetaImageWithDescriptors) {
159     // Generates a raw boot.img
160     const size_t boot_image_size = 5 * 1024 * 1024;
161     const size_t boot_partition_size = 10 * 1024 * 1024;
162     base::FilePath boot_path = GenerateImage("boot.img", boot_image_size);
163     // Adds AVB Hash Footer.
164     AddAvbFooter(boot_path, "hash", "boot", boot_partition_size, "SHA256_RSA4096", 10,
165                  data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
166                  "--internal_release_string \"unit test\"");
167 
168     // Generates a raw system.img, use a smaller size to speed-up unit test.
169     const size_t system_image_size = 10 * 1024 * 1024;
170     const size_t system_partition_size = 15 * 1024 * 1024;
171     base::FilePath system_path = GenerateImage("system.img", system_image_size);
172     // Adds AVB Hashtree Footer.
173     AddAvbFooter(system_path, "hashtree", "system", system_partition_size, "SHA512_RSA8192", 20,
174                  data_dir_.Append("testkey_rsa8192.pem"), "d00df00d",
175                  "--internal_release_string \"unit test\"");
176 
177     // Makes a vbmeta.img including both 'boot' and 'system' descriptors.
178     GenerateVBMetaImage("vbmeta.img", "SHA256_RSA2048", 0, data_dir_.Append("testkey_rsa2048.pem"),
179                         {boot_path, system_path}, /* include_descriptor_image_paths */
180                         {},                       /* chain_partitions */
181                         "--internal_release_string \"unit test\"");
182     EXPECT_EQ("a069cbfc30c816cddf3b53f1ad53b7ca5d61a3d93845eb596bbb1b40caa1c62f",
183               CalcVBMetaDigest("vbmeta.img", "sha256"));
184     EXPECT_EQ(
185             "Minimum libavb version:   1.0\n"
186             "Header Block:             256 bytes\n"
187             "Authentication Block:     320 bytes\n"
188             "Auxiliary Block:          960 bytes\n"
189             "Public key (sha1):        cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
190             "Algorithm:                SHA256_RSA2048\n"
191             "Rollback Index:           0\n"
192             "Flags:                    0\n"
193             "Rollback Index Location:  0\n"
194             "Release String:           'unit test'\n"
195             "Descriptors:\n"
196             "    Hash descriptor:\n"
197             "      Image Size:            5242880 bytes\n"
198             "      Hash Algorithm:        sha256\n"
199             "      Partition Name:        boot\n"
200             "      Salt:                  d00df00d\n"
201             "      Digest:                "
202             "222dd01e98284a1fcd7781f85d1392e43a530511a64eff96db197db90ebc4df1\n"
203             "      Flags:                 0\n"
204             "    Hashtree descriptor:\n"
205             "      Version of dm-verity:  1\n"
206             "      Image Size:            10485760 bytes\n"
207             "      Tree Offset:           10485760\n"
208             "      Tree Size:             86016 bytes\n"
209             "      Data Block Size:       4096 bytes\n"
210             "      Hash Block Size:       4096 bytes\n"
211             "      FEC num roots:         2\n"
212             "      FEC offset:            10571776\n"
213             "      FEC size:              90112 bytes\n"
214             "      Hash Algorithm:        sha1\n"
215             "      Partition Name:        system\n"
216             "      Salt:                  d00df00d\n"
217             "      Root Digest:           a3d5dd307341393d85de356c384ff543ec1ed81b\n"
218             "      Flags:                 0\n",
219             InfoImage("vbmeta.img"));
220 }
221 
TEST_F(BaseFsAvbTest,GenerateVBMetaImageWithChainDescriptors)222 TEST_F(BaseFsAvbTest, GenerateVBMetaImageWithChainDescriptors) {
223     // Generates a raw boot.img
224     const size_t boot_image_size = 5 * 1024 * 1024;
225     const size_t boot_partition_size = 10 * 1024 * 1024;
226     base::FilePath boot_path = GenerateImage("boot.img", boot_image_size);
227     // Adds AVB Hash Footer.
228     AddAvbFooter(boot_path, "hash", "boot", boot_partition_size, "SHA256_RSA2048", 10,
229                  data_dir_.Append("testkey_rsa2048.pem"), "d00df00d",
230                  "--internal_release_string \"unit test\"");
231 
232     // Generates a raw system.img, use a smaller size to speed-up unit test.
233     const size_t system_image_size = 10 * 1024 * 1024;
234     const size_t system_partition_size = 15 * 1024 * 1024;
235     base::FilePath system_path = GenerateImage("system.img", system_image_size);
236     // Adds AVB Hashtree Footer.
237     AddAvbFooter(system_path, "hashtree", "system", system_partition_size, "SHA512_RSA4096", 20,
238                  data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
239                  "--internal_release_string \"unit test\"");
240 
241     // Make a vbmeta image with chain partitions.
242     base::FilePath rsa2048_public_key =
243             ExtractPublicKeyAvb(data_dir_.Append("testkey_rsa2048.pem"));
244     base::FilePath rsa4096_public_key =
245             ExtractPublicKeyAvb(data_dir_.Append("testkey_rsa4096.pem"));
246     GenerateVBMetaImage("vbmeta.img", "SHA256_RSA8192", 0, data_dir_.Append("testkey_rsa8192.pem"),
247                         {},                               /* include_descriptor_image_paths */
248                         {{"boot", 1, rsa2048_public_key}, /* chain_partitions */
249                          {"system", 2, rsa4096_public_key}},
250                         "--internal_release_string \"unit test\"");
251 
252     // vbmeta digest calculation includes the chained vbmeta from boot.img and system.img.
253     EXPECT_EQ("abbe11b316901f3336e26630f64c4732dadbe14532186ac8640e4141a403721f",
254               CalcVBMetaDigest("vbmeta.img", "sha256"));
255     EXPECT_EQ(
256             "Minimum libavb version:   1.0\n"
257             "Header Block:             256 bytes\n"
258             "Authentication Block:     1088 bytes\n"
259             "Auxiliary Block:          3840 bytes\n"
260             "Public key (sha1):        5227b569de003adc7f8ec3fc03e05dfbd969abad\n"
261             "Algorithm:                SHA256_RSA8192\n"
262             "Rollback Index:           0\n"
263             "Flags:                    0\n"
264             "Rollback Index Location:  0\n"
265             "Release String:           'unit test'\n"
266             "Descriptors:\n"
267             "    Chain Partition descriptor:\n"
268             "      Partition Name:          boot\n"
269             "      Rollback Index Location: 1\n"
270             "      Public key (sha1):       cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
271             "      Flags:                   0\n"
272             "    Chain Partition descriptor:\n"
273             "      Partition Name:          system\n"
274             "      Rollback Index Location: 2\n"
275             "      Public key (sha1):       2597c218aae470a130f61162feaae70afd97f011\n"
276             "      Flags:                   0\n",
277             InfoImage("vbmeta.img"));
278 }
279 
280 }  // namespace fs_avb_host_test
281 
main(int argc,char ** argv)282 int main(int argc, char** argv) {
283     ::testing::InitGoogleTest(&argc, argv);
284     return RUN_ALL_TESTS();
285 }
286