hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 */
 
#include <assert.h>
#include <err.h>
#include <malloc.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <ta_crypt.h>
#include <ta_os_test.h>
#include <utee_defines.h>
 
#include "xtest_helpers.h"
#include "xtest_test.h"
 
/* Round up the even multiple of size, size has to be a multiple of 2 */
#define ROUNDUP(v, size) (((v) + (size - 1)) & ~(size - 1))
 
TEEC_Context xtest_teec_ctx;
 
TEEC_Result xtest_teec_ctx_init(void)
{
   return TEEC_InitializeContext(xtest_tee_name, &xtest_teec_ctx);
}
 
TEEC_Result xtest_teec_open_session(TEEC_Session *session,
                   const TEEC_UUID *uuid, TEEC_Operation *op,
                   uint32_t *ret_orig)
{
   return TEEC_OpenSession(&xtest_teec_ctx, session, uuid,
               TEEC_LOGIN_PUBLIC, NULL, op, ret_orig);
}
 
void xtest_teec_ctx_deinit(void)
{
   TEEC_FinalizeContext(&xtest_teec_ctx);
}
 
TEEC_Result ta_crypt_cmd_allocate_operation(ADBG_Case_t *c, TEEC_Session *s,
                       TEE_OperationHandle *oph,
                       uint32_t algo, uint32_t mode,
                       uint32_t max_key_size)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
 
   op.params[0].value.a = 0;
   op.params[0].value.b = algo;
   op.params[1].value.a = mode;
   op.params[1].value.b = max_key_size;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_VALUE_INPUT,
                    TEEC_NONE, TEEC_NONE);
 
   res = TEEC_InvokeCommand(s, TA_CRYPT_CMD_ALLOCATE_OPERATION, &op,
                &ret_orig);
 
   if (res != TEEC_SUCCESS) {
       (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
                           ret_orig);
   }
 
   if (res == TEEC_SUCCESS)
       *oph = (TEE_OperationHandle)(uintptr_t)op.params[0].value.a;
 
   return res;
}
 
TEEC_Result ta_crypt_cmd_allocate_transient_object(ADBG_Case_t *c,
                          TEEC_Session *s,
                          TEE_ObjectType obj_type,
                          uint32_t max_obj_size,
                          TEE_ObjectHandle *o)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
 
   op.params[0].value.a = obj_type;
   op.params[0].value.b = max_obj_size;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_OUTPUT,
                    TEEC_NONE, TEEC_NONE);
 
   res = TEEC_InvokeCommand(s, TA_CRYPT_CMD_ALLOCATE_TRANSIENT_OBJECT, &op,
                &ret_orig);
 
   if (res != TEEC_SUCCESS) {
       (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
               ret_orig);
   }
 
   if (res == TEEC_SUCCESS)
       *o = (TEE_ObjectHandle)(uintptr_t)op.params[1].value.a;
 
   return res;
}
 
void xtest_add_attr(size_t *attr_count, TEE_Attribute *attrs, uint32_t attr_id,
           const void *buf, size_t len)
{
   attrs[*attr_count].attributeID = attr_id;
   attrs[*attr_count].content.ref.buffer = (void *)buf;
   attrs[*attr_count].content.ref.length = len;
   (*attr_count)++;
}
 
void xtest_add_attr_value(size_t *attr_count, TEE_Attribute *attrs,
             uint32_t attr_id, uint32_t value_a, uint32_t value_b)
{
   attrs[*attr_count].attributeID = attr_id;
   attrs[*attr_count].content.value.a = value_a;
   attrs[*attr_count].content.value.b = value_b;
   (*attr_count)++;
}
 
struct tee_attr_packed {
   uint32_t attr_id;
   uint32_t a;
   uint32_t b;
};
 
TEE_Result pack_attrs(const TEE_Attribute *attrs, uint32_t attr_count,
             uint8_t **buf, size_t *blen)
{
   struct tee_attr_packed *a = NULL;
   uint8_t *b = NULL;
   size_t bl = 0;
   size_t n = 0;
 
   *buf = NULL;
   *blen = 0;
   if (attr_count == 0)
       return TEE_SUCCESS;
 
   bl = sizeof(uint32_t) + sizeof(struct tee_attr_packed) * attr_count;
   for (n = 0; n < attr_count; n++) {
       if ((attrs[n].attributeID & TEE_ATTR_BIT_VALUE) != 0)
           continue; /* Only memrefs need to be updated */
 
       if (!attrs[n].content.ref.buffer)
           continue;
 
       /* Make room for padding */
       bl += ROUNDUP(attrs[n].content.ref.length, 4);
   }
 
   b = calloc(1, bl);
   if (!b)
       return TEE_ERROR_OUT_OF_MEMORY;
 
   *buf = b;
   *blen = bl;
 
   *(uint32_t *)(void *)b = attr_count;
   b += sizeof(uint32_t);
   a = (struct tee_attr_packed *)(void *)b;
   b += sizeof(struct tee_attr_packed) * attr_count;
 
   for (n = 0; n < attr_count; n++) {
       a[n].attr_id = attrs[n].attributeID;
       if (attrs[n].attributeID & TEE_ATTR_BIT_VALUE) {
           a[n].a = attrs[n].content.value.a;
           a[n].b = attrs[n].content.value.b;
           continue;
       }
 
       a[n].b = attrs[n].content.ref.length;
 
       if (!attrs[n].content.ref.buffer) {
           a[n].a = 0;
           continue;
       }
 
       memcpy(b, attrs[n].content.ref.buffer,
              attrs[n].content.ref.length);
 
       /* Make buffer pointer relative to *buf */
       a[n].a = (uint32_t)(uintptr_t)(b - *buf);
 
       /* Round up to good alignment */
       b += ROUNDUP(attrs[n].content.ref.length, 4);
   }
 
   return TEE_SUCCESS;
}
 
TEEC_Result ta_crypt_cmd_populate_transient_object(ADBG_Case_t *c,
                          TEEC_Session *s,
                          TEE_ObjectHandle o,
                          const TEE_Attribute *attrs,
                          uint32_t attr_count)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
   uint8_t *buf = NULL;
   size_t blen = 0;
 
   res = pack_attrs(attrs, attr_count, &buf, &blen);
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
       return res;
 
   assert((uintptr_t)o <= UINT32_MAX);
   op.params[0].value.a = (uint32_t)(uintptr_t)o;
 
   op.params[1].tmpref.buffer = buf;
   op.params[1].tmpref.size = blen;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
                    TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
                    TEEC_NONE);
 
   res = TEEC_InvokeCommand(s, TA_CRYPT_CMD_POPULATE_TRANSIENT_OBJECT, &op,
                &ret_orig);
 
   if (res != TEEC_SUCCESS && res != TEEC_ERROR_TARGET_DEAD) {
       (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
                           ret_orig);
   }
 
   free(buf);
   return res;
}
 
TEE_Result ta_crypt_cmd_set_operation_key(ADBG_Case_t *c, TEEC_Session *s,
                     TEE_OperationHandle oph,
                     TEE_ObjectHandle key)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
 
   assert((uintptr_t)oph <= UINT32_MAX);
   op.params[0].value.a = (uint32_t)(uintptr_t)oph;
 
   assert((uintptr_t)key <= UINT32_MAX);
   op.params[0].value.b = (uint32_t)(uintptr_t)key;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
                    TEEC_NONE);
 
   res = TEEC_InvokeCommand(s, TA_CRYPT_CMD_SET_OPERATION_KEY, &op,
                &ret_orig);
 
   if (res != TEEC_SUCCESS) {
       (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
                           ret_orig);
   }
 
   return res;
}
 
TEEC_Result ta_crypt_cmd_free_transient_object(ADBG_Case_t *c, TEEC_Session *s,
                          TEE_ObjectHandle o)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
 
   assert((uintptr_t)o <= UINT32_MAX);
   op.params[0].value.a = (uint32_t)(uintptr_t)o;
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
                    TEEC_NONE);
 
   res = TEEC_InvokeCommand(s, TA_CRYPT_CMD_FREE_TRANSIENT_OBJECT, &op,
                &ret_orig);
 
   if (res != TEEC_SUCCESS) {
       (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
                           ret_orig);
   }
 
   return res;
}
 
TEEC_Result ta_crypt_cmd_derive_key(ADBG_Case_t *c, TEEC_Session *s,
                   TEE_OperationHandle oph, TEE_ObjectHandle o,
                   const TEE_Attribute *params,
                   uint32_t paramCount)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
   uint8_t *buf = NULL;
   size_t blen = 0;
 
   res = pack_attrs(params, paramCount, &buf, &blen);
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
       return res;
 
   assert((uintptr_t)oph <= UINT32_MAX);
   op.params[0].value.a = (uint32_t)(uintptr_t)oph;
 
   assert((uintptr_t)o <= UINT32_MAX);
   op.params[0].value.b = (uint32_t)(uintptr_t)o;
 
   op.params[1].tmpref.buffer = buf;
   op.params[1].tmpref.size = blen;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
                    TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
                    TEEC_NONE);
 
   res = TEEC_InvokeCommand(s, TA_CRYPT_CMD_DERIVE_KEY, &op, &ret_orig);
 
   if (res != TEEC_SUCCESS) {
       (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
                           ret_orig);
   }
 
   free(buf);
   return res;
}
 
TEEC_Result ta_crypt_cmd_get_object_buffer_attribute(ADBG_Case_t *c,
                            TEEC_Session *s,
                            TEE_ObjectHandle o,
                            uint32_t attr_id,
                            void *buf, size_t *blen)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
 
   assert((uintptr_t)o <= UINT32_MAX);
   op.params[0].value.a = (uint32_t)(uintptr_t)o;
   op.params[0].value.b = attr_id;
 
   op.params[1].tmpref.buffer = buf;
   op.params[1].tmpref.size = *blen;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
                    TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE,
                    TEEC_NONE);
 
   res = TEEC_InvokeCommand(s, TA_CRYPT_CMD_GET_OBJECT_BUFFER_ATTRIBUTE,
                &op, &ret_orig);
 
   if (res != TEEC_SUCCESS) {
       (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
                           ret_orig);
   }
 
   if (res == TEEC_SUCCESS)
       *blen = op.params[1].tmpref.size;
 
   return res;
}
 
TEEC_Result ta_crypt_cmd_free_operation(ADBG_Case_t *c, TEEC_Session *s,
                   TEE_OperationHandle oph)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
 
   op.params[0].value.a = (uint32_t)(uintptr_t)oph;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
                    TEEC_NONE);
 
   res = TEEC_InvokeCommand(s, TA_CRYPT_CMD_FREE_OPERATION, &op,
                &ret_orig);
 
   if (res != TEEC_SUCCESS) {
       (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
                           ret_orig);
   }
 
   return res;
}
 
bool ta_crypt_cmd_is_algo_supported(ADBG_Case_t *c, TEEC_Session *s,
                   uint32_t algo, uint32_t element)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
   TEEC_Result st = TEEC_ERROR_GENERIC;
 
   op.params[0].value.a = algo;
   op.params[0].value.b = element;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_OUTPUT,
                    TEEC_NONE, TEEC_NONE);
 
   res = TEEC_InvokeCommand(s, TA_CRYPT_CMD_IS_ALGO_SUPPORTED, &op,
                &ret_orig);
   if (res != TEEC_SUCCESS) {
       (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
                           ret_orig);
       return res;
   }
 
   st = op.params[1].value.a;
   ADBG_EXPECT_TRUE(c, st == TEEC_SUCCESS ||
            st == TEEC_ERROR_NOT_SUPPORTED);
   if (st == TEE_SUCCESS)
       return true;
   return false;
}
 
TEEC_Result ta_os_test_cmd_client_identity(TEEC_Session *session,
                      uint32_t *login,
                      TEEC_UUID *client_uuid)
{
   TEEC_Operation operation = { };
   TEEC_Result result = TEEC_ERROR_GENERIC;
 
   operation.params[1].tmpref.buffer = client_uuid;
   operation.params[1].tmpref.size = sizeof(*client_uuid);
 
   operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
                       TEEC_MEMREF_TEMP_OUTPUT,
                       TEEC_NONE, TEEC_NONE);
 
   result = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_CLIENT_IDENTITY,
                   &operation, NULL);
 
   if (result != TEEC_SUCCESS)
       return result;
 
   *login = operation.params[0].value.a;
 
   return TEEC_SUCCESS;
}
 
void xtest_mutex_init(pthread_mutex_t *mutex)
{
   int e = pthread_mutex_init(mutex, NULL);
 
   if (e)
       errx(1, "pthread_mutex_init: %s", strerror(e));
}
 
void xtest_mutex_destroy(pthread_mutex_t *mutex)
{
   int e = pthread_mutex_destroy(mutex);
 
   if (e)
       errx(1, "pthread_mutex_destroy: %s", strerror(e));
}
 
void xtest_mutex_lock(pthread_mutex_t *mutex)
{
   int e = pthread_mutex_lock(mutex);
 
   if (e)
       errx(1, "pthread_mutex_lock: %s", strerror(e));
}
 
void xtest_mutex_unlock(pthread_mutex_t *mutex)
{
   int e = pthread_mutex_unlock(mutex);
 
   if (e)
       errx(1, "pthread_mutex_unlock: %s", strerror(e));
}
 
void xtest_barrier_init(pthread_barrier_t *barrier, unsigned count)
{
   int e = pthread_barrier_init(barrier, NULL, count);
 
   if (e)
       errx(1, "pthread_barrier_init: %s", strerror(e));
}
 
void xtest_barrier_destroy(pthread_barrier_t *barrier)
{
   int e = pthread_barrier_destroy(barrier);
 
   if (e)
       errx(1, "pthread_barrier_destroy: %s", strerror(e));
}
 
int xtest_barrier_wait(pthread_barrier_t *barrier)
{
   int e = pthread_barrier_wait(barrier);
 
   if (e && e != PTHREAD_BARRIER_SERIAL_THREAD)
       errx(1, "pthread _barrier_wait: %s", strerror(e));
   return e;
}