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