154aa6d63Sopenharmony_ci/* 254aa6d63Sopenharmony_ci * Copyright (c) 2024-2024 Huawei Device Co., Ltd. 354aa6d63Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 454aa6d63Sopenharmony_ci * you may not use this file except in compliance with the License. 554aa6d63Sopenharmony_ci * You may obtain a copy of the License at 654aa6d63Sopenharmony_ci * 754aa6d63Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 854aa6d63Sopenharmony_ci * 954aa6d63Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1054aa6d63Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1154aa6d63Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1254aa6d63Sopenharmony_ci * See the License for the specific language governing permissions and 1354aa6d63Sopenharmony_ci * limitations under the License. 1454aa6d63Sopenharmony_ci */ 1554aa6d63Sopenharmony_ci#ifndef SIGNATURETOOLS_MERKLE_TREE_BUILDER_H 1654aa6d63Sopenharmony_ci#define SIGNATURETOOLS_MERKLE_TREE_BUILDER_H 1754aa6d63Sopenharmony_ci 1854aa6d63Sopenharmony_ci#include <algorithm> 1954aa6d63Sopenharmony_ci#include <string> 2054aa6d63Sopenharmony_ci#include <vector> 2154aa6d63Sopenharmony_ci#include <iostream> 2254aa6d63Sopenharmony_ci#include <memory> 2354aa6d63Sopenharmony_ci#include <sstream> 2454aa6d63Sopenharmony_ci 2554aa6d63Sopenharmony_ci#include "thread_pool.h" 2654aa6d63Sopenharmony_ci#include "byte_buffer.h" 2754aa6d63Sopenharmony_ci#include "merkle_tree.h" 2854aa6d63Sopenharmony_ci#include "fs_digest_utils.h" 2954aa6d63Sopenharmony_ci#include "signature_tools_log.h" 3054aa6d63Sopenharmony_ci 3154aa6d63Sopenharmony_cinamespace OHOS { 3254aa6d63Sopenharmony_cinamespace SignatureTools { 3354aa6d63Sopenharmony_ciclass MerkleTreeBuilder { 3454aa6d63Sopenharmony_cipublic: 3554aa6d63Sopenharmony_ci MerkleTreeBuilder(); 3654aa6d63Sopenharmony_ci MerkleTree* GenerateMerkleTree(std::istream& inputStream, long size, 3754aa6d63Sopenharmony_ci const FsVerityHashAlgorithm& fsVerityHashAlgorithm); 3854aa6d63Sopenharmony_ci 3954aa6d63Sopenharmony_ciprivate: 4054aa6d63Sopenharmony_ci static const int FSVERITY_HASH_PAGE_SIZE; 4154aa6d63Sopenharmony_ci static const int64_t INPUTSTREAM_MAX_SIZE; 4254aa6d63Sopenharmony_ci static const int CHUNK_SIZE; 4354aa6d63Sopenharmony_ci static const long MAX_READ_SIZE; 4454aa6d63Sopenharmony_ci static const int MAX_PROCESSORS; 4554aa6d63Sopenharmony_ci static const int BLOCKINGQUEUE; 4654aa6d63Sopenharmony_ci static ByteBuffer* Slice(ByteBuffer* buffer, int begin, int end); 4754aa6d63Sopenharmony_ci static std::vector<int64_t> GetOffsetArrays(long dataSize, int digestSize); 4854aa6d63Sopenharmony_ci static std::vector<long> GetLevelSize(long dataSize, int digestSize); 4954aa6d63Sopenharmony_ci static long GetChunkCount(long dataSize, long divisor); 5054aa6d63Sopenharmony_ci static long GetFullChunkSize(long dataSize, long divisor, long multiplier); 5154aa6d63Sopenharmony_ci void SetAlgorithm(const std::string& algorithm); 5254aa6d63Sopenharmony_ci void TransInputStreamToHashData(std::istream& inputStream, long size, 5354aa6d63Sopenharmony_ci ByteBuffer* outputBuffer, int bufStartIdx); 5454aa6d63Sopenharmony_ci std::vector<std::vector<int8_t>> GetDataHashes(std::istream& inputStream, long size); 5554aa6d63Sopenharmony_ci 5654aa6d63Sopenharmony_ci void RunHashTask(std::vector<std::vector<int8_t>>& hashes, ByteBuffer* buffer, 5754aa6d63Sopenharmony_ci int readChunkIndex, int bufStartIdx); 5854aa6d63Sopenharmony_ci void TransInputDataToHashData(ByteBuffer* inputBuffer, ByteBuffer* outputBuffer, 5954aa6d63Sopenharmony_ci int64_t bufStartIdx, int64_t outputStartIdx); 6054aa6d63Sopenharmony_ci void GenerateHashDataByInputData(std::istream& inputStream, long size, ByteBuffer* outputBuffer, 6154aa6d63Sopenharmony_ci std::vector<int64_t>& offsetArrays, int digestSize); 6254aa6d63Sopenharmony_ci void GenerateHashDataByHashData(ByteBuffer* buffer, std::vector<int64_t>& offsetArrays, int digestSize); 6354aa6d63Sopenharmony_ci MerkleTree* GetMerkleTree(ByteBuffer* dataBuffer, long inputDataSize, 6454aa6d63Sopenharmony_ci FsVerityHashAlgorithm fsVerityHashAlgorithm); 6554aa6d63Sopenharmony_ci void DataRoundupChunkSize(ByteBuffer* data, long originalDataSize, int digestSize); 6654aa6d63Sopenharmony_ci bool CheckCalculateHashResult; 6754aa6d63Sopenharmony_ci const int POOL_SIZE = std::min(MAX_PROCESSORS, static_cast<int>(std::thread::hardware_concurrency())); 6854aa6d63Sopenharmony_ci std::string mAlgorithm = "SHA-256"; 6954aa6d63Sopenharmony_ci std::shared_ptr<Uscript::ThreadPool> mPools; 7054aa6d63Sopenharmony_ci}; 7154aa6d63Sopenharmony_ci} // namespace SignatureTools 7254aa6d63Sopenharmony_ci} // namespace OHOS 7354aa6d63Sopenharmony_ci#endif // SIGNATURETOOLS_MERKLE_TREE_BUILDER_H