xref: /optee_os/lib/libutee/tee_api_operations.c (revision 8734de308f37378d479424ff156278afc8a5ca2c)
11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause
2b0104773SPascal Brand /*
3b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
4b0104773SPascal Brand  */
579170ce0SJerome Forissier #include <config.h>
6b0104773SPascal Brand #include <stdlib.h>
7b0104773SPascal Brand #include <string.h>
8b796ebf3SJerome Forissier #include <string_ext.h>
9b0104773SPascal Brand #include <tee_api.h>
108854d3c6SJerome Forissier #include <tee_api_defines_extensions.h>
11b0104773SPascal Brand #include <tee_internal_api_extensions.h>
12b0104773SPascal Brand #include <utee_syscalls.h>
13b0104773SPascal Brand #include <utee_defines.h>
14fc26c92aSJens Wiklander #include <util.h>
15e86f1266SJens Wiklander #include "tee_api_private.h"
16b0104773SPascal Brand 
17b0104773SPascal Brand struct __TEE_OperationHandle {
18b0104773SPascal Brand 	TEE_OperationInfo info;
19b0104773SPascal Brand 	TEE_ObjectHandle key1;
20b0104773SPascal Brand 	TEE_ObjectHandle key2;
21642a1607SCedric Chaumont 	uint32_t operationState;/* Operation state : INITIAL or ACTIVE */
22b0104773SPascal Brand 	uint8_t *buffer;	/* buffer to collect complete blocks */
23b0104773SPascal Brand 	bool buffer_two_blocks;	/* True if two blocks need to be buffered */
24b0104773SPascal Brand 	size_t block_size;	/* Block size of cipher */
25b0104773SPascal Brand 	size_t buffer_offs;	/* Offset in buffer */
26b0104773SPascal Brand 	uint32_t state;		/* Handle to state in TEE Core */
27b0104773SPascal Brand };
28b0104773SPascal Brand 
29b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */
30b0104773SPascal Brand 
31b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation,
32b0104773SPascal Brand 				 uint32_t algorithm, uint32_t mode,
33b0104773SPascal Brand 				 uint32_t maxKeySize)
34b0104773SPascal Brand {
35b0104773SPascal Brand 	TEE_Result res;
36b0104773SPascal Brand 	TEE_OperationHandle op = TEE_HANDLE_NULL;
37b0104773SPascal Brand 	uint32_t handle_state = 0;
38b0104773SPascal Brand 	size_t block_size = 1;
39b0104773SPascal Brand 	uint32_t req_key_usage;
40b0104773SPascal Brand 	bool with_private_key = false;
41b0104773SPascal Brand 	bool buffer_two_blocks = false;
42b0104773SPascal Brand 
439b52c538SCedric Chaumont 	if (!operation)
44b0104773SPascal Brand 		TEE_Panic(0);
45b0104773SPascal Brand 
465b385b3fSJerome Forissier 	if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP)
47b0104773SPascal Brand 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
48b0104773SPascal Brand 
49218d9055SCedric Chaumont 	/* Check algorithm max key size */
50218d9055SCedric Chaumont 	switch (algorithm) {
51218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA1:
52218d9055SCedric Chaumont 		if (maxKeySize < 512)
53218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
54218d9055SCedric Chaumont 		if (maxKeySize > 1024)
55218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
56218d9055SCedric Chaumont 		if (maxKeySize % 64 != 0)
57218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
58218d9055SCedric Chaumont 		break;
59218d9055SCedric Chaumont 
60218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA224:
61218d9055SCedric Chaumont 		if (maxKeySize != 2048)
62218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
63218d9055SCedric Chaumont 		break;
64218d9055SCedric Chaumont 
65218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA256:
66218d9055SCedric Chaumont 		if (maxKeySize != 2048 && maxKeySize != 3072)
67218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
68218d9055SCedric Chaumont 		break;
69218d9055SCedric Chaumont 
701220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P192:
711220586eSCedric Chaumont 	case TEE_ALG_ECDH_P192:
721220586eSCedric Chaumont 		if (maxKeySize != 192)
731220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
741220586eSCedric Chaumont 		break;
751220586eSCedric Chaumont 
761220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P224:
771220586eSCedric Chaumont 	case TEE_ALG_ECDH_P224:
781220586eSCedric Chaumont 		if (maxKeySize != 224)
791220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
801220586eSCedric Chaumont 		break;
811220586eSCedric Chaumont 
821220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P256:
831220586eSCedric Chaumont 	case TEE_ALG_ECDH_P256:
8491fc6bd8SJerome Forissier 	case TEE_ALG_SM2_PKE:
850f151943SJerome Forissier 	case TEE_ALG_SM2_DSA_SM3:
861220586eSCedric Chaumont 		if (maxKeySize != 256)
871220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
881220586eSCedric Chaumont 		break;
891220586eSCedric Chaumont 
905b385b3fSJerome Forissier 	case TEE_ALG_SM2_KEP:
915b385b3fSJerome Forissier 		/* Two 256-bit keys */
925b385b3fSJerome Forissier 		if (maxKeySize != 512)
935b385b3fSJerome Forissier 			return TEE_ERROR_NOT_SUPPORTED;
945b385b3fSJerome Forissier 		break;
955b385b3fSJerome Forissier 
961220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P384:
971220586eSCedric Chaumont 	case TEE_ALG_ECDH_P384:
981220586eSCedric Chaumont 		if (maxKeySize != 384)
991220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1001220586eSCedric Chaumont 		break;
1011220586eSCedric Chaumont 
1021220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P521:
1031220586eSCedric Chaumont 	case TEE_ALG_ECDH_P521:
1041220586eSCedric Chaumont 		if (maxKeySize != 521)
1051220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1061220586eSCedric Chaumont 		break;
1071220586eSCedric Chaumont 
108218d9055SCedric Chaumont 	default:
109218d9055SCedric Chaumont 		break;
110218d9055SCedric Chaumont 	}
111218d9055SCedric Chaumont 
112cf5c060cSJens Wiklander 	/* Check algorithm mode (and maxKeySize for digests) */
113b0104773SPascal Brand 	switch (algorithm) {
114b0104773SPascal Brand 	case TEE_ALG_AES_CTS:
115b0104773SPascal Brand 	case TEE_ALG_AES_XTS:
116b0104773SPascal Brand 		buffer_two_blocks = true;
117919a5a68SJerome Forissier 		fallthrough;
1184bd53c54SJerome Forissier 	case TEE_ALG_AES_ECB_NOPAD:
119b0104773SPascal Brand 	case TEE_ALG_AES_CBC_NOPAD:
120b0104773SPascal Brand 	case TEE_ALG_AES_CCM:
121b0104773SPascal Brand 	case TEE_ALG_DES_ECB_NOPAD:
122b0104773SPascal Brand 	case TEE_ALG_DES_CBC_NOPAD:
123b0104773SPascal Brand 	case TEE_ALG_DES3_ECB_NOPAD:
124b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_NOPAD:
125ade6f848SJerome Forissier 	case TEE_ALG_SM4_ECB_NOPAD:
126ade6f848SJerome Forissier 	case TEE_ALG_SM4_CBC_NOPAD:
127ade6f848SJerome Forissier 	case TEE_ALG_SM4_CTR:
128b0104773SPascal Brand 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
129b0104773SPascal Brand 			block_size = TEE_AES_BLOCK_SIZE;
130ade6f848SJerome Forissier 		else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4)
131ade6f848SJerome Forissier 			block_size = TEE_SM4_BLOCK_SIZE;
132b0104773SPascal Brand 		else
133b0104773SPascal Brand 			block_size = TEE_DES_BLOCK_SIZE;
134919a5a68SJerome Forissier 		fallthrough;
13557aabac5SBogdan Liulko 	case TEE_ALG_AES_CTR:
136afc0c182SBogdan Liulko 	case TEE_ALG_AES_GCM:
137b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT)
138b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
139b0104773SPascal Brand 		else if (mode == TEE_MODE_DECRYPT)
140b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
141b0104773SPascal Brand 		else
142b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
143b0104773SPascal Brand 		break;
144b0104773SPascal Brand 
1456a2e0a9fSGabor Szekely #if defined(CFG_CRYPTO_RSASSA_NA1)
1466a2e0a9fSGabor Szekely 	case TEE_ALG_RSASSA_PKCS1_V1_5:
1476a2e0a9fSGabor Szekely #endif
148b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
149b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
150b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
151b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
152b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
153b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
154b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
155b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
156b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
157b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
158b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
159b0104773SPascal Brand 	case TEE_ALG_DSA_SHA1:
160218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA224:
161218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA256:
1621220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P192:
1631220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P224:
1641220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P256:
1651220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P384:
1661220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P521:
1670f151943SJerome Forissier 	case TEE_ALG_SM2_DSA_SM3:
168b0104773SPascal Brand 		if (mode == TEE_MODE_SIGN) {
169b0104773SPascal Brand 			with_private_key = true;
170b0104773SPascal Brand 			req_key_usage = TEE_USAGE_SIGN;
171b0104773SPascal Brand 		} else if (mode == TEE_MODE_VERIFY) {
172b0104773SPascal Brand 			req_key_usage = TEE_USAGE_VERIFY;
173b0104773SPascal Brand 		} else {
174b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
175b0104773SPascal Brand 		}
176b0104773SPascal Brand 		break;
177b0104773SPascal Brand 
178b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_V1_5:
179b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
180b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
181b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
182b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
183b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
18491fc6bd8SJerome Forissier 	case TEE_ALG_SM2_PKE:
185b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
186b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
187b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
188b0104773SPascal Brand 			with_private_key = true;
189b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
190b0104773SPascal Brand 		} else {
191b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
192b0104773SPascal Brand 		}
193b0104773SPascal Brand 		break;
194b0104773SPascal Brand 
195b0104773SPascal Brand 	case TEE_ALG_RSA_NOPAD:
196b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
197b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
198b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
199b0104773SPascal Brand 			with_private_key = true;
200b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
201b0104773SPascal Brand 		} else {
202b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
203b0104773SPascal Brand 		}
204b0104773SPascal Brand 		break;
205b0104773SPascal Brand 
206b0104773SPascal Brand 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
2071220586eSCedric Chaumont 	case TEE_ALG_ECDH_P192:
2081220586eSCedric Chaumont 	case TEE_ALG_ECDH_P224:
2091220586eSCedric Chaumont 	case TEE_ALG_ECDH_P256:
2101220586eSCedric Chaumont 	case TEE_ALG_ECDH_P384:
2111220586eSCedric Chaumont 	case TEE_ALG_ECDH_P521:
212cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_MD5_DERIVE_KEY:
213cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA1_DERIVE_KEY:
214cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA224_DERIVE_KEY:
215cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA256_DERIVE_KEY:
216cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA384_DERIVE_KEY:
217cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA512_DERIVE_KEY:
2188854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY:
2198854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY:
2208854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY:
2218854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY:
2228854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY:
2230f2293b7SJerome Forissier 	case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY:
2245b385b3fSJerome Forissier 	case TEE_ALG_SM2_KEP:
225b0104773SPascal Brand 		if (mode != TEE_MODE_DERIVE)
226b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
227b0104773SPascal Brand 		with_private_key = true;
228b0104773SPascal Brand 		req_key_usage = TEE_USAGE_DERIVE;
229b0104773SPascal Brand 		break;
230b0104773SPascal Brand 
231b0104773SPascal Brand 	case TEE_ALG_MD5:
232b0104773SPascal Brand 	case TEE_ALG_SHA1:
233b0104773SPascal Brand 	case TEE_ALG_SHA224:
234b0104773SPascal Brand 	case TEE_ALG_SHA256:
235b0104773SPascal Brand 	case TEE_ALG_SHA384:
236b0104773SPascal Brand 	case TEE_ALG_SHA512:
23747645577SJerome Forissier 	case TEE_ALG_SM3:
238b0104773SPascal Brand 		if (mode != TEE_MODE_DIGEST)
239b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
240cf5c060cSJens Wiklander 		if (maxKeySize)
241cf5c060cSJens Wiklander 			return TEE_ERROR_NOT_SUPPORTED;
24205304565SCedric Chaumont 		/* v1.1: flags always set for digest operations */
243b0104773SPascal Brand 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
244b0104773SPascal Brand 		req_key_usage = 0;
245b0104773SPascal Brand 		break;
246b0104773SPascal Brand 
247b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_NOPAD:
248b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_NOPAD:
249b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_PKCS5:
250b0104773SPascal Brand 	case TEE_ALG_AES_CMAC:
251b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_PKCS5:
252b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
253b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
254b0104773SPascal Brand 	case TEE_ALG_HMAC_MD5:
255b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA1:
256b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA224:
257b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA256:
258b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA384:
259b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA512:
26047645577SJerome Forissier 	case TEE_ALG_HMAC_SM3:
261b0104773SPascal Brand 		if (mode != TEE_MODE_MAC)
262b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
263b0104773SPascal Brand 		req_key_usage = TEE_USAGE_MAC;
264b0104773SPascal Brand 		break;
265b0104773SPascal Brand 
266b0104773SPascal Brand 	default:
267b0104773SPascal Brand 		return TEE_ERROR_NOT_SUPPORTED;
268b0104773SPascal Brand 	}
269b0104773SPascal Brand 
270b66f219bSJens Wiklander 	op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO);
2719b52c538SCedric Chaumont 	if (!op)
272b0104773SPascal Brand 		return TEE_ERROR_OUT_OF_MEMORY;
273b0104773SPascal Brand 
274b0104773SPascal Brand 	op->info.algorithm = algorithm;
275b0104773SPascal Brand 	op->info.operationClass = TEE_ALG_GET_CLASS(algorithm);
2766a2e0a9fSGabor Szekely #ifdef CFG_CRYPTO_RSASSA_NA1
2776a2e0a9fSGabor Szekely 	if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5)
2786a2e0a9fSGabor Szekely 		op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE;
2796a2e0a9fSGabor Szekely #endif
280b0104773SPascal Brand 	op->info.mode = mode;
2812e5e6460SAlbert Schwarzkopf 	op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm);
282b0104773SPascal Brand 	op->info.maxKeySize = maxKeySize;
283b0104773SPascal Brand 	op->info.requiredKeyUsage = req_key_usage;
284b0104773SPascal Brand 	op->info.handleState = handle_state;
285b0104773SPascal Brand 
286b0104773SPascal Brand 	if (block_size > 1) {
287b0104773SPascal Brand 		size_t buffer_size = block_size;
288b0104773SPascal Brand 
289b0104773SPascal Brand 		if (buffer_two_blocks)
290b0104773SPascal Brand 			buffer_size *= 2;
291b0104773SPascal Brand 
2929b52c538SCedric Chaumont 		op->buffer = TEE_Malloc(buffer_size,
2939b52c538SCedric Chaumont 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
294b0104773SPascal Brand 		if (op->buffer == NULL) {
295b0104773SPascal Brand 			res = TEE_ERROR_OUT_OF_MEMORY;
296b66f219bSJens Wiklander 			goto out;
297b0104773SPascal Brand 		}
298b0104773SPascal Brand 	}
299b0104773SPascal Brand 	op->block_size = block_size;
300b0104773SPascal Brand 	op->buffer_two_blocks = buffer_two_blocks;
301b0104773SPascal Brand 
302b0104773SPascal Brand 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
303b0104773SPascal Brand 		uint32_t mks = maxKeySize;
304b0104773SPascal Brand 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
305b0104773SPascal Brand 						       with_private_key);
306b0104773SPascal Brand 
307b0104773SPascal Brand 		/*
308b0104773SPascal Brand 		 * If two keys are expected the max key size is the sum of
309b0104773SPascal Brand 		 * the size of both keys.
310b0104773SPascal Brand 		 */
311b0104773SPascal Brand 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
312b0104773SPascal Brand 			mks /= 2;
313b0104773SPascal Brand 
314b0104773SPascal Brand 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
315b0104773SPascal Brand 		if (res != TEE_SUCCESS)
316b66f219bSJens Wiklander 			goto out;
317b0104773SPascal Brand 
31805304565SCedric Chaumont 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
3199b52c538SCedric Chaumont 			res = TEE_AllocateTransientObject(key_type, mks,
320b0104773SPascal Brand 							  &op->key2);
321b0104773SPascal Brand 			if (res != TEE_SUCCESS)
322b66f219bSJens Wiklander 				goto out;
323b0104773SPascal Brand 		}
324b0104773SPascal Brand 	}
325b0104773SPascal Brand 
3262c028fdeSJerome Forissier 	res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1,
327e86f1266SJens Wiklander 				     (unsigned long)op->key2, &op->state);
328b66f219bSJens Wiklander 	if (res != TEE_SUCCESS)
329b66f219bSJens Wiklander 		goto out;
330b0104773SPascal Brand 
33105304565SCedric Chaumont 	/*
33205304565SCedric Chaumont 	 * Initialize digest operations
33305304565SCedric Chaumont 	 * Other multi-stage operations initialized w/ TEE_xxxInit functions
33405304565SCedric Chaumont 	 * Non-applicable on asymmetric operations
33505304565SCedric Chaumont 	 */
33605304565SCedric Chaumont 	if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) {
3372c028fdeSJerome Forissier 		res = _utee_hash_init(op->state, NULL, 0);
33805304565SCedric Chaumont 		if (res != TEE_SUCCESS)
339b66f219bSJens Wiklander 			goto out;
34005304565SCedric Chaumont 		/* v1.1: flags always set for digest operations */
34105304565SCedric Chaumont 		op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
34205304565SCedric Chaumont 	}
34305304565SCedric Chaumont 
344642a1607SCedric Chaumont 	op->operationState = TEE_OPERATION_STATE_INITIAL;
345642a1607SCedric Chaumont 
346b0104773SPascal Brand 	*operation = op;
347b0104773SPascal Brand 
348b66f219bSJens Wiklander out:
349b66f219bSJens Wiklander 	if (res != TEE_SUCCESS) {
350b66f219bSJens Wiklander 		if (res != TEE_ERROR_OUT_OF_MEMORY &&
3519b52c538SCedric Chaumont 		    res != TEE_ERROR_NOT_SUPPORTED)
352b36311adSJerome Forissier 			TEE_Panic(res);
353b66f219bSJens Wiklander 		if (op) {
354b66f219bSJens Wiklander 			if (op->state) {
355b66f219bSJens Wiklander 				TEE_FreeOperation(op);
356b66f219bSJens Wiklander 			} else {
357b66f219bSJens Wiklander 				TEE_Free(op->buffer);
358b66f219bSJens Wiklander 				TEE_FreeTransientObject(op->key1);
359b66f219bSJens Wiklander 				TEE_FreeTransientObject(op->key2);
360b66f219bSJens Wiklander 				TEE_Free(op);
361b66f219bSJens Wiklander 			}
362b66f219bSJens Wiklander 		}
363b66f219bSJens Wiklander 	}
364b66f219bSJens Wiklander 
365b0104773SPascal Brand 	return res;
366b0104773SPascal Brand }
367b0104773SPascal Brand 
368b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation)
369b0104773SPascal Brand {
370e889e80bSCedric Chaumont 	TEE_Result res;
371e889e80bSCedric Chaumont 
372e889e80bSCedric Chaumont 	if (operation == TEE_HANDLE_NULL)
373e889e80bSCedric Chaumont 		TEE_Panic(0);
374e889e80bSCedric Chaumont 
375b0104773SPascal Brand 	/*
376b0104773SPascal Brand 	 * Note that keys should not be freed here, since they are
377b0104773SPascal Brand 	 * claimed by the operation they will be freed by
378b0104773SPascal Brand 	 * utee_cryp_state_free().
379b0104773SPascal Brand 	 */
3802c028fdeSJerome Forissier 	res = _utee_cryp_state_free(operation->state);
381e889e80bSCedric Chaumont 	if (res != TEE_SUCCESS)
382b36311adSJerome Forissier 		TEE_Panic(res);
383e889e80bSCedric Chaumont 
384b0104773SPascal Brand 	TEE_Free(operation->buffer);
385b0104773SPascal Brand 	TEE_Free(operation);
386b0104773SPascal Brand }
387b0104773SPascal Brand 
388b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation,
389b0104773SPascal Brand 			  TEE_OperationInfo *operationInfo)
390b0104773SPascal Brand {
391b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
392b0104773SPascal Brand 		TEE_Panic(0);
393b0104773SPascal Brand 
3946915bbbbSJens Wiklander 	__utee_check_out_annotation(operationInfo, sizeof(*operationInfo));
395b0104773SPascal Brand 
396b0104773SPascal Brand 	*operationInfo = operation->info;
397bac3a8a7SJens Wiklander 	if (operationInfo->handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
398bac3a8a7SJens Wiklander 		operationInfo->keySize = 0;
399bac3a8a7SJens Wiklander 		operationInfo->requiredKeyUsage = 0;
400bac3a8a7SJens Wiklander 	}
401b0104773SPascal Brand }
402b0104773SPascal Brand 
403ee2f75afSJens Wiklander TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle op,
404ee2f75afSJens Wiklander 					TEE_OperationInfoMultiple *op_info,
405ee2f75afSJens Wiklander 					uint32_t *size)
40605304565SCedric Chaumont {
40705304565SCedric Chaumont 	TEE_Result res = TEE_SUCCESS;
408ee2f75afSJens Wiklander 	TEE_ObjectInfo kinfo = { };
409ee2f75afSJens Wiklander 	size_t max_key_count = 0;
410ee2f75afSJens Wiklander 	bool two_keys = false;
41105304565SCedric Chaumont 
412ee2f75afSJens Wiklander 	if (op == TEE_HANDLE_NULL) {
41305304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
41405304565SCedric Chaumont 		goto out;
41505304565SCedric Chaumont 	}
41605304565SCedric Chaumont 
417ee2f75afSJens Wiklander 	__utee_check_outbuf_annotation(op_info, size);
41805304565SCedric Chaumont 
419ee2f75afSJens Wiklander 	if (*size < sizeof(*op_info)) {
420ee2f75afSJens Wiklander 		res = TEE_ERROR_BAD_PARAMETERS;
421ee2f75afSJens Wiklander 		goto out;
422ee2f75afSJens Wiklander 	}
423ee2f75afSJens Wiklander 	max_key_count = (*size - sizeof(*op_info)) /
42405304565SCedric Chaumont 			sizeof(TEE_OperationInfoKey);
42505304565SCedric Chaumont 
426ee2f75afSJens Wiklander 	TEE_MemFill(op_info, 0, *size);
42705304565SCedric Chaumont 
42805304565SCedric Chaumont 	/* Two keys flag (TEE_ALG_AES_XTS only) */
429ee2f75afSJens Wiklander 	two_keys = op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
430ee2f75afSJens Wiklander 
431ee2f75afSJens Wiklander 	if (op->info.mode == TEE_MODE_DIGEST) {
432ee2f75afSJens Wiklander 		op_info->numberOfKeys = 0;
433ee2f75afSJens Wiklander 	} else if (!two_keys) {
434ee2f75afSJens Wiklander 		if (max_key_count < 1) {
43505304565SCedric Chaumont 			res = TEE_ERROR_SHORT_BUFFER;
43605304565SCedric Chaumont 			goto out;
43705304565SCedric Chaumont 		}
43805304565SCedric Chaumont 
439ee2f75afSJens Wiklander 		res = TEE_GetObjectInfo1(op->key1, &kinfo);
440ee2f75afSJens Wiklander 		/* Key1 is not a valid handle, "can't happen". */
441ee2f75afSJens Wiklander 		if (res)
44205304565SCedric Chaumont 			goto out;
44305304565SCedric Chaumont 
444ee2f75afSJens Wiklander 		op_info->keyInformation[0].keySize = kinfo.keySize;
445ee2f75afSJens Wiklander 		op_info->keyInformation[0].requiredKeyUsage =
446ee2f75afSJens Wiklander 			op->info.requiredKeyUsage;
447ee2f75afSJens Wiklander 		op_info->numberOfKeys = 1;
448ee2f75afSJens Wiklander 	} else {
449ee2f75afSJens Wiklander 		if (max_key_count < 2) {
450ee2f75afSJens Wiklander 			res = TEE_ERROR_SHORT_BUFFER;
45105304565SCedric Chaumont 			goto out;
45205304565SCedric Chaumont 		}
45305304565SCedric Chaumont 
454ee2f75afSJens Wiklander 		res = TEE_GetObjectInfo1(op->key1, &kinfo);
455ee2f75afSJens Wiklander 		/* Key1 is not a valid handle, "can't happen". */
456ee2f75afSJens Wiklander 		if (res)
457ee2f75afSJens Wiklander 			goto out;
458ee2f75afSJens Wiklander 
459ee2f75afSJens Wiklander 		op_info->keyInformation[0].keySize = kinfo.keySize;
460ee2f75afSJens Wiklander 		op_info->keyInformation[0].requiredKeyUsage =
461ee2f75afSJens Wiklander 			op->info.requiredKeyUsage;
462ee2f75afSJens Wiklander 
463ee2f75afSJens Wiklander 		res = TEE_GetObjectInfo1(op->key2, &kinfo);
464ee2f75afSJens Wiklander 		/* Key2 is not a valid handle, "can't happen". */
465ee2f75afSJens Wiklander 		if (res)
466ee2f75afSJens Wiklander 			goto out;
467ee2f75afSJens Wiklander 
468ee2f75afSJens Wiklander 		op_info->keyInformation[1].keySize = kinfo.keySize;
469ee2f75afSJens Wiklander 		op_info->keyInformation[1].requiredKeyUsage =
470ee2f75afSJens Wiklander 			op->info.requiredKeyUsage;
471ee2f75afSJens Wiklander 
472ee2f75afSJens Wiklander 		op_info->numberOfKeys = 2;
47305304565SCedric Chaumont 	}
47405304565SCedric Chaumont 
475ee2f75afSJens Wiklander 	op_info->algorithm = op->info.algorithm;
476ee2f75afSJens Wiklander 	op_info->operationClass = op->info.operationClass;
477ee2f75afSJens Wiklander 	op_info->mode = op->info.mode;
478ee2f75afSJens Wiklander 	op_info->digestLength = op->info.digestLength;
479ee2f75afSJens Wiklander 	op_info->maxKeySize = op->info.maxKeySize;
480ee2f75afSJens Wiklander 	op_info->handleState = op->info.handleState;
481ee2f75afSJens Wiklander 	op_info->operationState = op->operationState;
48205304565SCedric Chaumont 
48305304565SCedric Chaumont out:
48405304565SCedric Chaumont 	if (res != TEE_SUCCESS &&
48505304565SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
486b36311adSJerome Forissier 		TEE_Panic(res);
48705304565SCedric Chaumont 
48805304565SCedric Chaumont 	return res;
48905304565SCedric Chaumont }
49005304565SCedric Chaumont 
491b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation)
492b0104773SPascal Brand {
493b0104773SPascal Brand 	TEE_Result res;
494b0104773SPascal Brand 
495b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
496b0104773SPascal Brand 		TEE_Panic(0);
497bf80076aSCedric Chaumont 
498642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET))
499bf80076aSCedric Chaumont 			TEE_Panic(0);
500bf80076aSCedric Chaumont 
501642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
502642a1607SCedric Chaumont 
503b0104773SPascal Brand 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
5042c028fdeSJerome Forissier 		res = _utee_hash_init(operation->state, NULL, 0);
505b0104773SPascal Brand 		if (res != TEE_SUCCESS)
506b0104773SPascal Brand 			TEE_Panic(res);
50705304565SCedric Chaumont 		operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
50805304565SCedric Chaumont 	} else {
509b0104773SPascal Brand 		operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
510b0104773SPascal Brand 	}
51105304565SCedric Chaumont }
512b0104773SPascal Brand 
513b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
514b0104773SPascal Brand 			       TEE_ObjectHandle key)
515b0104773SPascal Brand {
5167583c59eSCedric Chaumont 	TEE_Result res;
517b0104773SPascal Brand 	uint32_t key_size = 0;
518b0104773SPascal Brand 	TEE_ObjectInfo key_info;
519b0104773SPascal Brand 
520a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
521a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
522a57c1e2eSCedric Chaumont 		goto out;
523a57c1e2eSCedric Chaumont 	}
524a57c1e2eSCedric Chaumont 
525642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
526642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
527642a1607SCedric Chaumont 		goto out;
528642a1607SCedric Chaumont 	}
529642a1607SCedric Chaumont 
530a57c1e2eSCedric Chaumont 	if (key == TEE_HANDLE_NULL) {
531a57c1e2eSCedric Chaumont 		/* Operation key cleared */
532a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key1);
5336c4ea258SJens Wiklander 		operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
5346c4ea258SJens Wiklander 		return TEE_SUCCESS;
535a57c1e2eSCedric Chaumont 	}
536a57c1e2eSCedric Chaumont 
537a57c1e2eSCedric Chaumont 	/* No key for digest operation */
538a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
539a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
540a57c1e2eSCedric Chaumont 		goto out;
541a57c1e2eSCedric Chaumont 	}
542a57c1e2eSCedric Chaumont 
543a57c1e2eSCedric Chaumont 	/* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */
544a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
545a57c1e2eSCedric Chaumont 	    0) {
546a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
547a57c1e2eSCedric Chaumont 		goto out;
548a57c1e2eSCedric Chaumont 	}
549a57c1e2eSCedric Chaumont 
5507583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key, &key_info);
551a57c1e2eSCedric Chaumont 	/* Key is not a valid handle */
5527583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
553a57c1e2eSCedric Chaumont 		goto out;
5547583c59eSCedric Chaumont 
555b0104773SPascal Brand 	/* Supplied key has to meet required usage */
556b0104773SPascal Brand 	if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
557b0104773SPascal Brand 	    operation->info.requiredKeyUsage) {
558a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
559a57c1e2eSCedric Chaumont 		goto out;
560b0104773SPascal Brand 	}
561b0104773SPascal Brand 
562a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info.keySize) {
563a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
564a57c1e2eSCedric Chaumont 		goto out;
565a57c1e2eSCedric Chaumont 	}
566b0104773SPascal Brand 
5677583c59eSCedric Chaumont 	key_size = key_info.keySize;
568b0104773SPascal Brand 
569b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
570b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
571b0104773SPascal Brand 
5727583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key);
5737583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
574a57c1e2eSCedric Chaumont 		goto out;
5757583c59eSCedric Chaumont 
576b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
577b0104773SPascal Brand 
578b0104773SPascal Brand 	operation->info.keySize = key_size;
579b0104773SPascal Brand 
5807583c59eSCedric Chaumont out:
581a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
582a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
583a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
584b36311adSJerome Forissier 		TEE_Panic(res);
585a57c1e2eSCedric Chaumont 
586a57c1e2eSCedric Chaumont 	return res;
587b0104773SPascal Brand }
588b0104773SPascal Brand 
589b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
590b0104773SPascal Brand 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
591b0104773SPascal Brand {
5927583c59eSCedric Chaumont 	TEE_Result res;
593b0104773SPascal Brand 	uint32_t key_size = 0;
594b0104773SPascal Brand 	TEE_ObjectInfo key_info1;
595b0104773SPascal Brand 	TEE_ObjectInfo key_info2;
596b0104773SPascal Brand 
597a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
598a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
599a57c1e2eSCedric Chaumont 		goto out;
600a57c1e2eSCedric Chaumont 	}
601a57c1e2eSCedric Chaumont 
602642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
603642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
604642a1607SCedric Chaumont 		goto out;
605642a1607SCedric Chaumont 	}
606642a1607SCedric Chaumont 
607a57c1e2eSCedric Chaumont 	/*
608a57c1e2eSCedric Chaumont 	 * Key1/Key2 and/or are not initialized and
609a57c1e2eSCedric Chaumont 	 * Either both keys are NULL or both are not NULL
610a57c1e2eSCedric Chaumont 	 */
6116c4ea258SJens Wiklander 	if (!key1 && !key2) {
6126c4ea258SJens Wiklander 		/* Clear the keys */
613a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key1);
614a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key2);
6156c4ea258SJens Wiklander 		operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
6166c4ea258SJens Wiklander 		return TEE_SUCCESS;
6176c4ea258SJens Wiklander 	} else if (!key1 || !key2) {
6186c4ea258SJens Wiklander 		/* Both keys are obviously not valid. */
619a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
620a57c1e2eSCedric Chaumont 		goto out;
621a57c1e2eSCedric Chaumont 	}
622a57c1e2eSCedric Chaumont 
623a57c1e2eSCedric Chaumont 	/* No key for digest operation */
624a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
625a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
626a57c1e2eSCedric Chaumont 		goto out;
627a57c1e2eSCedric Chaumont 	}
628a57c1e2eSCedric Chaumont 
6295b385b3fSJerome Forissier 	/* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */
630a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
631a57c1e2eSCedric Chaumont 	    0) {
632a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
633a57c1e2eSCedric Chaumont 		goto out;
634a57c1e2eSCedric Chaumont 	}
635a57c1e2eSCedric Chaumont 
6367583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key1, &key_info1);
637a57c1e2eSCedric Chaumont 	/* Key1 is not a valid handle */
6387583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
639a57c1e2eSCedric Chaumont 		goto out;
6407583c59eSCedric Chaumont 
641b0104773SPascal Brand 	/* Supplied key has to meet required usage */
642b0104773SPascal Brand 	if ((key_info1.objectUsage & operation->info.
643b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
644a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
645a57c1e2eSCedric Chaumont 		goto out;
646b0104773SPascal Brand 	}
647b0104773SPascal Brand 
6487583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key2, &key_info2);
649a57c1e2eSCedric Chaumont 	/* Key2 is not a valid handle */
6507583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
6517583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
6527583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
653a57c1e2eSCedric Chaumont 		goto out;
6547583c59eSCedric Chaumont 	}
6557583c59eSCedric Chaumont 
656b0104773SPascal Brand 	/* Supplied key has to meet required usage */
657b0104773SPascal Brand 	if ((key_info2.objectUsage & operation->info.
658b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
659a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
660a57c1e2eSCedric Chaumont 		goto out;
661b0104773SPascal Brand 	}
662b0104773SPascal Brand 
663b0104773SPascal Brand 	/*
6645b385b3fSJerome Forissier 	 * All the multi key algorithm currently supported requires the keys to
6655b385b3fSJerome Forissier 	 * be of equal size.
666b0104773SPascal Brand 	 */
6675b385b3fSJerome Forissier 	if (key_info1.keySize != key_info2.keySize) {
668a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
669a57c1e2eSCedric Chaumont 		goto out;
670b0104773SPascal Brand 
671a57c1e2eSCedric Chaumont 	}
672a57c1e2eSCedric Chaumont 
673a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info1.keySize) {
674a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
675a57c1e2eSCedric Chaumont 		goto out;
676a57c1e2eSCedric Chaumont 	}
677b0104773SPascal Brand 
678b0104773SPascal Brand 	/*
679b0104773SPascal Brand 	 * Odd that only the size of one key should be reported while
680b0104773SPascal Brand 	 * size of two key are used when allocating the operation.
681b0104773SPascal Brand 	 */
6827583c59eSCedric Chaumont 	key_size = key_info1.keySize;
683b0104773SPascal Brand 
684b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
685b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key2);
686b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
687b0104773SPascal Brand 
6887583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key1);
6897583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
690a57c1e2eSCedric Chaumont 		goto out;
6917583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key2, key2);
6927583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
6937583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
6947583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
695a57c1e2eSCedric Chaumont 		goto out;
6967583c59eSCedric Chaumont 	}
6977583c59eSCedric Chaumont 
698b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
699b0104773SPascal Brand 
700b0104773SPascal Brand 	operation->info.keySize = key_size;
701b0104773SPascal Brand 
7027583c59eSCedric Chaumont out:
703a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
704a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
705a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT_2 &&
706a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE &&
707a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
708b36311adSJerome Forissier 		TEE_Panic(res);
709a57c1e2eSCedric Chaumont 
710a57c1e2eSCedric Chaumont 	return res;
711b0104773SPascal Brand }
712b0104773SPascal Brand 
713b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
714b0104773SPascal Brand {
715b0104773SPascal Brand 	TEE_Result res;
716b0104773SPascal Brand 
717b0104773SPascal Brand 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
718b0104773SPascal Brand 		TEE_Panic(0);
719b0104773SPascal Brand 	if (dst_op->info.algorithm != src_op->info.algorithm)
720b0104773SPascal Brand 		TEE_Panic(0);
721*8734de30SJens Wiklander 	if (dst_op->info.mode != src_op->info.mode)
722*8734de30SJens Wiklander 		TEE_Panic(0);
723b0104773SPascal Brand 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
724b0104773SPascal Brand 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
725b0104773SPascal Brand 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
726b0104773SPascal Brand 
727b0104773SPascal Brand 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
728b0104773SPascal Brand 			key1 = src_op->key1;
729b0104773SPascal Brand 			key2 = src_op->key2;
730b0104773SPascal Brand 		}
731b0104773SPascal Brand 
732b0104773SPascal Brand 		if ((src_op->info.handleState &
733b0104773SPascal Brand 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
734b0104773SPascal Brand 			TEE_SetOperationKey(dst_op, key1);
735b0104773SPascal Brand 		} else {
736b0104773SPascal Brand 			TEE_SetOperationKey2(dst_op, key1, key2);
737b0104773SPascal Brand 		}
738b0104773SPascal Brand 	}
739b0104773SPascal Brand 	dst_op->info.handleState = src_op->info.handleState;
740b0104773SPascal Brand 	dst_op->info.keySize = src_op->info.keySize;
741642a1607SCedric Chaumont 	dst_op->operationState = src_op->operationState;
742b0104773SPascal Brand 
743b0104773SPascal Brand 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
744b0104773SPascal Brand 	    dst_op->block_size != src_op->block_size)
745b0104773SPascal Brand 		TEE_Panic(0);
746b0104773SPascal Brand 
747b0104773SPascal Brand 	if (dst_op->buffer != NULL) {
748b0104773SPascal Brand 		if (src_op->buffer == NULL)
749b0104773SPascal Brand 			TEE_Panic(0);
750b0104773SPascal Brand 
751b0104773SPascal Brand 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
752b0104773SPascal Brand 		dst_op->buffer_offs = src_op->buffer_offs;
753b0104773SPascal Brand 	} else if (src_op->buffer != NULL) {
754b0104773SPascal Brand 		TEE_Panic(0);
755b0104773SPascal Brand 	}
756b0104773SPascal Brand 
7572c028fdeSJerome Forissier 	res = _utee_cryp_state_copy(dst_op->state, src_op->state);
758b0104773SPascal Brand 	if (res != TEE_SUCCESS)
759b0104773SPascal Brand 		TEE_Panic(res);
760b0104773SPascal Brand }
761b0104773SPascal Brand 
762b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */
763b0104773SPascal Brand 
7648f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV,
7656d15db08SJerome Forissier 				uint32_t IVLen)
7666d15db08SJerome Forissier {
7676d15db08SJerome Forissier 	TEE_Result res;
7686d15db08SJerome Forissier 
7696d15db08SJerome Forissier 	/*
7706d15db08SJerome Forissier 	 * Note : IV and IVLen are never used in current implementation
7716d15db08SJerome Forissier 	 * This is why coherent values of IV and IVLen are not checked
7726d15db08SJerome Forissier 	 */
7732c028fdeSJerome Forissier 	res = _utee_hash_init(operation->state, IV, IVLen);
7746d15db08SJerome Forissier 	if (res != TEE_SUCCESS)
7756d15db08SJerome Forissier 		TEE_Panic(res);
7766d15db08SJerome Forissier 	operation->buffer_offs = 0;
7776d15db08SJerome Forissier 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
7786d15db08SJerome Forissier }
7796d15db08SJerome Forissier 
780b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation,
7818f07fe6fSJerome Forissier 		      const void *chunk, uint32_t chunkSize)
782b0104773SPascal Brand {
78373d6c3baSJoakim Bech 	TEE_Result res = TEE_ERROR_GENERIC;
784b0104773SPascal Brand 
78573d6c3baSJoakim Bech 	if (operation == TEE_HANDLE_NULL ||
78673d6c3baSJoakim Bech 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
787b0104773SPascal Brand 		TEE_Panic(0);
78873d6c3baSJoakim Bech 
789642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
790642a1607SCedric Chaumont 
7912c028fdeSJerome Forissier 	res = _utee_hash_update(operation->state, chunk, chunkSize);
792b0104773SPascal Brand 	if (res != TEE_SUCCESS)
793b0104773SPascal Brand 		TEE_Panic(res);
794b0104773SPascal Brand }
795b0104773SPascal Brand 
7968f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
79779a3c601SCedric Chaumont 			     uint32_t chunkLen, void *hash, uint32_t *hashLen)
798b0104773SPascal Brand {
79987c2f6b6SCedric Chaumont 	TEE_Result res;
800e86f1266SJens Wiklander 	uint64_t hl;
80187c2f6b6SCedric Chaumont 
80287c2f6b6SCedric Chaumont 	if ((operation == TEE_HANDLE_NULL) ||
80387c2f6b6SCedric Chaumont 	    (!chunk && chunkLen) ||
80487c2f6b6SCedric Chaumont 	    (operation->info.operationClass != TEE_OPERATION_DIGEST)) {
80587c2f6b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
80687c2f6b6SCedric Chaumont 		goto out;
80787c2f6b6SCedric Chaumont 	}
8086915bbbbSJens Wiklander 	__utee_check_inout_annotation(hashLen, sizeof(*hashLen));
80987c2f6b6SCedric Chaumont 
810e86f1266SJens Wiklander 	hl = *hashLen;
8112c028fdeSJerome Forissier 	res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl);
812e86f1266SJens Wiklander 	*hashLen = hl;
8136d15db08SJerome Forissier 	if (res != TEE_SUCCESS)
8146d15db08SJerome Forissier 		goto out;
8156d15db08SJerome Forissier 
8166d15db08SJerome Forissier 	/* Reset operation state */
8176d15db08SJerome Forissier 	init_hash_operation(operation, NULL, 0);
81887c2f6b6SCedric Chaumont 
819642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
820642a1607SCedric Chaumont 
82187c2f6b6SCedric Chaumont out:
82287c2f6b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
82387c2f6b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
824b36311adSJerome Forissier 		TEE_Panic(res);
82573d6c3baSJoakim Bech 
82687c2f6b6SCedric Chaumont 	return res;
827b0104773SPascal Brand }
828b0104773SPascal Brand 
829b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */
830b0104773SPascal Brand 
8318f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV,
8328f07fe6fSJerome Forissier 		    uint32_t IVLen)
833b0104773SPascal Brand {
834b0104773SPascal Brand 	TEE_Result res;
835b0104773SPascal Brand 
836b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
837b0104773SPascal Brand 		TEE_Panic(0);
838642a1607SCedric Chaumont 
839b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
840b0104773SPascal Brand 		TEE_Panic(0);
841642a1607SCedric Chaumont 
842642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
843642a1607SCedric Chaumont 	    !(operation->key1))
844642a1607SCedric Chaumont 		TEE_Panic(0);
845642a1607SCedric Chaumont 
846642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
847642a1607SCedric Chaumont 		TEE_ResetOperation(operation);
848642a1607SCedric Chaumont 
849642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
850642a1607SCedric Chaumont 
8512c028fdeSJerome Forissier 	res = _utee_cipher_init(operation->state, IV, IVLen);
852b0104773SPascal Brand 	if (res != TEE_SUCCESS)
853b0104773SPascal Brand 		TEE_Panic(res);
854642a1607SCedric Chaumont 
855b0104773SPascal Brand 	operation->buffer_offs = 0;
856b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
857b0104773SPascal Brand }
858b0104773SPascal Brand 
859b0104773SPascal Brand static TEE_Result tee_buffer_update(
860b0104773SPascal Brand 		TEE_OperationHandle op,
861e86f1266SJens Wiklander 		TEE_Result(*update_func)(unsigned long state, const void *src,
862e86f1266SJens Wiklander 				size_t slen, void *dst, uint64_t *dlen),
863b0104773SPascal Brand 		const void *src_data, size_t src_len,
864e86f1266SJens Wiklander 		void *dest_data, uint64_t *dest_len)
865b0104773SPascal Brand {
866b0104773SPascal Brand 	TEE_Result res;
867b0104773SPascal Brand 	const uint8_t *src = src_data;
868b0104773SPascal Brand 	size_t slen = src_len;
869b0104773SPascal Brand 	uint8_t *dst = dest_data;
870b0104773SPascal Brand 	size_t dlen = *dest_len;
871b0104773SPascal Brand 	size_t acc_dlen = 0;
872e86f1266SJens Wiklander 	uint64_t tmp_dlen;
873b0104773SPascal Brand 	size_t l;
874b0104773SPascal Brand 	size_t buffer_size;
875d3588802SPascal Brand 	size_t buffer_left;
876b0104773SPascal Brand 
877090268f5SJens Wiklander 	if (!src) {
878090268f5SJens Wiklander 		if (slen)
879090268f5SJens Wiklander 			TEE_Panic(0);
880090268f5SJens Wiklander 		goto out;
881090268f5SJens Wiklander 	}
882090268f5SJens Wiklander 
883d3588802SPascal Brand 	if (op->buffer_two_blocks) {
884b0104773SPascal Brand 		buffer_size = op->block_size * 2;
885d3588802SPascal Brand 		buffer_left = 1;
886d3588802SPascal Brand 	} else {
887b0104773SPascal Brand 		buffer_size = op->block_size;
888d3588802SPascal Brand 		buffer_left = 0;
889d3588802SPascal Brand 	}
890b0104773SPascal Brand 
891b0104773SPascal Brand 	if (op->buffer_offs > 0) {
892b0104773SPascal Brand 		/* Fill up complete block */
893b0104773SPascal Brand 		if (op->buffer_offs < op->block_size)
894b0104773SPascal Brand 			l = MIN(slen, op->block_size - op->buffer_offs);
895b0104773SPascal Brand 		else
896b0104773SPascal Brand 			l = MIN(slen, buffer_size - op->buffer_offs);
897b0104773SPascal Brand 		memcpy(op->buffer + op->buffer_offs, src, l);
898b0104773SPascal Brand 		op->buffer_offs += l;
899b0104773SPascal Brand 		src += l;
900b0104773SPascal Brand 		slen -= l;
901b0104773SPascal Brand 		if ((op->buffer_offs % op->block_size) != 0)
902b0104773SPascal Brand 			goto out;	/* Nothing left to do */
903b0104773SPascal Brand 	}
904b0104773SPascal Brand 
905b0104773SPascal Brand 	/* If we can feed from buffer */
906d3588802SPascal Brand 	if ((op->buffer_offs > 0) &&
907d3588802SPascal Brand 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
9082ff3fdbbSPascal Brand 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
909b0104773SPascal Brand 				op->block_size);
910b0104773SPascal Brand 		l = MIN(op->buffer_offs, l);
911b0104773SPascal Brand 		tmp_dlen = dlen;
912b0104773SPascal Brand 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
913b0104773SPascal Brand 		if (res != TEE_SUCCESS)
914b0104773SPascal Brand 			TEE_Panic(res);
915b0104773SPascal Brand 		dst += tmp_dlen;
916b0104773SPascal Brand 		dlen -= tmp_dlen;
917b0104773SPascal Brand 		acc_dlen += tmp_dlen;
918b0104773SPascal Brand 		op->buffer_offs -= l;
919b0104773SPascal Brand 		if (op->buffer_offs > 0) {
920b0104773SPascal Brand 			/*
921b0104773SPascal Brand 			 * Slen is small enough to be contained in rest buffer.
922b0104773SPascal Brand 			 */
923b0104773SPascal Brand 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
924b0104773SPascal Brand 			memcpy(op->buffer + op->buffer_offs, src, slen);
925b0104773SPascal Brand 			op->buffer_offs += slen;
926b0104773SPascal Brand 			goto out;	/* Nothing left to do */
927b0104773SPascal Brand 		}
928b0104773SPascal Brand 	}
929b0104773SPascal Brand 
930d3588802SPascal Brand 	if (slen >= (buffer_size + buffer_left)) {
931b0104773SPascal Brand 		/* Buffer is empty, feed as much as possible from src */
932bf7a587fSJerome Forissier 		if (op->info.algorithm == TEE_ALG_AES_CTS)
933b1ecda78SJerome Forissier 			l = ROUNDUP(slen - buffer_size, op->block_size);
934bf7a587fSJerome Forissier 		else
935bf7a587fSJerome Forissier 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
936b0104773SPascal Brand 
937b0104773SPascal Brand 		tmp_dlen = dlen;
938b0104773SPascal Brand 		res = update_func(op->state, src, l, dst, &tmp_dlen);
939b0104773SPascal Brand 		if (res != TEE_SUCCESS)
940b0104773SPascal Brand 			TEE_Panic(res);
941b0104773SPascal Brand 		src += l;
942b0104773SPascal Brand 		slen -= l;
943b0104773SPascal Brand 		dst += tmp_dlen;
944b0104773SPascal Brand 		dlen -= tmp_dlen;
945b0104773SPascal Brand 		acc_dlen += tmp_dlen;
946b0104773SPascal Brand 	}
947b0104773SPascal Brand 
948b0104773SPascal Brand 	/* Slen is small enough to be contained in buffer. */
949b0104773SPascal Brand 	memcpy(op->buffer + op->buffer_offs, src, slen);
950b0104773SPascal Brand 	op->buffer_offs += slen;
951b0104773SPascal Brand 
952b0104773SPascal Brand out:
953b0104773SPascal Brand 	*dest_len = acc_dlen;
954b0104773SPascal Brand 	return TEE_SUCCESS;
955b0104773SPascal Brand }
956b0104773SPascal Brand 
9578f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData,
95879a3c601SCedric Chaumont 			    uint32_t srcLen, void *destData, uint32_t *destLen)
959b0104773SPascal Brand {
960dea1f2b6SCedric Chaumont 	TEE_Result res;
961b0104773SPascal Brand 	size_t req_dlen;
962e86f1266SJens Wiklander 	uint64_t dl;
963b0104773SPascal Brand 
9646915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
965dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
966dea1f2b6SCedric Chaumont 		goto out;
967dea1f2b6SCedric Chaumont 	}
9686915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
969dea1f2b6SCedric Chaumont 
970642a1607SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
971dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
972dea1f2b6SCedric Chaumont 		goto out;
973dea1f2b6SCedric Chaumont 	}
974dea1f2b6SCedric Chaumont 
975642a1607SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
976642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
977642a1607SCedric Chaumont 		goto out;
978642a1607SCedric Chaumont 	}
979642a1607SCedric Chaumont 
980642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
981dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
982dea1f2b6SCedric Chaumont 		goto out;
983dea1f2b6SCedric Chaumont 	}
984b0104773SPascal Brand 
985e32c5ddfSJerome Forissier 	if (!srcData && !srcLen) {
986090268f5SJens Wiklander 		*destLen = 0;
987e32c5ddfSJerome Forissier 		res = TEE_SUCCESS;
988e32c5ddfSJerome Forissier 		goto out;
989e32c5ddfSJerome Forissier 	}
990e32c5ddfSJerome Forissier 
991b0104773SPascal Brand 	/* Calculate required dlen */
99257aabac5SBogdan Liulko 	if (operation->block_size > 1) {
99357aabac5SBogdan Liulko 		req_dlen = ((operation->buffer_offs + srcLen) /
99457aabac5SBogdan Liulko 			    operation->block_size) * operation->block_size;
99557aabac5SBogdan Liulko 	} else {
99657aabac5SBogdan Liulko 		req_dlen = srcLen;
99757aabac5SBogdan Liulko 	}
998642a1607SCedric Chaumont 	if (operation->buffer_two_blocks) {
999642a1607SCedric Chaumont 		if (req_dlen > operation->block_size * 2)
1000642a1607SCedric Chaumont 			req_dlen -= operation->block_size * 2;
1001b0104773SPascal Brand 		else
1002b0104773SPascal Brand 			req_dlen = 0;
1003b0104773SPascal Brand 	}
1004b0104773SPascal Brand 	/*
1005b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1006b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1007b0104773SPascal Brand 	 * can't restore sync with this API.
1008b0104773SPascal Brand 	 */
1009b0104773SPascal Brand 	if (*destLen < req_dlen) {
1010b0104773SPascal Brand 		*destLen = req_dlen;
1011dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1012dea1f2b6SCedric Chaumont 		goto out;
1013b0104773SPascal Brand 	}
1014b0104773SPascal Brand 
1015e86f1266SJens Wiklander 	dl = *destLen;
101657aabac5SBogdan Liulko 	if (operation->block_size > 1) {
10172c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_cipher_update, srcData,
101857aabac5SBogdan Liulko 					srcLen, destData, &dl);
101957aabac5SBogdan Liulko 	} else {
102057aabac5SBogdan Liulko 		if (srcLen > 0) {
10212c028fdeSJerome Forissier 			res = _utee_cipher_update(operation->state, srcData,
102257aabac5SBogdan Liulko 						  srcLen, destData, &dl);
102357aabac5SBogdan Liulko 		} else {
102457aabac5SBogdan Liulko 			res = TEE_SUCCESS;
102557aabac5SBogdan Liulko 			dl = 0;
102657aabac5SBogdan Liulko 		}
102757aabac5SBogdan Liulko 	}
1028e86f1266SJens Wiklander 	*destLen = dl;
1029b0104773SPascal Brand 
1030dea1f2b6SCedric Chaumont out:
1031dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1032dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1033b36311adSJerome Forissier 		TEE_Panic(res);
1034dea1f2b6SCedric Chaumont 
1035dea1f2b6SCedric Chaumont 	return res;
1036b0104773SPascal Brand }
1037b0104773SPascal Brand 
1038642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation,
10398f07fe6fSJerome Forissier 			     const void *srcData, uint32_t srcLen,
10408f07fe6fSJerome Forissier 			     void *destData, uint32_t *destLen)
1041b0104773SPascal Brand {
10426915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1043b0104773SPascal Brand 	uint8_t *dst = destData;
1044b0104773SPascal Brand 	size_t acc_dlen = 0;
10456915bbbbSJens Wiklander 	uint64_t tmp_dlen = 0;
10466915bbbbSJens Wiklander 	size_t req_dlen = 0;
1047b0104773SPascal Brand 
10486915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1049dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1050dea1f2b6SCedric Chaumont 		goto out;
1051dea1f2b6SCedric Chaumont 	}
10526915bbbbSJens Wiklander 	if (destLen)
10536915bbbbSJens Wiklander 		__utee_check_inout_annotation(destLen, sizeof(*destLen));
1054dea1f2b6SCedric Chaumont 
1055642a1607SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1056dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1057dea1f2b6SCedric Chaumont 		goto out;
1058dea1f2b6SCedric Chaumont 	}
1059dea1f2b6SCedric Chaumont 
1060642a1607SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1061642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1062642a1607SCedric Chaumont 		goto out;
1063642a1607SCedric Chaumont 	}
1064642a1607SCedric Chaumont 
1065642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1066dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1067dea1f2b6SCedric Chaumont 		goto out;
1068dea1f2b6SCedric Chaumont 	}
1069b0104773SPascal Brand 
1070b0104773SPascal Brand 	/*
1071b0104773SPascal Brand 	 * Check that the final block doesn't require padding for those
1072b0104773SPascal Brand 	 * algorithms that requires client to supply padding.
1073b0104773SPascal Brand 	 */
1074642a1607SCedric Chaumont 	if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
1075642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1076642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
1077642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1078642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
1079ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD ||
1080ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD ||
1081ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) {
1082642a1607SCedric Chaumont 		if (((operation->buffer_offs + srcLen) % operation->block_size)
1083642a1607SCedric Chaumont 		    != 0) {
1084dea1f2b6SCedric Chaumont 			res = TEE_ERROR_BAD_PARAMETERS;
1085dea1f2b6SCedric Chaumont 			goto out;
1086dea1f2b6SCedric Chaumont 		}
1087b0104773SPascal Brand 	}
1088b0104773SPascal Brand 
1089b0104773SPascal Brand 	/*
1090b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1091b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1092b0104773SPascal Brand 	 * can't restore sync with this API.
1093b0104773SPascal Brand 	 */
109457aabac5SBogdan Liulko 	if (operation->block_size > 1) {
1095642a1607SCedric Chaumont 		req_dlen = operation->buffer_offs + srcLen;
109657aabac5SBogdan Liulko 	} else {
109757aabac5SBogdan Liulko 		req_dlen = srcLen;
109857aabac5SBogdan Liulko 	}
10996915bbbbSJens Wiklander 	if (destLen)
11006915bbbbSJens Wiklander 		tmp_dlen = *destLen;
11016915bbbbSJens Wiklander 	if (tmp_dlen < req_dlen) {
11026915bbbbSJens Wiklander 		if (destLen)
1103b0104773SPascal Brand 			*destLen = req_dlen;
1104dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1105dea1f2b6SCedric Chaumont 		goto out;
1106b0104773SPascal Brand 	}
1107b0104773SPascal Brand 
110857aabac5SBogdan Liulko 	if (operation->block_size > 1) {
11092c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_cipher_update,
111057aabac5SBogdan Liulko 					srcData, srcLen, dst, &tmp_dlen);
1111dea1f2b6SCedric Chaumont 		if (res != TEE_SUCCESS)
1112dea1f2b6SCedric Chaumont 			goto out;
1113dea1f2b6SCedric Chaumont 
1114b0104773SPascal Brand 		dst += tmp_dlen;
1115b0104773SPascal Brand 		acc_dlen += tmp_dlen;
1116b0104773SPascal Brand 
1117b0104773SPascal Brand 		tmp_dlen = *destLen - acc_dlen;
11182c028fdeSJerome Forissier 		res = _utee_cipher_final(operation->state, operation->buffer,
11192c028fdeSJerome Forissier 					 operation->buffer_offs, dst,
11202c028fdeSJerome Forissier 					 &tmp_dlen);
112157aabac5SBogdan Liulko 	} else {
11222c028fdeSJerome Forissier 		res = _utee_cipher_final(operation->state, srcData, srcLen, dst,
11232c028fdeSJerome Forissier 					 &tmp_dlen);
112457aabac5SBogdan Liulko 	}
1125b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1126dea1f2b6SCedric Chaumont 		goto out;
1127dea1f2b6SCedric Chaumont 
1128b0104773SPascal Brand 	acc_dlen += tmp_dlen;
11296915bbbbSJens Wiklander 	if (destLen)
1130b0104773SPascal Brand 		*destLen = acc_dlen;
1131dea1f2b6SCedric Chaumont 
1132642a1607SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1133642a1607SCedric Chaumont 
1134642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1135642a1607SCedric Chaumont 
1136dea1f2b6SCedric Chaumont out:
1137dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1138dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1139b36311adSJerome Forissier 		TEE_Panic(res);
1140dea1f2b6SCedric Chaumont 
1141dea1f2b6SCedric Chaumont 	return res;
1142b0104773SPascal Brand }
1143b0104773SPascal Brand 
1144b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */
1145b0104773SPascal Brand 
11468f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
1147b0104773SPascal Brand {
1148b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
1149b0104773SPascal Brand 		TEE_Panic(0);
1150642a1607SCedric Chaumont 
1151b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1152b0104773SPascal Brand 		TEE_Panic(0);
1153642a1607SCedric Chaumont 
1154642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1155642a1607SCedric Chaumont 	    !(operation->key1))
1156642a1607SCedric Chaumont 		TEE_Panic(0);
1157642a1607SCedric Chaumont 
1158642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1159642a1607SCedric Chaumont 		TEE_ResetOperation(operation);
1160642a1607SCedric Chaumont 
1161642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1162642a1607SCedric Chaumont 
11636d15db08SJerome Forissier 	init_hash_operation(operation, IV, IVLen);
1164b0104773SPascal Brand }
1165b0104773SPascal Brand 
11668f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk,
116728e0efc6SCedric Chaumont 		   uint32_t chunkSize)
1168b0104773SPascal Brand {
1169b0104773SPascal Brand 	TEE_Result res;
1170b0104773SPascal Brand 
117128e0efc6SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
1172b0104773SPascal Brand 		TEE_Panic(0);
1173642a1607SCedric Chaumont 
117428e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1175b0104773SPascal Brand 		TEE_Panic(0);
1176642a1607SCedric Chaumont 
117728e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1178b0104773SPascal Brand 		TEE_Panic(0);
1179b0104773SPascal Brand 
1180642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE)
1181642a1607SCedric Chaumont 		TEE_Panic(0);
1182642a1607SCedric Chaumont 
11832c028fdeSJerome Forissier 	res = _utee_hash_update(operation->state, chunk, chunkSize);
1184b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1185b0104773SPascal Brand 		TEE_Panic(res);
1186b0104773SPascal Brand }
1187b0104773SPascal Brand 
118828e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
11898f07fe6fSJerome Forissier 			       const void *message, uint32_t messageLen,
119079a3c601SCedric Chaumont 			       void *mac, uint32_t *macLen)
1191b0104773SPascal Brand {
1192b0104773SPascal Brand 	TEE_Result res;
1193e86f1266SJens Wiklander 	uint64_t ml;
1194b0104773SPascal Brand 
11956915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!message && messageLen)) {
119628e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
119728e0efc6SCedric Chaumont 		goto out;
119828e0efc6SCedric Chaumont 	}
11996915bbbbSJens Wiklander 	__utee_check_inout_annotation(macLen, sizeof(*macLen));
1200b0104773SPascal Brand 
120128e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
120228e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
120328e0efc6SCedric Chaumont 		goto out;
120428e0efc6SCedric Chaumont 	}
120528e0efc6SCedric Chaumont 
120628e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
120728e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
120828e0efc6SCedric Chaumont 		goto out;
120928e0efc6SCedric Chaumont 	}
121028e0efc6SCedric Chaumont 
1211642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1212642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1213642a1607SCedric Chaumont 		goto out;
1214642a1607SCedric Chaumont 	}
1215642a1607SCedric Chaumont 
1216e86f1266SJens Wiklander 	ml = *macLen;
12172c028fdeSJerome Forissier 	res = _utee_hash_final(operation->state, message, messageLen, mac, &ml);
1218e86f1266SJens Wiklander 	*macLen = ml;
121928e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS)
122028e0efc6SCedric Chaumont 		goto out;
122128e0efc6SCedric Chaumont 
122228e0efc6SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
122328e0efc6SCedric Chaumont 
1224642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1225642a1607SCedric Chaumont 
122628e0efc6SCedric Chaumont out:
122728e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
122828e0efc6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
122928e0efc6SCedric Chaumont 		TEE_Panic(res);
123028e0efc6SCedric Chaumont 
1231b0104773SPascal Brand 	return res;
1232b0104773SPascal Brand }
1233b0104773SPascal Brand 
1234b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
12358f07fe6fSJerome Forissier 			       const void *message, uint32_t messageLen,
12368f07fe6fSJerome Forissier 			       const void *mac, uint32_t macLen)
1237b0104773SPascal Brand {
1238b0104773SPascal Brand 	TEE_Result res;
1239b0104773SPascal Brand 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
12407f74c64aSPascal Brand 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1241b0104773SPascal Brand 
124228e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
124328e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
124428e0efc6SCedric Chaumont 		goto out;
124528e0efc6SCedric Chaumont 	}
124628e0efc6SCedric Chaumont 
124728e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
124828e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
124928e0efc6SCedric Chaumont 		goto out;
125028e0efc6SCedric Chaumont 	}
125128e0efc6SCedric Chaumont 
1252642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1253642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1254642a1607SCedric Chaumont 		goto out;
1255642a1607SCedric Chaumont 	}
1256642a1607SCedric Chaumont 
1257b0104773SPascal Brand 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1258b0104773SPascal Brand 				  &computed_mac_size);
1259b0104773SPascal Brand 	if (res != TEE_SUCCESS)
126028e0efc6SCedric Chaumont 		goto out;
126128e0efc6SCedric Chaumont 
126228e0efc6SCedric Chaumont 	if (computed_mac_size != macLen) {
126328e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
126428e0efc6SCedric Chaumont 		goto out;
126528e0efc6SCedric Chaumont 	}
126628e0efc6SCedric Chaumont 
126748e10604SJerome Forissier 	if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) {
126828e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
126928e0efc6SCedric Chaumont 		goto out;
127028e0efc6SCedric Chaumont 	}
127128e0efc6SCedric Chaumont 
1272642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1273642a1607SCedric Chaumont 
127428e0efc6SCedric Chaumont out:
127528e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
127628e0efc6SCedric Chaumont 	    res != TEE_ERROR_MAC_INVALID)
127728e0efc6SCedric Chaumont 		TEE_Panic(res);
127828e0efc6SCedric Chaumont 
1279b0104773SPascal Brand 	return res;
1280b0104773SPascal Brand }
1281b0104773SPascal Brand 
1282b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */
1283b0104773SPascal Brand 
12848f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce,
128579a3c601SCedric Chaumont 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1286b0104773SPascal Brand 		      uint32_t payloadLen)
1287b0104773SPascal Brand {
1288b0104773SPascal Brand 	TEE_Result res;
1289b0104773SPascal Brand 
1290b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || nonce == NULL) {
1291b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1292b5816c88SCedric Chaumont 		goto out;
1293b5816c88SCedric Chaumont 	}
1294b5816c88SCedric Chaumont 
1295b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1296b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1297b5816c88SCedric Chaumont 		goto out;
1298b5816c88SCedric Chaumont 	}
1299b0104773SPascal Brand 
1300642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
1301642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1302642a1607SCedric Chaumont 		goto out;
1303642a1607SCedric Chaumont 	}
1304642a1607SCedric Chaumont 
1305b0104773SPascal Brand 	/*
1306b0104773SPascal Brand 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1307b0104773SPascal Brand 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1308b0104773SPascal Brand 	 * according to the same principle so we have to check here instead to
1309b0104773SPascal Brand 	 * be GP compliant.
1310b0104773SPascal Brand 	 */
1311b5816c88SCedric Chaumont 	if (operation->info.algorithm == TEE_ALG_AES_GCM) {
1312b0104773SPascal Brand 		/*
1313b0104773SPascal Brand 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1314b0104773SPascal Brand 		 */
1315b5816c88SCedric Chaumont 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) {
1316b5816c88SCedric Chaumont 			res = TEE_ERROR_NOT_SUPPORTED;
1317b5816c88SCedric Chaumont 			goto out;
1318b5816c88SCedric Chaumont 		}
1319b0104773SPascal Brand 	}
1320b0104773SPascal Brand 
13212c028fdeSJerome Forissier 	res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8,
13222c028fdeSJerome Forissier 				 AADLen, payloadLen);
1323b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1324b5816c88SCedric Chaumont 		goto out;
1325b5816c88SCedric Chaumont 
13267acaf5adSAlbert Schwarzkopf 	operation->info.digestLength = tagLen / 8;
1327f2674567SSumit Garg 	operation->buffer_offs = 0;
1328b5816c88SCedric Chaumont 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1329b5816c88SCedric Chaumont 
1330b5816c88SCedric Chaumont out:
1331b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1332b5816c88SCedric Chaumont 	    res != TEE_ERROR_NOT_SUPPORTED)
1333b0104773SPascal Brand 			TEE_Panic(res);
1334b5816c88SCedric Chaumont 
1335b0104773SPascal Brand 	return res;
1336b0104773SPascal Brand }
1337b0104773SPascal Brand 
13388f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata,
133979a3c601SCedric Chaumont 		     uint32_t AADdataLen)
1340b0104773SPascal Brand {
1341b0104773SPascal Brand 	TEE_Result res;
1342b0104773SPascal Brand 
1343b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1344b5816c88SCedric Chaumont 	    (AADdata == NULL && AADdataLen != 0))
1345b0104773SPascal Brand 		TEE_Panic(0);
1346642a1607SCedric Chaumont 
1347b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE)
1348b0104773SPascal Brand 		TEE_Panic(0);
1349642a1607SCedric Chaumont 
1350b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1351b0104773SPascal Brand 		TEE_Panic(0);
1352b0104773SPascal Brand 
13532c028fdeSJerome Forissier 	res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen);
1354642a1607SCedric Chaumont 
1355642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1356642a1607SCedric Chaumont 
1357b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1358b0104773SPascal Brand 		TEE_Panic(res);
1359b0104773SPascal Brand }
1360b0104773SPascal Brand 
13618f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData,
136279a3c601SCedric Chaumont 			uint32_t srcLen, void *destData, uint32_t *destLen)
1363b0104773SPascal Brand {
13646915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
13656915bbbbSJens Wiklander 	size_t req_dlen = 0;
13666915bbbbSJens Wiklander 	uint64_t dl = 0;
1367b0104773SPascal Brand 
13686915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1369b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1370b5816c88SCedric Chaumont 		goto out;
1371b5816c88SCedric Chaumont 	}
13726915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1373b5816c88SCedric Chaumont 
1374b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1375b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1376b5816c88SCedric Chaumont 		goto out;
1377b5816c88SCedric Chaumont 	}
1378b5816c88SCedric Chaumont 
1379b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1380b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1381b5816c88SCedric Chaumont 		goto out;
1382b5816c88SCedric Chaumont 	}
1383b0104773SPascal Brand 
1384827308b8SJerome Forissier 	if (!srcData && !srcLen) {
1385090268f5SJens Wiklander 		*destLen = 0;
1386827308b8SJerome Forissier 		res = TEE_SUCCESS;
1387827308b8SJerome Forissier 		goto out;
1388827308b8SJerome Forissier 	}
1389827308b8SJerome Forissier 
1390b0104773SPascal Brand 	/*
1391b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1392b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1393b0104773SPascal Brand 	 * can't restore sync with this API.
1394b0104773SPascal Brand 	 */
1395afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
1396b5816c88SCedric Chaumont 		req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen,
1397b5816c88SCedric Chaumont 				     operation->block_size);
1398afc0c182SBogdan Liulko 	} else {
1399afc0c182SBogdan Liulko 		req_dlen = srcLen;
1400afc0c182SBogdan Liulko 	}
1401afc0c182SBogdan Liulko 
14026915bbbbSJens Wiklander 	dl = *destLen;
14036915bbbbSJens Wiklander 	if (dl < req_dlen) {
1404b0104773SPascal Brand 		*destLen = req_dlen;
1405b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1406b5816c88SCedric Chaumont 		goto out;
1407b0104773SPascal Brand 	}
1408b0104773SPascal Brand 
1409afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
14102c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1411afc0c182SBogdan Liulko 					srcData, srcLen, destData, &dl);
1412afc0c182SBogdan Liulko 	} else {
1413afc0c182SBogdan Liulko 		if (srcLen > 0) {
14142c028fdeSJerome Forissier 			res = _utee_authenc_update_payload(operation->state,
1415afc0c182SBogdan Liulko 							   srcData, srcLen,
1416afc0c182SBogdan Liulko 							   destData, &dl);
1417afc0c182SBogdan Liulko 		} else {
1418afc0c182SBogdan Liulko 			dl = 0;
1419afc0c182SBogdan Liulko 			res = TEE_SUCCESS;
1420afc0c182SBogdan Liulko 		}
1421afc0c182SBogdan Liulko 	}
1422afc0c182SBogdan Liulko 	if (res != TEE_SUCCESS)
1423afc0c182SBogdan Liulko 		goto out;
1424afc0c182SBogdan Liulko 
1425e86f1266SJens Wiklander 	*destLen = dl;
1426b0104773SPascal Brand 
1427642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1428642a1607SCedric Chaumont 
1429b5816c88SCedric Chaumont out:
1430b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1431b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1432b5816c88SCedric Chaumont 			TEE_Panic(res);
1433b5816c88SCedric Chaumont 
1434b5816c88SCedric Chaumont 	return res;
1435b0104773SPascal Brand }
1436b0104773SPascal Brand 
1437b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation,
14388f07fe6fSJerome Forissier 			      const void *srcData, uint32_t srcLen,
143979a3c601SCedric Chaumont 			      void *destData, uint32_t *destLen, void *tag,
144079a3c601SCedric Chaumont 			      uint32_t *tagLen)
1441b0104773SPascal Brand {
1442b0104773SPascal Brand 	TEE_Result res;
1443b0104773SPascal Brand 	uint8_t *dst = destData;
1444b0104773SPascal Brand 	size_t acc_dlen = 0;
1445e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1446b0104773SPascal Brand 	size_t req_dlen;
1447e86f1266SJens Wiklander 	uint64_t tl;
1448b0104773SPascal Brand 
14496915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1450b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1451b5816c88SCedric Chaumont 		goto out;
1452b5816c88SCedric Chaumont 	}
14536915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
14546915bbbbSJens Wiklander 	__utee_check_inout_annotation(tagLen, sizeof(*tagLen));
1455b5816c88SCedric Chaumont 
1456b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1457b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1458b5816c88SCedric Chaumont 		goto out;
1459b5816c88SCedric Chaumont 	}
1460b5816c88SCedric Chaumont 
1461b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1462b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1463b5816c88SCedric Chaumont 		goto out;
1464b5816c88SCedric Chaumont 	}
1465b0104773SPascal Brand 
1466b0104773SPascal Brand 	/*
1467b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1468b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1469b0104773SPascal Brand 	 * can't restore sync with this API.
14702733280aSEtienne Carriere 	 *
14712733280aSEtienne Carriere 	 * Need to check this before update_payload since sync would be lost if
14722733280aSEtienne Carriere 	 * we return short buffer after that.
1473b0104773SPascal Brand 	 */
14742733280aSEtienne Carriere 	res = TEE_ERROR_GENERIC;
14752733280aSEtienne Carriere 
1476b5816c88SCedric Chaumont 	req_dlen = operation->buffer_offs + srcLen;
1477b0104773SPascal Brand 	if (*destLen < req_dlen) {
1478b0104773SPascal Brand 		*destLen = req_dlen;
1479b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1480b0104773SPascal Brand 	}
1481b0104773SPascal Brand 
14827acaf5adSAlbert Schwarzkopf 	if (*tagLen < operation->info.digestLength) {
14837acaf5adSAlbert Schwarzkopf 		*tagLen = operation->info.digestLength;
1484b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1485b0104773SPascal Brand 	}
1486b0104773SPascal Brand 
14872733280aSEtienne Carriere 	if (res == TEE_ERROR_SHORT_BUFFER)
14882733280aSEtienne Carriere 		goto out;
14892733280aSEtienne Carriere 
1490afc0c182SBogdan Liulko 	tl = *tagLen;
1491b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1492afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
14932c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1494afc0c182SBogdan Liulko 					srcData, srcLen, dst, &tmp_dlen);
1495b5816c88SCedric Chaumont 		if (res != TEE_SUCCESS)
1496b5816c88SCedric Chaumont 			goto out;
1497b5816c88SCedric Chaumont 
1498b0104773SPascal Brand 		dst += tmp_dlen;
1499b0104773SPascal Brand 		acc_dlen += tmp_dlen;
1500b0104773SPascal Brand 
1501b0104773SPascal Brand 		tmp_dlen = *destLen - acc_dlen;
15022c028fdeSJerome Forissier 		res = _utee_authenc_enc_final(operation->state,
1503afc0c182SBogdan Liulko 					      operation->buffer,
1504afc0c182SBogdan Liulko 					      operation->buffer_offs, dst,
1505afc0c182SBogdan Liulko 					      &tmp_dlen, tag, &tl);
1506afc0c182SBogdan Liulko 	} else {
15072c028fdeSJerome Forissier 		res = _utee_authenc_enc_final(operation->state, srcData,
1508afc0c182SBogdan Liulko 					      srcLen, dst, &tmp_dlen,
1509e86f1266SJens Wiklander 					      tag, &tl);
1510afc0c182SBogdan Liulko 	}
1511e86f1266SJens Wiklander 	*tagLen = tl;
1512b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1513b5816c88SCedric Chaumont 		goto out;
1514b0104773SPascal Brand 
1515b5816c88SCedric Chaumont 	acc_dlen += tmp_dlen;
1516b0104773SPascal Brand 	*destLen = acc_dlen;
1517642a1607SCedric Chaumont 
1518b5816c88SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1519b5816c88SCedric Chaumont 
1520642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1521642a1607SCedric Chaumont 
1522b5816c88SCedric Chaumont out:
1523b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1524b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1525b5816c88SCedric Chaumont 			TEE_Panic(res);
1526b0104773SPascal Brand 
1527b0104773SPascal Brand 	return res;
1528b0104773SPascal Brand }
1529b0104773SPascal Brand 
1530b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation,
15318f07fe6fSJerome Forissier 			      const void *srcData, uint32_t srcLen,
1532b5816c88SCedric Chaumont 			      void *destData, uint32_t *destLen, void *tag,
153379a3c601SCedric Chaumont 			      uint32_t tagLen)
1534b0104773SPascal Brand {
1535b0104773SPascal Brand 	TEE_Result res;
1536b0104773SPascal Brand 	uint8_t *dst = destData;
1537b0104773SPascal Brand 	size_t acc_dlen = 0;
1538e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1539b0104773SPascal Brand 	size_t req_dlen;
1540b0104773SPascal Brand 
15416915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1542b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1543b5816c88SCedric Chaumont 		goto out;
1544b5816c88SCedric Chaumont 	}
15456915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1546b5816c88SCedric Chaumont 
1547b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1548b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1549b5816c88SCedric Chaumont 		goto out;
1550b5816c88SCedric Chaumont 	}
1551b5816c88SCedric Chaumont 
1552b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1553b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1554b5816c88SCedric Chaumont 		goto out;
1555b5816c88SCedric Chaumont 	}
1556b0104773SPascal Brand 
1557b0104773SPascal Brand 	/*
1558b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1559b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1560b0104773SPascal Brand 	 * can't restore sync with this API.
1561b0104773SPascal Brand 	 */
1562b5816c88SCedric Chaumont 	req_dlen = operation->buffer_offs + srcLen;
1563b0104773SPascal Brand 	if (*destLen < req_dlen) {
1564b0104773SPascal Brand 		*destLen = req_dlen;
1565b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1566b5816c88SCedric Chaumont 		goto out;
1567b0104773SPascal Brand 	}
1568b0104773SPascal Brand 
1569b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1570afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
15712c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1572afc0c182SBogdan Liulko 					srcData, srcLen, dst, &tmp_dlen);
1573b5816c88SCedric Chaumont 		if (res != TEE_SUCCESS)
1574b5816c88SCedric Chaumont 			goto out;
1575b5816c88SCedric Chaumont 
1576b0104773SPascal Brand 		dst += tmp_dlen;
1577b0104773SPascal Brand 		acc_dlen += tmp_dlen;
1578b0104773SPascal Brand 
1579b0104773SPascal Brand 		tmp_dlen = *destLen - acc_dlen;
15802c028fdeSJerome Forissier 		res = _utee_authenc_dec_final(operation->state,
1581afc0c182SBogdan Liulko 					      operation->buffer,
1582afc0c182SBogdan Liulko 					      operation->buffer_offs, dst,
1583afc0c182SBogdan Liulko 					      &tmp_dlen, tag, tagLen);
1584afc0c182SBogdan Liulko 	} else {
15852c028fdeSJerome Forissier 		res = _utee_authenc_dec_final(operation->state, srcData,
1586afc0c182SBogdan Liulko 					      srcLen, dst, &tmp_dlen,
1587b5816c88SCedric Chaumont 					      tag, tagLen);
1588afc0c182SBogdan Liulko 	}
1589b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1590b5816c88SCedric Chaumont 		goto out;
1591b5816c88SCedric Chaumont 
1592b0104773SPascal Brand 	/* Supplied tagLen should match what we initiated with */
15937acaf5adSAlbert Schwarzkopf 	if (tagLen != operation->info.digestLength)
1594b0104773SPascal Brand 		res = TEE_ERROR_MAC_INVALID;
1595b0104773SPascal Brand 
1596b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1597b0104773SPascal Brand 	*destLen = acc_dlen;
1598642a1607SCedric Chaumont 
1599b5816c88SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1600b5816c88SCedric Chaumont 
1601642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1602642a1607SCedric Chaumont 
1603b5816c88SCedric Chaumont out:
1604b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1605b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER &&
1606b5816c88SCedric Chaumont 	    res != TEE_ERROR_MAC_INVALID)
1607b5816c88SCedric Chaumont 			TEE_Panic(res);
1608b0104773SPascal Brand 
1609b0104773SPascal Brand 	return res;
1610b0104773SPascal Brand }
1611b0104773SPascal Brand 
1612b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */
1613b0104773SPascal Brand 
161412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation,
16158f07fe6fSJerome Forissier 				 const TEE_Attribute *params,
16168f07fe6fSJerome Forissier 				 uint32_t paramCount, const void *srcData,
161779a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
161879a3c601SCedric Chaumont 				 uint32_t *destLen)
1619b0104773SPascal Brand {
16206915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1621e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
16226915bbbbSJens Wiklander 	uint64_t dl = 0;
1623b0104773SPascal Brand 
16246915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
1625b0104773SPascal Brand 		TEE_Panic(0);
16266915bbbbSJens Wiklander 
16276915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
16286915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
16296915bbbbSJens Wiklander 
163012e66b6fSCedric Chaumont 	if (!operation->key1)
1631b0104773SPascal Brand 		TEE_Panic(0);
163212e66b6fSCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
163312e66b6fSCedric Chaumont 		TEE_Panic(0);
163412e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_ENCRYPT)
1635b0104773SPascal Brand 		TEE_Panic(0);
1636b0104773SPascal Brand 
1637e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1638e86f1266SJens Wiklander 	dl = *destLen;
16392c028fdeSJerome Forissier 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1640e86f1266SJens Wiklander 				  srcLen, destData, &dl);
1641e86f1266SJens Wiklander 	*destLen = dl;
164212e66b6fSCedric Chaumont 
16438844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
16448844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
16458844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1646b0104773SPascal Brand 		TEE_Panic(res);
164712e66b6fSCedric Chaumont 
1648b0104773SPascal Brand 	return res;
1649b0104773SPascal Brand }
1650b0104773SPascal Brand 
165112e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation,
16528f07fe6fSJerome Forissier 				 const TEE_Attribute *params,
16538f07fe6fSJerome Forissier 				 uint32_t paramCount, const void *srcData,
165479a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
165579a3c601SCedric Chaumont 				 uint32_t *destLen)
1656b0104773SPascal Brand {
16576915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1658e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
16596915bbbbSJens Wiklander 	uint64_t dl = 0;
1660b0104773SPascal Brand 
16616915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
1662b0104773SPascal Brand 		TEE_Panic(0);
16636915bbbbSJens Wiklander 
16646915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
16656915bbbbSJens Wiklander 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
16666915bbbbSJens Wiklander 
166712e66b6fSCedric Chaumont 	if (!operation->key1)
1668b0104773SPascal Brand 		TEE_Panic(0);
166912e66b6fSCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
167012e66b6fSCedric Chaumont 		TEE_Panic(0);
167112e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_DECRYPT)
1672b0104773SPascal Brand 		TEE_Panic(0);
1673b0104773SPascal Brand 
1674e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1675e86f1266SJens Wiklander 	dl = *destLen;
16762c028fdeSJerome Forissier 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1677e86f1266SJens Wiklander 				  srcLen, destData, &dl);
1678e86f1266SJens Wiklander 	*destLen = dl;
167912e66b6fSCedric Chaumont 
16808844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
16818844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
16828844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1683b0104773SPascal Brand 		TEE_Panic(res);
168412e66b6fSCedric Chaumont 
1685b0104773SPascal Brand 	return res;
1686b0104773SPascal Brand }
1687b0104773SPascal Brand 
168812e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation,
16898f07fe6fSJerome Forissier 				    const TEE_Attribute *params,
16908f07fe6fSJerome Forissier 				    uint32_t paramCount, const void *digest,
169179a3c601SCedric Chaumont 				    uint32_t digestLen, void *signature,
169279a3c601SCedric Chaumont 				    uint32_t *signatureLen)
1693b0104773SPascal Brand {
16946915bbbbSJens Wiklander 	TEE_Result res = TEE_SUCCESS;
1695e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
16966915bbbbSJens Wiklander 	uint64_t sl = 0;
1697b0104773SPascal Brand 
16986915bbbbSJens Wiklander 	if (operation == TEE_HANDLE_NULL || (!digest && digestLen))
1699b0104773SPascal Brand 		TEE_Panic(0);
17006915bbbbSJens Wiklander 
17016915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
17026915bbbbSJens Wiklander 	__utee_check_inout_annotation(signatureLen, sizeof(*signatureLen));
17036915bbbbSJens Wiklander 
170412e66b6fSCedric Chaumont 	if (!operation->key1)
1705b0104773SPascal Brand 		TEE_Panic(0);
170612e66b6fSCedric Chaumont 	if (operation->info.operationClass !=
170712e66b6fSCedric Chaumont 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
170812e66b6fSCedric Chaumont 		TEE_Panic(0);
170912e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_SIGN)
1710b0104773SPascal Brand 		TEE_Panic(0);
1711b0104773SPascal Brand 
1712e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1713e86f1266SJens Wiklander 	sl = *signatureLen;
17142c028fdeSJerome Forissier 	res = _utee_asymm_operate(operation->state, ua, paramCount, digest,
1715e86f1266SJens Wiklander 				  digestLen, signature, &sl);
1716e86f1266SJens Wiklander 	*signatureLen = sl;
171712e66b6fSCedric Chaumont 
1718b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1719b0104773SPascal Brand 		TEE_Panic(res);
172012e66b6fSCedric Chaumont 
1721b0104773SPascal Brand 	return res;
1722b0104773SPascal Brand }
1723b0104773SPascal Brand 
172412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,
17258f07fe6fSJerome Forissier 				      const TEE_Attribute *params,
17268f07fe6fSJerome Forissier 				      uint32_t paramCount, const void *digest,
17278f07fe6fSJerome Forissier 				      uint32_t digestLen,
17288f07fe6fSJerome Forissier 				      const void *signature,
172979a3c601SCedric Chaumont 				      uint32_t signatureLen)
1730b0104773SPascal Brand {
1731b0104773SPascal Brand 	TEE_Result res;
1732e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1733b0104773SPascal Brand 
173412e66b6fSCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
173512e66b6fSCedric Chaumont 	    (digest == NULL && digestLen != 0) ||
1736b0104773SPascal Brand 	    (signature == NULL && signatureLen != 0))
1737b0104773SPascal Brand 		TEE_Panic(0);
17386915bbbbSJens Wiklander 
17396915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
17406915bbbbSJens Wiklander 
174112e66b6fSCedric Chaumont 	if (!operation->key1)
1742b0104773SPascal Brand 		TEE_Panic(0);
174312e66b6fSCedric Chaumont 	if (operation->info.operationClass !=
174412e66b6fSCedric Chaumont 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
174512e66b6fSCedric Chaumont 		TEE_Panic(0);
174612e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_VERIFY)
1747b0104773SPascal Brand 		TEE_Panic(0);
1748b0104773SPascal Brand 
1749e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
17502c028fdeSJerome Forissier 	res = _utee_asymm_verify(operation->state, ua, paramCount, digest,
175112e66b6fSCedric Chaumont 				 digestLen, signature, signatureLen);
175212e66b6fSCedric Chaumont 
1753b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1754b0104773SPascal Brand 		TEE_Panic(res);
175512e66b6fSCedric Chaumont 
1756b0104773SPascal Brand 	return res;
1757b0104773SPascal Brand }
1758b0104773SPascal Brand 
1759b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */
1760b0104773SPascal Brand 
1761b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation,
1762b0104773SPascal Brand 		   const TEE_Attribute *params, uint32_t paramCount,
1763b0104773SPascal Brand 		   TEE_ObjectHandle derivedKey)
1764b0104773SPascal Brand {
1765b0104773SPascal Brand 	TEE_Result res;
1766b0104773SPascal Brand 	TEE_ObjectInfo key_info;
1767e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1768b0104773SPascal Brand 
1769b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1770b0104773SPascal Brand 		TEE_Panic(0);
17716915bbbbSJens Wiklander 
17726915bbbbSJens Wiklander 	__utee_check_attr_in_annotation(params, paramCount);
17736915bbbbSJens Wiklander 
17748854d3c6SJerome Forissier 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
17758854d3c6SJerome Forissier 	    TEE_OPERATION_KEY_DERIVATION)
1776b0104773SPascal Brand 		TEE_Panic(0);
1777b0104773SPascal Brand 
1778b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1779b0104773SPascal Brand 		TEE_Panic(0);
178084fa9467SCedric Chaumont 	if (!operation->key1)
178184fa9467SCedric Chaumont 		TEE_Panic(0);
1782b0104773SPascal Brand 	if (operation->info.mode != TEE_MODE_DERIVE)
1783b0104773SPascal Brand 		TEE_Panic(0);
1784b0104773SPascal Brand 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1785b0104773SPascal Brand 		TEE_Panic(0);
1786b0104773SPascal Brand 
17872c028fdeSJerome Forissier 	res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info);
1788b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1789b36311adSJerome Forissier 		TEE_Panic(res);
1790b0104773SPascal Brand 
1791b0104773SPascal Brand 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1792b0104773SPascal Brand 		TEE_Panic(0);
1793b0104773SPascal Brand 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1794b0104773SPascal Brand 		TEE_Panic(0);
1795b0104773SPascal Brand 
1796e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
17972c028fdeSJerome Forissier 	res = _utee_cryp_derive_key(operation->state, ua, paramCount,
1798e86f1266SJens Wiklander 				    (unsigned long)derivedKey);
1799b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1800b0104773SPascal Brand 		TEE_Panic(res);
1801b0104773SPascal Brand }
1802b0104773SPascal Brand 
1803b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */
1804b0104773SPascal Brand 
180579a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1806b0104773SPascal Brand {
1807b0104773SPascal Brand 	TEE_Result res;
1808b0104773SPascal Brand 
18092c028fdeSJerome Forissier 	res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1810b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1811b0104773SPascal Brand 		TEE_Panic(res);
1812b0104773SPascal Brand }
1813433c4257SJens Wiklander 
1814433c4257SJens Wiklander int rand(void)
1815433c4257SJens Wiklander {
1816433c4257SJens Wiklander 	int rc;
1817433c4257SJens Wiklander 
1818433c4257SJens Wiklander 	TEE_GenerateRandom(&rc, sizeof(rc));
1819433c4257SJens Wiklander 
1820433c4257SJens Wiklander 	/*
1821433c4257SJens Wiklander 	 * RAND_MAX is the larges int, INT_MAX which is all bits but the
1822433c4257SJens Wiklander 	 * highest bit set.
1823433c4257SJens Wiklander 	 */
1824433c4257SJens Wiklander 	return rc & RAND_MAX;
1825433c4257SJens Wiklander }
182679170ce0SJerome Forissier 
182779170ce0SJerome Forissier TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element)
182879170ce0SJerome Forissier {
182979170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_AES)) {
183079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
183179170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_ECB_NOPAD)
183279170ce0SJerome Forissier 				goto check_element_none;
183379170ce0SJerome Forissier 		}
183479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
183579170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CBC_NOPAD)
183679170ce0SJerome Forissier 				goto check_element_none;
183779170ce0SJerome Forissier 		}
183879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
183979170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CTR)
184079170ce0SJerome Forissier 				goto check_element_none;
184179170ce0SJerome Forissier 		}
184279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CTS)) {
184379170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CTS)
184479170ce0SJerome Forissier 				goto check_element_none;
184579170ce0SJerome Forissier 		}
184679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_XTS)) {
184779170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_XTS)
184879170ce0SJerome Forissier 				goto check_element_none;
184979170ce0SJerome Forissier 		}
185079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
185179170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CBC_MAC_NOPAD ||
185279170ce0SJerome Forissier 			    alg == TEE_ALG_AES_CBC_MAC_PKCS5)
185379170ce0SJerome Forissier 				goto check_element_none;
185479170ce0SJerome Forissier 		}
185579170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CMAC)) {
185679170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CMAC)
185779170ce0SJerome Forissier 				goto check_element_none;
185879170ce0SJerome Forissier 		}
185979170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CCM)) {
186079170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CCM)
186179170ce0SJerome Forissier 				goto check_element_none;
186279170ce0SJerome Forissier 		}
186379170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_GCM)) {
186479170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_GCM)
186579170ce0SJerome Forissier 				goto check_element_none;
186679170ce0SJerome Forissier 		}
186779170ce0SJerome Forissier 	}
186879170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_DES)) {
186979170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
187079170ce0SJerome Forissier 			if (alg == TEE_ALG_DES_ECB_NOPAD ||
187179170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_ECB_NOPAD)
187279170ce0SJerome Forissier 				goto check_element_none;
187379170ce0SJerome Forissier 		}
187479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
187579170ce0SJerome Forissier 			if (alg == TEE_ALG_DES_CBC_NOPAD ||
187679170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_CBC_NOPAD)
187779170ce0SJerome Forissier 				goto check_element_none;
187879170ce0SJerome Forissier 		}
187979170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
188079170ce0SJerome Forissier 			if (alg == TEE_ALG_DES_CBC_MAC_NOPAD ||
188179170ce0SJerome Forissier 			    alg == TEE_ALG_DES_CBC_MAC_PKCS5 ||
188279170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_CBC_MAC_NOPAD ||
188379170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_CBC_MAC_PKCS5)
188479170ce0SJerome Forissier 				goto check_element_none;
188579170ce0SJerome Forissier 		}
188679170ce0SJerome Forissier 	}
188779170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_MD5)) {
188879170ce0SJerome Forissier 		if (alg == TEE_ALG_MD5)
188979170ce0SJerome Forissier 			goto check_element_none;
189079170ce0SJerome Forissier 	}
189179170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
189279170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA1)
189379170ce0SJerome Forissier 			goto check_element_none;
189479170ce0SJerome Forissier 	}
189579170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
189679170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA224)
189779170ce0SJerome Forissier 			goto check_element_none;
189879170ce0SJerome Forissier 	}
189979170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
190079170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA256)
190179170ce0SJerome Forissier 			goto check_element_none;
190279170ce0SJerome Forissier 	}
190379170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
190479170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA384)
190579170ce0SJerome Forissier 			goto check_element_none;
190679170ce0SJerome Forissier 	}
190779170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
190879170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA512)
190979170ce0SJerome Forissier 			goto check_element_none;
191079170ce0SJerome Forissier 	}
191179170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
191279170ce0SJerome Forissier 		if (alg == TEE_ALG_MD5SHA1)
191379170ce0SJerome Forissier 			goto check_element_none;
191479170ce0SJerome Forissier 	}
191579170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_HMAC)) {
191679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
191779170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_MD5)
191879170ce0SJerome Forissier 				goto check_element_none;
191979170ce0SJerome Forissier 		}
192079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
192179170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA1)
192279170ce0SJerome Forissier 				goto check_element_none;
192379170ce0SJerome Forissier 		}
192479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
192579170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA224)
192679170ce0SJerome Forissier 				goto check_element_none;
192779170ce0SJerome Forissier 		}
192879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
192979170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA256)
193079170ce0SJerome Forissier 				goto check_element_none;
193179170ce0SJerome Forissier 		}
193279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
193379170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA384)
193479170ce0SJerome Forissier 				goto check_element_none;
193579170ce0SJerome Forissier 		}
193679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
193779170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA512)
193879170ce0SJerome Forissier 				goto check_element_none;
193979170ce0SJerome Forissier 		}
194079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SM3)) {
194179170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SM3)
194279170ce0SJerome Forissier 				goto check_element_none;
194379170ce0SJerome Forissier 		}
194479170ce0SJerome Forissier 	}
194579170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM3)) {
194679170ce0SJerome Forissier 		if (alg == TEE_ALG_SM3)
194779170ce0SJerome Forissier 			goto check_element_none;
194879170ce0SJerome Forissier 	}
194979170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM4)) {
195079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
195179170ce0SJerome Forissier 			if (alg == TEE_ALG_SM4_ECB_NOPAD)
195279170ce0SJerome Forissier 				goto check_element_none;
195379170ce0SJerome Forissier 		}
195479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
195579170ce0SJerome Forissier 			if (alg == TEE_ALG_SM4_CBC_NOPAD)
195679170ce0SJerome Forissier 				goto check_element_none;
195779170ce0SJerome Forissier 		}
195879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
195979170ce0SJerome Forissier 			if (alg == TEE_ALG_SM4_CTR)
196079170ce0SJerome Forissier 				goto check_element_none;
196179170ce0SJerome Forissier 		}
196279170ce0SJerome Forissier 	}
196379170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_RSA)) {
196479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
196579170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5)
196679170ce0SJerome Forissier 				goto check_element_none;
196779170ce0SJerome Forissier 		}
196879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
196979170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 ||
197079170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 ||
197179170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1)
197279170ce0SJerome Forissier 				goto check_element_none;
197379170ce0SJerome Forissier 		}
197479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
197579170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1)
197679170ce0SJerome Forissier 				goto check_element_none;
197779170ce0SJerome Forissier 		}
197879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
197979170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 ||
198079170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 ||
198179170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224)
198279170ce0SJerome Forissier 				goto check_element_none;
198379170ce0SJerome Forissier 		}
198479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
198579170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 ||
198679170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 ||
198779170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256)
198879170ce0SJerome Forissier 				goto check_element_none;
198979170ce0SJerome Forissier 		}
199079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
199179170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 ||
199279170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 ||
199379170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384)
199479170ce0SJerome Forissier 				goto check_element_none;
199579170ce0SJerome Forissier 		}
199679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
199779170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 ||
199879170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 ||
199979170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512)
200079170ce0SJerome Forissier 				goto check_element_none;
200179170ce0SJerome Forissier 		}
200279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) {
200379170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5)
200479170ce0SJerome Forissier 				goto check_element_none;
200579170ce0SJerome Forissier 		}
200679170ce0SJerome Forissier 		if (alg == TEE_ALG_RSA_NOPAD)
200779170ce0SJerome Forissier 			goto check_element_none;
200879170ce0SJerome Forissier 	}
200979170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_DSA)) {
201079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
201179170ce0SJerome Forissier 			if (alg == TEE_ALG_DSA_SHA1)
201279170ce0SJerome Forissier 				goto check_element_none;
201379170ce0SJerome Forissier 		}
201479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
201579170ce0SJerome Forissier 			if (alg == TEE_ALG_DSA_SHA224)
201679170ce0SJerome Forissier 				goto check_element_none;
201779170ce0SJerome Forissier 		}
201879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
201979170ce0SJerome Forissier 			if (alg == TEE_ALG_DSA_SHA256)
202079170ce0SJerome Forissier 				goto check_element_none;
202179170ce0SJerome Forissier 		}
202279170ce0SJerome Forissier 	}
202379170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_DH)) {
202479170ce0SJerome Forissier 		if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
202579170ce0SJerome Forissier 			goto check_element_none;
202679170ce0SJerome Forissier 	}
202779170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_ECC)) {
202879170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) &&
202979170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P192)
203079170ce0SJerome Forissier 			return TEE_SUCCESS;
203179170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) &&
203279170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P224)
203379170ce0SJerome Forissier 			return TEE_SUCCESS;
203479170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) &&
203579170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P256)
203679170ce0SJerome Forissier 			return TEE_SUCCESS;
203779170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) &&
203879170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P384)
203979170ce0SJerome Forissier 			return TEE_SUCCESS;
204079170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) &&
204179170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P521)
204279170ce0SJerome Forissier 			return TEE_SUCCESS;
204379170ce0SJerome Forissier 	}
204479170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) {
204579170ce0SJerome Forissier 		if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2)
204679170ce0SJerome Forissier 			return TEE_SUCCESS;
204779170ce0SJerome Forissier 	}
204879170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) {
204979170ce0SJerome Forissier 		if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2)
205079170ce0SJerome Forissier 			return TEE_SUCCESS;
205179170ce0SJerome Forissier 	}
205279170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) {
205379170ce0SJerome Forissier 		if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2)
205479170ce0SJerome Forissier 			return TEE_SUCCESS;
205579170ce0SJerome Forissier 	}
205679170ce0SJerome Forissier 
205779170ce0SJerome Forissier 	return TEE_ERROR_NOT_SUPPORTED;
205879170ce0SJerome Forissier check_element_none:
205979170ce0SJerome Forissier 	if (element == TEE_CRYPTO_ELEMENT_NONE)
206079170ce0SJerome Forissier 		return TEE_SUCCESS;
206179170ce0SJerome Forissier 	return TEE_ERROR_NOT_SUPPORTED;
206279170ce0SJerome Forissier }
2063