• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FOUNDATION_ACE_NAPI_UTILS_DATA_PROTECTOR_H
17 #define FOUNDATION_ACE_NAPI_UTILS_DATA_PROTECTOR_H
18 
19 #include <cstdint>
20 
21 class DataProtector {
22 public:
DataProtector()23     DataProtector() : encryptedAddrOrData(0) {}
DataProtector(const uintptr_t pointer)24     explicit DataProtector(const uintptr_t pointer)
25     {
26         Update(pointer);
27     }
28 
29     ~DataProtector() = default;
30 
31     static uintptr_t AutDecrypt(const uintptr_t pointer, [[maybe_unused]]const uintptr_t address);
32     static uintptr_t PacEncrypt(const uintptr_t pointer, [[maybe_unused]]const uintptr_t address);
33 
Update(const uintptr_t pointer)34     void Update(const uintptr_t pointer)
35     {
36 #if defined(NAPI_ENABLE_DATA_PROTECT)
37         if (pointer == 0) {
38             encryptedAddrOrData = 0;
39             return;
40         }
41         encryptedAddrOrData = PacEncrypt(pointer, reinterpret_cast<uintptr_t>(&encryptedAddrOrData));
42 #else
43         encryptedAddrOrData = pointer;
44 #endif
45     }
46 
GetData()47     uintptr_t GetData() const
48     {
49 #if defined(NAPI_ENABLE_DATA_PROTECT)
50         if (encryptedAddrOrData == 0) {
51             return encryptedAddrOrData;
52         }
53         return AutDecrypt(encryptedAddrOrData, reinterpret_cast<uintptr_t>(&encryptedAddrOrData));
54 #else
55         return encryptedAddrOrData;
56 #endif
57     }
58 
59 private:
60     uintptr_t encryptedAddrOrData;
61 };
62 #endif
63