blob: b30f54870ca7f50630bb45cb72f105640a3663b8 [file] [log] [blame]
b.liu68a94c92025-05-24 12:53:41 +08001
lichengzhangd7aea6c2025-06-05 16:35:54 +08002#include "gsw/gsw_tz_interface.h"
b.liu68a94c92025-05-24 12:53:41 +08003#include <dlfcn.h>
4#include <stdio.h>
5#include <string.h>
6#include <stdint.h>
lichengzhangd7aea6c2025-06-05 16:35:54 +08007#include <stdlib.h>
b.liu68a94c92025-05-24 12:53:41 +08008
9#ifndef LOG_ERR_LEVEL
10#define LOG_ERR_LEVEL 3 /* error conditions */
11#endif
12#ifndef LOG_WARN_LEVEL
13#define LOG_WARN_LEVEL 4 /* warning conditions */
14#endif
15#ifndef LOG_INFO_LEVEL
16#define LOG_INFO_LEVEL 6 /* informational */
17#endif
18#ifndef LOG_DEBUG_LEVEL
19#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
20#endif
21#ifndef LOG_VERBOSE_LEVEL
22#define LOG_VERBOSE_LEVEL 8
23#endif
24
l.yang6a42e4d2025-05-28 01:04:20 -070025#define GSW_TEE "[HAL][GSW_TEE]"
26
b.liu68a94c92025-05-24 12:53:41 +080027#define LOGV(fmt, args ...) \
28 do{ \
29 char *file_ptr_1001 = __FILE__; \
30 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
31 char line_1001[10] = {0}; \
32 sprintf(line_1001, "%d", __LINE__); \
33 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
34 if(*ptr_1001 == '/') \
35 break; \
36 ptr_1001--; \
37 } \
l.yang6a42e4d2025-05-28 01:04:20 -070038 mbtk_log(LOG_VERBOSE_LEVEL, "%s#%s: "GSW_TEE"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080039 } while(0)
40
41#define LOGI(fmt, args...) \
42 do{ \
43 char *file_ptr_1001 = __FILE__; \
44 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
45 char line_1001[10] = {0}; \
46 sprintf(line_1001, "%d", __LINE__); \
47 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
48 if(*ptr_1001 == '/') \
49 break; \
50 ptr_1001--; \
51 } \
l.yang6a42e4d2025-05-28 01:04:20 -070052 mbtk_log(LOG_INFO_LEVEL, "%s#%s: "GSW_TEE"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080053 } while(0)
54
55#define LOGD(fmt, args...) \
56 do{ \
57 char *file_ptr_1001 = __FILE__; \
58 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
59 char line_1001[10] = {0}; \
60 sprintf(line_1001, "%d", __LINE__); \
61 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
62 if(*ptr_1001 == '/') \
63 break; \
64 ptr_1001--; \
65 } \
l.yang6a42e4d2025-05-28 01:04:20 -070066 mbtk_log(LOG_DEBUG_LEVEL, "%s#%s: "GSW_TEE"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080067 } while(0)
68
69#define LOGW(fmt, args...) \
70 do{ \
71 char *file_ptr_1001 = __FILE__; \
72 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
73 char line_1001[10] = {0}; \
74 sprintf(line_1001, "%d", __LINE__); \
75 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
76 if(*ptr_1001 == '/') \
77 break; \
78 ptr_1001--; \
79 } \
l.yang6a42e4d2025-05-28 01:04:20 -070080 mbtk_log(LOG_WARN_LEVEL, "%s#%s: "GSW_TEE"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080081 } while(0)
82
83#define LOGE(fmt, args...) \
84 do{ \
85 char *file_ptr_1001 = __FILE__; \
86 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
87 char line_1001[10] = {0}; \
88 sprintf(line_1001, "%d", __LINE__); \
89 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
90 if(*ptr_1001 == '/') \
91 break; \
92 ptr_1001--; \
93 } \
l.yang6a42e4d2025-05-28 01:04:20 -070094 mbtk_log(LOG_ERR_LEVEL, "%s#%s: "GSW_TEE"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080095 } while(0)
96
lichengzhangd7aea6c2025-06-05 16:35:54 +080097/**
98 * struct TEEC_Context - Represents a connection between a client application
99 * and a TEE.
100 */
101 typedef struct {
102 /* Implementation defined */
103 struct {
104 int fd;
105 bool reg_mem;
106 bool memref_null;
107 } imp;
108} TEEC_Context;
109
110/**
111 * struct TEEC_Session - Represents a connection between a client application
112 * and a trusted application.
113 */
114 typedef struct {
115 /* Implementation defined */
116 struct {
117 TEEC_Context *ctx;
118 uint32_t session_id;
119 } imp;
120} TEEC_Session;
121
b.liu68a94c92025-05-24 12:53:41 +0800122struct test_ctx {
lichengzhangd7aea6c2025-06-05 16:35:54 +0800123 TEEC_Context ctx;
124 TEEC_Session sess;
b.liu68a94c92025-05-24 12:53:41 +0800125};
126
lichengzhangd7aea6c2025-06-05 16:35:54 +0800127#define TEEC_SUCCESS 0x00000000
128#define TEEC_ERROR_STORAGE_NOT_AVAILABLE 0xF0100003
129#define TEEC_ERROR_GENERIC 0xFFFF0000
130#define TEEC_ERROR_ACCESS_DENIED 0xFFFF0001
131#define TEEC_ERROR_CANCEL 0xFFFF0002
132#define TEEC_ERROR_ACCESS_CONFLICT 0xFFFF0003
133#define TEEC_ERROR_EXCESS_DATA 0xFFFF0004
134#define TEEC_ERROR_BAD_FORMAT 0xFFFF0005
135#define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006
136#define TEEC_ERROR_BAD_STATE 0xFFFF0007
137#define TEEC_ERROR_ITEM_NOT_FOUND 0xFFFF0008
138#define TEEC_ERROR_NOT_IMPLEMENTED 0xFFFF0009
139#define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A
140#define TEEC_ERROR_NO_DATA 0xFFFF000B
141#define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C
142#define TEEC_ERROR_BUSY 0xFFFF000D
143#define TEEC_ERROR_COMMUNICATION 0xFFFF000E
144#define TEEC_ERROR_SECURITY 0xFFFF000F
145#define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010
146#define TEEC_ERROR_EXTERNAL_CANCEL 0xFFFF0011
147#define TEEC_ERROR_TARGET_DEAD 0xFFFF3024
b.liu68a94c92025-05-24 12:53:41 +0800148
149struct test_ctx ctx;
150
151#define lib_secure_path "/lib/libsecure_storage.so"
152static void *dlHandle_secure;
153
154#define lib_mbtk_path "/lib/libmbtk_lib.so"
155static void *dlHandle_mbtk;
156
lichengzhangd7aea6c2025-06-05 16:35:54 +0800157typedef uint32_t TEEC_Result;
b.liu68a94c92025-05-24 12:53:41 +0800158
159
160static void (*mbtk_log)(int level, const char *format, ...);
161static void (*mbtk_log_init)(char *path, char *tag);
162
163
164
165int (*prepare_tee_session)(struct test_ctx *ctx);
166void (*terminate_tee_session)(struct test_ctx *ctx);
167TEEC_Result (*read_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t data_len);
168TEEC_Result (*write_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t data_len);
169TEEC_Result (*delete_secure_object)(struct test_ctx *ctx, const char *id);
170
171
172static int tee_api_import(void)
173{
174
175 dlHandle_mbtk = dlopen(lib_mbtk_path, RTLD_NOW);
176 if (dlHandle_mbtk == NULL)
177 {
lichengzhangd7aea6c2025-06-05 16:35:54 +0800178 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800179 }
180
181 dlHandle_secure = dlopen(lib_secure_path, RTLD_NOW);
182 if (dlHandle_secure == NULL)
183 {
lichengzhangd7aea6c2025-06-05 16:35:54 +0800184 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800185 }
186
187 mbtk_log_init = (void (*)(char *path, char *tag))dlsym(dlHandle_mbtk, "mbtk_log_init");
188 if (mbtk_log_init == NULL)
189 {
lichengzhangd7aea6c2025-06-05 16:35:54 +0800190 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800191 }
192
193 mbtk_log = (void (*)(int level, const char *format, ...))dlsym(dlHandle_mbtk, "mbtk_log");
194 if (mbtk_log == NULL)
195 {
lichengzhangd7aea6c2025-06-05 16:35:54 +0800196 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800197 }
198
199 prepare_tee_session = (int (*)(struct test_ctx *ctx))dlsym(dlHandle_secure, "prepare_tee_session");
200 if (prepare_tee_session == NULL)
201 {
202 LOGE("prepare_tee_session dlsym fail\n");
lichengzhangd7aea6c2025-06-05 16:35:54 +0800203 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800204 }
205
206 terminate_tee_session = (void (*)(struct test_ctx *ctx))dlsym(dlHandle_secure, "terminate_tee_session");
207 if (terminate_tee_session == NULL)
208 {
209 LOGE("terminate_tee_session dlsym fail\n");
lichengzhangd7aea6c2025-06-05 16:35:54 +0800210 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800211 }
212
213 read_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id,char *data, size_t data_len))dlsym(dlHandle_secure, "read_secure_object");
214 if (read_secure_object == NULL)
215 {
216 LOGE("read_secure_object dlsym fail\n");
lichengzhangd7aea6c2025-06-05 16:35:54 +0800217 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800218 }
219
220 write_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id,char *data, size_t data_len))dlsym(dlHandle_secure, "write_secure_object");
221 if (write_secure_object == NULL)
222 {
223 LOGE("write_secure_object dlsym fail\n");
lichengzhangd7aea6c2025-06-05 16:35:54 +0800224 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800225 }
226
227 delete_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id))dlsym(dlHandle_secure, "delete_secure_object");
228 if (delete_secure_object == NULL)
229 {
230 LOGE("delete_secure_object dlsym fail\n");
lichengzhangd7aea6c2025-06-05 16:35:54 +0800231 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800232 }
233
234 return GSW_HAL_SUCCESS;
235}
236
237/**
238* @brief init tee sdk
239* @param [in] None
240* @param [out] None
241* @retval GSW_HAL_SUCCESS is success\other is fail
242*/
243int32_t gsw_tee_sdk_init(void)
244{
245 int32_t ret = 0;
246 ret = tee_api_import();
247 if(ret)
248 {
249 LOGE("tee_api_import fail\n");
250 return ret;
251 }
252 ret = prepare_tee_session(&ctx);
253
254 return ret;
255}
256
257
258/**
259* @brief read sensitive data from tee
260* @param [in] char* in_obj_name :Sensitive data name
261* @param [in] unsigned int* p_out_buf_len:The size of sensitive data output cache
262* @param [out] char* out_buf:Cache of sensitive data output
263* @param [out] unsigned int* p_out_buf_len:Sensitive data length
264* @retval GSW_HAL_SUCCESS is success\other is fail
265*/
266#define basic_buf_len 7000
267int32_t gsw_tee_read_secure_data(const char* in_obj_name, char* out_buf, unsigned int* p_out_buf_len)
268{
lichengzhangd7aea6c2025-06-05 16:35:54 +0800269 if (in_obj_name == NULL || out_buf == NULL)
270 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800271 int32_t ret = 0;
272 TEEC_Result res;
lichengzhangd7aea6c2025-06-05 16:35:54 +0800273 char *tmp_buf = malloc(basic_buf_len * sizeof(char));
274 res = read_secure_object(&ctx, in_obj_name, tmp_buf, basic_buf_len);
b.liu68a94c92025-05-24 12:53:41 +0800275 if (res != TEEC_SUCCESS)
276 {
277 LOGE("Failed to read an object from the secure storage");
lichengzhangd7aea6c2025-06-05 16:35:54 +0800278 ret = GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800279 }
lichengzhangd7aea6c2025-06-05 16:35:54 +0800280 memcpy(out_buf, tmp_buf, strlen(tmp_buf)+1);
b.liu68a94c92025-05-24 12:53:41 +0800281 *p_out_buf_len = strlen(out_buf);
lichengzhangd7aea6c2025-06-05 16:35:54 +0800282 free(tmp_buf);
b.liu68a94c92025-05-24 12:53:41 +0800283 return ret;
284}
285
286
287/**
288* @brief write sensitive data to tee
289* @param [in] char* in_obj_name :Sensitive data name
290* @param [in] char* in_buf:A cache for writing sensitive data
291* @param [out] unsigned int in_buf_len:Sensitive data length
292* @retval GSW_HAL_SUCCESS is success\other is fail
293*/
294int32_t gsw_tee_write_secure_data(const char* in_obj_name, char* in_buf, unsigned int in_buf_len)
295{
lichengzhangd7aea6c2025-06-05 16:35:54 +0800296 if (in_obj_name == NULL || in_buf == NULL)
297 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800298 int32_t ret = 0;
299 TEEC_Result res;
300 res = write_secure_object(&ctx, in_obj_name,in_buf, in_buf_len);
301 if (res != TEEC_SUCCESS)
302 {
303 LOGE("Failed to write an object from the secure storage");
lichengzhangd7aea6c2025-06-05 16:35:54 +0800304 ret = GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800305 }
306
307 return ret;
308}
309
310
311/**
312* @brief delete sensitive data from tee
313* @param [in] char* in_obj_name :Sensitive data name
314* @retval GSW_HAL_SUCCESS is success\other is fail
315*/
316int32_t gsw_tee_delete_secure_data(const char* in_obj_name)
317{
lichengzhangd7aea6c2025-06-05 16:35:54 +0800318 if (in_obj_name == NULL)
319 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800320 int32_t ret = 0;
321 TEEC_Result res;
322 res = delete_secure_object(&ctx, in_obj_name);
323 if (res != TEEC_SUCCESS)
324 {
325 LOGE("Failed to delete the object: 0x%x", res);
lichengzhangd7aea6c2025-06-05 16:35:54 +0800326 ret = GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800327 }
328
329
330 return ret;
331
332}
333
334/**
335* @brief check sensitive data from tee
336* @param [in] char* in_obj_name :Sensitive data name
337* @retval GSW_HAL_SUCCESS is exist\ other is not exist or fail
338*/
339int32_t gsw_tee_check_secure_data(const char* in_obj_name)
340{
lichengzhangd7aea6c2025-06-05 16:35:54 +0800341 if (in_obj_name == NULL)
342 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800343 int32_t ret = 1;
344 TEEC_Result res;
lichengzhangd7aea6c2025-06-05 16:35:54 +0800345 char *tmp_buf = malloc(basic_buf_len * sizeof(char));
346 res = read_secure_object(&ctx, in_obj_name, tmp_buf, basic_buf_len);
b.liu68a94c92025-05-24 12:53:41 +0800347
348 if (res == TEEC_ERROR_ITEM_NOT_FOUND)
349 {
350 LOGE("the obj no found\n");
lichengzhangd7aea6c2025-06-05 16:35:54 +0800351 ret = GSW_HAL_ERROR_TEE_SFS_FILE_NOEXIST;
b.liu68a94c92025-05-24 12:53:41 +0800352 }
353 else if (res == TEEC_SUCCESS)
354 {
355 LOGE("the obj is exist\n");
356 ret = GSW_HAL_SUCCESS;
357 }
358 else
359 {
360 LOGE("Failed to read an object from the secure storage");
lichengzhangd7aea6c2025-06-05 16:35:54 +0800361 ret = GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800362 }
lichengzhangd7aea6c2025-06-05 16:35:54 +0800363 free(tmp_buf);
b.liu68a94c92025-05-24 12:53:41 +0800364 return ret;
365}
366
lichengzhangd7aea6c2025-06-05 16:35:54 +0800367/**
368* @brief deinit tee sdk
369* @param [in] None
370* @param [out] None
371* @retval GSW_HAL_SUCCESS is success\other is fail
372*/
b.liu68a94c92025-05-24 12:53:41 +0800373int32_t gsw_tee_sdk_deinit(void)
374{
375 if (terminate_tee_session) {
376 terminate_tee_session(&ctx); // 终止TEE会话
377 terminate_tee_session = NULL;
378 }
379
380 if (dlHandle_secure) {
381 dlclose(dlHandle_secure); // 卸载安全库
382 dlHandle_secure = NULL;
383 }
384
385 if (dlHandle_mbtk) {
386 dlclose(dlHandle_mbtk); // 卸载日志库
387 dlHandle_mbtk = NULL;
388 }
389
390 return GSW_HAL_SUCCESS;
391}
392