• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef SIGNATURETOOLS_FSVERITY_GENERATOR_H
16 #define SIGNATURETOOLS_FSVERITY_GENERATOR_H
17 
18 #include <vector>
19 #include <istream>
20 #include <memory>
21 
22 #include "merkle_tree.h"
23 #include "merkle_tree_builder.h"
24 #include "fs_verity_descriptor.h"
25 #include "fs_verity_digest.h"
26 
27 namespace OHOS {
28 namespace SignatureTools {
29 class FsVerityGenerator {
30 public:
31     MerkleTree* GenerateMerkleTree(std::istream& inputStream, long size,
32                                    const FsVerityHashAlgorithm& fsVerityHashAlgorithm);
33     bool GenerateFsVerityDigest(std::istream& inputStream, long size, int flags);
GetFsVerityDigest()34     std::vector<int8_t> GetFsVerityDigest()
35     {
36         return fsVerityDigest;
37     }
38 
GetDescriptorDigest()39     std::vector<int8_t> GetDescriptorDigest()
40     {
41         return descriptorDigest;
42     }
43 
GetTreeBytes()44     std::vector<int8_t> GetTreeBytes()
45     {
46         return treeBytes;
47     }
48 
GetRootHash()49     std::vector<int8_t> GetRootHash()
50     {
51         return rootHash;
52     }
53 
GetSalt()54     std::vector<int8_t> GetSalt()
55     {
56         return salt;
57     }
58 
GetSaltSize()59     int GetSaltSize()
60     {
61         return salt.empty() ? 0 : salt.size();
62     }
63 
SetCsOffset(uint64_t & csOffset)64     void SetCsOffset(uint64_t& csOffset)
65     {
66         this->csOffset = csOffset;
67     }
68 
GetFsVerityHashAlgorithm()69     static uint8_t GetFsVerityHashAlgorithm()
70     {
71         return FS_VERITY_HASH_ALGORITHM.GetId();
72     }
73 
GetLog2BlockSize()74     static uint8_t GetLog2BlockSize()
75     {
76         return LOG_2_OF_FSVERITY_HASH_PAGE_SIZE;
77     }
78 
79 protected:
80     std::vector<int8_t> salt;
81 
82 private:
83     static const FsVerityHashAlgorithm FS_VERITY_HASH_ALGORITHM;
84     static const int8_t LOG_2_OF_FSVERITY_HASH_PAGE_SIZE;
85     std::vector<int8_t> descriptorDigest;
86     std::vector<int8_t> fsVerityDigest;
87     std::vector<int8_t> treeBytes;
88     std::vector<int8_t> rootHash;
89     uint64_t csOffset = 0;
90 };
91 } // namespace SignatureTools
92 } // namespace OHOS
93 #endif // SIGNATURETOOLS_FSVERITY_GENERATOR_H