blob: ee0cc4147d2eec5ce62665bd32e21e6d2b462782 [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
44static void xtest_tee_test_1001(ADBG_Case_t *Case_p);
Jens Wiklander1d70a112017-10-16 15:16:39 +020045static void xtest_tee_test_1002(ADBG_Case_t *Case_p);
Jens Wiklander0c86bc32017-11-13 19:52:03 +010046static void xtest_tee_test_1003(ADBG_Case_t *Case_p);
Pascal Brandc639ac82015-07-02 08:53:34 +020047static void xtest_tee_test_1004(ADBG_Case_t *Case_p);
48static void xtest_tee_test_1005(ADBG_Case_t *Case_p);
49static void xtest_tee_test_1006(ADBG_Case_t *Case_p);
50static void xtest_tee_test_1007(ADBG_Case_t *Case_p);
51static void xtest_tee_test_1008(ADBG_Case_t *Case_p);
52static void xtest_tee_test_1009(ADBG_Case_t *Case_p);
53static void xtest_tee_test_1010(ADBG_Case_t *Case_p);
54static void xtest_tee_test_1011(ADBG_Case_t *Case_p);
55static void xtest_tee_test_1012(ADBG_Case_t *Case_p);
Jens Wiklanderac27ec12015-07-15 15:23:14 +020056static void xtest_tee_test_1013(ADBG_Case_t *Case_p);
Etienne Carriere50abf9a2017-03-24 11:33:50 +010057#ifdef CFG_SECURE_DATA_PATH
58static void xtest_tee_test_1014(ADBG_Case_t *Case_p);
59#endif
Jens Wiklander272d3642017-04-03 13:03:47 +020060static void xtest_tee_test_1015(ADBG_Case_t *Case_p);
Jerome Forissiere916b102017-06-07 17:55:52 +020061static void xtest_tee_test_1016(ADBG_Case_t *Case_p);
Jens Wiklander87e81702018-03-20 12:00:00 +080062static void xtest_tee_test_1017(ADBG_Case_t *Case_p);
Pascal Brandc639ac82015-07-02 08:53:34 +020063
Jens Wiklander74abfe32017-01-03 14:17:47 +010064ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Jens Wiklander1d70a112017-10-16 15:16:39 +020065ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander0c86bc32017-11-13 19:52:03 +010066ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
67 "Core internal read/write mutex");
Jens Wiklander74abfe32017-01-03 14:17:47 +010068ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
69ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
70ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
71 "Test Basic OS features");
72ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
73ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
Jens Wiklander25a57fe2016-12-26 21:46:24 +010074 "TEE internal client API");
Jens Wiklander74abfe32017-01-03 14:17:47 +010075ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
76ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
77 "Invalid memory access");
78ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
Jens Wiklanderf7b9c632017-01-03 17:32:26 +010079 "Test TA-to-TA features with User Crypt TA");
Jens Wiklander74abfe32017-01-03 14:17:47 +010080ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
Jens Wiklander25a57fe2016-12-26 21:46:24 +010081 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklander74abfe32017-01-03 14:17:47 +010082ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Jens Wiklander25a57fe2016-12-26 21:46:24 +010083 "Test concurency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +010084#ifdef CFG_SECURE_DATA_PATH
85ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
86 "Test secure data path against SDP TAs and pTAs");
87#endif
Jens Wiklander272d3642017-04-03 13:03:47 +020088ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
89 "FS hash-tree corner cases");
Jerome Forissiere916b102017-06-07 17:55:52 +020090ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
91 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +080092ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
93 "Test coalescing memrefs");
Jens Wiklanderac27ec12015-07-15 15:23:14 +020094
Pascal Brandc639ac82015-07-02 08:53:34 +020095struct xtest_crypto_session {
96 ADBG_Case_t *c;
97 TEEC_Session *session;
98 uint32_t cmd_id_sha256;
99 uint32_t cmd_id_aes256ecb_encrypt;
100 uint32_t cmd_id_aes256ecb_decrypt;
101};
102
103static void xtest_crypto_test(struct xtest_crypto_session *cs)
104{
105 uint32_t ret_orig;
106 uint8_t crypt_out[16];
107 uint8_t crypt_in[16] = { 22, 17 };
108
109 crypt_in[15] = 60;
110
111 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
112 {
113 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
114
115 op.params[0].tmpref.buffer = crypt_in;
116 op.params[0].tmpref.size = sizeof(crypt_in);
117 op.params[1].tmpref.buffer = crypt_out;
118 op.params[1].tmpref.size = sizeof(crypt_out);
119 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
120 TEEC_MEMREF_TEMP_OUTPUT,
121 TEEC_NONE, TEEC_NONE);
122
123 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
124 TEEC_InvokeCommand(cs->session,
125 cs->
126 cmd_id_aes256ecb_encrypt,
127 &op,
128 &ret_orig));
129 }
130 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
131
132 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
133 {
134 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
135 uint8_t out[16];
136
137 op.params[0].tmpref.buffer = crypt_out;
138 op.params[0].tmpref.size = sizeof(crypt_out);
139 op.params[1].tmpref.buffer = out;
140 op.params[1].tmpref.size = sizeof(out);
141 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
142 TEEC_MEMREF_TEMP_OUTPUT,
143 TEEC_NONE, TEEC_NONE);
144
145 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
146 TEEC_InvokeCommand(cs->session,
147 cs->
148 cmd_id_aes256ecb_decrypt,
149 &op,
150 &ret_orig));
151
152 if (!ADBG_EXPECT(cs->c, 0,
153 memcmp(crypt_in, out, sizeof(crypt_in)))) {
154 Do_ADBG_Log("crypt_in:");
155 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
156 Do_ADBG_Log("out:");
157 Do_ADBG_HexLog(out, sizeof(out), 16);
158 }
159 }
160 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
161
162 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
163 {
164 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
165 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
166 static const uint8_t sha256_out[] = {
167 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
168 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
169 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
170 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
171 };
172 uint8_t out[32] = { 0 };
173
174 op.params[0].tmpref.buffer = (void *)sha256_in;
175 op.params[0].tmpref.size = sizeof(sha256_in);
176 op.params[1].tmpref.buffer = out;
177 op.params[1].tmpref.size = sizeof(out);
178 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
179 TEEC_MEMREF_TEMP_OUTPUT,
180 TEEC_NONE, TEEC_NONE);
181
182 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
183 TEEC_InvokeCommand(cs->session,
184 cs->
185 cmd_id_sha256,
186 &op,
187 &ret_orig));
188
189 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
190 sizeof(sha256_out)))) {
191 Do_ADBG_Log("sha256_out:");
192 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
193 Do_ADBG_Log("out:");
194 Do_ADBG_HexLog(out, sizeof(out), 16);
195 }
196 }
197 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
198
Etienne Carrierea3198522017-10-26 09:48:55 +0200199 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200200 {
201 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
202 static const uint8_t in[] = {
203 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
204 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
205 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
206 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
207 };
208 static const uint8_t exp_out[] = {
209 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
210 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
211 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
212 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
213 };
214 uint8_t out[sizeof(exp_out)];
215
216 op.params[0].tmpref.buffer = (void *)in;
217 op.params[0].tmpref.size = sizeof(in);
218 op.params[1].tmpref.buffer = out;
219 op.params[1].tmpref.size = sizeof(out);
220 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
221 TEEC_MEMREF_TEMP_OUTPUT,
222 TEEC_NONE, TEEC_NONE);
223
224 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
225 TEEC_InvokeCommand(cs->session,
226 cs->
227 cmd_id_aes256ecb_encrypt,
228 &op,
229 &ret_orig));
230
231 if (!ADBG_EXPECT(cs->c, 0,
232 memcmp(exp_out, out, sizeof(exp_out)))) {
233 Do_ADBG_Log("exp_out:");
234 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
235 Do_ADBG_Log("out:");
236 Do_ADBG_HexLog(out, sizeof(out), 16);
237 }
238 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200239 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200240
Etienne Carrierea3198522017-10-26 09:48:55 +0200241 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200242 {
243 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
244 static const uint8_t in[] = {
245 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
246 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
247 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
248 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
249 };
250 static const uint8_t exp_out[] = {
251 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
252 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
253 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
254 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
255 };
256 uint8_t out[sizeof(exp_out)];
257
258 op.params[0].tmpref.buffer = (void *)in;
259 op.params[0].tmpref.size = sizeof(in);
260 op.params[1].tmpref.buffer = out;
261 op.params[1].tmpref.size = sizeof(out);
262 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
263 TEEC_MEMREF_TEMP_OUTPUT,
264 TEEC_NONE, TEEC_NONE);
265
266 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
267 TEEC_InvokeCommand(cs->session,
268 cs->
269 cmd_id_aes256ecb_decrypt,
270 &op,
271 &ret_orig));
272
273 if (!ADBG_EXPECT(cs->c, 0,
274 memcmp(exp_out, out, sizeof(exp_out)))) {
275 Do_ADBG_Log("exp_out:");
276 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
277 Do_ADBG_Log("out:");
278 Do_ADBG_HexLog(out, sizeof(out), 16);
279 }
280 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200281 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200282}
283
284static void xtest_tee_test_1001(ADBG_Case_t *c)
285{
Jens Wiklandercf16e842016-02-10 09:07:09 +0100286 TEEC_Result res;
287 TEEC_Session session = { 0 };
288 uint32_t ret_orig;
Pascal Brandc639ac82015-07-02 08:53:34 +0200289
Etienne Carriere11093162017-10-26 09:49:04 +0200290 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100291 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100292 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200293 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
294 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100295 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200296 }
297 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Pascal Brandc639ac82015-07-02 08:53:34 +0200298
Jens Wiklandercf16e842016-02-10 09:07:09 +0100299 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100300 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Jens Wiklandercf16e842016-02-10 09:07:09 +0100301 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200302}
303
Jens Wiklander1d70a112017-10-16 15:16:39 +0200304static void xtest_tee_test_1002(ADBG_Case_t *c)
305{
306 TEEC_Result res;
307 TEEC_Session session = { 0 };
308 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
309 uint32_t ret_orig;
310 uint8_t buf[16 * 1024];
311 uint8_t exp_sum = 0;
312 size_t n;
313
Etienne Carriere11093162017-10-26 09:49:04 +0200314 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200315 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
316 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200317 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
318 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200319 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200320 }
321 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200322
323 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
324 TEEC_NONE, TEEC_NONE);
325 op.params[0].tmpref.size = sizeof(buf);
326 op.params[0].tmpref.buffer = buf;
327
328 for (n = 0; n < sizeof(buf); n++)
329 buf[n] = n + 1;
330 for (n = 0; n < sizeof(buf); n++)
331 exp_sum += buf[n];
332
333 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
334 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
335 goto out;
336
337 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
338out:
339 TEEC_CloseSession(&session);
340}
341
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100342struct test_1003_arg {
343 uint32_t test_type;
344 size_t repeat;
345 size_t max_before_lockers;
346 size_t max_during_lockers;
347 size_t before_lockers;
348 size_t during_lockers;
349 TEEC_Result res;
350 uint32_t error_orig;
351};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200352
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100353static void *test_1003_thread(void *arg)
354{
355 struct test_1003_arg *a = arg;
356 TEEC_Session session = { 0 };
357 size_t rounds = 64 * 1024;
358 size_t n;
359
360 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
361 NULL, &a->error_orig);
362 if (a->res != TEEC_SUCCESS)
363 return NULL;
364
365 for (n = 0; n < a->repeat; n++) {
366 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
367
368 op.params[0].value.a = a->test_type;
369 op.params[0].value.b = rounds;
370
371 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
372 TEEC_VALUE_OUTPUT,
373 TEEC_NONE, TEEC_NONE);
374 a->res = TEEC_InvokeCommand(&session,
375 PTA_INVOKE_TESTS_CMD_MUTEX,
376 &op, &a->error_orig);
377 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
378 op.params[1].value.b != 1) {
379 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
380 a->res = TEEC_ERROR_BAD_STATE;
381 a->error_orig = 42;
382 break;
383 }
384
385 if (a->test_type == PTA_MUTEX_TEST_READER) {
386 if (op.params[1].value.a > a->max_before_lockers)
387 a->max_before_lockers = op.params[1].value.a;
388
389 if (op.params[1].value.b > a->max_during_lockers)
390 a->max_during_lockers = op.params[1].value.b;
391
392 a->before_lockers += op.params[1].value.a;
393 a->during_lockers += op.params[1].value.b;
394 }
395 }
396 TEEC_CloseSession(&session);
397
398 return NULL;
399}
400
401static void xtest_tee_test_1003(ADBG_Case_t *c)
402{
403 size_t num_threads = 3 * 2;
404 TEEC_Result res;
405 TEEC_Session session = { 0 };
406 uint32_t ret_orig;
407 size_t repeat = 20;
408 pthread_t thr[num_threads];
409 struct test_1003_arg arg[num_threads];
410 size_t max_read_concurrency = 0;
411 size_t max_read_waiters = 0;
412 size_t num_concurrent_read_lockers = 0;
413 size_t num_concurrent_read_waiters = 0;
414 size_t n;
415 size_t nt = num_threads;
416 double mean_read_concurrency;
417 double mean_read_waiters;
418 size_t num_writers = 0;
419 size_t num_readers = 0;
420
421 /* Pseudo TA is optional: warn and nicely exit if not found */
422 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
423 &ret_orig);
424 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
425 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
426 return;
427 }
428 ADBG_EXPECT_TEEC_SUCCESS(c, res);
429 TEEC_CloseSession(&session);
430
431 memset(arg, 0, sizeof(arg));
432
433 for (n = 0; n < nt; n++) {
434 if (n % 3) {
435 arg[n].test_type = PTA_MUTEX_TEST_READER;
436 num_readers++;
437 } else {
438 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
439 num_writers++;
440 }
441 arg[n].repeat = repeat;
442 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
443 test_1003_thread, arg + n)))
444 nt = n; /* break loop and start cleanup */
445 }
446
447 for (n = 0; n < nt; n++) {
448 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
449 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
450 Do_ADBG_Log("error origin %" PRIu32,
451 arg[n].error_orig);
452 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
453 if (arg[n].max_during_lockers > max_read_concurrency)
454 max_read_concurrency =
455 arg[n].max_during_lockers;
456
457 if (arg[n].max_before_lockers > max_read_waiters)
458 max_read_waiters = arg[n].max_before_lockers;
459
460 num_concurrent_read_lockers += arg[n].during_lockers;
461 num_concurrent_read_waiters += arg[n].before_lockers;
462 }
463 }
464
465 mean_read_concurrency = (double)num_concurrent_read_lockers /
466 (double)(repeat * num_readers);
467 mean_read_waiters = (double)num_concurrent_read_waiters /
468 (double)(repeat * num_readers);
469
470 Do_ADBG_Log(" Number of parallel threads: %zu (%zu writers and %zu readers)",
471 num_threads, num_writers, num_readers);
472 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
473 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
474 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
475 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
476}
Jens Wiklander1d70a112017-10-16 15:16:39 +0200477
Pascal Brandc639ac82015-07-02 08:53:34 +0200478static void xtest_tee_test_1004(ADBG_Case_t *c)
479{
480 TEEC_Session session = { 0 };
481 uint32_t ret_orig;
482 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
483 TA_CRYPT_CMD_AES256ECB_ENC,
484 TA_CRYPT_CMD_AES256ECB_DEC };
485
486 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
487 &session, &crypt_user_ta_uuid,
488 NULL, &ret_orig)))
489 return;
490
491 /* Run the "complete crypto test suite" */
492 xtest_crypto_test(&cs);
493
494 TEEC_CloseSession(&session);
495}
496
497#ifndef TEEC_ERROR_TARGET_DEAD
498/* To be removed when we have TEEC_ERROR_TARGET_DEAD from tee_client_api.h */
499#define TEEC_ERROR_TARGET_DEAD 0xFFFF3024
500#endif
501
Etienne Carriere92c34422018-02-09 13:11:40 +0100502static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200503{
504 TEEC_Session session = { 0 };
505 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
506 uint32_t ret_orig;
507
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300508 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200509 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300510 &ret_orig)))
511 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200512
513 op.params[0].value.a = n;
514 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
515 TEEC_NONE);
516
517 (void)ADBG_EXPECT_TEEC_RESULT(c,
518 TEEC_ERROR_TARGET_DEAD,
519 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
520 &ret_orig));
521
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300522 (void)ADBG_EXPECT_TEEC_RESULT(c,
523 TEEC_ERROR_TARGET_DEAD,
524 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200525 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300526
Pascal Brandc639ac82015-07-02 08:53:34 +0200527 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
528
529 TEEC_CloseSession(&session);
530}
531
Etienne Carriere92c34422018-02-09 13:11:40 +0100532static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
533 size_t size)
534{
535 TEEC_Session session = { 0 };
536 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
537 uint32_t ret_orig;
538 TEEC_SharedMemory shm;
539
540 memset(&shm, 0, sizeof(shm));
541 shm.size = size;
542 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
543 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
544 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
545 return;
546
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].value.a = (uint32_t)n;
553 op.params[1].memref.parent = &shm;
554 op.params[1].memref.size = size;
555 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
556 TEEC_NONE, TEEC_NONE);
557
558 (void)ADBG_EXPECT_TEEC_RESULT(c,
559 TEEC_ERROR_TARGET_DEAD,
560 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
561 &ret_orig));
562
563 (void)ADBG_EXPECT_TEEC_RESULT(c,
564 TEEC_ERROR_TARGET_DEAD,
565 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
566 &ret_orig));
567
568 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
569
570 TEEC_CloseSession(&session);
571}
572
Pascal Brandc639ac82015-07-02 08:53:34 +0200573static void xtest_tee_test_1005(ADBG_Case_t *c)
574{
575 uint32_t ret_orig;
576#define MAX_SESSIONS 3
577 TEEC_Session sessions[MAX_SESSIONS];
578 int i;
579
580 for (i = 0; i < MAX_SESSIONS; i++) {
581 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200582 xtest_teec_open_session(&sessions[i],
583 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200584 NULL, &ret_orig)))
585 break;
586 }
587
588 for (; --i >= 0; )
589 TEEC_CloseSession(&sessions[i]);
590}
591
592static void xtest_tee_test_1006(ADBG_Case_t *c)
593{
594 TEEC_Session session = { 0 };
595 uint32_t ret_orig;
596 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
597 uint8_t buf[32];
598
599 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
600 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
601 &ret_orig)))
602 return;
603
604 op.params[0].tmpref.buffer = buf;
605 op.params[0].tmpref.size = sizeof(buf);
606 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
607 TEEC_NONE, TEEC_NONE);
608
609 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
610 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
611 &ret_orig));
612
613 TEEC_CloseSession(&session);
614}
615
616static void xtest_tee_test_1007(ADBG_Case_t *c)
617{
618 TEEC_Session session = { 0 };
619 uint32_t ret_orig;
620
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300621 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200622 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300623 &ret_orig)))
624 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200625
626 (void)ADBG_EXPECT_TEEC_RESULT(c,
627 TEEC_ERROR_TARGET_DEAD,
628 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
629 &ret_orig));
630
631 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
632
633 (void)ADBG_EXPECT_TEEC_RESULT(c,
634 TEEC_ERROR_TARGET_DEAD,
635 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
636 &ret_orig));
637
638 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
639
640 TEEC_CloseSession(&session);
641}
642
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100643#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jerome Forissierf02a2212015-10-29 14:33:35 +0100644#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000645# ifdef __ANDROID__
646#define TA_DIR "/system/lib/optee_armtz"
647# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100648#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000649# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100650#endif
651
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100652static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600653{
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100654 char buf[PATH_MAX];
David Brownb2865ab2016-08-02 11:44:41 -0600655
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100656 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100657 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100658 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200659 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
660 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
661 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600662 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200663
Jens Wiklanderb7940892015-10-23 16:02:40 +0200664 return fopen(buf, mode);
665}
666
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100667static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200668{
669 TEEC_Session session = { 0 };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100670 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
671 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200672 TEEC_Result res;
673 uint32_t ret_orig;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100674 FILE *f = NULL;
675 bool r = false;
676 uint8_t *buf = NULL;
677 size_t sz;
678 size_t fread_res;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200679
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100680 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
681 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
682 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200683
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100684 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
685 if (!ADBG_EXPECT_NOT_NULL(c, f))
686 goto out;
687 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
688 goto out;
689 sz = ftell(f);
690 rewind(f);
691
692 buf = malloc(sz);
693 if (!ADBG_EXPECT_NOT_NULL(c, buf))
694 goto out;
695
696 fread_res = fread(buf, 1, sz, f);
697 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
698 goto out;
699
Jens Wiklander4441fe22015-10-23 16:53:02 +0200700 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100701 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200702
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100703 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200704
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100705 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
706 TEEC_NONE, TEEC_NONE);
707 op.params[0].tmpref.buffer = buf;
708 op.params[0].tmpref.size = sz;
709
710 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
711 &ret_orig);
712 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
713out:
714 free(buf);
715 if (f)
716 fclose(f);
717 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200718 return r;
719}
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100720#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Jens Wiklander4441fe22015-10-23 16:53:02 +0200721
Pascal Brandc639ac82015-07-02 08:53:34 +0200722static void xtest_tee_test_1008(ADBG_Case_t *c)
723{
724 TEEC_Session session = { 0 };
725 TEEC_Session session_crypt = { 0 };
726 uint32_t ret_orig;
727
728 Do_ADBG_BeginSubCase(c, "Invoke command");
729 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300730 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200731 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300732 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200733
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300734 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
735 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
736 NULL, &ret_orig));
737 TEEC_CloseSession(&session);
738 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200739
Pascal Brandc639ac82015-07-02 08:53:34 +0200740 }
741 Do_ADBG_EndSubCase(c, "Invoke command");
742
743 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
744 {
745 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
746
747 op.params[0].value.a = 2000;
748 op.paramTypes = TEEC_PARAM_TYPES(
749 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
750
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300751 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200752 xtest_teec_open_session(&session,
753 &os_test_ta_uuid,
754 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300755 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200756
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300757 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
758 TEEC_InvokeCommand(&session,
759 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
760 &op, &ret_orig));
761 TEEC_CloseSession(&session);
762 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200763 }
764 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
765
766 Do_ADBG_BeginSubCase(c, "Create session fail");
767 {
Jens Wiklanderb7940892015-10-23 16:02:40 +0200768 size_t n;
769
Pascal Brandc639ac82015-07-02 08:53:34 +0200770 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
771 xtest_teec_open_session(&session_crypt,
772 &create_fail_test_ta_uuid, NULL,
773 &ret_orig));
Pascal Brandc639ac82015-07-02 08:53:34 +0200774 /*
775 * Run this several times to see that there's no memory leakage.
776 */
777 for (n = 0; n < 100; n++) {
778 Do_ADBG_Log("n = %zu", n);
779 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
780 xtest_teec_open_session(&session_crypt,
781 &create_fail_test_ta_uuid,
782 NULL, &ret_orig));
783 }
784 }
785 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200786
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100787#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jens Wiklander4441fe22015-10-23 16:53:02 +0200788 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
789 ADBG_EXPECT_TRUE(c,
790 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
791 ADBG_EXPECT_TRUE(c,
792 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
793 ADBG_EXPECT_TRUE(c,
794 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
795 ADBG_EXPECT_TRUE(c,
796 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
797 ADBG_EXPECT_TRUE(c,
798 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
799 ADBG_EXPECT_TRUE(c,
800 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
801 ADBG_EXPECT_TRUE(c,
802 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
803 ADBG_EXPECT_TRUE(c,
804 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
805 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
806 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 30000, 1)); /* payload */
807 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100808#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Pascal Brandc639ac82015-07-02 08:53:34 +0200809}
810
Pascal Brandc639ac82015-07-02 08:53:34 +0200811static void *cancellation_thread(void *arg)
812{
813 /*
814 * Sleep 0.5 seconds before cancellation to make sure that the other
815 * thread is in RPC_WAIT.
816 */
817 (void)usleep(500000);
818 TEEC_RequestCancellation(arg);
819 return NULL;
820}
Pascal Brandc639ac82015-07-02 08:53:34 +0200821
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300822static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
823 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200824{
825 TEEC_Session session = { 0 };
826 uint32_t ret_orig;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300827 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200828
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300829 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200830 {
831 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
832
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300833 if (ADBG_EXPECT_TEEC_SUCCESS(c,
834 xtest_teec_open_session(&session, &os_test_ta_uuid,
835 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200836
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300837 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
838 TEEC_ORIGIN_TRUSTED_APP,
839 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200840
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300841 op.params[0].value.a = timeout;
842 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
843 TEEC_NONE,
844 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300845 if (cancel) {
846 (void)ADBG_EXPECT(c, 0,
847 pthread_create(&thr, NULL,
848 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200849
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300850 (void)ADBG_EXPECT_TEEC_RESULT(c,
851 TEEC_ERROR_CANCEL,
852 TEEC_InvokeCommand(&session,
853 TA_OS_TEST_CMD_WAIT,
854 &op,
855 &ret_orig));
856 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300857
858 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
859 TEEC_InvokeCommand(&session,
860 TA_OS_TEST_CMD_WAIT,
861 &op,
862 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300863 if (cancel)
864 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300865
866 TEEC_CloseSession(&session);
867 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200868 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300869 Do_ADBG_EndSubCase(c, "%s", subcase);
870}
871
872static void xtest_tee_test_1009(ADBG_Case_t *c)
873{
874 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
875 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300876 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300877 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200878}
879
880static void xtest_tee_test_1010(ADBG_Case_t *c)
881{
Etienne Carriere92c34422018-02-09 13:11:40 +0100882 unsigned int n;
883 unsigned int idx;
884 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200885
886 for (n = 1; n <= 5; n++) {
887 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
888 xtest_tee_test_invalid_mem_access(c, n);
889 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
890 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100891
892 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
893 for (n = 1; n <= 5; n++) {
894 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200895 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100896 n, memref_sz[idx]);
897 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
898 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200899 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100900 n, memref_sz[idx]);
901 }
902 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200903}
904
905static void xtest_tee_test_1011(ADBG_Case_t *c)
906{
907 TEEC_Session session = { 0 };
908 uint32_t ret_orig;
909 struct xtest_crypto_session cs = {
910 c, &session, TA_RPC_CMD_CRYPT_SHA256,
911 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
912 TA_RPC_CMD_CRYPT_AES256ECB_DEC
913 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100914 struct xtest_crypto_session cs_privmem = {
915 c, &session,
916 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
917 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
918 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
919 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200920 TEEC_UUID uuid = rpc_test_ta_uuid;
921
922 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
923 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
924 return;
925
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100926 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200927 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100928 * Run the "complete crypto test suite" using TA-to-TA
929 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200930 */
931 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100932 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
933
934 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
935 /*
936 * Run the "complete crypto test suite" using TA-to-TA
937 * communication via TA private memory.
938 */
939 xtest_crypto_test(&cs_privmem);
940 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
941
Pascal Brandc639ac82015-07-02 08:53:34 +0200942 TEEC_CloseSession(&session);
943}
944
945/*
946 * Note that this test is failing when
947 * - running twice in a raw
948 * - and the user TA is statically linked
949 * This is because the counter is not reseted when opening the first session
950 * in case the TA is statically linked
951 */
952static void xtest_tee_test_1012(ADBG_Case_t *c)
953{
954 TEEC_Session session1 = { 0 };
955 TEEC_Session session2 = { 0 };
956 uint32_t ret_orig;
957 TEEC_UUID uuid = sims_test_ta_uuid;
958
959 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
960 {
961 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
962 static const uint8_t in[] = {
963 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
964 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
965 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
966 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
967 };
968 uint8_t out[32] = { 0 };
969 int i;
970
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300971 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200972 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300973 &ret_orig)))
974 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200975
976 op.params[0].value.a = 0;
977 op.params[1].tmpref.buffer = (void *)in;
978 op.params[1].tmpref.size = sizeof(in);
979 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
980 TEEC_MEMREF_TEMP_INPUT,
981 TEEC_NONE, TEEC_NONE);
982
983 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
984 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
985 &ret_orig));
986
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100987 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300988 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200989 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300990 &ret_orig)))
991 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200992
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(&session2, TA_SIMS_CMD_READ,
1002 &op, &ret_orig));
1003
1004 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
1005 sizeof(out))) {
1006 Do_ADBG_Log("in:");
1007 Do_ADBG_HexLog(in, sizeof(in), 16);
1008 Do_ADBG_Log("out:");
1009 Do_ADBG_HexLog(out, sizeof(out), 16);
1010 }
1011
1012 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1013 TEEC_NONE, TEEC_NONE,
1014 TEEC_NONE);
1015
1016 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1017 TEEC_InvokeCommand(&session1,
1018 TA_SIMS_CMD_GET_COUNTER,
1019 &op, &ret_orig));
1020
1021 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
1022
1023 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1024 TEEC_InvokeCommand(&session2,
1025 TA_SIMS_CMD_GET_COUNTER, &op,
1026 &ret_orig));
1027
1028 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1029 TEEC_CloseSession(&session2);
1030 }
1031
1032 memset(out, 0, sizeof(out));
1033 op.params[0].value.a = 0;
1034 op.params[1].tmpref.buffer = out;
1035 op.params[1].tmpref.size = sizeof(out);
1036 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1037 TEEC_MEMREF_TEMP_OUTPUT,
1038 TEEC_NONE, TEEC_NONE);
1039
1040 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1041 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1042 &ret_orig));
1043
1044 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1045 Do_ADBG_Log("in:");
1046 Do_ADBG_HexLog(in, sizeof(in), 16);
1047 Do_ADBG_Log("out:");
1048 Do_ADBG_HexLog(out, sizeof(out), 16);
1049 }
1050
1051 TEEC_CloseSession(&session1);
1052 }
1053}
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001054
1055struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001056 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001057 uint32_t cmd;
1058 uint32_t repeat;
1059 TEEC_SharedMemory *shm;
1060 uint32_t error_orig;
1061 TEEC_Result res;
1062 uint32_t max_concurrency;
1063 const uint8_t *in;
1064 size_t in_len;
1065 uint8_t *out;
1066 size_t out_len;
1067};
1068
1069static void *test_1013_thread(void *arg)
1070{
1071 struct test_1013_thread_arg *a = arg;
1072 TEEC_Session session = { 0 };
1073 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1074 uint8_t p2 = TEEC_NONE;
1075 uint8_t p3 = TEEC_NONE;
1076
Jens Wiklander70672972016-04-06 00:01:45 +02001077 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001078 &a->error_orig);
1079 if (a->res != TEEC_SUCCESS)
1080 return NULL;
1081
1082 op.params[0].memref.parent = a->shm;
1083 op.params[0].memref.size = a->shm->size;
1084 op.params[0].memref.offset = 0;
1085 op.params[1].value.a = a->repeat;
1086 op.params[1].value.b = 0;
1087 op.params[2].tmpref.buffer = (void *)a->in;
1088 op.params[2].tmpref.size = a->in_len;
1089 op.params[3].tmpref.buffer = a->out;
1090 op.params[3].tmpref.size = a->out_len;
1091
1092 if (a->in_len)
1093 p2 = TEEC_MEMREF_TEMP_INPUT;
1094 if (a->out_len)
1095 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1096
1097 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1098 TEEC_VALUE_INOUT, p2, p3);
1099
1100 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1101 a->max_concurrency = op.params[1].value.b;
1102 a->out_len = op.params[3].tmpref.size;
1103 TEEC_CloseSession(&session);
1104 return NULL;
1105}
1106
Pascal Brand4fa35582015-12-17 10:59:12 +01001107#define NUM_THREADS 3
1108
Jens Wiklander70672972016-04-06 00:01:45 +02001109static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1110 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001111{
Pascal Brand4fa35582015-12-17 10:59:12 +01001112 size_t num_threads = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001113 size_t nt;
1114 size_t n;
Jens Wiklander70672972016-04-06 00:01:45 +02001115 size_t repeat = 1000;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001116 pthread_t thr[num_threads];
1117 TEEC_SharedMemory shm;
1118 size_t max_concurrency;
1119 struct test_1013_thread_arg arg[num_threads];
1120 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1121 static const uint8_t sha256_out[] = {
1122 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1123 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1124 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1125 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1126 };
1127 uint8_t out[32] = { 0 };
1128
Jens Wiklander70672972016-04-06 00:01:45 +02001129 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001130 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001131
1132 memset(&shm, 0, sizeof(shm));
1133 shm.size = sizeof(struct ta_concurrent_shm);
1134 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1135 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1136 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1137 return;
1138
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001139 memset(shm.buffer, 0, shm.size);
1140 memset(arg, 0, sizeof(arg));
1141 max_concurrency = 0;
1142 nt = num_threads;
1143
1144 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001145 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001146 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001147 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001148 arg[n].shm = &shm;
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 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1156 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1157 if (arg[n].max_concurrency > max_concurrency)
1158 max_concurrency = arg[n].max_concurrency;
1159 }
1160
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001161 /*
1162 * Concurrency can be limited by several factors, for instance in a
1163 * single CPU system it's dependent on the Preemtion Model used by
1164 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1165 * best result there).
1166 */
1167 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
1168 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, num_threads);
Pascal Brand4fa35582015-12-17 10:59:12 +01001169 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001170 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001171
Jens Wiklander70672972016-04-06 00:01:45 +02001172 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001173 memset(shm.buffer, 0, shm.size);
1174 memset(arg, 0, sizeof(arg));
1175 max_concurrency = 0;
1176 nt = num_threads;
1177
1178 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001179 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001180 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001181 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001182 arg[n].shm = &shm;
1183 arg[n].in = sha256_in;
1184 arg[n].in_len = sizeof(sha256_in);
1185 arg[n].out = out;
1186 arg[n].out_len = sizeof(out);
1187 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1188 test_1013_thread, arg + n)))
1189 nt = n; /* break loop and start cleanup */
1190 }
1191
1192 for (n = 0; n < nt; n++) {
1193 if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) &&
1194 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
1195 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1196 arg[n].out, arg[n].out_len);
1197 if (arg[n].max_concurrency > max_concurrency)
1198 max_concurrency = arg[n].max_concurrency;
1199 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001200 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001201 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001202
Pascal Brand4fa35582015-12-17 10:59:12 +01001203 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001204 TEEC_ReleaseSharedMemory(&shm);
1205}
Pascal Brand4fa35582015-12-17 10:59:12 +01001206
1207static void xtest_tee_test_1013(ADBG_Case_t *c)
1208{
1209 int i;
1210 double mean_concurrency;
1211 double concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001212 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001213
1214 if (level == 0)
1215 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001216
Jens Wiklander70672972016-04-06 00:01:45 +02001217 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001218 mean_concurrency = 0;
1219 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001220 xtest_tee_test_1013_single(c, &concurrency,
1221 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001222 mean_concurrency += concurrency;
1223 }
1224 mean_concurrency /= nb_loops;
1225
1226 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1227 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001228 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001229
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001230#ifndef CFG_PAGED_USER_TA
Jens Wiklander70672972016-04-06 00:01:45 +02001231 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1232 mean_concurrency = 0;
1233 for (i = 0; i < nb_loops; i++) {
1234 xtest_tee_test_1013_single(c, &concurrency,
1235 &concurrent_large_ta_uuid);
1236 mean_concurrency += concurrency;
1237 }
1238 mean_concurrency /= nb_loops;
1239
1240 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1241 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1242 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001243#endif
Jens Wiklander70672972016-04-06 00:01:45 +02001244}
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001245
1246#ifdef CFG_SECURE_DATA_PATH
1247static void xtest_tee_test_1014(ADBG_Case_t *c)
1248{
1249 UNUSED(c);
1250
1251 int size = 17000;
1252 int loop = 10;
1253 int ion_heap = DEFAULT_ION_HEAP_TYPE;
1254 int rnd_offset = 1;
1255 int test;
1256 int ret;
1257
1258 test = TEST_NS_TO_TA;
1259 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001260 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001261 ADBG_EXPECT(c, 0, ret);
1262 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1263
1264 test = TEST_TA_TO_TA;
1265 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001266 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001267 ADBG_EXPECT(c, 0, ret);
1268 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1269
1270 test = TEST_TA_TO_PTA;
1271 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP pTA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001272 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001273 ADBG_EXPECT(c, 0, ret);
1274 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP pTA");
1275
1276 test = TEST_NS_TO_PTA;
Etienne Carrierea3198522017-10-26 09:48:55 +02001277 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001278 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001279 ADBG_EXPECT(c, 1, ret);
Etienne Carrierea3198522017-10-26 09:48:55 +02001280 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001281}
1282#endif
Jens Wiklander272d3642017-04-03 13:03:47 +02001283
1284static void xtest_tee_test_1015(ADBG_Case_t *c)
1285{
1286 TEEC_Result res;
1287 TEEC_Session session = { 0 };
1288 uint32_t ret_orig;
1289
Etienne Carriere11093162017-10-26 09:49:04 +02001290 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001291 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1292 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001293 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1294 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001295 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001296 }
1297 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001298
1299 ADBG_EXPECT_TEEC_SUCCESS(c,
1300 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1301 NULL, &ret_orig));
1302 TEEC_CloseSession(&session);
1303}
Jerome Forissiere916b102017-06-07 17:55:52 +02001304
1305static void xtest_tee_test_1016(ADBG_Case_t *c)
1306{
1307 TEEC_Session session = { 0 };
1308 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1309 uint32_t ret_orig;
1310
1311 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1312 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1313 &ret_orig)))
1314 return;
1315
1316 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1317 TEEC_NONE);
1318
1319 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1320 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1321 &ret_orig));
1322
1323 TEEC_CloseSession(&session);
1324}
Jens Wiklander87e81702018-03-20 12:00:00 +08001325
1326static void xtest_tee_test_1017(ADBG_Case_t *c)
1327{
1328 TEEC_Session session = { 0 };
1329 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1330 uint32_t ret_orig;
1331 TEEC_SharedMemory shm;
1332 size_t page_size = 4096;
1333
1334 memset(&shm, 0, sizeof(shm));
1335 shm.size = 8 * page_size;
1336 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1337 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1338 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1339 return;
1340
1341 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1342 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1343 &ret_orig)))
1344 goto out;
1345
1346 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1347 TEEC_MEMREF_PARTIAL_INPUT,
1348 TEEC_MEMREF_PARTIAL_OUTPUT,
1349 TEEC_MEMREF_PARTIAL_OUTPUT);
1350
1351 /*
1352 * The first two memrefs are supposed to be combined into in
1353 * region and the last two memrefs should have one region each
1354 * when the parameters are mapped for the TA.
1355 */
1356 op.params[0].memref.parent = &shm;
1357 op.params[0].memref.size = page_size;
1358 op.params[0].memref.offset = 0;
1359
1360 op.params[1].memref.parent = &shm;
1361 op.params[1].memref.size = page_size;
1362 op.params[1].memref.offset = page_size;
1363
1364 op.params[2].memref.parent = &shm;
1365 op.params[2].memref.size = page_size;
1366 op.params[2].memref.offset = 4 * page_size;
1367
1368 op.params[3].memref.parent = &shm;
1369 op.params[3].memref.size = 2 * page_size;
1370 op.params[3].memref.offset = 6 * page_size;
1371
1372 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1373 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1374 &ret_orig));
1375
1376 TEEC_CloseSession(&session);
1377out:
1378 TEEC_ReleaseSharedMemory(&shm);
1379}