1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker *
4*288bf522SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker *
8*288bf522SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker *
10*288bf522SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker */
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Worker #include "verity/build_verity_tree.h"
18*288bf522SAndroid Build Coastguard Worker
19*288bf522SAndroid Build Coastguard Worker #include <android-base/logging.h>
20*288bf522SAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
21*288bf522SAndroid Build Coastguard Worker #include <sparse/sparse.h>
22*288bf522SAndroid Build Coastguard Worker
23*288bf522SAndroid Build Coastguard Worker #undef NDEBUG
24*288bf522SAndroid Build Coastguard Worker
generate_verity_tree(const std::string & data_filename,const std::string & verity_filename,HashTreeBuilder * builder,const std::vector<unsigned char> & salt_content,size_t block_size,bool sparse,bool verbose)25*288bf522SAndroid Build Coastguard Worker bool generate_verity_tree(const std::string& data_filename,
26*288bf522SAndroid Build Coastguard Worker const std::string& verity_filename,
27*288bf522SAndroid Build Coastguard Worker HashTreeBuilder* builder,
28*288bf522SAndroid Build Coastguard Worker const std::vector<unsigned char>& salt_content,
29*288bf522SAndroid Build Coastguard Worker size_t block_size, bool sparse, bool verbose) {
30*288bf522SAndroid Build Coastguard Worker android::base::unique_fd data_fd(open(data_filename.c_str(), O_RDONLY));
31*288bf522SAndroid Build Coastguard Worker if (data_fd == -1) {
32*288bf522SAndroid Build Coastguard Worker PLOG(ERROR) << "failed to open " << data_filename;
33*288bf522SAndroid Build Coastguard Worker return false;
34*288bf522SAndroid Build Coastguard Worker }
35*288bf522SAndroid Build Coastguard Worker
36*288bf522SAndroid Build Coastguard Worker std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)> file(nullptr, sparse_file_destroy);
37*288bf522SAndroid Build Coastguard Worker if (sparse) {
38*288bf522SAndroid Build Coastguard Worker file.reset(sparse_file_import(data_fd, false, false));
39*288bf522SAndroid Build Coastguard Worker } else {
40*288bf522SAndroid Build Coastguard Worker file.reset(sparse_file_import_auto(data_fd, false, verbose));
41*288bf522SAndroid Build Coastguard Worker }
42*288bf522SAndroid Build Coastguard Worker
43*288bf522SAndroid Build Coastguard Worker if (!file) {
44*288bf522SAndroid Build Coastguard Worker LOG(ERROR) << "failed to read file " << data_filename;
45*288bf522SAndroid Build Coastguard Worker return false;
46*288bf522SAndroid Build Coastguard Worker }
47*288bf522SAndroid Build Coastguard Worker
48*288bf522SAndroid Build Coastguard Worker int64_t len = sparse_file_len(file.get(), false, false);
49*288bf522SAndroid Build Coastguard Worker if (len % block_size != 0) {
50*288bf522SAndroid Build Coastguard Worker LOG(ERROR) << "file size " << len << " is not a multiple of " << block_size
51*288bf522SAndroid Build Coastguard Worker << " byte";
52*288bf522SAndroid Build Coastguard Worker return false;
53*288bf522SAndroid Build Coastguard Worker }
54*288bf522SAndroid Build Coastguard Worker
55*288bf522SAndroid Build Coastguard Worker // Initialize the builder to compute the hash tree.
56*288bf522SAndroid Build Coastguard Worker if (!builder->Initialize(len, salt_content)) {
57*288bf522SAndroid Build Coastguard Worker LOG(ERROR) << "Failed to initialize HashTreeBuilder";
58*288bf522SAndroid Build Coastguard Worker return false;
59*288bf522SAndroid Build Coastguard Worker }
60*288bf522SAndroid Build Coastguard Worker
61*288bf522SAndroid Build Coastguard Worker auto hash_callback = [](void* priv, const void* data, size_t len) {
62*288bf522SAndroid Build Coastguard Worker auto sparse_hasher = static_cast<HashTreeBuilder*>(priv);
63*288bf522SAndroid Build Coastguard Worker return sparse_hasher->Update(static_cast<const unsigned char*>(data), len)
64*288bf522SAndroid Build Coastguard Worker ? 0
65*288bf522SAndroid Build Coastguard Worker : 1;
66*288bf522SAndroid Build Coastguard Worker };
67*288bf522SAndroid Build Coastguard Worker sparse_file_callback(file.get(), false, false, hash_callback, builder);
68*288bf522SAndroid Build Coastguard Worker
69*288bf522SAndroid Build Coastguard Worker if (!builder->BuildHashTree()) {
70*288bf522SAndroid Build Coastguard Worker return false;
71*288bf522SAndroid Build Coastguard Worker }
72*288bf522SAndroid Build Coastguard Worker
73*288bf522SAndroid Build Coastguard Worker return builder->WriteHashTreeToFile(verity_filename);
74*288bf522SAndroid Build Coastguard Worker }
75