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 #include "fs_verity_info_segment.h"
16 
17 namespace OHOS {
18 namespace SignatureTools {
19 
FsVerityInfoSegment()20 FsVerityInfoSegment::FsVerityInfoSegment()
21 {
22     magic = MAGIC;
23     reserved = std::vector<int8_t>(RESERVED_BYTE_ARRAY_LENGTH);
24 }
25 
FsVerityInfoSegment(int8_t version, int8_t hashAlgorithm, int8_t log2BlockSize)26 FsVerityInfoSegment::FsVerityInfoSegment(int8_t version, int8_t hashAlgorithm, int8_t log2BlockSize)
27 {
28     magic = MAGIC;
29     this->version = version;
30     this->hashAlgorithm = hashAlgorithm;
31     this->log2BlockSize = log2BlockSize;
32     reserved = std::vector<int8_t>(RESERVED_BYTE_ARRAY_LENGTH);
33 }
34 
FsVerityInfoSegment(int magic, int8_t version, int8_t hashAlgorithm, int8_t log2BlockSize, const std::vector<int8_t>& reserved)35 FsVerityInfoSegment::FsVerityInfoSegment(int magic, int8_t version, int8_t hashAlgorithm,
36                                          int8_t log2BlockSize, const std::vector<int8_t>& reserved)
37 {
38     this->magic = magic;
39     this->version = version;
40     this->hashAlgorithm = hashAlgorithm;
41     this->log2BlockSize = log2BlockSize;
42     this->reserved = reserved;
43 }
44 
~FsVerityInfoSegment()45 FsVerityInfoSegment:: ~FsVerityInfoSegment()
46 {
47 }
48 
Size()49 int FsVerityInfoSegment::Size()
50 {
51     return FS_VERITY_INFO_SEGMENT_SIZE;
52 }
53 
ToByteArray(std::vector<int8_t>& ret)54 void FsVerityInfoSegment::ToByteArray(std::vector<int8_t>& ret)
55 {
56     std::unique_ptr<ByteBuffer> bf = std::make_unique<ByteBuffer>(FS_VERITY_INFO_SEGMENT_SIZE);
57     bf->PutInt32(magic);
58     bf->PutByte(version);
59     bf->PutByte(hashAlgorithm);
60     bf->PutByte(log2BlockSize);
61     bf->PutData(reserved.data(), reserved.size());
62     ret = std::vector<int8_t>(bf->GetBufferPtr(), bf->GetBufferPtr() + bf->GetPosition());
63 }
64 
FromByteArray(const std::vector<int8_t>& bytes)65 FsVerityInfoSegment FsVerityInfoSegment::FromByteArray(const std::vector<int8_t>& bytes)
66 {
67     if (bytes.size() != FS_VERITY_INFO_SEGMENT_SIZE) {
68         PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR,
69                             "The signed size of FsVerityInfoSegment is incorrect");
70         return FsVerityInfoSegment();
71     }
72 
73     ByteBuffer bf(bytes.size());
74     bf.PutData((const char*)bytes.data(), bytes.size());
75     bf.SetPosition(0);
76     int inMagic;
77     bf.GetInt32(inMagic);
78     if (inMagic != MAGIC) {
79         PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR,
80                             "The magic number of FsVerityInfoSegment is incorrect");
81         return FsVerityInfoSegment();
82     }
83 
84     int8_t inVersion;
85     bf.GetInt8(inVersion);
86     if (inVersion != FsVerityDescriptor::VERSION) {
87         PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR,
88                             "The version of FsVerityInfoSegment is incorrect");
89         return FsVerityInfoSegment();
90     }
91 
92     int8_t inHashAlgorithm;
93     bf.GetInt8(inHashAlgorithm);
94     if (inHashAlgorithm != FsVerityGenerator::GetFsVerityHashAlgorithm()) {
95         PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR,
96                             "The hash algorithm of FsVerityInfoSegment is incorrect");
97         return FsVerityInfoSegment();
98     }
99 
100     int8_t inLog2BlockSize;
101     bf.GetInt8(inLog2BlockSize);
102     if (inLog2BlockSize != FsVerityGenerator::GetLog2BlockSize()) {
103         PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR,
104                             "The log block size of FsVerityInfoSegment is incorrect");
105         return FsVerityInfoSegment();
106     }
107 
108     std::vector<int8_t> inReservedBytes(RESERVED_BYTE_ARRAY_LENGTH);
109     char reverseArr[RESERVED_BYTE_ARRAY_LENGTH];
110     bf.GetData(reverseArr, RESERVED_BYTE_ARRAY_LENGTH);
111     std::vector<int8_t> reverseData(reverseArr, reverseArr + RESERVED_BYTE_ARRAY_LENGTH);
112 
113     return FsVerityInfoSegment(inMagic, inVersion, inHashAlgorithm, inLog2BlockSize, reverseData);
114 }
115 
116 }
117 }