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#include "securec.h"
1654aa6d63Sopenharmony_ci#include "fs_verity_digest.h"
1754aa6d63Sopenharmony_ci#include "signature_tools_log.h"
1854aa6d63Sopenharmony_ci
1954aa6d63Sopenharmony_cinamespace OHOS {
2054aa6d63Sopenharmony_cinamespace SignatureTools {
2154aa6d63Sopenharmony_ciconst std::string FsVerityDigest::FSVERITY_DIGEST_MAGIC = "FSVerity";
2254aa6d63Sopenharmony_ciconst int FsVerityDigest::DIGEST_HEADER_SIZE = 12;
2354aa6d63Sopenharmony_ci
2454aa6d63Sopenharmony_civoid FsVerityDigest::GetFsVerityDigest(int8_t algoID, std::vector<int8_t>& digest,
2554aa6d63Sopenharmony_ci                                       std::vector<int8_t> &ret)
2654aa6d63Sopenharmony_ci{
2754aa6d63Sopenharmony_ci    const int size = DIGEST_HEADER_SIZE + digest.size();
2854aa6d63Sopenharmony_ci    if (size <= 0) {
2954aa6d63Sopenharmony_ci        ret = std::vector<int8_t>();
3054aa6d63Sopenharmony_ci        return;
3154aa6d63Sopenharmony_ci    }
3254aa6d63Sopenharmony_ci    std::unique_ptr<ByteBuffer> buffer = std::make_unique<ByteBuffer>(ByteBuffer(size));
3354aa6d63Sopenharmony_ci    buffer->PutData(FSVERITY_DIGEST_MAGIC.c_str(), (int32_t)FSVERITY_DIGEST_MAGIC.length());
3454aa6d63Sopenharmony_ci    buffer->PutInt16(algoID);
3554aa6d63Sopenharmony_ci    buffer->PutInt16((int16_t)digest.size());
3654aa6d63Sopenharmony_ci    buffer->PutData(digest.data(), digest.size());
3754aa6d63Sopenharmony_ci    buffer->Flip();
3854aa6d63Sopenharmony_ci    char dataArr[size];
3954aa6d63Sopenharmony_ci    if (memset_s(dataArr, sizeof(dataArr), 0, sizeof(dataArr)) != RET_OK) {
4054aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("memcpy_s failed");
4154aa6d63Sopenharmony_ci        ret = std::vector<int8_t>();
4254aa6d63Sopenharmony_ci        return;
4354aa6d63Sopenharmony_ci    }
4454aa6d63Sopenharmony_ci    buffer->GetData(dataArr, size);
4554aa6d63Sopenharmony_ci    ret = std::vector<int8_t>(dataArr, dataArr + size);
4654aa6d63Sopenharmony_ci    return;
4754aa6d63Sopenharmony_ci}
4854aa6d63Sopenharmony_ci} // namespace SignatureTools
4954aa6d63Sopenharmony_ci} // namespace OHOS
50