blob: 86f69c3178c50b893c8a3946c0e7dcaf11ff48f1 [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{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010054 uint32_t ret_orig = 0;
55 uint8_t crypt_out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020056 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;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010084 uint8_t out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020085
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 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100121 uint8_t out[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200122
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 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100163 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200164
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 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100205 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200206
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{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100235 TEEC_Result res = TEEC_ERROR_GENERIC;
236 TEEC_Session session = { };
237 uint32_t ret_orig = 0;
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{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100256 TEEC_Result res = TEEC_ERROR_GENERIC;
257 TEEC_Session session = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200258 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100259 uint32_t ret_orig = 0;
260 uint8_t buf[16 * 1024] = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200261 uint8_t exp_sum = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100262 size_t n = 0;
Jens Wiklander1d70a112017-10-16 15:16:39 +0200263
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;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100307 TEEC_Session session = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100308 size_t rounds = 64 * 1024;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100309 size_t n = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100310
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
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100352#define TEST_1003_THREAD_COUNT (3 * 2)
353
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100354static void xtest_tee_test_1003(ADBG_Case_t *c)
355{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100356 TEEC_Result res = TEEC_ERROR_GENERIC;
357 TEEC_Session session = { };
358 uint32_t ret_orig = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100359 size_t repeat = 20;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100360 struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100361 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;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100365 size_t n = 0;
366 size_t nt = TEST_1003_THREAD_COUNT;
367 double mean_read_concurrency = 0;
368 double mean_read_waiters = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100369 size_t num_writers = 0;
370 size_t num_readers = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100371 pthread_t thr[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100372
373 /* Pseudo TA is optional: warn and nicely exit if not found */
374 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
375 &ret_orig);
376 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
377 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
378 return;
379 }
380 ADBG_EXPECT_TEEC_SUCCESS(c, res);
381 TEEC_CloseSession(&session);
382
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100383 for (n = 0; n < nt; n++) {
384 if (n % 3) {
385 arg[n].test_type = PTA_MUTEX_TEST_READER;
386 num_readers++;
387 } else {
388 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
389 num_writers++;
390 }
391 arg[n].repeat = repeat;
392 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
393 test_1003_thread, arg + n)))
394 nt = n; /* break loop and start cleanup */
395 }
396
397 for (n = 0; n < nt; n++) {
398 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
399 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
400 Do_ADBG_Log("error origin %" PRIu32,
401 arg[n].error_orig);
402 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
403 if (arg[n].max_during_lockers > max_read_concurrency)
404 max_read_concurrency =
405 arg[n].max_during_lockers;
406
407 if (arg[n].max_before_lockers > max_read_waiters)
408 max_read_waiters = arg[n].max_before_lockers;
409
410 num_concurrent_read_lockers += arg[n].during_lockers;
411 num_concurrent_read_waiters += arg[n].before_lockers;
412 }
413 }
414
415 mean_read_concurrency = (double)num_concurrent_read_lockers /
416 (double)(repeat * num_readers);
417 mean_read_waiters = (double)num_concurrent_read_waiters /
418 (double)(repeat * num_readers);
419
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100420 Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)",
421 TEST_1003_THREAD_COUNT, num_writers, num_readers);
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100422 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
423 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
424 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
425 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
426}
Jens Wiklander14f48872018-06-29 15:30:13 +0200427ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
428 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200429
Pascal Brandc639ac82015-07-02 08:53:34 +0200430static void xtest_tee_test_1004(ADBG_Case_t *c)
431{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100432 TEEC_Session session = { };
433 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200434 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
435 TA_CRYPT_CMD_AES256ECB_ENC,
436 TA_CRYPT_CMD_AES256ECB_DEC };
437
438 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
439 &session, &crypt_user_ta_uuid,
440 NULL, &ret_orig)))
441 return;
442
443 /* Run the "complete crypto test suite" */
444 xtest_crypto_test(&cs);
445
446 TEEC_CloseSession(&session);
447}
Jens Wiklander14f48872018-06-29 15:30:13 +0200448ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200449
Etienne Carriere92c34422018-02-09 13:11:40 +0100450static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200451{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100452 TEEC_Session session = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200453 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100454 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200455
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300456 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200457 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300458 &ret_orig)))
459 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200460
461 op.params[0].value.a = n;
462 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
463 TEEC_NONE);
464
465 (void)ADBG_EXPECT_TEEC_RESULT(c,
466 TEEC_ERROR_TARGET_DEAD,
467 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
468 &ret_orig));
469
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300470 (void)ADBG_EXPECT_TEEC_RESULT(c,
471 TEEC_ERROR_TARGET_DEAD,
472 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200473 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300474
Pascal Brandc639ac82015-07-02 08:53:34 +0200475 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
476
477 TEEC_CloseSession(&session);
478}
479
Etienne Carriere92c34422018-02-09 13:11:40 +0100480static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
481 size_t size)
482{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100483 TEEC_Session session = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100484 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100485 uint32_t ret_orig = 0;
486 TEEC_SharedMemory shm = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100487
Etienne Carriere92c34422018-02-09 13:11:40 +0100488 shm.size = size;
489 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
490 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
491 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
492 return;
493
494 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
495 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
496 &ret_orig)))
497 return;
498
499 op.params[0].value.a = (uint32_t)n;
500 op.params[1].memref.parent = &shm;
501 op.params[1].memref.size = size;
502 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
503 TEEC_NONE, TEEC_NONE);
504
505 (void)ADBG_EXPECT_TEEC_RESULT(c,
506 TEEC_ERROR_TARGET_DEAD,
507 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
508 &ret_orig));
509
510 (void)ADBG_EXPECT_TEEC_RESULT(c,
511 TEEC_ERROR_TARGET_DEAD,
512 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
513 &ret_orig));
514
515 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
516
517 TEEC_CloseSession(&session);
518}
519
Pascal Brandc639ac82015-07-02 08:53:34 +0200520static void xtest_tee_test_1005(ADBG_Case_t *c)
521{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100522 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200523#define MAX_SESSIONS 3
524 TEEC_Session sessions[MAX_SESSIONS];
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100525 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200526
527 for (i = 0; i < MAX_SESSIONS; i++) {
528 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200529 xtest_teec_open_session(&sessions[i],
530 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200531 NULL, &ret_orig)))
532 break;
533 }
534
535 for (; --i >= 0; )
536 TEEC_CloseSession(&sessions[i]);
537}
Jens Wiklander14f48872018-06-29 15:30:13 +0200538ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
Pascal Brandc639ac82015-07-02 08:53:34 +0200539
540static void xtest_tee_test_1006(ADBG_Case_t *c)
541{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100542 TEEC_Session session = { };
543 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200544 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100545 uint8_t buf[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200546
547 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
548 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
549 &ret_orig)))
550 return;
551
552 op.params[0].tmpref.buffer = buf;
553 op.params[0].tmpref.size = sizeof(buf);
554 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
555 TEEC_NONE, TEEC_NONE);
556
557 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
558 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
559 &ret_orig));
560
561 TEEC_CloseSession(&session);
562}
Jens Wiklander14f48872018-06-29 15:30:13 +0200563ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
564 "Test Basic OS features");
Pascal Brandc639ac82015-07-02 08:53:34 +0200565
566static void xtest_tee_test_1007(ADBG_Case_t *c)
567{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100568 TEEC_Session session = { };
569 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200570
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300571 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200572 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300573 &ret_orig)))
574 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200575
576 (void)ADBG_EXPECT_TEEC_RESULT(c,
577 TEEC_ERROR_TARGET_DEAD,
578 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
579 &ret_orig));
580
581 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
582
583 (void)ADBG_EXPECT_TEEC_RESULT(c,
584 TEEC_ERROR_TARGET_DEAD,
585 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
586 &ret_orig));
587
588 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
589
590 TEEC_CloseSession(&session);
591}
Jens Wiklander14f48872018-06-29 15:30:13 +0200592ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
Pascal Brandc639ac82015-07-02 08:53:34 +0200593
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100594#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jerome Forissierf02a2212015-10-29 14:33:35 +0100595#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000596# ifdef __ANDROID__
Yongqin Liu286f1fc2018-06-21 22:09:15 +0800597#define TA_DIR "/vendor/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000598# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100599#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000600# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100601#endif
602
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100603static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600604{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100605 char buf[PATH_MAX] = { };
David Brownb2865ab2016-08-02 11:44:41 -0600606
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100607 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100608 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100609 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200610 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
611 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
612 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600613 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200614
Jens Wiklanderb7940892015-10-23 16:02:40 +0200615 return fopen(buf, mode);
616}
617
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100618static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200619{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100620 TEEC_Session session = { };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100621 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
622 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100623 TEEC_Result res = TEEC_ERROR_GENERIC;
624 uint32_t ret_orig = 0;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100625 FILE *f = NULL;
626 bool r = false;
627 uint8_t *buf = NULL;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100628 size_t sz = 0;
629 size_t fread_res = 0;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200630
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100631 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
632 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
633 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200634
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100635 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
636 if (!ADBG_EXPECT_NOT_NULL(c, f))
637 goto out;
638 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
639 goto out;
640 sz = ftell(f);
641 rewind(f);
642
643 buf = malloc(sz);
644 if (!ADBG_EXPECT_NOT_NULL(c, buf))
645 goto out;
646
647 fread_res = fread(buf, 1, sz, f);
648 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
649 goto out;
650
Jens Wiklander4441fe22015-10-23 16:53:02 +0200651 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100652 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200653
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100654 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200655
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100656 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
657 TEEC_NONE, TEEC_NONE);
658 op.params[0].tmpref.buffer = buf;
659 op.params[0].tmpref.size = sz;
660
661 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
662 &ret_orig);
663 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
664out:
665 free(buf);
666 if (f)
667 fclose(f);
668 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200669 return r;
670}
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100671#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Jens Wiklander4441fe22015-10-23 16:53:02 +0200672
Pascal Brandc639ac82015-07-02 08:53:34 +0200673static void xtest_tee_test_1008(ADBG_Case_t *c)
674{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100675 TEEC_Session session = { };
676 TEEC_Session session_crypt = { };
677 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200678
679 Do_ADBG_BeginSubCase(c, "Invoke command");
680 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300681 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200682 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300683 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200684
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300685 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
686 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
687 NULL, &ret_orig));
688 TEEC_CloseSession(&session);
689 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200690
Pascal Brandc639ac82015-07-02 08:53:34 +0200691 }
692 Do_ADBG_EndSubCase(c, "Invoke command");
693
694 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
695 {
696 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
697
698 op.params[0].value.a = 2000;
699 op.paramTypes = TEEC_PARAM_TYPES(
700 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
701
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300702 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200703 xtest_teec_open_session(&session,
704 &os_test_ta_uuid,
705 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300706 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200707
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300708 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
709 TEEC_InvokeCommand(&session,
710 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
711 &op, &ret_orig));
712 TEEC_CloseSession(&session);
713 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200714 }
715 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
716
717 Do_ADBG_BeginSubCase(c, "Create session fail");
718 {
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100719 size_t n = 0;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200720
Pascal Brandc639ac82015-07-02 08:53:34 +0200721 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
722 xtest_teec_open_session(&session_crypt,
723 &create_fail_test_ta_uuid, NULL,
724 &ret_orig));
Pascal Brandc639ac82015-07-02 08:53:34 +0200725 /*
726 * Run this several times to see that there's no memory leakage.
727 */
728 for (n = 0; n < 100; n++) {
729 Do_ADBG_Log("n = %zu", n);
730 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
731 xtest_teec_open_session(&session_crypt,
732 &create_fail_test_ta_uuid,
733 NULL, &ret_orig));
734 }
735 }
736 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200737
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100738#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jens Wiklander4441fe22015-10-23 16:53:02 +0200739 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
740 ADBG_EXPECT_TRUE(c,
741 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
742 ADBG_EXPECT_TRUE(c,
743 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
744 ADBG_EXPECT_TRUE(c,
745 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
746 ADBG_EXPECT_TRUE(c,
747 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
748 ADBG_EXPECT_TRUE(c,
749 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
750 ADBG_EXPECT_TRUE(c,
751 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
752 ADBG_EXPECT_TRUE(c,
753 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
754 ADBG_EXPECT_TRUE(c,
755 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
756 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
Jerome Forissier895c5ca2019-02-28 17:27:46 +0100757 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
Jens Wiklander4441fe22015-10-23 16:53:02 +0200758 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100759#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Pascal Brandc639ac82015-07-02 08:53:34 +0200760}
Jens Wiklander14f48872018-06-29 15:30:13 +0200761ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
762 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200763
Pascal Brandc639ac82015-07-02 08:53:34 +0200764static void *cancellation_thread(void *arg)
765{
766 /*
767 * Sleep 0.5 seconds before cancellation to make sure that the other
768 * thread is in RPC_WAIT.
769 */
770 (void)usleep(500000);
771 TEEC_RequestCancellation(arg);
772 return NULL;
773}
Pascal Brandc639ac82015-07-02 08:53:34 +0200774
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300775static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
776 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200777{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100778 TEEC_Session session = { };
779 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300780 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200781
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100782 memset(&thr, 0, sizeof(thr));
783
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 Carrieree4ec9e42019-03-28 13:30:21 +0100838 unsigned int n = 0;
839 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100840 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{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100865 TEEC_Session session = { };
866 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200867 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{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100914 TEEC_Session session1 = { };
915 TEEC_Session session2 = { };
916 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200917 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 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100928 uint8_t out[32] = { };
929 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200930
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 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001013 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001014}
Jens Wiklander14f48872018-06-29 15:30:13 +02001015ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1016 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001017
1018struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001019 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001020 uint32_t cmd;
1021 uint32_t repeat;
1022 TEEC_SharedMemory *shm;
1023 uint32_t error_orig;
1024 TEEC_Result res;
1025 uint32_t max_concurrency;
1026 const uint8_t *in;
1027 size_t in_len;
1028 uint8_t *out;
1029 size_t out_len;
1030};
1031
1032static void *test_1013_thread(void *arg)
1033{
1034 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001035 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001036 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1037 uint8_t p2 = TEEC_NONE;
1038 uint8_t p3 = TEEC_NONE;
1039
Jens Wiklander70672972016-04-06 00:01:45 +02001040 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001041 &a->error_orig);
1042 if (a->res != TEEC_SUCCESS)
1043 return NULL;
1044
1045 op.params[0].memref.parent = a->shm;
1046 op.params[0].memref.size = a->shm->size;
1047 op.params[0].memref.offset = 0;
1048 op.params[1].value.a = a->repeat;
1049 op.params[1].value.b = 0;
1050 op.params[2].tmpref.buffer = (void *)a->in;
1051 op.params[2].tmpref.size = a->in_len;
1052 op.params[3].tmpref.buffer = a->out;
1053 op.params[3].tmpref.size = a->out_len;
1054
1055 if (a->in_len)
1056 p2 = TEEC_MEMREF_TEMP_INPUT;
1057 if (a->out_len)
1058 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1059
1060 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1061 TEEC_VALUE_INOUT, p2, p3);
1062
1063 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1064 a->max_concurrency = op.params[1].value.b;
1065 a->out_len = op.params[3].tmpref.size;
1066 TEEC_CloseSession(&session);
1067 return NULL;
1068}
1069
Pascal Brand4fa35582015-12-17 10:59:12 +01001070#define NUM_THREADS 3
1071
Jens Wiklander70672972016-04-06 00:01:45 +02001072static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1073 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001074{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001075 size_t nt = 0;
1076 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001077 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001078 TEEC_SharedMemory shm = { };
1079 size_t max_concurrency = 0;
1080 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001081 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1082 static const uint8_t sha256_out[] = {
1083 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1084 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1085 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1086 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1087 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001088 uint8_t out[32] = { };
1089 pthread_t thr[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001090
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
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001094 shm.size = sizeof(struct ta_concurrent_shm);
1095 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1096 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1097 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1098 return;
1099
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001100 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001101 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001102 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001103
1104 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001105 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001106 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001107 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001108 arg[n].shm = &shm;
1109 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1110 test_1013_thread, arg + n)))
1111 nt = n; /* break loop and start cleanup */
1112 }
1113
1114 for (n = 0; n < nt; n++) {
1115 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1116 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1117 if (arg[n].max_concurrency > max_concurrency)
1118 max_concurrency = arg[n].max_concurrency;
1119 }
1120
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001121 /*
1122 * Concurrency can be limited by several factors, for instance in a
1123 * single CPU system it's dependent on the Preemtion Model used by
1124 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1125 * best result there).
1126 */
1127 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001128 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, NUM_THREADS);
Pascal Brand4fa35582015-12-17 10:59:12 +01001129 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001130 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001131
Jens Wiklander70672972016-04-06 00:01:45 +02001132 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001133 memset(shm.buffer, 0, shm.size);
1134 memset(arg, 0, sizeof(arg));
1135 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001136 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001137
1138 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001139 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001140 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001141 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001142 arg[n].shm = &shm;
1143 arg[n].in = sha256_in;
1144 arg[n].in_len = sizeof(sha256_in);
1145 arg[n].out = out;
1146 arg[n].out_len = sizeof(out);
1147 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1148 test_1013_thread, arg + n)))
1149 nt = n; /* break loop and start cleanup */
1150 }
1151
1152 for (n = 0; n < nt; n++) {
1153 if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) &&
1154 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
1155 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1156 arg[n].out, arg[n].out_len);
1157 if (arg[n].max_concurrency > max_concurrency)
1158 max_concurrency = arg[n].max_concurrency;
1159 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001160 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001161 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001162
Pascal Brand4fa35582015-12-17 10:59:12 +01001163 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001164 TEEC_ReleaseSharedMemory(&shm);
1165}
Pascal Brand4fa35582015-12-17 10:59:12 +01001166
1167static void xtest_tee_test_1013(ADBG_Case_t *c)
1168{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001169 int i = 0;
1170 double mean_concurrency = 0;
1171 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001172 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001173
1174 if (level == 0)
1175 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001176
Jens Wiklander70672972016-04-06 00:01:45 +02001177 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001178 mean_concurrency = 0;
1179 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001180 xtest_tee_test_1013_single(c, &concurrency,
1181 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001182 mean_concurrency += concurrency;
1183 }
1184 mean_concurrency /= nb_loops;
1185
1186 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1187 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001188 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001189
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001190#ifndef CFG_PAGED_USER_TA
Jens Wiklander70672972016-04-06 00:01:45 +02001191 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1192 mean_concurrency = 0;
1193 for (i = 0; i < nb_loops; i++) {
1194 xtest_tee_test_1013_single(c, &concurrency,
1195 &concurrent_large_ta_uuid);
1196 mean_concurrency += concurrency;
1197 }
1198 mean_concurrency /= nb_loops;
1199
1200 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1201 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1202 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001203#endif
Jens Wiklander70672972016-04-06 00:01:45 +02001204}
Jens Wiklander14f48872018-06-29 15:30:13 +02001205ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
1206 "Test concurency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001207
1208#ifdef CFG_SECURE_DATA_PATH
1209static void xtest_tee_test_1014(ADBG_Case_t *c)
1210{
1211 UNUSED(c);
1212
1213 int size = 17000;
1214 int loop = 10;
1215 int ion_heap = DEFAULT_ION_HEAP_TYPE;
1216 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001217 int test = 0;
1218 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001219
1220 test = TEST_NS_TO_TA;
1221 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001222 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001223 ADBG_EXPECT(c, 0, ret);
1224 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1225
1226 test = TEST_TA_TO_TA;
1227 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001228 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001229 ADBG_EXPECT(c, 0, ret);
1230 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1231
1232 test = TEST_TA_TO_PTA;
1233 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP pTA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001234 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001235 ADBG_EXPECT(c, 0, ret);
1236 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP pTA");
1237
1238 test = TEST_NS_TO_PTA;
Etienne Carrierea3198522017-10-26 09:48:55 +02001239 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001240 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001241 ADBG_EXPECT(c, 1, ret);
Etienne Carrierea3198522017-10-26 09:48:55 +02001242 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001243}
Jens Wiklander14f48872018-06-29 15:30:13 +02001244ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1245 "Test secure data path against SDP TAs and pTAs");
1246#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001247
1248static void xtest_tee_test_1015(ADBG_Case_t *c)
1249{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001250 TEEC_Result res = TEEC_ERROR_GENERIC;
1251 TEEC_Session session = { };
1252 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001253
Etienne Carriere11093162017-10-26 09:49:04 +02001254 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001255 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1256 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001257 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1258 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001259 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001260 }
1261 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001262
1263 ADBG_EXPECT_TEEC_SUCCESS(c,
1264 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1265 NULL, &ret_orig));
1266 TEEC_CloseSession(&session);
1267}
Jens Wiklander14f48872018-06-29 15:30:13 +02001268ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1269 "FS hash-tree corner cases");
Jerome Forissiere916b102017-06-07 17:55:52 +02001270
1271static void xtest_tee_test_1016(ADBG_Case_t *c)
1272{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001273 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001274 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001275 uint32_t ret_orig = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001276
1277 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1278 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1279 &ret_orig)))
1280 return;
1281
1282 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1283 TEEC_NONE);
1284
1285 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1286 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1287 &ret_orig));
1288
1289 TEEC_CloseSession(&session);
1290}
Jens Wiklander14f48872018-06-29 15:30:13 +02001291ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1292 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001293
1294static void xtest_tee_test_1017(ADBG_Case_t *c)
1295{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001296 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001297 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001298 uint32_t ret_orig = 0;
1299 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001300 size_t page_size = 4096;
1301
Jens Wiklander87e81702018-03-20 12:00:00 +08001302 shm.size = 8 * page_size;
1303 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1304 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1305 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1306 return;
1307
1308 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1309 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1310 &ret_orig)))
1311 goto out;
1312
1313 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1314 TEEC_MEMREF_PARTIAL_INPUT,
1315 TEEC_MEMREF_PARTIAL_OUTPUT,
1316 TEEC_MEMREF_PARTIAL_OUTPUT);
1317
1318 /*
1319 * The first two memrefs are supposed to be combined into in
1320 * region and the last two memrefs should have one region each
1321 * when the parameters are mapped for the TA.
1322 */
1323 op.params[0].memref.parent = &shm;
1324 op.params[0].memref.size = page_size;
1325 op.params[0].memref.offset = 0;
1326
1327 op.params[1].memref.parent = &shm;
1328 op.params[1].memref.size = page_size;
1329 op.params[1].memref.offset = page_size;
1330
1331 op.params[2].memref.parent = &shm;
1332 op.params[2].memref.size = page_size;
1333 op.params[2].memref.offset = 4 * page_size;
1334
1335 op.params[3].memref.parent = &shm;
1336 op.params[3].memref.size = 2 * page_size;
1337 op.params[3].memref.offset = 6 * page_size;
1338
1339 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1340 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1341 &ret_orig));
1342
1343 TEEC_CloseSession(&session);
1344out:
1345 TEEC_ReleaseSharedMemory(&shm);
1346}
Jens Wiklander14f48872018-06-29 15:30:13 +02001347ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1348 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001349
1350static void xtest_tee_test_1018(ADBG_Case_t *c)
1351{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001352 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001353 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001354 uint32_t ret_orig = 0;
1355 TEEC_SharedMemory shm = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001356 size_t page_size = 4096;
1357
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001358 shm.size = 8 * page_size;
1359 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1360 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1361 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1362 return;
1363
1364 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1365 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1366 &ret_orig)))
1367 goto out;
1368
1369 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1370 TEEC_MEMREF_PARTIAL_INPUT,
1371 TEEC_MEMREF_PARTIAL_OUTPUT,
1372 TEEC_MEMREF_PARTIAL_OUTPUT);
1373
1374 /*
1375 * The first two memrefs are supposed to be combined into in
1376 * region and the last two memrefs should have one region each
1377 * when the parameters are mapped for the TA.
1378 */
1379 op.params[0].memref.parent = &shm;
1380 op.params[0].memref.size = page_size;
1381 op.params[0].memref.offset = 0;
1382
1383 op.params[1].memref.parent = &shm;
1384 op.params[1].memref.size = page_size;
1385 op.params[1].memref.offset = page_size;
1386
1387 op.params[2].memref.parent = &shm;
1388 op.params[2].memref.size = page_size;
1389 op.params[2].memref.offset = 4 * page_size;
1390
1391 op.params[3].memref.parent = &shm;
1392 op.params[3].memref.size = 3 * page_size;
1393 op.params[3].memref.offset = 6 * page_size;
1394
1395 /*
1396 * Depending on the tee driver we may have different error codes.
1397 * What's most important is that secure world doesn't panic and
1398 * that someone detects an error.
1399 */
1400 ADBG_EXPECT_NOT(c, TEE_SUCCESS,
1401 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1402 &ret_orig));
1403
1404 TEEC_CloseSession(&session);
1405out:
1406 TEEC_ReleaseSharedMemory(&shm);
1407}
Jens Wiklander14f48872018-06-29 15:30:13 +02001408ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1409 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001410
Victor Chong3ff36f52018-06-07 04:37:00 +01001411#if defined(CFG_TA_DYNLINK)
Jerome Forissier53bde722018-05-31 09:14:54 +02001412static void xtest_tee_test_1019(ADBG_Case_t *c)
1413{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001414 TEEC_Session session = { };
1415 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001416
1417 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1418 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1419 &ret_orig)))
1420 return;
1421
1422 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1423 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1424 &ret_orig));
1425
1426 (void)ADBG_EXPECT_TEEC_RESULT(c,
1427 TEEC_ERROR_TARGET_DEAD,
1428 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1429 NULL, &ret_orig));
1430
1431 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1432
1433 TEEC_CloseSession(&session);
1434}
Jens Wiklander14f48872018-06-29 15:30:13 +02001435ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1436 "Test dynamically linked TA");
1437#endif /*CFG_TA_DYNLINK*/
Jerome Forissier3357f872018-10-05 15:13:44 +02001438
1439static void xtest_tee_test_1020(ADBG_Case_t *c)
1440{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001441 TEEC_Result res = TEEC_ERROR_GENERIC;
1442 TEEC_Session session = { };
1443 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001444
1445 /* Pseudo TA is optional: warn and nicely exit if not found */
1446 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1447 &ret_orig);
1448 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1449 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1450 return;
1451 }
1452 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1453
1454 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1455 NULL, &ret_orig);
1456 if (res != TEEC_SUCCESS) {
1457 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1458 ret_orig);
1459 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1460 Do_ADBG_Log(" - 1020 - skip test, feature not "
1461 "implemented");
1462 goto out;
1463 }
1464 /* Error */
1465 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1466 }
1467out:
1468 TEEC_CloseSession(&session);
1469}
1470ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1471 "Test lockdep algorithm");