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
1654aa6d63Sopenharmony_ci#include <vector>
1754aa6d63Sopenharmony_ci
1854aa6d63Sopenharmony_ci#include "byte_buffer.h"
1954aa6d63Sopenharmony_ci#include "byte_array_utils.h"
2054aa6d63Sopenharmony_ci#include "signature_tools_log.h"
2154aa6d63Sopenharmony_ci#include "sign_head.h"
2254aa6d63Sopenharmony_ci
2354aa6d63Sopenharmony_cinamespace OHOS {
2454aa6d63Sopenharmony_cinamespace SignatureTools {
2554aa6d63Sopenharmony_ci
2654aa6d63Sopenharmony_ciconst int SignHead::SIGN_HEAD_LEN = 32;
2754aa6d63Sopenharmony_ciconst std::string SignHead::MAGIC = "hw signed app   ";
2854aa6d63Sopenharmony_ciconst std::string SignHead::ELF_MAGIC = "elf sign block  ";
2954aa6d63Sopenharmony_ciconst std::string SignHead::VERSION = "1000";
3054aa6d63Sopenharmony_ciconst int SignHead::NUM_OF_BLOCK = 2;
3154aa6d63Sopenharmony_ciconst int SignHead::RESERVE_LENGTH = 4;
3254aa6d63Sopenharmony_ciconst int32_t SignHead::ELF_BLOCK_LEN = 12;
3354aa6d63Sopenharmony_ciconst int32_t SignHead::BIN_BLOCK_LEN = 8;
3454aa6d63Sopenharmony_cistd::vector<int8_t> SignHead::m_reserve = std::vector<int8_t>(SignHead::RESERVE_LENGTH, 0);
3554aa6d63Sopenharmony_ci
3654aa6d63Sopenharmony_ciSignHead::SignHead()
3754aa6d63Sopenharmony_ci{
3854aa6d63Sopenharmony_ci}
3954aa6d63Sopenharmony_ci
4054aa6d63Sopenharmony_cistd::vector<int8_t> SignHead::GetSignHead(const int subBlockSize)
4154aa6d63Sopenharmony_ci{
4254aa6d63Sopenharmony_ci    int size = subBlockSize;
4354aa6d63Sopenharmony_ci    std::vector<int8_t> signHead(SIGN_HEAD_LEN, 0);
4454aa6d63Sopenharmony_ci    int start = 0;
4554aa6d63Sopenharmony_ci    start = ByteArrayUtils::InsertCharToByteArray(signHead, start, MAGIC);
4654aa6d63Sopenharmony_ci    if (start < 0) {
4754aa6d63Sopenharmony_ci        PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertCharToByteArray failed.");
4854aa6d63Sopenharmony_ci        return std::vector<int8_t>();
4954aa6d63Sopenharmony_ci    }
5054aa6d63Sopenharmony_ci    start = ByteArrayUtils::InsertCharToByteArray(signHead, start, VERSION);
5154aa6d63Sopenharmony_ci    if (start < 0) {
5254aa6d63Sopenharmony_ci        PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertCharToByteArray failed.");
5354aa6d63Sopenharmony_ci        return std::vector<int8_t>();
5454aa6d63Sopenharmony_ci    }
5554aa6d63Sopenharmony_ci    start = ByteArrayUtils::InsertIntToByteArray(signHead, start, size);
5654aa6d63Sopenharmony_ci    if (start < 0) {
5754aa6d63Sopenharmony_ci        PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertIntToByteArray failed.");
5854aa6d63Sopenharmony_ci        return std::vector<int8_t>();
5954aa6d63Sopenharmony_ci    }
6054aa6d63Sopenharmony_ci    start = ByteArrayUtils::InsertIntToByteArray(signHead, start, NUM_OF_BLOCK);
6154aa6d63Sopenharmony_ci    if (start < 0) {
6254aa6d63Sopenharmony_ci        PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertIntToByteArray failed.");
6354aa6d63Sopenharmony_ci        return std::vector<int8_t>();
6454aa6d63Sopenharmony_ci    }
6554aa6d63Sopenharmony_ci    start = ByteArrayUtils::InsertCharToByteArray(signHead, start, std::string(m_reserve.begin(), m_reserve.end()));
6654aa6d63Sopenharmony_ci    if (start < 0) {
6754aa6d63Sopenharmony_ci        PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertCharToByteArray failed.");
6854aa6d63Sopenharmony_ci        return std::vector<int8_t>();
6954aa6d63Sopenharmony_ci    }
7054aa6d63Sopenharmony_ci    return signHead;
7154aa6d63Sopenharmony_ci}
7254aa6d63Sopenharmony_ci
7354aa6d63Sopenharmony_cistd::vector<int8_t> SignHead::GetSignHeadLittleEndian(const int subBlockSize, const int subBlockNum)
7454aa6d63Sopenharmony_ci{
7554aa6d63Sopenharmony_ci    ByteBuffer bf = ByteBuffer(SignHead::SIGN_HEAD_LEN);
7654aa6d63Sopenharmony_ci    for (char c : SignHead::ELF_MAGIC) {
7754aa6d63Sopenharmony_ci        bf.PutByte(c);
7854aa6d63Sopenharmony_ci    }
7954aa6d63Sopenharmony_ci    for (char c : SignHead::VERSION) {
8054aa6d63Sopenharmony_ci        bf.PutByte(c);
8154aa6d63Sopenharmony_ci    }
8254aa6d63Sopenharmony_ci    bf.PutInt32(subBlockSize);
8354aa6d63Sopenharmony_ci    bf.PutInt32(subBlockNum);
8454aa6d63Sopenharmony_ci    for (char c : SignHead::m_reserve) {
8554aa6d63Sopenharmony_ci        bf.PutByte(c);
8654aa6d63Sopenharmony_ci    }
8754aa6d63Sopenharmony_ci    int8_t ret[SignHead::SIGN_HEAD_LEN];
8854aa6d63Sopenharmony_ci    bf.GetData(0, ret, SignHead::SIGN_HEAD_LEN);
8954aa6d63Sopenharmony_ci    std::vector<int8_t> byte(ret, ret + SignHead::SIGN_HEAD_LEN);
9054aa6d63Sopenharmony_ci
9154aa6d63Sopenharmony_ci    return byte;
9254aa6d63Sopenharmony_ci}
9354aa6d63Sopenharmony_ci
9454aa6d63Sopenharmony_ci} // namespace SignatureTools
9554aa6d63Sopenharmony_ci} // namespace OHOS
96