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