blob: f390b8c615c21b5d552551aaccb0002b9277704a [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
Etienne Carrierea4653552017-01-11 10:04:24 +010014#include <limits.h>
15#include <pthread.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020016#include <stdio.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010017#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020018#include <string.h>
David Brownb2865ab2016-08-02 11:44:41 -060019#include <sys/stat.h>
20#include <sys/types.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010021#include <unistd.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020022
23#include "xtest_test.h"
24#include "xtest_helpers.h"
Jens Wiklander4441fe22015-10-23 16:53:02 +020025#include <signed_hdr.h>
Etienne Carriere92c34422018-02-09 13:11:40 +010026#include <util.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020027
Etienne Carriere726d8bc2017-03-21 15:45:59 +010028#include <pta_invoke_tests.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020029#include <ta_crypt.h>
30#include <ta_os_test.h>
31#include <ta_create_fail_test.h>
32#include <ta_rpc_test.h>
33#include <ta_sims_test.h>
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +030034#include <ta_miss_test.h>
35#include <ta_sims_keepalive_test.h>
Jens Wiklanderac27ec12015-07-15 15:23:14 +020036#include <ta_concurrent.h>
Etienne Carriere50abf9a2017-03-24 11:33:50 +010037#include <sdp_basic.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010038#ifdef CFG_SECSTOR_TA_MGMT_PTA
39#include <pta_secstor_ta_mgmt.h>
40#endif
41
42#ifndef MIN
43#define MIN(a, b) ((a) < (b) ? (a) : (b))
44#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020045
Pascal Brandc639ac82015-07-02 08:53:34 +020046struct xtest_crypto_session {
47 ADBG_Case_t *c;
48 TEEC_Session *session;
49 uint32_t cmd_id_sha256;
50 uint32_t cmd_id_aes256ecb_encrypt;
51 uint32_t cmd_id_aes256ecb_decrypt;
52};
53
54static void xtest_crypto_test(struct xtest_crypto_session *cs)
55{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010056 uint32_t ret_orig = 0;
57 uint8_t crypt_out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020058 uint8_t crypt_in[16] = { 22, 17 };
59
60 crypt_in[15] = 60;
61
62 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
63 {
64 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
65
66 op.params[0].tmpref.buffer = crypt_in;
67 op.params[0].tmpref.size = sizeof(crypt_in);
68 op.params[1].tmpref.buffer = crypt_out;
69 op.params[1].tmpref.size = sizeof(crypt_out);
70 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
71 TEEC_MEMREF_TEMP_OUTPUT,
72 TEEC_NONE, TEEC_NONE);
73
74 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
75 TEEC_InvokeCommand(cs->session,
76 cs->
77 cmd_id_aes256ecb_encrypt,
78 &op,
79 &ret_orig));
80 }
81 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
82
83 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
84 {
85 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010086 uint8_t out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020087
88 op.params[0].tmpref.buffer = crypt_out;
89 op.params[0].tmpref.size = sizeof(crypt_out);
90 op.params[1].tmpref.buffer = out;
91 op.params[1].tmpref.size = sizeof(out);
92 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
93 TEEC_MEMREF_TEMP_OUTPUT,
94 TEEC_NONE, TEEC_NONE);
95
96 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
97 TEEC_InvokeCommand(cs->session,
98 cs->
99 cmd_id_aes256ecb_decrypt,
100 &op,
101 &ret_orig));
102
103 if (!ADBG_EXPECT(cs->c, 0,
104 memcmp(crypt_in, out, sizeof(crypt_in)))) {
105 Do_ADBG_Log("crypt_in:");
106 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
107 Do_ADBG_Log("out:");
108 Do_ADBG_HexLog(out, sizeof(out), 16);
109 }
110 }
111 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
112
113 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
114 {
115 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
116 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
117 static const uint8_t sha256_out[] = {
118 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
119 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
120 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
121 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
122 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100123 uint8_t out[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200124
125 op.params[0].tmpref.buffer = (void *)sha256_in;
126 op.params[0].tmpref.size = sizeof(sha256_in);
127 op.params[1].tmpref.buffer = out;
128 op.params[1].tmpref.size = sizeof(out);
129 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
130 TEEC_MEMREF_TEMP_OUTPUT,
131 TEEC_NONE, TEEC_NONE);
132
133 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
134 TEEC_InvokeCommand(cs->session,
135 cs->
136 cmd_id_sha256,
137 &op,
138 &ret_orig));
139
140 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
141 sizeof(sha256_out)))) {
142 Do_ADBG_Log("sha256_out:");
143 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
144 Do_ADBG_Log("out:");
145 Do_ADBG_HexLog(out, sizeof(out), 16);
146 }
147 }
148 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
149
Etienne Carrierea3198522017-10-26 09:48:55 +0200150 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200151 {
152 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
153 static const uint8_t in[] = {
154 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
155 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
156 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
157 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
158 };
159 static const uint8_t exp_out[] = {
160 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
161 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
162 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
163 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
164 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100165 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200166
167 op.params[0].tmpref.buffer = (void *)in;
168 op.params[0].tmpref.size = sizeof(in);
169 op.params[1].tmpref.buffer = out;
170 op.params[1].tmpref.size = sizeof(out);
171 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
172 TEEC_MEMREF_TEMP_OUTPUT,
173 TEEC_NONE, TEEC_NONE);
174
175 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
176 TEEC_InvokeCommand(cs->session,
177 cs->
178 cmd_id_aes256ecb_encrypt,
179 &op,
180 &ret_orig));
181
182 if (!ADBG_EXPECT(cs->c, 0,
183 memcmp(exp_out, out, sizeof(exp_out)))) {
184 Do_ADBG_Log("exp_out:");
185 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
186 Do_ADBG_Log("out:");
187 Do_ADBG_HexLog(out, sizeof(out), 16);
188 }
189 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200190 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200191
Etienne Carrierea3198522017-10-26 09:48:55 +0200192 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200193 {
194 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
195 static const uint8_t in[] = {
196 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
197 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
198 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
199 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
200 };
201 static const uint8_t exp_out[] = {
202 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
203 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
204 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
205 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
206 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100207 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200208
209 op.params[0].tmpref.buffer = (void *)in;
210 op.params[0].tmpref.size = sizeof(in);
211 op.params[1].tmpref.buffer = out;
212 op.params[1].tmpref.size = sizeof(out);
213 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
214 TEEC_MEMREF_TEMP_OUTPUT,
215 TEEC_NONE, TEEC_NONE);
216
217 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
218 TEEC_InvokeCommand(cs->session,
219 cs->
220 cmd_id_aes256ecb_decrypt,
221 &op,
222 &ret_orig));
223
224 if (!ADBG_EXPECT(cs->c, 0,
225 memcmp(exp_out, out, sizeof(exp_out)))) {
226 Do_ADBG_Log("exp_out:");
227 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
228 Do_ADBG_Log("out:");
229 Do_ADBG_HexLog(out, sizeof(out), 16);
230 }
231 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200232 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200233}
234
235static void xtest_tee_test_1001(ADBG_Case_t *c)
236{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100237 TEEC_Result res = TEEC_ERROR_GENERIC;
238 TEEC_Session session = { };
239 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200240
Etienne Carriere11093162017-10-26 09:49:04 +0200241 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100242 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100243 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200244 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
245 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100246 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200247 }
248 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Pascal Brandc639ac82015-07-02 08:53:34 +0200249
Jens Wiklandercf16e842016-02-10 09:07:09 +0100250 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100251 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Jens Wiklandercf16e842016-02-10 09:07:09 +0100252 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200253}
Jens Wiklander14f48872018-06-29 15:30:13 +0200254ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200255
Jens Wiklander1d70a112017-10-16 15:16:39 +0200256static void xtest_tee_test_1002(ADBG_Case_t *c)
257{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100258 TEEC_Result res = TEEC_ERROR_GENERIC;
259 TEEC_Session session = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200260 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100261 uint32_t ret_orig = 0;
262 uint8_t buf[16 * 1024] = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200263 uint8_t exp_sum = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100264 size_t n = 0;
Jens Wiklander1d70a112017-10-16 15:16:39 +0200265
Etienne Carriere11093162017-10-26 09:49:04 +0200266 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200267 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
268 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200269 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
270 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200271 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200272 }
273 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200274
275 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
276 TEEC_NONE, TEEC_NONE);
277 op.params[0].tmpref.size = sizeof(buf);
278 op.params[0].tmpref.buffer = buf;
279
280 for (n = 0; n < sizeof(buf); n++)
281 buf[n] = n + 1;
282 for (n = 0; n < sizeof(buf); n++)
283 exp_sum += buf[n];
284
285 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
286 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
287 goto out;
288
289 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
290out:
291 TEEC_CloseSession(&session);
292}
Jens Wiklander14f48872018-06-29 15:30:13 +0200293ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200294
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100295struct test_1003_arg {
296 uint32_t test_type;
297 size_t repeat;
298 size_t max_before_lockers;
299 size_t max_during_lockers;
300 size_t before_lockers;
301 size_t during_lockers;
302 TEEC_Result res;
303 uint32_t error_orig;
304};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200305
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100306static void *test_1003_thread(void *arg)
307{
308 struct test_1003_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100309 TEEC_Session session = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100310 size_t rounds = 64 * 1024;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100311 size_t n = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100312
313 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
314 NULL, &a->error_orig);
315 if (a->res != TEEC_SUCCESS)
316 return NULL;
317
318 for (n = 0; n < a->repeat; n++) {
319 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
320
321 op.params[0].value.a = a->test_type;
322 op.params[0].value.b = rounds;
323
324 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
325 TEEC_VALUE_OUTPUT,
326 TEEC_NONE, TEEC_NONE);
327 a->res = TEEC_InvokeCommand(&session,
328 PTA_INVOKE_TESTS_CMD_MUTEX,
329 &op, &a->error_orig);
330 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
331 op.params[1].value.b != 1) {
332 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
333 a->res = TEEC_ERROR_BAD_STATE;
334 a->error_orig = 42;
335 break;
336 }
337
338 if (a->test_type == PTA_MUTEX_TEST_READER) {
339 if (op.params[1].value.a > a->max_before_lockers)
340 a->max_before_lockers = op.params[1].value.a;
341
342 if (op.params[1].value.b > a->max_during_lockers)
343 a->max_during_lockers = op.params[1].value.b;
344
345 a->before_lockers += op.params[1].value.a;
346 a->during_lockers += op.params[1].value.b;
347 }
348 }
349 TEEC_CloseSession(&session);
350
351 return NULL;
352}
353
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100354#define TEST_1003_THREAD_COUNT (3 * 2)
355
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100356static void xtest_tee_test_1003(ADBG_Case_t *c)
357{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100358 TEEC_Result res = TEEC_ERROR_GENERIC;
359 TEEC_Session session = { };
360 uint32_t ret_orig = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100361 size_t repeat = 20;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100362 struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100363 size_t max_read_concurrency = 0;
364 size_t max_read_waiters = 0;
365 size_t num_concurrent_read_lockers = 0;
366 size_t num_concurrent_read_waiters = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100367 size_t n = 0;
368 size_t nt = TEST_1003_THREAD_COUNT;
369 double mean_read_concurrency = 0;
370 double mean_read_waiters = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100371 size_t num_writers = 0;
372 size_t num_readers = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100373 pthread_t thr[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100374
375 /* Pseudo TA is optional: warn and nicely exit if not found */
376 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
377 &ret_orig);
378 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
379 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
380 return;
381 }
382 ADBG_EXPECT_TEEC_SUCCESS(c, res);
383 TEEC_CloseSession(&session);
384
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100385 for (n = 0; n < nt; n++) {
386 if (n % 3) {
387 arg[n].test_type = PTA_MUTEX_TEST_READER;
388 num_readers++;
389 } else {
390 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
391 num_writers++;
392 }
393 arg[n].repeat = repeat;
394 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
395 test_1003_thread, arg + n)))
396 nt = n; /* break loop and start cleanup */
397 }
398
399 for (n = 0; n < nt; n++) {
400 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
401 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
402 Do_ADBG_Log("error origin %" PRIu32,
403 arg[n].error_orig);
404 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
405 if (arg[n].max_during_lockers > max_read_concurrency)
406 max_read_concurrency =
407 arg[n].max_during_lockers;
408
409 if (arg[n].max_before_lockers > max_read_waiters)
410 max_read_waiters = arg[n].max_before_lockers;
411
412 num_concurrent_read_lockers += arg[n].during_lockers;
413 num_concurrent_read_waiters += arg[n].before_lockers;
414 }
415 }
416
417 mean_read_concurrency = (double)num_concurrent_read_lockers /
418 (double)(repeat * num_readers);
419 mean_read_waiters = (double)num_concurrent_read_waiters /
420 (double)(repeat * num_readers);
421
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100422 Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)",
423 TEST_1003_THREAD_COUNT, num_writers, num_readers);
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100424 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
425 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
426 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
427 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
428}
Jens Wiklander14f48872018-06-29 15:30:13 +0200429ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
430 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200431
Pascal Brandc639ac82015-07-02 08:53:34 +0200432static void xtest_tee_test_1004(ADBG_Case_t *c)
433{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100434 TEEC_Session session = { };
435 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200436 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
437 TA_CRYPT_CMD_AES256ECB_ENC,
438 TA_CRYPT_CMD_AES256ECB_DEC };
439
440 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
441 &session, &crypt_user_ta_uuid,
442 NULL, &ret_orig)))
443 return;
444
445 /* Run the "complete crypto test suite" */
446 xtest_crypto_test(&cs);
447
448 TEEC_CloseSession(&session);
449}
Jens Wiklander14f48872018-06-29 15:30:13 +0200450ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200451
Etienne Carriere92c34422018-02-09 13:11:40 +0100452static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200453{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100454 TEEC_Session session = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200455 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100456 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200457
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300458 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200459 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300460 &ret_orig)))
461 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200462
463 op.params[0].value.a = n;
464 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
465 TEEC_NONE);
466
467 (void)ADBG_EXPECT_TEEC_RESULT(c,
468 TEEC_ERROR_TARGET_DEAD,
469 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
470 &ret_orig));
471
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300472 (void)ADBG_EXPECT_TEEC_RESULT(c,
473 TEEC_ERROR_TARGET_DEAD,
474 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200475 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300476
Pascal Brandc639ac82015-07-02 08:53:34 +0200477 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
478
479 TEEC_CloseSession(&session);
480}
481
Etienne Carriere92c34422018-02-09 13:11:40 +0100482static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
483 size_t size)
484{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100485 TEEC_Session session = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100486 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100487 uint32_t ret_orig = 0;
488 TEEC_SharedMemory shm = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100489
Etienne Carriere92c34422018-02-09 13:11:40 +0100490 shm.size = size;
491 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
492 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
493 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
494 return;
495
496 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
497 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
498 &ret_orig)))
499 return;
500
501 op.params[0].value.a = (uint32_t)n;
502 op.params[1].memref.parent = &shm;
503 op.params[1].memref.size = size;
504 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
505 TEEC_NONE, TEEC_NONE);
506
507 (void)ADBG_EXPECT_TEEC_RESULT(c,
508 TEEC_ERROR_TARGET_DEAD,
509 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
510 &ret_orig));
511
512 (void)ADBG_EXPECT_TEEC_RESULT(c,
513 TEEC_ERROR_TARGET_DEAD,
514 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
515 &ret_orig));
516
517 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
518
519 TEEC_CloseSession(&session);
520}
521
Pascal Brandc639ac82015-07-02 08:53:34 +0200522static void xtest_tee_test_1005(ADBG_Case_t *c)
523{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100524 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200525#define MAX_SESSIONS 3
526 TEEC_Session sessions[MAX_SESSIONS];
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100527 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200528
529 for (i = 0; i < MAX_SESSIONS; i++) {
530 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200531 xtest_teec_open_session(&sessions[i],
532 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200533 NULL, &ret_orig)))
534 break;
535 }
536
537 for (; --i >= 0; )
538 TEEC_CloseSession(&sessions[i]);
539}
Jens Wiklander14f48872018-06-29 15:30:13 +0200540ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
Pascal Brandc639ac82015-07-02 08:53:34 +0200541
542static void xtest_tee_test_1006(ADBG_Case_t *c)
543{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100544 TEEC_Session session = { };
545 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200546 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100547 uint8_t buf[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200548
549 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
550 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
551 &ret_orig)))
552 return;
553
554 op.params[0].tmpref.buffer = buf;
555 op.params[0].tmpref.size = sizeof(buf);
556 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
557 TEEC_NONE, TEEC_NONE);
558
559 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
560 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
561 &ret_orig));
562
563 TEEC_CloseSession(&session);
564}
Jens Wiklander14f48872018-06-29 15:30:13 +0200565ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
566 "Test Basic OS features");
Pascal Brandc639ac82015-07-02 08:53:34 +0200567
568static void xtest_tee_test_1007(ADBG_Case_t *c)
569{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100570 TEEC_Session session = { };
571 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200572
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300573 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200574 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300575 &ret_orig)))
576 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200577
578 (void)ADBG_EXPECT_TEEC_RESULT(c,
579 TEEC_ERROR_TARGET_DEAD,
580 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
581 &ret_orig));
582
583 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
584
585 (void)ADBG_EXPECT_TEEC_RESULT(c,
586 TEEC_ERROR_TARGET_DEAD,
587 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
588 &ret_orig));
589
590 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
591
592 TEEC_CloseSession(&session);
593}
Jens Wiklander14f48872018-06-29 15:30:13 +0200594ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
Pascal Brandc639ac82015-07-02 08:53:34 +0200595
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100596#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jerome Forissierf02a2212015-10-29 14:33:35 +0100597#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000598# ifdef __ANDROID__
Yongqin Liu286f1fc2018-06-21 22:09:15 +0800599#define TA_DIR "/vendor/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000600# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100601#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000602# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100603#endif
604
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100605static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600606{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100607 char buf[PATH_MAX] = { };
David Brownb2865ab2016-08-02 11:44:41 -0600608
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100609 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100610 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100611 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200612 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
613 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
614 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600615 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200616
Jens Wiklanderb7940892015-10-23 16:02:40 +0200617 return fopen(buf, mode);
618}
619
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100620static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200621{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100622 TEEC_Session session = { };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100623 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
624 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100625 TEEC_Result res = TEEC_ERROR_GENERIC;
626 uint32_t ret_orig = 0;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100627 FILE *f = NULL;
628 bool r = false;
629 uint8_t *buf = NULL;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100630 size_t sz = 0;
631 size_t fread_res = 0;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200632
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100633 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
634 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
635 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200636
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100637 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
638 if (!ADBG_EXPECT_NOT_NULL(c, f))
639 goto out;
640 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
641 goto out;
642 sz = ftell(f);
643 rewind(f);
644
645 buf = malloc(sz);
646 if (!ADBG_EXPECT_NOT_NULL(c, buf))
647 goto out;
648
649 fread_res = fread(buf, 1, sz, f);
650 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
651 goto out;
652
Jens Wiklander4441fe22015-10-23 16:53:02 +0200653 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100654 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200655
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100656 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200657
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100658 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
659 TEEC_NONE, TEEC_NONE);
660 op.params[0].tmpref.buffer = buf;
661 op.params[0].tmpref.size = sz;
662
663 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
664 &ret_orig);
665 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
666out:
667 free(buf);
668 if (f)
669 fclose(f);
670 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200671 return r;
672}
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100673#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Jens Wiklander4441fe22015-10-23 16:53:02 +0200674
Pascal Brandc639ac82015-07-02 08:53:34 +0200675static void xtest_tee_test_1008(ADBG_Case_t *c)
676{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100677 TEEC_Session session = { };
678 TEEC_Session session_crypt = { };
679 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200680
681 Do_ADBG_BeginSubCase(c, "Invoke command");
682 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300683 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200684 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300685 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200686
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300687 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
688 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
689 NULL, &ret_orig));
690 TEEC_CloseSession(&session);
691 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200692
Pascal Brandc639ac82015-07-02 08:53:34 +0200693 }
694 Do_ADBG_EndSubCase(c, "Invoke command");
695
696 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
697 {
698 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
699
700 op.params[0].value.a = 2000;
701 op.paramTypes = TEEC_PARAM_TYPES(
702 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
703
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300704 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200705 xtest_teec_open_session(&session,
706 &os_test_ta_uuid,
707 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300708 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200709
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300710 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
711 TEEC_InvokeCommand(&session,
712 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
713 &op, &ret_orig));
714 TEEC_CloseSession(&session);
715 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200716 }
717 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
718
719 Do_ADBG_BeginSubCase(c, "Create session fail");
720 {
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100721 size_t n = 0;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200722
Pascal Brandc639ac82015-07-02 08:53:34 +0200723 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
724 xtest_teec_open_session(&session_crypt,
725 &create_fail_test_ta_uuid, NULL,
726 &ret_orig));
Pascal Brandc639ac82015-07-02 08:53:34 +0200727 /*
728 * Run this several times to see that there's no memory leakage.
729 */
730 for (n = 0; n < 100; n++) {
731 Do_ADBG_Log("n = %zu", n);
732 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
733 xtest_teec_open_session(&session_crypt,
734 &create_fail_test_ta_uuid,
735 NULL, &ret_orig));
736 }
737 }
738 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200739
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100740#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jens Wiklander4441fe22015-10-23 16:53:02 +0200741 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
742 ADBG_EXPECT_TRUE(c,
743 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
744 ADBG_EXPECT_TRUE(c,
745 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
746 ADBG_EXPECT_TRUE(c,
747 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
748 ADBG_EXPECT_TRUE(c,
749 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
750 ADBG_EXPECT_TRUE(c,
751 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
752 ADBG_EXPECT_TRUE(c,
753 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
754 ADBG_EXPECT_TRUE(c,
755 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
756 ADBG_EXPECT_TRUE(c,
757 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
758 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
Jerome Forissier895c5ca2019-02-28 17:27:46 +0100759 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
Jens Wiklander4441fe22015-10-23 16:53:02 +0200760 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100761#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Pascal Brandc639ac82015-07-02 08:53:34 +0200762}
Jens Wiklander14f48872018-06-29 15:30:13 +0200763ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
764 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200765
Pascal Brandc639ac82015-07-02 08:53:34 +0200766static void *cancellation_thread(void *arg)
767{
768 /*
769 * Sleep 0.5 seconds before cancellation to make sure that the other
770 * thread is in RPC_WAIT.
771 */
772 (void)usleep(500000);
773 TEEC_RequestCancellation(arg);
774 return NULL;
775}
Pascal Brandc639ac82015-07-02 08:53:34 +0200776
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300777static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
778 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200779{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100780 TEEC_Session session = { };
781 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300782 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200783
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100784 memset(&thr, 0, sizeof(thr));
785
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300786 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200787 {
788 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
789
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300790 if (ADBG_EXPECT_TEEC_SUCCESS(c,
791 xtest_teec_open_session(&session, &os_test_ta_uuid,
792 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200793
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300794 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
795 TEEC_ORIGIN_TRUSTED_APP,
796 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200797
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300798 op.params[0].value.a = timeout;
799 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
800 TEEC_NONE,
801 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300802 if (cancel) {
803 (void)ADBG_EXPECT(c, 0,
804 pthread_create(&thr, NULL,
805 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200806
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300807 (void)ADBG_EXPECT_TEEC_RESULT(c,
808 TEEC_ERROR_CANCEL,
809 TEEC_InvokeCommand(&session,
810 TA_OS_TEST_CMD_WAIT,
811 &op,
812 &ret_orig));
813 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300814
815 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
816 TEEC_InvokeCommand(&session,
817 TA_OS_TEST_CMD_WAIT,
818 &op,
819 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300820 if (cancel)
821 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300822
823 TEEC_CloseSession(&session);
824 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200825 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300826 Do_ADBG_EndSubCase(c, "%s", subcase);
827}
828
829static void xtest_tee_test_1009(ADBG_Case_t *c)
830{
831 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
832 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300833 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300834 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200835}
Jens Wiklander14f48872018-06-29 15:30:13 +0200836ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200837
838static void xtest_tee_test_1010(ADBG_Case_t *c)
839{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100840 unsigned int n = 0;
841 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100842 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200843
844 for (n = 1; n <= 5; n++) {
845 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
846 xtest_tee_test_invalid_mem_access(c, n);
847 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
848 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100849
850 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
851 for (n = 1; n <= 5; n++) {
852 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200853 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100854 n, memref_sz[idx]);
855 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
856 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200857 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100858 n, memref_sz[idx]);
859 }
860 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200861}
Jens Wiklander14f48872018-06-29 15:30:13 +0200862ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
863 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200864
865static void xtest_tee_test_1011(ADBG_Case_t *c)
866{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100867 TEEC_Session session = { };
868 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200869 struct xtest_crypto_session cs = {
870 c, &session, TA_RPC_CMD_CRYPT_SHA256,
871 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
872 TA_RPC_CMD_CRYPT_AES256ECB_DEC
873 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100874 struct xtest_crypto_session cs_privmem = {
875 c, &session,
876 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
877 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
878 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
879 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200880 TEEC_UUID uuid = rpc_test_ta_uuid;
881
882 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
883 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
884 return;
885
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100886 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200887 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100888 * Run the "complete crypto test suite" using TA-to-TA
889 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200890 */
891 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100892 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
893
894 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
895 /*
896 * Run the "complete crypto test suite" using TA-to-TA
897 * communication via TA private memory.
898 */
899 xtest_crypto_test(&cs_privmem);
900 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
901
Pascal Brandc639ac82015-07-02 08:53:34 +0200902 TEEC_CloseSession(&session);
903}
Jens Wiklander14f48872018-06-29 15:30:13 +0200904ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
905 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200906
907/*
908 * Note that this test is failing when
909 * - running twice in a raw
910 * - and the user TA is statically linked
911 * This is because the counter is not reseted when opening the first session
912 * in case the TA is statically linked
913 */
914static void xtest_tee_test_1012(ADBG_Case_t *c)
915{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100916 TEEC_Session session1 = { };
917 TEEC_Session session2 = { };
918 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200919 TEEC_UUID uuid = sims_test_ta_uuid;
920
921 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
922 {
923 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
924 static const uint8_t in[] = {
925 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
926 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
927 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
928 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
929 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100930 uint8_t out[32] = { };
931 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200932
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300933 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200934 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300935 &ret_orig)))
936 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200937
938 op.params[0].value.a = 0;
939 op.params[1].tmpref.buffer = (void *)in;
940 op.params[1].tmpref.size = sizeof(in);
941 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
942 TEEC_MEMREF_TEMP_INPUT,
943 TEEC_NONE, TEEC_NONE);
944
945 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
946 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
947 &ret_orig));
948
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100949 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300950 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200951 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300952 &ret_orig)))
953 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200954
955 op.params[0].value.a = 0;
956 op.params[1].tmpref.buffer = out;
957 op.params[1].tmpref.size = sizeof(out);
958 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
959 TEEC_MEMREF_TEMP_OUTPUT,
960 TEEC_NONE, TEEC_NONE);
961
962 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
963 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
964 &op, &ret_orig));
965
966 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
967 sizeof(out))) {
968 Do_ADBG_Log("in:");
969 Do_ADBG_HexLog(in, sizeof(in), 16);
970 Do_ADBG_Log("out:");
971 Do_ADBG_HexLog(out, sizeof(out), 16);
972 }
973
974 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
975 TEEC_NONE, TEEC_NONE,
976 TEEC_NONE);
977
978 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
979 TEEC_InvokeCommand(&session1,
980 TA_SIMS_CMD_GET_COUNTER,
981 &op, &ret_orig));
982
983 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
984
985 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
986 TEEC_InvokeCommand(&session2,
987 TA_SIMS_CMD_GET_COUNTER, &op,
988 &ret_orig));
989
990 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
991 TEEC_CloseSession(&session2);
992 }
993
994 memset(out, 0, sizeof(out));
995 op.params[0].value.a = 0;
996 op.params[1].tmpref.buffer = out;
997 op.params[1].tmpref.size = sizeof(out);
998 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
999 TEEC_MEMREF_TEMP_OUTPUT,
1000 TEEC_NONE, TEEC_NONE);
1001
1002 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1003 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1004 &ret_orig));
1005
1006 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1007 Do_ADBG_Log("in:");
1008 Do_ADBG_HexLog(in, sizeof(in), 16);
1009 Do_ADBG_Log("out:");
1010 Do_ADBG_HexLog(out, sizeof(out), 16);
1011 }
1012
1013 TEEC_CloseSession(&session1);
1014 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001015 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001016}
Jens Wiklander14f48872018-06-29 15:30:13 +02001017ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1018 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001019
1020struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001021 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001022 uint32_t cmd;
1023 uint32_t repeat;
1024 TEEC_SharedMemory *shm;
1025 uint32_t error_orig;
1026 TEEC_Result res;
1027 uint32_t max_concurrency;
1028 const uint8_t *in;
1029 size_t in_len;
1030 uint8_t *out;
1031 size_t out_len;
1032};
1033
1034static void *test_1013_thread(void *arg)
1035{
1036 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001037 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001038 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1039 uint8_t p2 = TEEC_NONE;
1040 uint8_t p3 = TEEC_NONE;
1041
Jens Wiklander70672972016-04-06 00:01:45 +02001042 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001043 &a->error_orig);
1044 if (a->res != TEEC_SUCCESS)
1045 return NULL;
1046
1047 op.params[0].memref.parent = a->shm;
1048 op.params[0].memref.size = a->shm->size;
1049 op.params[0].memref.offset = 0;
1050 op.params[1].value.a = a->repeat;
1051 op.params[1].value.b = 0;
1052 op.params[2].tmpref.buffer = (void *)a->in;
1053 op.params[2].tmpref.size = a->in_len;
1054 op.params[3].tmpref.buffer = a->out;
1055 op.params[3].tmpref.size = a->out_len;
1056
1057 if (a->in_len)
1058 p2 = TEEC_MEMREF_TEMP_INPUT;
1059 if (a->out_len)
1060 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1061
1062 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1063 TEEC_VALUE_INOUT, p2, p3);
1064
1065 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1066 a->max_concurrency = op.params[1].value.b;
1067 a->out_len = op.params[3].tmpref.size;
1068 TEEC_CloseSession(&session);
1069 return NULL;
1070}
1071
Pascal Brand4fa35582015-12-17 10:59:12 +01001072#define NUM_THREADS 3
1073
Jens Wiklander70672972016-04-06 00:01:45 +02001074static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1075 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001076{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001077 size_t nt = 0;
1078 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001079 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001080 TEEC_SharedMemory shm = { };
1081 size_t max_concurrency = 0;
1082 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001083 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1084 static const uint8_t sha256_out[] = {
1085 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1086 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1087 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1088 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1089 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001090 uint8_t out[32] = { };
1091 pthread_t thr[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001092
Jens Wiklander70672972016-04-06 00:01:45 +02001093 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001094 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001095
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001096 shm.size = sizeof(struct ta_concurrent_shm);
1097 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1098 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1099 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1100 return;
1101
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001102 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001103 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001104 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001105
1106 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001107 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001108 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001109 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001110 arg[n].shm = &shm;
1111 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1112 test_1013_thread, arg + n)))
1113 nt = n; /* break loop and start cleanup */
1114 }
1115
1116 for (n = 0; n < nt; n++) {
1117 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1118 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1119 if (arg[n].max_concurrency > max_concurrency)
1120 max_concurrency = arg[n].max_concurrency;
1121 }
1122
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001123 /*
1124 * Concurrency can be limited by several factors, for instance in a
1125 * single CPU system it's dependent on the Preemtion Model used by
1126 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1127 * best result there).
1128 */
1129 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001130 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, NUM_THREADS);
Pascal Brand4fa35582015-12-17 10:59:12 +01001131 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001132 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001133
Jens Wiklander70672972016-04-06 00:01:45 +02001134 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001135 memset(shm.buffer, 0, shm.size);
1136 memset(arg, 0, sizeof(arg));
1137 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001138 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001139
1140 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001141 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001142 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001143 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001144 arg[n].shm = &shm;
1145 arg[n].in = sha256_in;
1146 arg[n].in_len = sizeof(sha256_in);
1147 arg[n].out = out;
1148 arg[n].out_len = sizeof(out);
1149 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1150 test_1013_thread, arg + n)))
1151 nt = n; /* break loop and start cleanup */
1152 }
1153
1154 for (n = 0; n < nt; n++) {
1155 if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) &&
1156 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
1157 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1158 arg[n].out, arg[n].out_len);
1159 if (arg[n].max_concurrency > max_concurrency)
1160 max_concurrency = arg[n].max_concurrency;
1161 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001162 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001163 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001164
Pascal Brand4fa35582015-12-17 10:59:12 +01001165 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001166 TEEC_ReleaseSharedMemory(&shm);
1167}
Pascal Brand4fa35582015-12-17 10:59:12 +01001168
1169static void xtest_tee_test_1013(ADBG_Case_t *c)
1170{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001171 int i = 0;
1172 double mean_concurrency = 0;
1173 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001174 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001175
1176 if (level == 0)
1177 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001178
Jens Wiklander70672972016-04-06 00:01:45 +02001179 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001180 mean_concurrency = 0;
1181 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001182 xtest_tee_test_1013_single(c, &concurrency,
1183 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001184 mean_concurrency += concurrency;
1185 }
1186 mean_concurrency /= nb_loops;
1187
1188 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1189 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001190 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001191
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001192#ifndef CFG_PAGED_USER_TA
Jens Wiklander70672972016-04-06 00:01:45 +02001193 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1194 mean_concurrency = 0;
1195 for (i = 0; i < nb_loops; i++) {
1196 xtest_tee_test_1013_single(c, &concurrency,
1197 &concurrent_large_ta_uuid);
1198 mean_concurrency += concurrency;
1199 }
1200 mean_concurrency /= nb_loops;
1201
1202 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1203 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1204 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001205#endif
Jens Wiklander70672972016-04-06 00:01:45 +02001206}
Jens Wiklander14f48872018-06-29 15:30:13 +02001207ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
1208 "Test concurency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001209
1210#ifdef CFG_SECURE_DATA_PATH
1211static void xtest_tee_test_1014(ADBG_Case_t *c)
1212{
1213 UNUSED(c);
1214
1215 int size = 17000;
1216 int loop = 10;
1217 int ion_heap = DEFAULT_ION_HEAP_TYPE;
1218 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001219 int test = 0;
1220 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001221
1222 test = TEST_NS_TO_TA;
1223 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001224 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001225 ADBG_EXPECT(c, 0, ret);
1226 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1227
1228 test = TEST_TA_TO_TA;
1229 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001230 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001231 ADBG_EXPECT(c, 0, ret);
1232 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1233
1234 test = TEST_TA_TO_PTA;
1235 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP pTA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001236 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001237 ADBG_EXPECT(c, 0, ret);
1238 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP pTA");
1239
1240 test = TEST_NS_TO_PTA;
Etienne Carrierea3198522017-10-26 09:48:55 +02001241 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001242 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001243 ADBG_EXPECT(c, 1, ret);
Etienne Carrierea3198522017-10-26 09:48:55 +02001244 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001245}
Jens Wiklander14f48872018-06-29 15:30:13 +02001246ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1247 "Test secure data path against SDP TAs and pTAs");
1248#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001249
1250static void xtest_tee_test_1015(ADBG_Case_t *c)
1251{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001252 TEEC_Result res = TEEC_ERROR_GENERIC;
1253 TEEC_Session session = { };
1254 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001255
Etienne Carriere11093162017-10-26 09:49:04 +02001256 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001257 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1258 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001259 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1260 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001261 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001262 }
1263 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001264
1265 ADBG_EXPECT_TEEC_SUCCESS(c,
1266 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1267 NULL, &ret_orig));
1268 TEEC_CloseSession(&session);
1269}
Jens Wiklander14f48872018-06-29 15:30:13 +02001270ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1271 "FS hash-tree corner cases");
Jerome Forissiere916b102017-06-07 17:55:52 +02001272
1273static void xtest_tee_test_1016(ADBG_Case_t *c)
1274{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001275 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001276 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001277 uint32_t ret_orig = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001278
1279 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1280 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1281 &ret_orig)))
1282 return;
1283
1284 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1285 TEEC_NONE);
1286
1287 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1288 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1289 &ret_orig));
1290
1291 TEEC_CloseSession(&session);
1292}
Jens Wiklander14f48872018-06-29 15:30:13 +02001293ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1294 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001295
1296static void xtest_tee_test_1017(ADBG_Case_t *c)
1297{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001298 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001299 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001300 uint32_t ret_orig = 0;
1301 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001302 size_t page_size = 4096;
1303
Jens Wiklander87e81702018-03-20 12:00:00 +08001304 shm.size = 8 * page_size;
1305 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1306 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1307 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1308 return;
1309
1310 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1311 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1312 &ret_orig)))
1313 goto out;
1314
1315 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1316 TEEC_MEMREF_PARTIAL_INPUT,
1317 TEEC_MEMREF_PARTIAL_OUTPUT,
1318 TEEC_MEMREF_PARTIAL_OUTPUT);
1319
1320 /*
1321 * The first two memrefs are supposed to be combined into in
1322 * region and the last two memrefs should have one region each
1323 * when the parameters are mapped for the TA.
1324 */
1325 op.params[0].memref.parent = &shm;
1326 op.params[0].memref.size = page_size;
1327 op.params[0].memref.offset = 0;
1328
1329 op.params[1].memref.parent = &shm;
1330 op.params[1].memref.size = page_size;
1331 op.params[1].memref.offset = page_size;
1332
1333 op.params[2].memref.parent = &shm;
1334 op.params[2].memref.size = page_size;
1335 op.params[2].memref.offset = 4 * page_size;
1336
1337 op.params[3].memref.parent = &shm;
1338 op.params[3].memref.size = 2 * page_size;
1339 op.params[3].memref.offset = 6 * page_size;
1340
1341 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1342 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1343 &ret_orig));
1344
1345 TEEC_CloseSession(&session);
1346out:
1347 TEEC_ReleaseSharedMemory(&shm);
1348}
Jens Wiklander14f48872018-06-29 15:30:13 +02001349ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1350 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001351
1352static void xtest_tee_test_1018(ADBG_Case_t *c)
1353{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001354 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001355 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001356 uint32_t ret_orig = 0;
1357 TEEC_SharedMemory shm = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001358 size_t page_size = 4096;
1359
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001360 shm.size = 8 * page_size;
1361 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1362 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1363 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1364 return;
1365
1366 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1367 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1368 &ret_orig)))
1369 goto out;
1370
1371 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1372 TEEC_MEMREF_PARTIAL_INPUT,
1373 TEEC_MEMREF_PARTIAL_OUTPUT,
1374 TEEC_MEMREF_PARTIAL_OUTPUT);
1375
1376 /*
1377 * The first two memrefs are supposed to be combined into in
1378 * region and the last two memrefs should have one region each
1379 * when the parameters are mapped for the TA.
1380 */
1381 op.params[0].memref.parent = &shm;
1382 op.params[0].memref.size = page_size;
1383 op.params[0].memref.offset = 0;
1384
1385 op.params[1].memref.parent = &shm;
1386 op.params[1].memref.size = page_size;
1387 op.params[1].memref.offset = page_size;
1388
1389 op.params[2].memref.parent = &shm;
1390 op.params[2].memref.size = page_size;
1391 op.params[2].memref.offset = 4 * page_size;
1392
1393 op.params[3].memref.parent = &shm;
1394 op.params[3].memref.size = 3 * page_size;
1395 op.params[3].memref.offset = 6 * page_size;
1396
1397 /*
1398 * Depending on the tee driver we may have different error codes.
1399 * What's most important is that secure world doesn't panic and
1400 * that someone detects an error.
1401 */
1402 ADBG_EXPECT_NOT(c, TEE_SUCCESS,
1403 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1404 &ret_orig));
1405
1406 TEEC_CloseSession(&session);
1407out:
1408 TEEC_ReleaseSharedMemory(&shm);
1409}
Jens Wiklander14f48872018-06-29 15:30:13 +02001410ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1411 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001412
Victor Chong3ff36f52018-06-07 04:37:00 +01001413#if defined(CFG_TA_DYNLINK)
Jerome Forissier53bde722018-05-31 09:14:54 +02001414static void xtest_tee_test_1019(ADBG_Case_t *c)
1415{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001416 TEEC_Session session = { };
1417 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001418
1419 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1420 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1421 &ret_orig)))
1422 return;
1423
1424 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1425 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1426 &ret_orig));
1427
1428 (void)ADBG_EXPECT_TEEC_RESULT(c,
1429 TEEC_ERROR_TARGET_DEAD,
1430 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1431 NULL, &ret_orig));
1432
1433 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1434
1435 TEEC_CloseSession(&session);
1436}
Jens Wiklander14f48872018-06-29 15:30:13 +02001437ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1438 "Test dynamically linked TA");
1439#endif /*CFG_TA_DYNLINK*/
Jerome Forissier3357f872018-10-05 15:13:44 +02001440
1441static void xtest_tee_test_1020(ADBG_Case_t *c)
1442{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001443 TEEC_Result res = TEEC_ERROR_GENERIC;
1444 TEEC_Session session = { };
1445 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001446
1447 /* Pseudo TA is optional: warn and nicely exit if not found */
1448 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1449 &ret_orig);
1450 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1451 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1452 return;
1453 }
1454 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1455
1456 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1457 NULL, &ret_orig);
1458 if (res != TEEC_SUCCESS) {
1459 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1460 ret_orig);
1461 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1462 Do_ADBG_Log(" - 1020 - skip test, feature not "
1463 "implemented");
1464 goto out;
1465 }
1466 /* Error */
1467 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1468 }
1469out:
1470 TEEC_CloseSession(&session);
1471}
1472ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1473 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001474
1475static TEEC_Result open_sec_session(TEEC_Session *session,
1476 const TEEC_UUID *uuid)
1477{
1478 TEEC_Result res = TEEC_ERROR_GENERIC;
1479 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1480 uint32_t ret_orig = 0;
1481
1482 op.params[0].tmpref.buffer = (void *)uuid;
1483 op.params[0].tmpref.size = sizeof(*uuid);
1484 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1485 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1486
1487 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1488 &op, &ret_orig);
1489 if (res != TEEC_SUCCESS)
1490 return TEEC_ERROR_GENERIC;
1491
1492 return res;
1493}
1494
1495static TEEC_Result sims_get_counter(TEEC_Session *session,
1496 uint32_t *counter)
1497{
1498 TEEC_Result res = TEEC_ERROR_GENERIC;
1499 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1500 uint32_t ret_orig = 0;
1501
1502 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1503 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1504
1505 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1506 &op, &ret_orig);
1507 if (res == TEEC_SUCCESS)
1508 *counter = op.params[0].value.a;
1509
1510 return res;
1511}
1512
1513static TEEC_Result trigger_panic(TEEC_Session *session,
1514 const TEEC_UUID *uuid)
1515{
1516 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1517 uint32_t ret_orig = 0;
1518
1519 if (!uuid) {
1520 op.params[0].tmpref.buffer = NULL;
1521 op.params[0].tmpref.size = 0;
1522 } else {
1523 op.params[0].tmpref.buffer = (void *)uuid;
1524 op.params[0].tmpref.size = sizeof(*uuid);
1525 }
1526 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1527 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1528
1529 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1530 &op, &ret_orig);
1531}
1532
1533static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1534 bool multi_instance)
1535{
1536 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1537 uint32_t counter = 0;
1538 uint32_t ret_orig = 0;
1539 uint32_t exp_counter = 0;
1540 TEEC_Session cs[3] = { };
1541
1542 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1543 xtest_teec_open_session(&cs[0], uuid, NULL,
1544 &ret_orig)))
1545 return;
1546
1547 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1548 xtest_teec_open_session(&cs[1], uuid, NULL,
1549 &ret_orig)))
1550 goto bail0;
1551
1552 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1553 goto bail1;
1554
1555 if (!ADBG_EXPECT(c, 0, counter))
1556 goto bail1;
1557
1558 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1559 goto bail1;
1560
1561 exp_counter = multi_instance ? 0 : 1;
1562 if (ADBG_EXPECT(c, exp_counter, counter))
1563 goto bail1;
1564
1565 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1566 trigger_panic(&cs[1], NULL)))
1567 goto bail1;
1568
1569 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1570 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1571 sims_get_counter(&cs[0], &counter)))
1572 goto bail1;
1573
1574 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1575 sims_get_counter(&cs[1], &counter)))
1576 goto bail1;
1577
1578 /* Attempt to open a session on panicked context */
1579 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1580 xtest_teec_open_session(&cs[1], uuid, NULL,
1581 &ret_orig)))
1582 goto bail1;
1583
1584 /* Sanity check of still valid TA context */
1585 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1586 xtest_teec_open_session(&cs[2], uuid, NULL,
1587 &ret_orig)))
1588 goto bail1;
1589
1590 /* Sanity check of still valid TA context */
1591 if (multi_instance) {
1592 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1593 sims_get_counter(&cs[0], &counter)))
1594 goto bail2;
1595
1596 if (!ADBG_EXPECT(c, 0, counter))
1597 goto bail2;
1598 }
1599
1600 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1601 goto bail2;
1602
1603 if (!ADBG_EXPECT(c, 0, counter))
1604 goto bail2;
1605
1606bail2:
1607 TEEC_CloseSession(&cs[2]);
1608bail1:
1609 TEEC_CloseSession(&cs[1]);
1610bail0:
1611 TEEC_CloseSession(&cs[0]);
1612}
1613
1614static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1615 const TEEC_UUID *uuid2)
1616{
1617 uint32_t ret_orig = 0;
1618 uint32_t counter = 0;
1619 TEEC_Session cs[3] = { };
1620
1621 /* Test pre-conditions */
1622 /* 2.1 - CA opens a session toward TA1 */
1623 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1624 xtest_teec_open_session(&cs[0], uuid1, NULL,
1625 &ret_orig)))
1626 return;
1627
1628 /* 2.2 - CA opens a session toward TA2 */
1629 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1630 xtest_teec_open_session(&cs[1], uuid2, NULL,
1631 &ret_orig)))
1632 goto bail0;
1633
1634 /* 2.3 - TA1 opens a session toward TA2 */
1635 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1636 goto bail1;
1637
1638 /* 2.4 - CA invokes TA2 which panics */
1639 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1640 trigger_panic(&cs[1], NULL)))
1641 goto bail1;
1642
1643 /* Expected results */
1644 /* 2.5 - Expect CA->TA1 session is still alive */
1645 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1646 goto bail1;
1647
1648 /* 2.6 - Expect CA->TA2 session is properly released */
1649 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1650 sims_get_counter(&cs[1], &counter)))
1651 goto bail1;
1652
1653bail1:
1654 TEEC_CloseSession(&cs[1]);
1655bail0:
1656 TEEC_CloseSession(&cs[0]);
1657
1658 memset(cs, 0, sizeof(cs));
1659
1660 /* Test pre-conditions */
1661 /* 2.1 - CA opens a session toward TA1 */
1662 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1663 xtest_teec_open_session(&cs[0], uuid1, NULL,
1664 &ret_orig)))
1665 return;
1666
1667 /* 2.2 - CA opens a session toward TA2 */
1668 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1669 xtest_teec_open_session(&cs[1], uuid2, NULL,
1670 &ret_orig)))
1671 goto bail2;
1672
1673 /* 2.3 - TA1 opens a session toward TA2 */
1674 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1675 goto bail3;
1676
1677 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1678 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1679 goto bail3;
1680
1681 /* Expected results */
1682 /* 2.5 - Expect CA->TA1 session is still alive */
1683 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1684 goto bail3;
1685
1686 /* 2.6 - Expect CA->TA2 session is properly released */
1687 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1688 sims_get_counter(&cs[1], &counter)))
1689 goto bail3;
1690
1691bail3:
1692 TEEC_CloseSession(&cs[1]);
1693bail2:
1694 TEEC_CloseSession(&cs[0]);
1695}
1696
1697static void xtest_tee_test_1021(ADBG_Case_t *c)
1698{
1699 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1700 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1701 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1702
1703 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1704 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1705 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1706
1707 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1708 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1709 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1710
1711 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1712 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1713 &sims_keepalive_test_ta_uuid);
1714 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1715}
1716ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1717 "Test panic context release");