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