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