1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2020, Linaro Limited
5  */
6 
7 #ifndef XML_TIMEARITHM_API_H_
8 #define XML_TIMEARITHM_API_H_
9 
10 #include <assert.h>
11 #include <openssl/bn.h>
12 #include <pthread.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <tee_api_defines.h>
18 #include <tee_client_api.h>
19 #include <unistd.h>
20 
21 #include "xml_common_api.h"
22 #include "xtest_helpers.h"
23 #include "xtest_test.h"
24 
25 #define Invoke_GetSystemTime Invoke_Simple_Function
26 #define Invoke_GetREETime Invoke_Simple_Function
27 #define Invoke_SetTAPersistentTime_and_GetTAPersistentTime_Overflow \
28 	Invoke_Simple_Function
29 #define Invoke_SetTAPersistentTime_and_GetTAPersistentTime \
30 	Invoke_Simple_Function
31 #define Invoke_BigIntConvertToOctetStringOverflow Invoke_Simple_Function
32 #define Invoke_BigIntConvertFromOctetStringOverflow Invoke_Simple_Function
33 #define Invoke_GetTAPersistentTime_NotSet_and_SetTAPersistentTime \
34 	Invoke_Simple_Function
35 #define Test_Tool_Erase_Persistent_Time Invoke_Simple_Function
36 
37 #define CMD_TEE_BigIntAdd			0x00000010
38 #define CMD_TEE_BigIntAddMod			0x00000011
39 #define CMD_TEE_BigIntCmp			0x00000012
40 #define CMD_TEE_BigIntCmpS32			0x00000013
41 #define CMD_TEE_BigIntComputeExtendedGcd	0x00000014
42 #define CMD_TEE_BigIntDiv			0x00000018
43 #define CMD_TEE_BigIntGetBit			0x00000020
44 #define CMD_TEE_BigIntGetBitCount		0x00000021
45 #define CMD_TEE_BigIntInvMod			0x00000023
46 #define CMD_TEE_BigIntIsProbablePrime		0x00000024
47 #define CMD_TEE_BigIntMod			0x00000025
48 #define CMD_TEE_BigIntMul			0x00000026
49 #define CMD_TEE_BigIntMulMod			0x00000027
50 #define CMD_TEE_BigIntNeg			0x00000028
51 #define CMD_TEE_BigIntRelativePrime		0x00000029
52 #define CMD_TEE_BigIntShiftRight		0x00000030
53 #define CMD_TEE_BigIntSquare			0x00000031
54 #define CMD_TEE_BigIntSquareMod			0x00000032
55 #define CMD_TEE_BigIntSub			0x00000033
56 #define CMD_TEE_BigIntSubMod			0x00000034
57 #define CMD_Arithm_TTA_Store_Value_S32		0x00000038
58 #define CMD_Arithm_TTA_New_BigInt		0x00000039
59 #define CMD_Arithm_BigIntConvertToOctetString	0x00000040
60 #define CMD_Arithm_BigIntComputeFMM		0x00000041
61 #define CMD_Arithm_BigIntConvertFromFMM		0x00000042
62 #define CMD_Arithm_BigIntConvertToFMM		0x00000043
63 #define CMD_Arithm_BigIntInitFMMContext		0x00000044
64 #define CMD_Arithm_TTA_New_BigIntFMM		0x00000045
65 #define CMD_Arithm_BigIntConvertFromOctetString 0x00000046
66 #define CMD_Arithm_BigIntConvertToS32		0x00000047
67 #define CMD_Arithm_BigIntConvertFromS32		0x00000048
68 
69 #define CMD_TEE_GetSystemTime                                        0x00000010
70 #define CMD_TEE_Wait                                                 0x00000011
71 #define CMD_TEE_SetTAPersistentTime_and_GetTAPersistentTime          0x00000012
72 #define CMD_TEE_GetREETime                                           0x00000013
73 #define CMD_TEE_SetTAPersistentTime_and_GetTAPersistentTimeOverflow  0x00000014
74 #define CMD_TEE_GetTAPersistentTimeNotSetAndSetTAPersistentTime      0x00000015
75 
76 
77 static TEEC_Session session01;
78 static TEEC_Session *SESSION01 = &session01;
79 static TEEC_Context context01;
80 static TEEC_Context *CONTEXT01 = &context01;
81 static TEEC_Context context02;
82 static TEEC_Context *CONTEXT02 = &context02;
83 static TEEC_Operation operation01;
84 static TEEC_Operation *OPERATION01 = &operation01;
85 
86 #define CMD_TEE_SetTAPersistentTime_and_GetTAPersistentTime_Overflow \
87 	CMD_TEE_SetTAPersistentTime_and_GetTAPersistentTimeOverflow
88 
89 #define CLIENT_APP01                    NULL
90 
91 #define TEEC_UNDEFINED_ERROR 0xDEADDEAD
92 
93 #define TEEC_ORIGIN_ANY_NOT_TRUSTED_APP  0x00000005
94 /* same as TEE_ORIGIN_NOT_TRUSTED_APP */
95 
96 static uint32_t saved_octet_string_sign;
97 static void *saved_octet_string;
98 static size_t saved_octet_string_size;
99 
100 /*Test data defines*/
101 static pthread_t THREAD02;
102 
103 #define BIT0_MASK 1
104 #define BIT1_MASK 2
105 #define BIT2_MASK 4
106 
107 #define NEGATIVE 0
108 #define POSITIVE 1
109 
110 #define RESULT_NOT_A_PRIME_SURE		0
111 #define RESULT_PRIME_SURE_OR_MAYBE	1
112 
113 #define RESULT_NOT_A_PRIME 0
114 #define RESULT_PRIME 1
115 
116 #define RESULT_EQUAL 0
117 #define RESULT_INTEGER_GREATER_THAN_ZERO 1
118 #define RESULT_INTEGER_LOWER_THAN_ZERO 2
119 
120 #define CASE_WAIT_CANCELLED 1
121 #define CASE_WAIT_SUCCESS 2
122 
123 
124 #define STORED_VALUE_BIG_INT_1			0
125 #define STORED_VALUE_BIG_INT_2			1
126 #define STORED_VALUE_BIG_INT_3			8
127 #define STORED_VALUE_BIG_INT_4			9
128 #define STORED_VALUE_FMM_BIG_INT_1		310
129 #define STORED_VALUE_FMM_BIG_INT_2		311
130 #define STORED_VALUE_FMM_CONTEXT_1		300
131 #define STORED_VALUE_FMM_RESULT			312
132 #define STORED_VALUE_MODULUS			2
133 #define STORED_VALUE_RESULT			3
134 #define STORED_VALUE_RESULT_4			6
135 #define STORED_VALUE_RESULT_5			7
136 #define STORED_VALUE_RESULT_FMM_CONVERTED	10
137 #define STORED_VALUE_RESULT_U			4
138 #define STORED_VALUE_RESULT_V			5
139 #define STORED_VALUE_SHORT_VAL_1		100
140 #define STORED_VALUE_NULL			200
141 
142 static uint32_t CONFIDENCE_LEVEL_80 = 80;
143 
144 static uint8_t BIG_VALUE1_SIZE_64_BITS[] = {
145 	0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
146 };
147 
148 static uint8_t BIG_VALUE11_SIZE_2048_BITS_PRIME_WITH_VALUE5[] = {
149 	0x0E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
150 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
151 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
152 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
153 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
154 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
155 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
156 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
157 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
158 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
159 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
160 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
161 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
162 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
163 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
164 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
165 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
166 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
167 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
168 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
169 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
170 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
171 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
172 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
173 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
174 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
175 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
176 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
177 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
178 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
179 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
180 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
181 };
182 
183 static uint8_t BIG_VALUE20_SIZE_2048_BITS_PRIME_WITH_VALUE18[] = {
184 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
185 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
186 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
190 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
191 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
192 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
193 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
194 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
195 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
196 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
197 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
199 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
201 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
204 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
205 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
207 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
209 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
210 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
212 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
213 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
214 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
215 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
216 };
217 
218 static uint8_t BIG_VALUE2_SIZE_64_BITS[] = {
219 	0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
220 };
221 
222 static uint8_t BIG_VALUE3_SIZE_32_BITS[] = {
223 	0x0F, 0xFF, 0xFF, 0xFF,
224 };
225 
226 static uint8_t BIG_VALUE4_SIZE_32_BITS[] = {
227 	0x01, 0x00, 0x00, 0x00,
228 };
229 
230 static uint8_t BIG_VALUE5_SIZE_2048_BITS[] = {
231 	0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
232 	0xFF, 0xFF, 0xFF, 0xFF,
233 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
234 	0xFF, 0xFF, 0xFF, 0xFF,
235 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
236 	0xFF, 0xFF, 0xFF, 0xFF,
237 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
238 	0xFF, 0xFF, 0xFF, 0xFF,
239 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
240 	0xFF, 0xFF, 0xFF, 0xFF,
241 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
242 	0xFF, 0xFF, 0xFF, 0xFF,
243 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
244 	0xFF, 0xFF, 0xFF, 0xFF,
245 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
246 	0xFF, 0xFF, 0xFF, 0xFF,
247 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
248 	0xFF, 0xFF, 0xFF, 0xFF,
249 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
250 	0xFF, 0xFF, 0xFF, 0xFF,
251 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
252 	0xFF, 0xFF, 0xFF, 0xFF,
253 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
254 	0xFF, 0xFF, 0xFF, 0xFF,
255 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
256 	0xFF, 0xFF, 0xFF, 0xFF,
257 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
258 	0xFF, 0xFF, 0xFF, 0xFF,
259 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
260 	0xFF, 0xFF, 0xFF, 0xFF,
261 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
262 	0xFF, 0xFF, 0xFF, 0xFF,
263 };
264 
265 static uint8_t BIG_VALUE6_SIZE_2048_BITS[] = {
266 	0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
267 	0x00, 0x00, 0x00, 0x00,
268 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
269 	0x00, 0x00, 0x00, 0x00,
270 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
271 	0x00, 0x00, 0x00, 0x00,
272 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273 	0x00, 0x00, 0x00, 0x00,
274 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
275 	0x00, 0x00, 0x00, 0x00,
276 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
277 	0x00, 0x00, 0x00, 0x00,
278 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
279 	0x00, 0x00, 0x00, 0x00,
280 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
281 	0x00, 0x00, 0x00, 0x00,
282 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
283 	0x00, 0x00, 0x00, 0x00,
284 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
285 	0x00, 0x00, 0x00, 0x00,
286 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
287 	0x00, 0x00, 0x00, 0x00,
288 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
289 	0x00, 0x00, 0x00, 0x00,
290 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291 	0x00, 0x00, 0x00, 0x00,
292 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293 	0x00, 0x00, 0x00, 0x00,
294 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
295 	0x00, 0x00, 0x00, 0x00,
296 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
297 	0x00, 0x00, 0x00, 0x00,
298 };
299 
300 static uint8_t BIG_VALUE7_SIZE_1024_BITS[] = {
301 	0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
302 	0xFF, 0xFF, 0xFF, 0xFF,
303 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
304 	0xFF, 0xFF, 0xFF, 0xFF,
305 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
306 	0xFF, 0xFF, 0xFF, 0xFF,
307 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
308 	0xFF, 0xFF, 0xFF, 0xFF,
309 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
310 	0xFF, 0xFF, 0xFF, 0xFF,
311 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
312 	0xFF, 0xFF, 0xFF, 0xFF,
313 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
314 	0xFF, 0xFF, 0xFF, 0xFF,
315 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
316 	0xFF, 0xFF, 0xFF, 0xFF,
317 };
318 
319 static uint8_t BIG_VALUE10_SIZE_32_BITS_PRIME_WITH_VALUE3[] = {
320 	0x0E, 0xFF, 0xFF, 0xFF,
321 };
322 
323 static uint8_t BIG_VALUE13_SIZE_32_BITS_PRIME[] = {
324 	0x00, 0x00, 0x0D, 0x69,
325 };
326 
327 static uint8_t BIG_VALUE14_SIZE_32_BITS_NOT_PRIME[] = {
328 	0x00, 0x00, 0x0D, 0x68,
329 };
330 
331 static uint8_t BIG_VALUE15_SIZE_32_BITS[] = {
332 	0x00, 0x00, 0x00, 0x03,
333 };
334 
335 static uint8_t BIG_VALUE16_SIZE_64_BITS[] = {
336 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
337 };
338 
339 static uint8_t BIG_VALUE17_SIZE_1024_BITS[] = {
340 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
341 	0x00, 0x00, 0x00, 0x00,
342 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
343 	0x00, 0x00, 0x00, 0x00,
344 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345 	0x00, 0x00, 0x00, 0x00,
346 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
347 	0x00, 0x00, 0x00, 0x00,
348 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
349 	0x00, 0x00, 0x00, 0x00,
350 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
351 	0x00, 0x00, 0x00, 0x00,
352 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
353 	0x00, 0x00, 0x00, 0x00,
354 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
355 	0x00, 0x00, 0x00, 0x03,
356 };
357 
358 static uint8_t BIG_VALUE18_SIZE_2048_BITS[] = {
359 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
360 	0x00, 0x00, 0x00, 0x00,
361 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
362 	0x00, 0x00, 0x00, 0x00,
363 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
364 	0x00, 0x00, 0x00, 0x00,
365 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
366 	0x00, 0x00, 0x00, 0x00,
367 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
368 	0x00, 0x00, 0x00, 0x00,
369 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
370 	0x00, 0x00, 0x00, 0x00,
371 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
372 	0x00, 0x00, 0x00, 0x00,
373 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
374 	0x00, 0x00, 0x00, 0x00,
375 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
376 	0x00, 0x00, 0x00, 0x00,
377 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
378 	0x00, 0x00, 0x00, 0x00,
379 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
380 	0x00, 0x00, 0x00, 0x00,
381 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
382 	0x00, 0x00, 0x00, 0x00,
383 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
384 	0x00, 0x00, 0x00, 0x00,
385 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
386 	0x00, 0x00, 0x00, 0x00,
387 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
388 	0x00, 0x00, 0x00, 0x00,
389 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
390 	0x00, 0x00, 0x00, 0x03,
391 };
392 
393 static uint8_t BIG_VALUE19_SIZE_32_BITS_PRIME_WITH_VALUE15[] = {
394 	0x00, 0x00, 0x00, 0x04,
395 };
396 
397 static uint8_t BIG_VALUE9_SIZE_64_BITS_PRIME_WITH_VALUE1[] = {
398 	0x0E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
399 };
400 
401 static uint8_t BIG_VALUE_ONE_SIZE_64_BITS[] = {
402 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
403 };
404 
405 static uint8_t BIG_VALUE_TWO_SIZE_1024_BITS[] = {
406 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
407 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
408 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
409 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
410 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
411 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
412 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
413 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
414 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
415 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
416 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
417 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
418 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
419 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
420 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
421 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
422 };
423 
424 static uint8_t BIG_VALUE_ZERO_SIZE_32_BITS[] = {
425 	0x00, 0x00, 0x00, 0x00,
426 };
427 
428 static uint8_t BIG_VALUE_ZERO_SIZE_64_BITS[] = {
429 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
430 };
431 
432 static uint8_t BIG_VALUE_ZERO_SIZE_2048_BITS[] = {
433 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
434 	0x00, 0x00, 0x00, 0x00,
435 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
436 	0x00, 0x00, 0x00, 0x00,
437 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
438 	0x00, 0x00, 0x00, 0x00,
439 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
440 	0x00, 0x00, 0x00, 0x00,
441 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
442 	0x00, 0x00, 0x00, 0x00,
443 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
444 	0x00, 0x00, 0x00, 0x00,
445 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
446 	0x00, 0x00, 0x00, 0x00,
447 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
448 	0x00, 0x00, 0x00, 0x00,
449 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
450 	0x00, 0x00, 0x00, 0x00,
451 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
452 	0x00, 0x00, 0x00, 0x00,
453 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
454 	0x00, 0x00, 0x00, 0x00,
455 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
456 	0x00, 0x00, 0x00, 0x00,
457 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
458 	0x00, 0x00, 0x00, 0x00,
459 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
460 	0x00, 0x00, 0x00, 0x00,
461 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
462 	0x00, 0x00, 0x00, 0x00,
463 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
464 	0x00, 0x00, 0x00, 0x00,
465 };
466 
467 static uint8_t BIG_VALUE_TWO_SIZE_2048_BITS[] = {
468 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
469 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
470 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
471 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
472 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
473 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
474 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
475 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
476 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
477 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
478 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
479 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
480 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
481 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
482 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
483 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
484 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
485 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
486 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
487 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
488 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
489 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
490 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
491 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
492 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
493 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
494 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
495 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
496 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
497 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
498 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
499 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
500 };
501 
502 static uint8_t BIG_VALUE25_SIZE_2048_BITS[] = {
503 	0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
504 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
505 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
506 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
507 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
508 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
509 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
510 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
511 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
512 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
513 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
514 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
515 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
516 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
517 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
518 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
519 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
520 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
521 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
522 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
523 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
524 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
525 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
526 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
527 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
528 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
529 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
530 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
531 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
532 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
533 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
534 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
535 };
536 
537 static uint8_t BIG_VALUE_TWO_SIZE_32_BITS[] = {
538 	0x00, 0x00, 0x00, 0x02,
539 };
540 
541 static uint8_t BIG_VALUE23_SIZE_32_BITS[] = {
542 	0x01, 0x00, 0x00, 0x01,
543 };
544 
545 static uint8_t BIG_VALUE22_SIZE_32_BITS_LIMIT_INT32[] = {
546 	0x80, 0x00, 0x00, 0x00,
547 };
548 
549 static uint8_t BIG_VALUE_TWO_SIZE_64_BITS[] = {
550 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
551 };
552 
553 static uint8_t BIG_VALUE24_SIZE_64_BITS[] = {
554 	0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
555 };
556 
557 static uint8_t BIG_VALUE21_SIZE_64_BITS[] = {
558 	0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
559 };
560 
561 /** ALL_TEEC_UUID
562  *
563  * These constants are the UUID of existing
564  * Trusted Applications
565  */
566 /* "SMARTCLSARITHMET" */
567 static TEEC_UUID UUID_TTA_testingInternalAPI_Arithmetique = {
568 	0x534D4152, 0x5443, 0x4C53,
569 	{ 0x41, 0x52, 0x49, 0x54, 0x48, 0x4D, 0x45, 0x54 }
570 };
571 /* "SMARTCSL_TIMEAPI" */
572 static TEEC_UUID UUID_TTA_testingInternalAPI_Time = {
573 	0x534D4152, 0x5443, 0x534C,
574 	{ 0x5F, 0x54, 0x49, 0x4D, 0x45, 0x41, 0x50, 0x49 }
575 };
576 
577 /*Helper functions/macros*/
578 
579 #define BN_DECLARE_AND_INIT(exit_label) \
580 	BN_CTX *ctx = NULL; \
581 	BIGNUM *a = NULL, *b = NULL, *s = NULL, *d = NULL, \
582 		*m = NULL, *l = NULL, \
583 	*r = NULL; \
584 	ctx = BN_CTX_new(); \
585 	if (ctx == NULL) { \
586 		goto exit_label; \
587 	} \
588 	a = BN_new(); \
589 	b = BN_new(); \
590 	s = BN_new(); \
591 	d = BN_new(); \
592 	m = BN_new(); \
593 	l = BN_new(); \
594 	r = BN_new();
595 
596 #define BN_FREE() \
597 	BN_free(a); \
598 	BN_free(b); \
599 	BN_free(s); \
600 	BN_free(d); \
601 	BN_free(m); \
602 	BN_free(l); \
603 	BN_free(r); \
604 	if (ctx) { \
605 		BN_CTX_free(ctx); \
606 	}
607 
cancellation_thread(void * arg)608 static void *cancellation_thread(void *arg)
609 {
610 	TEEC_RequestCancellation((TEEC_Operation *)arg);
611 	return NULL;
612 }
613 
614 #define RequestCancellation(op) \
615 	(void)ADBG_EXPECT(c, 0, \
616 			  pthread_create(&THREAD02, NULL, cancellation_thread, \
617 					 (void *)op)); \
618 	(void)ADBG_EXPECT(c, 0, pthread_join(THREAD02, NULL));
619 
Invoke_BigIntCmpS32(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t op,uint32_t short_value,uint32_t exp_cmp_res)620 static TEEC_Result Invoke_BigIntCmpS32(ADBG_Case_t *c, TEEC_Session *sess,
621 				       uint32_t cmd_id, uint32_t op,
622 				       uint32_t short_value,
623 				       uint32_t exp_cmp_res)
624 {
625 	return Invoke_Simple_Function_v2(c, sess, cmd_id, exp_cmp_res, 0,
626 					 op, short_value);
627 }
628 
Invoke_BigIntCmp(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t op1,uint32_t op2,uint32_t exp_cmp_res)629 static TEEC_Result Invoke_BigIntCmp(ADBG_Case_t *c, TEEC_Session *sess,
630 				    uint32_t cmd_id, uint32_t op1,
631 				    uint32_t op2, uint32_t exp_cmp_res)
632 {
633 	return Invoke_Simple_Function_v2(c, sess, cmd_id, exp_cmp_res, 0,
634 					 op1, op2);
635 }
636 
Invoke_BigIntComputeExtendedGcd(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t gcd,uint32_t dst_u,uint32_t dst_v,uint32_t op1,uint32_t op2)637 static TEEC_Result Invoke_BigIntComputeExtendedGcd(ADBG_Case_t *c,
638 						   TEEC_Session *sess,
639 						   uint32_t cmd_id,
640 						   uint32_t gcd, uint32_t dst_u,
641 						   uint32_t dst_v, uint32_t op1,
642 						   uint32_t op2)
643 {
644 	return Invoke_Simple_Function_v3(c, sess, cmd_id, gcd, 0, dst_u, dst_v,
645 					 op1, op2);
646 }
647 
Invoke_BigIntAddMod(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t dest,uint32_t op1,uint32_t op2,uint32_t mod)648 static TEEC_Result Invoke_BigIntAddMod(ADBG_Case_t *c, TEEC_Session *sess,
649 				       uint32_t cmd_id, uint32_t dest,
650 				       uint32_t op1, uint32_t op2, uint32_t mod)
651 {
652 	return Invoke_Simple_Function_v2(c, sess, cmd_id, dest, mod, op1, op2);
653 }
654 
Invoke_BigIntAdd(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t dest,uint32_t op1,uint32_t op2)655 static TEEC_Result Invoke_BigIntAdd(ADBG_Case_t *c, TEEC_Session *sess,
656 				    uint32_t cmd_id, uint32_t dest,
657 				    uint32_t op1, uint32_t op2)
658 {
659 	return Invoke_Simple_Function_v2(c, sess, cmd_id, dest, 0, op1, op2);
660 }
661 
Invoke_BigIntMul(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t dest,uint32_t op1,uint32_t op2)662 static TEEC_Result Invoke_BigIntMul(ADBG_Case_t *c, TEEC_Session *sess,
663 				    uint32_t cmd_id, uint32_t dest,
664 				    uint32_t op1, uint32_t op2)
665 {
666 	return Invoke_Simple_Function_v2(c, sess, cmd_id, dest, 0, op1, op2);
667 }
668 
Invoke_BigIntMulMod(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t dst,uint32_t op1,uint32_t op2,uint32_t mod)669 static TEEC_Result Invoke_BigIntMulMod(ADBG_Case_t *c, TEEC_Session *sess,
670 				       uint32_t cmd_id, uint32_t dst,
671 				       uint32_t op1, uint32_t op2, uint32_t mod)
672 {
673 	return Invoke_Simple_Function_v2(c, sess, cmd_id, dst, mod, op1, op2);
674 }
675 
Invoke_StoreS32(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t dest,uint32_t sign,const uint8_t * value,size_t value_size)676 static TEEC_Result Invoke_StoreS32(ADBG_Case_t *c, TEEC_Session *sess,
677 				   uint32_t cmd_id, uint32_t dest,
678 				   uint32_t sign, const uint8_t *value,
679 				   size_t value_size)
680 {
681 	uint32_t v = 0;
682 	size_t n = 0;
683 
684 	if (value_size > 4)
685 		return TEEC_ERROR_BAD_PARAMETERS;
686 
687 	for (n = 0; n < value_size; n++) {
688 		v <<= 8;
689 		v |= value[n];
690 	}
691 	return Invoke_Simple_Function_v2(c, sess, cmd_id, dest, sign, v, 0);
692 }
693 
694 
Invoke_BigIntConvertFromS32(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t dst,uint32_t short_val)695 static TEEC_Result Invoke_BigIntConvertFromS32(ADBG_Case_t *c,
696 					       TEEC_Session *sess,
697 					       uint32_t cmd_id, uint32_t dst,
698 					       uint32_t short_val)
699 {
700 	return Invoke_Simple_Function_v1(c, sess, cmd_id, dst, short_val);
701 }
702 
Invoke_BigIntConvertToS32(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t dst,uint32_t src)703 static TEEC_Result Invoke_BigIntConvertToS32(ADBG_Case_t *c,
704 					     TEEC_Session *sess,
705 					     uint32_t cmd_id, uint32_t dst,
706 					     uint32_t src)
707 {
708 	return Invoke_Simple_Function_v1(c, sess, cmd_id, src, dst);
709 }
710 
Invoke_BigIntGetBit(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd,uint32_t src,uint32_t bit_idx,uint32_t exp_result)711 static TEEC_Result Invoke_BigIntGetBit(ADBG_Case_t *c, TEEC_Session *sess,
712 				       uint32_t cmd, uint32_t src,
713 				       uint32_t bit_idx, uint32_t exp_result)
714 {
715 	TEEC_Result res = TEEC_SUCCESS;
716 	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
717 	uint32_t org;
718 
719 	op.params[0].value.a = src;
720 	op.params[0].value.b = bit_idx;
721 
722 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_OUTPUT,
723 					 TEEC_NONE, TEEC_NONE);
724 
725 	res = TEEC_InvokeCommand(sess, cmd, &op, &org);
726 
727 	if (!res && !ADBG_EXPECT_COMPARE_UNSIGNED(c, exp_result, ==,
728 						  op.params[1].value.a))
729 		res = TEEC_ERROR_GENERIC;
730 
731 	return res;
732 }
733 
Invoke_BigIntGetBitCount(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd,uint32_t src,uint32_t exp_bit_count)734 static TEEC_Result Invoke_BigIntGetBitCount(ADBG_Case_t *c, TEEC_Session *sess,
735 					    uint32_t cmd, uint32_t src,
736 					    uint32_t exp_bit_count)
737 {
738 	TEEC_Result res = TEEC_SUCCESS;
739 	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
740 	uint32_t org;
741 
742 	op.params[0].value.a = src;
743 
744 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_OUTPUT,
745 					 TEEC_NONE, TEEC_NONE);
746 
747 	res = TEEC_InvokeCommand(sess, cmd, &op, &org);
748 
749 	if (!res && !ADBG_EXPECT_COMPARE_UNSIGNED(c, exp_bit_count, ==,
750 						  op.params[1].value.a))
751 		res = TEEC_ERROR_GENERIC;
752 
753 	return res;
754 }
755 
Invoke_Wait(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmdId,uint32_t Case)756 static TEEC_Result Invoke_Wait(ADBG_Case_t *c, TEEC_Session *sess,
757 			       uint32_t cmdId, uint32_t Case)
758 {
759 	return Invoke_Simple_Function_v1(c, sess, cmdId, Case, 0);
760 }
761 
Invoke_BigIntInitFMMContext(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t ctx,uint32_t mod_size,uint32_t mod)762 static TEEC_Result Invoke_BigIntInitFMMContext(ADBG_Case_t *c,
763 					       TEEC_Session *sess,
764 					       uint32_t cmd_id, uint32_t ctx,
765 					       uint32_t mod_size, uint32_t mod)
766 {
767 	return Invoke_Simple_Function_v2(c, sess, cmd_id, ctx, mod, mod_size,
768 					 0);
769 }
770 
Invoke_NewBigIntFMM(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t role,uint32_t mod_size)771 static TEEC_Result Invoke_NewBigIntFMM(ADBG_Case_t *c, TEEC_Session *sess,
772 				       uint32_t cmd_id, uint32_t role,
773 				       uint32_t mod_size)
774 {
775 	return Invoke_Simple_Function_v1(c, sess, cmd_id, role, mod_size);
776 }
777 
Invoke_BigIntConvertToFMM(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t dst,uint32_t src,uint32_t mod,uint32_t ctx)778 static TEEC_Result Invoke_BigIntConvertToFMM(ADBG_Case_t *c, TEEC_Session *sess,
779 					     uint32_t cmd_id, uint32_t dst,
780 					     uint32_t src, uint32_t mod,
781 					     uint32_t ctx)
782 {
783 	return Invoke_Simple_Function_v3(c, sess, cmd_id, ctx, mod, dst, 0,
784 					 src, 0);
785 }
786 
Invoke_BigIntConvertFromFMM(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t dst,uint32_t src,uint32_t mod,uint32_t ctx)787 static TEEC_Result Invoke_BigIntConvertFromFMM(ADBG_Case_t *c,
788 					       TEEC_Session *sess,
789 					       uint32_t cmd_id, uint32_t dst,
790 					       uint32_t src, uint32_t mod,
791 					       uint32_t ctx)
792 {
793 	return Invoke_Simple_Function_v3(c, sess, cmd_id, ctx, mod, dst, 0,
794 					 src, 0);
795 }
796 
Invoke_BigIntComputeFMM(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t dst,uint32_t op1,uint32_t op2,uint32_t mod,uint32_t ctx)797 static TEEC_Result Invoke_BigIntComputeFMM(ADBG_Case_t *c, TEEC_Session *sess,
798 					   uint32_t cmd_id, uint32_t dst,
799 					   uint32_t op1, uint32_t op2,
800 					   uint32_t mod, uint32_t ctx)
801 {
802 	return Invoke_Simple_Function_v3(c, sess, cmd_id, ctx, mod, dst, 0,
803 					 op1, op2);
804 }
805 
Invoke_BigIntDiv(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t q,uint32_t r,uint32_t op1,uint32_t op2)806 static TEEC_Result Invoke_BigIntDiv(ADBG_Case_t *c, TEEC_Session *sess,
807 				    uint32_t cmd_id, uint32_t q, uint32_t r,
808 				    uint32_t op1, uint32_t op2)
809 {
810 	return Invoke_Simple_Function_v2(c, sess, cmd_id, q, r, op1, op2);
811 }
812 
Invoke_BigIntMod(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t dst,uint32_t op,uint32_t mod)813 static TEEC_Result Invoke_BigIntMod(ADBG_Case_t *c, TEEC_Session *sess,
814 				    uint32_t cmd_id, uint32_t dst, uint32_t op,
815 				    uint32_t mod)
816 {
817 	return Invoke_Simple_Function_v2(c, sess, cmd_id, dst, mod, op, 0);
818 }
819 
Invoke_BigIntInvMod(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd,uint32_t dst,uint32_t op,uint32_t mod)820 static TEEC_Result Invoke_BigIntInvMod(ADBG_Case_t *c, TEEC_Session *sess,
821 				       uint32_t cmd, uint32_t dst, uint32_t op,
822 				       uint32_t mod)
823 {
824 	return Invoke_Simple_Function_v2(c, sess, cmd, dst, mod, op, 0);
825 }
826 
Invoke_BigIntIsProbablePrime(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd,uint32_t op,uint32_t conf_lvl,uint32_t exp_result)827 static TEEC_Result Invoke_BigIntIsProbablePrime(ADBG_Case_t *c,
828 						TEEC_Session *sess,
829 						uint32_t cmd, uint32_t op,
830 						uint32_t conf_lvl,
831 						uint32_t exp_result)
832 {
833 	return Invoke_Simple_Function_v2(c, sess, cmd, exp_result, 0, op,
834 					 conf_lvl);
835 }
836 
Invoke_BigIntNeg(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd,uint32_t dst,uint32_t op)837 static TEEC_Result Invoke_BigIntNeg(ADBG_Case_t *c, TEEC_Session *sess,
838 				    uint32_t cmd, uint32_t dst, uint32_t op)
839 {
840 	return Invoke_Simple_Function_v2(c, sess, cmd, dst, 0, op, 0);
841 }
842 
Invoke_BigIntRelativePrime(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd,uint32_t op1,uint32_t op2,bool exp_result)843 static TEEC_Result Invoke_BigIntRelativePrime(ADBG_Case_t *c,
844 					      TEEC_Session *sess, uint32_t cmd,
845 					      uint32_t op1, uint32_t op2,
846 					      bool exp_result)
847 {
848 	return Invoke_Simple_Function_v2(c, sess, cmd, exp_result, 0, op1, op2);
849 }
850 
Invoke_BigIntShiftRight(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd,uint32_t dst,uint32_t op,uint32_t bits)851 static TEEC_Result Invoke_BigIntShiftRight(ADBG_Case_t *c, TEEC_Session *sess,
852 					   uint32_t cmd, uint32_t dst,
853 					   uint32_t op, uint32_t bits)
854 {
855 	return Invoke_Simple_Function_v2(c, sess, cmd, dst, op, bits, 0);
856 }
857 
Invoke_BigIntSquareMod(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd,uint32_t dst,uint32_t op,uint32_t mod)858 static TEEC_Result Invoke_BigIntSquareMod(ADBG_Case_t *c, TEEC_Session *sess,
859 					  uint32_t cmd, uint32_t dst,
860 					  uint32_t op, uint32_t mod)
861 {
862 	return Invoke_Simple_Function_v2(c, sess, cmd, dst, mod, op, 0);
863 }
864 
Invoke_BigIntSquare(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd,uint32_t dst,uint32_t op)865 static TEEC_Result Invoke_BigIntSquare(ADBG_Case_t *c, TEEC_Session *sess,
866 				       uint32_t cmd, uint32_t dst, uint32_t op)
867 {
868 	return Invoke_Simple_Function_v2(c, sess, cmd, dst, 0, op, 0);
869 }
870 
Invoke_BigIntSubMod(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd,uint32_t dst,uint32_t op1,uint32_t op2,uint32_t mod)871 static TEEC_Result Invoke_BigIntSubMod(ADBG_Case_t *c, TEEC_Session *sess,
872 				       uint32_t cmd, uint32_t dst, uint32_t op1,
873 				       uint32_t op2, uint32_t mod)
874 {
875 	return Invoke_Simple_Function_v2(c, sess, cmd, dst, mod, op1, op2);
876 }
877 
Invoke_BigIntSub(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd,uint32_t dst,uint32_t op1,uint32_t op2)878 static TEEC_Result Invoke_BigIntSub(ADBG_Case_t *c, TEEC_Session *sess,
879 				    uint32_t cmd, uint32_t dst, uint32_t op1,
880 				    uint32_t op2)
881 {
882 	return Invoke_Simple_Function_v2(c, sess, cmd, dst, 0, op1, op2);
883 }
884 
Invoke_NewBigInt(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t role,uint32_t size)885 static TEEC_Result Invoke_NewBigInt(ADBG_Case_t *c, TEEC_Session *sess,
886 				    uint32_t cmd_id, uint32_t role,
887 				    uint32_t size)
888 {
889 	return Invoke_Simple_Function_v1(c, sess, cmd_id, role, size);
890 }
891 
Macro_StoreBigInt(ADBG_Case_t * c,TEEC_Session * sess,uint32_t role,uint32_t size,uint32_t sign,const uint8_t * value,size_t value_size)892 static TEEC_Result Macro_StoreBigInt(ADBG_Case_t *c, TEEC_Session *sess,
893 				     uint32_t role, uint32_t size,
894 				     uint32_t sign, const uint8_t *value,
895 				     size_t value_size)
896 {
897 	TEEC_Result res = TEE_SUCCESS;
898 	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
899 	uint32_t org;
900 	TEEC_SharedMemory shm_value = {
901 		.size = value_size,
902 		.flags = TEEC_MEM_INPUT,
903 	};
904 
905 	res = Invoke_NewBigInt(c, sess, CMD_Arithm_TTA_New_BigInt, role, size);
906 	if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
907 		return res;
908 
909 	res = TEEC_AllocateSharedMemory(sess->ctx, &shm_value);
910 	if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
911 		return res;
912 	memcpy(shm_value.buffer, value, value_size);
913 
914 	op.params[0].value.a = role;
915 	op.params[0].value.b = size;
916 	op.params[1].value.a = sign;
917 	SET_SHARED_MEMORY_OPERATION_PARAMETER(2, 0, &shm_value,
918 					      shm_value.size);
919 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_INPUT,
920 					 TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE);
921 	res = TEEC_InvokeCommand(sess, CMD_Arithm_BigIntConvertFromOctetString,
922 				 &op, &org);
923 	TEEC_ReleaseSharedMemory(&shm_value);
924 	return res;
925 }
926 
Invoke_BigIntConvertToOctetString(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t bigint,size_t size)927 static TEEC_Result Invoke_BigIntConvertToOctetString(ADBG_Case_t *c,
928 						     TEEC_Session *sess,
929 						     uint32_t cmd_id,
930 						     uint32_t bigint,
931 						     size_t size)
932 {
933 	TEEC_Result res = TEE_SUCCESS;
934 	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
935 	uint32_t org;
936 	TEEC_SharedMemory shm = {
937 		.size = (size + 7) / 8,
938 		.flags = TEEC_MEM_OUTPUT,
939 	};
940 
941 	res = TEEC_AllocateSharedMemory(sess->ctx, &shm);
942 	if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
943 		return res;
944 
945 	op.params[0].value.a = bigint;
946 	SET_SHARED_MEMORY_OPERATION_PARAMETER(2, 0, &shm, shm.size);
947 
948 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_OUTPUT,
949 					 TEEC_MEMREF_PARTIAL_OUTPUT, TEEC_NONE);
950 
951 	res = TEEC_InvokeCommand(sess, cmd_id, &op, &org);
952 
953 	if (!res) {
954 		size_t sz = op.params[2].memref.size;
955 
956 		saved_octet_string = malloc(sz);
957 		memcpy(saved_octet_string, shm.buffer, sz);
958 		saved_octet_string_size = sz;
959 		saved_octet_string_sign = op.params[1].value.a;
960 
961 	}
962 	TEEC_ReleaseSharedMemory(&shm);
963 	return res;
964 }
965 
Invoke_BigIntConvertFromOctetString(ADBG_Case_t * c,TEEC_Session * sess,uint32_t cmd_id,uint32_t dst,size_t size,uint32_t sign,const uint8_t * value,size_t value_size)966 static TEEC_Result Invoke_BigIntConvertFromOctetString(ADBG_Case_t *c,
967 						       TEEC_Session *sess,
968 						       uint32_t cmd_id,
969 						       uint32_t dst,
970 						       size_t size,
971 						       uint32_t sign,
972 						       const uint8_t *value,
973 						       size_t value_size)
974 {
975 	TEEC_Result res = TEE_SUCCESS;
976 	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
977 	uint32_t org;
978 	TEEC_SharedMemory shm = {
979 		.size = (size + 7) / 8,
980 		.flags = TEEC_MEM_INPUT,
981 	};
982 
983 	if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, value_size, <=, shm.size))
984 		return TEEC_ERROR_BAD_PARAMETERS;
985 
986 	res = TEEC_AllocateSharedMemory(sess->ctx, &shm);
987 	if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
988 		return res;
989 	memcpy(shm.buffer, value, value_size);
990 
991 	op.params[0].value.a = dst;
992 	op.params[1].value.a = sign;
993 	SET_SHARED_MEMORY_OPERATION_PARAMETER(2, 0, &shm, shm.size);
994 
995 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_INPUT,
996 					 TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE);
997 
998 	res = TEEC_InvokeCommand(sess, cmd_id, &op, &org);
999 
1000 	TEEC_ReleaseSharedMemory(&shm);
1001 	return res;
1002 }
1003 #endif /* XML_TIMEARITHM_API_H_ */
1004