blob: e02a4aff2a52a6d34f3225bf87e98c4850371887 [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
500#ifndef TEEC_ERROR_TARGET_DEAD
501/* To be removed when we have TEEC_ERROR_TARGET_DEAD from tee_client_api.h */
502#define TEEC_ERROR_TARGET_DEAD 0xFFFF3024
503#endif
504
Etienne Carriere92c34422018-02-09 13:11:40 +0100505static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200506{
507 TEEC_Session session = { 0 };
508 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
509 uint32_t ret_orig;
510
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300511 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200512 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300513 &ret_orig)))
514 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200515
516 op.params[0].value.a = n;
517 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
518 TEEC_NONE);
519
520 (void)ADBG_EXPECT_TEEC_RESULT(c,
521 TEEC_ERROR_TARGET_DEAD,
522 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
523 &ret_orig));
524
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300525 (void)ADBG_EXPECT_TEEC_RESULT(c,
526 TEEC_ERROR_TARGET_DEAD,
527 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200528 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300529
Pascal Brandc639ac82015-07-02 08:53:34 +0200530 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
531
532 TEEC_CloseSession(&session);
533}
534
Etienne Carriere92c34422018-02-09 13:11:40 +0100535static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
536 size_t size)
537{
538 TEEC_Session session = { 0 };
539 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
540 uint32_t ret_orig;
541 TEEC_SharedMemory shm;
542
543 memset(&shm, 0, sizeof(shm));
544 shm.size = size;
545 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
546 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
547 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
548 return;
549
550 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
551 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
552 &ret_orig)))
553 return;
554
555 op.params[0].value.a = (uint32_t)n;
556 op.params[1].memref.parent = &shm;
557 op.params[1].memref.size = size;
558 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
559 TEEC_NONE, TEEC_NONE);
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_RESULT(c,
567 TEEC_ERROR_TARGET_DEAD,
568 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
569 &ret_orig));
570
571 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
572
573 TEEC_CloseSession(&session);
574}
575
Pascal Brandc639ac82015-07-02 08:53:34 +0200576static void xtest_tee_test_1005(ADBG_Case_t *c)
577{
578 uint32_t ret_orig;
579#define MAX_SESSIONS 3
580 TEEC_Session sessions[MAX_SESSIONS];
581 int i;
582
583 for (i = 0; i < MAX_SESSIONS; i++) {
584 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200585 xtest_teec_open_session(&sessions[i],
586 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200587 NULL, &ret_orig)))
588 break;
589 }
590
591 for (; --i >= 0; )
592 TEEC_CloseSession(&sessions[i]);
593}
594
595static void xtest_tee_test_1006(ADBG_Case_t *c)
596{
597 TEEC_Session session = { 0 };
598 uint32_t ret_orig;
599 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
600 uint8_t buf[32];
601
602 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
603 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
604 &ret_orig)))
605 return;
606
607 op.params[0].tmpref.buffer = buf;
608 op.params[0].tmpref.size = sizeof(buf);
609 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
610 TEEC_NONE, TEEC_NONE);
611
612 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
613 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
614 &ret_orig));
615
616 TEEC_CloseSession(&session);
617}
618
619static void xtest_tee_test_1007(ADBG_Case_t *c)
620{
621 TEEC_Session session = { 0 };
622 uint32_t ret_orig;
623
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300624 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200625 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300626 &ret_orig)))
627 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200628
629 (void)ADBG_EXPECT_TEEC_RESULT(c,
630 TEEC_ERROR_TARGET_DEAD,
631 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
632 &ret_orig));
633
634 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
635
636 (void)ADBG_EXPECT_TEEC_RESULT(c,
637 TEEC_ERROR_TARGET_DEAD,
638 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
639 &ret_orig));
640
641 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
642
643 TEEC_CloseSession(&session);
644}
645
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100646#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jerome Forissierf02a2212015-10-29 14:33:35 +0100647#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000648# ifdef __ANDROID__
649#define TA_DIR "/system/lib/optee_armtz"
650# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100651#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000652# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100653#endif
654
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100655static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600656{
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100657 char buf[PATH_MAX];
David Brownb2865ab2016-08-02 11:44:41 -0600658
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100659 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100660 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100661 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200662 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
663 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
664 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600665 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200666
Jens Wiklanderb7940892015-10-23 16:02:40 +0200667 return fopen(buf, mode);
668}
669
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100670static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200671{
672 TEEC_Session session = { 0 };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100673 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
674 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200675 TEEC_Result res;
676 uint32_t ret_orig;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100677 FILE *f = NULL;
678 bool r = false;
679 uint8_t *buf = NULL;
680 size_t sz;
681 size_t fread_res;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200682
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100683 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
684 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
685 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200686
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100687 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
688 if (!ADBG_EXPECT_NOT_NULL(c, f))
689 goto out;
690 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
691 goto out;
692 sz = ftell(f);
693 rewind(f);
694
695 buf = malloc(sz);
696 if (!ADBG_EXPECT_NOT_NULL(c, buf))
697 goto out;
698
699 fread_res = fread(buf, 1, sz, f);
700 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
701 goto out;
702
Jens Wiklander4441fe22015-10-23 16:53:02 +0200703 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100704 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200705
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100706 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200707
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100708 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
709 TEEC_NONE, TEEC_NONE);
710 op.params[0].tmpref.buffer = buf;
711 op.params[0].tmpref.size = sz;
712
713 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
714 &ret_orig);
715 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
716out:
717 free(buf);
718 if (f)
719 fclose(f);
720 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200721 return r;
722}
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100723#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Jens Wiklander4441fe22015-10-23 16:53:02 +0200724
Pascal Brandc639ac82015-07-02 08:53:34 +0200725static void xtest_tee_test_1008(ADBG_Case_t *c)
726{
727 TEEC_Session session = { 0 };
728 TEEC_Session session_crypt = { 0 };
729 uint32_t ret_orig;
730
731 Do_ADBG_BeginSubCase(c, "Invoke command");
732 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300733 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200734 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300735 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200736
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300737 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
738 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
739 NULL, &ret_orig));
740 TEEC_CloseSession(&session);
741 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200742
Pascal Brandc639ac82015-07-02 08:53:34 +0200743 }
744 Do_ADBG_EndSubCase(c, "Invoke command");
745
746 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
747 {
748 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
749
750 op.params[0].value.a = 2000;
751 op.paramTypes = TEEC_PARAM_TYPES(
752 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
753
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300754 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200755 xtest_teec_open_session(&session,
756 &os_test_ta_uuid,
757 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300758 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200759
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300760 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
761 TEEC_InvokeCommand(&session,
762 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
763 &op, &ret_orig));
764 TEEC_CloseSession(&session);
765 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200766 }
767 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
768
769 Do_ADBG_BeginSubCase(c, "Create session fail");
770 {
Jens Wiklanderb7940892015-10-23 16:02:40 +0200771 size_t n;
772
Pascal Brandc639ac82015-07-02 08:53:34 +0200773 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
774 xtest_teec_open_session(&session_crypt,
775 &create_fail_test_ta_uuid, NULL,
776 &ret_orig));
Pascal Brandc639ac82015-07-02 08:53:34 +0200777 /*
778 * Run this several times to see that there's no memory leakage.
779 */
780 for (n = 0; n < 100; n++) {
781 Do_ADBG_Log("n = %zu", n);
782 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
783 xtest_teec_open_session(&session_crypt,
784 &create_fail_test_ta_uuid,
785 NULL, &ret_orig));
786 }
787 }
788 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200789
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100790#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jens Wiklander4441fe22015-10-23 16:53:02 +0200791 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
792 ADBG_EXPECT_TRUE(c,
793 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
794 ADBG_EXPECT_TRUE(c,
795 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
796 ADBG_EXPECT_TRUE(c,
797 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
798 ADBG_EXPECT_TRUE(c,
799 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
800 ADBG_EXPECT_TRUE(c,
801 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
802 ADBG_EXPECT_TRUE(c,
803 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
804 ADBG_EXPECT_TRUE(c,
805 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
806 ADBG_EXPECT_TRUE(c,
807 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
808 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
809 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 30000, 1)); /* payload */
810 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100811#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Pascal Brandc639ac82015-07-02 08:53:34 +0200812}
813
Pascal Brandc639ac82015-07-02 08:53:34 +0200814static void *cancellation_thread(void *arg)
815{
816 /*
817 * Sleep 0.5 seconds before cancellation to make sure that the other
818 * thread is in RPC_WAIT.
819 */
820 (void)usleep(500000);
821 TEEC_RequestCancellation(arg);
822 return NULL;
823}
Pascal Brandc639ac82015-07-02 08:53:34 +0200824
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300825static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
826 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200827{
828 TEEC_Session session = { 0 };
829 uint32_t ret_orig;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300830 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200831
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300832 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200833 {
834 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
835
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300836 if (ADBG_EXPECT_TEEC_SUCCESS(c,
837 xtest_teec_open_session(&session, &os_test_ta_uuid,
838 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200839
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300840 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
841 TEEC_ORIGIN_TRUSTED_APP,
842 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200843
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300844 op.params[0].value.a = timeout;
845 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
846 TEEC_NONE,
847 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300848 if (cancel) {
849 (void)ADBG_EXPECT(c, 0,
850 pthread_create(&thr, NULL,
851 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200852
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300853 (void)ADBG_EXPECT_TEEC_RESULT(c,
854 TEEC_ERROR_CANCEL,
855 TEEC_InvokeCommand(&session,
856 TA_OS_TEST_CMD_WAIT,
857 &op,
858 &ret_orig));
859 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300860
861 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
862 TEEC_InvokeCommand(&session,
863 TA_OS_TEST_CMD_WAIT,
864 &op,
865 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300866 if (cancel)
867 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300868
869 TEEC_CloseSession(&session);
870 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200871 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300872 Do_ADBG_EndSubCase(c, "%s", subcase);
873}
874
875static void xtest_tee_test_1009(ADBG_Case_t *c)
876{
877 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
878 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300879 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300880 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200881}
882
883static void xtest_tee_test_1010(ADBG_Case_t *c)
884{
Etienne Carriere92c34422018-02-09 13:11:40 +0100885 unsigned int n;
886 unsigned int idx;
887 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200888
889 for (n = 1; n <= 5; n++) {
890 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
891 xtest_tee_test_invalid_mem_access(c, n);
892 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
893 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100894
895 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
896 for (n = 1; n <= 5; n++) {
897 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200898 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100899 n, memref_sz[idx]);
900 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
901 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200902 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100903 n, memref_sz[idx]);
904 }
905 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200906}
907
908static void xtest_tee_test_1011(ADBG_Case_t *c)
909{
910 TEEC_Session session = { 0 };
911 uint32_t ret_orig;
912 struct xtest_crypto_session cs = {
913 c, &session, TA_RPC_CMD_CRYPT_SHA256,
914 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
915 TA_RPC_CMD_CRYPT_AES256ECB_DEC
916 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100917 struct xtest_crypto_session cs_privmem = {
918 c, &session,
919 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
920 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
921 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
922 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200923 TEEC_UUID uuid = rpc_test_ta_uuid;
924
925 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
926 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
927 return;
928
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100929 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200930 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100931 * Run the "complete crypto test suite" using TA-to-TA
932 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200933 */
934 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100935 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
936
937 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
938 /*
939 * Run the "complete crypto test suite" using TA-to-TA
940 * communication via TA private memory.
941 */
942 xtest_crypto_test(&cs_privmem);
943 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
944
Pascal Brandc639ac82015-07-02 08:53:34 +0200945 TEEC_CloseSession(&session);
946}
947
948/*
949 * Note that this test is failing when
950 * - running twice in a raw
951 * - and the user TA is statically linked
952 * This is because the counter is not reseted when opening the first session
953 * in case the TA is statically linked
954 */
955static void xtest_tee_test_1012(ADBG_Case_t *c)
956{
957 TEEC_Session session1 = { 0 };
958 TEEC_Session session2 = { 0 };
959 uint32_t ret_orig;
960 TEEC_UUID uuid = sims_test_ta_uuid;
961
962 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
963 {
964 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
965 static const uint8_t in[] = {
966 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
967 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
968 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
969 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
970 };
971 uint8_t out[32] = { 0 };
972 int i;
973
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300974 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200975 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300976 &ret_orig)))
977 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200978
979 op.params[0].value.a = 0;
980 op.params[1].tmpref.buffer = (void *)in;
981 op.params[1].tmpref.size = sizeof(in);
982 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
983 TEEC_MEMREF_TEMP_INPUT,
984 TEEC_NONE, TEEC_NONE);
985
986 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
987 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
988 &ret_orig));
989
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100990 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300991 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200992 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300993 &ret_orig)))
994 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200995
996 op.params[0].value.a = 0;
997 op.params[1].tmpref.buffer = out;
998 op.params[1].tmpref.size = sizeof(out);
999 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1000 TEEC_MEMREF_TEMP_OUTPUT,
1001 TEEC_NONE, TEEC_NONE);
1002
1003 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1004 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
1005 &op, &ret_orig));
1006
1007 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
1008 sizeof(out))) {
1009 Do_ADBG_Log("in:");
1010 Do_ADBG_HexLog(in, sizeof(in), 16);
1011 Do_ADBG_Log("out:");
1012 Do_ADBG_HexLog(out, sizeof(out), 16);
1013 }
1014
1015 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1016 TEEC_NONE, TEEC_NONE,
1017 TEEC_NONE);
1018
1019 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1020 TEEC_InvokeCommand(&session1,
1021 TA_SIMS_CMD_GET_COUNTER,
1022 &op, &ret_orig));
1023
1024 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
1025
1026 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1027 TEEC_InvokeCommand(&session2,
1028 TA_SIMS_CMD_GET_COUNTER, &op,
1029 &ret_orig));
1030
1031 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1032 TEEC_CloseSession(&session2);
1033 }
1034
1035 memset(out, 0, sizeof(out));
1036 op.params[0].value.a = 0;
1037 op.params[1].tmpref.buffer = out;
1038 op.params[1].tmpref.size = sizeof(out);
1039 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1040 TEEC_MEMREF_TEMP_OUTPUT,
1041 TEEC_NONE, TEEC_NONE);
1042
1043 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1044 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1045 &ret_orig));
1046
1047 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1048 Do_ADBG_Log("in:");
1049 Do_ADBG_HexLog(in, sizeof(in), 16);
1050 Do_ADBG_Log("out:");
1051 Do_ADBG_HexLog(out, sizeof(out), 16);
1052 }
1053
1054 TEEC_CloseSession(&session1);
1055 }
1056}
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001057
1058struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001059 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001060 uint32_t cmd;
1061 uint32_t repeat;
1062 TEEC_SharedMemory *shm;
1063 uint32_t error_orig;
1064 TEEC_Result res;
1065 uint32_t max_concurrency;
1066 const uint8_t *in;
1067 size_t in_len;
1068 uint8_t *out;
1069 size_t out_len;
1070};
1071
1072static void *test_1013_thread(void *arg)
1073{
1074 struct test_1013_thread_arg *a = arg;
1075 TEEC_Session session = { 0 };
1076 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1077 uint8_t p2 = TEEC_NONE;
1078 uint8_t p3 = TEEC_NONE;
1079
Jens Wiklander70672972016-04-06 00:01:45 +02001080 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001081 &a->error_orig);
1082 if (a->res != TEEC_SUCCESS)
1083 return NULL;
1084
1085 op.params[0].memref.parent = a->shm;
1086 op.params[0].memref.size = a->shm->size;
1087 op.params[0].memref.offset = 0;
1088 op.params[1].value.a = a->repeat;
1089 op.params[1].value.b = 0;
1090 op.params[2].tmpref.buffer = (void *)a->in;
1091 op.params[2].tmpref.size = a->in_len;
1092 op.params[3].tmpref.buffer = a->out;
1093 op.params[3].tmpref.size = a->out_len;
1094
1095 if (a->in_len)
1096 p2 = TEEC_MEMREF_TEMP_INPUT;
1097 if (a->out_len)
1098 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1099
1100 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1101 TEEC_VALUE_INOUT, p2, p3);
1102
1103 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1104 a->max_concurrency = op.params[1].value.b;
1105 a->out_len = op.params[3].tmpref.size;
1106 TEEC_CloseSession(&session);
1107 return NULL;
1108}
1109
Pascal Brand4fa35582015-12-17 10:59:12 +01001110#define NUM_THREADS 3
1111
Jens Wiklander70672972016-04-06 00:01:45 +02001112static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1113 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001114{
Pascal Brand4fa35582015-12-17 10:59:12 +01001115 size_t num_threads = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001116 size_t nt;
1117 size_t n;
Jens Wiklander70672972016-04-06 00:01:45 +02001118 size_t repeat = 1000;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001119 pthread_t thr[num_threads];
1120 TEEC_SharedMemory shm;
1121 size_t max_concurrency;
1122 struct test_1013_thread_arg arg[num_threads];
1123 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1124 static const uint8_t sha256_out[] = {
1125 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1126 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1127 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1128 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1129 };
1130 uint8_t out[32] = { 0 };
1131
Jens Wiklander70672972016-04-06 00:01:45 +02001132 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001133 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001134
1135 memset(&shm, 0, sizeof(shm));
1136 shm.size = sizeof(struct ta_concurrent_shm);
1137 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1138 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1139 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1140 return;
1141
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001142 memset(shm.buffer, 0, shm.size);
1143 memset(arg, 0, sizeof(arg));
1144 max_concurrency = 0;
1145 nt = num_threads;
1146
1147 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001148 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001149 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001150 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001151 arg[n].shm = &shm;
1152 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1153 test_1013_thread, arg + n)))
1154 nt = n; /* break loop and start cleanup */
1155 }
1156
1157 for (n = 0; n < nt; n++) {
1158 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1159 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1160 if (arg[n].max_concurrency > max_concurrency)
1161 max_concurrency = arg[n].max_concurrency;
1162 }
1163
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001164 /*
1165 * Concurrency can be limited by several factors, for instance in a
1166 * single CPU system it's dependent on the Preemtion Model used by
1167 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1168 * best result there).
1169 */
1170 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
1171 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, num_threads);
Pascal Brand4fa35582015-12-17 10:59:12 +01001172 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001173 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001174
Jens Wiklander70672972016-04-06 00:01:45 +02001175 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001176 memset(shm.buffer, 0, shm.size);
1177 memset(arg, 0, sizeof(arg));
1178 max_concurrency = 0;
1179 nt = num_threads;
1180
1181 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001182 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001183 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001184 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001185 arg[n].shm = &shm;
1186 arg[n].in = sha256_in;
1187 arg[n].in_len = sizeof(sha256_in);
1188 arg[n].out = out;
1189 arg[n].out_len = sizeof(out);
1190 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1191 test_1013_thread, arg + n)))
1192 nt = n; /* break loop and start cleanup */
1193 }
1194
1195 for (n = 0; n < nt; n++) {
1196 if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) &&
1197 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
1198 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1199 arg[n].out, arg[n].out_len);
1200 if (arg[n].max_concurrency > max_concurrency)
1201 max_concurrency = arg[n].max_concurrency;
1202 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001203 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001204 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001205
Pascal Brand4fa35582015-12-17 10:59:12 +01001206 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001207 TEEC_ReleaseSharedMemory(&shm);
1208}
Pascal Brand4fa35582015-12-17 10:59:12 +01001209
1210static void xtest_tee_test_1013(ADBG_Case_t *c)
1211{
1212 int i;
1213 double mean_concurrency;
1214 double concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001215 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001216
1217 if (level == 0)
1218 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001219
Jens Wiklander70672972016-04-06 00:01:45 +02001220 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001221 mean_concurrency = 0;
1222 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001223 xtest_tee_test_1013_single(c, &concurrency,
1224 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001225 mean_concurrency += concurrency;
1226 }
1227 mean_concurrency /= nb_loops;
1228
1229 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1230 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001231 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001232
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001233#ifndef CFG_PAGED_USER_TA
Jens Wiklander70672972016-04-06 00:01:45 +02001234 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1235 mean_concurrency = 0;
1236 for (i = 0; i < nb_loops; i++) {
1237 xtest_tee_test_1013_single(c, &concurrency,
1238 &concurrent_large_ta_uuid);
1239 mean_concurrency += concurrency;
1240 }
1241 mean_concurrency /= nb_loops;
1242
1243 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1244 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1245 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001246#endif
Jens Wiklander70672972016-04-06 00:01:45 +02001247}
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001248
1249#ifdef CFG_SECURE_DATA_PATH
1250static void xtest_tee_test_1014(ADBG_Case_t *c)
1251{
1252 UNUSED(c);
1253
1254 int size = 17000;
1255 int loop = 10;
1256 int ion_heap = DEFAULT_ION_HEAP_TYPE;
1257 int rnd_offset = 1;
1258 int test;
1259 int ret;
1260
1261 test = TEST_NS_TO_TA;
1262 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001263 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001264 ADBG_EXPECT(c, 0, ret);
1265 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1266
1267 test = TEST_TA_TO_TA;
1268 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001269 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001270 ADBG_EXPECT(c, 0, ret);
1271 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1272
1273 test = TEST_TA_TO_PTA;
1274 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP pTA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001275 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001276 ADBG_EXPECT(c, 0, ret);
1277 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP pTA");
1278
1279 test = TEST_NS_TO_PTA;
Etienne Carrierea3198522017-10-26 09:48:55 +02001280 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001281 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001282 ADBG_EXPECT(c, 1, ret);
Etienne Carrierea3198522017-10-26 09:48:55 +02001283 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001284}
1285#endif
Jens Wiklander272d3642017-04-03 13:03:47 +02001286
1287static void xtest_tee_test_1015(ADBG_Case_t *c)
1288{
1289 TEEC_Result res;
1290 TEEC_Session session = { 0 };
1291 uint32_t ret_orig;
1292
Etienne Carriere11093162017-10-26 09:49:04 +02001293 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001294 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1295 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001296 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1297 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001298 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001299 }
1300 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001301
1302 ADBG_EXPECT_TEEC_SUCCESS(c,
1303 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1304 NULL, &ret_orig));
1305 TEEC_CloseSession(&session);
1306}
Jerome Forissiere916b102017-06-07 17:55:52 +02001307
1308static void xtest_tee_test_1016(ADBG_Case_t *c)
1309{
1310 TEEC_Session session = { 0 };
1311 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1312 uint32_t ret_orig;
1313
1314 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1315 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1316 &ret_orig)))
1317 return;
1318
1319 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1320 TEEC_NONE);
1321
1322 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1323 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1324 &ret_orig));
1325
1326 TEEC_CloseSession(&session);
1327}
Jens Wiklander87e81702018-03-20 12:00:00 +08001328
1329static void xtest_tee_test_1017(ADBG_Case_t *c)
1330{
1331 TEEC_Session session = { 0 };
1332 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1333 uint32_t ret_orig;
1334 TEEC_SharedMemory shm;
1335 size_t page_size = 4096;
1336
1337 memset(&shm, 0, sizeof(shm));
1338 shm.size = 8 * page_size;
1339 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1340 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1341 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1342 return;
1343
1344 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1345 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1346 &ret_orig)))
1347 goto out;
1348
1349 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1350 TEEC_MEMREF_PARTIAL_INPUT,
1351 TEEC_MEMREF_PARTIAL_OUTPUT,
1352 TEEC_MEMREF_PARTIAL_OUTPUT);
1353
1354 /*
1355 * The first two memrefs are supposed to be combined into in
1356 * region and the last two memrefs should have one region each
1357 * when the parameters are mapped for the TA.
1358 */
1359 op.params[0].memref.parent = &shm;
1360 op.params[0].memref.size = page_size;
1361 op.params[0].memref.offset = 0;
1362
1363 op.params[1].memref.parent = &shm;
1364 op.params[1].memref.size = page_size;
1365 op.params[1].memref.offset = page_size;
1366
1367 op.params[2].memref.parent = &shm;
1368 op.params[2].memref.size = page_size;
1369 op.params[2].memref.offset = 4 * page_size;
1370
1371 op.params[3].memref.parent = &shm;
1372 op.params[3].memref.size = 2 * page_size;
1373 op.params[3].memref.offset = 6 * page_size;
1374
1375 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1376 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1377 &ret_orig));
1378
1379 TEEC_CloseSession(&session);
1380out:
1381 TEEC_ReleaseSharedMemory(&shm);
1382}
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001383
1384static void xtest_tee_test_1018(ADBG_Case_t *c)
1385{
1386 TEEC_Session session = { 0 };
1387 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1388 uint32_t ret_orig;
1389 TEEC_SharedMemory shm;
1390 size_t page_size = 4096;
1391
1392 memset(&shm, 0, sizeof(shm));
1393 shm.size = 8 * page_size;
1394 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1395 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1396 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1397 return;
1398
1399 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1400 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1401 &ret_orig)))
1402 goto out;
1403
1404 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1405 TEEC_MEMREF_PARTIAL_INPUT,
1406 TEEC_MEMREF_PARTIAL_OUTPUT,
1407 TEEC_MEMREF_PARTIAL_OUTPUT);
1408
1409 /*
1410 * The first two memrefs are supposed to be combined into in
1411 * region and the last two memrefs should have one region each
1412 * when the parameters are mapped for the TA.
1413 */
1414 op.params[0].memref.parent = &shm;
1415 op.params[0].memref.size = page_size;
1416 op.params[0].memref.offset = 0;
1417
1418 op.params[1].memref.parent = &shm;
1419 op.params[1].memref.size = page_size;
1420 op.params[1].memref.offset = page_size;
1421
1422 op.params[2].memref.parent = &shm;
1423 op.params[2].memref.size = page_size;
1424 op.params[2].memref.offset = 4 * page_size;
1425
1426 op.params[3].memref.parent = &shm;
1427 op.params[3].memref.size = 3 * page_size;
1428 op.params[3].memref.offset = 6 * page_size;
1429
1430 /*
1431 * Depending on the tee driver we may have different error codes.
1432 * What's most important is that secure world doesn't panic and
1433 * that someone detects an error.
1434 */
1435 ADBG_EXPECT_NOT(c, TEE_SUCCESS,
1436 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1437 &ret_orig));
1438
1439 TEEC_CloseSession(&session);
1440out:
1441 TEEC_ReleaseSharedMemory(&shm);
1442}