blob: bbd3c399716b2e57004ef7fee101e3be54b92459 [file] [log] [blame]
Pascal Brandc639ac82015-07-02 08:53:34 +02001/*
2 * Copyright (c) 2014, STMicroelectronics International N.V.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <stdio.h>
15#include <string.h>
Jens Wiklanderb7940892015-10-23 16:02:40 +020016#include <limits.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020017
18#ifdef USER_SPACE
19#include <pthread.h>
20#include <unistd.h>
21#endif
22
23#include "xtest_test.h"
24#include "xtest_helpers.h"
Jens Wiklander4441fe22015-10-23 16:53:02 +020025#include <signed_hdr.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020026
27#include <ta_crypt.h>
28#include <ta_os_test.h>
29#include <ta_create_fail_test.h>
30#include <ta_rpc_test.h>
31#include <ta_sims_test.h>
32
33static void xtest_tee_test_1001(ADBG_Case_t *Case_p);
34static void xtest_tee_test_1004(ADBG_Case_t *Case_p);
35static void xtest_tee_test_1005(ADBG_Case_t *Case_p);
36static void xtest_tee_test_1006(ADBG_Case_t *Case_p);
37static void xtest_tee_test_1007(ADBG_Case_t *Case_p);
38static void xtest_tee_test_1008(ADBG_Case_t *Case_p);
39static void xtest_tee_test_1009(ADBG_Case_t *Case_p);
40static void xtest_tee_test_1010(ADBG_Case_t *Case_p);
41static void xtest_tee_test_1011(ADBG_Case_t *Case_p);
42static void xtest_tee_test_1012(ADBG_Case_t *Case_p);
43
44ADBG_CASE_DEFINE(XTEST_TEE_1001, xtest_tee_test_1001,
45 /* Title */
46 "Registering TAs",
47 /* Short description */
48 "Short description ...",
49 /* Requirement IDs */
50 "TEE-??",
51 /* How to implement */
52 "Description of how to implement ..."
53 );
54
55ADBG_CASE_DEFINE(XTEST_TEE_1004, xtest_tee_test_1004,
56 /* Title */
57 "Test User Crypt TA",
58 /* Short description */
59 "Short description ...",
60 /* Requirement IDs */
61 "TEE-??",
62 /* How to implement */
63 "Description of how to implement ..."
64 );
65
66ADBG_CASE_DEFINE(XTEST_TEE_1005, xtest_tee_test_1005,
67 /* Title */
68 "Many sessions",
69 /* Short description */
70 "Short description ...",
71 /* Requirement IDs */
72 "TEE-??",
73 /* How to implement */
74 "Description of how to implement ..."
75 );
76
77ADBG_CASE_DEFINE(XTEST_TEE_1006, xtest_tee_test_1006,
78 /* Title */
79 "Test Basic OS features",
80 /* Short description */
81 "Short description ...",
82 /* Requirement IDs */
83 "TEE-??",
84 /* How to implement */
85 "Description of how to implement ..."
86 );
87
88ADBG_CASE_DEFINE(XTEST_TEE_1007, xtest_tee_test_1007,
89 /* Title */
90 "Test Panic",
91 /* Short description */
92 "Short description ...",
93 /* Requirement IDs */
94 "TEE-??",
95 /* How to implement */
96 "Description of how to implement ..."
97 );
98
99ADBG_CASE_DEFINE(XTEST_TEE_1008, xtest_tee_test_1008,
100 /* Title */
101 "TEE internal client API",
102 /* Short description */
103 "Short description ...",
104 /* Requirement IDs */
105 "TEE-??",
106 /* How to implement */
107 "Description of how to implement ..."
108 );
109
110ADBG_CASE_DEFINE(XTEST_TEE_1009, xtest_tee_test_1009,
111 /* Title */
112 "TEE Wait",
113 /* Short description */
114 "Short description ...",
115 /* Requirement IDs */
116 "TEE-??",
117 /* How to implement */
118 "Description of how to implement ..."
119 );
120
121ADBG_CASE_DEFINE(XTEST_TEE_1010, xtest_tee_test_1010,
122 /* Title */
123 "Invalid memory access",
124 /* Short description */
125 "Short description ...",
126 /* Requirement IDs */
127 "TEE-??",
128 /* How to implement */
129 "Description of how to implement ..."
130 );
131
132ADBG_CASE_DEFINE(XTEST_TEE_1011, xtest_tee_test_1011,
133 /* Title */
134 "Test RPC features with User Crypt TA",
135 /* Short description */
136 "Short description ...",
137 /* Requirement IDs */
138 "TEE-??",
139 /* How to implement */
140 "Description of how to implement ..."
141 );
142
143ADBG_CASE_DEFINE(XTEST_TEE_1012, xtest_tee_test_1012,
144 /* Title */
145 "Test Single Instance Multi Session features with SIMS TA",
146 /* Short description */
147 "Short description ...",
148 /* Requirement IDs */
149 "TEE-??",
150 /* How to implement */
151 "Description of how to implement ..."
152 );
153
154struct xtest_crypto_session {
155 ADBG_Case_t *c;
156 TEEC_Session *session;
157 uint32_t cmd_id_sha256;
158 uint32_t cmd_id_aes256ecb_encrypt;
159 uint32_t cmd_id_aes256ecb_decrypt;
160};
161
162static void xtest_crypto_test(struct xtest_crypto_session *cs)
163{
164 uint32_t ret_orig;
165 uint8_t crypt_out[16];
166 uint8_t crypt_in[16] = { 22, 17 };
167
168 crypt_in[15] = 60;
169
170 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
171 {
172 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
173
174 op.params[0].tmpref.buffer = crypt_in;
175 op.params[0].tmpref.size = sizeof(crypt_in);
176 op.params[1].tmpref.buffer = crypt_out;
177 op.params[1].tmpref.size = sizeof(crypt_out);
178 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
179 TEEC_MEMREF_TEMP_OUTPUT,
180 TEEC_NONE, TEEC_NONE);
181
182 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
183 TEEC_InvokeCommand(cs->session,
184 cs->
185 cmd_id_aes256ecb_encrypt,
186 &op,
187 &ret_orig));
188 }
189 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
190
191 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
192 {
193 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
194 uint8_t out[16];
195
196 op.params[0].tmpref.buffer = crypt_out;
197 op.params[0].tmpref.size = sizeof(crypt_out);
198 op.params[1].tmpref.buffer = out;
199 op.params[1].tmpref.size = sizeof(out);
200 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
201 TEEC_MEMREF_TEMP_OUTPUT,
202 TEEC_NONE, TEEC_NONE);
203
204 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
205 TEEC_InvokeCommand(cs->session,
206 cs->
207 cmd_id_aes256ecb_decrypt,
208 &op,
209 &ret_orig));
210
211 if (!ADBG_EXPECT(cs->c, 0,
212 memcmp(crypt_in, out, sizeof(crypt_in)))) {
213 Do_ADBG_Log("crypt_in:");
214 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
215 Do_ADBG_Log("out:");
216 Do_ADBG_HexLog(out, sizeof(out), 16);
217 }
218 }
219 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
220
221 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
222 {
223 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
224 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
225 static const uint8_t sha256_out[] = {
226 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
227 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
228 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
229 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
230 };
231 uint8_t out[32] = { 0 };
232
233 op.params[0].tmpref.buffer = (void *)sha256_in;
234 op.params[0].tmpref.size = sizeof(sha256_in);
235 op.params[1].tmpref.buffer = out;
236 op.params[1].tmpref.size = sizeof(out);
237 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
238 TEEC_MEMREF_TEMP_OUTPUT,
239 TEEC_NONE, TEEC_NONE);
240
241 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
242 TEEC_InvokeCommand(cs->session,
243 cs->
244 cmd_id_sha256,
245 &op,
246 &ret_orig));
247
248 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
249 sizeof(sha256_out)))) {
250 Do_ADBG_Log("sha256_out:");
251 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
252 Do_ADBG_Log("out:");
253 Do_ADBG_HexLog(out, sizeof(out), 16);
254 }
255 }
256 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
257
258 Do_ADBG_BeginSubCase(cs->c,
259 "AES-256 ECB encrypt test, 32 bytes input, with fixed key");
260 {
261 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
262 static const uint8_t in[] = {
263 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
264 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
265 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
266 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
267 };
268 static const uint8_t exp_out[] = {
269 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
270 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
271 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
272 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
273 };
274 uint8_t out[sizeof(exp_out)];
275
276 op.params[0].tmpref.buffer = (void *)in;
277 op.params[0].tmpref.size = sizeof(in);
278 op.params[1].tmpref.buffer = out;
279 op.params[1].tmpref.size = sizeof(out);
280 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
281 TEEC_MEMREF_TEMP_OUTPUT,
282 TEEC_NONE, TEEC_NONE);
283
284 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
285 TEEC_InvokeCommand(cs->session,
286 cs->
287 cmd_id_aes256ecb_encrypt,
288 &op,
289 &ret_orig));
290
291 if (!ADBG_EXPECT(cs->c, 0,
292 memcmp(exp_out, out, sizeof(exp_out)))) {
293 Do_ADBG_Log("exp_out:");
294 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
295 Do_ADBG_Log("out:");
296 Do_ADBG_HexLog(out, sizeof(out), 16);
297 }
298 }
299 Do_ADBG_EndSubCase(cs->c,
300 "AES-256 ECB encrypt test, 32 bytes input, with fixed key");
301
302 Do_ADBG_BeginSubCase(cs->c,
303 "AES-256 ECB decrypt test, 32 bytes input, with fixed key");
304 {
305 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
306 static const uint8_t in[] = {
307 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
308 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
309 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
310 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
311 };
312 static const uint8_t exp_out[] = {
313 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
314 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
315 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
316 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
317 };
318 uint8_t out[sizeof(exp_out)];
319
320 op.params[0].tmpref.buffer = (void *)in;
321 op.params[0].tmpref.size = sizeof(in);
322 op.params[1].tmpref.buffer = out;
323 op.params[1].tmpref.size = sizeof(out);
324 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
325 TEEC_MEMREF_TEMP_OUTPUT,
326 TEEC_NONE, TEEC_NONE);
327
328 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
329 TEEC_InvokeCommand(cs->session,
330 cs->
331 cmd_id_aes256ecb_decrypt,
332 &op,
333 &ret_orig));
334
335 if (!ADBG_EXPECT(cs->c, 0,
336 memcmp(exp_out, out, sizeof(exp_out)))) {
337 Do_ADBG_Log("exp_out:");
338 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
339 Do_ADBG_Log("out:");
340 Do_ADBG_HexLog(out, sizeof(out), 16);
341 }
342 }
343 Do_ADBG_EndSubCase(cs->c,
344 "AES-256 ECB decrypt test, 32 bytes input, with fixed key");
345}
346
347static void xtest_tee_test_1001(ADBG_Case_t *c)
348{
349#ifdef USER_SPACE
350 (void)c;
351#else
352#define REG_TA(name) \
353 (void)ADBG_EXPECT_TEEC_SUCCESS(c, \
354 TEEC_RegisterTA(name, name ## _size))
355
356 REG_TA(crypt_user_ta);
357 REG_TA(os_test_ta);
358 REG_TA(create_fail_test_ta);
359 REG_TA(rpc_test_ta);
360 REG_TA(sims_test_ta);
361
362 TEEC_UnregisterTA(crypt_user_ta);
363 TEEC_UnregisterTA(os_test_ta);
364 TEEC_UnregisterTA(create_fail_test_ta);
365 TEEC_UnregisterTA(rpc_test_ta);
366 TEEC_UnregisterTA(sims_test_ta);
367
368 REG_TA(crypt_user_ta);
369 REG_TA(os_test_ta);
370 REG_TA(create_fail_test_ta);
371 REG_TA(rpc_test_ta);
372 REG_TA(sims_test_ta);
373 REG_TA(storage_ta);
374
375 REG_TA(gp_tta_testing_client_api_ta);
376 REG_TA(gp_tta_answer_success_to_open_session_invoke_ta);
377 REG_TA(gp_tta_answer_error_to_invoke_ta);
378 REG_TA(gp_tta_answer_error_to_open_session_ta);
379 REG_TA(gp_tta_check_open_session_with_4_parameters_ta);
380 REG_TA(gp_tta_time_ta);
381 REG_TA(gp_tta_ds_ta);
382 REG_TA(gp_tta_tcf_ta);
383 REG_TA(gp_tta_crypto_ta);
384 REG_TA(gp_tta_arithm_ta);
385 REG_TA(gp_tta_ica_ta);
386 REG_TA(gp_tta_ica2_ta);
387 REG_TA(gp_tta_tcf_singleinstance_ta);
388 REG_TA(gp_tta_tcf_multipleinstance_ta);
389#endif /*!USER_SPACE*/
390}
391
392static void xtest_tee_test_1004(ADBG_Case_t *c)
393{
394 TEEC_Session session = { 0 };
395 uint32_t ret_orig;
396 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
397 TA_CRYPT_CMD_AES256ECB_ENC,
398 TA_CRYPT_CMD_AES256ECB_DEC };
399
400 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
401 &session, &crypt_user_ta_uuid,
402 NULL, &ret_orig)))
403 return;
404
405 /* Run the "complete crypto test suite" */
406 xtest_crypto_test(&cs);
407
408 TEEC_CloseSession(&session);
409}
410
411#ifndef TEEC_ERROR_TARGET_DEAD
412/* To be removed when we have TEEC_ERROR_TARGET_DEAD from tee_client_api.h */
413#define TEEC_ERROR_TARGET_DEAD 0xFFFF3024
414#endif
415
416static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, uint32_t n)
417{
418 TEEC_Session session = { 0 };
419 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
420 uint32_t ret_orig;
421
422 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
423 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
424 &ret_orig));
425
426 op.params[0].value.a = n;
427 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
428 TEEC_NONE);
429
430 (void)ADBG_EXPECT_TEEC_RESULT(c,
431 TEEC_ERROR_TARGET_DEAD,
432 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
433 &ret_orig));
434
435 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
436 TEEC_InvokeCommand(&session,
437 TA_OS_TEST_CMD_BAD_MEM_ACCESS,
438 &op,
439 &ret_orig));
440 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
441
442 TEEC_CloseSession(&session);
443}
444
445static void xtest_tee_test_1005(ADBG_Case_t *c)
446{
447 uint32_t ret_orig;
448#define MAX_SESSIONS 3
449 TEEC_Session sessions[MAX_SESSIONS];
450 int i;
451
452 for (i = 0; i < MAX_SESSIONS; i++) {
453 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
454 xtest_teec_open_session(&sessions[i], &os_test_ta_uuid,
455 NULL, &ret_orig)))
456 break;
457 }
458
459 for (; --i >= 0; )
460 TEEC_CloseSession(&sessions[i]);
461}
462
463static void xtest_tee_test_1006(ADBG_Case_t *c)
464{
465 TEEC_Session session = { 0 };
466 uint32_t ret_orig;
467 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
468 uint8_t buf[32];
469
470 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
471 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
472 &ret_orig)))
473 return;
474
475 op.params[0].tmpref.buffer = buf;
476 op.params[0].tmpref.size = sizeof(buf);
477 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
478 TEEC_NONE, TEEC_NONE);
479
480 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
481 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
482 &ret_orig));
483
484 TEEC_CloseSession(&session);
485}
486
487static void xtest_tee_test_1007(ADBG_Case_t *c)
488{
489 TEEC_Session session = { 0 };
490 uint32_t ret_orig;
491
492 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
493 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
494 &ret_orig));
495
496 (void)ADBG_EXPECT_TEEC_RESULT(c,
497 TEEC_ERROR_TARGET_DEAD,
498 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
499 &ret_orig));
500
501 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
502
503 (void)ADBG_EXPECT_TEEC_RESULT(c,
504 TEEC_ERROR_TARGET_DEAD,
505 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
506 &ret_orig));
507
508 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
509
510 TEEC_CloseSession(&session);
511}
512
Jens Wiklanderb7940892015-10-23 16:02:40 +0200513static void uuid_to_full_name(char *buf, size_t blen, const TEEC_UUID *uuid,
514 const char *extra_suffix)
515{
Jens Wiklander4441fe22015-10-23 16:53:02 +0200516 static const char ta_dir[] = "/lib/optee_armtz";
Jens Wiklanderb7940892015-10-23 16:02:40 +0200517
518 snprintf(buf, blen,
519 "%s/%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x.ta%s",
520 ta_dir, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
521 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
522 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
523 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
524 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7],
525 extra_suffix ? extra_suffix : "");
526}
527
528static FILE *open_ta_file(const TEEC_UUID *uuid, const char *extra_suffix,
529 const char *mode)
530{
531 char buf[PATH_MAX];
532
533 uuid_to_full_name(buf, sizeof(buf), uuid, extra_suffix);
534 return fopen(buf, mode);
535}
536
537static bool rm_file(const TEEC_UUID *uuid, const char *extra_suffix)
538{
539 char buf[PATH_MAX];
540
541 uuid_to_full_name(buf, sizeof(buf), uuid, extra_suffix);
542 return !unlink(buf);
543}
544
Jens Wiklander4441fe22015-10-23 16:53:02 +0200545static bool rename_file(const TEEC_UUID *old_uuid, const char *old_extra_suffix,
546 const TEEC_UUID *new_uuid, const char *new_extra_suffix)
547{
548 char old_buf[PATH_MAX];
549 char new_buf[PATH_MAX];
550
551 uuid_to_full_name(old_buf, sizeof(old_buf), old_uuid, old_extra_suffix);
552 uuid_to_full_name(new_buf, sizeof(new_buf), new_uuid, new_extra_suffix);
553 return !rename(old_buf, new_buf);
554}
555
556static bool copy_file(const TEEC_UUID *src_uuid, const char *src_extra_suffix,
557 const TEEC_UUID *dst_uuid, const char *dst_extra_suffix)
Jens Wiklanderb7940892015-10-23 16:02:40 +0200558{
559 char buf[4 * 1024];
Jens Wiklander4441fe22015-10-23 16:53:02 +0200560 FILE *src = open_ta_file(src_uuid, src_extra_suffix, "r");
561 FILE *dst = open_ta_file(dst_uuid, dst_extra_suffix, "w");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200562 size_t r;
563 size_t w;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200564 bool ret = false;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200565
Jens Wiklander4441fe22015-10-23 16:53:02 +0200566 if (src && dst) {
567 do {
568 r = fread(buf, 1, sizeof(buf), src);
569 if (!r) {
570 ret = !!feof(src);
571 break;
572 }
573 w = fwrite(buf, 1, r, dst);
574 } while (w == r);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200575 }
Jens Wiklander4441fe22015-10-23 16:53:02 +0200576
577 if (src)
578 fclose(src);
579 if (dst)
580 fclose(dst);
581 return ret;
582}
583
584static bool corrupt_file(FILE *f, long offs, uint8_t mask)
585{
586 uint8_t b;
587
588 if (fseek(f, offs, SEEK_SET))
589 return false;
590
591 if (fread(&b, 1, 1, f) != 1)
592 return false;
593
594 b ^= mask;
595
596 if (fseek(f, offs, SEEK_SET))
597 return false;
598
599 if (fwrite(&b, 1, 1, f) != 1)
600 return false;
601
602 return true;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200603}
604
605static void load_fake_ta(ADBG_Case_t *c)
606{
607 static const TEEC_UUID fake_uuid = {
608 0x7e0a0900, 0x586b, 0x11e5,
609 { 0x93, 0x1f, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b }
610 };
611 TEEC_Session session = { 0 };
612 TEEC_Result res;
613 uint32_t ret_orig;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200614 bool r;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200615
Jens Wiklander4441fe22015-10-23 16:53:02 +0200616 r = copy_file(&create_fail_test_ta_uuid, NULL, &fake_uuid, NULL);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200617
618 if (ADBG_EXPECT_TRUE(c, r)) {
Jens Wiklander4441fe22015-10-23 16:53:02 +0200619 res = xtest_teec_open_session(&session, &fake_uuid, NULL,
620 &ret_orig);
621 if (res == TEEC_SUCCESS)
622 TEEC_CloseSession(&session);
623 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200624 }
625
626 ADBG_EXPECT_TRUE(c, rm_file(&fake_uuid, NULL));
627}
628
Jens Wiklander4441fe22015-10-23 16:53:02 +0200629static bool load_corrupt_ta(ADBG_Case_t *c, long offs, uint8_t mask)
630{
631 TEEC_Session session = { 0 };
632 TEEC_Result res;
633 uint32_t ret_orig;
634 FILE *f;
635 bool r;
636
637 r = copy_file(&create_fail_test_ta_uuid, NULL,
638 &create_fail_test_ta_uuid, "save");
639 if (!ADBG_EXPECT_TRUE(c, r)) {
640 rm_file(&create_fail_test_ta_uuid, "save");
641 return false;
642 }
643
644 f = open_ta_file(&create_fail_test_ta_uuid, NULL, "r+");
645 if (!ADBG_EXPECT_NOT_NULL(c, f)) {
646 rm_file(&create_fail_test_ta_uuid, "save");
647 return false;
648 }
649 r = corrupt_file(f, offs, mask);
650 fclose(f);
651
652 if (ADBG_EXPECT_TRUE(c, r)) {
653 res = xtest_teec_open_session(&session,
654 &create_fail_test_ta_uuid,
655 NULL, &ret_orig);
656 if (res == TEEC_SUCCESS)
657 TEEC_CloseSession(&session);
658 r &= ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
659 }
660
661 r &= ADBG_EXPECT_TRUE(c, rename_file(&create_fail_test_ta_uuid, "save",
662 &create_fail_test_ta_uuid, NULL));
663 return r;
664}
665
Pascal Brandc639ac82015-07-02 08:53:34 +0200666static void xtest_tee_test_1008(ADBG_Case_t *c)
667{
668 TEEC_Session session = { 0 };
669 TEEC_Session session_crypt = { 0 };
670 uint32_t ret_orig;
671
672 Do_ADBG_BeginSubCase(c, "Invoke command");
673 {
674 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
675 xtest_teec_open_session(&session_crypt,
676 &crypt_user_ta_uuid, NULL,
677 &ret_orig));
678
679 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
680 xtest_teec_open_session(&session, &os_test_ta_uuid,
681 NULL, &ret_orig));
682
683 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
684 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
685 NULL, &ret_orig));
686
687 TEEC_CloseSession(&session);
688 TEEC_CloseSession(&session_crypt);
689 }
690 Do_ADBG_EndSubCase(c, "Invoke command");
691
692 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
693 {
694 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
695
696 op.params[0].value.a = 2000;
697 op.paramTypes = TEEC_PARAM_TYPES(
698 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
699
700 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
701 xtest_teec_open_session(&session,
702 &os_test_ta_uuid,
703 NULL,
704 &ret_orig));
705
706 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
707 TEEC_InvokeCommand(&session,
708 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
709 &op, &ret_orig));
710
711 TEEC_CloseSession(&session);
712 }
713 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
714
715 Do_ADBG_BeginSubCase(c, "Create session fail");
716 {
Jens Wiklanderb7940892015-10-23 16:02:40 +0200717 size_t n;
718
Pascal Brandc639ac82015-07-02 08:53:34 +0200719 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
720 xtest_teec_open_session(&session_crypt,
721 &create_fail_test_ta_uuid, NULL,
722 &ret_orig));
Pascal Brandc639ac82015-07-02 08:53:34 +0200723 /*
724 * Run this several times to see that there's no memory leakage.
725 */
726 for (n = 0; n < 100; n++) {
727 Do_ADBG_Log("n = %zu", n);
728 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
729 xtest_teec_open_session(&session_crypt,
730 &create_fail_test_ta_uuid,
731 NULL, &ret_orig));
732 }
733 }
734 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200735
736 Do_ADBG_BeginSubCase(c, "Load fake uuid TA");
737 load_fake_ta(c);
738 Do_ADBG_EndSubCase(c, "Load fake uuid TA");
739
Jens Wiklander4441fe22015-10-23 16:53:02 +0200740 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
741 ADBG_EXPECT_TRUE(c,
742 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
743 ADBG_EXPECT_TRUE(c,
744 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
745 ADBG_EXPECT_TRUE(c,
746 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
747 ADBG_EXPECT_TRUE(c,
748 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
749 ADBG_EXPECT_TRUE(c,
750 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
751 ADBG_EXPECT_TRUE(c,
752 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
753 ADBG_EXPECT_TRUE(c,
754 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
755 ADBG_EXPECT_TRUE(c,
756 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
757 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
758 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 30000, 1)); /* payload */
759 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200760}
761
762#ifdef USER_SPACE
763static void *cancellation_thread(void *arg)
764{
765 /*
766 * Sleep 0.5 seconds before cancellation to make sure that the other
767 * thread is in RPC_WAIT.
768 */
769 (void)usleep(500000);
770 TEEC_RequestCancellation(arg);
771 return NULL;
772}
773#endif
774
775static void xtest_tee_test_1009(ADBG_Case_t *c)
776{
777 TEEC_Session session = { 0 };
778 uint32_t ret_orig;
779
780 Do_ADBG_BeginSubCase(c, "TEE Wait 0.1s");
781 {
782 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
783
784 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
785 xtest_teec_open_session(&session, &os_test_ta_uuid,
786 NULL, &ret_orig));
787
788 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
789 ret_orig);
790
791 op.params[0].value.a = 100;
792 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE,
793 TEEC_NONE, TEEC_NONE);
794
795 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
796 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_WAIT, &op,
797 &ret_orig));
798 TEEC_CloseSession(&session);
799 }
800 Do_ADBG_EndSubCase(c, "TEE Wait 0.1s");
801
802 Do_ADBG_BeginSubCase(c, "TEE Wait 0.5s");
803 {
804 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
805
806 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
807 xtest_teec_open_session(&session, &os_test_ta_uuid,
808 NULL, &ret_orig));
809
810 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
811 ret_orig);
812
813 op.params[0].value.a = 500;
814 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE,
815 TEEC_NONE, TEEC_NONE);
816
817 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
818 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_WAIT, &op,
819 &ret_orig));
820
821 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
822 ret_orig);
823 TEEC_CloseSession(&session);
824 }
825 Do_ADBG_EndSubCase(c, "TEE Wait 0.5s");
826
827#ifdef USER_SPACE
828 Do_ADBG_BeginSubCase(c, "TEE Wait 2s cancel");
829 {
830 pthread_t thr;
831 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
832
833 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
834 xtest_teec_open_session(&session, &os_test_ta_uuid,
835 NULL, &ret_orig));
836
837 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
838 ret_orig);
839
840 op.params[0].value.a = 2000;
841 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE,
842 TEEC_NONE, TEEC_NONE);
843
844 (void)ADBG_EXPECT(c, 0,
845 pthread_create(&thr, NULL, cancellation_thread, &op));
846
847 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_CANCEL,
848 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_WAIT, &op,
849 &ret_orig));
850
851 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
852 ret_orig);
853 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
854 TEEC_CloseSession(&session);
855 }
856 Do_ADBG_EndSubCase(c, "TEE Wait 2s cancel");
857#endif
858
859 Do_ADBG_BeginSubCase(c, "TEE Wait 2s");
860 {
861 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
862
863 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
864 xtest_teec_open_session(&session, &os_test_ta_uuid,
865 NULL, &ret_orig));
866
867 op.params[0].value.a = 2000;
868 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE,
869 TEEC_NONE, TEEC_NONE);
870
871 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
872 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_WAIT, &op,
873 &ret_orig));
874
875 TEEC_CloseSession(&session);
876 }
877 Do_ADBG_EndSubCase(c, "TEE Wait 2s");
878}
879
880static void xtest_tee_test_1010(ADBG_Case_t *c)
881{
882 unsigned n;
883
884 for (n = 1; n <= 5; n++) {
885 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
886 xtest_tee_test_invalid_mem_access(c, n);
887 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
888 }
889}
890
891static void xtest_tee_test_1011(ADBG_Case_t *c)
892{
893 TEEC_Session session = { 0 };
894 uint32_t ret_orig;
895 struct xtest_crypto_session cs = {
896 c, &session, TA_RPC_CMD_CRYPT_SHA256,
897 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
898 TA_RPC_CMD_CRYPT_AES256ECB_DEC
899 };
900 TEEC_UUID uuid = rpc_test_ta_uuid;
901
902 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
903 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
904 return;
905
906 /*
907 * Run the "complete crypto test suite" using RPC
908 */
909 xtest_crypto_test(&cs);
910 TEEC_CloseSession(&session);
911}
912
913/*
914 * Note that this test is failing when
915 * - running twice in a raw
916 * - and the user TA is statically linked
917 * This is because the counter is not reseted when opening the first session
918 * in case the TA is statically linked
919 */
920static void xtest_tee_test_1012(ADBG_Case_t *c)
921{
922 TEEC_Session session1 = { 0 };
923 TEEC_Session session2 = { 0 };
924 uint32_t ret_orig;
925 TEEC_UUID uuid = sims_test_ta_uuid;
926
927 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
928 {
929 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
930 static const uint8_t in[] = {
931 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
932 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
933 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
934 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
935 };
936 uint8_t out[32] = { 0 };
937 int i;
938
939 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
940 xtest_teec_open_session(&session1, &uuid, NULL,
941 &ret_orig));
942
943 op.params[0].value.a = 0;
944 op.params[1].tmpref.buffer = (void *)in;
945 op.params[1].tmpref.size = sizeof(in);
946 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
947 TEEC_MEMREF_TEMP_INPUT,
948 TEEC_NONE, TEEC_NONE);
949
950 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
951 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
952 &ret_orig));
953
954 for (i = 1; i < 1000; i++) {
955 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
956 xtest_teec_open_session(&session2, &uuid, NULL,
957 &ret_orig));
958
959 op.params[0].value.a = 0;
960 op.params[1].tmpref.buffer = out;
961 op.params[1].tmpref.size = sizeof(out);
962 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
963 TEEC_MEMREF_TEMP_OUTPUT,
964 TEEC_NONE, TEEC_NONE);
965
966 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
967 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
968 &op, &ret_orig));
969
970 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
971 sizeof(out))) {
972 Do_ADBG_Log("in:");
973 Do_ADBG_HexLog(in, sizeof(in), 16);
974 Do_ADBG_Log("out:");
975 Do_ADBG_HexLog(out, sizeof(out), 16);
976 }
977
978 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
979 TEEC_NONE, TEEC_NONE,
980 TEEC_NONE);
981
982 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
983 TEEC_InvokeCommand(&session1,
984 TA_SIMS_CMD_GET_COUNTER,
985 &op, &ret_orig));
986
987 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
988
989 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
990 TEEC_InvokeCommand(&session2,
991 TA_SIMS_CMD_GET_COUNTER, &op,
992 &ret_orig));
993
994 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
995 TEEC_CloseSession(&session2);
996 }
997
998 memset(out, 0, sizeof(out));
999 op.params[0].value.a = 0;
1000 op.params[1].tmpref.buffer = out;
1001 op.params[1].tmpref.size = sizeof(out);
1002 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1003 TEEC_MEMREF_TEMP_OUTPUT,
1004 TEEC_NONE, TEEC_NONE);
1005
1006 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1007 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1008 &ret_orig));
1009
1010 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1011 Do_ADBG_Log("in:");
1012 Do_ADBG_HexLog(in, sizeof(in), 16);
1013 Do_ADBG_Log("out:");
1014 Do_ADBG_HexLog(out, sizeof(out), 16);
1015 }
1016
1017 TEEC_CloseSession(&session1);
1018 }
1019}