1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (c) 2014, STMicroelectronics International N.V.
4 * All rights reserved.
5 */
6
7 #include <aes_taf.h>
8 #include <arith_taf.h>
9 #include <cryp_taf.h>
10 #include <derive_key_taf.h>
11 #include <mbedtls_taf.h>
12 #include <seed_rng_taf.h>
13 #include <sha2_taf.h>
14 #include <ta_crypt.h>
15 #include <tee_ta_api.h>
16 #include <trace.h>
17
18 static TEE_Result set_global(uint32_t param_types, TEE_Param params[4]);
19 static TEE_Result get_global(uint32_t param_types, TEE_Param params[4]);
20 static int _globalvalue;
21
22 /*
23 * Trusted Application Entry Points
24 */
25
26 /* Called each time a new instance is created */
TA_CreateEntryPoint(void)27 TEE_Result TA_CreateEntryPoint(void)
28 {
29 return TEE_SUCCESS;
30 }
31
32 /* Called each time an instance is destroyed */
TA_DestroyEntryPoint(void)33 void TA_DestroyEntryPoint(void)
34 {
35 }
36
37 /* Called each time a session is opened */
TA_OpenSessionEntryPoint(uint32_t nParamTypes,TEE_Param pParams[4],void ** ppSessionContext)38 TEE_Result TA_OpenSessionEntryPoint(uint32_t nParamTypes,
39 TEE_Param pParams[4],
40 void **ppSessionContext)
41 {
42 (void)nParamTypes;
43 (void)pParams;
44 (void)ppSessionContext;
45 return TEE_SUCCESS;
46 }
47
48 /* Called each time a session is closed */
TA_CloseSessionEntryPoint(void * pSessionContext)49 void TA_CloseSessionEntryPoint(void *pSessionContext)
50 {
51 (void)pSessionContext;
52 }
53
54 /*
55 * To provoke the linker to produce R_ARM_ABS32 relocations we need to
56 * pre-initilize a pointer to the function and then also call the function
57 * directly.
58 */
59 static TEE_Result (*ta_cmd_entries[])(uint32_t, TEE_Param *) = {
60 [TA_CRYPT_CMD_SHA224] = ta_entry_sha224,
61 [TA_CRYPT_CMD_SHA256] = ta_entry_sha256,
62 };
63
64 /* Called when a command is invoked */
TA_InvokeCommandEntryPoint(void * pSessionContext,uint32_t nCommandID,uint32_t nParamTypes,TEE_Param pParams[4])65 TEE_Result TA_InvokeCommandEntryPoint(void *pSessionContext,
66 uint32_t nCommandID, uint32_t nParamTypes,
67 TEE_Param pParams[4])
68 {
69 static bool use_fptr = false;
70
71 (void)pSessionContext;
72
73 switch (nCommandID) {
74 case TA_CRYPT_CMD_SHA224:
75 use_fptr = !use_fptr;
76 if (use_fptr)
77 return ta_cmd_entries[nCommandID](nParamTypes, pParams);
78 else
79 return ta_entry_sha224(nParamTypes, pParams);
80
81 case TA_CRYPT_CMD_SHA256:
82 use_fptr = !use_fptr;
83 if (use_fptr)
84 return ta_cmd_entries[nCommandID](nParamTypes, pParams);
85 else
86 return ta_entry_sha256(nParamTypes, pParams);
87
88 case TA_CRYPT_CMD_AES256ECB_ENC:
89 return ta_entry_aes256ecb_encrypt(nParamTypes, pParams);
90
91 case TA_CRYPT_CMD_AES256ECB_DEC:
92 return ta_entry_aes256ecb_decrypt(nParamTypes, pParams);
93
94 case TA_CRYPT_CMD_ALLOCATE_OPERATION:
95 return ta_entry_allocate_operation(nParamTypes, pParams);
96
97 case TA_CRYPT_CMD_FREE_OPERATION:
98 return ta_entry_free_operation(nParamTypes, pParams);
99
100 case TA_CRYPT_CMD_GET_OPERATION_INFO:
101 return ta_entry_get_operation_info(nParamTypes, pParams);
102
103 case TA_CRYPT_CMD_RESET_OPERATION:
104 return ta_entry_reset_operation(nParamTypes, pParams);
105
106 case TA_CRYPT_CMD_SET_OPERATION_KEY:
107 return ta_entry_set_operation_key(nParamTypes, pParams);
108
109 case TA_CRYPT_CMD_SET_OPERATION_KEY2:
110 return ta_entry_set_operation_key2(nParamTypes, pParams);
111
112 case TA_CRYPT_CMD_COPY_OPERATION:
113 return ta_entry_copy_operation(nParamTypes, pParams);
114
115 case TA_CRYPT_CMD_DIGEST_UPDATE:
116 return ta_entry_digest_update(nParamTypes, pParams);
117
118 case TA_CRYPT_CMD_DIGEST_DO_FINAL:
119 return ta_entry_digest_do_final(nParamTypes, pParams);
120
121 case TA_CRYPT_CMD_CIPHER_INIT:
122 return ta_entry_cipher_init(nParamTypes, pParams);
123
124 case TA_CRYPT_CMD_CIPHER_UPDATE:
125 return ta_entry_cipher_update(nParamTypes, pParams);
126
127 case TA_CRYPT_CMD_CIPHER_DO_FINAL:
128 return ta_entry_cipher_do_final(nParamTypes, pParams);
129
130 case TA_CRYPT_CMD_MAC_INIT:
131 return ta_entry_mac_init(nParamTypes, pParams);
132
133 case TA_CRYPT_CMD_MAC_UPDATE:
134 return ta_entry_mac_update(nParamTypes, pParams);
135
136 case TA_CRYPT_CMD_MAC_FINAL_COMPUTE:
137 return ta_entry_mac_final_compute(nParamTypes, pParams);
138
139 case TA_CRYPT_CMD_MAC_FINAL_COMPARE:
140 return ta_entry_mac_final_compare(nParamTypes, pParams);
141
142 case TA_CRYPT_CMD_ALLOCATE_TRANSIENT_OBJECT:
143 return ta_entry_allocate_transient_object(nParamTypes, pParams);
144
145 case TA_CRYPT_CMD_FREE_TRANSIENT_OBJECT:
146 return ta_entry_free_transient_object(nParamTypes, pParams);
147
148 case TA_CRYPT_CMD_RESET_TRANSIENT_OBJECT:
149 return ta_entry_reset_transient_object(nParamTypes, pParams);
150
151 case TA_CRYPT_CMD_POPULATE_TRANSIENT_OBJECT:
152 return ta_entry_populate_transient_object(nParamTypes, pParams);
153
154 case TA_CRYPT_CMD_COPY_OBJECT_ATTRIBUTES:
155 return ta_entry_copy_object_attributes(nParamTypes, pParams);
156
157 case TA_CRYPT_CMD_GENERATE_KEY:
158 return ta_entry_generate_key(nParamTypes, pParams);
159
160 case TA_CRYPT_CMD_ASYMMETRIC_ENCRYPT:
161 return ta_entry_asymmetric_encrypt(nParamTypes, pParams);
162
163 case TA_CRYPT_CMD_ASYMMETRIC_DECRYPT:
164 return ta_entry_asymmetric_decrypt(nParamTypes, pParams);
165
166 case TA_CRYPT_CMD_ASYMMETRIC_SIGN_DIGEST:
167 return ta_entry_asymmetric_sign_digest(nParamTypes, pParams);
168
169 case TA_CRYPT_CMD_ASYMMETRIC_VERIFY_DIGEST:
170 return ta_entry_asymmetric_verify_digest(nParamTypes, pParams);
171
172 case TA_CRYPT_CMD_DERIVE_KEY:
173 return ta_entry_derive_key(nParamTypes, pParams);
174
175 case TA_CRYPT_CMD_RANDOM_NUMBER_GENERATE:
176 return ta_entry_random_number_generate(nParamTypes, pParams);
177
178 case TA_CRYPT_CMD_AE_INIT:
179 return ta_entry_ae_init(nParamTypes, pParams);
180
181 case TA_CRYPT_CMD_AE_UPDATE_AAD:
182 return ta_entry_ae_update_aad(nParamTypes, pParams);
183
184 case TA_CRYPT_CMD_AE_UPDATE:
185 return ta_entry_ae_update(nParamTypes, pParams);
186
187 case TA_CRYPT_CMD_AE_ENCRYPT_FINAL:
188 return ta_entry_ae_encrypt_final(nParamTypes, pParams);
189
190 case TA_CRYPT_CMD_AE_DECRYPT_FINAL:
191 return ta_entry_ae_decrypt_final(nParamTypes, pParams);
192
193 case TA_CRYPT_CMD_GET_OBJECT_BUFFER_ATTRIBUTE:
194 return ta_entry_get_object_buffer_attribute(nParamTypes,
195 pParams);
196 case TA_CRYPT_CMD_GET_OBJECT_VALUE_ATTRIBUTE:
197 return ta_entry_get_object_value_attribute(nParamTypes,
198 pParams);
199 case TA_CRYPT_CMD_SETGLOBAL:
200 return set_global(nParamTypes, pParams);
201
202 case TA_CRYPT_CMD_GETGLOBAL:
203 return get_global(nParamTypes, pParams);
204
205 #ifdef CFG_TA_MBEDTLS
206 case TA_CRYPT_CMD_MBEDTLS_SELF_TESTS:
207 return ta_entry_mbedtls_self_tests(nParamTypes, pParams);
208 case TA_CRYPT_CMD_MBEDTLS_CHECK_CERT:
209 return ta_entry_mbedtls_check_cert(nParamTypes, pParams);
210 case TA_CRYPT_CMD_MBEDTLS_SIGN_CERT:
211 return ta_entry_mbedtls_sign_cert(nParamTypes, pParams);
212 #endif
213 #ifdef CFG_SYSTEM_PTA
214 case TA_CRYPT_CMD_SEED_RNG_POOL:
215 return seed_rng_pool(nParamTypes, pParams);
216 case TA_CRYPT_CMD_DERIVE_TA_UNIQUE_KEY:
217 return derive_ta_unique_key_test(nParamTypes, pParams);
218 case TA_CRYPT_CMD_DERIVE_TA_UNIQUE_KEY_SHM:
219 return derive_ta_unique_key_test_shm(nParamTypes, pParams);
220 #endif
221 case TA_CRYPT_CMD_ARITH_NEW_VAR:
222 return ta_entry_arith_new_var(nParamTypes, pParams);
223 case TA_CRYPT_CMD_ARITH_NEW_FMM_CTX:
224 return ta_entry_arith_new_fmm_ctx(nParamTypes, pParams);
225 case TA_CRYPT_CMD_ARITH_NEW_FMM_VAR:
226 return ta_entry_arith_new_fmm_var(nParamTypes, pParams);
227 case TA_CRYPT_CMD_ARITH_FREE_HANDLE:
228 return ta_entry_arith_free_handle(nParamTypes, pParams);
229 case TA_CRYPT_CMD_ARITH_FROM_OCTET_STRING:
230 return ta_entry_arith_from_octet_string(nParamTypes, pParams);
231 case TA_CRYPT_CMD_ARITH_FROM_S32:
232 return ta_entry_arith_from_s32(nParamTypes, pParams);
233 case TA_CRYPT_CMD_ARITH_GET_VALUE:
234 return ta_entry_arith_get_value(nParamTypes, pParams);
235 case TA_CRYPT_CMD_ARITH_GET_VALUE_S32:
236 return ta_entry_arith_get_value_s32(nParamTypes, pParams);
237 case TA_CRYPT_CMD_ARITH_GET_BIT:
238 return ta_entry_arith_get_bit(nParamTypes, pParams);
239 case TA_CRYPT_CMD_ARITH_GET_BIT_COUNT:
240 return ta_entry_arith_get_bit_count(nParamTypes, pParams);
241 case TA_CRYPT_CMD_ARITH_SHIFT_RIGHT:
242 return ta_entry_arith_shift_right(nParamTypes, pParams);
243 case TA_CRYPT_CMD_ARITH_CMP:
244 return ta_entry_arith_cmp(nParamTypes, pParams);
245 case TA_CRYPT_CMD_ARITH_CMP_S32:
246 return ta_entry_arith_cmp_s32(nParamTypes, pParams);
247 case TA_CRYPT_CMD_ARITH_ADD:
248 return ta_entry_arith_add(nParamTypes, pParams);
249 case TA_CRYPT_CMD_ARITH_SUB:
250 return ta_entry_arith_sub(nParamTypes, pParams);
251 case TA_CRYPT_CMD_ARITH_MUL:
252 return ta_entry_arith_mul(nParamTypes, pParams);
253 case TA_CRYPT_CMD_ARITH_NEG:
254 return ta_entry_arith_neg(nParamTypes, pParams);
255 case TA_CRYPT_CMD_ARITH_SQR:
256 return ta_entry_arith_sqr(nParamTypes, pParams);
257 case TA_CRYPT_CMD_ARITH_DIV:
258 return ta_entry_arith_div(nParamTypes, pParams);
259 case TA_CRYPT_CMD_ARITH_MOD:
260 return ta_entry_arith_mod(nParamTypes, pParams);
261 case TA_CRYPT_CMD_ARITH_ADDMOD:
262 return ta_entry_arith_addmod(nParamTypes, pParams);
263 case TA_CRYPT_CMD_ARITH_SUBMOD:
264 return ta_entry_arith_submod(nParamTypes, pParams);
265 case TA_CRYPT_CMD_ARITH_MULMOD:
266 return ta_entry_arith_mulmod(nParamTypes, pParams);
267 case TA_CRYPT_CMD_ARITH_SQRMOD:
268 return ta_entry_arith_sqrmod(nParamTypes, pParams);
269 case TA_CRYPT_CMD_ARITH_INVMOD:
270 return ta_entry_arith_invmod(nParamTypes, pParams);
271 case TA_CRYPT_CMD_ARITH_IS_RELATIVE_PRIME:
272 return ta_entry_arith_is_rel_prime(nParamTypes, pParams);
273 case TA_CRYPT_CMD_ARITH_COMPUTE_EGCD:
274 return ta_entry_arith_compute_egcd(nParamTypes, pParams);
275 case TA_CRYPT_CMD_ARITH_IS_PRIME:
276 return ta_entry_arith_is_prime(nParamTypes, pParams);
277 case TA_CRYPT_CMD_ARITH_TO_FMM:
278 return ta_entry_arith_to_fmm(nParamTypes, pParams);
279 case TA_CRYPT_CMD_ARITH_FROM_FMM:
280 return ta_entry_arith_from_fmm(nParamTypes, pParams);
281 case TA_CRYPT_CMD_ARITH_COMPUTE_FMM:
282 return ta_entry_arith_compute_fmm(nParamTypes, pParams);
283 case TA_CRYPT_CMD_IS_ALGO_SUPPORTED:
284 return ta_entry_is_algo_supported(nParamTypes, pParams);
285
286 default:
287 return TEE_ERROR_BAD_PARAMETERS;
288 }
289 }
290
set_global(uint32_t param_types,TEE_Param params[4])291 static TEE_Result set_global(uint32_t param_types, TEE_Param params[4])
292 {
293 int i = 0;
294
295 /* Param 0 is a memref, input/output */
296 if (TEE_PARAM_TYPE_VALUE_INPUT != TEE_PARAM_TYPE_GET(param_types, 0))
297 return TEE_ERROR_BAD_PARAMETERS;
298
299 /* Other parameters must be of type TEE_PARAM_TYPE_NONE */
300 for (i = 1; i < 4; i++) {
301 if (TEE_PARAM_TYPE_NONE != TEE_PARAM_TYPE_GET(param_types, i))
302 return TEE_ERROR_BAD_PARAMETERS;
303 }
304
305 _globalvalue = params[0].value.a;
306 return TEE_SUCCESS;
307 }
308
get_global(uint32_t param_types,TEE_Param params[4])309 static TEE_Result get_global(uint32_t param_types, TEE_Param params[4])
310 {
311 int i = 0;
312
313 /* Param 0 is a memref, input/output */
314 if (TEE_PARAM_TYPE_VALUE_OUTPUT != TEE_PARAM_TYPE_GET(param_types, 0))
315 return TEE_ERROR_BAD_PARAMETERS;
316
317 /* Other parameters must be of type TEE_PARAM_TYPE_NONE */
318 for (i = 1; i < 4; i++) {
319 if (TEE_PARAM_TYPE_NONE != TEE_PARAM_TYPE_GET(param_types, i))
320 return TEE_ERROR_BAD_PARAMETERS;
321 }
322
323 params[0].value.a = _globalvalue;
324 return TEE_SUCCESS;
325 }
326