xref: /optee_os/lib/libutee/tee_api_operations.c (revision 2c028fdebbedee91f88f6c5325b5064a124dfe46)
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 
112218d9055SCedric Chaumont 	/* Check algorithm mode */
113b0104773SPascal Brand 	switch (algorithm) {
114b0104773SPascal Brand 	case TEE_ALG_AES_CTS:
115b0104773SPascal Brand 	case TEE_ALG_AES_XTS:
116b0104773SPascal Brand 		buffer_two_blocks = true;
1174bd53c54SJerome 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;
134afc0c182SBogdan Liulko 		/* 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;
24005304565SCedric Chaumont 		/* v1.1: flags always set for digest operations */
241b0104773SPascal Brand 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
242b0104773SPascal Brand 		req_key_usage = 0;
243b0104773SPascal Brand 		break;
244b0104773SPascal Brand 
245b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_NOPAD:
246b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_NOPAD:
247b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_PKCS5:
248b0104773SPascal Brand 	case TEE_ALG_AES_CMAC:
249b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_PKCS5:
250b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
251b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
252b0104773SPascal Brand 	case TEE_ALG_HMAC_MD5:
253b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA1:
254b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA224:
255b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA256:
256b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA384:
257b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA512:
25847645577SJerome Forissier 	case TEE_ALG_HMAC_SM3:
259b0104773SPascal Brand 		if (mode != TEE_MODE_MAC)
260b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
261b0104773SPascal Brand 		req_key_usage = TEE_USAGE_MAC;
262b0104773SPascal Brand 		break;
263b0104773SPascal Brand 
264b0104773SPascal Brand 	default:
265b0104773SPascal Brand 		return TEE_ERROR_NOT_SUPPORTED;
266b0104773SPascal Brand 	}
267b0104773SPascal Brand 
268b66f219bSJens Wiklander 	op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO);
2699b52c538SCedric Chaumont 	if (!op)
270b0104773SPascal Brand 		return TEE_ERROR_OUT_OF_MEMORY;
271b0104773SPascal Brand 
272b0104773SPascal Brand 	op->info.algorithm = algorithm;
273b0104773SPascal Brand 	op->info.operationClass = TEE_ALG_GET_CLASS(algorithm);
2746a2e0a9fSGabor Szekely #ifdef CFG_CRYPTO_RSASSA_NA1
2756a2e0a9fSGabor Szekely 	if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5)
2766a2e0a9fSGabor Szekely 		op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE;
2776a2e0a9fSGabor Szekely #endif
278b0104773SPascal Brand 	op->info.mode = mode;
2792e5e6460SAlbert Schwarzkopf 	op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm);
280b0104773SPascal Brand 	op->info.maxKeySize = maxKeySize;
281b0104773SPascal Brand 	op->info.requiredKeyUsage = req_key_usage;
282b0104773SPascal Brand 	op->info.handleState = handle_state;
283b0104773SPascal Brand 
284b0104773SPascal Brand 	if (block_size > 1) {
285b0104773SPascal Brand 		size_t buffer_size = block_size;
286b0104773SPascal Brand 
287b0104773SPascal Brand 		if (buffer_two_blocks)
288b0104773SPascal Brand 			buffer_size *= 2;
289b0104773SPascal Brand 
2909b52c538SCedric Chaumont 		op->buffer = TEE_Malloc(buffer_size,
2919b52c538SCedric Chaumont 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
292b0104773SPascal Brand 		if (op->buffer == NULL) {
293b0104773SPascal Brand 			res = TEE_ERROR_OUT_OF_MEMORY;
294b66f219bSJens Wiklander 			goto out;
295b0104773SPascal Brand 		}
296b0104773SPascal Brand 	}
297b0104773SPascal Brand 	op->block_size = block_size;
298b0104773SPascal Brand 	op->buffer_two_blocks = buffer_two_blocks;
299b0104773SPascal Brand 
300b0104773SPascal Brand 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
301b0104773SPascal Brand 		uint32_t mks = maxKeySize;
302b0104773SPascal Brand 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
303b0104773SPascal Brand 						       with_private_key);
304b0104773SPascal Brand 
305b0104773SPascal Brand 		/*
306b0104773SPascal Brand 		 * If two keys are expected the max key size is the sum of
307b0104773SPascal Brand 		 * the size of both keys.
308b0104773SPascal Brand 		 */
309b0104773SPascal Brand 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
310b0104773SPascal Brand 			mks /= 2;
311b0104773SPascal Brand 
312b0104773SPascal Brand 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
313b0104773SPascal Brand 		if (res != TEE_SUCCESS)
314b66f219bSJens Wiklander 			goto out;
315b0104773SPascal Brand 
31605304565SCedric Chaumont 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
3179b52c538SCedric Chaumont 			res = TEE_AllocateTransientObject(key_type, mks,
318b0104773SPascal Brand 							  &op->key2);
319b0104773SPascal Brand 			if (res != TEE_SUCCESS)
320b66f219bSJens Wiklander 				goto out;
321b0104773SPascal Brand 		}
322b0104773SPascal Brand 	}
323b0104773SPascal Brand 
324*2c028fdeSJerome Forissier 	res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1,
325e86f1266SJens Wiklander 				     (unsigned long)op->key2, &op->state);
326b66f219bSJens Wiklander 	if (res != TEE_SUCCESS)
327b66f219bSJens Wiklander 		goto out;
328b0104773SPascal Brand 
32905304565SCedric Chaumont 	/*
33005304565SCedric Chaumont 	 * Initialize digest operations
33105304565SCedric Chaumont 	 * Other multi-stage operations initialized w/ TEE_xxxInit functions
33205304565SCedric Chaumont 	 * Non-applicable on asymmetric operations
33305304565SCedric Chaumont 	 */
33405304565SCedric Chaumont 	if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) {
335*2c028fdeSJerome Forissier 		res = _utee_hash_init(op->state, NULL, 0);
33605304565SCedric Chaumont 		if (res != TEE_SUCCESS)
337b66f219bSJens Wiklander 			goto out;
33805304565SCedric Chaumont 		/* v1.1: flags always set for digest operations */
33905304565SCedric Chaumont 		op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
34005304565SCedric Chaumont 	}
34105304565SCedric Chaumont 
342642a1607SCedric Chaumont 	op->operationState = TEE_OPERATION_STATE_INITIAL;
343642a1607SCedric Chaumont 
344b0104773SPascal Brand 	*operation = op;
345b0104773SPascal Brand 
346b66f219bSJens Wiklander out:
347b66f219bSJens Wiklander 	if (res != TEE_SUCCESS) {
348b66f219bSJens Wiklander 		if (res != TEE_ERROR_OUT_OF_MEMORY &&
3499b52c538SCedric Chaumont 		    res != TEE_ERROR_NOT_SUPPORTED)
350b36311adSJerome Forissier 			TEE_Panic(res);
351b66f219bSJens Wiklander 		if (op) {
352b66f219bSJens Wiklander 			if (op->state) {
353b66f219bSJens Wiklander 				TEE_FreeOperation(op);
354b66f219bSJens Wiklander 			} else {
355b66f219bSJens Wiklander 				TEE_Free(op->buffer);
356b66f219bSJens Wiklander 				TEE_FreeTransientObject(op->key1);
357b66f219bSJens Wiklander 				TEE_FreeTransientObject(op->key2);
358b66f219bSJens Wiklander 				TEE_Free(op);
359b66f219bSJens Wiklander 			}
360b66f219bSJens Wiklander 		}
361b66f219bSJens Wiklander 	}
362b66f219bSJens Wiklander 
363b0104773SPascal Brand 	return res;
364b0104773SPascal Brand }
365b0104773SPascal Brand 
366b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation)
367b0104773SPascal Brand {
368e889e80bSCedric Chaumont 	TEE_Result res;
369e889e80bSCedric Chaumont 
370e889e80bSCedric Chaumont 	if (operation == TEE_HANDLE_NULL)
371e889e80bSCedric Chaumont 		TEE_Panic(0);
372e889e80bSCedric Chaumont 
373b0104773SPascal Brand 	/*
374b0104773SPascal Brand 	 * Note that keys should not be freed here, since they are
375b0104773SPascal Brand 	 * claimed by the operation they will be freed by
376b0104773SPascal Brand 	 * utee_cryp_state_free().
377b0104773SPascal Brand 	 */
378*2c028fdeSJerome Forissier 	res = _utee_cryp_state_free(operation->state);
379e889e80bSCedric Chaumont 	if (res != TEE_SUCCESS)
380b36311adSJerome Forissier 		TEE_Panic(res);
381e889e80bSCedric Chaumont 
382b0104773SPascal Brand 	TEE_Free(operation->buffer);
383b0104773SPascal Brand 	TEE_Free(operation);
384b0104773SPascal Brand }
385b0104773SPascal Brand 
386b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation,
387b0104773SPascal Brand 			  TEE_OperationInfo *operationInfo)
388b0104773SPascal Brand {
389b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
390b0104773SPascal Brand 		TEE_Panic(0);
391b0104773SPascal Brand 
39205304565SCedric Chaumont 	if (!operationInfo)
393b0104773SPascal Brand 		TEE_Panic(0);
394b0104773SPascal Brand 
395b0104773SPascal Brand 	*operationInfo = operation->info;
396b0104773SPascal Brand }
397b0104773SPascal Brand 
39805304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation,
39905304565SCedric Chaumont 			  TEE_OperationInfoMultiple *operationInfoMultiple,
40005304565SCedric Chaumont 			  uint32_t *operationSize)
40105304565SCedric Chaumont {
40205304565SCedric Chaumont 	TEE_Result res = TEE_SUCCESS;
40305304565SCedric Chaumont 	TEE_ObjectInfo key_info1;
40405304565SCedric Chaumont 	TEE_ObjectInfo key_info2;
40505304565SCedric Chaumont 	uint32_t num_of_keys;
40605304565SCedric Chaumont 	size_t n;
40705304565SCedric Chaumont 
40805304565SCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
40905304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
41005304565SCedric Chaumont 		goto out;
41105304565SCedric Chaumont 	}
41205304565SCedric Chaumont 
41305304565SCedric Chaumont 	if (!operationInfoMultiple) {
41405304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
41505304565SCedric Chaumont 		goto out;
41605304565SCedric Chaumont 	}
41705304565SCedric Chaumont 
41805304565SCedric Chaumont 	if (!operationSize) {
41905304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
42005304565SCedric Chaumont 		goto out;
42105304565SCedric Chaumont 	}
42205304565SCedric Chaumont 
42305304565SCedric Chaumont 	num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/
42405304565SCedric Chaumont 			sizeof(TEE_OperationInfoKey);
42505304565SCedric Chaumont 
42605304565SCedric Chaumont 	if (num_of_keys > 2) {
42705304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
42805304565SCedric Chaumont 		goto out;
42905304565SCedric Chaumont 	}
43005304565SCedric Chaumont 
43105304565SCedric Chaumont 	/* Two keys flag (TEE_ALG_AES_XTS only) */
43205304565SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
43305304565SCedric Chaumont 	    0 &&
43405304565SCedric Chaumont 	    (num_of_keys != 2)) {
43505304565SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
43605304565SCedric Chaumont 		goto out;
43705304565SCedric Chaumont 	}
43805304565SCedric Chaumont 
43905304565SCedric Chaumont 	/* Clear */
44005304565SCedric Chaumont 	for (n = 0; n < num_of_keys; n++) {
44105304565SCedric Chaumont 		operationInfoMultiple->keyInformation[n].keySize = 0;
44205304565SCedric Chaumont 		operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0;
44305304565SCedric Chaumont 	}
44405304565SCedric Chaumont 
44505304565SCedric Chaumont 	if (num_of_keys == 2) {
44605304565SCedric Chaumont 		res = TEE_GetObjectInfo1(operation->key2, &key_info2);
44705304565SCedric Chaumont 		/* Key2 is not a valid handle */
44805304565SCedric Chaumont 		if (res != TEE_SUCCESS)
44905304565SCedric Chaumont 			goto out;
45005304565SCedric Chaumont 
45105304565SCedric Chaumont 		operationInfoMultiple->keyInformation[1].keySize =
45205304565SCedric Chaumont 			key_info2.keySize;
45305304565SCedric Chaumont 		operationInfoMultiple->keyInformation[1].requiredKeyUsage =
45405304565SCedric Chaumont 			operation->info.requiredKeyUsage;
45505304565SCedric Chaumont 	}
45605304565SCedric Chaumont 
45705304565SCedric Chaumont 	if (num_of_keys >= 1) {
45805304565SCedric Chaumont 		res = TEE_GetObjectInfo1(operation->key1, &key_info1);
45905304565SCedric Chaumont 		/* Key1 is not a valid handle */
46005304565SCedric Chaumont 		if (res != TEE_SUCCESS) {
46105304565SCedric Chaumont 			if (num_of_keys == 2) {
46205304565SCedric Chaumont 				operationInfoMultiple->keyInformation[1].
46305304565SCedric Chaumont 							keySize = 0;
46405304565SCedric Chaumont 				operationInfoMultiple->keyInformation[1].
46505304565SCedric Chaumont 							requiredKeyUsage = 0;
46605304565SCedric Chaumont 			}
46705304565SCedric Chaumont 			goto out;
46805304565SCedric Chaumont 		}
46905304565SCedric Chaumont 
47005304565SCedric Chaumont 		operationInfoMultiple->keyInformation[0].keySize =
47105304565SCedric Chaumont 			key_info1.keySize;
47205304565SCedric Chaumont 		operationInfoMultiple->keyInformation[0].requiredKeyUsage =
47305304565SCedric Chaumont 			operation->info.requiredKeyUsage;
47405304565SCedric Chaumont 	}
47505304565SCedric Chaumont 
47605304565SCedric Chaumont 	/* No key */
47705304565SCedric Chaumont 	operationInfoMultiple->algorithm = operation->info.algorithm;
47805304565SCedric Chaumont 	operationInfoMultiple->operationClass = operation->info.operationClass;
47905304565SCedric Chaumont 	operationInfoMultiple->mode = operation->info.mode;
48005304565SCedric Chaumont 	operationInfoMultiple->digestLength = operation->info.digestLength;
48105304565SCedric Chaumont 	operationInfoMultiple->maxKeySize = operation->info.maxKeySize;
48205304565SCedric Chaumont 	operationInfoMultiple->handleState = operation->info.handleState;
48305304565SCedric Chaumont 	operationInfoMultiple->operationState = operation->operationState;
48405304565SCedric Chaumont 	operationInfoMultiple->numberOfKeys = num_of_keys;
48505304565SCedric Chaumont 
48605304565SCedric Chaumont out:
48705304565SCedric Chaumont 	if (res != TEE_SUCCESS &&
48805304565SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
489b36311adSJerome Forissier 		TEE_Panic(res);
49005304565SCedric Chaumont 
49105304565SCedric Chaumont 	return res;
49205304565SCedric Chaumont }
49305304565SCedric Chaumont 
494b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation)
495b0104773SPascal Brand {
496b0104773SPascal Brand 	TEE_Result res;
497b0104773SPascal Brand 
498b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
499b0104773SPascal Brand 		TEE_Panic(0);
500bf80076aSCedric Chaumont 
501642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET))
502bf80076aSCedric Chaumont 			TEE_Panic(0);
503bf80076aSCedric Chaumont 
504642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
505642a1607SCedric Chaumont 
506b0104773SPascal Brand 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
507*2c028fdeSJerome Forissier 		res = _utee_hash_init(operation->state, NULL, 0);
508b0104773SPascal Brand 		if (res != TEE_SUCCESS)
509b0104773SPascal Brand 			TEE_Panic(res);
51005304565SCedric Chaumont 		operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
51105304565SCedric Chaumont 	} else {
512b0104773SPascal Brand 		operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
513b0104773SPascal Brand 	}
51405304565SCedric Chaumont }
515b0104773SPascal Brand 
516b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
517b0104773SPascal Brand 			       TEE_ObjectHandle key)
518b0104773SPascal Brand {
5197583c59eSCedric Chaumont 	TEE_Result res;
520b0104773SPascal Brand 	uint32_t key_size = 0;
521b0104773SPascal Brand 	TEE_ObjectInfo key_info;
522b0104773SPascal Brand 
523a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
524a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
525a57c1e2eSCedric Chaumont 		goto out;
526a57c1e2eSCedric Chaumont 	}
527a57c1e2eSCedric Chaumont 
528642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
529642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
530642a1607SCedric Chaumont 		goto out;
531642a1607SCedric Chaumont 	}
532642a1607SCedric Chaumont 
533a57c1e2eSCedric Chaumont 	if (key == TEE_HANDLE_NULL) {
534a57c1e2eSCedric Chaumont 		/* Operation key cleared */
535a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key1);
536a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
537a57c1e2eSCedric Chaumont 		goto out;
538a57c1e2eSCedric Chaumont 	}
539a57c1e2eSCedric Chaumont 
540a57c1e2eSCedric Chaumont 	/* No key for digest operation */
541a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
542a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
543a57c1e2eSCedric Chaumont 		goto out;
544a57c1e2eSCedric Chaumont 	}
545a57c1e2eSCedric Chaumont 
546a57c1e2eSCedric Chaumont 	/* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */
547a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
548a57c1e2eSCedric Chaumont 	    0) {
549a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
550a57c1e2eSCedric Chaumont 		goto out;
551a57c1e2eSCedric Chaumont 	}
552a57c1e2eSCedric Chaumont 
5537583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key, &key_info);
554a57c1e2eSCedric Chaumont 	/* Key is not a valid handle */
5557583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
556a57c1e2eSCedric Chaumont 		goto out;
5577583c59eSCedric Chaumont 
558b0104773SPascal Brand 	/* Supplied key has to meet required usage */
559b0104773SPascal Brand 	if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
560b0104773SPascal Brand 	    operation->info.requiredKeyUsage) {
561a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
562a57c1e2eSCedric Chaumont 		goto out;
563b0104773SPascal Brand 	}
564b0104773SPascal Brand 
565a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info.keySize) {
566a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
567a57c1e2eSCedric Chaumont 		goto out;
568a57c1e2eSCedric Chaumont 	}
569b0104773SPascal Brand 
5707583c59eSCedric Chaumont 	key_size = key_info.keySize;
571b0104773SPascal Brand 
572b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
573b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
574b0104773SPascal Brand 
5757583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key);
5767583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
577a57c1e2eSCedric Chaumont 		goto out;
5787583c59eSCedric Chaumont 
579b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
580b0104773SPascal Brand 
581b0104773SPascal Brand 	operation->info.keySize = key_size;
582b0104773SPascal Brand 
5837583c59eSCedric Chaumont out:
584a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
585a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
586a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
587b36311adSJerome Forissier 		TEE_Panic(res);
588a57c1e2eSCedric Chaumont 
589a57c1e2eSCedric Chaumont 	return res;
590b0104773SPascal Brand }
591b0104773SPascal Brand 
592b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
593b0104773SPascal Brand 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
594b0104773SPascal Brand {
5957583c59eSCedric Chaumont 	TEE_Result res;
596b0104773SPascal Brand 	uint32_t key_size = 0;
597b0104773SPascal Brand 	TEE_ObjectInfo key_info1;
598b0104773SPascal Brand 	TEE_ObjectInfo key_info2;
599b0104773SPascal Brand 
600a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
601a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
602a57c1e2eSCedric Chaumont 		goto out;
603a57c1e2eSCedric Chaumont 	}
604a57c1e2eSCedric Chaumont 
605642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
606642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
607642a1607SCedric Chaumont 		goto out;
608642a1607SCedric Chaumont 	}
609642a1607SCedric Chaumont 
610a57c1e2eSCedric Chaumont 	/*
611a57c1e2eSCedric Chaumont 	 * Key1/Key2 and/or are not initialized and
612a57c1e2eSCedric Chaumont 	 * Either both keys are NULL or both are not NULL
613a57c1e2eSCedric Chaumont 	 */
614a57c1e2eSCedric Chaumont 	if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) {
615a57c1e2eSCedric Chaumont 		/* Clear operation key1 (if needed) */
616a57c1e2eSCedric Chaumont 		if (key1 == TEE_HANDLE_NULL)
617a57c1e2eSCedric Chaumont 			TEE_ResetTransientObject(operation->key1);
618a57c1e2eSCedric Chaumont 		/* Clear operation key2 (if needed) */
619a57c1e2eSCedric Chaumont 		if (key2 == TEE_HANDLE_NULL)
620a57c1e2eSCedric Chaumont 			TEE_ResetTransientObject(operation->key2);
621a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
622a57c1e2eSCedric Chaumont 		goto out;
623a57c1e2eSCedric Chaumont 	}
624a57c1e2eSCedric Chaumont 
625a57c1e2eSCedric Chaumont 	/* No key for digest operation */
626a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
627a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
628a57c1e2eSCedric Chaumont 		goto out;
629a57c1e2eSCedric Chaumont 	}
630a57c1e2eSCedric Chaumont 
6315b385b3fSJerome Forissier 	/* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */
632a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
633a57c1e2eSCedric Chaumont 	    0) {
634a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
635a57c1e2eSCedric Chaumont 		goto out;
636a57c1e2eSCedric Chaumont 	}
637a57c1e2eSCedric Chaumont 
6387583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key1, &key_info1);
639a57c1e2eSCedric Chaumont 	/* Key1 is not a valid handle */
6407583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
641a57c1e2eSCedric Chaumont 		goto out;
6427583c59eSCedric Chaumont 
643b0104773SPascal Brand 	/* Supplied key has to meet required usage */
644b0104773SPascal Brand 	if ((key_info1.objectUsage & operation->info.
645b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
646a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
647a57c1e2eSCedric Chaumont 		goto out;
648b0104773SPascal Brand 	}
649b0104773SPascal Brand 
6507583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key2, &key_info2);
651a57c1e2eSCedric Chaumont 	/* Key2 is not a valid handle */
6527583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
6537583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
6547583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
655a57c1e2eSCedric Chaumont 		goto out;
6567583c59eSCedric Chaumont 	}
6577583c59eSCedric Chaumont 
658b0104773SPascal Brand 	/* Supplied key has to meet required usage */
659b0104773SPascal Brand 	if ((key_info2.objectUsage & operation->info.
660b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
661a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
662a57c1e2eSCedric Chaumont 		goto out;
663b0104773SPascal Brand 	}
664b0104773SPascal Brand 
665b0104773SPascal Brand 	/*
6665b385b3fSJerome Forissier 	 * All the multi key algorithm currently supported requires the keys to
6675b385b3fSJerome Forissier 	 * be of equal size.
668b0104773SPascal Brand 	 */
6695b385b3fSJerome Forissier 	if (key_info1.keySize != key_info2.keySize) {
670a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
671a57c1e2eSCedric Chaumont 		goto out;
672b0104773SPascal Brand 
673a57c1e2eSCedric Chaumont 	}
674a57c1e2eSCedric Chaumont 
675a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info1.keySize) {
676a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
677a57c1e2eSCedric Chaumont 		goto out;
678a57c1e2eSCedric Chaumont 	}
679b0104773SPascal Brand 
680b0104773SPascal Brand 	/*
681b0104773SPascal Brand 	 * Odd that only the size of one key should be reported while
682b0104773SPascal Brand 	 * size of two key are used when allocating the operation.
683b0104773SPascal Brand 	 */
6847583c59eSCedric Chaumont 	key_size = key_info1.keySize;
685b0104773SPascal Brand 
686b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
687b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key2);
688b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
689b0104773SPascal Brand 
6907583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key1);
6917583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
692a57c1e2eSCedric Chaumont 		goto out;
6937583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key2, key2);
6947583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
6957583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
6967583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
697a57c1e2eSCedric Chaumont 		goto out;
6987583c59eSCedric Chaumont 	}
6997583c59eSCedric Chaumont 
700b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
701b0104773SPascal Brand 
702b0104773SPascal Brand 	operation->info.keySize = key_size;
703b0104773SPascal Brand 
7047583c59eSCedric Chaumont out:
705a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
706a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
707a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT_2 &&
708a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE &&
709a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
710b36311adSJerome Forissier 		TEE_Panic(res);
711a57c1e2eSCedric Chaumont 
712a57c1e2eSCedric Chaumont 	return res;
713b0104773SPascal Brand }
714b0104773SPascal Brand 
715b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
716b0104773SPascal Brand {
717b0104773SPascal Brand 	TEE_Result res;
718b0104773SPascal Brand 
719b0104773SPascal Brand 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
720b0104773SPascal Brand 		TEE_Panic(0);
721b0104773SPascal Brand 	if (dst_op->info.algorithm != src_op->info.algorithm)
722b0104773SPascal Brand 		TEE_Panic(0);
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 
757*2c028fdeSJerome 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 	 */
773*2c028fdeSJerome 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 
791*2c028fdeSJerome 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 	    !hash ||
80587c2f6b6SCedric Chaumont 	    !hashLen ||
80687c2f6b6SCedric Chaumont 	    (operation->info.operationClass != TEE_OPERATION_DIGEST)) {
80787c2f6b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
80887c2f6b6SCedric Chaumont 		goto out;
80987c2f6b6SCedric Chaumont 	}
81087c2f6b6SCedric Chaumont 
811e86f1266SJens Wiklander 	hl = *hashLen;
812*2c028fdeSJerome Forissier 	res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl);
813e86f1266SJens Wiklander 	*hashLen = hl;
8146d15db08SJerome Forissier 	if (res != TEE_SUCCESS)
8156d15db08SJerome Forissier 		goto out;
8166d15db08SJerome Forissier 
8176d15db08SJerome Forissier 	/* Reset operation state */
8186d15db08SJerome Forissier 	init_hash_operation(operation, NULL, 0);
81987c2f6b6SCedric Chaumont 
820642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
821642a1607SCedric Chaumont 
82287c2f6b6SCedric Chaumont out:
82387c2f6b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
82487c2f6b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
825b36311adSJerome Forissier 		TEE_Panic(res);
82673d6c3baSJoakim Bech 
82787c2f6b6SCedric Chaumont 	return res;
828b0104773SPascal Brand }
829b0104773SPascal Brand 
830b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */
831b0104773SPascal Brand 
8328f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV,
8338f07fe6fSJerome Forissier 		    uint32_t IVLen)
834b0104773SPascal Brand {
835b0104773SPascal Brand 	TEE_Result res;
836b0104773SPascal Brand 
837b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
838b0104773SPascal Brand 		TEE_Panic(0);
839642a1607SCedric Chaumont 
840b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
841b0104773SPascal Brand 		TEE_Panic(0);
842642a1607SCedric Chaumont 
843642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
844642a1607SCedric Chaumont 	    !(operation->key1))
845642a1607SCedric Chaumont 		TEE_Panic(0);
846642a1607SCedric Chaumont 
847642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
848642a1607SCedric Chaumont 		TEE_ResetOperation(operation);
849642a1607SCedric Chaumont 
850642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
851642a1607SCedric Chaumont 
852*2c028fdeSJerome Forissier 	res = _utee_cipher_init(operation->state, IV, IVLen);
853b0104773SPascal Brand 	if (res != TEE_SUCCESS)
854b0104773SPascal Brand 		TEE_Panic(res);
855642a1607SCedric Chaumont 
856b0104773SPascal Brand 	operation->buffer_offs = 0;
857b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
858b0104773SPascal Brand }
859b0104773SPascal Brand 
860b0104773SPascal Brand static TEE_Result tee_buffer_update(
861b0104773SPascal Brand 		TEE_OperationHandle op,
862e86f1266SJens Wiklander 		TEE_Result(*update_func)(unsigned long state, const void *src,
863e86f1266SJens Wiklander 				size_t slen, void *dst, uint64_t *dlen),
864b0104773SPascal Brand 		const void *src_data, size_t src_len,
865e86f1266SJens Wiklander 		void *dest_data, uint64_t *dest_len)
866b0104773SPascal Brand {
867b0104773SPascal Brand 	TEE_Result res;
868b0104773SPascal Brand 	const uint8_t *src = src_data;
869b0104773SPascal Brand 	size_t slen = src_len;
870b0104773SPascal Brand 	uint8_t *dst = dest_data;
871b0104773SPascal Brand 	size_t dlen = *dest_len;
872b0104773SPascal Brand 	size_t acc_dlen = 0;
873e86f1266SJens Wiklander 	uint64_t tmp_dlen;
874b0104773SPascal Brand 	size_t l;
875b0104773SPascal Brand 	size_t buffer_size;
876d3588802SPascal Brand 	size_t buffer_left;
877b0104773SPascal Brand 
878090268f5SJens Wiklander 	if (!src) {
879090268f5SJens Wiklander 		if (slen)
880090268f5SJens Wiklander 			TEE_Panic(0);
881090268f5SJens Wiklander 		goto out;
882090268f5SJens Wiklander 	}
883090268f5SJens Wiklander 
884d3588802SPascal Brand 	if (op->buffer_two_blocks) {
885b0104773SPascal Brand 		buffer_size = op->block_size * 2;
886d3588802SPascal Brand 		buffer_left = 1;
887d3588802SPascal Brand 	} else {
888b0104773SPascal Brand 		buffer_size = op->block_size;
889d3588802SPascal Brand 		buffer_left = 0;
890d3588802SPascal Brand 	}
891b0104773SPascal Brand 
892b0104773SPascal Brand 	if (op->buffer_offs > 0) {
893b0104773SPascal Brand 		/* Fill up complete block */
894b0104773SPascal Brand 		if (op->buffer_offs < op->block_size)
895b0104773SPascal Brand 			l = MIN(slen, op->block_size - op->buffer_offs);
896b0104773SPascal Brand 		else
897b0104773SPascal Brand 			l = MIN(slen, buffer_size - op->buffer_offs);
898b0104773SPascal Brand 		memcpy(op->buffer + op->buffer_offs, src, l);
899b0104773SPascal Brand 		op->buffer_offs += l;
900b0104773SPascal Brand 		src += l;
901b0104773SPascal Brand 		slen -= l;
902b0104773SPascal Brand 		if ((op->buffer_offs % op->block_size) != 0)
903b0104773SPascal Brand 			goto out;	/* Nothing left to do */
904b0104773SPascal Brand 	}
905b0104773SPascal Brand 
906b0104773SPascal Brand 	/* If we can feed from buffer */
907d3588802SPascal Brand 	if ((op->buffer_offs > 0) &&
908d3588802SPascal Brand 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
9092ff3fdbbSPascal Brand 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
910b0104773SPascal Brand 				op->block_size);
911b0104773SPascal Brand 		l = MIN(op->buffer_offs, l);
912b0104773SPascal Brand 		tmp_dlen = dlen;
913b0104773SPascal Brand 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
914b0104773SPascal Brand 		if (res != TEE_SUCCESS)
915b0104773SPascal Brand 			TEE_Panic(res);
916b0104773SPascal Brand 		dst += tmp_dlen;
917b0104773SPascal Brand 		dlen -= tmp_dlen;
918b0104773SPascal Brand 		acc_dlen += tmp_dlen;
919b0104773SPascal Brand 		op->buffer_offs -= l;
920b0104773SPascal Brand 		if (op->buffer_offs > 0) {
921b0104773SPascal Brand 			/*
922b0104773SPascal Brand 			 * Slen is small enough to be contained in rest buffer.
923b0104773SPascal Brand 			 */
924b0104773SPascal Brand 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
925b0104773SPascal Brand 			memcpy(op->buffer + op->buffer_offs, src, slen);
926b0104773SPascal Brand 			op->buffer_offs += slen;
927b0104773SPascal Brand 			goto out;	/* Nothing left to do */
928b0104773SPascal Brand 		}
929b0104773SPascal Brand 	}
930b0104773SPascal Brand 
931d3588802SPascal Brand 	if (slen >= (buffer_size + buffer_left)) {
932b0104773SPascal Brand 		/* Buffer is empty, feed as much as possible from src */
933bf7a587fSJerome Forissier 		if (op->info.algorithm == TEE_ALG_AES_CTS)
934b1ecda78SJerome Forissier 			l = ROUNDUP(slen - buffer_size, op->block_size);
935bf7a587fSJerome Forissier 		else
936bf7a587fSJerome Forissier 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
937b0104773SPascal Brand 
938b0104773SPascal Brand 		tmp_dlen = dlen;
939b0104773SPascal Brand 		res = update_func(op->state, src, l, dst, &tmp_dlen);
940b0104773SPascal Brand 		if (res != TEE_SUCCESS)
941b0104773SPascal Brand 			TEE_Panic(res);
942b0104773SPascal Brand 		src += l;
943b0104773SPascal Brand 		slen -= l;
944b0104773SPascal Brand 		dst += tmp_dlen;
945b0104773SPascal Brand 		dlen -= tmp_dlen;
946b0104773SPascal Brand 		acc_dlen += tmp_dlen;
947b0104773SPascal Brand 	}
948b0104773SPascal Brand 
949b0104773SPascal Brand 	/* Slen is small enough to be contained in buffer. */
950b0104773SPascal Brand 	memcpy(op->buffer + op->buffer_offs, src, slen);
951b0104773SPascal Brand 	op->buffer_offs += slen;
952b0104773SPascal Brand 
953b0104773SPascal Brand out:
954b0104773SPascal Brand 	*dest_len = acc_dlen;
955b0104773SPascal Brand 	return TEE_SUCCESS;
956b0104773SPascal Brand }
957b0104773SPascal Brand 
9588f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData,
95979a3c601SCedric Chaumont 			    uint32_t srcLen, void *destData, uint32_t *destLen)
960b0104773SPascal Brand {
961dea1f2b6SCedric Chaumont 	TEE_Result res;
962b0104773SPascal Brand 	size_t req_dlen;
963e86f1266SJens Wiklander 	uint64_t dl;
964b0104773SPascal Brand 
965642a1607SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
966dea1f2b6SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
967dea1f2b6SCedric Chaumont 	    destLen == NULL ||
968dea1f2b6SCedric Chaumont 	    (destData == NULL && *destLen != 0)) {
969dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
970dea1f2b6SCedric Chaumont 		goto out;
971dea1f2b6SCedric Chaumont 	}
972dea1f2b6SCedric Chaumont 
973642a1607SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
974dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
975dea1f2b6SCedric Chaumont 		goto out;
976dea1f2b6SCedric Chaumont 	}
977dea1f2b6SCedric Chaumont 
978642a1607SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
979642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
980642a1607SCedric Chaumont 		goto out;
981642a1607SCedric Chaumont 	}
982642a1607SCedric Chaumont 
983642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
984dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
985dea1f2b6SCedric Chaumont 		goto out;
986dea1f2b6SCedric Chaumont 	}
987b0104773SPascal Brand 
988e32c5ddfSJerome Forissier 	if (!srcData && !srcLen) {
989090268f5SJens Wiklander 		*destLen = 0;
990e32c5ddfSJerome Forissier 		res = TEE_SUCCESS;
991e32c5ddfSJerome Forissier 		goto out;
992e32c5ddfSJerome Forissier 	}
993e32c5ddfSJerome Forissier 
994b0104773SPascal Brand 	/* Calculate required dlen */
99557aabac5SBogdan Liulko 	if (operation->block_size > 1) {
99657aabac5SBogdan Liulko 		req_dlen = ((operation->buffer_offs + srcLen) /
99757aabac5SBogdan Liulko 			    operation->block_size) * operation->block_size;
99857aabac5SBogdan Liulko 	} else {
99957aabac5SBogdan Liulko 		req_dlen = srcLen;
100057aabac5SBogdan Liulko 	}
1001642a1607SCedric Chaumont 	if (operation->buffer_two_blocks) {
1002642a1607SCedric Chaumont 		if (req_dlen > operation->block_size * 2)
1003642a1607SCedric Chaumont 			req_dlen -= operation->block_size * 2;
1004b0104773SPascal Brand 		else
1005b0104773SPascal Brand 			req_dlen = 0;
1006b0104773SPascal Brand 	}
1007b0104773SPascal Brand 	/*
1008b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1009b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1010b0104773SPascal Brand 	 * can't restore sync with this API.
1011b0104773SPascal Brand 	 */
1012b0104773SPascal Brand 	if (*destLen < req_dlen) {
1013b0104773SPascal Brand 		*destLen = req_dlen;
1014dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1015dea1f2b6SCedric Chaumont 		goto out;
1016b0104773SPascal Brand 	}
1017b0104773SPascal Brand 
1018e86f1266SJens Wiklander 	dl = *destLen;
101957aabac5SBogdan Liulko 	if (operation->block_size > 1) {
1020*2c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_cipher_update, srcData,
102157aabac5SBogdan Liulko 					srcLen, destData, &dl);
102257aabac5SBogdan Liulko 	} else {
102357aabac5SBogdan Liulko 		if (srcLen > 0) {
1024*2c028fdeSJerome Forissier 			res = _utee_cipher_update(operation->state, srcData,
102557aabac5SBogdan Liulko 						  srcLen, destData, &dl);
102657aabac5SBogdan Liulko 		} else {
102757aabac5SBogdan Liulko 			res = TEE_SUCCESS;
102857aabac5SBogdan Liulko 			dl = 0;
102957aabac5SBogdan Liulko 		}
103057aabac5SBogdan Liulko 	}
1031e86f1266SJens Wiklander 	*destLen = dl;
1032b0104773SPascal Brand 
1033dea1f2b6SCedric Chaumont out:
1034dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1035dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1036b36311adSJerome Forissier 		TEE_Panic(res);
1037dea1f2b6SCedric Chaumont 
1038dea1f2b6SCedric Chaumont 	return res;
1039b0104773SPascal Brand }
1040b0104773SPascal Brand 
1041642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation,
10428f07fe6fSJerome Forissier 			     const void *srcData, uint32_t srcLen,
10438f07fe6fSJerome Forissier 			     void *destData, uint32_t *destLen)
1044b0104773SPascal Brand {
1045b0104773SPascal Brand 	TEE_Result res;
1046b0104773SPascal Brand 	uint8_t *dst = destData;
1047b0104773SPascal Brand 	size_t acc_dlen = 0;
1048e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1049b0104773SPascal Brand 	size_t req_dlen;
1050b0104773SPascal Brand 
1051642a1607SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1052dea1f2b6SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
1053dea1f2b6SCedric Chaumont 	    destLen == NULL ||
1054dea1f2b6SCedric Chaumont 	    (destData == NULL && *destLen != 0)) {
1055dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1056dea1f2b6SCedric Chaumont 		goto out;
1057dea1f2b6SCedric Chaumont 	}
1058dea1f2b6SCedric Chaumont 
1059642a1607SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1060dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1061dea1f2b6SCedric Chaumont 		goto out;
1062dea1f2b6SCedric Chaumont 	}
1063dea1f2b6SCedric Chaumont 
1064642a1607SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1065642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1066642a1607SCedric Chaumont 		goto out;
1067642a1607SCedric Chaumont 	}
1068642a1607SCedric Chaumont 
1069642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1070dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1071dea1f2b6SCedric Chaumont 		goto out;
1072dea1f2b6SCedric Chaumont 	}
1073b0104773SPascal Brand 
1074b0104773SPascal Brand 	/*
1075b0104773SPascal Brand 	 * Check that the final block doesn't require padding for those
1076b0104773SPascal Brand 	 * algorithms that requires client to supply padding.
1077b0104773SPascal Brand 	 */
1078642a1607SCedric Chaumont 	if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
1079642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1080642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
1081642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1082642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
1083ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD ||
1084ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD ||
1085ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) {
1086642a1607SCedric Chaumont 		if (((operation->buffer_offs + srcLen) % operation->block_size)
1087642a1607SCedric Chaumont 		    != 0) {
1088dea1f2b6SCedric Chaumont 			res = TEE_ERROR_BAD_PARAMETERS;
1089dea1f2b6SCedric Chaumont 			goto out;
1090dea1f2b6SCedric Chaumont 		}
1091b0104773SPascal Brand 	}
1092b0104773SPascal Brand 
1093b0104773SPascal Brand 	/*
1094b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1095b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1096b0104773SPascal Brand 	 * can't restore sync with this API.
1097b0104773SPascal Brand 	 */
109857aabac5SBogdan Liulko 	if (operation->block_size > 1) {
1099642a1607SCedric Chaumont 		req_dlen = operation->buffer_offs + srcLen;
110057aabac5SBogdan Liulko 	} else {
110157aabac5SBogdan Liulko 		req_dlen = srcLen;
110257aabac5SBogdan Liulko 	}
1103b0104773SPascal Brand 	if (*destLen < req_dlen) {
1104b0104773SPascal Brand 		*destLen = req_dlen;
1105dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1106dea1f2b6SCedric Chaumont 		goto out;
1107b0104773SPascal Brand 	}
1108b0104773SPascal Brand 
1109b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
111057aabac5SBogdan Liulko 	if (operation->block_size > 1) {
1111*2c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_cipher_update,
111257aabac5SBogdan Liulko 					srcData, srcLen, dst, &tmp_dlen);
1113dea1f2b6SCedric Chaumont 		if (res != TEE_SUCCESS)
1114dea1f2b6SCedric Chaumont 			goto out;
1115dea1f2b6SCedric Chaumont 
1116b0104773SPascal Brand 		dst += tmp_dlen;
1117b0104773SPascal Brand 		acc_dlen += tmp_dlen;
1118b0104773SPascal Brand 
1119b0104773SPascal Brand 		tmp_dlen = *destLen - acc_dlen;
1120*2c028fdeSJerome Forissier 		res = _utee_cipher_final(operation->state, operation->buffer,
1121*2c028fdeSJerome Forissier 					 operation->buffer_offs, dst,
1122*2c028fdeSJerome Forissier 					 &tmp_dlen);
112357aabac5SBogdan Liulko 	} else {
1124*2c028fdeSJerome Forissier 		res = _utee_cipher_final(operation->state, srcData, srcLen, dst,
1125*2c028fdeSJerome Forissier 					 &tmp_dlen);
112657aabac5SBogdan Liulko 	}
1127b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1128dea1f2b6SCedric Chaumont 		goto out;
1129dea1f2b6SCedric Chaumont 
1130b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1131b0104773SPascal Brand 	*destLen = acc_dlen;
1132dea1f2b6SCedric Chaumont 
1133642a1607SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1134642a1607SCedric Chaumont 
1135642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1136642a1607SCedric Chaumont 
1137dea1f2b6SCedric Chaumont out:
1138dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1139dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1140b36311adSJerome Forissier 		TEE_Panic(res);
1141dea1f2b6SCedric Chaumont 
1142dea1f2b6SCedric Chaumont 	return res;
1143b0104773SPascal Brand }
1144b0104773SPascal Brand 
1145b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */
1146b0104773SPascal Brand 
11478f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
1148b0104773SPascal Brand {
1149b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
1150b0104773SPascal Brand 		TEE_Panic(0);
1151642a1607SCedric Chaumont 
1152b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1153b0104773SPascal Brand 		TEE_Panic(0);
1154642a1607SCedric Chaumont 
1155642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1156642a1607SCedric Chaumont 	    !(operation->key1))
1157642a1607SCedric Chaumont 		TEE_Panic(0);
1158642a1607SCedric Chaumont 
1159642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1160642a1607SCedric Chaumont 		TEE_ResetOperation(operation);
1161642a1607SCedric Chaumont 
1162642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1163642a1607SCedric Chaumont 
11646d15db08SJerome Forissier 	init_hash_operation(operation, IV, IVLen);
1165b0104773SPascal Brand }
1166b0104773SPascal Brand 
11678f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk,
116828e0efc6SCedric Chaumont 		   uint32_t chunkSize)
1169b0104773SPascal Brand {
1170b0104773SPascal Brand 	TEE_Result res;
1171b0104773SPascal Brand 
117228e0efc6SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
1173b0104773SPascal Brand 		TEE_Panic(0);
1174642a1607SCedric Chaumont 
117528e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1176b0104773SPascal Brand 		TEE_Panic(0);
1177642a1607SCedric Chaumont 
117828e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1179b0104773SPascal Brand 		TEE_Panic(0);
1180b0104773SPascal Brand 
1181642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE)
1182642a1607SCedric Chaumont 		TEE_Panic(0);
1183642a1607SCedric Chaumont 
1184*2c028fdeSJerome Forissier 	res = _utee_hash_update(operation->state, chunk, chunkSize);
1185b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1186b0104773SPascal Brand 		TEE_Panic(res);
1187b0104773SPascal Brand }
1188b0104773SPascal Brand 
118928e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
11908f07fe6fSJerome Forissier 			       const void *message, uint32_t messageLen,
119179a3c601SCedric Chaumont 			       void *mac, uint32_t *macLen)
1192b0104773SPascal Brand {
1193b0104773SPascal Brand 	TEE_Result res;
1194e86f1266SJens Wiklander 	uint64_t ml;
1195b0104773SPascal Brand 
119628e0efc6SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
119728e0efc6SCedric Chaumont 	    (message == NULL && messageLen != 0) ||
119828e0efc6SCedric Chaumont 	    mac == NULL ||
119928e0efc6SCedric Chaumont 	    macLen == NULL) {
120028e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
120128e0efc6SCedric Chaumont 		goto out;
120228e0efc6SCedric Chaumont 	}
1203b0104773SPascal Brand 
120428e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
120528e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
120628e0efc6SCedric Chaumont 		goto out;
120728e0efc6SCedric Chaumont 	}
120828e0efc6SCedric Chaumont 
120928e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
121028e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
121128e0efc6SCedric Chaumont 		goto out;
121228e0efc6SCedric Chaumont 	}
121328e0efc6SCedric Chaumont 
1214642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1215642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1216642a1607SCedric Chaumont 		goto out;
1217642a1607SCedric Chaumont 	}
1218642a1607SCedric Chaumont 
1219e86f1266SJens Wiklander 	ml = *macLen;
1220*2c028fdeSJerome Forissier 	res = _utee_hash_final(operation->state, message, messageLen, mac, &ml);
1221e86f1266SJens Wiklander 	*macLen = ml;
122228e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS)
122328e0efc6SCedric Chaumont 		goto out;
122428e0efc6SCedric Chaumont 
122528e0efc6SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
122628e0efc6SCedric Chaumont 
1227642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1228642a1607SCedric Chaumont 
122928e0efc6SCedric Chaumont out:
123028e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
123128e0efc6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
123228e0efc6SCedric Chaumont 		TEE_Panic(res);
123328e0efc6SCedric Chaumont 
1234b0104773SPascal Brand 	return res;
1235b0104773SPascal Brand }
1236b0104773SPascal Brand 
1237b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
12388f07fe6fSJerome Forissier 			       const void *message, uint32_t messageLen,
12398f07fe6fSJerome Forissier 			       const void *mac, uint32_t macLen)
1240b0104773SPascal Brand {
1241b0104773SPascal Brand 	TEE_Result res;
1242b0104773SPascal Brand 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
12437f74c64aSPascal Brand 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1244b0104773SPascal Brand 
124528e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
124628e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
124728e0efc6SCedric Chaumont 		goto out;
124828e0efc6SCedric Chaumont 	}
124928e0efc6SCedric Chaumont 
125028e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
125128e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
125228e0efc6SCedric Chaumont 		goto out;
125328e0efc6SCedric Chaumont 	}
125428e0efc6SCedric Chaumont 
1255642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1256642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1257642a1607SCedric Chaumont 		goto out;
1258642a1607SCedric Chaumont 	}
1259642a1607SCedric Chaumont 
1260b0104773SPascal Brand 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1261b0104773SPascal Brand 				  &computed_mac_size);
1262b0104773SPascal Brand 	if (res != TEE_SUCCESS)
126328e0efc6SCedric Chaumont 		goto out;
126428e0efc6SCedric Chaumont 
126528e0efc6SCedric Chaumont 	if (computed_mac_size != macLen) {
126628e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
126728e0efc6SCedric Chaumont 		goto out;
126828e0efc6SCedric Chaumont 	}
126928e0efc6SCedric Chaumont 
127048e10604SJerome Forissier 	if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) {
127128e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
127228e0efc6SCedric Chaumont 		goto out;
127328e0efc6SCedric Chaumont 	}
127428e0efc6SCedric Chaumont 
1275642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1276642a1607SCedric Chaumont 
127728e0efc6SCedric Chaumont out:
127828e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
127928e0efc6SCedric Chaumont 	    res != TEE_ERROR_MAC_INVALID)
128028e0efc6SCedric Chaumont 		TEE_Panic(res);
128128e0efc6SCedric Chaumont 
1282b0104773SPascal Brand 	return res;
1283b0104773SPascal Brand }
1284b0104773SPascal Brand 
1285b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */
1286b0104773SPascal Brand 
12878f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce,
128879a3c601SCedric Chaumont 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1289b0104773SPascal Brand 		      uint32_t payloadLen)
1290b0104773SPascal Brand {
1291b0104773SPascal Brand 	TEE_Result res;
1292b0104773SPascal Brand 
1293b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || nonce == NULL) {
1294b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1295b5816c88SCedric Chaumont 		goto out;
1296b5816c88SCedric Chaumont 	}
1297b5816c88SCedric Chaumont 
1298b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1299b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1300b5816c88SCedric Chaumont 		goto out;
1301b5816c88SCedric Chaumont 	}
1302b0104773SPascal Brand 
1303642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
1304642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1305642a1607SCedric Chaumont 		goto out;
1306642a1607SCedric Chaumont 	}
1307642a1607SCedric Chaumont 
1308b0104773SPascal Brand 	/*
1309b0104773SPascal Brand 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1310b0104773SPascal Brand 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1311b0104773SPascal Brand 	 * according to the same principle so we have to check here instead to
1312b0104773SPascal Brand 	 * be GP compliant.
1313b0104773SPascal Brand 	 */
1314b5816c88SCedric Chaumont 	if (operation->info.algorithm == TEE_ALG_AES_GCM) {
1315b0104773SPascal Brand 		/*
1316b0104773SPascal Brand 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1317b0104773SPascal Brand 		 */
1318b5816c88SCedric Chaumont 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) {
1319b5816c88SCedric Chaumont 			res = TEE_ERROR_NOT_SUPPORTED;
1320b5816c88SCedric Chaumont 			goto out;
1321b5816c88SCedric Chaumont 		}
1322b0104773SPascal Brand 	}
1323b0104773SPascal Brand 
1324*2c028fdeSJerome Forissier 	res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8,
1325*2c028fdeSJerome Forissier 				 AADLen, payloadLen);
1326b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1327b5816c88SCedric Chaumont 		goto out;
1328b5816c88SCedric Chaumont 
13297acaf5adSAlbert Schwarzkopf 	operation->info.digestLength = tagLen / 8;
1330f2674567SSumit Garg 	operation->buffer_offs = 0;
1331b5816c88SCedric Chaumont 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1332b5816c88SCedric Chaumont 
1333b5816c88SCedric Chaumont out:
1334b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1335b5816c88SCedric Chaumont 	    res != TEE_ERROR_NOT_SUPPORTED)
1336b0104773SPascal Brand 			TEE_Panic(res);
1337b5816c88SCedric Chaumont 
1338b0104773SPascal Brand 	return res;
1339b0104773SPascal Brand }
1340b0104773SPascal Brand 
13418f07fe6fSJerome Forissier void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata,
134279a3c601SCedric Chaumont 		     uint32_t AADdataLen)
1343b0104773SPascal Brand {
1344b0104773SPascal Brand 	TEE_Result res;
1345b0104773SPascal Brand 
1346b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1347b5816c88SCedric Chaumont 	    (AADdata == NULL && AADdataLen != 0))
1348b0104773SPascal Brand 		TEE_Panic(0);
1349642a1607SCedric Chaumont 
1350b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE)
1351b0104773SPascal Brand 		TEE_Panic(0);
1352642a1607SCedric Chaumont 
1353b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1354b0104773SPascal Brand 		TEE_Panic(0);
1355b0104773SPascal Brand 
1356*2c028fdeSJerome Forissier 	res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen);
1357642a1607SCedric Chaumont 
1358642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1359642a1607SCedric Chaumont 
1360b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1361b0104773SPascal Brand 		TEE_Panic(res);
1362b0104773SPascal Brand }
1363b0104773SPascal Brand 
13648f07fe6fSJerome Forissier TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData,
136579a3c601SCedric Chaumont 			uint32_t srcLen, void *destData, uint32_t *destLen)
1366b0104773SPascal Brand {
1367b5816c88SCedric Chaumont 	TEE_Result res;
1368b0104773SPascal Brand 	size_t req_dlen;
1369e86f1266SJens Wiklander 	uint64_t dl;
1370b0104773SPascal Brand 
1371b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1372b5816c88SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
1373b5816c88SCedric Chaumont 	    destLen == NULL ||
1374b5816c88SCedric Chaumont 	    (destData == NULL && *destLen != 0)) {
1375b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1376b5816c88SCedric Chaumont 		goto out;
1377b5816c88SCedric Chaumont 	}
1378b5816c88SCedric Chaumont 
1379b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1380b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1381b5816c88SCedric Chaumont 		goto out;
1382b5816c88SCedric Chaumont 	}
1383b5816c88SCedric Chaumont 
1384b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1385b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1386b5816c88SCedric Chaumont 		goto out;
1387b5816c88SCedric Chaumont 	}
1388b0104773SPascal Brand 
1389827308b8SJerome Forissier 	if (!srcData && !srcLen) {
1390090268f5SJens Wiklander 		*destLen = 0;
1391827308b8SJerome Forissier 		res = TEE_SUCCESS;
1392827308b8SJerome Forissier 		goto out;
1393827308b8SJerome Forissier 	}
1394827308b8SJerome Forissier 
1395b0104773SPascal Brand 	/*
1396b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1397b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1398b0104773SPascal Brand 	 * can't restore sync with this API.
1399b0104773SPascal Brand 	 */
1400afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
1401b5816c88SCedric Chaumont 		req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen,
1402b5816c88SCedric Chaumont 				     operation->block_size);
1403afc0c182SBogdan Liulko 	} else {
1404afc0c182SBogdan Liulko 		req_dlen = srcLen;
1405afc0c182SBogdan Liulko 	}
1406afc0c182SBogdan Liulko 
1407b0104773SPascal Brand 	if (*destLen < req_dlen) {
1408b0104773SPascal Brand 		*destLen = req_dlen;
1409b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1410b5816c88SCedric Chaumont 		goto out;
1411b0104773SPascal Brand 	}
1412b0104773SPascal Brand 
1413e86f1266SJens Wiklander 	dl = *destLen;
1414afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
1415*2c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1416afc0c182SBogdan Liulko 					srcData, srcLen, destData, &dl);
1417afc0c182SBogdan Liulko 	} else {
1418afc0c182SBogdan Liulko 		if (srcLen > 0) {
1419*2c028fdeSJerome Forissier 			res = _utee_authenc_update_payload(operation->state,
1420afc0c182SBogdan Liulko 							   srcData, srcLen,
1421afc0c182SBogdan Liulko 							   destData, &dl);
1422afc0c182SBogdan Liulko 		} else {
1423afc0c182SBogdan Liulko 			dl = 0;
1424afc0c182SBogdan Liulko 			res = TEE_SUCCESS;
1425afc0c182SBogdan Liulko 		}
1426afc0c182SBogdan Liulko 	}
1427afc0c182SBogdan Liulko 	if (res != TEE_SUCCESS)
1428afc0c182SBogdan Liulko 		goto out;
1429afc0c182SBogdan Liulko 
1430e86f1266SJens Wiklander 	*destLen = dl;
1431b0104773SPascal Brand 
1432642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1433642a1607SCedric Chaumont 
1434b5816c88SCedric Chaumont out:
1435b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1436b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1437b5816c88SCedric Chaumont 			TEE_Panic(res);
1438b5816c88SCedric Chaumont 
1439b5816c88SCedric Chaumont 	return res;
1440b0104773SPascal Brand }
1441b0104773SPascal Brand 
1442b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation,
14438f07fe6fSJerome Forissier 			      const void *srcData, uint32_t srcLen,
144479a3c601SCedric Chaumont 			      void *destData, uint32_t *destLen, void *tag,
144579a3c601SCedric Chaumont 			      uint32_t *tagLen)
1446b0104773SPascal Brand {
1447b0104773SPascal Brand 	TEE_Result res;
1448b0104773SPascal Brand 	uint8_t *dst = destData;
1449b0104773SPascal Brand 	size_t acc_dlen = 0;
1450e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1451b0104773SPascal Brand 	size_t req_dlen;
1452e86f1266SJens Wiklander 	uint64_t tl;
1453b0104773SPascal Brand 
1454b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1455b5816c88SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
1456b5816c88SCedric Chaumont 	    destLen == NULL ||
1457b5816c88SCedric Chaumont 	    (destData == NULL && *destLen != 0) ||
1458b5816c88SCedric Chaumont 	    tag == NULL || tagLen == NULL) {
1459b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1460b5816c88SCedric Chaumont 		goto out;
1461b5816c88SCedric Chaumont 	}
1462b5816c88SCedric Chaumont 
1463b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1464b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1465b5816c88SCedric Chaumont 		goto out;
1466b5816c88SCedric Chaumont 	}
1467b5816c88SCedric Chaumont 
1468b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1469b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1470b5816c88SCedric Chaumont 		goto out;
1471b5816c88SCedric Chaumont 	}
1472b0104773SPascal Brand 
1473b0104773SPascal Brand 	/*
1474b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1475b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1476b0104773SPascal Brand 	 * can't restore sync with this API.
14772733280aSEtienne Carriere 	 *
14782733280aSEtienne Carriere 	 * Need to check this before update_payload since sync would be lost if
14792733280aSEtienne Carriere 	 * we return short buffer after that.
1480b0104773SPascal Brand 	 */
14812733280aSEtienne Carriere 	res = TEE_ERROR_GENERIC;
14822733280aSEtienne Carriere 
1483b5816c88SCedric Chaumont 	req_dlen = operation->buffer_offs + srcLen;
1484b0104773SPascal Brand 	if (*destLen < req_dlen) {
1485b0104773SPascal Brand 		*destLen = req_dlen;
1486b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1487b0104773SPascal Brand 	}
1488b0104773SPascal Brand 
14897acaf5adSAlbert Schwarzkopf 	if (*tagLen < operation->info.digestLength) {
14907acaf5adSAlbert Schwarzkopf 		*tagLen = operation->info.digestLength;
1491b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1492b0104773SPascal Brand 	}
1493b0104773SPascal Brand 
14942733280aSEtienne Carriere 	if (res == TEE_ERROR_SHORT_BUFFER)
14952733280aSEtienne Carriere 		goto out;
14962733280aSEtienne Carriere 
1497afc0c182SBogdan Liulko 	tl = *tagLen;
1498b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1499afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
1500*2c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1501afc0c182SBogdan Liulko 					srcData, srcLen, dst, &tmp_dlen);
1502b5816c88SCedric Chaumont 		if (res != TEE_SUCCESS)
1503b5816c88SCedric Chaumont 			goto out;
1504b5816c88SCedric Chaumont 
1505b0104773SPascal Brand 		dst += tmp_dlen;
1506b0104773SPascal Brand 		acc_dlen += tmp_dlen;
1507b0104773SPascal Brand 
1508b0104773SPascal Brand 		tmp_dlen = *destLen - acc_dlen;
1509*2c028fdeSJerome Forissier 		res = _utee_authenc_enc_final(operation->state,
1510afc0c182SBogdan Liulko 					      operation->buffer,
1511afc0c182SBogdan Liulko 					      operation->buffer_offs, dst,
1512afc0c182SBogdan Liulko 					      &tmp_dlen, tag, &tl);
1513afc0c182SBogdan Liulko 	} else {
1514*2c028fdeSJerome Forissier 		res = _utee_authenc_enc_final(operation->state, srcData,
1515afc0c182SBogdan Liulko 					      srcLen, dst, &tmp_dlen,
1516e86f1266SJens Wiklander 					      tag, &tl);
1517afc0c182SBogdan Liulko 	}
1518e86f1266SJens Wiklander 	*tagLen = tl;
1519b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1520b5816c88SCedric Chaumont 		goto out;
1521b0104773SPascal Brand 
1522b5816c88SCedric Chaumont 	acc_dlen += tmp_dlen;
1523b0104773SPascal Brand 	*destLen = acc_dlen;
1524642a1607SCedric Chaumont 
1525b5816c88SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1526b5816c88SCedric Chaumont 
1527642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1528642a1607SCedric Chaumont 
1529b5816c88SCedric Chaumont out:
1530b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1531b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1532b5816c88SCedric Chaumont 			TEE_Panic(res);
1533b0104773SPascal Brand 
1534b0104773SPascal Brand 	return res;
1535b0104773SPascal Brand }
1536b0104773SPascal Brand 
1537b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation,
15388f07fe6fSJerome Forissier 			      const void *srcData, uint32_t srcLen,
1539b5816c88SCedric Chaumont 			      void *destData, uint32_t *destLen, void *tag,
154079a3c601SCedric Chaumont 			      uint32_t tagLen)
1541b0104773SPascal Brand {
1542b0104773SPascal Brand 	TEE_Result res;
1543b0104773SPascal Brand 	uint8_t *dst = destData;
1544b0104773SPascal Brand 	size_t acc_dlen = 0;
1545e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1546b0104773SPascal Brand 	size_t req_dlen;
1547b0104773SPascal Brand 
1548b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1549b5816c88SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
1550b5816c88SCedric Chaumont 	    destLen == NULL ||
1551b5816c88SCedric Chaumont 	    (destData == NULL && *destLen != 0) ||
1552b5816c88SCedric Chaumont 	    (tag == NULL && tagLen != 0)) {
1553b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1554b5816c88SCedric Chaumont 		goto out;
1555b5816c88SCedric Chaumont 	}
1556b5816c88SCedric Chaumont 
1557b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1558b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1559b5816c88SCedric Chaumont 		goto out;
1560b5816c88SCedric Chaumont 	}
1561b5816c88SCedric Chaumont 
1562b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1563b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1564b5816c88SCedric Chaumont 		goto out;
1565b5816c88SCedric Chaumont 	}
1566b0104773SPascal Brand 
1567b0104773SPascal Brand 	/*
1568b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1569b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1570b0104773SPascal Brand 	 * can't restore sync with this API.
1571b0104773SPascal Brand 	 */
1572b5816c88SCedric Chaumont 	req_dlen = operation->buffer_offs + srcLen;
1573b0104773SPascal Brand 	if (*destLen < req_dlen) {
1574b0104773SPascal Brand 		*destLen = req_dlen;
1575b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1576b5816c88SCedric Chaumont 		goto out;
1577b0104773SPascal Brand 	}
1578b0104773SPascal Brand 
1579b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1580afc0c182SBogdan Liulko 	if (operation->block_size > 1) {
1581*2c028fdeSJerome Forissier 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1582afc0c182SBogdan Liulko 					srcData, srcLen, dst, &tmp_dlen);
1583b5816c88SCedric Chaumont 		if (res != TEE_SUCCESS)
1584b5816c88SCedric Chaumont 			goto out;
1585b5816c88SCedric Chaumont 
1586b0104773SPascal Brand 		dst += tmp_dlen;
1587b0104773SPascal Brand 		acc_dlen += tmp_dlen;
1588b0104773SPascal Brand 
1589b0104773SPascal Brand 		tmp_dlen = *destLen - acc_dlen;
1590*2c028fdeSJerome Forissier 		res = _utee_authenc_dec_final(operation->state,
1591afc0c182SBogdan Liulko 					      operation->buffer,
1592afc0c182SBogdan Liulko 					      operation->buffer_offs, dst,
1593afc0c182SBogdan Liulko 					      &tmp_dlen, tag, tagLen);
1594afc0c182SBogdan Liulko 	} else {
1595*2c028fdeSJerome Forissier 		res = _utee_authenc_dec_final(operation->state, srcData,
1596afc0c182SBogdan Liulko 					      srcLen, dst, &tmp_dlen,
1597b5816c88SCedric Chaumont 					      tag, tagLen);
1598afc0c182SBogdan Liulko 	}
1599b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1600b5816c88SCedric Chaumont 		goto out;
1601b5816c88SCedric Chaumont 
1602b0104773SPascal Brand 	/* Supplied tagLen should match what we initiated with */
16037acaf5adSAlbert Schwarzkopf 	if (tagLen != operation->info.digestLength)
1604b0104773SPascal Brand 		res = TEE_ERROR_MAC_INVALID;
1605b0104773SPascal Brand 
1606b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1607b0104773SPascal Brand 	*destLen = acc_dlen;
1608642a1607SCedric Chaumont 
1609b5816c88SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1610b5816c88SCedric Chaumont 
1611642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1612642a1607SCedric Chaumont 
1613b5816c88SCedric Chaumont out:
1614b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1615b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER &&
1616b5816c88SCedric Chaumont 	    res != TEE_ERROR_MAC_INVALID)
1617b5816c88SCedric Chaumont 			TEE_Panic(res);
1618b0104773SPascal Brand 
1619b0104773SPascal Brand 	return res;
1620b0104773SPascal Brand }
1621b0104773SPascal Brand 
1622b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */
1623b0104773SPascal Brand 
162412e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation,
16258f07fe6fSJerome Forissier 				 const TEE_Attribute *params,
16268f07fe6fSJerome Forissier 				 uint32_t paramCount, const void *srcData,
162779a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
162879a3c601SCedric Chaumont 				 uint32_t *destLen)
1629b0104773SPascal Brand {
1630b0104773SPascal Brand 	TEE_Result res;
1631e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1632e86f1266SJens Wiklander 	uint64_t dl;
1633b0104773SPascal Brand 
163412e66b6fSCedric Chaumont 	if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1635b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
1636b0104773SPascal Brand 		TEE_Panic(0);
163712e66b6fSCedric Chaumont 	if (params == NULL && paramCount != 0)
1638b0104773SPascal Brand 		TEE_Panic(0);
163912e66b6fSCedric Chaumont 	if (!operation->key1)
1640b0104773SPascal Brand 		TEE_Panic(0);
164112e66b6fSCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
164212e66b6fSCedric Chaumont 		TEE_Panic(0);
164312e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_ENCRYPT)
1644b0104773SPascal Brand 		TEE_Panic(0);
1645b0104773SPascal Brand 
1646e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1647e86f1266SJens Wiklander 	dl = *destLen;
1648*2c028fdeSJerome Forissier 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1649e86f1266SJens Wiklander 				  srcLen, destData, &dl);
1650e86f1266SJens Wiklander 	*destLen = dl;
165112e66b6fSCedric Chaumont 
16528844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
16538844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
16548844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1655b0104773SPascal Brand 		TEE_Panic(res);
165612e66b6fSCedric Chaumont 
1657b0104773SPascal Brand 	return res;
1658b0104773SPascal Brand }
1659b0104773SPascal Brand 
166012e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation,
16618f07fe6fSJerome Forissier 				 const TEE_Attribute *params,
16628f07fe6fSJerome Forissier 				 uint32_t paramCount, const void *srcData,
166379a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
166479a3c601SCedric Chaumont 				 uint32_t *destLen)
1665b0104773SPascal Brand {
1666b0104773SPascal Brand 	TEE_Result res;
1667e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1668e86f1266SJens Wiklander 	uint64_t dl;
1669b0104773SPascal Brand 
167012e66b6fSCedric Chaumont 	if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1671b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
1672b0104773SPascal Brand 		TEE_Panic(0);
167312e66b6fSCedric Chaumont 	if (params == NULL && paramCount != 0)
1674b0104773SPascal Brand 		TEE_Panic(0);
167512e66b6fSCedric Chaumont 	if (!operation->key1)
1676b0104773SPascal Brand 		TEE_Panic(0);
167712e66b6fSCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
167812e66b6fSCedric Chaumont 		TEE_Panic(0);
167912e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_DECRYPT)
1680b0104773SPascal Brand 		TEE_Panic(0);
1681b0104773SPascal Brand 
1682e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1683e86f1266SJens Wiklander 	dl = *destLen;
1684*2c028fdeSJerome Forissier 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1685e86f1266SJens Wiklander 				  srcLen, destData, &dl);
1686e86f1266SJens Wiklander 	*destLen = dl;
168712e66b6fSCedric Chaumont 
16888844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
16898844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
16908844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1691b0104773SPascal Brand 		TEE_Panic(res);
169212e66b6fSCedric Chaumont 
1693b0104773SPascal Brand 	return res;
1694b0104773SPascal Brand }
1695b0104773SPascal Brand 
169612e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation,
16978f07fe6fSJerome Forissier 				    const TEE_Attribute *params,
16988f07fe6fSJerome Forissier 				    uint32_t paramCount, const void *digest,
169979a3c601SCedric Chaumont 				    uint32_t digestLen, void *signature,
170079a3c601SCedric Chaumont 				    uint32_t *signatureLen)
1701b0104773SPascal Brand {
1702b0104773SPascal Brand 	TEE_Result res;
1703e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1704e86f1266SJens Wiklander 	uint64_t sl;
1705b0104773SPascal Brand 
170612e66b6fSCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
170712e66b6fSCedric Chaumont 	    (digest == NULL && digestLen != 0) ||
1708b0104773SPascal Brand 	    signature == NULL || signatureLen == NULL)
1709b0104773SPascal Brand 		TEE_Panic(0);
171012e66b6fSCedric Chaumont 	if (params == NULL && paramCount != 0)
1711b0104773SPascal Brand 		TEE_Panic(0);
171212e66b6fSCedric Chaumont 	if (!operation->key1)
1713b0104773SPascal Brand 		TEE_Panic(0);
171412e66b6fSCedric Chaumont 	if (operation->info.operationClass !=
171512e66b6fSCedric Chaumont 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
171612e66b6fSCedric Chaumont 		TEE_Panic(0);
171712e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_SIGN)
1718b0104773SPascal Brand 		TEE_Panic(0);
1719b0104773SPascal Brand 
1720e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1721e86f1266SJens Wiklander 	sl = *signatureLen;
1722*2c028fdeSJerome Forissier 	res = _utee_asymm_operate(operation->state, ua, paramCount, digest,
1723e86f1266SJens Wiklander 				  digestLen, signature, &sl);
1724e86f1266SJens Wiklander 	*signatureLen = sl;
172512e66b6fSCedric Chaumont 
1726b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1727b0104773SPascal Brand 		TEE_Panic(res);
172812e66b6fSCedric Chaumont 
1729b0104773SPascal Brand 	return res;
1730b0104773SPascal Brand }
1731b0104773SPascal Brand 
173212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,
17338f07fe6fSJerome Forissier 				      const TEE_Attribute *params,
17348f07fe6fSJerome Forissier 				      uint32_t paramCount, const void *digest,
17358f07fe6fSJerome Forissier 				      uint32_t digestLen,
17368f07fe6fSJerome Forissier 				      const void *signature,
173779a3c601SCedric Chaumont 				      uint32_t signatureLen)
1738b0104773SPascal Brand {
1739b0104773SPascal Brand 	TEE_Result res;
1740e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1741b0104773SPascal Brand 
174212e66b6fSCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
174312e66b6fSCedric Chaumont 	    (digest == NULL && digestLen != 0) ||
1744b0104773SPascal Brand 	    (signature == NULL && signatureLen != 0))
1745b0104773SPascal Brand 		TEE_Panic(0);
174612e66b6fSCedric Chaumont 	if (params == NULL && paramCount != 0)
1747b0104773SPascal Brand 		TEE_Panic(0);
174812e66b6fSCedric Chaumont 	if (!operation->key1)
1749b0104773SPascal Brand 		TEE_Panic(0);
175012e66b6fSCedric Chaumont 	if (operation->info.operationClass !=
175112e66b6fSCedric Chaumont 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
175212e66b6fSCedric Chaumont 		TEE_Panic(0);
175312e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_VERIFY)
1754b0104773SPascal Brand 		TEE_Panic(0);
1755b0104773SPascal Brand 
1756e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1757*2c028fdeSJerome Forissier 	res = _utee_asymm_verify(operation->state, ua, paramCount, digest,
175812e66b6fSCedric Chaumont 				 digestLen, signature, signatureLen);
175912e66b6fSCedric Chaumont 
1760b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1761b0104773SPascal Brand 		TEE_Panic(res);
176212e66b6fSCedric Chaumont 
1763b0104773SPascal Brand 	return res;
1764b0104773SPascal Brand }
1765b0104773SPascal Brand 
1766b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */
1767b0104773SPascal Brand 
1768b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation,
1769b0104773SPascal Brand 		   const TEE_Attribute *params, uint32_t paramCount,
1770b0104773SPascal Brand 		   TEE_ObjectHandle derivedKey)
1771b0104773SPascal Brand {
1772b0104773SPascal Brand 	TEE_Result res;
1773b0104773SPascal Brand 	TEE_ObjectInfo key_info;
1774e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1775b0104773SPascal Brand 
1776b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1777b0104773SPascal Brand 		TEE_Panic(0);
177884fa9467SCedric Chaumont 	if (params == NULL && paramCount != 0)
1779b0104773SPascal Brand 		TEE_Panic(0);
17808854d3c6SJerome Forissier 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
17818854d3c6SJerome Forissier 	    TEE_OPERATION_KEY_DERIVATION)
1782b0104773SPascal Brand 		TEE_Panic(0);
1783b0104773SPascal Brand 
1784b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1785b0104773SPascal Brand 		TEE_Panic(0);
178684fa9467SCedric Chaumont 	if (!operation->key1)
178784fa9467SCedric Chaumont 		TEE_Panic(0);
1788b0104773SPascal Brand 	if (operation->info.mode != TEE_MODE_DERIVE)
1789b0104773SPascal Brand 		TEE_Panic(0);
1790b0104773SPascal Brand 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1791b0104773SPascal Brand 		TEE_Panic(0);
1792b0104773SPascal Brand 
1793*2c028fdeSJerome Forissier 	res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info);
1794b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1795b36311adSJerome Forissier 		TEE_Panic(res);
1796b0104773SPascal Brand 
1797b0104773SPascal Brand 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1798b0104773SPascal Brand 		TEE_Panic(0);
1799b0104773SPascal Brand 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1800b0104773SPascal Brand 		TEE_Panic(0);
1801b0104773SPascal Brand 
1802e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1803*2c028fdeSJerome Forissier 	res = _utee_cryp_derive_key(operation->state, ua, paramCount,
1804e86f1266SJens Wiklander 				    (unsigned long)derivedKey);
1805b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1806b0104773SPascal Brand 		TEE_Panic(res);
1807b0104773SPascal Brand }
1808b0104773SPascal Brand 
1809b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */
1810b0104773SPascal Brand 
181179a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1812b0104773SPascal Brand {
1813b0104773SPascal Brand 	TEE_Result res;
1814b0104773SPascal Brand 
1815*2c028fdeSJerome Forissier 	res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1816b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1817b0104773SPascal Brand 		TEE_Panic(res);
1818b0104773SPascal Brand }
1819433c4257SJens Wiklander 
1820433c4257SJens Wiklander int rand(void)
1821433c4257SJens Wiklander {
1822433c4257SJens Wiklander 	int rc;
1823433c4257SJens Wiklander 
1824433c4257SJens Wiklander 	TEE_GenerateRandom(&rc, sizeof(rc));
1825433c4257SJens Wiklander 
1826433c4257SJens Wiklander 	/*
1827433c4257SJens Wiklander 	 * RAND_MAX is the larges int, INT_MAX which is all bits but the
1828433c4257SJens Wiklander 	 * highest bit set.
1829433c4257SJens Wiklander 	 */
1830433c4257SJens Wiklander 	return rc & RAND_MAX;
1831433c4257SJens Wiklander }
183279170ce0SJerome Forissier 
183379170ce0SJerome Forissier TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element)
183479170ce0SJerome Forissier {
183579170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_AES)) {
183679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
183779170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_ECB_NOPAD)
183879170ce0SJerome Forissier 				goto check_element_none;
183979170ce0SJerome Forissier 		}
184079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
184179170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CBC_NOPAD)
184279170ce0SJerome Forissier 				goto check_element_none;
184379170ce0SJerome Forissier 		}
184479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
184579170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CTR)
184679170ce0SJerome Forissier 				goto check_element_none;
184779170ce0SJerome Forissier 		}
184879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CTS)) {
184979170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CTS)
185079170ce0SJerome Forissier 				goto check_element_none;
185179170ce0SJerome Forissier 		}
185279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_XTS)) {
185379170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_XTS)
185479170ce0SJerome Forissier 				goto check_element_none;
185579170ce0SJerome Forissier 		}
185679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
185779170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CBC_MAC_NOPAD ||
185879170ce0SJerome Forissier 			    alg == TEE_ALG_AES_CBC_MAC_PKCS5)
185979170ce0SJerome Forissier 				goto check_element_none;
186079170ce0SJerome Forissier 		}
186179170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CMAC)) {
186279170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CMAC)
186379170ce0SJerome Forissier 				goto check_element_none;
186479170ce0SJerome Forissier 		}
186579170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CCM)) {
186679170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_CCM)
186779170ce0SJerome Forissier 				goto check_element_none;
186879170ce0SJerome Forissier 		}
186979170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_GCM)) {
187079170ce0SJerome Forissier 			if (alg == TEE_ALG_AES_GCM)
187179170ce0SJerome Forissier 				goto check_element_none;
187279170ce0SJerome Forissier 		}
187379170ce0SJerome Forissier 	}
187479170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_DES)) {
187579170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
187679170ce0SJerome Forissier 			if (alg == TEE_ALG_DES_ECB_NOPAD ||
187779170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_ECB_NOPAD)
187879170ce0SJerome Forissier 				goto check_element_none;
187979170ce0SJerome Forissier 		}
188079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
188179170ce0SJerome Forissier 			if (alg == TEE_ALG_DES_CBC_NOPAD ||
188279170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_CBC_NOPAD)
188379170ce0SJerome Forissier 				goto check_element_none;
188479170ce0SJerome Forissier 		}
188579170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
188679170ce0SJerome Forissier 			if (alg == TEE_ALG_DES_CBC_MAC_NOPAD ||
188779170ce0SJerome Forissier 			    alg == TEE_ALG_DES_CBC_MAC_PKCS5 ||
188879170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_CBC_MAC_NOPAD ||
188979170ce0SJerome Forissier 			    alg == TEE_ALG_DES3_CBC_MAC_PKCS5)
189079170ce0SJerome Forissier 				goto check_element_none;
189179170ce0SJerome Forissier 		}
189279170ce0SJerome Forissier 	}
189379170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_MD5)) {
189479170ce0SJerome Forissier 		if (alg == TEE_ALG_MD5)
189579170ce0SJerome Forissier 			goto check_element_none;
189679170ce0SJerome Forissier 	}
189779170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
189879170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA1)
189979170ce0SJerome Forissier 			goto check_element_none;
190079170ce0SJerome Forissier 	}
190179170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
190279170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA224)
190379170ce0SJerome Forissier 			goto check_element_none;
190479170ce0SJerome Forissier 	}
190579170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
190679170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA256)
190779170ce0SJerome Forissier 			goto check_element_none;
190879170ce0SJerome Forissier 	}
190979170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
191079170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA384)
191179170ce0SJerome Forissier 			goto check_element_none;
191279170ce0SJerome Forissier 	}
191379170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
191479170ce0SJerome Forissier 		if (alg == TEE_ALG_SHA512)
191579170ce0SJerome Forissier 			goto check_element_none;
191679170ce0SJerome Forissier 	}
191779170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
191879170ce0SJerome Forissier 		if (alg == TEE_ALG_MD5SHA1)
191979170ce0SJerome Forissier 			goto check_element_none;
192079170ce0SJerome Forissier 	}
192179170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_HMAC)) {
192279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
192379170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_MD5)
192479170ce0SJerome Forissier 				goto check_element_none;
192579170ce0SJerome Forissier 		}
192679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
192779170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA1)
192879170ce0SJerome Forissier 				goto check_element_none;
192979170ce0SJerome Forissier 		}
193079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
193179170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA224)
193279170ce0SJerome Forissier 				goto check_element_none;
193379170ce0SJerome Forissier 		}
193479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
193579170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA256)
193679170ce0SJerome Forissier 				goto check_element_none;
193779170ce0SJerome Forissier 		}
193879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
193979170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA384)
194079170ce0SJerome Forissier 				goto check_element_none;
194179170ce0SJerome Forissier 		}
194279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
194379170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SHA512)
194479170ce0SJerome Forissier 				goto check_element_none;
194579170ce0SJerome Forissier 		}
194679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SM3)) {
194779170ce0SJerome Forissier 			if (alg == TEE_ALG_HMAC_SM3)
194879170ce0SJerome Forissier 				goto check_element_none;
194979170ce0SJerome Forissier 		}
195079170ce0SJerome Forissier 	}
195179170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM3)) {
195279170ce0SJerome Forissier 		if (alg == TEE_ALG_SM3)
195379170ce0SJerome Forissier 			goto check_element_none;
195479170ce0SJerome Forissier 	}
195579170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM4)) {
195679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
195779170ce0SJerome Forissier 			if (alg == TEE_ALG_SM4_ECB_NOPAD)
195879170ce0SJerome Forissier 				goto check_element_none;
195979170ce0SJerome Forissier 		}
196079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
196179170ce0SJerome Forissier 			if (alg == TEE_ALG_SM4_CBC_NOPAD)
196279170ce0SJerome Forissier 				goto check_element_none;
196379170ce0SJerome Forissier 		}
196479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
196579170ce0SJerome Forissier 			if (alg == TEE_ALG_SM4_CTR)
196679170ce0SJerome Forissier 				goto check_element_none;
196779170ce0SJerome Forissier 		}
196879170ce0SJerome Forissier 	}
196979170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_RSA)) {
197079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
197179170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5)
197279170ce0SJerome Forissier 				goto check_element_none;
197379170ce0SJerome Forissier 		}
197479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
197579170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 ||
197679170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 ||
197779170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1)
197879170ce0SJerome Forissier 				goto check_element_none;
197979170ce0SJerome Forissier 		}
198079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
198179170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1)
198279170ce0SJerome Forissier 				goto check_element_none;
198379170ce0SJerome Forissier 		}
198479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
198579170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 ||
198679170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 ||
198779170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224)
198879170ce0SJerome Forissier 				goto check_element_none;
198979170ce0SJerome Forissier 		}
199079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
199179170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 ||
199279170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 ||
199379170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256)
199479170ce0SJerome Forissier 				goto check_element_none;
199579170ce0SJerome Forissier 		}
199679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
199779170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 ||
199879170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 ||
199979170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384)
200079170ce0SJerome Forissier 				goto check_element_none;
200179170ce0SJerome Forissier 		}
200279170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
200379170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 ||
200479170ce0SJerome Forissier 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 ||
200579170ce0SJerome Forissier 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512)
200679170ce0SJerome Forissier 				goto check_element_none;
200779170ce0SJerome Forissier 		}
200879170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) {
200979170ce0SJerome Forissier 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5)
201079170ce0SJerome Forissier 				goto check_element_none;
201179170ce0SJerome Forissier 		}
201279170ce0SJerome Forissier 		if (alg == TEE_ALG_RSA_NOPAD)
201379170ce0SJerome Forissier 			goto check_element_none;
201479170ce0SJerome Forissier 	}
201579170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_DSA)) {
201679170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
201779170ce0SJerome Forissier 			if (alg == TEE_ALG_DSA_SHA1)
201879170ce0SJerome Forissier 				goto check_element_none;
201979170ce0SJerome Forissier 		}
202079170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
202179170ce0SJerome Forissier 			if (alg == TEE_ALG_DSA_SHA224)
202279170ce0SJerome Forissier 				goto check_element_none;
202379170ce0SJerome Forissier 		}
202479170ce0SJerome Forissier 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
202579170ce0SJerome Forissier 			if (alg == TEE_ALG_DSA_SHA256)
202679170ce0SJerome Forissier 				goto check_element_none;
202779170ce0SJerome Forissier 		}
202879170ce0SJerome Forissier 	}
202979170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_DH)) {
203079170ce0SJerome Forissier 		if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
203179170ce0SJerome Forissier 			goto check_element_none;
203279170ce0SJerome Forissier 	}
203379170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_ECC)) {
203479170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) &&
203579170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P192)
203679170ce0SJerome Forissier 			return TEE_SUCCESS;
203779170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) &&
203879170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P224)
203979170ce0SJerome Forissier 			return TEE_SUCCESS;
204079170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) &&
204179170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P256)
204279170ce0SJerome Forissier 			return TEE_SUCCESS;
204379170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) &&
204479170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P384)
204579170ce0SJerome Forissier 			return TEE_SUCCESS;
204679170ce0SJerome Forissier 		if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) &&
204779170ce0SJerome Forissier 		    element == TEE_ECC_CURVE_NIST_P521)
204879170ce0SJerome Forissier 			return TEE_SUCCESS;
204979170ce0SJerome Forissier 	}
205079170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) {
205179170ce0SJerome Forissier 		if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2)
205279170ce0SJerome Forissier 			return TEE_SUCCESS;
205379170ce0SJerome Forissier 	}
205479170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) {
205579170ce0SJerome Forissier 		if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2)
205679170ce0SJerome Forissier 			return TEE_SUCCESS;
205779170ce0SJerome Forissier 	}
205879170ce0SJerome Forissier 	if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) {
205979170ce0SJerome Forissier 		if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2)
206079170ce0SJerome Forissier 			return TEE_SUCCESS;
206179170ce0SJerome Forissier 	}
206279170ce0SJerome Forissier 
206379170ce0SJerome Forissier 	return TEE_ERROR_NOT_SUPPORTED;
206479170ce0SJerome Forissier check_element_none:
206579170ce0SJerome Forissier 	if (element == TEE_CRYPTO_ELEMENT_NONE)
206679170ce0SJerome Forissier 		return TEE_SUCCESS;
206779170ce0SJerome Forissier 	return TEE_ERROR_NOT_SUPPORTED;
206879170ce0SJerome Forissier }
2069