blob: cc564217a4c8b898e38ea6fd39ffdbd8cb4a629a [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
14#include <stdio.h>
15#include <string.h>
16#include <stdlib.h>
17#include <sys/types.h>
18#include <unistd.h>
19
20#include "xtest_test.h"
21#include "xtest_helpers.h"
Pascal Brandc639ac82015-07-02 08:53:34 +020022#include "tee_api_defines.h"
23#include "tee_client_api.h"
24
25#define OFFSET0 0
26
27#define PARAM_0 0
28#define PARAM_1 1
29#define PARAM_2 2
30#define PARAM_3 3
31
32struct xtest_session {
33 ADBG_Case_t *c;
34 TEEC_Session session;
35 TEEC_Context context;
36};
37
38/* Compares two memories and checks if their length and content is the same */
39#define EXPECT_SHARED_MEM_BUFFER(c, exp_buf, exp_blen, op, param_num, shrm) \
40 do { \
41 if ((exp_buf) == NULL) { \
42 ADBG_EXPECT((c), exp_blen, \
43 (op)->params[(param_num)].memref.size); \
44 } else { \
45 ADBG_EXPECT_COMPARE_POINTER((c), (shrm), ==, \
46 (op)->params[(param_num)].memref.parent); \
47 ADBG_EXPECT_BUFFER((c), (exp_buf), (exp_blen), \
48 (shrm)->buffer, \
49 (op)->params[(param_num)].memref.size); \
50 } \
51 } while (0)
52
53/*
54 * Compares the content of the memory cells in OP with the expected value
55 * contained.
56 */
57#define EXPECT_OP_TMP_MEM_BUFFER(c, exp_buf, exp_blen, op, param_num, buf) \
58 do { \
59 if ((exp_buf) == NULL) { \
60 ADBG_EXPECT((c), exp_blen, \
61 (op)->params[(param_num)].tmpref.size); \
62 } else { \
63 ADBG_EXPECT_COMPARE_POINTER((c), (buf), ==, \
64 (op)->params[(param_num)].tmpref.buffer); \
65 ADBG_EXPECT_BUFFER((c), (exp_buf), (exp_blen), \
66 (buf), \
67 (op)->params[(param_num)].memref.size); \
68 } \
69 } while (0)
70
71/* Initiates the memory and allocates size uint32_t. */
72
73/* Registers the TEEC_SharedMemory to the TEE. */
74static TEEC_Result RegisterSharedMemory(TEEC_Context *ctx,
75 TEEC_SharedMemory *shm, uint32_t size,
76 uint32_t flags)
77{
78 shm->flags = flags;
79 shm->size = size;
80 return TEEC_RegisterSharedMemory(ctx, shm);
81}
82
83/* Allocates shared memory inside of the TEE. */
84static TEEC_Result AllocateSharedMemory(TEEC_Context *ctx,
85 TEEC_SharedMemory *shm, uint32_t size,
86 uint32_t flags)
87{
88 shm->flags = flags;
89 shm->size = size;
90 return TEEC_AllocateSharedMemory(ctx, shm);
91}
92
93static void CloseSession_null(struct xtest_session *cs)
94{
95 Do_ADBG_BeginSubCase(cs->c, "CloseSession_null");
96 {
97 /* In reality doesn't test anything. */
98 TEEC_CloseSession(NULL);
99 }
100 Do_ADBG_EndSubCase(cs->c, "CloseSession_null");
101}
102
103static void Allocate_In(struct xtest_session *cs)
104{
105 Do_ADBG_BeginSubCase(cs->c, "Allocate_In");
106 {
107 TEEC_SharedMemory shm;
108 uint32_t size = 1024;
109
110 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
111 TEEC_InitializeContext(_device, &cs->context));
112 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
113 AllocateSharedMemory(&cs->context, &shm, size,
114 TEEC_MEM_INPUT));
115 TEEC_ReleaseSharedMemory(&shm);
116 TEEC_FinalizeContext(&cs->context);
117 }
118 Do_ADBG_EndSubCase(cs->c, "Allocate_In");
119}
120
121static void Allocate_out_of_memory(struct xtest_session *cs)
122{
123 Do_ADBG_BeginSubCase(cs->c, "Allocate_out_of_memory");
124 {
125 TEEC_SharedMemory shm;
126 uint32_t SIZE_OVER_MEMORY_CAPACITY = INT32_MAX;
127
128 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
129 TEEC_InitializeContext(_device, &cs->context));
130 ADBG_EXPECT_TEEC_RESULT(cs->c, TEEC_ERROR_OUT_OF_MEMORY,
131 AllocateSharedMemory(&cs->context, &shm,
132 SIZE_OVER_MEMORY_CAPACITY,
133 TEEC_MEM_INPUT));
134 ADBG_EXPECT_POINTER(cs->c, NULL, shm.buffer);
135 TEEC_FinalizeContext(&cs->context);
136 }
137 Do_ADBG_EndSubCase(cs->c, "Allocate_out_of_memory");
138}
139
140static void OpenSession_error_notExistingTA(struct xtest_session *cs)
141{
142 Do_ADBG_BeginSubCase(cs->c, "OpenSession_error_notExistingTA");
143 {
144 TEEC_UUID NONEXISTING_TA_UUID = { 0x534D1192, 0x6143, 0x234C,
145 { 0x47, 0x55, 0x53, 0x52,
146 0x54, 0x4F, 0x4F, 0x59 } };
147 uint32_t ret_orig;
148
149 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
150 TEEC_InitializeContext(_device, &cs->context));
151
152 ADBG_EXPECT_COMPARE_UNSIGNED(cs->c, TEEC_SUCCESS, !=,
153 TEEC_OpenSession(&cs->context, &cs->session,
154 &NONEXISTING_TA_UUID,
155 TEEC_LOGIN_PUBLIC, NULL, NULL,
156 &ret_orig));
157 ADBG_EXPECT_COMPARE_UNSIGNED(cs->c, TEEC_ORIGIN_TRUSTED_APP, !=,
158 ret_orig);
159 TEEC_FinalizeContext(&cs->context);
160 }
161 Do_ADBG_EndSubCase(cs->c, "OpenSession_error_notExistingTA");
162}
163
164static void Allocate_InOut(struct xtest_session *cs)
165{
166 Do_ADBG_BeginSubCase(cs->c, "Allocate_InOut");
167 {
168 TEEC_SharedMemory shm;
169 uint8_t val[] = { 54, 76, 98, 32 };
170
171 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
172 TEEC_InitializeContext(_device, &cs->context));
173
174 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
175 AllocateSharedMemory(&cs->context, &shm, sizeof(val),
176 TEEC_MEM_INPUT | TEEC_MEM_OUTPUT));
177
178 TEEC_ReleaseSharedMemory(&shm);
179 TEEC_FinalizeContext(&cs->context);
180 }
181 Do_ADBG_EndSubCase(cs->c, "Allocate_InOut");
182}
183
184static void Register_In(struct xtest_session *cs)
185{
186 Do_ADBG_BeginSubCase(cs->c, "Register_In");
187 {
188 TEEC_SharedMemory shm;
189 uint8_t val[] = { 32, 65, 43, 21, 98 };
190
191 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
192 TEEC_InitializeContext(_device, &cs->context));
193
194 shm.buffer = val;
195
196 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
197 RegisterSharedMemory(&cs->context, &shm, sizeof(val),
198 TEEC_MEM_INPUT));
199 TEEC_ReleaseSharedMemory(&shm);
200 TEEC_FinalizeContext(&cs->context);
201 }
202 Do_ADBG_EndSubCase(cs->c, "Register_In");
203}
204
205static void Register_notZeroLength_Out(struct xtest_session *cs)
206{
207 Do_ADBG_BeginSubCase(cs->c, "Register_notZeroLength_Out");
208 {
209 TEEC_SharedMemory shm;
210 uint8_t val[] = { 56, 67, 78, 99 };
211
212 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
213 TEEC_InitializeContext(_device, &cs->context));
214
215 shm.buffer = val;
216
217 ADBG_EXPECT_TEEC_SUCCESS(cs->c, RegisterSharedMemory(
218 &cs->context, &shm,
219 sizeof(val), TEEC_MEM_OUTPUT));
220 TEEC_ReleaseSharedMemory(&shm);
221 TEEC_FinalizeContext(&cs->context);
222 }
223 Do_ADBG_EndSubCase(cs->c, "Register_notZeroLength_Out");
224}
225
226static void Register_InOut(struct xtest_session *cs)
227{
228 Do_ADBG_BeginSubCase(cs->c, "Register_InOut");
229 {
230 TEEC_SharedMemory shm;
231 uint8_t val[] = { 54, 76, 23, 98, 255, 23, 86 };
232
233 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
234 TEEC_InitializeContext(_device, &cs->context));
235
236 shm.buffer = val;
237 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
238 RegisterSharedMemory(&cs->context, &shm, sizeof(val),
239 TEEC_MEM_INPUT | TEEC_MEM_OUTPUT));
240
241 TEEC_ReleaseSharedMemory(&shm);
242 TEEC_FinalizeContext(&cs->context);
243 }
244 Do_ADBG_EndSubCase(cs->c, "Register_InOut");
245}
246
247static void Register_zeroLength_Out(struct xtest_session *cs)
248{
249 Do_ADBG_BeginSubCase(cs->c, "Register_zeroLength_Out");
250 {
251 uint8_t val[] = { 65, 76, 98, 32 };
252 TEEC_SharedMemory shm;
253
254 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
255 TEEC_InitializeContext(_device, &cs->context));
256
257 shm.buffer = val;
258 ADBG_EXPECT_TEEC_SUCCESS(cs->c, RegisterSharedMemory(
259 &cs->context, &shm, 0,
260 TEEC_MEM_OUTPUT));
261
262 TEEC_ReleaseSharedMemory(&shm);
263 TEEC_FinalizeContext(&cs->context);
264 }
265 Do_ADBG_EndSubCase(cs->c, "Register_zeroLength_Out");
266}
267
268static void Allocate_Out(struct xtest_session *cs)
269{
270 Do_ADBG_BeginSubCase(cs->c, "Allocate_Out");
271 {
272 TEEC_SharedMemory shm;
273
274 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
275 TEEC_InitializeContext(_device, &cs->context));
276
277 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
278 AllocateSharedMemory(&cs->context, &shm, 0,
279 TEEC_MEM_OUTPUT));
280
281 TEEC_ReleaseSharedMemory(&shm);
282 TEEC_FinalizeContext(&cs->context);
283 }
284 Do_ADBG_EndSubCase(cs->c, "Allocate_Out");
285}
286
287static void FinalizeContext_null(struct xtest_session *cs)
288{
289 Do_ADBG_BeginSubCase(cs->c, "FinalizeContext_null");
290 {
291 TEEC_FinalizeContext(NULL);
292 }
293 Do_ADBG_EndSubCase(cs->c, "FinalizeContext_null");
294}
295
296static void InitializeContext_NotExistingTEE(struct xtest_session *cs)
297{
298 Do_ADBG_BeginSubCase(cs->c, "InitializeContext_NotExistingTEE");
299 {
300 ADBG_EXPECT_COMPARE_UNSIGNED(cs->c, TEEC_SUCCESS, !=,
301 TEEC_InitializeContext("Invalid TEE name",
302 &cs->context));
303 }
304 Do_ADBG_EndSubCase(cs->c, "InitializeContext_NotExistingTEE");
305}
306
307static void AllocateThenRegister_SameMemory(struct xtest_session *cs)
308{
309 Do_ADBG_BeginSubCase(cs->c, "AllocateThenRegister_SameMemory");
310 {
311 TEEC_SharedMemory shm;
312 uint32_t size_allocation = 32;
313
314 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
315 TEEC_InitializeContext(_device, &cs->context));
316
317 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
318 AllocateSharedMemory(&cs->context, &shm,
319 size_allocation, TEEC_MEM_INPUT));
320
321 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
322 RegisterSharedMemory(&cs->context, &shm,
323 size_allocation, TEEC_MEM_INPUT));
324 }
325 Do_ADBG_EndSubCase(cs->c, "AllocateThenRegister_SameMemory");
326}
327
328static void AllocateSameMemory_twice(struct xtest_session *cs)
329{
330 Do_ADBG_BeginSubCase(cs->c, "AllocateSameMemory_twice");
331 {
332 TEEC_SharedMemory shm;
333 uint32_t size_allocation = 32;
334
335 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
336 TEEC_InitializeContext(_device, &cs->context));
337
338 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
339 AllocateSharedMemory(&cs->context, &shm,
340 size_allocation, TEEC_MEM_INPUT));
341
342 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
343 AllocateSharedMemory(&cs->context, &shm,
344 size_allocation, TEEC_MEM_INPUT));
345
346 TEEC_ReleaseSharedMemory(&shm);
347 TEEC_FinalizeContext(&cs->context);
348 }
349 Do_ADBG_EndSubCase(cs->c, "AllocateSameMemory_twice");
350}
351
352static void RegisterSameMemory_twice(struct xtest_session *cs)
353{
354 Do_ADBG_BeginSubCase(cs->c, "RegisterSameMemory_twice");
355 {
356 uint8_t val[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
357 TEEC_SharedMemory shm;
358
359 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
360 TEEC_InitializeContext(_device, &cs->context));
361
362 shm.buffer = val;
363 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
364 RegisterSharedMemory(&cs->context, &shm, sizeof(val),
365 TEEC_MEM_INPUT));
366
367 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
368 RegisterSharedMemory(&cs->context, &shm, sizeof(val),
369 TEEC_MEM_INPUT));
370
371 TEEC_ReleaseSharedMemory(&shm);
372 TEEC_FinalizeContext(&cs->context);
373 }
374 Do_ADBG_EndSubCase(cs->c, "RegisterSameMemory_twice");
375}
376
377static void Allocate_sharedMemory_maxSize(struct xtest_session *cs)
378{
379 Do_ADBG_BeginSubCase(cs->c,
380 "Allocate_sharedMemory_MaxSize_Above_and_Below, allocate max size");
381 {
382 uint32_t size_max = TEEC_CONFIG_SHAREDMEM_MAX_SIZE;
383 TEEC_SharedMemory shm;
384
385 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
386 TEEC_InitializeContext(_device, &cs->context));
387
388 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
389 AllocateSharedMemory(&cs->context, &shm, size_max,
390 TEEC_MEM_INPUT));
391
392 TEEC_ReleaseSharedMemory(&shm);
393 }
394 Do_ADBG_EndSubCase(cs->c,
395 "Allocate_sharedMemory_MaxSize_Above_and_Below, allocate max size");
396}
397
398static void Allocate_sharedMemory_belowMaxSize(struct xtest_session *cs)
399{
400 Do_ADBG_BeginSubCase(cs->c,
401 "Allocate_sharedMemory_MaxSize_Above_and_Below, "
402 "allocate just below max size");
403 {
404 TEEC_SharedMemory shm;
405 uint32_t size_below = TEEC_CONFIG_SHAREDMEM_MAX_SIZE - 1;
406
407 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
408 TEEC_InitializeContext(_device, &cs->context));
409
410 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
411 AllocateSharedMemory(&cs->context, &shm, size_below,
412 TEEC_MEM_INPUT));
413
414 TEEC_ReleaseSharedMemory(&shm);
415 TEEC_FinalizeContext(&cs->context);
416 }
417 Do_ADBG_EndSubCase(cs->c,
418 "Allocate_sharedMemory_MaxSize_Above_and_Below, "
419 "allocate just below max size");
420}
421
422static void Allocate_sharedMemory_aboveMaxSize(struct xtest_session *cs)
423{
424 Do_ADBG_BeginSubCase(cs->c,
425 "Allocate_sharedMemory_MaxSize_Above_and_Below, "
426 "allocate just above max size");
427 {
428 TEEC_Result res;
429 TEEC_SharedMemory shm;
430 uint32_t size_above = TEEC_CONFIG_SHAREDMEM_MAX_SIZE + 1;
431
432 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
433 TEEC_InitializeContext(_device, &cs->context));
434
435 res = AllocateSharedMemory(&cs->context, &shm, size_above,
436 TEEC_MEM_INPUT);
437
438 ADBG_EXPECT_TRUE(cs->c, res == TEEC_ERROR_OUT_OF_MEMORY ||
439 res == TEEC_SUCCESS);
440
441 TEEC_ReleaseSharedMemory(&shm);
442 TEEC_FinalizeContext(&cs->context);
443 }
444 Do_ADBG_EndSubCase(cs->c,
445 "Allocate_sharedMemory_MaxSize_Above_and_Below, "
446 "allocate just above max size");
447}
448
449static void Register_sharedMemory_maxSize(struct xtest_session *cs)
450{
451 Do_ADBG_BeginSubCase(cs->c, "Register_sharedMemory_maxSize");
452 {
453 uint32_t size_max = TEEC_CONFIG_SHAREDMEM_MAX_SIZE;
454 uint8_t val[size_max];
455 TEEC_SharedMemory shm;
456
457 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
458 TEEC_InitializeContext(_device, &cs->context));
459
460 shm.buffer = val;
461 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
462 RegisterSharedMemory(&cs->context, &shm, size_max,
463 TEEC_MEM_INPUT));
464
465 TEEC_ReleaseSharedMemory(&shm);
466 TEEC_FinalizeContext(&cs->context);
467 }
468 Do_ADBG_EndSubCase(cs->c, "Register_sharedMemory_maxSize");
469}
470
471static void Register_sharedMemory_aboveMaxSize(struct xtest_session *cs)
472{
473 Do_ADBG_BeginSubCase(cs->c, "Register_sharedMemory_aboveMaxSize");
474 {
475 TEEC_Result res;
476 uint32_t size_aboveMax = 0xffffffff;
477 uint8_t val[1];
478 TEEC_SharedMemory shm;
479
480 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
481 TEEC_InitializeContext(_device, &cs->context));
482
483 shm.buffer = val;
484 res = RegisterSharedMemory(&cs->context, &shm, size_aboveMax,
485 TEEC_MEM_INPUT);
486
487 ADBG_EXPECT_TRUE(cs->c, res == TEEC_ERROR_OUT_OF_MEMORY ||
488 res == TEEC_SUCCESS);
489
490 TEEC_ReleaseSharedMemory(&shm);
491 }
492 Do_ADBG_EndSubCase(cs->c, "Register_sharedMemory_aboveMaxSize");
493}
494
495static void Register_sharedMemory_belowMaxSize(struct xtest_session *cs)
496{
497 Do_ADBG_BeginSubCase(cs->c, "Register_sharedMemory_belowMaxSize");
498 {
499 uint32_t size_belowMax = TEEC_CONFIG_SHAREDMEM_MAX_SIZE - 1;
500 uint8_t val[size_belowMax];
501 TEEC_SharedMemory shm;
502
503 ADBG_EXPECT(cs->c, TEEC_SUCCESS,
504 TEEC_InitializeContext(_device, &cs->context));
505
506 shm.buffer = val;
507 ADBG_EXPECT_TEEC_SUCCESS(cs->c,
508 RegisterSharedMemory(&cs->context, &shm, size_belowMax,
509 TEEC_MEM_INPUT));
510
511 TEEC_ReleaseSharedMemory(&shm);
512 }
513 Do_ADBG_EndSubCase(cs->c, "Register_sharedMemory_belowMaxSize");
514}
515
516static void xtest_teec_TEE(ADBG_Case_t *c)
517{
518 struct xtest_session connection = { c };
519
520 CloseSession_null(&connection);
521
522 Allocate_In(&connection);
523
524 Allocate_out_of_memory(&connection);
525
526 OpenSession_error_notExistingTA(&connection);
527
528 Allocate_InOut(&connection);
529
530 Register_In(&connection);
531
532 Register_notZeroLength_Out(&connection);
533
534 Register_InOut(&connection);
535
536 Register_zeroLength_Out(&connection);
537
538 Allocate_Out(&connection);
539
540 FinalizeContext_null(&connection);
541
542 InitializeContext_NotExistingTEE(&connection);
543
544 AllocateThenRegister_SameMemory(&connection);
545
546 AllocateSameMemory_twice(&connection);
547
548 RegisterSameMemory_twice(&connection);
549
550 Allocate_sharedMemory_maxSize(&connection);
551
552 Allocate_sharedMemory_belowMaxSize(&connection);
553
554 Allocate_sharedMemory_aboveMaxSize(&connection);
555
556 Register_sharedMemory_maxSize(&connection);
557
558 Register_sharedMemory_aboveMaxSize(&connection);
559
560 Register_sharedMemory_belowMaxSize(&connection);
561}
562
563ADBG_CASE_DEFINE(XTEST_TEE_5006, xtest_teec_TEE,
564 /* Title */
565 "Tests for Global platform TEEC",
566 /* Short description */
567 "Invocation of all tests for TEE Client API",
568 /* Requirement IDs */
569 "TEE-??",
570 /* How to implement */
571 "Description of how to implement ...");