xref: /optee_os/lib/libutee/tee_api_operations.c (revision 5b385b3f835df16fad1922cfbe6dbe112d2047b5)
11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause
2b0104773SPascal Brand /*
3b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
4b0104773SPascal Brand  */
5b0104773SPascal Brand #include <stdlib.h>
6b0104773SPascal Brand #include <string.h>
7b796ebf3SJerome Forissier #include <string_ext.h>
8b0104773SPascal Brand 
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 	uint32_t ae_tag_len;	/*
28b0104773SPascal Brand 				 * tag_len in bytes for AE operation else unused
29b0104773SPascal Brand 				 */
30b0104773SPascal Brand };
31b0104773SPascal Brand 
32b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */
33b0104773SPascal Brand 
34b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation,
35b0104773SPascal Brand 				 uint32_t algorithm, uint32_t mode,
36b0104773SPascal Brand 				 uint32_t maxKeySize)
37b0104773SPascal Brand {
38b0104773SPascal Brand 	TEE_Result res;
39b0104773SPascal Brand 	TEE_OperationHandle op = TEE_HANDLE_NULL;
40b0104773SPascal Brand 	uint32_t handle_state = 0;
41b0104773SPascal Brand 	size_t block_size = 1;
42b0104773SPascal Brand 	uint32_t req_key_usage;
43b0104773SPascal Brand 	bool with_private_key = false;
44b0104773SPascal Brand 	bool buffer_two_blocks = false;
45b0104773SPascal Brand 
469b52c538SCedric Chaumont 	if (!operation)
47b0104773SPascal Brand 		TEE_Panic(0);
48b0104773SPascal Brand 
49*5b385b3fSJerome Forissier 	if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP)
50b0104773SPascal Brand 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
51b0104773SPascal Brand 
52218d9055SCedric Chaumont 	/* Check algorithm max key size */
53218d9055SCedric Chaumont 	switch (algorithm) {
54218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA1:
55218d9055SCedric Chaumont 		if (maxKeySize < 512)
56218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
57218d9055SCedric Chaumont 		if (maxKeySize > 1024)
58218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
59218d9055SCedric Chaumont 		if (maxKeySize % 64 != 0)
60218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
61218d9055SCedric Chaumont 		break;
62218d9055SCedric Chaumont 
63218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA224:
64218d9055SCedric Chaumont 		if (maxKeySize != 2048)
65218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
66218d9055SCedric Chaumont 		break;
67218d9055SCedric Chaumont 
68218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA256:
69218d9055SCedric Chaumont 		if (maxKeySize != 2048 && maxKeySize != 3072)
70218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
71218d9055SCedric Chaumont 		break;
72218d9055SCedric Chaumont 
731220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P192:
741220586eSCedric Chaumont 	case TEE_ALG_ECDH_P192:
751220586eSCedric Chaumont 		if (maxKeySize != 192)
761220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
771220586eSCedric Chaumont 		break;
781220586eSCedric Chaumont 
791220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P224:
801220586eSCedric Chaumont 	case TEE_ALG_ECDH_P224:
811220586eSCedric Chaumont 		if (maxKeySize != 224)
821220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
831220586eSCedric Chaumont 		break;
841220586eSCedric Chaumont 
851220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P256:
861220586eSCedric Chaumont 	case TEE_ALG_ECDH_P256:
8791fc6bd8SJerome Forissier 	case TEE_ALG_SM2_PKE:
880f151943SJerome Forissier 	case TEE_ALG_SM2_DSA_SM3:
891220586eSCedric Chaumont 		if (maxKeySize != 256)
901220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
911220586eSCedric Chaumont 		break;
921220586eSCedric Chaumont 
93*5b385b3fSJerome Forissier 	case TEE_ALG_SM2_KEP:
94*5b385b3fSJerome Forissier 		/* Two 256-bit keys */
95*5b385b3fSJerome Forissier 		if (maxKeySize != 512)
96*5b385b3fSJerome Forissier 			return TEE_ERROR_NOT_SUPPORTED;
97*5b385b3fSJerome Forissier 		break;
98*5b385b3fSJerome Forissier 
991220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P384:
1001220586eSCedric Chaumont 	case TEE_ALG_ECDH_P384:
1011220586eSCedric Chaumont 		if (maxKeySize != 384)
1021220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1031220586eSCedric Chaumont 		break;
1041220586eSCedric Chaumont 
1051220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P521:
1061220586eSCedric Chaumont 	case TEE_ALG_ECDH_P521:
1071220586eSCedric Chaumont 		if (maxKeySize != 521)
1081220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1091220586eSCedric Chaumont 		break;
1101220586eSCedric Chaumont 
111218d9055SCedric Chaumont 	default:
112218d9055SCedric Chaumont 		break;
113218d9055SCedric Chaumont 	}
114218d9055SCedric Chaumont 
115218d9055SCedric Chaumont 	/* Check algorithm mode */
116b0104773SPascal Brand 	switch (algorithm) {
117b0104773SPascal Brand 	case TEE_ALG_AES_CTS:
118b0104773SPascal Brand 	case TEE_ALG_AES_XTS:
119b0104773SPascal Brand 		buffer_two_blocks = true;
1204bd53c54SJerome Forissier 		/* FALLTHROUGH */
1214bd53c54SJerome Forissier 	case TEE_ALG_AES_ECB_NOPAD:
122b0104773SPascal Brand 	case TEE_ALG_AES_CBC_NOPAD:
123b0104773SPascal Brand 	case TEE_ALG_AES_CCM:
124b0104773SPascal Brand 	case TEE_ALG_DES_ECB_NOPAD:
125b0104773SPascal Brand 	case TEE_ALG_DES_CBC_NOPAD:
126b0104773SPascal Brand 	case TEE_ALG_DES3_ECB_NOPAD:
127b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_NOPAD:
128ade6f848SJerome Forissier 	case TEE_ALG_SM4_ECB_NOPAD:
129ade6f848SJerome Forissier 	case TEE_ALG_SM4_CBC_NOPAD:
130ade6f848SJerome Forissier 	case TEE_ALG_SM4_CTR:
131b0104773SPascal Brand 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
132b0104773SPascal Brand 			block_size = TEE_AES_BLOCK_SIZE;
133ade6f848SJerome Forissier 		else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4)
134ade6f848SJerome Forissier 			block_size = TEE_SM4_BLOCK_SIZE;
135b0104773SPascal Brand 		else
136b0104773SPascal Brand 			block_size = TEE_DES_BLOCK_SIZE;
137afc0c182SBogdan Liulko 		/* FALLTHROUGH */
13857aabac5SBogdan Liulko 	case TEE_ALG_AES_CTR:
139afc0c182SBogdan Liulko 	case TEE_ALG_AES_GCM:
140b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT)
141b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
142b0104773SPascal Brand 		else if (mode == TEE_MODE_DECRYPT)
143b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
144b0104773SPascal Brand 		else
145b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
146b0104773SPascal Brand 		break;
147b0104773SPascal Brand 
1486a2e0a9fSGabor Szekely #if defined(CFG_CRYPTO_RSASSA_NA1)
1496a2e0a9fSGabor Szekely 	case TEE_ALG_RSASSA_PKCS1_V1_5:
1506a2e0a9fSGabor Szekely #endif
151b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
152b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
153b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
154b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
155b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
156b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
157b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
158b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
159b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
160b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
161b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
162b0104773SPascal Brand 	case TEE_ALG_DSA_SHA1:
163218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA224:
164218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA256:
1651220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P192:
1661220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P224:
1671220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P256:
1681220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P384:
1691220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P521:
1700f151943SJerome Forissier 	case TEE_ALG_SM2_DSA_SM3:
171b0104773SPascal Brand 		if (mode == TEE_MODE_SIGN) {
172b0104773SPascal Brand 			with_private_key = true;
173b0104773SPascal Brand 			req_key_usage = TEE_USAGE_SIGN;
174b0104773SPascal Brand 		} else if (mode == TEE_MODE_VERIFY) {
175b0104773SPascal Brand 			req_key_usage = TEE_USAGE_VERIFY;
176b0104773SPascal Brand 		} else {
177b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
178b0104773SPascal Brand 		}
179b0104773SPascal Brand 		break;
180b0104773SPascal Brand 
181b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_V1_5:
182b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
183b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
184b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
185b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
186b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
18791fc6bd8SJerome Forissier 	case TEE_ALG_SM2_PKE:
188b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
189b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
190b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
191b0104773SPascal Brand 			with_private_key = true;
192b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
193b0104773SPascal Brand 		} else {
194b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
195b0104773SPascal Brand 		}
196b0104773SPascal Brand 		break;
197b0104773SPascal Brand 
198b0104773SPascal Brand 	case TEE_ALG_RSA_NOPAD:
199b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
200b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
201b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
202b0104773SPascal Brand 			with_private_key = true;
203b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
204b0104773SPascal Brand 		} else {
205b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
206b0104773SPascal Brand 		}
207b0104773SPascal Brand 		break;
208b0104773SPascal Brand 
209b0104773SPascal Brand 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
2101220586eSCedric Chaumont 	case TEE_ALG_ECDH_P192:
2111220586eSCedric Chaumont 	case TEE_ALG_ECDH_P224:
2121220586eSCedric Chaumont 	case TEE_ALG_ECDH_P256:
2131220586eSCedric Chaumont 	case TEE_ALG_ECDH_P384:
2141220586eSCedric Chaumont 	case TEE_ALG_ECDH_P521:
215cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_MD5_DERIVE_KEY:
216cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA1_DERIVE_KEY:
217cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA224_DERIVE_KEY:
218cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA256_DERIVE_KEY:
219cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA384_DERIVE_KEY:
220cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA512_DERIVE_KEY:
2218854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY:
2228854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY:
2238854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY:
2248854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY:
2258854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY:
2260f2293b7SJerome Forissier 	case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY:
227*5b385b3fSJerome Forissier 	case TEE_ALG_SM2_KEP:
228b0104773SPascal Brand 		if (mode != TEE_MODE_DERIVE)
229b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
230b0104773SPascal Brand 		with_private_key = true;
231b0104773SPascal Brand 		req_key_usage = TEE_USAGE_DERIVE;
232b0104773SPascal Brand 		break;
233b0104773SPascal Brand 
234b0104773SPascal Brand 	case TEE_ALG_MD5:
235b0104773SPascal Brand 	case TEE_ALG_SHA1:
236b0104773SPascal Brand 	case TEE_ALG_SHA224:
237b0104773SPascal Brand 	case TEE_ALG_SHA256:
238b0104773SPascal Brand 	case TEE_ALG_SHA384:
239b0104773SPascal Brand 	case TEE_ALG_SHA512:
24047645577SJerome Forissier 	case TEE_ALG_SM3:
241b0104773SPascal Brand 		if (mode != TEE_MODE_DIGEST)
242b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
24305304565SCedric Chaumont 		/* v1.1: flags always set for digest operations */
244b0104773SPascal Brand 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
245b0104773SPascal Brand 		req_key_usage = 0;
246b0104773SPascal Brand 		break;
247b0104773SPascal Brand 
248b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_NOPAD:
249b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_NOPAD:
250b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_PKCS5:
251b0104773SPascal Brand 	case TEE_ALG_AES_CMAC:
252b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_PKCS5:
253b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
254b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
255b0104773SPascal Brand 	case TEE_ALG_HMAC_MD5:
256b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA1:
257b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA224:
258b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA256:
259b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA384:
260b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA512:
26147645577SJerome Forissier 	case TEE_ALG_HMAC_SM3:
262b0104773SPascal Brand 		if (mode != TEE_MODE_MAC)
263b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
264b0104773SPascal Brand 		req_key_usage = TEE_USAGE_MAC;
265b0104773SPascal Brand 		break;
266b0104773SPascal Brand 
267b0104773SPascal Brand 	default:
268b0104773SPascal Brand 		return TEE_ERROR_NOT_SUPPORTED;
269b0104773SPascal Brand 	}
270b0104773SPascal Brand 
271b66f219bSJens Wiklander 	op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO);
2729b52c538SCedric Chaumont 	if (!op)
273b0104773SPascal Brand 		return TEE_ERROR_OUT_OF_MEMORY;
274b0104773SPascal Brand 
275b0104773SPascal Brand 	op->info.algorithm = algorithm;
276b0104773SPascal Brand 	op->info.operationClass = TEE_ALG_GET_CLASS(algorithm);
2776a2e0a9fSGabor Szekely #ifdef CFG_CRYPTO_RSASSA_NA1
2786a2e0a9fSGabor Szekely 	if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5)
2796a2e0a9fSGabor Szekely 		op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE;
2806a2e0a9fSGabor Szekely #endif
281b0104773SPascal Brand 	op->info.mode = mode;
282b0104773SPascal Brand 	op->info.maxKeySize = maxKeySize;
283b0104773SPascal Brand 	op->info.requiredKeyUsage = req_key_usage;
284b0104773SPascal Brand 	op->info.handleState = handle_state;
285b0104773SPascal Brand 
286b0104773SPascal Brand 	if (block_size > 1) {
287b0104773SPascal Brand 		size_t buffer_size = block_size;
288b0104773SPascal Brand 
289b0104773SPascal Brand 		if (buffer_two_blocks)
290b0104773SPascal Brand 			buffer_size *= 2;
291b0104773SPascal Brand 
2929b52c538SCedric Chaumont 		op->buffer = TEE_Malloc(buffer_size,
2939b52c538SCedric Chaumont 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
294b0104773SPascal Brand 		if (op->buffer == NULL) {
295b0104773SPascal Brand 			res = TEE_ERROR_OUT_OF_MEMORY;
296b66f219bSJens Wiklander 			goto out;
297b0104773SPascal Brand 		}
298b0104773SPascal Brand 	}
299b0104773SPascal Brand 	op->block_size = block_size;
300b0104773SPascal Brand 	op->buffer_two_blocks = buffer_two_blocks;
301b0104773SPascal Brand 
302b0104773SPascal Brand 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
303b0104773SPascal Brand 		uint32_t mks = maxKeySize;
304b0104773SPascal Brand 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
305b0104773SPascal Brand 						       with_private_key);
306b0104773SPascal Brand 
307b0104773SPascal Brand 		/*
308b0104773SPascal Brand 		 * If two keys are expected the max key size is the sum of
309b0104773SPascal Brand 		 * the size of both keys.
310b0104773SPascal Brand 		 */
311b0104773SPascal Brand 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
312b0104773SPascal Brand 			mks /= 2;
313b0104773SPascal Brand 
314b0104773SPascal Brand 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
315b0104773SPascal Brand 		if (res != TEE_SUCCESS)
316b66f219bSJens Wiklander 			goto out;
317b0104773SPascal Brand 
31805304565SCedric Chaumont 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
3199b52c538SCedric Chaumont 			res = TEE_AllocateTransientObject(key_type, mks,
320b0104773SPascal Brand 							  &op->key2);
321b0104773SPascal Brand 			if (res != TEE_SUCCESS)
322b66f219bSJens Wiklander 				goto out;
323b0104773SPascal Brand 		}
324b0104773SPascal Brand 	}
325b0104773SPascal Brand 
326e86f1266SJens Wiklander 	res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1,
327e86f1266SJens Wiklander 				    (unsigned long)op->key2, &op->state);
328b66f219bSJens Wiklander 	if (res != TEE_SUCCESS)
329b66f219bSJens Wiklander 		goto out;
330b0104773SPascal Brand 
33105304565SCedric Chaumont 	/*
33205304565SCedric Chaumont 	 * Initialize digest operations
33305304565SCedric Chaumont 	 * Other multi-stage operations initialized w/ TEE_xxxInit functions
33405304565SCedric Chaumont 	 * Non-applicable on asymmetric operations
33505304565SCedric Chaumont 	 */
33605304565SCedric Chaumont 	if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) {
33705304565SCedric Chaumont 		res = utee_hash_init(op->state, NULL, 0);
33805304565SCedric Chaumont 		if (res != TEE_SUCCESS)
339b66f219bSJens Wiklander 			goto out;
34005304565SCedric Chaumont 		/* v1.1: flags always set for digest operations */
34105304565SCedric Chaumont 		op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
34205304565SCedric Chaumont 	}
34305304565SCedric Chaumont 
344642a1607SCedric Chaumont 	op->operationState = TEE_OPERATION_STATE_INITIAL;
345642a1607SCedric Chaumont 
346b0104773SPascal Brand 	*operation = op;
347b0104773SPascal Brand 
348b66f219bSJens Wiklander out:
349b66f219bSJens Wiklander 	if (res != TEE_SUCCESS) {
350b66f219bSJens Wiklander 		if (res != TEE_ERROR_OUT_OF_MEMORY &&
3519b52c538SCedric Chaumont 		    res != TEE_ERROR_NOT_SUPPORTED)
352b36311adSJerome Forissier 			TEE_Panic(res);
353b66f219bSJens Wiklander 		if (op) {
354b66f219bSJens Wiklander 			if (op->state) {
355b66f219bSJens Wiklander 				TEE_FreeOperation(op);
356b66f219bSJens Wiklander 			} else {
357b66f219bSJens Wiklander 				TEE_Free(op->buffer);
358b66f219bSJens Wiklander 				TEE_FreeTransientObject(op->key1);
359b66f219bSJens Wiklander 				TEE_FreeTransientObject(op->key2);
360b66f219bSJens Wiklander 				TEE_Free(op);
361b66f219bSJens Wiklander 			}
362b66f219bSJens Wiklander 		}
363b66f219bSJens Wiklander 	}
364b66f219bSJens Wiklander 
365b0104773SPascal Brand 	return res;
366b0104773SPascal Brand }
367b0104773SPascal Brand 
368b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation)
369b0104773SPascal Brand {
370e889e80bSCedric Chaumont 	TEE_Result res;
371e889e80bSCedric Chaumont 
372e889e80bSCedric Chaumont 	if (operation == TEE_HANDLE_NULL)
373e889e80bSCedric Chaumont 		TEE_Panic(0);
374e889e80bSCedric Chaumont 
375b0104773SPascal Brand 	/*
376b0104773SPascal Brand 	 * Note that keys should not be freed here, since they are
377b0104773SPascal Brand 	 * claimed by the operation they will be freed by
378b0104773SPascal Brand 	 * utee_cryp_state_free().
379b0104773SPascal Brand 	 */
380e889e80bSCedric Chaumont 	res = utee_cryp_state_free(operation->state);
381e889e80bSCedric Chaumont 	if (res != TEE_SUCCESS)
382b36311adSJerome Forissier 		TEE_Panic(res);
383e889e80bSCedric Chaumont 
384b0104773SPascal Brand 	TEE_Free(operation->buffer);
385b0104773SPascal Brand 	TEE_Free(operation);
386b0104773SPascal Brand }
387b0104773SPascal Brand 
388b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation,
389b0104773SPascal Brand 			  TEE_OperationInfo *operationInfo)
390b0104773SPascal Brand {
391b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
392b0104773SPascal Brand 		TEE_Panic(0);
393b0104773SPascal Brand 
39405304565SCedric Chaumont 	if (!operationInfo)
395b0104773SPascal Brand 		TEE_Panic(0);
396b0104773SPascal Brand 
397b0104773SPascal Brand 	*operationInfo = operation->info;
398b0104773SPascal Brand }
399b0104773SPascal Brand 
40005304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation,
40105304565SCedric Chaumont 			  TEE_OperationInfoMultiple *operationInfoMultiple,
40205304565SCedric Chaumont 			  uint32_t *operationSize)
40305304565SCedric Chaumont {
40405304565SCedric Chaumont 	TEE_Result res = TEE_SUCCESS;
40505304565SCedric Chaumont 	TEE_ObjectInfo key_info1;
40605304565SCedric Chaumont 	TEE_ObjectInfo key_info2;
40705304565SCedric Chaumont 	uint32_t num_of_keys;
40805304565SCedric Chaumont 	size_t n;
40905304565SCedric Chaumont 
41005304565SCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
41105304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
41205304565SCedric Chaumont 		goto out;
41305304565SCedric Chaumont 	}
41405304565SCedric Chaumont 
41505304565SCedric Chaumont 	if (!operationInfoMultiple) {
41605304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
41705304565SCedric Chaumont 		goto out;
41805304565SCedric Chaumont 	}
41905304565SCedric Chaumont 
42005304565SCedric Chaumont 	if (!operationSize) {
42105304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
42205304565SCedric Chaumont 		goto out;
42305304565SCedric Chaumont 	}
42405304565SCedric Chaumont 
42505304565SCedric Chaumont 	num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/
42605304565SCedric Chaumont 			sizeof(TEE_OperationInfoKey);
42705304565SCedric Chaumont 
42805304565SCedric Chaumont 	if (num_of_keys > 2) {
42905304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
43005304565SCedric Chaumont 		goto out;
43105304565SCedric Chaumont 	}
43205304565SCedric Chaumont 
43305304565SCedric Chaumont 	/* Two keys flag (TEE_ALG_AES_XTS only) */
43405304565SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
43505304565SCedric Chaumont 	    0 &&
43605304565SCedric Chaumont 	    (num_of_keys != 2)) {
43705304565SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
43805304565SCedric Chaumont 		goto out;
43905304565SCedric Chaumont 	}
44005304565SCedric Chaumont 
44105304565SCedric Chaumont 	/* Clear */
44205304565SCedric Chaumont 	for (n = 0; n < num_of_keys; n++) {
44305304565SCedric Chaumont 		operationInfoMultiple->keyInformation[n].keySize = 0;
44405304565SCedric Chaumont 		operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0;
44505304565SCedric Chaumont 	}
44605304565SCedric Chaumont 
44705304565SCedric Chaumont 	if (num_of_keys == 2) {
44805304565SCedric Chaumont 		res = TEE_GetObjectInfo1(operation->key2, &key_info2);
44905304565SCedric Chaumont 		/* Key2 is not a valid handle */
45005304565SCedric Chaumont 		if (res != TEE_SUCCESS)
45105304565SCedric Chaumont 			goto out;
45205304565SCedric Chaumont 
45305304565SCedric Chaumont 		operationInfoMultiple->keyInformation[1].keySize =
45405304565SCedric Chaumont 			key_info2.keySize;
45505304565SCedric Chaumont 		operationInfoMultiple->keyInformation[1].requiredKeyUsage =
45605304565SCedric Chaumont 			operation->info.requiredKeyUsage;
45705304565SCedric Chaumont 	}
45805304565SCedric Chaumont 
45905304565SCedric Chaumont 	if (num_of_keys >= 1) {
46005304565SCedric Chaumont 		res = TEE_GetObjectInfo1(operation->key1, &key_info1);
46105304565SCedric Chaumont 		/* Key1 is not a valid handle */
46205304565SCedric Chaumont 		if (res != TEE_SUCCESS) {
46305304565SCedric Chaumont 			if (num_of_keys == 2) {
46405304565SCedric Chaumont 				operationInfoMultiple->keyInformation[1].
46505304565SCedric Chaumont 							keySize = 0;
46605304565SCedric Chaumont 				operationInfoMultiple->keyInformation[1].
46705304565SCedric Chaumont 							requiredKeyUsage = 0;
46805304565SCedric Chaumont 			}
46905304565SCedric Chaumont 			goto out;
47005304565SCedric Chaumont 		}
47105304565SCedric Chaumont 
47205304565SCedric Chaumont 		operationInfoMultiple->keyInformation[0].keySize =
47305304565SCedric Chaumont 			key_info1.keySize;
47405304565SCedric Chaumont 		operationInfoMultiple->keyInformation[0].requiredKeyUsage =
47505304565SCedric Chaumont 			operation->info.requiredKeyUsage;
47605304565SCedric Chaumont 	}
47705304565SCedric Chaumont 
47805304565SCedric Chaumont 	/* No key */
47905304565SCedric Chaumont 	operationInfoMultiple->algorithm = operation->info.algorithm;
48005304565SCedric Chaumont 	operationInfoMultiple->operationClass = operation->info.operationClass;
48105304565SCedric Chaumont 	operationInfoMultiple->mode = operation->info.mode;
48205304565SCedric Chaumont 	operationInfoMultiple->digestLength = operation->info.digestLength;
48305304565SCedric Chaumont 	operationInfoMultiple->maxKeySize = operation->info.maxKeySize;
48405304565SCedric Chaumont 	operationInfoMultiple->handleState = operation->info.handleState;
48505304565SCedric Chaumont 	operationInfoMultiple->operationState = operation->operationState;
48605304565SCedric Chaumont 	operationInfoMultiple->numberOfKeys = num_of_keys;
48705304565SCedric Chaumont 
48805304565SCedric Chaumont out:
48905304565SCedric Chaumont 	if (res != TEE_SUCCESS &&
49005304565SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
491b36311adSJerome Forissier 		TEE_Panic(res);
49205304565SCedric Chaumont 
49305304565SCedric Chaumont 	return res;
49405304565SCedric Chaumont }
49505304565SCedric Chaumont 
496b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation)
497b0104773SPascal Brand {
498b0104773SPascal Brand 	TEE_Result res;
499b0104773SPascal Brand 
500b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
501b0104773SPascal Brand 		TEE_Panic(0);
502bf80076aSCedric Chaumont 
503642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET))
504bf80076aSCedric Chaumont 			TEE_Panic(0);
505bf80076aSCedric Chaumont 
506642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
507642a1607SCedric Chaumont 
508b0104773SPascal Brand 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
509b0104773SPascal Brand 		res = utee_hash_init(operation->state, NULL, 0);
510b0104773SPascal Brand 		if (res != TEE_SUCCESS)
511b0104773SPascal Brand 			TEE_Panic(res);
51205304565SCedric Chaumont 		operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
51305304565SCedric Chaumont 	} else {
514b0104773SPascal Brand 		operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
515b0104773SPascal Brand 	}
51605304565SCedric Chaumont }
517b0104773SPascal Brand 
518b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
519b0104773SPascal Brand 			       TEE_ObjectHandle key)
520b0104773SPascal Brand {
5217583c59eSCedric Chaumont 	TEE_Result res;
522b0104773SPascal Brand 	uint32_t key_size = 0;
523b0104773SPascal Brand 	TEE_ObjectInfo key_info;
524b0104773SPascal Brand 
525a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
526a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
527a57c1e2eSCedric Chaumont 		goto out;
528a57c1e2eSCedric Chaumont 	}
529a57c1e2eSCedric Chaumont 
530642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
531642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
532642a1607SCedric Chaumont 		goto out;
533642a1607SCedric Chaumont 	}
534642a1607SCedric Chaumont 
535a57c1e2eSCedric Chaumont 	if (key == TEE_HANDLE_NULL) {
536a57c1e2eSCedric Chaumont 		/* Operation key cleared */
537a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key1);
538a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
539a57c1e2eSCedric Chaumont 		goto out;
540a57c1e2eSCedric Chaumont 	}
541a57c1e2eSCedric Chaumont 
542a57c1e2eSCedric Chaumont 	/* No key for digest operation */
543a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
544a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
545a57c1e2eSCedric Chaumont 		goto out;
546a57c1e2eSCedric Chaumont 	}
547a57c1e2eSCedric Chaumont 
548a57c1e2eSCedric Chaumont 	/* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */
549a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
550a57c1e2eSCedric Chaumont 	    0) {
551a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
552a57c1e2eSCedric Chaumont 		goto out;
553a57c1e2eSCedric Chaumont 	}
554a57c1e2eSCedric Chaumont 
5557583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key, &key_info);
556a57c1e2eSCedric Chaumont 	/* Key is not a valid handle */
5577583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
558a57c1e2eSCedric Chaumont 		goto out;
5597583c59eSCedric Chaumont 
560b0104773SPascal Brand 	/* Supplied key has to meet required usage */
561b0104773SPascal Brand 	if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
562b0104773SPascal Brand 	    operation->info.requiredKeyUsage) {
563a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
564a57c1e2eSCedric Chaumont 		goto out;
565b0104773SPascal Brand 	}
566b0104773SPascal Brand 
567a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info.keySize) {
568a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
569a57c1e2eSCedric Chaumont 		goto out;
570a57c1e2eSCedric Chaumont 	}
571b0104773SPascal Brand 
5727583c59eSCedric Chaumont 	key_size = key_info.keySize;
573b0104773SPascal Brand 
574b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
575b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
576b0104773SPascal Brand 
5777583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key);
5787583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
579a57c1e2eSCedric Chaumont 		goto out;
5807583c59eSCedric Chaumont 
581b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
582b0104773SPascal Brand 
583b0104773SPascal Brand 	operation->info.keySize = key_size;
584b0104773SPascal Brand 
5857583c59eSCedric Chaumont out:
586a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
587a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
588a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
589b36311adSJerome Forissier 		TEE_Panic(res);
590a57c1e2eSCedric Chaumont 
591a57c1e2eSCedric Chaumont 	return res;
592b0104773SPascal Brand }
593b0104773SPascal Brand 
594b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
595b0104773SPascal Brand 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
596b0104773SPascal Brand {
5977583c59eSCedric Chaumont 	TEE_Result res;
598b0104773SPascal Brand 	uint32_t key_size = 0;
599b0104773SPascal Brand 	TEE_ObjectInfo key_info1;
600b0104773SPascal Brand 	TEE_ObjectInfo key_info2;
601b0104773SPascal Brand 
602a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
603a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
604a57c1e2eSCedric Chaumont 		goto out;
605a57c1e2eSCedric Chaumont 	}
606a57c1e2eSCedric Chaumont 
607642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
608642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
609642a1607SCedric Chaumont 		goto out;
610642a1607SCedric Chaumont 	}
611642a1607SCedric Chaumont 
612a57c1e2eSCedric Chaumont 	/*
613a57c1e2eSCedric Chaumont 	 * Key1/Key2 and/or are not initialized and
614a57c1e2eSCedric Chaumont 	 * Either both keys are NULL or both are not NULL
615a57c1e2eSCedric Chaumont 	 */
616a57c1e2eSCedric Chaumont 	if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) {
617a57c1e2eSCedric Chaumont 		/* Clear operation key1 (if needed) */
618a57c1e2eSCedric Chaumont 		if (key1 == TEE_HANDLE_NULL)
619a57c1e2eSCedric Chaumont 			TEE_ResetTransientObject(operation->key1);
620a57c1e2eSCedric Chaumont 		/* Clear operation key2 (if needed) */
621a57c1e2eSCedric Chaumont 		if (key2 == TEE_HANDLE_NULL)
622a57c1e2eSCedric Chaumont 			TEE_ResetTransientObject(operation->key2);
623a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
624a57c1e2eSCedric Chaumont 		goto out;
625a57c1e2eSCedric Chaumont 	}
626a57c1e2eSCedric Chaumont 
627a57c1e2eSCedric Chaumont 	/* No key for digest operation */
628a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
629a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
630a57c1e2eSCedric Chaumont 		goto out;
631a57c1e2eSCedric Chaumont 	}
632a57c1e2eSCedric Chaumont 
633*5b385b3fSJerome Forissier 	/* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */
634a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
635a57c1e2eSCedric Chaumont 	    0) {
636a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
637a57c1e2eSCedric Chaumont 		goto out;
638a57c1e2eSCedric Chaumont 	}
639a57c1e2eSCedric Chaumont 
6407583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key1, &key_info1);
641a57c1e2eSCedric Chaumont 	/* Key1 is not a valid handle */
6427583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
643a57c1e2eSCedric Chaumont 		goto out;
6447583c59eSCedric Chaumont 
645b0104773SPascal Brand 	/* Supplied key has to meet required usage */
646b0104773SPascal Brand 	if ((key_info1.objectUsage & operation->info.
647b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
648a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
649a57c1e2eSCedric Chaumont 		goto out;
650b0104773SPascal Brand 	}
651b0104773SPascal Brand 
6527583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key2, &key_info2);
653a57c1e2eSCedric Chaumont 	/* Key2 is not a valid handle */
6547583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
6557583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
6567583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
657a57c1e2eSCedric Chaumont 		goto out;
6587583c59eSCedric Chaumont 	}
6597583c59eSCedric Chaumont 
660b0104773SPascal Brand 	/* Supplied key has to meet required usage */
661b0104773SPascal Brand 	if ((key_info2.objectUsage & operation->info.
662b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
663a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
664a57c1e2eSCedric Chaumont 		goto out;
665b0104773SPascal Brand 	}
666b0104773SPascal Brand 
667b0104773SPascal Brand 	/*
668*5b385b3fSJerome Forissier 	 * All the multi key algorithm currently supported requires the keys to
669*5b385b3fSJerome Forissier 	 * be of equal size.
670b0104773SPascal Brand 	 */
671*5b385b3fSJerome Forissier 	if (key_info1.keySize != key_info2.keySize) {
672a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
673a57c1e2eSCedric Chaumont 		goto out;
674b0104773SPascal Brand 
675a57c1e2eSCedric Chaumont 	}
676a57c1e2eSCedric Chaumont 
677a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info1.keySize) {
678a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
679a57c1e2eSCedric Chaumont 		goto out;
680a57c1e2eSCedric Chaumont 	}
681b0104773SPascal Brand 
682b0104773SPascal Brand 	/*
683b0104773SPascal Brand 	 * Odd that only the size of one key should be reported while
684b0104773SPascal Brand 	 * size of two key are used when allocating the operation.
685b0104773SPascal Brand 	 */
6867583c59eSCedric Chaumont 	key_size = key_info1.keySize;
687b0104773SPascal Brand 
688b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
689b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key2);
690b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
691b0104773SPascal Brand 
6927583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key1);
6937583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
694a57c1e2eSCedric Chaumont 		goto out;
6957583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key2, key2);
6967583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
6977583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
6987583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
699a57c1e2eSCedric Chaumont 		goto out;
7007583c59eSCedric Chaumont 	}
7017583c59eSCedric Chaumont 
702b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
703b0104773SPascal Brand 
704b0104773SPascal Brand 	operation->info.keySize = key_size;
705b0104773SPascal Brand 
7067583c59eSCedric Chaumont out:
707a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
708a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
709a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT_2 &&
710a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE &&
711a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
712b36311adSJerome Forissier 		TEE_Panic(res);
713a57c1e2eSCedric Chaumont 
714a57c1e2eSCedric Chaumont 	return res;
715b0104773SPascal Brand }
716b0104773SPascal Brand 
717b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
718b0104773SPascal Brand {
719b0104773SPascal Brand 	TEE_Result res;
720b0104773SPascal Brand 
721b0104773SPascal Brand 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
722b0104773SPascal Brand 		TEE_Panic(0);
723b0104773SPascal Brand 	if (dst_op->info.algorithm != src_op->info.algorithm)
724b0104773SPascal Brand 		TEE_Panic(0);
725b0104773SPascal Brand 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
726b0104773SPascal Brand 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
727b0104773SPascal Brand 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
728b0104773SPascal Brand 
729b0104773SPascal Brand 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
730b0104773SPascal Brand 			key1 = src_op->key1;
731b0104773SPascal Brand 			key2 = src_op->key2;
732b0104773SPascal Brand 		}
733b0104773SPascal Brand 
734b0104773SPascal Brand 		if ((src_op->info.handleState &
735b0104773SPascal Brand 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
736b0104773SPascal Brand 			TEE_SetOperationKey(dst_op, key1);
737b0104773SPascal Brand 		} else {
738b0104773SPascal Brand 			TEE_SetOperationKey2(dst_op, key1, key2);
739b0104773SPascal Brand 		}
740b0104773SPascal Brand 	}
741b0104773SPascal Brand 	dst_op->info.handleState = src_op->info.handleState;
742b0104773SPascal Brand 	dst_op->info.keySize = src_op->info.keySize;
743642a1607SCedric Chaumont 	dst_op->operationState = src_op->operationState;
744b0104773SPascal Brand 
745b0104773SPascal Brand 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
746b0104773SPascal Brand 	    dst_op->block_size != src_op->block_size)
747b0104773SPascal Brand 		TEE_Panic(0);
748b0104773SPascal Brand 
749b0104773SPascal Brand 	if (dst_op->buffer != NULL) {
750b0104773SPascal Brand 		if (src_op->buffer == NULL)
751b0104773SPascal Brand 			TEE_Panic(0);
752b0104773SPascal Brand 
753b0104773SPascal Brand 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
754b0104773SPascal Brand 		dst_op->buffer_offs = src_op->buffer_offs;
755b0104773SPascal Brand 	} else if (src_op->buffer != NULL) {
756b0104773SPascal Brand 		TEE_Panic(0);
757b0104773SPascal Brand 	}
758b0104773SPascal Brand 
759b0104773SPascal Brand 	res = utee_cryp_state_copy(dst_op->state, src_op->state);
760b0104773SPascal Brand 	if (res != TEE_SUCCESS)
761b0104773SPascal Brand 		TEE_Panic(res);
762b0104773SPascal Brand }
763b0104773SPascal Brand 
764b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */
765b0104773SPascal Brand 
7668f07fe6fSJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, const void *IV,
7676d15db08SJerome Forissier 				uint32_t IVLen)
7686d15db08SJerome Forissier {
7696d15db08SJerome Forissier 	TEE_Result res;
7706d15db08SJerome Forissier 
7716d15db08SJerome Forissier 	/*
7726d15db08SJerome Forissier 	 * Note : IV and IVLen are never used in current implementation
7736d15db08SJerome Forissier 	 * This is why coherent values of IV and IVLen are not checked
7746d15db08SJerome Forissier 	 */
7756d15db08SJerome Forissier 	res = utee_hash_init(operation->state, IV, IVLen);
7766d15db08SJerome Forissier 	if (res != TEE_SUCCESS)
7776d15db08SJerome Forissier 		TEE_Panic(res);
7786d15db08SJerome Forissier 	operation->buffer_offs = 0;
7796d15db08SJerome Forissier 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
7806d15db08SJerome Forissier }
7816d15db08SJerome Forissier 
782b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation,
7838f07fe6fSJerome Forissier 		      const void *chunk, uint32_t chunkSize)
784b0104773SPascal Brand {
78573d6c3baSJoakim Bech 	TEE_Result res = TEE_ERROR_GENERIC;
786b0104773SPascal Brand 
78773d6c3baSJoakim Bech 	if (operation == TEE_HANDLE_NULL ||
78873d6c3baSJoakim Bech 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
789b0104773SPascal Brand 		TEE_Panic(0);
79073d6c3baSJoakim Bech 
791642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
792642a1607SCedric Chaumont 
793b0104773SPascal Brand 	res = utee_hash_update(operation->state, chunk, chunkSize);
794b0104773SPascal Brand 	if (res != TEE_SUCCESS)
795b0104773SPascal Brand 		TEE_Panic(res);
796b0104773SPascal Brand }
797b0104773SPascal Brand 
7988f07fe6fSJerome Forissier TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
79979a3c601SCedric Chaumont 			     uint32_t chunkLen, void *hash, uint32_t *hashLen)
800b0104773SPascal Brand {
80187c2f6b6SCedric Chaumont 	TEE_Result res;
802e86f1266SJens Wiklander 	uint64_t hl;
80387c2f6b6SCedric Chaumont 
80487c2f6b6SCedric Chaumont 	if ((operation == TEE_HANDLE_NULL) ||
80587c2f6b6SCedric Chaumont 	    (!chunk && chunkLen) ||
80687c2f6b6SCedric Chaumont 	    !hash ||
80787c2f6b6SCedric Chaumont 	    !hashLen ||
80887c2f6b6SCedric Chaumont 	    (operation->info.operationClass != TEE_OPERATION_DIGEST)) {
80987c2f6b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
81087c2f6b6SCedric Chaumont 		goto out;
81187c2f6b6SCedric Chaumont 	}
81287c2f6b6SCedric Chaumont 
813e86f1266SJens Wiklander 	hl = *hashLen;
814e86f1266SJens Wiklander 	res = utee_hash_final(operation->state, chunk, chunkLen, hash, &hl);
815e86f1266SJens Wiklander 	*hashLen = hl;
8166d15db08SJerome Forissier 	if (res != TEE_SUCCESS)
8176d15db08SJerome Forissier 		goto out;
8186d15db08SJerome Forissier 
8196d15db08SJerome Forissier 	/* Reset operation state */
8206d15db08SJerome Forissier 	init_hash_operation(operation, NULL, 0);
82187c2f6b6SCedric Chaumont 
822642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
823642a1607SCedric Chaumont 
82487c2f6b6SCedric Chaumont out:
82587c2f6b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
82687c2f6b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
827b36311adSJerome Forissier 		TEE_Panic(res);
82873d6c3baSJoakim Bech 
82987c2f6b6SCedric Chaumont 	return res;
830b0104773SPascal Brand }
831b0104773SPascal Brand 
832b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */
833b0104773SPascal Brand 
8348f07fe6fSJerome Forissier void TEE_CipherInit(TEE_OperationHandle operation, const void *IV,
8358f07fe6fSJerome Forissier 		    uint32_t IVLen)
836b0104773SPascal Brand {
837b0104773SPascal Brand 	TEE_Result res;
838b0104773SPascal Brand 
839b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
840b0104773SPascal Brand 		TEE_Panic(0);
841642a1607SCedric Chaumont 
842b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
843b0104773SPascal Brand 		TEE_Panic(0);
844642a1607SCedric Chaumont 
845642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
846642a1607SCedric Chaumont 	    !(operation->key1))
847642a1607SCedric Chaumont 		TEE_Panic(0);
848642a1607SCedric Chaumont 
849642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
850642a1607SCedric Chaumont 		TEE_ResetOperation(operation);
851642a1607SCedric Chaumont 
852642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
853642a1607SCedric Chaumont 
854b0104773SPascal Brand 	res = utee_cipher_init(operation->state, IV, IVLen);
855b0104773SPascal Brand 	if (res != TEE_SUCCESS)
856b0104773SPascal Brand 		TEE_Panic(res);
857642a1607SCedric Chaumont 
858b0104773SPascal Brand 	operation->buffer_offs = 0;
859b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
860b0104773SPascal Brand }
861b0104773SPascal Brand 
862b0104773SPascal Brand static TEE_Result tee_buffer_update(
863b0104773SPascal Brand 		TEE_OperationHandle op,
864e86f1266SJens Wiklander 		TEE_Result(*update_func)(unsigned long state, const void *src,
865e86f1266SJens Wiklander 				size_t slen, void *dst, uint64_t *dlen),
866b0104773SPascal Brand 		const void *src_data, size_t src_len,
867e86f1266SJens Wiklander 		void *dest_data, uint64_t *dest_len)
868b0104773SPascal Brand {
869b0104773SPascal Brand 	TEE_Result res;
870b0104773SPascal Brand 	const uint8_t *src = src_data;
871b0104773SPascal Brand 	size_t slen = src_len;
872b0104773SPascal Brand 	uint8_t *dst = dest_data;
873b0104773SPascal Brand 	size_t dlen = *dest_len;
874b0104773SPascal Brand 	size_t acc_dlen = 0;
875e86f1266SJens Wiklander 	uint64_t tmp_dlen;
876b0104773SPascal Brand 	size_t l;
877b0104773SPascal Brand 	size_t buffer_size;
878d3588802SPascal Brand 	size_t buffer_left;
879b0104773SPascal Brand 
880090268f5SJens Wiklander 	if (!src) {
881090268f5SJens Wiklander 		if (slen)
882090268f5SJens Wiklander 			TEE_Panic(0);
883090268f5SJens Wiklander 		goto out;
884090268f5SJens Wiklander 	}
885090268f5SJens Wiklander 
886d3588802SPascal Brand 	if (op->buffer_two_blocks) {
887b0104773SPascal Brand 		buffer_size = op->block_size * 2;
888d3588802SPascal Brand 		buffer_left = 1;
889d3588802SPascal Brand 	} else {
890b0104773SPascal Brand 		buffer_size = op->block_size;
891d3588802SPascal Brand 		buffer_left = 0;
892d3588802SPascal Brand 	}
893b0104773SPascal Brand 
894b0104773SPascal Brand 	if (op->buffer_offs > 0) {
895b0104773SPascal Brand 		/* Fill up complete block */
896b0104773SPascal Brand 		if (op->buffer_offs < op->block_size)
897b0104773SPascal Brand 			l = MIN(slen, op->block_size - op->buffer_offs);
898b0104773SPascal Brand 		else
899b0104773SPascal Brand 			l = MIN(slen, buffer_size - op->buffer_offs);
900b0104773SPascal Brand 		memcpy(op->buffer + op->buffer_offs, src, l);
901b0104773SPascal Brand 		op->buffer_offs += l;
902b0104773SPascal Brand 		src += l;
903b0104773SPascal Brand 		slen -= l;
904b0104773SPascal Brand 		if ((op->buffer_offs % op->block_size) != 0)
905b0104773SPascal Brand 			goto out;	/* Nothing left to do */
906b0104773SPascal Brand 	}
907b0104773SPascal Brand 
908b0104773SPascal Brand 	/* If we can feed from buffer */
909d3588802SPascal Brand 	if ((op->buffer_offs > 0) &&
910d3588802SPascal Brand 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
9112ff3fdbbSPascal Brand 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
912b0104773SPascal Brand 				op->block_size);
913b0104773SPascal Brand 		l = MIN(op->buffer_offs, l);
914b0104773SPascal Brand 		tmp_dlen = dlen;
915b0104773SPascal Brand 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
916b0104773SPascal Brand 		if (res != TEE_SUCCESS)
917b0104773SPascal Brand 			TEE_Panic(res);
918b0104773SPascal Brand 		dst += tmp_dlen;
919b0104773SPascal Brand 		dlen -= tmp_dlen;
920b0104773SPascal Brand 		acc_dlen += tmp_dlen;
921b0104773SPascal Brand 		op->buffer_offs -= l;
922b0104773SPascal Brand 		if (op->buffer_offs > 0) {
923b0104773SPascal Brand 			/*
924b0104773SPascal Brand 			 * Slen is small enough to be contained in rest buffer.
925b0104773SPascal Brand 			 */
926b0104773SPascal Brand 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
927b0104773SPascal Brand 			memcpy(op->buffer + op->buffer_offs, src, slen);
928b0104773SPascal Brand 			op->buffer_offs += slen;
929b0104773SPascal Brand 			goto out;	/* Nothing left to do */
930b0104773SPascal Brand 		}
931b0104773SPascal Brand 	}
932b0104773SPascal Brand 
933d3588802SPascal Brand 	if (slen >= (buffer_size + buffer_left)) {
934b0104773SPascal Brand 		/* Buffer is empty, feed as much as possible from src */
935bf7a587fSJerome Forissier 		if (op->info.algorithm == TEE_ALG_AES_CTS)
936b1ecda78SJerome Forissier 			l = ROUNDUP(slen - buffer_size, op->block_size);
937bf7a587fSJerome Forissier 		else
938bf7a587fSJerome Forissier 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
939b0104773SPascal Brand 
940b0104773SPascal Brand 		tmp_dlen = dlen;
941b0104773SPascal Brand 		res = update_func(op->state, src, l, dst, &tmp_dlen);
942b0104773SPascal Brand 		if (res != TEE_SUCCESS)
943b0104773SPascal Brand 			TEE_Panic(res);
944b0104773SPascal Brand 		src += l;
945b0104773SPascal Brand 		slen -= l;
946b0104773SPascal Brand 		dst += tmp_dlen;
947b0104773SPascal Brand 		dlen -= tmp_dlen;
948b0104773SPascal Brand 		acc_dlen += tmp_dlen;
949b0104773SPascal Brand 	}
950b0104773SPascal Brand 
951b0104773SPascal Brand 	/* Slen is small enough to be contained in buffer. */
952b0104773SPascal Brand 	memcpy(op->buffer + op->buffer_offs, src, slen);
953b0104773SPascal Brand 	op->buffer_offs += slen;
954b0104773SPascal Brand 
955b0104773SPascal Brand out:
956b0104773SPascal Brand 	*dest_len = acc_dlen;
957b0104773SPascal Brand 	return TEE_SUCCESS;
958b0104773SPascal Brand }
959b0104773SPascal Brand 
9608f07fe6fSJerome Forissier TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData,
96179a3c601SCedric Chaumont 			    uint32_t srcLen, void *destData, uint32_t *destLen)
962b0104773SPascal Brand {
963dea1f2b6SCedric Chaumont 	TEE_Result res;
964b0104773SPascal Brand 	size_t req_dlen;
965e86f1266SJens Wiklander 	uint64_t dl;
966b0104773SPascal Brand 
967642a1607SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
968dea1f2b6SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
969dea1f2b6SCedric Chaumont 	    destLen == NULL ||
970dea1f2b6SCedric Chaumont 	    (destData == NULL && *destLen != 0)) {
971dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
972dea1f2b6SCedric Chaumont 		goto out;
973dea1f2b6SCedric Chaumont 	}
974dea1f2b6SCedric Chaumont 
975642a1607SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
976dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
977dea1f2b6SCedric Chaumont 		goto out;
978dea1f2b6SCedric Chaumont 	}
979dea1f2b6SCedric Chaumont 
980642a1607SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
981642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
982642a1607SCedric Chaumont 		goto out;
983642a1607SCedric Chaumont 	}
984642a1607SCedric Chaumont 
985642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
986dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
987dea1f2b6SCedric Chaumont 		goto out;
988dea1f2b6SCedric Chaumont 	}
989b0104773SPascal Brand 
990e32c5ddfSJerome Forissier 	if (!srcData && !srcLen) {
991090268f5SJens Wiklander 		*destLen = 0;
992e32c5ddfSJerome Forissier 		res = TEE_SUCCESS;
993e32c5ddfSJerome Forissier 		goto out;
994e32c5ddfSJerome Forissier 	}
995e32c5ddfSJerome Forissier 
996b0104773SPascal Brand 	/* Calculate required dlen */
99757aabac5SBogdan Liulko 	if (operation->block_size > 1) {
99857aabac5SBogdan Liulko 		req_dlen = ((operation->buffer_offs + srcLen) /
99957aabac5SBogdan Liulko 			    operation->block_size) * operation->block_size;
100057aabac5SBogdan Liulko 	} else {
100157aabac5SBogdan Liulko 		req_dlen = srcLen;
100257aabac5SBogdan Liulko 	}
1003642a1607SCedric Chaumont 	if (operation->buffer_two_blocks) {
1004642a1607SCedric Chaumont 		if (req_dlen > operation->block_size * 2)
1005642a1607SCedric Chaumont 			req_dlen -= operation->block_size * 2;
1006b0104773SPascal Brand 		else
1007b0104773SPascal Brand 			req_dlen = 0;
1008b0104773SPascal Brand 	}
1009b0104773SPascal Brand 	/*
1010b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1011b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1012b0104773SPascal Brand 	 * can't restore sync with this API.
1013b0104773SPascal Brand 	 */
1014b0104773SPascal Brand 	if (*destLen < req_dlen) {
1015b0104773SPascal Brand 		*destLen = req_dlen;
1016dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1017dea1f2b6SCedric Chaumont 		goto out;
1018b0104773SPascal Brand 	}
1019b0104773SPascal Brand 
1020e86f1266SJens Wiklander 	dl = *destLen;
102157aabac5SBogdan Liulko 	if (operation->block_size > 1) {
102257aabac5SBogdan Liulko 		res = tee_buffer_update(operation, utee_cipher_update, srcData,
102357aabac5SBogdan Liulko 					srcLen, destData, &dl);
102457aabac5SBogdan Liulko 	} else {
102557aabac5SBogdan Liulko 		if (srcLen > 0) {
102657aabac5SBogdan Liulko 			res = utee_cipher_update(operation->state, srcData,
102757aabac5SBogdan Liulko 						 srcLen, destData, &dl);
102857aabac5SBogdan Liulko 		} else {
102957aabac5SBogdan Liulko 			res = TEE_SUCCESS;
103057aabac5SBogdan Liulko 			dl = 0;
103157aabac5SBogdan Liulko 		}
103257aabac5SBogdan Liulko 	}
1033e86f1266SJens Wiklander 	*destLen = dl;
1034b0104773SPascal Brand 
1035dea1f2b6SCedric Chaumont out:
1036dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1037dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1038b36311adSJerome Forissier 		TEE_Panic(res);
1039dea1f2b6SCedric Chaumont 
1040dea1f2b6SCedric Chaumont 	return res;
1041b0104773SPascal Brand }
1042b0104773SPascal Brand 
1043642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation,
10448f07fe6fSJerome Forissier 			     const void *srcData, uint32_t srcLen,
10458f07fe6fSJerome Forissier 			     void *destData, uint32_t *destLen)
1046b0104773SPascal Brand {
1047b0104773SPascal Brand 	TEE_Result res;
1048b0104773SPascal Brand 	uint8_t *dst = destData;
1049b0104773SPascal Brand 	size_t acc_dlen = 0;
1050e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1051b0104773SPascal Brand 	size_t req_dlen;
1052b0104773SPascal Brand 
1053642a1607SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1054dea1f2b6SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
1055dea1f2b6SCedric Chaumont 	    destLen == NULL ||
1056dea1f2b6SCedric Chaumont 	    (destData == NULL && *destLen != 0)) {
1057dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1058dea1f2b6SCedric Chaumont 		goto out;
1059dea1f2b6SCedric Chaumont 	}
1060dea1f2b6SCedric Chaumont 
1061642a1607SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1062dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1063dea1f2b6SCedric Chaumont 		goto out;
1064dea1f2b6SCedric Chaumont 	}
1065dea1f2b6SCedric Chaumont 
1066642a1607SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1067642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1068642a1607SCedric Chaumont 		goto out;
1069642a1607SCedric Chaumont 	}
1070642a1607SCedric Chaumont 
1071642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1072dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1073dea1f2b6SCedric Chaumont 		goto out;
1074dea1f2b6SCedric Chaumont 	}
1075b0104773SPascal Brand 
1076b0104773SPascal Brand 	/*
1077b0104773SPascal Brand 	 * Check that the final block doesn't require padding for those
1078b0104773SPascal Brand 	 * algorithms that requires client to supply padding.
1079b0104773SPascal Brand 	 */
1080642a1607SCedric Chaumont 	if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
1081642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1082642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
1083642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1084642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
1085ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD ||
1086ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD ||
1087ade6f848SJerome Forissier 	    operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) {
1088642a1607SCedric Chaumont 		if (((operation->buffer_offs + srcLen) % operation->block_size)
1089642a1607SCedric Chaumont 		    != 0) {
1090dea1f2b6SCedric Chaumont 			res = TEE_ERROR_BAD_PARAMETERS;
1091dea1f2b6SCedric Chaumont 			goto out;
1092dea1f2b6SCedric Chaumont 		}
1093b0104773SPascal Brand 	}
1094b0104773SPascal Brand 
1095b0104773SPascal Brand 	/*
1096b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1097b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1098b0104773SPascal Brand 	 * can't restore sync with this API.
1099b0104773SPascal Brand 	 */
110057aabac5SBogdan Liulko 	if (operation->block_size > 1) {
1101642a1607SCedric Chaumont 		req_dlen = operation->buffer_offs + srcLen;
110257aabac5SBogdan Liulko 	} else {
110357aabac5SBogdan Liulko 		req_dlen = srcLen;
110457aabac5SBogdan Liulko 	}
1105b0104773SPascal Brand 	if (*destLen < req_dlen) {
1106b0104773SPascal Brand 		*destLen = req_dlen;
1107dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1108dea1f2b6SCedric Chaumont 		goto out;
1109b0104773SPascal Brand 	}
1110b0104773SPascal Brand 
1111b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
111257aabac5SBogdan Liulko 	if (operation->block_size > 1) {
111357aabac5SBogdan Liulko 		res = tee_buffer_update(operation, utee_cipher_update,
111457aabac5SBogdan Liulko 					srcData, srcLen, dst, &tmp_dlen);
1115dea1f2b6SCedric Chaumont 		if (res != TEE_SUCCESS)
1116dea1f2b6SCedric Chaumont 			goto out;
1117dea1f2b6SCedric Chaumont 
1118b0104773SPascal Brand 		dst += tmp_dlen;
1119b0104773SPascal Brand 		acc_dlen += tmp_dlen;
1120b0104773SPascal Brand 
1121b0104773SPascal Brand 		tmp_dlen = *destLen - acc_dlen;
1122642a1607SCedric Chaumont 		res = utee_cipher_final(operation->state, operation->buffer,
1123642a1607SCedric Chaumont 					operation->buffer_offs, dst, &tmp_dlen);
112457aabac5SBogdan Liulko 	} else {
112557aabac5SBogdan Liulko 		res = utee_cipher_final(operation->state, srcData,
112657aabac5SBogdan Liulko 					srcLen, dst, &tmp_dlen);
112757aabac5SBogdan Liulko 	}
1128b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1129dea1f2b6SCedric Chaumont 		goto out;
1130dea1f2b6SCedric Chaumont 
1131b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1132b0104773SPascal Brand 	*destLen = acc_dlen;
1133dea1f2b6SCedric Chaumont 
1134642a1607SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1135642a1607SCedric Chaumont 
1136642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1137642a1607SCedric Chaumont 
1138dea1f2b6SCedric Chaumont out:
1139dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1140dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1141b36311adSJerome Forissier 		TEE_Panic(res);
1142dea1f2b6SCedric Chaumont 
1143dea1f2b6SCedric Chaumont 	return res;
1144b0104773SPascal Brand }
1145b0104773SPascal Brand 
1146b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */
1147b0104773SPascal Brand 
11488f07fe6fSJerome Forissier void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
1149b0104773SPascal Brand {
1150b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
1151b0104773SPascal Brand 		TEE_Panic(0);
1152642a1607SCedric Chaumont 
1153b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1154b0104773SPascal Brand 		TEE_Panic(0);
1155642a1607SCedric Chaumont 
1156642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1157642a1607SCedric Chaumont 	    !(operation->key1))
1158642a1607SCedric Chaumont 		TEE_Panic(0);
1159642a1607SCedric Chaumont 
1160642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1161642a1607SCedric Chaumont 		TEE_ResetOperation(operation);
1162642a1607SCedric Chaumont 
1163642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1164642a1607SCedric Chaumont 
11656d15db08SJerome Forissier 	init_hash_operation(operation, IV, IVLen);
1166b0104773SPascal Brand }
1167b0104773SPascal Brand 
11688f07fe6fSJerome Forissier void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk,
116928e0efc6SCedric Chaumont 		   uint32_t chunkSize)
1170b0104773SPascal Brand {
1171b0104773SPascal Brand 	TEE_Result res;
1172b0104773SPascal Brand 
117328e0efc6SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
1174b0104773SPascal Brand 		TEE_Panic(0);
1175642a1607SCedric Chaumont 
117628e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1177b0104773SPascal Brand 		TEE_Panic(0);
1178642a1607SCedric Chaumont 
117928e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1180b0104773SPascal Brand 		TEE_Panic(0);
1181b0104773SPascal Brand 
1182642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE)
1183642a1607SCedric Chaumont 		TEE_Panic(0);
1184642a1607SCedric Chaumont 
118528e0efc6SCedric Chaumont 	res = utee_hash_update(operation->state, chunk, chunkSize);
1186b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1187b0104773SPascal Brand 		TEE_Panic(res);
1188b0104773SPascal Brand }
1189b0104773SPascal Brand 
119028e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
11918f07fe6fSJerome Forissier 			       const void *message, uint32_t messageLen,
119279a3c601SCedric Chaumont 			       void *mac, uint32_t *macLen)
1193b0104773SPascal Brand {
1194b0104773SPascal Brand 	TEE_Result res;
1195e86f1266SJens Wiklander 	uint64_t ml;
1196b0104773SPascal Brand 
119728e0efc6SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
119828e0efc6SCedric Chaumont 	    (message == NULL && messageLen != 0) ||
119928e0efc6SCedric Chaumont 	    mac == NULL ||
120028e0efc6SCedric Chaumont 	    macLen == NULL) {
120128e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
120228e0efc6SCedric Chaumont 		goto out;
120328e0efc6SCedric Chaumont 	}
1204b0104773SPascal Brand 
120528e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
120628e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
120728e0efc6SCedric Chaumont 		goto out;
120828e0efc6SCedric Chaumont 	}
120928e0efc6SCedric Chaumont 
121028e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
121128e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
121228e0efc6SCedric Chaumont 		goto out;
121328e0efc6SCedric Chaumont 	}
121428e0efc6SCedric Chaumont 
1215642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1216642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1217642a1607SCedric Chaumont 		goto out;
1218642a1607SCedric Chaumont 	}
1219642a1607SCedric Chaumont 
1220e86f1266SJens Wiklander 	ml = *macLen;
1221e86f1266SJens Wiklander 	res = utee_hash_final(operation->state, message, messageLen, mac, &ml);
1222e86f1266SJens Wiklander 	*macLen = ml;
122328e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS)
122428e0efc6SCedric Chaumont 		goto out;
122528e0efc6SCedric Chaumont 
122628e0efc6SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
122728e0efc6SCedric Chaumont 
1228642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1229642a1607SCedric Chaumont 
123028e0efc6SCedric Chaumont out:
123128e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
123228e0efc6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
123328e0efc6SCedric Chaumont 		TEE_Panic(res);
123428e0efc6SCedric Chaumont 
1235b0104773SPascal Brand 	return res;
1236b0104773SPascal Brand }
1237b0104773SPascal Brand 
1238b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
12398f07fe6fSJerome Forissier 			       const void *message, uint32_t messageLen,
12408f07fe6fSJerome Forissier 			       const void *mac, uint32_t macLen)
1241b0104773SPascal Brand {
1242b0104773SPascal Brand 	TEE_Result res;
1243b0104773SPascal Brand 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
12447f74c64aSPascal Brand 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1245b0104773SPascal Brand 
124628e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
124728e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
124828e0efc6SCedric Chaumont 		goto out;
124928e0efc6SCedric Chaumont 	}
125028e0efc6SCedric Chaumont 
125128e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
125228e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
125328e0efc6SCedric Chaumont 		goto out;
125428e0efc6SCedric Chaumont 	}
125528e0efc6SCedric Chaumont 
1256642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1257642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1258642a1607SCedric Chaumont 		goto out;
1259642a1607SCedric Chaumont 	}
1260642a1607SCedric Chaumont 
1261b0104773SPascal Brand 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1262b0104773SPascal Brand 				  &computed_mac_size);
1263b0104773SPascal Brand 	if (res != TEE_SUCCESS)
126428e0efc6SCedric Chaumont 		goto out;
126528e0efc6SCedric Chaumont 
126628e0efc6SCedric Chaumont 	if (computed_mac_size != macLen) {
126728e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
126828e0efc6SCedric Chaumont 		goto out;
126928e0efc6SCedric Chaumont 	}
127028e0efc6SCedric Chaumont 
127148e10604SJerome Forissier 	if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) {
127228e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
127328e0efc6SCedric Chaumont 		goto out;
127428e0efc6SCedric Chaumont 	}
127528e0efc6SCedric Chaumont 
1276642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1277642a1607SCedric Chaumont 
127828e0efc6SCedric Chaumont out:
127928e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
128028e0efc6SCedric Chaumont 	    res != TEE_ERROR_MAC_INVALID)
128128e0efc6SCedric Chaumont 		TEE_Panic(res);
128228e0efc6SCedric Chaumont 
1283b0104773SPascal Brand 	return res;
1284b0104773SPascal Brand }
1285b0104773SPascal Brand 
1286b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */
1287b0104773SPascal Brand 
12888f07fe6fSJerome Forissier TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce,
128979a3c601SCedric Chaumont 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1290b0104773SPascal Brand 		      uint32_t payloadLen)
1291b0104773SPascal Brand {
1292b0104773SPascal Brand 	TEE_Result res;
1293b0104773SPascal Brand 
1294b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || nonce == NULL) {
1295b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1296b5816c88SCedric Chaumont 		goto out;
1297b5816c88SCedric Chaumont 	}
1298b5816c88SCedric Chaumont 
1299b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1300b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1301b5816c88SCedric Chaumont 		goto out;
1302b5816c88SCedric Chaumont 	}
1303b0104773SPascal Brand 
1304642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
1305642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1306642a1607SCedric Chaumont 		goto out;
1307642a1607SCedric Chaumont 	}
1308642a1607SCedric Chaumont 
1309b0104773SPascal Brand 	/*
1310b0104773SPascal Brand 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1311b0104773SPascal Brand 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1312b0104773SPascal Brand 	 * according to the same principle so we have to check here instead to
1313b0104773SPascal Brand 	 * be GP compliant.
1314b0104773SPascal Brand 	 */
1315b5816c88SCedric Chaumont 	if (operation->info.algorithm == TEE_ALG_AES_GCM) {
1316b0104773SPascal Brand 		/*
1317b0104773SPascal Brand 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1318b0104773SPascal Brand 		 */
1319b5816c88SCedric Chaumont 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) {
1320b5816c88SCedric Chaumont 			res = TEE_ERROR_NOT_SUPPORTED;
1321b5816c88SCedric Chaumont 			goto out;
1322b5816c88SCedric Chaumont 		}
1323b0104773SPascal Brand 	}
1324b0104773SPascal Brand 
1325b5816c88SCedric Chaumont 	res = utee_authenc_init(operation->state, nonce, nonceLen,
1326b5816c88SCedric Chaumont 				tagLen / 8, AADLen, payloadLen);
1327b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1328b5816c88SCedric Chaumont 		goto out;
1329b5816c88SCedric Chaumont 
1330b5816c88SCedric Chaumont 	operation->ae_tag_len = tagLen / 8;
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 
1356b5816c88SCedric Chaumont 	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) {
1415afc0c182SBogdan Liulko 		res = tee_buffer_update(operation, utee_authenc_update_payload,
1416afc0c182SBogdan Liulko 					srcData, srcLen, destData, &dl);
1417afc0c182SBogdan Liulko 	} else {
1418afc0c182SBogdan Liulko 		if (srcLen > 0) {
1419afc0c182SBogdan Liulko 			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 
1489b5816c88SCedric Chaumont 	if (*tagLen < operation->ae_tag_len) {
1490b5816c88SCedric Chaumont 		*tagLen = operation->ae_tag_len;
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) {
1500afc0c182SBogdan Liulko 		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;
1509afc0c182SBogdan Liulko 		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 {
1514afc0c182SBogdan Liulko 		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) {
1581afc0c182SBogdan Liulko 		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;
1590afc0c182SBogdan Liulko 		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 {
1595afc0c182SBogdan Liulko 		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 */
1603b5816c88SCedric Chaumont 	if (tagLen != operation->ae_tag_len)
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;
1648e86f1266SJens Wiklander 	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;
1684e86f1266SJens Wiklander 	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;
1722e86f1266SJens Wiklander 	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);
1757e86f1266SJens Wiklander 	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 
1793e86f1266SJens Wiklander 	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);
1803e86f1266SJens Wiklander 	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 
1815b0104773SPascal Brand 	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 }
1832