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