xref: /optee_os/lib/libutee/tee_api_operations.c (revision eee637e7c7de229fdcbeb4cbf4d8750e59131c33)
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 		res = tee_buffer_update(operation, _utee_cipher_update,
1113 					srcData, srcLen, dst, &tmp_dlen);
1114 		if (res != TEE_SUCCESS)
1115 			goto out;
1116 
1117 		dst += tmp_dlen;
1118 		acc_dlen += tmp_dlen;
1119 
1120 		tmp_dlen = *destLen - acc_dlen;
1121 		res = _utee_cipher_final(operation->state, operation->buffer,
1122 					 operation->buffer_offs, dst,
1123 					 &tmp_dlen);
1124 	} else {
1125 		res = _utee_cipher_final(operation->state, srcData, srcLen, dst,
1126 					 &tmp_dlen);
1127 	}
1128 	if (res != TEE_SUCCESS)
1129 		goto out;
1130 
1131 	acc_dlen += tmp_dlen;
1132 	if (destLen)
1133 		*destLen = acc_dlen;
1134 
1135 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1136 
1137 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1138 
1139 out:
1140 	if (res != TEE_SUCCESS &&
1141 	    res != TEE_ERROR_SHORT_BUFFER)
1142 		TEE_Panic(res);
1143 
1144 	return res;
1145 }
1146 
1147 /* Cryptographic Operations API - MAC Functions */
1148 
1149 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
1150 {
1151 	if (operation == TEE_HANDLE_NULL)
1152 		TEE_Panic(0);
1153 
1154 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1155 		TEE_Panic(0);
1156 
1157 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1158 	    !(operation->key1))
1159 		TEE_Panic(0);
1160 
1161 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1162 		TEE_ResetOperation(operation);
1163 
1164 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1165 
1166 	init_hash_operation(operation, IV, IVLen);
1167 }
1168 
1169 void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk,
1170 		   uint32_t chunkSize)
1171 {
1172 	TEE_Result res;
1173 
1174 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
1175 		TEE_Panic(0);
1176 
1177 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1178 		TEE_Panic(0);
1179 
1180 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1181 		TEE_Panic(0);
1182 
1183 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE)
1184 		TEE_Panic(0);
1185 
1186 	res = _utee_hash_update(operation->state, chunk, chunkSize);
1187 	if (res != TEE_SUCCESS)
1188 		TEE_Panic(res);
1189 }
1190 
1191 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
1192 			       const void *message, uint32_t messageLen,
1193 			       void *mac, uint32_t *macLen)
1194 {
1195 	TEE_Result res;
1196 	uint64_t ml;
1197 
1198 	if (operation == TEE_HANDLE_NULL || (!message && messageLen)) {
1199 		res = TEE_ERROR_BAD_PARAMETERS;
1200 		goto out;
1201 	}
1202 	__utee_check_inout_annotation(macLen, sizeof(*macLen));
1203 
1204 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1205 		res = TEE_ERROR_BAD_PARAMETERS;
1206 		goto out;
1207 	}
1208 
1209 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1210 		res = TEE_ERROR_BAD_PARAMETERS;
1211 		goto out;
1212 	}
1213 
1214 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1215 		res = TEE_ERROR_BAD_PARAMETERS;
1216 		goto out;
1217 	}
1218 
1219 	ml = *macLen;
1220 	res = _utee_hash_final(operation->state, message, messageLen, mac, &ml);
1221 	*macLen = ml;
1222 	if (res != TEE_SUCCESS)
1223 		goto out;
1224 
1225 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1226 
1227 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1228 
1229 out:
1230 	if (res != TEE_SUCCESS &&
1231 	    res != TEE_ERROR_SHORT_BUFFER)
1232 		TEE_Panic(res);
1233 
1234 	return res;
1235 }
1236 
1237 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
1238 			       const void *message, uint32_t messageLen,
1239 			       const void *mac, uint32_t macLen)
1240 {
1241 	TEE_Result res;
1242 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
1243 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1244 
1245 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1246 		res = TEE_ERROR_BAD_PARAMETERS;
1247 		goto out;
1248 	}
1249 
1250 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1251 		res = TEE_ERROR_BAD_PARAMETERS;
1252 		goto out;
1253 	}
1254 
1255 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1256 		res = TEE_ERROR_BAD_PARAMETERS;
1257 		goto out;
1258 	}
1259 
1260 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1261 				  &computed_mac_size);
1262 	if (res != TEE_SUCCESS)
1263 		goto out;
1264 
1265 	if (computed_mac_size != macLen) {
1266 		res = TEE_ERROR_MAC_INVALID;
1267 		goto out;
1268 	}
1269 
1270 	if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) {
1271 		res = TEE_ERROR_MAC_INVALID;
1272 		goto out;
1273 	}
1274 
1275 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1276 
1277 out:
1278 	if (res != TEE_SUCCESS &&
1279 	    res != TEE_ERROR_MAC_INVALID)
1280 		TEE_Panic(res);
1281 
1282 	return res;
1283 }
1284 
1285 /* Cryptographic Operations API - Authenticated Encryption Functions */
1286 
1287 TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce,
1288 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1289 		      uint32_t payloadLen)
1290 {
1291 	TEE_Result res;
1292 
1293 	if (operation == TEE_HANDLE_NULL || nonce == NULL) {
1294 		res = TEE_ERROR_BAD_PARAMETERS;
1295 		goto out;
1296 	}
1297 
1298 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1299 		res = TEE_ERROR_BAD_PARAMETERS;
1300 		goto out;
1301 	}
1302 
1303 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
1304 		res = TEE_ERROR_BAD_PARAMETERS;
1305 		goto out;
1306 	}
1307 
1308 	/*
1309 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1310 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1311 	 * according to the same principle so we have to check here instead to
1312 	 * be GP compliant.
1313 	 */
1314 	if (operation->info.algorithm == TEE_ALG_AES_GCM) {
1315 		/*
1316 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1317 		 */
1318 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) {
1319 			res = TEE_ERROR_NOT_SUPPORTED;
1320 			goto out;
1321 		}
1322 	}
1323 
1324 	res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8,
1325 				 AADLen, payloadLen);
1326 	if (res != TEE_SUCCESS)
1327 		goto out;
1328 
1329 	operation->info.digestLength = tagLen / 8;
1330 	operation->buffer_offs = 0;
1331 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1332 
1333 out:
1334 	if (res != TEE_SUCCESS &&
1335 	    res != TEE_ERROR_NOT_SUPPORTED)
1336 			TEE_Panic(res);
1337 
1338 	return res;
1339 }
1340 
1341 void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata,
1342 		     uint32_t AADdataLen)
1343 {
1344 	TEE_Result res;
1345 
1346 	if (operation == TEE_HANDLE_NULL ||
1347 	    (AADdata == NULL && AADdataLen != 0))
1348 		TEE_Panic(0);
1349 
1350 	if (operation->info.operationClass != TEE_OPERATION_AE)
1351 		TEE_Panic(0);
1352 
1353 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1354 		TEE_Panic(0);
1355 
1356 	res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen);
1357 
1358 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1359 
1360 	if (res != TEE_SUCCESS)
1361 		TEE_Panic(res);
1362 }
1363 
1364 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData,
1365 			uint32_t srcLen, void *destData, uint32_t *destLen)
1366 {
1367 	TEE_Result res = TEE_SUCCESS;
1368 	size_t req_dlen = 0;
1369 	uint64_t dl = 0;
1370 
1371 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1372 		res = TEE_ERROR_BAD_PARAMETERS;
1373 		goto out;
1374 	}
1375 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1376 
1377 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1378 		res = TEE_ERROR_BAD_PARAMETERS;
1379 		goto out;
1380 	}
1381 
1382 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1383 		res = TEE_ERROR_BAD_PARAMETERS;
1384 		goto out;
1385 	}
1386 
1387 	if (!srcData && !srcLen) {
1388 		*destLen = 0;
1389 		res = TEE_SUCCESS;
1390 		goto out;
1391 	}
1392 
1393 	/*
1394 	 * Check that required destLen is big enough before starting to feed
1395 	 * data to the algorithm. Errors during feeding of data are fatal as we
1396 	 * can't restore sync with this API.
1397 	 */
1398 	if (operation->block_size > 1) {
1399 		req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen,
1400 				     operation->block_size);
1401 	} else {
1402 		req_dlen = srcLen;
1403 	}
1404 
1405 	dl = *destLen;
1406 	if (dl < req_dlen) {
1407 		*destLen = req_dlen;
1408 		res = TEE_ERROR_SHORT_BUFFER;
1409 		goto out;
1410 	}
1411 
1412 	if (operation->block_size > 1) {
1413 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1414 					srcData, srcLen, destData, &dl);
1415 	} else {
1416 		if (srcLen > 0) {
1417 			res = _utee_authenc_update_payload(operation->state,
1418 							   srcData, srcLen,
1419 							   destData, &dl);
1420 		} else {
1421 			dl = 0;
1422 			res = TEE_SUCCESS;
1423 		}
1424 	}
1425 	if (res != TEE_SUCCESS)
1426 		goto out;
1427 
1428 	*destLen = dl;
1429 
1430 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1431 
1432 out:
1433 	if (res != TEE_SUCCESS &&
1434 	    res != TEE_ERROR_SHORT_BUFFER)
1435 			TEE_Panic(res);
1436 
1437 	return res;
1438 }
1439 
1440 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation,
1441 			      const void *srcData, uint32_t srcLen,
1442 			      void *destData, uint32_t *destLen, void *tag,
1443 			      uint32_t *tagLen)
1444 {
1445 	TEE_Result res;
1446 	uint8_t *dst = destData;
1447 	size_t acc_dlen = 0;
1448 	uint64_t tmp_dlen;
1449 	size_t req_dlen;
1450 	uint64_t tl;
1451 
1452 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1453 		res = TEE_ERROR_BAD_PARAMETERS;
1454 		goto out;
1455 	}
1456 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1457 	__utee_check_inout_annotation(tagLen, sizeof(*tagLen));
1458 
1459 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1460 		res = TEE_ERROR_BAD_PARAMETERS;
1461 		goto out;
1462 	}
1463 
1464 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1465 		res = TEE_ERROR_BAD_PARAMETERS;
1466 		goto out;
1467 	}
1468 
1469 	/*
1470 	 * Check that required destLen is big enough before starting to feed
1471 	 * data to the algorithm. Errors during feeding of data are fatal as we
1472 	 * can't restore sync with this API.
1473 	 *
1474 	 * Need to check this before update_payload since sync would be lost if
1475 	 * we return short buffer after that.
1476 	 */
1477 	res = TEE_ERROR_GENERIC;
1478 
1479 	req_dlen = operation->buffer_offs + srcLen;
1480 	if (*destLen < req_dlen) {
1481 		*destLen = req_dlen;
1482 		res = TEE_ERROR_SHORT_BUFFER;
1483 	}
1484 
1485 	if (*tagLen < operation->info.digestLength) {
1486 		*tagLen = operation->info.digestLength;
1487 		res = TEE_ERROR_SHORT_BUFFER;
1488 	}
1489 
1490 	if (res == TEE_ERROR_SHORT_BUFFER)
1491 		goto out;
1492 
1493 	tl = *tagLen;
1494 	tmp_dlen = *destLen - acc_dlen;
1495 	if (operation->block_size > 1) {
1496 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1497 					srcData, srcLen, dst, &tmp_dlen);
1498 		if (res != TEE_SUCCESS)
1499 			goto out;
1500 
1501 		dst += tmp_dlen;
1502 		acc_dlen += tmp_dlen;
1503 
1504 		tmp_dlen = *destLen - acc_dlen;
1505 		res = _utee_authenc_enc_final(operation->state,
1506 					      operation->buffer,
1507 					      operation->buffer_offs, dst,
1508 					      &tmp_dlen, tag, &tl);
1509 	} else {
1510 		res = _utee_authenc_enc_final(operation->state, srcData,
1511 					      srcLen, dst, &tmp_dlen,
1512 					      tag, &tl);
1513 	}
1514 	*tagLen = tl;
1515 	if (res != TEE_SUCCESS)
1516 		goto out;
1517 
1518 	acc_dlen += tmp_dlen;
1519 	*destLen = acc_dlen;
1520 
1521 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1522 
1523 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1524 
1525 out:
1526 	if (res != TEE_SUCCESS &&
1527 	    res != TEE_ERROR_SHORT_BUFFER)
1528 			TEE_Panic(res);
1529 
1530 	return res;
1531 }
1532 
1533 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation,
1534 			      const void *srcData, uint32_t srcLen,
1535 			      void *destData, uint32_t *destLen, void *tag,
1536 			      uint32_t tagLen)
1537 {
1538 	TEE_Result res;
1539 	uint8_t *dst = destData;
1540 	size_t acc_dlen = 0;
1541 	uint64_t tmp_dlen;
1542 	size_t req_dlen;
1543 
1544 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1545 		res = TEE_ERROR_BAD_PARAMETERS;
1546 		goto out;
1547 	}
1548 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1549 
1550 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1551 		res = TEE_ERROR_BAD_PARAMETERS;
1552 		goto out;
1553 	}
1554 
1555 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1556 		res = TEE_ERROR_BAD_PARAMETERS;
1557 		goto out;
1558 	}
1559 
1560 	/*
1561 	 * Check that required destLen is big enough before starting to feed
1562 	 * data to the algorithm. Errors during feeding of data are fatal as we
1563 	 * can't restore sync with this API.
1564 	 */
1565 	req_dlen = operation->buffer_offs + srcLen;
1566 	if (*destLen < req_dlen) {
1567 		*destLen = req_dlen;
1568 		res = TEE_ERROR_SHORT_BUFFER;
1569 		goto out;
1570 	}
1571 
1572 	tmp_dlen = *destLen - acc_dlen;
1573 	if (operation->block_size > 1) {
1574 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1575 					srcData, srcLen, dst, &tmp_dlen);
1576 		if (res != TEE_SUCCESS)
1577 			goto out;
1578 
1579 		dst += tmp_dlen;
1580 		acc_dlen += tmp_dlen;
1581 
1582 		tmp_dlen = *destLen - acc_dlen;
1583 		res = _utee_authenc_dec_final(operation->state,
1584 					      operation->buffer,
1585 					      operation->buffer_offs, dst,
1586 					      &tmp_dlen, tag, tagLen);
1587 	} else {
1588 		res = _utee_authenc_dec_final(operation->state, srcData,
1589 					      srcLen, dst, &tmp_dlen,
1590 					      tag, tagLen);
1591 	}
1592 	if (res != TEE_SUCCESS)
1593 		goto out;
1594 
1595 	/* Supplied tagLen should match what we initiated with */
1596 	if (tagLen != operation->info.digestLength)
1597 		res = TEE_ERROR_MAC_INVALID;
1598 
1599 	acc_dlen += tmp_dlen;
1600 	*destLen = acc_dlen;
1601 
1602 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1603 
1604 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1605 
1606 out:
1607 	if (res != TEE_SUCCESS &&
1608 	    res != TEE_ERROR_SHORT_BUFFER &&
1609 	    res != TEE_ERROR_MAC_INVALID)
1610 			TEE_Panic(res);
1611 
1612 	return res;
1613 }
1614 
1615 /* Cryptographic Operations API - Asymmetric Functions */
1616 
1617 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation,
1618 				 const TEE_Attribute *params,
1619 				 uint32_t paramCount, const void *srcData,
1620 				 uint32_t srcLen, void *destData,
1621 				 uint32_t *destLen)
1622 {
1623 	TEE_Result res = TEE_SUCCESS;
1624 	struct utee_attribute ua[paramCount];
1625 	uint64_t dl = 0;
1626 
1627 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
1628 		TEE_Panic(0);
1629 
1630 	__utee_check_attr_in_annotation(params, paramCount);
1631 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1632 
1633 	if (!operation->key1)
1634 		TEE_Panic(0);
1635 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1636 		TEE_Panic(0);
1637 	if (operation->info.mode != TEE_MODE_ENCRYPT)
1638 		TEE_Panic(0);
1639 
1640 	__utee_from_attr(ua, params, paramCount);
1641 	dl = *destLen;
1642 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1643 				  srcLen, destData, &dl);
1644 	*destLen = dl;
1645 
1646 	if (res != TEE_SUCCESS &&
1647 	    res != TEE_ERROR_SHORT_BUFFER &&
1648 	    res != TEE_ERROR_BAD_PARAMETERS)
1649 		TEE_Panic(res);
1650 
1651 	return res;
1652 }
1653 
1654 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation,
1655 				 const TEE_Attribute *params,
1656 				 uint32_t paramCount, const void *srcData,
1657 				 uint32_t srcLen, void *destData,
1658 				 uint32_t *destLen)
1659 {
1660 	TEE_Result res = TEE_SUCCESS;
1661 	struct utee_attribute ua[paramCount];
1662 	uint64_t dl = 0;
1663 
1664 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
1665 		TEE_Panic(0);
1666 
1667 	__utee_check_attr_in_annotation(params, paramCount);
1668 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1669 
1670 	if (!operation->key1)
1671 		TEE_Panic(0);
1672 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1673 		TEE_Panic(0);
1674 	if (operation->info.mode != TEE_MODE_DECRYPT)
1675 		TEE_Panic(0);
1676 
1677 	__utee_from_attr(ua, params, paramCount);
1678 	dl = *destLen;
1679 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1680 				  srcLen, destData, &dl);
1681 	*destLen = dl;
1682 
1683 	if (res != TEE_SUCCESS &&
1684 	    res != TEE_ERROR_SHORT_BUFFER &&
1685 	    res != TEE_ERROR_BAD_PARAMETERS)
1686 		TEE_Panic(res);
1687 
1688 	return res;
1689 }
1690 
1691 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation,
1692 				    const TEE_Attribute *params,
1693 				    uint32_t paramCount, const void *digest,
1694 				    uint32_t digestLen, void *signature,
1695 				    uint32_t *signatureLen)
1696 {
1697 	TEE_Result res = TEE_SUCCESS;
1698 	struct utee_attribute ua[paramCount];
1699 	uint64_t sl = 0;
1700 
1701 	if (operation == TEE_HANDLE_NULL || (!digest && digestLen))
1702 		TEE_Panic(0);
1703 
1704 	__utee_check_attr_in_annotation(params, paramCount);
1705 	__utee_check_inout_annotation(signatureLen, sizeof(*signatureLen));
1706 
1707 	if (!operation->key1)
1708 		TEE_Panic(0);
1709 	if (operation->info.operationClass !=
1710 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1711 		TEE_Panic(0);
1712 	if (operation->info.mode != TEE_MODE_SIGN)
1713 		TEE_Panic(0);
1714 
1715 	__utee_from_attr(ua, params, paramCount);
1716 	sl = *signatureLen;
1717 	res = _utee_asymm_operate(operation->state, ua, paramCount, digest,
1718 				  digestLen, signature, &sl);
1719 	*signatureLen = sl;
1720 
1721 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1722 		TEE_Panic(res);
1723 
1724 	return res;
1725 }
1726 
1727 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,
1728 				      const TEE_Attribute *params,
1729 				      uint32_t paramCount, const void *digest,
1730 				      uint32_t digestLen,
1731 				      const void *signature,
1732 				      uint32_t signatureLen)
1733 {
1734 	TEE_Result res;
1735 	struct utee_attribute ua[paramCount];
1736 
1737 	if (operation == TEE_HANDLE_NULL ||
1738 	    (digest == NULL && digestLen != 0) ||
1739 	    (signature == NULL && signatureLen != 0))
1740 		TEE_Panic(0);
1741 
1742 	__utee_check_attr_in_annotation(params, paramCount);
1743 
1744 	if (!operation->key1)
1745 		TEE_Panic(0);
1746 	if (operation->info.operationClass !=
1747 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1748 		TEE_Panic(0);
1749 	if (operation->info.mode != TEE_MODE_VERIFY)
1750 		TEE_Panic(0);
1751 
1752 	__utee_from_attr(ua, params, paramCount);
1753 	res = _utee_asymm_verify(operation->state, ua, paramCount, digest,
1754 				 digestLen, signature, signatureLen);
1755 
1756 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1757 		TEE_Panic(res);
1758 
1759 	return res;
1760 }
1761 
1762 /* Cryptographic Operations API - Key Derivation Functions */
1763 
1764 void TEE_DeriveKey(TEE_OperationHandle operation,
1765 		   const TEE_Attribute *params, uint32_t paramCount,
1766 		   TEE_ObjectHandle derivedKey)
1767 {
1768 	TEE_Result res;
1769 	TEE_ObjectInfo key_info;
1770 	struct utee_attribute ua[paramCount];
1771 
1772 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1773 		TEE_Panic(0);
1774 
1775 	__utee_check_attr_in_annotation(params, paramCount);
1776 
1777 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
1778 	    TEE_OPERATION_KEY_DERIVATION)
1779 		TEE_Panic(0);
1780 
1781 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1782 		TEE_Panic(0);
1783 	if (!operation->key1)
1784 		TEE_Panic(0);
1785 	if (operation->info.mode != TEE_MODE_DERIVE)
1786 		TEE_Panic(0);
1787 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1788 		TEE_Panic(0);
1789 
1790 	res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info);
1791 	if (res != TEE_SUCCESS)
1792 		TEE_Panic(res);
1793 
1794 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1795 		TEE_Panic(0);
1796 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1797 		TEE_Panic(0);
1798 
1799 	__utee_from_attr(ua, params, paramCount);
1800 	res = _utee_cryp_derive_key(operation->state, ua, paramCount,
1801 				    (unsigned long)derivedKey);
1802 	if (res != TEE_SUCCESS)
1803 		TEE_Panic(res);
1804 }
1805 
1806 /* Cryptographic Operations API - Random Number Generation Functions */
1807 
1808 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1809 {
1810 	TEE_Result res;
1811 
1812 	res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1813 	if (res != TEE_SUCCESS)
1814 		TEE_Panic(res);
1815 }
1816 
1817 int rand(void)
1818 {
1819 	int rc;
1820 
1821 	TEE_GenerateRandom(&rc, sizeof(rc));
1822 
1823 	/*
1824 	 * RAND_MAX is the larges int, INT_MAX which is all bits but the
1825 	 * highest bit set.
1826 	 */
1827 	return rc & RAND_MAX;
1828 }
1829 
1830 TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element)
1831 {
1832 	if (IS_ENABLED(CFG_CRYPTO_AES)) {
1833 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
1834 			if (alg == TEE_ALG_AES_ECB_NOPAD)
1835 				goto check_element_none;
1836 		}
1837 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
1838 			if (alg == TEE_ALG_AES_CBC_NOPAD)
1839 				goto check_element_none;
1840 		}
1841 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
1842 			if (alg == TEE_ALG_AES_CTR)
1843 				goto check_element_none;
1844 		}
1845 		if (IS_ENABLED(CFG_CRYPTO_CTS)) {
1846 			if (alg == TEE_ALG_AES_CTS)
1847 				goto check_element_none;
1848 		}
1849 		if (IS_ENABLED(CFG_CRYPTO_XTS)) {
1850 			if (alg == TEE_ALG_AES_XTS)
1851 				goto check_element_none;
1852 		}
1853 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
1854 			if (alg == TEE_ALG_AES_CBC_MAC_NOPAD ||
1855 			    alg == TEE_ALG_AES_CBC_MAC_PKCS5)
1856 				goto check_element_none;
1857 		}
1858 		if (IS_ENABLED(CFG_CRYPTO_CMAC)) {
1859 			if (alg == TEE_ALG_AES_CMAC)
1860 				goto check_element_none;
1861 		}
1862 		if (IS_ENABLED(CFG_CRYPTO_CCM)) {
1863 			if (alg == TEE_ALG_AES_CCM)
1864 				goto check_element_none;
1865 		}
1866 		if (IS_ENABLED(CFG_CRYPTO_GCM)) {
1867 			if (alg == TEE_ALG_AES_GCM)
1868 				goto check_element_none;
1869 		}
1870 	}
1871 	if (IS_ENABLED(CFG_CRYPTO_DES)) {
1872 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
1873 			if (alg == TEE_ALG_DES_ECB_NOPAD ||
1874 			    alg == TEE_ALG_DES3_ECB_NOPAD)
1875 				goto check_element_none;
1876 		}
1877 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
1878 			if (alg == TEE_ALG_DES_CBC_NOPAD ||
1879 			    alg == TEE_ALG_DES3_CBC_NOPAD)
1880 				goto check_element_none;
1881 		}
1882 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
1883 			if (alg == TEE_ALG_DES_CBC_MAC_NOPAD ||
1884 			    alg == TEE_ALG_DES_CBC_MAC_PKCS5 ||
1885 			    alg == TEE_ALG_DES3_CBC_MAC_NOPAD ||
1886 			    alg == TEE_ALG_DES3_CBC_MAC_PKCS5)
1887 				goto check_element_none;
1888 		}
1889 	}
1890 	if (IS_ENABLED(CFG_CRYPTO_MD5)) {
1891 		if (alg == TEE_ALG_MD5)
1892 			goto check_element_none;
1893 	}
1894 	if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
1895 		if (alg == TEE_ALG_SHA1)
1896 			goto check_element_none;
1897 	}
1898 	if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
1899 		if (alg == TEE_ALG_SHA224)
1900 			goto check_element_none;
1901 	}
1902 	if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
1903 		if (alg == TEE_ALG_SHA256)
1904 			goto check_element_none;
1905 	}
1906 	if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
1907 		if (alg == TEE_ALG_SHA384)
1908 			goto check_element_none;
1909 	}
1910 	if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
1911 		if (alg == TEE_ALG_SHA512)
1912 			goto check_element_none;
1913 	}
1914 	if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
1915 		if (alg == TEE_ALG_MD5SHA1)
1916 			goto check_element_none;
1917 	}
1918 	if (IS_ENABLED(CFG_CRYPTO_HMAC)) {
1919 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
1920 			if (alg == TEE_ALG_HMAC_MD5)
1921 				goto check_element_none;
1922 		}
1923 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
1924 			if (alg == TEE_ALG_HMAC_SHA1)
1925 				goto check_element_none;
1926 		}
1927 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
1928 			if (alg == TEE_ALG_HMAC_SHA224)
1929 				goto check_element_none;
1930 		}
1931 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
1932 			if (alg == TEE_ALG_HMAC_SHA256)
1933 				goto check_element_none;
1934 		}
1935 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
1936 			if (alg == TEE_ALG_HMAC_SHA384)
1937 				goto check_element_none;
1938 		}
1939 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
1940 			if (alg == TEE_ALG_HMAC_SHA512)
1941 				goto check_element_none;
1942 		}
1943 		if (IS_ENABLED(CFG_CRYPTO_SM3)) {
1944 			if (alg == TEE_ALG_HMAC_SM3)
1945 				goto check_element_none;
1946 		}
1947 	}
1948 	if (IS_ENABLED(CFG_CRYPTO_SM3)) {
1949 		if (alg == TEE_ALG_SM3)
1950 			goto check_element_none;
1951 	}
1952 	if (IS_ENABLED(CFG_CRYPTO_SM4)) {
1953 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
1954 			if (alg == TEE_ALG_SM4_ECB_NOPAD)
1955 				goto check_element_none;
1956 		}
1957 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
1958 			if (alg == TEE_ALG_SM4_CBC_NOPAD)
1959 				goto check_element_none;
1960 		}
1961 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
1962 			if (alg == TEE_ALG_SM4_CTR)
1963 				goto check_element_none;
1964 		}
1965 	}
1966 	if (IS_ENABLED(CFG_CRYPTO_RSA)) {
1967 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
1968 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5)
1969 				goto check_element_none;
1970 		}
1971 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
1972 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 ||
1973 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 ||
1974 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1)
1975 				goto check_element_none;
1976 		}
1977 		if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
1978 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1)
1979 				goto check_element_none;
1980 		}
1981 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
1982 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 ||
1983 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 ||
1984 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224)
1985 				goto check_element_none;
1986 		}
1987 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
1988 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 ||
1989 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 ||
1990 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256)
1991 				goto check_element_none;
1992 		}
1993 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
1994 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 ||
1995 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 ||
1996 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384)
1997 				goto check_element_none;
1998 		}
1999 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
2000 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 ||
2001 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 ||
2002 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512)
2003 				goto check_element_none;
2004 		}
2005 		if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) {
2006 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5)
2007 				goto check_element_none;
2008 		}
2009 		if (alg == TEE_ALG_RSA_NOPAD)
2010 			goto check_element_none;
2011 	}
2012 	if (IS_ENABLED(CFG_CRYPTO_DSA)) {
2013 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
2014 			if (alg == TEE_ALG_DSA_SHA1)
2015 				goto check_element_none;
2016 		}
2017 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
2018 			if (alg == TEE_ALG_DSA_SHA224)
2019 				goto check_element_none;
2020 		}
2021 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
2022 			if (alg == TEE_ALG_DSA_SHA256)
2023 				goto check_element_none;
2024 		}
2025 	}
2026 	if (IS_ENABLED(CFG_CRYPTO_DH)) {
2027 		if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
2028 			goto check_element_none;
2029 	}
2030 	if (IS_ENABLED(CFG_CRYPTO_ECC)) {
2031 		if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) &&
2032 		    element == TEE_ECC_CURVE_NIST_P192)
2033 			return TEE_SUCCESS;
2034 		if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) &&
2035 		    element == TEE_ECC_CURVE_NIST_P224)
2036 			return TEE_SUCCESS;
2037 		if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) &&
2038 		    element == TEE_ECC_CURVE_NIST_P256)
2039 			return TEE_SUCCESS;
2040 		if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) &&
2041 		    element == TEE_ECC_CURVE_NIST_P384)
2042 			return TEE_SUCCESS;
2043 		if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) &&
2044 		    element == TEE_ECC_CURVE_NIST_P521)
2045 			return TEE_SUCCESS;
2046 	}
2047 	if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) {
2048 		if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2)
2049 			return TEE_SUCCESS;
2050 	}
2051 	if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) {
2052 		if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2)
2053 			return TEE_SUCCESS;
2054 	}
2055 	if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) {
2056 		if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2)
2057 			return TEE_SUCCESS;
2058 	}
2059 
2060 	return TEE_ERROR_NOT_SUPPORTED;
2061 check_element_none:
2062 	if (element == TEE_CRYPTO_ELEMENT_NONE)
2063 		return TEE_SUCCESS;
2064 	return TEE_ERROR_NOT_SUPPORTED;
2065 }
2066