hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
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
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2019, Linaro Limited
 */
 
#include <pta_system.h>
#include <string.h>
#include <tee_internal_api.h>
 
#include "derive_key_taf.h"
 
#define TA_DERIVED_KEY_MIN_SIZE        16
#define TA_DERIVED_KEY_MAX_SIZE        32
#define TA_DERIVED_EXTRA_DATA_MAX_SIZE    1024
 
static const TEE_UUID system_uuid = PTA_SYSTEM_UUID;
 
/*
 * Helper function that just derives a key.
 */
static TEE_Result derive_unique_key(TEE_TASessionHandle session,
                   uint8_t *key, uint16_t key_size,
                   uint8_t *extra, uint16_t extra_size)
{
   TEE_Param params[TEE_NUM_PARAMS] = { 0 };
   TEE_Result res = TEE_ERROR_GENERIC;
   uint32_t ret_origin = 0;
   uint32_t param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
                          TEE_PARAM_TYPE_MEMREF_OUTPUT,
                          TEE_PARAM_TYPE_NONE,
                          TEE_PARAM_TYPE_NONE);
   if (extra && extra_size > 0) {
       params[0].memref.buffer = extra;
       params[0].memref.size = extra_size;
   }
 
   params[1].memref.buffer = key;
   params[1].memref.size = key_size;
 
   res = TEE_InvokeTACommand(session, TEE_TIMEOUT_INFINITE,
                 PTA_SYSTEM_DERIVE_TA_UNIQUE_KEY,
                 param_types, params, &ret_origin);
   if (res != TEE_SUCCESS)
       EMSG("Failure when calling PTA_SYSTEM_DERIVE_TA_UNIQUE_KEY");
 
   return res;
}
 
TEE_Result derive_ta_unique_key_test(uint32_t param_types,
                    TEE_Param params[TEE_NUM_PARAMS] __unused)
{
   size_t i = 0;
   TEE_Result res_final = TEE_SUCCESS;
   TEE_Result res = TEE_ERROR_GENERIC;
   TEE_TASessionHandle session = TEE_HANDLE_NULL;
   uint8_t big_key[64] = { };
   uint8_t extra_key_data[] = { "My dummy data" };
   uint8_t key1[32] = { };
   uint8_t key2[32] = { };
   uint32_t ret_origin = 0;
 
   if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE,
                      TEE_PARAM_TYPE_NONE))
       return TEE_ERROR_BAD_PARAMETERS;
 
   res = TEE_OpenTASession(&system_uuid, TEE_TIMEOUT_INFINITE, 0, NULL,
               &session, &ret_origin);
 
   if (res != TEE_SUCCESS)
       return res;
 
   /*
    * Testing for successful calls to the PTA and that two calls with same
    * input data generates the same output data (keys).
    */
   res = derive_unique_key(session, key1, sizeof(key1), NULL, 0);
   if (res != TEE_SUCCESS)
       res_final = TEE_ERROR_GENERIC;
 
   res = derive_unique_key(session, key2, sizeof(key2), NULL, 0);
   if (res != TEE_SUCCESS)
       res_final = TEE_ERROR_GENERIC;
 
   if (TEE_MemCompare(key1, key2, sizeof(key1)) != 0)
       res_final = TEE_ERROR_GENERIC;
 
   TEE_MemFill(key1, 0, sizeof(key1));
   TEE_MemFill(key2, 0, sizeof(key2));
 
   /*
    * Testing for successful calls to the PTA and that two calls with same
    * input data generates the same output data (keys). Here we are using
    * extra data also.
    */
   res = derive_unique_key(session, key1, sizeof(key1), extra_key_data,
               sizeof(extra_key_data));
   if (res != TEE_SUCCESS)
       res_final = TEE_ERROR_GENERIC;
 
   res = derive_unique_key(session, key2, sizeof(key2), extra_key_data,
               sizeof(extra_key_data));
   if (res != TEE_SUCCESS)
       res_final = TEE_ERROR_GENERIC;
 
   if (TEE_MemCompare(key1, key2, sizeof(key1)) != 0)
       res_final = TEE_ERROR_GENERIC;
 
   TEE_MemFill(key1, 0, sizeof(key1));
   TEE_MemFill(key2, 0, sizeof(key2));
 
   /*
    * Testing for successful calls to the PTA and that two calls with
    * different input data do not generate the same output data (keys). We
    * do that by using one key with and one key without extra data.
    */
   res = derive_unique_key(session, key1, sizeof(key1), extra_key_data,
               sizeof(extra_key_data));
   if (res != TEE_SUCCESS)
       res_final = TEE_ERROR_GENERIC;
 
   res = derive_unique_key(session, key2, sizeof(key2), NULL, 0);
   if (res != TEE_SUCCESS)
       res_final = TEE_ERROR_GENERIC;
 
   /* They should not be equal */
   if (TEE_MemCompare(key1, key2, sizeof(key1)) == 0)
       res_final = TEE_ERROR_GENERIC;
 
   TEE_MemFill(key1, 0, sizeof(key1));
   TEE_MemFill(key2, 0, sizeof(key2));
 
   /*
    * Testing limits for extra data size (if this would success, then we
    * would overwrite the buffer extra_key_data also).
    */
   res = derive_unique_key(session, key1, sizeof(key1), extra_key_data,
               TA_DERIVED_EXTRA_DATA_MAX_SIZE + 1);
   /* This shall fail */
   if (res == TEE_SUCCESS)
       res_final = TEE_ERROR_GENERIC;
 
   TEE_MemFill(key1, 0, sizeof(key1));
 
   /* Testing limits. */
   for (i = 0; i < sizeof(big_key); i++) {
       uint8_t *extra = NULL;
       uint8_t extra_size = 0;
 
       TEE_MemFill(big_key, 0, sizeof(big_key));
 
       /* Alternate between using and not using extra data. */
       if (i % 2) {
           extra = extra_key_data;
           extra_size = sizeof(extra_key_data);
       }
 
       res = derive_unique_key(session, big_key, i, extra, extra_size);
       if (i < TA_DERIVED_KEY_MIN_SIZE) {
           if (res != TEE_SUCCESS)
               continue;
 
           EMSG("Small key test iteration %zu failed", i);
           res_final = TEE_ERROR_GENERIC;
           break;
       }
 
       if (i > TA_DERIVED_KEY_MAX_SIZE) {
           if (res != TEE_SUCCESS)
               continue;
 
           EMSG("Big key test iteration %zu failed", i);
           res_final = TEE_ERROR_GENERIC;
           break;
       }
 
       if (res != TEE_SUCCESS) {
           res_final = TEE_ERROR_GENERIC;
           break;
       }
   }
 
   TEE_CloseTASession(session);
 
   return res_final;
}
 
TEE_Result derive_ta_unique_key_test_shm(uint32_t param_types,
                    TEE_Param params[TEE_NUM_PARAMS])
{
   TEE_Result res = TEE_ERROR_GENERIC;
   TEE_TASessionHandle session = TEE_HANDLE_NULL;
   uint32_t ret_origin = 0;
 
   if (param_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;
 
   res = TEE_OpenTASession(&system_uuid, TEE_TIMEOUT_INFINITE, 0, NULL,
               &session, &ret_origin);
 
   if (res != TEE_SUCCESS)
       return res;
 
   /*
    * Testing for unsuccessful calls to the PTA. They should be
    * unsuccessful since we are using an out buffer coming from normal
    * world.
    */
   res = TEE_InvokeTACommand(session, TEE_TIMEOUT_INFINITE,
                 PTA_SYSTEM_DERIVE_TA_UNIQUE_KEY,
                 param_types, params, &ret_origin);
   TEE_CloseTASession(session);
 
   return res;
}