1*d289c2baSAndroid Build Coastguard Worker /*
2*d289c2baSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*d289c2baSAndroid Build Coastguard Worker *
4*d289c2baSAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person
5*d289c2baSAndroid Build Coastguard Worker * obtaining a copy of this software and associated documentation
6*d289c2baSAndroid Build Coastguard Worker * files (the "Software"), to deal in the Software without
7*d289c2baSAndroid Build Coastguard Worker * restriction, including without limitation the rights to use, copy,
8*d289c2baSAndroid Build Coastguard Worker * modify, merge, publish, distribute, sublicense, and/or sell copies
9*d289c2baSAndroid Build Coastguard Worker * of the Software, and to permit persons to whom the Software is
10*d289c2baSAndroid Build Coastguard Worker * furnished to do so, subject to the following conditions:
11*d289c2baSAndroid Build Coastguard Worker *
12*d289c2baSAndroid Build Coastguard Worker * The above copyright notice and this permission notice shall be
13*d289c2baSAndroid Build Coastguard Worker * included in all copies or substantial portions of the Software.
14*d289c2baSAndroid Build Coastguard Worker *
15*d289c2baSAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*d289c2baSAndroid Build Coastguard Worker * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*d289c2baSAndroid Build Coastguard Worker * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*d289c2baSAndroid Build Coastguard Worker * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*d289c2baSAndroid Build Coastguard Worker * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*d289c2baSAndroid Build Coastguard Worker * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*d289c2baSAndroid Build Coastguard Worker * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*d289c2baSAndroid Build Coastguard Worker * SOFTWARE.
23*d289c2baSAndroid Build Coastguard Worker */
24*d289c2baSAndroid Build Coastguard Worker
25*d289c2baSAndroid Build Coastguard Worker #include <android-base/file.h>
26*d289c2baSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
27*d289c2baSAndroid Build Coastguard Worker #include <base/files/file_util.h>
28*d289c2baSAndroid Build Coastguard Worker #include <base/strings/string_split.h>
29*d289c2baSAndroid Build Coastguard Worker #include <base/strings/string_util.h>
30*d289c2baSAndroid Build Coastguard Worker #include <endian.h>
31*d289c2baSAndroid Build Coastguard Worker #include <inttypes.h>
32*d289c2baSAndroid Build Coastguard Worker #include <libavb/avb_sha.h>
33*d289c2baSAndroid Build Coastguard Worker #include <libavb/libavb.h>
34*d289c2baSAndroid Build Coastguard Worker #include <string.h>
35*d289c2baSAndroid Build Coastguard Worker
36*d289c2baSAndroid Build Coastguard Worker #include <iostream>
37*d289c2baSAndroid Build Coastguard Worker
38*d289c2baSAndroid Build Coastguard Worker #include "avb_unittest_util.h"
39*d289c2baSAndroid Build Coastguard Worker #include "fake_avb_ops.h"
40*d289c2baSAndroid Build Coastguard Worker
41*d289c2baSAndroid Build Coastguard Worker namespace avb {
42*d289c2baSAndroid Build Coastguard Worker
43*d289c2baSAndroid Build Coastguard Worker class AvbToolTest : public BaseAvbToolTest {
44*d289c2baSAndroid Build Coastguard Worker public:
AvbToolTest()45*d289c2baSAndroid Build Coastguard Worker AvbToolTest() {}
46*d289c2baSAndroid Build Coastguard Worker
SetUp()47*d289c2baSAndroid Build Coastguard Worker virtual void SetUp() override {
48*d289c2baSAndroid Build Coastguard Worker BaseAvbToolTest::SetUp();
49*d289c2baSAndroid Build Coastguard Worker ops_.set_partition_dir(testdir_);
50*d289c2baSAndroid Build Coastguard Worker ops_.set_stored_rollback_indexes({{0, 0}, {1, 0}, {2, 0}, {3, 0}});
51*d289c2baSAndroid Build Coastguard Worker ops_.set_stored_is_device_unlocked(false);
52*d289c2baSAndroid Build Coastguard Worker }
53*d289c2baSAndroid Build Coastguard Worker
54*d289c2baSAndroid Build Coastguard Worker void AddHashFooterTest(bool sparse_image);
55*d289c2baSAndroid Build Coastguard Worker void CreateRootfsWithHashtreeFooter(bool sparse_image,
56*d289c2baSAndroid Build Coastguard Worker const std::string& hash_algorithm,
57*d289c2baSAndroid Build Coastguard Worker const std::string& root_digest,
58*d289c2baSAndroid Build Coastguard Worker std::filesystem::path* rootfs_path);
59*d289c2baSAndroid Build Coastguard Worker void AddHashtreeFooterTest(bool sparse_image);
60*d289c2baSAndroid Build Coastguard Worker void AddHashtreeFooterFECTest(bool sparse_image);
61*d289c2baSAndroid Build Coastguard Worker
62*d289c2baSAndroid Build Coastguard Worker void GenerateImageWithHashAndHashtreeSetup();
63*d289c2baSAndroid Build Coastguard Worker
64*d289c2baSAndroid Build Coastguard Worker FakeAvbOps ops_;
65*d289c2baSAndroid Build Coastguard Worker };
66*d289c2baSAndroid Build Coastguard Worker
67*d289c2baSAndroid Build Coastguard Worker // This test ensure that the version is increased in both
68*d289c2baSAndroid Build Coastguard Worker // avb_boot_image.h and the avb tool.
TEST_F(AvbToolTest,AvbVersionInSync)69*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AvbVersionInSync) {
70*d289c2baSAndroid Build Coastguard Worker std::filesystem::path path = testdir_ / "version.txt";
71*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "./avbtool.py version > %s", path.c_str());
72*d289c2baSAndroid Build Coastguard Worker std::string printed_version;
73*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(path.string(), &printed_version));
74*d289c2baSAndroid Build Coastguard Worker base::TrimWhitespaceASCII(printed_version, base::TRIM_ALL, &printed_version);
75*d289c2baSAndroid Build Coastguard Worker // See comments in libavb/avb_version.c and avbtool's get_release_string()
76*d289c2baSAndroid Build Coastguard Worker // about being in sync.
77*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(printed_version,
78*d289c2baSAndroid Build Coastguard Worker std::string("avbtool ") + std::string(avb_version_string()));
79*d289c2baSAndroid Build Coastguard Worker }
80*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,DefaultReleaseString)81*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, DefaultReleaseString) {
82*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
83*d289c2baSAndroid Build Coastguard Worker "vbmeta.img", "SHA256_RSA2048", 0, "test/data/testkey_rsa2048.pem");
84*d289c2baSAndroid Build Coastguard Worker
85*d289c2baSAndroid Build Coastguard Worker // Default release string is "avbtool " + avb_version_string().
86*d289c2baSAndroid Build Coastguard Worker AvbVBMetaImageHeader h;
87*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_header_to_host_byte_order(
88*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<AvbVBMetaImageHeader*>(vbmeta_image_.data()), &h);
89*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(std::string("avbtool ") + std::string(avb_version_string()),
90*d289c2baSAndroid Build Coastguard Worker std::string((const char*)h.release_string));
91*d289c2baSAndroid Build Coastguard Worker }
92*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,ReleaseStringAppend)93*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, ReleaseStringAppend) {
94*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage("vbmeta.img",
95*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
96*d289c2baSAndroid Build Coastguard Worker 0,
97*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
98*d289c2baSAndroid Build Coastguard Worker "--append_to_release_string \"Woot XYZ\"");
99*d289c2baSAndroid Build Coastguard Worker
100*d289c2baSAndroid Build Coastguard Worker // Note that avbtool inserts the space by itself.
101*d289c2baSAndroid Build Coastguard Worker std::string expected_str =
102*d289c2baSAndroid Build Coastguard Worker std::string("avbtool ") + std::string(avb_version_string()) + " Woot XYZ";
103*d289c2baSAndroid Build Coastguard Worker
104*d289c2baSAndroid Build Coastguard Worker AvbVBMetaImageHeader h;
105*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_header_to_host_byte_order(
106*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<AvbVBMetaImageHeader*>(vbmeta_image_.data()), &h);
107*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(expected_str, std::string((const char*)h.release_string));
108*d289c2baSAndroid Build Coastguard Worker }
109*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,ReleaseStringAppendTruncated)110*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, ReleaseStringAppendTruncated) {
111*d289c2baSAndroid Build Coastguard Worker // Append enough text that truncation is sure to happen.
112*d289c2baSAndroid Build Coastguard Worker std::string append_str = "0123456789abcdef0123456789abcdef0123456789abcdef";
113*d289c2baSAndroid Build Coastguard Worker std::string expected_str = std::string("avbtool ") +
114*d289c2baSAndroid Build Coastguard Worker std::string(avb_version_string()) + " " +
115*d289c2baSAndroid Build Coastguard Worker append_str;
116*d289c2baSAndroid Build Coastguard Worker EXPECT_GT(expected_str.size(), (size_t)(AVB_RELEASE_STRING_SIZE - 1));
117*d289c2baSAndroid Build Coastguard Worker expected_str.resize(AVB_RELEASE_STRING_SIZE - 1);
118*d289c2baSAndroid Build Coastguard Worker
119*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
120*d289c2baSAndroid Build Coastguard Worker "vbmeta.img",
121*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
122*d289c2baSAndroid Build Coastguard Worker 0,
123*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
124*d289c2baSAndroid Build Coastguard Worker std::string("--append_to_release_string \"") + append_str + "\"");
125*d289c2baSAndroid Build Coastguard Worker
126*d289c2baSAndroid Build Coastguard Worker // This checks that it ends with a NUL byte.
127*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_VBMETA_VERIFY_RESULT_OK,
128*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_verify(
129*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.data(), vbmeta_image_.size(), nullptr, nullptr));
130*d289c2baSAndroid Build Coastguard Worker
131*d289c2baSAndroid Build Coastguard Worker // For good measure we also check here.
132*d289c2baSAndroid Build Coastguard Worker AvbVBMetaImageHeader h;
133*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_header_to_host_byte_order(
134*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<AvbVBMetaImageHeader*>(vbmeta_image_.data()), &h);
135*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(expected_str, std::string((const char*)h.release_string));
136*d289c2baSAndroid Build Coastguard Worker }
137*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,ExtractPublicKey)138*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, ExtractPublicKey) {
139*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage("vbmeta.img",
140*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
141*d289c2baSAndroid Build Coastguard Worker 0,
142*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
143*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"");
144*d289c2baSAndroid Build Coastguard Worker
145*d289c2baSAndroid Build Coastguard Worker std::string key_data = PublicKeyAVB("test/data/testkey_rsa2048.pem");
146*d289c2baSAndroid Build Coastguard Worker
147*d289c2baSAndroid Build Coastguard Worker AvbVBMetaImageHeader h;
148*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_header_to_host_byte_order(
149*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<AvbVBMetaImageHeader*>(vbmeta_image_.data()), &h);
150*d289c2baSAndroid Build Coastguard Worker uint8_t* d = reinterpret_cast<uint8_t*>(vbmeta_image_.data());
151*d289c2baSAndroid Build Coastguard Worker size_t auxiliary_data_block_offset =
152*d289c2baSAndroid Build Coastguard Worker sizeof(AvbVBMetaImageHeader) + h.authentication_data_block_size;
153*d289c2baSAndroid Build Coastguard Worker EXPECT_GT(h.auxiliary_data_block_size, key_data.size());
154*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0,
155*d289c2baSAndroid Build Coastguard Worker memcmp(key_data.data(),
156*d289c2baSAndroid Build Coastguard Worker d + auxiliary_data_block_offset + h.public_key_offset,
157*d289c2baSAndroid Build Coastguard Worker key_data.size()));
158*d289c2baSAndroid Build Coastguard Worker }
159*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,CheckDescriptors)160*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, CheckDescriptors) {
161*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage("vbmeta.img",
162*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
163*d289c2baSAndroid Build Coastguard Worker 0,
164*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
165*d289c2baSAndroid Build Coastguard Worker "--prop foo:brillo "
166*d289c2baSAndroid Build Coastguard Worker "--prop bar:chromeos "
167*d289c2baSAndroid Build Coastguard Worker "--prop prisoner:24601 "
168*d289c2baSAndroid Build Coastguard Worker "--prop hexnumber:0xcafe "
169*d289c2baSAndroid Build Coastguard Worker "--prop hexnumber_capital:0xCAFE "
170*d289c2baSAndroid Build Coastguard Worker "--prop large_hexnumber:0xfedcba9876543210 "
171*d289c2baSAndroid Build Coastguard Worker "--prop larger_than_uint64:0xfedcba98765432101 "
172*d289c2baSAndroid Build Coastguard Worker "--prop almost_a_number:423x "
173*d289c2baSAndroid Build Coastguard Worker "--prop_from_file blob:test/data/small_blob.bin "
174*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"");
175*d289c2baSAndroid Build Coastguard Worker
176*d289c2baSAndroid Build Coastguard Worker AvbVBMetaImageHeader h;
177*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_header_to_host_byte_order(
178*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<AvbVBMetaImageHeader*>(vbmeta_image_.data()), &h);
179*d289c2baSAndroid Build Coastguard Worker
180*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_VBMETA_VERIFY_RESULT_OK,
181*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_verify(
182*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.data(), vbmeta_image_.size(), nullptr, nullptr));
183*d289c2baSAndroid Build Coastguard Worker
184*d289c2baSAndroid Build Coastguard Worker const char* s;
185*d289c2baSAndroid Build Coastguard Worker size_t len;
186*d289c2baSAndroid Build Coastguard Worker uint64_t val;
187*d289c2baSAndroid Build Coastguard Worker
188*d289c2baSAndroid Build Coastguard Worker // Basic.
189*d289c2baSAndroid Build Coastguard Worker s = avb_property_lookup(
190*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.data(), vbmeta_image_.size(), "foo", 0, &len);
191*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, strcmp(s, "brillo"));
192*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(6U, len);
193*d289c2baSAndroid Build Coastguard Worker s = avb_property_lookup(
194*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.data(), vbmeta_image_.size(), "bar", 0, &len);
195*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, strcmp(s, "chromeos"));
196*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(8U, len);
197*d289c2baSAndroid Build Coastguard Worker s = avb_property_lookup(
198*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.data(), vbmeta_image_.size(), "non-existant", 0, &len);
199*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0U, len);
200*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(NULL, s);
201*d289c2baSAndroid Build Coastguard Worker
202*d289c2baSAndroid Build Coastguard Worker // Numbers.
203*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(
204*d289c2baSAndroid Build Coastguard Worker 0,
205*d289c2baSAndroid Build Coastguard Worker avb_property_lookup_uint64(
206*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.data(), vbmeta_image_.size(), "prisoner", 0, &val));
207*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(24601U, val);
208*d289c2baSAndroid Build Coastguard Worker
209*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(
210*d289c2baSAndroid Build Coastguard Worker 0,
211*d289c2baSAndroid Build Coastguard Worker avb_property_lookup_uint64(
212*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.data(), vbmeta_image_.size(), "hexnumber", 0, &val));
213*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0xcafeU, val);
214*d289c2baSAndroid Build Coastguard Worker
215*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(0,
216*d289c2baSAndroid Build Coastguard Worker avb_property_lookup_uint64(vbmeta_image_.data(),
217*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.size(),
218*d289c2baSAndroid Build Coastguard Worker "hexnumber_capital",
219*d289c2baSAndroid Build Coastguard Worker 0,
220*d289c2baSAndroid Build Coastguard Worker &val));
221*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0xcafeU, val);
222*d289c2baSAndroid Build Coastguard Worker
223*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(0,
224*d289c2baSAndroid Build Coastguard Worker avb_property_lookup_uint64(vbmeta_image_.data(),
225*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.size(),
226*d289c2baSAndroid Build Coastguard Worker "large_hexnumber",
227*d289c2baSAndroid Build Coastguard Worker 0,
228*d289c2baSAndroid Build Coastguard Worker &val));
229*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0xfedcba9876543210U, val);
230*d289c2baSAndroid Build Coastguard Worker
231*d289c2baSAndroid Build Coastguard Worker // We could catch overflows and return an error ... but we currently don't.
232*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(0,
233*d289c2baSAndroid Build Coastguard Worker avb_property_lookup_uint64(vbmeta_image_.data(),
234*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.size(),
235*d289c2baSAndroid Build Coastguard Worker "larger_than_uint64",
236*d289c2baSAndroid Build Coastguard Worker 0,
237*d289c2baSAndroid Build Coastguard Worker &val));
238*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0xedcba98765432101U, val);
239*d289c2baSAndroid Build Coastguard Worker
240*d289c2baSAndroid Build Coastguard Worker // Number-parsing failures.
241*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0,
242*d289c2baSAndroid Build Coastguard Worker avb_property_lookup_uint64(
243*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.data(), vbmeta_image_.size(), "foo", 0, &val));
244*d289c2baSAndroid Build Coastguard Worker
245*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0,
246*d289c2baSAndroid Build Coastguard Worker avb_property_lookup_uint64(vbmeta_image_.data(),
247*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.size(),
248*d289c2baSAndroid Build Coastguard Worker "almost_a_number",
249*d289c2baSAndroid Build Coastguard Worker 0,
250*d289c2baSAndroid Build Coastguard Worker &val));
251*d289c2baSAndroid Build Coastguard Worker
252*d289c2baSAndroid Build Coastguard Worker // Blobs.
253*d289c2baSAndroid Build Coastguard Worker //
254*d289c2baSAndroid Build Coastguard Worker // test/data/small_blob.bin is 21 byte file full of NUL-bytes except
255*d289c2baSAndroid Build Coastguard Worker // for the string "brillo ftw!" at index 2 and '\n' at the last
256*d289c2baSAndroid Build Coastguard Worker // byte.
257*d289c2baSAndroid Build Coastguard Worker s = avb_property_lookup(
258*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.data(), vbmeta_image_.size(), "blob", 0, &len);
259*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(21U, len);
260*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, memcmp(s, "\0\0", 2));
261*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, memcmp(s + 2, "brillo ftw!", 11));
262*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, memcmp(s + 13, "\0\0\0\0\0\0\0", 7));
263*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ('\n', s[20]);
264*d289c2baSAndroid Build Coastguard Worker }
265*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,Padding)266*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, Padding) {
267*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage("vbmeta.img",
268*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
269*d289c2baSAndroid Build Coastguard Worker 0,
270*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
271*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"");
272*d289c2baSAndroid Build Coastguard Worker
273*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage("vbmeta_padded.img",
274*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
275*d289c2baSAndroid Build Coastguard Worker 0,
276*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
277*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" --padding_size 4096");
278*d289c2baSAndroid Build Coastguard Worker
279*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_path = testdir_ / "vbmeta.img";
280*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_padded_path = testdir_ / "vbmeta_padded.img";
281*d289c2baSAndroid Build Coastguard Worker int64_t vbmeta_size, vbmeta_padded_size;
282*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(
283*d289c2baSAndroid Build Coastguard Worker base::GetFileSize(base::FilePath(vbmeta_path.c_str()), &vbmeta_size));
284*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(base::GetFileSize(base::FilePath(vbmeta_padded_path.c_str()),
285*d289c2baSAndroid Build Coastguard Worker &vbmeta_padded_size));
286*d289c2baSAndroid Build Coastguard Worker
287*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(vbmeta_size, vbmeta_padded_size);
288*d289c2baSAndroid Build Coastguard Worker
289*d289c2baSAndroid Build Coastguard Worker // The padded size should be a multiple of 4096.
290*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(vbmeta_padded_size % 4096, 0);
291*d289c2baSAndroid Build Coastguard Worker
292*d289c2baSAndroid Build Coastguard Worker // When rounded up the unpadded size should equal the padded size.
293*d289c2baSAndroid Build Coastguard Worker int64_t vbmeta_size_rounded_up = ((vbmeta_size + 4095) / 4096) * 4096;
294*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(vbmeta_size_rounded_up, vbmeta_padded_size);
295*d289c2baSAndroid Build Coastguard Worker }
296*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,CheckRollbackIndex)297*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, CheckRollbackIndex) {
298*d289c2baSAndroid Build Coastguard Worker uint64_t rollback_index = 42;
299*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage("vbmeta.img",
300*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
301*d289c2baSAndroid Build Coastguard Worker rollback_index,
302*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
303*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"");
304*d289c2baSAndroid Build Coastguard Worker
305*d289c2baSAndroid Build Coastguard Worker AvbVBMetaImageHeader h;
306*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_header_to_host_byte_order(
307*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<AvbVBMetaImageHeader*>(vbmeta_image_.data()), &h);
308*d289c2baSAndroid Build Coastguard Worker
309*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(rollback_index, h.rollback_index);
310*d289c2baSAndroid Build Coastguard Worker }
311*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,CheckRollbackIndexLocationOmitted)312*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, CheckRollbackIndexLocationOmitted) {
313*d289c2baSAndroid Build Coastguard Worker uint32_t expected_rollback_index_location = 0;
314*d289c2baSAndroid Build Coastguard Worker
315*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage("vbmeta.img",
316*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
317*d289c2baSAndroid Build Coastguard Worker 0,
318*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
319*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"");
320*d289c2baSAndroid Build Coastguard Worker
321*d289c2baSAndroid Build Coastguard Worker AvbVBMetaImageHeader h;
322*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_header_to_host_byte_order(
323*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<AvbVBMetaImageHeader*>(vbmeta_image_.data()), &h);
324*d289c2baSAndroid Build Coastguard Worker
325*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(expected_rollback_index_location, h.rollback_index_location);
326*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1u, h.required_libavb_version_major);
327*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0u, h.required_libavb_version_minor);
328*d289c2baSAndroid Build Coastguard Worker }
329*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,CheckRollbackIndexLocation)330*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, CheckRollbackIndexLocation) {
331*d289c2baSAndroid Build Coastguard Worker uint32_t rollback_index_location = 42;
332*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
333*d289c2baSAndroid Build Coastguard Worker "vbmeta.img",
334*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
335*d289c2baSAndroid Build Coastguard Worker 0,
336*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
337*d289c2baSAndroid Build Coastguard Worker android::base::StringPrintf("--rollback_index_location %d",
338*d289c2baSAndroid Build Coastguard Worker rollback_index_location));
339*d289c2baSAndroid Build Coastguard Worker
340*d289c2baSAndroid Build Coastguard Worker AvbVBMetaImageHeader h;
341*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_header_to_host_byte_order(
342*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<AvbVBMetaImageHeader*>(vbmeta_image_.data()), &h);
343*d289c2baSAndroid Build Coastguard Worker
344*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(rollback_index_location, h.rollback_index_location);
345*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1u, h.required_libavb_version_major);
346*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(2u, h.required_libavb_version_minor);
347*d289c2baSAndroid Build Coastguard Worker }
348*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,CheckPubkeyReturned)349*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, CheckPubkeyReturned) {
350*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage("vbmeta.img",
351*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
352*d289c2baSAndroid Build Coastguard Worker 0,
353*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
354*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"");
355*d289c2baSAndroid Build Coastguard Worker
356*d289c2baSAndroid Build Coastguard Worker const uint8_t* pubkey = NULL;
357*d289c2baSAndroid Build Coastguard Worker size_t pubkey_length = 0;
358*d289c2baSAndroid Build Coastguard Worker
359*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
360*d289c2baSAndroid Build Coastguard Worker AVB_VBMETA_VERIFY_RESULT_OK,
361*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_verify(
362*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.data(), vbmeta_image_.size(), &pubkey, &pubkey_length));
363*d289c2baSAndroid Build Coastguard Worker
364*d289c2baSAndroid Build Coastguard Worker AvbVBMetaImageHeader h;
365*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_header_to_host_byte_order(
366*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<AvbVBMetaImageHeader*>(vbmeta_image_.data()), &h);
367*d289c2baSAndroid Build Coastguard Worker
368*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(pubkey_length, h.public_key_size);
369*d289c2baSAndroid Build Coastguard Worker
370*d289c2baSAndroid Build Coastguard Worker const uint8_t* expected_pubkey =
371*d289c2baSAndroid Build Coastguard Worker vbmeta_image_.data() + sizeof(AvbVBMetaImageHeader) +
372*d289c2baSAndroid Build Coastguard Worker h.authentication_data_block_size + h.public_key_offset;
373*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(pubkey, expected_pubkey);
374*d289c2baSAndroid Build Coastguard Worker }
375*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,Info)376*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, Info) {
377*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage("vbmeta.img",
378*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
379*d289c2baSAndroid Build Coastguard Worker 0,
380*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
381*d289c2baSAndroid Build Coastguard Worker "--prop foo:brillo "
382*d289c2baSAndroid Build Coastguard Worker "--prop bar:chromeos "
383*d289c2baSAndroid Build Coastguard Worker "--prop prisoner:24601 "
384*d289c2baSAndroid Build Coastguard Worker "--prop hexnumber:0xcafe "
385*d289c2baSAndroid Build Coastguard Worker "--prop hexnumber_capital:0xCAFE "
386*d289c2baSAndroid Build Coastguard Worker "--prop large_hexnumber:0xfedcba9876543210 "
387*d289c2baSAndroid Build Coastguard Worker "--prop larger_than_uint64:0xfedcba98765432101 "
388*d289c2baSAndroid Build Coastguard Worker "--prop almost_a_number:423x "
389*d289c2baSAndroid Build Coastguard Worker "--prop_from_file blob:test/data/small_blob.bin "
390*d289c2baSAndroid Build Coastguard Worker "--prop_from_file large_blob:test/data/large_blob.bin "
391*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"");
392*d289c2baSAndroid Build Coastguard Worker
393*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
394*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
395*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
396*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
397*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 3200 bytes\n"
398*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
399*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
400*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
401*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
402*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
403*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
404*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
405*d289c2baSAndroid Build Coastguard Worker " Prop: foo -> 'brillo'\n"
406*d289c2baSAndroid Build Coastguard Worker " Prop: bar -> 'chromeos'\n"
407*d289c2baSAndroid Build Coastguard Worker " Prop: prisoner -> '24601'\n"
408*d289c2baSAndroid Build Coastguard Worker " Prop: hexnumber -> '0xcafe'\n"
409*d289c2baSAndroid Build Coastguard Worker " Prop: hexnumber_capital -> '0xCAFE'\n"
410*d289c2baSAndroid Build Coastguard Worker " Prop: large_hexnumber -> '0xfedcba9876543210'\n"
411*d289c2baSAndroid Build Coastguard Worker " Prop: larger_than_uint64 -> '0xfedcba98765432101'\n"
412*d289c2baSAndroid Build Coastguard Worker " Prop: almost_a_number -> '423x'\n"
413*d289c2baSAndroid Build Coastguard Worker " Prop: blob -> '\\x00\\x00brillo "
414*d289c2baSAndroid Build Coastguard Worker "ftw!\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\n'\n"
415*d289c2baSAndroid Build Coastguard Worker " Prop: large_blob -> (2048 bytes)\n",
416*d289c2baSAndroid Build Coastguard Worker InfoImage(vbmeta_image_path_.string()));
417*d289c2baSAndroid Build Coastguard Worker }
418*d289c2baSAndroid Build Coastguard Worker
collect_descriptors(const AvbDescriptor * descriptor,void * user_data)419*d289c2baSAndroid Build Coastguard Worker static bool collect_descriptors(const AvbDescriptor* descriptor,
420*d289c2baSAndroid Build Coastguard Worker void* user_data) {
421*d289c2baSAndroid Build Coastguard Worker std::vector<const AvbDescriptor*>* descriptors =
422*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<std::vector<const AvbDescriptor*>*>(user_data);
423*d289c2baSAndroid Build Coastguard Worker descriptors->push_back(descriptor);
424*d289c2baSAndroid Build Coastguard Worker return true; // Keep iterating.
425*d289c2baSAndroid Build Coastguard Worker }
426*d289c2baSAndroid Build Coastguard Worker
AddHashFooterGetExpectedVBMetaInfo(const bool sparse_image,const uint64_t partition_size)427*d289c2baSAndroid Build Coastguard Worker static std::string AddHashFooterGetExpectedVBMetaInfo(
428*d289c2baSAndroid Build Coastguard Worker const bool sparse_image, const uint64_t partition_size) {
429*d289c2baSAndroid Build Coastguard Worker return android::base::StringPrintf(
430*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
431*d289c2baSAndroid Build Coastguard Worker "Image size: %" PRIu64
432*d289c2baSAndroid Build Coastguard Worker " bytes\n"
433*d289c2baSAndroid Build Coastguard Worker "Original image size: 1052672 bytes\n"
434*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 1052672\n"
435*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1280 bytes\n"
436*d289c2baSAndroid Build Coastguard Worker "--\n"
437*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0%s\n"
438*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
439*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
440*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 704 bytes\n"
441*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
442*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
443*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
444*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
445*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
446*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
447*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
448*d289c2baSAndroid Build Coastguard Worker " Hash descriptor:\n"
449*d289c2baSAndroid Build Coastguard Worker " Image Size: 1052672 bytes\n"
450*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha256\n"
451*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
452*d289c2baSAndroid Build Coastguard Worker " Salt: d00df00d\n"
453*d289c2baSAndroid Build Coastguard Worker " Digest: "
454*d289c2baSAndroid Build Coastguard Worker "9a58cc996d405e08a1e00f96dbfe9104fedf41cb83b1f"
455*d289c2baSAndroid Build Coastguard Worker "5e4ed357fbcf58d88d9\n"
456*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n",
457*d289c2baSAndroid Build Coastguard Worker partition_size,
458*d289c2baSAndroid Build Coastguard Worker sparse_image ? " (Sparse)" : "");
459*d289c2baSAndroid Build Coastguard Worker }
460*d289c2baSAndroid Build Coastguard Worker
AddHashFooterTest(bool sparse_image)461*d289c2baSAndroid Build Coastguard Worker void AvbToolTest::AddHashFooterTest(bool sparse_image) {
462*d289c2baSAndroid Build Coastguard Worker const size_t rootfs_size = 1028 * 1024;
463*d289c2baSAndroid Build Coastguard Worker const size_t partition_size = 1536 * 1024;
464*d289c2baSAndroid Build Coastguard Worker const size_t resized_partition_size = 1280 * 1024;
465*d289c2baSAndroid Build Coastguard Worker
466*d289c2baSAndroid Build Coastguard Worker // Generate a 1028 KiB file with known content. Some content have
467*d289c2baSAndroid Build Coastguard Worker // been arranged to ensure FILL_DATA segments in the sparse file.
468*d289c2baSAndroid Build Coastguard Worker std::vector<uint8_t> rootfs;
469*d289c2baSAndroid Build Coastguard Worker rootfs.resize(rootfs_size);
470*d289c2baSAndroid Build Coastguard Worker for (size_t n = 0; n < rootfs_size; n++) {
471*d289c2baSAndroid Build Coastguard Worker if ((n >= 5 * 1000 && n < 105 * 1000) ||
472*d289c2baSAndroid Build Coastguard Worker (n >= 205 * 1000 && n < 305 * 1000) ||
473*d289c2baSAndroid Build Coastguard Worker (n >= 505 * 1000 && n < 605 * 1000)) {
474*d289c2baSAndroid Build Coastguard Worker rootfs[n] = uint8_t(n) & 0x03;
475*d289c2baSAndroid Build Coastguard Worker } else {
476*d289c2baSAndroid Build Coastguard Worker rootfs[n] = uint8_t(n);
477*d289c2baSAndroid Build Coastguard Worker }
478*d289c2baSAndroid Build Coastguard Worker }
479*d289c2baSAndroid Build Coastguard Worker std::filesystem::path external_vbmeta_path = testdir_ / "external_vbmeta.bin";
480*d289c2baSAndroid Build Coastguard Worker std::filesystem::path extracted_vbmeta_path =
481*d289c2baSAndroid Build Coastguard Worker testdir_ / "extracted_vbmeta.bin";
482*d289c2baSAndroid Build Coastguard Worker std::filesystem::path rootfs_path = testdir_ / "rootfs.bin";
483*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(rootfs_size,
484*d289c2baSAndroid Build Coastguard Worker static_cast<const size_t>(
485*d289c2baSAndroid Build Coastguard Worker base::WriteFile(base::FilePath(rootfs_path.c_str()),
486*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const char*>(rootfs.data()),
487*d289c2baSAndroid Build Coastguard Worker rootfs.size())));
488*d289c2baSAndroid Build Coastguard Worker
489*d289c2baSAndroid Build Coastguard Worker if (sparse_image) {
490*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
491*d289c2baSAndroid Build Coastguard Worker 0, "mv %s %s.unsparse", rootfs_path.c_str(), rootfs_path.c_str());
492*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
493*d289c2baSAndroid Build Coastguard Worker 0, "img2simg %s.unsparse %s", rootfs_path.c_str(), rootfs_path.c_str());
494*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "rm -f %s.unsparse", rootfs_path.c_str());
495*d289c2baSAndroid Build Coastguard Worker }
496*d289c2baSAndroid Build Coastguard Worker
497*d289c2baSAndroid Build Coastguard Worker /* Do this twice to check that 'add_hash_footer' is idempotent. */
498*d289c2baSAndroid Build Coastguard Worker for (int n = 0; n < 2; n++) {
499*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
500*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer --salt d00df00d "
501*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm sha256 --image %s "
502*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
503*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
504*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
505*d289c2baSAndroid Build Coastguard Worker "--output_vbmeta %s "
506*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
507*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
508*d289c2baSAndroid Build Coastguard Worker (int)partition_size,
509*d289c2baSAndroid Build Coastguard Worker external_vbmeta_path.c_str());
510*d289c2baSAndroid Build Coastguard Worker
511*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(AddHashFooterGetExpectedVBMetaInfo(sparse_image, partition_size),
512*d289c2baSAndroid Build Coastguard Worker InfoImage(rootfs_path.string()));
513*d289c2baSAndroid Build Coastguard Worker
514*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
515*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
516*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
517*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
518*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 704 bytes\n"
519*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
520*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
521*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
522*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
523*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
524*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
525*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
526*d289c2baSAndroid Build Coastguard Worker " Hash descriptor:\n"
527*d289c2baSAndroid Build Coastguard Worker " Image Size: 1052672 bytes\n"
528*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha256\n"
529*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
530*d289c2baSAndroid Build Coastguard Worker " Salt: d00df00d\n"
531*d289c2baSAndroid Build Coastguard Worker " Digest: "
532*d289c2baSAndroid Build Coastguard Worker "9a58cc996d405e08a1e00f96dbfe9104fedf41cb83b1f"
533*d289c2baSAndroid Build Coastguard Worker "5e4ed357fbcf58d88d9\n"
534*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n",
535*d289c2baSAndroid Build Coastguard Worker InfoImage(external_vbmeta_path.string()));
536*d289c2baSAndroid Build Coastguard Worker
537*d289c2baSAndroid Build Coastguard Worker // Check that the extracted vbmeta matches the externally generally one.
538*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
539*d289c2baSAndroid Build Coastguard Worker "./avbtool.py extract_vbmeta_image --image %s "
540*d289c2baSAndroid Build Coastguard Worker "--output %s",
541*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
542*d289c2baSAndroid Build Coastguard Worker extracted_vbmeta_path.c_str());
543*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
544*d289c2baSAndroid Build Coastguard Worker "diff %s %s",
545*d289c2baSAndroid Build Coastguard Worker external_vbmeta_path.c_str(),
546*d289c2baSAndroid Build Coastguard Worker extracted_vbmeta_path.c_str());
547*d289c2baSAndroid Build Coastguard Worker }
548*d289c2baSAndroid Build Coastguard Worker
549*d289c2baSAndroid Build Coastguard Worker // Resize the image and check that the only thing that has changed
550*d289c2baSAndroid Build Coastguard Worker // is where the footer is. First check that resizing to a smaller
551*d289c2baSAndroid Build Coastguard Worker // size than the original rootfs fails. Then resize to something
552*d289c2baSAndroid Build Coastguard Worker // larger than the original rootfs but smaller than the current
553*d289c2baSAndroid Build Coastguard Worker // partition size.
554*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(1,
555*d289c2baSAndroid Build Coastguard Worker "./avbtool.py resize_image --image %s "
556*d289c2baSAndroid Build Coastguard Worker "--partition_size %d",
557*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
558*d289c2baSAndroid Build Coastguard Worker (int)(rootfs_size - 16 * 1024));
559*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
560*d289c2baSAndroid Build Coastguard Worker "./avbtool.py resize_image --image %s "
561*d289c2baSAndroid Build Coastguard Worker "--partition_size %d",
562*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
563*d289c2baSAndroid Build Coastguard Worker (int)resized_partition_size);
564*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
565*d289c2baSAndroid Build Coastguard Worker AddHashFooterGetExpectedVBMetaInfo(sparse_image, resized_partition_size),
566*d289c2baSAndroid Build Coastguard Worker InfoImage(rootfs_path.string()));
567*d289c2baSAndroid Build Coastguard Worker
568*d289c2baSAndroid Build Coastguard Worker if (sparse_image) {
569*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
570*d289c2baSAndroid Build Coastguard Worker 0, "mv %s %s.sparse", rootfs_path.c_str(), rootfs_path.c_str());
571*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
572*d289c2baSAndroid Build Coastguard Worker 0, "simg2img %s.sparse %s", rootfs_path.c_str(), rootfs_path.c_str());
573*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "rm -f %s.sparse", rootfs_path.c_str());
574*d289c2baSAndroid Build Coastguard Worker }
575*d289c2baSAndroid Build Coastguard Worker
576*d289c2baSAndroid Build Coastguard Worker // Manually calculate the hash to check that it agrees with avbtool.
577*d289c2baSAndroid Build Coastguard Worker AvbSHA256Ctx hasher_ctx;
578*d289c2baSAndroid Build Coastguard Worker const uint8_t hasher_salt[4] = {0xd0, 0x0d, 0xf0, 0x0d};
579*d289c2baSAndroid Build Coastguard Worker avb_sha256_init(&hasher_ctx);
580*d289c2baSAndroid Build Coastguard Worker avb_sha256_update(&hasher_ctx, hasher_salt, 4);
581*d289c2baSAndroid Build Coastguard Worker avb_sha256_update(&hasher_ctx, rootfs.data(), rootfs_size);
582*d289c2baSAndroid Build Coastguard Worker uint8_t* hasher_digest = avb_sha256_final(&hasher_ctx);
583*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("9a58cc996d405e08a1e00f96dbfe9104fedf41cb83b1f5e4ed357fbcf58d88d9",
584*d289c2baSAndroid Build Coastguard Worker mem_to_hexstring(hasher_digest, AVB_SHA256_DIGEST_SIZE));
585*d289c2baSAndroid Build Coastguard Worker
586*d289c2baSAndroid Build Coastguard Worker // Now check that we can find the VBMeta block again from the footer.
587*d289c2baSAndroid Build Coastguard Worker std::string part_data;
588*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(
589*d289c2baSAndroid Build Coastguard Worker android::base::ReadFileToString(rootfs_path.string(), &part_data));
590*d289c2baSAndroid Build Coastguard Worker
591*d289c2baSAndroid Build Coastguard Worker // Check footer contains correct data.
592*d289c2baSAndroid Build Coastguard Worker AvbFooter f;
593*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(0,
594*d289c2baSAndroid Build Coastguard Worker avb_footer_validate_and_byteswap(
595*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const AvbFooter*>(
596*d289c2baSAndroid Build Coastguard Worker part_data.data() + part_data.size() - AVB_FOOTER_SIZE),
597*d289c2baSAndroid Build Coastguard Worker &f));
598*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
599*d289c2baSAndroid Build Coastguard Worker std::string(reinterpret_cast<const char*>(f.magic), AVB_FOOTER_MAGIC_LEN),
600*d289c2baSAndroid Build Coastguard Worker AVB_FOOTER_MAGIC);
601*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_FOOTER_VERSION_MAJOR, (int)f.version_major);
602*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_FOOTER_VERSION_MINOR, (int)f.version_minor);
603*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1052672UL, f.original_image_size);
604*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1052672UL, f.vbmeta_offset);
605*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1280UL, f.vbmeta_size);
606*d289c2baSAndroid Build Coastguard Worker
607*d289c2baSAndroid Build Coastguard Worker // Check that the vbmeta image at |f.vbmeta_offset| checks out.
608*d289c2baSAndroid Build Coastguard Worker const uint8_t* vbmeta_data =
609*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const uint8_t*>(part_data.data() + f.vbmeta_offset);
610*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_VBMETA_VERIFY_RESULT_OK,
611*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_verify(vbmeta_data, f.vbmeta_size, NULL, NULL));
612*d289c2baSAndroid Build Coastguard Worker
613*d289c2baSAndroid Build Coastguard Worker // Collect all descriptors.
614*d289c2baSAndroid Build Coastguard Worker std::vector<const AvbDescriptor*> descriptors;
615*d289c2baSAndroid Build Coastguard Worker avb_descriptor_foreach(
616*d289c2baSAndroid Build Coastguard Worker vbmeta_data, f.vbmeta_size, collect_descriptors, &descriptors);
617*d289c2baSAndroid Build Coastguard Worker
618*d289c2baSAndroid Build Coastguard Worker // We should only have a single descriptor and it should be a
619*d289c2baSAndroid Build Coastguard Worker // hash descriptor.
620*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1UL, descriptors.size());
621*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_DESCRIPTOR_TAG_HASH, avb_be64toh(descriptors[0]->tag));
622*d289c2baSAndroid Build Coastguard Worker AvbHashDescriptor d;
623*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(
624*d289c2baSAndroid Build Coastguard Worker 0,
625*d289c2baSAndroid Build Coastguard Worker avb_hash_descriptor_validate_and_byteswap(
626*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const AvbHashDescriptor*>(descriptors[0]), &d));
627*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1052672UL, d.image_size);
628*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(6UL, d.partition_name_len);
629*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(4UL, d.salt_len);
630*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(32UL, d.digest_len);
631*d289c2baSAndroid Build Coastguard Worker const uint8_t* desc_end = reinterpret_cast<const uint8_t*>(descriptors[0]) +
632*d289c2baSAndroid Build Coastguard Worker sizeof(AvbHashDescriptor);
633*d289c2baSAndroid Build Coastguard Worker uint64_t o = 0;
634*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("foobar",
635*d289c2baSAndroid Build Coastguard Worker std::string(reinterpret_cast<const char*>(desc_end + o),
636*d289c2baSAndroid Build Coastguard Worker d.partition_name_len));
637*d289c2baSAndroid Build Coastguard Worker o += d.partition_name_len;
638*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("d00df00d", mem_to_hexstring(desc_end + o, d.salt_len));
639*d289c2baSAndroid Build Coastguard Worker o += d.salt_len;
640*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("9a58cc996d405e08a1e00f96dbfe9104fedf41cb83b1f5e4ed357fbcf58d88d9",
641*d289c2baSAndroid Build Coastguard Worker mem_to_hexstring(desc_end + o, d.digest_len));
642*d289c2baSAndroid Build Coastguard Worker
643*d289c2baSAndroid Build Coastguard Worker // Check that the footer is correctly erased.
644*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
645*d289c2baSAndroid Build Coastguard Worker 0, "./avbtool.py erase_footer --image %s", rootfs_path.c_str());
646*d289c2baSAndroid Build Coastguard Worker int64_t erased_footer_file_size;
647*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(base::GetFileSize(base::FilePath(rootfs_path.c_str()),
648*d289c2baSAndroid Build Coastguard Worker &erased_footer_file_size));
649*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(static_cast<size_t>(erased_footer_file_size), rootfs_size);
650*d289c2baSAndroid Build Coastguard Worker
651*d289c2baSAndroid Build Coastguard Worker // Check that --do_not_append_vbmeta_image works as intended.
652*d289c2baSAndroid Build Coastguard Worker // In this case we don't modify the input image so it should work read-only.
653*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "chmod a-w %s", rootfs_path.c_str());
654*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
655*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer --salt d00df00d "
656*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm sha256 --image %s "
657*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
658*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
659*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
660*d289c2baSAndroid Build Coastguard Worker "--output_vbmeta %s_2nd_run --do_not_append_vbmeta_image "
661*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
662*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
663*d289c2baSAndroid Build Coastguard Worker (int)partition_size,
664*d289c2baSAndroid Build Coastguard Worker external_vbmeta_path.c_str());
665*d289c2baSAndroid Build Coastguard Worker int64_t file_size;
666*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(
667*d289c2baSAndroid Build Coastguard Worker base::GetFileSize(base::FilePath(rootfs_path.c_str()), &file_size));
668*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(static_cast<size_t>(file_size), rootfs_size);
669*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
670*d289c2baSAndroid Build Coastguard Worker "diff %s %s_2nd_run",
671*d289c2baSAndroid Build Coastguard Worker external_vbmeta_path.c_str(),
672*d289c2baSAndroid Build Coastguard Worker external_vbmeta_path.c_str());
673*d289c2baSAndroid Build Coastguard Worker }
674*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashFooter)675*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashFooter) {
676*d289c2baSAndroid Build Coastguard Worker AddHashFooterTest(false);
677*d289c2baSAndroid Build Coastguard Worker }
678*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashFooterSparse)679*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashFooterSparse) {
680*d289c2baSAndroid Build Coastguard Worker AddHashFooterTest(true);
681*d289c2baSAndroid Build Coastguard Worker }
682*d289c2baSAndroid Build Coastguard Worker
RemoveLinesStartingWith(const std::string & str,const std::string & prefix)683*d289c2baSAndroid Build Coastguard Worker static std::string RemoveLinesStartingWith(const std::string& str,
684*d289c2baSAndroid Build Coastguard Worker const std::string& prefix) {
685*d289c2baSAndroid Build Coastguard Worker std::vector<std::string> lines;
686*d289c2baSAndroid Build Coastguard Worker std::string ret;
687*d289c2baSAndroid Build Coastguard Worker
688*d289c2baSAndroid Build Coastguard Worker lines = base::SplitString(
689*d289c2baSAndroid Build Coastguard Worker str, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
690*d289c2baSAndroid Build Coastguard Worker for (const std::string& line : lines) {
691*d289c2baSAndroid Build Coastguard Worker if (!base::StartsWith(line, prefix, base::CompareCase::SENSITIVE)) {
692*d289c2baSAndroid Build Coastguard Worker ret += line;
693*d289c2baSAndroid Build Coastguard Worker ret += '\n';
694*d289c2baSAndroid Build Coastguard Worker }
695*d289c2baSAndroid Build Coastguard Worker }
696*d289c2baSAndroid Build Coastguard Worker return ret;
697*d289c2baSAndroid Build Coastguard Worker }
698*d289c2baSAndroid Build Coastguard Worker
699*d289c2baSAndroid Build Coastguard Worker // NOTE: make_ext4fs was removed and there is no replacement for how we use
700*d289c2baSAndroid Build Coastguard Worker // it... so this is currently disabled..
TEST_F(AvbToolTest,DISABLED_AddHashFooterSparseWithHoleAtTheEnd)701*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, DISABLED_AddHashFooterSparseWithHoleAtTheEnd) {
702*d289c2baSAndroid Build Coastguard Worker const size_t partition_size = 10 * 1024 * 1024;
703*d289c2baSAndroid Build Coastguard Worker const size_t metadata_size = 128 * 1024;
704*d289c2baSAndroid Build Coastguard Worker
705*d289c2baSAndroid Build Coastguard Worker // It's not enough to run img2simg on a file with a lot of zeroes at
706*d289c2baSAndroid Build Coastguard Worker // the end since that will turn up as "Fill with value (for value =
707*d289c2baSAndroid Build Coastguard Worker // 0x00000000)" and not "Don't care". Instead, use make_ext4fs for
708*d289c2baSAndroid Build Coastguard Worker // this since it will put a big hole (e.g. "Don't care" chunk) at
709*d289c2baSAndroid Build Coastguard Worker // the end.
710*d289c2baSAndroid Build Coastguard Worker std::filesystem::path partition_path = testdir_ / "partition.bin";
711*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
712*d289c2baSAndroid Build Coastguard Worker "make_ext4fs -s -L test -l %zd %s",
713*d289c2baSAndroid Build Coastguard Worker partition_size - metadata_size,
714*d289c2baSAndroid Build Coastguard Worker partition_path.c_str());
715*d289c2baSAndroid Build Coastguard Worker
716*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
717*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer --salt d00df00d "
718*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm sha256 --image %s "
719*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
720*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
721*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
722*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
723*d289c2baSAndroid Build Coastguard Worker partition_path.c_str(),
724*d289c2baSAndroid Build Coastguard Worker (int)partition_size);
725*d289c2baSAndroid Build Coastguard Worker
726*d289c2baSAndroid Build Coastguard Worker // Since we may be using an arbritary version of make_ext4fs
727*d289c2baSAndroid Build Coastguard Worker // (because of different branches) the contents of the resulting
728*d289c2baSAndroid Build Coastguard Worker // disk image may slightly change. It's enough to just remove the
729*d289c2baSAndroid Build Coastguard Worker // "Digest:" line from the output to work around this.
730*d289c2baSAndroid Build Coastguard Worker std::string info = RemoveLinesStartingWith(InfoImage(partition_path.string()),
731*d289c2baSAndroid Build Coastguard Worker " Digest:");
732*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
733*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
734*d289c2baSAndroid Build Coastguard Worker "Image size: 10485760 bytes\n"
735*d289c2baSAndroid Build Coastguard Worker "Original image size: 10354688 bytes\n"
736*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 10354688\n"
737*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1280 bytes\n"
738*d289c2baSAndroid Build Coastguard Worker "--\n"
739*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0 (Sparse)\n"
740*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
741*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
742*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 704 bytes\n"
743*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
744*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
745*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
746*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
747*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
748*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
749*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
750*d289c2baSAndroid Build Coastguard Worker " Hash descriptor:\n"
751*d289c2baSAndroid Build Coastguard Worker " Image Size: 10354688 bytes\n"
752*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha256\n"
753*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
754*d289c2baSAndroid Build Coastguard Worker " Salt: d00df00d\n"
755*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n",
756*d289c2baSAndroid Build Coastguard Worker info);
757*d289c2baSAndroid Build Coastguard Worker
758*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
759*d289c2baSAndroid Build Coastguard Worker 0, "mv %s %s.sparse", partition_path.c_str(), partition_path.c_str());
760*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
761*d289c2baSAndroid Build Coastguard Worker "simg2img %s.sparse %s",
762*d289c2baSAndroid Build Coastguard Worker partition_path.c_str(),
763*d289c2baSAndroid Build Coastguard Worker partition_path.c_str());
764*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "rm -f %s.sparse", partition_path.c_str());
765*d289c2baSAndroid Build Coastguard Worker }
766*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashFooterCalcMaxImageSize)767*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashFooterCalcMaxImageSize) {
768*d289c2baSAndroid Build Coastguard Worker const size_t partition_size = 10 * 1024 * 1024;
769*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / "max_size.txt";
770*d289c2baSAndroid Build Coastguard Worker
771*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
772*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer "
773*d289c2baSAndroid Build Coastguard Worker "--partition_size %zd "
774*d289c2baSAndroid Build Coastguard Worker "--calc_max_image_size > %s",
775*d289c2baSAndroid Build Coastguard Worker partition_size,
776*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
777*d289c2baSAndroid Build Coastguard Worker std::string max_image_size_data;
778*d289c2baSAndroid Build Coastguard Worker EXPECT_TRUE(android::base::ReadFileToString(output_path.string(),
779*d289c2baSAndroid Build Coastguard Worker &max_image_size_data));
780*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("10416128\n", max_image_size_data);
781*d289c2baSAndroid Build Coastguard Worker size_t max_image_size = atoll(max_image_size_data.c_str());
782*d289c2baSAndroid Build Coastguard Worker
783*d289c2baSAndroid Build Coastguard Worker // Metadata takes up 68 KiB.
784*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(68 * 1024ULL, partition_size - max_image_size);
785*d289c2baSAndroid Build Coastguard Worker
786*d289c2baSAndroid Build Coastguard Worker // Check that we can add a hash footer for an image this size for
787*d289c2baSAndroid Build Coastguard Worker // such a partition size.
788*d289c2baSAndroid Build Coastguard Worker std::string boot_path = GenerateImage("boot", max_image_size);
789*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
790*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer"
791*d289c2baSAndroid Build Coastguard Worker " --image %s"
792*d289c2baSAndroid Build Coastguard Worker " --partition_name boot"
793*d289c2baSAndroid Build Coastguard Worker " --partition_size %zd"
794*d289c2baSAndroid Build Coastguard Worker " --salt deadbeef"
795*d289c2baSAndroid Build Coastguard Worker " --algorithm SHA512_RSA4096 "
796*d289c2baSAndroid Build Coastguard Worker " --key test/data/testkey_rsa4096.pem"
797*d289c2baSAndroid Build Coastguard Worker " --internal_release_string \"\"",
798*d289c2baSAndroid Build Coastguard Worker boot_path.c_str(),
799*d289c2baSAndroid Build Coastguard Worker partition_size);
800*d289c2baSAndroid Build Coastguard Worker }
801*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashFooterWithPersistentDigest)802*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashFooterWithPersistentDigest) {
803*d289c2baSAndroid Build Coastguard Worker size_t partition_size = 1024 * 1024;
804*d289c2baSAndroid Build Coastguard Worker std::string path = GenerateImage("digest_location", 1024);
805*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
806*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer "
807*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm sha256 --image %s "
808*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
809*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
810*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
811*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" "
812*d289c2baSAndroid Build Coastguard Worker "--use_persistent_digest",
813*d289c2baSAndroid Build Coastguard Worker path.c_str(),
814*d289c2baSAndroid Build Coastguard Worker (int)partition_size);
815*d289c2baSAndroid Build Coastguard Worker // There are two important bits specific to these flags:
816*d289c2baSAndroid Build Coastguard Worker // Minimum libavb version = 1.1
817*d289c2baSAndroid Build Coastguard Worker // Hash descriptor -> Digest = (empty)
818*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
819*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
820*d289c2baSAndroid Build Coastguard Worker "Image size: 1048576 bytes\n"
821*d289c2baSAndroid Build Coastguard Worker "Original image size: 1024 bytes\n"
822*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 4096\n"
823*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1280 bytes\n"
824*d289c2baSAndroid Build Coastguard Worker "--\n"
825*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.1\n"
826*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
827*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
828*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 704 bytes\n"
829*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
830*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
831*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
832*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
833*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
834*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
835*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
836*d289c2baSAndroid Build Coastguard Worker " Hash descriptor:\n"
837*d289c2baSAndroid Build Coastguard Worker " Image Size: 1024 bytes\n"
838*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha256\n"
839*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
840*d289c2baSAndroid Build Coastguard Worker " Salt: \n"
841*d289c2baSAndroid Build Coastguard Worker " Digest: \n"
842*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n",
843*d289c2baSAndroid Build Coastguard Worker InfoImage(path));
844*d289c2baSAndroid Build Coastguard Worker }
845*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashFooterWithNoAB)846*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashFooterWithNoAB) {
847*d289c2baSAndroid Build Coastguard Worker size_t partition_size = 1024 * 1024;
848*d289c2baSAndroid Build Coastguard Worker std::string path = GenerateImage("digest_location", 1024);
849*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
850*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer --salt d00df00d "
851*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm sha256 --image %s "
852*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
853*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
854*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
855*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" "
856*d289c2baSAndroid Build Coastguard Worker "--do_not_use_ab",
857*d289c2baSAndroid Build Coastguard Worker path.c_str(),
858*d289c2baSAndroid Build Coastguard Worker (int)partition_size);
859*d289c2baSAndroid Build Coastguard Worker // There are two important bits specific to these flags:
860*d289c2baSAndroid Build Coastguard Worker // Minimum libavb version = 1.1
861*d289c2baSAndroid Build Coastguard Worker // Hash descriptor -> Flags = 1
862*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
863*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
864*d289c2baSAndroid Build Coastguard Worker "Image size: 1048576 bytes\n"
865*d289c2baSAndroid Build Coastguard Worker "Original image size: 1024 bytes\n"
866*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 4096\n"
867*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1280 bytes\n"
868*d289c2baSAndroid Build Coastguard Worker "--\n"
869*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.1\n"
870*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
871*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
872*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 704 bytes\n"
873*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
874*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
875*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
876*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
877*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
878*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
879*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
880*d289c2baSAndroid Build Coastguard Worker " Hash descriptor:\n"
881*d289c2baSAndroid Build Coastguard Worker " Image Size: 1024 bytes\n"
882*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha256\n"
883*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
884*d289c2baSAndroid Build Coastguard Worker " Salt: d00df00d\n"
885*d289c2baSAndroid Build Coastguard Worker " Digest: "
886*d289c2baSAndroid Build Coastguard Worker "91386fea3e251ad0c2cb6859e4f4772f37fdb69f17d46636ddc9e7fbfd3bf3d0\n"
887*d289c2baSAndroid Build Coastguard Worker " Flags: 1\n",
888*d289c2baSAndroid Build Coastguard Worker InfoImage(path));
889*d289c2baSAndroid Build Coastguard Worker }
890*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashFooterWithPersistentDigestAndNoAB)891*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashFooterWithPersistentDigestAndNoAB) {
892*d289c2baSAndroid Build Coastguard Worker size_t partition_size = 1024 * 1024;
893*d289c2baSAndroid Build Coastguard Worker std::string path = GenerateImage("digest_location", 1024);
894*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
895*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer "
896*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm sha256 --image %s "
897*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
898*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
899*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
900*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" "
901*d289c2baSAndroid Build Coastguard Worker "--use_persistent_digest --do_not_use_ab",
902*d289c2baSAndroid Build Coastguard Worker path.c_str(),
903*d289c2baSAndroid Build Coastguard Worker (int)partition_size);
904*d289c2baSAndroid Build Coastguard Worker // There are three important bits specific to these flags:
905*d289c2baSAndroid Build Coastguard Worker // Minimum libavb version = 1.1
906*d289c2baSAndroid Build Coastguard Worker // Hash descriptor -> Digest = (empty)
907*d289c2baSAndroid Build Coastguard Worker // Hash descriptor -> Flags = 1
908*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
909*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
910*d289c2baSAndroid Build Coastguard Worker "Image size: 1048576 bytes\n"
911*d289c2baSAndroid Build Coastguard Worker "Original image size: 1024 bytes\n"
912*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 4096\n"
913*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1280 bytes\n"
914*d289c2baSAndroid Build Coastguard Worker "--\n"
915*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.1\n"
916*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
917*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
918*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 704 bytes\n"
919*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
920*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
921*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
922*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
923*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
924*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
925*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
926*d289c2baSAndroid Build Coastguard Worker " Hash descriptor:\n"
927*d289c2baSAndroid Build Coastguard Worker " Image Size: 1024 bytes\n"
928*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha256\n"
929*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
930*d289c2baSAndroid Build Coastguard Worker " Salt: \n"
931*d289c2baSAndroid Build Coastguard Worker " Digest: \n"
932*d289c2baSAndroid Build Coastguard Worker " Flags: 1\n",
933*d289c2baSAndroid Build Coastguard Worker InfoImage(path));
934*d289c2baSAndroid Build Coastguard Worker }
935*d289c2baSAndroid Build Coastguard Worker
CreateRootfsWithHashtreeFooter(bool sparse_image,const std::string & hash_algorithm,const std::string & root_digest,std::filesystem::path * output_rootfs_path)936*d289c2baSAndroid Build Coastguard Worker void AvbToolTest::CreateRootfsWithHashtreeFooter(
937*d289c2baSAndroid Build Coastguard Worker bool sparse_image,
938*d289c2baSAndroid Build Coastguard Worker const std::string& hash_algorithm,
939*d289c2baSAndroid Build Coastguard Worker const std::string& root_digest,
940*d289c2baSAndroid Build Coastguard Worker std::filesystem::path* output_rootfs_path) {
941*d289c2baSAndroid Build Coastguard Worker const size_t rootfs_size = 1028 * 1024;
942*d289c2baSAndroid Build Coastguard Worker const size_t partition_size = 1536 * 1024;
943*d289c2baSAndroid Build Coastguard Worker
944*d289c2baSAndroid Build Coastguard Worker // Generate a 1028 KiB file with known content.
945*d289c2baSAndroid Build Coastguard Worker std::vector<uint8_t> rootfs;
946*d289c2baSAndroid Build Coastguard Worker rootfs.resize(rootfs_size);
947*d289c2baSAndroid Build Coastguard Worker for (size_t n = 0; n < rootfs_size; n++)
948*d289c2baSAndroid Build Coastguard Worker rootfs[n] = uint8_t(n);
949*d289c2baSAndroid Build Coastguard Worker std::filesystem::path external_vbmeta_path = testdir_ / "external_vbmeta.bin";
950*d289c2baSAndroid Build Coastguard Worker std::filesystem::path extracted_vbmeta_path =
951*d289c2baSAndroid Build Coastguard Worker testdir_ / "extracted_vbmeta.bin";
952*d289c2baSAndroid Build Coastguard Worker std::filesystem::path rootfs_path = testdir_ / "rootfs.bin";
953*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(rootfs_size,
954*d289c2baSAndroid Build Coastguard Worker static_cast<const size_t>(
955*d289c2baSAndroid Build Coastguard Worker base::WriteFile(base::FilePath(rootfs_path.c_str()),
956*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const char*>(rootfs.data()),
957*d289c2baSAndroid Build Coastguard Worker rootfs.size())));
958*d289c2baSAndroid Build Coastguard Worker
959*d289c2baSAndroid Build Coastguard Worker if (sparse_image) {
960*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
961*d289c2baSAndroid Build Coastguard Worker 0, "mv %s %s.unsparse", rootfs_path.c_str(), rootfs_path.c_str());
962*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
963*d289c2baSAndroid Build Coastguard Worker 0, "img2simg %s.unsparse %s", rootfs_path.c_str(), rootfs_path.c_str());
964*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "rm -f %s.unsparse", rootfs_path.c_str());
965*d289c2baSAndroid Build Coastguard Worker }
966*d289c2baSAndroid Build Coastguard Worker
967*d289c2baSAndroid Build Coastguard Worker /* Do this twice to check that 'add_hashtree_footer' is idempotent. */
968*d289c2baSAndroid Build Coastguard Worker for (int n = 0; n < 2; n++) {
969*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
970*d289c2baSAndroid Build Coastguard Worker 0,
971*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d --image %s "
972*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm %s "
973*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
974*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
975*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
976*d289c2baSAndroid Build Coastguard Worker "--output_vbmeta_image %s "
977*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" "
978*d289c2baSAndroid Build Coastguard Worker "--do_not_generate_fec",
979*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
980*d289c2baSAndroid Build Coastguard Worker hash_algorithm.c_str(),
981*d289c2baSAndroid Build Coastguard Worker (int)partition_size,
982*d289c2baSAndroid Build Coastguard Worker external_vbmeta_path.c_str());
983*d289c2baSAndroid Build Coastguard Worker
984*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(android::base::StringPrintf(
985*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
986*d289c2baSAndroid Build Coastguard Worker "Image size: 1572864 bytes\n"
987*d289c2baSAndroid Build Coastguard Worker "Original image size: 1052672 bytes\n"
988*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 1069056\n"
989*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1344 bytes\n"
990*d289c2baSAndroid Build Coastguard Worker "--\n"
991*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0%s\n"
992*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
993*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
994*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 768 bytes\n"
995*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): "
996*d289c2baSAndroid Build Coastguard Worker "cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
997*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
998*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
999*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
1000*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
1001*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
1002*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
1003*d289c2baSAndroid Build Coastguard Worker " Hashtree descriptor:\n"
1004*d289c2baSAndroid Build Coastguard Worker " Version of dm-verity: 1\n"
1005*d289c2baSAndroid Build Coastguard Worker " Image Size: 1052672 bytes\n"
1006*d289c2baSAndroid Build Coastguard Worker " Tree Offset: 1052672\n"
1007*d289c2baSAndroid Build Coastguard Worker " Tree Size: 16384 bytes\n"
1008*d289c2baSAndroid Build Coastguard Worker " Data Block Size: 4096 bytes\n"
1009*d289c2baSAndroid Build Coastguard Worker " Hash Block Size: 4096 bytes\n"
1010*d289c2baSAndroid Build Coastguard Worker " FEC num roots: 0\n"
1011*d289c2baSAndroid Build Coastguard Worker " FEC offset: 0\n"
1012*d289c2baSAndroid Build Coastguard Worker " FEC size: 0 bytes\n"
1013*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: %s\n"
1014*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
1015*d289c2baSAndroid Build Coastguard Worker " Salt: d00df00d\n"
1016*d289c2baSAndroid Build Coastguard Worker " Root Digest: "
1017*d289c2baSAndroid Build Coastguard Worker "%s\n"
1018*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n",
1019*d289c2baSAndroid Build Coastguard Worker sparse_image ? " (Sparse)" : "",
1020*d289c2baSAndroid Build Coastguard Worker hash_algorithm.c_str(),
1021*d289c2baSAndroid Build Coastguard Worker root_digest.c_str()),
1022*d289c2baSAndroid Build Coastguard Worker InfoImage(rootfs_path.string()));
1023*d289c2baSAndroid Build Coastguard Worker
1024*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(android::base::StringPrintf(
1025*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
1026*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
1027*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
1028*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 768 bytes\n"
1029*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): "
1030*d289c2baSAndroid Build Coastguard Worker "cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
1031*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
1032*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
1033*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
1034*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
1035*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
1036*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
1037*d289c2baSAndroid Build Coastguard Worker " Hashtree descriptor:\n"
1038*d289c2baSAndroid Build Coastguard Worker " Version of dm-verity: 1\n"
1039*d289c2baSAndroid Build Coastguard Worker " Image Size: 1052672 bytes\n"
1040*d289c2baSAndroid Build Coastguard Worker " Tree Offset: 1052672\n"
1041*d289c2baSAndroid Build Coastguard Worker " Tree Size: 16384 bytes\n"
1042*d289c2baSAndroid Build Coastguard Worker " Data Block Size: 4096 bytes\n"
1043*d289c2baSAndroid Build Coastguard Worker " Hash Block Size: 4096 bytes\n"
1044*d289c2baSAndroid Build Coastguard Worker " FEC num roots: 0\n"
1045*d289c2baSAndroid Build Coastguard Worker " FEC offset: 0\n"
1046*d289c2baSAndroid Build Coastguard Worker " FEC size: 0 bytes\n"
1047*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: %s\n"
1048*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
1049*d289c2baSAndroid Build Coastguard Worker " Salt: d00df00d\n"
1050*d289c2baSAndroid Build Coastguard Worker " Root Digest: "
1051*d289c2baSAndroid Build Coastguard Worker "%s\n"
1052*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n",
1053*d289c2baSAndroid Build Coastguard Worker hash_algorithm.c_str(),
1054*d289c2baSAndroid Build Coastguard Worker root_digest.c_str()),
1055*d289c2baSAndroid Build Coastguard Worker InfoImage(external_vbmeta_path.string()));
1056*d289c2baSAndroid Build Coastguard Worker
1057*d289c2baSAndroid Build Coastguard Worker // Check that the extracted vbmeta matches the externally generally one.
1058*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1059*d289c2baSAndroid Build Coastguard Worker "./avbtool.py extract_vbmeta_image --image %s "
1060*d289c2baSAndroid Build Coastguard Worker "--output %s",
1061*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
1062*d289c2baSAndroid Build Coastguard Worker extracted_vbmeta_path.c_str());
1063*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1064*d289c2baSAndroid Build Coastguard Worker "diff %s %s",
1065*d289c2baSAndroid Build Coastguard Worker external_vbmeta_path.c_str(),
1066*d289c2baSAndroid Build Coastguard Worker extracted_vbmeta_path.c_str());
1067*d289c2baSAndroid Build Coastguard Worker }
1068*d289c2baSAndroid Build Coastguard Worker
1069*d289c2baSAndroid Build Coastguard Worker *output_rootfs_path = rootfs_path.c_str();
1070*d289c2baSAndroid Build Coastguard Worker }
1071*d289c2baSAndroid Build Coastguard Worker
AddHashtreeFooterTest(bool sparse_image)1072*d289c2baSAndroid Build Coastguard Worker void AvbToolTest::AddHashtreeFooterTest(bool sparse_image) {
1073*d289c2baSAndroid Build Coastguard Worker std::filesystem::path rootfs_path;
1074*d289c2baSAndroid Build Coastguard Worker CreateRootfsWithHashtreeFooter(sparse_image,
1075*d289c2baSAndroid Build Coastguard Worker "sha1",
1076*d289c2baSAndroid Build Coastguard Worker "e811611467dcd6e8dc4324e45f706c2bdd51db67",
1077*d289c2baSAndroid Build Coastguard Worker &rootfs_path);
1078*d289c2baSAndroid Build Coastguard Worker
1079*d289c2baSAndroid Build Coastguard Worker /* Zero the hashtree on a copy of the image. */
1080*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "cp %s %s.zht", rootfs_path.c_str(), rootfs_path.c_str());
1081*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
1082*d289c2baSAndroid Build Coastguard Worker 0, "./avbtool.py zero_hashtree --image %s.zht ", rootfs_path.c_str());
1083*d289c2baSAndroid Build Coastguard Worker
1084*d289c2baSAndroid Build Coastguard Worker if (sparse_image) {
1085*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
1086*d289c2baSAndroid Build Coastguard Worker 0, "mv %s %s.sparse", rootfs_path.c_str(), rootfs_path.c_str());
1087*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
1088*d289c2baSAndroid Build Coastguard Worker 0, "simg2img %s.sparse %s", rootfs_path.c_str(), rootfs_path.c_str());
1089*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "rm -f %s.sparse", rootfs_path.c_str());
1090*d289c2baSAndroid Build Coastguard Worker
1091*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
1092*d289c2baSAndroid Build Coastguard Worker 0, "mv %s.zht %s.zht.sparse", rootfs_path.c_str(), rootfs_path.c_str());
1093*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1094*d289c2baSAndroid Build Coastguard Worker "simg2img %s.zht.sparse %s.zht",
1095*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
1096*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str());
1097*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "rm -f %s.zht.sparse", rootfs_path.c_str());
1098*d289c2baSAndroid Build Coastguard Worker }
1099*d289c2baSAndroid Build Coastguard Worker
1100*d289c2baSAndroid Build Coastguard Worker // To check that we generate the correct hashtree we can use
1101*d289c2baSAndroid Build Coastguard Worker // veritysetup(1) - another codebase for working with dm-verity
1102*d289c2baSAndroid Build Coastguard Worker // hashtrees - to verify it.
1103*d289c2baSAndroid Build Coastguard Worker //
1104*d289c2baSAndroid Build Coastguard Worker // If we don't want to impose the requirement of having the
1105*d289c2baSAndroid Build Coastguard Worker // veritysetup(1) command available on builders we can comment this
1106*d289c2baSAndroid Build Coastguard Worker // out.
1107*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1108*d289c2baSAndroid Build Coastguard Worker "veritysetup --no-superblock --format=1 --hash=sha1 "
1109*d289c2baSAndroid Build Coastguard Worker "--data-block-size=4096 --hash-block-size=4096 "
1110*d289c2baSAndroid Build Coastguard Worker "--salt=d00df00d "
1111*d289c2baSAndroid Build Coastguard Worker "--data-blocks=257 "
1112*d289c2baSAndroid Build Coastguard Worker "--hash-offset=1052672 "
1113*d289c2baSAndroid Build Coastguard Worker "verify "
1114*d289c2baSAndroid Build Coastguard Worker "%s %s "
1115*d289c2baSAndroid Build Coastguard Worker "e811611467dcd6e8dc4324e45f706c2bdd51db67",
1116*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
1117*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str());
1118*d289c2baSAndroid Build Coastguard Worker
1119*d289c2baSAndroid Build Coastguard Worker // Now check that we can find the VBMeta block again from the footer.
1120*d289c2baSAndroid Build Coastguard Worker std::string part_data;
1121*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(
1122*d289c2baSAndroid Build Coastguard Worker android::base::ReadFileToString(rootfs_path.string(), &part_data));
1123*d289c2baSAndroid Build Coastguard Worker
1124*d289c2baSAndroid Build Coastguard Worker // Also read the zeroed hash-tree version.
1125*d289c2baSAndroid Build Coastguard Worker std::string zht_part_data;
1126*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(base::ReadFileToString(
1127*d289c2baSAndroid Build Coastguard Worker base::FilePath(rootfs_path.string() + ".zht"), &zht_part_data));
1128*d289c2baSAndroid Build Coastguard Worker
1129*d289c2baSAndroid Build Coastguard Worker // Check footer contains correct data.
1130*d289c2baSAndroid Build Coastguard Worker AvbFooter f;
1131*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(0,
1132*d289c2baSAndroid Build Coastguard Worker avb_footer_validate_and_byteswap(
1133*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const AvbFooter*>(
1134*d289c2baSAndroid Build Coastguard Worker part_data.data() + part_data.size() - AVB_FOOTER_SIZE),
1135*d289c2baSAndroid Build Coastguard Worker &f));
1136*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
1137*d289c2baSAndroid Build Coastguard Worker std::string(reinterpret_cast<const char*>(f.magic), AVB_FOOTER_MAGIC_LEN),
1138*d289c2baSAndroid Build Coastguard Worker AVB_FOOTER_MAGIC);
1139*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_FOOTER_VERSION_MAJOR, (int)f.version_major);
1140*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_FOOTER_VERSION_MINOR, (int)f.version_minor);
1141*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1052672UL, f.original_image_size);
1142*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1069056UL, f.vbmeta_offset);
1143*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1344UL, f.vbmeta_size);
1144*d289c2baSAndroid Build Coastguard Worker
1145*d289c2baSAndroid Build Coastguard Worker // Check that the vbmeta image at |f.vbmeta_offset| checks out.
1146*d289c2baSAndroid Build Coastguard Worker const uint8_t* vbmeta_data =
1147*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const uint8_t*>(part_data.data() + f.vbmeta_offset);
1148*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_VBMETA_VERIFY_RESULT_OK,
1149*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_verify(vbmeta_data, f.vbmeta_size, NULL, NULL));
1150*d289c2baSAndroid Build Coastguard Worker
1151*d289c2baSAndroid Build Coastguard Worker // Collect all descriptors.
1152*d289c2baSAndroid Build Coastguard Worker std::vector<const AvbDescriptor*> descriptors;
1153*d289c2baSAndroid Build Coastguard Worker avb_descriptor_foreach(
1154*d289c2baSAndroid Build Coastguard Worker vbmeta_data, f.vbmeta_size, collect_descriptors, &descriptors);
1155*d289c2baSAndroid Build Coastguard Worker
1156*d289c2baSAndroid Build Coastguard Worker // We should only have a single descriptor and it should be a
1157*d289c2baSAndroid Build Coastguard Worker // hashtree descriptor.
1158*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1UL, descriptors.size());
1159*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_DESCRIPTOR_TAG_HASHTREE, avb_be64toh(descriptors[0]->tag));
1160*d289c2baSAndroid Build Coastguard Worker AvbHashtreeDescriptor d;
1161*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(
1162*d289c2baSAndroid Build Coastguard Worker 0,
1163*d289c2baSAndroid Build Coastguard Worker avb_hashtree_descriptor_validate_and_byteswap(
1164*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const AvbHashtreeDescriptor*>(descriptors[0]), &d));
1165*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1UL, d.dm_verity_version);
1166*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1052672UL, d.image_size);
1167*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1052672UL, d.tree_offset);
1168*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(16384UL, d.tree_size);
1169*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(4096UL, d.data_block_size);
1170*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(4096UL, d.hash_block_size);
1171*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(6UL, d.partition_name_len);
1172*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(4UL, d.salt_len);
1173*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(20UL, d.root_digest_len);
1174*d289c2baSAndroid Build Coastguard Worker const uint8_t* desc_end = reinterpret_cast<const uint8_t*>(descriptors[0]) +
1175*d289c2baSAndroid Build Coastguard Worker sizeof(AvbHashtreeDescriptor);
1176*d289c2baSAndroid Build Coastguard Worker uint64_t o = 0;
1177*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("foobar",
1178*d289c2baSAndroid Build Coastguard Worker std::string(reinterpret_cast<const char*>(desc_end + o),
1179*d289c2baSAndroid Build Coastguard Worker d.partition_name_len));
1180*d289c2baSAndroid Build Coastguard Worker o += d.partition_name_len;
1181*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("d00df00d", mem_to_hexstring(desc_end + o, d.salt_len));
1182*d289c2baSAndroid Build Coastguard Worker o += d.salt_len;
1183*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("e811611467dcd6e8dc4324e45f706c2bdd51db67",
1184*d289c2baSAndroid Build Coastguard Worker mem_to_hexstring(desc_end + o, d.root_digest_len));
1185*d289c2baSAndroid Build Coastguard Worker
1186*d289c2baSAndroid Build Coastguard Worker // Check that the zeroed hashtree version differ only by the hashtree + fec
1187*d289c2baSAndroid Build Coastguard Worker // being zeroed out.
1188*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(part_data.size(), zht_part_data.size());
1189*d289c2baSAndroid Build Coastguard Worker size_t zht_ht_begin = d.tree_offset;
1190*d289c2baSAndroid Build Coastguard Worker size_t zht_ht_end = zht_ht_begin + d.tree_size;
1191*d289c2baSAndroid Build Coastguard Worker size_t zht_fec_begin = zht_ht_end;
1192*d289c2baSAndroid Build Coastguard Worker size_t zht_fec_end = zht_fec_begin + d.fec_size;
1193*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, memcmp(part_data.data(), zht_part_data.data(), zht_ht_begin));
1194*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(0,
1195*d289c2baSAndroid Build Coastguard Worker memcmp(part_data.data() + zht_ht_begin,
1196*d289c2baSAndroid Build Coastguard Worker zht_part_data.data() + zht_ht_begin,
1197*d289c2baSAndroid Build Coastguard Worker zht_fec_end - zht_ht_begin));
1198*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0,
1199*d289c2baSAndroid Build Coastguard Worker memcmp(part_data.data() + zht_fec_end,
1200*d289c2baSAndroid Build Coastguard Worker zht_part_data.data() + zht_fec_end,
1201*d289c2baSAndroid Build Coastguard Worker zht_part_data.size() - zht_fec_end));
1202*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, strncmp(zht_part_data.data() + zht_ht_begin, "ZeRoHaSH", 8));
1203*d289c2baSAndroid Build Coastguard Worker for (size_t n = zht_ht_begin + 8; n < zht_ht_end; n++) {
1204*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, zht_part_data.data()[n]);
1205*d289c2baSAndroid Build Coastguard Worker }
1206*d289c2baSAndroid Build Coastguard Worker if (d.fec_size > 0) {
1207*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, strncmp(zht_part_data.data() + zht_fec_begin, "ZeRoHaSH", 8));
1208*d289c2baSAndroid Build Coastguard Worker for (size_t n = zht_fec_begin + 8; n < zht_fec_end; n++) {
1209*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, zht_part_data.data()[n]);
1210*d289c2baSAndroid Build Coastguard Worker }
1211*d289c2baSAndroid Build Coastguard Worker }
1212*d289c2baSAndroid Build Coastguard Worker
1213*d289c2baSAndroid Build Coastguard Worker // Check that we correctly generate dm-verity kernel cmdline
1214*d289c2baSAndroid Build Coastguard Worker // snippets, if requested.
1215*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_dmv_path =
1216*d289c2baSAndroid Build Coastguard Worker testdir_ / "vbmeta_dm_verity_desc.bin";
1217*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1218*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
1219*d289c2baSAndroid Build Coastguard Worker "--output %s "
1220*d289c2baSAndroid Build Coastguard Worker "--setup_rootfs_from_kernel %s "
1221*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
1222*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
1223*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
1224*d289c2baSAndroid Build Coastguard Worker vbmeta_dmv_path.c_str(),
1225*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str());
1226*d289c2baSAndroid Build Coastguard Worker
1227*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
1228*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
1229*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
1230*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
1231*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 896 bytes\n"
1232*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
1233*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
1234*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
1235*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
1236*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
1237*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
1238*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
1239*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline descriptor:\n"
1240*d289c2baSAndroid Build Coastguard Worker " Flags: 1\n"
1241*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline: 'dm=\"1 vroot none ro 1,0 2056 verity 1 "
1242*d289c2baSAndroid Build Coastguard Worker "PARTUUID=$(ANDROID_SYSTEM_PARTUUID) PARTUUID=$(ANDROID_SYSTEM_PARTUUID) "
1243*d289c2baSAndroid Build Coastguard Worker "4096 4096 257 257 sha1 e811611467dcd6e8dc4324e45f706c2bdd51db67 "
1244*d289c2baSAndroid Build Coastguard Worker "d00df00d 2 $(ANDROID_VERITY_MODE) ignore_zero_blocks\" root=/dev/dm-0'\n"
1245*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline descriptor:\n"
1246*d289c2baSAndroid Build Coastguard Worker " Flags: 2\n"
1247*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline: "
1248*d289c2baSAndroid Build Coastguard Worker "'root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)'\n",
1249*d289c2baSAndroid Build Coastguard Worker InfoImage(vbmeta_dmv_path.string()));
1250*d289c2baSAndroid Build Coastguard Worker
1251*d289c2baSAndroid Build Coastguard Worker // Check that the footer is correctly erased and the hashtree
1252*d289c2baSAndroid Build Coastguard Worker // remains - see above for why the constant 1069056 is used.
1253*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1254*d289c2baSAndroid Build Coastguard Worker "./avbtool.py erase_footer --image %s --keep_hashtree",
1255*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str());
1256*d289c2baSAndroid Build Coastguard Worker int64_t erased_footer_file_size;
1257*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(base::GetFileSize(base::FilePath(rootfs_path.c_str()),
1258*d289c2baSAndroid Build Coastguard Worker &erased_footer_file_size));
1259*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(static_cast<size_t>(erased_footer_file_size), 1069056UL);
1260*d289c2baSAndroid Build Coastguard Worker
1261*d289c2baSAndroid Build Coastguard Worker const size_t rootfs_size = 1028 * 1024;
1262*d289c2baSAndroid Build Coastguard Worker const size_t partition_size = 1536 * 1024;
1263*d289c2baSAndroid Build Coastguard Worker std::filesystem::path external_vbmeta_path = testdir_ / "external_vbmeta.bin";
1264*d289c2baSAndroid Build Coastguard Worker // Check that --do_not_append_vbmeta_image works as intended.
1265*d289c2baSAndroid Build Coastguard Worker //
1266*d289c2baSAndroid Build Coastguard Worker // For this we need to reset the size of the image to the original
1267*d289c2baSAndroid Build Coastguard Worker // size because it's not possible to identify the existing hashtree.
1268*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "truncate -s %d %s", (int)rootfs_size, rootfs_path.c_str());
1269*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1270*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d --image %s "
1271*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
1272*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
1273*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
1274*d289c2baSAndroid Build Coastguard Worker "--output_vbmeta %s_2nd_run --do_not_append_vbmeta_image "
1275*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" "
1276*d289c2baSAndroid Build Coastguard Worker "--do_not_generate_fec",
1277*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
1278*d289c2baSAndroid Build Coastguard Worker (int)partition_size,
1279*d289c2baSAndroid Build Coastguard Worker external_vbmeta_path.c_str());
1280*d289c2baSAndroid Build Coastguard Worker int64_t file_size;
1281*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(
1282*d289c2baSAndroid Build Coastguard Worker base::GetFileSize(base::FilePath(rootfs_path.c_str()), &file_size));
1283*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(static_cast<size_t>(file_size), 1069056UL);
1284*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1285*d289c2baSAndroid Build Coastguard Worker "diff %s %s_2nd_run",
1286*d289c2baSAndroid Build Coastguard Worker external_vbmeta_path.c_str(),
1287*d289c2baSAndroid Build Coastguard Worker external_vbmeta_path.c_str());
1288*d289c2baSAndroid Build Coastguard Worker }
1289*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooter)1290*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooter) {
1291*d289c2baSAndroid Build Coastguard Worker AddHashtreeFooterTest(false);
1292*d289c2baSAndroid Build Coastguard Worker }
1293*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterSparse)1294*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterSparse) {
1295*d289c2baSAndroid Build Coastguard Worker AddHashtreeFooterTest(true);
1296*d289c2baSAndroid Build Coastguard Worker }
1297*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterSparseWithBlake2b256)1298*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterSparseWithBlake2b256) {
1299*d289c2baSAndroid Build Coastguard Worker std::filesystem::path rootfs_path;
1300*d289c2baSAndroid Build Coastguard Worker CreateRootfsWithHashtreeFooter(
1301*d289c2baSAndroid Build Coastguard Worker true,
1302*d289c2baSAndroid Build Coastguard Worker "blake2b-256",
1303*d289c2baSAndroid Build Coastguard Worker "9ed423dda921619181bf1889746fe2dd28ae1e673be8d802b4713122e3209513",
1304*d289c2baSAndroid Build Coastguard Worker &rootfs_path);
1305*d289c2baSAndroid Build Coastguard Worker }
1306*d289c2baSAndroid Build Coastguard Worker
AddHashtreeFooterFECTest(bool sparse_image)1307*d289c2baSAndroid Build Coastguard Worker void AvbToolTest::AddHashtreeFooterFECTest(bool sparse_image) {
1308*d289c2baSAndroid Build Coastguard Worker const size_t rootfs_size = 1028 * 1024;
1309*d289c2baSAndroid Build Coastguard Worker const size_t partition_size = 1536 * 1024;
1310*d289c2baSAndroid Build Coastguard Worker
1311*d289c2baSAndroid Build Coastguard Worker // Generate a 1028 KiB file with known content.
1312*d289c2baSAndroid Build Coastguard Worker std::vector<uint8_t> rootfs;
1313*d289c2baSAndroid Build Coastguard Worker rootfs.resize(rootfs_size);
1314*d289c2baSAndroid Build Coastguard Worker for (size_t n = 0; n < rootfs_size; n++)
1315*d289c2baSAndroid Build Coastguard Worker rootfs[n] = uint8_t(n);
1316*d289c2baSAndroid Build Coastguard Worker std::filesystem::path rootfs_path = testdir_ / "rootfs.bin";
1317*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(rootfs_size,
1318*d289c2baSAndroid Build Coastguard Worker static_cast<const size_t>(
1319*d289c2baSAndroid Build Coastguard Worker base::WriteFile(base::FilePath(rootfs_path.c_str()),
1320*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const char*>(rootfs.data()),
1321*d289c2baSAndroid Build Coastguard Worker rootfs.size())));
1322*d289c2baSAndroid Build Coastguard Worker
1323*d289c2baSAndroid Build Coastguard Worker if (sparse_image) {
1324*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
1325*d289c2baSAndroid Build Coastguard Worker 0, "mv %s %s.unsparse", rootfs_path.c_str(), rootfs_path.c_str());
1326*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
1327*d289c2baSAndroid Build Coastguard Worker 0, "img2simg %s.unsparse %s", rootfs_path.c_str(), rootfs_path.c_str());
1328*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "rm -f %s.unsparse", rootfs_path.c_str());
1329*d289c2baSAndroid Build Coastguard Worker }
1330*d289c2baSAndroid Build Coastguard Worker
1331*d289c2baSAndroid Build Coastguard Worker /* Do this twice to check that 'add_hashtree_footer' is idempotent. */
1332*d289c2baSAndroid Build Coastguard Worker for (int n = 0; n < 2; n++) {
1333*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
1334*d289c2baSAndroid Build Coastguard Worker 0,
1335*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d --image %s "
1336*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
1337*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
1338*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
1339*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
1340*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
1341*d289c2baSAndroid Build Coastguard Worker (int)partition_size);
1342*d289c2baSAndroid Build Coastguard Worker
1343*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(android::base::StringPrintf(
1344*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
1345*d289c2baSAndroid Build Coastguard Worker "Image size: 1572864 bytes\n"
1346*d289c2baSAndroid Build Coastguard Worker "Original image size: 1052672 bytes\n"
1347*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 1085440\n"
1348*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1344 bytes\n"
1349*d289c2baSAndroid Build Coastguard Worker "--\n"
1350*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0%s\n"
1351*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
1352*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
1353*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 768 bytes\n"
1354*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): "
1355*d289c2baSAndroid Build Coastguard Worker "cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
1356*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
1357*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
1358*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
1359*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
1360*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
1361*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
1362*d289c2baSAndroid Build Coastguard Worker " Hashtree descriptor:\n"
1363*d289c2baSAndroid Build Coastguard Worker " Version of dm-verity: 1\n"
1364*d289c2baSAndroid Build Coastguard Worker " Image Size: 1052672 bytes\n"
1365*d289c2baSAndroid Build Coastguard Worker " Tree Offset: 1052672\n"
1366*d289c2baSAndroid Build Coastguard Worker " Tree Size: 16384 bytes\n"
1367*d289c2baSAndroid Build Coastguard Worker " Data Block Size: 4096 bytes\n"
1368*d289c2baSAndroid Build Coastguard Worker " Hash Block Size: 4096 bytes\n"
1369*d289c2baSAndroid Build Coastguard Worker " FEC num roots: 2\n"
1370*d289c2baSAndroid Build Coastguard Worker " FEC offset: 1069056\n"
1371*d289c2baSAndroid Build Coastguard Worker " FEC size: 16384 bytes\n"
1372*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha1\n"
1373*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
1374*d289c2baSAndroid Build Coastguard Worker " Salt: d00df00d\n"
1375*d289c2baSAndroid Build Coastguard Worker " Root Digest: "
1376*d289c2baSAndroid Build Coastguard Worker "e811611467dcd6e8dc4324e45f706c2bdd51db67\n"
1377*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n",
1378*d289c2baSAndroid Build Coastguard Worker sparse_image ? " (Sparse)" : ""),
1379*d289c2baSAndroid Build Coastguard Worker InfoImage(rootfs_path.string()));
1380*d289c2baSAndroid Build Coastguard Worker }
1381*d289c2baSAndroid Build Coastguard Worker
1382*d289c2baSAndroid Build Coastguard Worker /* Zero the hashtree and FEC on a copy of the image. */
1383*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "cp %s %s.zht", rootfs_path.c_str(), rootfs_path.c_str());
1384*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
1385*d289c2baSAndroid Build Coastguard Worker 0, "./avbtool.py zero_hashtree --image %s.zht ", rootfs_path.c_str());
1386*d289c2baSAndroid Build Coastguard Worker
1387*d289c2baSAndroid Build Coastguard Worker if (sparse_image) {
1388*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
1389*d289c2baSAndroid Build Coastguard Worker 0, "mv %s %s.sparse", rootfs_path.c_str(), rootfs_path.c_str());
1390*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
1391*d289c2baSAndroid Build Coastguard Worker 0, "simg2img %s.sparse %s", rootfs_path.c_str(), rootfs_path.c_str());
1392*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "rm -f %s.sparse", rootfs_path.c_str());
1393*d289c2baSAndroid Build Coastguard Worker
1394*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
1395*d289c2baSAndroid Build Coastguard Worker 0, "mv %s.zht %s.zht.sparse", rootfs_path.c_str(), rootfs_path.c_str());
1396*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1397*d289c2baSAndroid Build Coastguard Worker "simg2img %s.zht.sparse %s.zht",
1398*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
1399*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str());
1400*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "rm -f %s.zht.sparse", rootfs_path.c_str());
1401*d289c2baSAndroid Build Coastguard Worker }
1402*d289c2baSAndroid Build Coastguard Worker
1403*d289c2baSAndroid Build Coastguard Worker /* TODO: would be nice to verify that the FEC data is correct. */
1404*d289c2baSAndroid Build Coastguard Worker
1405*d289c2baSAndroid Build Coastguard Worker // Now check that we can find the VBMeta block again from the footer.
1406*d289c2baSAndroid Build Coastguard Worker std::string part_data;
1407*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(
1408*d289c2baSAndroid Build Coastguard Worker android::base::ReadFileToString(rootfs_path.string(), &part_data));
1409*d289c2baSAndroid Build Coastguard Worker
1410*d289c2baSAndroid Build Coastguard Worker // Also read the zeroed hash-tree version.
1411*d289c2baSAndroid Build Coastguard Worker std::string zht_part_data;
1412*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(base::ReadFileToString(
1413*d289c2baSAndroid Build Coastguard Worker base::FilePath(rootfs_path.string() + ".zht"), &zht_part_data));
1414*d289c2baSAndroid Build Coastguard Worker
1415*d289c2baSAndroid Build Coastguard Worker // Check footer contains correct data.
1416*d289c2baSAndroid Build Coastguard Worker AvbFooter f;
1417*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(0,
1418*d289c2baSAndroid Build Coastguard Worker avb_footer_validate_and_byteswap(
1419*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const AvbFooter*>(
1420*d289c2baSAndroid Build Coastguard Worker part_data.data() + part_data.size() - AVB_FOOTER_SIZE),
1421*d289c2baSAndroid Build Coastguard Worker &f));
1422*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
1423*d289c2baSAndroid Build Coastguard Worker std::string(reinterpret_cast<const char*>(f.magic), AVB_FOOTER_MAGIC_LEN),
1424*d289c2baSAndroid Build Coastguard Worker AVB_FOOTER_MAGIC);
1425*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_FOOTER_VERSION_MAJOR, (int)f.version_major);
1426*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_FOOTER_VERSION_MINOR, (int)f.version_minor);
1427*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1052672UL, f.original_image_size);
1428*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1085440UL, f.vbmeta_offset);
1429*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1344UL, f.vbmeta_size);
1430*d289c2baSAndroid Build Coastguard Worker
1431*d289c2baSAndroid Build Coastguard Worker // Check that the vbmeta image at |f.vbmeta_offset| checks out.
1432*d289c2baSAndroid Build Coastguard Worker const uint8_t* vbmeta_data =
1433*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const uint8_t*>(part_data.data() + f.vbmeta_offset);
1434*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_VBMETA_VERIFY_RESULT_OK,
1435*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_verify(vbmeta_data, f.vbmeta_size, NULL, NULL));
1436*d289c2baSAndroid Build Coastguard Worker
1437*d289c2baSAndroid Build Coastguard Worker // Collect all descriptors.
1438*d289c2baSAndroid Build Coastguard Worker std::vector<const AvbDescriptor*> descriptors;
1439*d289c2baSAndroid Build Coastguard Worker avb_descriptor_foreach(
1440*d289c2baSAndroid Build Coastguard Worker vbmeta_data, f.vbmeta_size, collect_descriptors, &descriptors);
1441*d289c2baSAndroid Build Coastguard Worker
1442*d289c2baSAndroid Build Coastguard Worker // We should only have a single descriptor and it should be a
1443*d289c2baSAndroid Build Coastguard Worker // hashtree descriptor.
1444*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1UL, descriptors.size());
1445*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_DESCRIPTOR_TAG_HASHTREE, avb_be64toh(descriptors[0]->tag));
1446*d289c2baSAndroid Build Coastguard Worker AvbHashtreeDescriptor d;
1447*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(
1448*d289c2baSAndroid Build Coastguard Worker 0,
1449*d289c2baSAndroid Build Coastguard Worker avb_hashtree_descriptor_validate_and_byteswap(
1450*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const AvbHashtreeDescriptor*>(descriptors[0]), &d));
1451*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1UL, d.dm_verity_version);
1452*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1052672UL, d.image_size);
1453*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1052672UL, d.tree_offset);
1454*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(16384UL, d.tree_size);
1455*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(4096UL, d.data_block_size);
1456*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(2UL, d.fec_num_roots);
1457*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1069056UL, d.fec_offset);
1458*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(16384UL, d.fec_size);
1459*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(6UL, d.partition_name_len);
1460*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(4UL, d.salt_len);
1461*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(20UL, d.root_digest_len);
1462*d289c2baSAndroid Build Coastguard Worker const uint8_t* desc_end = reinterpret_cast<const uint8_t*>(descriptors[0]) +
1463*d289c2baSAndroid Build Coastguard Worker sizeof(AvbHashtreeDescriptor);
1464*d289c2baSAndroid Build Coastguard Worker uint64_t o = 0;
1465*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("foobar",
1466*d289c2baSAndroid Build Coastguard Worker std::string(reinterpret_cast<const char*>(desc_end + o),
1467*d289c2baSAndroid Build Coastguard Worker d.partition_name_len));
1468*d289c2baSAndroid Build Coastguard Worker o += d.partition_name_len;
1469*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("d00df00d", mem_to_hexstring(desc_end + o, d.salt_len));
1470*d289c2baSAndroid Build Coastguard Worker o += d.salt_len;
1471*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("e811611467dcd6e8dc4324e45f706c2bdd51db67",
1472*d289c2baSAndroid Build Coastguard Worker mem_to_hexstring(desc_end + o, d.root_digest_len));
1473*d289c2baSAndroid Build Coastguard Worker
1474*d289c2baSAndroid Build Coastguard Worker // Check that the zeroed hashtree version differ only by the hashtree + fec
1475*d289c2baSAndroid Build Coastguard Worker // being zeroed out.
1476*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(part_data.size(), zht_part_data.size());
1477*d289c2baSAndroid Build Coastguard Worker size_t zht_ht_begin = d.tree_offset;
1478*d289c2baSAndroid Build Coastguard Worker size_t zht_ht_end = zht_ht_begin + d.tree_size;
1479*d289c2baSAndroid Build Coastguard Worker size_t zht_fec_begin = zht_ht_end;
1480*d289c2baSAndroid Build Coastguard Worker size_t zht_fec_end = zht_fec_begin + d.fec_size;
1481*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, memcmp(part_data.data(), zht_part_data.data(), zht_ht_begin));
1482*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(0,
1483*d289c2baSAndroid Build Coastguard Worker memcmp(part_data.data() + zht_ht_begin,
1484*d289c2baSAndroid Build Coastguard Worker zht_part_data.data() + zht_ht_begin,
1485*d289c2baSAndroid Build Coastguard Worker zht_fec_end - zht_ht_begin));
1486*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0,
1487*d289c2baSAndroid Build Coastguard Worker memcmp(part_data.data() + zht_fec_end,
1488*d289c2baSAndroid Build Coastguard Worker zht_part_data.data() + zht_fec_end,
1489*d289c2baSAndroid Build Coastguard Worker zht_part_data.size() - zht_fec_end));
1490*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, strncmp(zht_part_data.data() + zht_ht_begin, "ZeRoHaSH", 8));
1491*d289c2baSAndroid Build Coastguard Worker for (size_t n = zht_ht_begin + 8; n < zht_ht_end; n++) {
1492*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, zht_part_data.data()[n]);
1493*d289c2baSAndroid Build Coastguard Worker }
1494*d289c2baSAndroid Build Coastguard Worker if (d.fec_size > 0) {
1495*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, strncmp(zht_part_data.data() + zht_fec_begin, "ZeRoHaSH", 8));
1496*d289c2baSAndroid Build Coastguard Worker for (size_t n = zht_fec_begin + 8; n < zht_fec_end; n++) {
1497*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(0, zht_part_data.data()[n]);
1498*d289c2baSAndroid Build Coastguard Worker }
1499*d289c2baSAndroid Build Coastguard Worker }
1500*d289c2baSAndroid Build Coastguard Worker
1501*d289c2baSAndroid Build Coastguard Worker // Check that we correctly generate dm-verity kernel cmdline
1502*d289c2baSAndroid Build Coastguard Worker // snippets, if requested.
1503*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_dmv_path =
1504*d289c2baSAndroid Build Coastguard Worker testdir_ / "vbmeta_dm_verity_desc.bin";
1505*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1506*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
1507*d289c2baSAndroid Build Coastguard Worker "--output %s "
1508*d289c2baSAndroid Build Coastguard Worker "--setup_rootfs_from_kernel %s "
1509*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
1510*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
1511*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
1512*d289c2baSAndroid Build Coastguard Worker vbmeta_dmv_path.c_str(),
1513*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str());
1514*d289c2baSAndroid Build Coastguard Worker
1515*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
1516*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
1517*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
1518*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
1519*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 960 bytes\n"
1520*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
1521*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
1522*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
1523*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
1524*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
1525*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
1526*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
1527*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline descriptor:\n"
1528*d289c2baSAndroid Build Coastguard Worker " Flags: 1\n"
1529*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline: 'dm=\"1 vroot none ro 1,0 2056 verity 1 "
1530*d289c2baSAndroid Build Coastguard Worker "PARTUUID=$(ANDROID_SYSTEM_PARTUUID) PARTUUID=$(ANDROID_SYSTEM_PARTUUID) "
1531*d289c2baSAndroid Build Coastguard Worker "4096 4096 257 257 sha1 e811611467dcd6e8dc4324e45f706c2bdd51db67 "
1532*d289c2baSAndroid Build Coastguard Worker "d00df00d 10 $(ANDROID_VERITY_MODE) ignore_zero_blocks "
1533*d289c2baSAndroid Build Coastguard Worker "use_fec_from_device "
1534*d289c2baSAndroid Build Coastguard Worker "PARTUUID=$(ANDROID_SYSTEM_PARTUUID) fec_roots 2 fec_blocks 261 "
1535*d289c2baSAndroid Build Coastguard Worker "fec_start 261\" root=/dev/dm-0'\n"
1536*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline descriptor:\n"
1537*d289c2baSAndroid Build Coastguard Worker " Flags: 2\n"
1538*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline: "
1539*d289c2baSAndroid Build Coastguard Worker "'root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)'\n",
1540*d289c2baSAndroid Build Coastguard Worker InfoImage(vbmeta_dmv_path.string()));
1541*d289c2baSAndroid Build Coastguard Worker
1542*d289c2baSAndroid Build Coastguard Worker // Check that the footer is correctly erased and the hashtree and
1543*d289c2baSAndroid Build Coastguard Worker // FEC data remains. The constant 1085440 is used because it's where
1544*d289c2baSAndroid Build Coastguard Worker // the FEC data ends (it's at offset 1069056 and size 16384).
1545*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1546*d289c2baSAndroid Build Coastguard Worker "./avbtool.py erase_footer --image %s --keep_hashtree",
1547*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str());
1548*d289c2baSAndroid Build Coastguard Worker int64_t erased_footer_file_size;
1549*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(base::GetFileSize(base::FilePath(rootfs_path.c_str()),
1550*d289c2baSAndroid Build Coastguard Worker &erased_footer_file_size));
1551*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(static_cast<size_t>(erased_footer_file_size), 1085440UL);
1552*d289c2baSAndroid Build Coastguard Worker }
1553*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterFEC)1554*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterFEC) {
1555*d289c2baSAndroid Build Coastguard Worker AddHashtreeFooterFECTest(false);
1556*d289c2baSAndroid Build Coastguard Worker }
1557*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterFECSparse)1558*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterFECSparse) {
1559*d289c2baSAndroid Build Coastguard Worker AddHashtreeFooterFECTest(true);
1560*d289c2baSAndroid Build Coastguard Worker }
1561*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterCalcMaxImageSize)1562*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterCalcMaxImageSize) {
1563*d289c2baSAndroid Build Coastguard Worker const size_t partition_size = 10 * 1024 * 1024;
1564*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / "max_size.txt";
1565*d289c2baSAndroid Build Coastguard Worker
1566*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1567*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer "
1568*d289c2baSAndroid Build Coastguard Worker "--partition_size %zd --calc_max_image_size "
1569*d289c2baSAndroid Build Coastguard Worker "--do_not_generate_fec > %s",
1570*d289c2baSAndroid Build Coastguard Worker partition_size,
1571*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
1572*d289c2baSAndroid Build Coastguard Worker std::string max_image_size_data;
1573*d289c2baSAndroid Build Coastguard Worker EXPECT_TRUE(android::base::ReadFileToString(output_path.string(),
1574*d289c2baSAndroid Build Coastguard Worker &max_image_size_data));
1575*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("10330112\n", max_image_size_data);
1576*d289c2baSAndroid Build Coastguard Worker size_t max_image_size = atoll(max_image_size_data.c_str());
1577*d289c2baSAndroid Build Coastguard Worker
1578*d289c2baSAndroid Build Coastguard Worker // Hashtree and metadata takes up 152 KiB - compare to below with
1579*d289c2baSAndroid Build Coastguard Worker // FEC which is 244 KiB.
1580*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(152 * 1024ULL, partition_size - max_image_size);
1581*d289c2baSAndroid Build Coastguard Worker
1582*d289c2baSAndroid Build Coastguard Worker // Check that we can add a hashtree with an image this size for such
1583*d289c2baSAndroid Build Coastguard Worker // a partition size.
1584*d289c2baSAndroid Build Coastguard Worker std::string system_path = GenerateImage("system", max_image_size);
1585*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1586*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer"
1587*d289c2baSAndroid Build Coastguard Worker " --image %s"
1588*d289c2baSAndroid Build Coastguard Worker " --partition_name system"
1589*d289c2baSAndroid Build Coastguard Worker " --partition_size %zd"
1590*d289c2baSAndroid Build Coastguard Worker " --salt deadbeef"
1591*d289c2baSAndroid Build Coastguard Worker " --algorithm SHA512_RSA4096 "
1592*d289c2baSAndroid Build Coastguard Worker " --key test/data/testkey_rsa4096.pem"
1593*d289c2baSAndroid Build Coastguard Worker " --internal_release_string \"\" "
1594*d289c2baSAndroid Build Coastguard Worker "--do_not_generate_fec",
1595*d289c2baSAndroid Build Coastguard Worker system_path.c_str(),
1596*d289c2baSAndroid Build Coastguard Worker partition_size);
1597*d289c2baSAndroid Build Coastguard Worker }
1598*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterCalcMaxImageSizeWithFEC)1599*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterCalcMaxImageSizeWithFEC) {
1600*d289c2baSAndroid Build Coastguard Worker const size_t partition_size = 10 * 1024 * 1024;
1601*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / "max_size.txt";
1602*d289c2baSAndroid Build Coastguard Worker
1603*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1604*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer "
1605*d289c2baSAndroid Build Coastguard Worker "--partition_size %zd --calc_max_image_size > %s",
1606*d289c2baSAndroid Build Coastguard Worker partition_size,
1607*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
1608*d289c2baSAndroid Build Coastguard Worker std::string max_image_size_data;
1609*d289c2baSAndroid Build Coastguard Worker EXPECT_TRUE(android::base::ReadFileToString(output_path.string(),
1610*d289c2baSAndroid Build Coastguard Worker &max_image_size_data));
1611*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("10235904\n", max_image_size_data);
1612*d289c2baSAndroid Build Coastguard Worker size_t max_image_size = atoll(max_image_size_data.c_str());
1613*d289c2baSAndroid Build Coastguard Worker
1614*d289c2baSAndroid Build Coastguard Worker // Hashtree, FEC codes, and metadata takes up 244 KiB - compare to
1615*d289c2baSAndroid Build Coastguard Worker // above wihtout FEC which is 152 KiB.
1616*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(244 * 1024ULL, partition_size - max_image_size);
1617*d289c2baSAndroid Build Coastguard Worker
1618*d289c2baSAndroid Build Coastguard Worker // Check that we can add a hashtree with an image this size for such
1619*d289c2baSAndroid Build Coastguard Worker // a partition size.
1620*d289c2baSAndroid Build Coastguard Worker std::string system_path = GenerateImage("system", max_image_size);
1621*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1622*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer"
1623*d289c2baSAndroid Build Coastguard Worker " --image %s"
1624*d289c2baSAndroid Build Coastguard Worker " --partition_name system"
1625*d289c2baSAndroid Build Coastguard Worker " --partition_size %zd"
1626*d289c2baSAndroid Build Coastguard Worker " --salt deadbeef"
1627*d289c2baSAndroid Build Coastguard Worker " --algorithm SHA512_RSA4096 "
1628*d289c2baSAndroid Build Coastguard Worker " --key test/data/testkey_rsa4096.pem"
1629*d289c2baSAndroid Build Coastguard Worker " --internal_release_string \"\"",
1630*d289c2baSAndroid Build Coastguard Worker system_path.c_str(),
1631*d289c2baSAndroid Build Coastguard Worker partition_size);
1632*d289c2baSAndroid Build Coastguard Worker }
1633*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterCalcMaxImageSizeWithNoHashtree)1634*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterCalcMaxImageSizeWithNoHashtree) {
1635*d289c2baSAndroid Build Coastguard Worker const size_t partition_size = 10 * 1024 * 1024;
1636*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / "max_size.txt";
1637*d289c2baSAndroid Build Coastguard Worker
1638*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1639*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer "
1640*d289c2baSAndroid Build Coastguard Worker "--no_hashtree "
1641*d289c2baSAndroid Build Coastguard Worker "--partition_size %zd --calc_max_image_size > %s",
1642*d289c2baSAndroid Build Coastguard Worker partition_size,
1643*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
1644*d289c2baSAndroid Build Coastguard Worker std::string max_image_size_data;
1645*d289c2baSAndroid Build Coastguard Worker EXPECT_TRUE(android::base::ReadFileToString(output_path.string(),
1646*d289c2baSAndroid Build Coastguard Worker &max_image_size_data));
1647*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("10416128\n", max_image_size_data);
1648*d289c2baSAndroid Build Coastguard Worker size_t max_image_size = atoll(max_image_size_data.c_str());
1649*d289c2baSAndroid Build Coastguard Worker
1650*d289c2baSAndroid Build Coastguard Worker // vbmeta(64) + footer(4) takes up 68 KiB
1651*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(68 * 1024ULL, partition_size - max_image_size);
1652*d289c2baSAndroid Build Coastguard Worker
1653*d289c2baSAndroid Build Coastguard Worker // Check that we can add a hashtree with an image this size for such
1654*d289c2baSAndroid Build Coastguard Worker // a partition size.
1655*d289c2baSAndroid Build Coastguard Worker std::string system_path = GenerateImage("system", max_image_size);
1656*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1657*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer"
1658*d289c2baSAndroid Build Coastguard Worker " --image %s"
1659*d289c2baSAndroid Build Coastguard Worker " --no_hashtree"
1660*d289c2baSAndroid Build Coastguard Worker " --partition_name system"
1661*d289c2baSAndroid Build Coastguard Worker " --partition_size %zd"
1662*d289c2baSAndroid Build Coastguard Worker " --salt deadbeef"
1663*d289c2baSAndroid Build Coastguard Worker " --algorithm SHA512_RSA4096 "
1664*d289c2baSAndroid Build Coastguard Worker " --key test/data/testkey_rsa4096.pem"
1665*d289c2baSAndroid Build Coastguard Worker " --internal_release_string \"\"",
1666*d289c2baSAndroid Build Coastguard Worker system_path.c_str(),
1667*d289c2baSAndroid Build Coastguard Worker partition_size);
1668*d289c2baSAndroid Build Coastguard Worker // with --no_hashtree, Tree/FEC sizes are 0 bytes
1669*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
1670*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
1671*d289c2baSAndroid Build Coastguard Worker "Image size: 10485760 bytes\n"
1672*d289c2baSAndroid Build Coastguard Worker "Original image size: 10416128 bytes\n"
1673*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 10416128\n"
1674*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 2112 bytes\n"
1675*d289c2baSAndroid Build Coastguard Worker "--\n"
1676*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
1677*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
1678*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 576 bytes\n"
1679*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 1280 bytes\n"
1680*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): 2597c218aae470a130f61162feaae70afd97f011\n"
1681*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA512_RSA4096\n"
1682*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
1683*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
1684*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
1685*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
1686*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
1687*d289c2baSAndroid Build Coastguard Worker " Hashtree descriptor:\n"
1688*d289c2baSAndroid Build Coastguard Worker " Version of dm-verity: 1\n"
1689*d289c2baSAndroid Build Coastguard Worker " Image Size: 10416128 bytes\n"
1690*d289c2baSAndroid Build Coastguard Worker " Tree Offset: 10416128\n"
1691*d289c2baSAndroid Build Coastguard Worker " Tree Size: 0 bytes\n"
1692*d289c2baSAndroid Build Coastguard Worker " Data Block Size: 4096 bytes\n"
1693*d289c2baSAndroid Build Coastguard Worker " Hash Block Size: 4096 bytes\n"
1694*d289c2baSAndroid Build Coastguard Worker " FEC num roots: 2\n"
1695*d289c2baSAndroid Build Coastguard Worker " FEC offset: 10416128\n"
1696*d289c2baSAndroid Build Coastguard Worker " FEC size: 0 bytes\n"
1697*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha1\n"
1698*d289c2baSAndroid Build Coastguard Worker " Partition Name: system\n"
1699*d289c2baSAndroid Build Coastguard Worker " Salt: deadbeef\n"
1700*d289c2baSAndroid Build Coastguard Worker " Root Digest: 4215bd42bcc99636f42956ce3d2c7884d6a8093b\n"
1701*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n",
1702*d289c2baSAndroid Build Coastguard Worker InfoImage(system_path));
1703*d289c2baSAndroid Build Coastguard Worker }
1704*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterWithPersistentDigest)1705*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterWithPersistentDigest) {
1706*d289c2baSAndroid Build Coastguard Worker size_t partition_size = 10 * 1024 * 1024;
1707*d289c2baSAndroid Build Coastguard Worker std::string path = GenerateImage("digest_location", partition_size / 2);
1708*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1709*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer "
1710*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm sha256 --image %s "
1711*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
1712*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
1713*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
1714*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" "
1715*d289c2baSAndroid Build Coastguard Worker "--use_persistent_digest",
1716*d289c2baSAndroid Build Coastguard Worker path.c_str(),
1717*d289c2baSAndroid Build Coastguard Worker (int)partition_size);
1718*d289c2baSAndroid Build Coastguard Worker // There are two important bits here specific to --use_persistent_digest:
1719*d289c2baSAndroid Build Coastguard Worker // Minimum libavb version = 1.1
1720*d289c2baSAndroid Build Coastguard Worker // Hashtree descriptor -> Root Digest = (empty)
1721*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
1722*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
1723*d289c2baSAndroid Build Coastguard Worker "Image size: 10485760 bytes\n"
1724*d289c2baSAndroid Build Coastguard Worker "Original image size: 5242880 bytes\n"
1725*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 5337088\n"
1726*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1344 bytes\n"
1727*d289c2baSAndroid Build Coastguard Worker "--\n"
1728*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.1\n"
1729*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
1730*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
1731*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 768 bytes\n"
1732*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
1733*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
1734*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
1735*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
1736*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
1737*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
1738*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
1739*d289c2baSAndroid Build Coastguard Worker " Hashtree descriptor:\n"
1740*d289c2baSAndroid Build Coastguard Worker " Version of dm-verity: 1\n"
1741*d289c2baSAndroid Build Coastguard Worker " Image Size: 5242880 bytes\n"
1742*d289c2baSAndroid Build Coastguard Worker " Tree Offset: 5242880\n"
1743*d289c2baSAndroid Build Coastguard Worker " Tree Size: 45056 bytes\n"
1744*d289c2baSAndroid Build Coastguard Worker " Data Block Size: 4096 bytes\n"
1745*d289c2baSAndroid Build Coastguard Worker " Hash Block Size: 4096 bytes\n"
1746*d289c2baSAndroid Build Coastguard Worker " FEC num roots: 2\n"
1747*d289c2baSAndroid Build Coastguard Worker " FEC offset: 5287936\n"
1748*d289c2baSAndroid Build Coastguard Worker " FEC size: 49152 bytes\n"
1749*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha256\n"
1750*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
1751*d289c2baSAndroid Build Coastguard Worker " Salt: \n"
1752*d289c2baSAndroid Build Coastguard Worker " Root Digest: \n"
1753*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n",
1754*d289c2baSAndroid Build Coastguard Worker InfoImage(path));
1755*d289c2baSAndroid Build Coastguard Worker }
1756*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterWithNoAB)1757*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterWithNoAB) {
1758*d289c2baSAndroid Build Coastguard Worker size_t partition_size = 10 * 1024 * 1024;
1759*d289c2baSAndroid Build Coastguard Worker std::string path = GenerateImage("digest_location", partition_size / 2);
1760*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1761*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d "
1762*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm sha256 --image %s "
1763*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
1764*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
1765*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
1766*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" "
1767*d289c2baSAndroid Build Coastguard Worker "--do_not_use_ab",
1768*d289c2baSAndroid Build Coastguard Worker path.c_str(),
1769*d289c2baSAndroid Build Coastguard Worker (int)partition_size);
1770*d289c2baSAndroid Build Coastguard Worker // There are two important bits here we're expecting with --do_not_use_ab:
1771*d289c2baSAndroid Build Coastguard Worker // Minimum libavb version = 1.1
1772*d289c2baSAndroid Build Coastguard Worker // Hashtree descriptor -> Flags = 1
1773*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
1774*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
1775*d289c2baSAndroid Build Coastguard Worker "Image size: 10485760 bytes\n"
1776*d289c2baSAndroid Build Coastguard Worker "Original image size: 5242880 bytes\n"
1777*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 5337088\n"
1778*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1344 bytes\n"
1779*d289c2baSAndroid Build Coastguard Worker "--\n"
1780*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.1\n"
1781*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
1782*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
1783*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 768 bytes\n"
1784*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
1785*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
1786*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
1787*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
1788*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
1789*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
1790*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
1791*d289c2baSAndroid Build Coastguard Worker " Hashtree descriptor:\n"
1792*d289c2baSAndroid Build Coastguard Worker " Version of dm-verity: 1\n"
1793*d289c2baSAndroid Build Coastguard Worker " Image Size: 5242880 bytes\n"
1794*d289c2baSAndroid Build Coastguard Worker " Tree Offset: 5242880\n"
1795*d289c2baSAndroid Build Coastguard Worker " Tree Size: 45056 bytes\n"
1796*d289c2baSAndroid Build Coastguard Worker " Data Block Size: 4096 bytes\n"
1797*d289c2baSAndroid Build Coastguard Worker " Hash Block Size: 4096 bytes\n"
1798*d289c2baSAndroid Build Coastguard Worker " FEC num roots: 2\n"
1799*d289c2baSAndroid Build Coastguard Worker " FEC offset: 5287936\n"
1800*d289c2baSAndroid Build Coastguard Worker " FEC size: 49152 bytes\n"
1801*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha256\n"
1802*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
1803*d289c2baSAndroid Build Coastguard Worker " Salt: d00df00d\n"
1804*d289c2baSAndroid Build Coastguard Worker " Root Digest: "
1805*d289c2baSAndroid Build Coastguard Worker "d0e31526f5a3f8e3f59acf726bd31ae7861ee78f9baa9195356bf479c6f9119d\n"
1806*d289c2baSAndroid Build Coastguard Worker " Flags: 1\n",
1807*d289c2baSAndroid Build Coastguard Worker InfoImage(path));
1808*d289c2baSAndroid Build Coastguard Worker }
1809*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterWithPersistentDigestAndNoAB)1810*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterWithPersistentDigestAndNoAB) {
1811*d289c2baSAndroid Build Coastguard Worker size_t partition_size = 10 * 1024 * 1024;
1812*d289c2baSAndroid Build Coastguard Worker std::string path = GenerateImage("digest_location", partition_size / 2);
1813*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1814*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer "
1815*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm sha256 --image %s "
1816*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
1817*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
1818*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
1819*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" "
1820*d289c2baSAndroid Build Coastguard Worker "--use_persistent_digest --do_not_use_ab",
1821*d289c2baSAndroid Build Coastguard Worker path.c_str(),
1822*d289c2baSAndroid Build Coastguard Worker (int)partition_size);
1823*d289c2baSAndroid Build Coastguard Worker // There are three important bits specific to these flags:
1824*d289c2baSAndroid Build Coastguard Worker // Minimum libavb version = 1.1
1825*d289c2baSAndroid Build Coastguard Worker // Hashtree descriptor -> Root Digest = (empty)
1826*d289c2baSAndroid Build Coastguard Worker // Hashtree descriptor -> Flags = 1
1827*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
1828*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
1829*d289c2baSAndroid Build Coastguard Worker "Image size: 10485760 bytes\n"
1830*d289c2baSAndroid Build Coastguard Worker "Original image size: 5242880 bytes\n"
1831*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 5337088\n"
1832*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1344 bytes\n"
1833*d289c2baSAndroid Build Coastguard Worker "--\n"
1834*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.1\n"
1835*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
1836*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
1837*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 768 bytes\n"
1838*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
1839*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
1840*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
1841*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
1842*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
1843*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
1844*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
1845*d289c2baSAndroid Build Coastguard Worker " Hashtree descriptor:\n"
1846*d289c2baSAndroid Build Coastguard Worker " Version of dm-verity: 1\n"
1847*d289c2baSAndroid Build Coastguard Worker " Image Size: 5242880 bytes\n"
1848*d289c2baSAndroid Build Coastguard Worker " Tree Offset: 5242880\n"
1849*d289c2baSAndroid Build Coastguard Worker " Tree Size: 45056 bytes\n"
1850*d289c2baSAndroid Build Coastguard Worker " Data Block Size: 4096 bytes\n"
1851*d289c2baSAndroid Build Coastguard Worker " Hash Block Size: 4096 bytes\n"
1852*d289c2baSAndroid Build Coastguard Worker " FEC num roots: 2\n"
1853*d289c2baSAndroid Build Coastguard Worker " FEC offset: 5287936\n"
1854*d289c2baSAndroid Build Coastguard Worker " FEC size: 49152 bytes\n"
1855*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha256\n"
1856*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
1857*d289c2baSAndroid Build Coastguard Worker " Salt: \n"
1858*d289c2baSAndroid Build Coastguard Worker " Root Digest: \n"
1859*d289c2baSAndroid Build Coastguard Worker " Flags: 1\n",
1860*d289c2baSAndroid Build Coastguard Worker InfoImage(path));
1861*d289c2baSAndroid Build Coastguard Worker }
1862*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterNoSizeOrName)1863*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterNoSizeOrName) {
1864*d289c2baSAndroid Build Coastguard Worker // Size must be a multiple of block size (4096 bytes)
1865*d289c2baSAndroid Build Coastguard Worker size_t file_size = 72 * 1024;
1866*d289c2baSAndroid Build Coastguard Worker std::string path = GenerateImage("data.bin", file_size);
1867*d289c2baSAndroid Build Coastguard Worker
1868*d289c2baSAndroid Build Coastguard Worker // Note how there is no --partition_size or --partition_name here.
1869*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1870*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d "
1871*d289c2baSAndroid Build Coastguard Worker "--image %s "
1872*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
1873*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
1874*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" ",
1875*d289c2baSAndroid Build Coastguard Worker path.c_str());
1876*d289c2baSAndroid Build Coastguard Worker
1877*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
1878*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
1879*d289c2baSAndroid Build Coastguard Worker "Image size: 94208 bytes\n"
1880*d289c2baSAndroid Build Coastguard Worker "Original image size: 73728 bytes\n"
1881*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 86016\n"
1882*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1344 bytes\n"
1883*d289c2baSAndroid Build Coastguard Worker "--\n"
1884*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
1885*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
1886*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
1887*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 768 bytes\n"
1888*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
1889*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
1890*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
1891*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
1892*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
1893*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
1894*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
1895*d289c2baSAndroid Build Coastguard Worker " Hashtree descriptor:\n"
1896*d289c2baSAndroid Build Coastguard Worker " Version of dm-verity: 1\n"
1897*d289c2baSAndroid Build Coastguard Worker " Image Size: 73728 bytes\n"
1898*d289c2baSAndroid Build Coastguard Worker " Tree Offset: 73728\n"
1899*d289c2baSAndroid Build Coastguard Worker " Tree Size: 4096 bytes\n"
1900*d289c2baSAndroid Build Coastguard Worker " Data Block Size: 4096 bytes\n"
1901*d289c2baSAndroid Build Coastguard Worker " Hash Block Size: 4096 bytes\n"
1902*d289c2baSAndroid Build Coastguard Worker " FEC num roots: 2\n"
1903*d289c2baSAndroid Build Coastguard Worker " FEC offset: 77824\n"
1904*d289c2baSAndroid Build Coastguard Worker " FEC size: 8192 bytes\n"
1905*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha1\n"
1906*d289c2baSAndroid Build Coastguard Worker " Partition Name: \n"
1907*d289c2baSAndroid Build Coastguard Worker " Salt: d00df00d\n"
1908*d289c2baSAndroid Build Coastguard Worker " Root Digest: 2f73fb340e982794643e1121d82d5195677c2b31\n"
1909*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n",
1910*d289c2baSAndroid Build Coastguard Worker InfoImage(path));
1911*d289c2baSAndroid Build Coastguard Worker
1912*d289c2baSAndroid Build Coastguard Worker // Check that at least avbtool can verify the image and hashtree.
1913*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1914*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
1915*d289c2baSAndroid Build Coastguard Worker "--image %s ",
1916*d289c2baSAndroid Build Coastguard Worker path.c_str());
1917*d289c2baSAndroid Build Coastguard Worker }
1918*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterSingleBlock)1919*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterSingleBlock) {
1920*d289c2baSAndroid Build Coastguard Worker // Tests a special case that the file size is just one block.
1921*d289c2baSAndroid Build Coastguard Worker size_t file_size = 4096;
1922*d289c2baSAndroid Build Coastguard Worker std::string path = GenerateImage("data.bin", file_size);
1923*d289c2baSAndroid Build Coastguard Worker
1924*d289c2baSAndroid Build Coastguard Worker // Note how there is no --partition_size or --partition_name here.
1925*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1926*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d "
1927*d289c2baSAndroid Build Coastguard Worker "--image %s "
1928*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
1929*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
1930*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" ",
1931*d289c2baSAndroid Build Coastguard Worker path.c_str());
1932*d289c2baSAndroid Build Coastguard Worker
1933*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
1934*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
1935*d289c2baSAndroid Build Coastguard Worker "Image size: 20480 bytes\n"
1936*d289c2baSAndroid Build Coastguard Worker "Original image size: 4096 bytes\n"
1937*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 12288\n"
1938*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1344 bytes\n"
1939*d289c2baSAndroid Build Coastguard Worker "--\n"
1940*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
1941*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
1942*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
1943*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 768 bytes\n"
1944*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
1945*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
1946*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
1947*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
1948*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
1949*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
1950*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
1951*d289c2baSAndroid Build Coastguard Worker " Hashtree descriptor:\n"
1952*d289c2baSAndroid Build Coastguard Worker " Version of dm-verity: 1\n"
1953*d289c2baSAndroid Build Coastguard Worker " Image Size: 4096 bytes\n"
1954*d289c2baSAndroid Build Coastguard Worker " Tree Offset: 4096\n"
1955*d289c2baSAndroid Build Coastguard Worker " Tree Size: 0 bytes\n"
1956*d289c2baSAndroid Build Coastguard Worker " Data Block Size: 4096 bytes\n"
1957*d289c2baSAndroid Build Coastguard Worker " Hash Block Size: 4096 bytes\n"
1958*d289c2baSAndroid Build Coastguard Worker " FEC num roots: 2\n"
1959*d289c2baSAndroid Build Coastguard Worker " FEC offset: 4096\n"
1960*d289c2baSAndroid Build Coastguard Worker " FEC size: 8192 bytes\n"
1961*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha1\n"
1962*d289c2baSAndroid Build Coastguard Worker " Partition Name: \n"
1963*d289c2baSAndroid Build Coastguard Worker " Salt: d00df00d\n"
1964*d289c2baSAndroid Build Coastguard Worker " Root Digest: 4bd1e1f0aa1c2c793bb9f3e52de6ae7393889e61\n"
1965*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n",
1966*d289c2baSAndroid Build Coastguard Worker InfoImage(path));
1967*d289c2baSAndroid Build Coastguard Worker
1968*d289c2baSAndroid Build Coastguard Worker // Check that at least avbtool can verify the image and hashtree.
1969*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
1970*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
1971*d289c2baSAndroid Build Coastguard Worker "--image %s ",
1972*d289c2baSAndroid Build Coastguard Worker path.c_str());
1973*d289c2baSAndroid Build Coastguard Worker }
1974*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterNoSizeWrongSize)1975*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterNoSizeWrongSize) {
1976*d289c2baSAndroid Build Coastguard Worker // Size must be a multiple of block size (4096 bytes) and this one isn't...
1977*d289c2baSAndroid Build Coastguard Worker size_t file_size = 70 * 1024;
1978*d289c2baSAndroid Build Coastguard Worker std::string path = GenerateImage("data.bin", file_size);
1979*d289c2baSAndroid Build Coastguard Worker
1980*d289c2baSAndroid Build Coastguard Worker // ... so we expect this command to fail.
1981*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(1,
1982*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d "
1983*d289c2baSAndroid Build Coastguard Worker "--image %s "
1984*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
1985*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
1986*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" ",
1987*d289c2baSAndroid Build Coastguard Worker path.c_str());
1988*d289c2baSAndroid Build Coastguard Worker }
1989*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterRoundImageSize)1990*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterRoundImageSize) {
1991*d289c2baSAndroid Build Coastguard Worker // Image size needs not to be a multiple of block size (4096 bytes) if
1992*d289c2baSAndroid Build Coastguard Worker // --partition_size is specified. avbtool will round the image size being
1993*d289c2baSAndroid Build Coastguard Worker // a multiple of block size, prior to add an AVB footer.
1994*d289c2baSAndroid Build Coastguard Worker size_t image_size = 70 * 1024;
1995*d289c2baSAndroid Build Coastguard Worker std::string path = GenerateImage("data.bin", image_size);
1996*d289c2baSAndroid Build Coastguard Worker
1997*d289c2baSAndroid Build Coastguard Worker size_t partition_size = 10 * 1024 * 1024;
1998*d289c2baSAndroid Build Coastguard Worker // Note that there is --partition_size here.
1999*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2000*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d "
2001*d289c2baSAndroid Build Coastguard Worker "--image %s "
2002*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
2003*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
2004*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
2005*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" ",
2006*d289c2baSAndroid Build Coastguard Worker path.c_str(),
2007*d289c2baSAndroid Build Coastguard Worker (int)partition_size);
2008*d289c2baSAndroid Build Coastguard Worker }
2009*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterNoWrongPartitionSize)2010*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterNoWrongPartitionSize) {
2011*d289c2baSAndroid Build Coastguard Worker // Partition size must be a multiple of block size (4096 bytes) and this
2012*d289c2baSAndroid Build Coastguard Worker // one isn't...
2013*d289c2baSAndroid Build Coastguard Worker size_t partition_size = 10 * 1024 * 1024 + 1024;
2014*d289c2baSAndroid Build Coastguard Worker
2015*d289c2baSAndroid Build Coastguard Worker // Image size doesn't matter in this case.
2016*d289c2baSAndroid Build Coastguard Worker size_t image_size = 70 * 1024;
2017*d289c2baSAndroid Build Coastguard Worker std::string path = GenerateImage("data.bin", image_size);
2018*d289c2baSAndroid Build Coastguard Worker
2019*d289c2baSAndroid Build Coastguard Worker // ... so we expect this command to fail.
2020*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(1,
2021*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d "
2022*d289c2baSAndroid Build Coastguard Worker "--image %s "
2023*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
2024*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
2025*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
2026*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" ",
2027*d289c2baSAndroid Build Coastguard Worker path.c_str(),
2028*d289c2baSAndroid Build Coastguard Worker (int)partition_size);
2029*d289c2baSAndroid Build Coastguard Worker }
2030*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashtreeFooterWithCheckAtMostOnce)2031*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashtreeFooterWithCheckAtMostOnce) {
2032*d289c2baSAndroid Build Coastguard Worker size_t partition_size = 10 * 1024 * 1024;
2033*d289c2baSAndroid Build Coastguard Worker std::string path = GenerateImage("digest_location", partition_size / 2);
2034*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2035*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d "
2036*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm sha256 --image %s "
2037*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
2038*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
2039*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
2040*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" "
2041*d289c2baSAndroid Build Coastguard Worker "--check_at_most_once",
2042*d289c2baSAndroid Build Coastguard Worker path.c_str(),
2043*d289c2baSAndroid Build Coastguard Worker (int)partition_size);
2044*d289c2baSAndroid Build Coastguard Worker // There are two important bits here we're expecting with --check_at_most_once:
2045*d289c2baSAndroid Build Coastguard Worker // Minimum libavb version = 1.1
2046*d289c2baSAndroid Build Coastguard Worker // Hashtree descriptor -> Flags = 2
2047*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
2048*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
2049*d289c2baSAndroid Build Coastguard Worker "Image size: 10485760 bytes\n"
2050*d289c2baSAndroid Build Coastguard Worker "Original image size: 5242880 bytes\n"
2051*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 5337088\n"
2052*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1344 bytes\n"
2053*d289c2baSAndroid Build Coastguard Worker "--\n"
2054*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.1\n"
2055*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
2056*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
2057*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 768 bytes\n"
2058*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
2059*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
2060*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
2061*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
2062*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
2063*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
2064*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
2065*d289c2baSAndroid Build Coastguard Worker " Hashtree descriptor:\n"
2066*d289c2baSAndroid Build Coastguard Worker " Version of dm-verity: 1\n"
2067*d289c2baSAndroid Build Coastguard Worker " Image Size: 5242880 bytes\n"
2068*d289c2baSAndroid Build Coastguard Worker " Tree Offset: 5242880\n"
2069*d289c2baSAndroid Build Coastguard Worker " Tree Size: 45056 bytes\n"
2070*d289c2baSAndroid Build Coastguard Worker " Data Block Size: 4096 bytes\n"
2071*d289c2baSAndroid Build Coastguard Worker " Hash Block Size: 4096 bytes\n"
2072*d289c2baSAndroid Build Coastguard Worker " FEC num roots: 2\n"
2073*d289c2baSAndroid Build Coastguard Worker " FEC offset: 5287936\n"
2074*d289c2baSAndroid Build Coastguard Worker " FEC size: 49152 bytes\n"
2075*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha256\n"
2076*d289c2baSAndroid Build Coastguard Worker " Partition Name: foobar\n"
2077*d289c2baSAndroid Build Coastguard Worker " Salt: d00df00d\n"
2078*d289c2baSAndroid Build Coastguard Worker " Root Digest: "
2079*d289c2baSAndroid Build Coastguard Worker "d0e31526f5a3f8e3f59acf726bd31ae7861ee78f9baa9195356bf479c6f9119d\n"
2080*d289c2baSAndroid Build Coastguard Worker " Flags: 2\n",
2081*d289c2baSAndroid Build Coastguard Worker InfoImage(path));
2082*d289c2baSAndroid Build Coastguard Worker }
2083*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,KernelCmdlineDescriptor)2084*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, KernelCmdlineDescriptor) {
2085*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_path =
2086*d289c2baSAndroid Build Coastguard Worker testdir_ / "vbmeta_kernel_cmdline_desc.bin";
2087*d289c2baSAndroid Build Coastguard Worker
2088*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2089*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
2090*d289c2baSAndroid Build Coastguard Worker "--output %s "
2091*d289c2baSAndroid Build Coastguard Worker "--kernel_cmdline 'foo bar baz' "
2092*d289c2baSAndroid Build Coastguard Worker "--kernel_cmdline 'second cmdline' "
2093*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
2094*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
2095*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2096*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str());
2097*d289c2baSAndroid Build Coastguard Worker
2098*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
2099*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
2100*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
2101*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
2102*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 640 bytes\n"
2103*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
2104*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
2105*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
2106*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
2107*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
2108*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
2109*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
2110*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline descriptor:\n"
2111*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n"
2112*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline: 'foo bar baz'\n"
2113*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline descriptor:\n"
2114*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n"
2115*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline: 'second cmdline'\n",
2116*d289c2baSAndroid Build Coastguard Worker InfoImage(vbmeta_path.string()));
2117*d289c2baSAndroid Build Coastguard Worker
2118*d289c2baSAndroid Build Coastguard Worker // Now check the VBMeta image.
2119*d289c2baSAndroid Build Coastguard Worker std::string image_data;
2120*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(
2121*d289c2baSAndroid Build Coastguard Worker android::base::ReadFileToString(vbmeta_path.string(), &image_data));
2122*d289c2baSAndroid Build Coastguard Worker
2123*d289c2baSAndroid Build Coastguard Worker const uint8_t* vbmeta_data =
2124*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const uint8_t*>(image_data.data());
2125*d289c2baSAndroid Build Coastguard Worker const size_t vbmeta_size = image_data.length();
2126*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_VBMETA_VERIFY_RESULT_OK,
2127*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_verify(vbmeta_data, vbmeta_size, NULL, NULL));
2128*d289c2baSAndroid Build Coastguard Worker
2129*d289c2baSAndroid Build Coastguard Worker // Collect all descriptors.
2130*d289c2baSAndroid Build Coastguard Worker std::vector<const AvbDescriptor*> descriptors;
2131*d289c2baSAndroid Build Coastguard Worker avb_descriptor_foreach(
2132*d289c2baSAndroid Build Coastguard Worker vbmeta_data, vbmeta_size, collect_descriptors, &descriptors);
2133*d289c2baSAndroid Build Coastguard Worker
2134*d289c2baSAndroid Build Coastguard Worker // We should have two descriptors - check them.
2135*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(2UL, descriptors.size());
2136*d289c2baSAndroid Build Coastguard Worker AvbKernelCmdlineDescriptor d;
2137*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE,
2138*d289c2baSAndroid Build Coastguard Worker avb_be64toh(descriptors[0]->tag));
2139*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(
2140*d289c2baSAndroid Build Coastguard Worker 0,
2141*d289c2baSAndroid Build Coastguard Worker avb_kernel_cmdline_descriptor_validate_and_byteswap(
2142*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const AvbKernelCmdlineDescriptor*>(descriptors[0]),
2143*d289c2baSAndroid Build Coastguard Worker &d));
2144*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("foo bar baz",
2145*d289c2baSAndroid Build Coastguard Worker std::string(reinterpret_cast<const char*>(descriptors[0]) +
2146*d289c2baSAndroid Build Coastguard Worker sizeof(AvbKernelCmdlineDescriptor),
2147*d289c2baSAndroid Build Coastguard Worker d.kernel_cmdline_length));
2148*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE,
2149*d289c2baSAndroid Build Coastguard Worker avb_be64toh(descriptors[1]->tag));
2150*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(
2151*d289c2baSAndroid Build Coastguard Worker 0,
2152*d289c2baSAndroid Build Coastguard Worker avb_kernel_cmdline_descriptor_validate_and_byteswap(
2153*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const AvbKernelCmdlineDescriptor*>(descriptors[1]),
2154*d289c2baSAndroid Build Coastguard Worker &d));
2155*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("second cmdline",
2156*d289c2baSAndroid Build Coastguard Worker std::string(reinterpret_cast<const char*>(descriptors[1]) +
2157*d289c2baSAndroid Build Coastguard Worker sizeof(AvbKernelCmdlineDescriptor),
2158*d289c2baSAndroid Build Coastguard Worker d.kernel_cmdline_length));
2159*d289c2baSAndroid Build Coastguard Worker }
2160*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,CalculateKernelCmdline)2161*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, CalculateKernelCmdline) {
2162*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_path = testdir_ / "vbmeta.bin";
2163*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2164*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
2165*d289c2baSAndroid Build Coastguard Worker "--output %s "
2166*d289c2baSAndroid Build Coastguard Worker "--kernel_cmdline 'foo bar baz' "
2167*d289c2baSAndroid Build Coastguard Worker "--kernel_cmdline 'second cmdline' "
2168*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
2169*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
2170*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2171*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str());
2172*d289c2baSAndroid Build Coastguard Worker
2173*d289c2baSAndroid Build Coastguard Worker std::filesystem::path out_path = testdir_ / "out.txt";
2174*d289c2baSAndroid Build Coastguard Worker std::string out;
2175*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2176*d289c2baSAndroid Build Coastguard Worker "./avbtool.py calculate_kernel_cmdline --image %s > %s",
2177*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str(),
2178*d289c2baSAndroid Build Coastguard Worker out_path.c_str());
2179*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(out_path.string(), &out));
2180*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(out, "foo bar baz second cmdline");
2181*d289c2baSAndroid Build Coastguard Worker }
2182*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,CalculateKernelCmdlineChainedAndWithFlags)2183*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, CalculateKernelCmdlineChainedAndWithFlags) {
2184*d289c2baSAndroid Build Coastguard Worker const size_t rootfs_size = 1028 * 1024;
2185*d289c2baSAndroid Build Coastguard Worker const size_t partition_size = 1536 * 1024;
2186*d289c2baSAndroid Build Coastguard Worker
2187*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pk_path = testdir_ / "testkey_rsa2048.avbpubkey";
2188*d289c2baSAndroid Build Coastguard Worker
2189*d289c2baSAndroid Build Coastguard Worker // Generate a 1028 KiB file with known content, add a hashtree, and cmdline
2190*d289c2baSAndroid Build Coastguard Worker // descriptors for setting up this hashtree. Notably this will create *two*
2191*d289c2baSAndroid Build Coastguard Worker // cmdline descriptors so we can test calculate_kernel_cmdline's
2192*d289c2baSAndroid Build Coastguard Worker // --hashtree_disabled option.
2193*d289c2baSAndroid Build Coastguard Worker std::vector<uint8_t> rootfs;
2194*d289c2baSAndroid Build Coastguard Worker rootfs.resize(rootfs_size);
2195*d289c2baSAndroid Build Coastguard Worker for (size_t n = 0; n < rootfs_size; n++)
2196*d289c2baSAndroid Build Coastguard Worker rootfs[n] = uint8_t(n);
2197*d289c2baSAndroid Build Coastguard Worker std::filesystem::path rootfs_path = testdir_ / "rootfs.bin";
2198*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(rootfs_size,
2199*d289c2baSAndroid Build Coastguard Worker static_cast<const size_t>(
2200*d289c2baSAndroid Build Coastguard Worker base::WriteFile(base::FilePath(rootfs_path.c_str()),
2201*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const char*>(rootfs.data()),
2202*d289c2baSAndroid Build Coastguard Worker rootfs.size())));
2203*d289c2baSAndroid Build Coastguard Worker
2204*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2205*d289c2baSAndroid Build Coastguard Worker 0,
2206*d289c2baSAndroid Build Coastguard Worker "./avbtool.py extract_public_key --key test/data/testkey_rsa2048.pem"
2207*d289c2baSAndroid Build Coastguard Worker " --output %s",
2208*d289c2baSAndroid Build Coastguard Worker pk_path.c_str());
2209*d289c2baSAndroid Build Coastguard Worker
2210*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2211*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d --image %s "
2212*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name rootfs "
2213*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
2214*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
2215*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" "
2216*d289c2baSAndroid Build Coastguard Worker "--setup_as_rootfs_from_kernel",
2217*d289c2baSAndroid Build Coastguard Worker rootfs_path.c_str(),
2218*d289c2baSAndroid Build Coastguard Worker (int)partition_size);
2219*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
2220*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
2221*d289c2baSAndroid Build Coastguard Worker "Image size: 1572864 bytes\n"
2222*d289c2baSAndroid Build Coastguard Worker "Original image size: 1052672 bytes\n"
2223*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 1085440\n"
2224*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1792 bytes\n"
2225*d289c2baSAndroid Build Coastguard Worker "--\n"
2226*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
2227*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
2228*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
2229*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 1216 bytes\n"
2230*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
2231*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
2232*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
2233*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
2234*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
2235*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
2236*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
2237*d289c2baSAndroid Build Coastguard Worker " Hashtree descriptor:\n"
2238*d289c2baSAndroid Build Coastguard Worker " Version of dm-verity: 1\n"
2239*d289c2baSAndroid Build Coastguard Worker " Image Size: 1052672 bytes\n"
2240*d289c2baSAndroid Build Coastguard Worker " Tree Offset: 1052672\n"
2241*d289c2baSAndroid Build Coastguard Worker " Tree Size: 16384 bytes\n"
2242*d289c2baSAndroid Build Coastguard Worker " Data Block Size: 4096 bytes\n"
2243*d289c2baSAndroid Build Coastguard Worker " Hash Block Size: 4096 bytes\n"
2244*d289c2baSAndroid Build Coastguard Worker " FEC num roots: 2\n"
2245*d289c2baSAndroid Build Coastguard Worker " FEC offset: 1069056\n"
2246*d289c2baSAndroid Build Coastguard Worker " FEC size: 16384 bytes\n"
2247*d289c2baSAndroid Build Coastguard Worker " Hash Algorithm: sha1\n"
2248*d289c2baSAndroid Build Coastguard Worker " Partition Name: rootfs\n"
2249*d289c2baSAndroid Build Coastguard Worker " Salt: d00df00d\n"
2250*d289c2baSAndroid Build Coastguard Worker " Root Digest: e811611467dcd6e8dc4324e45f706c2bdd51db67\n"
2251*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n"
2252*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline descriptor:\n"
2253*d289c2baSAndroid Build Coastguard Worker " Flags: 1\n"
2254*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline: 'dm=\"1 vroot none ro 1,0 2056 verity 1 "
2255*d289c2baSAndroid Build Coastguard Worker "PARTUUID=$(ANDROID_SYSTEM_PARTUUID) PARTUUID=$(ANDROID_SYSTEM_PARTUUID) "
2256*d289c2baSAndroid Build Coastguard Worker "4096 4096 257 257 sha1 e811611467dcd6e8dc4324e45f706c2bdd51db67 "
2257*d289c2baSAndroid Build Coastguard Worker "d00df00d 10 $(ANDROID_VERITY_MODE) ignore_zero_blocks "
2258*d289c2baSAndroid Build Coastguard Worker "use_fec_from_device PARTUUID=$(ANDROID_SYSTEM_PARTUUID) fec_roots 2 "
2259*d289c2baSAndroid Build Coastguard Worker "fec_blocks 261 fec_start 261\" root=/dev/dm-0'\n"
2260*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline descriptor:\n"
2261*d289c2baSAndroid Build Coastguard Worker " Flags: 2\n"
2262*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline: "
2263*d289c2baSAndroid Build Coastguard Worker "'root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)'\n",
2264*d289c2baSAndroid Build Coastguard Worker InfoImage(rootfs_path.string()));
2265*d289c2baSAndroid Build Coastguard Worker
2266*d289c2baSAndroid Build Coastguard Worker // Chain to the rootfs.img and include two cmdline descriptors.
2267*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_path = testdir_ / "vbmeta.bin";
2268*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2269*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
2270*d289c2baSAndroid Build Coastguard Worker "--output %s "
2271*d289c2baSAndroid Build Coastguard Worker "--kernel_cmdline 'foo bar baz' "
2272*d289c2baSAndroid Build Coastguard Worker "--kernel_cmdline 'second cmdline' "
2273*d289c2baSAndroid Build Coastguard Worker "--chain_partition rootfs:1:%s "
2274*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
2275*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
2276*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2277*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str(),
2278*d289c2baSAndroid Build Coastguard Worker pk_path.c_str());
2279*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
2280*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
2281*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
2282*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
2283*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 1280 bytes\n"
2284*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
2285*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
2286*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
2287*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
2288*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
2289*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
2290*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
2291*d289c2baSAndroid Build Coastguard Worker " Chain Partition descriptor:\n"
2292*d289c2baSAndroid Build Coastguard Worker " Partition Name: rootfs\n"
2293*d289c2baSAndroid Build Coastguard Worker " Rollback Index Location: 1\n"
2294*d289c2baSAndroid Build Coastguard Worker " Public key (sha1): "
2295*d289c2baSAndroid Build Coastguard Worker "cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
2296*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n"
2297*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline descriptor:\n"
2298*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n"
2299*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline: 'foo bar baz'\n"
2300*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline descriptor:\n"
2301*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n"
2302*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline: 'second cmdline'\n",
2303*d289c2baSAndroid Build Coastguard Worker InfoImage(vbmeta_path.string()));
2304*d289c2baSAndroid Build Coastguard Worker
2305*d289c2baSAndroid Build Coastguard Worker std::filesystem::path out_path = testdir_ / "out.txt";
2306*d289c2baSAndroid Build Coastguard Worker std::string out;
2307*d289c2baSAndroid Build Coastguard Worker
2308*d289c2baSAndroid Build Coastguard Worker // First check the kernel cmdline without --hashtree_disabled - compare with
2309*d289c2baSAndroid Build Coastguard Worker // above info_image output.
2310*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2311*d289c2baSAndroid Build Coastguard Worker "./avbtool.py calculate_kernel_cmdline --image %s > %s",
2312*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str(),
2313*d289c2baSAndroid Build Coastguard Worker out_path.c_str());
2314*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(out_path.string(), &out));
2315*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
2316*d289c2baSAndroid Build Coastguard Worker "dm=\"1 vroot none ro 1,0 2056 verity 1 "
2317*d289c2baSAndroid Build Coastguard Worker "PARTUUID=$(ANDROID_SYSTEM_PARTUUID) PARTUUID=$(ANDROID_SYSTEM_PARTUUID) "
2318*d289c2baSAndroid Build Coastguard Worker "4096 4096 257 257 sha1 e811611467dcd6e8dc4324e45f706c2bdd51db67 "
2319*d289c2baSAndroid Build Coastguard Worker "d00df00d 10 $(ANDROID_VERITY_MODE) ignore_zero_blocks "
2320*d289c2baSAndroid Build Coastguard Worker "use_fec_from_device PARTUUID=$(ANDROID_SYSTEM_PARTUUID) fec_roots 2 "
2321*d289c2baSAndroid Build Coastguard Worker "fec_blocks 261 fec_start 261\" root=/dev/dm-0 foo bar baz second "
2322*d289c2baSAndroid Build Coastguard Worker "cmdline",
2323*d289c2baSAndroid Build Coastguard Worker out);
2324*d289c2baSAndroid Build Coastguard Worker
2325*d289c2baSAndroid Build Coastguard Worker // Then check the kernel cmdline with --hashtree_disabled - compare with above
2326*d289c2baSAndroid Build Coastguard Worker // info_image output.
2327*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2328*d289c2baSAndroid Build Coastguard Worker "./avbtool.py calculate_kernel_cmdline --image %s "
2329*d289c2baSAndroid Build Coastguard Worker "--hashtree_disabled > %s",
2330*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str(),
2331*d289c2baSAndroid Build Coastguard Worker out_path.c_str());
2332*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(out_path.string(), &out));
2333*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
2334*d289c2baSAndroid Build Coastguard Worker "root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID) foo bar baz second cmdline",
2335*d289c2baSAndroid Build Coastguard Worker out);
2336*d289c2baSAndroid Build Coastguard Worker }
2337*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AddHashFooterSmallImageWithExternalVbmeta)2338*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AddHashFooterSmallImageWithExternalVbmeta) {
2339*d289c2baSAndroid Build Coastguard Worker const size_t image_size = 37;
2340*d289c2baSAndroid Build Coastguard Worker const size_t partition_size = 20 * 4096;
2341*d289c2baSAndroid Build Coastguard Worker
2342*d289c2baSAndroid Build Coastguard Worker std::vector<uint8_t> image(image_size, 0);
2343*d289c2baSAndroid Build Coastguard Worker for (size_t n = 0; n < image_size; n++) {
2344*d289c2baSAndroid Build Coastguard Worker image[n] = uint8_t(n);
2345*d289c2baSAndroid Build Coastguard Worker }
2346*d289c2baSAndroid Build Coastguard Worker
2347*d289c2baSAndroid Build Coastguard Worker std::filesystem::path ext_vbmeta_path = testdir_ / "ext_vbmeta.bin";
2348*d289c2baSAndroid Build Coastguard Worker std::filesystem::path image_path = testdir_ / "kernel.bin";
2349*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(image_size,
2350*d289c2baSAndroid Build Coastguard Worker static_cast<const size_t>(
2351*d289c2baSAndroid Build Coastguard Worker base::WriteFile(base::FilePath(image_path.c_str()),
2352*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const char*>(image.data()),
2353*d289c2baSAndroid Build Coastguard Worker image.size())));
2354*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2355*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer --salt d00df00d "
2356*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm sha256 --image %s "
2357*d289c2baSAndroid Build Coastguard Worker "--partition_size %zu --partition_name kernel "
2358*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
2359*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
2360*d289c2baSAndroid Build Coastguard Worker "--output_vbmeta %s "
2361*d289c2baSAndroid Build Coastguard Worker "--do_not_append_vbmeta_image "
2362*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2363*d289c2baSAndroid Build Coastguard Worker image_path.c_str(),
2364*d289c2baSAndroid Build Coastguard Worker partition_size,
2365*d289c2baSAndroid Build Coastguard Worker ext_vbmeta_path.c_str());
2366*d289c2baSAndroid Build Coastguard Worker
2367*d289c2baSAndroid Build Coastguard Worker // It is not this unit test's job to check the vbmeta content.
2368*d289c2baSAndroid Build Coastguard Worker
2369*d289c2baSAndroid Build Coastguard Worker int64_t file_size;
2370*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(
2371*d289c2baSAndroid Build Coastguard Worker base::GetFileSize(base::FilePath(image_path.c_str()), &file_size));
2372*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(static_cast<size_t>(file_size), image_size);
2373*d289c2baSAndroid Build Coastguard Worker }
2374*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,IncludeDescriptor)2375*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, IncludeDescriptor) {
2376*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta1_path = testdir_ / "vbmeta_id1.bin";
2377*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta2_path = testdir_ / "vbmeta_id2.bin";
2378*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta3_path = testdir_ / "vbmeta_id3.bin";
2379*d289c2baSAndroid Build Coastguard Worker
2380*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2381*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
2382*d289c2baSAndroid Build Coastguard Worker "--output %s "
2383*d289c2baSAndroid Build Coastguard Worker "--kernel_cmdline 'something' "
2384*d289c2baSAndroid Build Coastguard Worker "--prop name:value "
2385*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2386*d289c2baSAndroid Build Coastguard Worker vbmeta1_path.c_str());
2387*d289c2baSAndroid Build Coastguard Worker
2388*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2389*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
2390*d289c2baSAndroid Build Coastguard Worker "--output %s "
2391*d289c2baSAndroid Build Coastguard Worker "--prop name2:value2 "
2392*d289c2baSAndroid Build Coastguard Worker "--prop name3:value3 "
2393*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2394*d289c2baSAndroid Build Coastguard Worker vbmeta2_path.c_str());
2395*d289c2baSAndroid Build Coastguard Worker
2396*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2397*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
2398*d289c2baSAndroid Build Coastguard Worker "--output %s "
2399*d289c2baSAndroid Build Coastguard Worker "--prop name4:value4 "
2400*d289c2baSAndroid Build Coastguard Worker "--include_descriptors_from_image %s "
2401*d289c2baSAndroid Build Coastguard Worker "--include_descriptors_from_image %s "
2402*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2403*d289c2baSAndroid Build Coastguard Worker vbmeta3_path.c_str(),
2404*d289c2baSAndroid Build Coastguard Worker vbmeta1_path.c_str(),
2405*d289c2baSAndroid Build Coastguard Worker vbmeta2_path.c_str());
2406*d289c2baSAndroid Build Coastguard Worker
2407*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
2408*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
2409*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
2410*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 0 bytes\n"
2411*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 256 bytes\n"
2412*d289c2baSAndroid Build Coastguard Worker "Algorithm: NONE\n"
2413*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
2414*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
2415*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
2416*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
2417*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
2418*d289c2baSAndroid Build Coastguard Worker " Prop: name4 -> 'value4'\n"
2419*d289c2baSAndroid Build Coastguard Worker " Prop: name -> 'value'\n"
2420*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline descriptor:\n"
2421*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n"
2422*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline: 'something'\n"
2423*d289c2baSAndroid Build Coastguard Worker " Prop: name2 -> 'value2'\n"
2424*d289c2baSAndroid Build Coastguard Worker " Prop: name3 -> 'value3'\n",
2425*d289c2baSAndroid Build Coastguard Worker InfoImage(vbmeta3_path.string()));
2426*d289c2baSAndroid Build Coastguard Worker }
2427*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,ChainedPartition)2428*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, ChainedPartition) {
2429*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_path = testdir_ / "vbmeta_cp.bin";
2430*d289c2baSAndroid Build Coastguard Worker
2431*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pk_path = testdir_ / "testkey_rsa2048.avbpubkey";
2432*d289c2baSAndroid Build Coastguard Worker
2433*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2434*d289c2baSAndroid Build Coastguard Worker 0,
2435*d289c2baSAndroid Build Coastguard Worker "./avbtool.py extract_public_key --key test/data/testkey_rsa2048.pem"
2436*d289c2baSAndroid Build Coastguard Worker " --output %s",
2437*d289c2baSAndroid Build Coastguard Worker pk_path.c_str());
2438*d289c2baSAndroid Build Coastguard Worker
2439*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2440*d289c2baSAndroid Build Coastguard Worker 0,
2441*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
2442*d289c2baSAndroid Build Coastguard Worker "--output %s "
2443*d289c2baSAndroid Build Coastguard Worker "--chain_partition system:1:%s "
2444*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 --key test/data/testkey_rsa2048.pem "
2445*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2446*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str(),
2447*d289c2baSAndroid Build Coastguard Worker pk_path.c_str());
2448*d289c2baSAndroid Build Coastguard Worker
2449*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
2450*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
2451*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
2452*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
2453*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 1152 bytes\n"
2454*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
2455*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
2456*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
2457*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
2458*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
2459*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
2460*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
2461*d289c2baSAndroid Build Coastguard Worker " Chain Partition descriptor:\n"
2462*d289c2baSAndroid Build Coastguard Worker " Partition Name: system\n"
2463*d289c2baSAndroid Build Coastguard Worker " Rollback Index Location: 1\n"
2464*d289c2baSAndroid Build Coastguard Worker " Public key (sha1): "
2465*d289c2baSAndroid Build Coastguard Worker "cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
2466*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n",
2467*d289c2baSAndroid Build Coastguard Worker InfoImage(vbmeta_path.string()));
2468*d289c2baSAndroid Build Coastguard Worker
2469*d289c2baSAndroid Build Coastguard Worker // Now check the VBMeta image.
2470*d289c2baSAndroid Build Coastguard Worker std::string image_data;
2471*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(
2472*d289c2baSAndroid Build Coastguard Worker android::base::ReadFileToString(vbmeta_path.string(), &image_data));
2473*d289c2baSAndroid Build Coastguard Worker
2474*d289c2baSAndroid Build Coastguard Worker const uint8_t* vbmeta_data =
2475*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const uint8_t*>(image_data.data());
2476*d289c2baSAndroid Build Coastguard Worker const size_t vbmeta_size = image_data.length();
2477*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_VBMETA_VERIFY_RESULT_OK,
2478*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_verify(vbmeta_data, vbmeta_size, NULL, NULL));
2479*d289c2baSAndroid Build Coastguard Worker
2480*d289c2baSAndroid Build Coastguard Worker // Collect all descriptors.
2481*d289c2baSAndroid Build Coastguard Worker std::vector<const AvbDescriptor*> descriptors;
2482*d289c2baSAndroid Build Coastguard Worker avb_descriptor_foreach(
2483*d289c2baSAndroid Build Coastguard Worker vbmeta_data, vbmeta_size, collect_descriptors, &descriptors);
2484*d289c2baSAndroid Build Coastguard Worker
2485*d289c2baSAndroid Build Coastguard Worker // We should have one descriptor - check it.
2486*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1UL, descriptors.size());
2487*d289c2baSAndroid Build Coastguard Worker
2488*d289c2baSAndroid Build Coastguard Worker std::string pk_data;
2489*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(pk_path.string(), &pk_data));
2490*d289c2baSAndroid Build Coastguard Worker
2491*d289c2baSAndroid Build Coastguard Worker AvbChainPartitionDescriptor d;
2492*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_DESCRIPTOR_TAG_CHAIN_PARTITION,
2493*d289c2baSAndroid Build Coastguard Worker avb_be64toh(descriptors[0]->tag));
2494*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(
2495*d289c2baSAndroid Build Coastguard Worker 0,
2496*d289c2baSAndroid Build Coastguard Worker avb_chain_partition_descriptor_validate_and_byteswap(
2497*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const AvbChainPartitionDescriptor*>(descriptors[0]),
2498*d289c2baSAndroid Build Coastguard Worker &d));
2499*d289c2baSAndroid Build Coastguard Worker const uint8_t* desc_end = reinterpret_cast<const uint8_t*>(descriptors[0]) +
2500*d289c2baSAndroid Build Coastguard Worker sizeof(AvbChainPartitionDescriptor);
2501*d289c2baSAndroid Build Coastguard Worker uint64_t o = 0;
2502*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("system",
2503*d289c2baSAndroid Build Coastguard Worker std::string(reinterpret_cast<const char*>(desc_end + o),
2504*d289c2baSAndroid Build Coastguard Worker d.partition_name_len));
2505*d289c2baSAndroid Build Coastguard Worker o += d.partition_name_len;
2506*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(pk_data,
2507*d289c2baSAndroid Build Coastguard Worker std::string(reinterpret_cast<const char*>(descriptors[0]) +
2508*d289c2baSAndroid Build Coastguard Worker sizeof(AvbChainPartitionDescriptor) + o,
2509*d289c2baSAndroid Build Coastguard Worker d.public_key_len));
2510*d289c2baSAndroid Build Coastguard Worker }
2511*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,ChainedPartitionNoAB)2512*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, ChainedPartitionNoAB) {
2513*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_path = testdir_ / "vbmeta_cp.bin";
2514*d289c2baSAndroid Build Coastguard Worker
2515*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pk_path = testdir_ / "testkey_rsa2048.avbpubkey";
2516*d289c2baSAndroid Build Coastguard Worker
2517*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2518*d289c2baSAndroid Build Coastguard Worker 0,
2519*d289c2baSAndroid Build Coastguard Worker "./avbtool.py extract_public_key --key test/data/testkey_rsa2048.pem"
2520*d289c2baSAndroid Build Coastguard Worker " --output %s",
2521*d289c2baSAndroid Build Coastguard Worker pk_path.c_str());
2522*d289c2baSAndroid Build Coastguard Worker
2523*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2524*d289c2baSAndroid Build Coastguard Worker 0,
2525*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
2526*d289c2baSAndroid Build Coastguard Worker "--output %s "
2527*d289c2baSAndroid Build Coastguard Worker "--chain_partition_do_not_use_ab system:1:%s "
2528*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 --key test/data/testkey_rsa2048.pem "
2529*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2530*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str(),
2531*d289c2baSAndroid Build Coastguard Worker pk_path.c_str());
2532*d289c2baSAndroid Build Coastguard Worker
2533*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
2534*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.3\n"
2535*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
2536*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
2537*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 1152 bytes\n"
2538*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
2539*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
2540*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
2541*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
2542*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
2543*d289c2baSAndroid Build Coastguard Worker "Release String: ''\n"
2544*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
2545*d289c2baSAndroid Build Coastguard Worker " Chain Partition descriptor:\n"
2546*d289c2baSAndroid Build Coastguard Worker " Partition Name: system\n"
2547*d289c2baSAndroid Build Coastguard Worker " Rollback Index Location: 1\n"
2548*d289c2baSAndroid Build Coastguard Worker " Public key (sha1): "
2549*d289c2baSAndroid Build Coastguard Worker "cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
2550*d289c2baSAndroid Build Coastguard Worker " Flags: 1\n",
2551*d289c2baSAndroid Build Coastguard Worker InfoImage(vbmeta_path.string()));
2552*d289c2baSAndroid Build Coastguard Worker
2553*d289c2baSAndroid Build Coastguard Worker // Now check the VBMeta image.
2554*d289c2baSAndroid Build Coastguard Worker std::string image_data;
2555*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(
2556*d289c2baSAndroid Build Coastguard Worker android::base::ReadFileToString(vbmeta_path.string(), &image_data));
2557*d289c2baSAndroid Build Coastguard Worker
2558*d289c2baSAndroid Build Coastguard Worker const uint8_t* vbmeta_data =
2559*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const uint8_t*>(image_data.data());
2560*d289c2baSAndroid Build Coastguard Worker const size_t vbmeta_size = image_data.length();
2561*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_VBMETA_VERIFY_RESULT_OK,
2562*d289c2baSAndroid Build Coastguard Worker avb_vbmeta_image_verify(vbmeta_data, vbmeta_size, NULL, NULL));
2563*d289c2baSAndroid Build Coastguard Worker
2564*d289c2baSAndroid Build Coastguard Worker // Collect all descriptors.
2565*d289c2baSAndroid Build Coastguard Worker std::vector<const AvbDescriptor*> descriptors;
2566*d289c2baSAndroid Build Coastguard Worker avb_descriptor_foreach(
2567*d289c2baSAndroid Build Coastguard Worker vbmeta_data, vbmeta_size, collect_descriptors, &descriptors);
2568*d289c2baSAndroid Build Coastguard Worker
2569*d289c2baSAndroid Build Coastguard Worker // We should have one descriptor - check it.
2570*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(1UL, descriptors.size());
2571*d289c2baSAndroid Build Coastguard Worker
2572*d289c2baSAndroid Build Coastguard Worker std::string pk_data;
2573*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(pk_path.string(), &pk_data));
2574*d289c2baSAndroid Build Coastguard Worker
2575*d289c2baSAndroid Build Coastguard Worker AvbChainPartitionDescriptor d;
2576*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_DESCRIPTOR_TAG_CHAIN_PARTITION,
2577*d289c2baSAndroid Build Coastguard Worker avb_be64toh(descriptors[0]->tag));
2578*d289c2baSAndroid Build Coastguard Worker EXPECT_NE(
2579*d289c2baSAndroid Build Coastguard Worker 0,
2580*d289c2baSAndroid Build Coastguard Worker avb_chain_partition_descriptor_validate_and_byteswap(
2581*d289c2baSAndroid Build Coastguard Worker reinterpret_cast<const AvbChainPartitionDescriptor*>(descriptors[0]),
2582*d289c2baSAndroid Build Coastguard Worker &d));
2583*d289c2baSAndroid Build Coastguard Worker const uint8_t* desc_end = reinterpret_cast<const uint8_t*>(descriptors[0]) +
2584*d289c2baSAndroid Build Coastguard Worker sizeof(AvbChainPartitionDescriptor);
2585*d289c2baSAndroid Build Coastguard Worker uint64_t o = 0;
2586*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("system",
2587*d289c2baSAndroid Build Coastguard Worker std::string(reinterpret_cast<const char*>(desc_end + o),
2588*d289c2baSAndroid Build Coastguard Worker d.partition_name_len));
2589*d289c2baSAndroid Build Coastguard Worker o += d.partition_name_len;
2590*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(pk_data,
2591*d289c2baSAndroid Build Coastguard Worker std::string(reinterpret_cast<const char*>(descriptors[0]) +
2592*d289c2baSAndroid Build Coastguard Worker sizeof(AvbChainPartitionDescriptor) + o,
2593*d289c2baSAndroid Build Coastguard Worker d.public_key_len));
2594*d289c2baSAndroid Build Coastguard Worker }
2595*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,ChainedPartitionNoLocationCollision)2596*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, ChainedPartitionNoLocationCollision) {
2597*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_path = testdir_ / "vbmeta_cp.bin";
2598*d289c2baSAndroid Build Coastguard Worker
2599*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pk_path = testdir_ / "testkey_rsa2048.avbpubkey";
2600*d289c2baSAndroid Build Coastguard Worker
2601*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2602*d289c2baSAndroid Build Coastguard Worker 0,
2603*d289c2baSAndroid Build Coastguard Worker "./avbtool.py extract_public_key --key test/data/testkey_rsa2048.pem"
2604*d289c2baSAndroid Build Coastguard Worker " --output %s",
2605*d289c2baSAndroid Build Coastguard Worker pk_path.c_str());
2606*d289c2baSAndroid Build Coastguard Worker
2607*d289c2baSAndroid Build Coastguard Worker // Check that avbtool bails if the same Rollback Index Location is
2608*d289c2baSAndroid Build Coastguard Worker // used for multiple chained partitions.
2609*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2610*d289c2baSAndroid Build Coastguard Worker 1,
2611*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
2612*d289c2baSAndroid Build Coastguard Worker "--output %s "
2613*d289c2baSAndroid Build Coastguard Worker "--chain_partition system:1:%s "
2614*d289c2baSAndroid Build Coastguard Worker "--chain_partition other:1:%s "
2615*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 --key test/data/testkey_rsa2048.pem "
2616*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2617*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str(),
2618*d289c2baSAndroid Build Coastguard Worker pk_path.c_str(),
2619*d289c2baSAndroid Build Coastguard Worker pk_path.c_str());
2620*d289c2baSAndroid Build Coastguard Worker }
2621*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,AppendVBMetaImage)2622*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, AppendVBMetaImage) {
2623*d289c2baSAndroid Build Coastguard Worker size_t boot_size = 5 * 1024 * 1024;
2624*d289c2baSAndroid Build Coastguard Worker size_t boot_partition_size = 32 * 1024 * 1024;
2625*d289c2baSAndroid Build Coastguard Worker std::string boot_path = GenerateImage("boot", boot_size);
2626*d289c2baSAndroid Build Coastguard Worker
2627*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage("vbmeta.img",
2628*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
2629*d289c2baSAndroid Build Coastguard Worker 0,
2630*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
2631*d289c2baSAndroid Build Coastguard Worker std::string("--append_to_release_string \"\" "
2632*d289c2baSAndroid Build Coastguard Worker "--kernel_cmdline foo"));
2633*d289c2baSAndroid Build Coastguard Worker
2634*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2635*d289c2baSAndroid Build Coastguard Worker "./avbtool.py append_vbmeta_image "
2636*d289c2baSAndroid Build Coastguard Worker "--image %s "
2637*d289c2baSAndroid Build Coastguard Worker "--partition_size %d "
2638*d289c2baSAndroid Build Coastguard Worker "--vbmeta_image %s ",
2639*d289c2baSAndroid Build Coastguard Worker boot_path.c_str(),
2640*d289c2baSAndroid Build Coastguard Worker (int)boot_partition_size,
2641*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
2642*d289c2baSAndroid Build Coastguard Worker
2643*d289c2baSAndroid Build Coastguard Worker std::string vbmeta_contents = InfoImage(vbmeta_image_path_.string());
2644*d289c2baSAndroid Build Coastguard Worker std::string boot_contents = InfoImage(boot_path);
2645*d289c2baSAndroid Build Coastguard Worker
2646*d289c2baSAndroid Build Coastguard Worker // Check that boot.img has the same vbmeta blob as from vbmeta.img -
2647*d289c2baSAndroid Build Coastguard Worker // we do this by inspecting 'avbtool info_image' output combined
2648*d289c2baSAndroid Build Coastguard Worker // with the known footer location given boot.img has 5 MiB known
2649*d289c2baSAndroid Build Coastguard Worker // content and the partition size is 32 MiB.
2650*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(
2651*d289c2baSAndroid Build Coastguard Worker "Minimum libavb version: 1.0\n"
2652*d289c2baSAndroid Build Coastguard Worker "Header Block: 256 bytes\n"
2653*d289c2baSAndroid Build Coastguard Worker "Authentication Block: 320 bytes\n"
2654*d289c2baSAndroid Build Coastguard Worker "Auxiliary Block: 576 bytes\n"
2655*d289c2baSAndroid Build Coastguard Worker "Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
2656*d289c2baSAndroid Build Coastguard Worker "Algorithm: SHA256_RSA2048\n"
2657*d289c2baSAndroid Build Coastguard Worker "Rollback Index: 0\n"
2658*d289c2baSAndroid Build Coastguard Worker "Flags: 0\n"
2659*d289c2baSAndroid Build Coastguard Worker "Rollback Index Location: 0\n"
2660*d289c2baSAndroid Build Coastguard Worker "Release String: 'avbtool 1.3.0 '\n"
2661*d289c2baSAndroid Build Coastguard Worker "Descriptors:\n"
2662*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline descriptor:\n"
2663*d289c2baSAndroid Build Coastguard Worker " Flags: 0\n"
2664*d289c2baSAndroid Build Coastguard Worker " Kernel Cmdline: 'foo'\n",
2665*d289c2baSAndroid Build Coastguard Worker vbmeta_contents);
2666*d289c2baSAndroid Build Coastguard Worker std::string known_footer =
2667*d289c2baSAndroid Build Coastguard Worker "Footer version: 1.0\n"
2668*d289c2baSAndroid Build Coastguard Worker "Image size: 33554432 bytes\n"
2669*d289c2baSAndroid Build Coastguard Worker "Original image size: 5242880 bytes\n"
2670*d289c2baSAndroid Build Coastguard Worker "VBMeta offset: 5242880\n"
2671*d289c2baSAndroid Build Coastguard Worker "VBMeta size: 1152 bytes\n"
2672*d289c2baSAndroid Build Coastguard Worker "--\n";
2673*d289c2baSAndroid Build Coastguard Worker ASSERT_EQ(known_footer + vbmeta_contents, boot_contents);
2674*d289c2baSAndroid Build Coastguard Worker
2675*d289c2baSAndroid Build Coastguard Worker // Also verify that the blobs are the same, bit for bit.
2676*d289c2baSAndroid Build Coastguard Worker base::File f = base::File(base::FilePath(boot_path),
2677*d289c2baSAndroid Build Coastguard Worker base::File::FLAG_OPEN | base::File::FLAG_READ);
2678*d289c2baSAndroid Build Coastguard Worker std::vector<uint8_t> loaded_vbmeta;
2679*d289c2baSAndroid Build Coastguard Worker loaded_vbmeta.resize(1152);
2680*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
2681*d289c2baSAndroid Build Coastguard Worker f.Read(
2682*d289c2baSAndroid Build Coastguard Worker 5 * 1024 * 1024, reinterpret_cast<char*>(loaded_vbmeta.data()), 1152),
2683*d289c2baSAndroid Build Coastguard Worker 1152);
2684*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(vbmeta_image_, loaded_vbmeta);
2685*d289c2baSAndroid Build Coastguard Worker }
2686*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,SigningHelperBasic)2687*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, SigningHelperBasic) {
2688*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_path = testdir_ / "vbmeta.bin";
2689*d289c2baSAndroid Build Coastguard Worker std::filesystem::path signing_helper_test_path =
2690*d289c2baSAndroid Build Coastguard Worker testdir_ / "signing_helper_test";
2691*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2692*d289c2baSAndroid Build Coastguard Worker 0,
2693*d289c2baSAndroid Build Coastguard Worker "SIGNING_HELPER_TEST=\"%s\" ./avbtool.py make_vbmeta_image "
2694*d289c2baSAndroid Build Coastguard Worker "--output %s "
2695*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 --key test/data/testkey_rsa2048.pem "
2696*d289c2baSAndroid Build Coastguard Worker "--signing_helper test/avbtool_signing_helper_test.py "
2697*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2698*d289c2baSAndroid Build Coastguard Worker signing_helper_test_path.c_str(),
2699*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str());
2700*d289c2baSAndroid Build Coastguard Worker
2701*d289c2baSAndroid Build Coastguard Worker // Now check the value in test file.
2702*d289c2baSAndroid Build Coastguard Worker std::string value;
2703*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(signing_helper_test_path.string(),
2704*d289c2baSAndroid Build Coastguard Worker &value));
2705*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("DONE", value);
2706*d289c2baSAndroid Build Coastguard Worker }
2707*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,SigningHelperWithFilesBasic)2708*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, SigningHelperWithFilesBasic) {
2709*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_path = testdir_ / "vbmeta.bin";
2710*d289c2baSAndroid Build Coastguard Worker std::filesystem::path signing_helper_test_path =
2711*d289c2baSAndroid Build Coastguard Worker testdir_ / "signing_helper_test";
2712*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2713*d289c2baSAndroid Build Coastguard Worker 0,
2714*d289c2baSAndroid Build Coastguard Worker "SIGNING_HELPER_TEST=\"%s\" ./avbtool.py make_vbmeta_image "
2715*d289c2baSAndroid Build Coastguard Worker "--output %s "
2716*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 --key test/data/testkey_rsa2048.pem "
2717*d289c2baSAndroid Build Coastguard Worker "--signing_helper_with_files "
2718*d289c2baSAndroid Build Coastguard Worker "test/avbtool_signing_helper_with_files_test.py "
2719*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2720*d289c2baSAndroid Build Coastguard Worker signing_helper_test_path.c_str(),
2721*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str());
2722*d289c2baSAndroid Build Coastguard Worker
2723*d289c2baSAndroid Build Coastguard Worker // Now check the value in test file.
2724*d289c2baSAndroid Build Coastguard Worker std::string value;
2725*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(signing_helper_test_path.string(),
2726*d289c2baSAndroid Build Coastguard Worker &value));
2727*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ("DONE", value);
2728*d289c2baSAndroid Build Coastguard Worker }
2729*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,SigningHelperReturnError)2730*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, SigningHelperReturnError) {
2731*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_path = testdir_ / "vbmeta.bin";
2732*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2733*d289c2baSAndroid Build Coastguard Worker 1,
2734*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
2735*d289c2baSAndroid Build Coastguard Worker "--output %s "
2736*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 --key test/data/testkey_rsa2048.pem "
2737*d289c2baSAndroid Build Coastguard Worker "--signing_helper test/avbtool_signing_helper_test.py "
2738*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2739*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str());
2740*d289c2baSAndroid Build Coastguard Worker }
2741*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,SigningHelperWithFilesReturnError)2742*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, SigningHelperWithFilesReturnError) {
2743*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_path = testdir_ / "vbmeta.bin";
2744*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2745*d289c2baSAndroid Build Coastguard Worker 1,
2746*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
2747*d289c2baSAndroid Build Coastguard Worker "--output %s "
2748*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 --key test/data/testkey_rsa2048.pem "
2749*d289c2baSAndroid Build Coastguard Worker "--signing_helper_with_files "
2750*d289c2baSAndroid Build Coastguard Worker "test/avbtool_signing_helper_with_files_test.py "
2751*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2752*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str());
2753*d289c2baSAndroid Build Coastguard Worker }
2754*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageNoSignature)2755*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageNoSignature) {
2756*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage("vbmeta.img",
2757*d289c2baSAndroid Build Coastguard Worker "", // NONE
2758*d289c2baSAndroid Build Coastguard Worker 0,
2759*d289c2baSAndroid Build Coastguard Worker "");
2760*d289c2baSAndroid Build Coastguard Worker
2761*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2762*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
2763*d289c2baSAndroid Build Coastguard Worker "--image %s ",
2764*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
2765*d289c2baSAndroid Build Coastguard Worker }
2766*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageValidSignature)2767*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageValidSignature) {
2768*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
2769*d289c2baSAndroid Build Coastguard Worker "vbmeta.img", "SHA256_RSA2048", 0, "test/data/testkey_rsa2048.pem");
2770*d289c2baSAndroid Build Coastguard Worker
2771*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2772*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
2773*d289c2baSAndroid Build Coastguard Worker "--image %s ",
2774*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
2775*d289c2baSAndroid Build Coastguard Worker }
2776*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageCorruptedVBMeta)2777*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageCorruptedVBMeta) {
2778*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
2779*d289c2baSAndroid Build Coastguard Worker "vbmeta.img", "SHA256_RSA2048", 0, "test/data/testkey_rsa2048.pem");
2780*d289c2baSAndroid Build Coastguard Worker
2781*d289c2baSAndroid Build Coastguard Worker // Corrupt four bytes of data in the end of the image. Since the aux
2782*d289c2baSAndroid Build Coastguard Worker // data is at the end and this data is signed, this will change the
2783*d289c2baSAndroid Build Coastguard Worker // value of the computed hash.
2784*d289c2baSAndroid Build Coastguard Worker uint8_t corrupt_data[4] = {0xff, 0xff, 0xff, 0xff};
2785*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_IO_RESULT_OK,
2786*d289c2baSAndroid Build Coastguard Worker ops_.avb_ops()->write_to_partition(ops_.avb_ops(),
2787*d289c2baSAndroid Build Coastguard Worker "vbmeta",
2788*d289c2baSAndroid Build Coastguard Worker -4, // offset from end
2789*d289c2baSAndroid Build Coastguard Worker sizeof corrupt_data,
2790*d289c2baSAndroid Build Coastguard Worker corrupt_data));
2791*d289c2baSAndroid Build Coastguard Worker
2792*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(1,
2793*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
2794*d289c2baSAndroid Build Coastguard Worker "--image %s ",
2795*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
2796*d289c2baSAndroid Build Coastguard Worker }
2797*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageOtherKeyMatching)2798*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageOtherKeyMatching) {
2799*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
2800*d289c2baSAndroid Build Coastguard Worker "vbmeta.img", "SHA256_RSA2048", 0, "test/data/testkey_rsa2048.pem");
2801*d289c2baSAndroid Build Coastguard Worker
2802*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2803*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
2804*d289c2baSAndroid Build Coastguard Worker "--image %s --key test/data/testkey_rsa2048.pem",
2805*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
2806*d289c2baSAndroid Build Coastguard Worker }
2807*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageOtherKeyNotMatching)2808*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageOtherKeyNotMatching) {
2809*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
2810*d289c2baSAndroid Build Coastguard Worker "vbmeta.img", "SHA256_RSA2048", 0, "test/data/testkey_rsa2048.pem");
2811*d289c2baSAndroid Build Coastguard Worker
2812*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(1,
2813*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
2814*d289c2baSAndroid Build Coastguard Worker "--image %s --key test/data/testkey_rsa4096.pem",
2815*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
2816*d289c2baSAndroid Build Coastguard Worker }
2817*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageBrokenSignature)2818*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageBrokenSignature) {
2819*d289c2baSAndroid Build Coastguard Worker std::filesystem::path vbmeta_path = testdir_ / "vbmeta.bin";
2820*d289c2baSAndroid Build Coastguard Worker std::filesystem::path signing_helper_test_path =
2821*d289c2baSAndroid Build Coastguard Worker testdir_ / "signing_helper_test";
2822*d289c2baSAndroid Build Coastguard Worker
2823*d289c2baSAndroid Build Coastguard Worker // Intentionally make the signer generate a wrong signature.
2824*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2825*d289c2baSAndroid Build Coastguard Worker 0,
2826*d289c2baSAndroid Build Coastguard Worker "SIGNING_HELPER_GENERATE_WRONG_SIGNATURE=1 ./avbtool.py "
2827*d289c2baSAndroid Build Coastguard Worker "make_vbmeta_image "
2828*d289c2baSAndroid Build Coastguard Worker "--output %s "
2829*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 --key test/data/testkey_rsa2048.pem "
2830*d289c2baSAndroid Build Coastguard Worker "--signing_helper test/avbtool_signing_helper_test.py "
2831*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\"",
2832*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str());
2833*d289c2baSAndroid Build Coastguard Worker
2834*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(1,
2835*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
2836*d289c2baSAndroid Build Coastguard Worker "--image %s ",
2837*d289c2baSAndroid Build Coastguard Worker vbmeta_path.c_str());
2838*d289c2baSAndroid Build Coastguard Worker }
2839*d289c2baSAndroid Build Coastguard Worker
2840*d289c2baSAndroid Build Coastguard Worker // Helper to generate boot.img, unsparse system.img, and vbmeta.img.
GenerateImageWithHashAndHashtreeSetup()2841*d289c2baSAndroid Build Coastguard Worker void AvbToolTest::GenerateImageWithHashAndHashtreeSetup() {
2842*d289c2baSAndroid Build Coastguard Worker const size_t boot_partition_size = 16 * 1024 * 1024;
2843*d289c2baSAndroid Build Coastguard Worker const size_t boot_image_size = 5 * 1024 * 1024;
2844*d289c2baSAndroid Build Coastguard Worker std::string boot_path = GenerateImage("boot.img", boot_image_size);
2845*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2846*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer"
2847*d289c2baSAndroid Build Coastguard Worker " --image %s"
2848*d289c2baSAndroid Build Coastguard Worker " --rollback_index 0"
2849*d289c2baSAndroid Build Coastguard Worker " --partition_name boot"
2850*d289c2baSAndroid Build Coastguard Worker " --partition_size %zd"
2851*d289c2baSAndroid Build Coastguard Worker " --salt deadbeef"
2852*d289c2baSAndroid Build Coastguard Worker " --internal_release_string \"\"",
2853*d289c2baSAndroid Build Coastguard Worker boot_path.c_str(),
2854*d289c2baSAndroid Build Coastguard Worker boot_partition_size);
2855*d289c2baSAndroid Build Coastguard Worker
2856*d289c2baSAndroid Build Coastguard Worker const size_t system_partition_size = 10 * 1024 * 1024;
2857*d289c2baSAndroid Build Coastguard Worker const size_t system_image_size = 8 * 1024 * 1024;
2858*d289c2baSAndroid Build Coastguard Worker std::string system_path = GenerateImage("system.img", system_image_size);
2859*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2860*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d --image %s "
2861*d289c2baSAndroid Build Coastguard Worker "--partition_size %zd --partition_name system "
2862*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" ",
2863*d289c2baSAndroid Build Coastguard Worker system_path.c_str(),
2864*d289c2baSAndroid Build Coastguard Worker system_partition_size);
2865*d289c2baSAndroid Build Coastguard Worker
2866*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
2867*d289c2baSAndroid Build Coastguard Worker "vbmeta.img",
2868*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
2869*d289c2baSAndroid Build Coastguard Worker 0,
2870*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
2871*d289c2baSAndroid Build Coastguard Worker android::base::StringPrintf("--include_descriptors_from_image %s "
2872*d289c2baSAndroid Build Coastguard Worker "--include_descriptors_from_image %s",
2873*d289c2baSAndroid Build Coastguard Worker boot_path.c_str(),
2874*d289c2baSAndroid Build Coastguard Worker system_path.c_str()));
2875*d289c2baSAndroid Build Coastguard Worker }
2876*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageWithHashAndHashtree)2877*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageWithHashAndHashtree) {
2878*d289c2baSAndroid Build Coastguard Worker GenerateImageWithHashAndHashtreeSetup();
2879*d289c2baSAndroid Build Coastguard Worker
2880*d289c2baSAndroid Build Coastguard Worker // Do two checks - one for system.img not sparse, and one where it
2881*d289c2baSAndroid Build Coastguard Worker // is sparse.
2882*d289c2baSAndroid Build Coastguard Worker for (int n = 0; n < 2; n++) {
2883*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2884*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
2885*d289c2baSAndroid Build Coastguard Worker "--image %s ",
2886*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
2887*d289c2baSAndroid Build Coastguard Worker if (n == 0) {
2888*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2889*d289c2baSAndroid Build Coastguard Worker "img2simg %s %s.sparse",
2890*d289c2baSAndroid Build Coastguard Worker (testdir_ / "system.img").c_str(),
2891*d289c2baSAndroid Build Coastguard Worker (testdir_ / "system.img").c_str());
2892*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2893*d289c2baSAndroid Build Coastguard Worker "mv %s.sparse %s",
2894*d289c2baSAndroid Build Coastguard Worker (testdir_ / "system.img").c_str(),
2895*d289c2baSAndroid Build Coastguard Worker (testdir_ / "system.img").c_str());
2896*d289c2baSAndroid Build Coastguard Worker }
2897*d289c2baSAndroid Build Coastguard Worker }
2898*d289c2baSAndroid Build Coastguard Worker }
2899*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageWithHashAndZeroedHashtree)2900*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageWithHashAndZeroedHashtree) {
2901*d289c2baSAndroid Build Coastguard Worker const size_t system_partition_size = 10 * 1024 * 1024;
2902*d289c2baSAndroid Build Coastguard Worker const size_t system_image_size = 8 * 1024 * 1024;
2903*d289c2baSAndroid Build Coastguard Worker std::string system_path = GenerateImage("system.img", system_image_size);
2904*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2905*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d --image %s "
2906*d289c2baSAndroid Build Coastguard Worker "--partition_size %zd --partition_name system "
2907*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" ",
2908*d289c2baSAndroid Build Coastguard Worker system_path.c_str(),
2909*d289c2baSAndroid Build Coastguard Worker system_partition_size);
2910*d289c2baSAndroid Build Coastguard Worker
2911*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
2912*d289c2baSAndroid Build Coastguard Worker "vbmeta.img",
2913*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
2914*d289c2baSAndroid Build Coastguard Worker 0,
2915*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
2916*d289c2baSAndroid Build Coastguard Worker android::base::StringPrintf("--include_descriptors_from_image %s ",
2917*d289c2baSAndroid Build Coastguard Worker system_path.c_str()));
2918*d289c2baSAndroid Build Coastguard Worker
2919*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2920*d289c2baSAndroid Build Coastguard Worker 0,
2921*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image --image %s --accept_zeroed_hashtree",
2922*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
2923*d289c2baSAndroid Build Coastguard Worker
2924*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2925*d289c2baSAndroid Build Coastguard Worker 0, "./avbtool.py zero_hashtree --image %s", system_path.c_str());
2926*d289c2baSAndroid Build Coastguard Worker
2927*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2928*d289c2baSAndroid Build Coastguard Worker 1, "./avbtool.py verify_image --image %s", vbmeta_image_path_.c_str());
2929*d289c2baSAndroid Build Coastguard Worker
2930*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2931*d289c2baSAndroid Build Coastguard Worker 0,
2932*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image --image %s --accept_zeroed_hashtree",
2933*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
2934*d289c2baSAndroid Build Coastguard Worker }
2935*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageWithNoHashtree)2936*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageWithNoHashtree) {
2937*d289c2baSAndroid Build Coastguard Worker const size_t system_partition_size = 10 * 1024 * 1024;
2938*d289c2baSAndroid Build Coastguard Worker const size_t system_image_size = 8 * 1024 * 1024;
2939*d289c2baSAndroid Build Coastguard Worker std::string system_path = GenerateImage("system.img", system_image_size);
2940*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
2941*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d --image %s "
2942*d289c2baSAndroid Build Coastguard Worker "--partition_size %zd --partition_name system "
2943*d289c2baSAndroid Build Coastguard Worker "--no_hashtree "
2944*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" ",
2945*d289c2baSAndroid Build Coastguard Worker system_path.c_str(),
2946*d289c2baSAndroid Build Coastguard Worker system_partition_size);
2947*d289c2baSAndroid Build Coastguard Worker
2948*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
2949*d289c2baSAndroid Build Coastguard Worker "vbmeta.img",
2950*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
2951*d289c2baSAndroid Build Coastguard Worker 0,
2952*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
2953*d289c2baSAndroid Build Coastguard Worker android::base::StringPrintf("--include_descriptors_from_image %s ",
2954*d289c2baSAndroid Build Coastguard Worker system_path.c_str()));
2955*d289c2baSAndroid Build Coastguard Worker
2956*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2957*d289c2baSAndroid Build Coastguard Worker 1, "./avbtool.py verify_image --image %s", vbmeta_image_path_.c_str());
2958*d289c2baSAndroid Build Coastguard Worker
2959*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
2960*d289c2baSAndroid Build Coastguard Worker 0,
2961*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image --image %s --accept_zeroed_hashtree",
2962*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
2963*d289c2baSAndroid Build Coastguard Worker }
2964*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageWithHashAndHashtreeCorruptHash)2965*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageWithHashAndHashtreeCorruptHash) {
2966*d289c2baSAndroid Build Coastguard Worker GenerateImageWithHashAndHashtreeSetup();
2967*d289c2baSAndroid Build Coastguard Worker
2968*d289c2baSAndroid Build Coastguard Worker // Corrupt four bytes of data in the middle of boot.img.
2969*d289c2baSAndroid Build Coastguard Worker uint8_t corrupt_data[4] = {0xff, 0xff, 0xff, 0xff};
2970*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_IO_RESULT_OK,
2971*d289c2baSAndroid Build Coastguard Worker ops_.avb_ops()->write_to_partition(ops_.avb_ops(),
2972*d289c2baSAndroid Build Coastguard Worker "boot",
2973*d289c2baSAndroid Build Coastguard Worker 105 * 1024, // offset from start
2974*d289c2baSAndroid Build Coastguard Worker sizeof corrupt_data,
2975*d289c2baSAndroid Build Coastguard Worker corrupt_data));
2976*d289c2baSAndroid Build Coastguard Worker
2977*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(1,
2978*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
2979*d289c2baSAndroid Build Coastguard Worker "--image %s ",
2980*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
2981*d289c2baSAndroid Build Coastguard Worker }
2982*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageWithHashAndHashtreeCorruptHashtree)2983*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageWithHashAndHashtreeCorruptHashtree) {
2984*d289c2baSAndroid Build Coastguard Worker GenerateImageWithHashAndHashtreeSetup();
2985*d289c2baSAndroid Build Coastguard Worker
2986*d289c2baSAndroid Build Coastguard Worker // Corrupt four bytes of data in the middle of system.img.
2987*d289c2baSAndroid Build Coastguard Worker uint8_t corrupt_data[4] = {0xff, 0xff, 0xff, 0xff};
2988*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(AVB_IO_RESULT_OK,
2989*d289c2baSAndroid Build Coastguard Worker ops_.avb_ops()->write_to_partition(ops_.avb_ops(),
2990*d289c2baSAndroid Build Coastguard Worker "system",
2991*d289c2baSAndroid Build Coastguard Worker 123 * 1024, // offset from start
2992*d289c2baSAndroid Build Coastguard Worker sizeof corrupt_data,
2993*d289c2baSAndroid Build Coastguard Worker corrupt_data));
2994*d289c2baSAndroid Build Coastguard Worker
2995*d289c2baSAndroid Build Coastguard Worker // Do two checks - one for system.img not sparse, and one where it
2996*d289c2baSAndroid Build Coastguard Worker // is sparse.
2997*d289c2baSAndroid Build Coastguard Worker for (int n = 0; n < 2; n++) {
2998*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(1,
2999*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
3000*d289c2baSAndroid Build Coastguard Worker "--image %s ",
3001*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
3002*d289c2baSAndroid Build Coastguard Worker if (n == 0) {
3003*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3004*d289c2baSAndroid Build Coastguard Worker "img2simg %s %s.sparse",
3005*d289c2baSAndroid Build Coastguard Worker (testdir_ / "system.img").c_str(),
3006*d289c2baSAndroid Build Coastguard Worker (testdir_ / "system.img").c_str());
3007*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3008*d289c2baSAndroid Build Coastguard Worker "mv %s.sparse %s",
3009*d289c2baSAndroid Build Coastguard Worker (testdir_ / "system.img").c_str(),
3010*d289c2baSAndroid Build Coastguard Worker (testdir_ / "system.img").c_str());
3011*d289c2baSAndroid Build Coastguard Worker }
3012*d289c2baSAndroid Build Coastguard Worker }
3013*d289c2baSAndroid Build Coastguard Worker }
3014*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageChainPartition)3015*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageChainPartition) {
3016*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pk4096_path = testdir_ / "testkey_rsa4096.avbpubkey";
3017*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3018*d289c2baSAndroid Build Coastguard Worker 0,
3019*d289c2baSAndroid Build Coastguard Worker "./avbtool.py extract_public_key --key test/data/testkey_rsa4096.pem"
3020*d289c2baSAndroid Build Coastguard Worker " --output %s",
3021*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str());
3022*d289c2baSAndroid Build Coastguard Worker
3023*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pk8192_path = testdir_ / "testkey_rsa8192.avbpubkey";
3024*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3025*d289c2baSAndroid Build Coastguard Worker 0,
3026*d289c2baSAndroid Build Coastguard Worker "./avbtool.py extract_public_key --key test/data/testkey_rsa8192.pem"
3027*d289c2baSAndroid Build Coastguard Worker " --output %s",
3028*d289c2baSAndroid Build Coastguard Worker pk8192_path.c_str());
3029*d289c2baSAndroid Build Coastguard Worker
3030*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
3031*d289c2baSAndroid Build Coastguard Worker "vbmeta.img",
3032*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
3033*d289c2baSAndroid Build Coastguard Worker 0,
3034*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
3035*d289c2baSAndroid Build Coastguard Worker android::base::StringPrintf("--chain_partition system:1:%s ",
3036*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str()));
3037*d289c2baSAndroid Build Coastguard Worker
3038*d289c2baSAndroid Build Coastguard Worker // Should not fail (name, rollback_index, contents all correct).
3039*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3040*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
3041*d289c2baSAndroid Build Coastguard Worker "--image %s "
3042*d289c2baSAndroid Build Coastguard Worker "--expected_chain_partition system:1:%s",
3043*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str(),
3044*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str());
3045*d289c2baSAndroid Build Coastguard Worker
3046*d289c2baSAndroid Build Coastguard Worker // Should fail because we didn't use --expected_chain_partition.
3047*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(1,
3048*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
3049*d289c2baSAndroid Build Coastguard Worker "--image %s ",
3050*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str());
3051*d289c2baSAndroid Build Coastguard Worker
3052*d289c2baSAndroid Build Coastguard Worker // Should fail because partition name is wrong.
3053*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(1,
3054*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
3055*d289c2baSAndroid Build Coastguard Worker "--image %s "
3056*d289c2baSAndroid Build Coastguard Worker "--expected_chain_partition xyz:1:%s",
3057*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str(),
3058*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str());
3059*d289c2baSAndroid Build Coastguard Worker
3060*d289c2baSAndroid Build Coastguard Worker // Should fail because rollback index location is wrong.
3061*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(1,
3062*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
3063*d289c2baSAndroid Build Coastguard Worker "--image %s "
3064*d289c2baSAndroid Build Coastguard Worker "--expected_chain_partition system:2:%s",
3065*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str(),
3066*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str());
3067*d289c2baSAndroid Build Coastguard Worker
3068*d289c2baSAndroid Build Coastguard Worker // Should fail because public key blob is wrong.
3069*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(1,
3070*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
3071*d289c2baSAndroid Build Coastguard Worker "--image %s "
3072*d289c2baSAndroid Build Coastguard Worker "--expected_chain_partition system:1:%s",
3073*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str(),
3074*d289c2baSAndroid Build Coastguard Worker pk8192_path.c_str());
3075*d289c2baSAndroid Build Coastguard Worker }
3076*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageChainPartitionWithFollow)3077*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageChainPartitionWithFollow) {
3078*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pk4096_path = testdir_ / "testkey_rsa4096.avbpubkey";
3079*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3080*d289c2baSAndroid Build Coastguard Worker 0,
3081*d289c2baSAndroid Build Coastguard Worker "./avbtool.py extract_public_key --key test/data/testkey_rsa4096.pem"
3082*d289c2baSAndroid Build Coastguard Worker " --output %s",
3083*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str());
3084*d289c2baSAndroid Build Coastguard Worker
3085*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
3086*d289c2baSAndroid Build Coastguard Worker "vbmeta.img",
3087*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
3088*d289c2baSAndroid Build Coastguard Worker 0,
3089*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
3090*d289c2baSAndroid Build Coastguard Worker android::base::StringPrintf("--chain_partition system:1:%s ",
3091*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str()));
3092*d289c2baSAndroid Build Coastguard Worker
3093*d289c2baSAndroid Build Coastguard Worker const size_t system_partition_size = 10 * 1024 * 1024;
3094*d289c2baSAndroid Build Coastguard Worker const size_t system_image_size = 8 * 1024 * 1024;
3095*d289c2baSAndroid Build Coastguard Worker std::string system_path = GenerateImage("system.img", system_image_size);
3096*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3097*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d --image %s "
3098*d289c2baSAndroid Build Coastguard Worker "--partition_size %zd --partition_name system "
3099*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA4096 "
3100*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa4096.pem "
3101*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" ",
3102*d289c2baSAndroid Build Coastguard Worker system_path.c_str(),
3103*d289c2baSAndroid Build Coastguard Worker system_partition_size);
3104*d289c2baSAndroid Build Coastguard Worker
3105*d289c2baSAndroid Build Coastguard Worker // Even without --expected_chain_partition this shouldn't fail because we use
3106*d289c2baSAndroid Build Coastguard Worker // --follow_chain_partitions and system.img exists... to avoid unstable paths
3107*d289c2baSAndroid Build Coastguard Worker // (e.g. /tmp/libavb.12345) in the output we need to run this from the test
3108*d289c2baSAndroid Build Coastguard Worker // directory itself. It's a little ugly but it works.
3109*d289c2baSAndroid Build Coastguard Worker char cwdbuf[PATH_MAX];
3110*d289c2baSAndroid Build Coastguard Worker ASSERT_NE(nullptr, getcwd(cwdbuf, sizeof cwdbuf));
3111*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3112*d289c2baSAndroid Build Coastguard Worker "cd %s && (%s/avbtool.py verify_image "
3113*d289c2baSAndroid Build Coastguard Worker "--image vbmeta.img --follow_chain_partitions > out.txt)",
3114*d289c2baSAndroid Build Coastguard Worker testdir_.c_str(),
3115*d289c2baSAndroid Build Coastguard Worker cwdbuf);
3116*d289c2baSAndroid Build Coastguard Worker std::filesystem::path out_path = testdir_ / "out.txt";
3117*d289c2baSAndroid Build Coastguard Worker std::string out;
3118*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(out_path.string(), &out));
3119*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
3120*d289c2baSAndroid Build Coastguard Worker "Verifying image vbmeta.img using embedded public key\n"
3121*d289c2baSAndroid Build Coastguard Worker "vbmeta: Successfully verified SHA256_RSA2048 vbmeta struct in "
3122*d289c2baSAndroid Build Coastguard Worker "vbmeta.img\n"
3123*d289c2baSAndroid Build Coastguard Worker "system: Chained but ROLLBACK_SLOT (which is 1) and KEY (which has sha1 "
3124*d289c2baSAndroid Build Coastguard Worker "2597c218aae470a130f61162feaae70afd97f011) not specified\n"
3125*d289c2baSAndroid Build Coastguard Worker "--\n"
3126*d289c2baSAndroid Build Coastguard Worker "Verifying image system.img using embedded public key\n"
3127*d289c2baSAndroid Build Coastguard Worker "vbmeta: Successfully verified footer and SHA256_RSA4096 vbmeta struct "
3128*d289c2baSAndroid Build Coastguard Worker "in system.img\n"
3129*d289c2baSAndroid Build Coastguard Worker "system: Successfully verified sha1 hashtree of system.img for image of "
3130*d289c2baSAndroid Build Coastguard Worker "8388608 bytes\n",
3131*d289c2baSAndroid Build Coastguard Worker out);
3132*d289c2baSAndroid Build Coastguard Worker
3133*d289c2baSAndroid Build Coastguard Worker // Make sure we also follow partitions *even* when specifying
3134*d289c2baSAndroid Build Coastguard Worker // --expect_chain_partition. The output is slightly different from above.
3135*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3136*d289c2baSAndroid Build Coastguard Worker "cd %s && (%s/avbtool.py verify_image "
3137*d289c2baSAndroid Build Coastguard Worker "--image vbmeta.img --expected_chain_partition system:1:%s "
3138*d289c2baSAndroid Build Coastguard Worker "--follow_chain_partitions > out.txt)",
3139*d289c2baSAndroid Build Coastguard Worker testdir_.c_str(),
3140*d289c2baSAndroid Build Coastguard Worker cwdbuf,
3141*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str());
3142*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(out_path.string(), &out));
3143*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
3144*d289c2baSAndroid Build Coastguard Worker "Verifying image vbmeta.img using embedded public key\n"
3145*d289c2baSAndroid Build Coastguard Worker "vbmeta: Successfully verified SHA256_RSA2048 vbmeta struct in "
3146*d289c2baSAndroid Build Coastguard Worker "vbmeta.img\n"
3147*d289c2baSAndroid Build Coastguard Worker "system: Successfully verified chain partition descriptor matches "
3148*d289c2baSAndroid Build Coastguard Worker "expected data\n"
3149*d289c2baSAndroid Build Coastguard Worker "--\n"
3150*d289c2baSAndroid Build Coastguard Worker "Verifying image system.img using embedded public key\n"
3151*d289c2baSAndroid Build Coastguard Worker "vbmeta: Successfully verified footer and SHA256_RSA4096 vbmeta struct "
3152*d289c2baSAndroid Build Coastguard Worker "in system.img\n"
3153*d289c2baSAndroid Build Coastguard Worker "system: Successfully verified sha1 hashtree of system.img for image of "
3154*d289c2baSAndroid Build Coastguard Worker "8388608 bytes\n",
3155*d289c2baSAndroid Build Coastguard Worker out);
3156*d289c2baSAndroid Build Coastguard Worker }
3157*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,VerifyImageChainPartitionOtherVBMeta)3158*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, VerifyImageChainPartitionOtherVBMeta) {
3159*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pk4096_path = testdir_ / "testkey_rsa4096.avbpubkey";
3160*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3161*d289c2baSAndroid Build Coastguard Worker 0,
3162*d289c2baSAndroid Build Coastguard Worker "./avbtool.py extract_public_key --key test/data/testkey_rsa4096.pem"
3163*d289c2baSAndroid Build Coastguard Worker " --output %s",
3164*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str());
3165*d289c2baSAndroid Build Coastguard Worker
3166*d289c2baSAndroid Build Coastguard Worker const size_t system_partition_size = 10 * 1024 * 1024;
3167*d289c2baSAndroid Build Coastguard Worker const size_t system_image_size = 8 * 1024 * 1024;
3168*d289c2baSAndroid Build Coastguard Worker std::string system_path = GenerateImage("system.img", system_image_size);
3169*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3170*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d --image %s "
3171*d289c2baSAndroid Build Coastguard Worker "--partition_size %zd --partition_name system "
3172*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" "
3173*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA4096 "
3174*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa4096.pem ",
3175*d289c2baSAndroid Build Coastguard Worker system_path.c_str(),
3176*d289c2baSAndroid Build Coastguard Worker system_partition_size,
3177*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str());
3178*d289c2baSAndroid Build Coastguard Worker
3179*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
3180*d289c2baSAndroid Build Coastguard Worker "vbmeta.img",
3181*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
3182*d289c2baSAndroid Build Coastguard Worker 0,
3183*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
3184*d289c2baSAndroid Build Coastguard Worker android::base::StringPrintf("--chain_partition vbmeta_google:1:%s ",
3185*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str()));
3186*d289c2baSAndroid Build Coastguard Worker
3187*d289c2baSAndroid Build Coastguard Worker // Should not fail (name, rollback_index, contents all correct).
3188*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3189*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
3190*d289c2baSAndroid Build Coastguard Worker "--image %s "
3191*d289c2baSAndroid Build Coastguard Worker "--expected_chain_partition vbmeta_google:1:%s",
3192*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str(),
3193*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str());
3194*d289c2baSAndroid Build Coastguard Worker
3195*d289c2baSAndroid Build Coastguard Worker // Should not fail (looks in system.img image).
3196*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3197*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
3198*d289c2baSAndroid Build Coastguard Worker "--image %s ",
3199*d289c2baSAndroid Build Coastguard Worker system_path.c_str());
3200*d289c2baSAndroid Build Coastguard Worker
3201*d289c2baSAndroid Build Coastguard Worker // Extract the vbmeta blob from the footer in system.img, put it into
3202*d289c2baSAndroid Build Coastguard Worker // vbmeta_google.img, and erase the footer from system.img (but keep
3203*d289c2baSAndroid Build Coastguard Worker // the hash tree in system.img)
3204*d289c2baSAndroid Build Coastguard Worker std::string vbmeta_google_path = GenerateImage("vbmeta_google.img", 0);
3205*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3206*d289c2baSAndroid Build Coastguard Worker "./avbtool.py extract_vbmeta_image"
3207*d289c2baSAndroid Build Coastguard Worker " --image %s"
3208*d289c2baSAndroid Build Coastguard Worker " --output %s",
3209*d289c2baSAndroid Build Coastguard Worker system_path.c_str(),
3210*d289c2baSAndroid Build Coastguard Worker vbmeta_google_path.c_str());
3211*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3212*d289c2baSAndroid Build Coastguard Worker "./avbtool.py erase_footer"
3213*d289c2baSAndroid Build Coastguard Worker " --image %s --keep_hashtree",
3214*d289c2baSAndroid Build Coastguard Worker system_path.c_str());
3215*d289c2baSAndroid Build Coastguard Worker
3216*d289c2baSAndroid Build Coastguard Worker // Should not fail - looks in system.img's detached vbmeta (vbmeta_google.img)
3217*d289c2baSAndroid Build Coastguard Worker // for vbmeta blob and system.img for the actual hashtree.
3218*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3219*d289c2baSAndroid Build Coastguard Worker "./avbtool.py verify_image "
3220*d289c2baSAndroid Build Coastguard Worker "--image %s ",
3221*d289c2baSAndroid Build Coastguard Worker vbmeta_google_path.c_str());
3222*d289c2baSAndroid Build Coastguard Worker }
3223*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,PrintPartitionDigests)3224*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, PrintPartitionDigests) {
3225*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pk4096_path = testdir_ / "testkey_rsa4096.avbpubkey";
3226*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3227*d289c2baSAndroid Build Coastguard Worker 0,
3228*d289c2baSAndroid Build Coastguard Worker "./avbtool.py extract_public_key --key test/data/testkey_rsa4096.pem"
3229*d289c2baSAndroid Build Coastguard Worker " --output %s",
3230*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str());
3231*d289c2baSAndroid Build Coastguard Worker
3232*d289c2baSAndroid Build Coastguard Worker const size_t boot_partition_size = 16 * 1024 * 1024;
3233*d289c2baSAndroid Build Coastguard Worker const size_t boot_image_size = 5 * 1024 * 1024;
3234*d289c2baSAndroid Build Coastguard Worker std::string boot_path = GenerateImage("boot.img", boot_image_size);
3235*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3236*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer"
3237*d289c2baSAndroid Build Coastguard Worker " --image %s"
3238*d289c2baSAndroid Build Coastguard Worker " --rollback_index 0"
3239*d289c2baSAndroid Build Coastguard Worker " --partition_name boot"
3240*d289c2baSAndroid Build Coastguard Worker " --partition_size %zd"
3241*d289c2baSAndroid Build Coastguard Worker " --salt deadbeef"
3242*d289c2baSAndroid Build Coastguard Worker " --internal_release_string \"\"",
3243*d289c2baSAndroid Build Coastguard Worker boot_path.c_str(),
3244*d289c2baSAndroid Build Coastguard Worker boot_partition_size);
3245*d289c2baSAndroid Build Coastguard Worker
3246*d289c2baSAndroid Build Coastguard Worker GenerateVBMetaImage(
3247*d289c2baSAndroid Build Coastguard Worker "vbmeta.img",
3248*d289c2baSAndroid Build Coastguard Worker "SHA256_RSA2048",
3249*d289c2baSAndroid Build Coastguard Worker 0,
3250*d289c2baSAndroid Build Coastguard Worker "test/data/testkey_rsa2048.pem",
3251*d289c2baSAndroid Build Coastguard Worker android::base::StringPrintf("--chain_partition system:1:%s "
3252*d289c2baSAndroid Build Coastguard Worker "--include_descriptors_from_image %s",
3253*d289c2baSAndroid Build Coastguard Worker pk4096_path.c_str(),
3254*d289c2baSAndroid Build Coastguard Worker boot_path.c_str()));
3255*d289c2baSAndroid Build Coastguard Worker
3256*d289c2baSAndroid Build Coastguard Worker const size_t system_partition_size = 10 * 1024 * 1024;
3257*d289c2baSAndroid Build Coastguard Worker const size_t system_image_size = 8 * 1024 * 1024;
3258*d289c2baSAndroid Build Coastguard Worker std::string system_path = GenerateImage("system.img", system_image_size);
3259*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3260*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d --image %s "
3261*d289c2baSAndroid Build Coastguard Worker "--partition_size %zd --partition_name system "
3262*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA4096 "
3263*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa4096.pem "
3264*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" ",
3265*d289c2baSAndroid Build Coastguard Worker system_path.c_str(),
3266*d289c2baSAndroid Build Coastguard Worker system_partition_size);
3267*d289c2baSAndroid Build Coastguard Worker
3268*d289c2baSAndroid Build Coastguard Worker std::filesystem::path out_path = testdir_ / "out.txt";
3269*d289c2baSAndroid Build Coastguard Worker std::string out;
3270*d289c2baSAndroid Build Coastguard Worker
3271*d289c2baSAndroid Build Coastguard Worker // Normal output
3272*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3273*d289c2baSAndroid Build Coastguard Worker "./avbtool.py print_partition_digests --image %s --output %s",
3274*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str(),
3275*d289c2baSAndroid Build Coastguard Worker out_path.c_str());
3276*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(out_path.string(), &out));
3277*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
3278*d289c2baSAndroid Build Coastguard Worker "system: d52d93c988d336a79abe1c05240ae9a79a9b7d61\n"
3279*d289c2baSAndroid Build Coastguard Worker "boot: "
3280*d289c2baSAndroid Build Coastguard Worker "184cb36243adb8b87d2d8c4802de32125fe294ec46753d732144ee65df68a23d\n",
3281*d289c2baSAndroid Build Coastguard Worker out);
3282*d289c2baSAndroid Build Coastguard Worker
3283*d289c2baSAndroid Build Coastguard Worker // JSON output
3284*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3285*d289c2baSAndroid Build Coastguard Worker 0,
3286*d289c2baSAndroid Build Coastguard Worker "./avbtool.py print_partition_digests --image %s --json --output %s",
3287*d289c2baSAndroid Build Coastguard Worker vbmeta_image_path_.c_str(),
3288*d289c2baSAndroid Build Coastguard Worker out_path.c_str());
3289*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(out_path.string(), &out));
3290*d289c2baSAndroid Build Coastguard Worker // The trailing whitespace comes from python. If they fix that bug we need
3291*d289c2baSAndroid Build Coastguard Worker // to update this test...
3292*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
3293*d289c2baSAndroid Build Coastguard Worker "{\n"
3294*d289c2baSAndroid Build Coastguard Worker " \"partitions\": [\n"
3295*d289c2baSAndroid Build Coastguard Worker " {\n"
3296*d289c2baSAndroid Build Coastguard Worker " \"name\": \"system\",\n"
3297*d289c2baSAndroid Build Coastguard Worker " \"digest\": \"d52d93c988d336a79abe1c05240ae9a79a9b7d61\"\n"
3298*d289c2baSAndroid Build Coastguard Worker " },\n"
3299*d289c2baSAndroid Build Coastguard Worker " {\n"
3300*d289c2baSAndroid Build Coastguard Worker " \"name\": \"boot\",\n"
3301*d289c2baSAndroid Build Coastguard Worker " \"digest\": "
3302*d289c2baSAndroid Build Coastguard Worker "\"184cb36243adb8b87d2d8c4802de32125fe294ec46753d732144ee65df68a23d\"\n"
3303*d289c2baSAndroid Build Coastguard Worker " }\n"
3304*d289c2baSAndroid Build Coastguard Worker " ]\n"
3305*d289c2baSAndroid Build Coastguard Worker "}",
3306*d289c2baSAndroid Build Coastguard Worker out);
3307*d289c2baSAndroid Build Coastguard Worker }
3308*d289c2baSAndroid Build Coastguard Worker
3309*d289c2baSAndroid Build Coastguard Worker class AvbToolTest_PrintRequiredVersion : public AvbToolTest {
3310*d289c2baSAndroid Build Coastguard Worker protected:
3311*d289c2baSAndroid Build Coastguard Worker const char* kOutputFile = "versions.txt";
3312*d289c2baSAndroid Build Coastguard Worker
PrintWithAddHashFooter(int target_required_minor_version)3313*d289c2baSAndroid Build Coastguard Worker void PrintWithAddHashFooter(int target_required_minor_version) {
3314*d289c2baSAndroid Build Coastguard Worker std::string extra_args;
3315*d289c2baSAndroid Build Coastguard Worker if (target_required_minor_version == 1) {
3316*d289c2baSAndroid Build Coastguard Worker // The --do_not_use_ab option will require 1.1.
3317*d289c2baSAndroid Build Coastguard Worker extra_args = "--do_not_use_ab";
3318*d289c2baSAndroid Build Coastguard Worker } else if (target_required_minor_version == 2) {
3319*d289c2baSAndroid Build Coastguard Worker extra_args = "--rollback_index_location 2";
3320*d289c2baSAndroid Build Coastguard Worker }
3321*d289c2baSAndroid Build Coastguard Worker
3322*d289c2baSAndroid Build Coastguard Worker const size_t boot_partition_size = 16 * 1024 * 1024;
3323*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / kOutputFile;
3324*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3325*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer"
3326*d289c2baSAndroid Build Coastguard Worker " --rollback_index 0"
3327*d289c2baSAndroid Build Coastguard Worker " --partition_name boot"
3328*d289c2baSAndroid Build Coastguard Worker " --partition_size %zd"
3329*d289c2baSAndroid Build Coastguard Worker " --salt deadbeef"
3330*d289c2baSAndroid Build Coastguard Worker " --internal_release_string \"\""
3331*d289c2baSAndroid Build Coastguard Worker " %s"
3332*d289c2baSAndroid Build Coastguard Worker " --print_required_libavb_version > %s",
3333*d289c2baSAndroid Build Coastguard Worker boot_partition_size,
3334*d289c2baSAndroid Build Coastguard Worker extra_args.c_str(),
3335*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
3336*d289c2baSAndroid Build Coastguard Worker CheckVersion(target_required_minor_version);
3337*d289c2baSAndroid Build Coastguard Worker }
3338*d289c2baSAndroid Build Coastguard Worker
PrintWithAddHashtreeFooter(int target_required_minor_version)3339*d289c2baSAndroid Build Coastguard Worker void PrintWithAddHashtreeFooter(int target_required_minor_version) {
3340*d289c2baSAndroid Build Coastguard Worker std::string extra_args;
3341*d289c2baSAndroid Build Coastguard Worker if (target_required_minor_version == 1) {
3342*d289c2baSAndroid Build Coastguard Worker // The --do_not_use_ab option will require 1.1.
3343*d289c2baSAndroid Build Coastguard Worker extra_args = "--do_not_use_ab --check_at_most_once";
3344*d289c2baSAndroid Build Coastguard Worker } else if (target_required_minor_version == 2) {
3345*d289c2baSAndroid Build Coastguard Worker extra_args = "--rollback_index_location 2";
3346*d289c2baSAndroid Build Coastguard Worker }
3347*d289c2baSAndroid Build Coastguard Worker const size_t system_partition_size = 10 * 1024 * 1024;
3348*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / kOutputFile;
3349*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3350*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hashtree_footer --salt d00df00d "
3351*d289c2baSAndroid Build Coastguard Worker "--partition_size %zd --partition_name system "
3352*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\""
3353*d289c2baSAndroid Build Coastguard Worker " %s"
3354*d289c2baSAndroid Build Coastguard Worker " --print_required_libavb_version > %s",
3355*d289c2baSAndroid Build Coastguard Worker system_partition_size,
3356*d289c2baSAndroid Build Coastguard Worker extra_args.c_str(),
3357*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
3358*d289c2baSAndroid Build Coastguard Worker CheckVersion(target_required_minor_version);
3359*d289c2baSAndroid Build Coastguard Worker }
3360*d289c2baSAndroid Build Coastguard Worker
PrintWithMakeVbmetaImage(int target_required_minor_version)3361*d289c2baSAndroid Build Coastguard Worker void PrintWithMakeVbmetaImage(int target_required_minor_version) {
3362*d289c2baSAndroid Build Coastguard Worker std::string extra_args;
3363*d289c2baSAndroid Build Coastguard Worker if (target_required_minor_version == 1) {
3364*d289c2baSAndroid Build Coastguard Worker // An included descriptor that requires 1.1 will require 1.1 for vbmeta.
3365*d289c2baSAndroid Build Coastguard Worker const size_t boot_partition_size = 16 * 1024 * 1024;
3366*d289c2baSAndroid Build Coastguard Worker std::string image_path = GenerateImage("test_print_version", 1024);
3367*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3368*d289c2baSAndroid Build Coastguard Worker "./avbtool.py add_hash_footer --salt d00df00d "
3369*d289c2baSAndroid Build Coastguard Worker "--hash_algorithm sha256 --image %s "
3370*d289c2baSAndroid Build Coastguard Worker "--partition_size %d --partition_name foobar "
3371*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
3372*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
3373*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\" "
3374*d289c2baSAndroid Build Coastguard Worker "--do_not_use_ab",
3375*d289c2baSAndroid Build Coastguard Worker image_path.c_str(),
3376*d289c2baSAndroid Build Coastguard Worker (int)boot_partition_size);
3377*d289c2baSAndroid Build Coastguard Worker extra_args = android::base::StringPrintf(
3378*d289c2baSAndroid Build Coastguard Worker "--include_descriptors_from_image %s", image_path.c_str());
3379*d289c2baSAndroid Build Coastguard Worker } else if (target_required_minor_version == 2) {
3380*d289c2baSAndroid Build Coastguard Worker extra_args = "--rollback_index_location 2";
3381*d289c2baSAndroid Build Coastguard Worker }
3382*d289c2baSAndroid Build Coastguard Worker
3383*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / kOutputFile;
3384*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3385*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_vbmeta_image "
3386*d289c2baSAndroid Build Coastguard Worker "--algorithm SHA256_RSA2048 "
3387*d289c2baSAndroid Build Coastguard Worker "--key test/data/testkey_rsa2048.pem "
3388*d289c2baSAndroid Build Coastguard Worker "--internal_release_string \"\""
3389*d289c2baSAndroid Build Coastguard Worker " %s"
3390*d289c2baSAndroid Build Coastguard Worker " --print_required_libavb_version > %s",
3391*d289c2baSAndroid Build Coastguard Worker extra_args.c_str(),
3392*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
3393*d289c2baSAndroid Build Coastguard Worker CheckVersion(target_required_minor_version);
3394*d289c2baSAndroid Build Coastguard Worker }
3395*d289c2baSAndroid Build Coastguard Worker
CheckVersion(int expected_required_minor_version)3396*d289c2baSAndroid Build Coastguard Worker void CheckVersion(int expected_required_minor_version) {
3397*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / kOutputFile;
3398*d289c2baSAndroid Build Coastguard Worker std::string output;
3399*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(android::base::ReadFileToString(output_path.string(), &output));
3400*d289c2baSAndroid Build Coastguard Worker EXPECT_EQ(
3401*d289c2baSAndroid Build Coastguard Worker output,
3402*d289c2baSAndroid Build Coastguard Worker android::base::StringPrintf("1.%d\n", expected_required_minor_version));
3403*d289c2baSAndroid Build Coastguard Worker }
3404*d289c2baSAndroid Build Coastguard Worker };
3405*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest_PrintRequiredVersion,HashFooter_1_0)3406*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest_PrintRequiredVersion, HashFooter_1_0) {
3407*d289c2baSAndroid Build Coastguard Worker PrintWithAddHashFooter(0);
3408*d289c2baSAndroid Build Coastguard Worker }
3409*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest_PrintRequiredVersion,HashFooter_1_1)3410*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest_PrintRequiredVersion, HashFooter_1_1) {
3411*d289c2baSAndroid Build Coastguard Worker PrintWithAddHashFooter(1);
3412*d289c2baSAndroid Build Coastguard Worker }
3413*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest_PrintRequiredVersion,HashFooter_1_2)3414*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest_PrintRequiredVersion, HashFooter_1_2) {
3415*d289c2baSAndroid Build Coastguard Worker PrintWithAddHashFooter(2);
3416*d289c2baSAndroid Build Coastguard Worker }
3417*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest_PrintRequiredVersion,HashtreeFooter_1_0)3418*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest_PrintRequiredVersion, HashtreeFooter_1_0) {
3419*d289c2baSAndroid Build Coastguard Worker PrintWithAddHashtreeFooter(0);
3420*d289c2baSAndroid Build Coastguard Worker }
3421*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest_PrintRequiredVersion,HashtreeFooter_1_1)3422*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest_PrintRequiredVersion, HashtreeFooter_1_1) {
3423*d289c2baSAndroid Build Coastguard Worker PrintWithAddHashtreeFooter(1);
3424*d289c2baSAndroid Build Coastguard Worker }
3425*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest_PrintRequiredVersion,HashtreeFooter_1_2)3426*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest_PrintRequiredVersion, HashtreeFooter_1_2) {
3427*d289c2baSAndroid Build Coastguard Worker PrintWithAddHashtreeFooter(2);
3428*d289c2baSAndroid Build Coastguard Worker }
3429*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest_PrintRequiredVersion,Vbmeta_1_0)3430*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest_PrintRequiredVersion, Vbmeta_1_0) {
3431*d289c2baSAndroid Build Coastguard Worker PrintWithMakeVbmetaImage(0);
3432*d289c2baSAndroid Build Coastguard Worker }
3433*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest_PrintRequiredVersion,Vbmeta_1_1)3434*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest_PrintRequiredVersion, Vbmeta_1_1) {
3435*d289c2baSAndroid Build Coastguard Worker PrintWithMakeVbmetaImage(1);
3436*d289c2baSAndroid Build Coastguard Worker }
3437*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest_PrintRequiredVersion,Vbmeta_1_2)3438*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest_PrintRequiredVersion, Vbmeta_1_2) {
3439*d289c2baSAndroid Build Coastguard Worker PrintWithMakeVbmetaImage(2);
3440*d289c2baSAndroid Build Coastguard Worker }
3441*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,MakeCertPikCertificate)3442*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, MakeCertPikCertificate) {
3443*d289c2baSAndroid Build Coastguard Worker std::filesystem::path subject_path = testdir_ / "tmp_subject";
3444*d289c2baSAndroid Build Coastguard Worker ASSERT_TRUE(base::WriteFile(
3445*d289c2baSAndroid Build Coastguard Worker base::FilePath(subject_path.c_str()), "fake PIK subject", 16));
3446*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pubkey_path = testdir_ / "tmp_pubkey.pem";
3447*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3448*d289c2baSAndroid Build Coastguard Worker 0,
3449*d289c2baSAndroid Build Coastguard Worker "openssl pkey -pubout -in test/data/testkey_cert_pik.pem -out %s",
3450*d289c2baSAndroid Build Coastguard Worker pubkey_path.c_str());
3451*d289c2baSAndroid Build Coastguard Worker
3452*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / "tmp_certificate.bin";
3453*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3454*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_certificate"
3455*d289c2baSAndroid Build Coastguard Worker " --subject %s"
3456*d289c2baSAndroid Build Coastguard Worker " --subject_key %s"
3457*d289c2baSAndroid Build Coastguard Worker " --subject_key_version 42"
3458*d289c2baSAndroid Build Coastguard Worker " --subject_is_intermediate_authority"
3459*d289c2baSAndroid Build Coastguard Worker " --authority_key test/data/testkey_cert_prk.pem"
3460*d289c2baSAndroid Build Coastguard Worker " --output %s",
3461*d289c2baSAndroid Build Coastguard Worker subject_path.c_str(),
3462*d289c2baSAndroid Build Coastguard Worker pubkey_path.c_str(),
3463*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
3464*d289c2baSAndroid Build Coastguard Worker
3465*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3466*d289c2baSAndroid Build Coastguard Worker 0, "diff test/data/cert_pik_certificate.bin %s", output_path.c_str());
3467*d289c2baSAndroid Build Coastguard Worker }
3468*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,MakeCertPskCertificate)3469*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, MakeCertPskCertificate) {
3470*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pubkey_path = testdir_ / "tmp_pubkey.pem";
3471*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3472*d289c2baSAndroid Build Coastguard Worker 0,
3473*d289c2baSAndroid Build Coastguard Worker "openssl pkey -pubout -in test/data/testkey_cert_psk.pem -out %s",
3474*d289c2baSAndroid Build Coastguard Worker pubkey_path.c_str());
3475*d289c2baSAndroid Build Coastguard Worker
3476*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / "tmp_certificate.bin";
3477*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3478*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_certificate"
3479*d289c2baSAndroid Build Coastguard Worker " --subject test/data/cert_product_id.bin"
3480*d289c2baSAndroid Build Coastguard Worker " --subject_key %s"
3481*d289c2baSAndroid Build Coastguard Worker " --subject_key_version 42"
3482*d289c2baSAndroid Build Coastguard Worker " --authority_key test/data/testkey_cert_pik.pem"
3483*d289c2baSAndroid Build Coastguard Worker " --output %s",
3484*d289c2baSAndroid Build Coastguard Worker pubkey_path.c_str(),
3485*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
3486*d289c2baSAndroid Build Coastguard Worker
3487*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3488*d289c2baSAndroid Build Coastguard Worker 0, "diff test/data/cert_psk_certificate.bin %s", output_path.c_str());
3489*d289c2baSAndroid Build Coastguard Worker }
3490*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,MakeCertPukCertificate)3491*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, MakeCertPukCertificate) {
3492*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pubkey_path = testdir_ / "tmp_pubkey.pem";
3493*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3494*d289c2baSAndroid Build Coastguard Worker 0,
3495*d289c2baSAndroid Build Coastguard Worker "openssl pkey -pubout -in test/data/testkey_cert_puk.pem -out %s",
3496*d289c2baSAndroid Build Coastguard Worker pubkey_path.c_str());
3497*d289c2baSAndroid Build Coastguard Worker
3498*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / "tmp_certificate.bin";
3499*d289c2baSAndroid Build Coastguard Worker
3500*d289c2baSAndroid Build Coastguard Worker // Test with both legacy manual unlock --usage as well as --usage_for_unlock.
3501*d289c2baSAndroid Build Coastguard Worker std::string usage_args[] = {"--usage com.google.android.things.vboot.unlock",
3502*d289c2baSAndroid Build Coastguard Worker "--usage_for_unlock"};
3503*d289c2baSAndroid Build Coastguard Worker for (const auto& usage : usage_args) {
3504*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3505*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_certificate"
3506*d289c2baSAndroid Build Coastguard Worker " --subject test/data/cert_product_id.bin"
3507*d289c2baSAndroid Build Coastguard Worker " --subject_key %s"
3508*d289c2baSAndroid Build Coastguard Worker " --subject_key_version 42"
3509*d289c2baSAndroid Build Coastguard Worker " %s"
3510*d289c2baSAndroid Build Coastguard Worker " --authority_key test/data/testkey_cert_pik.pem"
3511*d289c2baSAndroid Build Coastguard Worker " --output %s",
3512*d289c2baSAndroid Build Coastguard Worker pubkey_path.c_str(),
3513*d289c2baSAndroid Build Coastguard Worker usage.c_str(),
3514*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
3515*d289c2baSAndroid Build Coastguard Worker
3516*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3517*d289c2baSAndroid Build Coastguard Worker 0, "diff test/data/cert_puk_certificate.bin %s", output_path.c_str());
3518*d289c2baSAndroid Build Coastguard Worker }
3519*d289c2baSAndroid Build Coastguard Worker }
3520*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,MakeCertPermanentAttributes)3521*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, MakeCertPermanentAttributes) {
3522*d289c2baSAndroid Build Coastguard Worker std::filesystem::path pubkey_path = testdir_ / "tmp_pubkey.pem";
3523*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3524*d289c2baSAndroid Build Coastguard Worker 0,
3525*d289c2baSAndroid Build Coastguard Worker "openssl pkey -pubout -in test/data/testkey_cert_prk.pem -out %s",
3526*d289c2baSAndroid Build Coastguard Worker pubkey_path.c_str());
3527*d289c2baSAndroid Build Coastguard Worker
3528*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / "tmp_attributes.bin";
3529*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3530*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_cert_permanent_attributes"
3531*d289c2baSAndroid Build Coastguard Worker " --root_authority_key %s"
3532*d289c2baSAndroid Build Coastguard Worker " --product_id test/data/cert_product_id.bin"
3533*d289c2baSAndroid Build Coastguard Worker " --output %s",
3534*d289c2baSAndroid Build Coastguard Worker pubkey_path.c_str(),
3535*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
3536*d289c2baSAndroid Build Coastguard Worker
3537*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0,
3538*d289c2baSAndroid Build Coastguard Worker "diff test/data/cert_permanent_attributes.bin %s",
3539*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
3540*d289c2baSAndroid Build Coastguard Worker }
3541*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,MakeCertMetadata)3542*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, MakeCertMetadata) {
3543*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / "tmp_metadata.bin";
3544*d289c2baSAndroid Build Coastguard Worker
3545*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3546*d289c2baSAndroid Build Coastguard Worker 0,
3547*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_cert_metadata"
3548*d289c2baSAndroid Build Coastguard Worker " --intermediate_key_certificate test/data/cert_pik_certificate.bin"
3549*d289c2baSAndroid Build Coastguard Worker " --product_key_certificate test/data/cert_psk_certificate.bin"
3550*d289c2baSAndroid Build Coastguard Worker " --output %s",
3551*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
3552*d289c2baSAndroid Build Coastguard Worker
3553*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(0, "diff test/data/cert_metadata.bin %s", output_path.c_str());
3554*d289c2baSAndroid Build Coastguard Worker }
3555*d289c2baSAndroid Build Coastguard Worker
TEST_F(AvbToolTest,MakeCertUnlockCredential)3556*d289c2baSAndroid Build Coastguard Worker TEST_F(AvbToolTest, MakeCertUnlockCredential) {
3557*d289c2baSAndroid Build Coastguard Worker std::filesystem::path output_path = testdir_ / "tmp_credential.bin";
3558*d289c2baSAndroid Build Coastguard Worker
3559*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3560*d289c2baSAndroid Build Coastguard Worker 0,
3561*d289c2baSAndroid Build Coastguard Worker "./avbtool.py make_cert_unlock_credential"
3562*d289c2baSAndroid Build Coastguard Worker " --intermediate_key_certificate test/data/cert_pik_certificate.bin"
3563*d289c2baSAndroid Build Coastguard Worker " --unlock_key_certificate test/data/cert_puk_certificate.bin"
3564*d289c2baSAndroid Build Coastguard Worker " --challenge test/data/cert_unlock_challenge.bin"
3565*d289c2baSAndroid Build Coastguard Worker " --unlock_key test/data/testkey_cert_puk.pem"
3566*d289c2baSAndroid Build Coastguard Worker " --output %s",
3567*d289c2baSAndroid Build Coastguard Worker output_path.c_str());
3568*d289c2baSAndroid Build Coastguard Worker
3569*d289c2baSAndroid Build Coastguard Worker EXPECT_COMMAND(
3570*d289c2baSAndroid Build Coastguard Worker 0, "diff test/data/cert_unlock_credential.bin %s", output_path.c_str());
3571*d289c2baSAndroid Build Coastguard Worker }
3572*d289c2baSAndroid Build Coastguard Worker
3573*d289c2baSAndroid Build Coastguard Worker } // namespace avb
3574