xref: /optee_os/lib/libutee/tee_api_operations.c (revision 28e0efc6ef6a986f3a6c28e1d6fb02e6747e8443)
1b0104773SPascal Brand /*
2b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
3b0104773SPascal Brand  * All rights reserved.
4b0104773SPascal Brand  *
5b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
6b0104773SPascal Brand  * modification, are permitted provided that the following conditions are met:
7b0104773SPascal Brand  *
8b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
9b0104773SPascal Brand  * this list of conditions and the following disclaimer.
10b0104773SPascal Brand  *
11b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
12b0104773SPascal Brand  * this list of conditions and the following disclaimer in the documentation
13b0104773SPascal Brand  * and/or other materials provided with the distribution.
14b0104773SPascal Brand  *
15b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16b0104773SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b0104773SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b0104773SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19b0104773SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20b0104773SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21b0104773SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22b0104773SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23b0104773SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24b0104773SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25b0104773SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
26b0104773SPascal Brand  */
27b0104773SPascal Brand #include <stdlib.h>
28b0104773SPascal Brand #include <string.h>
29b796ebf3SJerome Forissier #include <string_ext.h>
30b0104773SPascal Brand 
31b0104773SPascal Brand #include <tee_api.h>
328854d3c6SJerome Forissier #include <tee_api_defines_extensions.h>
33b0104773SPascal Brand #include <tee_internal_api_extensions.h>
34b0104773SPascal Brand #include <utee_syscalls.h>
35b0104773SPascal Brand #include <utee_defines.h>
36fc26c92aSJens Wiklander #include <util.h>
37b0104773SPascal Brand 
38b0104773SPascal Brand struct __TEE_OperationHandle {
39b0104773SPascal Brand 	TEE_OperationInfo info;
40b0104773SPascal Brand 	TEE_ObjectHandle key1;
41b0104773SPascal Brand 	TEE_ObjectHandle key2;
42b0104773SPascal Brand 	uint8_t *buffer;	/* buffer to collect complete blocks */
43b0104773SPascal Brand 	bool buffer_two_blocks;	/* True if two blocks need to be buffered */
44b0104773SPascal Brand 	size_t block_size;	/* Block size of cipher */
45b0104773SPascal Brand 	size_t buffer_offs;	/* Offset in buffer */
46b0104773SPascal Brand 	uint32_t state;		/* Handle to state in TEE Core */
47b0104773SPascal Brand 	uint32_t ae_tag_len;	/*
48b0104773SPascal Brand 				 * tag_len in bytes for AE operation else unused
49b0104773SPascal Brand 				 */
50b0104773SPascal Brand };
51b0104773SPascal Brand 
52b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */
53b0104773SPascal Brand 
54b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation,
55b0104773SPascal Brand 				 uint32_t algorithm, uint32_t mode,
56b0104773SPascal Brand 				 uint32_t maxKeySize)
57b0104773SPascal Brand {
58b0104773SPascal Brand 	TEE_Result res;
59b0104773SPascal Brand 	TEE_OperationHandle op = TEE_HANDLE_NULL;
60b0104773SPascal Brand 	uint32_t handle_state = 0;
61b0104773SPascal Brand 	size_t block_size = 1;
62b0104773SPascal Brand 	uint32_t req_key_usage;
63b0104773SPascal Brand 	bool with_private_key = false;
64b0104773SPascal Brand 	bool buffer_two_blocks = false;
65b0104773SPascal Brand 
669b52c538SCedric Chaumont 	if (!operation)
67b0104773SPascal Brand 		TEE_Panic(0);
68b0104773SPascal Brand 
69b0104773SPascal Brand 	if (algorithm == TEE_ALG_AES_XTS)
70b0104773SPascal Brand 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
71b0104773SPascal Brand 
72218d9055SCedric Chaumont 	/* Check algorithm max key size */
73218d9055SCedric Chaumont 	switch (algorithm) {
74218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA1:
75218d9055SCedric Chaumont 		if (maxKeySize < 512)
76218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
77218d9055SCedric Chaumont 		if (maxKeySize > 1024)
78218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
79218d9055SCedric Chaumont 		if (maxKeySize % 64 != 0)
80218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
81218d9055SCedric Chaumont 		break;
82218d9055SCedric Chaumont 
83218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA224:
84218d9055SCedric Chaumont 		if (maxKeySize != 2048)
85218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
86218d9055SCedric Chaumont 		break;
87218d9055SCedric Chaumont 
88218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA256:
89218d9055SCedric Chaumont 		if (maxKeySize != 2048 && maxKeySize != 3072)
90218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
91218d9055SCedric Chaumont 		break;
92218d9055SCedric Chaumont 
931220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P192:
941220586eSCedric Chaumont 	case TEE_ALG_ECDH_P192:
951220586eSCedric Chaumont 		if (maxKeySize != 192)
961220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
971220586eSCedric Chaumont 		break;
981220586eSCedric Chaumont 
991220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P224:
1001220586eSCedric Chaumont 	case TEE_ALG_ECDH_P224:
1011220586eSCedric Chaumont 		if (maxKeySize != 224)
1021220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1031220586eSCedric Chaumont 		break;
1041220586eSCedric Chaumont 
1051220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P256:
1061220586eSCedric Chaumont 	case TEE_ALG_ECDH_P256:
1071220586eSCedric Chaumont 		if (maxKeySize != 256)
1081220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1091220586eSCedric Chaumont 		break;
1101220586eSCedric Chaumont 
1111220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P384:
1121220586eSCedric Chaumont 	case TEE_ALG_ECDH_P384:
1131220586eSCedric Chaumont 		if (maxKeySize != 384)
1141220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1151220586eSCedric Chaumont 		break;
1161220586eSCedric Chaumont 
1171220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P521:
1181220586eSCedric Chaumont 	case TEE_ALG_ECDH_P521:
1191220586eSCedric Chaumont 		if (maxKeySize != 521)
1201220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1211220586eSCedric Chaumont 		break;
1221220586eSCedric Chaumont 
123218d9055SCedric Chaumont 	default:
124218d9055SCedric Chaumont 		break;
125218d9055SCedric Chaumont 	}
126218d9055SCedric Chaumont 
127218d9055SCedric Chaumont 	/* Check algorithm mode */
128b0104773SPascal Brand 	switch (algorithm) {
129b0104773SPascal Brand 	case TEE_ALG_AES_CTS:
130b0104773SPascal Brand 	case TEE_ALG_AES_XTS:
131b0104773SPascal Brand 		buffer_two_blocks = true;
132b0104773SPascal Brand 	 /*FALLTHROUGH*/ case TEE_ALG_AES_ECB_NOPAD:
133b0104773SPascal Brand 	case TEE_ALG_AES_CBC_NOPAD:
134b0104773SPascal Brand 	case TEE_ALG_AES_CTR:
135b0104773SPascal Brand 	case TEE_ALG_AES_CCM:
136b0104773SPascal Brand 	case TEE_ALG_AES_GCM:
137b0104773SPascal Brand 	case TEE_ALG_DES_ECB_NOPAD:
138b0104773SPascal Brand 	case TEE_ALG_DES_CBC_NOPAD:
139b0104773SPascal Brand 	case TEE_ALG_DES3_ECB_NOPAD:
140b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_NOPAD:
141b0104773SPascal Brand 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
142b0104773SPascal Brand 			block_size = TEE_AES_BLOCK_SIZE;
143b0104773SPascal Brand 		else
144b0104773SPascal Brand 			block_size = TEE_DES_BLOCK_SIZE;
145b0104773SPascal Brand 
146b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT)
147b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
148b0104773SPascal Brand 		else if (mode == TEE_MODE_DECRYPT)
149b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
150b0104773SPascal Brand 		else
151b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
152b0104773SPascal Brand 		break;
153b0104773SPascal Brand 
154b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
155b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
156b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
157b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
158b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
159b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
160b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
161b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
162b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
163b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
164b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
165b0104773SPascal Brand 	case TEE_ALG_DSA_SHA1:
166218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA224:
167218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA256:
1681220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P192:
1691220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P224:
1701220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P256:
1711220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P384:
1721220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P521:
173b0104773SPascal Brand 		if (mode == TEE_MODE_SIGN) {
174b0104773SPascal Brand 			with_private_key = true;
175b0104773SPascal Brand 			req_key_usage = TEE_USAGE_SIGN;
176b0104773SPascal Brand 		} else if (mode == TEE_MODE_VERIFY) {
177b0104773SPascal Brand 			req_key_usage = TEE_USAGE_VERIFY;
178b0104773SPascal Brand 		} else {
179b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
180b0104773SPascal Brand 		}
181b0104773SPascal Brand 		break;
182b0104773SPascal Brand 
183b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_V1_5:
184b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
185b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
186b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
187b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
188b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
189b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
190b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
191b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
192b0104773SPascal Brand 			with_private_key = true;
193b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
194b0104773SPascal Brand 		} else {
195b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
196b0104773SPascal Brand 		}
197b0104773SPascal Brand 		break;
198b0104773SPascal Brand 
199b0104773SPascal Brand 	case TEE_ALG_RSA_NOPAD:
200b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
201b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
202b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
203b0104773SPascal Brand 			with_private_key = true;
204b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
205b0104773SPascal Brand 		} else {
206b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
207b0104773SPascal Brand 		}
208b0104773SPascal Brand 		break;
209b0104773SPascal Brand 
210b0104773SPascal Brand 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
2111220586eSCedric Chaumont 	case TEE_ALG_ECDH_P192:
2121220586eSCedric Chaumont 	case TEE_ALG_ECDH_P224:
2131220586eSCedric Chaumont 	case TEE_ALG_ECDH_P256:
2141220586eSCedric Chaumont 	case TEE_ALG_ECDH_P384:
2151220586eSCedric Chaumont 	case TEE_ALG_ECDH_P521:
216cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_MD5_DERIVE_KEY:
217cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA1_DERIVE_KEY:
218cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA224_DERIVE_KEY:
219cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA256_DERIVE_KEY:
220cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA384_DERIVE_KEY:
221cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA512_DERIVE_KEY:
2228854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY:
2238854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY:
2248854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY:
2258854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY:
2268854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY:
2270f2293b7SJerome Forissier 	case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY:
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:
240b0104773SPascal Brand 		if (mode != TEE_MODE_DIGEST)
241b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
242b0104773SPascal Brand 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
243b0104773SPascal Brand 		req_key_usage = 0;
244b0104773SPascal Brand 		break;
245b0104773SPascal Brand 
246b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_NOPAD:
247b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_NOPAD:
248b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_PKCS5:
249b0104773SPascal Brand 	case TEE_ALG_AES_CMAC:
250b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_PKCS5:
251b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
252b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
253b0104773SPascal Brand 	case TEE_ALG_HMAC_MD5:
254b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA1:
255b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA224:
256b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA256:
257b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA384:
258b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA512:
259b0104773SPascal Brand 		if (mode != TEE_MODE_MAC)
260b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
261b0104773SPascal Brand 		req_key_usage = TEE_USAGE_MAC;
262b0104773SPascal Brand 		break;
263b0104773SPascal Brand 
264b0104773SPascal Brand 	default:
265b0104773SPascal Brand 		return TEE_ERROR_NOT_SUPPORTED;
266b0104773SPascal Brand 	}
267b0104773SPascal Brand 
268b0104773SPascal Brand 	op = TEE_Malloc(sizeof(*op), 0);
2699b52c538SCedric Chaumont 	if (!op)
270b0104773SPascal Brand 		return TEE_ERROR_OUT_OF_MEMORY;
271b0104773SPascal Brand 
272b0104773SPascal Brand 	op->info.algorithm = algorithm;
273b0104773SPascal Brand 	op->info.operationClass = TEE_ALG_GET_CLASS(algorithm);
274b0104773SPascal Brand 	op->info.mode = mode;
275b0104773SPascal Brand 	op->info.maxKeySize = maxKeySize;
276b0104773SPascal Brand 	op->info.requiredKeyUsage = req_key_usage;
277b0104773SPascal Brand 	op->info.handleState = handle_state;
278b0104773SPascal Brand 
279b0104773SPascal Brand 	if (block_size > 1) {
280b0104773SPascal Brand 		size_t buffer_size = block_size;
281b0104773SPascal Brand 
282b0104773SPascal Brand 		if (buffer_two_blocks)
283b0104773SPascal Brand 			buffer_size *= 2;
284b0104773SPascal Brand 
2859b52c538SCedric Chaumont 		op->buffer = TEE_Malloc(buffer_size,
2869b52c538SCedric Chaumont 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
287b0104773SPascal Brand 		if (op->buffer == NULL) {
288b0104773SPascal Brand 			res = TEE_ERROR_OUT_OF_MEMORY;
2899b52c538SCedric Chaumont 			goto err0;
290b0104773SPascal Brand 		}
291b0104773SPascal Brand 	}
292b0104773SPascal Brand 	op->block_size = block_size;
293b0104773SPascal Brand 	op->buffer_two_blocks = buffer_two_blocks;
294b0104773SPascal Brand 
295b0104773SPascal Brand 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
296b0104773SPascal Brand 		uint32_t mks = maxKeySize;
297b0104773SPascal Brand 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
298b0104773SPascal Brand 						       with_private_key);
299b0104773SPascal Brand 
300b0104773SPascal Brand 		/*
301b0104773SPascal Brand 		 * If two keys are expected the max key size is the sum of
302b0104773SPascal Brand 		 * the size of both keys.
303b0104773SPascal Brand 		 */
304b0104773SPascal Brand 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
305b0104773SPascal Brand 			mks /= 2;
306b0104773SPascal Brand 
307b0104773SPascal Brand 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
308b0104773SPascal Brand 		if (res != TEE_SUCCESS)
3099b52c538SCedric Chaumont 			goto err1;
310b0104773SPascal Brand 
311b0104773SPascal Brand 		if ((op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
312b0104773SPascal Brand 		    0) {
3139b52c538SCedric Chaumont 			res = TEE_AllocateTransientObject(key_type, mks,
314b0104773SPascal Brand 							  &op->key2);
315b0104773SPascal Brand 			if (res != TEE_SUCCESS)
3169b52c538SCedric Chaumont 				goto err2;
317b0104773SPascal Brand 		}
318b0104773SPascal Brand 	}
319b0104773SPascal Brand 
320b0104773SPascal Brand 	res = utee_cryp_state_alloc(algorithm, mode, (uint32_t) op->key1,
321b0104773SPascal Brand 				    (uint32_t) op->key2, &op->state);
3229b52c538SCedric Chaumont 	if (res != TEE_SUCCESS) {
3239b52c538SCedric Chaumont 		if ((op->info.handleState &
3249b52c538SCedric Chaumont 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 0)
3259b52c538SCedric Chaumont 			goto err2;
3269b52c538SCedric Chaumont 		goto err1;
3279b52c538SCedric Chaumont 	}
328b0104773SPascal Brand 
329b0104773SPascal Brand 	/* For multi-stage operation do an "init". */
330b0104773SPascal Brand 	TEE_ResetOperation(op);
331b0104773SPascal Brand 	*operation = op;
3329b52c538SCedric Chaumont 	goto out;
333b0104773SPascal Brand 
3349b52c538SCedric Chaumont err2:
335b0104773SPascal Brand 	TEE_FreeTransientObject(op->key2);
3369b52c538SCedric Chaumont err1:
3379b52c538SCedric Chaumont 	TEE_FreeTransientObject(op->key1);
3389b52c538SCedric Chaumont err0:
339b0104773SPascal Brand 	TEE_FreeOperation(op);
340b0104773SPascal Brand 
3419b52c538SCedric Chaumont 	if (res != TEE_SUCCESS &&
3429b52c538SCedric Chaumont 	    res != TEE_ERROR_OUT_OF_MEMORY &&
3439b52c538SCedric Chaumont 	    res != TEE_ERROR_NOT_SUPPORTED)
3449b52c538SCedric Chaumont 		TEE_Panic(0);
3459b52c538SCedric Chaumont out:
346b0104773SPascal Brand 	return res;
347b0104773SPascal Brand }
348b0104773SPascal Brand 
349b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation)
350b0104773SPascal Brand {
351e889e80bSCedric Chaumont 	TEE_Result res;
352e889e80bSCedric Chaumont 
353e889e80bSCedric Chaumont 	if (operation == TEE_HANDLE_NULL)
354e889e80bSCedric Chaumont 		TEE_Panic(0);
355e889e80bSCedric Chaumont 
356b0104773SPascal Brand 	/*
357b0104773SPascal Brand 	 * Note that keys should not be freed here, since they are
358b0104773SPascal Brand 	 * claimed by the operation they will be freed by
359b0104773SPascal Brand 	 * utee_cryp_state_free().
360b0104773SPascal Brand 	 */
361e889e80bSCedric Chaumont 	res = utee_cryp_state_free(operation->state);
362e889e80bSCedric Chaumont 	if (res != TEE_SUCCESS)
363e889e80bSCedric Chaumont 		TEE_Panic(0);
364e889e80bSCedric Chaumont 
365b0104773SPascal Brand 	TEE_Free(operation->buffer);
366b0104773SPascal Brand 	TEE_Free(operation);
367b0104773SPascal Brand }
368b0104773SPascal Brand 
369b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation,
370b0104773SPascal Brand 			  TEE_OperationInfo *operationInfo)
371b0104773SPascal Brand {
372b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
373b0104773SPascal Brand 		TEE_Panic(0);
374b0104773SPascal Brand 
375b0104773SPascal Brand 	if (operationInfo == NULL)
376b0104773SPascal Brand 		TEE_Panic(0);
377b0104773SPascal Brand 
378b0104773SPascal Brand 	*operationInfo = operation->info;
379b0104773SPascal Brand }
380b0104773SPascal Brand 
381b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation)
382b0104773SPascal Brand {
383b0104773SPascal Brand 	TEE_Result res;
384b0104773SPascal Brand 
385b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
386b0104773SPascal Brand 		TEE_Panic(0);
387bf80076aSCedric Chaumont 
388bf80076aSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) != 0) {
389bf80076aSCedric Chaumont 		if (operation->info.keySize == 0)
390bf80076aSCedric Chaumont 			TEE_Panic(0);
391bf80076aSCedric Chaumont 	}
392bf80076aSCedric Chaumont 
393b0104773SPascal Brand 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
394b0104773SPascal Brand 		res = utee_hash_init(operation->state, NULL, 0);
395b0104773SPascal Brand 		if (res != TEE_SUCCESS)
396b0104773SPascal Brand 			TEE_Panic(res);
397b0104773SPascal Brand 	}
398b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
399b0104773SPascal Brand }
400b0104773SPascal Brand 
401b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
402b0104773SPascal Brand 			       TEE_ObjectHandle key)
403b0104773SPascal Brand {
4047583c59eSCedric Chaumont 	TEE_Result res;
405b0104773SPascal Brand 	uint32_t key_size = 0;
406b0104773SPascal Brand 	TEE_ObjectInfo key_info;
407b0104773SPascal Brand 
408a57c1e2eSCedric Chaumont 	/* Operation is not a valid handle */
409a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
410a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
411a57c1e2eSCedric Chaumont 		goto out;
412a57c1e2eSCedric Chaumont 	}
413a57c1e2eSCedric Chaumont 
414a57c1e2eSCedric Chaumont 	/* Key is not initialized */
415a57c1e2eSCedric Chaumont 	if (key == TEE_HANDLE_NULL) {
416a57c1e2eSCedric Chaumont 		/* Operation key cleared */
417a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key1);
418a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
419a57c1e2eSCedric Chaumont 		goto out;
420a57c1e2eSCedric Chaumont 	}
421a57c1e2eSCedric Chaumont 
422a57c1e2eSCedric Chaumont 	/* No key for digest operation */
423a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
424a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
425a57c1e2eSCedric Chaumont 		goto out;
426a57c1e2eSCedric Chaumont 	}
427a57c1e2eSCedric Chaumont 
428a57c1e2eSCedric Chaumont 	/* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */
429a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
430a57c1e2eSCedric Chaumont 	    0) {
431a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
432a57c1e2eSCedric Chaumont 		goto out;
433a57c1e2eSCedric Chaumont 	}
434a57c1e2eSCedric Chaumont 
4357583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key, &key_info);
436a57c1e2eSCedric Chaumont 	/* Key is not a valid handle */
4377583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
438a57c1e2eSCedric Chaumont 		goto out;
4397583c59eSCedric Chaumont 
440b0104773SPascal Brand 	/* Supplied key has to meet required usage */
441b0104773SPascal Brand 	if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
442b0104773SPascal Brand 	    operation->info.requiredKeyUsage) {
443a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
444a57c1e2eSCedric Chaumont 		goto out;
445b0104773SPascal Brand 	}
446b0104773SPascal Brand 
447a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info.keySize) {
448a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
449a57c1e2eSCedric Chaumont 		goto out;
450a57c1e2eSCedric Chaumont 	}
451b0104773SPascal Brand 
4527583c59eSCedric Chaumont 	key_size = key_info.keySize;
453b0104773SPascal Brand 
454b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
455b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
456b0104773SPascal Brand 
4577583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key);
4587583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
459a57c1e2eSCedric Chaumont 		goto out;
4607583c59eSCedric Chaumont 
461b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
462b0104773SPascal Brand 
463b0104773SPascal Brand 	operation->info.keySize = key_size;
464b0104773SPascal Brand 
4657583c59eSCedric Chaumont out:
466a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
467a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
468a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
469a57c1e2eSCedric Chaumont 		TEE_Panic(0);
470a57c1e2eSCedric Chaumont 
471a57c1e2eSCedric Chaumont 	return res;
472b0104773SPascal Brand }
473b0104773SPascal Brand 
474b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
475b0104773SPascal Brand 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
476b0104773SPascal Brand {
4777583c59eSCedric Chaumont 	TEE_Result res;
478b0104773SPascal Brand 	uint32_t key_size = 0;
479b0104773SPascal Brand 	TEE_ObjectInfo key_info1;
480b0104773SPascal Brand 	TEE_ObjectInfo key_info2;
481b0104773SPascal Brand 
482a57c1e2eSCedric Chaumont 	/* Operation is not a valid handle */
483a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
484a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
485a57c1e2eSCedric Chaumont 		goto out;
486a57c1e2eSCedric Chaumont 	}
487a57c1e2eSCedric Chaumont 
488a57c1e2eSCedric Chaumont 	/*
489a57c1e2eSCedric Chaumont 	 * Key1/Key2 and/or are not initialized and
490a57c1e2eSCedric Chaumont 	 * Either both keys are NULL or both are not NULL
491a57c1e2eSCedric Chaumont 	 */
492a57c1e2eSCedric Chaumont 	if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) {
493a57c1e2eSCedric Chaumont 		/* Clear operation key1 (if needed) */
494a57c1e2eSCedric Chaumont 		if (key1 == TEE_HANDLE_NULL)
495a57c1e2eSCedric Chaumont 			TEE_ResetTransientObject(operation->key1);
496a57c1e2eSCedric Chaumont 		/* Clear operation key2 (if needed) */
497a57c1e2eSCedric Chaumont 		if (key2 == TEE_HANDLE_NULL)
498a57c1e2eSCedric Chaumont 			TEE_ResetTransientObject(operation->key2);
499a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
500a57c1e2eSCedric Chaumont 		goto out;
501a57c1e2eSCedric Chaumont 	}
502a57c1e2eSCedric Chaumont 
503a57c1e2eSCedric Chaumont 	/* No key for digest operation */
504a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
505a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
506a57c1e2eSCedric Chaumont 		goto out;
507a57c1e2eSCedric Chaumont 	}
508a57c1e2eSCedric Chaumont 
509a57c1e2eSCedric Chaumont 	/* Two keys flag expected (TEE_ALG_AES_XTS only) */
510a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
511a57c1e2eSCedric Chaumont 	    0) {
512a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
513a57c1e2eSCedric Chaumont 		goto out;
514a57c1e2eSCedric Chaumont 	}
515a57c1e2eSCedric Chaumont 
5167583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key1, &key_info1);
517a57c1e2eSCedric Chaumont 	/* Key1 is not a valid handle */
5187583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
519a57c1e2eSCedric Chaumont 		goto out;
5207583c59eSCedric Chaumont 
521b0104773SPascal Brand 	/* Supplied key has to meet required usage */
522b0104773SPascal Brand 	if ((key_info1.objectUsage & operation->info.
523b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
524a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
525a57c1e2eSCedric Chaumont 		goto out;
526b0104773SPascal Brand 	}
527b0104773SPascal Brand 
5287583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key2, &key_info2);
529a57c1e2eSCedric Chaumont 	/* Key2 is not a valid handle */
5307583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
5317583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
5327583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
533a57c1e2eSCedric Chaumont 		goto out;
5347583c59eSCedric Chaumont 	}
5357583c59eSCedric Chaumont 
536b0104773SPascal Brand 	/* Supplied key has to meet required usage */
537b0104773SPascal Brand 	if ((key_info2.objectUsage & operation->info.
538b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
539a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
540a57c1e2eSCedric Chaumont 		goto out;
541b0104773SPascal Brand 	}
542b0104773SPascal Brand 
543b0104773SPascal Brand 	/*
544b0104773SPascal Brand 	 * AES-XTS (the only multi key algorithm supported, requires the
545b0104773SPascal Brand 	 * keys to be of equal size.
546b0104773SPascal Brand 	 */
547b0104773SPascal Brand 	if (operation->info.algorithm == TEE_ALG_AES_XTS &&
548a57c1e2eSCedric Chaumont 	    key_info1.keySize != key_info2.keySize) {
549a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
550a57c1e2eSCedric Chaumont 		goto out;
551b0104773SPascal Brand 
552a57c1e2eSCedric Chaumont 	}
553a57c1e2eSCedric Chaumont 
554a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info1.keySize) {
555a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
556a57c1e2eSCedric Chaumont 		goto out;
557a57c1e2eSCedric Chaumont 	}
558b0104773SPascal Brand 
559b0104773SPascal Brand 	/*
560b0104773SPascal Brand 	 * Odd that only the size of one key should be reported while
561b0104773SPascal Brand 	 * size of two key are used when allocating the operation.
562b0104773SPascal Brand 	 */
5637583c59eSCedric Chaumont 	key_size = key_info1.keySize;
564b0104773SPascal Brand 
565b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
566b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key2);
567b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
568b0104773SPascal Brand 
5697583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key1);
5707583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
571a57c1e2eSCedric Chaumont 		goto out;
5727583c59eSCedric Chaumont 
5737583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key2, key2);
5747583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
5757583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
5767583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
577a57c1e2eSCedric Chaumont 		goto out;
5787583c59eSCedric Chaumont 	}
5797583c59eSCedric Chaumont 
580b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
581b0104773SPascal Brand 
582b0104773SPascal Brand 	operation->info.keySize = key_size;
583b0104773SPascal Brand 
5847583c59eSCedric Chaumont out:
585a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
586a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
587a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT_2 &&
588a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE &&
589a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
590a57c1e2eSCedric Chaumont 		TEE_Panic(0);
591a57c1e2eSCedric Chaumont 
592a57c1e2eSCedric Chaumont 	return res;
593b0104773SPascal Brand }
594b0104773SPascal Brand 
595b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
596b0104773SPascal Brand {
597b0104773SPascal Brand 	TEE_Result res;
598b0104773SPascal Brand 
599b0104773SPascal Brand 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
600b0104773SPascal Brand 		TEE_Panic(0);
601b0104773SPascal Brand 	if (dst_op->info.algorithm != src_op->info.algorithm)
602b0104773SPascal Brand 		TEE_Panic(0);
603b0104773SPascal Brand 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
604b0104773SPascal Brand 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
605b0104773SPascal Brand 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
606b0104773SPascal Brand 
607b0104773SPascal Brand 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
608b0104773SPascal Brand 			key1 = src_op->key1;
609b0104773SPascal Brand 			key2 = src_op->key2;
610b0104773SPascal Brand 		}
611b0104773SPascal Brand 
612b0104773SPascal Brand 		if ((src_op->info.handleState &
613b0104773SPascal Brand 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
614b0104773SPascal Brand 			TEE_SetOperationKey(dst_op, key1);
615b0104773SPascal Brand 		} else {
616b0104773SPascal Brand 			TEE_SetOperationKey2(dst_op, key1, key2);
617b0104773SPascal Brand 		}
618b0104773SPascal Brand 	}
619b0104773SPascal Brand 	dst_op->info.handleState = src_op->info.handleState;
620b0104773SPascal Brand 	dst_op->info.keySize = src_op->info.keySize;
621b0104773SPascal Brand 
622b0104773SPascal Brand 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
623b0104773SPascal Brand 	    dst_op->block_size != src_op->block_size)
624b0104773SPascal Brand 		TEE_Panic(0);
625b0104773SPascal Brand 
626b0104773SPascal Brand 	if (dst_op->buffer != NULL) {
627b0104773SPascal Brand 		if (src_op->buffer == NULL)
628b0104773SPascal Brand 			TEE_Panic(0);
629b0104773SPascal Brand 
630b0104773SPascal Brand 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
631b0104773SPascal Brand 		dst_op->buffer_offs = src_op->buffer_offs;
632b0104773SPascal Brand 	} else if (src_op->buffer != NULL) {
633b0104773SPascal Brand 		TEE_Panic(0);
634b0104773SPascal Brand 	}
635b0104773SPascal Brand 
636b0104773SPascal Brand 	res = utee_cryp_state_copy(dst_op->state, src_op->state);
637b0104773SPascal Brand 	if (res != TEE_SUCCESS)
638b0104773SPascal Brand 		TEE_Panic(res);
639b0104773SPascal Brand }
640b0104773SPascal Brand 
641b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */
642b0104773SPascal Brand 
643b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation,
64479a3c601SCedric Chaumont 		      void *chunk, uint32_t chunkSize)
645b0104773SPascal Brand {
64673d6c3baSJoakim Bech 	TEE_Result res = TEE_ERROR_GENERIC;
647b0104773SPascal Brand 
64873d6c3baSJoakim Bech 	if (operation == TEE_HANDLE_NULL ||
64973d6c3baSJoakim Bech 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
650b0104773SPascal Brand 		TEE_Panic(0);
65173d6c3baSJoakim Bech 
652b0104773SPascal Brand 	res = utee_hash_update(operation->state, chunk, chunkSize);
653b0104773SPascal Brand 	if (res != TEE_SUCCESS)
654b0104773SPascal Brand 		TEE_Panic(res);
655b0104773SPascal Brand }
656b0104773SPascal Brand 
657b0104773SPascal Brand TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
65879a3c601SCedric Chaumont 			     uint32_t chunkLen, void *hash, uint32_t *hashLen)
659b0104773SPascal Brand {
66087c2f6b6SCedric Chaumont 	TEE_Result res;
66187c2f6b6SCedric Chaumont 
66287c2f6b6SCedric Chaumont 	if ((operation == TEE_HANDLE_NULL) ||
66387c2f6b6SCedric Chaumont 	    (!chunk && chunkLen) ||
66487c2f6b6SCedric Chaumont 	    !hash ||
66587c2f6b6SCedric Chaumont 	    !hashLen ||
66687c2f6b6SCedric Chaumont 	    (operation->info.operationClass != TEE_OPERATION_DIGEST)) {
66787c2f6b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
66887c2f6b6SCedric Chaumont 		goto out;
66987c2f6b6SCedric Chaumont 	}
67087c2f6b6SCedric Chaumont 
67187c2f6b6SCedric Chaumont 	res = utee_hash_final(operation->state, chunk, chunkLen, hash,
67287c2f6b6SCedric Chaumont 			       hashLen);
67387c2f6b6SCedric Chaumont 
67487c2f6b6SCedric Chaumont out:
67587c2f6b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
67687c2f6b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
677b0104773SPascal Brand 		TEE_Panic(0);
67873d6c3baSJoakim Bech 
67987c2f6b6SCedric Chaumont 	return res;
680b0104773SPascal Brand }
681b0104773SPascal Brand 
682b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */
683b0104773SPascal Brand 
68479a3c601SCedric Chaumont void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
685b0104773SPascal Brand {
686b0104773SPascal Brand 	TEE_Result res;
687b0104773SPascal Brand 
688b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
689b0104773SPascal Brand 		TEE_Panic(0);
690b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
691b0104773SPascal Brand 		TEE_Panic(0);
692b0104773SPascal Brand 	res = utee_cipher_init(operation->state, IV, IVLen);
693b0104773SPascal Brand 	if (res != TEE_SUCCESS)
694b0104773SPascal Brand 		TEE_Panic(res);
695b0104773SPascal Brand 	operation->buffer_offs = 0;
696b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
697b0104773SPascal Brand }
698b0104773SPascal Brand 
699b0104773SPascal Brand static TEE_Result tee_buffer_update(
700b0104773SPascal Brand 		TEE_OperationHandle op,
701b0104773SPascal Brand 		TEE_Result(*update_func) (uint32_t state, const void *src,
7027f74c64aSPascal Brand 					  size_t slen, void *dst, uint32_t *dlen),
703b0104773SPascal Brand 		const void *src_data, size_t src_len,
7047f74c64aSPascal Brand 		void *dest_data, uint32_t *dest_len)
705b0104773SPascal Brand {
706b0104773SPascal Brand 	TEE_Result res;
707b0104773SPascal Brand 	const uint8_t *src = src_data;
708b0104773SPascal Brand 	size_t slen = src_len;
709b0104773SPascal Brand 	uint8_t *dst = dest_data;
710b0104773SPascal Brand 	size_t dlen = *dest_len;
711b0104773SPascal Brand 	size_t acc_dlen = 0;
7127f74c64aSPascal Brand 	uint32_t tmp_dlen;
713b0104773SPascal Brand 	size_t l;
714b0104773SPascal Brand 	size_t buffer_size;
715d3588802SPascal Brand 	size_t buffer_left;
716b0104773SPascal Brand 
717d3588802SPascal Brand 	if (op->buffer_two_blocks) {
718b0104773SPascal Brand 		buffer_size = op->block_size * 2;
719d3588802SPascal Brand 		buffer_left = 1;
720d3588802SPascal Brand 	} else {
721b0104773SPascal Brand 		buffer_size = op->block_size;
722d3588802SPascal Brand 		buffer_left = 0;
723d3588802SPascal Brand 	}
724b0104773SPascal Brand 
725b0104773SPascal Brand 	if (op->buffer_offs > 0) {
726b0104773SPascal Brand 		/* Fill up complete block */
727b0104773SPascal Brand 		if (op->buffer_offs < op->block_size)
728b0104773SPascal Brand 			l = MIN(slen, op->block_size - op->buffer_offs);
729b0104773SPascal Brand 		else
730b0104773SPascal Brand 			l = MIN(slen, buffer_size - op->buffer_offs);
731b0104773SPascal Brand 		memcpy(op->buffer + op->buffer_offs, src, l);
732b0104773SPascal Brand 		op->buffer_offs += l;
733b0104773SPascal Brand 		src += l;
734b0104773SPascal Brand 		slen -= l;
735b0104773SPascal Brand 		if ((op->buffer_offs % op->block_size) != 0)
736b0104773SPascal Brand 			goto out;	/* Nothing left to do */
737b0104773SPascal Brand 	}
738b0104773SPascal Brand 
739b0104773SPascal Brand 	/* If we can feed from buffer */
740d3588802SPascal Brand 	if ((op->buffer_offs > 0) &&
741d3588802SPascal Brand 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
7422ff3fdbbSPascal Brand 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
743b0104773SPascal Brand 				op->block_size);
744b0104773SPascal Brand 		l = MIN(op->buffer_offs, l);
745b0104773SPascal Brand 		tmp_dlen = dlen;
746b0104773SPascal Brand 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
747b0104773SPascal Brand 		if (res != TEE_SUCCESS)
748b0104773SPascal Brand 			TEE_Panic(res);
749b0104773SPascal Brand 		dst += tmp_dlen;
750b0104773SPascal Brand 		dlen -= tmp_dlen;
751b0104773SPascal Brand 		acc_dlen += tmp_dlen;
752b0104773SPascal Brand 		op->buffer_offs -= l;
753b0104773SPascal Brand 		if (op->buffer_offs > 0) {
754b0104773SPascal Brand 			/*
755b0104773SPascal Brand 			 * Slen is small enough to be contained in rest buffer.
756b0104773SPascal Brand 			 */
757b0104773SPascal Brand 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
758b0104773SPascal Brand 			memcpy(op->buffer + op->buffer_offs, src, slen);
759b0104773SPascal Brand 			op->buffer_offs += slen;
760b0104773SPascal Brand 			goto out;	/* Nothing left to do */
761b0104773SPascal Brand 		}
762b0104773SPascal Brand 	}
763b0104773SPascal Brand 
764d3588802SPascal Brand 	if (slen >= (buffer_size + buffer_left)) {
765b0104773SPascal Brand 		/* Buffer is empty, feed as much as possible from src */
766b0104773SPascal Brand 		if (TEE_ALIGNMENT_IS_OK(src, uint32_t)) {
7672ff3fdbbSPascal Brand 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
768b0104773SPascal Brand 
769b0104773SPascal Brand 			tmp_dlen = dlen;
770b0104773SPascal Brand 			res = update_func(op->state, src, l, dst, &tmp_dlen);
771b0104773SPascal Brand 			if (res != TEE_SUCCESS)
772b0104773SPascal Brand 				TEE_Panic(res);
773b0104773SPascal Brand 			src += l;
774b0104773SPascal Brand 			slen -= l;
775b0104773SPascal Brand 			dst += tmp_dlen;
776b0104773SPascal Brand 			dlen -= tmp_dlen;
777b0104773SPascal Brand 			acc_dlen += tmp_dlen;
778b0104773SPascal Brand 		} else {
779b0104773SPascal Brand 			/*
780b0104773SPascal Brand 			 * Supplied data isn't well aligned, we're forced to
781b0104773SPascal Brand 			 * feed through the buffer.
782b0104773SPascal Brand 			 */
783b0104773SPascal Brand 			while (slen >= op->block_size) {
784b0104773SPascal Brand 				memcpy(op->buffer, src, op->block_size);
785b0104773SPascal Brand 
786b0104773SPascal Brand 				tmp_dlen = dlen;
787b0104773SPascal Brand 				res =
788b0104773SPascal Brand 				    update_func(op->state, op->buffer,
789b0104773SPascal Brand 						op->block_size, dst, &tmp_dlen);
790b0104773SPascal Brand 				if (res != TEE_SUCCESS)
791b0104773SPascal Brand 					TEE_Panic(res);
792b0104773SPascal Brand 				src += op->block_size;
793b0104773SPascal Brand 				slen -= op->block_size;
794b0104773SPascal Brand 				dst += tmp_dlen;
795b0104773SPascal Brand 				dlen -= tmp_dlen;
796b0104773SPascal Brand 				acc_dlen += tmp_dlen;
797b0104773SPascal Brand 			}
798b0104773SPascal Brand 		}
799b0104773SPascal Brand 	}
800b0104773SPascal Brand 
801b0104773SPascal Brand 	/* Slen is small enough to be contained in buffer. */
802b0104773SPascal Brand 	memcpy(op->buffer + op->buffer_offs, src, slen);
803b0104773SPascal Brand 	op->buffer_offs += slen;
804b0104773SPascal Brand 
805b0104773SPascal Brand out:
806b0104773SPascal Brand 	*dest_len = acc_dlen;
807b0104773SPascal Brand 	return TEE_SUCCESS;
808b0104773SPascal Brand }
809b0104773SPascal Brand 
810b0104773SPascal Brand TEE_Result TEE_CipherUpdate(TEE_OperationHandle op, const void *srcData,
81179a3c601SCedric Chaumont 			    uint32_t srcLen, void *destData, uint32_t *destLen)
812b0104773SPascal Brand {
813dea1f2b6SCedric Chaumont 	TEE_Result res;
814b0104773SPascal Brand 	size_t req_dlen;
815b0104773SPascal Brand 
816dea1f2b6SCedric Chaumont 	if (op == TEE_HANDLE_NULL ||
817dea1f2b6SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
818dea1f2b6SCedric Chaumont 	    destLen == NULL ||
819dea1f2b6SCedric Chaumont 	    (destData == NULL && *destLen != 0)) {
820dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
821dea1f2b6SCedric Chaumont 		goto out;
822dea1f2b6SCedric Chaumont 	}
823dea1f2b6SCedric Chaumont 
824dea1f2b6SCedric Chaumont 	if (op->info.operationClass != TEE_OPERATION_CIPHER) {
825dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
826dea1f2b6SCedric Chaumont 		goto out;
827dea1f2b6SCedric Chaumont 	}
828dea1f2b6SCedric Chaumont 
829dea1f2b6SCedric Chaumont 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
830dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
831dea1f2b6SCedric Chaumont 		goto out;
832dea1f2b6SCedric Chaumont 	}
833b0104773SPascal Brand 
834b0104773SPascal Brand 	/* Calculate required dlen */
835b0104773SPascal Brand 	req_dlen = ((op->buffer_offs + srcLen) / op->block_size) *
836b0104773SPascal Brand 	    op->block_size;
837b0104773SPascal Brand 	if (op->buffer_two_blocks) {
838b0104773SPascal Brand 		if (req_dlen > op->block_size * 2)
839b0104773SPascal Brand 			req_dlen -= op->block_size * 2;
840b0104773SPascal Brand 		else
841b0104773SPascal Brand 			req_dlen = 0;
842b0104773SPascal Brand 	}
843b0104773SPascal Brand 	/*
844b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
845b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
846b0104773SPascal Brand 	 * can't restore sync with this API.
847b0104773SPascal Brand 	 */
848b0104773SPascal Brand 	if (*destLen < req_dlen) {
849b0104773SPascal Brand 		*destLen = req_dlen;
850dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
851dea1f2b6SCedric Chaumont 		goto out;
852b0104773SPascal Brand 	}
853b0104773SPascal Brand 
854dea1f2b6SCedric Chaumont 	res = tee_buffer_update(op, utee_cipher_update, srcData, srcLen,
855dea1f2b6SCedric Chaumont 				destData, destLen);
856b0104773SPascal Brand 
857dea1f2b6SCedric Chaumont out:
858dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
859dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
860dea1f2b6SCedric Chaumont 		TEE_Panic(0);
861dea1f2b6SCedric Chaumont 
862dea1f2b6SCedric Chaumont 	return res;
863b0104773SPascal Brand }
864b0104773SPascal Brand 
865b0104773SPascal Brand TEE_Result TEE_CipherDoFinal(TEE_OperationHandle op,
86679a3c601SCedric Chaumont 			     const void *srcData, uint32_t srcLen, void *destData,
86779a3c601SCedric Chaumont 			     uint32_t *destLen)
868b0104773SPascal Brand {
869b0104773SPascal Brand 	TEE_Result res;
870b0104773SPascal Brand 	uint8_t *dst = destData;
871b0104773SPascal Brand 	size_t acc_dlen = 0;
8727f74c64aSPascal Brand 	uint32_t tmp_dlen;
873b0104773SPascal Brand 	size_t req_dlen;
874b0104773SPascal Brand 
875dea1f2b6SCedric Chaumont 	if (op == TEE_HANDLE_NULL ||
876dea1f2b6SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
877dea1f2b6SCedric Chaumont 	    destLen == NULL ||
878dea1f2b6SCedric Chaumont 	    (destData == NULL && *destLen != 0)) {
879dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
880dea1f2b6SCedric Chaumont 		goto out;
881dea1f2b6SCedric Chaumont 	}
882dea1f2b6SCedric Chaumont 
883dea1f2b6SCedric Chaumont 	if (op->info.operationClass != TEE_OPERATION_CIPHER) {
884dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
885dea1f2b6SCedric Chaumont 		goto out;
886dea1f2b6SCedric Chaumont 	}
887dea1f2b6SCedric Chaumont 
888dea1f2b6SCedric Chaumont 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
889dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
890dea1f2b6SCedric Chaumont 		goto out;
891dea1f2b6SCedric Chaumont 	}
892b0104773SPascal Brand 
893b0104773SPascal Brand 	/*
894b0104773SPascal Brand 	 * Check that the final block doesn't require padding for those
895b0104773SPascal Brand 	 * algorithms that requires client to supply padding.
896b0104773SPascal Brand 	 */
897b0104773SPascal Brand 	if (op->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
898b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
899b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
900b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
901b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
902b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) {
903dea1f2b6SCedric Chaumont 		if (((op->buffer_offs + srcLen) % op->block_size) != 0) {
904dea1f2b6SCedric Chaumont 			res = TEE_ERROR_BAD_PARAMETERS;
905dea1f2b6SCedric Chaumont 			goto out;
906dea1f2b6SCedric Chaumont 		}
907b0104773SPascal Brand 	}
908b0104773SPascal Brand 
909b0104773SPascal Brand 	/*
910b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
911b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
912b0104773SPascal Brand 	 * can't restore sync with this API.
913b0104773SPascal Brand 	 */
914b0104773SPascal Brand 	req_dlen = op->buffer_offs + srcLen;
915b0104773SPascal Brand 	if (*destLen < req_dlen) {
916b0104773SPascal Brand 		*destLen = req_dlen;
917dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
918dea1f2b6SCedric Chaumont 		goto out;
919b0104773SPascal Brand 	}
920b0104773SPascal Brand 
921b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
922dea1f2b6SCedric Chaumont 	res = tee_buffer_update(op, utee_cipher_update, srcData, srcLen, dst,
923b0104773SPascal Brand 				&tmp_dlen);
924dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS)
925dea1f2b6SCedric Chaumont 		goto out;
926dea1f2b6SCedric Chaumont 
927b0104773SPascal Brand 	dst += tmp_dlen;
928b0104773SPascal Brand 	acc_dlen += tmp_dlen;
929b0104773SPascal Brand 
930b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
931b0104773SPascal Brand 	res = utee_cipher_final(op->state, op->buffer, op->buffer_offs,
932b0104773SPascal Brand 				dst, &tmp_dlen);
933b0104773SPascal Brand 	if (res != TEE_SUCCESS)
934dea1f2b6SCedric Chaumont 		goto out;
935dea1f2b6SCedric Chaumont 
936b0104773SPascal Brand 	acc_dlen += tmp_dlen;
937b0104773SPascal Brand 
938b0104773SPascal Brand 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
939b0104773SPascal Brand 	*destLen = acc_dlen;
940dea1f2b6SCedric Chaumont 
941dea1f2b6SCedric Chaumont out:
942dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
943dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
944dea1f2b6SCedric Chaumont 		TEE_Panic(0);
945dea1f2b6SCedric Chaumont 
946dea1f2b6SCedric Chaumont 	return res;
947b0104773SPascal Brand }
948b0104773SPascal Brand 
949b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */
950b0104773SPascal Brand 
951*28e0efc6SCedric Chaumont void TEE_MACInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen)
952b0104773SPascal Brand {
953b0104773SPascal Brand 	TEE_Result res;
954b0104773SPascal Brand 
955b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
956b0104773SPascal Brand 		TEE_Panic(0);
957*28e0efc6SCedric Chaumont 	if (!operation->key1)
958b0104773SPascal Brand 		TEE_Panic(0);
959b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_MAC)
960b0104773SPascal Brand 		TEE_Panic(0);
961*28e0efc6SCedric Chaumont 	/*
962*28e0efc6SCedric Chaumont 	 * Note : IV and IVLen are never used in current implementation
963*28e0efc6SCedric Chaumont 	 * This is why coherent values of IV and IVLen are not checked
964*28e0efc6SCedric Chaumont 	 */
965b0104773SPascal Brand 	res = utee_hash_init(operation->state, IV, IVLen);
966b0104773SPascal Brand 	if (res != TEE_SUCCESS)
967b0104773SPascal Brand 		TEE_Panic(res);
968b0104773SPascal Brand 	operation->buffer_offs = 0;
969b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
970b0104773SPascal Brand }
971b0104773SPascal Brand 
972*28e0efc6SCedric Chaumont void TEE_MACUpdate(TEE_OperationHandle operation, void *chunk,
973*28e0efc6SCedric Chaumont 		   uint32_t chunkSize)
974b0104773SPascal Brand {
975b0104773SPascal Brand 	TEE_Result res;
976b0104773SPascal Brand 
977*28e0efc6SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
978b0104773SPascal Brand 		TEE_Panic(0);
979*28e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC)
980b0104773SPascal Brand 		TEE_Panic(0);
981*28e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
982b0104773SPascal Brand 		TEE_Panic(0);
983b0104773SPascal Brand 
984*28e0efc6SCedric Chaumont 	res = utee_hash_update(operation->state, chunk, chunkSize);
985b0104773SPascal Brand 	if (res != TEE_SUCCESS)
986b0104773SPascal Brand 		TEE_Panic(res);
987b0104773SPascal Brand }
988b0104773SPascal Brand 
989*28e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
990*28e0efc6SCedric Chaumont 			       void *message, uint32_t messageLen,
99179a3c601SCedric Chaumont 			       void *mac, uint32_t *macLen)
992b0104773SPascal Brand {
993b0104773SPascal Brand 	TEE_Result res;
994b0104773SPascal Brand 
995*28e0efc6SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
996*28e0efc6SCedric Chaumont 	    (message == NULL && messageLen != 0) ||
997*28e0efc6SCedric Chaumont 	    mac == NULL ||
998*28e0efc6SCedric Chaumont 	    macLen == NULL) {
999*28e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1000*28e0efc6SCedric Chaumont 		goto out;
1001*28e0efc6SCedric Chaumont 	}
1002b0104773SPascal Brand 
1003*28e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1004*28e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1005*28e0efc6SCedric Chaumont 		goto out;
1006*28e0efc6SCedric Chaumont 	}
1007*28e0efc6SCedric Chaumont 
1008*28e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1009*28e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1010*28e0efc6SCedric Chaumont 		goto out;
1011*28e0efc6SCedric Chaumont 	}
1012*28e0efc6SCedric Chaumont 
1013*28e0efc6SCedric Chaumont 	res = utee_hash_final(operation->state, message, messageLen, mac,
1014*28e0efc6SCedric Chaumont 			      macLen);
1015*28e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS)
1016*28e0efc6SCedric Chaumont 		goto out;
1017*28e0efc6SCedric Chaumont 
1018*28e0efc6SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1019*28e0efc6SCedric Chaumont 
1020*28e0efc6SCedric Chaumont out:
1021*28e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1022*28e0efc6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1023*28e0efc6SCedric Chaumont 		TEE_Panic(res);
1024*28e0efc6SCedric Chaumont 
1025b0104773SPascal Brand 	return res;
1026b0104773SPascal Brand }
1027b0104773SPascal Brand 
1028b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
1029*28e0efc6SCedric Chaumont 			       void *message, uint32_t messageLen,
1030*28e0efc6SCedric Chaumont 			       void *mac, uint32_t macLen)
1031b0104773SPascal Brand {
1032b0104773SPascal Brand 	TEE_Result res;
1033b0104773SPascal Brand 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
10347f74c64aSPascal Brand 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1035b0104773SPascal Brand 
1036*28e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1037*28e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1038*28e0efc6SCedric Chaumont 		goto out;
1039*28e0efc6SCedric Chaumont 	}
1040*28e0efc6SCedric Chaumont 
1041*28e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1042*28e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1043*28e0efc6SCedric Chaumont 		goto out;
1044*28e0efc6SCedric Chaumont 	}
1045*28e0efc6SCedric Chaumont 
1046b0104773SPascal Brand 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1047b0104773SPascal Brand 				  &computed_mac_size);
1048b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1049*28e0efc6SCedric Chaumont 		goto out;
1050*28e0efc6SCedric Chaumont 
1051*28e0efc6SCedric Chaumont 	if (computed_mac_size != macLen) {
1052*28e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
1053*28e0efc6SCedric Chaumont 		goto out;
1054*28e0efc6SCedric Chaumont 	}
1055*28e0efc6SCedric Chaumont 
1056*28e0efc6SCedric Chaumont 	if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) {
1057*28e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
1058*28e0efc6SCedric Chaumont 		goto out;
1059*28e0efc6SCedric Chaumont 	}
1060*28e0efc6SCedric Chaumont 
1061*28e0efc6SCedric Chaumont out:
1062*28e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1063*28e0efc6SCedric Chaumont 	    res != TEE_ERROR_MAC_INVALID)
1064*28e0efc6SCedric Chaumont 		TEE_Panic(res);
1065*28e0efc6SCedric Chaumont 
1066b0104773SPascal Brand 	return res;
1067b0104773SPascal Brand }
1068b0104773SPascal Brand 
1069b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */
1070b0104773SPascal Brand 
1071b0104773SPascal Brand TEE_Result TEE_AEInit(TEE_OperationHandle op, const void *nonce,
107279a3c601SCedric Chaumont 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1073b0104773SPascal Brand 		      uint32_t payloadLen)
1074b0104773SPascal Brand {
1075b0104773SPascal Brand 	TEE_Result res;
1076b0104773SPascal Brand 
1077b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || nonce == NULL)
1078b0104773SPascal Brand 		TEE_Panic(0);
1079b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
1080b0104773SPascal Brand 		TEE_Panic(0);
1081b0104773SPascal Brand 
1082b0104773SPascal Brand 	/*
1083b0104773SPascal Brand 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1084b0104773SPascal Brand 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1085b0104773SPascal Brand 	 * according to the same principle so we have to check here instead to
1086b0104773SPascal Brand 	 * be GP compliant.
1087b0104773SPascal Brand 	 */
1088b0104773SPascal Brand 	if (op->info.algorithm == TEE_ALG_AES_GCM) {
1089b0104773SPascal Brand 		/*
1090b0104773SPascal Brand 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1091b0104773SPascal Brand 		 */
1092b0104773SPascal Brand 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0))
1093b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
1094b0104773SPascal Brand 	}
1095b0104773SPascal Brand 
1096b0104773SPascal Brand 	res = utee_authenc_init(op->state, nonce, nonceLen, tagLen / 8, AADLen,
1097b0104773SPascal Brand 				payloadLen);
1098b0104773SPascal Brand 	if (res != TEE_SUCCESS) {
1099b0104773SPascal Brand 		if (res != TEE_ERROR_NOT_SUPPORTED)
1100b0104773SPascal Brand 			TEE_Panic(res);
1101b0104773SPascal Brand 		return res;
1102b0104773SPascal Brand 	}
1103b0104773SPascal Brand 	op->ae_tag_len = tagLen / 8;
1104b0104773SPascal Brand 
1105b0104773SPascal Brand 	op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1106b0104773SPascal Brand 	return TEE_SUCCESS;
1107b0104773SPascal Brand }
1108b0104773SPascal Brand 
1109b0104773SPascal Brand void TEE_AEUpdateAAD(TEE_OperationHandle op, const void *AADdata,
111079a3c601SCedric Chaumont 		     uint32_t AADdataLen)
1111b0104773SPascal Brand {
1112b0104773SPascal Brand 	TEE_Result res;
1113b0104773SPascal Brand 
1114b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (AADdata == NULL && AADdataLen != 0))
1115b0104773SPascal Brand 		TEE_Panic(0);
1116b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
1117b0104773SPascal Brand 		TEE_Panic(0);
1118b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1119b0104773SPascal Brand 		TEE_Panic(0);
1120b0104773SPascal Brand 
1121b0104773SPascal Brand 	res = utee_authenc_update_aad(op->state, AADdata, AADdataLen);
1122b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1123b0104773SPascal Brand 		TEE_Panic(res);
1124b0104773SPascal Brand }
1125b0104773SPascal Brand 
1126b0104773SPascal Brand TEE_Result TEE_AEUpdate(TEE_OperationHandle op, const void *srcData,
112779a3c601SCedric Chaumont 			uint32_t srcLen, void *destData, uint32_t *destLen)
1128b0104773SPascal Brand {
1129b0104773SPascal Brand 	size_t req_dlen;
1130b0104773SPascal Brand 
1131b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1132b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
1133b0104773SPascal Brand 		TEE_Panic(0);
1134b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
1135b0104773SPascal Brand 		TEE_Panic(0);
1136b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1137b0104773SPascal Brand 		TEE_Panic(0);
1138b0104773SPascal Brand 
1139b0104773SPascal Brand 	/*
1140b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1141b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1142b0104773SPascal Brand 	 * can't restore sync with this API.
1143b0104773SPascal Brand 	 */
11442ff3fdbbSPascal Brand 	req_dlen = ROUNDDOWN(op->buffer_offs + srcLen, op->block_size);
1145b0104773SPascal Brand 	if (*destLen < req_dlen) {
1146b0104773SPascal Brand 		*destLen = req_dlen;
1147b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
1148b0104773SPascal Brand 	}
1149b0104773SPascal Brand 
1150b0104773SPascal Brand 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
1151b0104773SPascal Brand 			  destData, destLen);
1152b0104773SPascal Brand 
1153b0104773SPascal Brand 	return TEE_SUCCESS;
1154b0104773SPascal Brand }
1155b0104773SPascal Brand 
1156b0104773SPascal Brand TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle op,
115779a3c601SCedric Chaumont 			      const void *srcData, uint32_t srcLen,
115879a3c601SCedric Chaumont 			      void *destData, uint32_t *destLen, void *tag,
115979a3c601SCedric Chaumont 			      uint32_t *tagLen)
1160b0104773SPascal Brand {
1161b0104773SPascal Brand 	TEE_Result res;
1162b0104773SPascal Brand 	uint8_t *dst = destData;
1163b0104773SPascal Brand 	size_t acc_dlen = 0;
11647f74c64aSPascal Brand 	uint32_t tmp_dlen;
1165b0104773SPascal Brand 	size_t req_dlen;
1166b0104773SPascal Brand 
1167b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1168b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0) ||
1169b0104773SPascal Brand 	    tag == NULL || tagLen == NULL)
1170b0104773SPascal Brand 		TEE_Panic(0);
1171b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
1172b0104773SPascal Brand 		TEE_Panic(0);
1173b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1174b0104773SPascal Brand 		TEE_Panic(0);
1175b0104773SPascal Brand 
1176b0104773SPascal Brand 	/*
1177b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1178b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1179b0104773SPascal Brand 	 * can't restore sync with this API.
1180b0104773SPascal Brand 	 */
1181b0104773SPascal Brand 	req_dlen = op->buffer_offs + srcLen;
1182b0104773SPascal Brand 	if (*destLen < req_dlen) {
1183b0104773SPascal Brand 		*destLen = req_dlen;
1184b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
1185b0104773SPascal Brand 	}
1186b0104773SPascal Brand 
1187b0104773SPascal Brand 	/*
1188b0104773SPascal Brand 	 * Need to check this before update_payload since sync would be lost if
1189b0104773SPascal Brand 	 * we return short buffer after that.
1190b0104773SPascal Brand 	 */
1191b0104773SPascal Brand 	if (*tagLen < op->ae_tag_len) {
1192b0104773SPascal Brand 		*tagLen = op->ae_tag_len;
1193b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
1194b0104773SPascal Brand 	}
1195b0104773SPascal Brand 
1196b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1197b0104773SPascal Brand 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
1198b0104773SPascal Brand 			  dst, &tmp_dlen);
1199b0104773SPascal Brand 	dst += tmp_dlen;
1200b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1201b0104773SPascal Brand 
1202b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1203b0104773SPascal Brand 	res =
1204b0104773SPascal Brand 	    utee_authenc_enc_final(op->state, op->buffer, op->buffer_offs, dst,
1205b0104773SPascal Brand 				   &tmp_dlen, tag, tagLen);
1206b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1207b0104773SPascal Brand 		TEE_Panic(res);
1208b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1209b0104773SPascal Brand 
1210b0104773SPascal Brand 	*destLen = acc_dlen;
1211b0104773SPascal Brand 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1212b0104773SPascal Brand 
1213b0104773SPascal Brand 	return res;
1214b0104773SPascal Brand }
1215b0104773SPascal Brand 
1216b0104773SPascal Brand TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle op,
121779a3c601SCedric Chaumont 			      const void *srcData, uint32_t srcLen,
121879a3c601SCedric Chaumont 			      void *destData, uint32_t *destLen, const void *tag,
121979a3c601SCedric Chaumont 			      uint32_t tagLen)
1220b0104773SPascal Brand {
1221b0104773SPascal Brand 	TEE_Result res;
1222b0104773SPascal Brand 	uint8_t *dst = destData;
1223b0104773SPascal Brand 	size_t acc_dlen = 0;
12247f74c64aSPascal Brand 	uint32_t tmp_dlen;
1225b0104773SPascal Brand 	size_t req_dlen;
1226b0104773SPascal Brand 
1227b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1228b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0) ||
1229b0104773SPascal Brand 	    (tag == NULL && tagLen != 0))
1230b0104773SPascal Brand 		TEE_Panic(0);
1231b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
1232b0104773SPascal Brand 		TEE_Panic(0);
1233b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1234b0104773SPascal Brand 		TEE_Panic(0);
1235b0104773SPascal Brand 
1236b0104773SPascal Brand 	/*
1237b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1238b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1239b0104773SPascal Brand 	 * can't restore sync with this API.
1240b0104773SPascal Brand 	 */
1241b0104773SPascal Brand 	req_dlen = op->buffer_offs + srcLen;
1242b0104773SPascal Brand 	if (*destLen < req_dlen) {
1243b0104773SPascal Brand 		*destLen = req_dlen;
1244b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
1245b0104773SPascal Brand 	}
1246b0104773SPascal Brand 
1247b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1248b0104773SPascal Brand 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
1249b0104773SPascal Brand 			  dst, &tmp_dlen);
1250b0104773SPascal Brand 	dst += tmp_dlen;
1251b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1252b0104773SPascal Brand 
1253b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1254b0104773SPascal Brand 	res =
1255b0104773SPascal Brand 	    utee_authenc_dec_final(op->state, op->buffer, op->buffer_offs, dst,
1256b0104773SPascal Brand 				   &tmp_dlen, tag, tagLen);
1257b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_MAC_INVALID)
1258b0104773SPascal Brand 		TEE_Panic(res);
1259b0104773SPascal Brand 	/* Supplied tagLen should match what we initiated with */
1260b0104773SPascal Brand 	if (tagLen != op->ae_tag_len)
1261b0104773SPascal Brand 		res = TEE_ERROR_MAC_INVALID;
1262b0104773SPascal Brand 
1263b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1264b0104773SPascal Brand 
1265b0104773SPascal Brand 	*destLen = acc_dlen;
1266b0104773SPascal Brand 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1267b0104773SPascal Brand 
1268b0104773SPascal Brand 	return res;
1269b0104773SPascal Brand }
1270b0104773SPascal Brand 
1271b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */
1272b0104773SPascal Brand 
1273b0104773SPascal Brand TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle op,
1274b0104773SPascal Brand 				 const TEE_Attribute *params,
1275b0104773SPascal Brand 				 uint32_t paramCount, const void *srcData,
127679a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
127779a3c601SCedric Chaumont 				 uint32_t *destLen)
1278b0104773SPascal Brand {
1279b0104773SPascal Brand 	TEE_Result res;
1280b0104773SPascal Brand 
1281b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1282b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
1283b0104773SPascal Brand 		TEE_Panic(0);
1284b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1285b0104773SPascal Brand 		TEE_Panic(0);
1286b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1287b0104773SPascal Brand 		TEE_Panic(0);
1288b0104773SPascal Brand 	if (op->info.mode != TEE_MODE_ENCRYPT)
1289b0104773SPascal Brand 		TEE_Panic(0);
1290b0104773SPascal Brand 
1291b0104773SPascal Brand 	res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen,
1292b0104773SPascal Brand 				 destData, destLen);
12938844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
12948844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
12958844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1296b0104773SPascal Brand 		TEE_Panic(res);
1297b0104773SPascal Brand 	return res;
1298b0104773SPascal Brand }
1299b0104773SPascal Brand 
1300b0104773SPascal Brand TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle op,
1301b0104773SPascal Brand 				 const TEE_Attribute *params,
1302b0104773SPascal Brand 				 uint32_t paramCount, const void *srcData,
130379a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
130479a3c601SCedric Chaumont 				 uint32_t *destLen)
1305b0104773SPascal Brand {
1306b0104773SPascal Brand 	TEE_Result res;
1307b0104773SPascal Brand 
1308b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1309b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
1310b0104773SPascal Brand 		TEE_Panic(0);
1311b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1312b0104773SPascal Brand 		TEE_Panic(0);
1313b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1314b0104773SPascal Brand 		TEE_Panic(0);
1315b0104773SPascal Brand 	if (op->info.mode != TEE_MODE_DECRYPT)
1316b0104773SPascal Brand 		TEE_Panic(0);
1317b0104773SPascal Brand 
1318b0104773SPascal Brand 	res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen,
1319b0104773SPascal Brand 				 destData, destLen);
13208844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
13218844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
13228844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1323b0104773SPascal Brand 		TEE_Panic(res);
1324b0104773SPascal Brand 	return res;
1325b0104773SPascal Brand }
1326b0104773SPascal Brand 
1327b0104773SPascal Brand TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle op,
1328b0104773SPascal Brand 				    const TEE_Attribute *params,
1329b0104773SPascal Brand 				    uint32_t paramCount, const void *digest,
133079a3c601SCedric Chaumont 				    uint32_t digestLen, void *signature,
133179a3c601SCedric Chaumont 				    uint32_t *signatureLen)
1332b0104773SPascal Brand {
1333b0104773SPascal Brand 	TEE_Result res;
1334b0104773SPascal Brand 
1335b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) ||
1336b0104773SPascal Brand 	    signature == NULL || signatureLen == NULL)
1337b0104773SPascal Brand 		TEE_Panic(0);
1338b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1339b0104773SPascal Brand 		TEE_Panic(0);
1340b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1341b0104773SPascal Brand 		TEE_Panic(0);
1342b0104773SPascal Brand 	if (op->info.mode != TEE_MODE_SIGN)
1343b0104773SPascal Brand 		TEE_Panic(0);
1344b0104773SPascal Brand 
1345b0104773SPascal Brand 	res =
1346b0104773SPascal Brand 	    utee_asymm_operate(op->state, params, paramCount, digest, digestLen,
1347b0104773SPascal Brand 			       signature, signatureLen);
1348b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1349b0104773SPascal Brand 		TEE_Panic(res);
1350b0104773SPascal Brand 	return res;
1351b0104773SPascal Brand }
1352b0104773SPascal Brand 
1353b0104773SPascal Brand TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle op,
1354b0104773SPascal Brand 				      const TEE_Attribute *params,
1355b0104773SPascal Brand 				      uint32_t paramCount, const void *digest,
135679a3c601SCedric Chaumont 				      uint32_t digestLen, const void *signature,
135779a3c601SCedric Chaumont 				      uint32_t signatureLen)
1358b0104773SPascal Brand {
1359b0104773SPascal Brand 	TEE_Result res;
1360b0104773SPascal Brand 
1361b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) ||
1362b0104773SPascal Brand 	    (signature == NULL && signatureLen != 0))
1363b0104773SPascal Brand 		TEE_Panic(0);
1364b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1365b0104773SPascal Brand 		TEE_Panic(0);
1366b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1367b0104773SPascal Brand 		TEE_Panic(0);
1368b0104773SPascal Brand 	if (op->info.mode != TEE_MODE_VERIFY)
1369b0104773SPascal Brand 		TEE_Panic(0);
1370b0104773SPascal Brand 
1371b0104773SPascal Brand 	res =
1372b0104773SPascal Brand 	    utee_asymm_verify(op->state, params, paramCount, digest, digestLen,
1373b0104773SPascal Brand 			      signature, signatureLen);
1374b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1375b0104773SPascal Brand 		TEE_Panic(res);
1376b0104773SPascal Brand 	return res;
1377b0104773SPascal Brand }
1378b0104773SPascal Brand 
1379b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */
1380b0104773SPascal Brand 
1381b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation,
1382b0104773SPascal Brand 		   const TEE_Attribute *params, uint32_t paramCount,
1383b0104773SPascal Brand 		   TEE_ObjectHandle derivedKey)
1384b0104773SPascal Brand {
1385b0104773SPascal Brand 	TEE_Result res;
1386b0104773SPascal Brand 	TEE_ObjectInfo key_info;
1387b0104773SPascal Brand 
1388b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1389b0104773SPascal Brand 		TEE_Panic(0);
1390b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1391b0104773SPascal Brand 		TEE_Panic(0);
13928854d3c6SJerome Forissier 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
13938854d3c6SJerome Forissier 			TEE_OPERATION_KEY_DERIVATION)
1394b0104773SPascal Brand 		TEE_Panic(0);
1395b0104773SPascal Brand 
1396b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1397b0104773SPascal Brand 		TEE_Panic(0);
1398b0104773SPascal Brand 	if (operation->info.mode != TEE_MODE_DERIVE)
1399b0104773SPascal Brand 		TEE_Panic(0);
1400b0104773SPascal Brand 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1401b0104773SPascal Brand 		TEE_Panic(0);
1402b0104773SPascal Brand 
1403b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t) derivedKey, &key_info);
1404b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1405b0104773SPascal Brand 		TEE_Panic(0);
1406b0104773SPascal Brand 
1407b0104773SPascal Brand 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1408b0104773SPascal Brand 		TEE_Panic(0);
1409b0104773SPascal Brand 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1410b0104773SPascal Brand 		TEE_Panic(0);
1411b0104773SPascal Brand 
1412b0104773SPascal Brand 	res = utee_cryp_derive_key(operation->state, params, paramCount,
1413b0104773SPascal Brand 				   (uint32_t) derivedKey);
1414b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1415b0104773SPascal Brand 		TEE_Panic(res);
1416b0104773SPascal Brand }
1417b0104773SPascal Brand 
1418b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */
1419b0104773SPascal Brand 
142079a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1421b0104773SPascal Brand {
1422b0104773SPascal Brand 	TEE_Result res;
1423b0104773SPascal Brand 
1424b0104773SPascal Brand 	res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1425b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1426b0104773SPascal Brand 		TEE_Panic(res);
1427b0104773SPascal Brand }
1428