hc
2023-11-22 d0a428a6556ea5a006e22e28b0b1cd037885fe20
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
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2016, Linaro Limited
 * All rights reserved.
 */
 
#include <pta_invoke_tests.h>
#include <string.h>
#include <ta_sdp_basic.h>
#include <tee_api.h>
#include <tee_internal_api_extensions.h>
#include <tee_internal_api.h>
#include <tee_ta_api.h>
#include <trace.h>
 
 
/*
 * Basic Secure Data Path access test commands:
 * - command INJECT: copy from non secure input into secure output.
 * - command TRANSFROM: read, transform and write from/to secure in/out.
 * - command DUMP: copy from secure input into non secure output.
 */
 
static TEE_Result cmd_inject(uint32_t types,
                TEE_Param params[TEE_NUM_PARAMS])
{
   TEE_Result rc = TEE_ERROR_GENERIC;
   const int sec_idx = 1;        /* highlight secure buffer index */
   const int ns_idx = 0;        /* highlight nonsecure buffer index */
 
   if (types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
                    TEE_PARAM_TYPE_MEMREF_OUTPUT,
                    TEE_PARAM_TYPE_NONE,
                    TEE_PARAM_TYPE_NONE)) {
       EMSG("bad parameters %x", (unsigned)types);
       return TEE_ERROR_BAD_PARAMETERS;
   }
 
   if (params[sec_idx].memref.size < params[ns_idx].memref.size)
       return TEE_ERROR_SHORT_BUFFER;
 
   /*
    * We could rely on the TEE to provide consistent buffer/size values
    * to reference a buffer with a unique and consistent secure attribute
    * value. Hence it is safe enough (and more optimal) to test only the
    * secure attribute of a single byte of it. Yet, since the current
    * test does not deal with performance, let check the secure attribute
    * of each byte of the buffer.
    */
   rc = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_ANY_OWNER |
                    TEE_MEMORY_ACCESS_READ |
                    TEE_MEMORY_ACCESS_NONSECURE,
                    params[ns_idx].memref.buffer,
                    params[ns_idx].memref.size);
   if (rc != TEE_SUCCESS) {
       EMSG("TEE_CheckMemoryAccessRights(nsec) failed %x", rc);
       return rc;
   }
 
   rc = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_ANY_OWNER |
                    TEE_MEMORY_ACCESS_WRITE |
                    TEE_MEMORY_ACCESS_SECURE,
                    params[sec_idx].memref.buffer,
                    params[sec_idx].memref.size);
   if (rc != TEE_SUCCESS) {
       EMSG("TEE_CheckMemoryAccessRights(secure) failed %x", rc);
       return rc;
   }
 
 
#ifdef CFG_CACHE_API
   /*
    * we should invalidate cache (here we assume buffer were not
    * filled through cpu core caches. We flush buffers so that
    * cache is not corrupted in cache target buffer not aligned
    * on cache line size.
    */
   rc = TEE_CacheFlush(params[sec_idx].memref.buffer,
               params[sec_idx].memref.size);
   if (rc != TEE_SUCCESS) {
       EMSG("TEE_CacheFlush(%p, %x) failed: 0x%x",
                   params[sec_idx].memref.buffer,
                   params[sec_idx].memref.size, rc);
       return rc;
   }
#endif /* CFG_CACHE_API */
 
   /* inject data */
   TEE_MemMove(params[sec_idx].memref.buffer,
           params[ns_idx].memref.buffer,
           params[sec_idx].memref.size);
 
#ifdef CFG_CACHE_API
   rc = TEE_CacheFlush(params[sec_idx].memref.buffer,
               params[sec_idx].memref.size);
   if (rc != TEE_SUCCESS) {
       EMSG("TEE_CacheFlush(%p, %x) failed: 0x%x",
                   params[sec_idx].memref.buffer,
                   params[sec_idx].memref.size, rc);
       return rc;
   }
#endif /* CFG_CACHE_API */
 
   return rc;
}
 
static TEE_Result cmd_transform(uint32_t types,
               TEE_Param params[TEE_NUM_PARAMS])
{
   TEE_Result rc = TEE_ERROR_GENERIC;
   unsigned char *p = NULL;
   size_t sz = 0;
 
   if (types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
                    TEE_PARAM_TYPE_NONE,
                    TEE_PARAM_TYPE_NONE,
                    TEE_PARAM_TYPE_NONE))
       return TEE_ERROR_BAD_PARAMETERS;
 
   /*
    * We could rely on the TEE to provide consistent buffer/size values
    * to reference a buffer with a unique and consistent secure attribute
    * value. Hence it is safe enough (and more optimal) to test only the
    * secure attribute of a single byte of it. Yet, since the current
    * test does not deal with performance, let check the secure attribute
    * of each byte of the buffer.
    */
   rc = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_ANY_OWNER |
                    TEE_MEMORY_ACCESS_READ |
                    TEE_MEMORY_ACCESS_WRITE |
                    TEE_MEMORY_ACCESS_SECURE,
                    params[0].memref.buffer,
                    params[0].memref.size);
   if (rc != TEE_SUCCESS) {
       EMSG("TEE_CheckMemoryAccessRights(secure) failed %x", rc);
       return rc;
   }
 
 
#ifdef CFG_CACHE_API
   /*
    * we should invalidate cache (here we assume buffer were not
    * filled through cpu core caches. We flush buffers so that
    * cache is not corrupted in cache target buffer not aligned
    * on cache line size.
    */
   rc = TEE_CacheFlush(params[0].memref.buffer,
               params[0].memref.size);
   if (rc != TEE_SUCCESS) {
       EMSG("TEE_CacheFlush(%p, %x) failed: 0x%x",
                   params[0].memref.buffer,
                   params[0].memref.size, rc);
       return rc;
   }
#endif /* CFG_CACHE_API */
 
   /* transform the data */
   p = (unsigned char *)params[0].memref.buffer;
   sz = params[0].memref.size;
   for (; sz; sz--, p++)
           *p = ~(*p) + 1;
 
#ifdef CFG_CACHE_API
   rc = TEE_CacheFlush(params[0].memref.buffer,
               params[0].memref.size);
   if (rc != TEE_SUCCESS) {
       EMSG("TEE_CacheFlush(%p, %x) failed: 0x%x",
                   params[0].memref.buffer,
                   params[0].memref.size, rc);
       return rc;
   }
#endif /* CFG_CACHE_API */
 
   return rc;
}
 
static TEE_Result cmd_dump(uint32_t types,
              TEE_Param params[TEE_NUM_PARAMS])
{
   TEE_Result rc = TEE_ERROR_GENERIC;
   const int sec_idx = 0;        /* highlight secure buffer index */
   const int ns_idx = 1;        /* highlight nonsecure buffer index */
 
   if (types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
                    TEE_PARAM_TYPE_MEMREF_OUTPUT,
                    TEE_PARAM_TYPE_NONE,
                    TEE_PARAM_TYPE_NONE))
       return TEE_ERROR_BAD_PARAMETERS;
 
   if (params[ns_idx].memref.size < params[sec_idx].memref.size)
       return TEE_ERROR_SHORT_BUFFER;
 
   /*
    * We could rely on the TEE to provide consistent buffer/size values
    * to reference a buffer with a unique and consistent secure attribute
    * value. Hence it is safe enough (and more optimal) to test only the
    * secure attribute of a single byte of it. Yet, since the current
    * test does not deal with performance, let check the secure attribute
    * of each byte of the buffer.
    */
   rc = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_ANY_OWNER |
                    TEE_MEMORY_ACCESS_WRITE |
                    TEE_MEMORY_ACCESS_NONSECURE,
                    params[ns_idx].memref.buffer,
                    params[ns_idx].memref.size);
   if (rc != TEE_SUCCESS) {
       EMSG("TEE_CheckMemoryAccessRights(nsec) failed %x", rc);
       return rc;
   }
 
   rc = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_ANY_OWNER |
                    TEE_MEMORY_ACCESS_READ |
                    TEE_MEMORY_ACCESS_SECURE,
                    params[sec_idx].memref.buffer,
                    params[sec_idx].memref.size);
   if (rc != TEE_SUCCESS) {
       EMSG("TEE_CheckMemoryAccessRights(secure) failed %x", rc);
       return rc;
   }
 
#ifdef CFG_CACHE_API
   /*
    * we should invalidate cache (here we assume buffer were not
    * filled through cpu core caches. We flush buffers so that
    * cache is not corrupted in cache target buffer not aligned
    * on cache line size.
    */
   rc = TEE_CacheFlush(params[sec_idx].memref.buffer,
               params[sec_idx].memref.size);
   if (rc != TEE_SUCCESS) {
       EMSG("TEE_CacheFlush(%p, %x) failed: 0x%x",
                   params[sec_idx].memref.buffer,
                   params[sec_idx].memref.size, rc);
       return rc;
   }
#endif /* CFG_CACHE_API */
 
   /* dump the data */
   TEE_MemMove(params[ns_idx].memref.buffer,
           params[sec_idx].memref.buffer,
           params[sec_idx].memref.size);
 
   return rc;
}
 
static TEE_Result cmd_invoke(uint32_t nParamTypes,
                TEE_Param pParams[TEE_NUM_PARAMS],
                uint32_t nCommandID)
{
        const TEE_UUID uuid = TA_SDP_BASIC_UUID;
        static TEE_TASessionHandle sess = TEE_HANDLE_NULL;
        uint32_t ret_orig = 0;
        TEE_Result res = TEE_ERROR_GENERIC;
 
   if (sess == TEE_HANDLE_NULL) {
           res = TEE_OpenTASession(&uuid, TEE_TIMEOUT_INFINITE, 0, NULL,
                   &sess, &ret_orig);
       if (res != TEE_SUCCESS) {
                   EMSG("SDP basic test TA: TEE_OpenTASession() FAILED \n");
                   goto cleanup_return;
       }
 
        }
 
        res = TEE_InvokeTACommand(sess, TEE_TIMEOUT_INFINITE,
                 nCommandID, nParamTypes, pParams, &ret_orig);
        if (res != TEE_SUCCESS) {
                EMSG("SDP basic test TA: TEE_OpenTASession() FAILED %x/%d\n",
                               res, ret_orig);
        }
 
cleanup_return:
   if (res != TEE_SUCCESS) {
       TEE_CloseTASession(sess);
       sess = TEE_HANDLE_NULL;
   }
        return res;
}
 
static TEE_Result cmd_invoke_pta(uint32_t nParamTypes,
                TEE_Param pParams[TEE_NUM_PARAMS],
                uint32_t nCommandID)
{
        const TEE_UUID uuid = PTA_INVOKE_TESTS_UUID;
        static TEE_TASessionHandle sess = TEE_HANDLE_NULL;
        uint32_t ret_orig = 0;
        TEE_Result res = TEE_ERROR_GENERIC;
 
   if (sess == TEE_HANDLE_NULL) {
           res = TEE_OpenTASession(&uuid, TEE_TIMEOUT_INFINITE, 0, NULL,
                   &sess, &ret_orig);
       if (res != TEE_SUCCESS) {
                   EMSG("SDP basic test TA: TEE_OpenTASession() FAILED \n");
                   goto cleanup_return;
       }
 
        }
 
        res = TEE_InvokeTACommand(sess, TEE_TIMEOUT_INFINITE,
                 nCommandID, nParamTypes, pParams, &ret_orig);
        if (res != TEE_SUCCESS) {
                EMSG("SDP basic test TA: TEE_OpenTASession() FAILED %x/%d\n",
                               res, ret_orig);
        }
 
cleanup_return:
   if (res != TEE_SUCCESS) {
       TEE_CloseTASession(sess);
       sess = TEE_HANDLE_NULL;
   }
        return res;
}
 
TEE_Result TA_CreateEntryPoint(void)
{
   return TEE_SUCCESS;
}
 
void TA_DestroyEntryPoint(void)
{
}
 
TEE_Result TA_OpenSessionEntryPoint(uint32_t nParamTypes,
                   TEE_Param pParams[TEE_NUM_PARAMS],
                   void **ppSessionContext)
{
   (void)nParamTypes;
   (void)pParams;
   (void)ppSessionContext;
   return TEE_SUCCESS;
}
 
void TA_CloseSessionEntryPoint(void *pSessionContext)
{
   (void)pSessionContext;
}
 
TEE_Result TA_InvokeCommandEntryPoint(void *pSessionContext,
                     uint32_t nCommandID, uint32_t nParamTypes,
                     TEE_Param pParams[TEE_NUM_PARAMS])
{
   (void)pSessionContext;
 
   switch (nCommandID) {
   case TA_SDP_BASIC_CMD_INJECT:
       return cmd_inject(nParamTypes, pParams);
   case TA_SDP_BASIC_CMD_TRANSFORM:
       return cmd_transform(nParamTypes, pParams);
   case TA_SDP_BASIC_CMD_DUMP:
       return cmd_dump(nParamTypes, pParams);
 
   case TA_SDP_BASIC_CMD_INVOKE_INJECT:
       return cmd_invoke(nParamTypes, pParams, TA_SDP_BASIC_CMD_INJECT);
   case TA_SDP_BASIC_CMD_INVOKE_TRANSFORM:
       return cmd_invoke(nParamTypes, pParams, TA_SDP_BASIC_CMD_TRANSFORM);
   case TA_SDP_BASIC_CMD_INVOKE_DUMP:
       return cmd_invoke(nParamTypes, pParams, TA_SDP_BASIC_CMD_DUMP);
 
   case TA_SDP_BASIC_CMD_PTA_INJECT:
       return cmd_invoke_pta(nParamTypes, pParams, PTA_INVOKE_TESTS_CMD_COPY_NSEC_TO_SEC);
   case TA_SDP_BASIC_CMD_PTA_TRANSFORM:
       return cmd_invoke_pta(nParamTypes, pParams, PTA_INVOKE_TESTS_CMD_READ_MODIFY_SEC);
   case TA_SDP_BASIC_CMD_PTA_DUMP:
       return cmd_invoke_pta(nParamTypes, pParams, PTA_INVOKE_TESTS_CMD_COPY_SEC_TO_NSEC);
 
   default:
       return TEE_ERROR_BAD_PARAMETERS;
   }
}