1 /*
2  * Copyright (c) 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 #ifndef CODE_SIGN_PAC_SIGN_CTX_H
17 #define CODE_SIGN_PAC_SIGN_CTX_H
18 
19 #include <cstdint>
20 
21 namespace OHOS {
22 namespace Security {
23 namespace CodeSign {
24 
25 enum CTXPurpose {
26     SIGN,
27     VERIFY
28 };
29 
30 enum ContextType {
31     SIGN_WITH_CONTEXT,
32     SIGN_WITHOUT_CONTEXT,
33     AUTH_CONTEXT
34 };
35 
36 class PACSignCtx {
37 public:
38     PACSignCtx(CTXPurpose purpose = CTXPurpose::VERIFY, uint32_t salt = 0);
39     ~PACSignCtx();
40     void Init(int index);
41     void InitSalt();
42     uint32_t Update(uint32_t value);
43     uint32_t SignSingle(uint32_t value, uint32_t index);
44     void SetIndex(uint32_t index);
45     uint32_t GetSalt();
46 
47 private:
48     void SetContext(uint32_t context);
49     uint64_t PaddingContext(ContextType type, int index = 0);
50     uint64_t GetRealContext();
51     uint32_t SignWithContext(uint32_t value);
52     uint32_t GetRandomSalt();
53 
54     uint64_t context_;
55     uint32_t salt_;
56     int index_;
57     CTXPurpose purpose_;
58 };
59 }
60 }
61 }
62 #endif