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