• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  * http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 
13 #include <string.h>
14 #include <securec.h>
15 #include "tee_core_api.h"
16 #include "tee_ext_api.h"
17 #include "tee_log.h"
18 #include "tee_mem_mgmt_api.h"
19 #include "tee_internal_se_api.h"
20 #include "tee_tui_gp_api.h"
21 #include "rpmb_fcntl.h"
22 #include "rpmb_driver_rw_api.h"
23 #include "tee_hw_ext_api.h"
24 #include "tee_hw_ext_api_legacy.h"
25 #include "tee_crypto_api.h"
26 #include "oemkey.h"
27 #include "tee_test_device_api.h"
28 #include "tee_object_api.h"
29 
30 #define CA_PKGN_VENDOR "/vendor/bin/tee_test_device_api"
31 #define CA_PKGN_SYSTEM "/system/bin/tee_test_device_api"
32 #define CA_PKGN_DATA "./tee_test_device_api"
33 #define CA_UID 0
34 #define SCP03_KEY_SIZE 16
35 
is_success_response(char * resp,uint32_t len)36 static int is_success_response(char *resp, uint32_t len)
37 {
38     if ((uint8_t)resp[len - 2] == 0x90 && resp[len - 1] == 0x0)
39         return 1;
40     return 0;
41 }
42 
CmdTestSEAPI(uint32_t nParamTypes,TEE_Param pParams[4])43 static TEE_Result CmdTestSEAPI(uint32_t nParamTypes, TEE_Param pParams[4])
44 {
45     (void)nParamTypes;
46     (void)pParams;
47     TEE_Result ret;
48     TEE_SEServiceHandle service = NULL;
49 
50     TEE_SEReaderHandle reader = NULL;
51     char reader_name[16] = { 0 };
52     uint32_t reader_len, name_len;
53     TEE_SESessionHandle session1, session2;
54     TEE_SEChannelHandle channel1, channel2;
55     int open_session_count = 3;
56     char rsp[256] = { 0 };
57     uint32_t rsp_len = 256;
58     char atr[100] = { 0 };
59     uint32_t atr_len = 100;
60     tlogi("[%s] begin --\n", __func__);
61 
62     ret = TEE_SEServiceOpen(&service);
63     if (ret != TEE_SUCCESS) {
64         tloge("test TEE_SEServiceOpen is failed! ret = 0x%x\n", ret);
65         //return ret;
66     }
67     tlogi("test TEE_SEServiceOpen is success!\n");
68 
69     reader_len = sizeof(reader);
70     ret = TEE_SEServiceGetReaders(service, &reader, &reader_len);
71     if (ret != TEE_SUCCESS) {
72         tloge("test TEE_SEServiceGetReaders is failed! ret = 0x%x\n", ret);
73         //goto clean;
74     }
75     tlogi("test TEE_SEServiceGetReaders is success!\n");
76 
77     name_len = 16;
78     ret = TEE_SEReaderGetName(reader, reader_name, &name_len);
79     if (ret != TEE_SUCCESS) {
80         tloge("test TEE_SEReaderGetName is failed! ret = 0x%x\n", ret);
81         //goto clean;
82     }
83     tlogi("test TEE_SEReaderGetName is success!\n");
84 
85     TEE_SEReaderProperties property;
86     TEE_SEReaderGetProperties(reader, &property);
87     tlogi("afterTEE_SEReaderGetProperties,property.sePresent=%d,property.teeOnly=%d,property.selectResponseEnable=%d\n",
88         property.sePresent, property.teeOnly, property.selectResponseEnable);
89 
90 open_session:
91     ret = TEE_SEReaderOpenSession(reader, &session1);
92     if (ret != TEE_SUCCESS) {
93         tloge("test TEE_SEReaderOpenSession for session1 is failed! ret = 0x%x\n", ret);
94         //goto clean;
95     }
96     tlogi("test TEE_SEReaderOpenSession for session1 is success!\n");
97     ret = TEE_SESessionGetATR(session1, atr, &atr_len);
98     if (ret != TEE_SUCCESS) {
99         tloge("test TEE_SESessionGetATR is failed! ret = 0x%x\n", ret);
100         //goto clean;
101     }
102     tlogi("test TEE_SESessionGetATR is success! atr_len =%d\n", atr_len);
103 
104     ret = TEE_SEReaderOpenSession(reader, &session2);
105     if (ret != TEE_SUCCESS) {
106         tloge("test TEE_SEReaderOpenSession for session2 is failed! ret = 0x%x\n", ret);
107         //goto clean;
108     }
109     tlogi("test TEE_SEReaderOpenSession for session2 is success!\n");
110 
111     open_session_count--;
112     if (open_session_count) {
113         if (open_session_count % 2) {
114             TEE_SEReaderCloseSessions(reader);
115         } else {
116             TEE_SESessionClose(session1);
117             TEE_SESessionClose(session2);
118         }
119         goto open_session;
120     }
121 
122     TEE_SEAID good_aid = { NULL, 0 }; // bufferlen should be within 5 to 16, or 0
123     ret = TEE_SESessionOpenBasicChannel(session1, &good_aid, &channel1);
124     if (ret != TEE_SUCCESS) {
125         tloge("test TEE_SESessionOpenBasicChannel is failed! ret = 0x%x\n", ret);
126         //goto clean;
127     }
128     tlogi("test TEE_SESessionOpenBasicChannel is success!\n");
129 
130     ret = TEE_SEChannelGetSelectResponse(channel1, rsp, &rsp_len);
131     if (ret != TEE_SUCCESS) {
132         tloge("test TEE_SEChannelGetSelectResponse is failed! ret = 0x%x\n", ret);
133         //goto clean;
134     }
135     tlogi("test TEE_SEChannelGetSelectResponse is success!\n");
136     if (!is_success_response(rsp, rsp_len)) {
137         tloge("is not success response!\n");
138         //goto clean;
139     }
140     uint8_t channel_id;
141     ret = TEE_SEChannelGetID(channel1, &channel_id);
142     if (ret != TEE_SUCCESS) {
143         tloge("test TEE_SEChannelGetID is failed! ret = 0x%x\n", ret);
144         //goto clean;
145     }
146     tlogi("test TEE_SEChannelGetID is success! channel_id = %d\n", channel_id);
147 
148     ret = TEE_SESessionOpenLogicalChannel(session2, &good_aid, &channel2);
149     if (ret != TEE_SUCCESS) {
150         tloge("test TEE_SESessionOpenLogicalChannel is failed! ret = 0x%x\n", ret);
151         //goto clean;
152     }
153     tlogi("test TEE_SESessionOpenLogicalChannel is success!\n");
154 
155     char hex_cmd_basic[] = { 0x80, 0xca, 0x9f, 0x7f, 0x00 };
156     char hex_cmd_logic[] = { 0x81, 0xca, 0x9f, 0x7f, 0x00 };
157 
158     rsp_len = 256;
159     ret = TEE_SEChannelTransmit(channel1, hex_cmd_basic, sizeof(hex_cmd_basic), rsp, &rsp_len);
160     if (ret != TEE_SUCCESS) {
161         tloge("test TEE_SEChannelTransmit for basic channel is failed! ret = 0x%x\n", ret);
162         //goto clean;
163     }
164     tlogi("test TEE_SEChannelTransmit for basic channel is success!\n");
165 
166     rsp_len = 256;
167     ret = TEE_SEChannelTransmit(channel2, hex_cmd_logic, sizeof(hex_cmd_logic), rsp, &rsp_len);
168     if (ret != TEE_SUCCESS) {
169         tloge("test TEE_SEChannelTransmit for logic channel is failed! ret = 0x%x\n", ret);
170         //goto clean;
171     }
172     tlogi("test TEE_SEChannelTransmit for logic channel is success!\n");
173 
174     TEE_SESessionClose(session1);
175     ret = TEE_SESessionIsClosed(session2);
176     if (ret != TEE_ERROR_BAD_STATE) {
177         tloge("test TEE_SESessionIsClosed for session2 is failed! ret = 0x%x\n", ret);
178         ret = TEE_ERROR_GENERIC;
179         //goto clean;
180     }
181     ret = TEE_SESessionIsClosed(session1);
182     if (ret != TEE_SUCCESS) {
183         tloge("test TEE_SESessionIsClosed for session1 is failed! ret = 0x%x\n", ret);
184         //goto clean;
185     }
186     ret = TEE_SESessionIsClosed(NULL);
187     if (ret != TEE_SUCCESS) {
188         tloge("test TEE_SESessionIsClosed for NULL is failed! ret = 0x%x\n", ret);
189         goto clean;
190     }
191 
192     TEE_SEChannelClose(channel1);
193     TEE_SESessionCloseChannels(session2);
194 
195 clean:
196     if (reader)
197         TEE_SEReaderCloseSessions(reader);
198     if (service)
199         TEE_SEServiceClose(service);
200 
201     return ret;
202 }
203 
CmdTestSEChannelSelectNext(uint32_t nParamTypes,TEE_Param pParams[4])204 static TEE_Result CmdTestSEChannelSelectNext(uint32_t nParamTypes, TEE_Param pParams[4])
205 {
206     (void)nParamTypes;
207     (void)pParams;
208     TEE_Result ret;
209     TEE_SEServiceHandle service = NULL;
210     TEE_SEReaderHandle reader = NULL;
211     uint32_t reader_len = 1;
212     TEE_SESessionHandle session1;
213     TEE_SEChannelHandle channel1;
214 
215     tlogi("[%s] begin --\n", __func__);
216 
217     ret = TEE_SEServiceOpen(&service);
218     if (ret != TEE_SUCCESS) {
219         tloge("test TEE_SEServiceOpen is failed! ret = 0x%x\n", ret);
220         return ret;
221     }
222     tlogi("test TEE_SEServiceOpen is success!\n");
223 
224     ret = TEE_SEServiceGetReaders(service, &reader, &reader_len);
225     if (ret != TEE_SUCCESS) {
226         tloge("test TEE_SEServiceGetReaders is failed! ret = 0x%x\n", ret);
227         goto clean;
228     }
229     tlogi("test TEE_SEServiceGetReaders is success!\n");
230 
231     ret = TEE_SEReaderOpenSession(reader, &session1);
232     if (ret != TEE_SUCCESS) {
233         tloge("test TEE_SEReaderOpenSession for session1 is failed! ret = 0x%x\n", ret);
234         goto clean;
235     }
236     tlogi("test TEE_SEReaderOpenSession for session1 is success!\n");
237 
238     uint8_t ppse_id[] = {0x32, 0x50, 0x41, 0x59, 0x2e, 0x53, 0x59, 0x53, 0x2e, 0x44, 0x44, 0x46, 0x30, 0x31};
239     TEE_SEAID aid = { ppse_id, sizeof(ppse_id) };
240     ret = TEE_SESessionOpenBasicChannel(session1, &aid, &channel1);
241     if (ret != TEE_SUCCESS) {
242         tloge("test TEE_SESessionOpenBasicChannel is failed! ret = 0x%x\n", ret);
243         goto clean;
244     }
245     tlogi("test TEE_SESessionOpenBasicChannel is success!\n");
246 
247     ret = TEE_SEChannelSelectNext(channel1);
248     if (ret != TEE_SUCCESS) {
249         tloge("test TEE_SEChannelSelectNext is failed! ret = 0x%x\n", ret);
250         goto clean;
251     }
252     tlogi("test TEE_SEChannelSelectNext is success!\n");
253 
254     TEE_SEChannelClose(channel1);
255     TEE_SESessionClose(session1);
256 
257 clean:
258     if (reader)
259         TEE_SEReaderCloseSessions(reader);
260     if (service)
261         TEE_SEServiceClose(service);
262 
263     return ret;
264 }
265 
scp_import_key(uint8_t * import_key,uint32_t keysize)266 static TEE_ObjectHandle scp_import_key(uint8_t *import_key, uint32_t keysize)
267 {
268     TEE_Attribute pattr;
269     TEE_Result ret;
270     uint32_t max_object_size = 512;
271     TEE_ObjectHandle gen_key;
272 
273     if (!import_key) {
274         tloge("import_key is null!\n");
275         return NULL;
276     }
277 
278     ret = TEE_AllocateTransientObject(TEE_TYPE_AES, max_object_size, &gen_key);
279     if (ret != TEE_SUCCESS) {
280         tloge("TEE_AllocateTransientObject is failed! ret = 0x%x\n", ret);
281         return NULL;
282     }
283     TEE_InitRefAttribute(&pattr, TEE_ATTR_SECRET_VALUE, import_key, keysize);
284     ret = TEE_PopulateTransientObject(gen_key, &pattr, 1);
285     if (ret != TEE_SUCCESS) {
286         tloge("TEE_PopulateTransientObject is failed! ret = 0x%x\n", ret);
287         TEE_FreeTransientObject(gen_key);
288         return NULL;
289     }
290 
291     return gen_key;
292 }
293 
CmdTestSESecureChannelOpenClose(uint32_t nParamTypes,TEE_Param pParams[4])294 static TEE_Result CmdTestSESecureChannelOpenClose(uint32_t nParamTypes, TEE_Param pParams[4])
295 {
296     (void)nParamTypes;
297     (void)pParams;
298     TEE_Result ret;
299     TEE_SEServiceHandle service = NULL;
300     TEE_SEReaderHandle reader = NULL;
301     uint32_t reader_len = 1;
302     TEE_SESessionHandle session1;
303     TEE_SEChannelHandle channel1;
304     char rsp[261] = { 0 };
305     uint32_t rsp_len = 261;
306 
307     tlogi("[%s] begin --\n", __func__);
308 
309     ret = TEE_SEServiceOpen(&service);
310     if (ret != TEE_SUCCESS) {
311         tloge("test TEE_SEServiceOpen is failed! ret = 0x%x\n", ret);
312         return ret;
313     }
314     tlogi("test TEE_SEServiceOpen is success!\n");
315 
316     ret = TEE_SEServiceGetReaders(service, &reader, &reader_len);
317     if (ret != TEE_SUCCESS) {
318         tloge("test TEE_SEServiceGetReaders is failed! ret = 0x%x\n", ret);
319         goto clean;
320     }
321     tlogi("test TEE_SEServiceGetReaders is success!\n");
322 
323     ret = TEE_SEReaderOpenSession(reader, &session1);
324     if (ret != TEE_SUCCESS) {
325         tloge("test TEE_SEReaderOpenSession for session1 is failed! ret = 0x%x\n", ret);
326         goto clean;
327     }
328     tlogi("test TEE_SEReaderOpenSession for session1 is success!\n");
329 
330     uint8_t test_aid[] = { 0xf0, 0xbb, 0xaa, 0xce, 0xaa, 0x68, 0x77, 0x5f, 0x77, 0x65, 0x61, 0x76, 0x65, 0x72, 0x00, 0x00 };
331     TEE_SEAID aid = { test_aid, sizeof(test_aid) };
332     ret = TEE_SESessionOpenLogicalChannel(session1, &aid, &channel1);
333     if (ret != TEE_SUCCESS) {
334         tloge("test TEE_SESessionOpenLogicalChannel is failed! ret = 0x%x\n", ret);
335         goto clean;
336     }
337     tlogi("test TEE_SESessionOpenLogicalChannel is success!\n");
338 
339     ret = TEE_SEChannelGetSelectResponse(channel1, rsp, &rsp_len);
340     if (ret != TEE_SUCCESS) {
341         tloge("test TEE_SEChannelGetSelectResponse is failed! ret = 0x%x\n", ret);
342         goto clean2;
343     }
344     tlogi("test TEE_SEChannelGetSelectResponse is success!\n");
345     if (!is_success_response(rsp, rsp_len)) {
346         tloge("TEE_SEChannelGetSelectResponse response error! len=%d, value is 0x%2x 0x%2x\n", rsp_len, rsp[0], rsp[1]);
347         goto clean2;
348     }
349 
350     uint8_t keys[SCP03_KEY_SIZE * 3] = {
351         0xe9, 0x49, 0x2c, 0x33, 0xf7, 0x48, 0x96, 0x05, 0x6f, 0xc5, 0xb9, 0x65, 0xf4, 0x5a, 0x4e, 0xf8,
352         0x69, 0x36, 0x1d, 0xa3, 0xd5, 0x32, 0x11, 0x56, 0xc4, 0x82, 0xfb, 0x02, 0x48, 0x18, 0xff, 0x1b,
353     };
354 
355     TEE_ObjectHandle encKeyhandle = scp_import_key(keys, SCP03_KEY_SIZE);
356     TEE_ObjectHandle macKeyhandle = scp_import_key(keys + SCP03_KEY_SIZE, SCP03_KEY_SIZE);
357     if ( encKeyhandle == NULL || macKeyhandle == NULL) {
358         tloge("scp_import_key failed!\n");
359         ret = TEE_ERROR_BAD_PARAMETERS;
360         goto clean2;
361     }
362 
363     TEE_SC_Params scParam = {
364         .scType = 0x01,   // type_scp03
365         .scOID = {
366             .buffer = (void *)"\x06\x08\x2A\x86\x48\x86\xFC\x6B\x04\x03",
367             .bufferLen = 10,
368         },
369         .scSecurityLevel = TEE_SC_CR_ENC_MAC,
370         .scCardKeyRef = {
371             .scKeyID = 0,
372             .scKeyVersion = 0,
373         },
374         .scDeviceKeyRef = {
375             .scKeyType = TEE_SC_KEY_SET,
376             .__TEE_key.scKeySetRef = {
377                 .scKeyEncHandle = encKeyhandle,
378                 .scKeyMacHandle = macKeyhandle,
379             },
380         },
381     };
382     ret = TEE_SESecureChannelOpen(channel1, &scParam);
383     if (ret != TEE_SUCCESS) {
384         tloge("test TEE_SESecureChannelOpen is failed! ret = 0x%x\n", ret);
385         goto clean3;
386     }
387     TEE_SESecureChannelClose(channel1);
388 
389 clean3:
390     if (encKeyhandle) {
391         TEE_FreeTransientObject(encKeyhandle);
392         encKeyhandle = NULL;
393     }
394     if (macKeyhandle) {
395         TEE_FreeTransientObject(macKeyhandle);
396         macKeyhandle = NULL;
397     }
398 clean2:
399     TEE_SEChannelClose(channel1);
400     TEE_SESessionClose(session1);
401 clean:
402     if (reader)
403         TEE_SEReaderCloseSessions(reader);
404     if (service)
405         TEE_SEServiceClose(service);
406 
407     return ret;
408 }
409 
test_TEE_TUICheckTextFormat()410 static TEE_Result test_TEE_TUICheckTextFormat()
411 {
412     static char *labeltext1 = "Welcome to TUI ";
413     uint32_t width = 0;
414     uint32_t height = 0;
415     uint32_t labelIndex = 0;
416 
417     TEE_Result ret = TEE_TUICheckTextFormat(labeltext1, &width, &height, &labelIndex);
418     if (ret != TEE_SUCCESS) {
419         tloge("test TEE_TUICheckTextFormat is failed! ret = 0x%x, labelIndex= %d\n", ret, labelIndex);
420     } else {
421         tlogi("test TEE_TUICheckTextFormat is success!\n");
422     }
423     return ret;
424 }
425 
CmdTestTUIAPI(uint32_t nParamTypes,TEE_Param pParams[4])426 static TEE_Result CmdTestTUIAPI(uint32_t nParamTypes, TEE_Param pParams[4])
427 {
428     (void)nParamTypes;
429     (void)pParams;
430     TEE_Result ret;
431     TEE_TUIScreenInfo myScreenInfo;
432     TEE_TUIScreenConfiguration myScreenConfig;
433     TEE_TUIEntryField myEntryFields[3];
434     TEE_TUIButtonType myKeyPressed;
435     char myLogin[26] = { 0 };
436     char myPassword[26] = { 0 };
437     char *pic;
438     uint32_t pic_len = 0;
439     uint32_t e_num = 3;
440     uint32_t font_type = 0;
441     char *lable_text = u_lable_text;
442     char *entry_text1 = u_entry_text1;
443     char *entry_text2 = u_entry_text2;
444     char *entry_text3 = u_entry_text3;
445 
446     tlogi("[%s] begin --\n", __func__);
447 
448     ret = TEE_TUIGetScreenInfo(TEE_TUI_PORTRAIT, e_num, &myScreenInfo);
449     if (ret != TEE_SUCCESS) {
450         tloge("test TEE_TUIGetScreenInfo is failed! ret = 0x%x\n", ret);
451         return ret;
452     }
453     tlogi("test TEE_TUIGetScreenInfo is success!\n");
454 
455     if (myScreenInfo.labelWidth == 1080) {
456         pic = pic1080s;
457         pic_len = sizeof(pic1080s);
458     } else {
459         pic = pic1440s;
460         pic_len = sizeof(pic1440s);
461     }
462     /* prepare the screen */
463     myScreenConfig.screenOrientation = TEE_TUI_PORTRAIT;
464     myScreenConfig.label.textColor[0] = 50;
465     myScreenConfig.label.textColor[1] = 50;
466     myScreenConfig.label.textColor[2] = 245;
467     myScreenConfig.label.text = lable_text;
468     myScreenConfig.label.textXOffset = 80;
469     myScreenConfig.label.textYOffset = 260;
470     myScreenConfig.label.imageXOffset = 3;
471     myScreenConfig.label.imageYOffset = 3;
472     myScreenConfig.label.image.source = TEE_TUI_REF_SOURCE;
473     myScreenConfig.label.image.ref.image = pic;
474     myScreenConfig.label.image.ref.imageLength = pic_len;
475     myScreenConfig.label.image.width = myScreenInfo.labelWidth;
476     myScreenConfig.label.image.height = myScreenInfo.labelHeight;
477     myScreenConfig.buttons[TEE_TUI_VALIDATE] = NULL;
478     myScreenConfig.buttons[TEE_TUI_CANCEL] = NULL;
479     myScreenConfig.buttons[TEE_TUI_CORRECTION] = NULL;
480     myScreenConfig.buttons[TEE_TUI_NEXT] = NULL;
481     myScreenConfig.buttons[TEE_TUI_PREVIOUS] = NULL;
482     myScreenConfig.buttons[TEE_TUI_OK] = NULL;
483     myScreenConfig.requestedButtons[TEE_TUI_VALIDATE] = true;
484     myScreenConfig.requestedButtons[TEE_TUI_CANCEL] = true;
485     myScreenConfig.requestedButtons[TEE_TUI_CORRECTION] = true;
486     myScreenConfig.requestedButtons[TEE_TUI_NEXT] = false;
487     myScreenConfig.requestedButtons[TEE_TUI_PREVIOUS] = true;
488     myScreenConfig.requestedButtons[TEE_TUI_OK] = false;
489 
490     myEntryFields[0].type = TEE_TUI_ALPHANUMERICAL;
491     myEntryFields[0].mode = TEE_TUI_CLEAR_MODE;
492     myEntryFields[0].label = entry_text1;
493     myEntryFields[0].minExpectedLength = 6;
494     myEntryFields[0].maxExpectedLength = 10;
495     myEntryFields[0].buffer = myLogin;
496     myEntryFields[0].bufferLength = 26;
497 
498     myEntryFields[1].type = TEE_TUI_ALPHANUMERICAL;
499     myEntryFields[1].mode = TEE_TUI_TEMPORARY_CLEAR_MODE;
500     myEntryFields[1].label = entry_text2;
501     myEntryFields[1].minExpectedLength = 6;
502     myEntryFields[1].maxExpectedLength = 25;
503     myEntryFields[1].buffer = myPassword;
504     myEntryFields[1].bufferLength = 26;
505 
506     myEntryFields[2].type = TEE_TUI_ALPHANUMERICAL;
507     myEntryFields[2].mode = TEE_TUI_TEMPORARY_CLEAR_MODE;
508     myEntryFields[2].label = entry_text3;
509     myEntryFields[2].minExpectedLength = 6;
510     myEntryFields[2].maxExpectedLength = 25;
511     myEntryFields[2].buffer = myPassword;
512     myEntryFields[2].bufferLength = 26;
513 
514     ret = TEE_TUIInitSession();
515     if (ret != TEE_SUCCESS) {
516         tloge("test TEE_TUIInitSession is failed! ret = 0x%x\n", ret);
517         return ret;
518     }
519     tlogi("test TEE_TUIInitSession is success!\n");
520 
521     ret = TEE_TUISetInfo(font_type);  // utf-8
522     if (ret != TEE_SUCCESS) {
523         tloge("test TEE_TUISetInfo is failed! ret = 0x%x\n", ret);
524         goto clean;
525     }
526     tlogi("test TEE_TUISetInfo is success!\n");
527 
528     /* prepare for new lable */
529     TEE_TUIScreenLabel backgroundLable = { 0 };
530     uint32_t len = sizeof(TEE_TUIScreenLabel);
531     backgroundLable.text = "test text for new lable";
532     backgroundLable.textXOffset = 0;
533     backgroundLable.textYOffset = 0;
534     backgroundLable.textColor[0] = 0;
535     backgroundLable.textColor[1] = 0;
536     backgroundLable.textColor[2] = 0;
537     backgroundLable.image.source = TEE_TUI_REF_SOURCE;
538     backgroundLable.image.ref.image = back_png;
539     backgroundLable.image.ref.imageLength = sizeof(back_png);
540     backgroundLable.imageXOffset = 0;
541     backgroundLable.imageYOffset = 50;
542 
543     ret = TEE_TUISetLabel(&backgroundLable, len);
544     if (ret != TEE_SUCCESS) {
545         tloge("test TEE_TUISetLabel is failed! ret = 0x%x\n", ret);
546         goto clean;
547     }
548     tlogi("test TEE_TUISetLabel is success!\n");
549 
550     ret = TEE_TUIDisplayScreen(&myScreenConfig, false, myEntryFields, e_num, &myKeyPressed);
551     if (ret != TEE_SUCCESS) {
552         tloge("test TEE_TUIDisplayScreen is failed! ret = 0x%x\n", ret);
553         goto clean;
554     }
555     tlogi("test TEE_TUIDisplayScreen is success!\n");
556 
557     if (myKeyPressed == TEE_TUI_VALIDATE) {
558         if (!strcmp(myPassword, "12345678") && !strcmp(myLogin, "qwertyui")) {
559             tlogi("your input is right!\n");
560         } else {
561             tloge("your input is wrong!\n");
562         }
563     }
564 
565     ret = test_TEE_TUICheckTextFormat();
566     if (ret != TEE_SUCCESS) {
567         goto clean;
568     }
569 
570     ret = TEE_TUINotify_fp();
571     if (ret != TEE_SUCCESS) {
572         tloge("test TEE_TUINotify_fp is failed! ret = 0x%x\n", ret);
573         goto clean;
574     }
575     tlogi("test TEE_TUINotify_fp is success!\n");
576 
577     ret = TEE_TUICloseSession();
578     if (ret != TEE_SUCCESS) {
579         tloge("test TEE_TUICloseSession is failed! ret = 0x%x\n", ret);
580         return ret;
581     }
582     tlogi("test TEE_TUICloseSession is success!\n");
583 
584     ret = TEE_TUISendEvent(TUI_EXIT);
585     if (ret != TEE_SUCCESS) {
586         tloge("test TEE_TUISendEvent is failed! ret = 0x%x\n", ret);
587         return ret;
588     }
589     tlogi("test TEE_TUISendEvent is success!\n");
590     return ret;
591 
592 clean:
593     TEE_TUICloseSession();
594     return ret;
595 }
596 
597 #define RPMB_BLOCK_SIZE 256
598 #define BUFFERLEN 400
TestNonceAPI()599 static TEE_Result TestNonceAPI()
600 {
601     TEE_Result ret;
602     uint8_t buffer1[BUFFERLEN + 1] = { 0 };
603     uint8_t buffer2[BUFFERLEN + 1] = { 0 };
604 
605     (void)memset_s(buffer1, BUFFERLEN, 'a', BUFFERLEN);
606 
607     ret = tee_ext_rpmb_driver_write(buffer1, BUFFERLEN, 0, 0);
608     if (ret != TEE_SUCCESS) {
609         tloge("test tee_ext_rpmb_driver_write is failed! ret = 0x%x\n", ret);
610         goto clean;
611     } else {
612         tlogi("test tee_ext_rpmb_driver_write is success\n");
613     }
614 
615     ret = tee_ext_rpmb_driver_remove(RPMB_BLOCK_SIZE, 0, 0);
616     if (ret != TEE_SUCCESS) {
617         tloge("test tee_ext_rpmb_driver_remove is failed! ret = 0x%x\n", ret);
618         goto clean;
619     } else {
620         tlogi("test tee_ext_rpmb_driver_remove is success\n");
621     }
622 
623     ret = memset_s(buffer1, BUFFERLEN, 0, RPMB_BLOCK_SIZE);
624     if (ret != 0) {
625         tloge("memset failed! ret = 0x%x\n", ret);
626         goto clean;
627     }
628 
629     ret = tee_ext_rpmb_driver_read(buffer2, BUFFERLEN, 0, 0);
630     if (ret != TEE_SUCCESS) {
631         tloge("test tee_ext_rpmb_driver_read is failed! ret = 0x%x\n", ret);
632         goto clean;
633     } else {
634         tlogi("test tee_ext_rpmb_driver_read is success\n");
635     }
636 
637     ret = TEE_MemCompare(buffer1, buffer2, BUFFERLEN);
638     if (ret != 0) {
639         tloge("TEE_MemCompare failed, ret = 0x%x\n", ret);
640         goto clean;
641     } else {
642         tlogi("TEE_MemCompare success\n");
643     }
644 
645 clean:
646     return ret;
647 }
648 
CmdTestRPMBAPI(uint32_t nParamTypes,TEE_Param pParams[4])649 static TEE_Result CmdTestRPMBAPI(uint32_t nParamTypes, TEE_Param pParams[4])
650 {
651     (void)nParamTypes;
652     (void)pParams;
653 
654     char *file_hello = "hello";
655     char *file_test = "test";
656     char *test_string = "Welcome to China!";
657     uint32_t read_size = 0;
658     uint8_t buffer[RPMB_BLOCK_SIZE] = { 0 };
659 
660     TEE_Result ret = TEE_RPMB_KEY_Status();
661     if (ret != TEE_RPMB_KEY_SUCCESS) {
662         tloge("test TEE_RPMB_KEY_Status is failed! ret = 0x%x\n", ret);
663     } else {
664         tlogi("test TEE_RPMB_KEY_Status is success, ret = 0x%x\n", ret);
665     }
666 
667     ret = TEE_RPMB_FS_Init();
668     if (ret != TEE_ERROR_ACCESS_DENIED) {
669         tloge("test TEE_RPMB_FS_Init is failed! ret = 0x%x\n", ret);
670     } else {
671         tlogi("test TEE_RPMB_FS_Init is success, this ta no permission, ret = 0x%x\n", ret);
672     }
673 
674     struct rpmb_fs_statdisk stat = { 0 };
675     ret = TEE_RPMB_FS_StatDisk(&stat);
676     if (ret != TEE_SUCCESS) {
677         tloge("test TEE_RPMB_FS_StatDisk is failed! ret = 0x%x\n", ret);
678     } else {
679         tlogi("test TEE_RPMB_FS_StatDisk is success, current TA Total=0x%x, used=0x%x, free=0x%x\n", stat.disk_size,
680             stat.ta_used_size, stat.free_size);
681     }
682 
683     // write file
684     ret = TEE_RPMB_FS_Write(file_hello, (const uint8_t *)test_string, strlen(test_string));
685     if (ret != TEE_SUCCESS) {
686         tloge("test TEE_RPMB_FS_Write is failed! ret = 0x%x\n", ret);
687         goto clean;
688     } else {
689         tlogi("test TEE_RPMB_FS_Write is success\n");
690     }
691 
692     struct rpmb_fs_stat file_st = { 0 };
693     ret = TEE_RPMB_FS_Stat(file_hello, &file_st);
694     if (ret != TEE_SUCCESS) {
695         tloge("test TEE_RPMB_FS_Stat is failed! ret = 0x%x\n", ret);
696         goto clean;
697     } else {
698         if ( file_st.size != strlen(test_string)) {
699             tloge("test TEE_RPMB_FS_Stat failed! get file size wrong, file_st.size = %d strlen of test_string = %d\n",
700                 ret, file_st.size, strlen(test_string));
701             goto clean;
702         } else {
703             tlogi("test TEE_RPMB_FS_Stat is success, file size = %d\n", file_st.size);
704         }
705     }
706 
707     ret = TEE_RPMB_FS_Read(file_hello, buffer, sizeof(buffer), &read_size);
708     if (ret != TEE_SUCCESS) {
709         tloge("test TEE_RPMB_FS_Read is failed! ret = 0x%x\n", ret);
710         goto clean;
711     } else {
712         if (TEE_MemCompare(buffer, test_string, read_size) != 0) {
713             tloge("test TEE_RPMB_FS_Read failed! get file context wrong\n");
714             goto clean;
715         } else {
716             tlogi("test TEE_RPMB_FS_Read is success\n");
717         }
718     }
719 
720     ret = TEE_RPMB_FS_Rename(file_hello, file_test);
721     if (ret != TEE_SUCCESS) {
722         tloge("test TEE_RPMB_FS_Rename is failed! ret = 0x%x\n", ret);
723         goto clean;
724     } else {
725         tlogi("test TEE_RPMB_FS_Rename is success\n");
726     }
727 
728     // set non_erasure
729     ret = TEE_RPMB_FS_SetAttr(file_test, TEE_RPMB_FMODE_NON_ERASURE);
730     if (ret != TEE_SUCCESS) {
731         tloge("test TEE_RPMB_FS_SetAttr is failed! ret = 0x%x\n", ret);
732         goto clean;
733     } else {
734         tlogi("test TEE_RPMB_FS_SetAttr is success\n");
735     }
736 
737     ret = TEE_RPMB_FS_Erase();
738     if (ret != TEE_SUCCESS) {
739         tloge("test TEE_RPMB_FS_Erase is failed! ret = 0x%x\n", ret);
740         goto clean;
741     } else {
742         tlogi("test TEE_RPMB_FS_Erase is success\n");
743     }
744     // read after erase, should read success
745     ret = TEE_RPMB_FS_Read(file_test, buffer, sizeof(buffer), &read_size);
746     if (ret != TEE_SUCCESS) {
747         tloge("test TEE_RPMB_FS_Read after erase is failed! ret = 0x%x\n", ret);
748         goto clean;
749     } else {
750         if ( TEE_MemCompare(buffer, test_string, read_size) != 0) {
751             tloge("test TEE_RPMB_FS_Read after erase failed! get file context wrong\n");
752             goto clean;
753         } else {
754             tlogi("test TEE_RPMB_FS_Read after erase is success\n");
755         }
756     }
757 
758     ret = TEE_RPMB_FS_Rm(file_test);
759     if (ret != TEE_SUCCESS) {
760         tloge("test TEE_RPMB_FS_Rm is failed! ret = 0x%x\n", ret);
761         goto clean;
762     } else {
763         tlogi("test TEE_RPMB_FS_Rm is success\n");
764     }
765 
766     // write file
767     ret = TEE_RPMB_FS_Write(file_hello, (const uint8_t *)test_string, strlen(test_string));
768     if (ret != TEE_SUCCESS) {
769         tloge("test TEE_RPMB_FS_Write is failed! ret = 0x%x\n", ret);
770         goto clean;
771     } else {
772         tlogi("test TEE_RPMB_FS_Write is success\n");
773     }
774 
775     ret = TEE_RPMB_FS_Format();
776     if (ret != TEE_SUCCESS) {
777         tloge("test TEE_RPMB_FS_Format is failed! ret = 0x%x\n", ret);
778         goto clean;
779     } else {
780         tlogi("test TEE_RPMB_FS_Format is success\n");
781     }
782 
783     // read file not exist
784     ret = TEE_RPMB_FS_Read(file_hello, buffer, sizeof(buffer), &read_size);
785     if (ret != TEE_ERROR_RPMB_FILE_NOT_FOUND) {
786         tloge("test TEE_RPMB_FS_Read for file not exist is failed! ret = 0x%x\n", ret);
787     } else {
788         tlogi("test TEE_RPMB_FS_Read for file not exist is success\n");
789     }
790 
791     ret = TestNonceAPI();
792     if (ret != TEE_SUCCESS) {
793         tloge("test TestNonceAPI is failed! ret = 0x%x\n", ret);
794     } else {
795         tlogi("test TestNonceAPI is success,  ret = 0x%x\n", ret);
796     }
797     return ret;
798 clean:
799     TEE_RPMB_FS_Rm(file_hello);
800     TEE_RPMB_FS_Rm(file_test);
801     return ret;
802 }
803 
804 #define SIZE_KOEM 16
805 #define SIZE_DIEID 32
806 #define SECRET_LEN 16
807 #define KEY_LEN 32
CmdTestHUKAPI(uint32_t nParamTypes,TEE_Param pParams[4])808 static TEE_Result CmdTestHUKAPI(uint32_t nParamTypes, TEE_Param pParams[4])
809 {
810     (void)nParamTypes;
811     (void)pParams;
812     TEE_Result ret;
813     uint8_t k_oem[SIZE_KOEM];
814     tlogi("[%s] begin --\n", __func__);
815 
816     ret = tee_hal_get_provision_key(k_oem, SIZE_KOEM);
817     if (ret != TEE_ERROR_NOT_SUPPORTED) {
818         tloge("test tee_hal_get_provision_key is failed! this ta should no permission. ret = 0x%x\n", ret);
819         return TEE_ERROR_GENERIC;
820     }
821     tlogi("test tee_hal_get_provision_key is success, match expect, this ta should no permission.\n");
822 
823     uint8_t die_id[SIZE_DIEID] = { 0 };
824     uint32_t die_id_size = sizeof(die_id);
825     ret = tee_ext_get_device_unique_id(die_id, &die_id_size);
826     if (ret != TEE_SUCCESS) {
827         tloge("test tee_ext_get_device_unique_id is failed! ret = 0x%x\n", ret);
828         goto clean;
829     }
830     tlogi("test tee_ext_get_device_unique_id is success! die_id_size = %d\n", die_id_size);
831 
832     uint8_t pSecret[SECRET_LEN] = { 0 };
833     uint8_t pKey[KEY_LEN] = { 0 };
834     TEE_GenerateRandom(pSecret, SECRET_LEN);
835     struct meminfo_t salt = { 0 };
836     salt.buffer = (uintptr_t)pSecret;
837     salt.size = SECRET_LEN;
838     struct meminfo_t key = { 0 };
839     key.buffer = (uintptr_t)pKey;
840     key.size = KEY_LEN;
841     ret = tee_ext_derive_key_iter(&salt, &key, 500, 10);  // user should check out key should not all zero
842     if (ret != TEE_SUCCESS) {
843         tloge("test tee_ext_derive_key_iter is failed! ret = 0x%x\n", ret);
844         goto clean;
845     }
846     tlogi("test tee_ext_derive_key_iter is success!\n");
847 #if 0
848     // test huk2 must teeos support huk2, else will fail
849     ret = tee_ext_derive_key_iter_by_huk2(&salt, &key, 500, 10);  // user should check out key should not all zero
850     if (ret != TEE_SUCCESS) {
851         tloge("test tee_ext_derive_key_iter_by_huk2 is failed! ret = 0x%x\n", ret);
852         goto clean;
853     }
854     tlogi("test tee_ext_derive_key_iter_by_huk2 is success!\n");
855 #endif
856     uint8_t salt1[16] = "123";
857     uint8_t salt2[16] = "456";
858     uint8_t key1[32] = { 0 };
859     uint8_t key2[32] = { 0 };
860     uint8_t key3[32] = { 0 };
861     uint8_t key4[32] = { 0 };
862     ret = TEE_EXT_DeriveTARootKey(salt1, sizeof(salt1), key1, sizeof(key1));
863     if (ret != TEE_SUCCESS) {
864         tloge("test TEE_EXT_DeriveTARootKey use salt1 is failed! ret = 0x%x\n", ret);
865         goto clean;
866     }
867     ret = TEE_EXT_DeriveTARootKey(salt2, sizeof(salt2), key2, sizeof(key2));
868     if (ret != TEE_SUCCESS) {
869         tloge("test TEE_EXT_DeriveTARootKey use salt2 is failed! ret = 0x%x\n", ret);
870         goto clean;
871     }
872 
873     ret = TEE_MemCompare(key1, key2, sizeof(key1));
874     if (ret != 0) {
875         tloge("TEE_MemCompare failed, key1 is same as key2, it is wrong. ret = 0x%x\n", ret);
876         goto clean;
877     }
878     tlogi("test TEE_EXT_DeriveTARootKey is success!\n");
879 
880     // test huk2 must teeos support huk2, else will fail
881     ret = tee_ext_derive_ta_root_key_by_huk2(salt1, sizeof(salt1), key3, sizeof(key3));
882     if (ret != TEE_SUCCESS) {
883         tloge("test tee_ext_derive_ta_root_key_by_huk2 use salt1 is failed! ret = 0x%x\n", ret);
884         goto clean;
885     }
886     ret = tee_ext_derive_ta_root_key_by_huk2(salt2, sizeof(salt2), key4, sizeof(key4));
887     if (ret != TEE_SUCCESS) {
888         tloge("test tee_ext_derive_ta_root_key_by_huk2 use salt2 is failed! ret = 0x%x\n", ret);
889         goto clean;
890     }
891 
892     ret = TEE_MemCompare(key3, key4, sizeof(key3));
893     if (ret != 0) {
894         tloge("TEE_MemCompare failed, key1 is same as key2, it is wrong. ret = 0x%x\n", ret);
895         goto clean;
896     }
897     tlogi("test tee_ext_derive_ta_root_key_by_huk2 is success!\n");
898 
899     // test huk2 must teeos support huk2, else will fail, user should check out key should not all zero
900     ret = tee_ext_root_derive_key2_by_huk2(pSecret, SECRET_LEN, key3, KEY_LEN);
901     if (ret != TEE_SUCCESS) {
902         tloge("test tee_ext_root_derive_key2_by_huk2 is failed! ret = 0x%x\n", ret);
903         goto clean;
904     }
905     tlogi("test tee_ext_root_derive_key2_by_huk2 is success!\n");
906 
907     // test huk2 must teeos support huk2, else will fail, user should check out key should not all zero
908     ret = tee_ext_root_uuid_derive_key_by_huk2(pSecret, SECRET_LEN, key4, KEY_LEN);
909     if (ret != TEE_SUCCESS) {
910         tloge("test tee_ext_root_uuid_derive_key_by_huk2 is failed! ret = 0x%x\n", ret);
911         goto clean;
912     }
913     tlogi("test tee_ext_root_uuid_derive_key_by_huk2 is success!\n");
914 
915 clean:
916     return ret;
917 }
918 
TA_CreateEntryPoint(void)919 TEE_Result TA_CreateEntryPoint(void)
920 {
921     tlogi("---- TA_CreateEntryPoint ---------");
922     TEE_Result ret;
923 
924     ret = AddCaller_CA_exec(CA_PKGN_VENDOR, CA_UID);
925     if (ret != TEE_SUCCESS) {
926         tloge("device_api ta add caller failed, ret: 0x%x", ret);
927         return ret;
928     }
929 
930     ret = AddCaller_CA_exec(CA_PKGN_SYSTEM, CA_UID);
931     if (ret != TEE_SUCCESS) {
932         tloge("device_api ta add caller failed, ret: 0x%x", ret);
933         return ret;
934     }
935 
936     ret = AddCaller_CA_exec(CA_PKGN_DATA, CA_UID);
937     if (ret != TEE_SUCCESS) {
938         tloge("device_api ta add caller failed, ret: 0x%x", ret);
939         return ret;
940     }
941 
942     return TEE_SUCCESS;
943 }
944 
TA_OpenSessionEntryPoint(uint32_t parmType,TEE_Param params[4],void ** sessionContext)945 TEE_Result TA_OpenSessionEntryPoint(uint32_t parmType, TEE_Param params[4], void **sessionContext)
946 {
947     (void)parmType;
948     (void)params;
949     (void)sessionContext;
950     tlogi("---- TA_OpenSessionEntryPoint --------");
951 
952     return TEE_SUCCESS;
953 }
954 
955 typedef TEE_Result (*func)(uint32_t nParamTypes, TEE_Param pParams[4]);
956 
957 struct testFunc {
958     uint32_t cmdId;
959     func funcName;
960 };
961 
962 struct testFunc g_testTable[] = {
963     { CMD_ID_TEST_SE_API, CmdTestSEAPI },
964     { CMD_ID_TEST_SECHANNELSELECTNEXT_API, CmdTestSEChannelSelectNext },
965     { CMD_ID_TEST_SESECURECHANNELOPENCLOSE_API, CmdTestSESecureChannelOpenClose },
966     { CMD_ID_TEST_TUI_API, CmdTestTUIAPI },
967     { CMD_ID_TEST_RPMB_API, CmdTestRPMBAPI },
968     { CMD_ID_TEST_HUK_API, CmdTestHUKAPI },
969 };
970 
971 uint32_t g_testTableSize = sizeof(g_testTable) / sizeof(g_testTable[0]);
972 
TA_InvokeCommandEntryPoint(void * sessionContext,uint32_t cmd,uint32_t parmType,TEE_Param params[4])973 TEE_Result TA_InvokeCommandEntryPoint(void *sessionContext, uint32_t cmd, uint32_t parmType, TEE_Param params[4])
974 {
975     TEE_Result ret = TEE_SUCCESS;
976     (void)sessionContext;
977     uint32_t i;
978     tlogi("---- TA invoke command ----------- command id: 0x%x", cmd);
979 
980     for (i = 0; i < g_testTableSize; i++) {
981         if (cmd == g_testTable[i].cmdId) {
982             ret = g_testTable[i].funcName(parmType, params);
983             if (ret != TEE_SUCCESS) {
984                 tloge("invoke command with cmdId: 0x%x failed! ret: 0x%x", cmd, ret);
985             } else {
986                 tlogi("invoke command with cmdId: 0x%x success! ret: 0x%x", cmd, ret);
987             }
988             return ret;
989         }
990     }
991 
992     tloge("not support this invoke command! cmdId: 0x%x", cmd);
993     return TEE_ERROR_GENERIC;
994 }
995 
TA_CloseSessionEntryPoint(void * sessionContext)996 void TA_CloseSessionEntryPoint(void *sessionContext)
997 {
998     (void)sessionContext;
999     tlogi("---- TA_CloseSessionEntryPoint -----");
1000 }
1001 
TA_DestroyEntryPoint(void)1002 void TA_DestroyEntryPoint(void)
1003 {
1004     tlogi("---- TA_DestroyEntryPoint ----");
1005 }
1006