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