xref: /optee_os/lib/libutee/tee_api_operations.c (revision fe2fd3ff46c0bada3dc9b2ae8c1025bfd8f57f3f)
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 
47696f56acSPingan Xie 	if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP ||
48696f56acSPingan Xie 	    algorithm == TEE_ALG_SM4_XTS)
49b0104773SPascal Brand 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
50b0104773SPascal Brand 
51218d9055SCedric Chaumont 	/* Check algorithm max key size */
52218d9055SCedric Chaumont 	switch (algorithm) {
53218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA1:
54218d9055SCedric Chaumont 		if (maxKeySize < 512)
55218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
56218d9055SCedric Chaumont 		if (maxKeySize > 1024)
57218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
58218d9055SCedric Chaumont 		if (maxKeySize % 64 != 0)
59218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
60218d9055SCedric Chaumont 		break;
61218d9055SCedric Chaumont 
62218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA224:
63218d9055SCedric Chaumont 		if (maxKeySize != 2048)
64218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
65218d9055SCedric Chaumont 		break;
66218d9055SCedric Chaumont 
67218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA256:
68218d9055SCedric Chaumont 		if (maxKeySize != 2048 && maxKeySize != 3072)
69218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
70218d9055SCedric Chaumont 		break;
71218d9055SCedric Chaumont 
72*fe2fd3ffSJens Wiklander 	case TEE_ALG_ECDSA_SHA1:
73*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDSA_P192:
74*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDH_P192:
751220586eSCedric Chaumont 		if (maxKeySize != 192)
761220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
771220586eSCedric Chaumont 		break;
781220586eSCedric Chaumont 
79*fe2fd3ffSJens Wiklander 	case TEE_ALG_ECDSA_SHA224:
80*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDSA_P224:
81*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDH_P224:
821220586eSCedric Chaumont 		if (maxKeySize != 224)
831220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
841220586eSCedric Chaumont 		break;
851220586eSCedric Chaumont 
86*fe2fd3ffSJens Wiklander 	case TEE_ALG_ECDSA_SHA256:
87*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDSA_P256:
88*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDH_P256:
8991fc6bd8SJerome Forissier 	case TEE_ALG_SM2_PKE:
900f151943SJerome Forissier 	case TEE_ALG_SM2_DSA_SM3:
911220586eSCedric Chaumont 		if (maxKeySize != 256)
921220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
931220586eSCedric Chaumont 		break;
941220586eSCedric Chaumont 
955b385b3fSJerome Forissier 	case TEE_ALG_SM2_KEP:
965b385b3fSJerome Forissier 		/* Two 256-bit keys */
975b385b3fSJerome Forissier 		if (maxKeySize != 512)
985b385b3fSJerome Forissier 			return TEE_ERROR_NOT_SUPPORTED;
995b385b3fSJerome Forissier 		break;
1005b385b3fSJerome Forissier 
101*fe2fd3ffSJens Wiklander 	case TEE_ALG_ECDSA_SHA384:
102*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDSA_P384:
103*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDH_P384:
1041220586eSCedric Chaumont 		if (maxKeySize != 384)
1051220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1061220586eSCedric Chaumont 		break;
1071220586eSCedric Chaumont 
108*fe2fd3ffSJens Wiklander 	case TEE_ALG_ECDSA_SHA512:
109*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDSA_P521:
110*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDH_P521:
1111220586eSCedric Chaumont 		if (maxKeySize != 521)
1121220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1131220586eSCedric Chaumont 		break;
114*fe2fd3ffSJens Wiklander 
115*fe2fd3ffSJens Wiklander 	case TEE_ALG_ECDH_DERIVE_SHARED_SECRET:
116*fe2fd3ffSJens Wiklander 		if (maxKeySize > 521)
117*fe2fd3ffSJens Wiklander 			return TEE_ERROR_NOT_SUPPORTED;
118*fe2fd3ffSJens Wiklander 		break;
119*fe2fd3ffSJens Wiklander 
120e1f9cee7SSergiy Kibrik 	case TEE_ALG_ED25519:
1213f61056dSSohaib ul Hassan 	case TEE_ALG_X25519:
1223f61056dSSohaib ul Hassan 		if (maxKeySize != 256)
1233f61056dSSohaib ul Hassan 			return TEE_ERROR_NOT_SUPPORTED;
1243f61056dSSohaib ul Hassan 		break;
125218d9055SCedric Chaumont 	default:
126218d9055SCedric Chaumont 		break;
127218d9055SCedric Chaumont 	}
128218d9055SCedric Chaumont 
129cf5c060cSJens Wiklander 	/* Check algorithm mode (and maxKeySize for digests) */
130b0104773SPascal Brand 	switch (algorithm) {
131b0104773SPascal Brand 	case TEE_ALG_AES_CTS:
132b0104773SPascal Brand 	case TEE_ALG_AES_XTS:
133696f56acSPingan Xie 	case TEE_ALG_SM4_XTS:
134b0104773SPascal Brand 		buffer_two_blocks = true;
135919a5a68SJerome Forissier 		fallthrough;
1364bd53c54SJerome Forissier 	case TEE_ALG_AES_ECB_NOPAD:
137b0104773SPascal Brand 	case TEE_ALG_AES_CBC_NOPAD:
138b0104773SPascal Brand 	case TEE_ALG_AES_CCM:
139b0104773SPascal Brand 	case TEE_ALG_DES_ECB_NOPAD:
140b0104773SPascal Brand 	case TEE_ALG_DES_CBC_NOPAD:
141b0104773SPascal Brand 	case TEE_ALG_DES3_ECB_NOPAD:
142b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_NOPAD:
143ade6f848SJerome Forissier 	case TEE_ALG_SM4_ECB_NOPAD:
144ade6f848SJerome Forissier 	case TEE_ALG_SM4_CBC_NOPAD:
145ade6f848SJerome Forissier 	case TEE_ALG_SM4_CTR:
146b0104773SPascal Brand 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
147b0104773SPascal Brand 			block_size = TEE_AES_BLOCK_SIZE;
148ade6f848SJerome Forissier 		else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4)
149ade6f848SJerome Forissier 			block_size = TEE_SM4_BLOCK_SIZE;
150b0104773SPascal Brand 		else
151b0104773SPascal Brand 			block_size = TEE_DES_BLOCK_SIZE;
152919a5a68SJerome Forissier 		fallthrough;
15357aabac5SBogdan Liulko 	case TEE_ALG_AES_CTR:
154afc0c182SBogdan Liulko 	case TEE_ALG_AES_GCM:
155b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT)
156b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
157b0104773SPascal Brand 		else if (mode == TEE_MODE_DECRYPT)
158b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
159b0104773SPascal Brand 		else
160b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
161b0104773SPascal Brand 		break;
162b0104773SPascal Brand 
1636a2e0a9fSGabor Szekely #if defined(CFG_CRYPTO_RSASSA_NA1)
1646a2e0a9fSGabor Szekely 	case TEE_ALG_RSASSA_PKCS1_V1_5:
1656a2e0a9fSGabor Szekely #endif
166b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
167b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
168b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
169b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
170b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
171b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
172b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
173b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
174b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
175b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
176b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
177b0104773SPascal Brand 	case TEE_ALG_DSA_SHA1:
178218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA224:
179218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA256:
180*fe2fd3ffSJens Wiklander 	case TEE_ALG_ECDSA_SHA1:
181*fe2fd3ffSJens Wiklander 	case TEE_ALG_ECDSA_SHA224:
182*fe2fd3ffSJens Wiklander 	case TEE_ALG_ECDSA_SHA256:
183*fe2fd3ffSJens Wiklander 	case TEE_ALG_ECDSA_SHA384:
184*fe2fd3ffSJens Wiklander 	case TEE_ALG_ECDSA_SHA512:
185*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDSA_P192:
186*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDSA_P224:
187*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDSA_P256:
188*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDSA_P384:
189*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDSA_P521:
1900f151943SJerome Forissier 	case TEE_ALG_SM2_DSA_SM3:
191e1f9cee7SSergiy Kibrik 	case TEE_ALG_ED25519:
192b0104773SPascal Brand 		if (mode == TEE_MODE_SIGN) {
193b0104773SPascal Brand 			with_private_key = true;
194b0104773SPascal Brand 			req_key_usage = TEE_USAGE_SIGN;
195b0104773SPascal Brand 		} else if (mode == TEE_MODE_VERIFY) {
196b0104773SPascal Brand 			req_key_usage = TEE_USAGE_VERIFY;
197b0104773SPascal Brand 		} else {
198b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
199b0104773SPascal Brand 		}
200b0104773SPascal Brand 		break;
201b0104773SPascal Brand 
202b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_V1_5:
203b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
204b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
205b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
206b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
207b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
20891fc6bd8SJerome Forissier 	case TEE_ALG_SM2_PKE:
209b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
210b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
211b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
212b0104773SPascal Brand 			with_private_key = true;
213b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
214b0104773SPascal Brand 		} else {
215b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
216b0104773SPascal Brand 		}
217b0104773SPascal Brand 		break;
218b0104773SPascal Brand 
219b0104773SPascal Brand 	case TEE_ALG_RSA_NOPAD:
220b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
221b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
222b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
223b0104773SPascal Brand 			with_private_key = true;
224b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
225b0104773SPascal Brand 		} else {
226b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
227b0104773SPascal Brand 		}
228b0104773SPascal Brand 		break;
229b0104773SPascal Brand 
230b0104773SPascal Brand 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
231*fe2fd3ffSJens Wiklander 	case TEE_ALG_ECDH_DERIVE_SHARED_SECRET:
232*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDH_P192:
233*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDH_P224:
234*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDH_P256:
235*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDH_P384:
236*fe2fd3ffSJens Wiklander 	case __OPTEE_ALG_ECDH_P521:
237cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_MD5_DERIVE_KEY:
238cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA1_DERIVE_KEY:
239cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA224_DERIVE_KEY:
240cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA256_DERIVE_KEY:
241cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA384_DERIVE_KEY:
242cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA512_DERIVE_KEY:
2438854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY:
2448854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY:
2458854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY:
2468854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY:
2478854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY:
2480f2293b7SJerome Forissier 	case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY:
2495b385b3fSJerome Forissier 	case TEE_ALG_SM2_KEP:
2503f61056dSSohaib ul Hassan 	case TEE_ALG_X25519:
251b0104773SPascal Brand 		if (mode != TEE_MODE_DERIVE)
252b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
253b0104773SPascal Brand 		with_private_key = true;
254b0104773SPascal Brand 		req_key_usage = TEE_USAGE_DERIVE;
255b0104773SPascal Brand 		break;
256b0104773SPascal Brand 
257b0104773SPascal Brand 	case TEE_ALG_MD5:
258b0104773SPascal Brand 	case TEE_ALG_SHA1:
259b0104773SPascal Brand 	case TEE_ALG_SHA224:
260b0104773SPascal Brand 	case TEE_ALG_SHA256:
261b0104773SPascal Brand 	case TEE_ALG_SHA384:
262b0104773SPascal Brand 	case TEE_ALG_SHA512:
26347645577SJerome Forissier 	case TEE_ALG_SM3:
264b0104773SPascal Brand 		if (mode != TEE_MODE_DIGEST)
265b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
266cf5c060cSJens Wiklander 		if (maxKeySize)
267cf5c060cSJens Wiklander 			return TEE_ERROR_NOT_SUPPORTED;
26805304565SCedric Chaumont 		/* v1.1: flags always set for digest operations */
269b0104773SPascal Brand 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
270b0104773SPascal Brand 		req_key_usage = 0;
271b0104773SPascal Brand 		break;
272b0104773SPascal Brand 
273b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_NOPAD:
274b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_NOPAD:
275b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_PKCS5:
276b0104773SPascal Brand 	case TEE_ALG_AES_CMAC:
277b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_PKCS5:
278b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
279b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
280eee637e7SAlexander Zakharov 	case TEE_ALG_DES3_CMAC:
281b0104773SPascal Brand 	case TEE_ALG_HMAC_MD5:
282b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA1:
283b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA224:
284b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA256:
285b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA384:
286b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA512:
28747645577SJerome Forissier 	case TEE_ALG_HMAC_SM3:
288b0104773SPascal Brand 		if (mode != TEE_MODE_MAC)
289b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
290b0104773SPascal Brand 		req_key_usage = TEE_USAGE_MAC;
291b0104773SPascal Brand 		break;
292b0104773SPascal Brand 
293b0104773SPascal Brand 	default:
294b0104773SPascal Brand 		return TEE_ERROR_NOT_SUPPORTED;
295b0104773SPascal Brand 	}
296b0104773SPascal Brand 
297b66f219bSJens Wiklander 	op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO);
2989b52c538SCedric Chaumont 	if (!op)
299b0104773SPascal Brand 		return TEE_ERROR_OUT_OF_MEMORY;
300b0104773SPascal Brand 
301b0104773SPascal Brand 	op->info.algorithm = algorithm;
302b0104773SPascal Brand 	op->info.operationClass = TEE_ALG_GET_CLASS(algorithm);
3036a2e0a9fSGabor Szekely #ifdef CFG_CRYPTO_RSASSA_NA1
3046a2e0a9fSGabor Szekely 	if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5)
3056a2e0a9fSGabor Szekely 		op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE;
3066a2e0a9fSGabor Szekely #endif
307b0104773SPascal Brand 	op->info.mode = mode;
3082e5e6460SAlbert Schwarzkopf 	op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm);
309b0104773SPascal Brand 	op->info.maxKeySize = maxKeySize;
310b0104773SPascal Brand 	op->info.requiredKeyUsage = req_key_usage;
311b0104773SPascal Brand 	op->info.handleState = handle_state;
312b0104773SPascal Brand 
313b0104773SPascal Brand 	if (block_size > 1) {
314b0104773SPascal Brand 		size_t buffer_size = block_size;
315b0104773SPascal Brand 
316b0104773SPascal Brand 		if (buffer_two_blocks)
317b0104773SPascal Brand 			buffer_size *= 2;
318b0104773SPascal Brand 
3199b52c538SCedric Chaumont 		op->buffer = TEE_Malloc(buffer_size,
3209b52c538SCedric Chaumont 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
321b0104773SPascal Brand 		if (op->buffer == NULL) {
322b0104773SPascal Brand 			res = TEE_ERROR_OUT_OF_MEMORY;
323b66f219bSJens Wiklander 			goto out;
324b0104773SPascal Brand 		}
325b0104773SPascal Brand 	}
326b0104773SPascal Brand 	op->block_size = block_size;
327b0104773SPascal Brand 	op->buffer_two_blocks = buffer_two_blocks;
328b0104773SPascal Brand 
329b0104773SPascal Brand 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
330b0104773SPascal Brand 		uint32_t mks = maxKeySize;
331b0104773SPascal Brand 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
332b0104773SPascal Brand 						       with_private_key);
333b0104773SPascal Brand 
334b0104773SPascal Brand 		/*
335b0104773SPascal Brand 		 * If two keys are expected the max key size is the sum of
336b0104773SPascal Brand 		 * the size of both keys.
337b0104773SPascal Brand 		 */
338b0104773SPascal Brand 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
339b0104773SPascal Brand 			mks /= 2;
340b0104773SPascal Brand 
341b0104773SPascal Brand 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
342b0104773SPascal Brand 		if (res != TEE_SUCCESS)
343b66f219bSJens Wiklander 			goto out;
344b0104773SPascal Brand 
34505304565SCedric Chaumont 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
3469b52c538SCedric Chaumont 			res = TEE_AllocateTransientObject(key_type, mks,
347b0104773SPascal Brand 							  &op->key2);
348b0104773SPascal Brand 			if (res != TEE_SUCCESS)
349b66f219bSJens Wiklander 				goto out;
350b0104773SPascal Brand 		}
351b0104773SPascal Brand 	}
352b0104773SPascal Brand 
3532c028fdeSJerome Forissier 	res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1,
354e86f1266SJens Wiklander 				     (unsigned long)op->key2, &op->state);
355b66f219bSJens Wiklander 	if (res != TEE_SUCCESS)
356b66f219bSJens Wiklander 		goto out;
357b0104773SPascal Brand 
35805304565SCedric Chaumont 	/*
35905304565SCedric Chaumont 	 * Initialize digest operations
36005304565SCedric Chaumont 	 * Other multi-stage operations initialized w/ TEE_xxxInit functions
36105304565SCedric Chaumont 	 * Non-applicable on asymmetric operations
36205304565SCedric Chaumont 	 */
36305304565SCedric Chaumont 	if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) {
3642c028fdeSJerome Forissier 		res = _utee_hash_init(op->state, NULL, 0);
36505304565SCedric Chaumont 		if (res != TEE_SUCCESS)
366b66f219bSJens Wiklander 			goto out;
36705304565SCedric Chaumont 		/* v1.1: flags always set for digest operations */
36805304565SCedric Chaumont 		op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
36905304565SCedric Chaumont 	}
37005304565SCedric Chaumont 
371642a1607SCedric Chaumont 	op->operationState = TEE_OPERATION_STATE_INITIAL;
372642a1607SCedric Chaumont 
373b0104773SPascal Brand 	*operation = op;
374b0104773SPascal Brand 
375b66f219bSJens Wiklander out:
376b66f219bSJens Wiklander 	if (res != TEE_SUCCESS) {
377b66f219bSJens Wiklander 		if (res != TEE_ERROR_OUT_OF_MEMORY &&
3789b52c538SCedric Chaumont 		    res != TEE_ERROR_NOT_SUPPORTED)
379b36311adSJerome Forissier 			TEE_Panic(res);
380b66f219bSJens Wiklander 		if (op) {
381b66f219bSJens Wiklander 			if (op->state) {
382b66f219bSJens Wiklander 				TEE_FreeOperation(op);
383b66f219bSJens Wiklander 			} else {
384b66f219bSJens Wiklander 				TEE_Free(op->buffer);
385b66f219bSJens Wiklander 				TEE_FreeTransientObject(op->key1);
386b66f219bSJens Wiklander 				TEE_FreeTransientObject(op->key2);
387b66f219bSJens Wiklander 				TEE_Free(op);
388b66f219bSJens Wiklander 			}
389b66f219bSJens Wiklander 		}
390b66f219bSJens Wiklander 	}
391b66f219bSJens Wiklander 
392b0104773SPascal Brand 	return res;
393b0104773SPascal Brand }
394b0104773SPascal Brand 
395b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation)
396b0104773SPascal Brand {
397e889e80bSCedric Chaumont 	TEE_Result res;
398e889e80bSCedric Chaumont 
399e889e80bSCedric Chaumont 	if (operation == TEE_HANDLE_NULL)
400e889e80bSCedric Chaumont 		TEE_Panic(0);
401e889e80bSCedric Chaumont 
402b0104773SPascal Brand 	/*
403b0104773SPascal Brand 	 * Note that keys should not be freed here, since they are
404b0104773SPascal Brand 	 * claimed by the operation they will be freed by
405b0104773SPascal Brand 	 * utee_cryp_state_free().
406b0104773SPascal Brand 	 */
4072c028fdeSJerome Forissier 	res = _utee_cryp_state_free(operation->state);
408e889e80bSCedric Chaumont 	if (res != TEE_SUCCESS)
409b36311adSJerome Forissier 		TEE_Panic(res);
410e889e80bSCedric Chaumont 
411b0104773SPascal Brand 	TEE_Free(operation->buffer);
412b0104773SPascal Brand 	TEE_Free(operation);
413b0104773SPascal Brand }
414b0104773SPascal Brand 
415b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation,
416b0104773SPascal Brand 			  TEE_OperationInfo *operationInfo)
417b0104773SPascal Brand {
418b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
419b0104773SPascal Brand 		TEE_Panic(0);
420b0104773SPascal Brand 
4216915bbbbSJens Wiklander 	__utee_check_out_annotation(operationInfo, sizeof(*operationInfo));
422b0104773SPascal Brand 
423b0104773SPascal Brand 	*operationInfo = operation->info;
424bac3a8a7SJens Wiklander 	if (operationInfo->handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
425bac3a8a7SJens Wiklander 		operationInfo->keySize = 0;
426bac3a8a7SJens Wiklander 		operationInfo->requiredKeyUsage = 0;
427bac3a8a7SJens Wiklander 	}
428b0104773SPascal Brand }
429b0104773SPascal Brand 
430ee2f75afSJens Wiklander TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle op,
431ee2f75afSJens Wiklander 					TEE_OperationInfoMultiple *op_info,
432ee2f75afSJens Wiklander 					uint32_t *size)
43305304565SCedric Chaumont {
43405304565SCedric Chaumont 	TEE_Result res = TEE_SUCCESS;
435ee2f75afSJens Wiklander 	TEE_ObjectInfo kinfo = { };
436ee2f75afSJens Wiklander 	size_t max_key_count = 0;
437ee2f75afSJens Wiklander 	bool two_keys = false;
43805304565SCedric Chaumont 
439ee2f75afSJens Wiklander 	if (op == TEE_HANDLE_NULL) {
44005304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
44105304565SCedric Chaumont 		goto out;
44205304565SCedric Chaumont 	}
44305304565SCedric Chaumont 
444ee2f75afSJens Wiklander 	__utee_check_outbuf_annotation(op_info, size);
44505304565SCedric Chaumont 
446ee2f75afSJens Wiklander 	if (*size < sizeof(*op_info)) {
447ee2f75afSJens Wiklander 		res = TEE_ERROR_BAD_PARAMETERS;
448ee2f75afSJens Wiklander 		goto out;
449ee2f75afSJens Wiklander 	}
450ee2f75afSJens Wiklander 	max_key_count = (*size - sizeof(*op_info)) /
45105304565SCedric Chaumont 			sizeof(TEE_OperationInfoKey);
45205304565SCedric Chaumont 
453ee2f75afSJens Wiklander 	TEE_MemFill(op_info, 0, *size);
45405304565SCedric Chaumont 
45505304565SCedric Chaumont 	/* Two keys flag (TEE_ALG_AES_XTS only) */
456ee2f75afSJens Wiklander 	two_keys = op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
457ee2f75afSJens Wiklander 
458ee2f75afSJens Wiklander 	if (op->info.mode == TEE_MODE_DIGEST) {
459ee2f75afSJens Wiklander 		op_info->numberOfKeys = 0;
460ee2f75afSJens Wiklander 	} else if (!two_keys) {
461ee2f75afSJens Wiklander 		if (max_key_count < 1) {
46205304565SCedric Chaumont 			res = TEE_ERROR_SHORT_BUFFER;
46305304565SCedric Chaumont 			goto out;
46405304565SCedric Chaumont 		}
46505304565SCedric Chaumont 
466ee2f75afSJens Wiklander 		res = TEE_GetObjectInfo1(op->key1, &kinfo);
467ee2f75afSJens Wiklander 		/* Key1 is not a valid handle, "can't happen". */
468ee2f75afSJens Wiklander 		if (res)
46905304565SCedric Chaumont 			goto out;
47005304565SCedric Chaumont 
471ee2f75afSJens Wiklander 		op_info->keyInformation[0].keySize = kinfo.keySize;
472ee2f75afSJens Wiklander 		op_info->keyInformation[0].requiredKeyUsage =
473ee2f75afSJens Wiklander 			op->info.requiredKeyUsage;
474ee2f75afSJens Wiklander 		op_info->numberOfKeys = 1;
475ee2f75afSJens Wiklander 	} else {
476ee2f75afSJens Wiklander 		if (max_key_count < 2) {
477ee2f75afSJens Wiklander 			res = TEE_ERROR_SHORT_BUFFER;
47805304565SCedric Chaumont 			goto out;
47905304565SCedric Chaumont 		}
48005304565SCedric Chaumont 
481ee2f75afSJens Wiklander 		res = TEE_GetObjectInfo1(op->key1, &kinfo);
482ee2f75afSJens Wiklander 		/* Key1 is not a valid handle, "can't happen". */
483ee2f75afSJens Wiklander 		if (res)
484ee2f75afSJens Wiklander 			goto out;
485ee2f75afSJens Wiklander 
486ee2f75afSJens Wiklander 		op_info->keyInformation[0].keySize = kinfo.keySize;
487ee2f75afSJens Wiklander 		op_info->keyInformation[0].requiredKeyUsage =
488ee2f75afSJens Wiklander 			op->info.requiredKeyUsage;
489ee2f75afSJens Wiklander 
490ee2f75afSJens Wiklander 		res = TEE_GetObjectInfo1(op->key2, &kinfo);
491ee2f75afSJens Wiklander 		/* Key2 is not a valid handle, "can't happen". */
492ee2f75afSJens Wiklander 		if (res)
493ee2f75afSJens Wiklander 			goto out;
494ee2f75afSJens Wiklander 
495ee2f75afSJens Wiklander 		op_info->keyInformation[1].keySize = kinfo.keySize;
496ee2f75afSJens Wiklander 		op_info->keyInformation[1].requiredKeyUsage =
497ee2f75afSJens Wiklander 			op->info.requiredKeyUsage;
498ee2f75afSJens Wiklander 
499ee2f75afSJens Wiklander 		op_info->numberOfKeys = 2;
50005304565SCedric Chaumont 	}
50105304565SCedric Chaumont 
502ee2f75afSJens Wiklander 	op_info->algorithm = op->info.algorithm;
503ee2f75afSJens Wiklander 	op_info->operationClass = op->info.operationClass;
504ee2f75afSJens Wiklander 	op_info->mode = op->info.mode;
505ee2f75afSJens Wiklander 	op_info->digestLength = op->info.digestLength;
506ee2f75afSJens Wiklander 	op_info->maxKeySize = op->info.maxKeySize;
507ee2f75afSJens Wiklander 	op_info->handleState = op->info.handleState;
508ee2f75afSJens Wiklander 	op_info->operationState = op->operationState;
50905304565SCedric Chaumont 
51005304565SCedric Chaumont out:
51105304565SCedric Chaumont 	if (res != TEE_SUCCESS &&
51205304565SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
513b36311adSJerome Forissier 		TEE_Panic(res);
51405304565SCedric Chaumont 
51505304565SCedric Chaumont 	return res;
51605304565SCedric Chaumont }
51705304565SCedric Chaumont 
518b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation)
519b0104773SPascal Brand {
520b0104773SPascal Brand 	TEE_Result res;
521b0104773SPascal Brand 
522b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
523b0104773SPascal Brand 		TEE_Panic(0);
524bf80076aSCedric Chaumont 
525642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET))
526bf80076aSCedric Chaumont 			TEE_Panic(0);
527bf80076aSCedric Chaumont 
528642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
529642a1607SCedric Chaumont 
530b0104773SPascal Brand 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
5312c028fdeSJerome Forissier 		res = _utee_hash_init(operation->state, NULL, 0);
532b0104773SPascal Brand 		if (res != TEE_SUCCESS)
533b0104773SPascal Brand 			TEE_Panic(res);
53405304565SCedric Chaumont 		operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
53505304565SCedric Chaumont 	} else {
536b0104773SPascal Brand 		operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
537b0104773SPascal Brand 	}
53805304565SCedric Chaumont }
539b0104773SPascal Brand 
540b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
541b0104773SPascal Brand 			       TEE_ObjectHandle key)
542b0104773SPascal Brand {
5437583c59eSCedric Chaumont 	TEE_Result res;
544b0104773SPascal Brand 	uint32_t key_size = 0;
545b0104773SPascal Brand 	TEE_ObjectInfo key_info;
546b0104773SPascal Brand 
547a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
548a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
549a57c1e2eSCedric Chaumont 		goto out;
550a57c1e2eSCedric Chaumont 	}
551a57c1e2eSCedric Chaumont 
552642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
553642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
554642a1607SCedric Chaumont 		goto out;
555642a1607SCedric Chaumont 	}
556642a1607SCedric Chaumont 
557a57c1e2eSCedric Chaumont 	if (key == TEE_HANDLE_NULL) {
558a57c1e2eSCedric Chaumont 		/* Operation key cleared */
559a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key1);
5606c4ea258SJens Wiklander 		operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
5616c4ea258SJens Wiklander 		return TEE_SUCCESS;
562a57c1e2eSCedric Chaumont 	}
563a57c1e2eSCedric Chaumont 
564a57c1e2eSCedric Chaumont 	/* No key for digest operation */
565a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
566a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
567a57c1e2eSCedric Chaumont 		goto out;
568a57c1e2eSCedric Chaumont 	}
569a57c1e2eSCedric Chaumont 
570a57c1e2eSCedric Chaumont 	/* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */
571a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
572a57c1e2eSCedric Chaumont 	    0) {
573a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
574a57c1e2eSCedric Chaumont 		goto out;
575a57c1e2eSCedric Chaumont 	}
576a57c1e2eSCedric Chaumont 
5777583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key, &key_info);
578a57c1e2eSCedric Chaumont 	/* Key is not a valid handle */
5797583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
580a57c1e2eSCedric Chaumont 		goto out;
5817583c59eSCedric Chaumont 
582b0104773SPascal Brand 	/* Supplied key has to meet required usage */
583b0104773SPascal Brand 	if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
584b0104773SPascal Brand 	    operation->info.requiredKeyUsage) {
585a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
586a57c1e2eSCedric Chaumont 		goto out;
587b0104773SPascal Brand 	}
588b0104773SPascal Brand 
589a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info.keySize) {
590a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
591a57c1e2eSCedric Chaumont 		goto out;
592a57c1e2eSCedric Chaumont 	}
593b0104773SPascal Brand 
5947583c59eSCedric Chaumont 	key_size = key_info.keySize;
595b0104773SPascal Brand 
596b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
597b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
598b0104773SPascal Brand 
5997583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key);
6007583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
601a57c1e2eSCedric Chaumont 		goto out;
6027583c59eSCedric Chaumont 
603b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
604b0104773SPascal Brand 
605b0104773SPascal Brand 	operation->info.keySize = key_size;
606b0104773SPascal Brand 
6077583c59eSCedric Chaumont out:
608a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
609a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
610a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
611b36311adSJerome Forissier 		TEE_Panic(res);
612a57c1e2eSCedric Chaumont 
613a57c1e2eSCedric Chaumont 	return res;
614b0104773SPascal Brand }
615b0104773SPascal Brand 
616b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
617b0104773SPascal Brand 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
618b0104773SPascal Brand {
6197583c59eSCedric Chaumont 	TEE_Result res;
620b0104773SPascal Brand 	uint32_t key_size = 0;
621b0104773SPascal Brand 	TEE_ObjectInfo key_info1;
622b0104773SPascal Brand 	TEE_ObjectInfo key_info2;
623b0104773SPascal Brand 
624a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
625a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
626a57c1e2eSCedric Chaumont 		goto out;
627a57c1e2eSCedric Chaumont 	}
628a57c1e2eSCedric Chaumont 
629642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
630642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
631642a1607SCedric Chaumont 		goto out;
632642a1607SCedric Chaumont 	}
633642a1607SCedric Chaumont 
634a57c1e2eSCedric Chaumont 	/*
635a57c1e2eSCedric Chaumont 	 * Key1/Key2 and/or are not initialized and
636a57c1e2eSCedric Chaumont 	 * Either both keys are NULL or both are not NULL
637a57c1e2eSCedric Chaumont 	 */
6386c4ea258SJens Wiklander 	if (!key1 && !key2) {
6396c4ea258SJens Wiklander 		/* Clear the keys */
640a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key1);
641a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key2);
6426c4ea258SJens Wiklander 		operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
6436c4ea258SJens Wiklander 		return TEE_SUCCESS;
6446c4ea258SJens Wiklander 	} else if (!key1 || !key2) {
6456c4ea258SJens Wiklander 		/* Both keys are obviously not valid. */
646a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
647a57c1e2eSCedric Chaumont 		goto out;
648a57c1e2eSCedric Chaumont 	}
649a57c1e2eSCedric Chaumont 
650a57c1e2eSCedric Chaumont 	/* No key for digest operation */
651a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
652a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
653a57c1e2eSCedric Chaumont 		goto out;
654a57c1e2eSCedric Chaumont 	}
655a57c1e2eSCedric Chaumont 
6565b385b3fSJerome Forissier 	/* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */
657a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
658a57c1e2eSCedric Chaumont 	    0) {
659a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
660a57c1e2eSCedric Chaumont 		goto out;
661a57c1e2eSCedric Chaumont 	}
662a57c1e2eSCedric Chaumont 
6637583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key1, &key_info1);
664a57c1e2eSCedric Chaumont 	/* Key1 is not a valid handle */
6657583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
666a57c1e2eSCedric Chaumont 		goto out;
6677583c59eSCedric Chaumont 
668b0104773SPascal Brand 	/* Supplied key has to meet required usage */
669b0104773SPascal Brand 	if ((key_info1.objectUsage & operation->info.
670b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
671a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
672a57c1e2eSCedric Chaumont 		goto out;
673b0104773SPascal Brand 	}
674b0104773SPascal Brand 
6757583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key2, &key_info2);
676a57c1e2eSCedric Chaumont 	/* Key2 is not a valid handle */
6777583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
6787583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
6797583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
680a57c1e2eSCedric Chaumont 		goto out;
6817583c59eSCedric Chaumont 	}
6827583c59eSCedric Chaumont 
683b0104773SPascal Brand 	/* Supplied key has to meet required usage */
684b0104773SPascal Brand 	if ((key_info2.objectUsage & operation->info.
685b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
686a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
687a57c1e2eSCedric Chaumont 		goto out;
688b0104773SPascal Brand 	}
689b0104773SPascal Brand 
690b0104773SPascal Brand 	/*
6915b385b3fSJerome Forissier 	 * All the multi key algorithm currently supported requires the keys to
6925b385b3fSJerome Forissier 	 * be of equal size.
693b0104773SPascal Brand 	 */
6945b385b3fSJerome Forissier 	if (key_info1.keySize != key_info2.keySize) {
695a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
696a57c1e2eSCedric Chaumont 		goto out;
697b0104773SPascal Brand 
698a57c1e2eSCedric Chaumont 	}
699a57c1e2eSCedric Chaumont 
700a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info1.keySize) {
701a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
702a57c1e2eSCedric Chaumont 		goto out;
703a57c1e2eSCedric Chaumont 	}
704b0104773SPascal Brand 
705b0104773SPascal Brand 	/*
706b0104773SPascal Brand 	 * Odd that only the size of one key should be reported while
707b0104773SPascal Brand 	 * size of two key are used when allocating the operation.
708b0104773SPascal Brand 	 */
7097583c59eSCedric Chaumont 	key_size = key_info1.keySize;
710b0104773SPascal Brand 
711b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
712b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key2);
713b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
714b0104773SPascal Brand 
7157583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key1);
7167583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
717a57c1e2eSCedric Chaumont 		goto out;
7187583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key2, key2);
7197583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
7207583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
7217583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
722a57c1e2eSCedric Chaumont 		goto out;
7237583c59eSCedric Chaumont 	}
7247583c59eSCedric Chaumont 
725b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
726b0104773SPascal Brand 
727b0104773SPascal Brand 	operation->info.keySize = key_size;
728b0104773SPascal Brand 
7297583c59eSCedric Chaumont out:
730a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
731a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
732a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT_2 &&
733a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE &&
734a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
735b36311adSJerome Forissier 		TEE_Panic(res);
736a57c1e2eSCedric Chaumont 
737a57c1e2eSCedric Chaumont 	return res;
738b0104773SPascal Brand }
739b0104773SPascal Brand 
740b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
741b0104773SPascal Brand {
742b0104773SPascal Brand 	TEE_Result res;
743b0104773SPascal Brand 
744b0104773SPascal Brand 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
745b0104773SPascal Brand 		TEE_Panic(0);
746b0104773SPascal Brand 	if (dst_op->info.algorithm != src_op->info.algorithm)
747b0104773SPascal Brand 		TEE_Panic(0);
7488734de30SJens Wiklander 	if (dst_op->info.mode != src_op->info.mode)
7498734de30SJens Wiklander 		TEE_Panic(0);
750b0104773SPascal Brand 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
751b0104773SPascal Brand 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
752b0104773SPascal Brand 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
753b0104773SPascal Brand 
754b0104773SPascal Brand 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
755b0104773SPascal Brand 			key1 = src_op->key1;
756b0104773SPascal Brand 			key2 = src_op->key2;
757b0104773SPascal Brand 		}
758b0104773SPascal Brand 
759b0104773SPascal Brand 		if ((src_op->info.handleState &
760b0104773SPascal Brand 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
761b0104773SPascal Brand 			TEE_SetOperationKey(dst_op, key1);
762b0104773SPascal Brand 		} else {
763b0104773SPascal Brand 			TEE_SetOperationKey2(dst_op, key1, key2);
764b0104773SPascal Brand 		}
765b0104773SPascal Brand 	}
766b0104773SPascal Brand 	dst_op->info.handleState = src_op->info.handleState;
767b0104773SPascal Brand 	dst_op->info.keySize = src_op->info.keySize;
7688e07702eSJens Wiklander 	dst_op->info.digestLength = src_op->info.digestLength;
769642a1607SCedric Chaumont 	dst_op->operationState = src_op->operationState;
770b0104773SPascal Brand 
771b0104773SPascal Brand 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
772b0104773SPascal Brand 	    dst_op->block_size != src_op->block_size)
773b0104773SPascal Brand 		TEE_Panic(0);
774b0104773SPascal Brand 
775b0104773SPascal Brand 	if (dst_op->buffer != NULL) {
776b0104773SPascal Brand 		if (src_op->buffer == NULL)
777b0104773SPascal Brand 			TEE_Panic(0);
778b0104773SPascal Brand 
779b0104773SPascal Brand 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
780b0104773SPascal Brand 		dst_op->buffer_offs = src_op->buffer_offs;
781b0104773SPascal Brand 	} else if (src_op->buffer != NULL) {
782b0104773SPascal Brand 		TEE_Panic(0);
783b0104773SPascal Brand 	}
784b0104773SPascal Brand 
7852c028fdeSJerome Forissier 	res = _utee_cryp_state_copy(dst_op->state, src_op->state);
786b0104773SPascal Brand 	if (res != TEE_SUCCESS)
787b0104773SPascal Brand 		TEE_Panic(res);
788b0104773SPascal Brand }
789b0104773SPascal Brand 
790b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */
791b0104773SPascal Brand 
7928f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV,
7936d15db08SJerome Forissier 				uint32_t IVLen)
7946d15db08SJerome Forissier {
7956d15db08SJerome Forissier 	TEE_Result res;
7966d15db08SJerome Forissier 
7976d15db08SJerome Forissier 	/*
7986d15db08SJerome Forissier 	 * Note : IV and IVLen are never used in current implementation
7996d15db08SJerome Forissier 	 * This is why coherent values of IV and IVLen are not checked
8006d15db08SJerome Forissier 	 */
8012c028fdeSJerome Forissier 	res = _utee_hash_init(operation->state, IV, IVLen);
8026d15db08SJerome Forissier 	if (res != TEE_SUCCESS)
8036d15db08SJerome Forissier 		TEE_Panic(res);
8046d15db08SJerome Forissier 	operation->buffer_offs = 0;
8056d15db08SJerome Forissier 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
8066d15db08SJerome Forissier }
8076d15db08SJerome Forissier 
808b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation,
8098f07fe6fSJerome Forissier 		      const void *chunk, uint32_t chunkSize)
810b0104773SPascal Brand {
81173d6c3baSJoakim Bech 	TEE_Result res = TEE_ERROR_GENERIC;
812b0104773SPascal Brand 
81373d6c3baSJoakim Bech 	if (operation == TEE_HANDLE_NULL ||
81473d6c3baSJoakim Bech 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
815b0104773SPascal Brand 		TEE_Panic(0);
81673d6c3baSJoakim Bech 
817642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
818642a1607SCedric Chaumont 
8192c028fdeSJerome Forissier 	res = _utee_hash_update(operation->state, chunk, chunkSize);
820b0104773SPascal Brand 	if (res != TEE_SUCCESS)
821b0104773SPascal Brand 		TEE_Panic(res);
822b0104773SPascal Brand }
823b0104773SPascal Brand 
8248f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
82579a3c601SCedric Chaumont 			     uint32_t chunkLen, void *hash, uint32_t *hashLen)
826b0104773SPascal Brand {
82787c2f6b6SCedric Chaumont 	TEE_Result res;
828e86f1266SJens Wiklander 	uint64_t hl;
82987c2f6b6SCedric Chaumont 
83087c2f6b6SCedric Chaumont 	if ((operation == TEE_HANDLE_NULL) ||
83187c2f6b6SCedric Chaumont 	    (!chunk && chunkLen) ||
83287c2f6b6SCedric Chaumont 	    (operation->info.operationClass != TEE_OPERATION_DIGEST)) {
83387c2f6b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
83487c2f6b6SCedric Chaumont 		goto out;
83587c2f6b6SCedric Chaumont 	}
8366915bbbbSJens Wiklander 	__utee_check_inout_annotation(hashLen, sizeof(*hashLen));
83787c2f6b6SCedric Chaumont 
838e86f1266SJens Wiklander 	hl = *hashLen;
8392c028fdeSJerome Forissier 	res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl);
840e86f1266SJens Wiklander 	*hashLen = hl;
8416d15db08SJerome Forissier 	if (res != TEE_SUCCESS)
8426d15db08SJerome Forissier 		goto out;
8436d15db08SJerome Forissier 
8446d15db08SJerome Forissier 	/* Reset operation state */
8456d15db08SJerome Forissier 	init_hash_operation(operation, NULL, 0);
84687c2f6b6SCedric Chaumont 
847642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
848642a1607SCedric Chaumont 
84987c2f6b6SCedric Chaumont out:
85087c2f6b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
85187c2f6b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
852b36311adSJerome Forissier 		TEE_Panic(res);
85373d6c3baSJoakim Bech 
85487c2f6b6SCedric Chaumont 	return res;
855b0104773SPascal Brand }
856b0104773SPascal Brand 
857b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */
858b0104773SPascal Brand 
8598f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV,
8608f07fe6fSJerome Forissier 		    uint32_t IVLen)
861b0104773SPascal Brand {
862b0104773SPascal Brand 	TEE_Result res;
863b0104773SPascal Brand 
864b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
865b0104773SPascal Brand 		TEE_Panic(0);
866642a1607SCedric Chaumont 
867b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
868b0104773SPascal Brand 		TEE_Panic(0);
869642a1607SCedric Chaumont 
870642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
871642a1607SCedric Chaumont 	    !(operation->key1))
872642a1607SCedric Chaumont 		TEE_Panic(0);
873642a1607SCedric Chaumont 
874642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
875642a1607SCedric Chaumont 		TEE_ResetOperation(operation);
876642a1607SCedric Chaumont 
877ad7aa2a5SSadiq Hussain 	if (IV && IVLen) {
878ad7aa2a5SSadiq Hussain 		if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
879ad7aa2a5SSadiq Hussain 		    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
880ad7aa2a5SSadiq Hussain 		    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
881ad7aa2a5SSadiq Hussain 		    operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD)
882ad7aa2a5SSadiq Hussain 			TEE_Panic(0);
883ad7aa2a5SSadiq Hussain 	}
884ad7aa2a5SSadiq Hussain 
885642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
886642a1607SCedric Chaumont 
8872c028fdeSJerome Forissier 	res = _utee_cipher_init(operation->state, IV, IVLen);
888b0104773SPascal Brand 	if (res != TEE_SUCCESS)
889b0104773SPascal Brand 		TEE_Panic(res);
890642a1607SCedric Chaumont 
891b0104773SPascal Brand 	operation->buffer_offs = 0;
892b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
893b0104773SPascal Brand }
894b0104773SPascal Brand 
895b0104773SPascal Brand static TEE_Result tee_buffer_update(
896b0104773SPascal Brand 		TEE_OperationHandle op,
897e86f1266SJens Wiklander 		TEE_Result(*update_func)(unsigned long state, const void *src,
898e86f1266SJens Wiklander 				size_t slen, void *dst, uint64_t *dlen),
899b0104773SPascal Brand 		const void *src_data, size_t src_len,
900e86f1266SJens Wiklander 		void *dest_data, uint64_t *dest_len)
901b0104773SPascal Brand {
902b0104773SPascal Brand 	TEE_Result res;
903b0104773SPascal Brand 	const uint8_t *src = src_data;
904b0104773SPascal Brand 	size_t slen = src_len;
905b0104773SPascal Brand 	uint8_t *dst = dest_data;
906b0104773SPascal Brand 	size_t dlen = *dest_len;
907b0104773SPascal Brand 	size_t acc_dlen = 0;
908e86f1266SJens Wiklander 	uint64_t tmp_dlen;
909b0104773SPascal Brand 	size_t l;
910b0104773SPascal Brand 	size_t buffer_size;
911d3588802SPascal Brand 	size_t buffer_left;
912b0104773SPascal Brand 
913090268f5SJens Wiklander 	if (!src) {
914090268f5SJens Wiklander 		if (slen)
915090268f5SJens Wiklander 			TEE_Panic(0);
916090268f5SJens Wiklander 		goto out;
917090268f5SJens Wiklander 	}
918090268f5SJens Wiklander 
919d3588802SPascal Brand 	if (op->buffer_two_blocks) {
920b0104773SPascal Brand 		buffer_size = op->block_size * 2;
921d3588802SPascal Brand 		buffer_left = 1;
922d3588802SPascal Brand 	} else {
923b0104773SPascal Brand 		buffer_size = op->block_size;
924d3588802SPascal Brand 		buffer_left = 0;
925d3588802SPascal Brand 	}
926b0104773SPascal Brand 
927b0104773SPascal Brand 	if (op->buffer_offs > 0) {
928b0104773SPascal Brand 		/* Fill up complete block */
929b0104773SPascal Brand 		if (op->buffer_offs < op->block_size)
930b0104773SPascal Brand 			l = MIN(slen, op->block_size - op->buffer_offs);
931b0104773SPascal Brand 		else
932b0104773SPascal Brand 			l = MIN(slen, buffer_size - op->buffer_offs);
933b0104773SPascal Brand 		memcpy(op->buffer + op->buffer_offs, src, l);
934b0104773SPascal Brand 		op->buffer_offs += l;
935b0104773SPascal Brand 		src += l;
936b0104773SPascal Brand 		slen -= l;
937b0104773SPascal Brand 		if ((op->buffer_offs % op->block_size) != 0)
938b0104773SPascal Brand 			goto out;	/* Nothing left to do */
939b0104773SPascal Brand 	}
940b0104773SPascal Brand 
941b0104773SPascal Brand 	/* If we can feed from buffer */
942d3588802SPascal Brand 	if ((op->buffer_offs > 0) &&
943d3588802SPascal Brand 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
9442ff3fdbbSPascal Brand 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
945b0104773SPascal Brand 				op->block_size);
946b0104773SPascal Brand 		l = MIN(op->buffer_offs, l);
947b0104773SPascal Brand 		tmp_dlen = dlen;
948b0104773SPascal Brand 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
949b0104773SPascal Brand 		if (res != TEE_SUCCESS)
950b0104773SPascal Brand 			TEE_Panic(res);
951b0104773SPascal Brand 		dst += tmp_dlen;
952b0104773SPascal Brand 		dlen -= tmp_dlen;
953b0104773SPascal Brand 		acc_dlen += tmp_dlen;
954b0104773SPascal Brand 		op->buffer_offs -= l;
955b0104773SPascal Brand 		if (op->buffer_offs > 0) {
956b0104773SPascal Brand 			/*
957b0104773SPascal Brand 			 * Slen is small enough to be contained in rest buffer.
958b0104773SPascal Brand 			 */
959b0104773SPascal Brand 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
960b0104773SPascal Brand 			memcpy(op->buffer + op->buffer_offs, src, slen);
961b0104773SPascal Brand 			op->buffer_offs += slen;
962b0104773SPascal Brand 			goto out;	/* Nothing left to do */
963b0104773SPascal Brand 		}
964b0104773SPascal Brand 	}
965b0104773SPascal Brand 
966d3588802SPascal Brand 	if (slen >= (buffer_size + buffer_left)) {
967b0104773SPascal Brand 		/* Buffer is empty, feed as much as possible from src */
968bf7a587fSJerome Forissier 		if (op->info.algorithm == TEE_ALG_AES_CTS)
969b1ecda78SJerome Forissier 			l = ROUNDUP(slen - buffer_size, op->block_size);
970bf7a587fSJerome Forissier 		else
971bf7a587fSJerome Forissier 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
972b0104773SPascal Brand 
973b0104773SPascal Brand 		tmp_dlen = dlen;
974b0104773SPascal Brand 		res = update_func(op->state, src, l, dst, &tmp_dlen);
975b0104773SPascal Brand 		if (res != TEE_SUCCESS)
976b0104773SPascal Brand 			TEE_Panic(res);
977b0104773SPascal Brand 		src += l;
978b0104773SPascal Brand 		slen -= l;
979b0104773SPascal Brand 		dst += tmp_dlen;
980b0104773SPascal Brand 		dlen -= tmp_dlen;
981b0104773SPascal Brand 		acc_dlen += tmp_dlen;
982b0104773SPascal Brand 	}
983b0104773SPascal Brand 
984b0104773SPascal Brand 	/* Slen is small enough to be contained in buffer. */
985b0104773SPascal Brand 	memcpy(op->buffer + op->buffer_offs, src, slen);
986b0104773SPascal Brand 	op->buffer_offs += slen;
987b0104773SPascal Brand 
988b0104773SPascal Brand out:
989b0104773SPascal Brand 	*dest_len = acc_dlen;
990b0104773SPascal Brand 	return TEE_SUCCESS;
991b0104773SPascal Brand }
992b0104773SPascal Brand 
9938f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData,
99479a3c601SCedric Chaumont 			    uint32_t srcLen, void *destData, uint32_t *destLen)
995b0104773SPascal Brand {
996dea1f2b6SCedric Chaumont 	TEE_Result res;
997b0104773SPascal Brand 	size_t req_dlen;
998e86f1266SJens Wiklander 	uint64_t dl;
999b0104773SPascal Brand 
10006915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1001dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1002dea1f2b6SCedric Chaumont 		goto out;
1003dea1f2b6SCedric Chaumont 	}
10046915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1005dea1f2b6SCedric Chaumont 
1006642a1607SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1007dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1008dea1f2b6SCedric Chaumont 		goto out;
1009dea1f2b6SCedric Chaumont 	}
1010dea1f2b6SCedric Chaumont 
1011642a1607SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1012642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1013642a1607SCedric Chaumont 		goto out;
1014642a1607SCedric Chaumont 	}
1015642a1607SCedric Chaumont 
1016642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1017dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1018dea1f2b6SCedric Chaumont 		goto out;
1019dea1f2b6SCedric Chaumont 	}
1020b0104773SPascal Brand 
1021e32c5ddfSJerome Forissier 	if (!srcData && !srcLen) {
1022090268f5SJens Wiklander 		*destLen = 0;
1023e32c5ddfSJerome Forissier 		res = TEE_SUCCESS;
1024e32c5ddfSJerome Forissier 		goto out;
1025e32c5ddfSJerome Forissier 	}
1026e32c5ddfSJerome Forissier 
1027b0104773SPascal Brand 	/* Calculate required dlen */
102857aabac5SBogdan Liulko 	if (operation->block_size > 1) {
102957aabac5SBogdan Liulko 		req_dlen = ((operation->buffer_offs + srcLen) /
103057aabac5SBogdan Liulko 			    operation->block_size) * operation->block_size;
103157aabac5SBogdan Liulko 	} else {
103257aabac5SBogdan Liulko 		req_dlen = srcLen;
103357aabac5SBogdan Liulko 	}
1034642a1607SCedric Chaumont 	if (operation->buffer_two_blocks) {
1035642a1607SCedric Chaumont 		if (req_dlen > operation->block_size * 2)
1036642a1607SCedric Chaumont 			req_dlen -= operation->block_size * 2;
1037b0104773SPascal Brand 		else
1038b0104773SPascal Brand 			req_dlen = 0;
1039b0104773SPascal Brand 	}
1040b0104773SPascal Brand 	/*
1041b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1042b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1043b0104773SPascal Brand 	 * can't restore sync with this API.
1044b0104773SPascal Brand 	 */
1045b0104773SPascal Brand 	if (*destLen < req_dlen) {
1046b0104773SPascal Brand 		*destLen = req_dlen;
1047dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1048dea1f2b6SCedric Chaumont 		goto out;
1049b0104773SPascal Brand 	}
1050b0104773SPascal Brand 
1051e86f1266SJens Wiklander 	dl = *destLen;
105257aabac5SBogdan Liulko 	if (operation->block_size > 1) {
10532c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_cipher_update, srcData,
105457aabac5SBogdan Liulko 					srcLen, destData, &dl);
105557aabac5SBogdan Liulko 	} else {
105657aabac5SBogdan Liulko 		if (srcLen > 0) {
10572c028fdeSJerome Forissier 			res = _utee_cipher_update(operation->state, srcData,
105857aabac5SBogdan Liulko 						  srcLen, destData, &dl);
105957aabac5SBogdan Liulko 		} else {
106057aabac5SBogdan Liulko 			res = TEE_SUCCESS;
106157aabac5SBogdan Liulko 			dl = 0;
106257aabac5SBogdan Liulko 		}
106357aabac5SBogdan Liulko 	}
1064e86f1266SJens Wiklander 	*destLen = dl;
1065b0104773SPascal Brand 
1066dea1f2b6SCedric Chaumont out:
1067dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1068dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1069b36311adSJerome Forissier 		TEE_Panic(res);
1070dea1f2b6SCedric Chaumont 
1071dea1f2b6SCedric Chaumont 	return res;
1072b0104773SPascal Brand }
1073b0104773SPascal Brand 
1074642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation,
10758f07fe6fSJerome Forissier 			     const void *srcData, uint32_t srcLen,
10768f07fe6fSJerome Forissier 			     void *destData, uint32_t *destLen)
1077b0104773SPascal Brand {
10786915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1079b0104773SPascal Brand 	uint8_t *dst = destData;
1080b0104773SPascal Brand 	size_t acc_dlen = 0;
10816915bbbbSJens Wiklander 	uint64_t tmp_dlen = 0;
10826915bbbbSJens Wiklander 	size_t req_dlen = 0;
1083b0104773SPascal Brand 
10846915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1085dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1086dea1f2b6SCedric Chaumont 		goto out;
1087dea1f2b6SCedric Chaumont 	}
10886915bbbbSJens Wiklander 	if (destLen)
10896915bbbbSJens Wiklander 		__utee_check_inout_annotation(destLen, sizeof(*destLen));
1090dea1f2b6SCedric Chaumont 
1091642a1607SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1092dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1093dea1f2b6SCedric Chaumont 		goto out;
1094dea1f2b6SCedric Chaumont 	}
1095dea1f2b6SCedric Chaumont 
1096642a1607SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1097642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1098642a1607SCedric Chaumont 		goto out;
1099642a1607SCedric Chaumont 	}
1100642a1607SCedric Chaumont 
1101642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1102dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1103dea1f2b6SCedric Chaumont 		goto out;
1104dea1f2b6SCedric Chaumont 	}
1105b0104773SPascal Brand 
1106b0104773SPascal Brand 	/*
1107b0104773SPascal Brand 	 * Check that the final block doesn't require padding for those
1108b0104773SPascal Brand 	 * algorithms that requires client to supply padding.
1109b0104773SPascal Brand 	 */
1110642a1607SCedric Chaumont 	if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
1111642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1112642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
1113642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1114642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
1115ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD ||
1116ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD ||
1117ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) {
1118642a1607SCedric Chaumont 		if (((operation->buffer_offs + srcLen) % operation->block_size)
1119642a1607SCedric Chaumont 		    != 0) {
1120dea1f2b6SCedric Chaumont 			res = TEE_ERROR_BAD_PARAMETERS;
1121dea1f2b6SCedric Chaumont 			goto out;
1122dea1f2b6SCedric Chaumont 		}
1123b0104773SPascal Brand 	}
1124b0104773SPascal Brand 
1125b0104773SPascal Brand 	/*
1126b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1127b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1128b0104773SPascal Brand 	 * can't restore sync with this API.
1129b0104773SPascal Brand 	 */
113057aabac5SBogdan Liulko 	if (operation->block_size > 1) {
1131642a1607SCedric Chaumont 		req_dlen = operation->buffer_offs + srcLen;
113257aabac5SBogdan Liulko 	} else {
113357aabac5SBogdan Liulko 		req_dlen = srcLen;
113457aabac5SBogdan Liulko 	}
11356915bbbbSJens Wiklander 	if (destLen)
11366915bbbbSJens Wiklander 		tmp_dlen = *destLen;
11376915bbbbSJens Wiklander 	if (tmp_dlen < req_dlen) {
11386915bbbbSJens Wiklander 		if (destLen)
1139b0104773SPascal Brand 			*destLen = req_dlen;
1140dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1141dea1f2b6SCedric Chaumont 		goto out;
1142b0104773SPascal Brand 	}
1143b0104773SPascal Brand 
114457aabac5SBogdan Liulko 	if (operation->block_size > 1) {
1145dea9063eSJens Wiklander 		if (srcLen) {
11462c028fdeSJerome Forissier 			res = tee_buffer_update(operation, _utee_cipher_update,
1147dea9063eSJens Wiklander 						srcData, srcLen, dst,
1148dea9063eSJens Wiklander 						&tmp_dlen);
1149dea1f2b6SCedric Chaumont 			if (res != TEE_SUCCESS)
1150dea1f2b6SCedric Chaumont 				goto out;
1151dea1f2b6SCedric Chaumont 
1152b0104773SPascal Brand 			dst += tmp_dlen;
1153b0104773SPascal Brand 			acc_dlen += tmp_dlen;
1154b0104773SPascal Brand 
1155b0104773SPascal Brand 			tmp_dlen = *destLen - acc_dlen;
1156dea9063eSJens Wiklander 		}
11572c028fdeSJerome Forissier 		res = _utee_cipher_final(operation->state, operation->buffer,
11582c028fdeSJerome Forissier 					 operation->buffer_offs, dst,
11592c028fdeSJerome Forissier 					 &tmp_dlen);
116057aabac5SBogdan Liulko 	} else {
11612c028fdeSJerome Forissier 		res = _utee_cipher_final(operation->state, srcData, srcLen, dst,
11622c028fdeSJerome Forissier 					 &tmp_dlen);
116357aabac5SBogdan Liulko 	}
1164b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1165dea1f2b6SCedric Chaumont 		goto out;
1166dea1f2b6SCedric Chaumont 
1167b0104773SPascal Brand 	acc_dlen += tmp_dlen;
11686915bbbbSJens Wiklander 	if (destLen)
1169b0104773SPascal Brand 		*destLen = acc_dlen;
1170dea1f2b6SCedric Chaumont 
1171642a1607SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1172642a1607SCedric Chaumont 
1173642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1174642a1607SCedric Chaumont 
1175dea1f2b6SCedric Chaumont out:
1176dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1177dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1178b36311adSJerome Forissier 		TEE_Panic(res);
1179dea1f2b6SCedric Chaumont 
1180dea1f2b6SCedric Chaumont 	return res;
1181b0104773SPascal Brand }
1182b0104773SPascal Brand 
1183b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */
1184b0104773SPascal Brand 
11858f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
1186b0104773SPascal Brand {
1187b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
1188b0104773SPascal Brand 		TEE_Panic(0);
1189642a1607SCedric Chaumont 
1190b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1191b0104773SPascal Brand 		TEE_Panic(0);
1192642a1607SCedric Chaumont 
1193642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1194642a1607SCedric Chaumont 	    !(operation->key1))
1195642a1607SCedric Chaumont 		TEE_Panic(0);
1196642a1607SCedric Chaumont 
1197642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1198642a1607SCedric Chaumont 		TEE_ResetOperation(operation);
1199642a1607SCedric Chaumont 
1200642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1201642a1607SCedric Chaumont 
12026d15db08SJerome Forissier 	init_hash_operation(operation, IV, IVLen);
1203b0104773SPascal Brand }
1204b0104773SPascal Brand 
12058f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk,
120628e0efc6SCedric Chaumont 		   uint32_t chunkSize)
1207b0104773SPascal Brand {
1208b0104773SPascal Brand 	TEE_Result res;
1209b0104773SPascal Brand 
121028e0efc6SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
1211b0104773SPascal Brand 		TEE_Panic(0);
1212642a1607SCedric Chaumont 
121328e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1214b0104773SPascal Brand 		TEE_Panic(0);
1215642a1607SCedric Chaumont 
121628e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1217b0104773SPascal Brand 		TEE_Panic(0);
1218b0104773SPascal Brand 
1219642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE)
1220642a1607SCedric Chaumont 		TEE_Panic(0);
1221642a1607SCedric Chaumont 
12222c028fdeSJerome Forissier 	res = _utee_hash_update(operation->state, chunk, chunkSize);
1223b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1224b0104773SPascal Brand 		TEE_Panic(res);
1225b0104773SPascal Brand }
1226b0104773SPascal Brand 
122728e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
12288f07fe6fSJerome Forissier 			       const void *message, uint32_t messageLen,
122979a3c601SCedric Chaumont 			       void *mac, uint32_t *macLen)
1230b0104773SPascal Brand {
1231b0104773SPascal Brand 	TEE_Result res;
1232e86f1266SJens Wiklander 	uint64_t ml;
1233b0104773SPascal Brand 
12346915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!message && messageLen)) {
123528e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
123628e0efc6SCedric Chaumont 		goto out;
123728e0efc6SCedric Chaumont 	}
12386915bbbbSJens Wiklander 	__utee_check_inout_annotation(macLen, sizeof(*macLen));
1239b0104773SPascal Brand 
124028e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
124128e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
124228e0efc6SCedric Chaumont 		goto out;
124328e0efc6SCedric Chaumont 	}
124428e0efc6SCedric Chaumont 
124528e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
124628e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
124728e0efc6SCedric Chaumont 		goto out;
124828e0efc6SCedric Chaumont 	}
124928e0efc6SCedric Chaumont 
1250642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1251642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1252642a1607SCedric Chaumont 		goto out;
1253642a1607SCedric Chaumont 	}
1254642a1607SCedric Chaumont 
1255e86f1266SJens Wiklander 	ml = *macLen;
12562c028fdeSJerome Forissier 	res = _utee_hash_final(operation->state, message, messageLen, mac, &ml);
1257e86f1266SJens Wiklander 	*macLen = ml;
125828e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS)
125928e0efc6SCedric Chaumont 		goto out;
126028e0efc6SCedric Chaumont 
126128e0efc6SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
126228e0efc6SCedric Chaumont 
1263642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1264642a1607SCedric Chaumont 
126528e0efc6SCedric Chaumont out:
126628e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
126728e0efc6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
126828e0efc6SCedric Chaumont 		TEE_Panic(res);
126928e0efc6SCedric Chaumont 
1270b0104773SPascal Brand 	return res;
1271b0104773SPascal Brand }
1272b0104773SPascal Brand 
1273b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
12748f07fe6fSJerome Forissier 			       const void *message, uint32_t messageLen,
12758f07fe6fSJerome Forissier 			       const void *mac, uint32_t macLen)
1276b0104773SPascal Brand {
1277b0104773SPascal Brand 	TEE_Result res;
1278ee4ba3d1SVictor Chong 	uint8_t computed_mac[TEE_MAX_HASH_SIZE] = { 0 };
12797f74c64aSPascal Brand 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1280b0104773SPascal Brand 
128128e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
128228e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
128328e0efc6SCedric Chaumont 		goto out;
128428e0efc6SCedric Chaumont 	}
128528e0efc6SCedric Chaumont 
128628e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
128728e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
128828e0efc6SCedric Chaumont 		goto out;
128928e0efc6SCedric Chaumont 	}
129028e0efc6SCedric Chaumont 
1291642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1292642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1293642a1607SCedric Chaumont 		goto out;
1294642a1607SCedric Chaumont 	}
1295642a1607SCedric Chaumont 
1296b0104773SPascal Brand 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1297b0104773SPascal Brand 				  &computed_mac_size);
1298b0104773SPascal Brand 	if (res != TEE_SUCCESS)
129928e0efc6SCedric Chaumont 		goto out;
130028e0efc6SCedric Chaumont 
130128e0efc6SCedric Chaumont 	if (computed_mac_size != macLen) {
130228e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
130328e0efc6SCedric Chaumont 		goto out;
130428e0efc6SCedric Chaumont 	}
130528e0efc6SCedric Chaumont 
130648e10604SJerome Forissier 	if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) {
130728e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
130828e0efc6SCedric Chaumont 		goto out;
130928e0efc6SCedric Chaumont 	}
131028e0efc6SCedric Chaumont 
1311642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1312642a1607SCedric Chaumont 
131328e0efc6SCedric Chaumont out:
131428e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
131528e0efc6SCedric Chaumont 	    res != TEE_ERROR_MAC_INVALID)
131628e0efc6SCedric Chaumont 		TEE_Panic(res);
131728e0efc6SCedric Chaumont 
1318b0104773SPascal Brand 	return res;
1319b0104773SPascal Brand }
1320b0104773SPascal Brand 
1321b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */
1322b0104773SPascal Brand 
13238f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce,
132479a3c601SCedric Chaumont 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1325b0104773SPascal Brand 		      uint32_t payloadLen)
1326b0104773SPascal Brand {
1327b0104773SPascal Brand 	TEE_Result res;
1328b0104773SPascal Brand 
1329b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || nonce == NULL) {
1330b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1331b5816c88SCedric Chaumont 		goto out;
1332b5816c88SCedric Chaumont 	}
1333b5816c88SCedric Chaumont 
1334b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1335b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1336b5816c88SCedric Chaumont 		goto out;
1337b5816c88SCedric Chaumont 	}
1338b0104773SPascal Brand 
1339642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
1340642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1341642a1607SCedric Chaumont 		goto out;
1342642a1607SCedric Chaumont 	}
1343642a1607SCedric Chaumont 
1344b0104773SPascal Brand 	/*
1345b0104773SPascal Brand 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1346b0104773SPascal Brand 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1347b0104773SPascal Brand 	 * according to the same principle so we have to check here instead to
1348b0104773SPascal Brand 	 * be GP compliant.
1349b0104773SPascal Brand 	 */
1350b5816c88SCedric Chaumont 	if (operation->info.algorithm == TEE_ALG_AES_GCM) {
1351b0104773SPascal Brand 		/*
1352b0104773SPascal Brand 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1353b0104773SPascal Brand 		 */
1354b5816c88SCedric Chaumont 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) {
1355b5816c88SCedric Chaumont 			res = TEE_ERROR_NOT_SUPPORTED;
1356b5816c88SCedric Chaumont 			goto out;
1357b5816c88SCedric Chaumont 		}
1358b0104773SPascal Brand 	}
1359b0104773SPascal Brand 
13602c028fdeSJerome Forissier 	res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8,
13612c028fdeSJerome Forissier 				 AADLen, payloadLen);
1362b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1363b5816c88SCedric Chaumont 		goto out;
1364b5816c88SCedric Chaumont 
13657acaf5adSAlbert Schwarzkopf 	operation->info.digestLength = tagLen / 8;
1366f2674567SSumit Garg 	operation->buffer_offs = 0;
1367b5816c88SCedric Chaumont 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1368b5816c88SCedric Chaumont 
1369b5816c88SCedric Chaumont out:
1370b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1371b5816c88SCedric Chaumont 	    res != TEE_ERROR_NOT_SUPPORTED)
1372b0104773SPascal Brand 			TEE_Panic(res);
1373b5816c88SCedric Chaumont 
1374b0104773SPascal Brand 	return res;
1375b0104773SPascal Brand }
1376b0104773SPascal Brand 
13778f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata,
137879a3c601SCedric Chaumont 		     uint32_t AADdataLen)
1379b0104773SPascal Brand {
1380b0104773SPascal Brand 	TEE_Result res;
1381b0104773SPascal Brand 
1382b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1383b5816c88SCedric Chaumont 	    (AADdata == NULL && AADdataLen != 0))
1384b0104773SPascal Brand 		TEE_Panic(0);
1385642a1607SCedric Chaumont 
1386b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE)
1387b0104773SPascal Brand 		TEE_Panic(0);
1388642a1607SCedric Chaumont 
1389b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1390b0104773SPascal Brand 		TEE_Panic(0);
1391b0104773SPascal Brand 
13922c028fdeSJerome Forissier 	res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen);
1393642a1607SCedric Chaumont 
1394642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1395642a1607SCedric Chaumont 
1396b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1397b0104773SPascal Brand 		TEE_Panic(res);
1398b0104773SPascal Brand }
1399b0104773SPascal Brand 
14008f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData,
140179a3c601SCedric Chaumont 			uint32_t srcLen, void *destData, uint32_t *destLen)
1402b0104773SPascal Brand {
14036915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
14046915bbbbSJens Wiklander 	size_t req_dlen = 0;
14056915bbbbSJens Wiklander 	uint64_t dl = 0;
1406b0104773SPascal Brand 
14076915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1408b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1409b5816c88SCedric Chaumont 		goto out;
1410b5816c88SCedric Chaumont 	}
14116915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1412b5816c88SCedric Chaumont 
1413b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1414b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1415b5816c88SCedric Chaumont 		goto out;
1416b5816c88SCedric Chaumont 	}
1417b5816c88SCedric Chaumont 
1418b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1419b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1420b5816c88SCedric Chaumont 		goto out;
1421b5816c88SCedric Chaumont 	}
1422b0104773SPascal Brand 
1423827308b8SJerome Forissier 	if (!srcData && !srcLen) {
1424090268f5SJens Wiklander 		*destLen = 0;
1425827308b8SJerome Forissier 		res = TEE_SUCCESS;
1426827308b8SJerome Forissier 		goto out;
1427827308b8SJerome Forissier 	}
1428827308b8SJerome Forissier 
1429b0104773SPascal Brand 	/*
1430b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1431b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1432b0104773SPascal Brand 	 * can't restore sync with this API.
1433b0104773SPascal Brand 	 */
1434afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
1435b5816c88SCedric Chaumont 		req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen,
1436b5816c88SCedric Chaumont 				     operation->block_size);
1437afc0c182SBogdan Liulko 	} else {
1438afc0c182SBogdan Liulko 		req_dlen = srcLen;
1439afc0c182SBogdan Liulko 	}
1440afc0c182SBogdan Liulko 
14416915bbbbSJens Wiklander 	dl = *destLen;
14426915bbbbSJens Wiklander 	if (dl < req_dlen) {
1443b0104773SPascal Brand 		*destLen = req_dlen;
1444b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1445b5816c88SCedric Chaumont 		goto out;
1446b0104773SPascal Brand 	}
1447b0104773SPascal Brand 
1448afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
14492c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1450afc0c182SBogdan Liulko 					srcData, srcLen, destData, &dl);
1451afc0c182SBogdan Liulko 	} else {
1452afc0c182SBogdan Liulko 		if (srcLen > 0) {
14532c028fdeSJerome Forissier 			res = _utee_authenc_update_payload(operation->state,
1454afc0c182SBogdan Liulko 							   srcData, srcLen,
1455afc0c182SBogdan Liulko 							   destData, &dl);
1456afc0c182SBogdan Liulko 		} else {
1457afc0c182SBogdan Liulko 			dl = 0;
1458afc0c182SBogdan Liulko 			res = TEE_SUCCESS;
1459afc0c182SBogdan Liulko 		}
1460afc0c182SBogdan Liulko 	}
1461afc0c182SBogdan Liulko 	if (res != TEE_SUCCESS)
1462afc0c182SBogdan Liulko 		goto out;
1463afc0c182SBogdan Liulko 
1464e86f1266SJens Wiklander 	*destLen = dl;
1465b0104773SPascal Brand 
1466642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1467642a1607SCedric Chaumont 
1468b5816c88SCedric Chaumont out:
1469b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1470b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1471b5816c88SCedric Chaumont 			TEE_Panic(res);
1472b5816c88SCedric Chaumont 
1473b5816c88SCedric Chaumont 	return res;
1474b0104773SPascal Brand }
1475b0104773SPascal Brand 
1476b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation,
14778f07fe6fSJerome Forissier 			      const void *srcData, uint32_t srcLen,
147879a3c601SCedric Chaumont 			      void *destData, uint32_t *destLen, void *tag,
147979a3c601SCedric Chaumont 			      uint32_t *tagLen)
1480b0104773SPascal Brand {
1481b0104773SPascal Brand 	TEE_Result res;
1482b0104773SPascal Brand 	uint8_t *dst = destData;
1483b0104773SPascal Brand 	size_t acc_dlen = 0;
1484e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1485b0104773SPascal Brand 	size_t req_dlen;
1486e86f1266SJens Wiklander 	uint64_t tl;
1487b0104773SPascal Brand 
14886915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1489b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1490b5816c88SCedric Chaumont 		goto out;
1491b5816c88SCedric Chaumont 	}
14926915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
14936915bbbbSJens Wiklander 	__utee_check_inout_annotation(tagLen, sizeof(*tagLen));
1494b5816c88SCedric Chaumont 
1495b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1496b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1497b5816c88SCedric Chaumont 		goto out;
1498b5816c88SCedric Chaumont 	}
1499b5816c88SCedric Chaumont 
1500b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1501b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1502b5816c88SCedric Chaumont 		goto out;
1503b5816c88SCedric Chaumont 	}
1504b0104773SPascal Brand 
1505b0104773SPascal Brand 	/*
1506b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1507b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1508b0104773SPascal Brand 	 * can't restore sync with this API.
15092733280aSEtienne Carriere 	 *
15102733280aSEtienne Carriere 	 * Need to check this before update_payload since sync would be lost if
15112733280aSEtienne Carriere 	 * we return short buffer after that.
1512b0104773SPascal Brand 	 */
15132733280aSEtienne Carriere 	res = TEE_ERROR_GENERIC;
15142733280aSEtienne Carriere 
1515b5816c88SCedric Chaumont 	req_dlen = operation->buffer_offs + srcLen;
1516b0104773SPascal Brand 	if (*destLen < req_dlen) {
1517b0104773SPascal Brand 		*destLen = req_dlen;
1518b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1519b0104773SPascal Brand 	}
1520b0104773SPascal Brand 
15217acaf5adSAlbert Schwarzkopf 	if (*tagLen < operation->info.digestLength) {
15227acaf5adSAlbert Schwarzkopf 		*tagLen = operation->info.digestLength;
1523b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1524b0104773SPascal Brand 	}
1525b0104773SPascal Brand 
15262733280aSEtienne Carriere 	if (res == TEE_ERROR_SHORT_BUFFER)
15272733280aSEtienne Carriere 		goto out;
15282733280aSEtienne Carriere 
1529afc0c182SBogdan Liulko 	tl = *tagLen;
1530b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1531afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
15322c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1533afc0c182SBogdan Liulko 					srcData, srcLen, dst, &tmp_dlen);
1534b5816c88SCedric Chaumont 		if (res != TEE_SUCCESS)
1535b5816c88SCedric Chaumont 			goto out;
1536b5816c88SCedric Chaumont 
1537b0104773SPascal Brand 		dst += tmp_dlen;
1538b0104773SPascal Brand 		acc_dlen += tmp_dlen;
1539b0104773SPascal Brand 
1540b0104773SPascal Brand 		tmp_dlen = *destLen - acc_dlen;
15412c028fdeSJerome Forissier 		res = _utee_authenc_enc_final(operation->state,
1542afc0c182SBogdan Liulko 					      operation->buffer,
1543afc0c182SBogdan Liulko 					      operation->buffer_offs, dst,
1544afc0c182SBogdan Liulko 					      &tmp_dlen, tag, &tl);
1545afc0c182SBogdan Liulko 	} else {
15462c028fdeSJerome Forissier 		res = _utee_authenc_enc_final(operation->state, srcData,
1547afc0c182SBogdan Liulko 					      srcLen, dst, &tmp_dlen,
1548e86f1266SJens Wiklander 					      tag, &tl);
1549afc0c182SBogdan Liulko 	}
1550e86f1266SJens Wiklander 	*tagLen = tl;
1551b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1552b5816c88SCedric Chaumont 		goto out;
1553b0104773SPascal Brand 
1554b5816c88SCedric Chaumont 	acc_dlen += tmp_dlen;
1555b0104773SPascal Brand 	*destLen = acc_dlen;
1556642a1607SCedric Chaumont 
1557b5816c88SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1558b5816c88SCedric Chaumont 
1559642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1560642a1607SCedric Chaumont 
1561b5816c88SCedric Chaumont out:
1562b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1563b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1564b5816c88SCedric Chaumont 			TEE_Panic(res);
1565b0104773SPascal Brand 
1566b0104773SPascal Brand 	return res;
1567b0104773SPascal Brand }
1568b0104773SPascal Brand 
1569b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation,
15708f07fe6fSJerome Forissier 			      const void *srcData, uint32_t srcLen,
1571b5816c88SCedric Chaumont 			      void *destData, uint32_t *destLen, void *tag,
157279a3c601SCedric Chaumont 			      uint32_t tagLen)
1573b0104773SPascal Brand {
1574b0104773SPascal Brand 	TEE_Result res;
1575b0104773SPascal Brand 	uint8_t *dst = destData;
1576b0104773SPascal Brand 	size_t acc_dlen = 0;
1577e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1578b0104773SPascal Brand 	size_t req_dlen;
1579b0104773SPascal Brand 
15806915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1581b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1582b5816c88SCedric Chaumont 		goto out;
1583b5816c88SCedric Chaumont 	}
15846915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1585b5816c88SCedric Chaumont 
1586b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1587b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1588b5816c88SCedric Chaumont 		goto out;
1589b5816c88SCedric Chaumont 	}
1590b5816c88SCedric Chaumont 
1591b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1592b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1593b5816c88SCedric Chaumont 		goto out;
1594b5816c88SCedric Chaumont 	}
1595b0104773SPascal Brand 
1596b0104773SPascal Brand 	/*
1597b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1598b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1599b0104773SPascal Brand 	 * can't restore sync with this API.
1600b0104773SPascal Brand 	 */
1601b5816c88SCedric Chaumont 	req_dlen = operation->buffer_offs + srcLen;
1602b0104773SPascal Brand 	if (*destLen < req_dlen) {
1603b0104773SPascal Brand 		*destLen = req_dlen;
1604b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1605b5816c88SCedric Chaumont 		goto out;
1606b0104773SPascal Brand 	}
1607b0104773SPascal Brand 
1608b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1609afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
16102c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1611afc0c182SBogdan Liulko 					srcData, srcLen, dst, &tmp_dlen);
1612b5816c88SCedric Chaumont 		if (res != TEE_SUCCESS)
1613b5816c88SCedric Chaumont 			goto out;
1614b5816c88SCedric Chaumont 
1615b0104773SPascal Brand 		dst += tmp_dlen;
1616b0104773SPascal Brand 		acc_dlen += tmp_dlen;
1617b0104773SPascal Brand 
1618b0104773SPascal Brand 		tmp_dlen = *destLen - acc_dlen;
16192c028fdeSJerome Forissier 		res = _utee_authenc_dec_final(operation->state,
1620afc0c182SBogdan Liulko 					      operation->buffer,
1621afc0c182SBogdan Liulko 					      operation->buffer_offs, dst,
1622afc0c182SBogdan Liulko 					      &tmp_dlen, tag, tagLen);
1623afc0c182SBogdan Liulko 	} else {
16242c028fdeSJerome Forissier 		res = _utee_authenc_dec_final(operation->state, srcData,
1625afc0c182SBogdan Liulko 					      srcLen, dst, &tmp_dlen,
1626b5816c88SCedric Chaumont 					      tag, tagLen);
1627afc0c182SBogdan Liulko 	}
1628b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1629b5816c88SCedric Chaumont 		goto out;
1630b5816c88SCedric Chaumont 
1631b0104773SPascal Brand 	/* Supplied tagLen should match what we initiated with */
16327acaf5adSAlbert Schwarzkopf 	if (tagLen != operation->info.digestLength)
1633b0104773SPascal Brand 		res = TEE_ERROR_MAC_INVALID;
1634b0104773SPascal Brand 
1635b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1636b0104773SPascal Brand 	*destLen = acc_dlen;
1637642a1607SCedric Chaumont 
1638b5816c88SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1639b5816c88SCedric Chaumont 
1640642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1641642a1607SCedric Chaumont 
1642b5816c88SCedric Chaumont out:
1643b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1644b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER &&
1645b5816c88SCedric Chaumont 	    res != TEE_ERROR_MAC_INVALID)
1646b5816c88SCedric Chaumont 			TEE_Panic(res);
1647b0104773SPascal Brand 
1648b0104773SPascal Brand 	return res;
1649b0104773SPascal Brand }
1650b0104773SPascal Brand 
1651b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */
1652b0104773SPascal Brand 
165312e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation,
16548f07fe6fSJerome Forissier 				 const TEE_Attribute *params,
16558f07fe6fSJerome Forissier 				 uint32_t paramCount, const void *srcData,
165679a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
165779a3c601SCedric Chaumont 				 uint32_t *destLen)
1658b0104773SPascal Brand {
16596915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1660e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
16616915bbbbSJens Wiklander 	uint64_t dl = 0;
1662b0104773SPascal Brand 
16636915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
1664b0104773SPascal Brand 		TEE_Panic(0);
16656915bbbbSJens Wiklander 
16666915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
16676915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
16686915bbbbSJens Wiklander 
166912e66b6fSCedric Chaumont 	if (!operation->key1)
1670b0104773SPascal Brand 		TEE_Panic(0);
167112e66b6fSCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
167212e66b6fSCedric Chaumont 		TEE_Panic(0);
167312e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_ENCRYPT)
1674b0104773SPascal Brand 		TEE_Panic(0);
1675b0104773SPascal Brand 
1676e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1677e86f1266SJens Wiklander 	dl = *destLen;
16782c028fdeSJerome Forissier 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1679e86f1266SJens Wiklander 				  srcLen, destData, &dl);
1680e86f1266SJens Wiklander 	*destLen = dl;
168112e66b6fSCedric Chaumont 
16828844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
16838844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
16848844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1685b0104773SPascal Brand 		TEE_Panic(res);
168612e66b6fSCedric Chaumont 
1687b0104773SPascal Brand 	return res;
1688b0104773SPascal Brand }
1689b0104773SPascal Brand 
169012e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation,
16918f07fe6fSJerome Forissier 				 const TEE_Attribute *params,
16928f07fe6fSJerome Forissier 				 uint32_t paramCount, const void *srcData,
169379a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
169479a3c601SCedric Chaumont 				 uint32_t *destLen)
1695b0104773SPascal Brand {
16966915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1697e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
16986915bbbbSJens Wiklander 	uint64_t dl = 0;
1699b0104773SPascal Brand 
17006915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
1701b0104773SPascal Brand 		TEE_Panic(0);
17026915bbbbSJens Wiklander 
17036915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
17046915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
17056915bbbbSJens Wiklander 
170612e66b6fSCedric Chaumont 	if (!operation->key1)
1707b0104773SPascal Brand 		TEE_Panic(0);
170812e66b6fSCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
170912e66b6fSCedric Chaumont 		TEE_Panic(0);
171012e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_DECRYPT)
1711b0104773SPascal Brand 		TEE_Panic(0);
1712b0104773SPascal Brand 
1713e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1714e86f1266SJens Wiklander 	dl = *destLen;
17152c028fdeSJerome Forissier 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1716e86f1266SJens Wiklander 				  srcLen, destData, &dl);
1717e86f1266SJens Wiklander 	*destLen = dl;
171812e66b6fSCedric Chaumont 
17198844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
17208844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
17218844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1722b0104773SPascal Brand 		TEE_Panic(res);
172312e66b6fSCedric Chaumont 
1724b0104773SPascal Brand 	return res;
1725b0104773SPascal Brand }
1726b0104773SPascal Brand 
172712e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation,
17288f07fe6fSJerome Forissier 				    const TEE_Attribute *params,
17298f07fe6fSJerome Forissier 				    uint32_t paramCount, const void *digest,
173079a3c601SCedric Chaumont 				    uint32_t digestLen, void *signature,
173179a3c601SCedric Chaumont 				    uint32_t *signatureLen)
1732b0104773SPascal Brand {
17336915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1734e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
17356915bbbbSJens Wiklander 	uint64_t sl = 0;
1736b0104773SPascal Brand 
17376915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!digest && digestLen))
1738b0104773SPascal Brand 		TEE_Panic(0);
17396915bbbbSJens Wiklander 
17406915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
17416915bbbbSJens Wiklander 	__utee_check_inout_annotation(signatureLen, sizeof(*signatureLen));
17426915bbbbSJens Wiklander 
174312e66b6fSCedric Chaumont 	if (!operation->key1)
1744b0104773SPascal Brand 		TEE_Panic(0);
174512e66b6fSCedric Chaumont 	if (operation->info.operationClass !=
174612e66b6fSCedric Chaumont 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
174712e66b6fSCedric Chaumont 		TEE_Panic(0);
174812e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_SIGN)
1749b0104773SPascal Brand 		TEE_Panic(0);
1750b0104773SPascal Brand 
1751e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1752e86f1266SJens Wiklander 	sl = *signatureLen;
17532c028fdeSJerome Forissier 	res = _utee_asymm_operate(operation->state, ua, paramCount, digest,
1754e86f1266SJens Wiklander 				  digestLen, signature, &sl);
1755e86f1266SJens Wiklander 	*signatureLen = sl;
175612e66b6fSCedric Chaumont 
1757b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1758b0104773SPascal Brand 		TEE_Panic(res);
175912e66b6fSCedric Chaumont 
1760b0104773SPascal Brand 	return res;
1761b0104773SPascal Brand }
1762b0104773SPascal Brand 
176312e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,
17648f07fe6fSJerome Forissier 				      const TEE_Attribute *params,
17658f07fe6fSJerome Forissier 				      uint32_t paramCount, const void *digest,
17668f07fe6fSJerome Forissier 				      uint32_t digestLen,
17678f07fe6fSJerome Forissier 				      const void *signature,
176879a3c601SCedric Chaumont 				      uint32_t signatureLen)
1769b0104773SPascal Brand {
1770b0104773SPascal Brand 	TEE_Result res;
1771e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1772b0104773SPascal Brand 
177312e66b6fSCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
177412e66b6fSCedric Chaumont 	    (digest == NULL && digestLen != 0) ||
1775b0104773SPascal Brand 	    (signature == NULL && signatureLen != 0))
1776b0104773SPascal Brand 		TEE_Panic(0);
17776915bbbbSJens Wiklander 
17786915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
17796915bbbbSJens Wiklander 
178012e66b6fSCedric Chaumont 	if (!operation->key1)
1781b0104773SPascal Brand 		TEE_Panic(0);
178212e66b6fSCedric Chaumont 	if (operation->info.operationClass !=
178312e66b6fSCedric Chaumont 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
178412e66b6fSCedric Chaumont 		TEE_Panic(0);
178512e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_VERIFY)
1786b0104773SPascal Brand 		TEE_Panic(0);
1787b0104773SPascal Brand 
1788e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
17892c028fdeSJerome Forissier 	res = _utee_asymm_verify(operation->state, ua, paramCount, digest,
179012e66b6fSCedric Chaumont 				 digestLen, signature, signatureLen);
179112e66b6fSCedric Chaumont 
1792b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1793b0104773SPascal Brand 		TEE_Panic(res);
179412e66b6fSCedric Chaumont 
1795b0104773SPascal Brand 	return res;
1796b0104773SPascal Brand }
1797b0104773SPascal Brand 
1798b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */
1799b0104773SPascal Brand 
1800b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation,
1801b0104773SPascal Brand 		   const TEE_Attribute *params, uint32_t paramCount,
1802b0104773SPascal Brand 		   TEE_ObjectHandle derivedKey)
1803b0104773SPascal Brand {
1804e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
180575d6a373SJens Wiklander 	struct utee_object_info key_info = { };
180675d6a373SJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1807b0104773SPascal Brand 
1808b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1809b0104773SPascal Brand 		TEE_Panic(0);
18106915bbbbSJens Wiklander 
18116915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
18126915bbbbSJens Wiklander 
18138854d3c6SJerome Forissier 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
18148854d3c6SJerome Forissier 	    TEE_OPERATION_KEY_DERIVATION)
1815b0104773SPascal Brand 		TEE_Panic(0);
1816b0104773SPascal Brand 
1817b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1818b0104773SPascal Brand 		TEE_Panic(0);
181984fa9467SCedric Chaumont 	if (!operation->key1)
182084fa9467SCedric Chaumont 		TEE_Panic(0);
1821b0104773SPascal Brand 	if (operation->info.mode != TEE_MODE_DERIVE)
1822b0104773SPascal Brand 		TEE_Panic(0);
1823b0104773SPascal Brand 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1824b0104773SPascal Brand 		TEE_Panic(0);
1825b0104773SPascal Brand 
18262c028fdeSJerome Forissier 	res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info);
1827b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1828b36311adSJerome Forissier 		TEE_Panic(res);
1829b0104773SPascal Brand 
183075d6a373SJens Wiklander 	if (key_info.obj_type != TEE_TYPE_GENERIC_SECRET)
1831b0104773SPascal Brand 		TEE_Panic(0);
183275d6a373SJens Wiklander 	if ((key_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1833b0104773SPascal Brand 		TEE_Panic(0);
1834b0104773SPascal Brand 
1835e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
18362c028fdeSJerome Forissier 	res = _utee_cryp_derive_key(operation->state, ua, paramCount,
1837e86f1266SJens Wiklander 				    (unsigned long)derivedKey);
1838b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1839b0104773SPascal Brand 		TEE_Panic(res);
1840b0104773SPascal Brand }
1841b0104773SPascal Brand 
1842b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */
1843b0104773SPascal Brand 
184479a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1845b0104773SPascal Brand {
1846b0104773SPascal Brand 	TEE_Result res;
1847b0104773SPascal Brand 
18482c028fdeSJerome Forissier 	res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1849b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1850b0104773SPascal Brand 		TEE_Panic(res);
1851b0104773SPascal Brand }
1852433c4257SJens Wiklander 
1853433c4257SJens Wiklander int rand(void)
1854433c4257SJens Wiklander {
1855433c4257SJens Wiklander 	int rc;
1856433c4257SJens Wiklander 
1857433c4257SJens Wiklander 	TEE_GenerateRandom(&rc, sizeof(rc));
1858433c4257SJens Wiklander 
1859433c4257SJens Wiklander 	/*
1860433c4257SJens Wiklander 	 * RAND_MAX is the larges int, INT_MAX which is all bits but the
1861433c4257SJens Wiklander 	 * highest bit set.
1862433c4257SJens Wiklander 	 */
1863433c4257SJens Wiklander 	return rc & RAND_MAX;
1864433c4257SJens Wiklander }
186579170ce0SJerome Forissier 
186679170ce0SJerome Forissier TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element)
186779170ce0SJerome Forissier {
186879170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_AES)) {
186979170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
187079170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_ECB_NOPAD)
187179170ce0SJerome Forissier 				goto check_element_none;
187279170ce0SJerome Forissier 		}
187379170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
187479170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CBC_NOPAD)
187579170ce0SJerome Forissier 				goto check_element_none;
187679170ce0SJerome Forissier 		}
187779170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
187879170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CTR)
187979170ce0SJerome Forissier 				goto check_element_none;
188079170ce0SJerome Forissier 		}
188179170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CTS)) {
188279170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CTS)
188379170ce0SJerome Forissier 				goto check_element_none;
188479170ce0SJerome Forissier 		}
188579170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_XTS)) {
188679170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_XTS)
188779170ce0SJerome Forissier 				goto check_element_none;
188879170ce0SJerome Forissier 		}
188979170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
189079170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CBC_MAC_NOPAD ||
189179170ce0SJerome Forissier 			    alg == TEE_ALG_AES_CBC_MAC_PKCS5)
189279170ce0SJerome Forissier 				goto check_element_none;
189379170ce0SJerome Forissier 		}
189479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CMAC)) {
189579170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CMAC)
189679170ce0SJerome Forissier 				goto check_element_none;
189779170ce0SJerome Forissier 		}
189879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CCM)) {
189979170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CCM)
190079170ce0SJerome Forissier 				goto check_element_none;
190179170ce0SJerome Forissier 		}
190279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_GCM)) {
190379170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_GCM)
190479170ce0SJerome Forissier 				goto check_element_none;
190579170ce0SJerome Forissier 		}
190679170ce0SJerome Forissier 	}
190779170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_DES)) {
190879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
190979170ce0SJerome Forissier 			if (alg == TEE_ALG_DES_ECB_NOPAD ||
191079170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_ECB_NOPAD)
191179170ce0SJerome Forissier 				goto check_element_none;
191279170ce0SJerome Forissier 		}
191379170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
191479170ce0SJerome Forissier 			if (alg == TEE_ALG_DES_CBC_NOPAD ||
191579170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_CBC_NOPAD)
191679170ce0SJerome Forissier 				goto check_element_none;
191779170ce0SJerome Forissier 		}
191879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
191979170ce0SJerome Forissier 			if (alg == TEE_ALG_DES_CBC_MAC_NOPAD ||
192079170ce0SJerome Forissier 			    alg == TEE_ALG_DES_CBC_MAC_PKCS5 ||
192179170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_CBC_MAC_NOPAD ||
192279170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_CBC_MAC_PKCS5)
192379170ce0SJerome Forissier 				goto check_element_none;
192479170ce0SJerome Forissier 		}
192579170ce0SJerome Forissier 	}
192679170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_MD5)) {
192779170ce0SJerome Forissier 		if (alg == TEE_ALG_MD5)
192879170ce0SJerome Forissier 			goto check_element_none;
192979170ce0SJerome Forissier 	}
193079170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
193179170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA1)
193279170ce0SJerome Forissier 			goto check_element_none;
193379170ce0SJerome Forissier 	}
193479170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
193579170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA224)
193679170ce0SJerome Forissier 			goto check_element_none;
193779170ce0SJerome Forissier 	}
193879170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
193979170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA256)
194079170ce0SJerome Forissier 			goto check_element_none;
194179170ce0SJerome Forissier 	}
194279170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
194379170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA384)
194479170ce0SJerome Forissier 			goto check_element_none;
194579170ce0SJerome Forissier 	}
194679170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
194779170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA512)
194879170ce0SJerome Forissier 			goto check_element_none;
194979170ce0SJerome Forissier 	}
195079170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
195179170ce0SJerome Forissier 		if (alg == TEE_ALG_MD5SHA1)
195279170ce0SJerome Forissier 			goto check_element_none;
195379170ce0SJerome Forissier 	}
195479170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_HMAC)) {
195579170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
195679170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_MD5)
195779170ce0SJerome Forissier 				goto check_element_none;
195879170ce0SJerome Forissier 		}
195979170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
196079170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA1)
196179170ce0SJerome Forissier 				goto check_element_none;
196279170ce0SJerome Forissier 		}
196379170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
196479170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA224)
196579170ce0SJerome Forissier 				goto check_element_none;
196679170ce0SJerome Forissier 		}
196779170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
196879170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA256)
196979170ce0SJerome Forissier 				goto check_element_none;
197079170ce0SJerome Forissier 		}
197179170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
197279170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA384)
197379170ce0SJerome Forissier 				goto check_element_none;
197479170ce0SJerome Forissier 		}
197579170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
197679170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA512)
197779170ce0SJerome Forissier 				goto check_element_none;
197879170ce0SJerome Forissier 		}
197979170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SM3)) {
198079170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SM3)
198179170ce0SJerome Forissier 				goto check_element_none;
198279170ce0SJerome Forissier 		}
198379170ce0SJerome Forissier 	}
198479170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM3)) {
198579170ce0SJerome Forissier 		if (alg == TEE_ALG_SM3)
198679170ce0SJerome Forissier 			goto check_element_none;
198779170ce0SJerome Forissier 	}
198879170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM4)) {
198979170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
199079170ce0SJerome Forissier 			if (alg == TEE_ALG_SM4_ECB_NOPAD)
199179170ce0SJerome Forissier 				goto check_element_none;
199279170ce0SJerome Forissier 		}
199379170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
199479170ce0SJerome Forissier 			if (alg == TEE_ALG_SM4_CBC_NOPAD)
199579170ce0SJerome Forissier 				goto check_element_none;
199679170ce0SJerome Forissier 		}
199779170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
199879170ce0SJerome Forissier 			if (alg == TEE_ALG_SM4_CTR)
199979170ce0SJerome Forissier 				goto check_element_none;
200079170ce0SJerome Forissier 		}
2001696f56acSPingan Xie 		if (IS_ENABLED(CFG_CRYPTO_XTS)) {
2002696f56acSPingan Xie 			if (alg == TEE_ALG_SM4_XTS)
2003696f56acSPingan Xie 				goto check_element_none;
2004696f56acSPingan Xie 		}
200579170ce0SJerome Forissier 	}
200679170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_RSA)) {
200779170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
200879170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5)
200979170ce0SJerome Forissier 				goto check_element_none;
201079170ce0SJerome Forissier 		}
201179170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
201279170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 ||
201379170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 ||
201479170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1)
201579170ce0SJerome Forissier 				goto check_element_none;
201679170ce0SJerome Forissier 		}
201779170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
201879170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1)
201979170ce0SJerome Forissier 				goto check_element_none;
202079170ce0SJerome Forissier 		}
202179170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
202279170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 ||
202379170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 ||
202479170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224)
202579170ce0SJerome Forissier 				goto check_element_none;
202679170ce0SJerome Forissier 		}
202779170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
202879170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 ||
202979170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 ||
203079170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256)
203179170ce0SJerome Forissier 				goto check_element_none;
203279170ce0SJerome Forissier 		}
203379170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
203479170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 ||
203579170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 ||
203679170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384)
203779170ce0SJerome Forissier 				goto check_element_none;
203879170ce0SJerome Forissier 		}
203979170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
204079170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 ||
204179170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 ||
204279170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512)
204379170ce0SJerome Forissier 				goto check_element_none;
204479170ce0SJerome Forissier 		}
204579170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) {
204679170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5)
204779170ce0SJerome Forissier 				goto check_element_none;
204879170ce0SJerome Forissier 		}
204979170ce0SJerome Forissier 		if (alg == TEE_ALG_RSA_NOPAD)
205079170ce0SJerome Forissier 			goto check_element_none;
205179170ce0SJerome Forissier 	}
205279170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_DSA)) {
205379170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
205479170ce0SJerome Forissier 			if (alg == TEE_ALG_DSA_SHA1)
205579170ce0SJerome Forissier 				goto check_element_none;
205679170ce0SJerome Forissier 		}
205779170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
205879170ce0SJerome Forissier 			if (alg == TEE_ALG_DSA_SHA224)
205979170ce0SJerome Forissier 				goto check_element_none;
206079170ce0SJerome Forissier 		}
206179170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
206279170ce0SJerome Forissier 			if (alg == TEE_ALG_DSA_SHA256)
206379170ce0SJerome Forissier 				goto check_element_none;
206479170ce0SJerome Forissier 		}
206579170ce0SJerome Forissier 	}
206679170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_DH)) {
206779170ce0SJerome Forissier 		if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
206879170ce0SJerome Forissier 			goto check_element_none;
206979170ce0SJerome Forissier 	}
207079170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_ECC)) {
2071*fe2fd3ffSJens Wiklander 		if ((alg == __OPTEE_ALG_ECDH_P192 ||
2072*fe2fd3ffSJens Wiklander 		     alg == __OPTEE_ALG_ECDSA_P192 ||
2073*fe2fd3ffSJens Wiklander 		     alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET ||
2074*fe2fd3ffSJens Wiklander 		     alg == TEE_ALG_ECDSA_SHA1) &&
207579170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P192)
207679170ce0SJerome Forissier 			return TEE_SUCCESS;
2077*fe2fd3ffSJens Wiklander 		if ((alg == __OPTEE_ALG_ECDH_P224 ||
2078*fe2fd3ffSJens Wiklander 		     alg == __OPTEE_ALG_ECDSA_P224 ||
2079*fe2fd3ffSJens Wiklander 		     alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET ||
2080*fe2fd3ffSJens Wiklander 		     alg == TEE_ALG_ECDSA_SHA224) &&
208179170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P224)
208279170ce0SJerome Forissier 			return TEE_SUCCESS;
2083*fe2fd3ffSJens Wiklander 		if ((alg == __OPTEE_ALG_ECDH_P256 ||
2084*fe2fd3ffSJens Wiklander 		     alg == __OPTEE_ALG_ECDSA_P256 ||
2085*fe2fd3ffSJens Wiklander 		     alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET ||
2086*fe2fd3ffSJens Wiklander 		     alg == TEE_ALG_ECDSA_SHA256) &&
208779170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P256)
208879170ce0SJerome Forissier 			return TEE_SUCCESS;
2089*fe2fd3ffSJens Wiklander 		if ((alg == __OPTEE_ALG_ECDH_P384 ||
2090*fe2fd3ffSJens Wiklander 		     alg == __OPTEE_ALG_ECDSA_P384 ||
2091*fe2fd3ffSJens Wiklander 		     alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET ||
2092*fe2fd3ffSJens Wiklander 		     alg == TEE_ALG_ECDSA_SHA384) &&
209379170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P384)
209479170ce0SJerome Forissier 			return TEE_SUCCESS;
2095*fe2fd3ffSJens Wiklander 		if ((alg == __OPTEE_ALG_ECDH_P521 ||
2096*fe2fd3ffSJens Wiklander 		     alg == __OPTEE_ALG_ECDSA_P521 ||
2097*fe2fd3ffSJens Wiklander 		     alg == TEE_ALG_ECDH_DERIVE_SHARED_SECRET ||
2098*fe2fd3ffSJens Wiklander 		     alg == TEE_ALG_ECDSA_SHA512) &&
209979170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P521)
210079170ce0SJerome Forissier 			return TEE_SUCCESS;
210179170ce0SJerome Forissier 	}
210279170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) {
210379170ce0SJerome Forissier 		if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2)
210479170ce0SJerome Forissier 			return TEE_SUCCESS;
210579170ce0SJerome Forissier 	}
210679170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) {
210779170ce0SJerome Forissier 		if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2)
210879170ce0SJerome Forissier 			return TEE_SUCCESS;
210979170ce0SJerome Forissier 	}
211079170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) {
211179170ce0SJerome Forissier 		if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2)
211279170ce0SJerome Forissier 			return TEE_SUCCESS;
211379170ce0SJerome Forissier 	}
21143f61056dSSohaib ul Hassan 	if (IS_ENABLED(CFG_CRYPTO_X25519)) {
21153f61056dSSohaib ul Hassan 		if (alg == TEE_ALG_X25519 && element == TEE_ECC_CURVE_25519)
21163f61056dSSohaib ul Hassan 			return TEE_SUCCESS;
21173f61056dSSohaib ul Hassan 	}
2118e1f9cee7SSergiy Kibrik 	if (IS_ENABLED(CFG_CRYPTO_ED25519)) {
2119e1f9cee7SSergiy Kibrik 		if (alg == TEE_ALG_ED25519 && element == TEE_ECC_CURVE_25519)
2120e1f9cee7SSergiy Kibrik 			return TEE_SUCCESS;
2121e1f9cee7SSergiy Kibrik 	}
212279170ce0SJerome Forissier 
212379170ce0SJerome Forissier 	return TEE_ERROR_NOT_SUPPORTED;
212479170ce0SJerome Forissier check_element_none:
212579170ce0SJerome Forissier 	if (element == TEE_CRYPTO_ELEMENT_NONE)
212679170ce0SJerome Forissier 		return TEE_SUCCESS;
212779170ce0SJerome Forissier 	return TEE_ERROR_NOT_SUPPORTED;
212879170ce0SJerome Forissier }
2129