b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 1 | #include <dlfcn.h> |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 2 | #include <stdbool.h> |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 3 | #include <stdio.h> |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 4 | #include <stdlib.h> |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 5 | #include <string.h> |
| 6 | #include <stdint.h> |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 7 | #include "gsw_secrypt_ss_interface.h" |
| 8 | #include "gsw_hal_errcode.h" |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 9 | #include "gsw_log_interface.h" |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 10 | |
l.yang | 6a42e4d | 2025-05-28 01:04:20 -0700 | [diff] [blame] | 11 | #define GSW_TEE "[HAL][GSW_TEE]" |
| 12 | |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 13 | /** |
| 14 | * struct TEEC_Context - Represents a connection between a client application |
| 15 | * and a TEE. |
| 16 | */ |
| 17 | typedef struct { |
| 18 | /* Implementation defined */ |
| 19 | struct { |
| 20 | int fd; |
| 21 | bool reg_mem; |
| 22 | bool memref_null; |
| 23 | } imp; |
| 24 | } TEEC_Context; |
| 25 | |
| 26 | /** |
| 27 | * struct TEEC_Session - Represents a connection between a client application |
| 28 | * and a trusted application. |
| 29 | */ |
| 30 | typedef struct { |
| 31 | /* Implementation defined */ |
| 32 | struct { |
| 33 | TEEC_Context *ctx; |
| 34 | uint32_t session_id; |
| 35 | } imp; |
| 36 | } TEEC_Session; |
| 37 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 38 | struct test_ctx { |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 39 | TEEC_Context ctx; |
| 40 | TEEC_Session sess; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 41 | }; |
| 42 | |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 43 | #define TEEC_SUCCESS 0x00000000 |
| 44 | #define TEEC_ERROR_STORAGE_NOT_AVAILABLE 0xF0100003 |
| 45 | #define TEEC_ERROR_GENERIC 0xFFFF0000 |
| 46 | #define TEEC_ERROR_ACCESS_DENIED 0xFFFF0001 |
| 47 | #define TEEC_ERROR_CANCEL 0xFFFF0002 |
| 48 | #define TEEC_ERROR_ACCESS_CONFLICT 0xFFFF0003 |
| 49 | #define TEEC_ERROR_EXCESS_DATA 0xFFFF0004 |
| 50 | #define TEEC_ERROR_BAD_FORMAT 0xFFFF0005 |
| 51 | #define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006 |
| 52 | #define TEEC_ERROR_BAD_STATE 0xFFFF0007 |
| 53 | #define TEEC_ERROR_ITEM_NOT_FOUND 0xFFFF0008 |
| 54 | #define TEEC_ERROR_NOT_IMPLEMENTED 0xFFFF0009 |
| 55 | #define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A |
| 56 | #define TEEC_ERROR_NO_DATA 0xFFFF000B |
| 57 | #define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C |
| 58 | #define TEEC_ERROR_BUSY 0xFFFF000D |
| 59 | #define TEEC_ERROR_COMMUNICATION 0xFFFF000E |
| 60 | #define TEEC_ERROR_SECURITY 0xFFFF000F |
| 61 | #define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010 |
| 62 | #define TEEC_ERROR_EXTERNAL_CANCEL 0xFFFF0011 |
| 63 | #define TEEC_ERROR_TARGET_DEAD 0xFFFF3024 |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 64 | |
| 65 | struct test_ctx ctx; |
| 66 | |
| 67 | #define lib_secure_path "/lib/libsecure_storage.so" |
| 68 | static void *dlHandle_secure; |
| 69 | |
| 70 | #define lib_mbtk_path "/lib/libmbtk_lib.so" |
| 71 | static void *dlHandle_mbtk; |
| 72 | |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 73 | typedef uint32_t TEEC_Result; |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 74 | // static void (*mbtk_log)(int level, const char *format, ...); |
| 75 | // static void (*mbtk_log_init)(char *path, char *tag); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 76 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 77 | int (*prepare_tee_session)(struct test_ctx *ctx); |
| 78 | void (*terminate_tee_session)(struct test_ctx *ctx); |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 79 | TEEC_Result (*read_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t *data_len); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 80 | TEEC_Result (*write_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t data_len); |
| 81 | TEEC_Result (*delete_secure_object)(struct test_ctx *ctx, const char *id); |
| 82 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 83 | static int tee_api_import(void) |
| 84 | { |
| 85 | |
| 86 | dlHandle_mbtk = dlopen(lib_mbtk_path, RTLD_NOW); |
| 87 | if (dlHandle_mbtk == NULL) |
| 88 | { |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 89 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | dlHandle_secure = dlopen(lib_secure_path, RTLD_NOW); |
| 93 | if (dlHandle_secure == NULL) |
| 94 | { |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 95 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 96 | } |
| 97 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 98 | // mbtk_log_init = (void (*)(char *path, char *tag))dlsym(dlHandle_mbtk, "mbtk_log_init"); |
| 99 | // if (mbtk_log_init == NULL) |
| 100 | // { |
| 101 | // return GSW_HAL_NORMAL_FAIL; |
| 102 | // } |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 103 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 104 | // mbtk_log = (void (*)(int level, const char *format, ...))dlsym(dlHandle_mbtk, "mbtk_log"); |
| 105 | // if (mbtk_log == NULL) |
| 106 | // { |
| 107 | // return GSW_HAL_NORMAL_FAIL; |
| 108 | // } |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 109 | |
| 110 | prepare_tee_session = (int (*)(struct test_ctx *ctx))dlsym(dlHandle_secure, "prepare_tee_session"); |
| 111 | if (prepare_tee_session == NULL) |
| 112 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 113 | LOGE(GSW_TEE,"prepare_tee_session dlsym fail\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 114 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | terminate_tee_session = (void (*)(struct test_ctx *ctx))dlsym(dlHandle_secure, "terminate_tee_session"); |
| 118 | if (terminate_tee_session == NULL) |
| 119 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 120 | LOGE(GSW_TEE,"terminate_tee_session dlsym fail\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 121 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 122 | } |
| 123 | |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 124 | read_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id,char *data, size_t *data_len))dlsym(dlHandle_secure, "read_secure_object"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 125 | if (read_secure_object == NULL) |
| 126 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 127 | LOGE(GSW_TEE,"read_secure_object dlsym fail\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 128 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | write_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id,char *data, size_t data_len))dlsym(dlHandle_secure, "write_secure_object"); |
| 132 | if (write_secure_object == NULL) |
| 133 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 134 | LOGE(GSW_TEE,"write_secure_object dlsym fail\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 135 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | delete_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id))dlsym(dlHandle_secure, "delete_secure_object"); |
| 139 | if (delete_secure_object == NULL) |
| 140 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 141 | LOGE(GSW_TEE,"delete_secure_object dlsym fail\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 142 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return GSW_HAL_SUCCESS; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @brief init tee sdk |
| 150 | * @param [in] None |
| 151 | * @param [out] None |
| 152 | * @retval GSW_HAL_SUCCESS is success\other is fail |
| 153 | */ |
| 154 | int32_t gsw_tee_sdk_init(void) |
| 155 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 156 | LOGE(GSW_TEE,"init start\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 157 | int32_t ret = 0; |
| 158 | ret = tee_api_import(); |
| 159 | if(ret) |
| 160 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 161 | LOGE(GSW_TEE,"tee_api_import fail\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 162 | return ret; |
| 163 | } |
| 164 | ret = prepare_tee_session(&ctx); |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 165 | LOGE(GSW_TEE,"init end\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 166 | return ret; |
| 167 | } |
| 168 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 169 | /** |
| 170 | * @brief read sensitive data from tee |
| 171 | * @param [in] char* in_obj_name :Sensitive data name |
| 172 | * @param [in] unsigned int* p_out_buf_len:The size of sensitive data output cache |
| 173 | * @param [out] char* out_buf:Cache of sensitive data output |
| 174 | * @param [out] unsigned int* p_out_buf_len:Sensitive data length |
| 175 | * @retval GSW_HAL_SUCCESS is success\other is fail |
| 176 | */ |
| 177 | #define basic_buf_len 7000 |
| 178 | int32_t gsw_tee_read_secure_data(const char* in_obj_name, char* out_buf, unsigned int* p_out_buf_len) |
| 179 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 180 | LOGE(GSW_TEE,"start read\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 181 | if (in_obj_name == NULL || out_buf == NULL) |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 182 | { |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 183 | return GSW_HAL_NORMAL_FAIL; |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 184 | } |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 185 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 186 | int32_t ret = 0; |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 187 | size_t size = basic_buf_len; |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 188 | char *tmp_buf = (char*)malloc(basic_buf_len); |
| 189 | if (NULL == tmp_buf) |
| 190 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 191 | LOGE(GSW_TEE,"Failed malloc fail"); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 192 | return GSW_HAL_NO_MEMORY; |
| 193 | } |
| 194 | |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 195 | TEEC_Result res = read_secure_object(&ctx, in_obj_name, tmp_buf, &size); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 196 | if (res != TEEC_SUCCESS) |
| 197 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 198 | LOGE(GSW_TEE,"Failed to read an object from the secure storage"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 199 | ret = GSW_HAL_NORMAL_FAIL; |
lichengzhang | 55aed35 | 2025-07-11 18:30:08 +0800 | [diff] [blame] | 200 | free(tmp_buf); |
| 201 | return ret; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 202 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 203 | LOGE(GSW_TEE,"really start end\n"); |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 204 | memcpy(out_buf, tmp_buf, size); |
| 205 | *p_out_buf_len = size; |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 206 | free(tmp_buf); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 207 | return ret; |
| 208 | } |
| 209 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 210 | /** |
| 211 | * @brief write sensitive data to tee |
| 212 | * @param [in] char* in_obj_name :Sensitive data name |
| 213 | * @param [in] char* in_buf:A cache for writing sensitive data |
| 214 | * @param [out] unsigned int in_buf_len:Sensitive data length |
| 215 | * @retval GSW_HAL_SUCCESS is success\other is fail |
| 216 | */ |
| 217 | int32_t gsw_tee_write_secure_data(const char* in_obj_name, char* in_buf, unsigned int in_buf_len) |
| 218 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 219 | LOGE(GSW_TEE,"write start\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 220 | if (in_obj_name == NULL || in_buf == NULL) |
| 221 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 222 | int32_t ret = 0; |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 223 | |
| 224 | TEEC_Result res = write_secure_object(&ctx, in_obj_name,in_buf, in_buf_len); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 225 | if (res != TEEC_SUCCESS) |
| 226 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 227 | LOGE(GSW_TEE,"Failed to write an object from the secure storage"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 228 | ret = GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 229 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 230 | LOGE(GSW_TEE,"write really end\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * @brief delete sensitive data from tee |
| 237 | * @param [in] char* in_obj_name :Sensitive data name |
| 238 | * @retval GSW_HAL_SUCCESS is success\other is fail |
| 239 | */ |
| 240 | int32_t gsw_tee_delete_secure_data(const char* in_obj_name) |
| 241 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 242 | LOGE(GSW_TEE,"delete start\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 243 | if (in_obj_name == NULL) |
| 244 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 245 | int32_t ret = 0; |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 246 | |
| 247 | TEEC_Result res = delete_secure_object(&ctx, in_obj_name); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 248 | if (res != TEEC_SUCCESS) |
| 249 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 250 | LOGE(GSW_TEE,"Failed to delete the object: 0x%x", res); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 251 | ret = GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 252 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 253 | LOGE(GSW_TEE,"delete really end\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 254 | return ret; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @brief check sensitive data from tee |
| 259 | * @param [in] char* in_obj_name :Sensitive data name |
| 260 | * @retval GSW_HAL_SUCCESS is exist\ other is not exist or fail |
| 261 | */ |
| 262 | int32_t gsw_tee_check_secure_data(const char* in_obj_name) |
| 263 | { |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 264 | if (in_obj_name == NULL) |
| 265 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 266 | int32_t ret = 1; |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 267 | size_t size = basic_buf_len; |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 268 | char *tmp_buf = (char*)malloc(basic_buf_len); |
| 269 | if (NULL == tmp_buf) |
| 270 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 271 | LOGE(GSW_TEE,"Failed malloc fail"); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 272 | return GSW_HAL_NO_MEMORY; |
| 273 | } |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 274 | TEEC_Result res = read_secure_object(&ctx, in_obj_name, tmp_buf, &size); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 275 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) |
| 276 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 277 | LOGE(GSW_TEE,"the obj no found\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 278 | ret = GSW_HAL_ERROR_TEE_SFS_FILE_NOEXIST; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 279 | } |
| 280 | else if (res == TEEC_SUCCESS) |
| 281 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 282 | LOGE(GSW_TEE,"the obj is exist\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 283 | ret = GSW_HAL_SUCCESS; |
| 284 | } |
| 285 | else |
| 286 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 287 | LOGE(GSW_TEE,"Failed to read an object from the secure storage"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 288 | ret = GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 289 | } |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 290 | free(tmp_buf); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 291 | return ret; |
| 292 | } |
| 293 | |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 294 | /** |
| 295 | * @brief deinit tee sdk |
| 296 | * @param [in] None |
| 297 | * @param [out] None |
| 298 | * @retval GSW_HAL_SUCCESS is success\other is fail |
| 299 | */ |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 300 | int32_t gsw_tee_sdk_deinit(void) |
| 301 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 302 | LOGE(GSW_TEE,"deinit start\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 303 | if (terminate_tee_session) { |
| 304 | terminate_tee_session(&ctx); // 终止TEE会话 |
| 305 | terminate_tee_session = NULL; |
| 306 | } |
| 307 | |
| 308 | if (dlHandle_secure) { |
| 309 | dlclose(dlHandle_secure); // 卸载安全库 |
| 310 | dlHandle_secure = NULL; |
| 311 | } |
| 312 | |
| 313 | if (dlHandle_mbtk) { |
| 314 | dlclose(dlHandle_mbtk); // 卸载日志库 |
| 315 | dlHandle_mbtk = NULL; |
| 316 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 317 | LOGE(GSW_TEE,"deinit end\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 318 | return GSW_HAL_SUCCESS; |
| 319 | } |
| 320 | |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 321 | int32_t gsw_secure_init(void) |
| 322 | { |
| 323 | static int s_init_flag = 0; |
| 324 | if (0xAA55 == s_init_flag) |
| 325 | { |
| 326 | return GSW_HAL_SUCCESS; |
| 327 | } |
| 328 | if (GSW_HAL_SUCCESS == gsw_tee_sdk_init()) |
| 329 | { |
| 330 | s_init_flag = 0xAA55; |
| 331 | return GSW_HAL_SUCCESS; |
| 332 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 333 | LOGE(GSW_TEE,"secure sdk init fail!!!"); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 334 | return GSW_HAL_NORMAL_FAIL; |
| 335 | } |
| 336 | |
| 337 | int32_t gsw_secure_storage_query(const char *objname, int32_t *exist_state) |
| 338 | { |
| 339 | #if 0 |
| 340 | if (NULL == objname || NULL == exist_state) |
| 341 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 342 | LOGE(GSW_TEE,"storage query input param error objname %p, exist_state %p",objname, exist_state); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 343 | return GSW_HAL_ARG_INVALID; |
| 344 | } |
| 345 | int32_t ret = gsw_secure_init(); |
| 346 | if (GSW_HAL_SUCCESS != ret) |
| 347 | { |
| 348 | return ret; |
| 349 | } |
| 350 | ret = gsw_tee_check_secure_data(objname); |
| 351 | if (GSW_HAL_SUCCESS == ret) |
| 352 | { |
| 353 | *exist_state = 1; //表明数据存在 |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | *exist_state = 0; |
| 358 | } |
| 359 | return ret; |
| 360 | #else |
| 361 | return GSW_HAL_NO_SUPPROT; |
| 362 | #endif |
| 363 | } |
| 364 | |
| 365 | int32_t gsw_secure_storage_read(const char *objname, uint8_t *outbuf, uint32_t buflen, uint32_t *outlen) |
| 366 | { |
| 367 | #if 0 |
| 368 | if (NULL == objname || NULL == outbuf || NULL == outlen) |
| 369 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 370 | LOGE(GSW_TEE,"storage read input param error objname %p, outbuf %p, outlen %p",objname, outbuf, outlen); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 371 | return GSW_HAL_ARG_INVALID; |
| 372 | } |
| 373 | int32_t ret = gsw_secure_init(); |
| 374 | if (GSW_HAL_SUCCESS != ret) |
| 375 | { |
| 376 | return ret; |
| 377 | } |
| 378 | *outlen = buflen; |
| 379 | return gsw_tee_read_secure_data(objname, (char*)outbuf, outlen); |
| 380 | #else |
| 381 | return GSW_HAL_NO_SUPPROT; |
| 382 | #endif |
| 383 | |
| 384 | } |
| 385 | |
| 386 | int32_t gsw_secure_storage_write(const char *objname, const uint8_t *inbuf, uint32_t inlen) |
| 387 | { |
| 388 | #if 0 |
| 389 | if (NULL == objname || NULL == inbuf || 0 == inlen) |
| 390 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 391 | LOGE(GSW_TEE,"storage write input param error objname %p, outbuf %p, inlen %u",objname, inbuf, inlen); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 392 | return GSW_HAL_ARG_INVALID; |
| 393 | } |
| 394 | int32_t ret = gsw_secure_init(); |
| 395 | if (GSW_HAL_SUCCESS != ret) |
| 396 | { |
| 397 | return ret; |
| 398 | } |
| 399 | return gsw_tee_write_secure_data(objname, (char*)inbuf, inlen); |
| 400 | #else |
| 401 | return GSW_HAL_NO_SUPPROT; |
| 402 | #endif |
| 403 | } |
| 404 | |
| 405 | int32_t gsw_secure_storage_delete(const char *objname) |
| 406 | { |
| 407 | #if 0 |
| 408 | if (NULL == objname) |
| 409 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 410 | LOGE(GSW_TEE,"storage delete input param error objname %p",objname); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 411 | return GSW_HAL_ARG_INVALID; |
| 412 | } |
| 413 | int32_t ret = gsw_secure_init(); |
| 414 | if (GSW_HAL_SUCCESS != ret) |
| 415 | { |
| 416 | return ret; |
| 417 | } |
| 418 | return gsw_tee_delete_secure_data(objname); |
| 419 | #else |
| 420 | return GSW_HAL_NO_SUPPROT; |
| 421 | #endif |
| 422 | } |