1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2022, Linaro Limited
4  */
5 
6 #include <inttypes.h>
7 #include <os_test.h>
8 #include <string.h>
9 #include <ta_os_test.h>
10 #include <tee_internal_api.h>
11 
12 void corrupt_pac(void);
13 
14 /* Assuming VA_MASK considering maximum 48 bit of VA */
15 #define VA_MASK		0xfffff0000000000
16 
sign_using_keyia(uint64_t ptr)17 static uint64_t sign_using_keyia(uint64_t ptr)
18 {
19 	asm volatile("paciza %0 " : "+r" (ptr));
20 	return ptr;
21 }
22 
ta_entry_pauth_test_nop(void)23 TEE_Result ta_entry_pauth_test_nop(void)
24 {
25 	uint64_t pac = 0;
26 	bool implemented = false;
27 	TEE_Result res = TEE_ERROR_GENERIC;
28 
29 	res = TEE_GetPropertyAsBool(
30 			TEE_PROPSET_TEE_IMPLEMENTATION,
31 			"org.trustedfirmware.optee.cpu.feat_pauth_implemented",
32 			&implemented);
33 	if (res != TEE_SUCCESS)
34 		return res;
35 
36 	if (!implemented)
37 		return TEE_ERROR_NOT_SUPPORTED;
38 
39 	/* Check if PAC instruction generates the authentication code */
40 	for (int i = 0; i < 10; i++)
41 		pac |= sign_using_keyia(i) & VA_MASK;
42 
43 	if (implemented && pac)
44 		return TEE_SUCCESS;
45 
46 	return TEE_ERROR_GENERIC;
47 }
48 
ta_entry_pauth_corrupt_pac(void)49 TEE_Result ta_entry_pauth_corrupt_pac(void)
50 {
51 	corrupt_pac();
52 
53 	return TEE_SUCCESS;
54 }
55