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 
16 #include <vector>
17 
18 #include "byte_buffer.h"
19 #include "byte_array_utils.h"
20 #include "signature_tools_log.h"
21 #include "sign_head.h"
22 
23 namespace OHOS {
24 namespace SignatureTools {
25 
26 const int SignHead::SIGN_HEAD_LEN = 32;
27 const std::string SignHead::MAGIC = "hw signed app   ";
28 const std::string SignHead::ELF_MAGIC = "elf sign block  ";
29 const std::string SignHead::VERSION = "1000";
30 const int SignHead::NUM_OF_BLOCK = 2;
31 const int SignHead::RESERVE_LENGTH = 4;
32 const int32_t SignHead::ELF_BLOCK_LEN = 12;
33 const int32_t SignHead::BIN_BLOCK_LEN = 8;
34 std::vector<int8_t> SignHead::m_reserve = std::vector<int8_t>(SignHead::RESERVE_LENGTH, 0);
35 
SignHead()36 SignHead::SignHead()
37 {
38 }
39 
GetSignHead(const int subBlockSize)40 std::vector<int8_t> SignHead::GetSignHead(const int subBlockSize)
41 {
42     int size = subBlockSize;
43     std::vector<int8_t> signHead(SIGN_HEAD_LEN, 0);
44     int start = 0;
45     start = ByteArrayUtils::InsertCharToByteArray(signHead, start, MAGIC);
46     if (start < 0) {
47         PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertCharToByteArray failed.");
48         return std::vector<int8_t>();
49     }
50     start = ByteArrayUtils::InsertCharToByteArray(signHead, start, VERSION);
51     if (start < 0) {
52         PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertCharToByteArray failed.");
53         return std::vector<int8_t>();
54     }
55     start = ByteArrayUtils::InsertIntToByteArray(signHead, start, size);
56     if (start < 0) {
57         PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertIntToByteArray failed.");
58         return std::vector<int8_t>();
59     }
60     start = ByteArrayUtils::InsertIntToByteArray(signHead, start, NUM_OF_BLOCK);
61     if (start < 0) {
62         PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertIntToByteArray failed.");
63         return std::vector<int8_t>();
64     }
65     start = ByteArrayUtils::InsertCharToByteArray(signHead, start, std::string(m_reserve.begin(), m_reserve.end()));
66     if (start < 0) {
67         PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "InsertCharToByteArray failed.");
68         return std::vector<int8_t>();
69     }
70     return signHead;
71 }
72 
GetSignHeadLittleEndian(const int subBlockSize, const int subBlockNum)73 std::vector<int8_t> SignHead::GetSignHeadLittleEndian(const int subBlockSize, const int subBlockNum)
74 {
75     ByteBuffer bf = ByteBuffer(SignHead::SIGN_HEAD_LEN);
76     for (char c : SignHead::ELF_MAGIC) {
77         bf.PutByte(c);
78     }
79     for (char c : SignHead::VERSION) {
80         bf.PutByte(c);
81     }
82     bf.PutInt32(subBlockSize);
83     bf.PutInt32(subBlockNum);
84     for (char c : SignHead::m_reserve) {
85         bf.PutByte(c);
86     }
87     int8_t ret[SignHead::SIGN_HEAD_LEN];
88     bf.GetData(0, ret, SignHead::SIGN_HEAD_LEN);
89     std::vector<int8_t> byte(ret, ret + SignHead::SIGN_HEAD_LEN);
90 
91     return byte;
92 }
93 
94 } // namespace SignatureTools
95 } // namespace OHOS
96