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