xref: /optee_os/lib/libutee/tee_api_operations.c (revision 1220586e7daa04e472ce72f534596b021b624312)
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 
66b0104773SPascal Brand 	if (operation == NULL)
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 
93*1220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P192:
94*1220586eSCedric Chaumont 	case TEE_ALG_ECDH_P192:
95*1220586eSCedric Chaumont 		if (maxKeySize != 192)
96*1220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
97*1220586eSCedric Chaumont 		break;
98*1220586eSCedric Chaumont 
99*1220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P224:
100*1220586eSCedric Chaumont 	case TEE_ALG_ECDH_P224:
101*1220586eSCedric Chaumont 		if (maxKeySize != 224)
102*1220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
103*1220586eSCedric Chaumont 		break;
104*1220586eSCedric Chaumont 
105*1220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P256:
106*1220586eSCedric Chaumont 	case TEE_ALG_ECDH_P256:
107*1220586eSCedric Chaumont 		if (maxKeySize != 256)
108*1220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
109*1220586eSCedric Chaumont 		break;
110*1220586eSCedric Chaumont 
111*1220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P384:
112*1220586eSCedric Chaumont 	case TEE_ALG_ECDH_P384:
113*1220586eSCedric Chaumont 		if (maxKeySize != 384)
114*1220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
115*1220586eSCedric Chaumont 		break;
116*1220586eSCedric Chaumont 
117*1220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P521:
118*1220586eSCedric Chaumont 	case TEE_ALG_ECDH_P521:
119*1220586eSCedric Chaumont 		if (maxKeySize != 521)
120*1220586eSCedric Chaumont 			return TEE_ERROR_NOT_SUPPORTED;
121*1220586eSCedric Chaumont 		break;
122*1220586eSCedric 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:
168*1220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P192:
169*1220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P224:
170*1220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P256:
171*1220586eSCedric Chaumont 	case TEE_ALG_ECDSA_P384:
172*1220586eSCedric 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:
211*1220586eSCedric Chaumont 	case TEE_ALG_ECDH_P192:
212*1220586eSCedric Chaumont 	case TEE_ALG_ECDH_P224:
213*1220586eSCedric Chaumont 	case TEE_ALG_ECDH_P256:
214*1220586eSCedric Chaumont 	case TEE_ALG_ECDH_P384:
215*1220586eSCedric 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);
269b0104773SPascal Brand 	if (op == NULL)
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 
285b0104773SPascal Brand 		op->buffer =
286b0104773SPascal Brand 		    TEE_Malloc(buffer_size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
287b0104773SPascal Brand 		if (op->buffer == NULL) {
288b0104773SPascal Brand 			res = TEE_ERROR_OUT_OF_MEMORY;
289b0104773SPascal Brand 			goto out;
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)
309b0104773SPascal Brand 			goto out;
310b0104773SPascal Brand 
311b0104773SPascal Brand 		if ((op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
312b0104773SPascal Brand 		    0) {
313b0104773SPascal Brand 			res =
314b0104773SPascal Brand 			    TEE_AllocateTransientObject(key_type, mks,
315b0104773SPascal Brand 							&op->key2);
316b0104773SPascal Brand 			if (res != TEE_SUCCESS)
317b0104773SPascal Brand 				goto out;
318b0104773SPascal Brand 		}
319b0104773SPascal Brand 	}
320b0104773SPascal Brand 
321b0104773SPascal Brand 	res = utee_cryp_state_alloc(algorithm, mode, (uint32_t) op->key1,
322b0104773SPascal Brand 				    (uint32_t) op->key2, &op->state);
323b0104773SPascal Brand 	if (res != TEE_SUCCESS)
324b0104773SPascal Brand 		goto out;
325b0104773SPascal Brand 
326b0104773SPascal Brand 	/* For multi-stage operation do an "init". */
327b0104773SPascal Brand 	TEE_ResetOperation(op);
328b0104773SPascal Brand 	*operation = op;
329b0104773SPascal Brand 
330b0104773SPascal Brand out:
331b0104773SPascal Brand 	if (res != TEE_SUCCESS) {
332b0104773SPascal Brand 		TEE_FreeTransientObject(op->key1);
333b0104773SPascal Brand 		TEE_FreeTransientObject(op->key2);
334b0104773SPascal Brand 		TEE_FreeOperation(op);
335b0104773SPascal Brand 	}
336b0104773SPascal Brand 
337b0104773SPascal Brand 	return res;
338b0104773SPascal Brand }
339b0104773SPascal Brand 
340b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation)
341b0104773SPascal Brand {
342b0104773SPascal Brand 	if (operation != TEE_HANDLE_NULL) {
343b0104773SPascal Brand 		/*
344b0104773SPascal Brand 		 * Note that keys should not be freed here, since they are
345b0104773SPascal Brand 		 * claimed by the operation they will be freed by
346b0104773SPascal Brand 		 * utee_cryp_state_free().
347b0104773SPascal Brand 		 */
348b0104773SPascal Brand 		utee_cryp_state_free(operation->state);
349b0104773SPascal Brand 		TEE_Free(operation->buffer);
350b0104773SPascal Brand 		TEE_Free(operation);
351b0104773SPascal Brand 	}
352b0104773SPascal Brand }
353b0104773SPascal Brand 
354b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation,
355b0104773SPascal Brand 			  TEE_OperationInfo *operationInfo)
356b0104773SPascal Brand {
357b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
358b0104773SPascal Brand 		TEE_Panic(0);
359b0104773SPascal Brand 
360b0104773SPascal Brand 	if (operationInfo == NULL)
361b0104773SPascal Brand 		TEE_Panic(0);
362b0104773SPascal Brand 
363b0104773SPascal Brand 	*operationInfo = operation->info;
364b0104773SPascal Brand }
365b0104773SPascal Brand 
366b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation)
367b0104773SPascal Brand {
368b0104773SPascal Brand 	TEE_Result res;
369b0104773SPascal Brand 
370b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
371b0104773SPascal Brand 		TEE_Panic(0);
372b0104773SPascal Brand 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
373b0104773SPascal Brand 		res = utee_hash_init(operation->state, NULL, 0);
374b0104773SPascal Brand 		if (res != TEE_SUCCESS)
375b0104773SPascal Brand 			TEE_Panic(res);
376b0104773SPascal Brand 	}
377b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
378b0104773SPascal Brand }
379b0104773SPascal Brand 
380b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
381b0104773SPascal Brand 			       TEE_ObjectHandle key)
382b0104773SPascal Brand {
3837583c59eSCedric Chaumont 	TEE_Result res;
384b0104773SPascal Brand 	uint32_t key_size = 0;
385b0104773SPascal Brand 
386b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
387b0104773SPascal Brand 		TEE_Panic(0);
388b0104773SPascal Brand 
389b0104773SPascal Brand 	/* No key for digests */
390b0104773SPascal Brand 	if (operation->info.operationClass == TEE_OPERATION_DIGEST)
391b0104773SPascal Brand 		TEE_Panic(0);
392b0104773SPascal Brand 
393b0104773SPascal Brand 	/* Two keys expected */
394b0104773SPascal Brand 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
395b0104773SPascal Brand 	    0)
396b0104773SPascal Brand 		TEE_Panic(0);
397b0104773SPascal Brand 
398b0104773SPascal Brand 	if (key != TEE_HANDLE_NULL) {
399b0104773SPascal Brand 		TEE_ObjectInfo key_info;
400b0104773SPascal Brand 
4017583c59eSCedric Chaumont 		res = TEE_GetObjectInfo1(key, &key_info);
4027583c59eSCedric Chaumont 		if (res != TEE_SUCCESS)
4037583c59eSCedric Chaumont 			goto err;
4047583c59eSCedric Chaumont 
405b0104773SPascal Brand 		/* Supplied key has to meet required usage */
406b0104773SPascal Brand 		if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
407b0104773SPascal Brand 		    operation->info.requiredKeyUsage) {
408b0104773SPascal Brand 			TEE_Panic(0);
409b0104773SPascal Brand 		}
410b0104773SPascal Brand 
4117583c59eSCedric Chaumont 		if (operation->info.maxKeySize < key_info.keySize)
412b0104773SPascal Brand 			TEE_Panic(0);
413b0104773SPascal Brand 
4147583c59eSCedric Chaumont 		key_size = key_info.keySize;
415b0104773SPascal Brand 	}
416b0104773SPascal Brand 
417b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
418b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
419b0104773SPascal Brand 
420b0104773SPascal Brand 	if (key != TEE_HANDLE_NULL) {
4217583c59eSCedric Chaumont 		res = TEE_CopyObjectAttributes1(operation->key1, key);
4227583c59eSCedric Chaumont 		if (res != TEE_SUCCESS)
4237583c59eSCedric Chaumont 			goto err;
4247583c59eSCedric Chaumont 
425b0104773SPascal Brand 		operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
426b0104773SPascal Brand 	}
427b0104773SPascal Brand 
428b0104773SPascal Brand 	operation->info.keySize = key_size;
429b0104773SPascal Brand 
4307583c59eSCedric Chaumont 	goto out;
4317583c59eSCedric Chaumont 
4327583c59eSCedric Chaumont err:
4337583c59eSCedric Chaumont 	if (res == TEE_ERROR_CORRUPT_OBJECT ||
4347583c59eSCedric Chaumont 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE)
4357583c59eSCedric Chaumont 		return res;
4367583c59eSCedric Chaumont 	TEE_Panic(0);
4377583c59eSCedric Chaumont out:
438b0104773SPascal Brand 	return TEE_SUCCESS;
439b0104773SPascal Brand }
440b0104773SPascal Brand 
441b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
442b0104773SPascal Brand 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
443b0104773SPascal Brand {
4447583c59eSCedric Chaumont 	TEE_Result res;
445b0104773SPascal Brand 	uint32_t key_size = 0;
446b0104773SPascal Brand 
447b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
448b0104773SPascal Brand 		TEE_Panic(0);
449b0104773SPascal Brand 
450b0104773SPascal Brand 	/* Two keys not expected */
451b0104773SPascal Brand 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
452b0104773SPascal Brand 	    0)
453b0104773SPascal Brand 		TEE_Panic(0);
454b0104773SPascal Brand 
455b0104773SPascal Brand 	/* Either both keys are NULL or both are not NULL */
456b0104773SPascal Brand 	if ((key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) &&
457b0104773SPascal Brand 	    key1 != key2)
458b0104773SPascal Brand 		TEE_Panic(0);
459b0104773SPascal Brand 
460b0104773SPascal Brand 	if (key1 != TEE_HANDLE_NULL) {
461b0104773SPascal Brand 		TEE_ObjectInfo key_info1;
462b0104773SPascal Brand 		TEE_ObjectInfo key_info2;
463b0104773SPascal Brand 
4647583c59eSCedric Chaumont 		res = TEE_GetObjectInfo1(key1, &key_info1);
4657583c59eSCedric Chaumont 		if (res != TEE_SUCCESS)
4667583c59eSCedric Chaumont 			goto err;
4677583c59eSCedric Chaumont 
468b0104773SPascal Brand 		/* Supplied key has to meet required usage */
469b0104773SPascal Brand 		if ((key_info1.objectUsage & operation->info.
470b0104773SPascal Brand 		     requiredKeyUsage) != operation->info.requiredKeyUsage) {
471b0104773SPascal Brand 			TEE_Panic(0);
472b0104773SPascal Brand 		}
473b0104773SPascal Brand 
4747583c59eSCedric Chaumont 		res = TEE_GetObjectInfo1(key2, &key_info2);
4757583c59eSCedric Chaumont 		if (res != TEE_SUCCESS) {
4767583c59eSCedric Chaumont 			if (res == TEE_ERROR_CORRUPT_OBJECT)
4777583c59eSCedric Chaumont 				res = TEE_ERROR_CORRUPT_OBJECT_2;
4787583c59eSCedric Chaumont 			goto err;
4797583c59eSCedric Chaumont 		}
4807583c59eSCedric Chaumont 
481b0104773SPascal Brand 		/* Supplied key has to meet required usage */
482b0104773SPascal Brand 		if ((key_info2.objectUsage & operation->info.
483b0104773SPascal Brand 		     requiredKeyUsage) != operation->info.requiredKeyUsage) {
484b0104773SPascal Brand 			TEE_Panic(0);
485b0104773SPascal Brand 		}
486b0104773SPascal Brand 
487b0104773SPascal Brand 		/*
488b0104773SPascal Brand 		 * AES-XTS (the only multi key algorithm supported, requires the
489b0104773SPascal Brand 		 * keys to be of equal size.
490b0104773SPascal Brand 		 */
491b0104773SPascal Brand 		if (operation->info.algorithm == TEE_ALG_AES_XTS &&
4927583c59eSCedric Chaumont 		    key_info1.keySize != key_info2.keySize)
493b0104773SPascal Brand 			TEE_Panic(0);
494b0104773SPascal Brand 
4957583c59eSCedric Chaumont 		if (operation->info.maxKeySize < key_info1.keySize)
496b0104773SPascal Brand 			TEE_Panic(0);
497b0104773SPascal Brand 
498b0104773SPascal Brand 		/*
499b0104773SPascal Brand 		 * Odd that only the size of one key should be reported while
500b0104773SPascal Brand 		 * size of two key are used when allocating the operation.
501b0104773SPascal Brand 		 */
5027583c59eSCedric Chaumont 		key_size = key_info1.keySize;
503b0104773SPascal Brand 	}
504b0104773SPascal Brand 
505b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
506b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key2);
507b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
508b0104773SPascal Brand 
509b0104773SPascal Brand 	if (key1 != TEE_HANDLE_NULL) {
5107583c59eSCedric Chaumont 		res = TEE_CopyObjectAttributes1(operation->key1, key1);
5117583c59eSCedric Chaumont 		if (res != TEE_SUCCESS)
5127583c59eSCedric Chaumont 			goto err;
5137583c59eSCedric Chaumont 
5147583c59eSCedric Chaumont 		res = TEE_CopyObjectAttributes1(operation->key2, key2);
5157583c59eSCedric Chaumont 		if (res != TEE_SUCCESS) {
5167583c59eSCedric Chaumont 			if (res == TEE_ERROR_CORRUPT_OBJECT)
5177583c59eSCedric Chaumont 				res = TEE_ERROR_CORRUPT_OBJECT_2;
5187583c59eSCedric Chaumont 			goto err;
5197583c59eSCedric Chaumont 		}
5207583c59eSCedric Chaumont 
521b0104773SPascal Brand 		operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
522b0104773SPascal Brand 	}
523b0104773SPascal Brand 
524b0104773SPascal Brand 	operation->info.keySize = key_size;
525b0104773SPascal Brand 
5267583c59eSCedric Chaumont 	goto out;
5277583c59eSCedric Chaumont 
5287583c59eSCedric Chaumont err:
5297583c59eSCedric Chaumont 	if (res == TEE_ERROR_CORRUPT_OBJECT ||
5307583c59eSCedric Chaumont 	    res == TEE_ERROR_CORRUPT_OBJECT_2 ||
5317583c59eSCedric Chaumont 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE ||
5327583c59eSCedric Chaumont 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
5337583c59eSCedric Chaumont 		return res;
5347583c59eSCedric Chaumont 	TEE_Panic(0);
5357583c59eSCedric Chaumont out:
536b0104773SPascal Brand 	return TEE_SUCCESS;
537b0104773SPascal Brand }
538b0104773SPascal Brand 
539b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
540b0104773SPascal Brand {
541b0104773SPascal Brand 	TEE_Result res;
542b0104773SPascal Brand 
543b0104773SPascal Brand 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
544b0104773SPascal Brand 		TEE_Panic(0);
545b0104773SPascal Brand 	if (dst_op->info.algorithm != src_op->info.algorithm)
546b0104773SPascal Brand 		TEE_Panic(0);
547b0104773SPascal Brand 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
548b0104773SPascal Brand 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
549b0104773SPascal Brand 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
550b0104773SPascal Brand 
551b0104773SPascal Brand 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
552b0104773SPascal Brand 			key1 = src_op->key1;
553b0104773SPascal Brand 			key2 = src_op->key2;
554b0104773SPascal Brand 		}
555b0104773SPascal Brand 
556b0104773SPascal Brand 		if ((src_op->info.handleState &
557b0104773SPascal Brand 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
558b0104773SPascal Brand 			TEE_SetOperationKey(dst_op, key1);
559b0104773SPascal Brand 		} else {
560b0104773SPascal Brand 			TEE_SetOperationKey2(dst_op, key1, key2);
561b0104773SPascal Brand 		}
562b0104773SPascal Brand 	}
563b0104773SPascal Brand 	dst_op->info.handleState = src_op->info.handleState;
564b0104773SPascal Brand 	dst_op->info.keySize = src_op->info.keySize;
565b0104773SPascal Brand 
566b0104773SPascal Brand 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
567b0104773SPascal Brand 	    dst_op->block_size != src_op->block_size)
568b0104773SPascal Brand 		TEE_Panic(0);
569b0104773SPascal Brand 
570b0104773SPascal Brand 	if (dst_op->buffer != NULL) {
571b0104773SPascal Brand 		if (src_op->buffer == NULL)
572b0104773SPascal Brand 			TEE_Panic(0);
573b0104773SPascal Brand 
574b0104773SPascal Brand 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
575b0104773SPascal Brand 		dst_op->buffer_offs = src_op->buffer_offs;
576b0104773SPascal Brand 	} else if (src_op->buffer != NULL) {
577b0104773SPascal Brand 		TEE_Panic(0);
578b0104773SPascal Brand 	}
579b0104773SPascal Brand 
580b0104773SPascal Brand 	res = utee_cryp_state_copy(dst_op->state, src_op->state);
581b0104773SPascal Brand 	if (res != TEE_SUCCESS)
582b0104773SPascal Brand 		TEE_Panic(res);
583b0104773SPascal Brand }
584b0104773SPascal Brand 
585b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */
586b0104773SPascal Brand 
587b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation,
58879a3c601SCedric Chaumont 		      void *chunk, uint32_t chunkSize)
589b0104773SPascal Brand {
59073d6c3baSJoakim Bech 	TEE_Result res = TEE_ERROR_GENERIC;
591b0104773SPascal Brand 
59273d6c3baSJoakim Bech 	if (operation == TEE_HANDLE_NULL ||
59373d6c3baSJoakim Bech 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
594b0104773SPascal Brand 		TEE_Panic(0);
59573d6c3baSJoakim Bech 
596b0104773SPascal Brand 	res = utee_hash_update(operation->state, chunk, chunkSize);
597b0104773SPascal Brand 	if (res != TEE_SUCCESS)
598b0104773SPascal Brand 		TEE_Panic(res);
599b0104773SPascal Brand }
600b0104773SPascal Brand 
601b0104773SPascal Brand TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
60279a3c601SCedric Chaumont 			     uint32_t chunkLen, void *hash, uint32_t *hashLen)
603b0104773SPascal Brand {
60473d6c3baSJoakim Bech 	if ((operation == TEE_HANDLE_NULL) || (!chunk && chunkLen) ||
60573d6c3baSJoakim Bech 	    !hash || !hashLen ||
60673d6c3baSJoakim Bech 	    (operation->info.operationClass != TEE_OPERATION_DIGEST))
607b0104773SPascal Brand 		TEE_Panic(0);
60873d6c3baSJoakim Bech 
609b0104773SPascal Brand 	return utee_hash_final(operation->state, chunk, chunkLen, hash,
610b0104773SPascal Brand 			       hashLen);
611b0104773SPascal Brand }
612b0104773SPascal Brand 
613b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */
614b0104773SPascal Brand 
61579a3c601SCedric Chaumont void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
616b0104773SPascal Brand {
617b0104773SPascal Brand 	TEE_Result res;
618b0104773SPascal Brand 
619b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
620b0104773SPascal Brand 		TEE_Panic(0);
621b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
622b0104773SPascal Brand 		TEE_Panic(0);
623b0104773SPascal Brand 	res = utee_cipher_init(operation->state, IV, IVLen);
624b0104773SPascal Brand 	if (res != TEE_SUCCESS)
625b0104773SPascal Brand 		TEE_Panic(res);
626b0104773SPascal Brand 	operation->buffer_offs = 0;
627b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
628b0104773SPascal Brand }
629b0104773SPascal Brand 
630b0104773SPascal Brand static TEE_Result tee_buffer_update(
631b0104773SPascal Brand 		TEE_OperationHandle op,
632b0104773SPascal Brand 		TEE_Result(*update_func) (uint32_t state, const void *src,
6337f74c64aSPascal Brand 					  size_t slen, void *dst, uint32_t *dlen),
634b0104773SPascal Brand 		const void *src_data, size_t src_len,
6357f74c64aSPascal Brand 		void *dest_data, uint32_t *dest_len)
636b0104773SPascal Brand {
637b0104773SPascal Brand 	TEE_Result res;
638b0104773SPascal Brand 	const uint8_t *src = src_data;
639b0104773SPascal Brand 	size_t slen = src_len;
640b0104773SPascal Brand 	uint8_t *dst = dest_data;
641b0104773SPascal Brand 	size_t dlen = *dest_len;
642b0104773SPascal Brand 	size_t acc_dlen = 0;
6437f74c64aSPascal Brand 	uint32_t tmp_dlen;
644b0104773SPascal Brand 	size_t l;
645b0104773SPascal Brand 	size_t buffer_size;
646d3588802SPascal Brand 	size_t buffer_left;
647b0104773SPascal Brand 
648d3588802SPascal Brand 	if (op->buffer_two_blocks) {
649b0104773SPascal Brand 		buffer_size = op->block_size * 2;
650d3588802SPascal Brand 		buffer_left = 1;
651d3588802SPascal Brand 	} else {
652b0104773SPascal Brand 		buffer_size = op->block_size;
653d3588802SPascal Brand 		buffer_left = 0;
654d3588802SPascal Brand 	}
655b0104773SPascal Brand 
656b0104773SPascal Brand 	if (op->buffer_offs > 0) {
657b0104773SPascal Brand 		/* Fill up complete block */
658b0104773SPascal Brand 		if (op->buffer_offs < op->block_size)
659b0104773SPascal Brand 			l = MIN(slen, op->block_size - op->buffer_offs);
660b0104773SPascal Brand 		else
661b0104773SPascal Brand 			l = MIN(slen, buffer_size - op->buffer_offs);
662b0104773SPascal Brand 		memcpy(op->buffer + op->buffer_offs, src, l);
663b0104773SPascal Brand 		op->buffer_offs += l;
664b0104773SPascal Brand 		src += l;
665b0104773SPascal Brand 		slen -= l;
666b0104773SPascal Brand 		if ((op->buffer_offs % op->block_size) != 0)
667b0104773SPascal Brand 			goto out;	/* Nothing left to do */
668b0104773SPascal Brand 	}
669b0104773SPascal Brand 
670b0104773SPascal Brand 	/* If we can feed from buffer */
671d3588802SPascal Brand 	if ((op->buffer_offs > 0) &&
672d3588802SPascal Brand 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
6732ff3fdbbSPascal Brand 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
674b0104773SPascal Brand 				op->block_size);
675b0104773SPascal Brand 		l = MIN(op->buffer_offs, l);
676b0104773SPascal Brand 		tmp_dlen = dlen;
677b0104773SPascal Brand 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
678b0104773SPascal Brand 		if (res != TEE_SUCCESS)
679b0104773SPascal Brand 			TEE_Panic(res);
680b0104773SPascal Brand 		dst += tmp_dlen;
681b0104773SPascal Brand 		dlen -= tmp_dlen;
682b0104773SPascal Brand 		acc_dlen += tmp_dlen;
683b0104773SPascal Brand 		op->buffer_offs -= l;
684b0104773SPascal Brand 		if (op->buffer_offs > 0) {
685b0104773SPascal Brand 			/*
686b0104773SPascal Brand 			 * Slen is small enough to be contained in rest buffer.
687b0104773SPascal Brand 			 */
688b0104773SPascal Brand 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
689b0104773SPascal Brand 			memcpy(op->buffer + op->buffer_offs, src, slen);
690b0104773SPascal Brand 			op->buffer_offs += slen;
691b0104773SPascal Brand 			goto out;	/* Nothing left to do */
692b0104773SPascal Brand 		}
693b0104773SPascal Brand 	}
694b0104773SPascal Brand 
695d3588802SPascal Brand 	if (slen >= (buffer_size + buffer_left)) {
696b0104773SPascal Brand 		/* Buffer is empty, feed as much as possible from src */
697b0104773SPascal Brand 		if (TEE_ALIGNMENT_IS_OK(src, uint32_t)) {
6982ff3fdbbSPascal Brand 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
699b0104773SPascal Brand 
700b0104773SPascal Brand 			tmp_dlen = dlen;
701b0104773SPascal Brand 			res = update_func(op->state, src, l, dst, &tmp_dlen);
702b0104773SPascal Brand 			if (res != TEE_SUCCESS)
703b0104773SPascal Brand 				TEE_Panic(res);
704b0104773SPascal Brand 			src += l;
705b0104773SPascal Brand 			slen -= l;
706b0104773SPascal Brand 			dst += tmp_dlen;
707b0104773SPascal Brand 			dlen -= tmp_dlen;
708b0104773SPascal Brand 			acc_dlen += tmp_dlen;
709b0104773SPascal Brand 		} else {
710b0104773SPascal Brand 			/*
711b0104773SPascal Brand 			 * Supplied data isn't well aligned, we're forced to
712b0104773SPascal Brand 			 * feed through the buffer.
713b0104773SPascal Brand 			 */
714b0104773SPascal Brand 			while (slen >= op->block_size) {
715b0104773SPascal Brand 				memcpy(op->buffer, src, op->block_size);
716b0104773SPascal Brand 
717b0104773SPascal Brand 				tmp_dlen = dlen;
718b0104773SPascal Brand 				res =
719b0104773SPascal Brand 				    update_func(op->state, op->buffer,
720b0104773SPascal Brand 						op->block_size, dst, &tmp_dlen);
721b0104773SPascal Brand 				if (res != TEE_SUCCESS)
722b0104773SPascal Brand 					TEE_Panic(res);
723b0104773SPascal Brand 				src += op->block_size;
724b0104773SPascal Brand 				slen -= op->block_size;
725b0104773SPascal Brand 				dst += tmp_dlen;
726b0104773SPascal Brand 				dlen -= tmp_dlen;
727b0104773SPascal Brand 				acc_dlen += tmp_dlen;
728b0104773SPascal Brand 			}
729b0104773SPascal Brand 		}
730b0104773SPascal Brand 	}
731b0104773SPascal Brand 
732b0104773SPascal Brand 	/* Slen is small enough to be contained in buffer. */
733b0104773SPascal Brand 	memcpy(op->buffer + op->buffer_offs, src, slen);
734b0104773SPascal Brand 	op->buffer_offs += slen;
735b0104773SPascal Brand 
736b0104773SPascal Brand out:
737b0104773SPascal Brand 	*dest_len = acc_dlen;
738b0104773SPascal Brand 	return TEE_SUCCESS;
739b0104773SPascal Brand }
740b0104773SPascal Brand 
741b0104773SPascal Brand TEE_Result TEE_CipherUpdate(TEE_OperationHandle op, const void *srcData,
74279a3c601SCedric Chaumont 			    uint32_t srcLen, void *destData, uint32_t *destLen)
743b0104773SPascal Brand {
744b0104773SPascal Brand 	size_t req_dlen;
745b0104773SPascal Brand 
746b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
747b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
748b0104773SPascal Brand 		TEE_Panic(0);
749b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_CIPHER)
750b0104773SPascal Brand 		TEE_Panic(0);
751b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
752b0104773SPascal Brand 		TEE_Panic(0);
753b0104773SPascal Brand 
754b0104773SPascal Brand 	/* Calculate required dlen */
755b0104773SPascal Brand 	req_dlen = ((op->buffer_offs + srcLen) / op->block_size) *
756b0104773SPascal Brand 	    op->block_size;
757b0104773SPascal Brand 	if (op->buffer_two_blocks) {
758b0104773SPascal Brand 		if (req_dlen > op->block_size * 2)
759b0104773SPascal Brand 			req_dlen -= op->block_size * 2;
760b0104773SPascal Brand 		else
761b0104773SPascal Brand 			req_dlen = 0;
762b0104773SPascal Brand 	}
763b0104773SPascal Brand 	/*
764b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
765b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
766b0104773SPascal Brand 	 * can't restore sync with this API.
767b0104773SPascal Brand 	 */
768b0104773SPascal Brand 	if (*destLen < req_dlen) {
769b0104773SPascal Brand 		*destLen = req_dlen;
770b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
771b0104773SPascal Brand 	}
772b0104773SPascal Brand 
773b0104773SPascal Brand 	tee_buffer_update(op, utee_cipher_update, srcData, srcLen, destData,
774b0104773SPascal Brand 			  destLen);
775b0104773SPascal Brand 
776b0104773SPascal Brand 	return TEE_SUCCESS;
777b0104773SPascal Brand }
778b0104773SPascal Brand 
779b0104773SPascal Brand TEE_Result TEE_CipherDoFinal(TEE_OperationHandle op,
78079a3c601SCedric Chaumont 			     const void *srcData, uint32_t srcLen, void *destData,
78179a3c601SCedric Chaumont 			     uint32_t *destLen)
782b0104773SPascal Brand {
783b0104773SPascal Brand 	TEE_Result res;
784b0104773SPascal Brand 	uint8_t *dst = destData;
785b0104773SPascal Brand 	size_t acc_dlen = 0;
7867f74c64aSPascal Brand 	uint32_t tmp_dlen;
787b0104773SPascal Brand 	size_t req_dlen;
788b0104773SPascal Brand 
789b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
790b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
791b0104773SPascal Brand 		TEE_Panic(0);
792b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_CIPHER)
793b0104773SPascal Brand 		TEE_Panic(0);
794b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
795b0104773SPascal Brand 		TEE_Panic(0);
796b0104773SPascal Brand 
797b0104773SPascal Brand 	/*
798b0104773SPascal Brand 	 * Check that the final block doesn't require padding for those
799b0104773SPascal Brand 	 * algorithms that requires client to supply padding.
800b0104773SPascal Brand 	 */
801b0104773SPascal Brand 	if (op->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
802b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
803b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
804b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
805b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
806b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) {
807b0104773SPascal Brand 		if (((op->buffer_offs + srcLen) % op->block_size) != 0)
808b0104773SPascal Brand 			return TEE_ERROR_BAD_PARAMETERS;
809b0104773SPascal Brand 	}
810b0104773SPascal Brand 
811b0104773SPascal Brand 	/*
812b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
813b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
814b0104773SPascal Brand 	 * can't restore sync with this API.
815b0104773SPascal Brand 	 */
816b0104773SPascal Brand 	req_dlen = op->buffer_offs + srcLen;
817b0104773SPascal Brand 	if (*destLen < req_dlen) {
818b0104773SPascal Brand 		*destLen = req_dlen;
819b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
820b0104773SPascal Brand 	}
821b0104773SPascal Brand 
822b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
823b0104773SPascal Brand 	tee_buffer_update(op, utee_cipher_update, srcData, srcLen, dst,
824b0104773SPascal Brand 			  &tmp_dlen);
825b0104773SPascal Brand 	dst += tmp_dlen;
826b0104773SPascal Brand 	acc_dlen += tmp_dlen;
827b0104773SPascal Brand 
828b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
829b0104773SPascal Brand 	res = utee_cipher_final(op->state, op->buffer, op->buffer_offs,
830b0104773SPascal Brand 				dst, &tmp_dlen);
831b0104773SPascal Brand 	if (res != TEE_SUCCESS)
832b0104773SPascal Brand 		TEE_Panic(res);
833b0104773SPascal Brand 	acc_dlen += tmp_dlen;
834b0104773SPascal Brand 
835b0104773SPascal Brand 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
836b0104773SPascal Brand 	*destLen = acc_dlen;
837b0104773SPascal Brand 	return TEE_SUCCESS;
838b0104773SPascal Brand }
839b0104773SPascal Brand 
840b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */
841b0104773SPascal Brand 
84279a3c601SCedric Chaumont void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
843b0104773SPascal Brand {
844b0104773SPascal Brand 	TEE_Result res;
845b0104773SPascal Brand 
846b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
847b0104773SPascal Brand 		TEE_Panic(0);
848b0104773SPascal Brand 	if (IV == NULL && IVLen != 0)
849b0104773SPascal Brand 		TEE_Panic(0);
850b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_MAC)
851b0104773SPascal Brand 		TEE_Panic(0);
852b0104773SPascal Brand 	res = utee_hash_init(operation->state, IV, IVLen);
853b0104773SPascal Brand 	if (res != TEE_SUCCESS)
854b0104773SPascal Brand 		TEE_Panic(res);
855b0104773SPascal Brand 	operation->buffer_offs = 0;
856b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
857b0104773SPascal Brand }
858b0104773SPascal Brand 
85979a3c601SCedric Chaumont void TEE_MACUpdate(TEE_OperationHandle op, const void *chunk, uint32_t chunkSize)
860b0104773SPascal Brand {
861b0104773SPascal Brand 	TEE_Result res;
862b0104773SPascal Brand 
863b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
864b0104773SPascal Brand 		TEE_Panic(0);
865b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_MAC)
866b0104773SPascal Brand 		TEE_Panic(0);
867b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
868b0104773SPascal Brand 		TEE_Panic(0);
869b0104773SPascal Brand 
870b0104773SPascal Brand 	res = utee_hash_update(op->state, chunk, chunkSize);
871b0104773SPascal Brand 	if (res != TEE_SUCCESS)
872b0104773SPascal Brand 		TEE_Panic(res);
873b0104773SPascal Brand }
874b0104773SPascal Brand 
875b0104773SPascal Brand TEE_Result TEE_MACComputeFinal(TEE_OperationHandle op,
87679a3c601SCedric Chaumont 			       const void *message, uint32_t messageLen,
87779a3c601SCedric Chaumont 			       void *mac, uint32_t *macLen)
878b0104773SPascal Brand {
879b0104773SPascal Brand 	TEE_Result res;
880b0104773SPascal Brand 
881b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (message == NULL && messageLen != 0) ||
882b0104773SPascal Brand 	    mac == NULL || macLen == NULL)
883b0104773SPascal Brand 		TEE_Panic(0);
884b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_MAC)
885b0104773SPascal Brand 		TEE_Panic(0);
886b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
887b0104773SPascal Brand 		TEE_Panic(0);
888b0104773SPascal Brand 
889b0104773SPascal Brand 	res = utee_hash_final(op->state, message, messageLen, mac, macLen);
890b0104773SPascal Brand 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
891b0104773SPascal Brand 	return res;
892b0104773SPascal Brand }
893b0104773SPascal Brand 
894b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
89579a3c601SCedric Chaumont 			       const void *message, uint32_t messageLen,
89679a3c601SCedric Chaumont 			       const void *mac, uint32_t macLen)
897b0104773SPascal Brand {
898b0104773SPascal Brand 	TEE_Result res;
899b0104773SPascal Brand 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
9007f74c64aSPascal Brand 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
901b0104773SPascal Brand 
902b0104773SPascal Brand 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
903b0104773SPascal Brand 				  &computed_mac_size);
904b0104773SPascal Brand 	if (res != TEE_SUCCESS)
905b0104773SPascal Brand 		return res;
906b0104773SPascal Brand 	if (computed_mac_size != macLen)
907b0104773SPascal Brand 		return TEE_ERROR_MAC_INVALID;
908b796ebf3SJerome Forissier 	if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0)
909b0104773SPascal Brand 		return TEE_ERROR_MAC_INVALID;
910b0104773SPascal Brand 	return TEE_SUCCESS;
911b0104773SPascal Brand }
912b0104773SPascal Brand 
913b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */
914b0104773SPascal Brand 
915b0104773SPascal Brand TEE_Result TEE_AEInit(TEE_OperationHandle op, const void *nonce,
91679a3c601SCedric Chaumont 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
917b0104773SPascal Brand 		      uint32_t payloadLen)
918b0104773SPascal Brand {
919b0104773SPascal Brand 	TEE_Result res;
920b0104773SPascal Brand 
921b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || nonce == NULL)
922b0104773SPascal Brand 		TEE_Panic(0);
923b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
924b0104773SPascal Brand 		TEE_Panic(0);
925b0104773SPascal Brand 
926b0104773SPascal Brand 	/*
927b0104773SPascal Brand 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
928b0104773SPascal Brand 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
929b0104773SPascal Brand 	 * according to the same principle so we have to check here instead to
930b0104773SPascal Brand 	 * be GP compliant.
931b0104773SPascal Brand 	 */
932b0104773SPascal Brand 	if (op->info.algorithm == TEE_ALG_AES_GCM) {
933b0104773SPascal Brand 		/*
934b0104773SPascal Brand 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
935b0104773SPascal Brand 		 */
936b0104773SPascal Brand 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0))
937b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
938b0104773SPascal Brand 	}
939b0104773SPascal Brand 
940b0104773SPascal Brand 	res = utee_authenc_init(op->state, nonce, nonceLen, tagLen / 8, AADLen,
941b0104773SPascal Brand 				payloadLen);
942b0104773SPascal Brand 	if (res != TEE_SUCCESS) {
943b0104773SPascal Brand 		if (res != TEE_ERROR_NOT_SUPPORTED)
944b0104773SPascal Brand 			TEE_Panic(res);
945b0104773SPascal Brand 		return res;
946b0104773SPascal Brand 	}
947b0104773SPascal Brand 	op->ae_tag_len = tagLen / 8;
948b0104773SPascal Brand 
949b0104773SPascal Brand 	op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
950b0104773SPascal Brand 	return TEE_SUCCESS;
951b0104773SPascal Brand }
952b0104773SPascal Brand 
953b0104773SPascal Brand void TEE_AEUpdateAAD(TEE_OperationHandle op, const void *AADdata,
95479a3c601SCedric Chaumont 		     uint32_t AADdataLen)
955b0104773SPascal Brand {
956b0104773SPascal Brand 	TEE_Result res;
957b0104773SPascal Brand 
958b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (AADdata == NULL && AADdataLen != 0))
959b0104773SPascal Brand 		TEE_Panic(0);
960b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
961b0104773SPascal Brand 		TEE_Panic(0);
962b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
963b0104773SPascal Brand 		TEE_Panic(0);
964b0104773SPascal Brand 
965b0104773SPascal Brand 	res = utee_authenc_update_aad(op->state, AADdata, AADdataLen);
966b0104773SPascal Brand 	if (res != TEE_SUCCESS)
967b0104773SPascal Brand 		TEE_Panic(res);
968b0104773SPascal Brand }
969b0104773SPascal Brand 
970b0104773SPascal Brand TEE_Result TEE_AEUpdate(TEE_OperationHandle op, const void *srcData,
97179a3c601SCedric Chaumont 			uint32_t srcLen, void *destData, uint32_t *destLen)
972b0104773SPascal Brand {
973b0104773SPascal Brand 	size_t req_dlen;
974b0104773SPascal Brand 
975b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
976b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
977b0104773SPascal Brand 		TEE_Panic(0);
978b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
979b0104773SPascal Brand 		TEE_Panic(0);
980b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
981b0104773SPascal Brand 		TEE_Panic(0);
982b0104773SPascal Brand 
983b0104773SPascal Brand 	/*
984b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
985b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
986b0104773SPascal Brand 	 * can't restore sync with this API.
987b0104773SPascal Brand 	 */
9882ff3fdbbSPascal Brand 	req_dlen = ROUNDDOWN(op->buffer_offs + srcLen, op->block_size);
989b0104773SPascal Brand 	if (*destLen < req_dlen) {
990b0104773SPascal Brand 		*destLen = req_dlen;
991b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
992b0104773SPascal Brand 	}
993b0104773SPascal Brand 
994b0104773SPascal Brand 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
995b0104773SPascal Brand 			  destData, destLen);
996b0104773SPascal Brand 
997b0104773SPascal Brand 	return TEE_SUCCESS;
998b0104773SPascal Brand }
999b0104773SPascal Brand 
1000b0104773SPascal Brand TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle op,
100179a3c601SCedric Chaumont 			      const void *srcData, uint32_t srcLen,
100279a3c601SCedric Chaumont 			      void *destData, uint32_t *destLen, void *tag,
100379a3c601SCedric Chaumont 			      uint32_t *tagLen)
1004b0104773SPascal Brand {
1005b0104773SPascal Brand 	TEE_Result res;
1006b0104773SPascal Brand 	uint8_t *dst = destData;
1007b0104773SPascal Brand 	size_t acc_dlen = 0;
10087f74c64aSPascal Brand 	uint32_t tmp_dlen;
1009b0104773SPascal Brand 	size_t req_dlen;
1010b0104773SPascal Brand 
1011b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1012b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0) ||
1013b0104773SPascal Brand 	    tag == NULL || tagLen == NULL)
1014b0104773SPascal Brand 		TEE_Panic(0);
1015b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
1016b0104773SPascal Brand 		TEE_Panic(0);
1017b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1018b0104773SPascal Brand 		TEE_Panic(0);
1019b0104773SPascal Brand 
1020b0104773SPascal Brand 	/*
1021b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1022b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1023b0104773SPascal Brand 	 * can't restore sync with this API.
1024b0104773SPascal Brand 	 */
1025b0104773SPascal Brand 	req_dlen = op->buffer_offs + srcLen;
1026b0104773SPascal Brand 	if (*destLen < req_dlen) {
1027b0104773SPascal Brand 		*destLen = req_dlen;
1028b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
1029b0104773SPascal Brand 	}
1030b0104773SPascal Brand 
1031b0104773SPascal Brand 	/*
1032b0104773SPascal Brand 	 * Need to check this before update_payload since sync would be lost if
1033b0104773SPascal Brand 	 * we return short buffer after that.
1034b0104773SPascal Brand 	 */
1035b0104773SPascal Brand 	if (*tagLen < op->ae_tag_len) {
1036b0104773SPascal Brand 		*tagLen = op->ae_tag_len;
1037b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
1038b0104773SPascal Brand 	}
1039b0104773SPascal Brand 
1040b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1041b0104773SPascal Brand 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
1042b0104773SPascal Brand 			  dst, &tmp_dlen);
1043b0104773SPascal Brand 	dst += tmp_dlen;
1044b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1045b0104773SPascal Brand 
1046b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1047b0104773SPascal Brand 	res =
1048b0104773SPascal Brand 	    utee_authenc_enc_final(op->state, op->buffer, op->buffer_offs, dst,
1049b0104773SPascal Brand 				   &tmp_dlen, tag, tagLen);
1050b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1051b0104773SPascal Brand 		TEE_Panic(res);
1052b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1053b0104773SPascal Brand 
1054b0104773SPascal Brand 	*destLen = acc_dlen;
1055b0104773SPascal Brand 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1056b0104773SPascal Brand 
1057b0104773SPascal Brand 	return res;
1058b0104773SPascal Brand }
1059b0104773SPascal Brand 
1060b0104773SPascal Brand TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle op,
106179a3c601SCedric Chaumont 			      const void *srcData, uint32_t srcLen,
106279a3c601SCedric Chaumont 			      void *destData, uint32_t *destLen, const void *tag,
106379a3c601SCedric Chaumont 			      uint32_t tagLen)
1064b0104773SPascal Brand {
1065b0104773SPascal Brand 	TEE_Result res;
1066b0104773SPascal Brand 	uint8_t *dst = destData;
1067b0104773SPascal Brand 	size_t acc_dlen = 0;
10687f74c64aSPascal Brand 	uint32_t tmp_dlen;
1069b0104773SPascal Brand 	size_t req_dlen;
1070b0104773SPascal Brand 
1071b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1072b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0) ||
1073b0104773SPascal Brand 	    (tag == NULL && tagLen != 0))
1074b0104773SPascal Brand 		TEE_Panic(0);
1075b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
1076b0104773SPascal Brand 		TEE_Panic(0);
1077b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1078b0104773SPascal Brand 		TEE_Panic(0);
1079b0104773SPascal Brand 
1080b0104773SPascal Brand 	/*
1081b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
1082b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
1083b0104773SPascal Brand 	 * can't restore sync with this API.
1084b0104773SPascal Brand 	 */
1085b0104773SPascal Brand 	req_dlen = op->buffer_offs + srcLen;
1086b0104773SPascal Brand 	if (*destLen < req_dlen) {
1087b0104773SPascal Brand 		*destLen = req_dlen;
1088b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
1089b0104773SPascal Brand 	}
1090b0104773SPascal Brand 
1091b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1092b0104773SPascal Brand 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
1093b0104773SPascal Brand 			  dst, &tmp_dlen);
1094b0104773SPascal Brand 	dst += tmp_dlen;
1095b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1096b0104773SPascal Brand 
1097b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
1098b0104773SPascal Brand 	res =
1099b0104773SPascal Brand 	    utee_authenc_dec_final(op->state, op->buffer, op->buffer_offs, dst,
1100b0104773SPascal Brand 				   &tmp_dlen, tag, tagLen);
1101b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_MAC_INVALID)
1102b0104773SPascal Brand 		TEE_Panic(res);
1103b0104773SPascal Brand 	/* Supplied tagLen should match what we initiated with */
1104b0104773SPascal Brand 	if (tagLen != op->ae_tag_len)
1105b0104773SPascal Brand 		res = TEE_ERROR_MAC_INVALID;
1106b0104773SPascal Brand 
1107b0104773SPascal Brand 	acc_dlen += tmp_dlen;
1108b0104773SPascal Brand 
1109b0104773SPascal Brand 	*destLen = acc_dlen;
1110b0104773SPascal Brand 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1111b0104773SPascal Brand 
1112b0104773SPascal Brand 	return res;
1113b0104773SPascal Brand }
1114b0104773SPascal Brand 
1115b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */
1116b0104773SPascal Brand 
1117b0104773SPascal Brand TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle op,
1118b0104773SPascal Brand 				 const TEE_Attribute *params,
1119b0104773SPascal Brand 				 uint32_t paramCount, const void *srcData,
112079a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
112179a3c601SCedric Chaumont 				 uint32_t *destLen)
1122b0104773SPascal Brand {
1123b0104773SPascal Brand 	TEE_Result res;
1124b0104773SPascal Brand 
1125b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1126b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
1127b0104773SPascal Brand 		TEE_Panic(0);
1128b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1129b0104773SPascal Brand 		TEE_Panic(0);
1130b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1131b0104773SPascal Brand 		TEE_Panic(0);
1132b0104773SPascal Brand 	if (op->info.mode != TEE_MODE_ENCRYPT)
1133b0104773SPascal Brand 		TEE_Panic(0);
1134b0104773SPascal Brand 
1135b0104773SPascal Brand 	res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen,
1136b0104773SPascal Brand 				 destData, destLen);
11378844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
11388844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
11398844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1140b0104773SPascal Brand 		TEE_Panic(res);
1141b0104773SPascal Brand 	return res;
1142b0104773SPascal Brand }
1143b0104773SPascal Brand 
1144b0104773SPascal Brand TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle op,
1145b0104773SPascal Brand 				 const TEE_Attribute *params,
1146b0104773SPascal Brand 				 uint32_t paramCount, const void *srcData,
114779a3c601SCedric Chaumont 				 uint32_t srcLen, void *destData,
114879a3c601SCedric Chaumont 				 uint32_t *destLen)
1149b0104773SPascal Brand {
1150b0104773SPascal Brand 	TEE_Result res;
1151b0104773SPascal Brand 
1152b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1153b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
1154b0104773SPascal Brand 		TEE_Panic(0);
1155b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1156b0104773SPascal Brand 		TEE_Panic(0);
1157b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1158b0104773SPascal Brand 		TEE_Panic(0);
1159b0104773SPascal Brand 	if (op->info.mode != TEE_MODE_DECRYPT)
1160b0104773SPascal Brand 		TEE_Panic(0);
1161b0104773SPascal Brand 
1162b0104773SPascal Brand 	res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen,
1163b0104773SPascal Brand 				 destData, destLen);
11648844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
11658844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
11668844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1167b0104773SPascal Brand 		TEE_Panic(res);
1168b0104773SPascal Brand 	return res;
1169b0104773SPascal Brand }
1170b0104773SPascal Brand 
1171b0104773SPascal Brand TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle op,
1172b0104773SPascal Brand 				    const TEE_Attribute *params,
1173b0104773SPascal Brand 				    uint32_t paramCount, const void *digest,
117479a3c601SCedric Chaumont 				    uint32_t digestLen, void *signature,
117579a3c601SCedric Chaumont 				    uint32_t *signatureLen)
1176b0104773SPascal Brand {
1177b0104773SPascal Brand 	TEE_Result res;
1178b0104773SPascal Brand 
1179b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) ||
1180b0104773SPascal Brand 	    signature == NULL || signatureLen == NULL)
1181b0104773SPascal Brand 		TEE_Panic(0);
1182b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1183b0104773SPascal Brand 		TEE_Panic(0);
1184b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1185b0104773SPascal Brand 		TEE_Panic(0);
1186b0104773SPascal Brand 	if (op->info.mode != TEE_MODE_SIGN)
1187b0104773SPascal Brand 		TEE_Panic(0);
1188b0104773SPascal Brand 
1189b0104773SPascal Brand 	res =
1190b0104773SPascal Brand 	    utee_asymm_operate(op->state, params, paramCount, digest, digestLen,
1191b0104773SPascal Brand 			       signature, signatureLen);
1192b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1193b0104773SPascal Brand 		TEE_Panic(res);
1194b0104773SPascal Brand 	return res;
1195b0104773SPascal Brand }
1196b0104773SPascal Brand 
1197b0104773SPascal Brand TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle op,
1198b0104773SPascal Brand 				      const TEE_Attribute *params,
1199b0104773SPascal Brand 				      uint32_t paramCount, const void *digest,
120079a3c601SCedric Chaumont 				      uint32_t digestLen, const void *signature,
120179a3c601SCedric Chaumont 				      uint32_t signatureLen)
1202b0104773SPascal Brand {
1203b0104773SPascal Brand 	TEE_Result res;
1204b0104773SPascal Brand 
1205b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) ||
1206b0104773SPascal Brand 	    (signature == NULL && signatureLen != 0))
1207b0104773SPascal Brand 		TEE_Panic(0);
1208b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1209b0104773SPascal Brand 		TEE_Panic(0);
1210b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1211b0104773SPascal Brand 		TEE_Panic(0);
1212b0104773SPascal Brand 	if (op->info.mode != TEE_MODE_VERIFY)
1213b0104773SPascal Brand 		TEE_Panic(0);
1214b0104773SPascal Brand 
1215b0104773SPascal Brand 	res =
1216b0104773SPascal Brand 	    utee_asymm_verify(op->state, params, paramCount, digest, digestLen,
1217b0104773SPascal Brand 			      signature, signatureLen);
1218b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1219b0104773SPascal Brand 		TEE_Panic(res);
1220b0104773SPascal Brand 	return res;
1221b0104773SPascal Brand }
1222b0104773SPascal Brand 
1223b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */
1224b0104773SPascal Brand 
1225b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation,
1226b0104773SPascal Brand 		   const TEE_Attribute *params, uint32_t paramCount,
1227b0104773SPascal Brand 		   TEE_ObjectHandle derivedKey)
1228b0104773SPascal Brand {
1229b0104773SPascal Brand 	TEE_Result res;
1230b0104773SPascal Brand 	TEE_ObjectInfo key_info;
1231b0104773SPascal Brand 
1232b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1233b0104773SPascal Brand 		TEE_Panic(0);
1234b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1235b0104773SPascal Brand 		TEE_Panic(0);
12368854d3c6SJerome Forissier 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
12378854d3c6SJerome Forissier 			TEE_OPERATION_KEY_DERIVATION)
1238b0104773SPascal Brand 		TEE_Panic(0);
1239b0104773SPascal Brand 
1240b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1241b0104773SPascal Brand 		TEE_Panic(0);
1242b0104773SPascal Brand 	if (operation->info.mode != TEE_MODE_DERIVE)
1243b0104773SPascal Brand 		TEE_Panic(0);
1244b0104773SPascal Brand 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1245b0104773SPascal Brand 		TEE_Panic(0);
1246b0104773SPascal Brand 
1247b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t) derivedKey, &key_info);
1248b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1249b0104773SPascal Brand 		TEE_Panic(0);
1250b0104773SPascal Brand 
1251b0104773SPascal Brand 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1252b0104773SPascal Brand 		TEE_Panic(0);
1253b0104773SPascal Brand 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1254b0104773SPascal Brand 		TEE_Panic(0);
1255b0104773SPascal Brand 
1256b0104773SPascal Brand 	res = utee_cryp_derive_key(operation->state, params, paramCount,
1257b0104773SPascal Brand 				   (uint32_t) derivedKey);
1258b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1259b0104773SPascal Brand 		TEE_Panic(res);
1260b0104773SPascal Brand }
1261b0104773SPascal Brand 
1262b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */
1263b0104773SPascal Brand 
126479a3c601SCedric Chaumont void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1265b0104773SPascal Brand {
1266b0104773SPascal Brand 	TEE_Result res;
1267b0104773SPascal Brand 
1268b0104773SPascal Brand 	res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1269b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1270b0104773SPascal Brand 		TEE_Panic(res);
1271b0104773SPascal Brand }
1272