xref: /optee_os/lib/libutee/tee_api_operations.c (revision dea9063e64e5036a0fca8e85bd0d89c008784f02)
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 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
49 
50 	/* Check algorithm max key size */
51 	switch (algorithm) {
52 	case TEE_ALG_DSA_SHA1:
53 		if (maxKeySize < 512)
54 			return TEE_ERROR_NOT_SUPPORTED;
55 		if (maxKeySize > 1024)
56 			return TEE_ERROR_NOT_SUPPORTED;
57 		if (maxKeySize % 64 != 0)
58 			return TEE_ERROR_NOT_SUPPORTED;
59 		break;
60 
61 	case TEE_ALG_DSA_SHA224:
62 		if (maxKeySize != 2048)
63 			return TEE_ERROR_NOT_SUPPORTED;
64 		break;
65 
66 	case TEE_ALG_DSA_SHA256:
67 		if (maxKeySize != 2048 && maxKeySize != 3072)
68 			return TEE_ERROR_NOT_SUPPORTED;
69 		break;
70 
71 	case TEE_ALG_ECDSA_P192:
72 	case TEE_ALG_ECDH_P192:
73 		if (maxKeySize != 192)
74 			return TEE_ERROR_NOT_SUPPORTED;
75 		break;
76 
77 	case TEE_ALG_ECDSA_P224:
78 	case TEE_ALG_ECDH_P224:
79 		if (maxKeySize != 224)
80 			return TEE_ERROR_NOT_SUPPORTED;
81 		break;
82 
83 	case TEE_ALG_ECDSA_P256:
84 	case TEE_ALG_ECDH_P256:
85 	case TEE_ALG_SM2_PKE:
86 	case TEE_ALG_SM2_DSA_SM3:
87 		if (maxKeySize != 256)
88 			return TEE_ERROR_NOT_SUPPORTED;
89 		break;
90 
91 	case TEE_ALG_SM2_KEP:
92 		/* Two 256-bit keys */
93 		if (maxKeySize != 512)
94 			return TEE_ERROR_NOT_SUPPORTED;
95 		break;
96 
97 	case TEE_ALG_ECDSA_P384:
98 	case TEE_ALG_ECDH_P384:
99 		if (maxKeySize != 384)
100 			return TEE_ERROR_NOT_SUPPORTED;
101 		break;
102 
103 	case TEE_ALG_ECDSA_P521:
104 	case TEE_ALG_ECDH_P521:
105 		if (maxKeySize != 521)
106 			return TEE_ERROR_NOT_SUPPORTED;
107 		break;
108 
109 	default:
110 		break;
111 	}
112 
113 	/* Check algorithm mode (and maxKeySize for digests) */
114 	switch (algorithm) {
115 	case TEE_ALG_AES_CTS:
116 	case TEE_ALG_AES_XTS:
117 		buffer_two_blocks = true;
118 		fallthrough;
119 	case TEE_ALG_AES_ECB_NOPAD:
120 	case TEE_ALG_AES_CBC_NOPAD:
121 	case TEE_ALG_AES_CCM:
122 	case TEE_ALG_DES_ECB_NOPAD:
123 	case TEE_ALG_DES_CBC_NOPAD:
124 	case TEE_ALG_DES3_ECB_NOPAD:
125 	case TEE_ALG_DES3_CBC_NOPAD:
126 	case TEE_ALG_SM4_ECB_NOPAD:
127 	case TEE_ALG_SM4_CBC_NOPAD:
128 	case TEE_ALG_SM4_CTR:
129 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
130 			block_size = TEE_AES_BLOCK_SIZE;
131 		else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4)
132 			block_size = TEE_SM4_BLOCK_SIZE;
133 		else
134 			block_size = TEE_DES_BLOCK_SIZE;
135 		fallthrough;
136 	case TEE_ALG_AES_CTR:
137 	case TEE_ALG_AES_GCM:
138 		if (mode == TEE_MODE_ENCRYPT)
139 			req_key_usage = TEE_USAGE_ENCRYPT;
140 		else if (mode == TEE_MODE_DECRYPT)
141 			req_key_usage = TEE_USAGE_DECRYPT;
142 		else
143 			return TEE_ERROR_NOT_SUPPORTED;
144 		break;
145 
146 #if defined(CFG_CRYPTO_RSASSA_NA1)
147 	case TEE_ALG_RSASSA_PKCS1_V1_5:
148 #endif
149 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
150 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
151 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
152 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
153 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
154 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
155 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
156 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
157 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
158 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
159 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
160 	case TEE_ALG_DSA_SHA1:
161 	case TEE_ALG_DSA_SHA224:
162 	case TEE_ALG_DSA_SHA256:
163 	case TEE_ALG_ECDSA_P192:
164 	case TEE_ALG_ECDSA_P224:
165 	case TEE_ALG_ECDSA_P256:
166 	case TEE_ALG_ECDSA_P384:
167 	case TEE_ALG_ECDSA_P521:
168 	case TEE_ALG_SM2_DSA_SM3:
169 		if (mode == TEE_MODE_SIGN) {
170 			with_private_key = true;
171 			req_key_usage = TEE_USAGE_SIGN;
172 		} else if (mode == TEE_MODE_VERIFY) {
173 			req_key_usage = TEE_USAGE_VERIFY;
174 		} else {
175 			return TEE_ERROR_NOT_SUPPORTED;
176 		}
177 		break;
178 
179 	case TEE_ALG_RSAES_PKCS1_V1_5:
180 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
181 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
182 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
183 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
184 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
185 	case TEE_ALG_SM2_PKE:
186 		if (mode == TEE_MODE_ENCRYPT) {
187 			req_key_usage = TEE_USAGE_ENCRYPT;
188 		} else if (mode == TEE_MODE_DECRYPT) {
189 			with_private_key = true;
190 			req_key_usage = TEE_USAGE_DECRYPT;
191 		} else {
192 			return TEE_ERROR_NOT_SUPPORTED;
193 		}
194 		break;
195 
196 	case TEE_ALG_RSA_NOPAD:
197 		if (mode == TEE_MODE_ENCRYPT) {
198 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
199 		} else if (mode == TEE_MODE_DECRYPT) {
200 			with_private_key = true;
201 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
202 		} else {
203 			return TEE_ERROR_NOT_SUPPORTED;
204 		}
205 		break;
206 
207 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
208 	case TEE_ALG_ECDH_P192:
209 	case TEE_ALG_ECDH_P224:
210 	case TEE_ALG_ECDH_P256:
211 	case TEE_ALG_ECDH_P384:
212 	case TEE_ALG_ECDH_P521:
213 	case TEE_ALG_HKDF_MD5_DERIVE_KEY:
214 	case TEE_ALG_HKDF_SHA1_DERIVE_KEY:
215 	case TEE_ALG_HKDF_SHA224_DERIVE_KEY:
216 	case TEE_ALG_HKDF_SHA256_DERIVE_KEY:
217 	case TEE_ALG_HKDF_SHA384_DERIVE_KEY:
218 	case TEE_ALG_HKDF_SHA512_DERIVE_KEY:
219 	case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY:
220 	case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY:
221 	case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY:
222 	case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY:
223 	case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY:
224 	case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY:
225 	case TEE_ALG_SM2_KEP:
226 		if (mode != TEE_MODE_DERIVE)
227 			return TEE_ERROR_NOT_SUPPORTED;
228 		with_private_key = true;
229 		req_key_usage = TEE_USAGE_DERIVE;
230 		break;
231 
232 	case TEE_ALG_MD5:
233 	case TEE_ALG_SHA1:
234 	case TEE_ALG_SHA224:
235 	case TEE_ALG_SHA256:
236 	case TEE_ALG_SHA384:
237 	case TEE_ALG_SHA512:
238 	case TEE_ALG_SM3:
239 		if (mode != TEE_MODE_DIGEST)
240 			return TEE_ERROR_NOT_SUPPORTED;
241 		if (maxKeySize)
242 			return TEE_ERROR_NOT_SUPPORTED;
243 		/* v1.1: flags always set for digest operations */
244 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
245 		req_key_usage = 0;
246 		break;
247 
248 	case TEE_ALG_DES_CBC_MAC_NOPAD:
249 	case TEE_ALG_AES_CBC_MAC_NOPAD:
250 	case TEE_ALG_AES_CBC_MAC_PKCS5:
251 	case TEE_ALG_AES_CMAC:
252 	case TEE_ALG_DES_CBC_MAC_PKCS5:
253 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
254 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
255 	case TEE_ALG_DES3_CMAC:
256 	case TEE_ALG_HMAC_MD5:
257 	case TEE_ALG_HMAC_SHA1:
258 	case TEE_ALG_HMAC_SHA224:
259 	case TEE_ALG_HMAC_SHA256:
260 	case TEE_ALG_HMAC_SHA384:
261 	case TEE_ALG_HMAC_SHA512:
262 	case TEE_ALG_HMAC_SM3:
263 		if (mode != TEE_MODE_MAC)
264 			return TEE_ERROR_NOT_SUPPORTED;
265 		req_key_usage = TEE_USAGE_MAC;
266 		break;
267 
268 	default:
269 		return TEE_ERROR_NOT_SUPPORTED;
270 	}
271 
272 	op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO);
273 	if (!op)
274 		return TEE_ERROR_OUT_OF_MEMORY;
275 
276 	op->info.algorithm = algorithm;
277 	op->info.operationClass = TEE_ALG_GET_CLASS(algorithm);
278 #ifdef CFG_CRYPTO_RSASSA_NA1
279 	if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5)
280 		op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE;
281 #endif
282 	op->info.mode = mode;
283 	op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm);
284 	op->info.maxKeySize = maxKeySize;
285 	op->info.requiredKeyUsage = req_key_usage;
286 	op->info.handleState = handle_state;
287 
288 	if (block_size > 1) {
289 		size_t buffer_size = block_size;
290 
291 		if (buffer_two_blocks)
292 			buffer_size *= 2;
293 
294 		op->buffer = TEE_Malloc(buffer_size,
295 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
296 		if (op->buffer == NULL) {
297 			res = TEE_ERROR_OUT_OF_MEMORY;
298 			goto out;
299 		}
300 	}
301 	op->block_size = block_size;
302 	op->buffer_two_blocks = buffer_two_blocks;
303 
304 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
305 		uint32_t mks = maxKeySize;
306 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
307 						       with_private_key);
308 
309 		/*
310 		 * If two keys are expected the max key size is the sum of
311 		 * the size of both keys.
312 		 */
313 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
314 			mks /= 2;
315 
316 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
317 		if (res != TEE_SUCCESS)
318 			goto out;
319 
320 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
321 			res = TEE_AllocateTransientObject(key_type, mks,
322 							  &op->key2);
323 			if (res != TEE_SUCCESS)
324 				goto out;
325 		}
326 	}
327 
328 	res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1,
329 				     (unsigned long)op->key2, &op->state);
330 	if (res != TEE_SUCCESS)
331 		goto out;
332 
333 	/*
334 	 * Initialize digest operations
335 	 * Other multi-stage operations initialized w/ TEE_xxxInit functions
336 	 * Non-applicable on asymmetric operations
337 	 */
338 	if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) {
339 		res = _utee_hash_init(op->state, NULL, 0);
340 		if (res != TEE_SUCCESS)
341 			goto out;
342 		/* v1.1: flags always set for digest operations */
343 		op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
344 	}
345 
346 	op->operationState = TEE_OPERATION_STATE_INITIAL;
347 
348 	*operation = op;
349 
350 out:
351 	if (res != TEE_SUCCESS) {
352 		if (res != TEE_ERROR_OUT_OF_MEMORY &&
353 		    res != TEE_ERROR_NOT_SUPPORTED)
354 			TEE_Panic(res);
355 		if (op) {
356 			if (op->state) {
357 				TEE_FreeOperation(op);
358 			} else {
359 				TEE_Free(op->buffer);
360 				TEE_FreeTransientObject(op->key1);
361 				TEE_FreeTransientObject(op->key2);
362 				TEE_Free(op);
363 			}
364 		}
365 	}
366 
367 	return res;
368 }
369 
370 void TEE_FreeOperation(TEE_OperationHandle operation)
371 {
372 	TEE_Result res;
373 
374 	if (operation == TEE_HANDLE_NULL)
375 		TEE_Panic(0);
376 
377 	/*
378 	 * Note that keys should not be freed here, since they are
379 	 * claimed by the operation they will be freed by
380 	 * utee_cryp_state_free().
381 	 */
382 	res = _utee_cryp_state_free(operation->state);
383 	if (res != TEE_SUCCESS)
384 		TEE_Panic(res);
385 
386 	TEE_Free(operation->buffer);
387 	TEE_Free(operation);
388 }
389 
390 void TEE_GetOperationInfo(TEE_OperationHandle operation,
391 			  TEE_OperationInfo *operationInfo)
392 {
393 	if (operation == TEE_HANDLE_NULL)
394 		TEE_Panic(0);
395 
396 	__utee_check_out_annotation(operationInfo, sizeof(*operationInfo));
397 
398 	*operationInfo = operation->info;
399 	if (operationInfo->handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
400 		operationInfo->keySize = 0;
401 		operationInfo->requiredKeyUsage = 0;
402 	}
403 }
404 
405 TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle op,
406 					TEE_OperationInfoMultiple *op_info,
407 					uint32_t *size)
408 {
409 	TEE_Result res = TEE_SUCCESS;
410 	TEE_ObjectInfo kinfo = { };
411 	size_t max_key_count = 0;
412 	bool two_keys = false;
413 
414 	if (op == TEE_HANDLE_NULL) {
415 		res = TEE_ERROR_BAD_PARAMETERS;
416 		goto out;
417 	}
418 
419 	__utee_check_outbuf_annotation(op_info, size);
420 
421 	if (*size < sizeof(*op_info)) {
422 		res = TEE_ERROR_BAD_PARAMETERS;
423 		goto out;
424 	}
425 	max_key_count = (*size - sizeof(*op_info)) /
426 			sizeof(TEE_OperationInfoKey);
427 
428 	TEE_MemFill(op_info, 0, *size);
429 
430 	/* Two keys flag (TEE_ALG_AES_XTS only) */
431 	two_keys = op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
432 
433 	if (op->info.mode == TEE_MODE_DIGEST) {
434 		op_info->numberOfKeys = 0;
435 	} else if (!two_keys) {
436 		if (max_key_count < 1) {
437 			res = TEE_ERROR_SHORT_BUFFER;
438 			goto out;
439 		}
440 
441 		res = TEE_GetObjectInfo1(op->key1, &kinfo);
442 		/* Key1 is not a valid handle, "can't happen". */
443 		if (res)
444 			goto out;
445 
446 		op_info->keyInformation[0].keySize = kinfo.keySize;
447 		op_info->keyInformation[0].requiredKeyUsage =
448 			op->info.requiredKeyUsage;
449 		op_info->numberOfKeys = 1;
450 	} else {
451 		if (max_key_count < 2) {
452 			res = TEE_ERROR_SHORT_BUFFER;
453 			goto out;
454 		}
455 
456 		res = TEE_GetObjectInfo1(op->key1, &kinfo);
457 		/* Key1 is not a valid handle, "can't happen". */
458 		if (res)
459 			goto out;
460 
461 		op_info->keyInformation[0].keySize = kinfo.keySize;
462 		op_info->keyInformation[0].requiredKeyUsage =
463 			op->info.requiredKeyUsage;
464 
465 		res = TEE_GetObjectInfo1(op->key2, &kinfo);
466 		/* Key2 is not a valid handle, "can't happen". */
467 		if (res)
468 			goto out;
469 
470 		op_info->keyInformation[1].keySize = kinfo.keySize;
471 		op_info->keyInformation[1].requiredKeyUsage =
472 			op->info.requiredKeyUsage;
473 
474 		op_info->numberOfKeys = 2;
475 	}
476 
477 	op_info->algorithm = op->info.algorithm;
478 	op_info->operationClass = op->info.operationClass;
479 	op_info->mode = op->info.mode;
480 	op_info->digestLength = op->info.digestLength;
481 	op_info->maxKeySize = op->info.maxKeySize;
482 	op_info->handleState = op->info.handleState;
483 	op_info->operationState = op->operationState;
484 
485 out:
486 	if (res != TEE_SUCCESS &&
487 	    res != TEE_ERROR_SHORT_BUFFER)
488 		TEE_Panic(res);
489 
490 	return res;
491 }
492 
493 void TEE_ResetOperation(TEE_OperationHandle operation)
494 {
495 	TEE_Result res;
496 
497 	if (operation == TEE_HANDLE_NULL)
498 		TEE_Panic(0);
499 
500 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET))
501 			TEE_Panic(0);
502 
503 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
504 
505 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
506 		res = _utee_hash_init(operation->state, NULL, 0);
507 		if (res != TEE_SUCCESS)
508 			TEE_Panic(res);
509 		operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
510 	} else {
511 		operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
512 	}
513 }
514 
515 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
516 			       TEE_ObjectHandle key)
517 {
518 	TEE_Result res;
519 	uint32_t key_size = 0;
520 	TEE_ObjectInfo key_info;
521 
522 	if (operation == TEE_HANDLE_NULL) {
523 		res = TEE_ERROR_BAD_PARAMETERS;
524 		goto out;
525 	}
526 
527 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
528 		res = TEE_ERROR_BAD_PARAMETERS;
529 		goto out;
530 	}
531 
532 	if (key == TEE_HANDLE_NULL) {
533 		/* Operation key cleared */
534 		TEE_ResetTransientObject(operation->key1);
535 		operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
536 		return TEE_SUCCESS;
537 	}
538 
539 	/* No key for digest operation */
540 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
541 		res = TEE_ERROR_BAD_PARAMETERS;
542 		goto out;
543 	}
544 
545 	/* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */
546 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
547 	    0) {
548 		res = TEE_ERROR_BAD_PARAMETERS;
549 		goto out;
550 	}
551 
552 	res = TEE_GetObjectInfo1(key, &key_info);
553 	/* Key is not a valid handle */
554 	if (res != TEE_SUCCESS)
555 		goto out;
556 
557 	/* Supplied key has to meet required usage */
558 	if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
559 	    operation->info.requiredKeyUsage) {
560 		res = TEE_ERROR_BAD_PARAMETERS;
561 		goto out;
562 	}
563 
564 	if (operation->info.maxKeySize < key_info.keySize) {
565 		res = TEE_ERROR_BAD_PARAMETERS;
566 		goto out;
567 	}
568 
569 	key_size = key_info.keySize;
570 
571 	TEE_ResetTransientObject(operation->key1);
572 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
573 
574 	res = TEE_CopyObjectAttributes1(operation->key1, key);
575 	if (res != TEE_SUCCESS)
576 		goto out;
577 
578 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
579 
580 	operation->info.keySize = key_size;
581 
582 out:
583 	if (res != TEE_SUCCESS  &&
584 	    res != TEE_ERROR_CORRUPT_OBJECT &&
585 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
586 		TEE_Panic(res);
587 
588 	return res;
589 }
590 
591 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
592 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
593 {
594 	TEE_Result res;
595 	uint32_t key_size = 0;
596 	TEE_ObjectInfo key_info1;
597 	TEE_ObjectInfo key_info2;
598 
599 	if (operation == TEE_HANDLE_NULL) {
600 		res = TEE_ERROR_BAD_PARAMETERS;
601 		goto out;
602 	}
603 
604 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
605 		res = TEE_ERROR_BAD_PARAMETERS;
606 		goto out;
607 	}
608 
609 	/*
610 	 * Key1/Key2 and/or are not initialized and
611 	 * Either both keys are NULL or both are not NULL
612 	 */
613 	if (!key1 && !key2) {
614 		/* Clear the keys */
615 		TEE_ResetTransientObject(operation->key1);
616 		TEE_ResetTransientObject(operation->key2);
617 		operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
618 		return TEE_SUCCESS;
619 	} else if (!key1 || !key2) {
620 		/* Both keys are obviously not valid. */
621 		res = TEE_ERROR_BAD_PARAMETERS;
622 		goto out;
623 	}
624 
625 	/* No key for digest operation */
626 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
627 		res = TEE_ERROR_BAD_PARAMETERS;
628 		goto out;
629 	}
630 
631 	/* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */
632 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
633 	    0) {
634 		res = TEE_ERROR_BAD_PARAMETERS;
635 		goto out;
636 	}
637 
638 	res = TEE_GetObjectInfo1(key1, &key_info1);
639 	/* Key1 is not a valid handle */
640 	if (res != TEE_SUCCESS)
641 		goto out;
642 
643 	/* Supplied key has to meet required usage */
644 	if ((key_info1.objectUsage & operation->info.
645 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
646 		res = TEE_ERROR_BAD_PARAMETERS;
647 		goto out;
648 	}
649 
650 	res = TEE_GetObjectInfo1(key2, &key_info2);
651 	/* Key2 is not a valid handle */
652 	if (res != TEE_SUCCESS) {
653 		if (res == TEE_ERROR_CORRUPT_OBJECT)
654 			res = TEE_ERROR_CORRUPT_OBJECT_2;
655 		goto out;
656 	}
657 
658 	/* Supplied key has to meet required usage */
659 	if ((key_info2.objectUsage & operation->info.
660 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
661 		res = TEE_ERROR_BAD_PARAMETERS;
662 		goto out;
663 	}
664 
665 	/*
666 	 * All the multi key algorithm currently supported requires the keys to
667 	 * be of equal size.
668 	 */
669 	if (key_info1.keySize != key_info2.keySize) {
670 		res = TEE_ERROR_BAD_PARAMETERS;
671 		goto out;
672 
673 	}
674 
675 	if (operation->info.maxKeySize < key_info1.keySize) {
676 		res = TEE_ERROR_BAD_PARAMETERS;
677 		goto out;
678 	}
679 
680 	/*
681 	 * Odd that only the size of one key should be reported while
682 	 * size of two key are used when allocating the operation.
683 	 */
684 	key_size = key_info1.keySize;
685 
686 	TEE_ResetTransientObject(operation->key1);
687 	TEE_ResetTransientObject(operation->key2);
688 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
689 
690 	res = TEE_CopyObjectAttributes1(operation->key1, key1);
691 	if (res != TEE_SUCCESS)
692 		goto out;
693 	res = TEE_CopyObjectAttributes1(operation->key2, key2);
694 	if (res != TEE_SUCCESS) {
695 		if (res == TEE_ERROR_CORRUPT_OBJECT)
696 			res = TEE_ERROR_CORRUPT_OBJECT_2;
697 		goto out;
698 	}
699 
700 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
701 
702 	operation->info.keySize = key_size;
703 
704 out:
705 	if (res != TEE_SUCCESS  &&
706 	    res != TEE_ERROR_CORRUPT_OBJECT &&
707 	    res != TEE_ERROR_CORRUPT_OBJECT_2 &&
708 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE &&
709 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
710 		TEE_Panic(res);
711 
712 	return res;
713 }
714 
715 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
716 {
717 	TEE_Result res;
718 
719 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
720 		TEE_Panic(0);
721 	if (dst_op->info.algorithm != src_op->info.algorithm)
722 		TEE_Panic(0);
723 	if (dst_op->info.mode != src_op->info.mode)
724 		TEE_Panic(0);
725 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
726 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
727 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
728 
729 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
730 			key1 = src_op->key1;
731 			key2 = src_op->key2;
732 		}
733 
734 		if ((src_op->info.handleState &
735 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
736 			TEE_SetOperationKey(dst_op, key1);
737 		} else {
738 			TEE_SetOperationKey2(dst_op, key1, key2);
739 		}
740 	}
741 	dst_op->info.handleState = src_op->info.handleState;
742 	dst_op->info.keySize = src_op->info.keySize;
743 	dst_op->info.digestLength = src_op->info.digestLength;
744 	dst_op->operationState = src_op->operationState;
745 
746 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
747 	    dst_op->block_size != src_op->block_size)
748 		TEE_Panic(0);
749 
750 	if (dst_op->buffer != NULL) {
751 		if (src_op->buffer == NULL)
752 			TEE_Panic(0);
753 
754 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
755 		dst_op->buffer_offs = src_op->buffer_offs;
756 	} else if (src_op->buffer != NULL) {
757 		TEE_Panic(0);
758 	}
759 
760 	res = _utee_cryp_state_copy(dst_op->state, src_op->state);
761 	if (res != TEE_SUCCESS)
762 		TEE_Panic(res);
763 }
764 
765 /* Cryptographic Operations API - Message Digest Functions */
766 
767 static void init_hash_operation(TEE_OperationHandle operation, const void *IV,
768 				uint32_t IVLen)
769 {
770 	TEE_Result res;
771 
772 	/*
773 	 * Note : IV and IVLen are never used in current implementation
774 	 * This is why coherent values of IV and IVLen are not checked
775 	 */
776 	res = _utee_hash_init(operation->state, IV, IVLen);
777 	if (res != TEE_SUCCESS)
778 		TEE_Panic(res);
779 	operation->buffer_offs = 0;
780 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
781 }
782 
783 void TEE_DigestUpdate(TEE_OperationHandle operation,
784 		      const void *chunk, uint32_t chunkSize)
785 {
786 	TEE_Result res = TEE_ERROR_GENERIC;
787 
788 	if (operation == TEE_HANDLE_NULL ||
789 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
790 		TEE_Panic(0);
791 
792 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
793 
794 	res = _utee_hash_update(operation->state, chunk, chunkSize);
795 	if (res != TEE_SUCCESS)
796 		TEE_Panic(res);
797 }
798 
799 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
800 			     uint32_t chunkLen, void *hash, uint32_t *hashLen)
801 {
802 	TEE_Result res;
803 	uint64_t hl;
804 
805 	if ((operation == TEE_HANDLE_NULL) ||
806 	    (!chunk && chunkLen) ||
807 	    (operation->info.operationClass != TEE_OPERATION_DIGEST)) {
808 		res = TEE_ERROR_BAD_PARAMETERS;
809 		goto out;
810 	}
811 	__utee_check_inout_annotation(hashLen, sizeof(*hashLen));
812 
813 	hl = *hashLen;
814 	res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl);
815 	*hashLen = hl;
816 	if (res != TEE_SUCCESS)
817 		goto out;
818 
819 	/* Reset operation state */
820 	init_hash_operation(operation, NULL, 0);
821 
822 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
823 
824 out:
825 	if (res != TEE_SUCCESS &&
826 	    res != TEE_ERROR_SHORT_BUFFER)
827 		TEE_Panic(res);
828 
829 	return res;
830 }
831 
832 /* Cryptographic Operations API - Symmetric Cipher Functions */
833 
834 void TEE_CipherInit(TEE_OperationHandle operation, const void *IV,
835 		    uint32_t IVLen)
836 {
837 	TEE_Result res;
838 
839 	if (operation == TEE_HANDLE_NULL)
840 		TEE_Panic(0);
841 
842 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
843 		TEE_Panic(0);
844 
845 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
846 	    !(operation->key1))
847 		TEE_Panic(0);
848 
849 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
850 		TEE_ResetOperation(operation);
851 
852 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
853 
854 	res = _utee_cipher_init(operation->state, IV, IVLen);
855 	if (res != TEE_SUCCESS)
856 		TEE_Panic(res);
857 
858 	operation->buffer_offs = 0;
859 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
860 }
861 
862 static TEE_Result tee_buffer_update(
863 		TEE_OperationHandle op,
864 		TEE_Result(*update_func)(unsigned long state, const void *src,
865 				size_t slen, void *dst, uint64_t *dlen),
866 		const void *src_data, size_t src_len,
867 		void *dest_data, uint64_t *dest_len)
868 {
869 	TEE_Result res;
870 	const uint8_t *src = src_data;
871 	size_t slen = src_len;
872 	uint8_t *dst = dest_data;
873 	size_t dlen = *dest_len;
874 	size_t acc_dlen = 0;
875 	uint64_t tmp_dlen;
876 	size_t l;
877 	size_t buffer_size;
878 	size_t buffer_left;
879 
880 	if (!src) {
881 		if (slen)
882 			TEE_Panic(0);
883 		goto out;
884 	}
885 
886 	if (op->buffer_two_blocks) {
887 		buffer_size = op->block_size * 2;
888 		buffer_left = 1;
889 	} else {
890 		buffer_size = op->block_size;
891 		buffer_left = 0;
892 	}
893 
894 	if (op->buffer_offs > 0) {
895 		/* Fill up complete block */
896 		if (op->buffer_offs < op->block_size)
897 			l = MIN(slen, op->block_size - op->buffer_offs);
898 		else
899 			l = MIN(slen, buffer_size - op->buffer_offs);
900 		memcpy(op->buffer + op->buffer_offs, src, l);
901 		op->buffer_offs += l;
902 		src += l;
903 		slen -= l;
904 		if ((op->buffer_offs % op->block_size) != 0)
905 			goto out;	/* Nothing left to do */
906 	}
907 
908 	/* If we can feed from buffer */
909 	if ((op->buffer_offs > 0) &&
910 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
911 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
912 				op->block_size);
913 		l = MIN(op->buffer_offs, l);
914 		tmp_dlen = dlen;
915 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
916 		if (res != TEE_SUCCESS)
917 			TEE_Panic(res);
918 		dst += tmp_dlen;
919 		dlen -= tmp_dlen;
920 		acc_dlen += tmp_dlen;
921 		op->buffer_offs -= l;
922 		if (op->buffer_offs > 0) {
923 			/*
924 			 * Slen is small enough to be contained in rest buffer.
925 			 */
926 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
927 			memcpy(op->buffer + op->buffer_offs, src, slen);
928 			op->buffer_offs += slen;
929 			goto out;	/* Nothing left to do */
930 		}
931 	}
932 
933 	if (slen >= (buffer_size + buffer_left)) {
934 		/* Buffer is empty, feed as much as possible from src */
935 		if (op->info.algorithm == TEE_ALG_AES_CTS)
936 			l = ROUNDUP(slen - buffer_size, op->block_size);
937 		else
938 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
939 
940 		tmp_dlen = dlen;
941 		res = update_func(op->state, src, l, dst, &tmp_dlen);
942 		if (res != TEE_SUCCESS)
943 			TEE_Panic(res);
944 		src += l;
945 		slen -= l;
946 		dst += tmp_dlen;
947 		dlen -= tmp_dlen;
948 		acc_dlen += tmp_dlen;
949 	}
950 
951 	/* Slen is small enough to be contained in buffer. */
952 	memcpy(op->buffer + op->buffer_offs, src, slen);
953 	op->buffer_offs += slen;
954 
955 out:
956 	*dest_len = acc_dlen;
957 	return TEE_SUCCESS;
958 }
959 
960 TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData,
961 			    uint32_t srcLen, void *destData, uint32_t *destLen)
962 {
963 	TEE_Result res;
964 	size_t req_dlen;
965 	uint64_t dl;
966 
967 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
968 		res = TEE_ERROR_BAD_PARAMETERS;
969 		goto out;
970 	}
971 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
972 
973 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
974 		res = TEE_ERROR_BAD_PARAMETERS;
975 		goto out;
976 	}
977 
978 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
979 		res = TEE_ERROR_BAD_PARAMETERS;
980 		goto out;
981 	}
982 
983 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
984 		res = TEE_ERROR_BAD_PARAMETERS;
985 		goto out;
986 	}
987 
988 	if (!srcData && !srcLen) {
989 		*destLen = 0;
990 		res = TEE_SUCCESS;
991 		goto out;
992 	}
993 
994 	/* Calculate required dlen */
995 	if (operation->block_size > 1) {
996 		req_dlen = ((operation->buffer_offs + srcLen) /
997 			    operation->block_size) * operation->block_size;
998 	} else {
999 		req_dlen = srcLen;
1000 	}
1001 	if (operation->buffer_two_blocks) {
1002 		if (req_dlen > operation->block_size * 2)
1003 			req_dlen -= operation->block_size * 2;
1004 		else
1005 			req_dlen = 0;
1006 	}
1007 	/*
1008 	 * Check that required destLen is big enough before starting to feed
1009 	 * data to the algorithm. Errors during feeding of data are fatal as we
1010 	 * can't restore sync with this API.
1011 	 */
1012 	if (*destLen < req_dlen) {
1013 		*destLen = req_dlen;
1014 		res = TEE_ERROR_SHORT_BUFFER;
1015 		goto out;
1016 	}
1017 
1018 	dl = *destLen;
1019 	if (operation->block_size > 1) {
1020 		res = tee_buffer_update(operation, _utee_cipher_update, srcData,
1021 					srcLen, destData, &dl);
1022 	} else {
1023 		if (srcLen > 0) {
1024 			res = _utee_cipher_update(operation->state, srcData,
1025 						  srcLen, destData, &dl);
1026 		} else {
1027 			res = TEE_SUCCESS;
1028 			dl = 0;
1029 		}
1030 	}
1031 	*destLen = dl;
1032 
1033 out:
1034 	if (res != TEE_SUCCESS &&
1035 	    res != TEE_ERROR_SHORT_BUFFER)
1036 		TEE_Panic(res);
1037 
1038 	return res;
1039 }
1040 
1041 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation,
1042 			     const void *srcData, uint32_t srcLen,
1043 			     void *destData, uint32_t *destLen)
1044 {
1045 	TEE_Result res = TEE_SUCCESS;
1046 	uint8_t *dst = destData;
1047 	size_t acc_dlen = 0;
1048 	uint64_t tmp_dlen = 0;
1049 	size_t req_dlen = 0;
1050 
1051 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1052 		res = TEE_ERROR_BAD_PARAMETERS;
1053 		goto out;
1054 	}
1055 	if (destLen)
1056 		__utee_check_inout_annotation(destLen, sizeof(*destLen));
1057 
1058 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1059 		res = TEE_ERROR_BAD_PARAMETERS;
1060 		goto out;
1061 	}
1062 
1063 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1064 		res = TEE_ERROR_BAD_PARAMETERS;
1065 		goto out;
1066 	}
1067 
1068 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1069 		res = TEE_ERROR_BAD_PARAMETERS;
1070 		goto out;
1071 	}
1072 
1073 	/*
1074 	 * Check that the final block doesn't require padding for those
1075 	 * algorithms that requires client to supply padding.
1076 	 */
1077 	if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
1078 	    operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1079 	    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
1080 	    operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1081 	    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
1082 	    operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD ||
1083 	    operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD ||
1084 	    operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) {
1085 		if (((operation->buffer_offs + srcLen) % operation->block_size)
1086 		    != 0) {
1087 			res = TEE_ERROR_BAD_PARAMETERS;
1088 			goto out;
1089 		}
1090 	}
1091 
1092 	/*
1093 	 * Check that required destLen is big enough before starting to feed
1094 	 * data to the algorithm. Errors during feeding of data are fatal as we
1095 	 * can't restore sync with this API.
1096 	 */
1097 	if (operation->block_size > 1) {
1098 		req_dlen = operation->buffer_offs + srcLen;
1099 	} else {
1100 		req_dlen = srcLen;
1101 	}
1102 	if (destLen)
1103 		tmp_dlen = *destLen;
1104 	if (tmp_dlen < req_dlen) {
1105 		if (destLen)
1106 			*destLen = req_dlen;
1107 		res = TEE_ERROR_SHORT_BUFFER;
1108 		goto out;
1109 	}
1110 
1111 	if (operation->block_size > 1) {
1112 		if (srcLen) {
1113 			res = tee_buffer_update(operation, _utee_cipher_update,
1114 						srcData, srcLen, dst,
1115 						&tmp_dlen);
1116 			if (res != TEE_SUCCESS)
1117 				goto out;
1118 
1119 			dst += tmp_dlen;
1120 			acc_dlen += tmp_dlen;
1121 
1122 			tmp_dlen = *destLen - acc_dlen;
1123 		}
1124 		res = _utee_cipher_final(operation->state, operation->buffer,
1125 					 operation->buffer_offs, dst,
1126 					 &tmp_dlen);
1127 	} else {
1128 		res = _utee_cipher_final(operation->state, srcData, srcLen, dst,
1129 					 &tmp_dlen);
1130 	}
1131 	if (res != TEE_SUCCESS)
1132 		goto out;
1133 
1134 	acc_dlen += tmp_dlen;
1135 	if (destLen)
1136 		*destLen = acc_dlen;
1137 
1138 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1139 
1140 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1141 
1142 out:
1143 	if (res != TEE_SUCCESS &&
1144 	    res != TEE_ERROR_SHORT_BUFFER)
1145 		TEE_Panic(res);
1146 
1147 	return res;
1148 }
1149 
1150 /* Cryptographic Operations API - MAC Functions */
1151 
1152 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
1153 {
1154 	if (operation == TEE_HANDLE_NULL)
1155 		TEE_Panic(0);
1156 
1157 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1158 		TEE_Panic(0);
1159 
1160 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1161 	    !(operation->key1))
1162 		TEE_Panic(0);
1163 
1164 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1165 		TEE_ResetOperation(operation);
1166 
1167 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1168 
1169 	init_hash_operation(operation, IV, IVLen);
1170 }
1171 
1172 void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk,
1173 		   uint32_t chunkSize)
1174 {
1175 	TEE_Result res;
1176 
1177 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
1178 		TEE_Panic(0);
1179 
1180 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1181 		TEE_Panic(0);
1182 
1183 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1184 		TEE_Panic(0);
1185 
1186 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE)
1187 		TEE_Panic(0);
1188 
1189 	res = _utee_hash_update(operation->state, chunk, chunkSize);
1190 	if (res != TEE_SUCCESS)
1191 		TEE_Panic(res);
1192 }
1193 
1194 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
1195 			       const void *message, uint32_t messageLen,
1196 			       void *mac, uint32_t *macLen)
1197 {
1198 	TEE_Result res;
1199 	uint64_t ml;
1200 
1201 	if (operation == TEE_HANDLE_NULL || (!message && messageLen)) {
1202 		res = TEE_ERROR_BAD_PARAMETERS;
1203 		goto out;
1204 	}
1205 	__utee_check_inout_annotation(macLen, sizeof(*macLen));
1206 
1207 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1208 		res = TEE_ERROR_BAD_PARAMETERS;
1209 		goto out;
1210 	}
1211 
1212 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1213 		res = TEE_ERROR_BAD_PARAMETERS;
1214 		goto out;
1215 	}
1216 
1217 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1218 		res = TEE_ERROR_BAD_PARAMETERS;
1219 		goto out;
1220 	}
1221 
1222 	ml = *macLen;
1223 	res = _utee_hash_final(operation->state, message, messageLen, mac, &ml);
1224 	*macLen = ml;
1225 	if (res != TEE_SUCCESS)
1226 		goto out;
1227 
1228 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1229 
1230 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1231 
1232 out:
1233 	if (res != TEE_SUCCESS &&
1234 	    res != TEE_ERROR_SHORT_BUFFER)
1235 		TEE_Panic(res);
1236 
1237 	return res;
1238 }
1239 
1240 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
1241 			       const void *message, uint32_t messageLen,
1242 			       const void *mac, uint32_t macLen)
1243 {
1244 	TEE_Result res;
1245 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
1246 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1247 
1248 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1249 		res = TEE_ERROR_BAD_PARAMETERS;
1250 		goto out;
1251 	}
1252 
1253 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1254 		res = TEE_ERROR_BAD_PARAMETERS;
1255 		goto out;
1256 	}
1257 
1258 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1259 		res = TEE_ERROR_BAD_PARAMETERS;
1260 		goto out;
1261 	}
1262 
1263 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1264 				  &computed_mac_size);
1265 	if (res != TEE_SUCCESS)
1266 		goto out;
1267 
1268 	if (computed_mac_size != macLen) {
1269 		res = TEE_ERROR_MAC_INVALID;
1270 		goto out;
1271 	}
1272 
1273 	if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) {
1274 		res = TEE_ERROR_MAC_INVALID;
1275 		goto out;
1276 	}
1277 
1278 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1279 
1280 out:
1281 	if (res != TEE_SUCCESS &&
1282 	    res != TEE_ERROR_MAC_INVALID)
1283 		TEE_Panic(res);
1284 
1285 	return res;
1286 }
1287 
1288 /* Cryptographic Operations API - Authenticated Encryption Functions */
1289 
1290 TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce,
1291 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1292 		      uint32_t payloadLen)
1293 {
1294 	TEE_Result res;
1295 
1296 	if (operation == TEE_HANDLE_NULL || nonce == NULL) {
1297 		res = TEE_ERROR_BAD_PARAMETERS;
1298 		goto out;
1299 	}
1300 
1301 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1302 		res = TEE_ERROR_BAD_PARAMETERS;
1303 		goto out;
1304 	}
1305 
1306 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
1307 		res = TEE_ERROR_BAD_PARAMETERS;
1308 		goto out;
1309 	}
1310 
1311 	/*
1312 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1313 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1314 	 * according to the same principle so we have to check here instead to
1315 	 * be GP compliant.
1316 	 */
1317 	if (operation->info.algorithm == TEE_ALG_AES_GCM) {
1318 		/*
1319 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1320 		 */
1321 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) {
1322 			res = TEE_ERROR_NOT_SUPPORTED;
1323 			goto out;
1324 		}
1325 	}
1326 
1327 	res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8,
1328 				 AADLen, payloadLen);
1329 	if (res != TEE_SUCCESS)
1330 		goto out;
1331 
1332 	operation->info.digestLength = tagLen / 8;
1333 	operation->buffer_offs = 0;
1334 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1335 
1336 out:
1337 	if (res != TEE_SUCCESS &&
1338 	    res != TEE_ERROR_NOT_SUPPORTED)
1339 			TEE_Panic(res);
1340 
1341 	return res;
1342 }
1343 
1344 void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata,
1345 		     uint32_t AADdataLen)
1346 {
1347 	TEE_Result res;
1348 
1349 	if (operation == TEE_HANDLE_NULL ||
1350 	    (AADdata == NULL && AADdataLen != 0))
1351 		TEE_Panic(0);
1352 
1353 	if (operation->info.operationClass != TEE_OPERATION_AE)
1354 		TEE_Panic(0);
1355 
1356 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1357 		TEE_Panic(0);
1358 
1359 	res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen);
1360 
1361 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1362 
1363 	if (res != TEE_SUCCESS)
1364 		TEE_Panic(res);
1365 }
1366 
1367 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData,
1368 			uint32_t srcLen, void *destData, uint32_t *destLen)
1369 {
1370 	TEE_Result res = TEE_SUCCESS;
1371 	size_t req_dlen = 0;
1372 	uint64_t dl = 0;
1373 
1374 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1375 		res = TEE_ERROR_BAD_PARAMETERS;
1376 		goto out;
1377 	}
1378 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1379 
1380 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1381 		res = TEE_ERROR_BAD_PARAMETERS;
1382 		goto out;
1383 	}
1384 
1385 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1386 		res = TEE_ERROR_BAD_PARAMETERS;
1387 		goto out;
1388 	}
1389 
1390 	if (!srcData && !srcLen) {
1391 		*destLen = 0;
1392 		res = TEE_SUCCESS;
1393 		goto out;
1394 	}
1395 
1396 	/*
1397 	 * Check that required destLen is big enough before starting to feed
1398 	 * data to the algorithm. Errors during feeding of data are fatal as we
1399 	 * can't restore sync with this API.
1400 	 */
1401 	if (operation->block_size > 1) {
1402 		req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen,
1403 				     operation->block_size);
1404 	} else {
1405 		req_dlen = srcLen;
1406 	}
1407 
1408 	dl = *destLen;
1409 	if (dl < req_dlen) {
1410 		*destLen = req_dlen;
1411 		res = TEE_ERROR_SHORT_BUFFER;
1412 		goto out;
1413 	}
1414 
1415 	if (operation->block_size > 1) {
1416 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1417 					srcData, srcLen, destData, &dl);
1418 	} else {
1419 		if (srcLen > 0) {
1420 			res = _utee_authenc_update_payload(operation->state,
1421 							   srcData, srcLen,
1422 							   destData, &dl);
1423 		} else {
1424 			dl = 0;
1425 			res = TEE_SUCCESS;
1426 		}
1427 	}
1428 	if (res != TEE_SUCCESS)
1429 		goto out;
1430 
1431 	*destLen = dl;
1432 
1433 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1434 
1435 out:
1436 	if (res != TEE_SUCCESS &&
1437 	    res != TEE_ERROR_SHORT_BUFFER)
1438 			TEE_Panic(res);
1439 
1440 	return res;
1441 }
1442 
1443 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation,
1444 			      const void *srcData, uint32_t srcLen,
1445 			      void *destData, uint32_t *destLen, void *tag,
1446 			      uint32_t *tagLen)
1447 {
1448 	TEE_Result res;
1449 	uint8_t *dst = destData;
1450 	size_t acc_dlen = 0;
1451 	uint64_t tmp_dlen;
1452 	size_t req_dlen;
1453 	uint64_t tl;
1454 
1455 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1456 		res = TEE_ERROR_BAD_PARAMETERS;
1457 		goto out;
1458 	}
1459 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1460 	__utee_check_inout_annotation(tagLen, sizeof(*tagLen));
1461 
1462 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1463 		res = TEE_ERROR_BAD_PARAMETERS;
1464 		goto out;
1465 	}
1466 
1467 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1468 		res = TEE_ERROR_BAD_PARAMETERS;
1469 		goto out;
1470 	}
1471 
1472 	/*
1473 	 * Check that required destLen is big enough before starting to feed
1474 	 * data to the algorithm. Errors during feeding of data are fatal as we
1475 	 * can't restore sync with this API.
1476 	 *
1477 	 * Need to check this before update_payload since sync would be lost if
1478 	 * we return short buffer after that.
1479 	 */
1480 	res = TEE_ERROR_GENERIC;
1481 
1482 	req_dlen = operation->buffer_offs + srcLen;
1483 	if (*destLen < req_dlen) {
1484 		*destLen = req_dlen;
1485 		res = TEE_ERROR_SHORT_BUFFER;
1486 	}
1487 
1488 	if (*tagLen < operation->info.digestLength) {
1489 		*tagLen = operation->info.digestLength;
1490 		res = TEE_ERROR_SHORT_BUFFER;
1491 	}
1492 
1493 	if (res == TEE_ERROR_SHORT_BUFFER)
1494 		goto out;
1495 
1496 	tl = *tagLen;
1497 	tmp_dlen = *destLen - acc_dlen;
1498 	if (operation->block_size > 1) {
1499 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1500 					srcData, srcLen, dst, &tmp_dlen);
1501 		if (res != TEE_SUCCESS)
1502 			goto out;
1503 
1504 		dst += tmp_dlen;
1505 		acc_dlen += tmp_dlen;
1506 
1507 		tmp_dlen = *destLen - acc_dlen;
1508 		res = _utee_authenc_enc_final(operation->state,
1509 					      operation->buffer,
1510 					      operation->buffer_offs, dst,
1511 					      &tmp_dlen, tag, &tl);
1512 	} else {
1513 		res = _utee_authenc_enc_final(operation->state, srcData,
1514 					      srcLen, dst, &tmp_dlen,
1515 					      tag, &tl);
1516 	}
1517 	*tagLen = tl;
1518 	if (res != TEE_SUCCESS)
1519 		goto out;
1520 
1521 	acc_dlen += tmp_dlen;
1522 	*destLen = acc_dlen;
1523 
1524 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1525 
1526 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1527 
1528 out:
1529 	if (res != TEE_SUCCESS &&
1530 	    res != TEE_ERROR_SHORT_BUFFER)
1531 			TEE_Panic(res);
1532 
1533 	return res;
1534 }
1535 
1536 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation,
1537 			      const void *srcData, uint32_t srcLen,
1538 			      void *destData, uint32_t *destLen, void *tag,
1539 			      uint32_t tagLen)
1540 {
1541 	TEE_Result res;
1542 	uint8_t *dst = destData;
1543 	size_t acc_dlen = 0;
1544 	uint64_t tmp_dlen;
1545 	size_t req_dlen;
1546 
1547 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1548 		res = TEE_ERROR_BAD_PARAMETERS;
1549 		goto out;
1550 	}
1551 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1552 
1553 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1554 		res = TEE_ERROR_BAD_PARAMETERS;
1555 		goto out;
1556 	}
1557 
1558 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1559 		res = TEE_ERROR_BAD_PARAMETERS;
1560 		goto out;
1561 	}
1562 
1563 	/*
1564 	 * Check that required destLen is big enough before starting to feed
1565 	 * data to the algorithm. Errors during feeding of data are fatal as we
1566 	 * can't restore sync with this API.
1567 	 */
1568 	req_dlen = operation->buffer_offs + srcLen;
1569 	if (*destLen < req_dlen) {
1570 		*destLen = req_dlen;
1571 		res = TEE_ERROR_SHORT_BUFFER;
1572 		goto out;
1573 	}
1574 
1575 	tmp_dlen = *destLen - acc_dlen;
1576 	if (operation->block_size > 1) {
1577 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1578 					srcData, srcLen, dst, &tmp_dlen);
1579 		if (res != TEE_SUCCESS)
1580 			goto out;
1581 
1582 		dst += tmp_dlen;
1583 		acc_dlen += tmp_dlen;
1584 
1585 		tmp_dlen = *destLen - acc_dlen;
1586 		res = _utee_authenc_dec_final(operation->state,
1587 					      operation->buffer,
1588 					      operation->buffer_offs, dst,
1589 					      &tmp_dlen, tag, tagLen);
1590 	} else {
1591 		res = _utee_authenc_dec_final(operation->state, srcData,
1592 					      srcLen, dst, &tmp_dlen,
1593 					      tag, tagLen);
1594 	}
1595 	if (res != TEE_SUCCESS)
1596 		goto out;
1597 
1598 	/* Supplied tagLen should match what we initiated with */
1599 	if (tagLen != operation->info.digestLength)
1600 		res = TEE_ERROR_MAC_INVALID;
1601 
1602 	acc_dlen += tmp_dlen;
1603 	*destLen = acc_dlen;
1604 
1605 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1606 
1607 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1608 
1609 out:
1610 	if (res != TEE_SUCCESS &&
1611 	    res != TEE_ERROR_SHORT_BUFFER &&
1612 	    res != TEE_ERROR_MAC_INVALID)
1613 			TEE_Panic(res);
1614 
1615 	return res;
1616 }
1617 
1618 /* Cryptographic Operations API - Asymmetric Functions */
1619 
1620 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation,
1621 				 const TEE_Attribute *params,
1622 				 uint32_t paramCount, const void *srcData,
1623 				 uint32_t srcLen, void *destData,
1624 				 uint32_t *destLen)
1625 {
1626 	TEE_Result res = TEE_SUCCESS;
1627 	struct utee_attribute ua[paramCount];
1628 	uint64_t dl = 0;
1629 
1630 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
1631 		TEE_Panic(0);
1632 
1633 	__utee_check_attr_in_annotation(params, paramCount);
1634 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1635 
1636 	if (!operation->key1)
1637 		TEE_Panic(0);
1638 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1639 		TEE_Panic(0);
1640 	if (operation->info.mode != TEE_MODE_ENCRYPT)
1641 		TEE_Panic(0);
1642 
1643 	__utee_from_attr(ua, params, paramCount);
1644 	dl = *destLen;
1645 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1646 				  srcLen, destData, &dl);
1647 	*destLen = dl;
1648 
1649 	if (res != TEE_SUCCESS &&
1650 	    res != TEE_ERROR_SHORT_BUFFER &&
1651 	    res != TEE_ERROR_BAD_PARAMETERS)
1652 		TEE_Panic(res);
1653 
1654 	return res;
1655 }
1656 
1657 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation,
1658 				 const TEE_Attribute *params,
1659 				 uint32_t paramCount, const void *srcData,
1660 				 uint32_t srcLen, void *destData,
1661 				 uint32_t *destLen)
1662 {
1663 	TEE_Result res = TEE_SUCCESS;
1664 	struct utee_attribute ua[paramCount];
1665 	uint64_t dl = 0;
1666 
1667 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
1668 		TEE_Panic(0);
1669 
1670 	__utee_check_attr_in_annotation(params, paramCount);
1671 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1672 
1673 	if (!operation->key1)
1674 		TEE_Panic(0);
1675 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1676 		TEE_Panic(0);
1677 	if (operation->info.mode != TEE_MODE_DECRYPT)
1678 		TEE_Panic(0);
1679 
1680 	__utee_from_attr(ua, params, paramCount);
1681 	dl = *destLen;
1682 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1683 				  srcLen, destData, &dl);
1684 	*destLen = dl;
1685 
1686 	if (res != TEE_SUCCESS &&
1687 	    res != TEE_ERROR_SHORT_BUFFER &&
1688 	    res != TEE_ERROR_BAD_PARAMETERS)
1689 		TEE_Panic(res);
1690 
1691 	return res;
1692 }
1693 
1694 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation,
1695 				    const TEE_Attribute *params,
1696 				    uint32_t paramCount, const void *digest,
1697 				    uint32_t digestLen, void *signature,
1698 				    uint32_t *signatureLen)
1699 {
1700 	TEE_Result res = TEE_SUCCESS;
1701 	struct utee_attribute ua[paramCount];
1702 	uint64_t sl = 0;
1703 
1704 	if (operation == TEE_HANDLE_NULL || (!digest && digestLen))
1705 		TEE_Panic(0);
1706 
1707 	__utee_check_attr_in_annotation(params, paramCount);
1708 	__utee_check_inout_annotation(signatureLen, sizeof(*signatureLen));
1709 
1710 	if (!operation->key1)
1711 		TEE_Panic(0);
1712 	if (operation->info.operationClass !=
1713 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1714 		TEE_Panic(0);
1715 	if (operation->info.mode != TEE_MODE_SIGN)
1716 		TEE_Panic(0);
1717 
1718 	__utee_from_attr(ua, params, paramCount);
1719 	sl = *signatureLen;
1720 	res = _utee_asymm_operate(operation->state, ua, paramCount, digest,
1721 				  digestLen, signature, &sl);
1722 	*signatureLen = sl;
1723 
1724 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1725 		TEE_Panic(res);
1726 
1727 	return res;
1728 }
1729 
1730 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,
1731 				      const TEE_Attribute *params,
1732 				      uint32_t paramCount, const void *digest,
1733 				      uint32_t digestLen,
1734 				      const void *signature,
1735 				      uint32_t signatureLen)
1736 {
1737 	TEE_Result res;
1738 	struct utee_attribute ua[paramCount];
1739 
1740 	if (operation == TEE_HANDLE_NULL ||
1741 	    (digest == NULL && digestLen != 0) ||
1742 	    (signature == NULL && signatureLen != 0))
1743 		TEE_Panic(0);
1744 
1745 	__utee_check_attr_in_annotation(params, paramCount);
1746 
1747 	if (!operation->key1)
1748 		TEE_Panic(0);
1749 	if (operation->info.operationClass !=
1750 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1751 		TEE_Panic(0);
1752 	if (operation->info.mode != TEE_MODE_VERIFY)
1753 		TEE_Panic(0);
1754 
1755 	__utee_from_attr(ua, params, paramCount);
1756 	res = _utee_asymm_verify(operation->state, ua, paramCount, digest,
1757 				 digestLen, signature, signatureLen);
1758 
1759 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1760 		TEE_Panic(res);
1761 
1762 	return res;
1763 }
1764 
1765 /* Cryptographic Operations API - Key Derivation Functions */
1766 
1767 void TEE_DeriveKey(TEE_OperationHandle operation,
1768 		   const TEE_Attribute *params, uint32_t paramCount,
1769 		   TEE_ObjectHandle derivedKey)
1770 {
1771 	TEE_Result res;
1772 	TEE_ObjectInfo key_info;
1773 	struct utee_attribute ua[paramCount];
1774 
1775 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1776 		TEE_Panic(0);
1777 
1778 	__utee_check_attr_in_annotation(params, paramCount);
1779 
1780 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
1781 	    TEE_OPERATION_KEY_DERIVATION)
1782 		TEE_Panic(0);
1783 
1784 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1785 		TEE_Panic(0);
1786 	if (!operation->key1)
1787 		TEE_Panic(0);
1788 	if (operation->info.mode != TEE_MODE_DERIVE)
1789 		TEE_Panic(0);
1790 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1791 		TEE_Panic(0);
1792 
1793 	res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info);
1794 	if (res != TEE_SUCCESS)
1795 		TEE_Panic(res);
1796 
1797 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1798 		TEE_Panic(0);
1799 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1800 		TEE_Panic(0);
1801 
1802 	__utee_from_attr(ua, params, paramCount);
1803 	res = _utee_cryp_derive_key(operation->state, ua, paramCount,
1804 				    (unsigned long)derivedKey);
1805 	if (res != TEE_SUCCESS)
1806 		TEE_Panic(res);
1807 }
1808 
1809 /* Cryptographic Operations API - Random Number Generation Functions */
1810 
1811 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1812 {
1813 	TEE_Result res;
1814 
1815 	res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1816 	if (res != TEE_SUCCESS)
1817 		TEE_Panic(res);
1818 }
1819 
1820 int rand(void)
1821 {
1822 	int rc;
1823 
1824 	TEE_GenerateRandom(&rc, sizeof(rc));
1825 
1826 	/*
1827 	 * RAND_MAX is the larges int, INT_MAX which is all bits but the
1828 	 * highest bit set.
1829 	 */
1830 	return rc & RAND_MAX;
1831 }
1832 
1833 TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element)
1834 {
1835 	if (IS_ENABLED(CFG_CRYPTO_AES)) {
1836 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
1837 			if (alg == TEE_ALG_AES_ECB_NOPAD)
1838 				goto check_element_none;
1839 		}
1840 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
1841 			if (alg == TEE_ALG_AES_CBC_NOPAD)
1842 				goto check_element_none;
1843 		}
1844 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
1845 			if (alg == TEE_ALG_AES_CTR)
1846 				goto check_element_none;
1847 		}
1848 		if (IS_ENABLED(CFG_CRYPTO_CTS)) {
1849 			if (alg == TEE_ALG_AES_CTS)
1850 				goto check_element_none;
1851 		}
1852 		if (IS_ENABLED(CFG_CRYPTO_XTS)) {
1853 			if (alg == TEE_ALG_AES_XTS)
1854 				goto check_element_none;
1855 		}
1856 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
1857 			if (alg == TEE_ALG_AES_CBC_MAC_NOPAD ||
1858 			    alg == TEE_ALG_AES_CBC_MAC_PKCS5)
1859 				goto check_element_none;
1860 		}
1861 		if (IS_ENABLED(CFG_CRYPTO_CMAC)) {
1862 			if (alg == TEE_ALG_AES_CMAC)
1863 				goto check_element_none;
1864 		}
1865 		if (IS_ENABLED(CFG_CRYPTO_CCM)) {
1866 			if (alg == TEE_ALG_AES_CCM)
1867 				goto check_element_none;
1868 		}
1869 		if (IS_ENABLED(CFG_CRYPTO_GCM)) {
1870 			if (alg == TEE_ALG_AES_GCM)
1871 				goto check_element_none;
1872 		}
1873 	}
1874 	if (IS_ENABLED(CFG_CRYPTO_DES)) {
1875 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
1876 			if (alg == TEE_ALG_DES_ECB_NOPAD ||
1877 			    alg == TEE_ALG_DES3_ECB_NOPAD)
1878 				goto check_element_none;
1879 		}
1880 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
1881 			if (alg == TEE_ALG_DES_CBC_NOPAD ||
1882 			    alg == TEE_ALG_DES3_CBC_NOPAD)
1883 				goto check_element_none;
1884 		}
1885 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
1886 			if (alg == TEE_ALG_DES_CBC_MAC_NOPAD ||
1887 			    alg == TEE_ALG_DES_CBC_MAC_PKCS5 ||
1888 			    alg == TEE_ALG_DES3_CBC_MAC_NOPAD ||
1889 			    alg == TEE_ALG_DES3_CBC_MAC_PKCS5)
1890 				goto check_element_none;
1891 		}
1892 	}
1893 	if (IS_ENABLED(CFG_CRYPTO_MD5)) {
1894 		if (alg == TEE_ALG_MD5)
1895 			goto check_element_none;
1896 	}
1897 	if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
1898 		if (alg == TEE_ALG_SHA1)
1899 			goto check_element_none;
1900 	}
1901 	if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
1902 		if (alg == TEE_ALG_SHA224)
1903 			goto check_element_none;
1904 	}
1905 	if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
1906 		if (alg == TEE_ALG_SHA256)
1907 			goto check_element_none;
1908 	}
1909 	if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
1910 		if (alg == TEE_ALG_SHA384)
1911 			goto check_element_none;
1912 	}
1913 	if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
1914 		if (alg == TEE_ALG_SHA512)
1915 			goto check_element_none;
1916 	}
1917 	if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
1918 		if (alg == TEE_ALG_MD5SHA1)
1919 			goto check_element_none;
1920 	}
1921 	if (IS_ENABLED(CFG_CRYPTO_HMAC)) {
1922 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
1923 			if (alg == TEE_ALG_HMAC_MD5)
1924 				goto check_element_none;
1925 		}
1926 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
1927 			if (alg == TEE_ALG_HMAC_SHA1)
1928 				goto check_element_none;
1929 		}
1930 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
1931 			if (alg == TEE_ALG_HMAC_SHA224)
1932 				goto check_element_none;
1933 		}
1934 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
1935 			if (alg == TEE_ALG_HMAC_SHA256)
1936 				goto check_element_none;
1937 		}
1938 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
1939 			if (alg == TEE_ALG_HMAC_SHA384)
1940 				goto check_element_none;
1941 		}
1942 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
1943 			if (alg == TEE_ALG_HMAC_SHA512)
1944 				goto check_element_none;
1945 		}
1946 		if (IS_ENABLED(CFG_CRYPTO_SM3)) {
1947 			if (alg == TEE_ALG_HMAC_SM3)
1948 				goto check_element_none;
1949 		}
1950 	}
1951 	if (IS_ENABLED(CFG_CRYPTO_SM3)) {
1952 		if (alg == TEE_ALG_SM3)
1953 			goto check_element_none;
1954 	}
1955 	if (IS_ENABLED(CFG_CRYPTO_SM4)) {
1956 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
1957 			if (alg == TEE_ALG_SM4_ECB_NOPAD)
1958 				goto check_element_none;
1959 		}
1960 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
1961 			if (alg == TEE_ALG_SM4_CBC_NOPAD)
1962 				goto check_element_none;
1963 		}
1964 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
1965 			if (alg == TEE_ALG_SM4_CTR)
1966 				goto check_element_none;
1967 		}
1968 	}
1969 	if (IS_ENABLED(CFG_CRYPTO_RSA)) {
1970 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
1971 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5)
1972 				goto check_element_none;
1973 		}
1974 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
1975 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 ||
1976 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 ||
1977 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1)
1978 				goto check_element_none;
1979 		}
1980 		if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
1981 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1)
1982 				goto check_element_none;
1983 		}
1984 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
1985 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 ||
1986 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 ||
1987 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224)
1988 				goto check_element_none;
1989 		}
1990 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
1991 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 ||
1992 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 ||
1993 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256)
1994 				goto check_element_none;
1995 		}
1996 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
1997 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 ||
1998 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 ||
1999 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384)
2000 				goto check_element_none;
2001 		}
2002 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
2003 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 ||
2004 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 ||
2005 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512)
2006 				goto check_element_none;
2007 		}
2008 		if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) {
2009 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5)
2010 				goto check_element_none;
2011 		}
2012 		if (alg == TEE_ALG_RSA_NOPAD)
2013 			goto check_element_none;
2014 	}
2015 	if (IS_ENABLED(CFG_CRYPTO_DSA)) {
2016 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
2017 			if (alg == TEE_ALG_DSA_SHA1)
2018 				goto check_element_none;
2019 		}
2020 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
2021 			if (alg == TEE_ALG_DSA_SHA224)
2022 				goto check_element_none;
2023 		}
2024 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
2025 			if (alg == TEE_ALG_DSA_SHA256)
2026 				goto check_element_none;
2027 		}
2028 	}
2029 	if (IS_ENABLED(CFG_CRYPTO_DH)) {
2030 		if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
2031 			goto check_element_none;
2032 	}
2033 	if (IS_ENABLED(CFG_CRYPTO_ECC)) {
2034 		if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) &&
2035 		    element == TEE_ECC_CURVE_NIST_P192)
2036 			return TEE_SUCCESS;
2037 		if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) &&
2038 		    element == TEE_ECC_CURVE_NIST_P224)
2039 			return TEE_SUCCESS;
2040 		if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) &&
2041 		    element == TEE_ECC_CURVE_NIST_P256)
2042 			return TEE_SUCCESS;
2043 		if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) &&
2044 		    element == TEE_ECC_CURVE_NIST_P384)
2045 			return TEE_SUCCESS;
2046 		if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) &&
2047 		    element == TEE_ECC_CURVE_NIST_P521)
2048 			return TEE_SUCCESS;
2049 	}
2050 	if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) {
2051 		if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2)
2052 			return TEE_SUCCESS;
2053 	}
2054 	if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) {
2055 		if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2)
2056 			return TEE_SUCCESS;
2057 	}
2058 	if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) {
2059 		if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2)
2060 			return TEE_SUCCESS;
2061 	}
2062 
2063 	return TEE_ERROR_NOT_SUPPORTED;
2064 check_element_none:
2065 	if (element == TEE_CRYPTO_ELEMENT_NONE)
2066 		return TEE_SUCCESS;
2067 	return TEE_ERROR_NOT_SUPPORTED;
2068 }
2069