1 /* 2 * Copyright (c) 2024-2024 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, long fsvTreeOffset); GetFsVerityDigest()34 std::vector<int8_t> GetFsVerityDigest() 35 { 36 return fsVerityDigest; 37 } 38 Getsalt()39 std::vector<int8_t> Getsalt() 40 { 41 return salt; 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 GetFsVerityHashAlgorithm()64 static uint8_t GetFsVerityHashAlgorithm() 65 { 66 return FS_VERITY_HASH_ALGORITHM.GetId(); 67 } 68 GetLog2BlockSize()69 static uint8_t GetLog2BlockSize() 70 { 71 return LOG_2_OF_FSVERITY_HASH_PAGE_SIZE; 72 } 73 74 protected: 75 std::vector<int8_t> salt; 76 77 private: 78 static const FsVerityHashAlgorithm FS_VERITY_HASH_ALGORITHM; 79 static const int8_t LOG_2_OF_FSVERITY_HASH_PAGE_SIZE; 80 std::vector<int8_t> fsVerityDigest; 81 std::vector<int8_t> treeBytes; 82 std::vector<int8_t> rootHash; 83 }; 84 } // namespace SignatureTools 85 } // namespace OHOS 86 #endif // SIGNATURETOOLS_FSVERITY_GENERATOR_H