blob: d06c8482703588d5e95947b9f849da8fa4e82859 [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>
Jens Wiklanderac27ec12015-07-15 15:23:14 +020034#include <ta_concurrent.h>
Etienne Carriere50abf9a2017-03-24 11:33:50 +010035#include <sdp_basic.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010036#ifdef CFG_SECSTOR_TA_MGMT_PTA
37#include <pta_secstor_ta_mgmt.h>
38#endif
39
40#ifndef MIN
41#define MIN(a, b) ((a) < (b) ? (a) : (b))
42#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020043
Pascal Brandc639ac82015-07-02 08:53:34 +020044struct xtest_crypto_session {
45 ADBG_Case_t *c;
46 TEEC_Session *session;
47 uint32_t cmd_id_sha256;
48 uint32_t cmd_id_aes256ecb_encrypt;
49 uint32_t cmd_id_aes256ecb_decrypt;
50};
51
52static void xtest_crypto_test(struct xtest_crypto_session *cs)
53{
54 uint32_t ret_orig;
55 uint8_t crypt_out[16];
56 uint8_t crypt_in[16] = { 22, 17 };
57
58 crypt_in[15] = 60;
59
60 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
61 {
62 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
63
64 op.params[0].tmpref.buffer = crypt_in;
65 op.params[0].tmpref.size = sizeof(crypt_in);
66 op.params[1].tmpref.buffer = crypt_out;
67 op.params[1].tmpref.size = sizeof(crypt_out);
68 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
69 TEEC_MEMREF_TEMP_OUTPUT,
70 TEEC_NONE, TEEC_NONE);
71
72 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
73 TEEC_InvokeCommand(cs->session,
74 cs->
75 cmd_id_aes256ecb_encrypt,
76 &op,
77 &ret_orig));
78 }
79 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
80
81 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
82 {
83 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
84 uint8_t out[16];
85
86 op.params[0].tmpref.buffer = crypt_out;
87 op.params[0].tmpref.size = sizeof(crypt_out);
88 op.params[1].tmpref.buffer = out;
89 op.params[1].tmpref.size = sizeof(out);
90 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
91 TEEC_MEMREF_TEMP_OUTPUT,
92 TEEC_NONE, TEEC_NONE);
93
94 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
95 TEEC_InvokeCommand(cs->session,
96 cs->
97 cmd_id_aes256ecb_decrypt,
98 &op,
99 &ret_orig));
100
101 if (!ADBG_EXPECT(cs->c, 0,
102 memcmp(crypt_in, out, sizeof(crypt_in)))) {
103 Do_ADBG_Log("crypt_in:");
104 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
105 Do_ADBG_Log("out:");
106 Do_ADBG_HexLog(out, sizeof(out), 16);
107 }
108 }
109 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
110
111 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
112 {
113 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
114 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
115 static const uint8_t sha256_out[] = {
116 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
117 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
118 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
119 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
120 };
121 uint8_t out[32] = { 0 };
122
123 op.params[0].tmpref.buffer = (void *)sha256_in;
124 op.params[0].tmpref.size = sizeof(sha256_in);
125 op.params[1].tmpref.buffer = out;
126 op.params[1].tmpref.size = sizeof(out);
127 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
128 TEEC_MEMREF_TEMP_OUTPUT,
129 TEEC_NONE, TEEC_NONE);
130
131 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
132 TEEC_InvokeCommand(cs->session,
133 cs->
134 cmd_id_sha256,
135 &op,
136 &ret_orig));
137
138 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
139 sizeof(sha256_out)))) {
140 Do_ADBG_Log("sha256_out:");
141 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
142 Do_ADBG_Log("out:");
143 Do_ADBG_HexLog(out, sizeof(out), 16);
144 }
145 }
146 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
147
Etienne Carrierea3198522017-10-26 09:48:55 +0200148 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200149 {
150 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
151 static const uint8_t in[] = {
152 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
153 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
154 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
155 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
156 };
157 static const uint8_t exp_out[] = {
158 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
159 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
160 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
161 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
162 };
163 uint8_t out[sizeof(exp_out)];
164
165 op.params[0].tmpref.buffer = (void *)in;
166 op.params[0].tmpref.size = sizeof(in);
167 op.params[1].tmpref.buffer = out;
168 op.params[1].tmpref.size = sizeof(out);
169 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
170 TEEC_MEMREF_TEMP_OUTPUT,
171 TEEC_NONE, TEEC_NONE);
172
173 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
174 TEEC_InvokeCommand(cs->session,
175 cs->
176 cmd_id_aes256ecb_encrypt,
177 &op,
178 &ret_orig));
179
180 if (!ADBG_EXPECT(cs->c, 0,
181 memcmp(exp_out, out, sizeof(exp_out)))) {
182 Do_ADBG_Log("exp_out:");
183 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
184 Do_ADBG_Log("out:");
185 Do_ADBG_HexLog(out, sizeof(out), 16);
186 }
187 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200188 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200189
Etienne Carrierea3198522017-10-26 09:48:55 +0200190 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200191 {
192 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
193 static const uint8_t in[] = {
194 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
195 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
196 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
197 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
198 };
199 static const uint8_t exp_out[] = {
200 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
201 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
202 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
203 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
204 };
205 uint8_t out[sizeof(exp_out)];
206
207 op.params[0].tmpref.buffer = (void *)in;
208 op.params[0].tmpref.size = sizeof(in);
209 op.params[1].tmpref.buffer = out;
210 op.params[1].tmpref.size = sizeof(out);
211 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
212 TEEC_MEMREF_TEMP_OUTPUT,
213 TEEC_NONE, TEEC_NONE);
214
215 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
216 TEEC_InvokeCommand(cs->session,
217 cs->
218 cmd_id_aes256ecb_decrypt,
219 &op,
220 &ret_orig));
221
222 if (!ADBG_EXPECT(cs->c, 0,
223 memcmp(exp_out, out, sizeof(exp_out)))) {
224 Do_ADBG_Log("exp_out:");
225 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
226 Do_ADBG_Log("out:");
227 Do_ADBG_HexLog(out, sizeof(out), 16);
228 }
229 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200230 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200231}
232
233static void xtest_tee_test_1001(ADBG_Case_t *c)
234{
Jens Wiklandercf16e842016-02-10 09:07:09 +0100235 TEEC_Result res;
236 TEEC_Session session = { 0 };
237 uint32_t ret_orig;
Pascal Brandc639ac82015-07-02 08:53:34 +0200238
Etienne Carriere11093162017-10-26 09:49:04 +0200239 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100240 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100241 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200242 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
243 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100244 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200245 }
246 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Pascal Brandc639ac82015-07-02 08:53:34 +0200247
Jens Wiklandercf16e842016-02-10 09:07:09 +0100248 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100249 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Jens Wiklandercf16e842016-02-10 09:07:09 +0100250 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200251}
Jens Wiklander14f48872018-06-29 15:30:13 +0200252ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200253
Jens Wiklander1d70a112017-10-16 15:16:39 +0200254static void xtest_tee_test_1002(ADBG_Case_t *c)
255{
256 TEEC_Result res;
257 TEEC_Session session = { 0 };
258 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
259 uint32_t ret_orig;
260 uint8_t buf[16 * 1024];
261 uint8_t exp_sum = 0;
262 size_t n;
263
Etienne Carriere11093162017-10-26 09:49:04 +0200264 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200265 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
266 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200267 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
268 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200269 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200270 }
271 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200272
273 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
274 TEEC_NONE, TEEC_NONE);
275 op.params[0].tmpref.size = sizeof(buf);
276 op.params[0].tmpref.buffer = buf;
277
278 for (n = 0; n < sizeof(buf); n++)
279 buf[n] = n + 1;
280 for (n = 0; n < sizeof(buf); n++)
281 exp_sum += buf[n];
282
283 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
284 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
285 goto out;
286
287 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
288out:
289 TEEC_CloseSession(&session);
290}
Jens Wiklander14f48872018-06-29 15:30:13 +0200291ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200292
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100293struct test_1003_arg {
294 uint32_t test_type;
295 size_t repeat;
296 size_t max_before_lockers;
297 size_t max_during_lockers;
298 size_t before_lockers;
299 size_t during_lockers;
300 TEEC_Result res;
301 uint32_t error_orig;
302};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200303
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100304static void *test_1003_thread(void *arg)
305{
306 struct test_1003_arg *a = arg;
307 TEEC_Session session = { 0 };
308 size_t rounds = 64 * 1024;
309 size_t n;
310
311 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
312 NULL, &a->error_orig);
313 if (a->res != TEEC_SUCCESS)
314 return NULL;
315
316 for (n = 0; n < a->repeat; n++) {
317 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
318
319 op.params[0].value.a = a->test_type;
320 op.params[0].value.b = rounds;
321
322 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
323 TEEC_VALUE_OUTPUT,
324 TEEC_NONE, TEEC_NONE);
325 a->res = TEEC_InvokeCommand(&session,
326 PTA_INVOKE_TESTS_CMD_MUTEX,
327 &op, &a->error_orig);
328 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
329 op.params[1].value.b != 1) {
330 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
331 a->res = TEEC_ERROR_BAD_STATE;
332 a->error_orig = 42;
333 break;
334 }
335
336 if (a->test_type == PTA_MUTEX_TEST_READER) {
337 if (op.params[1].value.a > a->max_before_lockers)
338 a->max_before_lockers = op.params[1].value.a;
339
340 if (op.params[1].value.b > a->max_during_lockers)
341 a->max_during_lockers = op.params[1].value.b;
342
343 a->before_lockers += op.params[1].value.a;
344 a->during_lockers += op.params[1].value.b;
345 }
346 }
347 TEEC_CloseSession(&session);
348
349 return NULL;
350}
351
352static void xtest_tee_test_1003(ADBG_Case_t *c)
353{
354 size_t num_threads = 3 * 2;
355 TEEC_Result res;
356 TEEC_Session session = { 0 };
357 uint32_t ret_orig;
358 size_t repeat = 20;
359 pthread_t thr[num_threads];
360 struct test_1003_arg arg[num_threads];
361 size_t max_read_concurrency = 0;
362 size_t max_read_waiters = 0;
363 size_t num_concurrent_read_lockers = 0;
364 size_t num_concurrent_read_waiters = 0;
365 size_t n;
366 size_t nt = num_threads;
367 double mean_read_concurrency;
368 double mean_read_waiters;
369 size_t num_writers = 0;
370 size_t num_readers = 0;
371
372 /* Pseudo TA is optional: warn and nicely exit if not found */
373 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
374 &ret_orig);
375 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
376 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
377 return;
378 }
379 ADBG_EXPECT_TEEC_SUCCESS(c, res);
380 TEEC_CloseSession(&session);
381
382 memset(arg, 0, sizeof(arg));
383
384 for (n = 0; n < nt; n++) {
385 if (n % 3) {
386 arg[n].test_type = PTA_MUTEX_TEST_READER;
387 num_readers++;
388 } else {
389 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
390 num_writers++;
391 }
392 arg[n].repeat = repeat;
393 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
394 test_1003_thread, arg + n)))
395 nt = n; /* break loop and start cleanup */
396 }
397
398 for (n = 0; n < nt; n++) {
399 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
400 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
401 Do_ADBG_Log("error origin %" PRIu32,
402 arg[n].error_orig);
403 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
404 if (arg[n].max_during_lockers > max_read_concurrency)
405 max_read_concurrency =
406 arg[n].max_during_lockers;
407
408 if (arg[n].max_before_lockers > max_read_waiters)
409 max_read_waiters = arg[n].max_before_lockers;
410
411 num_concurrent_read_lockers += arg[n].during_lockers;
412 num_concurrent_read_waiters += arg[n].before_lockers;
413 }
414 }
415
416 mean_read_concurrency = (double)num_concurrent_read_lockers /
417 (double)(repeat * num_readers);
418 mean_read_waiters = (double)num_concurrent_read_waiters /
419 (double)(repeat * num_readers);
420
421 Do_ADBG_Log(" Number of parallel threads: %zu (%zu writers and %zu readers)",
422 num_threads, num_writers, num_readers);
423 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
424 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
425 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
426 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
427}
Jens Wiklander14f48872018-06-29 15:30:13 +0200428ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
429 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200430
Pascal Brandc639ac82015-07-02 08:53:34 +0200431static void xtest_tee_test_1004(ADBG_Case_t *c)
432{
433 TEEC_Session session = { 0 };
434 uint32_t ret_orig;
435 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
436 TA_CRYPT_CMD_AES256ECB_ENC,
437 TA_CRYPT_CMD_AES256ECB_DEC };
438
439 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
440 &session, &crypt_user_ta_uuid,
441 NULL, &ret_orig)))
442 return;
443
444 /* Run the "complete crypto test suite" */
445 xtest_crypto_test(&cs);
446
447 TEEC_CloseSession(&session);
448}
Jens Wiklander14f48872018-06-29 15:30:13 +0200449ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200450
Etienne Carriere92c34422018-02-09 13:11:40 +0100451static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200452{
453 TEEC_Session session = { 0 };
454 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
455 uint32_t ret_orig;
456
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300457 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200458 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300459 &ret_orig)))
460 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200461
462 op.params[0].value.a = n;
463 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
464 TEEC_NONE);
465
466 (void)ADBG_EXPECT_TEEC_RESULT(c,
467 TEEC_ERROR_TARGET_DEAD,
468 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
469 &ret_orig));
470
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300471 (void)ADBG_EXPECT_TEEC_RESULT(c,
472 TEEC_ERROR_TARGET_DEAD,
473 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200474 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300475
Pascal Brandc639ac82015-07-02 08:53:34 +0200476 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
477
478 TEEC_CloseSession(&session);
479}
480
Etienne Carriere92c34422018-02-09 13:11:40 +0100481static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
482 size_t size)
483{
484 TEEC_Session session = { 0 };
485 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
486 uint32_t ret_orig;
487 TEEC_SharedMemory shm;
488
489 memset(&shm, 0, sizeof(shm));
490 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{
524 uint32_t ret_orig;
525#define MAX_SESSIONS 3
526 TEEC_Session sessions[MAX_SESSIONS];
527 int i;
528
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{
544 TEEC_Session session = { 0 };
545 uint32_t ret_orig;
546 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
547 uint8_t buf[32];
548
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{
570 TEEC_Session session = { 0 };
571 uint32_t ret_orig;
572
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{
Jens Wiklanderec545fb2017-11-24 16:58:07 +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{
622 TEEC_Session session = { 0 };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100623 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
624 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200625 TEEC_Result res;
626 uint32_t ret_orig;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100627 FILE *f = NULL;
628 bool r = false;
629 uint8_t *buf = NULL;
630 size_t sz;
631 size_t fread_res;
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{
677 TEEC_Session session = { 0 };
678 TEEC_Session session_crypt = { 0 };
679 uint32_t ret_orig;
680
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 {
Jens Wiklanderb7940892015-10-23 16:02:40 +0200721 size_t n;
722
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 */
759 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 30000, 1)); /* payload */
760 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{
780 TEEC_Session session = { 0 };
781 uint32_t ret_orig;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300782 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200783
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300784 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200785 {
786 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
787
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300788 if (ADBG_EXPECT_TEEC_SUCCESS(c,
789 xtest_teec_open_session(&session, &os_test_ta_uuid,
790 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200791
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300792 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
793 TEEC_ORIGIN_TRUSTED_APP,
794 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200795
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300796 op.params[0].value.a = timeout;
797 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
798 TEEC_NONE,
799 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300800 if (cancel) {
801 (void)ADBG_EXPECT(c, 0,
802 pthread_create(&thr, NULL,
803 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200804
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300805 (void)ADBG_EXPECT_TEEC_RESULT(c,
806 TEEC_ERROR_CANCEL,
807 TEEC_InvokeCommand(&session,
808 TA_OS_TEST_CMD_WAIT,
809 &op,
810 &ret_orig));
811 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300812
813 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
814 TEEC_InvokeCommand(&session,
815 TA_OS_TEST_CMD_WAIT,
816 &op,
817 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300818 if (cancel)
819 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300820
821 TEEC_CloseSession(&session);
822 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200823 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300824 Do_ADBG_EndSubCase(c, "%s", subcase);
825}
826
827static void xtest_tee_test_1009(ADBG_Case_t *c)
828{
829 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
830 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300831 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300832 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200833}
Jens Wiklander14f48872018-06-29 15:30:13 +0200834ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200835
836static void xtest_tee_test_1010(ADBG_Case_t *c)
837{
Etienne Carriere92c34422018-02-09 13:11:40 +0100838 unsigned int n;
839 unsigned int idx;
840 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200841
842 for (n = 1; n <= 5; n++) {
843 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
844 xtest_tee_test_invalid_mem_access(c, n);
845 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
846 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100847
848 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
849 for (n = 1; n <= 5; n++) {
850 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200851 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100852 n, memref_sz[idx]);
853 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
854 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200855 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100856 n, memref_sz[idx]);
857 }
858 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200859}
Jens Wiklander14f48872018-06-29 15:30:13 +0200860ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
861 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200862
863static void xtest_tee_test_1011(ADBG_Case_t *c)
864{
865 TEEC_Session session = { 0 };
866 uint32_t ret_orig;
867 struct xtest_crypto_session cs = {
868 c, &session, TA_RPC_CMD_CRYPT_SHA256,
869 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
870 TA_RPC_CMD_CRYPT_AES256ECB_DEC
871 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100872 struct xtest_crypto_session cs_privmem = {
873 c, &session,
874 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
875 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
876 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
877 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200878 TEEC_UUID uuid = rpc_test_ta_uuid;
879
880 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
881 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
882 return;
883
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100884 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200885 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100886 * Run the "complete crypto test suite" using TA-to-TA
887 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200888 */
889 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100890 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
891
892 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
893 /*
894 * Run the "complete crypto test suite" using TA-to-TA
895 * communication via TA private memory.
896 */
897 xtest_crypto_test(&cs_privmem);
898 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
899
Pascal Brandc639ac82015-07-02 08:53:34 +0200900 TEEC_CloseSession(&session);
901}
Jens Wiklander14f48872018-06-29 15:30:13 +0200902ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
903 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200904
905/*
906 * Note that this test is failing when
907 * - running twice in a raw
908 * - and the user TA is statically linked
909 * This is because the counter is not reseted when opening the first session
910 * in case the TA is statically linked
911 */
912static void xtest_tee_test_1012(ADBG_Case_t *c)
913{
914 TEEC_Session session1 = { 0 };
915 TEEC_Session session2 = { 0 };
916 uint32_t ret_orig;
917 TEEC_UUID uuid = sims_test_ta_uuid;
918
919 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
920 {
921 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
922 static const uint8_t in[] = {
923 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
924 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
925 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
926 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
927 };
928 uint8_t out[32] = { 0 };
929 int i;
930
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300931 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200932 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300933 &ret_orig)))
934 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200935
936 op.params[0].value.a = 0;
937 op.params[1].tmpref.buffer = (void *)in;
938 op.params[1].tmpref.size = sizeof(in);
939 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
940 TEEC_MEMREF_TEMP_INPUT,
941 TEEC_NONE, TEEC_NONE);
942
943 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
944 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
945 &ret_orig));
946
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100947 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300948 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200949 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300950 &ret_orig)))
951 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200952
953 op.params[0].value.a = 0;
954 op.params[1].tmpref.buffer = out;
955 op.params[1].tmpref.size = sizeof(out);
956 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
957 TEEC_MEMREF_TEMP_OUTPUT,
958 TEEC_NONE, TEEC_NONE);
959
960 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
961 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
962 &op, &ret_orig));
963
964 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
965 sizeof(out))) {
966 Do_ADBG_Log("in:");
967 Do_ADBG_HexLog(in, sizeof(in), 16);
968 Do_ADBG_Log("out:");
969 Do_ADBG_HexLog(out, sizeof(out), 16);
970 }
971
972 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
973 TEEC_NONE, TEEC_NONE,
974 TEEC_NONE);
975
976 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
977 TEEC_InvokeCommand(&session1,
978 TA_SIMS_CMD_GET_COUNTER,
979 &op, &ret_orig));
980
981 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
982
983 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
984 TEEC_InvokeCommand(&session2,
985 TA_SIMS_CMD_GET_COUNTER, &op,
986 &ret_orig));
987
988 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
989 TEEC_CloseSession(&session2);
990 }
991
992 memset(out, 0, sizeof(out));
993 op.params[0].value.a = 0;
994 op.params[1].tmpref.buffer = out;
995 op.params[1].tmpref.size = sizeof(out);
996 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
997 TEEC_MEMREF_TEMP_OUTPUT,
998 TEEC_NONE, TEEC_NONE);
999
1000 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1001 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1002 &ret_orig));
1003
1004 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1005 Do_ADBG_Log("in:");
1006 Do_ADBG_HexLog(in, sizeof(in), 16);
1007 Do_ADBG_Log("out:");
1008 Do_ADBG_HexLog(out, sizeof(out), 16);
1009 }
1010
1011 TEEC_CloseSession(&session1);
1012 }
1013}
Jens Wiklander14f48872018-06-29 15:30:13 +02001014ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1015 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001016
1017struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001018 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001019 uint32_t cmd;
1020 uint32_t repeat;
1021 TEEC_SharedMemory *shm;
1022 uint32_t error_orig;
1023 TEEC_Result res;
1024 uint32_t max_concurrency;
1025 const uint8_t *in;
1026 size_t in_len;
1027 uint8_t *out;
1028 size_t out_len;
1029};
1030
1031static void *test_1013_thread(void *arg)
1032{
1033 struct test_1013_thread_arg *a = arg;
1034 TEEC_Session session = { 0 };
1035 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1036 uint8_t p2 = TEEC_NONE;
1037 uint8_t p3 = TEEC_NONE;
1038
Jens Wiklander70672972016-04-06 00:01:45 +02001039 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001040 &a->error_orig);
1041 if (a->res != TEEC_SUCCESS)
1042 return NULL;
1043
1044 op.params[0].memref.parent = a->shm;
1045 op.params[0].memref.size = a->shm->size;
1046 op.params[0].memref.offset = 0;
1047 op.params[1].value.a = a->repeat;
1048 op.params[1].value.b = 0;
1049 op.params[2].tmpref.buffer = (void *)a->in;
1050 op.params[2].tmpref.size = a->in_len;
1051 op.params[3].tmpref.buffer = a->out;
1052 op.params[3].tmpref.size = a->out_len;
1053
1054 if (a->in_len)
1055 p2 = TEEC_MEMREF_TEMP_INPUT;
1056 if (a->out_len)
1057 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1058
1059 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1060 TEEC_VALUE_INOUT, p2, p3);
1061
1062 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1063 a->max_concurrency = op.params[1].value.b;
1064 a->out_len = op.params[3].tmpref.size;
1065 TEEC_CloseSession(&session);
1066 return NULL;
1067}
1068
Pascal Brand4fa35582015-12-17 10:59:12 +01001069#define NUM_THREADS 3
1070
Jens Wiklander70672972016-04-06 00:01:45 +02001071static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1072 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001073{
Pascal Brand4fa35582015-12-17 10:59:12 +01001074 size_t num_threads = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001075 size_t nt;
1076 size_t n;
Jens Wiklander70672972016-04-06 00:01:45 +02001077 size_t repeat = 1000;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001078 pthread_t thr[num_threads];
1079 TEEC_SharedMemory shm;
1080 size_t max_concurrency;
1081 struct test_1013_thread_arg arg[num_threads];
1082 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1083 static const uint8_t sha256_out[] = {
1084 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1085 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1086 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1087 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1088 };
1089 uint8_t out[32] = { 0 };
1090
Jens Wiklander70672972016-04-06 00:01:45 +02001091 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001092 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001093
1094 memset(&shm, 0, sizeof(shm));
1095 shm.size = sizeof(struct ta_concurrent_shm);
1096 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1097 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1098 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1099 return;
1100
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001101 memset(shm.buffer, 0, shm.size);
1102 memset(arg, 0, sizeof(arg));
1103 max_concurrency = 0;
1104 nt = num_threads;
1105
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);
1130 (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;
1138 nt = num_threads;
1139
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{
1171 int i;
1172 double mean_concurrency;
1173 double concurrency;
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;
1219 int test;
1220 int ret;
1221
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{
1252 TEEC_Result res;
1253 TEEC_Session session = { 0 };
1254 uint32_t ret_orig;
1255
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{
1275 TEEC_Session session = { 0 };
1276 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1277 uint32_t ret_orig;
1278
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{
1298 TEEC_Session session = { 0 };
1299 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1300 uint32_t ret_orig;
1301 TEEC_SharedMemory shm;
1302 size_t page_size = 4096;
1303
1304 memset(&shm, 0, sizeof(shm));
1305 shm.size = 8 * page_size;
1306 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1307 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1308 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1309 return;
1310
1311 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1312 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1313 &ret_orig)))
1314 goto out;
1315
1316 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1317 TEEC_MEMREF_PARTIAL_INPUT,
1318 TEEC_MEMREF_PARTIAL_OUTPUT,
1319 TEEC_MEMREF_PARTIAL_OUTPUT);
1320
1321 /*
1322 * The first two memrefs are supposed to be combined into in
1323 * region and the last two memrefs should have one region each
1324 * when the parameters are mapped for the TA.
1325 */
1326 op.params[0].memref.parent = &shm;
1327 op.params[0].memref.size = page_size;
1328 op.params[0].memref.offset = 0;
1329
1330 op.params[1].memref.parent = &shm;
1331 op.params[1].memref.size = page_size;
1332 op.params[1].memref.offset = page_size;
1333
1334 op.params[2].memref.parent = &shm;
1335 op.params[2].memref.size = page_size;
1336 op.params[2].memref.offset = 4 * page_size;
1337
1338 op.params[3].memref.parent = &shm;
1339 op.params[3].memref.size = 2 * page_size;
1340 op.params[3].memref.offset = 6 * page_size;
1341
1342 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1343 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1344 &ret_orig));
1345
1346 TEEC_CloseSession(&session);
1347out:
1348 TEEC_ReleaseSharedMemory(&shm);
1349}
Jens Wiklander14f48872018-06-29 15:30:13 +02001350ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1351 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001352
1353static void xtest_tee_test_1018(ADBG_Case_t *c)
1354{
1355 TEEC_Session session = { 0 };
1356 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1357 uint32_t ret_orig;
1358 TEEC_SharedMemory shm;
1359 size_t page_size = 4096;
1360
1361 memset(&shm, 0, sizeof(shm));
1362 shm.size = 8 * page_size;
1363 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1364 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1365 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1366 return;
1367
1368 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1369 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1370 &ret_orig)))
1371 goto out;
1372
1373 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1374 TEEC_MEMREF_PARTIAL_INPUT,
1375 TEEC_MEMREF_PARTIAL_OUTPUT,
1376 TEEC_MEMREF_PARTIAL_OUTPUT);
1377
1378 /*
1379 * The first two memrefs are supposed to be combined into in
1380 * region and the last two memrefs should have one region each
1381 * when the parameters are mapped for the TA.
1382 */
1383 op.params[0].memref.parent = &shm;
1384 op.params[0].memref.size = page_size;
1385 op.params[0].memref.offset = 0;
1386
1387 op.params[1].memref.parent = &shm;
1388 op.params[1].memref.size = page_size;
1389 op.params[1].memref.offset = page_size;
1390
1391 op.params[2].memref.parent = &shm;
1392 op.params[2].memref.size = page_size;
1393 op.params[2].memref.offset = 4 * page_size;
1394
1395 op.params[3].memref.parent = &shm;
1396 op.params[3].memref.size = 3 * page_size;
1397 op.params[3].memref.offset = 6 * page_size;
1398
1399 /*
1400 * Depending on the tee driver we may have different error codes.
1401 * What's most important is that secure world doesn't panic and
1402 * that someone detects an error.
1403 */
1404 ADBG_EXPECT_NOT(c, TEE_SUCCESS,
1405 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1406 &ret_orig));
1407
1408 TEEC_CloseSession(&session);
1409out:
1410 TEEC_ReleaseSharedMemory(&shm);
1411}
Jens Wiklander14f48872018-06-29 15:30:13 +02001412ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1413 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001414
Victor Chong3ff36f52018-06-07 04:37:00 +01001415#if defined(CFG_TA_DYNLINK)
Jerome Forissier53bde722018-05-31 09:14:54 +02001416static void xtest_tee_test_1019(ADBG_Case_t *c)
1417{
1418 TEEC_Session session = { 0 };
1419 uint32_t ret_orig;
1420
1421 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1422 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1423 &ret_orig)))
1424 return;
1425
1426 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1427 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1428 &ret_orig));
1429
1430 (void)ADBG_EXPECT_TEEC_RESULT(c,
1431 TEEC_ERROR_TARGET_DEAD,
1432 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1433 NULL, &ret_orig));
1434
1435 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1436
1437 TEEC_CloseSession(&session);
1438}
Jens Wiklander14f48872018-06-29 15:30:13 +02001439ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1440 "Test dynamically linked TA");
1441#endif /*CFG_TA_DYNLINK*/