• 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 #include "utils/macros.h"
21 
22 class DataProtector {
23 public:
DataProtector()24     DataProtector() : encryptedAddrOrData(0) {}
DataProtector(const uintptr_t pointer)25     explicit DataProtector(const uintptr_t pointer)
26     {
27         Update(pointer);
28     }
29 
30     ~DataProtector() = default;
31 
32     NAPI_EXPORT uintptr_t AutDecrypt(const uintptr_t pointer, [[maybe_unused]]const uintptr_t address) const;
33     NAPI_EXPORT uintptr_t PacEncrypt(const uintptr_t pointer, [[maybe_unused]]const uintptr_t address);
34 
Update(const uintptr_t pointer)35     void Update(const uintptr_t pointer)
36     {
37         if (pointer == 0) {
38             encryptedAddrOrData = 0;
39             return;
40         }
41         encryptedAddrOrData = PacEncrypt(pointer, reinterpret_cast<uintptr_t>(&encryptedAddrOrData));
42     }
43 
GetData()44     uintptr_t GetData() const
45     {
46         if (encryptedAddrOrData == 0) {
47             return encryptedAddrOrData;
48         }
49         return AutDecrypt(encryptedAddrOrData, reinterpret_cast<uintptr_t>(&encryptedAddrOrData));
50     }
51 
52 private:
53     uintptr_t encryptedAddrOrData;
54 };
55 #endif
56