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 #ifndef SIGNATURETOOLS_FSVERITY_DESCRIPTOR_H
16 #define SIGNATURETOOLS_FSVERITY_DESCRIPTOR_H
17 
18 #include <stdint.h>
19 #include <vector>
20 #include <memory>
21 
22 #include "byte_buffer.h"
23 #include "signature_tools_log.h"
24 
25 namespace OHOS {
26 namespace SignatureTools {
27 class FsVerityDescriptor {
28 public:
29     static const uint8_t VERSION = 1;
30     static const int PAGE_SIZE_4K = 4096;
31     static const int FLAG_STORE_MERKLE_TREE_OFFSET = 0x1;
32     static const int FS_VERITY_DESCRIPTOR_TYPE = 0x1;
33     static const uint8_t CODE_SIGN_VERSION = 0x1;
34     static const int DESCRIPTOR_SIZE = 256;
35     static const int ROOT_HASH_FILED_SIZE = 64;
36     static const int SALT_SIZE = 32;
37     static const int RESERVED_SIZE_AFTER_FLAGS = 4;
38     static const int RESERVED_SIZE_AFTER_TREE_OFFSET = 127;
39 
40     class Builder;
41     FsVerityDescriptor();
GetFileSize()42     long GetFileSize()
43     {
44         return fileSize;
45     }
46 
GetMerkleTreeOffset()47     long GetMerkleTreeOffset()
48     {
49         return merkleTreeOffset;
50     }
51 
GetSignSize()52     int GetSignSize()
53     {
54         return signSize;
55     }
56 
57     static FsVerityDescriptor FromByteArray(std::vector<int8_t>& bytes);
58     void ToByteArray(std::vector<int8_t> &ret);
59     void GetByteForGenerateDigest(std::vector<int8_t>& ret);
60 
FsVerityDescriptor(Builder& builder)61     FsVerityDescriptor(Builder& builder)
62     {
63         this->version = builder.version;
64         this->fileSize = builder.fileSize;
65         this->hashAlgorithm = builder.hashAlgorithm;
66         this->log2BlockSize = builder.log2BlockSize;
67         this->saltSize = builder.saltSize;
68         this->signSize = builder.signSize;
69         this->salt = builder.salt;
70         this->rawRootHash = builder.rawRootHash;
71         this->flags = builder.flags;
72         this->merkleTreeOffset = builder.merkleTreeOffset;
73         this->csVersion = builder.csVersion;
74     }
75 
76     void WriteBytesWithSize(ByteBuffer* buffer, std::vector<int8_t>& src, int size);
77 
78 public:
79 class Builder {
80     friend class FsVerityDescriptor;
81 public:
SetVersion(uint8_t version)82     Builder& SetVersion(uint8_t version)
83     {
84         this->version = version;
85         return *this;
86     }
87 
SetFileSize(long fileSize)88     Builder& SetFileSize(long fileSize)
89     {
90         this->fileSize = fileSize;
91         return *this;
92     }
93 
SetHashAlgorithm(uint8_t hashAlgorithm)94     Builder& SetHashAlgorithm(uint8_t hashAlgorithm)
95     {
96         this->hashAlgorithm = hashAlgorithm;
97         return *this;
98     }
99 
SetLog2BlockSize(uint8_t log2BlockSize)100     Builder& SetLog2BlockSize(uint8_t log2BlockSize)
101     {
102         this->log2BlockSize = log2BlockSize;
103         return *this;
104     }
105 
SetSignSize(int signSize)106     Builder& SetSignSize(int signSize)
107     {
108         this->signSize = signSize;
109         return *this;
110     }
111 
SetSaltSize(uint8_t saltSize)112     Builder& SetSaltSize(uint8_t saltSize)
113     {
114         this->saltSize = saltSize;
115         return *this;
116     }
117 
SetSalt(const std::vector<int8_t>& salt)118     Builder& SetSalt(const std::vector<int8_t>& salt)
119     {
120         this->salt = salt;
121         return *this;
122     }
123 
SetRawRootHash(const std::vector<int8_t>& rawRootHash)124     Builder& SetRawRootHash(const std::vector<int8_t>& rawRootHash)
125     {
126         this->rawRootHash = rawRootHash;
127         return *this;
128     }
129 
SetFlags(int flags)130     Builder& SetFlags(int flags)
131     {
132         this->flags = flags;
133         return *this;
134     }
135 
SetMerkleTreeOffset(long merkleTreeOffset)136     Builder& SetMerkleTreeOffset(long merkleTreeOffset)
137     {
138         this->merkleTreeOffset = merkleTreeOffset;
139         return *this;
140     }
141 
SetCsVersion(uint8_t csVersion)142     Builder& SetCsVersion(uint8_t csVersion)
143     {
144         this->csVersion = csVersion;
145         return *this;
146     }
147 
Build()148     FsVerityDescriptor Build()
149     {
150         return FsVerityDescriptor(*this);
151     }
152 
153 private:
154     uint8_t version = VERSION;
155     long fileSize;
156     uint8_t hashAlgorithm;
157     uint8_t log2BlockSize;
158     uint8_t saltSize;
159     int signSize;
160     std::vector<int8_t> salt;
161     std::vector<int8_t> rawRootHash;
162     std::int32_t flags;
163     std::int64_t merkleTreeOffset;
164     uint8_t csVersion;
165 };
166 
167 private:
168     uint8_t version;
169     long fileSize;
170     uint8_t hashAlgorithm;
171     uint8_t log2BlockSize;
172     uint8_t saltSize;
173     int signSize;
174     std::vector<int8_t> salt;
175     std::vector<int8_t> rawRootHash;
176     int flags;
177     long merkleTreeOffset;
178     uint8_t csVersion;
179 };
180 } // namespace SignatureTools
181 } // namespace OHOS
182 #endif // SIGNATURETOOLS_FSVERITY_DESCRIPTOR_H