blob: 9d701a142e443817ac959235f59e83c16cc516f6 [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);
Jerome Forissier53bde722018-05-31 09:14:54 +020064#if defined(CFG_TA_DYNLINK) && !defined(__ANDROID__)
65static void xtest_tee_test_1019(ADBG_Case_t *Case_p);
66#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020067
Jens Wiklander74abfe32017-01-03 14:17:47 +010068ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Jens Wiklander1d70a112017-10-16 15:16:39 +020069ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander0c86bc32017-11-13 19:52:03 +010070ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
71 "Core internal read/write mutex");
Jens Wiklander74abfe32017-01-03 14:17:47 +010072ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
73ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
74ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
75 "Test Basic OS features");
76ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
77ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
Jens Wiklander25a57fe2016-12-26 21:46:24 +010078 "TEE internal client API");
Jens Wiklander74abfe32017-01-03 14:17:47 +010079ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
80ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
81 "Invalid memory access");
82ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
Jens Wiklanderf7b9c632017-01-03 17:32:26 +010083 "Test TA-to-TA features with User Crypt TA");
Jens Wiklander74abfe32017-01-03 14:17:47 +010084ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
Jens Wiklander25a57fe2016-12-26 21:46:24 +010085 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklander74abfe32017-01-03 14:17:47 +010086ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Jens Wiklander25a57fe2016-12-26 21:46:24 +010087 "Test concurency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +010088#ifdef CFG_SECURE_DATA_PATH
89ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
90 "Test secure data path against SDP TAs and pTAs");
91#endif
Jens Wiklander272d3642017-04-03 13:03:47 +020092ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
93 "FS hash-tree corner cases");
Jerome Forissiere916b102017-06-07 17:55:52 +020094ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
95 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +080096ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
97 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +080098ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
99 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +0200100#if defined(CFG_TA_DYNLINK) && !defined(__ANDROID__)
101ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
102 "Test dynamically linked TA");
103#endif
Jens Wiklanderac27ec12015-07-15 15:23:14 +0200104
Pascal Brandc639ac82015-07-02 08:53:34 +0200105struct xtest_crypto_session {
106 ADBG_Case_t *c;
107 TEEC_Session *session;
108 uint32_t cmd_id_sha256;
109 uint32_t cmd_id_aes256ecb_encrypt;
110 uint32_t cmd_id_aes256ecb_decrypt;
111};
112
113static void xtest_crypto_test(struct xtest_crypto_session *cs)
114{
115 uint32_t ret_orig;
116 uint8_t crypt_out[16];
117 uint8_t crypt_in[16] = { 22, 17 };
118
119 crypt_in[15] = 60;
120
121 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
122 {
123 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
124
125 op.params[0].tmpref.buffer = crypt_in;
126 op.params[0].tmpref.size = sizeof(crypt_in);
127 op.params[1].tmpref.buffer = crypt_out;
128 op.params[1].tmpref.size = sizeof(crypt_out);
129 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
130 TEEC_MEMREF_TEMP_OUTPUT,
131 TEEC_NONE, TEEC_NONE);
132
133 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
134 TEEC_InvokeCommand(cs->session,
135 cs->
136 cmd_id_aes256ecb_encrypt,
137 &op,
138 &ret_orig));
139 }
140 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
141
142 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
143 {
144 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
145 uint8_t out[16];
146
147 op.params[0].tmpref.buffer = crypt_out;
148 op.params[0].tmpref.size = sizeof(crypt_out);
149 op.params[1].tmpref.buffer = out;
150 op.params[1].tmpref.size = sizeof(out);
151 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
152 TEEC_MEMREF_TEMP_OUTPUT,
153 TEEC_NONE, TEEC_NONE);
154
155 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
156 TEEC_InvokeCommand(cs->session,
157 cs->
158 cmd_id_aes256ecb_decrypt,
159 &op,
160 &ret_orig));
161
162 if (!ADBG_EXPECT(cs->c, 0,
163 memcmp(crypt_in, out, sizeof(crypt_in)))) {
164 Do_ADBG_Log("crypt_in:");
165 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
166 Do_ADBG_Log("out:");
167 Do_ADBG_HexLog(out, sizeof(out), 16);
168 }
169 }
170 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
171
172 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
173 {
174 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
175 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
176 static const uint8_t sha256_out[] = {
177 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
178 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
179 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
180 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
181 };
182 uint8_t out[32] = { 0 };
183
184 op.params[0].tmpref.buffer = (void *)sha256_in;
185 op.params[0].tmpref.size = sizeof(sha256_in);
186 op.params[1].tmpref.buffer = out;
187 op.params[1].tmpref.size = sizeof(out);
188 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
189 TEEC_MEMREF_TEMP_OUTPUT,
190 TEEC_NONE, TEEC_NONE);
191
192 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
193 TEEC_InvokeCommand(cs->session,
194 cs->
195 cmd_id_sha256,
196 &op,
197 &ret_orig));
198
199 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
200 sizeof(sha256_out)))) {
201 Do_ADBG_Log("sha256_out:");
202 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
203 Do_ADBG_Log("out:");
204 Do_ADBG_HexLog(out, sizeof(out), 16);
205 }
206 }
207 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
208
Etienne Carrierea3198522017-10-26 09:48:55 +0200209 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200210 {
211 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
212 static const uint8_t in[] = {
213 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
214 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
215 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
216 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
217 };
218 static const uint8_t exp_out[] = {
219 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
220 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
221 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
222 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
223 };
224 uint8_t out[sizeof(exp_out)];
225
226 op.params[0].tmpref.buffer = (void *)in;
227 op.params[0].tmpref.size = sizeof(in);
228 op.params[1].tmpref.buffer = out;
229 op.params[1].tmpref.size = sizeof(out);
230 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
231 TEEC_MEMREF_TEMP_OUTPUT,
232 TEEC_NONE, TEEC_NONE);
233
234 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
235 TEEC_InvokeCommand(cs->session,
236 cs->
237 cmd_id_aes256ecb_encrypt,
238 &op,
239 &ret_orig));
240
241 if (!ADBG_EXPECT(cs->c, 0,
242 memcmp(exp_out, out, sizeof(exp_out)))) {
243 Do_ADBG_Log("exp_out:");
244 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
245 Do_ADBG_Log("out:");
246 Do_ADBG_HexLog(out, sizeof(out), 16);
247 }
248 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200249 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200250
Etienne Carrierea3198522017-10-26 09:48:55 +0200251 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200252 {
253 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
254 static const uint8_t in[] = {
255 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
256 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
257 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
258 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
259 };
260 static const uint8_t exp_out[] = {
261 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
262 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
263 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
264 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
265 };
266 uint8_t out[sizeof(exp_out)];
267
268 op.params[0].tmpref.buffer = (void *)in;
269 op.params[0].tmpref.size = sizeof(in);
270 op.params[1].tmpref.buffer = out;
271 op.params[1].tmpref.size = sizeof(out);
272 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
273 TEEC_MEMREF_TEMP_OUTPUT,
274 TEEC_NONE, TEEC_NONE);
275
276 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
277 TEEC_InvokeCommand(cs->session,
278 cs->
279 cmd_id_aes256ecb_decrypt,
280 &op,
281 &ret_orig));
282
283 if (!ADBG_EXPECT(cs->c, 0,
284 memcmp(exp_out, out, sizeof(exp_out)))) {
285 Do_ADBG_Log("exp_out:");
286 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
287 Do_ADBG_Log("out:");
288 Do_ADBG_HexLog(out, sizeof(out), 16);
289 }
290 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200291 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200292}
293
294static void xtest_tee_test_1001(ADBG_Case_t *c)
295{
Jens Wiklandercf16e842016-02-10 09:07:09 +0100296 TEEC_Result res;
297 TEEC_Session session = { 0 };
298 uint32_t ret_orig;
Pascal Brandc639ac82015-07-02 08:53:34 +0200299
Etienne Carriere11093162017-10-26 09:49:04 +0200300 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100301 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100302 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200303 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
304 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100305 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200306 }
307 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Pascal Brandc639ac82015-07-02 08:53:34 +0200308
Jens Wiklandercf16e842016-02-10 09:07:09 +0100309 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100310 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Jens Wiklandercf16e842016-02-10 09:07:09 +0100311 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200312}
313
Jens Wiklander1d70a112017-10-16 15:16:39 +0200314static void xtest_tee_test_1002(ADBG_Case_t *c)
315{
316 TEEC_Result res;
317 TEEC_Session session = { 0 };
318 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
319 uint32_t ret_orig;
320 uint8_t buf[16 * 1024];
321 uint8_t exp_sum = 0;
322 size_t n;
323
Etienne Carriere11093162017-10-26 09:49:04 +0200324 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200325 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
326 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200327 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
328 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200329 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200330 }
331 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200332
333 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
334 TEEC_NONE, TEEC_NONE);
335 op.params[0].tmpref.size = sizeof(buf);
336 op.params[0].tmpref.buffer = buf;
337
338 for (n = 0; n < sizeof(buf); n++)
339 buf[n] = n + 1;
340 for (n = 0; n < sizeof(buf); n++)
341 exp_sum += buf[n];
342
343 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
344 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
345 goto out;
346
347 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
348out:
349 TEEC_CloseSession(&session);
350}
351
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100352struct test_1003_arg {
353 uint32_t test_type;
354 size_t repeat;
355 size_t max_before_lockers;
356 size_t max_during_lockers;
357 size_t before_lockers;
358 size_t during_lockers;
359 TEEC_Result res;
360 uint32_t error_orig;
361};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200362
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100363static void *test_1003_thread(void *arg)
364{
365 struct test_1003_arg *a = arg;
366 TEEC_Session session = { 0 };
367 size_t rounds = 64 * 1024;
368 size_t n;
369
370 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
371 NULL, &a->error_orig);
372 if (a->res != TEEC_SUCCESS)
373 return NULL;
374
375 for (n = 0; n < a->repeat; n++) {
376 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
377
378 op.params[0].value.a = a->test_type;
379 op.params[0].value.b = rounds;
380
381 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
382 TEEC_VALUE_OUTPUT,
383 TEEC_NONE, TEEC_NONE);
384 a->res = TEEC_InvokeCommand(&session,
385 PTA_INVOKE_TESTS_CMD_MUTEX,
386 &op, &a->error_orig);
387 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
388 op.params[1].value.b != 1) {
389 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
390 a->res = TEEC_ERROR_BAD_STATE;
391 a->error_orig = 42;
392 break;
393 }
394
395 if (a->test_type == PTA_MUTEX_TEST_READER) {
396 if (op.params[1].value.a > a->max_before_lockers)
397 a->max_before_lockers = op.params[1].value.a;
398
399 if (op.params[1].value.b > a->max_during_lockers)
400 a->max_during_lockers = op.params[1].value.b;
401
402 a->before_lockers += op.params[1].value.a;
403 a->during_lockers += op.params[1].value.b;
404 }
405 }
406 TEEC_CloseSession(&session);
407
408 return NULL;
409}
410
411static void xtest_tee_test_1003(ADBG_Case_t *c)
412{
413 size_t num_threads = 3 * 2;
414 TEEC_Result res;
415 TEEC_Session session = { 0 };
416 uint32_t ret_orig;
417 size_t repeat = 20;
418 pthread_t thr[num_threads];
419 struct test_1003_arg arg[num_threads];
420 size_t max_read_concurrency = 0;
421 size_t max_read_waiters = 0;
422 size_t num_concurrent_read_lockers = 0;
423 size_t num_concurrent_read_waiters = 0;
424 size_t n;
425 size_t nt = num_threads;
426 double mean_read_concurrency;
427 double mean_read_waiters;
428 size_t num_writers = 0;
429 size_t num_readers = 0;
430
431 /* Pseudo TA is optional: warn and nicely exit if not found */
432 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
433 &ret_orig);
434 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
435 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
436 return;
437 }
438 ADBG_EXPECT_TEEC_SUCCESS(c, res);
439 TEEC_CloseSession(&session);
440
441 memset(arg, 0, sizeof(arg));
442
443 for (n = 0; n < nt; n++) {
444 if (n % 3) {
445 arg[n].test_type = PTA_MUTEX_TEST_READER;
446 num_readers++;
447 } else {
448 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
449 num_writers++;
450 }
451 arg[n].repeat = repeat;
452 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
453 test_1003_thread, arg + n)))
454 nt = n; /* break loop and start cleanup */
455 }
456
457 for (n = 0; n < nt; n++) {
458 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
459 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
460 Do_ADBG_Log("error origin %" PRIu32,
461 arg[n].error_orig);
462 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
463 if (arg[n].max_during_lockers > max_read_concurrency)
464 max_read_concurrency =
465 arg[n].max_during_lockers;
466
467 if (arg[n].max_before_lockers > max_read_waiters)
468 max_read_waiters = arg[n].max_before_lockers;
469
470 num_concurrent_read_lockers += arg[n].during_lockers;
471 num_concurrent_read_waiters += arg[n].before_lockers;
472 }
473 }
474
475 mean_read_concurrency = (double)num_concurrent_read_lockers /
476 (double)(repeat * num_readers);
477 mean_read_waiters = (double)num_concurrent_read_waiters /
478 (double)(repeat * num_readers);
479
480 Do_ADBG_Log(" Number of parallel threads: %zu (%zu writers and %zu readers)",
481 num_threads, num_writers, num_readers);
482 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
483 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
484 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
485 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
486}
Jens Wiklander1d70a112017-10-16 15:16:39 +0200487
Pascal Brandc639ac82015-07-02 08:53:34 +0200488static void xtest_tee_test_1004(ADBG_Case_t *c)
489{
490 TEEC_Session session = { 0 };
491 uint32_t ret_orig;
492 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
493 TA_CRYPT_CMD_AES256ECB_ENC,
494 TA_CRYPT_CMD_AES256ECB_DEC };
495
496 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
497 &session, &crypt_user_ta_uuid,
498 NULL, &ret_orig)))
499 return;
500
501 /* Run the "complete crypto test suite" */
502 xtest_crypto_test(&cs);
503
504 TEEC_CloseSession(&session);
505}
506
Etienne Carriere92c34422018-02-09 13:11:40 +0100507static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200508{
509 TEEC_Session session = { 0 };
510 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
511 uint32_t ret_orig;
512
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300513 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200514 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300515 &ret_orig)))
516 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200517
518 op.params[0].value.a = n;
519 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
520 TEEC_NONE);
521
522 (void)ADBG_EXPECT_TEEC_RESULT(c,
523 TEEC_ERROR_TARGET_DEAD,
524 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
525 &ret_orig));
526
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300527 (void)ADBG_EXPECT_TEEC_RESULT(c,
528 TEEC_ERROR_TARGET_DEAD,
529 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200530 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300531
Pascal Brandc639ac82015-07-02 08:53:34 +0200532 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
533
534 TEEC_CloseSession(&session);
535}
536
Etienne Carriere92c34422018-02-09 13:11:40 +0100537static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
538 size_t size)
539{
540 TEEC_Session session = { 0 };
541 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
542 uint32_t ret_orig;
543 TEEC_SharedMemory shm;
544
545 memset(&shm, 0, sizeof(shm));
546 shm.size = size;
547 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
548 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
549 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
550 return;
551
552 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
553 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
554 &ret_orig)))
555 return;
556
557 op.params[0].value.a = (uint32_t)n;
558 op.params[1].memref.parent = &shm;
559 op.params[1].memref.size = size;
560 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
561 TEEC_NONE, TEEC_NONE);
562
563 (void)ADBG_EXPECT_TEEC_RESULT(c,
564 TEEC_ERROR_TARGET_DEAD,
565 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
566 &ret_orig));
567
568 (void)ADBG_EXPECT_TEEC_RESULT(c,
569 TEEC_ERROR_TARGET_DEAD,
570 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
571 &ret_orig));
572
573 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
574
575 TEEC_CloseSession(&session);
576}
577
Pascal Brandc639ac82015-07-02 08:53:34 +0200578static void xtest_tee_test_1005(ADBG_Case_t *c)
579{
580 uint32_t ret_orig;
581#define MAX_SESSIONS 3
582 TEEC_Session sessions[MAX_SESSIONS];
583 int i;
584
585 for (i = 0; i < MAX_SESSIONS; i++) {
586 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200587 xtest_teec_open_session(&sessions[i],
588 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200589 NULL, &ret_orig)))
590 break;
591 }
592
593 for (; --i >= 0; )
594 TEEC_CloseSession(&sessions[i]);
595}
596
597static void xtest_tee_test_1006(ADBG_Case_t *c)
598{
599 TEEC_Session session = { 0 };
600 uint32_t ret_orig;
601 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
602 uint8_t buf[32];
603
604 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
605 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
606 &ret_orig)))
607 return;
608
609 op.params[0].tmpref.buffer = buf;
610 op.params[0].tmpref.size = sizeof(buf);
611 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
612 TEEC_NONE, TEEC_NONE);
613
614 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
615 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
616 &ret_orig));
617
618 TEEC_CloseSession(&session);
619}
620
621static void xtest_tee_test_1007(ADBG_Case_t *c)
622{
623 TEEC_Session session = { 0 };
624 uint32_t ret_orig;
625
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300626 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200627 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300628 &ret_orig)))
629 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200630
631 (void)ADBG_EXPECT_TEEC_RESULT(c,
632 TEEC_ERROR_TARGET_DEAD,
633 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
634 &ret_orig));
635
636 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
637
638 (void)ADBG_EXPECT_TEEC_RESULT(c,
639 TEEC_ERROR_TARGET_DEAD,
640 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
641 &ret_orig));
642
643 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
644
645 TEEC_CloseSession(&session);
646}
647
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100648#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jerome Forissierf02a2212015-10-29 14:33:35 +0100649#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000650# ifdef __ANDROID__
651#define TA_DIR "/system/lib/optee_armtz"
652# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100653#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000654# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100655#endif
656
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100657static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600658{
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100659 char buf[PATH_MAX];
David Brownb2865ab2016-08-02 11:44:41 -0600660
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100661 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100662 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100663 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200664 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
665 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
666 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600667 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200668
Jens Wiklanderb7940892015-10-23 16:02:40 +0200669 return fopen(buf, mode);
670}
671
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100672static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200673{
674 TEEC_Session session = { 0 };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100675 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
676 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200677 TEEC_Result res;
678 uint32_t ret_orig;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100679 FILE *f = NULL;
680 bool r = false;
681 uint8_t *buf = NULL;
682 size_t sz;
683 size_t fread_res;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200684
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100685 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
686 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
687 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200688
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100689 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
690 if (!ADBG_EXPECT_NOT_NULL(c, f))
691 goto out;
692 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
693 goto out;
694 sz = ftell(f);
695 rewind(f);
696
697 buf = malloc(sz);
698 if (!ADBG_EXPECT_NOT_NULL(c, buf))
699 goto out;
700
701 fread_res = fread(buf, 1, sz, f);
702 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
703 goto out;
704
Jens Wiklander4441fe22015-10-23 16:53:02 +0200705 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100706 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200707
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100708 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200709
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100710 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
711 TEEC_NONE, TEEC_NONE);
712 op.params[0].tmpref.buffer = buf;
713 op.params[0].tmpref.size = sz;
714
715 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
716 &ret_orig);
717 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
718out:
719 free(buf);
720 if (f)
721 fclose(f);
722 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200723 return r;
724}
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100725#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Jens Wiklander4441fe22015-10-23 16:53:02 +0200726
Pascal Brandc639ac82015-07-02 08:53:34 +0200727static void xtest_tee_test_1008(ADBG_Case_t *c)
728{
729 TEEC_Session session = { 0 };
730 TEEC_Session session_crypt = { 0 };
731 uint32_t ret_orig;
732
733 Do_ADBG_BeginSubCase(c, "Invoke command");
734 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300735 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200736 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300737 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200738
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300739 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
740 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
741 NULL, &ret_orig));
742 TEEC_CloseSession(&session);
743 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200744
Pascal Brandc639ac82015-07-02 08:53:34 +0200745 }
746 Do_ADBG_EndSubCase(c, "Invoke command");
747
748 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
749 {
750 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
751
752 op.params[0].value.a = 2000;
753 op.paramTypes = TEEC_PARAM_TYPES(
754 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
755
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300756 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200757 xtest_teec_open_session(&session,
758 &os_test_ta_uuid,
759 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300760 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200761
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300762 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
763 TEEC_InvokeCommand(&session,
764 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
765 &op, &ret_orig));
766 TEEC_CloseSession(&session);
767 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200768 }
769 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
770
771 Do_ADBG_BeginSubCase(c, "Create session fail");
772 {
Jens Wiklanderb7940892015-10-23 16:02:40 +0200773 size_t n;
774
Pascal Brandc639ac82015-07-02 08:53:34 +0200775 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
776 xtest_teec_open_session(&session_crypt,
777 &create_fail_test_ta_uuid, NULL,
778 &ret_orig));
Pascal Brandc639ac82015-07-02 08:53:34 +0200779 /*
780 * Run this several times to see that there's no memory leakage.
781 */
782 for (n = 0; n < 100; n++) {
783 Do_ADBG_Log("n = %zu", n);
784 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
785 xtest_teec_open_session(&session_crypt,
786 &create_fail_test_ta_uuid,
787 NULL, &ret_orig));
788 }
789 }
790 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200791
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100792#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jens Wiklander4441fe22015-10-23 16:53:02 +0200793 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
794 ADBG_EXPECT_TRUE(c,
795 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
796 ADBG_EXPECT_TRUE(c,
797 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
798 ADBG_EXPECT_TRUE(c,
799 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
800 ADBG_EXPECT_TRUE(c,
801 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
802 ADBG_EXPECT_TRUE(c,
803 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
804 ADBG_EXPECT_TRUE(c,
805 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
806 ADBG_EXPECT_TRUE(c,
807 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
808 ADBG_EXPECT_TRUE(c,
809 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
810 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
811 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 30000, 1)); /* payload */
812 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100813#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Pascal Brandc639ac82015-07-02 08:53:34 +0200814}
815
Pascal Brandc639ac82015-07-02 08:53:34 +0200816static void *cancellation_thread(void *arg)
817{
818 /*
819 * Sleep 0.5 seconds before cancellation to make sure that the other
820 * thread is in RPC_WAIT.
821 */
822 (void)usleep(500000);
823 TEEC_RequestCancellation(arg);
824 return NULL;
825}
Pascal Brandc639ac82015-07-02 08:53:34 +0200826
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300827static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
828 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200829{
830 TEEC_Session session = { 0 };
831 uint32_t ret_orig;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300832 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200833
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300834 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200835 {
836 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
837
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300838 if (ADBG_EXPECT_TEEC_SUCCESS(c,
839 xtest_teec_open_session(&session, &os_test_ta_uuid,
840 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200841
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300842 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
843 TEEC_ORIGIN_TRUSTED_APP,
844 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200845
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300846 op.params[0].value.a = timeout;
847 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
848 TEEC_NONE,
849 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300850 if (cancel) {
851 (void)ADBG_EXPECT(c, 0,
852 pthread_create(&thr, NULL,
853 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200854
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300855 (void)ADBG_EXPECT_TEEC_RESULT(c,
856 TEEC_ERROR_CANCEL,
857 TEEC_InvokeCommand(&session,
858 TA_OS_TEST_CMD_WAIT,
859 &op,
860 &ret_orig));
861 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300862
863 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
864 TEEC_InvokeCommand(&session,
865 TA_OS_TEST_CMD_WAIT,
866 &op,
867 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300868 if (cancel)
869 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300870
871 TEEC_CloseSession(&session);
872 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200873 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300874 Do_ADBG_EndSubCase(c, "%s", subcase);
875}
876
877static void xtest_tee_test_1009(ADBG_Case_t *c)
878{
879 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
880 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300881 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300882 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200883}
884
885static void xtest_tee_test_1010(ADBG_Case_t *c)
886{
Etienne Carriere92c34422018-02-09 13:11:40 +0100887 unsigned int n;
888 unsigned int idx;
889 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200890
891 for (n = 1; n <= 5; n++) {
892 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
893 xtest_tee_test_invalid_mem_access(c, n);
894 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
895 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100896
897 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
898 for (n = 1; n <= 5; n++) {
899 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200900 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100901 n, memref_sz[idx]);
902 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
903 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200904 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100905 n, memref_sz[idx]);
906 }
907 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200908}
909
910static void xtest_tee_test_1011(ADBG_Case_t *c)
911{
912 TEEC_Session session = { 0 };
913 uint32_t ret_orig;
914 struct xtest_crypto_session cs = {
915 c, &session, TA_RPC_CMD_CRYPT_SHA256,
916 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
917 TA_RPC_CMD_CRYPT_AES256ECB_DEC
918 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100919 struct xtest_crypto_session cs_privmem = {
920 c, &session,
921 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
922 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
923 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
924 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200925 TEEC_UUID uuid = rpc_test_ta_uuid;
926
927 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
928 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
929 return;
930
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100931 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200932 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100933 * Run the "complete crypto test suite" using TA-to-TA
934 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200935 */
936 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100937 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
938
939 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
940 /*
941 * Run the "complete crypto test suite" using TA-to-TA
942 * communication via TA private memory.
943 */
944 xtest_crypto_test(&cs_privmem);
945 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
946
Pascal Brandc639ac82015-07-02 08:53:34 +0200947 TEEC_CloseSession(&session);
948}
949
950/*
951 * Note that this test is failing when
952 * - running twice in a raw
953 * - and the user TA is statically linked
954 * This is because the counter is not reseted when opening the first session
955 * in case the TA is statically linked
956 */
957static void xtest_tee_test_1012(ADBG_Case_t *c)
958{
959 TEEC_Session session1 = { 0 };
960 TEEC_Session session2 = { 0 };
961 uint32_t ret_orig;
962 TEEC_UUID uuid = sims_test_ta_uuid;
963
964 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
965 {
966 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
967 static const uint8_t in[] = {
968 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
969 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
970 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
971 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
972 };
973 uint8_t out[32] = { 0 };
974 int i;
975
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300976 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200977 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300978 &ret_orig)))
979 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200980
981 op.params[0].value.a = 0;
982 op.params[1].tmpref.buffer = (void *)in;
983 op.params[1].tmpref.size = sizeof(in);
984 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
985 TEEC_MEMREF_TEMP_INPUT,
986 TEEC_NONE, TEEC_NONE);
987
988 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
989 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
990 &ret_orig));
991
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100992 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300993 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200994 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300995 &ret_orig)))
996 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200997
998 op.params[0].value.a = 0;
999 op.params[1].tmpref.buffer = out;
1000 op.params[1].tmpref.size = sizeof(out);
1001 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1002 TEEC_MEMREF_TEMP_OUTPUT,
1003 TEEC_NONE, TEEC_NONE);
1004
1005 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1006 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
1007 &op, &ret_orig));
1008
1009 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
1010 sizeof(out))) {
1011 Do_ADBG_Log("in:");
1012 Do_ADBG_HexLog(in, sizeof(in), 16);
1013 Do_ADBG_Log("out:");
1014 Do_ADBG_HexLog(out, sizeof(out), 16);
1015 }
1016
1017 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1018 TEEC_NONE, TEEC_NONE,
1019 TEEC_NONE);
1020
1021 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1022 TEEC_InvokeCommand(&session1,
1023 TA_SIMS_CMD_GET_COUNTER,
1024 &op, &ret_orig));
1025
1026 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
1027
1028 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1029 TEEC_InvokeCommand(&session2,
1030 TA_SIMS_CMD_GET_COUNTER, &op,
1031 &ret_orig));
1032
1033 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1034 TEEC_CloseSession(&session2);
1035 }
1036
1037 memset(out, 0, sizeof(out));
1038 op.params[0].value.a = 0;
1039 op.params[1].tmpref.buffer = out;
1040 op.params[1].tmpref.size = sizeof(out);
1041 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1042 TEEC_MEMREF_TEMP_OUTPUT,
1043 TEEC_NONE, TEEC_NONE);
1044
1045 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1046 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1047 &ret_orig));
1048
1049 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1050 Do_ADBG_Log("in:");
1051 Do_ADBG_HexLog(in, sizeof(in), 16);
1052 Do_ADBG_Log("out:");
1053 Do_ADBG_HexLog(out, sizeof(out), 16);
1054 }
1055
1056 TEEC_CloseSession(&session1);
1057 }
1058}
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001059
1060struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001061 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001062 uint32_t cmd;
1063 uint32_t repeat;
1064 TEEC_SharedMemory *shm;
1065 uint32_t error_orig;
1066 TEEC_Result res;
1067 uint32_t max_concurrency;
1068 const uint8_t *in;
1069 size_t in_len;
1070 uint8_t *out;
1071 size_t out_len;
1072};
1073
1074static void *test_1013_thread(void *arg)
1075{
1076 struct test_1013_thread_arg *a = arg;
1077 TEEC_Session session = { 0 };
1078 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1079 uint8_t p2 = TEEC_NONE;
1080 uint8_t p3 = TEEC_NONE;
1081
Jens Wiklander70672972016-04-06 00:01:45 +02001082 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001083 &a->error_orig);
1084 if (a->res != TEEC_SUCCESS)
1085 return NULL;
1086
1087 op.params[0].memref.parent = a->shm;
1088 op.params[0].memref.size = a->shm->size;
1089 op.params[0].memref.offset = 0;
1090 op.params[1].value.a = a->repeat;
1091 op.params[1].value.b = 0;
1092 op.params[2].tmpref.buffer = (void *)a->in;
1093 op.params[2].tmpref.size = a->in_len;
1094 op.params[3].tmpref.buffer = a->out;
1095 op.params[3].tmpref.size = a->out_len;
1096
1097 if (a->in_len)
1098 p2 = TEEC_MEMREF_TEMP_INPUT;
1099 if (a->out_len)
1100 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1101
1102 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1103 TEEC_VALUE_INOUT, p2, p3);
1104
1105 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1106 a->max_concurrency = op.params[1].value.b;
1107 a->out_len = op.params[3].tmpref.size;
1108 TEEC_CloseSession(&session);
1109 return NULL;
1110}
1111
Pascal Brand4fa35582015-12-17 10:59:12 +01001112#define NUM_THREADS 3
1113
Jens Wiklander70672972016-04-06 00:01:45 +02001114static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1115 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001116{
Pascal Brand4fa35582015-12-17 10:59:12 +01001117 size_t num_threads = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001118 size_t nt;
1119 size_t n;
Jens Wiklander70672972016-04-06 00:01:45 +02001120 size_t repeat = 1000;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001121 pthread_t thr[num_threads];
1122 TEEC_SharedMemory shm;
1123 size_t max_concurrency;
1124 struct test_1013_thread_arg arg[num_threads];
1125 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1126 static const uint8_t sha256_out[] = {
1127 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1128 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1129 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1130 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1131 };
1132 uint8_t out[32] = { 0 };
1133
Jens Wiklander70672972016-04-06 00:01:45 +02001134 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001135 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001136
1137 memset(&shm, 0, sizeof(shm));
1138 shm.size = sizeof(struct ta_concurrent_shm);
1139 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1140 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1141 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1142 return;
1143
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001144 memset(shm.buffer, 0, shm.size);
1145 memset(arg, 0, sizeof(arg));
1146 max_concurrency = 0;
1147 nt = num_threads;
1148
1149 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001150 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001151 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001152 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001153 arg[n].shm = &shm;
1154 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1155 test_1013_thread, arg + n)))
1156 nt = n; /* break loop and start cleanup */
1157 }
1158
1159 for (n = 0; n < nt; n++) {
1160 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1161 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1162 if (arg[n].max_concurrency > max_concurrency)
1163 max_concurrency = arg[n].max_concurrency;
1164 }
1165
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001166 /*
1167 * Concurrency can be limited by several factors, for instance in a
1168 * single CPU system it's dependent on the Preemtion Model used by
1169 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1170 * best result there).
1171 */
1172 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
1173 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, num_threads);
Pascal Brand4fa35582015-12-17 10:59:12 +01001174 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001175 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001176
Jens Wiklander70672972016-04-06 00:01:45 +02001177 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001178 memset(shm.buffer, 0, shm.size);
1179 memset(arg, 0, sizeof(arg));
1180 max_concurrency = 0;
1181 nt = num_threads;
1182
1183 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001184 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001185 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001186 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001187 arg[n].shm = &shm;
1188 arg[n].in = sha256_in;
1189 arg[n].in_len = sizeof(sha256_in);
1190 arg[n].out = out;
1191 arg[n].out_len = sizeof(out);
1192 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1193 test_1013_thread, arg + n)))
1194 nt = n; /* break loop and start cleanup */
1195 }
1196
1197 for (n = 0; n < nt; n++) {
1198 if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) &&
1199 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
1200 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1201 arg[n].out, arg[n].out_len);
1202 if (arg[n].max_concurrency > max_concurrency)
1203 max_concurrency = arg[n].max_concurrency;
1204 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001205 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001206 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001207
Pascal Brand4fa35582015-12-17 10:59:12 +01001208 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001209 TEEC_ReleaseSharedMemory(&shm);
1210}
Pascal Brand4fa35582015-12-17 10:59:12 +01001211
1212static void xtest_tee_test_1013(ADBG_Case_t *c)
1213{
1214 int i;
1215 double mean_concurrency;
1216 double concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001217 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001218
1219 if (level == 0)
1220 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001221
Jens Wiklander70672972016-04-06 00:01:45 +02001222 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001223 mean_concurrency = 0;
1224 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001225 xtest_tee_test_1013_single(c, &concurrency,
1226 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001227 mean_concurrency += concurrency;
1228 }
1229 mean_concurrency /= nb_loops;
1230
1231 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1232 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001233 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001234
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001235#ifndef CFG_PAGED_USER_TA
Jens Wiklander70672972016-04-06 00:01:45 +02001236 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1237 mean_concurrency = 0;
1238 for (i = 0; i < nb_loops; i++) {
1239 xtest_tee_test_1013_single(c, &concurrency,
1240 &concurrent_large_ta_uuid);
1241 mean_concurrency += concurrency;
1242 }
1243 mean_concurrency /= nb_loops;
1244
1245 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1246 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1247 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001248#endif
Jens Wiklander70672972016-04-06 00:01:45 +02001249}
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001250
1251#ifdef CFG_SECURE_DATA_PATH
1252static void xtest_tee_test_1014(ADBG_Case_t *c)
1253{
1254 UNUSED(c);
1255
1256 int size = 17000;
1257 int loop = 10;
1258 int ion_heap = DEFAULT_ION_HEAP_TYPE;
1259 int rnd_offset = 1;
1260 int test;
1261 int ret;
1262
1263 test = TEST_NS_TO_TA;
1264 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001265 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001266 ADBG_EXPECT(c, 0, ret);
1267 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1268
1269 test = TEST_TA_TO_TA;
1270 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001271 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001272 ADBG_EXPECT(c, 0, ret);
1273 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1274
1275 test = TEST_TA_TO_PTA;
1276 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP pTA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001277 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001278 ADBG_EXPECT(c, 0, ret);
1279 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP pTA");
1280
1281 test = TEST_NS_TO_PTA;
Etienne Carrierea3198522017-10-26 09:48:55 +02001282 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001283 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001284 ADBG_EXPECT(c, 1, ret);
Etienne Carrierea3198522017-10-26 09:48:55 +02001285 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001286}
1287#endif
Jens Wiklander272d3642017-04-03 13:03:47 +02001288
1289static void xtest_tee_test_1015(ADBG_Case_t *c)
1290{
1291 TEEC_Result res;
1292 TEEC_Session session = { 0 };
1293 uint32_t ret_orig;
1294
Etienne Carriere11093162017-10-26 09:49:04 +02001295 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001296 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1297 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001298 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1299 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001300 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001301 }
1302 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001303
1304 ADBG_EXPECT_TEEC_SUCCESS(c,
1305 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1306 NULL, &ret_orig));
1307 TEEC_CloseSession(&session);
1308}
Jerome Forissiere916b102017-06-07 17:55:52 +02001309
1310static void xtest_tee_test_1016(ADBG_Case_t *c)
1311{
1312 TEEC_Session session = { 0 };
1313 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1314 uint32_t ret_orig;
1315
1316 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1317 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1318 &ret_orig)))
1319 return;
1320
1321 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1322 TEEC_NONE);
1323
1324 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1325 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1326 &ret_orig));
1327
1328 TEEC_CloseSession(&session);
1329}
Jens Wiklander87e81702018-03-20 12:00:00 +08001330
1331static void xtest_tee_test_1017(ADBG_Case_t *c)
1332{
1333 TEEC_Session session = { 0 };
1334 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1335 uint32_t ret_orig;
1336 TEEC_SharedMemory shm;
1337 size_t page_size = 4096;
1338
1339 memset(&shm, 0, sizeof(shm));
1340 shm.size = 8 * page_size;
1341 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1342 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1343 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1344 return;
1345
1346 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1347 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1348 &ret_orig)))
1349 goto out;
1350
1351 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1352 TEEC_MEMREF_PARTIAL_INPUT,
1353 TEEC_MEMREF_PARTIAL_OUTPUT,
1354 TEEC_MEMREF_PARTIAL_OUTPUT);
1355
1356 /*
1357 * The first two memrefs are supposed to be combined into in
1358 * region and the last two memrefs should have one region each
1359 * when the parameters are mapped for the TA.
1360 */
1361 op.params[0].memref.parent = &shm;
1362 op.params[0].memref.size = page_size;
1363 op.params[0].memref.offset = 0;
1364
1365 op.params[1].memref.parent = &shm;
1366 op.params[1].memref.size = page_size;
1367 op.params[1].memref.offset = page_size;
1368
1369 op.params[2].memref.parent = &shm;
1370 op.params[2].memref.size = page_size;
1371 op.params[2].memref.offset = 4 * page_size;
1372
1373 op.params[3].memref.parent = &shm;
1374 op.params[3].memref.size = 2 * page_size;
1375 op.params[3].memref.offset = 6 * page_size;
1376
1377 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1378 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1379 &ret_orig));
1380
1381 TEEC_CloseSession(&session);
1382out:
1383 TEEC_ReleaseSharedMemory(&shm);
1384}
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001385
1386static void xtest_tee_test_1018(ADBG_Case_t *c)
1387{
1388 TEEC_Session session = { 0 };
1389 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1390 uint32_t ret_orig;
1391 TEEC_SharedMemory shm;
1392 size_t page_size = 4096;
1393
1394 memset(&shm, 0, sizeof(shm));
1395 shm.size = 8 * page_size;
1396 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1397 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1398 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1399 return;
1400
1401 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1402 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1403 &ret_orig)))
1404 goto out;
1405
1406 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1407 TEEC_MEMREF_PARTIAL_INPUT,
1408 TEEC_MEMREF_PARTIAL_OUTPUT,
1409 TEEC_MEMREF_PARTIAL_OUTPUT);
1410
1411 /*
1412 * The first two memrefs are supposed to be combined into in
1413 * region and the last two memrefs should have one region each
1414 * when the parameters are mapped for the TA.
1415 */
1416 op.params[0].memref.parent = &shm;
1417 op.params[0].memref.size = page_size;
1418 op.params[0].memref.offset = 0;
1419
1420 op.params[1].memref.parent = &shm;
1421 op.params[1].memref.size = page_size;
1422 op.params[1].memref.offset = page_size;
1423
1424 op.params[2].memref.parent = &shm;
1425 op.params[2].memref.size = page_size;
1426 op.params[2].memref.offset = 4 * page_size;
1427
1428 op.params[3].memref.parent = &shm;
1429 op.params[3].memref.size = 3 * page_size;
1430 op.params[3].memref.offset = 6 * page_size;
1431
1432 /*
1433 * Depending on the tee driver we may have different error codes.
1434 * What's most important is that secure world doesn't panic and
1435 * that someone detects an error.
1436 */
1437 ADBG_EXPECT_NOT(c, TEE_SUCCESS,
1438 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1439 &ret_orig));
1440
1441 TEEC_CloseSession(&session);
1442out:
1443 TEEC_ReleaseSharedMemory(&shm);
1444}
Jerome Forissier53bde722018-05-31 09:14:54 +02001445
1446#if defined(CFG_TA_DYNLINK) && !defined(__ANDROID__)
1447static void xtest_tee_test_1019(ADBG_Case_t *c)
1448{
1449 TEEC_Session session = { 0 };
1450 uint32_t ret_orig;
1451
1452 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1453 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1454 &ret_orig)))
1455 return;
1456
1457 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1458 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1459 &ret_orig));
1460
1461 (void)ADBG_EXPECT_TEEC_RESULT(c,
1462 TEEC_ERROR_TARGET_DEAD,
1463 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1464 NULL, &ret_orig));
1465
1466 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1467
1468 TEEC_CloseSession(&session);
1469}
1470#endif