1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2015-2016, Linaro Limited
4  * All rights reserved.
5  */
6 
7 #include <atomic.h>
8 #include <tee_ta_api.h>
9 #include <tee_api.h>
10 #include <ta_concurrent_large.h>
11 #include <trace.h>
12 #include <utee_defines.h>
13 
TA_CreateEntryPoint(void)14 TEE_Result TA_CreateEntryPoint(void)
15 {
16 	return TEE_SUCCESS;
17 }
18 
TA_DestroyEntryPoint(void)19 void TA_DestroyEntryPoint(void)
20 {
21 }
22 
TA_OpenSessionEntryPoint(uint32_t param_types,TEE_Param params[4],void ** session_ctx)23 TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types,
24 				    TEE_Param params[4],
25 				    void **session_ctx)
26 {
27 	(void)param_types;
28 	(void)params;
29 	(void)session_ctx;
30 	return TEE_SUCCESS;
31 }
32 
TA_CloseSessionEntryPoint(void * session_ctx)33 void TA_CloseSessionEntryPoint(void *session_ctx)
34 {
35 	(void)session_ctx;
36 }
37 
inc_active_count(struct ta_concurrent_shm * shm)38 static uint32_t inc_active_count(struct ta_concurrent_shm *shm)
39 {
40 	return atomic_inc32(&shm->active_count);
41 }
42 
dec_active_count(struct ta_concurrent_shm * shm)43 static uint32_t dec_active_count(struct ta_concurrent_shm *shm)
44 {
45 	return atomic_dec32(&shm->active_count);
46 }
47 
ta_entry_busy_loop(uint32_t param_types,TEE_Param params[4])48 static TEE_Result ta_entry_busy_loop(uint32_t param_types, TEE_Param params[4])
49 {
50 	size_t num_rounds = 0;
51 	uint32_t req_param_types =
52 		TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
53 				TEE_PARAM_TYPE_VALUE_INOUT,
54 				TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE);
55 
56 	if (param_types != req_param_types) {
57 		EMSG("got param_types 0x%x, expected 0x%x",
58 			param_types, req_param_types);
59 		return TEE_ERROR_BAD_PARAMETERS;
60 	}
61 
62 	if (params[0].memref.size < sizeof(struct ta_concurrent_shm))
63 		return TEE_ERROR_BAD_PARAMETERS;
64 
65 	params[1].value.b = inc_active_count(params[0].memref.buffer);
66 
67 	num_rounds = params[1].value.a;
68 	while (num_rounds) {
69 		volatile size_t n = 1000;
70 
71 		while (n)
72 			n--;
73 
74 		num_rounds--;
75 	}
76 
77 	dec_active_count(params[0].memref.buffer);
78 	return TEE_SUCCESS;
79 }
80 
ta_entry_sha256(uint32_t param_types,TEE_Param params[4])81 static TEE_Result ta_entry_sha256(uint32_t param_types, TEE_Param params[4])
82 {
83 	TEE_Result res = TEE_ERROR_GENERIC;
84 	TEE_OperationHandle op = TEE_HANDLE_NULL;
85 	void *out = NULL;
86 	uint32_t out_len = 0;
87 	size_t num_rounds = 0;
88 	uint32_t req_param_types =
89 		TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
90 				TEE_PARAM_TYPE_VALUE_INOUT,
91 				TEE_PARAM_TYPE_MEMREF_INPUT,
92 				TEE_PARAM_TYPE_MEMREF_OUTPUT);
93 
94 	if (param_types != req_param_types) {
95 		EMSG("got param_types 0x%x, expected 0x%x",
96 			param_types, req_param_types);
97 		return TEE_ERROR_BAD_PARAMETERS;
98 	}
99 
100 	if (params[0].memref.size < sizeof(struct ta_concurrent_shm))
101 		return TEE_ERROR_BAD_PARAMETERS;
102 	if (params[3].memref.size < TEE_SHA256_HASH_SIZE)
103 		return TEE_ERROR_BAD_PARAMETERS;
104 
105 	params[1].value.b = inc_active_count(params[0].memref.buffer);
106 
107 	out_len = params[3].memref.size;
108 	out = TEE_Malloc(out_len, 0);
109 	if (!out) {
110 		res = TEE_ERROR_OUT_OF_MEMORY;
111 		goto out;
112 	}
113 
114 	res = TEE_AllocateOperation(&op, TEE_ALG_SHA256, TEE_MODE_DIGEST, 0);
115 	if (res != TEE_SUCCESS)
116 		goto out;
117 
118 
119 	num_rounds = params[1].value.a;
120 	while (num_rounds) {
121 		TEE_ResetOperation(op);
122 		res = TEE_DigestDoFinal(op, params[2].memref.buffer,
123 					params[2].memref.size, out, &out_len);
124 		num_rounds--;
125 	}
126 
127 	TEE_MemMove(params[3].memref.buffer, out, out_len);
128 	params[3].memref.size = out_len;
129 
130 out:
131 	if (out)
132 		TEE_Free(out);
133 	if (op)
134 		TEE_FreeOperation(op);
135 	dec_active_count(params[0].memref.buffer);
136 	return res;
137 }
138 
TA_InvokeCommandEntryPoint(void * session_ctx,uint32_t cmd_id,uint32_t param_types,TEE_Param params[4])139 TEE_Result TA_InvokeCommandEntryPoint(void *session_ctx,
140 				      uint32_t cmd_id, uint32_t param_types,
141 				      TEE_Param params[4])
142 {
143 	(void)session_ctx;
144 
145 	switch (cmd_id) {
146 	case TA_CONCURRENT_CMD_BUSY_LOOP:
147 		return ta_entry_busy_loop(param_types, params);
148 	case TA_CONCURRENT_CMD_SHA256:
149 		return ta_entry_sha256(param_types, params);
150 	default:
151 		return TEE_ERROR_BAD_PARAMETERS;
152 	}
153 }
154