xref: /optee_os/lib/libutee/tee_api_operations.c (revision f21873cdf15780da35ecd3525c507c76a4c857a8)
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>
37e86f1266SJens Wiklander #include "tee_api_private.h"
38b0104773SPascal Brand 
39b0104773SPascal Brand struct __TEE_OperationHandle {
40b0104773SPascal Brand 	TEE_OperationInfo info;
41b0104773SPascal Brand 	TEE_ObjectHandle key1;
42b0104773SPascal Brand 	TEE_ObjectHandle key2;
43642a1607SCedric Chaumont 	uint32_t operationState;/* Operation state : INITIAL or ACTIVE */
44b0104773SPascal Brand 	uint8_t *buffer;	/* buffer to collect complete blocks */
45b0104773SPascal Brand 	bool buffer_two_blocks;	/* True if two blocks need to be buffered */
46b0104773SPascal Brand 	size_t block_size;	/* Block size of cipher */
47b0104773SPascal Brand 	size_t buffer_offs;	/* Offset in buffer */
48b0104773SPascal Brand 	uint32_t state;		/* Handle to state in TEE Core */
49b0104773SPascal Brand 	uint32_t ae_tag_len;	/*
50b0104773SPascal Brand 				 * tag_len in bytes for AE operation else unused
51b0104773SPascal Brand 				 */
52b0104773SPascal Brand };
53b0104773SPascal Brand 
54b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */
55b0104773SPascal Brand 
56b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation,
57b0104773SPascal Brand 				 uint32_t algorithm, uint32_t mode,
58b0104773SPascal Brand 				 uint32_t maxKeySize)
59b0104773SPascal Brand {
60b0104773SPascal Brand 	TEE_Result res;
61b0104773SPascal Brand 	TEE_OperationHandle op = TEE_HANDLE_NULL;
62b0104773SPascal Brand 	uint32_t handle_state = 0;
63b0104773SPascal Brand 	size_t block_size = 1;
64b0104773SPascal Brand 	uint32_t req_key_usage;
65b0104773SPascal Brand 	bool with_private_key = false;
66b0104773SPascal Brand 	bool buffer_two_blocks = false;
67b0104773SPascal Brand 
689b52c538SCedric Chaumont 	if (!operation)
69b0104773SPascal Brand 		TEE_Panic(0);
70b0104773SPascal Brand 
71b0104773SPascal Brand 	if (algorithm == TEE_ALG_AES_XTS)
72b0104773SPascal Brand 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
73b0104773SPascal Brand 
74218d9055SCedric Chaumont 	/* Check algorithm max key size */
75218d9055SCedric Chaumont 	switch (algorithm) {
76218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA1:
77218d9055SCedric Chaumont 		if (maxKeySize < 512)
78218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
79218d9055SCedric Chaumont 		if (maxKeySize > 1024)
80218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
81218d9055SCedric Chaumont 		if (maxKeySize % 64 != 0)
82218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
83218d9055SCedric Chaumont 		break;
84218d9055SCedric Chaumont 
85218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA224:
86218d9055SCedric Chaumont 		if (maxKeySize != 2048)
87218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
88218d9055SCedric Chaumont 		break;
89218d9055SCedric Chaumont 
90218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA256:
91218d9055SCedric Chaumont 		if (maxKeySize != 2048 && maxKeySize != 3072)
92218d9055SCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
93218d9055SCedric Chaumont 		break;
94218d9055SCedric Chaumont 
951220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P192:
961220586eSCedric Chaumont 	case TEE_ALG_ECDH_P192:
971220586eSCedric Chaumont 		if (maxKeySize != 192)
981220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
991220586eSCedric Chaumont 		break;
1001220586eSCedric Chaumont 
1011220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P224:
1021220586eSCedric Chaumont 	case TEE_ALG_ECDH_P224:
1031220586eSCedric Chaumont 		if (maxKeySize != 224)
1041220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1051220586eSCedric Chaumont 		break;
1061220586eSCedric Chaumont 
1071220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P256:
1081220586eSCedric Chaumont 	case TEE_ALG_ECDH_P256:
1091220586eSCedric Chaumont 		if (maxKeySize != 256)
1101220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1111220586eSCedric Chaumont 		break;
1121220586eSCedric Chaumont 
1131220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P384:
1141220586eSCedric Chaumont 	case TEE_ALG_ECDH_P384:
1151220586eSCedric Chaumont 		if (maxKeySize != 384)
1161220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1171220586eSCedric Chaumont 		break;
1181220586eSCedric Chaumont 
1191220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P521:
1201220586eSCedric Chaumont 	case TEE_ALG_ECDH_P521:
1211220586eSCedric Chaumont 		if (maxKeySize != 521)
1221220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
1231220586eSCedric Chaumont 		break;
1241220586eSCedric Chaumont 
125218d9055SCedric Chaumont 	default:
126218d9055SCedric Chaumont 		break;
127218d9055SCedric Chaumont 	}
128218d9055SCedric Chaumont 
129218d9055SCedric Chaumont 	/* Check algorithm mode */
130b0104773SPascal Brand 	switch (algorithm) {
131b0104773SPascal Brand 	case TEE_ALG_AES_CTS:
132b0104773SPascal Brand 	case TEE_ALG_AES_XTS:
133b0104773SPascal Brand 		buffer_two_blocks = true;
134b0104773SPascal Brand 	 /*FALLTHROUGH*/ case TEE_ALG_AES_ECB_NOPAD:
135b0104773SPascal Brand 	case TEE_ALG_AES_CBC_NOPAD:
136b0104773SPascal Brand 	case TEE_ALG_AES_CTR:
137b0104773SPascal Brand 	case TEE_ALG_AES_CCM:
138b0104773SPascal Brand 	case TEE_ALG_AES_GCM:
139b0104773SPascal Brand 	case TEE_ALG_DES_ECB_NOPAD:
140b0104773SPascal Brand 	case TEE_ALG_DES_CBC_NOPAD:
141b0104773SPascal Brand 	case TEE_ALG_DES3_ECB_NOPAD:
142b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_NOPAD:
143b0104773SPascal Brand 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
144b0104773SPascal Brand 			block_size = TEE_AES_BLOCK_SIZE;
145b0104773SPascal Brand 		else
146b0104773SPascal Brand 			block_size = TEE_DES_BLOCK_SIZE;
147b0104773SPascal Brand 
148b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT)
149b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
150b0104773SPascal Brand 		else if (mode == TEE_MODE_DECRYPT)
151b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
152b0104773SPascal Brand 		else
153b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
154b0104773SPascal Brand 		break;
155b0104773SPascal Brand 
156b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
157b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
158b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
159b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
160b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
161b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
162b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
163b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
164b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
165b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
166b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
167b0104773SPascal Brand 	case TEE_ALG_DSA_SHA1:
168218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA224:
169218d9055SCedric Chaumont 	case TEE_ALG_DSA_SHA256:
1701220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P192:
1711220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P224:
1721220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P256:
1731220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P384:
1741220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P521:
175b0104773SPascal Brand 		if (mode == TEE_MODE_SIGN) {
176b0104773SPascal Brand 			with_private_key = true;
177b0104773SPascal Brand 			req_key_usage = TEE_USAGE_SIGN;
178b0104773SPascal Brand 		} else if (mode == TEE_MODE_VERIFY) {
179b0104773SPascal Brand 			req_key_usage = TEE_USAGE_VERIFY;
180b0104773SPascal Brand 		} else {
181b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
182b0104773SPascal Brand 		}
183b0104773SPascal Brand 		break;
184b0104773SPascal Brand 
185b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_V1_5:
186b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
187b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
188b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
189b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
190b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
191b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
192b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
193b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
194b0104773SPascal Brand 			with_private_key = true;
195b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
196b0104773SPascal Brand 		} else {
197b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
198b0104773SPascal Brand 		}
199b0104773SPascal Brand 		break;
200b0104773SPascal Brand 
201b0104773SPascal Brand 	case TEE_ALG_RSA_NOPAD:
202b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
203b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
204b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
205b0104773SPascal Brand 			with_private_key = true;
206b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
207b0104773SPascal Brand 		} else {
208b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
209b0104773SPascal Brand 		}
210b0104773SPascal Brand 		break;
211b0104773SPascal Brand 
212b0104773SPascal Brand 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
2131220586eSCedric Chaumont 	case TEE_ALG_ECDH_P192:
2141220586eSCedric Chaumont 	case TEE_ALG_ECDH_P224:
2151220586eSCedric Chaumont 	case TEE_ALG_ECDH_P256:
2161220586eSCedric Chaumont 	case TEE_ALG_ECDH_P384:
2171220586eSCedric Chaumont 	case TEE_ALG_ECDH_P521:
218cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_MD5_DERIVE_KEY:
219cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA1_DERIVE_KEY:
220cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA224_DERIVE_KEY:
221cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA256_DERIVE_KEY:
222cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA384_DERIVE_KEY:
223cdb198a7SJerome Forissier 	case TEE_ALG_HKDF_SHA512_DERIVE_KEY:
2248854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY:
2258854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY:
2268854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY:
2278854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY:
2288854d3c6SJerome Forissier 	case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY:
2290f2293b7SJerome Forissier 	case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY:
230b0104773SPascal Brand 		if (mode != TEE_MODE_DERIVE)
231b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
232b0104773SPascal Brand 		with_private_key = true;
233b0104773SPascal Brand 		req_key_usage = TEE_USAGE_DERIVE;
234b0104773SPascal Brand 		break;
235b0104773SPascal Brand 
236b0104773SPascal Brand 	case TEE_ALG_MD5:
237b0104773SPascal Brand 	case TEE_ALG_SHA1:
238b0104773SPascal Brand 	case TEE_ALG_SHA224:
239b0104773SPascal Brand 	case TEE_ALG_SHA256:
240b0104773SPascal Brand 	case TEE_ALG_SHA384:
241b0104773SPascal Brand 	case TEE_ALG_SHA512:
242b0104773SPascal Brand 		if (mode != TEE_MODE_DIGEST)
243b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
24405304565SCedric Chaumont 		/* v1.1: flags always set for digest operations */
245b0104773SPascal Brand 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
246b0104773SPascal Brand 		req_key_usage = 0;
247b0104773SPascal Brand 		break;
248b0104773SPascal Brand 
249b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_NOPAD:
250b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_NOPAD:
251b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_PKCS5:
252b0104773SPascal Brand 	case TEE_ALG_AES_CMAC:
253b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_PKCS5:
254b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
255b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
256b0104773SPascal Brand 	case TEE_ALG_HMAC_MD5:
257b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA1:
258b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA224:
259b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA256:
260b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA384:
261b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA512:
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);
277b0104773SPascal Brand 	op->info.mode = mode;
278b0104773SPascal Brand 	op->info.maxKeySize = maxKeySize;
279b0104773SPascal Brand 	op->info.requiredKeyUsage = req_key_usage;
280b0104773SPascal Brand 	op->info.handleState = handle_state;
281b0104773SPascal Brand 
282b0104773SPascal Brand 	if (block_size > 1) {
283b0104773SPascal Brand 		size_t buffer_size = block_size;
284b0104773SPascal Brand 
285b0104773SPascal Brand 		if (buffer_two_blocks)
286b0104773SPascal Brand 			buffer_size *= 2;
287b0104773SPascal Brand 
2889b52c538SCedric Chaumont 		op->buffer = TEE_Malloc(buffer_size,
2899b52c538SCedric Chaumont 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
290b0104773SPascal Brand 		if (op->buffer == NULL) {
291b0104773SPascal Brand 			res = TEE_ERROR_OUT_OF_MEMORY;
292b66f219bSJens Wiklander 			goto out;
293b0104773SPascal Brand 		}
294b0104773SPascal Brand 	}
295b0104773SPascal Brand 	op->block_size = block_size;
296b0104773SPascal Brand 	op->buffer_two_blocks = buffer_two_blocks;
297b0104773SPascal Brand 
298b0104773SPascal Brand 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
299b0104773SPascal Brand 		uint32_t mks = maxKeySize;
300b0104773SPascal Brand 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
301b0104773SPascal Brand 						       with_private_key);
302b0104773SPascal Brand 
303b0104773SPascal Brand 		/*
304b0104773SPascal Brand 		 * If two keys are expected the max key size is the sum of
305b0104773SPascal Brand 		 * the size of both keys.
306b0104773SPascal Brand 		 */
307b0104773SPascal Brand 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
308b0104773SPascal Brand 			mks /= 2;
309b0104773SPascal Brand 
310b0104773SPascal Brand 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
311b0104773SPascal Brand 		if (res != TEE_SUCCESS)
312b66f219bSJens Wiklander 			goto out;
313b0104773SPascal Brand 
31405304565SCedric Chaumont 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
3159b52c538SCedric Chaumont 			res = TEE_AllocateTransientObject(key_type, mks,
316b0104773SPascal Brand 							  &op->key2);
317b0104773SPascal Brand 			if (res != TEE_SUCCESS)
318b66f219bSJens Wiklander 				goto out;
319b0104773SPascal Brand 		}
320b0104773SPascal Brand 	}
321b0104773SPascal Brand 
322e86f1266SJens Wiklander 	res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1,
323e86f1266SJens Wiklander 				    (unsigned long)op->key2, &op->state);
324b66f219bSJens Wiklander 	if (res != TEE_SUCCESS)
325b66f219bSJens Wiklander 		goto out;
326b0104773SPascal Brand 
32705304565SCedric Chaumont 	/*
32805304565SCedric Chaumont 	 * Initialize digest operations
32905304565SCedric Chaumont 	 * Other multi-stage operations initialized w/ TEE_xxxInit functions
33005304565SCedric Chaumont 	 * Non-applicable on asymmetric operations
33105304565SCedric Chaumont 	 */
33205304565SCedric Chaumont 	if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) {
33305304565SCedric Chaumont 		res = utee_hash_init(op->state, NULL, 0);
33405304565SCedric Chaumont 		if (res != TEE_SUCCESS)
335b66f219bSJens Wiklander 			goto out;
33605304565SCedric Chaumont 		/* v1.1: flags always set for digest operations */
33705304565SCedric Chaumont 		op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
33805304565SCedric Chaumont 	}
33905304565SCedric Chaumont 
340642a1607SCedric Chaumont 	op->operationState = TEE_OPERATION_STATE_INITIAL;
341642a1607SCedric Chaumont 
342b0104773SPascal Brand 	*operation = op;
343b0104773SPascal Brand 
344b66f219bSJens Wiklander out:
345b66f219bSJens Wiklander 	if (res != TEE_SUCCESS) {
346b66f219bSJens Wiklander 		if (res != TEE_ERROR_OUT_OF_MEMORY &&
3479b52c538SCedric Chaumont 		    res != TEE_ERROR_NOT_SUPPORTED)
3489b52c538SCedric Chaumont 			TEE_Panic(0);
349b66f219bSJens Wiklander 		if (op) {
350b66f219bSJens Wiklander 			if (op->state) {
351b66f219bSJens Wiklander 				TEE_FreeOperation(op);
352b66f219bSJens Wiklander 			} else {
353b66f219bSJens Wiklander 				TEE_Free(op->buffer);
354b66f219bSJens Wiklander 				TEE_FreeTransientObject(op->key1);
355b66f219bSJens Wiklander 				TEE_FreeTransientObject(op->key2);
356b66f219bSJens Wiklander 				TEE_Free(op);
357b66f219bSJens Wiklander 			}
358b66f219bSJens Wiklander 		}
359b66f219bSJens Wiklander 	}
360b66f219bSJens Wiklander 
361b0104773SPascal Brand 	return res;
362b0104773SPascal Brand }
363b0104773SPascal Brand 
364b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation)
365b0104773SPascal Brand {
366e889e80bSCedric Chaumont 	TEE_Result res;
367e889e80bSCedric Chaumont 
368e889e80bSCedric Chaumont 	if (operation == TEE_HANDLE_NULL)
369e889e80bSCedric Chaumont 		TEE_Panic(0);
370e889e80bSCedric Chaumont 
371b0104773SPascal Brand 	/*
372b0104773SPascal Brand 	 * Note that keys should not be freed here, since they are
373b0104773SPascal Brand 	 * claimed by the operation they will be freed by
374b0104773SPascal Brand 	 * utee_cryp_state_free().
375b0104773SPascal Brand 	 */
376e889e80bSCedric Chaumont 	res = utee_cryp_state_free(operation->state);
377e889e80bSCedric Chaumont 	if (res != TEE_SUCCESS)
378e889e80bSCedric Chaumont 		TEE_Panic(0);
379e889e80bSCedric Chaumont 
380b0104773SPascal Brand 	TEE_Free(operation->buffer);
381b0104773SPascal Brand 	TEE_Free(operation);
382b0104773SPascal Brand }
383b0104773SPascal Brand 
384b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation,
385b0104773SPascal Brand 			  TEE_OperationInfo *operationInfo)
386b0104773SPascal Brand {
387b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
388b0104773SPascal Brand 		TEE_Panic(0);
389b0104773SPascal Brand 
39005304565SCedric Chaumont 	if (!operationInfo)
391b0104773SPascal Brand 		TEE_Panic(0);
392b0104773SPascal Brand 
393b0104773SPascal Brand 	*operationInfo = operation->info;
394b0104773SPascal Brand }
395b0104773SPascal Brand 
39605304565SCedric Chaumont TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation,
39705304565SCedric Chaumont 			  TEE_OperationInfoMultiple *operationInfoMultiple,
39805304565SCedric Chaumont 			  uint32_t *operationSize)
39905304565SCedric Chaumont {
40005304565SCedric Chaumont 	TEE_Result res = TEE_SUCCESS;
40105304565SCedric Chaumont 	TEE_ObjectInfo key_info1;
40205304565SCedric Chaumont 	TEE_ObjectInfo key_info2;
40305304565SCedric Chaumont 	uint32_t num_of_keys;
40405304565SCedric Chaumont 	size_t n;
40505304565SCedric Chaumont 
40605304565SCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
40705304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
40805304565SCedric Chaumont 		goto out;
40905304565SCedric Chaumont 	}
41005304565SCedric Chaumont 
41105304565SCedric Chaumont 	if (!operationInfoMultiple) {
41205304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
41305304565SCedric Chaumont 		goto out;
41405304565SCedric Chaumont 	}
41505304565SCedric Chaumont 
41605304565SCedric Chaumont 	if (!operationSize) {
41705304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
41805304565SCedric Chaumont 		goto out;
41905304565SCedric Chaumont 	}
42005304565SCedric Chaumont 
42105304565SCedric Chaumont 	num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/
42205304565SCedric Chaumont 			sizeof(TEE_OperationInfoKey);
42305304565SCedric Chaumont 
42405304565SCedric Chaumont 	if (num_of_keys > 2) {
42505304565SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
42605304565SCedric Chaumont 		goto out;
42705304565SCedric Chaumont 	}
42805304565SCedric Chaumont 
42905304565SCedric Chaumont 	/* Two keys flag (TEE_ALG_AES_XTS only) */
43005304565SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
43105304565SCedric Chaumont 	    0 &&
43205304565SCedric Chaumont 	    (num_of_keys != 2)) {
43305304565SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
43405304565SCedric Chaumont 		goto out;
43505304565SCedric Chaumont 	}
43605304565SCedric Chaumont 
43705304565SCedric Chaumont 	/* Clear */
43805304565SCedric Chaumont 	for (n = 0; n < num_of_keys; n++) {
43905304565SCedric Chaumont 		operationInfoMultiple->keyInformation[n].keySize = 0;
44005304565SCedric Chaumont 		operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0;
44105304565SCedric Chaumont 	}
44205304565SCedric Chaumont 
44305304565SCedric Chaumont 	if (num_of_keys == 2) {
44405304565SCedric Chaumont 		res = TEE_GetObjectInfo1(operation->key2, &key_info2);
44505304565SCedric Chaumont 		/* Key2 is not a valid handle */
44605304565SCedric Chaumont 		if (res != TEE_SUCCESS)
44705304565SCedric Chaumont 			goto out;
44805304565SCedric Chaumont 
44905304565SCedric Chaumont 		operationInfoMultiple->keyInformation[1].keySize =
45005304565SCedric Chaumont 			key_info2.keySize;
45105304565SCedric Chaumont 		operationInfoMultiple->keyInformation[1].requiredKeyUsage =
45205304565SCedric Chaumont 			operation->info.requiredKeyUsage;
45305304565SCedric Chaumont 	}
45405304565SCedric Chaumont 
45505304565SCedric Chaumont 	if (num_of_keys >= 1) {
45605304565SCedric Chaumont 		res = TEE_GetObjectInfo1(operation->key1, &key_info1);
45705304565SCedric Chaumont 		/* Key1 is not a valid handle */
45805304565SCedric Chaumont 		if (res != TEE_SUCCESS) {
45905304565SCedric Chaumont 			if (num_of_keys == 2) {
46005304565SCedric Chaumont 				operationInfoMultiple->keyInformation[1].
46105304565SCedric Chaumont 							keySize = 0;
46205304565SCedric Chaumont 				operationInfoMultiple->keyInformation[1].
46305304565SCedric Chaumont 							requiredKeyUsage = 0;
46405304565SCedric Chaumont 			}
46505304565SCedric Chaumont 			goto out;
46605304565SCedric Chaumont 		}
46705304565SCedric Chaumont 
46805304565SCedric Chaumont 		operationInfoMultiple->keyInformation[0].keySize =
46905304565SCedric Chaumont 			key_info1.keySize;
47005304565SCedric Chaumont 		operationInfoMultiple->keyInformation[0].requiredKeyUsage =
47105304565SCedric Chaumont 			operation->info.requiredKeyUsage;
47205304565SCedric Chaumont 	}
47305304565SCedric Chaumont 
47405304565SCedric Chaumont 	/* No key */
47505304565SCedric Chaumont 	operationInfoMultiple->algorithm = operation->info.algorithm;
47605304565SCedric Chaumont 	operationInfoMultiple->operationClass = operation->info.operationClass;
47705304565SCedric Chaumont 	operationInfoMultiple->mode = operation->info.mode;
47805304565SCedric Chaumont 	operationInfoMultiple->digestLength = operation->info.digestLength;
47905304565SCedric Chaumont 	operationInfoMultiple->maxKeySize = operation->info.maxKeySize;
48005304565SCedric Chaumont 	operationInfoMultiple->handleState = operation->info.handleState;
48105304565SCedric Chaumont 	operationInfoMultiple->operationState = operation->operationState;
48205304565SCedric Chaumont 	operationInfoMultiple->numberOfKeys = num_of_keys;
48305304565SCedric Chaumont 
48405304565SCedric Chaumont out:
48505304565SCedric Chaumont 	if (res != TEE_SUCCESS &&
48605304565SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
48705304565SCedric Chaumont 		TEE_Panic(0);
48805304565SCedric Chaumont 
48905304565SCedric Chaumont 	return res;
49005304565SCedric Chaumont }
49105304565SCedric Chaumont 
492b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation)
493b0104773SPascal Brand {
494b0104773SPascal Brand 	TEE_Result res;
495b0104773SPascal Brand 
496b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
497b0104773SPascal Brand 		TEE_Panic(0);
498bf80076aSCedric Chaumont 
499642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET))
500bf80076aSCedric Chaumont 			TEE_Panic(0);
501bf80076aSCedric Chaumont 
502642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
503642a1607SCedric Chaumont 
504b0104773SPascal Brand 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
505b0104773SPascal Brand 		res = utee_hash_init(operation->state, NULL, 0);
506b0104773SPascal Brand 		if (res != TEE_SUCCESS)
507b0104773SPascal Brand 			TEE_Panic(res);
50805304565SCedric Chaumont 		operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
50905304565SCedric Chaumont 	} else {
510b0104773SPascal Brand 		operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
511b0104773SPascal Brand 	}
51205304565SCedric Chaumont }
513b0104773SPascal Brand 
514b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
515b0104773SPascal Brand 			       TEE_ObjectHandle key)
516b0104773SPascal Brand {
5177583c59eSCedric Chaumont 	TEE_Result res;
518b0104773SPascal Brand 	uint32_t key_size = 0;
519b0104773SPascal Brand 	TEE_ObjectInfo key_info;
520b0104773SPascal Brand 
521a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
522a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
523a57c1e2eSCedric Chaumont 		goto out;
524a57c1e2eSCedric Chaumont 	}
525a57c1e2eSCedric Chaumont 
526642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
527642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
528642a1607SCedric Chaumont 		goto out;
529642a1607SCedric Chaumont 	}
530642a1607SCedric Chaumont 
531a57c1e2eSCedric Chaumont 	if (key == TEE_HANDLE_NULL) {
532a57c1e2eSCedric Chaumont 		/* Operation key cleared */
533a57c1e2eSCedric Chaumont 		TEE_ResetTransientObject(operation->key1);
534a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
535a57c1e2eSCedric Chaumont 		goto out;
536a57c1e2eSCedric Chaumont 	}
537a57c1e2eSCedric Chaumont 
538a57c1e2eSCedric Chaumont 	/* No key for digest operation */
539a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
540a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
541a57c1e2eSCedric Chaumont 		goto out;
542a57c1e2eSCedric Chaumont 	}
543a57c1e2eSCedric Chaumont 
544a57c1e2eSCedric Chaumont 	/* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */
545a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
546a57c1e2eSCedric Chaumont 	    0) {
547a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
548a57c1e2eSCedric Chaumont 		goto out;
549a57c1e2eSCedric Chaumont 	}
550a57c1e2eSCedric Chaumont 
5517583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key, &key_info);
552a57c1e2eSCedric Chaumont 	/* Key is not a valid handle */
5537583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
554a57c1e2eSCedric Chaumont 		goto out;
5557583c59eSCedric Chaumont 
556b0104773SPascal Brand 	/* Supplied key has to meet required usage */
557b0104773SPascal Brand 	if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
558b0104773SPascal Brand 	    operation->info.requiredKeyUsage) {
559a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
560a57c1e2eSCedric Chaumont 		goto out;
561b0104773SPascal Brand 	}
562b0104773SPascal Brand 
563a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info.keySize) {
564a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
565a57c1e2eSCedric Chaumont 		goto out;
566a57c1e2eSCedric Chaumont 	}
567b0104773SPascal Brand 
5687583c59eSCedric Chaumont 	key_size = key_info.keySize;
569b0104773SPascal Brand 
570b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
571b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
572b0104773SPascal Brand 
5737583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key);
5747583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
575a57c1e2eSCedric Chaumont 		goto out;
5767583c59eSCedric Chaumont 
577b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
578b0104773SPascal Brand 
579b0104773SPascal Brand 	operation->info.keySize = key_size;
580b0104773SPascal Brand 
5817583c59eSCedric Chaumont out:
582a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
583a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
584a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
585a57c1e2eSCedric Chaumont 		TEE_Panic(0);
586a57c1e2eSCedric Chaumont 
587a57c1e2eSCedric Chaumont 	return res;
588b0104773SPascal Brand }
589b0104773SPascal Brand 
590b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
591b0104773SPascal Brand 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
592b0104773SPascal Brand {
5937583c59eSCedric Chaumont 	TEE_Result res;
594b0104773SPascal Brand 	uint32_t key_size = 0;
595b0104773SPascal Brand 	TEE_ObjectInfo key_info1;
596b0104773SPascal Brand 	TEE_ObjectInfo key_info2;
597b0104773SPascal Brand 
598a57c1e2eSCedric Chaumont 	if (operation == TEE_HANDLE_NULL) {
599a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
600a57c1e2eSCedric Chaumont 		goto out;
601a57c1e2eSCedric Chaumont 	}
602a57c1e2eSCedric Chaumont 
603642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
604642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
605642a1607SCedric Chaumont 		goto out;
606642a1607SCedric Chaumont 	}
607642a1607SCedric Chaumont 
608a57c1e2eSCedric Chaumont 	/*
609a57c1e2eSCedric Chaumont 	 * Key1/Key2 and/or are not initialized and
610a57c1e2eSCedric Chaumont 	 * Either both keys are NULL or both are not NULL
611a57c1e2eSCedric Chaumont 	 */
612a57c1e2eSCedric Chaumont 	if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) {
613a57c1e2eSCedric Chaumont 		/* Clear operation key1 (if needed) */
614a57c1e2eSCedric Chaumont 		if (key1 == TEE_HANDLE_NULL)
615a57c1e2eSCedric Chaumont 			TEE_ResetTransientObject(operation->key1);
616a57c1e2eSCedric Chaumont 		/* Clear operation key2 (if needed) */
617a57c1e2eSCedric Chaumont 		if (key2 == TEE_HANDLE_NULL)
618a57c1e2eSCedric Chaumont 			TEE_ResetTransientObject(operation->key2);
619a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
620a57c1e2eSCedric Chaumont 		goto out;
621a57c1e2eSCedric Chaumont 	}
622a57c1e2eSCedric Chaumont 
623a57c1e2eSCedric Chaumont 	/* No key for digest operation */
624a57c1e2eSCedric Chaumont 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
625a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
626a57c1e2eSCedric Chaumont 		goto out;
627a57c1e2eSCedric Chaumont 	}
628a57c1e2eSCedric Chaumont 
629a57c1e2eSCedric Chaumont 	/* Two keys flag expected (TEE_ALG_AES_XTS only) */
630a57c1e2eSCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
631a57c1e2eSCedric Chaumont 	    0) {
632a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
633a57c1e2eSCedric Chaumont 		goto out;
634a57c1e2eSCedric Chaumont 	}
635a57c1e2eSCedric Chaumont 
6367583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key1, &key_info1);
637a57c1e2eSCedric Chaumont 	/* Key1 is not a valid handle */
6387583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
639a57c1e2eSCedric Chaumont 		goto out;
6407583c59eSCedric Chaumont 
641b0104773SPascal Brand 	/* Supplied key has to meet required usage */
642b0104773SPascal Brand 	if ((key_info1.objectUsage & operation->info.
643b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
644a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
645a57c1e2eSCedric Chaumont 		goto out;
646b0104773SPascal Brand 	}
647b0104773SPascal Brand 
6487583c59eSCedric Chaumont 	res = TEE_GetObjectInfo1(key2, &key_info2);
649a57c1e2eSCedric Chaumont 	/* Key2 is not a valid handle */
6507583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
6517583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
6527583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
653a57c1e2eSCedric Chaumont 		goto out;
6547583c59eSCedric Chaumont 	}
6557583c59eSCedric Chaumont 
656b0104773SPascal Brand 	/* Supplied key has to meet required usage */
657b0104773SPascal Brand 	if ((key_info2.objectUsage & operation->info.
658b0104773SPascal Brand 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
659a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
660a57c1e2eSCedric Chaumont 		goto out;
661b0104773SPascal Brand 	}
662b0104773SPascal Brand 
663b0104773SPascal Brand 	/*
664b0104773SPascal Brand 	 * AES-XTS (the only multi key algorithm supported, requires the
665b0104773SPascal Brand 	 * keys to be of equal size.
666b0104773SPascal Brand 	 */
667b0104773SPascal Brand 	if (operation->info.algorithm == TEE_ALG_AES_XTS &&
668a57c1e2eSCedric Chaumont 	    key_info1.keySize != key_info2.keySize) {
669a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
670a57c1e2eSCedric Chaumont 		goto out;
671b0104773SPascal Brand 
672a57c1e2eSCedric Chaumont 	}
673a57c1e2eSCedric Chaumont 
674a57c1e2eSCedric Chaumont 	if (operation->info.maxKeySize < key_info1.keySize) {
675a57c1e2eSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
676a57c1e2eSCedric Chaumont 		goto out;
677a57c1e2eSCedric Chaumont 	}
678b0104773SPascal Brand 
679b0104773SPascal Brand 	/*
680b0104773SPascal Brand 	 * Odd that only the size of one key should be reported while
681b0104773SPascal Brand 	 * size of two key are used when allocating the operation.
682b0104773SPascal Brand 	 */
6837583c59eSCedric Chaumont 	key_size = key_info1.keySize;
684b0104773SPascal Brand 
685b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
686b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key2);
687b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
688b0104773SPascal Brand 
6897583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key1, key1);
6907583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
691a57c1e2eSCedric Chaumont 		goto out;
6927583c59eSCedric Chaumont 
6937583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(operation->key2, key2);
6947583c59eSCedric Chaumont 	if (res != TEE_SUCCESS) {
6957583c59eSCedric Chaumont 		if (res == TEE_ERROR_CORRUPT_OBJECT)
6967583c59eSCedric Chaumont 			res = TEE_ERROR_CORRUPT_OBJECT_2;
697a57c1e2eSCedric Chaumont 		goto out;
6987583c59eSCedric Chaumont 	}
6997583c59eSCedric Chaumont 
700b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
701b0104773SPascal Brand 
702b0104773SPascal Brand 	operation->info.keySize = key_size;
703b0104773SPascal Brand 
7047583c59eSCedric Chaumont out:
705a57c1e2eSCedric Chaumont 	if (res != TEE_SUCCESS  &&
706a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
707a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT_2 &&
708a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE &&
709a57c1e2eSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
710a57c1e2eSCedric Chaumont 		TEE_Panic(0);
711a57c1e2eSCedric Chaumont 
712a57c1e2eSCedric Chaumont 	return res;
713b0104773SPascal Brand }
714b0104773SPascal Brand 
715b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
716b0104773SPascal Brand {
717b0104773SPascal Brand 	TEE_Result res;
718b0104773SPascal Brand 
719b0104773SPascal Brand 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
720b0104773SPascal Brand 		TEE_Panic(0);
721b0104773SPascal Brand 	if (dst_op->info.algorithm != src_op->info.algorithm)
722b0104773SPascal Brand 		TEE_Panic(0);
723b0104773SPascal Brand 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
724b0104773SPascal Brand 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
725b0104773SPascal Brand 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
726b0104773SPascal Brand 
727b0104773SPascal Brand 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
728b0104773SPascal Brand 			key1 = src_op->key1;
729b0104773SPascal Brand 			key2 = src_op->key2;
730b0104773SPascal Brand 		}
731b0104773SPascal Brand 
732b0104773SPascal Brand 		if ((src_op->info.handleState &
733b0104773SPascal Brand 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
734b0104773SPascal Brand 			TEE_SetOperationKey(dst_op, key1);
735b0104773SPascal Brand 		} else {
736b0104773SPascal Brand 			TEE_SetOperationKey2(dst_op, key1, key2);
737b0104773SPascal Brand 		}
738b0104773SPascal Brand 	}
739b0104773SPascal Brand 	dst_op->info.handleState = src_op->info.handleState;
740b0104773SPascal Brand 	dst_op->info.keySize = src_op->info.keySize;
741642a1607SCedric Chaumont 	dst_op->operationState = src_op->operationState;
742b0104773SPascal Brand 
743b0104773SPascal Brand 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
744b0104773SPascal Brand 	    dst_op->block_size != src_op->block_size)
745b0104773SPascal Brand 		TEE_Panic(0);
746b0104773SPascal Brand 
747b0104773SPascal Brand 	if (dst_op->buffer != NULL) {
748b0104773SPascal Brand 		if (src_op->buffer == NULL)
749b0104773SPascal Brand 			TEE_Panic(0);
750b0104773SPascal Brand 
751b0104773SPascal Brand 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
752b0104773SPascal Brand 		dst_op->buffer_offs = src_op->buffer_offs;
753b0104773SPascal Brand 	} else if (src_op->buffer != NULL) {
754b0104773SPascal Brand 		TEE_Panic(0);
755b0104773SPascal Brand 	}
756b0104773SPascal Brand 
757b0104773SPascal Brand 	res = utee_cryp_state_copy(dst_op->state, src_op->state);
758b0104773SPascal Brand 	if (res != TEE_SUCCESS)
759b0104773SPascal Brand 		TEE_Panic(res);
760b0104773SPascal Brand }
761b0104773SPascal Brand 
762b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */
763b0104773SPascal Brand 
7646d15db08SJerome Forissier static void init_hash_operation(TEE_OperationHandle operation, void *IV,
7656d15db08SJerome Forissier 				uint32_t IVLen)
7666d15db08SJerome Forissier {
7676d15db08SJerome Forissier 	TEE_Result res;
7686d15db08SJerome Forissier 
7696d15db08SJerome Forissier 	/*
7706d15db08SJerome Forissier 	 * Note : IV and IVLen are never used in current implementation
7716d15db08SJerome Forissier 	 * This is why coherent values of IV and IVLen are not checked
7726d15db08SJerome Forissier 	 */
7736d15db08SJerome Forissier 	res = utee_hash_init(operation->state, IV, IVLen);
7746d15db08SJerome Forissier 	if (res != TEE_SUCCESS)
7756d15db08SJerome Forissier 		TEE_Panic(res);
7766d15db08SJerome Forissier 	operation->buffer_offs = 0;
7776d15db08SJerome Forissier 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
7786d15db08SJerome Forissier }
7796d15db08SJerome Forissier 
780b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation,
78179a3c601SCedric Chaumont 		      void *chunk, uint32_t chunkSize)
782b0104773SPascal Brand {
78373d6c3baSJoakim Bech 	TEE_Result res = TEE_ERROR_GENERIC;
784b0104773SPascal Brand 
78573d6c3baSJoakim Bech 	if (operation == TEE_HANDLE_NULL ||
78673d6c3baSJoakim Bech 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
787b0104773SPascal Brand 		TEE_Panic(0);
78873d6c3baSJoakim Bech 
789642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
790642a1607SCedric Chaumont 
791b0104773SPascal Brand 	res = utee_hash_update(operation->state, chunk, chunkSize);
792b0104773SPascal Brand 	if (res != TEE_SUCCESS)
793b0104773SPascal Brand 		TEE_Panic(res);
794b0104773SPascal Brand }
795b0104773SPascal Brand 
796642a1607SCedric Chaumont TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, void *chunk,
79779a3c601SCedric Chaumont 			     uint32_t chunkLen, void *hash, uint32_t *hashLen)
798b0104773SPascal Brand {
79987c2f6b6SCedric Chaumont 	TEE_Result res;
800e86f1266SJens Wiklander 	uint64_t hl;
80187c2f6b6SCedric Chaumont 
80287c2f6b6SCedric Chaumont 	if ((operation == TEE_HANDLE_NULL) ||
80387c2f6b6SCedric Chaumont 	    (!chunk && chunkLen) ||
80487c2f6b6SCedric Chaumont 	    !hash ||
80587c2f6b6SCedric Chaumont 	    !hashLen ||
80687c2f6b6SCedric Chaumont 	    (operation->info.operationClass != TEE_OPERATION_DIGEST)) {
80787c2f6b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
80887c2f6b6SCedric Chaumont 		goto out;
80987c2f6b6SCedric Chaumont 	}
81087c2f6b6SCedric Chaumont 
811e86f1266SJens Wiklander 	hl = *hashLen;
812e86f1266SJens Wiklander 	res = utee_hash_final(operation->state, chunk, chunkLen, hash, &hl);
813e86f1266SJens Wiklander 	*hashLen = hl;
8146d15db08SJerome Forissier 	if (res != TEE_SUCCESS)
8156d15db08SJerome Forissier 		goto out;
8166d15db08SJerome Forissier 
8176d15db08SJerome Forissier 	/* Reset operation state */
8186d15db08SJerome Forissier 	init_hash_operation(operation, NULL, 0);
81987c2f6b6SCedric Chaumont 
820642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
821642a1607SCedric Chaumont 
82287c2f6b6SCedric Chaumont out:
82387c2f6b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
82487c2f6b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
825b0104773SPascal Brand 		TEE_Panic(0);
82673d6c3baSJoakim Bech 
82787c2f6b6SCedric Chaumont 	return res;
828b0104773SPascal Brand }
829b0104773SPascal Brand 
830b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */
831b0104773SPascal Brand 
832642a1607SCedric Chaumont void TEE_CipherInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen)
833b0104773SPascal Brand {
834b0104773SPascal Brand 	TEE_Result res;
835b0104773SPascal Brand 
836b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
837b0104773SPascal Brand 		TEE_Panic(0);
838642a1607SCedric Chaumont 
839b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
840b0104773SPascal Brand 		TEE_Panic(0);
841642a1607SCedric Chaumont 
842642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
843642a1607SCedric Chaumont 	    !(operation->key1))
844642a1607SCedric Chaumont 		TEE_Panic(0);
845642a1607SCedric Chaumont 
846642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
847642a1607SCedric Chaumont 		TEE_ResetOperation(operation);
848642a1607SCedric Chaumont 
849642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
850642a1607SCedric Chaumont 
851b0104773SPascal Brand 	res = utee_cipher_init(operation->state, IV, IVLen);
852b0104773SPascal Brand 	if (res != TEE_SUCCESS)
853b0104773SPascal Brand 		TEE_Panic(res);
854642a1607SCedric Chaumont 
855b0104773SPascal Brand 	operation->buffer_offs = 0;
856b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
857b0104773SPascal Brand }
858b0104773SPascal Brand 
859b0104773SPascal Brand static TEE_Result tee_buffer_update(
860b0104773SPascal Brand 		TEE_OperationHandle op,
861e86f1266SJens Wiklander 		TEE_Result(*update_func)(unsigned long state, const void *src,
862e86f1266SJens Wiklander 				size_t slen, void *dst, uint64_t *dlen),
863b0104773SPascal Brand 		const void *src_data, size_t src_len,
864e86f1266SJens Wiklander 		void *dest_data, uint64_t *dest_len)
865b0104773SPascal Brand {
866b0104773SPascal Brand 	TEE_Result res;
867b0104773SPascal Brand 	const uint8_t *src = src_data;
868b0104773SPascal Brand 	size_t slen = src_len;
869b0104773SPascal Brand 	uint8_t *dst = dest_data;
870b0104773SPascal Brand 	size_t dlen = *dest_len;
871b0104773SPascal Brand 	size_t acc_dlen = 0;
872e86f1266SJens Wiklander 	uint64_t tmp_dlen;
873b0104773SPascal Brand 	size_t l;
874b0104773SPascal Brand 	size_t buffer_size;
875d3588802SPascal Brand 	size_t buffer_left;
876b0104773SPascal Brand 
877d3588802SPascal Brand 	if (op->buffer_two_blocks) {
878b0104773SPascal Brand 		buffer_size = op->block_size * 2;
879d3588802SPascal Brand 		buffer_left = 1;
880d3588802SPascal Brand 	} else {
881b0104773SPascal Brand 		buffer_size = op->block_size;
882d3588802SPascal Brand 		buffer_left = 0;
883d3588802SPascal Brand 	}
884b0104773SPascal Brand 
885b0104773SPascal Brand 	if (op->buffer_offs > 0) {
886b0104773SPascal Brand 		/* Fill up complete block */
887b0104773SPascal Brand 		if (op->buffer_offs < op->block_size)
888b0104773SPascal Brand 			l = MIN(slen, op->block_size - op->buffer_offs);
889b0104773SPascal Brand 		else
890b0104773SPascal Brand 			l = MIN(slen, buffer_size - op->buffer_offs);
891b0104773SPascal Brand 		memcpy(op->buffer + op->buffer_offs, src, l);
892b0104773SPascal Brand 		op->buffer_offs += l;
893b0104773SPascal Brand 		src += l;
894b0104773SPascal Brand 		slen -= l;
895b0104773SPascal Brand 		if ((op->buffer_offs % op->block_size) != 0)
896b0104773SPascal Brand 			goto out;	/* Nothing left to do */
897b0104773SPascal Brand 	}
898b0104773SPascal Brand 
899b0104773SPascal Brand 	/* If we can feed from buffer */
900d3588802SPascal Brand 	if ((op->buffer_offs > 0) &&
901d3588802SPascal Brand 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
9022ff3fdbbSPascal Brand 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
903b0104773SPascal Brand 				op->block_size);
904b0104773SPascal Brand 		l = MIN(op->buffer_offs, l);
905b0104773SPascal Brand 		tmp_dlen = dlen;
906b0104773SPascal Brand 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
907b0104773SPascal Brand 		if (res != TEE_SUCCESS)
908b0104773SPascal Brand 			TEE_Panic(res);
909b0104773SPascal Brand 		dst += tmp_dlen;
910b0104773SPascal Brand 		dlen -= tmp_dlen;
911b0104773SPascal Brand 		acc_dlen += tmp_dlen;
912b0104773SPascal Brand 		op->buffer_offs -= l;
913b0104773SPascal Brand 		if (op->buffer_offs > 0) {
914b0104773SPascal Brand 			/*
915b0104773SPascal Brand 			 * Slen is small enough to be contained in rest buffer.
916b0104773SPascal Brand 			 */
917b0104773SPascal Brand 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
918b0104773SPascal Brand 			memcpy(op->buffer + op->buffer_offs, src, slen);
919b0104773SPascal Brand 			op->buffer_offs += slen;
920b0104773SPascal Brand 			goto out;	/* Nothing left to do */
921b0104773SPascal Brand 		}
922b0104773SPascal Brand 	}
923b0104773SPascal Brand 
924d3588802SPascal Brand 	if (slen >= (buffer_size + buffer_left)) {
925b0104773SPascal Brand 		/* Buffer is empty, feed as much as possible from src */
9262ff3fdbbSPascal Brand 		l = ROUNDUP(slen - buffer_size + 1, op->block_size);
927b0104773SPascal Brand 
928b0104773SPascal Brand 		tmp_dlen = dlen;
929b0104773SPascal Brand 		res = update_func(op->state, src, l, dst, &tmp_dlen);
930b0104773SPascal Brand 		if (res != TEE_SUCCESS)
931b0104773SPascal Brand 			TEE_Panic(res);
932b0104773SPascal Brand 		src += l;
933b0104773SPascal Brand 		slen -= l;
934b0104773SPascal Brand 		dst += tmp_dlen;
935b0104773SPascal Brand 		dlen -= tmp_dlen;
936b0104773SPascal Brand 		acc_dlen += tmp_dlen;
937b0104773SPascal Brand 	}
938b0104773SPascal Brand 
939b0104773SPascal Brand 	/* Slen is small enough to be contained in buffer. */
940b0104773SPascal Brand 	memcpy(op->buffer + op->buffer_offs, src, slen);
941b0104773SPascal Brand 	op->buffer_offs += slen;
942b0104773SPascal Brand 
943b0104773SPascal Brand out:
944b0104773SPascal Brand 	*dest_len = acc_dlen;
945b0104773SPascal Brand 	return TEE_SUCCESS;
946b0104773SPascal Brand }
947b0104773SPascal Brand 
948642a1607SCedric Chaumont TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, void *srcData,
94979a3c601SCedric Chaumont 			    uint32_t srcLen, void *destData, uint32_t *destLen)
950b0104773SPascal Brand {
951dea1f2b6SCedric Chaumont 	TEE_Result res;
952b0104773SPascal Brand 	size_t req_dlen;
953e86f1266SJens Wiklander 	uint64_t dl;
954b0104773SPascal Brand 
955642a1607SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
956dea1f2b6SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
957dea1f2b6SCedric Chaumont 	    destLen == NULL ||
958dea1f2b6SCedric Chaumont 	    (destData == NULL && *destLen != 0)) {
959dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
960dea1f2b6SCedric Chaumont 		goto out;
961dea1f2b6SCedric Chaumont 	}
962dea1f2b6SCedric Chaumont 
963642a1607SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
964dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
965dea1f2b6SCedric Chaumont 		goto out;
966dea1f2b6SCedric Chaumont 	}
967dea1f2b6SCedric Chaumont 
968642a1607SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
969642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
970642a1607SCedric Chaumont 		goto out;
971642a1607SCedric Chaumont 	}
972642a1607SCedric Chaumont 
973642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
974dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
975dea1f2b6SCedric Chaumont 		goto out;
976dea1f2b6SCedric Chaumont 	}
977b0104773SPascal Brand 
978e32c5ddfSJerome Forissier 	if (!srcData && !srcLen) {
979e32c5ddfSJerome Forissier 		res = TEE_SUCCESS;
980e32c5ddfSJerome Forissier 		goto out;
981e32c5ddfSJerome Forissier 	}
982e32c5ddfSJerome Forissier 
983b0104773SPascal Brand 	/* Calculate required dlen */
984642a1607SCedric Chaumont 	req_dlen = ((operation->buffer_offs + srcLen) / operation->block_size) *
985642a1607SCedric Chaumont 	    operation->block_size;
986642a1607SCedric Chaumont 	if (operation->buffer_two_blocks) {
987642a1607SCedric Chaumont 		if (req_dlen > operation->block_size * 2)
988642a1607SCedric Chaumont 			req_dlen -= operation->block_size * 2;
989b0104773SPascal Brand 		else
990b0104773SPascal Brand 			req_dlen = 0;
991b0104773SPascal Brand 	}
992b0104773SPascal Brand 	/*
993b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
994b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
995b0104773SPascal Brand 	 * can't restore sync with this API.
996b0104773SPascal Brand 	 */
997b0104773SPascal Brand 	if (*destLen < req_dlen) {
998b0104773SPascal Brand 		*destLen = req_dlen;
999dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1000dea1f2b6SCedric Chaumont 		goto out;
1001b0104773SPascal Brand 	}
1002b0104773SPascal Brand 
1003e86f1266SJens Wiklander 	dl = *destLen;
1004642a1607SCedric Chaumont 	res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen,
1005e86f1266SJens Wiklander 				destData, &dl);
1006e86f1266SJens Wiklander 	*destLen = dl;
1007b0104773SPascal Brand 
1008dea1f2b6SCedric Chaumont out:
1009dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1010dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1011dea1f2b6SCedric Chaumont 		TEE_Panic(0);
1012dea1f2b6SCedric Chaumont 
1013dea1f2b6SCedric Chaumont 	return res;
1014b0104773SPascal Brand }
1015b0104773SPascal Brand 
1016642a1607SCedric Chaumont TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation,
1017642a1607SCedric Chaumont 			     void *srcData, uint32_t srcLen, void *destData,
101879a3c601SCedric Chaumont 			     uint32_t *destLen)
1019b0104773SPascal Brand {
1020b0104773SPascal Brand 	TEE_Result res;
1021b0104773SPascal Brand 	uint8_t *dst = destData;
1022b0104773SPascal Brand 	size_t acc_dlen = 0;
1023e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1024b0104773SPascal Brand 	size_t req_dlen;
1025b0104773SPascal Brand 
1026642a1607SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1027dea1f2b6SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
1028dea1f2b6SCedric Chaumont 	    destLen == NULL ||
1029dea1f2b6SCedric Chaumont 	    (destData == NULL && *destLen != 0)) {
1030dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1031dea1f2b6SCedric Chaumont 		goto out;
1032dea1f2b6SCedric Chaumont 	}
1033dea1f2b6SCedric Chaumont 
1034642a1607SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1035dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1036dea1f2b6SCedric Chaumont 		goto out;
1037dea1f2b6SCedric Chaumont 	}
1038dea1f2b6SCedric Chaumont 
1039642a1607SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1040642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1041642a1607SCedric Chaumont 		goto out;
1042642a1607SCedric Chaumont 	}
1043642a1607SCedric Chaumont 
1044642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1045dea1f2b6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1046dea1f2b6SCedric Chaumont 		goto out;
1047dea1f2b6SCedric Chaumont 	}
1048b0104773SPascal Brand 
1049b0104773SPascal Brand 	/*
1050b0104773SPascal Brand 	 * Check that the final block doesn't require padding for those
1051b0104773SPascal Brand 	 * algorithms that requires client to supply padding.
1052b0104773SPascal Brand 	 */
1053642a1607SCedric Chaumont 	if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
1054642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1055642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
1056642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1057642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
1058642a1607SCedric Chaumont 	    operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) {
1059642a1607SCedric Chaumont 		if (((operation->buffer_offs + srcLen) % operation->block_size)
1060642a1607SCedric Chaumont 		    != 0) {
1061dea1f2b6SCedric Chaumont 			res = TEE_ERROR_BAD_PARAMETERS;
1062dea1f2b6SCedric Chaumont 			goto out;
1063dea1f2b6SCedric Chaumont 		}
1064b0104773SPascal Brand 	}
1065b0104773SPascal Brand 
1066*f21873cdSJerome Forissier 	if (!srcData && !srcLen) {
1067*f21873cdSJerome Forissier 		res = TEE_SUCCESS;
1068*f21873cdSJerome Forissier 		goto out;
1069*f21873cdSJerome Forissier 	}
1070*f21873cdSJerome Forissier 
1071b0104773SPascal Brand 	/*
1072b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1073b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1074b0104773SPascal Brand 	 * can't restore sync with this API.
1075b0104773SPascal Brand 	 */
1076642a1607SCedric Chaumont 	req_dlen = operation->buffer_offs + srcLen;
1077b0104773SPascal Brand 	if (*destLen < req_dlen) {
1078b0104773SPascal Brand 		*destLen = req_dlen;
1079dea1f2b6SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1080dea1f2b6SCedric Chaumont 		goto out;
1081b0104773SPascal Brand 	}
1082b0104773SPascal Brand 
1083b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1084642a1607SCedric Chaumont 	res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen,
1085642a1607SCedric Chaumont 				dst, &tmp_dlen);
1086dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS)
1087dea1f2b6SCedric Chaumont 		goto out;
1088dea1f2b6SCedric Chaumont 
1089b0104773SPascal Brand 	dst += tmp_dlen;
1090b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1091b0104773SPascal Brand 
1092b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1093642a1607SCedric Chaumont 	res = utee_cipher_final(operation->state, operation->buffer,
1094642a1607SCedric Chaumont 				operation->buffer_offs, dst, &tmp_dlen);
1095b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1096dea1f2b6SCedric Chaumont 		goto out;
1097dea1f2b6SCedric Chaumont 
1098b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1099b0104773SPascal Brand 	*destLen = acc_dlen;
1100dea1f2b6SCedric Chaumont 
1101642a1607SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1102642a1607SCedric Chaumont 
1103642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1104642a1607SCedric Chaumont 
1105dea1f2b6SCedric Chaumont out:
1106dea1f2b6SCedric Chaumont 	if (res != TEE_SUCCESS &&
1107dea1f2b6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1108dea1f2b6SCedric Chaumont 		TEE_Panic(0);
1109dea1f2b6SCedric Chaumont 
1110dea1f2b6SCedric Chaumont 	return res;
1111b0104773SPascal Brand }
1112b0104773SPascal Brand 
1113b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */
1114b0104773SPascal Brand 
111528e0efc6SCedric Chaumont void TEE_MACInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen)
1116b0104773SPascal Brand {
1117b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
1118b0104773SPascal Brand 		TEE_Panic(0);
1119642a1607SCedric Chaumont 
1120b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1121b0104773SPascal Brand 		TEE_Panic(0);
1122642a1607SCedric Chaumont 
1123642a1607SCedric Chaumont 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1124642a1607SCedric Chaumont 	    !(operation->key1))
1125642a1607SCedric Chaumont 		TEE_Panic(0);
1126642a1607SCedric Chaumont 
1127642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1128642a1607SCedric Chaumont 		TEE_ResetOperation(operation);
1129642a1607SCedric Chaumont 
1130642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1131642a1607SCedric Chaumont 
11326d15db08SJerome Forissier 	init_hash_operation(operation, IV, IVLen);
1133b0104773SPascal Brand }
1134b0104773SPascal Brand 
113528e0efc6SCedric Chaumont void TEE_MACUpdate(TEE_OperationHandle operation, void *chunk,
113628e0efc6SCedric Chaumont 		   uint32_t chunkSize)
1137b0104773SPascal Brand {
1138b0104773SPascal Brand 	TEE_Result res;
1139b0104773SPascal Brand 
114028e0efc6SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
1141b0104773SPascal Brand 		TEE_Panic(0);
1142642a1607SCedric Chaumont 
114328e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1144b0104773SPascal Brand 		TEE_Panic(0);
1145642a1607SCedric Chaumont 
114628e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1147b0104773SPascal Brand 		TEE_Panic(0);
1148b0104773SPascal Brand 
1149642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE)
1150642a1607SCedric Chaumont 		TEE_Panic(0);
1151642a1607SCedric Chaumont 
115228e0efc6SCedric Chaumont 	res = utee_hash_update(operation->state, chunk, chunkSize);
1153b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1154b0104773SPascal Brand 		TEE_Panic(res);
1155b0104773SPascal Brand }
1156b0104773SPascal Brand 
115728e0efc6SCedric Chaumont TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
115828e0efc6SCedric Chaumont 			       void *message, uint32_t messageLen,
115979a3c601SCedric Chaumont 			       void *mac, uint32_t *macLen)
1160b0104773SPascal Brand {
1161b0104773SPascal Brand 	TEE_Result res;
1162e86f1266SJens Wiklander 	uint64_t ml;
1163b0104773SPascal Brand 
116428e0efc6SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
116528e0efc6SCedric Chaumont 	    (message == NULL && messageLen != 0) ||
116628e0efc6SCedric Chaumont 	    mac == NULL ||
116728e0efc6SCedric Chaumont 	    macLen == NULL) {
116828e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
116928e0efc6SCedric Chaumont 		goto out;
117028e0efc6SCedric Chaumont 	}
1171b0104773SPascal Brand 
117228e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
117328e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
117428e0efc6SCedric Chaumont 		goto out;
117528e0efc6SCedric Chaumont 	}
117628e0efc6SCedric Chaumont 
117728e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
117828e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
117928e0efc6SCedric Chaumont 		goto out;
118028e0efc6SCedric Chaumont 	}
118128e0efc6SCedric Chaumont 
1182642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1183642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1184642a1607SCedric Chaumont 		goto out;
1185642a1607SCedric Chaumont 	}
1186642a1607SCedric Chaumont 
1187e86f1266SJens Wiklander 	ml = *macLen;
1188e86f1266SJens Wiklander 	res = utee_hash_final(operation->state, message, messageLen, mac, &ml);
1189e86f1266SJens Wiklander 	*macLen = ml;
119028e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS)
119128e0efc6SCedric Chaumont 		goto out;
119228e0efc6SCedric Chaumont 
119328e0efc6SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
119428e0efc6SCedric Chaumont 
1195642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1196642a1607SCedric Chaumont 
119728e0efc6SCedric Chaumont out:
119828e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
119928e0efc6SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
120028e0efc6SCedric Chaumont 		TEE_Panic(res);
120128e0efc6SCedric Chaumont 
1202b0104773SPascal Brand 	return res;
1203b0104773SPascal Brand }
1204b0104773SPascal Brand 
1205b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
120628e0efc6SCedric Chaumont 			       void *message, uint32_t messageLen,
120728e0efc6SCedric Chaumont 			       void *mac, uint32_t macLen)
1208b0104773SPascal Brand {
1209b0104773SPascal Brand 	TEE_Result res;
1210b0104773SPascal Brand 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
12117f74c64aSPascal Brand 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1212b0104773SPascal Brand 
121328e0efc6SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
121428e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
121528e0efc6SCedric Chaumont 		goto out;
121628e0efc6SCedric Chaumont 	}
121728e0efc6SCedric Chaumont 
121828e0efc6SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
121928e0efc6SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
122028e0efc6SCedric Chaumont 		goto out;
122128e0efc6SCedric Chaumont 	}
122228e0efc6SCedric Chaumont 
1223642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1224642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1225642a1607SCedric Chaumont 		goto out;
1226642a1607SCedric Chaumont 	}
1227642a1607SCedric Chaumont 
1228b0104773SPascal Brand 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1229b0104773SPascal Brand 				  &computed_mac_size);
1230b0104773SPascal Brand 	if (res != TEE_SUCCESS)
123128e0efc6SCedric Chaumont 		goto out;
123228e0efc6SCedric Chaumont 
123328e0efc6SCedric Chaumont 	if (computed_mac_size != macLen) {
123428e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
123528e0efc6SCedric Chaumont 		goto out;
123628e0efc6SCedric Chaumont 	}
123728e0efc6SCedric Chaumont 
123828e0efc6SCedric Chaumont 	if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) {
123928e0efc6SCedric Chaumont 		res = TEE_ERROR_MAC_INVALID;
124028e0efc6SCedric Chaumont 		goto out;
124128e0efc6SCedric Chaumont 	}
124228e0efc6SCedric Chaumont 
1243642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1244642a1607SCedric Chaumont 
124528e0efc6SCedric Chaumont out:
124628e0efc6SCedric Chaumont 	if (res != TEE_SUCCESS &&
124728e0efc6SCedric Chaumont 	    res != TEE_ERROR_MAC_INVALID)
124828e0efc6SCedric Chaumont 		TEE_Panic(res);
124928e0efc6SCedric Chaumont 
1250b0104773SPascal Brand 	return res;
1251b0104773SPascal Brand }
1252b0104773SPascal Brand 
1253b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */
1254b0104773SPascal Brand 
1255b5816c88SCedric Chaumont TEE_Result TEE_AEInit(TEE_OperationHandle operation, void *nonce,
125679a3c601SCedric Chaumont 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1257b0104773SPascal Brand 		      uint32_t payloadLen)
1258b0104773SPascal Brand {
1259b0104773SPascal Brand 	TEE_Result res;
1260b0104773SPascal Brand 
1261b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL || nonce == NULL) {
1262b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1263b5816c88SCedric Chaumont 		goto out;
1264b5816c88SCedric Chaumont 	}
1265b5816c88SCedric Chaumont 
1266b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1267b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1268b5816c88SCedric Chaumont 		goto out;
1269b5816c88SCedric Chaumont 	}
1270b0104773SPascal Brand 
1271642a1607SCedric Chaumont 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
1272642a1607SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1273642a1607SCedric Chaumont 		goto out;
1274642a1607SCedric Chaumont 	}
1275642a1607SCedric Chaumont 
1276b0104773SPascal Brand 	/*
1277b0104773SPascal Brand 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1278b0104773SPascal Brand 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1279b0104773SPascal Brand 	 * according to the same principle so we have to check here instead to
1280b0104773SPascal Brand 	 * be GP compliant.
1281b0104773SPascal Brand 	 */
1282b5816c88SCedric Chaumont 	if (operation->info.algorithm == TEE_ALG_AES_GCM) {
1283b0104773SPascal Brand 		/*
1284b0104773SPascal Brand 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1285b0104773SPascal Brand 		 */
1286b5816c88SCedric Chaumont 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) {
1287b5816c88SCedric Chaumont 			res = TEE_ERROR_NOT_SUPPORTED;
1288b5816c88SCedric Chaumont 			goto out;
1289b5816c88SCedric Chaumont 		}
1290b0104773SPascal Brand 	}
1291b0104773SPascal Brand 
1292b5816c88SCedric Chaumont 	res = utee_authenc_init(operation->state, nonce, nonceLen,
1293b5816c88SCedric Chaumont 				tagLen / 8, AADLen, payloadLen);
1294b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1295b5816c88SCedric Chaumont 		goto out;
1296b5816c88SCedric Chaumont 
1297b5816c88SCedric Chaumont 	operation->ae_tag_len = tagLen / 8;
1298b5816c88SCedric Chaumont 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1299b5816c88SCedric Chaumont 
1300b5816c88SCedric Chaumont out:
1301b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1302b5816c88SCedric Chaumont 	    res != TEE_ERROR_NOT_SUPPORTED)
1303b0104773SPascal Brand 			TEE_Panic(res);
1304b5816c88SCedric Chaumont 
1305b0104773SPascal Brand 	return res;
1306b0104773SPascal Brand }
1307b0104773SPascal Brand 
1308b5816c88SCedric Chaumont void TEE_AEUpdateAAD(TEE_OperationHandle operation, void *AADdata,
130979a3c601SCedric Chaumont 		     uint32_t AADdataLen)
1310b0104773SPascal Brand {
1311b0104773SPascal Brand 	TEE_Result res;
1312b0104773SPascal Brand 
1313b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1314b5816c88SCedric Chaumont 	    (AADdata == NULL && AADdataLen != 0))
1315b0104773SPascal Brand 		TEE_Panic(0);
1316642a1607SCedric Chaumont 
1317b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE)
1318b0104773SPascal Brand 		TEE_Panic(0);
1319642a1607SCedric Chaumont 
1320b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1321b0104773SPascal Brand 		TEE_Panic(0);
1322b0104773SPascal Brand 
1323b5816c88SCedric Chaumont 	res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen);
1324642a1607SCedric Chaumont 
1325642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1326642a1607SCedric Chaumont 
1327b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1328b0104773SPascal Brand 		TEE_Panic(res);
1329b0104773SPascal Brand }
1330b0104773SPascal Brand 
1331b5816c88SCedric Chaumont TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, void *srcData,
133279a3c601SCedric Chaumont 			uint32_t srcLen, void *destData, uint32_t *destLen)
1333b0104773SPascal Brand {
1334b5816c88SCedric Chaumont 	TEE_Result res;
1335b0104773SPascal Brand 	size_t req_dlen;
1336e86f1266SJens Wiklander 	uint64_t dl;
1337b0104773SPascal Brand 
1338b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1339b5816c88SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
1340b5816c88SCedric Chaumont 	    destLen == NULL ||
1341b5816c88SCedric Chaumont 	    (destData == NULL && *destLen != 0)) {
1342b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1343b5816c88SCedric Chaumont 		goto out;
1344b5816c88SCedric Chaumont 	}
1345b5816c88SCedric Chaumont 
1346b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1347b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1348b5816c88SCedric Chaumont 		goto out;
1349b5816c88SCedric Chaumont 	}
1350b5816c88SCedric Chaumont 
1351b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1352b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1353b5816c88SCedric Chaumont 		goto out;
1354b5816c88SCedric Chaumont 	}
1355b0104773SPascal Brand 
1356b0104773SPascal Brand 	/*
1357b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1358b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1359b0104773SPascal Brand 	 * can't restore sync with this API.
1360b0104773SPascal Brand 	 */
1361b5816c88SCedric Chaumont 	req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen,
1362b5816c88SCedric Chaumont 			     operation->block_size);
1363b0104773SPascal Brand 	if (*destLen < req_dlen) {
1364b0104773SPascal Brand 		*destLen = req_dlen;
1365b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1366b5816c88SCedric Chaumont 		goto out;
1367b0104773SPascal Brand 	}
1368b0104773SPascal Brand 
1369e86f1266SJens Wiklander 	dl = *destLen;
1370b5816c88SCedric Chaumont 	res = tee_buffer_update(operation, utee_authenc_update_payload, srcData,
1371e86f1266SJens Wiklander 				srcLen, destData, &dl);
1372e86f1266SJens Wiklander 	*destLen = dl;
1373b0104773SPascal Brand 
1374642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1375642a1607SCedric Chaumont 
1376b5816c88SCedric Chaumont out:
1377b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1378b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1379b5816c88SCedric Chaumont 			TEE_Panic(res);
1380b5816c88SCedric Chaumont 
1381b5816c88SCedric Chaumont 	return res;
1382b0104773SPascal Brand }
1383b0104773SPascal Brand 
1384b5816c88SCedric Chaumont TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation,
1385b5816c88SCedric Chaumont 			      void *srcData, uint32_t srcLen,
138679a3c601SCedric Chaumont 			      void *destData, uint32_t *destLen, void *tag,
138779a3c601SCedric Chaumont 			      uint32_t *tagLen)
1388b0104773SPascal Brand {
1389b0104773SPascal Brand 	TEE_Result res;
1390b0104773SPascal Brand 	uint8_t *dst = destData;
1391b0104773SPascal Brand 	size_t acc_dlen = 0;
1392e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1393b0104773SPascal Brand 	size_t req_dlen;
1394e86f1266SJens Wiklander 	uint64_t tl;
1395b0104773SPascal Brand 
1396b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1397b5816c88SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
1398b5816c88SCedric Chaumont 	    destLen == NULL ||
1399b5816c88SCedric Chaumont 	    (destData == NULL && *destLen != 0) ||
1400b5816c88SCedric Chaumont 	    tag == NULL || tagLen == NULL) {
1401b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1402b5816c88SCedric Chaumont 		goto out;
1403b5816c88SCedric Chaumont 	}
1404b5816c88SCedric Chaumont 
1405b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1406b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1407b5816c88SCedric Chaumont 		goto out;
1408b5816c88SCedric Chaumont 	}
1409b5816c88SCedric Chaumont 
1410b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1411b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1412b5816c88SCedric Chaumont 		goto out;
1413b5816c88SCedric Chaumont 	}
1414b0104773SPascal Brand 
1415b0104773SPascal Brand 	/*
1416b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1417b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1418b0104773SPascal Brand 	 * can't restore sync with this API.
1419b0104773SPascal Brand 	 */
1420b5816c88SCedric Chaumont 	req_dlen = operation->buffer_offs + srcLen;
1421b0104773SPascal Brand 	if (*destLen < req_dlen) {
1422b0104773SPascal Brand 		*destLen = req_dlen;
1423b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1424b5816c88SCedric Chaumont 		goto out;
1425b0104773SPascal Brand 	}
1426b0104773SPascal Brand 
1427b0104773SPascal Brand 	/*
1428b0104773SPascal Brand 	 * Need to check this before update_payload since sync would be lost if
1429b0104773SPascal Brand 	 * we return short buffer after that.
1430b0104773SPascal Brand 	 */
1431b5816c88SCedric Chaumont 	if (*tagLen < operation->ae_tag_len) {
1432b5816c88SCedric Chaumont 		*tagLen = operation->ae_tag_len;
1433b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1434b5816c88SCedric Chaumont 		goto out;
1435b0104773SPascal Brand 	}
1436b0104773SPascal Brand 
1437b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1438b5816c88SCedric Chaumont 	res = tee_buffer_update(operation, utee_authenc_update_payload, srcData,
1439b5816c88SCedric Chaumont 				srcLen, dst, &tmp_dlen);
1440b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1441b5816c88SCedric Chaumont 		goto out;
1442b5816c88SCedric Chaumont 
1443b0104773SPascal Brand 	dst += tmp_dlen;
1444b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1445b0104773SPascal Brand 
1446b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1447e86f1266SJens Wiklander 	tl = *tagLen;
1448b5816c88SCedric Chaumont 	res = utee_authenc_enc_final(operation->state, operation->buffer,
1449b5816c88SCedric Chaumont 				     operation->buffer_offs, dst, &tmp_dlen,
1450e86f1266SJens Wiklander 				     tag, &tl);
1451e86f1266SJens Wiklander 	*tagLen = tl;
1452b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1453b5816c88SCedric Chaumont 		goto out;
1454b0104773SPascal Brand 
1455b5816c88SCedric Chaumont 	acc_dlen += tmp_dlen;
1456b0104773SPascal Brand 	*destLen = acc_dlen;
1457642a1607SCedric Chaumont 
1458b5816c88SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1459b5816c88SCedric Chaumont 
1460642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1461642a1607SCedric Chaumont 
1462b5816c88SCedric Chaumont out:
1463b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1464b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER)
1465b5816c88SCedric Chaumont 			TEE_Panic(res);
1466b0104773SPascal Brand 
1467b0104773SPascal Brand 	return res;
1468b0104773SPascal Brand }
1469b0104773SPascal Brand 
1470b5816c88SCedric Chaumont TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation,
1471b5816c88SCedric Chaumont 			      void *srcData, uint32_t srcLen,
1472b5816c88SCedric Chaumont 			      void *destData, uint32_t *destLen, void *tag,
147379a3c601SCedric Chaumont 			      uint32_t tagLen)
1474b0104773SPascal Brand {
1475b0104773SPascal Brand 	TEE_Result res;
1476b0104773SPascal Brand 	uint8_t *dst = destData;
1477b0104773SPascal Brand 	size_t acc_dlen = 0;
1478e86f1266SJens Wiklander 	uint64_t tmp_dlen;
1479b0104773SPascal Brand 	size_t req_dlen;
1480b0104773SPascal Brand 
1481b5816c88SCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
1482b5816c88SCedric Chaumont 	    (srcData == NULL && srcLen != 0) ||
1483b5816c88SCedric Chaumont 	    destLen == NULL ||
1484b5816c88SCedric Chaumont 	    (destData == NULL && *destLen != 0) ||
1485b5816c88SCedric Chaumont 	    (tag == NULL && tagLen != 0)) {
1486b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1487b5816c88SCedric Chaumont 		goto out;
1488b5816c88SCedric Chaumont 	}
1489b5816c88SCedric Chaumont 
1490b5816c88SCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1491b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1492b5816c88SCedric Chaumont 		goto out;
1493b5816c88SCedric Chaumont 	}
1494b5816c88SCedric Chaumont 
1495b5816c88SCedric Chaumont 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1496b5816c88SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
1497b5816c88SCedric Chaumont 		goto out;
1498b5816c88SCedric Chaumont 	}
1499b0104773SPascal Brand 
1500b0104773SPascal Brand 	/*
1501b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1502b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1503b0104773SPascal Brand 	 * can't restore sync with this API.
1504b0104773SPascal Brand 	 */
1505b5816c88SCedric Chaumont 	req_dlen = operation->buffer_offs + srcLen;
1506b0104773SPascal Brand 	if (*destLen < req_dlen) {
1507b0104773SPascal Brand 		*destLen = req_dlen;
1508b5816c88SCedric Chaumont 		res = TEE_ERROR_SHORT_BUFFER;
1509b5816c88SCedric Chaumont 		goto out;
1510b0104773SPascal Brand 	}
1511b0104773SPascal Brand 
1512b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1513b5816c88SCedric Chaumont 	res = tee_buffer_update(operation, utee_authenc_update_payload, srcData,
1514b5816c88SCedric Chaumont 				srcLen, dst, &tmp_dlen);
1515b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1516b5816c88SCedric Chaumont 		goto out;
1517b5816c88SCedric Chaumont 
1518b0104773SPascal Brand 	dst += tmp_dlen;
1519b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1520b0104773SPascal Brand 
1521b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1522b5816c88SCedric Chaumont 	res = utee_authenc_dec_final(operation->state, operation->buffer,
1523b5816c88SCedric Chaumont 				     operation->buffer_offs, dst, &tmp_dlen,
1524b5816c88SCedric Chaumont 				     tag, tagLen);
1525b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS)
1526b5816c88SCedric Chaumont 		goto out;
1527b5816c88SCedric Chaumont 
1528b0104773SPascal Brand 	/* Supplied tagLen should match what we initiated with */
1529b5816c88SCedric Chaumont 	if (tagLen != operation->ae_tag_len)
1530b0104773SPascal Brand 		res = TEE_ERROR_MAC_INVALID;
1531b0104773SPascal Brand 
1532b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1533b0104773SPascal Brand 	*destLen = acc_dlen;
1534642a1607SCedric Chaumont 
1535b5816c88SCedric Chaumont 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1536b5816c88SCedric Chaumont 
1537642a1607SCedric Chaumont 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1538642a1607SCedric Chaumont 
1539b5816c88SCedric Chaumont out:
1540b5816c88SCedric Chaumont 	if (res != TEE_SUCCESS &&
1541b5816c88SCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER &&
1542b5816c88SCedric Chaumont 	    res != TEE_ERROR_MAC_INVALID)
1543b5816c88SCedric Chaumont 			TEE_Panic(res);
1544b0104773SPascal Brand 
1545b0104773SPascal Brand 	return res;
1546b0104773SPascal Brand }
1547b0104773SPascal Brand 
1548b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */
1549b0104773SPascal Brand 
155012e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation,
155112e66b6fSCedric Chaumont 				 TEE_Attribute *params,
155212e66b6fSCedric Chaumont 				 uint32_t paramCount, void *srcData,
155379a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
155479a3c601SCedric Chaumont 				 uint32_t *destLen)
1555b0104773SPascal Brand {
1556b0104773SPascal Brand 	TEE_Result res;
1557e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1558e86f1266SJens Wiklander 	uint64_t dl;
1559b0104773SPascal Brand 
156012e66b6fSCedric Chaumont 	if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1561b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
1562b0104773SPascal Brand 		TEE_Panic(0);
156312e66b6fSCedric Chaumont 	if (params == NULL && paramCount != 0)
1564b0104773SPascal Brand 		TEE_Panic(0);
156512e66b6fSCedric Chaumont 	if (!operation->key1)
1566b0104773SPascal Brand 		TEE_Panic(0);
156712e66b6fSCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
156812e66b6fSCedric Chaumont 		TEE_Panic(0);
156912e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_ENCRYPT)
1570b0104773SPascal Brand 		TEE_Panic(0);
1571b0104773SPascal Brand 
1572e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1573e86f1266SJens Wiklander 	dl = *destLen;
1574e86f1266SJens Wiklander 	res = utee_asymm_operate(operation->state, ua, paramCount, srcData,
1575e86f1266SJens Wiklander 				 srcLen, destData, &dl);
1576e86f1266SJens Wiklander 	*destLen = dl;
157712e66b6fSCedric Chaumont 
15788844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
15798844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
15808844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1581b0104773SPascal Brand 		TEE_Panic(res);
158212e66b6fSCedric Chaumont 
1583b0104773SPascal Brand 	return res;
1584b0104773SPascal Brand }
1585b0104773SPascal Brand 
158612e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation,
158712e66b6fSCedric Chaumont 				 TEE_Attribute *params,
158812e66b6fSCedric Chaumont 				 uint32_t paramCount, void *srcData,
158979a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
159079a3c601SCedric Chaumont 				 uint32_t *destLen)
1591b0104773SPascal Brand {
1592b0104773SPascal Brand 	TEE_Result res;
1593e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1594e86f1266SJens Wiklander 	uint64_t dl;
1595b0104773SPascal Brand 
159612e66b6fSCedric Chaumont 	if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1597b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
1598b0104773SPascal Brand 		TEE_Panic(0);
159912e66b6fSCedric Chaumont 	if (params == NULL && paramCount != 0)
1600b0104773SPascal Brand 		TEE_Panic(0);
160112e66b6fSCedric Chaumont 	if (!operation->key1)
1602b0104773SPascal Brand 		TEE_Panic(0);
160312e66b6fSCedric Chaumont 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
160412e66b6fSCedric Chaumont 		TEE_Panic(0);
160512e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_DECRYPT)
1606b0104773SPascal Brand 		TEE_Panic(0);
1607b0104773SPascal Brand 
1608e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1609e86f1266SJens Wiklander 	dl = *destLen;
1610e86f1266SJens Wiklander 	res = utee_asymm_operate(operation->state, ua, paramCount, srcData,
1611e86f1266SJens Wiklander 				 srcLen, destData, &dl);
1612e86f1266SJens Wiklander 	*destLen = dl;
161312e66b6fSCedric Chaumont 
16148844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
16158844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
16168844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1617b0104773SPascal Brand 		TEE_Panic(res);
161812e66b6fSCedric Chaumont 
1619b0104773SPascal Brand 	return res;
1620b0104773SPascal Brand }
1621b0104773SPascal Brand 
162212e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation,
162312e66b6fSCedric Chaumont 				    TEE_Attribute *params,
162412e66b6fSCedric Chaumont 				    uint32_t paramCount, void *digest,
162579a3c601SCedric Chaumont 				    uint32_t digestLen, void *signature,
162679a3c601SCedric Chaumont 				    uint32_t *signatureLen)
1627b0104773SPascal Brand {
1628b0104773SPascal Brand 	TEE_Result res;
1629e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1630e86f1266SJens Wiklander 	uint64_t sl;
1631b0104773SPascal Brand 
163212e66b6fSCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
163312e66b6fSCedric Chaumont 	    (digest == NULL && digestLen != 0) ||
1634b0104773SPascal Brand 	    signature == NULL || signatureLen == NULL)
1635b0104773SPascal Brand 		TEE_Panic(0);
163612e66b6fSCedric Chaumont 	if (params == NULL && paramCount != 0)
1637b0104773SPascal Brand 		TEE_Panic(0);
163812e66b6fSCedric Chaumont 	if (!operation->key1)
1639b0104773SPascal Brand 		TEE_Panic(0);
164012e66b6fSCedric Chaumont 	if (operation->info.operationClass !=
164112e66b6fSCedric Chaumont 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
164212e66b6fSCedric Chaumont 		TEE_Panic(0);
164312e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_SIGN)
1644b0104773SPascal Brand 		TEE_Panic(0);
1645b0104773SPascal Brand 
1646e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1647e86f1266SJens Wiklander 	sl = *signatureLen;
1648e86f1266SJens Wiklander 	res = utee_asymm_operate(operation->state, ua, paramCount, digest,
1649e86f1266SJens Wiklander 				 digestLen, signature, &sl);
1650e86f1266SJens Wiklander 	*signatureLen = sl;
165112e66b6fSCedric Chaumont 
1652b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1653b0104773SPascal Brand 		TEE_Panic(res);
165412e66b6fSCedric Chaumont 
1655b0104773SPascal Brand 	return res;
1656b0104773SPascal Brand }
1657b0104773SPascal Brand 
165812e66b6fSCedric Chaumont TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,
165912e66b6fSCedric Chaumont 				      TEE_Attribute *params,
166012e66b6fSCedric Chaumont 				      uint32_t paramCount, void *digest,
166112e66b6fSCedric Chaumont 				      uint32_t digestLen, void *signature,
166279a3c601SCedric Chaumont 				      uint32_t signatureLen)
1663b0104773SPascal Brand {
1664b0104773SPascal Brand 	TEE_Result res;
1665e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1666b0104773SPascal Brand 
166712e66b6fSCedric Chaumont 	if (operation == TEE_HANDLE_NULL ||
166812e66b6fSCedric Chaumont 	    (digest == NULL && digestLen != 0) ||
1669b0104773SPascal Brand 	    (signature == NULL && signatureLen != 0))
1670b0104773SPascal Brand 		TEE_Panic(0);
167112e66b6fSCedric Chaumont 	if (params == NULL && paramCount != 0)
1672b0104773SPascal Brand 		TEE_Panic(0);
167312e66b6fSCedric Chaumont 	if (!operation->key1)
1674b0104773SPascal Brand 		TEE_Panic(0);
167512e66b6fSCedric Chaumont 	if (operation->info.operationClass !=
167612e66b6fSCedric Chaumont 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
167712e66b6fSCedric Chaumont 		TEE_Panic(0);
167812e66b6fSCedric Chaumont 	if (operation->info.mode != TEE_MODE_VERIFY)
1679b0104773SPascal Brand 		TEE_Panic(0);
1680b0104773SPascal Brand 
1681e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1682e86f1266SJens Wiklander 	res = utee_asymm_verify(operation->state, ua, paramCount, digest,
168312e66b6fSCedric Chaumont 				digestLen, signature, signatureLen);
168412e66b6fSCedric Chaumont 
1685b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1686b0104773SPascal Brand 		TEE_Panic(res);
168712e66b6fSCedric Chaumont 
1688b0104773SPascal Brand 	return res;
1689b0104773SPascal Brand }
1690b0104773SPascal Brand 
1691b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */
1692b0104773SPascal Brand 
1693b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation,
1694b0104773SPascal Brand 		   const TEE_Attribute *params, uint32_t paramCount,
1695b0104773SPascal Brand 		   TEE_ObjectHandle derivedKey)
1696b0104773SPascal Brand {
1697b0104773SPascal Brand 	TEE_Result res;
1698b0104773SPascal Brand 	TEE_ObjectInfo key_info;
1699e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
1700b0104773SPascal Brand 
1701b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1702b0104773SPascal Brand 		TEE_Panic(0);
170384fa9467SCedric Chaumont 	if (params == NULL && paramCount != 0)
1704b0104773SPascal Brand 		TEE_Panic(0);
17058854d3c6SJerome Forissier 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
17068854d3c6SJerome Forissier 	    TEE_OPERATION_KEY_DERIVATION)
1707b0104773SPascal Brand 		TEE_Panic(0);
1708b0104773SPascal Brand 
1709b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1710b0104773SPascal Brand 		TEE_Panic(0);
171184fa9467SCedric Chaumont 	if (!operation->key1)
171284fa9467SCedric Chaumont 		TEE_Panic(0);
1713b0104773SPascal Brand 	if (operation->info.mode != TEE_MODE_DERIVE)
1714b0104773SPascal Brand 		TEE_Panic(0);
1715b0104773SPascal Brand 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1716b0104773SPascal Brand 		TEE_Panic(0);
1717b0104773SPascal Brand 
1718e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info);
1719b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1720b0104773SPascal Brand 		TEE_Panic(0);
1721b0104773SPascal Brand 
1722b0104773SPascal Brand 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1723b0104773SPascal Brand 		TEE_Panic(0);
1724b0104773SPascal Brand 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1725b0104773SPascal Brand 		TEE_Panic(0);
1726b0104773SPascal Brand 
1727e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
1728e86f1266SJens Wiklander 	res = utee_cryp_derive_key(operation->state, ua, paramCount,
1729e86f1266SJens Wiklander 				   (unsigned long)derivedKey);
1730b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1731b0104773SPascal Brand 		TEE_Panic(res);
1732b0104773SPascal Brand }
1733b0104773SPascal Brand 
1734b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */
1735b0104773SPascal Brand 
173679a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1737b0104773SPascal Brand {
1738b0104773SPascal Brand 	TEE_Result res;
1739b0104773SPascal Brand 
1740b0104773SPascal Brand 	res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1741b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1742b0104773SPascal Brand 		TEE_Panic(res);
1743b0104773SPascal Brand }
1744