xref: /optee_os/lib/libutee/tee_api_operations.c (revision ad7aa2a537fbbaadc5ebd9bd69ea7cc1e842ca14)
11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause
2b0104773SPascal Brand /*
3b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
4eee637e7SAlexander Zakharov  * Copyright (c) 2021, SumUp Services GmbH
5b0104773SPascal Brand  */
679170ce0SJerome Forissier #include <config.h>
7b0104773SPascal Brand #include <stdlib.h>
8b0104773SPascal Brand #include <string.h>
9b796ebf3SJerome Forissier #include <string_ext.h>
10b0104773SPascal Brand #include <tee_api.h>
118854d3c6SJerome Forissier #include <tee_api_defines_extensions.h>
12b0104773SPascal Brand #include <tee_internal_api_extensions.h>
13b0104773SPascal Brand #include <utee_syscalls.h>
14b0104773SPascal Brand #include <utee_defines.h>
15fc26c92aSJens Wiklander #include <util.h>
16e86f1266SJens Wiklander #include "tee_api_private.h"
17b0104773SPascal Brand 
18b0104773SPascal Brand struct __TEE_OperationHandle {
19b0104773SPascal Brand 	TEE_OperationInfo info;
20b0104773SPascal Brand 	TEE_ObjectHandle key1;
21b0104773SPascal Brand 	TEE_ObjectHandle key2;
22642a1607SCedric Chaumont 	uint32_t operationState;/* Operation state : INITIAL or ACTIVE */
23b0104773SPascal Brand 	uint8_t *buffer;	/* buffer to collect complete blocks */
24b0104773SPascal Brand 	bool buffer_two_blocks;	/* True if two blocks need to be buffered */
25b0104773SPascal Brand 	size_t block_size;	/* Block size of cipher */
26b0104773SPascal Brand 	size_t buffer_offs;	/* Offset in buffer */
27b0104773SPascal Brand 	uint32_t state;		/* Handle to state in TEE Core */
28b0104773SPascal Brand };
29b0104773SPascal Brand 
30b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */
31b0104773SPascal Brand 
32b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation,
33b0104773SPascal Brand 				 uint32_t algorithm, uint32_t mode,
34b0104773SPascal Brand 				 uint32_t maxKeySize)
35b0104773SPascal Brand {
36b0104773SPascal Brand 	TEE_Result res;
37b0104773SPascal Brand 	TEE_OperationHandle op = TEE_HANDLE_NULL;
38b0104773SPascal Brand 	uint32_t handle_state = 0;
39b0104773SPascal Brand 	size_t block_size = 1;
40b0104773SPascal Brand 	uint32_t req_key_usage;
41b0104773SPascal Brand 	bool with_private_key = false;
42b0104773SPascal Brand 	bool buffer_two_blocks = false;
43b0104773SPascal Brand 
449b52c538SCedric Chaumont 	if (!operation)
45b0104773SPascal Brand 		TEE_Panic(0);
46b0104773SPascal Brand 
475b385b3fSJerome Forissier 	if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP)
48b0104773SPascal Brand 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
49b0104773SPascal Brand 
50218d9055SCedric Chaumont 	/* Check algorithm max key size */
51218d9055SCedric Chaumont 	switch (algorithm) {
52218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA1:
53218d9055SCedric Chaumont 		if (maxKeySize < 512)
54218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
55218d9055SCedric Chaumont 		if (maxKeySize > 1024)
56218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
57218d9055SCedric Chaumont 		if (maxKeySize % 64 != 0)
58218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
59218d9055SCedric Chaumont 		break;
60218d9055SCedric Chaumont 
61218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA224:
62218d9055SCedric Chaumont 		if (maxKeySize != 2048)
63218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
64218d9055SCedric Chaumont 		break;
65218d9055SCedric Chaumont 
66218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA256:
67218d9055SCedric Chaumont 		if (maxKeySize != 2048 && maxKeySize != 3072)
68218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
69218d9055SCedric Chaumont 		break;
70218d9055SCedric Chaumont 
711220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P192:
721220586eSCedric Chaumont 	case TEE_ALG_ECDH_P192:
731220586eSCedric Chaumont 		if (maxKeySize != 192)
741220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
751220586eSCedric Chaumont 		break;
761220586eSCedric Chaumont 
771220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P224:
781220586eSCedric Chaumont 	case TEE_ALG_ECDH_P224:
791220586eSCedric Chaumont 		if (maxKeySize != 224)
801220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
811220586eSCedric Chaumont 		break;
821220586eSCedric Chaumont 
831220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P256:
841220586eSCedric Chaumont 	case TEE_ALG_ECDH_P256:
8591fc6bd8SJerome Forissier 	case TEE_ALG_SM2_PKE:
860f151943SJerome Forissier 	case TEE_ALG_SM2_DSA_SM3:
871220586eSCedric Chaumont 		if (maxKeySize != 256)
881220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
891220586eSCedric Chaumont 		break;
901220586eSCedric Chaumont 
915b385b3fSJerome Forissier 	case TEE_ALG_SM2_KEP:
925b385b3fSJerome Forissier 		/* Two 256-bit keys */
935b385b3fSJerome Forissier 		if (maxKeySize != 512)
945b385b3fSJerome Forissier 			return TEE_ERROR_NOT_SUPPORTED;
955b385b3fSJerome Forissier 		break;
965b385b3fSJerome Forissier 
971220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P384:
981220586eSCedric Chaumont 	case TEE_ALG_ECDH_P384:
991220586eSCedric Chaumont 		if (maxKeySize != 384)
1001220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1011220586eSCedric Chaumont 		break;
1021220586eSCedric Chaumont 
1031220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P521:
1041220586eSCedric Chaumont 	case TEE_ALG_ECDH_P521:
1051220586eSCedric Chaumont 		if (maxKeySize != 521)
1061220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1071220586eSCedric Chaumont 		break;
1081220586eSCedric Chaumont 
109218d9055SCedric Chaumont 	default:
110218d9055SCedric Chaumont 		break;
111218d9055SCedric Chaumont 	}
112218d9055SCedric Chaumont 
113cf5c060cSJens Wiklander 	/* Check algorithm mode (and maxKeySize for digests) */
114b0104773SPascal Brand 	switch (algorithm) {
115b0104773SPascal Brand 	case TEE_ALG_AES_CTS:
116b0104773SPascal Brand 	case TEE_ALG_AES_XTS:
117b0104773SPascal Brand 		buffer_two_blocks = true;
118919a5a68SJerome Forissier 		fallthrough;
1194bd53c54SJerome Forissier 	case TEE_ALG_AES_ECB_NOPAD:
120b0104773SPascal Brand 	case TEE_ALG_AES_CBC_NOPAD:
121b0104773SPascal Brand 	case TEE_ALG_AES_CCM:
122b0104773SPascal Brand 	case TEE_ALG_DES_ECB_NOPAD:
123b0104773SPascal Brand 	case TEE_ALG_DES_CBC_NOPAD:
124b0104773SPascal Brand 	case TEE_ALG_DES3_ECB_NOPAD:
125b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_NOPAD:
126ade6f848SJerome Forissier 	case TEE_ALG_SM4_ECB_NOPAD:
127ade6f848SJerome Forissier 	case TEE_ALG_SM4_CBC_NOPAD:
128ade6f848SJerome Forissier 	case TEE_ALG_SM4_CTR:
129b0104773SPascal Brand 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
130b0104773SPascal Brand 			block_size = TEE_AES_BLOCK_SIZE;
131ade6f848SJerome Forissier 		else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4)
132ade6f848SJerome Forissier 			block_size = TEE_SM4_BLOCK_SIZE;
133b0104773SPascal Brand 		else
134b0104773SPascal Brand 			block_size = TEE_DES_BLOCK_SIZE;
135919a5a68SJerome Forissier 		fallthrough;
13657aabac5SBogdan Liulko 	case TEE_ALG_AES_CTR:
137afc0c182SBogdan Liulko 	case TEE_ALG_AES_GCM:
138b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT)
139b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
140b0104773SPascal Brand 		else if (mode == TEE_MODE_DECRYPT)
141b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
142b0104773SPascal Brand 		else
143b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
144b0104773SPascal Brand 		break;
145b0104773SPascal Brand 
1466a2e0a9fSGabor Szekely #if defined(CFG_CRYPTO_RSASSA_NA1)
1476a2e0a9fSGabor Szekely 	case TEE_ALG_RSASSA_PKCS1_V1_5:
1486a2e0a9fSGabor Szekely #endif
149b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
150b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
151b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
152b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
153b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
154b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
155b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
156b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
157b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
158b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
159b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
160b0104773SPascal Brand 	case TEE_ALG_DSA_SHA1:
161218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA224:
162218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA256:
1631220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P192:
1641220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P224:
1651220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P256:
1661220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P384:
1671220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P521:
1680f151943SJerome Forissier 	case TEE_ALG_SM2_DSA_SM3:
169b0104773SPascal Brand 		if (mode == TEE_MODE_SIGN) {
170b0104773SPascal Brand 			with_private_key = true;
171b0104773SPascal Brand 			req_key_usage = TEE_USAGE_SIGN;
172b0104773SPascal Brand 		} else if (mode == TEE_MODE_VERIFY) {
173b0104773SPascal Brand 			req_key_usage = TEE_USAGE_VERIFY;
174b0104773SPascal Brand 		} else {
175b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
176b0104773SPascal Brand 		}
177b0104773SPascal Brand 		break;
178b0104773SPascal Brand 
179b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_V1_5:
180b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
181b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
182b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
183b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
184b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
18591fc6bd8SJerome Forissier 	case TEE_ALG_SM2_PKE:
186b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
187b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
188b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
189b0104773SPascal Brand 			with_private_key = true;
190b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
191b0104773SPascal Brand 		} else {
192b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
193b0104773SPascal Brand 		}
194b0104773SPascal Brand 		break;
195b0104773SPascal Brand 
196b0104773SPascal Brand 	case TEE_ALG_RSA_NOPAD:
197b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
198b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
199b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
200b0104773SPascal Brand 			with_private_key = true;
201b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
202b0104773SPascal Brand 		} else {
203b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
204b0104773SPascal Brand 		}
205b0104773SPascal Brand 		break;
206b0104773SPascal Brand 
207b0104773SPascal Brand 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
2081220586eSCedric Chaumont 	case TEE_ALG_ECDH_P192:
2091220586eSCedric Chaumont 	case TEE_ALG_ECDH_P224:
2101220586eSCedric Chaumont 	case TEE_ALG_ECDH_P256:
2111220586eSCedric Chaumont 	case TEE_ALG_ECDH_P384:
2121220586eSCedric Chaumont 	case TEE_ALG_ECDH_P521:
213cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_MD5_DERIVE_KEY:
214cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA1_DERIVE_KEY:
215cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA224_DERIVE_KEY:
216cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA256_DERIVE_KEY:
217cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA384_DERIVE_KEY:
218cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA512_DERIVE_KEY:
2198854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY:
2208854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY:
2218854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY:
2228854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY:
2238854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY:
2240f2293b7SJerome Forissier 	case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY:
2255b385b3fSJerome Forissier 	case TEE_ALG_SM2_KEP:
226b0104773SPascal Brand 		if (mode != TEE_MODE_DERIVE)
227b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
228b0104773SPascal Brand 		with_private_key = true;
229b0104773SPascal Brand 		req_key_usage = TEE_USAGE_DERIVE;
230b0104773SPascal Brand 		break;
231b0104773SPascal Brand 
232b0104773SPascal Brand 	case TEE_ALG_MD5:
233b0104773SPascal Brand 	case TEE_ALG_SHA1:
234b0104773SPascal Brand 	case TEE_ALG_SHA224:
235b0104773SPascal Brand 	case TEE_ALG_SHA256:
236b0104773SPascal Brand 	case TEE_ALG_SHA384:
237b0104773SPascal Brand 	case TEE_ALG_SHA512:
23847645577SJerome Forissier 	case TEE_ALG_SM3:
239b0104773SPascal Brand 		if (mode != TEE_MODE_DIGEST)
240b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
241cf5c060cSJens Wiklander 		if (maxKeySize)
242cf5c060cSJens Wiklander 			return TEE_ERROR_NOT_SUPPORTED;
24305304565SCedric Chaumont 		/* v1.1: flags always set for digest operations */
244b0104773SPascal Brand 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
245b0104773SPascal Brand 		req_key_usage = 0;
246b0104773SPascal Brand 		break;
247b0104773SPascal Brand 
248b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_NOPAD:
249b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_NOPAD:
250b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_PKCS5:
251b0104773SPascal Brand 	case TEE_ALG_AES_CMAC:
252b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_PKCS5:
253b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
254b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
255eee637e7SAlexander Zakharov 	case TEE_ALG_DES3_CMAC:
256b0104773SPascal Brand 	case TEE_ALG_HMAC_MD5:
257b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA1:
258b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA224:
259b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA256:
260b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA384:
261b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA512:
26247645577SJerome Forissier 	case TEE_ALG_HMAC_SM3:
263b0104773SPascal Brand 		if (mode != TEE_MODE_MAC)
264b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
265b0104773SPascal Brand 		req_key_usage = TEE_USAGE_MAC;
266b0104773SPascal Brand 		break;
267b0104773SPascal Brand 
268b0104773SPascal Brand 	default:
269b0104773SPascal Brand 		return TEE_ERROR_NOT_SUPPORTED;
270b0104773SPascal Brand 	}
271b0104773SPascal Brand 
272b66f219bSJens Wiklander 	op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO);
2739b52c538SCedric Chaumont 	if (!op)
274b0104773SPascal Brand 		return TEE_ERROR_OUT_OF_MEMORY;
275b0104773SPascal Brand 
276b0104773SPascal Brand 	op->info.algorithm = algorithm;
277b0104773SPascal Brand 	op->info.operationClass = TEE_ALG_GET_CLASS(algorithm);
2786a2e0a9fSGabor Szekely #ifdef CFG_CRYPTO_RSASSA_NA1
2796a2e0a9fSGabor Szekely 	if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5)
2806a2e0a9fSGabor Szekely 		op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE;
2816a2e0a9fSGabor Szekely #endif
282b0104773SPascal Brand 	op->info.mode = mode;
2832e5e6460SAlbert Schwarzkopf 	op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm);
284b0104773SPascal Brand 	op->info.maxKeySize = maxKeySize;
285b0104773SPascal Brand 	op->info.requiredKeyUsage = req_key_usage;
286b0104773SPascal Brand 	op->info.handleState = handle_state;
287b0104773SPascal Brand 
288b0104773SPascal Brand 	if (block_size > 1) {
289b0104773SPascal Brand 		size_t buffer_size = block_size;
290b0104773SPascal Brand 
291b0104773SPascal Brand 		if (buffer_two_blocks)
292b0104773SPascal Brand 			buffer_size *= 2;
293b0104773SPascal Brand 
2949b52c538SCedric Chaumont 		op->buffer = TEE_Malloc(buffer_size,
2959b52c538SCedric Chaumont 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
296b0104773SPascal Brand 		if (op->buffer == NULL) {
297b0104773SPascal Brand 			res = TEE_ERROR_OUT_OF_MEMORY;
298b66f219bSJens Wiklander 			goto out;
299b0104773SPascal Brand 		}
300b0104773SPascal Brand 	}
301b0104773SPascal Brand 	op->block_size = block_size;
302b0104773SPascal Brand 	op->buffer_two_blocks = buffer_two_blocks;
303b0104773SPascal Brand 
304b0104773SPascal Brand 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
305b0104773SPascal Brand 		uint32_t mks = maxKeySize;
306b0104773SPascal Brand 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
307b0104773SPascal Brand 						       with_private_key);
308b0104773SPascal Brand 
309b0104773SPascal Brand 		/*
310b0104773SPascal Brand 		 * If two keys are expected the max key size is the sum of
311b0104773SPascal Brand 		 * the size of both keys.
312b0104773SPascal Brand 		 */
313b0104773SPascal Brand 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
314b0104773SPascal Brand 			mks /= 2;
315b0104773SPascal Brand 
316b0104773SPascal Brand 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
317b0104773SPascal Brand 		if (res != TEE_SUCCESS)
318b66f219bSJens Wiklander 			goto out;
319b0104773SPascal Brand 
32005304565SCedric Chaumont 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
3219b52c538SCedric Chaumont 			res = TEE_AllocateTransientObject(key_type, mks,
322b0104773SPascal Brand 							  &op->key2);
323b0104773SPascal Brand 			if (res != TEE_SUCCESS)
324b66f219bSJens Wiklander 				goto out;
325b0104773SPascal Brand 		}
326b0104773SPascal Brand 	}
327b0104773SPascal Brand 
3282c028fdeSJerome Forissier 	res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1,
329e86f1266SJens Wiklander 				     (unsigned long)op->key2, &op->state);
330b66f219bSJens Wiklander 	if (res != TEE_SUCCESS)
331b66f219bSJens Wiklander 		goto out;
332b0104773SPascal Brand 
33305304565SCedric Chaumont 	/*
33405304565SCedric Chaumont 	 * Initialize digest operations
33505304565SCedric Chaumont 	 * Other multi-stage operations initialized w/ TEE_xxxInit functions
33605304565SCedric Chaumont 	 * Non-applicable on asymmetric operations
33705304565SCedric Chaumont 	 */
33805304565SCedric Chaumont 	if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) {
3392c028fdeSJerome Forissier 		res = _utee_hash_init(op->state, NULL, 0);
34005304565SCedric Chaumont 		if (res != TEE_SUCCESS)
341b66f219bSJens Wiklander 			goto out;
34205304565SCedric Chaumont 		/* v1.1: flags always set for digest operations */
34305304565SCedric Chaumont 		op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
34405304565SCedric Chaumont 	}
34505304565SCedric Chaumont 
346642a1607SCedric Chaumont 	op->operationState = TEE_OPERATION_STATE_INITIAL;
347642a1607SCedric Chaumont 
348b0104773SPascal Brand 	*operation = op;
349b0104773SPascal Brand 
350b66f219bSJens Wiklander out:
351b66f219bSJens Wiklander 	if (res != TEE_SUCCESS) {
352b66f219bSJens Wiklander 		if (res != TEE_ERROR_OUT_OF_MEMORY &&
3539b52c538SCedric Chaumont 		    res != TEE_ERROR_NOT_SUPPORTED)
354b36311adSJerome Forissier 			TEE_Panic(res);
355b66f219bSJens Wiklander 		if (op) {
356b66f219bSJens Wiklander 			if (op->state) {
357b66f219bSJens Wiklander 				TEE_FreeOperation(op);
358b66f219bSJens Wiklander 			} else {
359b66f219bSJens Wiklander 				TEE_Free(op->buffer);
360b66f219bSJens Wiklander 				TEE_FreeTransientObject(op->key1);
361b66f219bSJens Wiklander 				TEE_FreeTransientObject(op->key2);
362b66f219bSJens Wiklander 				TEE_Free(op);
363b66f219bSJens Wiklander 			}
364b66f219bSJens Wiklander 		}
365b66f219bSJens Wiklander 	}
366b66f219bSJens Wiklander 
367b0104773SPascal Brand 	return res;
368b0104773SPascal Brand }
369b0104773SPascal Brand 
370b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation)
371b0104773SPascal Brand {
372e889e80bSCedric Chaumont 	TEE_Result res;
373e889e80bSCedric Chaumont 
374e889e80bSCedric Chaumont 	if (operation == TEE_HANDLE_NULL)
375e889e80bSCedric Chaumont 		TEE_Panic(0);
376e889e80bSCedric Chaumont 
377b0104773SPascal Brand 	/*
378b0104773SPascal Brand 	 * Note that keys should not be freed here, since they are
379b0104773SPascal Brand 	 * claimed by the operation they will be freed by
380b0104773SPascal Brand 	 * utee_cryp_state_free().
381b0104773SPascal Brand 	 */
3822c028fdeSJerome Forissier 	res = _utee_cryp_state_free(operation->state);
383e889e80bSCedric Chaumont 	if (res != TEE_SUCCESS)
384b36311adSJerome Forissier 		TEE_Panic(res);
385e889e80bSCedric Chaumont 
386b0104773SPascal Brand 	TEE_Free(operation->buffer);
387b0104773SPascal Brand 	TEE_Free(operation);
388b0104773SPascal Brand }
389b0104773SPascal Brand 
390b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation,
391b0104773SPascal Brand 			  TEE_OperationInfo *operationInfo)
392b0104773SPascal Brand {
393b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
394b0104773SPascal Brand 		TEE_Panic(0);
395b0104773SPascal Brand 
3966915bbbbSJens Wiklander 	__utee_check_out_annotation(operationInfo, sizeof(*operationInfo));
397b0104773SPascal Brand 
398b0104773SPascal Brand 	*operationInfo = operation->info;
399bac3a8a7SJens Wiklander 	if (operationInfo->handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
400bac3a8a7SJens Wiklander 		operationInfo->keySize = 0;
401bac3a8a7SJens Wiklander 		operationInfo->requiredKeyUsage = 0;
402bac3a8a7SJens Wiklander 	}
403b0104773SPascal Brand }
404b0104773SPascal Brand 
405ee2f75afSJens Wiklander TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle op,
406ee2f75afSJens Wiklander 					TEE_OperationInfoMultiple *op_info,
407ee2f75afSJens Wiklander 					uint32_t *size)
40805304565SCedric Chaumont {
40905304565SCedric Chaumont 	TEE_Result res = TEE_SUCCESS;
410ee2f75afSJens Wiklander 	TEE_ObjectInfo kinfo = { };
411ee2f75afSJens Wiklander 	size_t max_key_count = 0;
412ee2f75afSJens Wiklander 	bool two_keys = false;
41305304565SCedric Chaumont 
414ee2f75afSJens Wiklander 	if (op == TEE_HANDLE_NULL) {
41505304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
41605304565SCedric Chaumont 		goto out;
41705304565SCedric Chaumont 	}
41805304565SCedric Chaumont 
419ee2f75afSJens Wiklander 	__utee_check_outbuf_annotation(op_info, size);
42005304565SCedric Chaumont 
421ee2f75afSJens Wiklander 	if (*size < sizeof(*op_info)) {
422ee2f75afSJens Wiklander 		res = TEE_ERROR_BAD_PARAMETERS;
423ee2f75afSJens Wiklander 		goto out;
424ee2f75afSJens Wiklander 	}
425ee2f75afSJens Wiklander 	max_key_count = (*size - sizeof(*op_info)) /
42605304565SCedric Chaumont 			sizeof(TEE_OperationInfoKey);
42705304565SCedric Chaumont 
428ee2f75afSJens Wiklander 	TEE_MemFill(op_info, 0, *size);
42905304565SCedric Chaumont 
43005304565SCedric Chaumont 	/* Two keys flag (TEE_ALG_AES_XTS only) */
431ee2f75afSJens Wiklander 	two_keys = op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
432ee2f75afSJens Wiklander 
433ee2f75afSJens Wiklander 	if (op->info.mode == TEE_MODE_DIGEST) {
434ee2f75afSJens Wiklander 		op_info->numberOfKeys = 0;
435ee2f75afSJens Wiklander 	} else if (!two_keys) {
436ee2f75afSJens Wiklander 		if (max_key_count < 1) {
43705304565SCedric Chaumont 			res = TEE_ERROR_SHORT_BUFFER;
43805304565SCedric Chaumont 			goto out;
43905304565SCedric Chaumont 		}
44005304565SCedric Chaumont 
441ee2f75afSJens Wiklander 		res = TEE_GetObjectInfo1(op->key1, &kinfo);
442ee2f75afSJens Wiklander 		/* Key1 is not a valid handle, "can't happen". */
443ee2f75afSJens Wiklander 		if (res)
44405304565SCedric Chaumont 			goto out;
44505304565SCedric Chaumont 
446ee2f75afSJens Wiklander 		op_info->keyInformation[0].keySize = kinfo.keySize;
447ee2f75afSJens Wiklander 		op_info->keyInformation[0].requiredKeyUsage =
448ee2f75afSJens Wiklander 			op->info.requiredKeyUsage;
449ee2f75afSJens Wiklander 		op_info->numberOfKeys = 1;
450ee2f75afSJens Wiklander 	} else {
451ee2f75afSJens Wiklander 		if (max_key_count < 2) {
452ee2f75afSJens Wiklander 			res = TEE_ERROR_SHORT_BUFFER;
45305304565SCedric Chaumont 			goto out;
45405304565SCedric Chaumont 		}
45505304565SCedric Chaumont 
456ee2f75afSJens Wiklander 		res = TEE_GetObjectInfo1(op->key1, &kinfo);
457ee2f75afSJens Wiklander 		/* Key1 is not a valid handle, "can't happen". */
458ee2f75afSJens Wiklander 		if (res)
459ee2f75afSJens Wiklander 			goto out;
460ee2f75afSJens Wiklander 
461ee2f75afSJens Wiklander 		op_info->keyInformation[0].keySize = kinfo.keySize;
462ee2f75afSJens Wiklander 		op_info->keyInformation[0].requiredKeyUsage =
463ee2f75afSJens Wiklander 			op->info.requiredKeyUsage;
464ee2f75afSJens Wiklander 
465ee2f75afSJens Wiklander 		res = TEE_GetObjectInfo1(op->key2, &kinfo);
466ee2f75afSJens Wiklander 		/* Key2 is not a valid handle, "can't happen". */
467ee2f75afSJens Wiklander 		if (res)
468ee2f75afSJens Wiklander 			goto out;
469ee2f75afSJens Wiklander 
470ee2f75afSJens Wiklander 		op_info->keyInformation[1].keySize = kinfo.keySize;
471ee2f75afSJens Wiklander 		op_info->keyInformation[1].requiredKeyUsage =
472ee2f75afSJens Wiklander 			op->info.requiredKeyUsage;
473ee2f75afSJens Wiklander 
474ee2f75afSJens Wiklander 		op_info->numberOfKeys = 2;
47505304565SCedric Chaumont 	}
47605304565SCedric Chaumont 
477ee2f75afSJens Wiklander 	op_info->algorithm = op->info.algorithm;
478ee2f75afSJens Wiklander 	op_info->operationClass = op->info.operationClass;
479ee2f75afSJens Wiklander 	op_info->mode = op->info.mode;
480ee2f75afSJens Wiklander 	op_info->digestLength = op->info.digestLength;
481ee2f75afSJens Wiklander 	op_info->maxKeySize = op->info.maxKeySize;
482ee2f75afSJens Wiklander 	op_info->handleState = op->info.handleState;
483ee2f75afSJens Wiklander 	op_info->operationState = op->operationState;
48405304565SCedric Chaumont 
48505304565SCedric Chaumont out:
48605304565SCedric Chaumont 	if (res != TEE_SUCCESS &&
48705304565SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
488b36311adSJerome Forissier 		TEE_Panic(res);
48905304565SCedric Chaumont 
49005304565SCedric Chaumont 	return res;
49105304565SCedric Chaumont }
49205304565SCedric Chaumont 
493b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation)
494b0104773SPascal Brand {
495b0104773SPascal Brand 	TEE_Result res;
496b0104773SPascal Brand 
497b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
498b0104773SPascal Brand 		TEE_Panic(0);
499bf80076aSCedric Chaumont 
500642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET))
501bf80076aSCedric Chaumont 			TEE_Panic(0);
502bf80076aSCedric Chaumont 
503642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
504642a1607SCedric Chaumont 
505b0104773SPascal Brand 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
5062c028fdeSJerome Forissier 		res = _utee_hash_init(operation->state, NULL, 0);
507b0104773SPascal Brand 		if (res != TEE_SUCCESS)
508b0104773SPascal Brand 			TEE_Panic(res);
50905304565SCedric Chaumont 		operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
51005304565SCedric Chaumont 	} else {
511b0104773SPascal Brand 		operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
512b0104773SPascal Brand 	}
51305304565SCedric Chaumont }
514b0104773SPascal Brand 
515b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
516b0104773SPascal Brand 			       TEE_ObjectHandle key)
517b0104773SPascal Brand {
5187583c59eSCedric Chaumont 	TEE_Result res;
519b0104773SPascal Brand 	uint32_t key_size = 0;
520b0104773SPascal Brand 	TEE_ObjectInfo key_info;
521b0104773SPascal Brand 
522a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
523a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
524a57c1e2eSCedric Chaumont 		goto out;
525a57c1e2eSCedric Chaumont 	}
526a57c1e2eSCedric Chaumont 
527642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
528642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
529642a1607SCedric Chaumont 		goto out;
530642a1607SCedric Chaumont 	}
531642a1607SCedric Chaumont 
532a57c1e2eSCedric Chaumont 	if (key == TEE_HANDLE_NULL) {
533a57c1e2eSCedric Chaumont 		/* Operation key cleared */
534a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key1);
5356c4ea258SJens Wiklander 		operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
5366c4ea258SJens Wiklander 		return TEE_SUCCESS;
537a57c1e2eSCedric Chaumont 	}
538a57c1e2eSCedric Chaumont 
539a57c1e2eSCedric Chaumont 	/* No key for digest operation */
540a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
541a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
542a57c1e2eSCedric Chaumont 		goto out;
543a57c1e2eSCedric Chaumont 	}
544a57c1e2eSCedric Chaumont 
545a57c1e2eSCedric Chaumont 	/* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */
546a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
547a57c1e2eSCedric Chaumont 	    0) {
548a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
549a57c1e2eSCedric Chaumont 		goto out;
550a57c1e2eSCedric Chaumont 	}
551a57c1e2eSCedric Chaumont 
5527583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key, &key_info);
553a57c1e2eSCedric Chaumont 	/* Key is not a valid handle */
5547583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
555a57c1e2eSCedric Chaumont 		goto out;
5567583c59eSCedric Chaumont 
557b0104773SPascal Brand 	/* Supplied key has to meet required usage */
558b0104773SPascal Brand 	if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
559b0104773SPascal Brand 	    operation->info.requiredKeyUsage) {
560a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
561a57c1e2eSCedric Chaumont 		goto out;
562b0104773SPascal Brand 	}
563b0104773SPascal Brand 
564a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info.keySize) {
565a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
566a57c1e2eSCedric Chaumont 		goto out;
567a57c1e2eSCedric Chaumont 	}
568b0104773SPascal Brand 
5697583c59eSCedric Chaumont 	key_size = key_info.keySize;
570b0104773SPascal Brand 
571b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
572b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
573b0104773SPascal Brand 
5747583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key);
5757583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
576a57c1e2eSCedric Chaumont 		goto out;
5777583c59eSCedric Chaumont 
578b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
579b0104773SPascal Brand 
580b0104773SPascal Brand 	operation->info.keySize = key_size;
581b0104773SPascal Brand 
5827583c59eSCedric Chaumont out:
583a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
584a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
585a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
586b36311adSJerome Forissier 		TEE_Panic(res);
587a57c1e2eSCedric Chaumont 
588a57c1e2eSCedric Chaumont 	return res;
589b0104773SPascal Brand }
590b0104773SPascal Brand 
591b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
592b0104773SPascal Brand 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
593b0104773SPascal Brand {
5947583c59eSCedric Chaumont 	TEE_Result res;
595b0104773SPascal Brand 	uint32_t key_size = 0;
596b0104773SPascal Brand 	TEE_ObjectInfo key_info1;
597b0104773SPascal Brand 	TEE_ObjectInfo key_info2;
598b0104773SPascal Brand 
599a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
600a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
601a57c1e2eSCedric Chaumont 		goto out;
602a57c1e2eSCedric Chaumont 	}
603a57c1e2eSCedric Chaumont 
604642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
605642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
606642a1607SCedric Chaumont 		goto out;
607642a1607SCedric Chaumont 	}
608642a1607SCedric Chaumont 
609a57c1e2eSCedric Chaumont 	/*
610a57c1e2eSCedric Chaumont 	 * Key1/Key2 and/or are not initialized and
611a57c1e2eSCedric Chaumont 	 * Either both keys are NULL or both are not NULL
612a57c1e2eSCedric Chaumont 	 */
6136c4ea258SJens Wiklander 	if (!key1 && !key2) {
6146c4ea258SJens Wiklander 		/* Clear the keys */
615a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key1);
616a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key2);
6176c4ea258SJens Wiklander 		operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
6186c4ea258SJens Wiklander 		return TEE_SUCCESS;
6196c4ea258SJens Wiklander 	} else if (!key1 || !key2) {
6206c4ea258SJens Wiklander 		/* Both keys are obviously not valid. */
621a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
622a57c1e2eSCedric Chaumont 		goto out;
623a57c1e2eSCedric Chaumont 	}
624a57c1e2eSCedric Chaumont 
625a57c1e2eSCedric Chaumont 	/* No key for digest operation */
626a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
627a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
628a57c1e2eSCedric Chaumont 		goto out;
629a57c1e2eSCedric Chaumont 	}
630a57c1e2eSCedric Chaumont 
6315b385b3fSJerome Forissier 	/* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */
632a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
633a57c1e2eSCedric Chaumont 	    0) {
634a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
635a57c1e2eSCedric Chaumont 		goto out;
636a57c1e2eSCedric Chaumont 	}
637a57c1e2eSCedric Chaumont 
6387583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key1, &key_info1);
639a57c1e2eSCedric Chaumont 	/* Key1 is not a valid handle */
6407583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
641a57c1e2eSCedric Chaumont 		goto out;
6427583c59eSCedric Chaumont 
643b0104773SPascal Brand 	/* Supplied key has to meet required usage */
644b0104773SPascal Brand 	if ((key_info1.objectUsage & operation->info.
645b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
646a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
647a57c1e2eSCedric Chaumont 		goto out;
648b0104773SPascal Brand 	}
649b0104773SPascal Brand 
6507583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key2, &key_info2);
651a57c1e2eSCedric Chaumont 	/* Key2 is not a valid handle */
6527583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
6537583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
6547583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
655a57c1e2eSCedric Chaumont 		goto out;
6567583c59eSCedric Chaumont 	}
6577583c59eSCedric Chaumont 
658b0104773SPascal Brand 	/* Supplied key has to meet required usage */
659b0104773SPascal Brand 	if ((key_info2.objectUsage & operation->info.
660b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
661a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
662a57c1e2eSCedric Chaumont 		goto out;
663b0104773SPascal Brand 	}
664b0104773SPascal Brand 
665b0104773SPascal Brand 	/*
6665b385b3fSJerome Forissier 	 * All the multi key algorithm currently supported requires the keys to
6675b385b3fSJerome Forissier 	 * be of equal size.
668b0104773SPascal Brand 	 */
6695b385b3fSJerome Forissier 	if (key_info1.keySize != key_info2.keySize) {
670a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
671a57c1e2eSCedric Chaumont 		goto out;
672b0104773SPascal Brand 
673a57c1e2eSCedric Chaumont 	}
674a57c1e2eSCedric Chaumont 
675a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info1.keySize) {
676a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
677a57c1e2eSCedric Chaumont 		goto out;
678a57c1e2eSCedric Chaumont 	}
679b0104773SPascal Brand 
680b0104773SPascal Brand 	/*
681b0104773SPascal Brand 	 * Odd that only the size of one key should be reported while
682b0104773SPascal Brand 	 * size of two key are used when allocating the operation.
683b0104773SPascal Brand 	 */
6847583c59eSCedric Chaumont 	key_size = key_info1.keySize;
685b0104773SPascal Brand 
686b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
687b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key2);
688b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
689b0104773SPascal Brand 
6907583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key1);
6917583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
692a57c1e2eSCedric Chaumont 		goto out;
6937583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key2, key2);
6947583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
6957583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
6967583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
697a57c1e2eSCedric Chaumont 		goto out;
6987583c59eSCedric Chaumont 	}
6997583c59eSCedric Chaumont 
700b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
701b0104773SPascal Brand 
702b0104773SPascal Brand 	operation->info.keySize = key_size;
703b0104773SPascal Brand 
7047583c59eSCedric Chaumont out:
705a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
706a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
707a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT_2 &&
708a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE &&
709a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
710b36311adSJerome Forissier 		TEE_Panic(res);
711a57c1e2eSCedric Chaumont 
712a57c1e2eSCedric Chaumont 	return res;
713b0104773SPascal Brand }
714b0104773SPascal Brand 
715b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
716b0104773SPascal Brand {
717b0104773SPascal Brand 	TEE_Result res;
718b0104773SPascal Brand 
719b0104773SPascal Brand 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
720b0104773SPascal Brand 		TEE_Panic(0);
721b0104773SPascal Brand 	if (dst_op->info.algorithm != src_op->info.algorithm)
722b0104773SPascal Brand 		TEE_Panic(0);
7238734de30SJens Wiklander 	if (dst_op->info.mode != src_op->info.mode)
7248734de30SJens Wiklander 		TEE_Panic(0);
725b0104773SPascal Brand 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
726b0104773SPascal Brand 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
727b0104773SPascal Brand 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
728b0104773SPascal Brand 
729b0104773SPascal Brand 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
730b0104773SPascal Brand 			key1 = src_op->key1;
731b0104773SPascal Brand 			key2 = src_op->key2;
732b0104773SPascal Brand 		}
733b0104773SPascal Brand 
734b0104773SPascal Brand 		if ((src_op->info.handleState &
735b0104773SPascal Brand 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
736b0104773SPascal Brand 			TEE_SetOperationKey(dst_op, key1);
737b0104773SPascal Brand 		} else {
738b0104773SPascal Brand 			TEE_SetOperationKey2(dst_op, key1, key2);
739b0104773SPascal Brand 		}
740b0104773SPascal Brand 	}
741b0104773SPascal Brand 	dst_op->info.handleState = src_op->info.handleState;
742b0104773SPascal Brand 	dst_op->info.keySize = src_op->info.keySize;
7438e07702eSJens Wiklander 	dst_op->info.digestLength = src_op->info.digestLength;
744642a1607SCedric Chaumont 	dst_op->operationState = src_op->operationState;
745b0104773SPascal Brand 
746b0104773SPascal Brand 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
747b0104773SPascal Brand 	    dst_op->block_size != src_op->block_size)
748b0104773SPascal Brand 		TEE_Panic(0);
749b0104773SPascal Brand 
750b0104773SPascal Brand 	if (dst_op->buffer != NULL) {
751b0104773SPascal Brand 		if (src_op->buffer == NULL)
752b0104773SPascal Brand 			TEE_Panic(0);
753b0104773SPascal Brand 
754b0104773SPascal Brand 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
755b0104773SPascal Brand 		dst_op->buffer_offs = src_op->buffer_offs;
756b0104773SPascal Brand 	} else if (src_op->buffer != NULL) {
757b0104773SPascal Brand 		TEE_Panic(0);
758b0104773SPascal Brand 	}
759b0104773SPascal Brand 
7602c028fdeSJerome Forissier 	res = _utee_cryp_state_copy(dst_op->state, src_op->state);
761b0104773SPascal Brand 	if (res != TEE_SUCCESS)
762b0104773SPascal Brand 		TEE_Panic(res);
763b0104773SPascal Brand }
764b0104773SPascal Brand 
765b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */
766b0104773SPascal Brand 
7678f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV,
7686d15db08SJerome Forissier 				uint32_t IVLen)
7696d15db08SJerome Forissier {
7706d15db08SJerome Forissier 	TEE_Result res;
7716d15db08SJerome Forissier 
7726d15db08SJerome Forissier 	/*
7736d15db08SJerome Forissier 	 * Note : IV and IVLen are never used in current implementation
7746d15db08SJerome Forissier 	 * This is why coherent values of IV and IVLen are not checked
7756d15db08SJerome Forissier 	 */
7762c028fdeSJerome Forissier 	res = _utee_hash_init(operation->state, IV, IVLen);
7776d15db08SJerome Forissier 	if (res != TEE_SUCCESS)
7786d15db08SJerome Forissier 		TEE_Panic(res);
7796d15db08SJerome Forissier 	operation->buffer_offs = 0;
7806d15db08SJerome Forissier 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
7816d15db08SJerome Forissier }
7826d15db08SJerome Forissier 
783b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation,
7848f07fe6fSJerome Forissier 		      const void *chunk, uint32_t chunkSize)
785b0104773SPascal Brand {
78673d6c3baSJoakim Bech 	TEE_Result res = TEE_ERROR_GENERIC;
787b0104773SPascal Brand 
78873d6c3baSJoakim Bech 	if (operation == TEE_HANDLE_NULL ||
78973d6c3baSJoakim Bech 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
790b0104773SPascal Brand 		TEE_Panic(0);
79173d6c3baSJoakim Bech 
792642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
793642a1607SCedric Chaumont 
7942c028fdeSJerome Forissier 	res = _utee_hash_update(operation->state, chunk, chunkSize);
795b0104773SPascal Brand 	if (res != TEE_SUCCESS)
796b0104773SPascal Brand 		TEE_Panic(res);
797b0104773SPascal Brand }
798b0104773SPascal Brand 
7998f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
80079a3c601SCedric Chaumont 			     uint32_t chunkLen, void *hash, uint32_t *hashLen)
801b0104773SPascal Brand {
80287c2f6b6SCedric Chaumont 	TEE_Result res;
803e86f1266SJens Wiklander 	uint64_t hl;
80487c2f6b6SCedric Chaumont 
80587c2f6b6SCedric Chaumont 	if ((operation == TEE_HANDLE_NULL) ||
80687c2f6b6SCedric Chaumont 	    (!chunk && chunkLen) ||
80787c2f6b6SCedric Chaumont 	    (operation->info.operationClass != TEE_OPERATION_DIGEST)) {
80887c2f6b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
80987c2f6b6SCedric Chaumont 		goto out;
81087c2f6b6SCedric Chaumont 	}
8116915bbbbSJens Wiklander 	__utee_check_inout_annotation(hashLen, sizeof(*hashLen));
81287c2f6b6SCedric Chaumont 
813e86f1266SJens Wiklander 	hl = *hashLen;
8142c028fdeSJerome Forissier 	res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl);
815e86f1266SJens Wiklander 	*hashLen = hl;
8166d15db08SJerome Forissier 	if (res != TEE_SUCCESS)
8176d15db08SJerome Forissier 		goto out;
8186d15db08SJerome Forissier 
8196d15db08SJerome Forissier 	/* Reset operation state */
8206d15db08SJerome Forissier 	init_hash_operation(operation, NULL, 0);
82187c2f6b6SCedric Chaumont 
822642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
823642a1607SCedric Chaumont 
82487c2f6b6SCedric Chaumont out:
82587c2f6b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
82687c2f6b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
827b36311adSJerome Forissier 		TEE_Panic(res);
82873d6c3baSJoakim Bech 
82987c2f6b6SCedric Chaumont 	return res;
830b0104773SPascal Brand }
831b0104773SPascal Brand 
832b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */
833b0104773SPascal Brand 
8348f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV,
8358f07fe6fSJerome Forissier 		    uint32_t IVLen)
836b0104773SPascal Brand {
837b0104773SPascal Brand 	TEE_Result res;
838b0104773SPascal Brand 
839b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
840b0104773SPascal Brand 		TEE_Panic(0);
841642a1607SCedric Chaumont 
842b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
843b0104773SPascal Brand 		TEE_Panic(0);
844642a1607SCedric Chaumont 
845642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
846642a1607SCedric Chaumont 	    !(operation->key1))
847642a1607SCedric Chaumont 		TEE_Panic(0);
848642a1607SCedric Chaumont 
849642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
850642a1607SCedric Chaumont 		TEE_ResetOperation(operation);
851642a1607SCedric Chaumont 
852*ad7aa2a5SSadiq Hussain 	if (IV && IVLen) {
853*ad7aa2a5SSadiq Hussain 		if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
854*ad7aa2a5SSadiq Hussain 		    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
855*ad7aa2a5SSadiq Hussain 		    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
856*ad7aa2a5SSadiq Hussain 		    operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD)
857*ad7aa2a5SSadiq Hussain 			TEE_Panic(0);
858*ad7aa2a5SSadiq Hussain 	}
859*ad7aa2a5SSadiq Hussain 
860642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
861642a1607SCedric Chaumont 
8622c028fdeSJerome Forissier 	res = _utee_cipher_init(operation->state, IV, IVLen);
863b0104773SPascal Brand 	if (res != TEE_SUCCESS)
864b0104773SPascal Brand 		TEE_Panic(res);
865642a1607SCedric Chaumont 
866b0104773SPascal Brand 	operation->buffer_offs = 0;
867b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
868b0104773SPascal Brand }
869b0104773SPascal Brand 
870b0104773SPascal Brand static TEE_Result tee_buffer_update(
871b0104773SPascal Brand 		TEE_OperationHandle op,
872e86f1266SJens Wiklander 		TEE_Result(*update_func)(unsigned long state, const void *src,
873e86f1266SJens Wiklander 				size_t slen, void *dst, uint64_t *dlen),
874b0104773SPascal Brand 		const void *src_data, size_t src_len,
875e86f1266SJens Wiklander 		void *dest_data, uint64_t *dest_len)
876b0104773SPascal Brand {
877b0104773SPascal Brand 	TEE_Result res;
878b0104773SPascal Brand 	const uint8_t *src = src_data;
879b0104773SPascal Brand 	size_t slen = src_len;
880b0104773SPascal Brand 	uint8_t *dst = dest_data;
881b0104773SPascal Brand 	size_t dlen = *dest_len;
882b0104773SPascal Brand 	size_t acc_dlen = 0;
883e86f1266SJens Wiklander 	uint64_t tmp_dlen;
884b0104773SPascal Brand 	size_t l;
885b0104773SPascal Brand 	size_t buffer_size;
886d3588802SPascal Brand 	size_t buffer_left;
887b0104773SPascal Brand 
888090268f5SJens Wiklander 	if (!src) {
889090268f5SJens Wiklander 		if (slen)
890090268f5SJens Wiklander 			TEE_Panic(0);
891090268f5SJens Wiklander 		goto out;
892090268f5SJens Wiklander 	}
893090268f5SJens Wiklander 
894d3588802SPascal Brand 	if (op->buffer_two_blocks) {
895b0104773SPascal Brand 		buffer_size = op->block_size * 2;
896d3588802SPascal Brand 		buffer_left = 1;
897d3588802SPascal Brand 	} else {
898b0104773SPascal Brand 		buffer_size = op->block_size;
899d3588802SPascal Brand 		buffer_left = 0;
900d3588802SPascal Brand 	}
901b0104773SPascal Brand 
902b0104773SPascal Brand 	if (op->buffer_offs > 0) {
903b0104773SPascal Brand 		/* Fill up complete block */
904b0104773SPascal Brand 		if (op->buffer_offs < op->block_size)
905b0104773SPascal Brand 			l = MIN(slen, op->block_size - op->buffer_offs);
906b0104773SPascal Brand 		else
907b0104773SPascal Brand 			l = MIN(slen, buffer_size - op->buffer_offs);
908b0104773SPascal Brand 		memcpy(op->buffer + op->buffer_offs, src, l);
909b0104773SPascal Brand 		op->buffer_offs += l;
910b0104773SPascal Brand 		src += l;
911b0104773SPascal Brand 		slen -= l;
912b0104773SPascal Brand 		if ((op->buffer_offs % op->block_size) != 0)
913b0104773SPascal Brand 			goto out;	/* Nothing left to do */
914b0104773SPascal Brand 	}
915b0104773SPascal Brand 
916b0104773SPascal Brand 	/* If we can feed from buffer */
917d3588802SPascal Brand 	if ((op->buffer_offs > 0) &&
918d3588802SPascal Brand 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
9192ff3fdbbSPascal Brand 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
920b0104773SPascal Brand 				op->block_size);
921b0104773SPascal Brand 		l = MIN(op->buffer_offs, l);
922b0104773SPascal Brand 		tmp_dlen = dlen;
923b0104773SPascal Brand 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
924b0104773SPascal Brand 		if (res != TEE_SUCCESS)
925b0104773SPascal Brand 			TEE_Panic(res);
926b0104773SPascal Brand 		dst += tmp_dlen;
927b0104773SPascal Brand 		dlen -= tmp_dlen;
928b0104773SPascal Brand 		acc_dlen += tmp_dlen;
929b0104773SPascal Brand 		op->buffer_offs -= l;
930b0104773SPascal Brand 		if (op->buffer_offs > 0) {
931b0104773SPascal Brand 			/*
932b0104773SPascal Brand 			 * Slen is small enough to be contained in rest buffer.
933b0104773SPascal Brand 			 */
934b0104773SPascal Brand 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
935b0104773SPascal Brand 			memcpy(op->buffer + op->buffer_offs, src, slen);
936b0104773SPascal Brand 			op->buffer_offs += slen;
937b0104773SPascal Brand 			goto out;	/* Nothing left to do */
938b0104773SPascal Brand 		}
939b0104773SPascal Brand 	}
940b0104773SPascal Brand 
941d3588802SPascal Brand 	if (slen >= (buffer_size + buffer_left)) {
942b0104773SPascal Brand 		/* Buffer is empty, feed as much as possible from src */
943bf7a587fSJerome Forissier 		if (op->info.algorithm == TEE_ALG_AES_CTS)
944b1ecda78SJerome Forissier 			l = ROUNDUP(slen - buffer_size, op->block_size);
945bf7a587fSJerome Forissier 		else
946bf7a587fSJerome Forissier 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
947b0104773SPascal Brand 
948b0104773SPascal Brand 		tmp_dlen = dlen;
949b0104773SPascal Brand 		res = update_func(op->state, src, l, dst, &tmp_dlen);
950b0104773SPascal Brand 		if (res != TEE_SUCCESS)
951b0104773SPascal Brand 			TEE_Panic(res);
952b0104773SPascal Brand 		src += l;
953b0104773SPascal Brand 		slen -= l;
954b0104773SPascal Brand 		dst += tmp_dlen;
955b0104773SPascal Brand 		dlen -= tmp_dlen;
956b0104773SPascal Brand 		acc_dlen += tmp_dlen;
957b0104773SPascal Brand 	}
958b0104773SPascal Brand 
959b0104773SPascal Brand 	/* Slen is small enough to be contained in buffer. */
960b0104773SPascal Brand 	memcpy(op->buffer + op->buffer_offs, src, slen);
961b0104773SPascal Brand 	op->buffer_offs += slen;
962b0104773SPascal Brand 
963b0104773SPascal Brand out:
964b0104773SPascal Brand 	*dest_len = acc_dlen;
965b0104773SPascal Brand 	return TEE_SUCCESS;
966b0104773SPascal Brand }
967b0104773SPascal Brand 
9688f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData,
96979a3c601SCedric Chaumont 			    uint32_t srcLen, void *destData, uint32_t *destLen)
970b0104773SPascal Brand {
971dea1f2b6SCedric Chaumont 	TEE_Result res;
972b0104773SPascal Brand 	size_t req_dlen;
973e86f1266SJens Wiklander 	uint64_t dl;
974b0104773SPascal Brand 
9756915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
976dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
977dea1f2b6SCedric Chaumont 		goto out;
978dea1f2b6SCedric Chaumont 	}
9796915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
980dea1f2b6SCedric Chaumont 
981642a1607SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
982dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
983dea1f2b6SCedric Chaumont 		goto out;
984dea1f2b6SCedric Chaumont 	}
985dea1f2b6SCedric Chaumont 
986642a1607SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
987642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
988642a1607SCedric Chaumont 		goto out;
989642a1607SCedric Chaumont 	}
990642a1607SCedric Chaumont 
991642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
992dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
993dea1f2b6SCedric Chaumont 		goto out;
994dea1f2b6SCedric Chaumont 	}
995b0104773SPascal Brand 
996e32c5ddfSJerome Forissier 	if (!srcData && !srcLen) {
997090268f5SJens Wiklander 		*destLen = 0;
998e32c5ddfSJerome Forissier 		res = TEE_SUCCESS;
999e32c5ddfSJerome Forissier 		goto out;
1000e32c5ddfSJerome Forissier 	}
1001e32c5ddfSJerome Forissier 
1002b0104773SPascal Brand 	/* Calculate required dlen */
100357aabac5SBogdan Liulko 	if (operation->block_size > 1) {
100457aabac5SBogdan Liulko 		req_dlen = ((operation->buffer_offs + srcLen) /
100557aabac5SBogdan Liulko 			    operation->block_size) * operation->block_size;
100657aabac5SBogdan Liulko 	} else {
100757aabac5SBogdan Liulko 		req_dlen = srcLen;
100857aabac5SBogdan Liulko 	}
1009642a1607SCedric Chaumont 	if (operation->buffer_two_blocks) {
1010642a1607SCedric Chaumont 		if (req_dlen > operation->block_size * 2)
1011642a1607SCedric Chaumont 			req_dlen -= operation->block_size * 2;
1012b0104773SPascal Brand 		else
1013b0104773SPascal Brand 			req_dlen = 0;
1014b0104773SPascal Brand 	}
1015b0104773SPascal Brand 	/*
1016b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1017b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1018b0104773SPascal Brand 	 * can't restore sync with this API.
1019b0104773SPascal Brand 	 */
1020b0104773SPascal Brand 	if (*destLen < req_dlen) {
1021b0104773SPascal Brand 		*destLen = req_dlen;
1022dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1023dea1f2b6SCedric Chaumont 		goto out;
1024b0104773SPascal Brand 	}
1025b0104773SPascal Brand 
1026e86f1266SJens Wiklander 	dl = *destLen;
102757aabac5SBogdan Liulko 	if (operation->block_size > 1) {
10282c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_cipher_update, srcData,
102957aabac5SBogdan Liulko 					srcLen, destData, &dl);
103057aabac5SBogdan Liulko 	} else {
103157aabac5SBogdan Liulko 		if (srcLen > 0) {
10322c028fdeSJerome Forissier 			res = _utee_cipher_update(operation->state, srcData,
103357aabac5SBogdan Liulko 						  srcLen, destData, &dl);
103457aabac5SBogdan Liulko 		} else {
103557aabac5SBogdan Liulko 			res = TEE_SUCCESS;
103657aabac5SBogdan Liulko 			dl = 0;
103757aabac5SBogdan Liulko 		}
103857aabac5SBogdan Liulko 	}
1039e86f1266SJens Wiklander 	*destLen = dl;
1040b0104773SPascal Brand 
1041dea1f2b6SCedric Chaumont out:
1042dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1043dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1044b36311adSJerome Forissier 		TEE_Panic(res);
1045dea1f2b6SCedric Chaumont 
1046dea1f2b6SCedric Chaumont 	return res;
1047b0104773SPascal Brand }
1048b0104773SPascal Brand 
1049642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation,
10508f07fe6fSJerome Forissier 			     const void *srcData, uint32_t srcLen,
10518f07fe6fSJerome Forissier 			     void *destData, uint32_t *destLen)
1052b0104773SPascal Brand {
10536915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1054b0104773SPascal Brand 	uint8_t *dst = destData;
1055b0104773SPascal Brand 	size_t acc_dlen = 0;
10566915bbbbSJens Wiklander 	uint64_t tmp_dlen = 0;
10576915bbbbSJens Wiklander 	size_t req_dlen = 0;
1058b0104773SPascal Brand 
10596915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1060dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1061dea1f2b6SCedric Chaumont 		goto out;
1062dea1f2b6SCedric Chaumont 	}
10636915bbbbSJens Wiklander 	if (destLen)
10646915bbbbSJens Wiklander 		__utee_check_inout_annotation(destLen, sizeof(*destLen));
1065dea1f2b6SCedric Chaumont 
1066642a1607SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1067dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1068dea1f2b6SCedric Chaumont 		goto out;
1069dea1f2b6SCedric Chaumont 	}
1070dea1f2b6SCedric Chaumont 
1071642a1607SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1072642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1073642a1607SCedric Chaumont 		goto out;
1074642a1607SCedric Chaumont 	}
1075642a1607SCedric Chaumont 
1076642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1077dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1078dea1f2b6SCedric Chaumont 		goto out;
1079dea1f2b6SCedric Chaumont 	}
1080b0104773SPascal Brand 
1081b0104773SPascal Brand 	/*
1082b0104773SPascal Brand 	 * Check that the final block doesn't require padding for those
1083b0104773SPascal Brand 	 * algorithms that requires client to supply padding.
1084b0104773SPascal Brand 	 */
1085642a1607SCedric Chaumont 	if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
1086642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1087642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
1088642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1089642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
1090ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD ||
1091ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD ||
1092ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) {
1093642a1607SCedric Chaumont 		if (((operation->buffer_offs + srcLen) % operation->block_size)
1094642a1607SCedric Chaumont 		    != 0) {
1095dea1f2b6SCedric Chaumont 			res = TEE_ERROR_BAD_PARAMETERS;
1096dea1f2b6SCedric Chaumont 			goto out;
1097dea1f2b6SCedric Chaumont 		}
1098b0104773SPascal Brand 	}
1099b0104773SPascal Brand 
1100b0104773SPascal Brand 	/*
1101b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1102b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1103b0104773SPascal Brand 	 * can't restore sync with this API.
1104b0104773SPascal Brand 	 */
110557aabac5SBogdan Liulko 	if (operation->block_size > 1) {
1106642a1607SCedric Chaumont 		req_dlen = operation->buffer_offs + srcLen;
110757aabac5SBogdan Liulko 	} else {
110857aabac5SBogdan Liulko 		req_dlen = srcLen;
110957aabac5SBogdan Liulko 	}
11106915bbbbSJens Wiklander 	if (destLen)
11116915bbbbSJens Wiklander 		tmp_dlen = *destLen;
11126915bbbbSJens Wiklander 	if (tmp_dlen < req_dlen) {
11136915bbbbSJens Wiklander 		if (destLen)
1114b0104773SPascal Brand 			*destLen = req_dlen;
1115dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1116dea1f2b6SCedric Chaumont 		goto out;
1117b0104773SPascal Brand 	}
1118b0104773SPascal Brand 
111957aabac5SBogdan Liulko 	if (operation->block_size > 1) {
1120dea9063eSJens Wiklander 		if (srcLen) {
11212c028fdeSJerome Forissier 			res = tee_buffer_update(operation, _utee_cipher_update,
1122dea9063eSJens Wiklander 						srcData, srcLen, dst,
1123dea9063eSJens Wiklander 						&tmp_dlen);
1124dea1f2b6SCedric Chaumont 			if (res != TEE_SUCCESS)
1125dea1f2b6SCedric Chaumont 				goto out;
1126dea1f2b6SCedric Chaumont 
1127b0104773SPascal Brand 			dst += tmp_dlen;
1128b0104773SPascal Brand 			acc_dlen += tmp_dlen;
1129b0104773SPascal Brand 
1130b0104773SPascal Brand 			tmp_dlen = *destLen - acc_dlen;
1131dea9063eSJens Wiklander 		}
11322c028fdeSJerome Forissier 		res = _utee_cipher_final(operation->state, operation->buffer,
11332c028fdeSJerome Forissier 					 operation->buffer_offs, dst,
11342c028fdeSJerome Forissier 					 &tmp_dlen);
113557aabac5SBogdan Liulko 	} else {
11362c028fdeSJerome Forissier 		res = _utee_cipher_final(operation->state, srcData, srcLen, dst,
11372c028fdeSJerome Forissier 					 &tmp_dlen);
113857aabac5SBogdan Liulko 	}
1139b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1140dea1f2b6SCedric Chaumont 		goto out;
1141dea1f2b6SCedric Chaumont 
1142b0104773SPascal Brand 	acc_dlen += tmp_dlen;
11436915bbbbSJens Wiklander 	if (destLen)
1144b0104773SPascal Brand 		*destLen = acc_dlen;
1145dea1f2b6SCedric Chaumont 
1146642a1607SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1147642a1607SCedric Chaumont 
1148642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1149642a1607SCedric Chaumont 
1150dea1f2b6SCedric Chaumont out:
1151dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1152dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1153b36311adSJerome Forissier 		TEE_Panic(res);
1154dea1f2b6SCedric Chaumont 
1155dea1f2b6SCedric Chaumont 	return res;
1156b0104773SPascal Brand }
1157b0104773SPascal Brand 
1158b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */
1159b0104773SPascal Brand 
11608f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
1161b0104773SPascal Brand {
1162b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
1163b0104773SPascal Brand 		TEE_Panic(0);
1164642a1607SCedric Chaumont 
1165b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1166b0104773SPascal Brand 		TEE_Panic(0);
1167642a1607SCedric Chaumont 
1168642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1169642a1607SCedric Chaumont 	    !(operation->key1))
1170642a1607SCedric Chaumont 		TEE_Panic(0);
1171642a1607SCedric Chaumont 
1172642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1173642a1607SCedric Chaumont 		TEE_ResetOperation(operation);
1174642a1607SCedric Chaumont 
1175642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1176642a1607SCedric Chaumont 
11776d15db08SJerome Forissier 	init_hash_operation(operation, IV, IVLen);
1178b0104773SPascal Brand }
1179b0104773SPascal Brand 
11808f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk,
118128e0efc6SCedric Chaumont 		   uint32_t chunkSize)
1182b0104773SPascal Brand {
1183b0104773SPascal Brand 	TEE_Result res;
1184b0104773SPascal Brand 
118528e0efc6SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
1186b0104773SPascal Brand 		TEE_Panic(0);
1187642a1607SCedric Chaumont 
118828e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1189b0104773SPascal Brand 		TEE_Panic(0);
1190642a1607SCedric Chaumont 
119128e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1192b0104773SPascal Brand 		TEE_Panic(0);
1193b0104773SPascal Brand 
1194642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE)
1195642a1607SCedric Chaumont 		TEE_Panic(0);
1196642a1607SCedric Chaumont 
11972c028fdeSJerome Forissier 	res = _utee_hash_update(operation->state, chunk, chunkSize);
1198b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1199b0104773SPascal Brand 		TEE_Panic(res);
1200b0104773SPascal Brand }
1201b0104773SPascal Brand 
120228e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
12038f07fe6fSJerome Forissier 			       const void *message, uint32_t messageLen,
120479a3c601SCedric Chaumont 			       void *mac, uint32_t *macLen)
1205b0104773SPascal Brand {
1206b0104773SPascal Brand 	TEE_Result res;
1207e86f1266SJens Wiklander 	uint64_t ml;
1208b0104773SPascal Brand 
12096915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!message && messageLen)) {
121028e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
121128e0efc6SCedric Chaumont 		goto out;
121228e0efc6SCedric Chaumont 	}
12136915bbbbSJens Wiklander 	__utee_check_inout_annotation(macLen, sizeof(*macLen));
1214b0104773SPascal Brand 
121528e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
121628e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
121728e0efc6SCedric Chaumont 		goto out;
121828e0efc6SCedric Chaumont 	}
121928e0efc6SCedric Chaumont 
122028e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
122128e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
122228e0efc6SCedric Chaumont 		goto out;
122328e0efc6SCedric Chaumont 	}
122428e0efc6SCedric Chaumont 
1225642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1226642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1227642a1607SCedric Chaumont 		goto out;
1228642a1607SCedric Chaumont 	}
1229642a1607SCedric Chaumont 
1230e86f1266SJens Wiklander 	ml = *macLen;
12312c028fdeSJerome Forissier 	res = _utee_hash_final(operation->state, message, messageLen, mac, &ml);
1232e86f1266SJens Wiklander 	*macLen = ml;
123328e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS)
123428e0efc6SCedric Chaumont 		goto out;
123528e0efc6SCedric Chaumont 
123628e0efc6SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
123728e0efc6SCedric Chaumont 
1238642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1239642a1607SCedric Chaumont 
124028e0efc6SCedric Chaumont out:
124128e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
124228e0efc6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
124328e0efc6SCedric Chaumont 		TEE_Panic(res);
124428e0efc6SCedric Chaumont 
1245b0104773SPascal Brand 	return res;
1246b0104773SPascal Brand }
1247b0104773SPascal Brand 
1248b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
12498f07fe6fSJerome Forissier 			       const void *message, uint32_t messageLen,
12508f07fe6fSJerome Forissier 			       const void *mac, uint32_t macLen)
1251b0104773SPascal Brand {
1252b0104773SPascal Brand 	TEE_Result res;
1253ee4ba3d1SVictor Chong 	uint8_t computed_mac[TEE_MAX_HASH_SIZE] = { 0 };
12547f74c64aSPascal Brand 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1255b0104773SPascal Brand 
125628e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
125728e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
125828e0efc6SCedric Chaumont 		goto out;
125928e0efc6SCedric Chaumont 	}
126028e0efc6SCedric Chaumont 
126128e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
126228e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
126328e0efc6SCedric Chaumont 		goto out;
126428e0efc6SCedric Chaumont 	}
126528e0efc6SCedric Chaumont 
1266642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1267642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1268642a1607SCedric Chaumont 		goto out;
1269642a1607SCedric Chaumont 	}
1270642a1607SCedric Chaumont 
1271b0104773SPascal Brand 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1272b0104773SPascal Brand 				  &computed_mac_size);
1273b0104773SPascal Brand 	if (res != TEE_SUCCESS)
127428e0efc6SCedric Chaumont 		goto out;
127528e0efc6SCedric Chaumont 
127628e0efc6SCedric Chaumont 	if (computed_mac_size != macLen) {
127728e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
127828e0efc6SCedric Chaumont 		goto out;
127928e0efc6SCedric Chaumont 	}
128028e0efc6SCedric Chaumont 
128148e10604SJerome Forissier 	if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) {
128228e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
128328e0efc6SCedric Chaumont 		goto out;
128428e0efc6SCedric Chaumont 	}
128528e0efc6SCedric Chaumont 
1286642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1287642a1607SCedric Chaumont 
128828e0efc6SCedric Chaumont out:
128928e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
129028e0efc6SCedric Chaumont 	    res != TEE_ERROR_MAC_INVALID)
129128e0efc6SCedric Chaumont 		TEE_Panic(res);
129228e0efc6SCedric Chaumont 
1293b0104773SPascal Brand 	return res;
1294b0104773SPascal Brand }
1295b0104773SPascal Brand 
1296b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */
1297b0104773SPascal Brand 
12988f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce,
129979a3c601SCedric Chaumont 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1300b0104773SPascal Brand 		      uint32_t payloadLen)
1301b0104773SPascal Brand {
1302b0104773SPascal Brand 	TEE_Result res;
1303b0104773SPascal Brand 
1304b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || nonce == NULL) {
1305b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1306b5816c88SCedric Chaumont 		goto out;
1307b5816c88SCedric Chaumont 	}
1308b5816c88SCedric Chaumont 
1309b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1310b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1311b5816c88SCedric Chaumont 		goto out;
1312b5816c88SCedric Chaumont 	}
1313b0104773SPascal Brand 
1314642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
1315642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1316642a1607SCedric Chaumont 		goto out;
1317642a1607SCedric Chaumont 	}
1318642a1607SCedric Chaumont 
1319b0104773SPascal Brand 	/*
1320b0104773SPascal Brand 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1321b0104773SPascal Brand 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1322b0104773SPascal Brand 	 * according to the same principle so we have to check here instead to
1323b0104773SPascal Brand 	 * be GP compliant.
1324b0104773SPascal Brand 	 */
1325b5816c88SCedric Chaumont 	if (operation->info.algorithm == TEE_ALG_AES_GCM) {
1326b0104773SPascal Brand 		/*
1327b0104773SPascal Brand 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1328b0104773SPascal Brand 		 */
1329b5816c88SCedric Chaumont 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) {
1330b5816c88SCedric Chaumont 			res = TEE_ERROR_NOT_SUPPORTED;
1331b5816c88SCedric Chaumont 			goto out;
1332b5816c88SCedric Chaumont 		}
1333b0104773SPascal Brand 	}
1334b0104773SPascal Brand 
13352c028fdeSJerome Forissier 	res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8,
13362c028fdeSJerome Forissier 				 AADLen, payloadLen);
1337b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1338b5816c88SCedric Chaumont 		goto out;
1339b5816c88SCedric Chaumont 
13407acaf5adSAlbert Schwarzkopf 	operation->info.digestLength = tagLen / 8;
1341f2674567SSumit Garg 	operation->buffer_offs = 0;
1342b5816c88SCedric Chaumont 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1343b5816c88SCedric Chaumont 
1344b5816c88SCedric Chaumont out:
1345b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1346b5816c88SCedric Chaumont 	    res != TEE_ERROR_NOT_SUPPORTED)
1347b0104773SPascal Brand 			TEE_Panic(res);
1348b5816c88SCedric Chaumont 
1349b0104773SPascal Brand 	return res;
1350b0104773SPascal Brand }
1351b0104773SPascal Brand 
13528f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata,
135379a3c601SCedric Chaumont 		     uint32_t AADdataLen)
1354b0104773SPascal Brand {
1355b0104773SPascal Brand 	TEE_Result res;
1356b0104773SPascal Brand 
1357b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1358b5816c88SCedric Chaumont 	    (AADdata == NULL && AADdataLen != 0))
1359b0104773SPascal Brand 		TEE_Panic(0);
1360642a1607SCedric Chaumont 
1361b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE)
1362b0104773SPascal Brand 		TEE_Panic(0);
1363642a1607SCedric Chaumont 
1364b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1365b0104773SPascal Brand 		TEE_Panic(0);
1366b0104773SPascal Brand 
13672c028fdeSJerome Forissier 	res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen);
1368642a1607SCedric Chaumont 
1369642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1370642a1607SCedric Chaumont 
1371b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1372b0104773SPascal Brand 		TEE_Panic(res);
1373b0104773SPascal Brand }
1374b0104773SPascal Brand 
13758f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData,
137679a3c601SCedric Chaumont 			uint32_t srcLen, void *destData, uint32_t *destLen)
1377b0104773SPascal Brand {
13786915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
13796915bbbbSJens Wiklander 	size_t req_dlen = 0;
13806915bbbbSJens Wiklander 	uint64_t dl = 0;
1381b0104773SPascal Brand 
13826915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1383b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1384b5816c88SCedric Chaumont 		goto out;
1385b5816c88SCedric Chaumont 	}
13866915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1387b5816c88SCedric Chaumont 
1388b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1389b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1390b5816c88SCedric Chaumont 		goto out;
1391b5816c88SCedric Chaumont 	}
1392b5816c88SCedric Chaumont 
1393b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1394b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1395b5816c88SCedric Chaumont 		goto out;
1396b5816c88SCedric Chaumont 	}
1397b0104773SPascal Brand 
1398827308b8SJerome Forissier 	if (!srcData && !srcLen) {
1399090268f5SJens Wiklander 		*destLen = 0;
1400827308b8SJerome Forissier 		res = TEE_SUCCESS;
1401827308b8SJerome Forissier 		goto out;
1402827308b8SJerome Forissier 	}
1403827308b8SJerome Forissier 
1404b0104773SPascal Brand 	/*
1405b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1406b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1407b0104773SPascal Brand 	 * can't restore sync with this API.
1408b0104773SPascal Brand 	 */
1409afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
1410b5816c88SCedric Chaumont 		req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen,
1411b5816c88SCedric Chaumont 				     operation->block_size);
1412afc0c182SBogdan Liulko 	} else {
1413afc0c182SBogdan Liulko 		req_dlen = srcLen;
1414afc0c182SBogdan Liulko 	}
1415afc0c182SBogdan Liulko 
14166915bbbbSJens Wiklander 	dl = *destLen;
14176915bbbbSJens Wiklander 	if (dl < req_dlen) {
1418b0104773SPascal Brand 		*destLen = req_dlen;
1419b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1420b5816c88SCedric Chaumont 		goto out;
1421b0104773SPascal Brand 	}
1422b0104773SPascal Brand 
1423afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
14242c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1425afc0c182SBogdan Liulko 					srcData, srcLen, destData, &dl);
1426afc0c182SBogdan Liulko 	} else {
1427afc0c182SBogdan Liulko 		if (srcLen > 0) {
14282c028fdeSJerome Forissier 			res = _utee_authenc_update_payload(operation->state,
1429afc0c182SBogdan Liulko 							   srcData, srcLen,
1430afc0c182SBogdan Liulko 							   destData, &dl);
1431afc0c182SBogdan Liulko 		} else {
1432afc0c182SBogdan Liulko 			dl = 0;
1433afc0c182SBogdan Liulko 			res = TEE_SUCCESS;
1434afc0c182SBogdan Liulko 		}
1435afc0c182SBogdan Liulko 	}
1436afc0c182SBogdan Liulko 	if (res != TEE_SUCCESS)
1437afc0c182SBogdan Liulko 		goto out;
1438afc0c182SBogdan Liulko 
1439e86f1266SJens Wiklander 	*destLen = dl;
1440b0104773SPascal Brand 
1441642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1442642a1607SCedric Chaumont 
1443b5816c88SCedric Chaumont out:
1444b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1445b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1446b5816c88SCedric Chaumont 			TEE_Panic(res);
1447b5816c88SCedric Chaumont 
1448b5816c88SCedric Chaumont 	return res;
1449b0104773SPascal Brand }
1450b0104773SPascal Brand 
1451b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation,
14528f07fe6fSJerome Forissier 			      const void *srcData, uint32_t srcLen,
145379a3c601SCedric Chaumont 			      void *destData, uint32_t *destLen, void *tag,
145479a3c601SCedric Chaumont 			      uint32_t *tagLen)
1455b0104773SPascal Brand {
1456b0104773SPascal Brand 	TEE_Result res;
1457b0104773SPascal Brand 	uint8_t *dst = destData;
1458b0104773SPascal Brand 	size_t acc_dlen = 0;
1459e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1460b0104773SPascal Brand 	size_t req_dlen;
1461e86f1266SJens Wiklander 	uint64_t tl;
1462b0104773SPascal Brand 
14636915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1464b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1465b5816c88SCedric Chaumont 		goto out;
1466b5816c88SCedric Chaumont 	}
14676915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
14686915bbbbSJens Wiklander 	__utee_check_inout_annotation(tagLen, sizeof(*tagLen));
1469b5816c88SCedric Chaumont 
1470b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1471b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1472b5816c88SCedric Chaumont 		goto out;
1473b5816c88SCedric Chaumont 	}
1474b5816c88SCedric Chaumont 
1475b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1476b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1477b5816c88SCedric Chaumont 		goto out;
1478b5816c88SCedric Chaumont 	}
1479b0104773SPascal Brand 
1480b0104773SPascal Brand 	/*
1481b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1482b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1483b0104773SPascal Brand 	 * can't restore sync with this API.
14842733280aSEtienne Carriere 	 *
14852733280aSEtienne Carriere 	 * Need to check this before update_payload since sync would be lost if
14862733280aSEtienne Carriere 	 * we return short buffer after that.
1487b0104773SPascal Brand 	 */
14882733280aSEtienne Carriere 	res = TEE_ERROR_GENERIC;
14892733280aSEtienne Carriere 
1490b5816c88SCedric Chaumont 	req_dlen = operation->buffer_offs + srcLen;
1491b0104773SPascal Brand 	if (*destLen < req_dlen) {
1492b0104773SPascal Brand 		*destLen = req_dlen;
1493b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1494b0104773SPascal Brand 	}
1495b0104773SPascal Brand 
14967acaf5adSAlbert Schwarzkopf 	if (*tagLen < operation->info.digestLength) {
14977acaf5adSAlbert Schwarzkopf 		*tagLen = operation->info.digestLength;
1498b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1499b0104773SPascal Brand 	}
1500b0104773SPascal Brand 
15012733280aSEtienne Carriere 	if (res == TEE_ERROR_SHORT_BUFFER)
15022733280aSEtienne Carriere 		goto out;
15032733280aSEtienne Carriere 
1504afc0c182SBogdan Liulko 	tl = *tagLen;
1505b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1506afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
15072c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1508afc0c182SBogdan Liulko 					srcData, srcLen, dst, &tmp_dlen);
1509b5816c88SCedric Chaumont 		if (res != TEE_SUCCESS)
1510b5816c88SCedric Chaumont 			goto out;
1511b5816c88SCedric Chaumont 
1512b0104773SPascal Brand 		dst += tmp_dlen;
1513b0104773SPascal Brand 		acc_dlen += tmp_dlen;
1514b0104773SPascal Brand 
1515b0104773SPascal Brand 		tmp_dlen = *destLen - acc_dlen;
15162c028fdeSJerome Forissier 		res = _utee_authenc_enc_final(operation->state,
1517afc0c182SBogdan Liulko 					      operation->buffer,
1518afc0c182SBogdan Liulko 					      operation->buffer_offs, dst,
1519afc0c182SBogdan Liulko 					      &tmp_dlen, tag, &tl);
1520afc0c182SBogdan Liulko 	} else {
15212c028fdeSJerome Forissier 		res = _utee_authenc_enc_final(operation->state, srcData,
1522afc0c182SBogdan Liulko 					      srcLen, dst, &tmp_dlen,
1523e86f1266SJens Wiklander 					      tag, &tl);
1524afc0c182SBogdan Liulko 	}
1525e86f1266SJens Wiklander 	*tagLen = tl;
1526b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1527b5816c88SCedric Chaumont 		goto out;
1528b0104773SPascal Brand 
1529b5816c88SCedric Chaumont 	acc_dlen += tmp_dlen;
1530b0104773SPascal Brand 	*destLen = acc_dlen;
1531642a1607SCedric Chaumont 
1532b5816c88SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1533b5816c88SCedric Chaumont 
1534642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1535642a1607SCedric Chaumont 
1536b5816c88SCedric Chaumont out:
1537b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1538b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1539b5816c88SCedric Chaumont 			TEE_Panic(res);
1540b0104773SPascal Brand 
1541b0104773SPascal Brand 	return res;
1542b0104773SPascal Brand }
1543b0104773SPascal Brand 
1544b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation,
15458f07fe6fSJerome Forissier 			      const void *srcData, uint32_t srcLen,
1546b5816c88SCedric Chaumont 			      void *destData, uint32_t *destLen, void *tag,
154779a3c601SCedric Chaumont 			      uint32_t tagLen)
1548b0104773SPascal Brand {
1549b0104773SPascal Brand 	TEE_Result res;
1550b0104773SPascal Brand 	uint8_t *dst = destData;
1551b0104773SPascal Brand 	size_t acc_dlen = 0;
1552e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1553b0104773SPascal Brand 	size_t req_dlen;
1554b0104773SPascal Brand 
15556915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1556b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1557b5816c88SCedric Chaumont 		goto out;
1558b5816c88SCedric Chaumont 	}
15596915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1560b5816c88SCedric Chaumont 
1561b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1562b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1563b5816c88SCedric Chaumont 		goto out;
1564b5816c88SCedric Chaumont 	}
1565b5816c88SCedric Chaumont 
1566b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1567b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1568b5816c88SCedric Chaumont 		goto out;
1569b5816c88SCedric Chaumont 	}
1570b0104773SPascal Brand 
1571b0104773SPascal Brand 	/*
1572b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1573b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1574b0104773SPascal Brand 	 * can't restore sync with this API.
1575b0104773SPascal Brand 	 */
1576b5816c88SCedric Chaumont 	req_dlen = operation->buffer_offs + srcLen;
1577b0104773SPascal Brand 	if (*destLen < req_dlen) {
1578b0104773SPascal Brand 		*destLen = req_dlen;
1579b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1580b5816c88SCedric Chaumont 		goto out;
1581b0104773SPascal Brand 	}
1582b0104773SPascal Brand 
1583b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1584afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
15852c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1586afc0c182SBogdan Liulko 					srcData, srcLen, dst, &tmp_dlen);
1587b5816c88SCedric Chaumont 		if (res != TEE_SUCCESS)
1588b5816c88SCedric Chaumont 			goto out;
1589b5816c88SCedric Chaumont 
1590b0104773SPascal Brand 		dst += tmp_dlen;
1591b0104773SPascal Brand 		acc_dlen += tmp_dlen;
1592b0104773SPascal Brand 
1593b0104773SPascal Brand 		tmp_dlen = *destLen - acc_dlen;
15942c028fdeSJerome Forissier 		res = _utee_authenc_dec_final(operation->state,
1595afc0c182SBogdan Liulko 					      operation->buffer,
1596afc0c182SBogdan Liulko 					      operation->buffer_offs, dst,
1597afc0c182SBogdan Liulko 					      &tmp_dlen, tag, tagLen);
1598afc0c182SBogdan Liulko 	} else {
15992c028fdeSJerome Forissier 		res = _utee_authenc_dec_final(operation->state, srcData,
1600afc0c182SBogdan Liulko 					      srcLen, dst, &tmp_dlen,
1601b5816c88SCedric Chaumont 					      tag, tagLen);
1602afc0c182SBogdan Liulko 	}
1603b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1604b5816c88SCedric Chaumont 		goto out;
1605b5816c88SCedric Chaumont 
1606b0104773SPascal Brand 	/* Supplied tagLen should match what we initiated with */
16077acaf5adSAlbert Schwarzkopf 	if (tagLen != operation->info.digestLength)
1608b0104773SPascal Brand 		res = TEE_ERROR_MAC_INVALID;
1609b0104773SPascal Brand 
1610b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1611b0104773SPascal Brand 	*destLen = acc_dlen;
1612642a1607SCedric Chaumont 
1613b5816c88SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1614b5816c88SCedric Chaumont 
1615642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1616642a1607SCedric Chaumont 
1617b5816c88SCedric Chaumont out:
1618b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1619b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER &&
1620b5816c88SCedric Chaumont 	    res != TEE_ERROR_MAC_INVALID)
1621b5816c88SCedric Chaumont 			TEE_Panic(res);
1622b0104773SPascal Brand 
1623b0104773SPascal Brand 	return res;
1624b0104773SPascal Brand }
1625b0104773SPascal Brand 
1626b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */
1627b0104773SPascal Brand 
162812e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation,
16298f07fe6fSJerome Forissier 				 const TEE_Attribute *params,
16308f07fe6fSJerome Forissier 				 uint32_t paramCount, const void *srcData,
163179a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
163279a3c601SCedric Chaumont 				 uint32_t *destLen)
1633b0104773SPascal Brand {
16346915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1635e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
16366915bbbbSJens Wiklander 	uint64_t dl = 0;
1637b0104773SPascal Brand 
16386915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
1639b0104773SPascal Brand 		TEE_Panic(0);
16406915bbbbSJens Wiklander 
16416915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
16426915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
16436915bbbbSJens Wiklander 
164412e66b6fSCedric Chaumont 	if (!operation->key1)
1645b0104773SPascal Brand 		TEE_Panic(0);
164612e66b6fSCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
164712e66b6fSCedric Chaumont 		TEE_Panic(0);
164812e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_ENCRYPT)
1649b0104773SPascal Brand 		TEE_Panic(0);
1650b0104773SPascal Brand 
1651e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1652e86f1266SJens Wiklander 	dl = *destLen;
16532c028fdeSJerome Forissier 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1654e86f1266SJens Wiklander 				  srcLen, destData, &dl);
1655e86f1266SJens Wiklander 	*destLen = dl;
165612e66b6fSCedric Chaumont 
16578844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
16588844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
16598844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1660b0104773SPascal Brand 		TEE_Panic(res);
166112e66b6fSCedric Chaumont 
1662b0104773SPascal Brand 	return res;
1663b0104773SPascal Brand }
1664b0104773SPascal Brand 
166512e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation,
16668f07fe6fSJerome Forissier 				 const TEE_Attribute *params,
16678f07fe6fSJerome Forissier 				 uint32_t paramCount, const void *srcData,
166879a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
166979a3c601SCedric Chaumont 				 uint32_t *destLen)
1670b0104773SPascal Brand {
16716915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1672e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
16736915bbbbSJens Wiklander 	uint64_t dl = 0;
1674b0104773SPascal Brand 
16756915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
1676b0104773SPascal Brand 		TEE_Panic(0);
16776915bbbbSJens Wiklander 
16786915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
16796915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
16806915bbbbSJens Wiklander 
168112e66b6fSCedric Chaumont 	if (!operation->key1)
1682b0104773SPascal Brand 		TEE_Panic(0);
168312e66b6fSCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
168412e66b6fSCedric Chaumont 		TEE_Panic(0);
168512e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_DECRYPT)
1686b0104773SPascal Brand 		TEE_Panic(0);
1687b0104773SPascal Brand 
1688e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1689e86f1266SJens Wiklander 	dl = *destLen;
16902c028fdeSJerome Forissier 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1691e86f1266SJens Wiklander 				  srcLen, destData, &dl);
1692e86f1266SJens Wiklander 	*destLen = dl;
169312e66b6fSCedric Chaumont 
16948844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
16958844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
16968844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1697b0104773SPascal Brand 		TEE_Panic(res);
169812e66b6fSCedric Chaumont 
1699b0104773SPascal Brand 	return res;
1700b0104773SPascal Brand }
1701b0104773SPascal Brand 
170212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation,
17038f07fe6fSJerome Forissier 				    const TEE_Attribute *params,
17048f07fe6fSJerome Forissier 				    uint32_t paramCount, const void *digest,
170579a3c601SCedric Chaumont 				    uint32_t digestLen, void *signature,
170679a3c601SCedric Chaumont 				    uint32_t *signatureLen)
1707b0104773SPascal Brand {
17086915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1709e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
17106915bbbbSJens Wiklander 	uint64_t sl = 0;
1711b0104773SPascal Brand 
17126915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!digest && digestLen))
1713b0104773SPascal Brand 		TEE_Panic(0);
17146915bbbbSJens Wiklander 
17156915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
17166915bbbbSJens Wiklander 	__utee_check_inout_annotation(signatureLen, sizeof(*signatureLen));
17176915bbbbSJens Wiklander 
171812e66b6fSCedric Chaumont 	if (!operation->key1)
1719b0104773SPascal Brand 		TEE_Panic(0);
172012e66b6fSCedric Chaumont 	if (operation->info.operationClass !=
172112e66b6fSCedric Chaumont 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
172212e66b6fSCedric Chaumont 		TEE_Panic(0);
172312e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_SIGN)
1724b0104773SPascal Brand 		TEE_Panic(0);
1725b0104773SPascal Brand 
1726e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1727e86f1266SJens Wiklander 	sl = *signatureLen;
17282c028fdeSJerome Forissier 	res = _utee_asymm_operate(operation->state, ua, paramCount, digest,
1729e86f1266SJens Wiklander 				  digestLen, signature, &sl);
1730e86f1266SJens Wiklander 	*signatureLen = sl;
173112e66b6fSCedric Chaumont 
1732b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1733b0104773SPascal Brand 		TEE_Panic(res);
173412e66b6fSCedric Chaumont 
1735b0104773SPascal Brand 	return res;
1736b0104773SPascal Brand }
1737b0104773SPascal Brand 
173812e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,
17398f07fe6fSJerome Forissier 				      const TEE_Attribute *params,
17408f07fe6fSJerome Forissier 				      uint32_t paramCount, const void *digest,
17418f07fe6fSJerome Forissier 				      uint32_t digestLen,
17428f07fe6fSJerome Forissier 				      const void *signature,
174379a3c601SCedric Chaumont 				      uint32_t signatureLen)
1744b0104773SPascal Brand {
1745b0104773SPascal Brand 	TEE_Result res;
1746e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1747b0104773SPascal Brand 
174812e66b6fSCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
174912e66b6fSCedric Chaumont 	    (digest == NULL && digestLen != 0) ||
1750b0104773SPascal Brand 	    (signature == NULL && signatureLen != 0))
1751b0104773SPascal Brand 		TEE_Panic(0);
17526915bbbbSJens Wiklander 
17536915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
17546915bbbbSJens Wiklander 
175512e66b6fSCedric Chaumont 	if (!operation->key1)
1756b0104773SPascal Brand 		TEE_Panic(0);
175712e66b6fSCedric Chaumont 	if (operation->info.operationClass !=
175812e66b6fSCedric Chaumont 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
175912e66b6fSCedric Chaumont 		TEE_Panic(0);
176012e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_VERIFY)
1761b0104773SPascal Brand 		TEE_Panic(0);
1762b0104773SPascal Brand 
1763e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
17642c028fdeSJerome Forissier 	res = _utee_asymm_verify(operation->state, ua, paramCount, digest,
176512e66b6fSCedric Chaumont 				 digestLen, signature, signatureLen);
176612e66b6fSCedric Chaumont 
1767b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1768b0104773SPascal Brand 		TEE_Panic(res);
176912e66b6fSCedric Chaumont 
1770b0104773SPascal Brand 	return res;
1771b0104773SPascal Brand }
1772b0104773SPascal Brand 
1773b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */
1774b0104773SPascal Brand 
1775b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation,
1776b0104773SPascal Brand 		   const TEE_Attribute *params, uint32_t paramCount,
1777b0104773SPascal Brand 		   TEE_ObjectHandle derivedKey)
1778b0104773SPascal Brand {
1779b0104773SPascal Brand 	TEE_Result res;
1780b0104773SPascal Brand 	TEE_ObjectInfo key_info;
1781e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1782b0104773SPascal Brand 
1783b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1784b0104773SPascal Brand 		TEE_Panic(0);
17856915bbbbSJens Wiklander 
17866915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
17876915bbbbSJens Wiklander 
17888854d3c6SJerome Forissier 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
17898854d3c6SJerome Forissier 	    TEE_OPERATION_KEY_DERIVATION)
1790b0104773SPascal Brand 		TEE_Panic(0);
1791b0104773SPascal Brand 
1792b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1793b0104773SPascal Brand 		TEE_Panic(0);
179484fa9467SCedric Chaumont 	if (!operation->key1)
179584fa9467SCedric Chaumont 		TEE_Panic(0);
1796b0104773SPascal Brand 	if (operation->info.mode != TEE_MODE_DERIVE)
1797b0104773SPascal Brand 		TEE_Panic(0);
1798b0104773SPascal Brand 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1799b0104773SPascal Brand 		TEE_Panic(0);
1800b0104773SPascal Brand 
18012c028fdeSJerome Forissier 	res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info);
1802b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1803b36311adSJerome Forissier 		TEE_Panic(res);
1804b0104773SPascal Brand 
1805b0104773SPascal Brand 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1806b0104773SPascal Brand 		TEE_Panic(0);
1807b0104773SPascal Brand 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1808b0104773SPascal Brand 		TEE_Panic(0);
1809b0104773SPascal Brand 
1810e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
18112c028fdeSJerome Forissier 	res = _utee_cryp_derive_key(operation->state, ua, paramCount,
1812e86f1266SJens Wiklander 				    (unsigned long)derivedKey);
1813b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1814b0104773SPascal Brand 		TEE_Panic(res);
1815b0104773SPascal Brand }
1816b0104773SPascal Brand 
1817b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */
1818b0104773SPascal Brand 
181979a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1820b0104773SPascal Brand {
1821b0104773SPascal Brand 	TEE_Result res;
1822b0104773SPascal Brand 
18232c028fdeSJerome Forissier 	res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1824b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1825b0104773SPascal Brand 		TEE_Panic(res);
1826b0104773SPascal Brand }
1827433c4257SJens Wiklander 
1828433c4257SJens Wiklander int rand(void)
1829433c4257SJens Wiklander {
1830433c4257SJens Wiklander 	int rc;
1831433c4257SJens Wiklander 
1832433c4257SJens Wiklander 	TEE_GenerateRandom(&rc, sizeof(rc));
1833433c4257SJens Wiklander 
1834433c4257SJens Wiklander 	/*
1835433c4257SJens Wiklander 	 * RAND_MAX is the larges int, INT_MAX which is all bits but the
1836433c4257SJens Wiklander 	 * highest bit set.
1837433c4257SJens Wiklander 	 */
1838433c4257SJens Wiklander 	return rc & RAND_MAX;
1839433c4257SJens Wiklander }
184079170ce0SJerome Forissier 
184179170ce0SJerome Forissier TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element)
184279170ce0SJerome Forissier {
184379170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_AES)) {
184479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
184579170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_ECB_NOPAD)
184679170ce0SJerome Forissier 				goto check_element_none;
184779170ce0SJerome Forissier 		}
184879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
184979170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CBC_NOPAD)
185079170ce0SJerome Forissier 				goto check_element_none;
185179170ce0SJerome Forissier 		}
185279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
185379170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CTR)
185479170ce0SJerome Forissier 				goto check_element_none;
185579170ce0SJerome Forissier 		}
185679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CTS)) {
185779170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CTS)
185879170ce0SJerome Forissier 				goto check_element_none;
185979170ce0SJerome Forissier 		}
186079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_XTS)) {
186179170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_XTS)
186279170ce0SJerome Forissier 				goto check_element_none;
186379170ce0SJerome Forissier 		}
186479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
186579170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CBC_MAC_NOPAD ||
186679170ce0SJerome Forissier 			    alg == TEE_ALG_AES_CBC_MAC_PKCS5)
186779170ce0SJerome Forissier 				goto check_element_none;
186879170ce0SJerome Forissier 		}
186979170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CMAC)) {
187079170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CMAC)
187179170ce0SJerome Forissier 				goto check_element_none;
187279170ce0SJerome Forissier 		}
187379170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CCM)) {
187479170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CCM)
187579170ce0SJerome Forissier 				goto check_element_none;
187679170ce0SJerome Forissier 		}
187779170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_GCM)) {
187879170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_GCM)
187979170ce0SJerome Forissier 				goto check_element_none;
188079170ce0SJerome Forissier 		}
188179170ce0SJerome Forissier 	}
188279170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_DES)) {
188379170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
188479170ce0SJerome Forissier 			if (alg == TEE_ALG_DES_ECB_NOPAD ||
188579170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_ECB_NOPAD)
188679170ce0SJerome Forissier 				goto check_element_none;
188779170ce0SJerome Forissier 		}
188879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
188979170ce0SJerome Forissier 			if (alg == TEE_ALG_DES_CBC_NOPAD ||
189079170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_CBC_NOPAD)
189179170ce0SJerome Forissier 				goto check_element_none;
189279170ce0SJerome Forissier 		}
189379170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
189479170ce0SJerome Forissier 			if (alg == TEE_ALG_DES_CBC_MAC_NOPAD ||
189579170ce0SJerome Forissier 			    alg == TEE_ALG_DES_CBC_MAC_PKCS5 ||
189679170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_CBC_MAC_NOPAD ||
189779170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_CBC_MAC_PKCS5)
189879170ce0SJerome Forissier 				goto check_element_none;
189979170ce0SJerome Forissier 		}
190079170ce0SJerome Forissier 	}
190179170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_MD5)) {
190279170ce0SJerome Forissier 		if (alg == TEE_ALG_MD5)
190379170ce0SJerome Forissier 			goto check_element_none;
190479170ce0SJerome Forissier 	}
190579170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
190679170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA1)
190779170ce0SJerome Forissier 			goto check_element_none;
190879170ce0SJerome Forissier 	}
190979170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
191079170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA224)
191179170ce0SJerome Forissier 			goto check_element_none;
191279170ce0SJerome Forissier 	}
191379170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
191479170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA256)
191579170ce0SJerome Forissier 			goto check_element_none;
191679170ce0SJerome Forissier 	}
191779170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
191879170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA384)
191979170ce0SJerome Forissier 			goto check_element_none;
192079170ce0SJerome Forissier 	}
192179170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
192279170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA512)
192379170ce0SJerome Forissier 			goto check_element_none;
192479170ce0SJerome Forissier 	}
192579170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
192679170ce0SJerome Forissier 		if (alg == TEE_ALG_MD5SHA1)
192779170ce0SJerome Forissier 			goto check_element_none;
192879170ce0SJerome Forissier 	}
192979170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_HMAC)) {
193079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
193179170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_MD5)
193279170ce0SJerome Forissier 				goto check_element_none;
193379170ce0SJerome Forissier 		}
193479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
193579170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA1)
193679170ce0SJerome Forissier 				goto check_element_none;
193779170ce0SJerome Forissier 		}
193879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
193979170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA224)
194079170ce0SJerome Forissier 				goto check_element_none;
194179170ce0SJerome Forissier 		}
194279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
194379170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA256)
194479170ce0SJerome Forissier 				goto check_element_none;
194579170ce0SJerome Forissier 		}
194679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
194779170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA384)
194879170ce0SJerome Forissier 				goto check_element_none;
194979170ce0SJerome Forissier 		}
195079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
195179170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA512)
195279170ce0SJerome Forissier 				goto check_element_none;
195379170ce0SJerome Forissier 		}
195479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SM3)) {
195579170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SM3)
195679170ce0SJerome Forissier 				goto check_element_none;
195779170ce0SJerome Forissier 		}
195879170ce0SJerome Forissier 	}
195979170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM3)) {
196079170ce0SJerome Forissier 		if (alg == TEE_ALG_SM3)
196179170ce0SJerome Forissier 			goto check_element_none;
196279170ce0SJerome Forissier 	}
196379170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM4)) {
196479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
196579170ce0SJerome Forissier 			if (alg == TEE_ALG_SM4_ECB_NOPAD)
196679170ce0SJerome Forissier 				goto check_element_none;
196779170ce0SJerome Forissier 		}
196879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
196979170ce0SJerome Forissier 			if (alg == TEE_ALG_SM4_CBC_NOPAD)
197079170ce0SJerome Forissier 				goto check_element_none;
197179170ce0SJerome Forissier 		}
197279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
197379170ce0SJerome Forissier 			if (alg == TEE_ALG_SM4_CTR)
197479170ce0SJerome Forissier 				goto check_element_none;
197579170ce0SJerome Forissier 		}
197679170ce0SJerome Forissier 	}
197779170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_RSA)) {
197879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
197979170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5)
198079170ce0SJerome Forissier 				goto check_element_none;
198179170ce0SJerome Forissier 		}
198279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
198379170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 ||
198479170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 ||
198579170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1)
198679170ce0SJerome Forissier 				goto check_element_none;
198779170ce0SJerome Forissier 		}
198879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
198979170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1)
199079170ce0SJerome Forissier 				goto check_element_none;
199179170ce0SJerome Forissier 		}
199279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
199379170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 ||
199479170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 ||
199579170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224)
199679170ce0SJerome Forissier 				goto check_element_none;
199779170ce0SJerome Forissier 		}
199879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
199979170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 ||
200079170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 ||
200179170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256)
200279170ce0SJerome Forissier 				goto check_element_none;
200379170ce0SJerome Forissier 		}
200479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
200579170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 ||
200679170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 ||
200779170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384)
200879170ce0SJerome Forissier 				goto check_element_none;
200979170ce0SJerome Forissier 		}
201079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
201179170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 ||
201279170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 ||
201379170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512)
201479170ce0SJerome Forissier 				goto check_element_none;
201579170ce0SJerome Forissier 		}
201679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) {
201779170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5)
201879170ce0SJerome Forissier 				goto check_element_none;
201979170ce0SJerome Forissier 		}
202079170ce0SJerome Forissier 		if (alg == TEE_ALG_RSA_NOPAD)
202179170ce0SJerome Forissier 			goto check_element_none;
202279170ce0SJerome Forissier 	}
202379170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_DSA)) {
202479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
202579170ce0SJerome Forissier 			if (alg == TEE_ALG_DSA_SHA1)
202679170ce0SJerome Forissier 				goto check_element_none;
202779170ce0SJerome Forissier 		}
202879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
202979170ce0SJerome Forissier 			if (alg == TEE_ALG_DSA_SHA224)
203079170ce0SJerome Forissier 				goto check_element_none;
203179170ce0SJerome Forissier 		}
203279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
203379170ce0SJerome Forissier 			if (alg == TEE_ALG_DSA_SHA256)
203479170ce0SJerome Forissier 				goto check_element_none;
203579170ce0SJerome Forissier 		}
203679170ce0SJerome Forissier 	}
203779170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_DH)) {
203879170ce0SJerome Forissier 		if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
203979170ce0SJerome Forissier 			goto check_element_none;
204079170ce0SJerome Forissier 	}
204179170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_ECC)) {
204279170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) &&
204379170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P192)
204479170ce0SJerome Forissier 			return TEE_SUCCESS;
204579170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) &&
204679170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P224)
204779170ce0SJerome Forissier 			return TEE_SUCCESS;
204879170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) &&
204979170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P256)
205079170ce0SJerome Forissier 			return TEE_SUCCESS;
205179170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) &&
205279170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P384)
205379170ce0SJerome Forissier 			return TEE_SUCCESS;
205479170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) &&
205579170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P521)
205679170ce0SJerome Forissier 			return TEE_SUCCESS;
205779170ce0SJerome Forissier 	}
205879170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) {
205979170ce0SJerome Forissier 		if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2)
206079170ce0SJerome Forissier 			return TEE_SUCCESS;
206179170ce0SJerome Forissier 	}
206279170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) {
206379170ce0SJerome Forissier 		if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2)
206479170ce0SJerome Forissier 			return TEE_SUCCESS;
206579170ce0SJerome Forissier 	}
206679170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) {
206779170ce0SJerome Forissier 		if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2)
206879170ce0SJerome Forissier 			return TEE_SUCCESS;
206979170ce0SJerome Forissier 	}
207079170ce0SJerome Forissier 
207179170ce0SJerome Forissier 	return TEE_ERROR_NOT_SUPPORTED;
207279170ce0SJerome Forissier check_element_none:
207379170ce0SJerome Forissier 	if (element == TEE_CRYPTO_ELEMENT_NONE)
207479170ce0SJerome Forissier 		return TEE_SUCCESS;
207579170ce0SJerome Forissier 	return TEE_ERROR_NOT_SUPPORTED;
207679170ce0SJerome Forissier }
2077