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 #include "data_protector.h"
17
18 #if defined(NAPI_ENABLE_DATA_PROTECT)
19 #include <sys/auxv.h>
20 #include <asm/hwcap.h>
21
AutDecrypt(const uintptr_t pointer,const uintptr_t address) const22 uintptr_t DataProtector::AutDecrypt(const uintptr_t pointer, [[maybe_unused]]const uintptr_t address) const
23 {
24 auto hwcaps = getauxval(AT_HWCAP);
25 if (!(hwcaps & HWCAP_PACA)) {
26 return pointer;
27 }
28 void *t1 = reinterpret_cast<void*>(pointer);
29 void *t2 = reinterpret_cast<void*>(address);
30 #ifdef PAC_DFI_PTR_BKEY
31 __asm__ __volatile__("autdb %0, %1":"+r"(t1):"r"(t2):);
32 #else
33 __asm__ __volatile__("autda %0, %1":"+r"(t1):"r"(t2):);
34 #endif
35 return reinterpret_cast<uintptr_t>(t1);
36 }
37
PacEncrypt(const uintptr_t pointer,const uintptr_t address)38 uintptr_t DataProtector::PacEncrypt(const uintptr_t pointer, [[maybe_unused]]const uintptr_t address)
39 {
40 auto hwcaps = getauxval(AT_HWCAP);
41 if (!(hwcaps & HWCAP_PACA)) {
42 return pointer;
43 }
44 void *t1 = reinterpret_cast<void*>(pointer);
45 void *t2 = reinterpret_cast<void*>(address);
46 #ifdef PAC_DFI_PTR_BKEY
47 __asm__ __volatile__("pacdb %0, %1":"+r"(t1):"r"(t2):);
48 #else
49 __asm__ __volatile__("pacda %0, %1":"+r"(t1):"r"(t2):);
50 #endif
51 return reinterpret_cast<uintptr_t>(t1);
52 }
53 #else
AutDecrypt(const uintptr_t pointer,const uintptr_t address) const54 uintptr_t DataProtector::AutDecrypt(const uintptr_t pointer, [[maybe_unused]]const uintptr_t address) const
55 {
56 return pointer;
57 }
58
PacEncrypt(const uintptr_t pointer,const uintptr_t address)59 uintptr_t DataProtector::PacEncrypt(const uintptr_t pointer, [[maybe_unused]]const uintptr_t address)
60 {
61 return pointer;
62 }
63 #endif