xref: /optee_os/lib/libutee/tee_api_operations.c (revision 17a66904a791447da1356331f01e7c1ca25329be)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2021, SumUp Services GmbH
5  */
6 #include <assert.h>
7 #include <config.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <string_ext.h>
11 #include <tee_api.h>
12 #include <tee_api_defines_extensions.h>
13 #include <tee_internal_api_extensions.h>
14 #include <utee_syscalls.h>
15 #include <utee_defines.h>
16 #include <util.h>
17 #include "tee_api_private.h"
18 
19 struct __TEE_OperationHandle {
20 	TEE_OperationInfo info;
21 	TEE_ObjectHandle key1;
22 	TEE_ObjectHandle key2;
23 	uint32_t operationState;/* Operation state : INITIAL or ACTIVE */
24 
25 	/*
26 	 * buffer to collect complete blocks or to keep a complete digest
27 	 * for TEE_DigestExtract().
28 	 */
29 	uint8_t *buffer;
30 	bool buffer_two_blocks;	/* True if two blocks need to be buffered */
31 	size_t block_size;	/* Block size of cipher */
32 	size_t buffer_offs;	/* Offset in buffer */
33 	uint32_t state;		/* Handle to state in TEE Core */
34 };
35 
36 /* Cryptographic Operations API - Generic Operation Functions */
37 
38 TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation,
39 				 uint32_t algorithm, uint32_t mode,
40 				 uint32_t maxKeySize)
41 {
42 	TEE_Result res;
43 	TEE_OperationHandle op = TEE_HANDLE_NULL;
44 	uint32_t handle_state = 0;
45 	size_t block_size = 1;
46 	uint32_t req_key_usage;
47 	bool with_private_key = false;
48 	bool buffer_two_blocks = false;
49 
50 	if (!operation)
51 		TEE_Panic(0);
52 
53 	if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP ||
54 	    algorithm == TEE_ALG_SM4_XTS)
55 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
56 
57 	/* Check algorithm max key size */
58 	switch (algorithm) {
59 	case TEE_ALG_DSA_SHA1:
60 		if (maxKeySize < 512)
61 			return TEE_ERROR_NOT_SUPPORTED;
62 		if (maxKeySize > 1024)
63 			return TEE_ERROR_NOT_SUPPORTED;
64 		if (maxKeySize % 64 != 0)
65 			return TEE_ERROR_NOT_SUPPORTED;
66 		break;
67 
68 	case TEE_ALG_DSA_SHA224:
69 		if (maxKeySize != 2048)
70 			return TEE_ERROR_NOT_SUPPORTED;
71 		break;
72 
73 	case TEE_ALG_DSA_SHA256:
74 		if (maxKeySize != 2048 && maxKeySize != 3072)
75 			return TEE_ERROR_NOT_SUPPORTED;
76 		break;
77 
78 	case TEE_ALG_ECDSA_SHA1:
79 	case __OPTEE_ALG_ECDSA_P192:
80 	case __OPTEE_ALG_ECDH_P192:
81 		if (maxKeySize != 192)
82 			return TEE_ERROR_NOT_SUPPORTED;
83 		break;
84 
85 	case TEE_ALG_ECDSA_SHA224:
86 	case __OPTEE_ALG_ECDSA_P224:
87 	case __OPTEE_ALG_ECDH_P224:
88 		if (maxKeySize != 224)
89 			return TEE_ERROR_NOT_SUPPORTED;
90 		break;
91 
92 	case TEE_ALG_ECDSA_SHA256:
93 	case __OPTEE_ALG_ECDSA_P256:
94 	case __OPTEE_ALG_ECDH_P256:
95 	case TEE_ALG_SM2_PKE:
96 	case TEE_ALG_SM2_DSA_SM3:
97 		if (maxKeySize != 256)
98 			return TEE_ERROR_NOT_SUPPORTED;
99 		break;
100 
101 	case TEE_ALG_SM2_KEP:
102 		/* Two 256-bit keys */
103 		if (maxKeySize != 512)
104 			return TEE_ERROR_NOT_SUPPORTED;
105 		break;
106 
107 	case TEE_ALG_ECDSA_SHA384:
108 	case __OPTEE_ALG_ECDSA_P384:
109 	case __OPTEE_ALG_ECDH_P384:
110 		if (maxKeySize != 384)
111 			return TEE_ERROR_NOT_SUPPORTED;
112 		break;
113 
114 	case TEE_ALG_ECDSA_SHA512:
115 	case __OPTEE_ALG_ECDSA_P521:
116 	case __OPTEE_ALG_ECDH_P521:
117 		if (maxKeySize != 521)
118 			return TEE_ERROR_NOT_SUPPORTED;
119 		break;
120 
121 	case TEE_ALG_ECDH_DERIVE_SHARED_SECRET:
122 		if (maxKeySize > 521)
123 			return TEE_ERROR_NOT_SUPPORTED;
124 		break;
125 
126 	case TEE_ALG_ED25519:
127 	case TEE_ALG_X25519:
128 		if (maxKeySize != 256)
129 			return TEE_ERROR_NOT_SUPPORTED;
130 		break;
131 	default:
132 		break;
133 	}
134 
135 	/* Check algorithm mode */
136 	switch (algorithm) {
137 	case TEE_ALG_AES_CTS:
138 	case TEE_ALG_AES_XTS:
139 	case TEE_ALG_SM4_XTS:
140 		buffer_two_blocks = true;
141 		fallthrough;
142 	case TEE_ALG_AES_ECB_NOPAD:
143 	case TEE_ALG_AES_CBC_NOPAD:
144 	case TEE_ALG_AES_CCM:
145 	case TEE_ALG_DES_ECB_NOPAD:
146 	case TEE_ALG_DES_CBC_NOPAD:
147 	case TEE_ALG_DES3_ECB_NOPAD:
148 	case TEE_ALG_DES3_CBC_NOPAD:
149 	case TEE_ALG_SM4_ECB_NOPAD:
150 	case TEE_ALG_SM4_CBC_NOPAD:
151 	case TEE_ALG_SM4_CTR:
152 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
153 			block_size = TEE_AES_BLOCK_SIZE;
154 		else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4)
155 			block_size = TEE_SM4_BLOCK_SIZE;
156 		else
157 			block_size = TEE_DES_BLOCK_SIZE;
158 		fallthrough;
159 	case TEE_ALG_AES_CTR:
160 	case TEE_ALG_AES_GCM:
161 		if (mode == TEE_MODE_ENCRYPT)
162 			req_key_usage = TEE_USAGE_ENCRYPT;
163 		else if (mode == TEE_MODE_DECRYPT)
164 			req_key_usage = TEE_USAGE_DECRYPT;
165 		else
166 			return TEE_ERROR_NOT_SUPPORTED;
167 		break;
168 
169 #if defined(CFG_CRYPTO_RSASSA_NA1)
170 	case TEE_ALG_RSASSA_PKCS1_V1_5:
171 #endif
172 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
173 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
174 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
175 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
176 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
177 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
178 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_MD5:
179 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
180 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
181 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
182 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
183 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
184 	case TEE_ALG_DSA_SHA1:
185 	case TEE_ALG_DSA_SHA224:
186 	case TEE_ALG_DSA_SHA256:
187 	case TEE_ALG_ECDSA_SHA1:
188 	case TEE_ALG_ECDSA_SHA224:
189 	case TEE_ALG_ECDSA_SHA256:
190 	case TEE_ALG_ECDSA_SHA384:
191 	case TEE_ALG_ECDSA_SHA512:
192 	case __OPTEE_ALG_ECDSA_P192:
193 	case __OPTEE_ALG_ECDSA_P224:
194 	case __OPTEE_ALG_ECDSA_P256:
195 	case __OPTEE_ALG_ECDSA_P384:
196 	case __OPTEE_ALG_ECDSA_P521:
197 	case TEE_ALG_SM2_DSA_SM3:
198 	case TEE_ALG_ED25519:
199 		if (mode == TEE_MODE_SIGN) {
200 			with_private_key = true;
201 			req_key_usage = TEE_USAGE_SIGN;
202 		} else if (mode == TEE_MODE_VERIFY) {
203 			req_key_usage = TEE_USAGE_VERIFY;
204 		} else {
205 			return TEE_ERROR_NOT_SUPPORTED;
206 		}
207 		break;
208 
209 	case TEE_ALG_RSAES_PKCS1_V1_5:
210 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_MD5:
211 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
212 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
213 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
214 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
215 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
216 	case TEE_ALG_SM2_PKE:
217 		if (mode == TEE_MODE_ENCRYPT) {
218 			req_key_usage = TEE_USAGE_ENCRYPT;
219 		} else if (mode == TEE_MODE_DECRYPT) {
220 			with_private_key = true;
221 			req_key_usage = TEE_USAGE_DECRYPT;
222 		} else {
223 			return TEE_ERROR_NOT_SUPPORTED;
224 		}
225 		break;
226 
227 	case TEE_ALG_RSA_NOPAD:
228 		if (mode == TEE_MODE_ENCRYPT) {
229 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
230 		} else if (mode == TEE_MODE_DECRYPT) {
231 			with_private_key = true;
232 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
233 		} else {
234 			return TEE_ERROR_NOT_SUPPORTED;
235 		}
236 		break;
237 
238 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
239 	case TEE_ALG_ECDH_DERIVE_SHARED_SECRET:
240 	case __OPTEE_ALG_ECDH_P192:
241 	case __OPTEE_ALG_ECDH_P224:
242 	case __OPTEE_ALG_ECDH_P256:
243 	case __OPTEE_ALG_ECDH_P384:
244 	case __OPTEE_ALG_ECDH_P521:
245 	case TEE_ALG_HKDF_MD5_DERIVE_KEY:
246 	case TEE_ALG_HKDF_SHA1_DERIVE_KEY:
247 	case TEE_ALG_HKDF_SHA224_DERIVE_KEY:
248 	case TEE_ALG_HKDF_SHA256_DERIVE_KEY:
249 	case TEE_ALG_HKDF_SHA384_DERIVE_KEY:
250 	case TEE_ALG_HKDF_SHA512_DERIVE_KEY:
251 	case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY:
252 	case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY:
253 	case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY:
254 	case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY:
255 	case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY:
256 	case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY:
257 	case TEE_ALG_SM2_KEP:
258 	case TEE_ALG_X25519:
259 		if (mode != TEE_MODE_DERIVE)
260 			return TEE_ERROR_NOT_SUPPORTED;
261 		with_private_key = true;
262 		req_key_usage = TEE_USAGE_DERIVE;
263 		break;
264 
265 	case TEE_ALG_MD5:
266 	case TEE_ALG_SHA1:
267 	case TEE_ALG_SHA224:
268 	case TEE_ALG_SHA256:
269 	case TEE_ALG_SHA384:
270 	case TEE_ALG_SHA512:
271 	case TEE_ALG_SHA3_224:
272 	case TEE_ALG_SHA3_256:
273 	case TEE_ALG_SHA3_384:
274 	case TEE_ALG_SHA3_512:
275 	case TEE_ALG_SHAKE128:
276 	case TEE_ALG_SHAKE256:
277 	case TEE_ALG_SM3:
278 		if (mode != TEE_MODE_DIGEST)
279 			return TEE_ERROR_NOT_SUPPORTED;
280 		/* v1.1: flags always set for digest operations */
281 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
282 		req_key_usage = 0;
283 		break;
284 
285 	case TEE_ALG_DES_CBC_MAC_NOPAD:
286 	case TEE_ALG_AES_CBC_MAC_NOPAD:
287 	case TEE_ALG_AES_CBC_MAC_PKCS5:
288 	case TEE_ALG_AES_CMAC:
289 	case TEE_ALG_DES_CBC_MAC_PKCS5:
290 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
291 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
292 	case TEE_ALG_DES3_CMAC:
293 	case TEE_ALG_HMAC_MD5:
294 	case TEE_ALG_HMAC_SHA1:
295 	case TEE_ALG_HMAC_SHA224:
296 	case TEE_ALG_HMAC_SHA256:
297 	case TEE_ALG_HMAC_SHA384:
298 	case TEE_ALG_HMAC_SHA512:
299 	case TEE_ALG_HMAC_SHA3_224:
300 	case TEE_ALG_HMAC_SHA3_256:
301 	case TEE_ALG_HMAC_SHA3_384:
302 	case TEE_ALG_HMAC_SHA3_512:
303 	case TEE_ALG_HMAC_SM3:
304 		if (mode != TEE_MODE_MAC)
305 			return TEE_ERROR_NOT_SUPPORTED;
306 		req_key_usage = TEE_USAGE_MAC;
307 		break;
308 
309 	default:
310 		return TEE_ERROR_NOT_SUPPORTED;
311 	}
312 
313 	op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO);
314 	if (!op)
315 		return TEE_ERROR_OUT_OF_MEMORY;
316 
317 	op->info.algorithm = algorithm;
318 	op->info.operationClass = TEE_ALG_GET_CLASS(algorithm);
319 #ifdef CFG_CRYPTO_RSASSA_NA1
320 	if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5)
321 		op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE;
322 #endif
323 	op->info.mode = mode;
324 	op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm);
325 	op->info.maxKeySize = maxKeySize;
326 	op->info.requiredKeyUsage = req_key_usage;
327 	op->info.handleState = handle_state;
328 
329 	/*
330 	 * Needed to buffer the digest if TEE_DigestExtract() doesn't
331 	 * retrieve the entire digest in one go.
332 	 */
333 	if (op->info.operationClass == TEE_OPERATION_DIGEST)
334 		block_size = op->info.digestLength;
335 
336 	if (block_size > 1) {
337 		size_t buffer_size = block_size;
338 
339 		if (buffer_two_blocks)
340 			buffer_size *= 2;
341 
342 		op->buffer = TEE_Malloc(buffer_size,
343 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
344 		if (op->buffer == NULL) {
345 			res = TEE_ERROR_OUT_OF_MEMORY;
346 			goto out;
347 		}
348 	}
349 	op->block_size = block_size;
350 	op->buffer_two_blocks = buffer_two_blocks;
351 
352 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
353 		uint32_t mks = maxKeySize;
354 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
355 						       with_private_key);
356 
357 		/*
358 		 * If two keys are expected the max key size is the sum of
359 		 * the size of both keys.
360 		 */
361 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
362 			mks /= 2;
363 
364 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
365 		if (res != TEE_SUCCESS)
366 			goto out;
367 
368 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
369 			res = TEE_AllocateTransientObject(key_type, mks,
370 							  &op->key2);
371 			if (res != TEE_SUCCESS)
372 				goto out;
373 		}
374 	}
375 
376 	res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1,
377 				     (unsigned long)op->key2, &op->state);
378 	if (res != TEE_SUCCESS)
379 		goto out;
380 
381 	/*
382 	 * Initialize digest operations
383 	 * Other multi-stage operations initialized w/ TEE_xxxInit functions
384 	 * Non-applicable on asymmetric operations
385 	 */
386 	if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) {
387 		res = _utee_hash_init(op->state, NULL, 0);
388 		if (res != TEE_SUCCESS)
389 			goto out;
390 		/* v1.1: flags always set for digest operations */
391 		op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
392 	}
393 
394 	op->operationState = TEE_OPERATION_STATE_INITIAL;
395 
396 	*operation = op;
397 
398 out:
399 	if (res != TEE_SUCCESS) {
400 		if (res != TEE_ERROR_OUT_OF_MEMORY &&
401 		    res != TEE_ERROR_NOT_SUPPORTED)
402 			TEE_Panic(res);
403 		if (op) {
404 			if (op->state) {
405 				TEE_FreeOperation(op);
406 			} else {
407 				TEE_Free(op->buffer);
408 				TEE_FreeTransientObject(op->key1);
409 				TEE_FreeTransientObject(op->key2);
410 				TEE_Free(op);
411 			}
412 		}
413 	}
414 
415 	return res;
416 }
417 
418 void TEE_FreeOperation(TEE_OperationHandle operation)
419 {
420 	TEE_Result res;
421 
422 	if (operation == TEE_HANDLE_NULL)
423 		return;
424 
425 	/*
426 	 * Note that keys should not be freed here, since they are
427 	 * claimed by the operation they will be freed by
428 	 * utee_cryp_state_free().
429 	 */
430 	res = _utee_cryp_state_free(operation->state);
431 	if (res != TEE_SUCCESS)
432 		TEE_Panic(res);
433 
434 	TEE_Free(operation->buffer);
435 	TEE_Free(operation);
436 }
437 
438 void __GP11_TEE_FreeOperation(TEE_OperationHandle operation)
439 {
440 	if (operation == TEE_HANDLE_NULL)
441 		TEE_Panic(0);
442 	TEE_FreeOperation(operation);
443 }
444 
445 void TEE_GetOperationInfo(TEE_OperationHandle operation,
446 			  TEE_OperationInfo *operationInfo)
447 {
448 	if (operation == TEE_HANDLE_NULL)
449 		TEE_Panic(0);
450 
451 	__utee_check_out_annotation(operationInfo, sizeof(*operationInfo));
452 
453 	*operationInfo = operation->info;
454 	if (operationInfo->handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
455 		operationInfo->keySize = 0;
456 		operationInfo->requiredKeyUsage = 0;
457 	}
458 }
459 
460 TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle op,
461 					TEE_OperationInfoMultiple *op_info,
462 					size_t *size)
463 {
464 	TEE_Result res = TEE_SUCCESS;
465 	TEE_ObjectInfo kinfo = { };
466 	size_t max_key_count = 0;
467 	bool two_keys = false;
468 
469 	if (op == TEE_HANDLE_NULL) {
470 		res = TEE_ERROR_BAD_PARAMETERS;
471 		goto out;
472 	}
473 
474 	__utee_check_outbuf_annotation(op_info, size);
475 
476 	if (*size < sizeof(*op_info)) {
477 		res = TEE_ERROR_BAD_PARAMETERS;
478 		goto out;
479 	}
480 	max_key_count = (*size - sizeof(*op_info)) /
481 			sizeof(TEE_OperationInfoKey);
482 
483 	TEE_MemFill(op_info, 0, *size);
484 
485 	/* Two keys flag (TEE_ALG_AES_XTS only) */
486 	two_keys = op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
487 
488 	if (op->info.mode == TEE_MODE_DIGEST) {
489 		op_info->numberOfKeys = 0;
490 	} else if (!two_keys) {
491 		if (max_key_count < 1) {
492 			res = TEE_ERROR_SHORT_BUFFER;
493 			goto out;
494 		}
495 
496 		res = TEE_GetObjectInfo1(op->key1, &kinfo);
497 		/* Key1 is not a valid handle, "can't happen". */
498 		if (res)
499 			goto out;
500 
501 		op_info->keyInformation[0].keySize = kinfo.objectSize;
502 		op_info->keyInformation[0].requiredKeyUsage =
503 			op->info.requiredKeyUsage;
504 		op_info->numberOfKeys = 1;
505 	} else {
506 		if (max_key_count < 2) {
507 			res = TEE_ERROR_SHORT_BUFFER;
508 			goto out;
509 		}
510 
511 		res = TEE_GetObjectInfo1(op->key1, &kinfo);
512 		/* Key1 is not a valid handle, "can't happen". */
513 		if (res)
514 			goto out;
515 
516 		op_info->keyInformation[0].keySize = kinfo.objectSize;
517 		op_info->keyInformation[0].requiredKeyUsage =
518 			op->info.requiredKeyUsage;
519 
520 		res = TEE_GetObjectInfo1(op->key2, &kinfo);
521 		/* Key2 is not a valid handle, "can't happen". */
522 		if (res)
523 			goto out;
524 
525 		op_info->keyInformation[1].keySize = kinfo.objectSize;
526 		op_info->keyInformation[1].requiredKeyUsage =
527 			op->info.requiredKeyUsage;
528 
529 		op_info->numberOfKeys = 2;
530 	}
531 
532 	op_info->algorithm = op->info.algorithm;
533 	op_info->operationClass = op->info.operationClass;
534 	op_info->mode = op->info.mode;
535 	op_info->digestLength = op->info.digestLength;
536 	op_info->maxKeySize = op->info.maxKeySize;
537 	op_info->handleState = op->info.handleState;
538 	op_info->operationState = op->operationState;
539 
540 out:
541 	if (res != TEE_SUCCESS &&
542 	    res != TEE_ERROR_SHORT_BUFFER)
543 		TEE_Panic(res);
544 
545 	return res;
546 }
547 
548 TEE_Result
549 __GP11_TEE_GetOperationInfoMultiple(TEE_OperationHandle operation,
550 				    TEE_OperationInfoMultiple *info,
551 				    uint32_t *operationSize)
552 {
553 	TEE_Result res = TEE_SUCCESS;
554 	size_t s = 0;
555 
556 	__utee_check_gp11_outbuf_annotation(info, operationSize);
557 	s = *operationSize;
558 	res = TEE_GetOperationInfoMultiple(operation, info, &s);
559 	*operationSize = s;
560 	return res;
561 }
562 
563 static void reset_operation_state(TEE_OperationHandle op)
564 {
565 	op->operationState = TEE_OPERATION_STATE_INITIAL;
566 
567 	if (op->info.operationClass == TEE_OPERATION_DIGEST) {
568 		TEE_Result res = _utee_hash_init(op->state, NULL, 0);
569 
570 		if (res != TEE_SUCCESS)
571 			TEE_Panic(res);
572 		op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
573 	} else {
574 		op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
575 	}
576 }
577 
578 void TEE_ResetOperation(TEE_OperationHandle operation)
579 {
580 	if (operation == TEE_HANDLE_NULL)
581 		TEE_Panic(0);
582 
583 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET))
584 		TEE_Panic(0);
585 
586 	reset_operation_state(operation);
587 }
588 
589 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
590 			       TEE_ObjectHandle key)
591 {
592 	TEE_Result res;
593 	uint32_t key_size = 0;
594 	TEE_ObjectInfo key_info;
595 
596 	if (operation == TEE_HANDLE_NULL) {
597 		res = TEE_ERROR_BAD_PARAMETERS;
598 		goto out;
599 	}
600 
601 	if (key == TEE_HANDLE_NULL) {
602 		/* Operation key cleared */
603 		TEE_ResetTransientObject(operation->key1);
604 		operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
605 		if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
606 			reset_operation_state(operation);
607 		return TEE_SUCCESS;
608 	}
609 
610 	/* No key for digest operation */
611 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
612 		res = TEE_ERROR_BAD_PARAMETERS;
613 		goto out;
614 	}
615 
616 	/* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */
617 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
618 	    0) {
619 		res = TEE_ERROR_BAD_PARAMETERS;
620 		goto out;
621 	}
622 
623 	res = TEE_GetObjectInfo1(key, &key_info);
624 	/* Key is not a valid handle */
625 	if (res != TEE_SUCCESS)
626 		goto out;
627 
628 	/* Supplied key has to meet required usage */
629 	if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
630 	    operation->info.requiredKeyUsage) {
631 		res = TEE_ERROR_BAD_PARAMETERS;
632 		goto out;
633 	}
634 
635 	if (operation->info.maxKeySize < key_info.objectSize) {
636 		res = TEE_ERROR_BAD_PARAMETERS;
637 		goto out;
638 	}
639 
640 	key_size = key_info.objectSize;
641 
642 	TEE_ResetTransientObject(operation->key1);
643 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
644 
645 	res = TEE_CopyObjectAttributes1(operation->key1, key);
646 	if (res != TEE_SUCCESS)
647 		goto out;
648 
649 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
650 
651 	operation->info.keySize = key_size;
652 
653 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
654 		reset_operation_state(operation);
655 
656 out:
657 	if (res != TEE_SUCCESS  &&
658 	    res != TEE_ERROR_CORRUPT_OBJECT &&
659 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
660 		TEE_Panic(res);
661 
662 	return res;
663 }
664 
665 TEE_Result __GP11_TEE_SetOperationKey(TEE_OperationHandle operation,
666 				      TEE_ObjectHandle key)
667 {
668 	if (operation == TEE_HANDLE_NULL ||
669 	    operation->operationState != TEE_OPERATION_STATE_INITIAL)
670 		TEE_Panic(0);
671 
672 	return TEE_SetOperationKey(operation, key);
673 }
674 
675 static TEE_Result set_operation_key2(TEE_OperationHandle operation,
676 				     TEE_ObjectHandle key1,
677 				     TEE_ObjectHandle key2)
678 {
679 	TEE_Result res;
680 	uint32_t key_size = 0;
681 	TEE_ObjectInfo key_info1;
682 	TEE_ObjectInfo key_info2;
683 
684 	if (operation == TEE_HANDLE_NULL) {
685 		res = TEE_ERROR_BAD_PARAMETERS;
686 		goto out;
687 	}
688 
689 	/*
690 	 * Key1/Key2 and/or are not initialized and
691 	 * Either both keys are NULL or both are not NULL
692 	 */
693 	if (!key1 && !key2) {
694 		/* Clear the keys */
695 		TEE_ResetTransientObject(operation->key1);
696 		TEE_ResetTransientObject(operation->key2);
697 		operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
698 		if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
699 			reset_operation_state(operation);
700 		return TEE_SUCCESS;
701 	} else if (!key1 || !key2) {
702 		/* Both keys are obviously not valid. */
703 		res = TEE_ERROR_BAD_PARAMETERS;
704 		goto out;
705 	}
706 
707 	/* No key for digest operation */
708 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
709 		res = TEE_ERROR_BAD_PARAMETERS;
710 		goto out;
711 	}
712 
713 	/* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */
714 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
715 	    0) {
716 		res = TEE_ERROR_BAD_PARAMETERS;
717 		goto out;
718 	}
719 
720 	res = TEE_GetObjectInfo1(key1, &key_info1);
721 	/* Key1 is not a valid handle */
722 	if (res != TEE_SUCCESS)
723 		goto out;
724 
725 	/* Supplied key has to meet required usage */
726 	if ((key_info1.objectUsage & operation->info.
727 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
728 		res = TEE_ERROR_BAD_PARAMETERS;
729 		goto out;
730 	}
731 
732 	res = TEE_GetObjectInfo1(key2, &key_info2);
733 	/* Key2 is not a valid handle */
734 	if (res != TEE_SUCCESS) {
735 		if (res == TEE_ERROR_CORRUPT_OBJECT)
736 			res = TEE_ERROR_CORRUPT_OBJECT_2;
737 		goto out;
738 	}
739 
740 	/* Supplied key has to meet required usage */
741 	if ((key_info2.objectUsage & operation->info.
742 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
743 		res = TEE_ERROR_BAD_PARAMETERS;
744 		goto out;
745 	}
746 
747 	/*
748 	 * All the multi key algorithm currently supported requires the keys to
749 	 * be of equal size.
750 	 */
751 	if (key_info1.objectSize != key_info2.objectSize) {
752 		res = TEE_ERROR_BAD_PARAMETERS;
753 		goto out;
754 
755 	}
756 
757 	if (operation->info.maxKeySize < key_info1.objectSize) {
758 		res = TEE_ERROR_BAD_PARAMETERS;
759 		goto out;
760 	}
761 
762 	/*
763 	 * Odd that only the size of one key should be reported while
764 	 * size of two key are used when allocating the operation.
765 	 */
766 	key_size = key_info1.objectSize;
767 
768 	TEE_ResetTransientObject(operation->key1);
769 	TEE_ResetTransientObject(operation->key2);
770 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
771 
772 	res = TEE_CopyObjectAttributes1(operation->key1, key1);
773 	if (res != TEE_SUCCESS)
774 		goto out;
775 	res = TEE_CopyObjectAttributes1(operation->key2, key2);
776 	if (res != TEE_SUCCESS) {
777 		if (res == TEE_ERROR_CORRUPT_OBJECT)
778 			res = TEE_ERROR_CORRUPT_OBJECT_2;
779 		goto out;
780 	}
781 
782 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
783 
784 	operation->info.keySize = key_size;
785 
786 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
787 		reset_operation_state(operation);
788 out:
789 	if (res != TEE_SUCCESS  &&
790 	    res != TEE_ERROR_CORRUPT_OBJECT &&
791 	    res != TEE_ERROR_CORRUPT_OBJECT_2 &&
792 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE &&
793 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
794 		TEE_Panic(res);
795 
796 	return res;
797 }
798 
799 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
800 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
801 {
802 	if (operation != TEE_HANDLE_NULL && key1 && key1 == key2)
803 		return TEE_ERROR_SECURITY;
804 
805 	return set_operation_key2(operation, key1, key2);
806 }
807 
808 TEE_Result __GP11_TEE_SetOperationKey2(TEE_OperationHandle operation,
809 				       TEE_ObjectHandle key1,
810 				       TEE_ObjectHandle key2)
811 {
812 	if (operation == TEE_HANDLE_NULL ||
813 	    operation->operationState != TEE_OPERATION_STATE_INITIAL)
814 		TEE_Panic(0);
815 
816 	return set_operation_key2(operation, key1, key2);
817 }
818 
819 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
820 {
821 	TEE_Result res;
822 
823 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
824 		TEE_Panic(0);
825 	if (dst_op->info.algorithm != src_op->info.algorithm)
826 		TEE_Panic(0);
827 	if (dst_op->info.mode != src_op->info.mode)
828 		TEE_Panic(0);
829 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
830 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
831 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
832 
833 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
834 			key1 = src_op->key1;
835 			key2 = src_op->key2;
836 		}
837 
838 		if ((src_op->info.handleState &
839 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
840 			TEE_SetOperationKey(dst_op, key1);
841 		} else {
842 			TEE_SetOperationKey2(dst_op, key1, key2);
843 		}
844 	}
845 	dst_op->info.handleState = src_op->info.handleState;
846 	dst_op->info.keySize = src_op->info.keySize;
847 	dst_op->info.digestLength = src_op->info.digestLength;
848 	dst_op->operationState = src_op->operationState;
849 
850 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
851 	    dst_op->block_size != src_op->block_size)
852 		TEE_Panic(0);
853 
854 	if (dst_op->buffer != NULL) {
855 		size_t sz = src_op->block_size;
856 
857 		if (src_op->buffer == NULL)
858 			TEE_Panic(0);
859 
860 		if (src_op->buffer_two_blocks)
861 			sz *= 2;
862 		memcpy(dst_op->buffer, src_op->buffer, sz);
863 		dst_op->buffer_offs = src_op->buffer_offs;
864 	} else if (src_op->buffer != NULL) {
865 		TEE_Panic(0);
866 	}
867 
868 	res = _utee_cryp_state_copy(dst_op->state, src_op->state);
869 	if (res != TEE_SUCCESS)
870 		TEE_Panic(res);
871 }
872 
873 /* Cryptographic Operations API - Message Digest Functions */
874 
875 static void init_hash_operation(TEE_OperationHandle operation, const void *IV,
876 				uint32_t IVLen)
877 {
878 	TEE_Result res;
879 
880 	/*
881 	 * Note : IV and IVLen are never used in current implementation
882 	 * This is why coherent values of IV and IVLen are not checked
883 	 */
884 	res = _utee_hash_init(operation->state, IV, IVLen);
885 	if (res != TEE_SUCCESS)
886 		TEE_Panic(res);
887 	operation->buffer_offs = 0;
888 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
889 }
890 
891 void TEE_DigestUpdate(TEE_OperationHandle operation,
892 		      const void *chunk, size_t chunkSize)
893 {
894 	TEE_Result res = TEE_ERROR_GENERIC;
895 
896 	if (operation == TEE_HANDLE_NULL ||
897 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
898 		TEE_Panic(0);
899 
900 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
901 
902 	res = _utee_hash_update(operation->state, chunk, chunkSize);
903 	if (res != TEE_SUCCESS)
904 		TEE_Panic(res);
905 }
906 
907 void __GP11_TEE_DigestUpdate(TEE_OperationHandle operation,
908 			     const void *chunk, uint32_t chunkSize)
909 {
910 	return TEE_DigestUpdate(operation, chunk, chunkSize);
911 }
912 
913 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
914 			     size_t chunkLen, void *hash, size_t *hashLen)
915 {
916 	TEE_Result res = TEE_SUCCESS;
917 	uint64_t hl = 0;
918 	size_t len = 0;
919 
920 	if ((operation == TEE_HANDLE_NULL) ||
921 	    (!chunk && chunkLen) ||
922 	    (operation->info.operationClass != TEE_OPERATION_DIGEST)) {
923 		res = TEE_ERROR_BAD_PARAMETERS;
924 		goto out;
925 	}
926 	if (operation->operationState == TEE_OPERATION_STATE_EXTRACTING &&
927 	    chunkLen) {
928 		res = TEE_ERROR_BAD_PARAMETERS;
929 		goto out;
930 	}
931 	__utee_check_inout_annotation(hashLen, sizeof(*hashLen));
932 
933 	if (operation->operationState == TEE_OPERATION_STATE_EXTRACTING &&
934 	    operation->buffer) {
935 		/*
936 		 * This is not an Extendable-Output Function and we have
937 		 * already started extracting
938 		 */
939 		len = MIN(operation->block_size - operation->buffer_offs,
940 			  *hashLen);
941 		memcpy(hash, operation->buffer + operation->buffer_offs, len);
942 		*hashLen = len;
943 	} else {
944 		hl = *hashLen;
945 		res = _utee_hash_final(operation->state, chunk, chunkLen, hash,
946 				       &hl);
947 		*hashLen = hl;
948 		if (res)
949 			goto out;
950 	}
951 
952 	/* Reset operation state */
953 	init_hash_operation(operation, NULL, 0);
954 
955 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
956 
957 out:
958 	if (res != TEE_SUCCESS &&
959 	    res != TEE_ERROR_SHORT_BUFFER)
960 		TEE_Panic(res);
961 
962 	return res;
963 }
964 
965 TEE_Result __GP11_TEE_DigestDoFinal(TEE_OperationHandle operation,
966 				    const void *chunk, uint32_t chunkLen,
967 				    void *hash, uint32_t *hashLen)
968 {
969 	TEE_Result res = TEE_SUCCESS;
970 	size_t l = 0;
971 
972 	__utee_check_inout_annotation(hashLen, sizeof(*hashLen));
973 	l = *hashLen;
974 	res = TEE_DigestDoFinal(operation, chunk, chunkLen, hash, &l);
975 	*hashLen = l;
976 	return res;
977 }
978 
979 TEE_Result TEE_DigestExtract(TEE_OperationHandle operation, void *hash,
980 			     size_t *hashLen)
981 {
982 	TEE_Result res = TEE_SUCCESS;
983 	uint64_t hl = 0;
984 	size_t len = 0;
985 
986 	if (operation == TEE_HANDLE_NULL ||
987 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
988 		TEE_Panic(0);
989 	__utee_check_inout_annotation(hashLen, sizeof(*hashLen));
990 
991 	if (!operation->buffer) {
992 		/* This is an Extendable-Output Function */
993 		operation->info.handleState |= TEE_HANDLE_FLAG_EXTRACTING;
994 		operation->operationState = TEE_OPERATION_STATE_EXTRACTING;
995 		hl = *hashLen;
996 		res = _utee_hash_final(operation->state, NULL, 0, hash, &hl);
997 		if (res)
998 			TEE_Panic(0);
999 		*hashLen = hl;
1000 
1001 		return TEE_SUCCESS;
1002 	}
1003 
1004 	if (operation->operationState != TEE_OPERATION_STATE_EXTRACTING) {
1005 		hl = operation->block_size;
1006 		res = _utee_hash_final(operation->state, NULL, 0,
1007 				       operation->buffer, &hl);
1008 		if (res)
1009 			TEE_Panic(0);
1010 		if (hl != operation->block_size)
1011 			TEE_Panic(0);
1012 		assert(!operation->buffer_offs);
1013 		operation->info.handleState |= TEE_HANDLE_FLAG_EXTRACTING;
1014 		operation->operationState = TEE_OPERATION_STATE_EXTRACTING;
1015 	}
1016 
1017 	len = MIN(operation->block_size - operation->buffer_offs, *hashLen);
1018 	memcpy(hash, operation->buffer + operation->buffer_offs, len);
1019 	*hashLen = len;
1020 	operation->buffer_offs += len;
1021 
1022 	return TEE_SUCCESS;
1023 }
1024 
1025 /* Cryptographic Operations API - Symmetric Cipher Functions */
1026 
1027 void TEE_CipherInit(TEE_OperationHandle operation, const void *IV,
1028 		    size_t IVLen)
1029 {
1030 	TEE_Result res;
1031 
1032 	if (operation == TEE_HANDLE_NULL)
1033 		TEE_Panic(0);
1034 
1035 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
1036 		TEE_Panic(0);
1037 
1038 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1039 	    !(operation->key1))
1040 		TEE_Panic(0);
1041 
1042 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1043 		TEE_ResetOperation(operation);
1044 
1045 	if (IV && IVLen) {
1046 		if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
1047 		    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
1048 		    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
1049 		    operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD)
1050 			TEE_Panic(0);
1051 	}
1052 
1053 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1054 
1055 	res = _utee_cipher_init(operation->state, IV, IVLen);
1056 	if (res != TEE_SUCCESS)
1057 		TEE_Panic(res);
1058 
1059 	operation->buffer_offs = 0;
1060 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1061 }
1062 
1063 void __GP11_TEE_CipherInit(TEE_OperationHandle operation, const void *IV,
1064 			   uint32_t IVLen)
1065 {
1066 	return TEE_CipherInit(operation, IV, IVLen);
1067 }
1068 
1069 static TEE_Result tee_buffer_update(
1070 		TEE_OperationHandle op,
1071 		TEE_Result(*update_func)(unsigned long state, const void *src,
1072 				size_t slen, void *dst, uint64_t *dlen),
1073 		const void *src_data, size_t src_len,
1074 		void *dest_data, uint64_t *dest_len)
1075 {
1076 	TEE_Result res;
1077 	const uint8_t *src = src_data;
1078 	size_t slen = src_len;
1079 	uint8_t *dst = dest_data;
1080 	size_t dlen = *dest_len;
1081 	size_t acc_dlen = 0;
1082 	uint64_t tmp_dlen;
1083 	size_t l;
1084 	size_t buffer_size;
1085 	size_t buffer_left;
1086 
1087 	if (!src) {
1088 		if (slen)
1089 			TEE_Panic(0);
1090 		goto out;
1091 	}
1092 
1093 	if (op->buffer_two_blocks) {
1094 		buffer_size = op->block_size * 2;
1095 		buffer_left = 1;
1096 	} else {
1097 		buffer_size = op->block_size;
1098 		buffer_left = 0;
1099 	}
1100 
1101 	if (op->buffer_offs > 0) {
1102 		/* Fill up complete block */
1103 		if (op->buffer_offs < op->block_size)
1104 			l = MIN(slen, op->block_size - op->buffer_offs);
1105 		else
1106 			l = MIN(slen, buffer_size - op->buffer_offs);
1107 		memcpy(op->buffer + op->buffer_offs, src, l);
1108 		op->buffer_offs += l;
1109 		src += l;
1110 		slen -= l;
1111 		if ((op->buffer_offs % op->block_size) != 0)
1112 			goto out;	/* Nothing left to do */
1113 	}
1114 
1115 	/* If we can feed from buffer */
1116 	if ((op->buffer_offs > 0) &&
1117 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
1118 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
1119 				op->block_size);
1120 		l = MIN(op->buffer_offs, l);
1121 		tmp_dlen = dlen;
1122 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
1123 		if (res != TEE_SUCCESS)
1124 			TEE_Panic(res);
1125 		dst += tmp_dlen;
1126 		dlen -= tmp_dlen;
1127 		acc_dlen += tmp_dlen;
1128 		op->buffer_offs -= l;
1129 		if (op->buffer_offs > 0) {
1130 			/*
1131 			 * Slen is small enough to be contained in rest buffer.
1132 			 */
1133 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
1134 			memcpy(op->buffer + op->buffer_offs, src, slen);
1135 			op->buffer_offs += slen;
1136 			goto out;	/* Nothing left to do */
1137 		}
1138 	}
1139 
1140 	if (slen >= (buffer_size + buffer_left)) {
1141 		/* Buffer is empty, feed as much as possible from src */
1142 		if (op->info.algorithm == TEE_ALG_AES_CTS)
1143 			l = ROUNDUP(slen - buffer_size, op->block_size);
1144 		else
1145 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
1146 
1147 		tmp_dlen = dlen;
1148 		res = update_func(op->state, src, l, dst, &tmp_dlen);
1149 		if (res != TEE_SUCCESS)
1150 			TEE_Panic(res);
1151 		src += l;
1152 		slen -= l;
1153 		dst += tmp_dlen;
1154 		dlen -= tmp_dlen;
1155 		acc_dlen += tmp_dlen;
1156 	}
1157 
1158 	/* Slen is small enough to be contained in buffer. */
1159 	memcpy(op->buffer + op->buffer_offs, src, slen);
1160 	op->buffer_offs += slen;
1161 
1162 out:
1163 	*dest_len = acc_dlen;
1164 	return TEE_SUCCESS;
1165 }
1166 
1167 TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData,
1168 			    size_t srcLen, void *destData, size_t *destLen)
1169 {
1170 	TEE_Result res;
1171 	size_t req_dlen;
1172 	uint64_t dl;
1173 
1174 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1175 		res = TEE_ERROR_BAD_PARAMETERS;
1176 		goto out;
1177 	}
1178 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1179 
1180 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1181 		res = TEE_ERROR_BAD_PARAMETERS;
1182 		goto out;
1183 	}
1184 
1185 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1186 		res = TEE_ERROR_BAD_PARAMETERS;
1187 		goto out;
1188 	}
1189 
1190 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1191 		res = TEE_ERROR_BAD_PARAMETERS;
1192 		goto out;
1193 	}
1194 
1195 	if (!srcData && !srcLen) {
1196 		*destLen = 0;
1197 		res = TEE_SUCCESS;
1198 		goto out;
1199 	}
1200 
1201 	/* Calculate required dlen */
1202 	if (operation->block_size > 1) {
1203 		req_dlen = ((operation->buffer_offs + srcLen) /
1204 			    operation->block_size) * operation->block_size;
1205 	} else {
1206 		req_dlen = srcLen;
1207 	}
1208 	if (operation->buffer_two_blocks) {
1209 		if (req_dlen > operation->block_size * 2)
1210 			req_dlen -= operation->block_size * 2;
1211 		else
1212 			req_dlen = 0;
1213 	}
1214 	/*
1215 	 * Check that required destLen is big enough before starting to feed
1216 	 * data to the algorithm. Errors during feeding of data are fatal as we
1217 	 * can't restore sync with this API.
1218 	 */
1219 	if (*destLen < req_dlen) {
1220 		*destLen = req_dlen;
1221 		res = TEE_ERROR_SHORT_BUFFER;
1222 		goto out;
1223 	}
1224 
1225 	dl = *destLen;
1226 	if (operation->block_size > 1) {
1227 		res = tee_buffer_update(operation, _utee_cipher_update, srcData,
1228 					srcLen, destData, &dl);
1229 	} else {
1230 		if (srcLen > 0) {
1231 			res = _utee_cipher_update(operation->state, srcData,
1232 						  srcLen, destData, &dl);
1233 		} else {
1234 			res = TEE_SUCCESS;
1235 			dl = 0;
1236 		}
1237 	}
1238 	*destLen = dl;
1239 
1240 out:
1241 	if (res != TEE_SUCCESS &&
1242 	    res != TEE_ERROR_SHORT_BUFFER)
1243 		TEE_Panic(res);
1244 
1245 	return res;
1246 }
1247 
1248 TEE_Result __GP11_TEE_CipherUpdate(TEE_OperationHandle operation,
1249 				   const void *srcData, uint32_t srcLen,
1250 				   void *destData, uint32_t *destLen)
1251 {
1252 	TEE_Result res = TEE_SUCCESS;
1253 	size_t dl = 0;
1254 
1255 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1256 	dl = *destLen;
1257 	res = TEE_CipherUpdate(operation, srcData, srcLen, destData, &dl);
1258 	*destLen = dl;
1259 	return res;
1260 }
1261 
1262 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation,
1263 			     const void *srcData, size_t srcLen,
1264 			     void *destData, size_t *destLen)
1265 {
1266 	TEE_Result res = TEE_SUCCESS;
1267 	uint8_t *dst = destData;
1268 	size_t acc_dlen = 0;
1269 	uint64_t tmp_dlen = 0;
1270 	size_t req_dlen = 0;
1271 
1272 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1273 		res = TEE_ERROR_BAD_PARAMETERS;
1274 		goto out;
1275 	}
1276 	if (destLen)
1277 		__utee_check_inout_annotation(destLen, sizeof(*destLen));
1278 
1279 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1280 		res = TEE_ERROR_BAD_PARAMETERS;
1281 		goto out;
1282 	}
1283 
1284 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1285 		res = TEE_ERROR_BAD_PARAMETERS;
1286 		goto out;
1287 	}
1288 
1289 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1290 		res = TEE_ERROR_BAD_PARAMETERS;
1291 		goto out;
1292 	}
1293 
1294 	/*
1295 	 * Check that the final block doesn't require padding for those
1296 	 * algorithms that requires client to supply padding.
1297 	 */
1298 	if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
1299 	    operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1300 	    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
1301 	    operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1302 	    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
1303 	    operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD ||
1304 	    operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD ||
1305 	    operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) {
1306 		if (((operation->buffer_offs + srcLen) % operation->block_size)
1307 		    != 0) {
1308 			res = TEE_ERROR_BAD_PARAMETERS;
1309 			goto out;
1310 		}
1311 	}
1312 
1313 	/*
1314 	 * Check that required destLen is big enough before starting to feed
1315 	 * data to the algorithm. Errors during feeding of data are fatal as we
1316 	 * can't restore sync with this API.
1317 	 */
1318 	if (operation->block_size > 1) {
1319 		req_dlen = operation->buffer_offs + srcLen;
1320 	} else {
1321 		req_dlen = srcLen;
1322 	}
1323 	if (destLen)
1324 		tmp_dlen = *destLen;
1325 	if (tmp_dlen < req_dlen) {
1326 		if (destLen)
1327 			*destLen = req_dlen;
1328 		res = TEE_ERROR_SHORT_BUFFER;
1329 		goto out;
1330 	}
1331 
1332 	if (operation->block_size > 1) {
1333 		if (srcLen) {
1334 			res = tee_buffer_update(operation, _utee_cipher_update,
1335 						srcData, srcLen, dst,
1336 						&tmp_dlen);
1337 			if (res != TEE_SUCCESS)
1338 				goto out;
1339 
1340 			dst += tmp_dlen;
1341 			acc_dlen += tmp_dlen;
1342 
1343 			tmp_dlen = *destLen - acc_dlen;
1344 		}
1345 		res = _utee_cipher_final(operation->state, operation->buffer,
1346 					 operation->buffer_offs, dst,
1347 					 &tmp_dlen);
1348 	} else {
1349 		res = _utee_cipher_final(operation->state, srcData, srcLen, dst,
1350 					 &tmp_dlen);
1351 	}
1352 	if (res != TEE_SUCCESS)
1353 		goto out;
1354 
1355 	acc_dlen += tmp_dlen;
1356 	if (destLen)
1357 		*destLen = acc_dlen;
1358 
1359 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1360 
1361 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1362 
1363 out:
1364 	if (res != TEE_SUCCESS &&
1365 	    res != TEE_ERROR_SHORT_BUFFER)
1366 		TEE_Panic(res);
1367 
1368 	return res;
1369 }
1370 
1371 TEE_Result __GP11_TEE_CipherDoFinal(TEE_OperationHandle operation,
1372 				    const void *srcData, uint32_t srcLen,
1373 				    void *destData, uint32_t *destLen)
1374 {
1375 	TEE_Result res = TEE_SUCCESS;
1376 	size_t dl = 0;
1377 
1378 	if (destLen) {
1379 		__utee_check_inout_annotation(destLen, sizeof(*destLen));
1380 		dl = *destLen;
1381 	}
1382 	res = TEE_CipherDoFinal(operation, srcData, srcLen, destData, &dl);
1383 	if (destLen)
1384 		*destLen = dl;
1385 	return res;
1386 }
1387 
1388 /* Cryptographic Operations API - MAC Functions */
1389 
1390 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, size_t IVLen)
1391 {
1392 	if (operation == TEE_HANDLE_NULL)
1393 		TEE_Panic(0);
1394 
1395 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1396 		TEE_Panic(0);
1397 
1398 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1399 	    !(operation->key1))
1400 		TEE_Panic(0);
1401 
1402 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1403 		TEE_ResetOperation(operation);
1404 
1405 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1406 
1407 	init_hash_operation(operation, IV, IVLen);
1408 }
1409 
1410 void __GP11_TEE_MACInit(TEE_OperationHandle operation, const void *IV,
1411 			uint32_t IVLen)
1412 {
1413 	return TEE_MACInit(operation, IV, IVLen);
1414 }
1415 
1416 void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk,
1417 		   size_t chunkSize)
1418 {
1419 	TEE_Result res;
1420 
1421 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
1422 		TEE_Panic(0);
1423 
1424 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1425 		TEE_Panic(0);
1426 
1427 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1428 		TEE_Panic(0);
1429 
1430 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE)
1431 		TEE_Panic(0);
1432 
1433 	res = _utee_hash_update(operation->state, chunk, chunkSize);
1434 	if (res != TEE_SUCCESS)
1435 		TEE_Panic(res);
1436 }
1437 
1438 void __GP11_TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk,
1439 			  uint32_t chunkSize)
1440 {
1441 	return TEE_MACUpdate(operation, chunk, chunkSize);
1442 }
1443 
1444 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
1445 			       const void *message, size_t messageLen,
1446 			       void *mac, size_t *macLen)
1447 {
1448 	TEE_Result res;
1449 	uint64_t ml;
1450 
1451 	if (operation == TEE_HANDLE_NULL || (!message && messageLen)) {
1452 		res = TEE_ERROR_BAD_PARAMETERS;
1453 		goto out;
1454 	}
1455 	__utee_check_inout_annotation(macLen, sizeof(*macLen));
1456 
1457 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1458 		res = TEE_ERROR_BAD_PARAMETERS;
1459 		goto out;
1460 	}
1461 
1462 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1463 		res = TEE_ERROR_BAD_PARAMETERS;
1464 		goto out;
1465 	}
1466 
1467 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1468 		res = TEE_ERROR_BAD_PARAMETERS;
1469 		goto out;
1470 	}
1471 
1472 	ml = *macLen;
1473 	res = _utee_hash_final(operation->state, message, messageLen, mac, &ml);
1474 	*macLen = ml;
1475 	if (res != TEE_SUCCESS)
1476 		goto out;
1477 
1478 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1479 
1480 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1481 
1482 out:
1483 	if (res != TEE_SUCCESS &&
1484 	    res != TEE_ERROR_SHORT_BUFFER)
1485 		TEE_Panic(res);
1486 
1487 	return res;
1488 }
1489 
1490 TEE_Result __GP11_TEE_MACComputeFinal(TEE_OperationHandle operation,
1491 				      const void *message, uint32_t messageLen,
1492 				      void *mac, uint32_t *macLen)
1493 {
1494 	TEE_Result res = TEE_SUCCESS;
1495 	size_t ml = 0;
1496 
1497 	__utee_check_inout_annotation(macLen, sizeof(*macLen));
1498 	ml = *macLen;
1499 	res = TEE_MACComputeFinal(operation, message, messageLen, mac, &ml);
1500 	*macLen = ml;
1501 	return res;
1502 }
1503 
1504 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
1505 			       const void *message, size_t messageLen,
1506 			       const void *mac, size_t macLen)
1507 {
1508 	TEE_Result res;
1509 	uint8_t computed_mac[TEE_MAX_HASH_SIZE] = { 0 };
1510 	size_t computed_mac_size = TEE_MAX_HASH_SIZE;
1511 
1512 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1513 		res = TEE_ERROR_BAD_PARAMETERS;
1514 		goto out;
1515 	}
1516 
1517 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1518 		res = TEE_ERROR_BAD_PARAMETERS;
1519 		goto out;
1520 	}
1521 
1522 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1523 		res = TEE_ERROR_BAD_PARAMETERS;
1524 		goto out;
1525 	}
1526 
1527 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1528 				  &computed_mac_size);
1529 	if (res != TEE_SUCCESS)
1530 		goto out;
1531 
1532 	if (computed_mac_size != macLen) {
1533 		res = TEE_ERROR_MAC_INVALID;
1534 		goto out;
1535 	}
1536 
1537 	if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) {
1538 		res = TEE_ERROR_MAC_INVALID;
1539 		goto out;
1540 	}
1541 
1542 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1543 
1544 out:
1545 	if (res != TEE_SUCCESS &&
1546 	    res != TEE_ERROR_MAC_INVALID)
1547 		TEE_Panic(res);
1548 
1549 	return res;
1550 }
1551 
1552 TEE_Result __GP11_TEE_MACCompareFinal(TEE_OperationHandle operation,
1553 				      const void *message, uint32_t messageLen,
1554 				      const void *mac, uint32_t macLen)
1555 {
1556 	return TEE_MACCompareFinal(operation, message, messageLen, mac, macLen);
1557 }
1558 
1559 /* Cryptographic Operations API - Authenticated Encryption Functions */
1560 
1561 TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce,
1562 		      size_t nonceLen, uint32_t tagLen, size_t AADLen,
1563 		      size_t payloadLen)
1564 {
1565 	TEE_Result res;
1566 
1567 	if (operation == TEE_HANDLE_NULL || nonce == NULL) {
1568 		res = TEE_ERROR_BAD_PARAMETERS;
1569 		goto out;
1570 	}
1571 
1572 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1573 		res = TEE_ERROR_BAD_PARAMETERS;
1574 		goto out;
1575 	}
1576 
1577 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
1578 		res = TEE_ERROR_BAD_PARAMETERS;
1579 		goto out;
1580 	}
1581 
1582 	/*
1583 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1584 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1585 	 * according to the same principle so we have to check here instead to
1586 	 * be GP compliant.
1587 	 */
1588 	if (operation->info.algorithm == TEE_ALG_AES_GCM) {
1589 		/*
1590 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1591 		 */
1592 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) {
1593 			res = TEE_ERROR_NOT_SUPPORTED;
1594 			goto out;
1595 		}
1596 	}
1597 
1598 	res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8,
1599 				 AADLen, payloadLen);
1600 	if (res != TEE_SUCCESS)
1601 		goto out;
1602 
1603 	operation->info.digestLength = tagLen / 8;
1604 	operation->buffer_offs = 0;
1605 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1606 
1607 out:
1608 	if (res != TEE_SUCCESS &&
1609 	    res != TEE_ERROR_NOT_SUPPORTED)
1610 			TEE_Panic(res);
1611 
1612 	return res;
1613 }
1614 
1615 TEE_Result __GP11_TEE_AEInit(TEE_OperationHandle operation, const void *nonce,
1616 			     uint32_t nonceLen, uint32_t tagLen,
1617 			     uint32_t AADLen, uint32_t payloadLen)
1618 {
1619 	return TEE_AEInit(operation, nonce, nonceLen, tagLen, AADLen,
1620 			  payloadLen);
1621 }
1622 
1623 void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata,
1624 		     size_t AADdataLen)
1625 {
1626 	TEE_Result res = TEE_SUCCESS;
1627 
1628 	if (operation == TEE_HANDLE_NULL || (!AADdata && AADdataLen))
1629 		TEE_Panic(0);
1630 
1631 	if (operation->info.operationClass != TEE_OPERATION_AE)
1632 		TEE_Panic(0);
1633 
1634 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1635 		TEE_Panic(0);
1636 
1637 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1638 		TEE_Panic(0);
1639 
1640 	res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen);
1641 	if (res != TEE_SUCCESS)
1642 		TEE_Panic(res);
1643 }
1644 
1645 void __GP11_TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata,
1646 			    uint32_t AADdataLen)
1647 {
1648 	TEE_Result res = TEE_SUCCESS;
1649 
1650 	if (operation == TEE_HANDLE_NULL ||
1651 	    (AADdata == NULL && AADdataLen != 0))
1652 		TEE_Panic(0);
1653 
1654 	if (operation->info.operationClass != TEE_OPERATION_AE)
1655 		TEE_Panic(0);
1656 
1657 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1658 		TEE_Panic(0);
1659 
1660 	res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen);
1661 
1662 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1663 
1664 	if (res != TEE_SUCCESS)
1665 		TEE_Panic(res);
1666 }
1667 
1668 static TEE_Result ae_update_helper(TEE_OperationHandle operation,
1669 				   const void *src, size_t slen, void *dst,
1670 				   size_t *dlen)
1671 {
1672 	TEE_Result res = TEE_SUCCESS;
1673 	size_t req_dlen = 0;
1674 	uint64_t dl = 0;
1675 
1676 	if (!src && !slen) {
1677 		*dlen = 0;
1678 		return TEE_SUCCESS;
1679 	}
1680 
1681 	/*
1682 	 * Check that required destLen is big enough before starting to feed
1683 	 * data to the algorithm. Errors during feeding of data are fatal as we
1684 	 * can't restore sync with this API.
1685 	 */
1686 	if (operation->block_size > 1) {
1687 		req_dlen = ROUNDDOWN(operation->buffer_offs + slen,
1688 				     operation->block_size);
1689 	} else {
1690 		req_dlen = slen;
1691 	}
1692 
1693 	dl = *dlen;
1694 	if (dl < req_dlen) {
1695 		*dlen = req_dlen;
1696 		return TEE_ERROR_SHORT_BUFFER;
1697 	}
1698 
1699 	if (operation->block_size > 1) {
1700 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1701 					src, slen, dst, &dl);
1702 	} else {
1703 		if (slen > 0) {
1704 			res = _utee_authenc_update_payload(operation->state,
1705 							   src, slen, dst, &dl);
1706 		} else {
1707 			dl = 0;
1708 			res = TEE_SUCCESS;
1709 		}
1710 	}
1711 
1712 	if (!res)
1713 		*dlen = dl;
1714 
1715 	return res;
1716 }
1717 
1718 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData,
1719 			size_t srcLen, void *destData, size_t *destLen)
1720 {
1721 	TEE_Result res = TEE_SUCCESS;
1722 
1723 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1724 		res = TEE_ERROR_BAD_PARAMETERS;
1725 		goto out;
1726 	}
1727 	__utee_check_outbuf_annotation(destData, destLen);
1728 
1729 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1730 		res = TEE_ERROR_BAD_PARAMETERS;
1731 		goto out;
1732 	}
1733 
1734 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1735 		res = TEE_ERROR_BAD_PARAMETERS;
1736 		goto out;
1737 	}
1738 
1739 	res = ae_update_helper(operation, srcData, srcLen, destData, destLen);
1740 	if (res != TEE_ERROR_SHORT_BUFFER && srcLen)
1741 		operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1742 
1743 out:
1744 	if (res != TEE_SUCCESS &&
1745 	    res != TEE_ERROR_SHORT_BUFFER)
1746 		TEE_Panic(res);
1747 
1748 	return res;
1749 }
1750 
1751 TEE_Result __GP11_TEE_AEUpdate(TEE_OperationHandle operation,
1752 			       const void *srcData, uint32_t srcLen,
1753 			       void *destData, uint32_t *destLen)
1754 {
1755 	TEE_Result res = TEE_SUCCESS;
1756 	size_t dl = 0;
1757 
1758 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1759 		res = TEE_ERROR_BAD_PARAMETERS;
1760 		goto out;
1761 	}
1762 	__utee_check_gp11_outbuf_annotation(destData, destLen);
1763 
1764 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1765 		res = TEE_ERROR_BAD_PARAMETERS;
1766 		goto out;
1767 	}
1768 
1769 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1770 		res = TEE_ERROR_BAD_PARAMETERS;
1771 		goto out;
1772 	}
1773 
1774 	dl = *destLen;
1775 	res = ae_update_helper(operation, srcData, srcLen, destData, &dl);
1776 	*destLen = dl;
1777 
1778 	if (res != TEE_SUCCESS)
1779 		goto out;
1780 
1781 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1782 
1783 out:
1784 	if (res != TEE_SUCCESS &&
1785 	    res != TEE_ERROR_SHORT_BUFFER)
1786 			TEE_Panic(res);
1787 
1788 	return res;
1789 }
1790 
1791 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation,
1792 			      const void *srcData, size_t srcLen,
1793 			      void *destData, size_t *destLen, void *tag,
1794 			      size_t *tagLen)
1795 {
1796 	TEE_Result res = TEE_SUCCESS;
1797 	uint8_t *dst = destData;
1798 	size_t acc_dlen = 0;
1799 	uint64_t tmp_dlen = 0;
1800 	size_t req_dlen = 0;
1801 	uint64_t tl = 0;
1802 
1803 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1804 		res = TEE_ERROR_BAD_PARAMETERS;
1805 		goto out;
1806 	}
1807 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1808 	__utee_check_inout_annotation(tagLen, sizeof(*tagLen));
1809 
1810 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1811 		res = TEE_ERROR_BAD_PARAMETERS;
1812 		goto out;
1813 	}
1814 
1815 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1816 		res = TEE_ERROR_BAD_PARAMETERS;
1817 		goto out;
1818 	}
1819 
1820 	/*
1821 	 * Check that required destLen is big enough before starting to feed
1822 	 * data to the algorithm. Errors during feeding of data are fatal as we
1823 	 * can't restore sync with this API.
1824 	 *
1825 	 * Need to check this before update_payload since sync would be lost if
1826 	 * we return short buffer after that.
1827 	 */
1828 	res = TEE_ERROR_GENERIC;
1829 
1830 	req_dlen = operation->buffer_offs + srcLen;
1831 	if (*destLen < req_dlen) {
1832 		*destLen = req_dlen;
1833 		res = TEE_ERROR_SHORT_BUFFER;
1834 	}
1835 
1836 	if (*tagLen < operation->info.digestLength) {
1837 		*tagLen = operation->info.digestLength;
1838 		res = TEE_ERROR_SHORT_BUFFER;
1839 	}
1840 
1841 	if (res == TEE_ERROR_SHORT_BUFFER)
1842 		goto out;
1843 
1844 	tl = *tagLen;
1845 	tmp_dlen = *destLen - acc_dlen;
1846 	if (operation->block_size > 1) {
1847 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1848 					srcData, srcLen, dst, &tmp_dlen);
1849 		if (res != TEE_SUCCESS)
1850 			goto out;
1851 
1852 		dst += tmp_dlen;
1853 		acc_dlen += tmp_dlen;
1854 
1855 		tmp_dlen = *destLen - acc_dlen;
1856 		res = _utee_authenc_enc_final(operation->state,
1857 					      operation->buffer,
1858 					      operation->buffer_offs, dst,
1859 					      &tmp_dlen, tag, &tl);
1860 	} else {
1861 		res = _utee_authenc_enc_final(operation->state, srcData,
1862 					      srcLen, dst, &tmp_dlen,
1863 					      tag, &tl);
1864 	}
1865 	*tagLen = tl;
1866 	if (res != TEE_SUCCESS)
1867 		goto out;
1868 
1869 	acc_dlen += tmp_dlen;
1870 	*destLen = acc_dlen;
1871 
1872 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1873 
1874 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1875 
1876 out:
1877 	if (res != TEE_SUCCESS &&
1878 	    res != TEE_ERROR_SHORT_BUFFER)
1879 			TEE_Panic(res);
1880 
1881 	return res;
1882 }
1883 
1884 TEE_Result __GP11_TEE_AEEncryptFinal(TEE_OperationHandle operation,
1885 				     const void *srcData, uint32_t srcLen,
1886 				     void *destData, uint32_t *destLen,
1887 				     void *tag, uint32_t *tagLen)
1888 {
1889 	TEE_Result res = TEE_SUCCESS;
1890 	size_t dl = 0;
1891 	size_t tl = 0;
1892 
1893 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1894 	__utee_check_inout_annotation(tagLen, sizeof(*tagLen));
1895 	dl = *destLen;
1896 	tl = *tagLen;
1897 	res = TEE_AEEncryptFinal(operation, srcData, srcLen, destData, &dl,
1898 				 tag, &tl);
1899 	*destLen = dl;
1900 	*tagLen = tl;
1901 	return res;
1902 }
1903 
1904 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation,
1905 			      const void *srcData, size_t srcLen,
1906 			      void *destData, size_t *destLen, void *tag,
1907 			      size_t tagLen)
1908 {
1909 	TEE_Result res = TEE_SUCCESS;
1910 	uint8_t *dst = destData;
1911 	size_t acc_dlen = 0;
1912 	uint64_t tmp_dlen = 0;
1913 	size_t req_dlen = 0;
1914 
1915 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1916 		res = TEE_ERROR_BAD_PARAMETERS;
1917 		goto out;
1918 	}
1919 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1920 
1921 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1922 		res = TEE_ERROR_BAD_PARAMETERS;
1923 		goto out;
1924 	}
1925 
1926 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1927 		res = TEE_ERROR_BAD_PARAMETERS;
1928 		goto out;
1929 	}
1930 
1931 	/*
1932 	 * Check that required destLen is big enough before starting to feed
1933 	 * data to the algorithm. Errors during feeding of data are fatal as we
1934 	 * can't restore sync with this API.
1935 	 */
1936 	req_dlen = operation->buffer_offs + srcLen;
1937 	if (*destLen < req_dlen) {
1938 		*destLen = req_dlen;
1939 		res = TEE_ERROR_SHORT_BUFFER;
1940 		goto out;
1941 	}
1942 
1943 	tmp_dlen = *destLen - acc_dlen;
1944 	if (operation->block_size > 1) {
1945 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1946 					srcData, srcLen, dst, &tmp_dlen);
1947 		if (res != TEE_SUCCESS)
1948 			goto out;
1949 
1950 		dst += tmp_dlen;
1951 		acc_dlen += tmp_dlen;
1952 
1953 		tmp_dlen = *destLen - acc_dlen;
1954 		res = _utee_authenc_dec_final(operation->state,
1955 					      operation->buffer,
1956 					      operation->buffer_offs, dst,
1957 					      &tmp_dlen, tag, tagLen);
1958 	} else {
1959 		res = _utee_authenc_dec_final(operation->state, srcData,
1960 					      srcLen, dst, &tmp_dlen,
1961 					      tag, tagLen);
1962 	}
1963 	if (res != TEE_SUCCESS)
1964 		goto out;
1965 
1966 	/* Supplied tagLen should match what we initiated with */
1967 	if (tagLen != operation->info.digestLength)
1968 		res = TEE_ERROR_MAC_INVALID;
1969 
1970 	acc_dlen += tmp_dlen;
1971 	*destLen = acc_dlen;
1972 
1973 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1974 
1975 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1976 
1977 out:
1978 	if (res != TEE_SUCCESS &&
1979 	    res != TEE_ERROR_SHORT_BUFFER &&
1980 	    res != TEE_ERROR_MAC_INVALID)
1981 			TEE_Panic(res);
1982 
1983 	return res;
1984 }
1985 
1986 TEE_Result __GP11_TEE_AEDecryptFinal(TEE_OperationHandle operation,
1987 				     const void *srcData, uint32_t srcLen,
1988 				     void *destData, uint32_t *destLen,
1989 				     void *tag, uint32_t tagLen)
1990 {
1991 	TEE_Result res = TEE_SUCCESS;
1992 	size_t dl = 0;
1993 
1994 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1995 	dl = *destLen;
1996 	res = TEE_AEDecryptFinal(operation, srcData, srcLen, destData, &dl,
1997 				 tag, tagLen);
1998 	*destLen = dl;
1999 	return res;
2000 }
2001 
2002 /* Cryptographic Operations API - Asymmetric Functions */
2003 
2004 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation,
2005 				 const TEE_Attribute *params,
2006 				 uint32_t paramCount, const void *srcData,
2007 				 size_t srcLen, void *destData,
2008 				 size_t *destLen)
2009 {
2010 	TEE_Result res = TEE_SUCCESS;
2011 	struct utee_attribute ua[paramCount];
2012 	uint64_t dl = 0;
2013 
2014 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
2015 		TEE_Panic(0);
2016 
2017 	__utee_check_attr_in_annotation(params, paramCount);
2018 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
2019 
2020 	if (!operation->key1)
2021 		TEE_Panic(0);
2022 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
2023 		TEE_Panic(0);
2024 	if (operation->info.mode != TEE_MODE_ENCRYPT)
2025 		TEE_Panic(0);
2026 
2027 	__utee_from_attr(ua, params, paramCount);
2028 	dl = *destLen;
2029 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
2030 				  srcLen, destData, &dl);
2031 	*destLen = dl;
2032 
2033 	if (res != TEE_SUCCESS &&
2034 	    res != TEE_ERROR_SHORT_BUFFER &&
2035 	    res != TEE_ERROR_BAD_PARAMETERS &&
2036 	    res != TEE_ERROR_CIPHERTEXT_INVALID &&
2037 	    res != TEE_ERROR_NOT_SUPPORTED)
2038 		TEE_Panic(res);
2039 
2040 	return res;
2041 }
2042 
2043 TEE_Result __GP11_TEE_AsymmetricEncrypt(TEE_OperationHandle operation,
2044 					const __GP11_TEE_Attribute *params,
2045 					uint32_t paramCount,
2046 					const void *srcData, uint32_t srcLen,
2047 					void *destData, uint32_t *destLen)
2048 {
2049 	TEE_Result res = TEE_SUCCESS;
2050 	struct utee_attribute ua[paramCount];
2051 	uint64_t dl = 0;
2052 
2053 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
2054 		TEE_Panic(0);
2055 
2056 	__utee_check_gp11_attr_in_annotation(params, paramCount);
2057 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
2058 
2059 	if (!operation->key1)
2060 		TEE_Panic(0);
2061 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
2062 		TEE_Panic(0);
2063 	if (operation->info.mode != TEE_MODE_ENCRYPT)
2064 		TEE_Panic(0);
2065 
2066 	__utee_from_gp11_attr(ua, params, paramCount);
2067 	dl = *destLen;
2068 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
2069 				  srcLen, destData, &dl);
2070 	*destLen = dl;
2071 
2072 	if (res != TEE_SUCCESS &&
2073 	    res != TEE_ERROR_SHORT_BUFFER &&
2074 	    res != TEE_ERROR_BAD_PARAMETERS)
2075 		TEE_Panic(res);
2076 
2077 	return res;
2078 }
2079 
2080 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation,
2081 				 const TEE_Attribute *params,
2082 				 uint32_t paramCount, const void *srcData,
2083 				 size_t srcLen, void *destData,
2084 				 size_t *destLen)
2085 {
2086 	TEE_Result res = TEE_SUCCESS;
2087 	struct utee_attribute ua[paramCount];
2088 	uint64_t dl = 0;
2089 
2090 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
2091 		TEE_Panic(0);
2092 
2093 	__utee_check_attr_in_annotation(params, paramCount);
2094 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
2095 
2096 	if (!operation->key1)
2097 		TEE_Panic(0);
2098 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
2099 		TEE_Panic(0);
2100 	if (operation->info.mode != TEE_MODE_DECRYPT)
2101 		TEE_Panic(0);
2102 
2103 	__utee_from_attr(ua, params, paramCount);
2104 	dl = *destLen;
2105 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
2106 				  srcLen, destData, &dl);
2107 	*destLen = dl;
2108 
2109 	if (res != TEE_SUCCESS &&
2110 	    res != TEE_ERROR_SHORT_BUFFER &&
2111 	    res != TEE_ERROR_BAD_PARAMETERS &&
2112 	    res != TEE_ERROR_CIPHERTEXT_INVALID &&
2113 	    res != TEE_ERROR_NOT_SUPPORTED)
2114 		TEE_Panic(res);
2115 
2116 	return res;
2117 }
2118 
2119 TEE_Result __GP11_TEE_AsymmetricDecrypt(TEE_OperationHandle operation,
2120 					const __GP11_TEE_Attribute *params,
2121 					uint32_t paramCount,
2122 					const void *srcData, uint32_t srcLen,
2123 					void *destData, uint32_t *destLen)
2124 {
2125 	TEE_Result res = TEE_SUCCESS;
2126 	struct utee_attribute ua[paramCount];
2127 	uint64_t dl = 0;
2128 
2129 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
2130 		TEE_Panic(0);
2131 
2132 	__utee_check_gp11_attr_in_annotation(params, paramCount);
2133 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
2134 
2135 	if (!operation->key1)
2136 		TEE_Panic(0);
2137 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
2138 		TEE_Panic(0);
2139 	if (operation->info.mode != TEE_MODE_DECRYPT)
2140 		TEE_Panic(0);
2141 
2142 	__utee_from_gp11_attr(ua, params, paramCount);
2143 	dl = *destLen;
2144 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
2145 				  srcLen, destData, &dl);
2146 	*destLen = dl;
2147 
2148 	if (res != TEE_SUCCESS &&
2149 	    res != TEE_ERROR_SHORT_BUFFER &&
2150 	    res != TEE_ERROR_BAD_PARAMETERS)
2151 		TEE_Panic(res);
2152 
2153 	return res;
2154 }
2155 
2156 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation,
2157 				    const TEE_Attribute *params,
2158 				    uint32_t paramCount, const void *digest,
2159 				    size_t digestLen, void *signature,
2160 				    size_t *signatureLen)
2161 {
2162 	TEE_Result res = TEE_SUCCESS;
2163 	struct utee_attribute ua[paramCount];
2164 	uint64_t sl = 0;
2165 
2166 	if (operation == TEE_HANDLE_NULL || (!digest && digestLen))
2167 		TEE_Panic(0);
2168 
2169 	__utee_check_attr_in_annotation(params, paramCount);
2170 	__utee_check_inout_annotation(signatureLen, sizeof(*signatureLen));
2171 
2172 	if (!operation->key1)
2173 		TEE_Panic(0);
2174 	if (operation->info.operationClass !=
2175 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
2176 		TEE_Panic(0);
2177 	if (operation->info.mode != TEE_MODE_SIGN)
2178 		TEE_Panic(0);
2179 
2180 	__utee_from_attr(ua, params, paramCount);
2181 	sl = *signatureLen;
2182 	res = _utee_asymm_operate(operation->state, ua, paramCount, digest,
2183 				  digestLen, signature, &sl);
2184 	*signatureLen = sl;
2185 
2186 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
2187 		TEE_Panic(res);
2188 
2189 	return res;
2190 }
2191 
2192 TEE_Result __GP11_TEE_AsymmetricSignDigest(TEE_OperationHandle operation,
2193 					   const __GP11_TEE_Attribute *params,
2194 					   uint32_t paramCount,
2195 					   const void *digest,
2196 					   uint32_t digestLen, void *signature,
2197 					   uint32_t *signatureLen)
2198 {
2199 	TEE_Result res = TEE_SUCCESS;
2200 	struct utee_attribute ua[paramCount];
2201 	uint64_t sl = 0;
2202 
2203 	if (operation == TEE_HANDLE_NULL || (!digest && digestLen))
2204 		TEE_Panic(0);
2205 
2206 	__utee_check_gp11_attr_in_annotation(params, paramCount);
2207 	__utee_check_inout_annotation(signatureLen, sizeof(*signatureLen));
2208 
2209 	if (!operation->key1)
2210 		TEE_Panic(0);
2211 	if (operation->info.operationClass !=
2212 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
2213 		TEE_Panic(0);
2214 	if (operation->info.mode != TEE_MODE_SIGN)
2215 		TEE_Panic(0);
2216 
2217 	__utee_from_gp11_attr(ua, params, paramCount);
2218 	sl = *signatureLen;
2219 	res = _utee_asymm_operate(operation->state, ua, paramCount, digest,
2220 				  digestLen, signature, &sl);
2221 	*signatureLen = sl;
2222 
2223 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
2224 		TEE_Panic(res);
2225 
2226 	return res;
2227 }
2228 
2229 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,
2230 				      const TEE_Attribute *params,
2231 				      uint32_t paramCount, const void *digest,
2232 				      size_t digestLen,
2233 				      const void *signature,
2234 				      size_t signatureLen)
2235 {
2236 	TEE_Result res;
2237 	struct utee_attribute ua[paramCount];
2238 
2239 	if (operation == TEE_HANDLE_NULL ||
2240 	    (digest == NULL && digestLen != 0) ||
2241 	    (signature == NULL && signatureLen != 0))
2242 		TEE_Panic(0);
2243 
2244 	__utee_check_attr_in_annotation(params, paramCount);
2245 
2246 	if (!operation->key1)
2247 		TEE_Panic(0);
2248 	if (operation->info.operationClass !=
2249 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
2250 		TEE_Panic(0);
2251 	if (operation->info.mode != TEE_MODE_VERIFY)
2252 		TEE_Panic(0);
2253 
2254 	__utee_from_attr(ua, params, paramCount);
2255 	res = _utee_asymm_verify(operation->state, ua, paramCount, digest,
2256 				 digestLen, signature, signatureLen);
2257 
2258 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
2259 		TEE_Panic(res);
2260 
2261 	return res;
2262 }
2263 
2264 TEE_Result __GP11_TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,
2265 					     const __GP11_TEE_Attribute *params,
2266 					     uint32_t paramCount,
2267 					     const void *digest,
2268 					     uint32_t digestLen,
2269 					     const void *signature,
2270 					     uint32_t signatureLen)
2271 {
2272 	TEE_Result res = TEE_SUCCESS;
2273 	struct utee_attribute ua[paramCount];
2274 
2275 	if (operation == TEE_HANDLE_NULL || (!digest && digestLen) ||
2276 	    (!signature && signatureLen))
2277 		TEE_Panic(0);
2278 
2279 	__utee_check_gp11_attr_in_annotation(params, paramCount);
2280 
2281 	if (!operation->key1)
2282 		TEE_Panic(0);
2283 	if (operation->info.operationClass !=
2284 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
2285 		TEE_Panic(0);
2286 	if (operation->info.mode != TEE_MODE_VERIFY)
2287 		TEE_Panic(0);
2288 
2289 	__utee_from_gp11_attr(ua, params, paramCount);
2290 	res = _utee_asymm_verify(operation->state, ua, paramCount, digest,
2291 				 digestLen, signature, signatureLen);
2292 
2293 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
2294 		TEE_Panic(res);
2295 
2296 	return res;
2297 }
2298 
2299 /* Cryptographic Operations API - Key Derivation Functions */
2300 
2301 void TEE_DeriveKey(TEE_OperationHandle operation,
2302 		   const TEE_Attribute *params, uint32_t paramCount,
2303 		   TEE_ObjectHandle derivedKey)
2304 {
2305 	struct utee_attribute ua[paramCount];
2306 	struct utee_object_info key_info = { };
2307 	TEE_Result res = TEE_SUCCESS;
2308 
2309 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
2310 		TEE_Panic(0);
2311 
2312 	__utee_check_attr_in_annotation(params, paramCount);
2313 
2314 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
2315 	    TEE_OPERATION_KEY_DERIVATION)
2316 		TEE_Panic(0);
2317 
2318 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
2319 		TEE_Panic(0);
2320 	if (!operation->key1)
2321 		TEE_Panic(0);
2322 	if (operation->info.mode != TEE_MODE_DERIVE)
2323 		TEE_Panic(0);
2324 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
2325 		TEE_Panic(0);
2326 
2327 	res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info);
2328 	if (res != TEE_SUCCESS)
2329 		TEE_Panic(res);
2330 
2331 	if (key_info.obj_type != TEE_TYPE_GENERIC_SECRET)
2332 		TEE_Panic(0);
2333 	if ((key_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
2334 		TEE_Panic(0);
2335 
2336 	__utee_from_attr(ua, params, paramCount);
2337 	res = _utee_cryp_derive_key(operation->state, ua, paramCount,
2338 				    (unsigned long)derivedKey);
2339 	if (res != TEE_SUCCESS)
2340 		TEE_Panic(res);
2341 }
2342 
2343 void __GP11_TEE_DeriveKey(TEE_OperationHandle operation,
2344 			  const __GP11_TEE_Attribute *params,
2345 			  uint32_t paramCount, TEE_ObjectHandle derivedKey)
2346 {
2347 	struct utee_attribute ua[paramCount];
2348 	struct utee_object_info key_info = { };
2349 	TEE_Result res = TEE_SUCCESS;
2350 
2351 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
2352 		TEE_Panic(0);
2353 
2354 	__utee_check_gp11_attr_in_annotation(params, paramCount);
2355 
2356 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
2357 	    TEE_OPERATION_KEY_DERIVATION)
2358 		TEE_Panic(0);
2359 
2360 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
2361 		TEE_Panic(0);
2362 	if (!operation->key1)
2363 		TEE_Panic(0);
2364 	if (operation->info.mode != TEE_MODE_DERIVE)
2365 		TEE_Panic(0);
2366 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
2367 		TEE_Panic(0);
2368 
2369 	res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info);
2370 	if (res != TEE_SUCCESS)
2371 		TEE_Panic(res);
2372 
2373 	if (key_info.obj_type != TEE_TYPE_GENERIC_SECRET)
2374 		TEE_Panic(0);
2375 	if ((key_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
2376 		TEE_Panic(0);
2377 
2378 	__utee_from_gp11_attr(ua, params, paramCount);
2379 	res = _utee_cryp_derive_key(operation->state, ua, paramCount,
2380 				    (unsigned long)derivedKey);
2381 	if (res != TEE_SUCCESS)
2382 		TEE_Panic(res);
2383 }
2384 
2385 /* Cryptographic Operations API - Random Number Generation Functions */
2386 
2387 void TEE_GenerateRandom(void *randomBuffer, size_t randomBufferLen)
2388 {
2389 	TEE_Result res;
2390 
2391 	res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
2392 	if (res != TEE_SUCCESS)
2393 		TEE_Panic(res);
2394 }
2395 
2396 void __GP11_TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
2397 {
2398 	TEE_GenerateRandom(randomBuffer, randomBufferLen);
2399 }
2400 
2401 int rand(void)
2402 {
2403 	int rc;
2404 
2405 	TEE_GenerateRandom(&rc, sizeof(rc));
2406 
2407 	/*
2408 	 * RAND_MAX is the larges int, INT_MAX which is all bits but the
2409 	 * highest bit set.
2410 	 */
2411 	return rc & RAND_MAX;
2412 }
2413 
2414 TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element)
2415 {
2416 	if (IS_ENABLED(CFG_CRYPTO_AES)) {
2417 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
2418 			if (alg == TEE_ALG_AES_ECB_NOPAD)
2419 				goto check_element_none;
2420 		}
2421 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
2422 			if (alg == TEE_ALG_AES_CBC_NOPAD)
2423 				goto check_element_none;
2424 		}
2425 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
2426 			if (alg == TEE_ALG_AES_CTR)
2427 				goto check_element_none;
2428 		}
2429 		if (IS_ENABLED(CFG_CRYPTO_CTS)) {
2430 			if (alg == TEE_ALG_AES_CTS)
2431 				goto check_element_none;
2432 		}
2433 		if (IS_ENABLED(CFG_CRYPTO_XTS)) {
2434 			if (alg == TEE_ALG_AES_XTS)
2435 				goto check_element_none;
2436 		}
2437 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
2438 			if (alg == TEE_ALG_AES_CBC_MAC_NOPAD ||
2439 			    alg == TEE_ALG_AES_CBC_MAC_PKCS5)
2440 				goto check_element_none;
2441 		}
2442 		if (IS_ENABLED(CFG_CRYPTO_CMAC)) {
2443 			if (alg == TEE_ALG_AES_CMAC)
2444 				goto check_element_none;
2445 		}
2446 		if (IS_ENABLED(CFG_CRYPTO_CCM)) {
2447 			if (alg == TEE_ALG_AES_CCM)
2448 				goto check_element_none;
2449 		}
2450 		if (IS_ENABLED(CFG_CRYPTO_GCM)) {
2451 			if (alg == TEE_ALG_AES_GCM)
2452 				goto check_element_none;
2453 		}
2454 	}
2455 	if (IS_ENABLED(CFG_CRYPTO_DES)) {
2456 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
2457 			if (alg == TEE_ALG_DES_ECB_NOPAD ||
2458 			    alg == TEE_ALG_DES3_ECB_NOPAD)
2459 				goto check_element_none;
2460 		}
2461 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
2462 			if (alg == TEE_ALG_DES_CBC_NOPAD ||
2463 			    alg == TEE_ALG_DES3_CBC_NOPAD)
2464 				goto check_element_none;
2465 		}
2466 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
2467 			if (alg == TEE_ALG_DES_CBC_MAC_NOPAD ||
2468 			    alg == TEE_ALG_DES_CBC_MAC_PKCS5 ||
2469 			    alg == TEE_ALG_DES3_CBC_MAC_NOPAD ||
2470 			    alg == TEE_ALG_DES3_CBC_MAC_PKCS5)
2471 				goto check_element_none;
2472 		}
2473 	}
2474 	if (IS_ENABLED(CFG_CRYPTO_MD5)) {
2475 		if (alg == TEE_ALG_MD5)
2476 			goto check_element_none;
2477 	}
2478 	if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
2479 		if (alg == TEE_ALG_SHA1)
2480 			goto check_element_none;
2481 	}
2482 	if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
2483 		if (alg == TEE_ALG_SHA224)
2484 			goto check_element_none;
2485 	}
2486 	if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
2487 		if (alg == TEE_ALG_SHA256)
2488 			goto check_element_none;
2489 	}
2490 	if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
2491 		if (alg == TEE_ALG_SHA384)
2492 			goto check_element_none;
2493 	}
2494 	if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
2495 		if (alg == TEE_ALG_SHA512)
2496 			goto check_element_none;
2497 	}
2498 	if (IS_ENABLED(CFG_CRYPTO_SHA3_224)) {
2499 		if (alg == TEE_ALG_SHA3_224)
2500 			goto check_element_none;
2501 	}
2502 	if (IS_ENABLED(CFG_CRYPTO_SHA3_256)) {
2503 		if (alg == TEE_ALG_SHA3_256)
2504 			goto check_element_none;
2505 	}
2506 	if (IS_ENABLED(CFG_CRYPTO_SHA3_384)) {
2507 		if (alg == TEE_ALG_SHA3_384)
2508 			goto check_element_none;
2509 	}
2510 	if (IS_ENABLED(CFG_CRYPTO_SHA3_512)) {
2511 		if (alg == TEE_ALG_SHA3_512)
2512 			goto check_element_none;
2513 	}
2514 	if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
2515 		if (alg == TEE_ALG_MD5SHA1)
2516 			goto check_element_none;
2517 	}
2518 	if (IS_ENABLED(CFG_CRYPTO_HMAC)) {
2519 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
2520 			if (alg == TEE_ALG_HMAC_MD5)
2521 				goto check_element_none;
2522 		}
2523 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
2524 			if (alg == TEE_ALG_HMAC_SHA1)
2525 				goto check_element_none;
2526 		}
2527 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
2528 			if (alg == TEE_ALG_HMAC_SHA224)
2529 				goto check_element_none;
2530 		}
2531 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
2532 			if (alg == TEE_ALG_HMAC_SHA256)
2533 				goto check_element_none;
2534 		}
2535 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
2536 			if (alg == TEE_ALG_HMAC_SHA384)
2537 				goto check_element_none;
2538 		}
2539 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
2540 			if (alg == TEE_ALG_HMAC_SHA512)
2541 				goto check_element_none;
2542 		}
2543 		if (IS_ENABLED(CFG_CRYPTO_SHA3_224)) {
2544 			if (alg == TEE_ALG_HMAC_SHA3_224)
2545 				goto check_element_none;
2546 		}
2547 		if (IS_ENABLED(CFG_CRYPTO_SHA3_256)) {
2548 			if (alg == TEE_ALG_HMAC_SHA3_256)
2549 				goto check_element_none;
2550 		}
2551 		if (IS_ENABLED(CFG_CRYPTO_SHA3_384)) {
2552 			if (alg == TEE_ALG_HMAC_SHA3_384)
2553 				goto check_element_none;
2554 		}
2555 		if (IS_ENABLED(CFG_CRYPTO_SHA3_512)) {
2556 			if (alg == TEE_ALG_HMAC_SHA3_512)
2557 				goto check_element_none;
2558 		}
2559 		if (IS_ENABLED(CFG_CRYPTO_SM3)) {
2560 			if (alg == TEE_ALG_HMAC_SM3)
2561 				goto check_element_none;
2562 		}
2563 	}
2564 	if (IS_ENABLED(CFG_CRYPTO_SM3)) {
2565 		if (alg == TEE_ALG_SM3)
2566 			goto check_element_none;
2567 	}
2568 	if (IS_ENABLED(CFG_CRYPTO_SM4)) {
2569 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
2570 			if (alg == TEE_ALG_SM4_ECB_NOPAD)
2571 				goto check_element_none;
2572 		}
2573 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
2574 			if (alg == TEE_ALG_SM4_CBC_NOPAD)
2575 				goto check_element_none;
2576 		}
2577 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
2578 			if (alg == TEE_ALG_SM4_CTR)
2579 				goto check_element_none;
2580 		}
2581 		if (IS_ENABLED(CFG_CRYPTO_XTS)) {
2582 			if (alg == TEE_ALG_SM4_XTS)
2583 				goto check_element_none;
2584 		}
2585 	}
2586 	if (IS_ENABLED(CFG_CRYPTO_RSA)) {
2587 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
2588 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5 ||
2589 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_MD5 ||
2590 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_MD5)
2591 				goto check_element_none;
2592 		}
2593 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
2594 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 ||
2595 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 ||
2596 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1)
2597 				goto check_element_none;
2598 		}
2599 		if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
2600 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1)
2601 				goto check_element_none;
2602 		}
2603 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
2604 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 ||
2605 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 ||
2606 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224)
2607 				goto check_element_none;
2608 		}
2609 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
2610 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 ||
2611 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 ||
2612 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256)
2613 				goto check_element_none;
2614 		}
2615 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
2616 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 ||
2617 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 ||
2618 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384)
2619 				goto check_element_none;
2620 		}
2621 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
2622 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 ||
2623 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 ||
2624 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512)
2625 				goto check_element_none;
2626 		}
2627 		if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) {
2628 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5)
2629 				goto check_element_none;
2630 		}
2631 		if (alg == TEE_ALG_RSA_NOPAD)
2632 			goto check_element_none;
2633 	}
2634 	if (IS_ENABLED(CFG_CRYPTO_DSA)) {
2635 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
2636 			if (alg == TEE_ALG_DSA_SHA1)
2637 				goto check_element_none;
2638 		}
2639 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
2640 			if (alg == TEE_ALG_DSA_SHA224)
2641 				goto check_element_none;
2642 		}
2643 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
2644 			if (alg == TEE_ALG_DSA_SHA256)
2645 				goto check_element_none;
2646 		}
2647 	}
2648 	if (IS_ENABLED(CFG_CRYPTO_DH)) {
2649 		if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
2650 			goto check_element_none;
2651 	}
2652 	if (IS_ENABLED(CFG_CRYPTO_ECC)) {
2653 		if ((alg == __OPTEE_ALG_ECDH_P192 ||
2654 		     alg == __OPTEE_ALG_ECDSA_P192 ||
2655 		     alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET ||
2656 		     alg == TEE_ALG_ECDSA_SHA1) &&
2657 		    element == TEE_ECC_CURVE_NIST_P192)
2658 			return TEE_SUCCESS;
2659 		if ((alg == __OPTEE_ALG_ECDH_P224 ||
2660 		     alg == __OPTEE_ALG_ECDSA_P224 ||
2661 		     alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET ||
2662 		     alg == TEE_ALG_ECDSA_SHA224) &&
2663 		    element == TEE_ECC_CURVE_NIST_P224)
2664 			return TEE_SUCCESS;
2665 		if ((alg == __OPTEE_ALG_ECDH_P256 ||
2666 		     alg == __OPTEE_ALG_ECDSA_P256 ||
2667 		     alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET ||
2668 		     alg == TEE_ALG_ECDSA_SHA256) &&
2669 		    element == TEE_ECC_CURVE_NIST_P256)
2670 			return TEE_SUCCESS;
2671 		if ((alg == __OPTEE_ALG_ECDH_P384 ||
2672 		     alg == __OPTEE_ALG_ECDSA_P384 ||
2673 		     alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET ||
2674 		     alg == TEE_ALG_ECDSA_SHA384) &&
2675 		    element == TEE_ECC_CURVE_NIST_P384)
2676 			return TEE_SUCCESS;
2677 		if ((alg == __OPTEE_ALG_ECDH_P521 ||
2678 		     alg == __OPTEE_ALG_ECDSA_P521 ||
2679 		     alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET ||
2680 		     alg == TEE_ALG_ECDSA_SHA512) &&
2681 		    element == TEE_ECC_CURVE_NIST_P521)
2682 			return TEE_SUCCESS;
2683 	}
2684 	if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) {
2685 		if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2)
2686 			return TEE_SUCCESS;
2687 	}
2688 	if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) {
2689 		if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2)
2690 			return TEE_SUCCESS;
2691 	}
2692 	if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) {
2693 		if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2)
2694 			return TEE_SUCCESS;
2695 	}
2696 	if (IS_ENABLED(CFG_CRYPTO_X25519)) {
2697 		if (alg == TEE_ALG_X25519 && element == TEE_ECC_CURVE_25519)
2698 			return TEE_SUCCESS;
2699 	}
2700 	if (IS_ENABLED(CFG_CRYPTO_ED25519)) {
2701 		if (alg == TEE_ALG_ED25519 && element == TEE_ECC_CURVE_25519)
2702 			return TEE_SUCCESS;
2703 	}
2704 
2705 	return TEE_ERROR_NOT_SUPPORTED;
2706 check_element_none:
2707 	if (element == TEE_CRYPTO_ELEMENT_NONE)
2708 		return TEE_SUCCESS;
2709 	return TEE_ERROR_NOT_SUPPORTED;
2710 }
2711