blob: 933dff77a9f87c04570fdfab74680a29d2973990 [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);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +080063static void xtest_tee_test_1018(ADBG_Case_t *Case_p);
Pascal Brandc639ac82015-07-02 08:53:34 +020064
Jens Wiklander74abfe32017-01-03 14:17:47 +010065ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Jens Wiklander1d70a112017-10-16 15:16:39 +020066ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander0c86bc32017-11-13 19:52:03 +010067ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
68 "Core internal read/write mutex");
Jens Wiklander74abfe32017-01-03 14:17:47 +010069ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
70ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
71ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
72 "Test Basic OS features");
73ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
74ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
Jens Wiklander25a57fe2016-12-26 21:46:24 +010075 "TEE internal client API");
Jens Wiklander74abfe32017-01-03 14:17:47 +010076ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
77ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
78 "Invalid memory access");
79ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
Jens Wiklanderf7b9c632017-01-03 17:32:26 +010080 "Test TA-to-TA features with User Crypt TA");
Jens Wiklander74abfe32017-01-03 14:17:47 +010081ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
Jens Wiklander25a57fe2016-12-26 21:46:24 +010082 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklander74abfe32017-01-03 14:17:47 +010083ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Jens Wiklander25a57fe2016-12-26 21:46:24 +010084 "Test concurency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +010085#ifdef CFG_SECURE_DATA_PATH
86ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
87 "Test secure data path against SDP TAs and pTAs");
88#endif
Jens Wiklander272d3642017-04-03 13:03:47 +020089ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
90 "FS hash-tree corner cases");
Jerome Forissiere916b102017-06-07 17:55:52 +020091ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
92 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +080093ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
94 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +080095ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
96 "Test memref out of bounds");
Jens Wiklanderac27ec12015-07-15 15:23:14 +020097
Pascal Brandc639ac82015-07-02 08:53:34 +020098struct xtest_crypto_session {
99 ADBG_Case_t *c;
100 TEEC_Session *session;
101 uint32_t cmd_id_sha256;
102 uint32_t cmd_id_aes256ecb_encrypt;
103 uint32_t cmd_id_aes256ecb_decrypt;
104};
105
106static void xtest_crypto_test(struct xtest_crypto_session *cs)
107{
108 uint32_t ret_orig;
109 uint8_t crypt_out[16];
110 uint8_t crypt_in[16] = { 22, 17 };
111
112 crypt_in[15] = 60;
113
114 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
115 {
116 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
117
118 op.params[0].tmpref.buffer = crypt_in;
119 op.params[0].tmpref.size = sizeof(crypt_in);
120 op.params[1].tmpref.buffer = crypt_out;
121 op.params[1].tmpref.size = sizeof(crypt_out);
122 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
123 TEEC_MEMREF_TEMP_OUTPUT,
124 TEEC_NONE, TEEC_NONE);
125
126 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
127 TEEC_InvokeCommand(cs->session,
128 cs->
129 cmd_id_aes256ecb_encrypt,
130 &op,
131 &ret_orig));
132 }
133 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
134
135 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
136 {
137 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
138 uint8_t out[16];
139
140 op.params[0].tmpref.buffer = crypt_out;
141 op.params[0].tmpref.size = sizeof(crypt_out);
142 op.params[1].tmpref.buffer = out;
143 op.params[1].tmpref.size = sizeof(out);
144 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
145 TEEC_MEMREF_TEMP_OUTPUT,
146 TEEC_NONE, TEEC_NONE);
147
148 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
149 TEEC_InvokeCommand(cs->session,
150 cs->
151 cmd_id_aes256ecb_decrypt,
152 &op,
153 &ret_orig));
154
155 if (!ADBG_EXPECT(cs->c, 0,
156 memcmp(crypt_in, out, sizeof(crypt_in)))) {
157 Do_ADBG_Log("crypt_in:");
158 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
159 Do_ADBG_Log("out:");
160 Do_ADBG_HexLog(out, sizeof(out), 16);
161 }
162 }
163 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
164
165 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
166 {
167 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
168 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
169 static const uint8_t sha256_out[] = {
170 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
171 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
172 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
173 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
174 };
175 uint8_t out[32] = { 0 };
176
177 op.params[0].tmpref.buffer = (void *)sha256_in;
178 op.params[0].tmpref.size = sizeof(sha256_in);
179 op.params[1].tmpref.buffer = out;
180 op.params[1].tmpref.size = sizeof(out);
181 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
182 TEEC_MEMREF_TEMP_OUTPUT,
183 TEEC_NONE, TEEC_NONE);
184
185 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
186 TEEC_InvokeCommand(cs->session,
187 cs->
188 cmd_id_sha256,
189 &op,
190 &ret_orig));
191
192 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
193 sizeof(sha256_out)))) {
194 Do_ADBG_Log("sha256_out:");
195 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
196 Do_ADBG_Log("out:");
197 Do_ADBG_HexLog(out, sizeof(out), 16);
198 }
199 }
200 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
201
Etienne Carrierea3198522017-10-26 09:48:55 +0200202 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200203 {
204 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
205 static const uint8_t in[] = {
206 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
207 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
208 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
209 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
210 };
211 static const uint8_t exp_out[] = {
212 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
213 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
214 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
215 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
216 };
217 uint8_t out[sizeof(exp_out)];
218
219 op.params[0].tmpref.buffer = (void *)in;
220 op.params[0].tmpref.size = sizeof(in);
221 op.params[1].tmpref.buffer = out;
222 op.params[1].tmpref.size = sizeof(out);
223 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
224 TEEC_MEMREF_TEMP_OUTPUT,
225 TEEC_NONE, TEEC_NONE);
226
227 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
228 TEEC_InvokeCommand(cs->session,
229 cs->
230 cmd_id_aes256ecb_encrypt,
231 &op,
232 &ret_orig));
233
234 if (!ADBG_EXPECT(cs->c, 0,
235 memcmp(exp_out, out, sizeof(exp_out)))) {
236 Do_ADBG_Log("exp_out:");
237 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
238 Do_ADBG_Log("out:");
239 Do_ADBG_HexLog(out, sizeof(out), 16);
240 }
241 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200242 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200243
Etienne Carrierea3198522017-10-26 09:48:55 +0200244 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200245 {
246 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
247 static const uint8_t in[] = {
248 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
249 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
250 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
251 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
252 };
253 static const uint8_t exp_out[] = {
254 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
255 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
256 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
257 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
258 };
259 uint8_t out[sizeof(exp_out)];
260
261 op.params[0].tmpref.buffer = (void *)in;
262 op.params[0].tmpref.size = sizeof(in);
263 op.params[1].tmpref.buffer = out;
264 op.params[1].tmpref.size = sizeof(out);
265 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
266 TEEC_MEMREF_TEMP_OUTPUT,
267 TEEC_NONE, TEEC_NONE);
268
269 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
270 TEEC_InvokeCommand(cs->session,
271 cs->
272 cmd_id_aes256ecb_decrypt,
273 &op,
274 &ret_orig));
275
276 if (!ADBG_EXPECT(cs->c, 0,
277 memcmp(exp_out, out, sizeof(exp_out)))) {
278 Do_ADBG_Log("exp_out:");
279 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
280 Do_ADBG_Log("out:");
281 Do_ADBG_HexLog(out, sizeof(out), 16);
282 }
283 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200284 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200285}
286
287static void xtest_tee_test_1001(ADBG_Case_t *c)
288{
Jens Wiklandercf16e842016-02-10 09:07:09 +0100289 TEEC_Result res;
290 TEEC_Session session = { 0 };
291 uint32_t ret_orig;
Pascal Brandc639ac82015-07-02 08:53:34 +0200292
Etienne Carriere11093162017-10-26 09:49:04 +0200293 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100294 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100295 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200296 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
297 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100298 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200299 }
300 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Pascal Brandc639ac82015-07-02 08:53:34 +0200301
Jens Wiklandercf16e842016-02-10 09:07:09 +0100302 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100303 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Jens Wiklandercf16e842016-02-10 09:07:09 +0100304 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200305}
306
Jens Wiklander1d70a112017-10-16 15:16:39 +0200307static void xtest_tee_test_1002(ADBG_Case_t *c)
308{
309 TEEC_Result res;
310 TEEC_Session session = { 0 };
311 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
312 uint32_t ret_orig;
313 uint8_t buf[16 * 1024];
314 uint8_t exp_sum = 0;
315 size_t n;
316
Etienne Carriere11093162017-10-26 09:49:04 +0200317 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200318 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
319 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200320 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
321 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200322 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200323 }
324 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200325
326 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
327 TEEC_NONE, TEEC_NONE);
328 op.params[0].tmpref.size = sizeof(buf);
329 op.params[0].tmpref.buffer = buf;
330
331 for (n = 0; n < sizeof(buf); n++)
332 buf[n] = n + 1;
333 for (n = 0; n < sizeof(buf); n++)
334 exp_sum += buf[n];
335
336 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
337 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
338 goto out;
339
340 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
341out:
342 TEEC_CloseSession(&session);
343}
344
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100345struct test_1003_arg {
346 uint32_t test_type;
347 size_t repeat;
348 size_t max_before_lockers;
349 size_t max_during_lockers;
350 size_t before_lockers;
351 size_t during_lockers;
352 TEEC_Result res;
353 uint32_t error_orig;
354};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200355
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100356static void *test_1003_thread(void *arg)
357{
358 struct test_1003_arg *a = arg;
359 TEEC_Session session = { 0 };
360 size_t rounds = 64 * 1024;
361 size_t n;
362
363 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
364 NULL, &a->error_orig);
365 if (a->res != TEEC_SUCCESS)
366 return NULL;
367
368 for (n = 0; n < a->repeat; n++) {
369 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
370
371 op.params[0].value.a = a->test_type;
372 op.params[0].value.b = rounds;
373
374 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
375 TEEC_VALUE_OUTPUT,
376 TEEC_NONE, TEEC_NONE);
377 a->res = TEEC_InvokeCommand(&session,
378 PTA_INVOKE_TESTS_CMD_MUTEX,
379 &op, &a->error_orig);
380 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
381 op.params[1].value.b != 1) {
382 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
383 a->res = TEEC_ERROR_BAD_STATE;
384 a->error_orig = 42;
385 break;
386 }
387
388 if (a->test_type == PTA_MUTEX_TEST_READER) {
389 if (op.params[1].value.a > a->max_before_lockers)
390 a->max_before_lockers = op.params[1].value.a;
391
392 if (op.params[1].value.b > a->max_during_lockers)
393 a->max_during_lockers = op.params[1].value.b;
394
395 a->before_lockers += op.params[1].value.a;
396 a->during_lockers += op.params[1].value.b;
397 }
398 }
399 TEEC_CloseSession(&session);
400
401 return NULL;
402}
403
404static void xtest_tee_test_1003(ADBG_Case_t *c)
405{
406 size_t num_threads = 3 * 2;
407 TEEC_Result res;
408 TEEC_Session session = { 0 };
409 uint32_t ret_orig;
410 size_t repeat = 20;
411 pthread_t thr[num_threads];
412 struct test_1003_arg arg[num_threads];
413 size_t max_read_concurrency = 0;
414 size_t max_read_waiters = 0;
415 size_t num_concurrent_read_lockers = 0;
416 size_t num_concurrent_read_waiters = 0;
417 size_t n;
418 size_t nt = num_threads;
419 double mean_read_concurrency;
420 double mean_read_waiters;
421 size_t num_writers = 0;
422 size_t num_readers = 0;
423
424 /* Pseudo TA is optional: warn and nicely exit if not found */
425 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
426 &ret_orig);
427 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
428 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
429 return;
430 }
431 ADBG_EXPECT_TEEC_SUCCESS(c, res);
432 TEEC_CloseSession(&session);
433
434 memset(arg, 0, sizeof(arg));
435
436 for (n = 0; n < nt; n++) {
437 if (n % 3) {
438 arg[n].test_type = PTA_MUTEX_TEST_READER;
439 num_readers++;
440 } else {
441 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
442 num_writers++;
443 }
444 arg[n].repeat = repeat;
445 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
446 test_1003_thread, arg + n)))
447 nt = n; /* break loop and start cleanup */
448 }
449
450 for (n = 0; n < nt; n++) {
451 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
452 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
453 Do_ADBG_Log("error origin %" PRIu32,
454 arg[n].error_orig);
455 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
456 if (arg[n].max_during_lockers > max_read_concurrency)
457 max_read_concurrency =
458 arg[n].max_during_lockers;
459
460 if (arg[n].max_before_lockers > max_read_waiters)
461 max_read_waiters = arg[n].max_before_lockers;
462
463 num_concurrent_read_lockers += arg[n].during_lockers;
464 num_concurrent_read_waiters += arg[n].before_lockers;
465 }
466 }
467
468 mean_read_concurrency = (double)num_concurrent_read_lockers /
469 (double)(repeat * num_readers);
470 mean_read_waiters = (double)num_concurrent_read_waiters /
471 (double)(repeat * num_readers);
472
473 Do_ADBG_Log(" Number of parallel threads: %zu (%zu writers and %zu readers)",
474 num_threads, num_writers, num_readers);
475 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
476 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
477 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
478 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
479}
Jens Wiklander1d70a112017-10-16 15:16:39 +0200480
Pascal Brandc639ac82015-07-02 08:53:34 +0200481static void xtest_tee_test_1004(ADBG_Case_t *c)
482{
483 TEEC_Session session = { 0 };
484 uint32_t ret_orig;
485 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
486 TA_CRYPT_CMD_AES256ECB_ENC,
487 TA_CRYPT_CMD_AES256ECB_DEC };
488
489 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
490 &session, &crypt_user_ta_uuid,
491 NULL, &ret_orig)))
492 return;
493
494 /* Run the "complete crypto test suite" */
495 xtest_crypto_test(&cs);
496
497 TEEC_CloseSession(&session);
498}
499
Etienne Carriere92c34422018-02-09 13:11:40 +0100500static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200501{
502 TEEC_Session session = { 0 };
503 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
504 uint32_t ret_orig;
505
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300506 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200507 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300508 &ret_orig)))
509 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200510
511 op.params[0].value.a = n;
512 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
513 TEEC_NONE);
514
515 (void)ADBG_EXPECT_TEEC_RESULT(c,
516 TEEC_ERROR_TARGET_DEAD,
517 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
518 &ret_orig));
519
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300520 (void)ADBG_EXPECT_TEEC_RESULT(c,
521 TEEC_ERROR_TARGET_DEAD,
522 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200523 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300524
Pascal Brandc639ac82015-07-02 08:53:34 +0200525 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
526
527 TEEC_CloseSession(&session);
528}
529
Etienne Carriere92c34422018-02-09 13:11:40 +0100530static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
531 size_t size)
532{
533 TEEC_Session session = { 0 };
534 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
535 uint32_t ret_orig;
536 TEEC_SharedMemory shm;
537
538 memset(&shm, 0, sizeof(shm));
539 shm.size = size;
540 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
541 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
542 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
543 return;
544
545 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
546 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
547 &ret_orig)))
548 return;
549
550 op.params[0].value.a = (uint32_t)n;
551 op.params[1].memref.parent = &shm;
552 op.params[1].memref.size = size;
553 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
554 TEEC_NONE, TEEC_NONE);
555
556 (void)ADBG_EXPECT_TEEC_RESULT(c,
557 TEEC_ERROR_TARGET_DEAD,
558 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
559 &ret_orig));
560
561 (void)ADBG_EXPECT_TEEC_RESULT(c,
562 TEEC_ERROR_TARGET_DEAD,
563 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
564 &ret_orig));
565
566 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
567
568 TEEC_CloseSession(&session);
569}
570
Pascal Brandc639ac82015-07-02 08:53:34 +0200571static void xtest_tee_test_1005(ADBG_Case_t *c)
572{
573 uint32_t ret_orig;
574#define MAX_SESSIONS 3
575 TEEC_Session sessions[MAX_SESSIONS];
576 int i;
577
578 for (i = 0; i < MAX_SESSIONS; i++) {
579 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200580 xtest_teec_open_session(&sessions[i],
581 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200582 NULL, &ret_orig)))
583 break;
584 }
585
586 for (; --i >= 0; )
587 TEEC_CloseSession(&sessions[i]);
588}
589
590static void xtest_tee_test_1006(ADBG_Case_t *c)
591{
592 TEEC_Session session = { 0 };
593 uint32_t ret_orig;
594 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
595 uint8_t buf[32];
596
597 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
598 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
599 &ret_orig)))
600 return;
601
602 op.params[0].tmpref.buffer = buf;
603 op.params[0].tmpref.size = sizeof(buf);
604 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
605 TEEC_NONE, TEEC_NONE);
606
607 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
608 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
609 &ret_orig));
610
611 TEEC_CloseSession(&session);
612}
613
614static void xtest_tee_test_1007(ADBG_Case_t *c)
615{
616 TEEC_Session session = { 0 };
617 uint32_t ret_orig;
618
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300619 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200620 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300621 &ret_orig)))
622 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200623
624 (void)ADBG_EXPECT_TEEC_RESULT(c,
625 TEEC_ERROR_TARGET_DEAD,
626 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
627 &ret_orig));
628
629 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
630
631 (void)ADBG_EXPECT_TEEC_RESULT(c,
632 TEEC_ERROR_TARGET_DEAD,
633 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
634 &ret_orig));
635
636 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
637
638 TEEC_CloseSession(&session);
639}
640
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100641#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jerome Forissierf02a2212015-10-29 14:33:35 +0100642#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000643# ifdef __ANDROID__
644#define TA_DIR "/system/lib/optee_armtz"
645# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100646#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000647# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100648#endif
649
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100650static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600651{
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100652 char buf[PATH_MAX];
David Brownb2865ab2016-08-02 11:44:41 -0600653
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100654 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100655 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100656 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200657 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
658 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
659 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600660 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200661
Jens Wiklanderb7940892015-10-23 16:02:40 +0200662 return fopen(buf, mode);
663}
664
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100665static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200666{
667 TEEC_Session session = { 0 };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100668 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
669 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200670 TEEC_Result res;
671 uint32_t ret_orig;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100672 FILE *f = NULL;
673 bool r = false;
674 uint8_t *buf = NULL;
675 size_t sz;
676 size_t fread_res;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200677
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100678 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
679 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
680 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200681
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100682 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
683 if (!ADBG_EXPECT_NOT_NULL(c, f))
684 goto out;
685 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
686 goto out;
687 sz = ftell(f);
688 rewind(f);
689
690 buf = malloc(sz);
691 if (!ADBG_EXPECT_NOT_NULL(c, buf))
692 goto out;
693
694 fread_res = fread(buf, 1, sz, f);
695 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
696 goto out;
697
Jens Wiklander4441fe22015-10-23 16:53:02 +0200698 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100699 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200700
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100701 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200702
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100703 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
704 TEEC_NONE, TEEC_NONE);
705 op.params[0].tmpref.buffer = buf;
706 op.params[0].tmpref.size = sz;
707
708 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
709 &ret_orig);
710 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
711out:
712 free(buf);
713 if (f)
714 fclose(f);
715 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200716 return r;
717}
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100718#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Jens Wiklander4441fe22015-10-23 16:53:02 +0200719
Pascal Brandc639ac82015-07-02 08:53:34 +0200720static void xtest_tee_test_1008(ADBG_Case_t *c)
721{
722 TEEC_Session session = { 0 };
723 TEEC_Session session_crypt = { 0 };
724 uint32_t ret_orig;
725
726 Do_ADBG_BeginSubCase(c, "Invoke command");
727 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300728 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200729 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300730 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200731
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300732 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
733 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
734 NULL, &ret_orig));
735 TEEC_CloseSession(&session);
736 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200737
Pascal Brandc639ac82015-07-02 08:53:34 +0200738 }
739 Do_ADBG_EndSubCase(c, "Invoke command");
740
741 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
742 {
743 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
744
745 op.params[0].value.a = 2000;
746 op.paramTypes = TEEC_PARAM_TYPES(
747 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
748
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300749 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200750 xtest_teec_open_session(&session,
751 &os_test_ta_uuid,
752 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300753 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200754
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300755 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
756 TEEC_InvokeCommand(&session,
757 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
758 &op, &ret_orig));
759 TEEC_CloseSession(&session);
760 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200761 }
762 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
763
764 Do_ADBG_BeginSubCase(c, "Create session fail");
765 {
Jens Wiklanderb7940892015-10-23 16:02:40 +0200766 size_t n;
767
Pascal Brandc639ac82015-07-02 08:53:34 +0200768 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
769 xtest_teec_open_session(&session_crypt,
770 &create_fail_test_ta_uuid, NULL,
771 &ret_orig));
Pascal Brandc639ac82015-07-02 08:53:34 +0200772 /*
773 * Run this several times to see that there's no memory leakage.
774 */
775 for (n = 0; n < 100; n++) {
776 Do_ADBG_Log("n = %zu", n);
777 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
778 xtest_teec_open_session(&session_crypt,
779 &create_fail_test_ta_uuid,
780 NULL, &ret_orig));
781 }
782 }
783 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200784
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100785#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jens Wiklander4441fe22015-10-23 16:53:02 +0200786 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
787 ADBG_EXPECT_TRUE(c,
788 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
789 ADBG_EXPECT_TRUE(c,
790 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
791 ADBG_EXPECT_TRUE(c,
792 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
793 ADBG_EXPECT_TRUE(c,
794 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
795 ADBG_EXPECT_TRUE(c,
796 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
797 ADBG_EXPECT_TRUE(c,
798 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
799 ADBG_EXPECT_TRUE(c,
800 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
801 ADBG_EXPECT_TRUE(c,
802 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
803 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
804 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 30000, 1)); /* payload */
805 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100806#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Pascal Brandc639ac82015-07-02 08:53:34 +0200807}
808
Pascal Brandc639ac82015-07-02 08:53:34 +0200809static void *cancellation_thread(void *arg)
810{
811 /*
812 * Sleep 0.5 seconds before cancellation to make sure that the other
813 * thread is in RPC_WAIT.
814 */
815 (void)usleep(500000);
816 TEEC_RequestCancellation(arg);
817 return NULL;
818}
Pascal Brandc639ac82015-07-02 08:53:34 +0200819
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300820static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
821 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200822{
823 TEEC_Session session = { 0 };
824 uint32_t ret_orig;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300825 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200826
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300827 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200828 {
829 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
830
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300831 if (ADBG_EXPECT_TEEC_SUCCESS(c,
832 xtest_teec_open_session(&session, &os_test_ta_uuid,
833 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200834
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300835 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
836 TEEC_ORIGIN_TRUSTED_APP,
837 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200838
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300839 op.params[0].value.a = timeout;
840 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
841 TEEC_NONE,
842 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300843 if (cancel) {
844 (void)ADBG_EXPECT(c, 0,
845 pthread_create(&thr, NULL,
846 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200847
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300848 (void)ADBG_EXPECT_TEEC_RESULT(c,
849 TEEC_ERROR_CANCEL,
850 TEEC_InvokeCommand(&session,
851 TA_OS_TEST_CMD_WAIT,
852 &op,
853 &ret_orig));
854 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300855
856 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
857 TEEC_InvokeCommand(&session,
858 TA_OS_TEST_CMD_WAIT,
859 &op,
860 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300861 if (cancel)
862 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300863
864 TEEC_CloseSession(&session);
865 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200866 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300867 Do_ADBG_EndSubCase(c, "%s", subcase);
868}
869
870static void xtest_tee_test_1009(ADBG_Case_t *c)
871{
872 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
873 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300874 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300875 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200876}
877
878static void xtest_tee_test_1010(ADBG_Case_t *c)
879{
Etienne Carriere92c34422018-02-09 13:11:40 +0100880 unsigned int n;
881 unsigned int idx;
882 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200883
884 for (n = 1; n <= 5; n++) {
885 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
886 xtest_tee_test_invalid_mem_access(c, n);
887 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
888 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100889
890 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
891 for (n = 1; n <= 5; n++) {
892 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200893 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100894 n, memref_sz[idx]);
895 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
896 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200897 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100898 n, memref_sz[idx]);
899 }
900 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200901}
902
903static void xtest_tee_test_1011(ADBG_Case_t *c)
904{
905 TEEC_Session session = { 0 };
906 uint32_t ret_orig;
907 struct xtest_crypto_session cs = {
908 c, &session, TA_RPC_CMD_CRYPT_SHA256,
909 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
910 TA_RPC_CMD_CRYPT_AES256ECB_DEC
911 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100912 struct xtest_crypto_session cs_privmem = {
913 c, &session,
914 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
915 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
916 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
917 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200918 TEEC_UUID uuid = rpc_test_ta_uuid;
919
920 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
921 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
922 return;
923
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100924 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200925 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100926 * Run the "complete crypto test suite" using TA-to-TA
927 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200928 */
929 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100930 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
931
932 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
933 /*
934 * Run the "complete crypto test suite" using TA-to-TA
935 * communication via TA private memory.
936 */
937 xtest_crypto_test(&cs_privmem);
938 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
939
Pascal Brandc639ac82015-07-02 08:53:34 +0200940 TEEC_CloseSession(&session);
941}
942
943/*
944 * Note that this test is failing when
945 * - running twice in a raw
946 * - and the user TA is statically linked
947 * This is because the counter is not reseted when opening the first session
948 * in case the TA is statically linked
949 */
950static void xtest_tee_test_1012(ADBG_Case_t *c)
951{
952 TEEC_Session session1 = { 0 };
953 TEEC_Session session2 = { 0 };
954 uint32_t ret_orig;
955 TEEC_UUID uuid = sims_test_ta_uuid;
956
957 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
958 {
959 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
960 static const uint8_t in[] = {
961 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
962 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
963 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
964 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
965 };
966 uint8_t out[32] = { 0 };
967 int i;
968
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300969 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200970 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300971 &ret_orig)))
972 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200973
974 op.params[0].value.a = 0;
975 op.params[1].tmpref.buffer = (void *)in;
976 op.params[1].tmpref.size = sizeof(in);
977 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
978 TEEC_MEMREF_TEMP_INPUT,
979 TEEC_NONE, TEEC_NONE);
980
981 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
982 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
983 &ret_orig));
984
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100985 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300986 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200987 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300988 &ret_orig)))
989 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200990
991 op.params[0].value.a = 0;
992 op.params[1].tmpref.buffer = out;
993 op.params[1].tmpref.size = sizeof(out);
994 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
995 TEEC_MEMREF_TEMP_OUTPUT,
996 TEEC_NONE, TEEC_NONE);
997
998 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
999 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
1000 &op, &ret_orig));
1001
1002 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
1003 sizeof(out))) {
1004 Do_ADBG_Log("in:");
1005 Do_ADBG_HexLog(in, sizeof(in), 16);
1006 Do_ADBG_Log("out:");
1007 Do_ADBG_HexLog(out, sizeof(out), 16);
1008 }
1009
1010 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1011 TEEC_NONE, TEEC_NONE,
1012 TEEC_NONE);
1013
1014 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1015 TEEC_InvokeCommand(&session1,
1016 TA_SIMS_CMD_GET_COUNTER,
1017 &op, &ret_orig));
1018
1019 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
1020
1021 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1022 TEEC_InvokeCommand(&session2,
1023 TA_SIMS_CMD_GET_COUNTER, &op,
1024 &ret_orig));
1025
1026 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1027 TEEC_CloseSession(&session2);
1028 }
1029
1030 memset(out, 0, sizeof(out));
1031 op.params[0].value.a = 0;
1032 op.params[1].tmpref.buffer = out;
1033 op.params[1].tmpref.size = sizeof(out);
1034 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1035 TEEC_MEMREF_TEMP_OUTPUT,
1036 TEEC_NONE, TEEC_NONE);
1037
1038 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1039 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1040 &ret_orig));
1041
1042 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1043 Do_ADBG_Log("in:");
1044 Do_ADBG_HexLog(in, sizeof(in), 16);
1045 Do_ADBG_Log("out:");
1046 Do_ADBG_HexLog(out, sizeof(out), 16);
1047 }
1048
1049 TEEC_CloseSession(&session1);
1050 }
1051}
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001052
1053struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001054 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001055 uint32_t cmd;
1056 uint32_t repeat;
1057 TEEC_SharedMemory *shm;
1058 uint32_t error_orig;
1059 TEEC_Result res;
1060 uint32_t max_concurrency;
1061 const uint8_t *in;
1062 size_t in_len;
1063 uint8_t *out;
1064 size_t out_len;
1065};
1066
1067static void *test_1013_thread(void *arg)
1068{
1069 struct test_1013_thread_arg *a = arg;
1070 TEEC_Session session = { 0 };
1071 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1072 uint8_t p2 = TEEC_NONE;
1073 uint8_t p3 = TEEC_NONE;
1074
Jens Wiklander70672972016-04-06 00:01:45 +02001075 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001076 &a->error_orig);
1077 if (a->res != TEEC_SUCCESS)
1078 return NULL;
1079
1080 op.params[0].memref.parent = a->shm;
1081 op.params[0].memref.size = a->shm->size;
1082 op.params[0].memref.offset = 0;
1083 op.params[1].value.a = a->repeat;
1084 op.params[1].value.b = 0;
1085 op.params[2].tmpref.buffer = (void *)a->in;
1086 op.params[2].tmpref.size = a->in_len;
1087 op.params[3].tmpref.buffer = a->out;
1088 op.params[3].tmpref.size = a->out_len;
1089
1090 if (a->in_len)
1091 p2 = TEEC_MEMREF_TEMP_INPUT;
1092 if (a->out_len)
1093 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1094
1095 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1096 TEEC_VALUE_INOUT, p2, p3);
1097
1098 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1099 a->max_concurrency = op.params[1].value.b;
1100 a->out_len = op.params[3].tmpref.size;
1101 TEEC_CloseSession(&session);
1102 return NULL;
1103}
1104
Pascal Brand4fa35582015-12-17 10:59:12 +01001105#define NUM_THREADS 3
1106
Jens Wiklander70672972016-04-06 00:01:45 +02001107static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1108 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001109{
Pascal Brand4fa35582015-12-17 10:59:12 +01001110 size_t num_threads = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001111 size_t nt;
1112 size_t n;
Jens Wiklander70672972016-04-06 00:01:45 +02001113 size_t repeat = 1000;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001114 pthread_t thr[num_threads];
1115 TEEC_SharedMemory shm;
1116 size_t max_concurrency;
1117 struct test_1013_thread_arg arg[num_threads];
1118 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1119 static const uint8_t sha256_out[] = {
1120 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1121 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1122 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1123 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1124 };
1125 uint8_t out[32] = { 0 };
1126
Jens Wiklander70672972016-04-06 00:01:45 +02001127 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001128 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001129
1130 memset(&shm, 0, sizeof(shm));
1131 shm.size = sizeof(struct ta_concurrent_shm);
1132 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1133 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1134 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1135 return;
1136
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001137 memset(shm.buffer, 0, shm.size);
1138 memset(arg, 0, sizeof(arg));
1139 max_concurrency = 0;
1140 nt = num_threads;
1141
1142 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001143 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001144 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001145 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001146 arg[n].shm = &shm;
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 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1154 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1155 if (arg[n].max_concurrency > max_concurrency)
1156 max_concurrency = arg[n].max_concurrency;
1157 }
1158
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001159 /*
1160 * Concurrency can be limited by several factors, for instance in a
1161 * single CPU system it's dependent on the Preemtion Model used by
1162 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1163 * best result there).
1164 */
1165 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
1166 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, num_threads);
Pascal Brand4fa35582015-12-17 10:59:12 +01001167 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001168 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001169
Jens Wiklander70672972016-04-06 00:01:45 +02001170 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001171 memset(shm.buffer, 0, shm.size);
1172 memset(arg, 0, sizeof(arg));
1173 max_concurrency = 0;
1174 nt = num_threads;
1175
1176 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001177 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001178 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001179 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001180 arg[n].shm = &shm;
1181 arg[n].in = sha256_in;
1182 arg[n].in_len = sizeof(sha256_in);
1183 arg[n].out = out;
1184 arg[n].out_len = sizeof(out);
1185 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1186 test_1013_thread, arg + n)))
1187 nt = n; /* break loop and start cleanup */
1188 }
1189
1190 for (n = 0; n < nt; n++) {
1191 if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) &&
1192 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
1193 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1194 arg[n].out, arg[n].out_len);
1195 if (arg[n].max_concurrency > max_concurrency)
1196 max_concurrency = arg[n].max_concurrency;
1197 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001198 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001199 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001200
Pascal Brand4fa35582015-12-17 10:59:12 +01001201 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001202 TEEC_ReleaseSharedMemory(&shm);
1203}
Pascal Brand4fa35582015-12-17 10:59:12 +01001204
1205static void xtest_tee_test_1013(ADBG_Case_t *c)
1206{
1207 int i;
1208 double mean_concurrency;
1209 double concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001210 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001211
1212 if (level == 0)
1213 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001214
Jens Wiklander70672972016-04-06 00:01:45 +02001215 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001216 mean_concurrency = 0;
1217 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001218 xtest_tee_test_1013_single(c, &concurrency,
1219 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001220 mean_concurrency += concurrency;
1221 }
1222 mean_concurrency /= nb_loops;
1223
1224 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1225 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001226 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001227
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001228#ifndef CFG_PAGED_USER_TA
Jens Wiklander70672972016-04-06 00:01:45 +02001229 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1230 mean_concurrency = 0;
1231 for (i = 0; i < nb_loops; i++) {
1232 xtest_tee_test_1013_single(c, &concurrency,
1233 &concurrent_large_ta_uuid);
1234 mean_concurrency += concurrency;
1235 }
1236 mean_concurrency /= nb_loops;
1237
1238 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1239 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1240 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001241#endif
Jens Wiklander70672972016-04-06 00:01:45 +02001242}
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001243
1244#ifdef CFG_SECURE_DATA_PATH
1245static void xtest_tee_test_1014(ADBG_Case_t *c)
1246{
1247 UNUSED(c);
1248
1249 int size = 17000;
1250 int loop = 10;
1251 int ion_heap = DEFAULT_ION_HEAP_TYPE;
1252 int rnd_offset = 1;
1253 int test;
1254 int ret;
1255
1256 test = TEST_NS_TO_TA;
1257 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001258 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001259 ADBG_EXPECT(c, 0, ret);
1260 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1261
1262 test = TEST_TA_TO_TA;
1263 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001264 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001265 ADBG_EXPECT(c, 0, ret);
1266 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1267
1268 test = TEST_TA_TO_PTA;
1269 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP pTA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001270 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001271 ADBG_EXPECT(c, 0, ret);
1272 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP pTA");
1273
1274 test = TEST_NS_TO_PTA;
Etienne Carrierea3198522017-10-26 09:48:55 +02001275 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001276 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001277 ADBG_EXPECT(c, 1, ret);
Etienne Carrierea3198522017-10-26 09:48:55 +02001278 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001279}
1280#endif
Jens Wiklander272d3642017-04-03 13:03:47 +02001281
1282static void xtest_tee_test_1015(ADBG_Case_t *c)
1283{
1284 TEEC_Result res;
1285 TEEC_Session session = { 0 };
1286 uint32_t ret_orig;
1287
Etienne Carriere11093162017-10-26 09:49:04 +02001288 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001289 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1290 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001291 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1292 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001293 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001294 }
1295 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001296
1297 ADBG_EXPECT_TEEC_SUCCESS(c,
1298 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1299 NULL, &ret_orig));
1300 TEEC_CloseSession(&session);
1301}
Jerome Forissiere916b102017-06-07 17:55:52 +02001302
1303static void xtest_tee_test_1016(ADBG_Case_t *c)
1304{
1305 TEEC_Session session = { 0 };
1306 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1307 uint32_t ret_orig;
1308
1309 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1310 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1311 &ret_orig)))
1312 return;
1313
1314 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1315 TEEC_NONE);
1316
1317 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1318 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1319 &ret_orig));
1320
1321 TEEC_CloseSession(&session);
1322}
Jens Wiklander87e81702018-03-20 12:00:00 +08001323
1324static void xtest_tee_test_1017(ADBG_Case_t *c)
1325{
1326 TEEC_Session session = { 0 };
1327 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1328 uint32_t ret_orig;
1329 TEEC_SharedMemory shm;
1330 size_t page_size = 4096;
1331
1332 memset(&shm, 0, sizeof(shm));
1333 shm.size = 8 * page_size;
1334 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1335 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1336 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1337 return;
1338
1339 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1340 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1341 &ret_orig)))
1342 goto out;
1343
1344 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1345 TEEC_MEMREF_PARTIAL_INPUT,
1346 TEEC_MEMREF_PARTIAL_OUTPUT,
1347 TEEC_MEMREF_PARTIAL_OUTPUT);
1348
1349 /*
1350 * The first two memrefs are supposed to be combined into in
1351 * region and the last two memrefs should have one region each
1352 * when the parameters are mapped for the TA.
1353 */
1354 op.params[0].memref.parent = &shm;
1355 op.params[0].memref.size = page_size;
1356 op.params[0].memref.offset = 0;
1357
1358 op.params[1].memref.parent = &shm;
1359 op.params[1].memref.size = page_size;
1360 op.params[1].memref.offset = page_size;
1361
1362 op.params[2].memref.parent = &shm;
1363 op.params[2].memref.size = page_size;
1364 op.params[2].memref.offset = 4 * page_size;
1365
1366 op.params[3].memref.parent = &shm;
1367 op.params[3].memref.size = 2 * page_size;
1368 op.params[3].memref.offset = 6 * page_size;
1369
1370 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1371 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1372 &ret_orig));
1373
1374 TEEC_CloseSession(&session);
1375out:
1376 TEEC_ReleaseSharedMemory(&shm);
1377}
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001378
1379static void xtest_tee_test_1018(ADBG_Case_t *c)
1380{
1381 TEEC_Session session = { 0 };
1382 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1383 uint32_t ret_orig;
1384 TEEC_SharedMemory shm;
1385 size_t page_size = 4096;
1386
1387 memset(&shm, 0, sizeof(shm));
1388 shm.size = 8 * page_size;
1389 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1390 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1391 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1392 return;
1393
1394 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1395 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1396 &ret_orig)))
1397 goto out;
1398
1399 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1400 TEEC_MEMREF_PARTIAL_INPUT,
1401 TEEC_MEMREF_PARTIAL_OUTPUT,
1402 TEEC_MEMREF_PARTIAL_OUTPUT);
1403
1404 /*
1405 * The first two memrefs are supposed to be combined into in
1406 * region and the last two memrefs should have one region each
1407 * when the parameters are mapped for the TA.
1408 */
1409 op.params[0].memref.parent = &shm;
1410 op.params[0].memref.size = page_size;
1411 op.params[0].memref.offset = 0;
1412
1413 op.params[1].memref.parent = &shm;
1414 op.params[1].memref.size = page_size;
1415 op.params[1].memref.offset = page_size;
1416
1417 op.params[2].memref.parent = &shm;
1418 op.params[2].memref.size = page_size;
1419 op.params[2].memref.offset = 4 * page_size;
1420
1421 op.params[3].memref.parent = &shm;
1422 op.params[3].memref.size = 3 * page_size;
1423 op.params[3].memref.offset = 6 * page_size;
1424
1425 /*
1426 * Depending on the tee driver we may have different error codes.
1427 * What's most important is that secure world doesn't panic and
1428 * that someone detects an error.
1429 */
1430 ADBG_EXPECT_NOT(c, TEE_SUCCESS,
1431 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1432 &ret_orig));
1433
1434 TEEC_CloseSession(&session);
1435out:
1436 TEEC_ReleaseSharedMemory(&shm);
1437}