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 "Algorithm: SHA256_RSA2048\n"
63 "Rollback Index: 0\n"
64 "Flags: 0\n"
65 "Release String: 'unit test'\n"
66 "Descriptors:\n"
67 " (none)\n",
68 InfoImage("vbmeta.img"));
69 }
70
TEST_F(BaseFsAvbTest,AddHashFooter)71 TEST_F(BaseFsAvbTest, AddHashFooter) {
72 // Generates a raw boot.img
73 const size_t image_size = 5 * 1024 * 1024;
74 const size_t partition_size = 10 * 1024 * 1024;
75 base::FilePath boot_path = GenerateImage("boot.img", image_size);
76 EXPECT_NE(0U, boot_path.value().size());
77 // Checks file size is as expected.
78 int64_t file_size;
79 ASSERT_TRUE(base::GetFileSize(boot_path, &file_size));
80 EXPECT_EQ(file_size, image_size);
81 // Appends AVB Hash Footer.
82 AddAvbFooter(boot_path, "hash", "boot", partition_size, "SHA256_RSA4096", 10,
83 data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
84 "--internal_release_string \"unit test\"");
85 // Extracts boot vbmeta from boot.img into boot-vbmeta.img.
86 ExtractVBMetaImage(boot_path, "boot-vbmeta.img");
87 EXPECT_EQ(
88 "Minimum libavb version: 1.0\n"
89 "Header Block: 256 bytes\n"
90 "Authentication Block: 576 bytes\n"
91 "Auxiliary Block: 1216 bytes\n"
92 "Algorithm: SHA256_RSA4096\n"
93 "Rollback Index: 10\n"
94 "Flags: 0\n"
95 "Release String: 'unit test'\n"
96 "Descriptors:\n"
97 " Hash descriptor:\n"
98 " Image Size: 5242880 bytes\n"
99 " Hash Algorithm: sha256\n"
100 " Partition Name: boot\n"
101 " Salt: d00df00d\n"
102 " Digest: "
103 "222dd01e98284a1fcd7781f85d1392e43a530511a64eff96db197db90ebc4df1\n"
104 " Flags: 0\n",
105 InfoImage("boot-vbmeta.img"));
106 }
107
TEST_F(BaseFsAvbTest,AddHashtreeFooter)108 TEST_F(BaseFsAvbTest, AddHashtreeFooter) {
109 // Generates a raw system.img
110 const size_t image_size = 50 * 1024 * 1024;
111 const size_t partition_size = 60 * 1024 * 1024;
112 base::FilePath system_path = GenerateImage("system.img", image_size);
113 EXPECT_NE(0U, system_path.value().size());
114 // Checks file size is as expected.
115 int64_t file_size;
116 ASSERT_TRUE(base::GetFileSize(system_path, &file_size));
117 EXPECT_EQ(file_size, image_size);
118 // Appends AVB Hashtree Footer.
119 AddAvbFooter(system_path, "hashtree", "system", partition_size, "SHA512_RSA8192", 20,
120 data_dir_.Append("testkey_rsa8192.pem"), "d00df00d",
121 "--internal_release_string \"unit test\"");
122 // Extracts system vbmeta from system.img into system-vbmeta.img.
123 ExtractVBMetaImage(system_path, "system-vbmeta.img");
124 EXPECT_EQ(
125 "Minimum libavb version: 1.0\n"
126 "Header Block: 256 bytes\n"
127 "Authentication Block: 1088 bytes\n"
128 "Auxiliary Block: 2304 bytes\n"
129 "Algorithm: SHA512_RSA8192\n"
130 "Rollback Index: 20\n"
131 "Flags: 0\n"
132 "Release String: 'unit test'\n"
133 "Descriptors:\n"
134 " Hashtree descriptor:\n"
135 " Version of dm-verity: 1\n"
136 " Image Size: 52428800 bytes\n"
137 " Tree Offset: 52428800\n"
138 " Tree Size: 413696 bytes\n"
139 " Data Block Size: 4096 bytes\n"
140 " Hash Block Size: 4096 bytes\n"
141 " FEC num roots: 2\n"
142 " FEC offset: 52842496\n"
143 " FEC size: 417792 bytes\n"
144 " Hash Algorithm: sha1\n"
145 " Partition Name: system\n"
146 " Salt: d00df00d\n"
147 " Root Digest: d20d40c02298e385ab6d398a61a3b91dc9947d99\n"
148 " Flags: 0\n",
149 InfoImage("system-vbmeta.img"));
150 }
151
TEST_F(BaseFsAvbTest,GenerateVBMetaImageWithDescriptors)152 TEST_F(BaseFsAvbTest, GenerateVBMetaImageWithDescriptors) {
153 // Generates a raw boot.img
154 const size_t boot_image_size = 5 * 1024 * 1024;
155 const size_t boot_partition_size = 10 * 1024 * 1024;
156 base::FilePath boot_path = GenerateImage("boot.img", boot_image_size);
157 // Adds AVB Hash Footer.
158 AddAvbFooter(boot_path, "hash", "boot", boot_partition_size, "SHA256_RSA4096", 10,
159 data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
160 "--internal_release_string \"unit test\"");
161
162 // Generates a raw system.img, use a smaller size to speed-up unit test.
163 const size_t system_image_size = 10 * 1024 * 1024;
164 const size_t system_partition_size = 15 * 1024 * 1024;
165 base::FilePath system_path = GenerateImage("system.img", system_image_size);
166 // Adds AVB Hashtree Footer.
167 AddAvbFooter(system_path, "hashtree", "system", system_partition_size, "SHA512_RSA8192", 20,
168 data_dir_.Append("testkey_rsa8192.pem"), "d00df00d",
169 "--internal_release_string \"unit test\"");
170
171 // Makes a vbmeta.img including both 'boot' and 'system' descriptors.
172 GenerateVBMetaImage("vbmeta.img", "SHA256_RSA2048", 0, data_dir_.Append("testkey_rsa2048.pem"),
173 {boot_path, system_path}, /* include_descriptor_image_paths */
174 {}, /* chain_partitions */
175 "--internal_release_string \"unit test\"");
176 EXPECT_EQ("a069cbfc30c816cddf3b53f1ad53b7ca5d61a3d93845eb596bbb1b40caa1c62f",
177 CalcVBMetaDigest("vbmeta.img", "sha256"));
178 EXPECT_EQ(
179 "Minimum libavb version: 1.0\n"
180 "Header Block: 256 bytes\n"
181 "Authentication Block: 320 bytes\n"
182 "Auxiliary Block: 960 bytes\n"
183 "Algorithm: SHA256_RSA2048\n"
184 "Rollback Index: 0\n"
185 "Flags: 0\n"
186 "Release String: 'unit test'\n"
187 "Descriptors:\n"
188 " Hash descriptor:\n"
189 " Image Size: 5242880 bytes\n"
190 " Hash Algorithm: sha256\n"
191 " Partition Name: boot\n"
192 " Salt: d00df00d\n"
193 " Digest: "
194 "222dd01e98284a1fcd7781f85d1392e43a530511a64eff96db197db90ebc4df1\n"
195 " Flags: 0\n"
196 " Hashtree descriptor:\n"
197 " Version of dm-verity: 1\n"
198 " Image Size: 10485760 bytes\n"
199 " Tree Offset: 10485760\n"
200 " Tree Size: 86016 bytes\n"
201 " Data Block Size: 4096 bytes\n"
202 " Hash Block Size: 4096 bytes\n"
203 " FEC num roots: 2\n"
204 " FEC offset: 10571776\n"
205 " FEC size: 90112 bytes\n"
206 " Hash Algorithm: sha1\n"
207 " Partition Name: system\n"
208 " Salt: d00df00d\n"
209 " Root Digest: a3d5dd307341393d85de356c384ff543ec1ed81b\n"
210 " Flags: 0\n",
211 InfoImage("vbmeta.img"));
212 }
213
TEST_F(BaseFsAvbTest,GenerateVBMetaImageWithChainDescriptors)214 TEST_F(BaseFsAvbTest, GenerateVBMetaImageWithChainDescriptors) {
215 // Generates a raw boot.img
216 const size_t boot_image_size = 5 * 1024 * 1024;
217 const size_t boot_partition_size = 10 * 1024 * 1024;
218 base::FilePath boot_path = GenerateImage("boot.img", boot_image_size);
219 // Adds AVB Hash Footer.
220 AddAvbFooter(boot_path, "hash", "boot", boot_partition_size, "SHA256_RSA2048", 10,
221 data_dir_.Append("testkey_rsa2048.pem"), "d00df00d",
222 "--internal_release_string \"unit test\"");
223
224 // Generates a raw system.img, use a smaller size to speed-up unit test.
225 const size_t system_image_size = 10 * 1024 * 1024;
226 const size_t system_partition_size = 15 * 1024 * 1024;
227 base::FilePath system_path = GenerateImage("system.img", system_image_size);
228 // Adds AVB Hashtree Footer.
229 AddAvbFooter(system_path, "hashtree", "system", system_partition_size, "SHA512_RSA4096", 20,
230 data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
231 "--internal_release_string \"unit test\"");
232
233 // Make a vbmeta image with chain partitions.
234 base::FilePath rsa2048_public_key =
235 ExtractPublicKeyAvb(data_dir_.Append("testkey_rsa2048.pem"));
236 base::FilePath rsa4096_public_key =
237 ExtractPublicKeyAvb(data_dir_.Append("testkey_rsa4096.pem"));
238 GenerateVBMetaImage("vbmeta.img", "SHA256_RSA8192", 0, data_dir_.Append("testkey_rsa8192.pem"),
239 {}, /* include_descriptor_image_paths */
240 {{"boot", 1, rsa2048_public_key}, /* chain_partitions */
241 {"system", 2, rsa4096_public_key}},
242 "--internal_release_string \"unit test\"");
243
244 // vbmeta digest calculation includes the chained vbmeta from boot.img and system.img.
245 EXPECT_EQ("abbe11b316901f3336e26630f64c4732dadbe14532186ac8640e4141a403721f",
246 CalcVBMetaDigest("vbmeta.img", "sha256"));
247 EXPECT_EQ(
248 "Minimum libavb version: 1.0\n"
249 "Header Block: 256 bytes\n"
250 "Authentication Block: 1088 bytes\n"
251 "Auxiliary Block: 3840 bytes\n"
252 "Algorithm: SHA256_RSA8192\n"
253 "Rollback Index: 0\n"
254 "Flags: 0\n"
255 "Release String: 'unit test'\n"
256 "Descriptors:\n"
257 " Chain Partition descriptor:\n"
258 " Partition Name: boot\n"
259 " Rollback Index Location: 1\n"
260 " Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
261 " Chain Partition descriptor:\n"
262 " Partition Name: system\n"
263 " Rollback Index Location: 2\n"
264 " Public key (sha1): 2597c218aae470a130f61162feaae70afd97f011\n",
265 InfoImage("vbmeta.img"));
266 }
267
268 } // namespace fs_avb_host_test
269
main(int argc,char ** argv)270 int main(int argc, char** argv) {
271 ::testing::InitGoogleTest(&argc, argv);
272 return RUN_ALL_TESTS();
273 }
274