xref: /optee_os/lib/libutee/tee_api_operations.c (revision b796ebf3db720ab1e0ad2203c7456160c023da2e)
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>
29*b796ebf3SJerome Forissier #include <string_ext.h>
30b0104773SPascal Brand 
31b0104773SPascal Brand #include <tee_api.h>
32b0104773SPascal Brand #include <tee_internal_api_extensions.h>
33b0104773SPascal Brand #include <utee_syscalls.h>
34b0104773SPascal Brand #include <utee_defines.h>
35b0104773SPascal Brand 
36b0104773SPascal Brand struct __TEE_OperationHandle {
37b0104773SPascal Brand 	TEE_OperationInfo info;
38b0104773SPascal Brand 	TEE_ObjectHandle key1;
39b0104773SPascal Brand 	TEE_ObjectHandle key2;
40b0104773SPascal Brand 	uint8_t *buffer;	/* buffer to collect complete blocks */
41b0104773SPascal Brand 	bool buffer_two_blocks;	/* True if two blocks need to be buffered */
42b0104773SPascal Brand 	size_t block_size;	/* Block size of cipher */
43b0104773SPascal Brand 	size_t buffer_offs;	/* Offset in buffer */
44b0104773SPascal Brand 	uint32_t state;		/* Handle to state in TEE Core */
45b0104773SPascal Brand 	uint32_t ae_tag_len;	/*
46b0104773SPascal Brand 				 * tag_len in bytes for AE operation else unused
47b0104773SPascal Brand 				 */
48b0104773SPascal Brand };
49b0104773SPascal Brand 
50b0104773SPascal Brand /* Cryptographic Operations API - Generic Operation Functions */
51b0104773SPascal Brand 
52b0104773SPascal Brand TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation,
53b0104773SPascal Brand 				 uint32_t algorithm, uint32_t mode,
54b0104773SPascal Brand 				 uint32_t maxKeySize)
55b0104773SPascal Brand {
56b0104773SPascal Brand 	TEE_Result res;
57b0104773SPascal Brand 	TEE_OperationHandle op = TEE_HANDLE_NULL;
58b0104773SPascal Brand 	uint32_t handle_state = 0;
59b0104773SPascal Brand 	size_t block_size = 1;
60b0104773SPascal Brand 	uint32_t req_key_usage;
61b0104773SPascal Brand 	bool with_private_key = false;
62b0104773SPascal Brand 	bool buffer_two_blocks = false;
63b0104773SPascal Brand 
64b0104773SPascal Brand 	if (operation == NULL)
65b0104773SPascal Brand 		TEE_Panic(0);
66b0104773SPascal Brand 
67b0104773SPascal Brand 	if (algorithm == TEE_ALG_AES_XTS)
68b0104773SPascal Brand 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
69b0104773SPascal Brand 
70b0104773SPascal Brand 	switch (algorithm) {
71b0104773SPascal Brand 	case TEE_ALG_AES_CTS:
72b0104773SPascal Brand 	case TEE_ALG_AES_XTS:
73b0104773SPascal Brand 		buffer_two_blocks = true;
74b0104773SPascal Brand 	 /*FALLTHROUGH*/ case TEE_ALG_AES_ECB_NOPAD:
75b0104773SPascal Brand 	case TEE_ALG_AES_CBC_NOPAD:
76b0104773SPascal Brand 	case TEE_ALG_AES_CTR:
77b0104773SPascal Brand 	case TEE_ALG_AES_CCM:
78b0104773SPascal Brand 	case TEE_ALG_AES_GCM:
79b0104773SPascal Brand 	case TEE_ALG_DES_ECB_NOPAD:
80b0104773SPascal Brand 	case TEE_ALG_DES_CBC_NOPAD:
81b0104773SPascal Brand 	case TEE_ALG_DES3_ECB_NOPAD:
82b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_NOPAD:
83b0104773SPascal Brand 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
84b0104773SPascal Brand 			block_size = TEE_AES_BLOCK_SIZE;
85b0104773SPascal Brand 		else
86b0104773SPascal Brand 			block_size = TEE_DES_BLOCK_SIZE;
87b0104773SPascal Brand 
88b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT)
89b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
90b0104773SPascal Brand 		else if (mode == TEE_MODE_DECRYPT)
91b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
92b0104773SPascal Brand 		else
93b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
94b0104773SPascal Brand 		break;
95b0104773SPascal Brand 
96b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
97b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
98b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
99b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
100b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
101b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
102b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
103b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
104b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
105b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
106b0104773SPascal Brand 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
107b0104773SPascal Brand 	case TEE_ALG_DSA_SHA1:
108b0104773SPascal Brand 		if (mode == TEE_MODE_SIGN) {
109b0104773SPascal Brand 			with_private_key = true;
110b0104773SPascal Brand 			req_key_usage = TEE_USAGE_SIGN;
111b0104773SPascal Brand 		} else if (mode == TEE_MODE_VERIFY) {
112b0104773SPascal Brand 			req_key_usage = TEE_USAGE_VERIFY;
113b0104773SPascal Brand 		} else {
114b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
115b0104773SPascal Brand 		}
116b0104773SPascal Brand 		break;
117b0104773SPascal Brand 
118b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_V1_5:
119b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
120b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
121b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
122b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
123b0104773SPascal Brand 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
124b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
125b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT;
126b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
127b0104773SPascal Brand 			with_private_key = true;
128b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT;
129b0104773SPascal Brand 		} else {
130b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
131b0104773SPascal Brand 		}
132b0104773SPascal Brand 		break;
133b0104773SPascal Brand 
134b0104773SPascal Brand 	case TEE_ALG_RSA_NOPAD:
135b0104773SPascal Brand 		if (mode == TEE_MODE_ENCRYPT) {
136b0104773SPascal Brand 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
137b0104773SPascal Brand 		} else if (mode == TEE_MODE_DECRYPT) {
138b0104773SPascal Brand 			with_private_key = true;
139b0104773SPascal Brand 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
140b0104773SPascal Brand 		} else {
141b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
142b0104773SPascal Brand 		}
143b0104773SPascal Brand 		break;
144b0104773SPascal Brand 
145b0104773SPascal Brand 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
146b0104773SPascal Brand 		if (mode != TEE_MODE_DERIVE)
147b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
148b0104773SPascal Brand 		with_private_key = true;
149b0104773SPascal Brand 		req_key_usage = TEE_USAGE_DERIVE;
150b0104773SPascal Brand 		break;
151b0104773SPascal Brand 
152b0104773SPascal Brand 	case TEE_ALG_MD5:
153b0104773SPascal Brand 	case TEE_ALG_SHA1:
154b0104773SPascal Brand 	case TEE_ALG_SHA224:
155b0104773SPascal Brand 	case TEE_ALG_SHA256:
156b0104773SPascal Brand 	case TEE_ALG_SHA384:
157b0104773SPascal Brand 	case TEE_ALG_SHA512:
158b0104773SPascal Brand 		if (mode != TEE_MODE_DIGEST)
159b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
160b0104773SPascal Brand 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
161b0104773SPascal Brand 		req_key_usage = 0;
162b0104773SPascal Brand 		break;
163b0104773SPascal Brand 
164b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_NOPAD:
165b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_NOPAD:
166b0104773SPascal Brand 	case TEE_ALG_AES_CBC_MAC_PKCS5:
167b0104773SPascal Brand 	case TEE_ALG_AES_CMAC:
168b0104773SPascal Brand 	case TEE_ALG_DES_CBC_MAC_PKCS5:
169b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
170b0104773SPascal Brand 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
171b0104773SPascal Brand 	case TEE_ALG_HMAC_MD5:
172b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA1:
173b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA224:
174b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA256:
175b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA384:
176b0104773SPascal Brand 	case TEE_ALG_HMAC_SHA512:
177b0104773SPascal Brand 		if (mode != TEE_MODE_MAC)
178b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
179b0104773SPascal Brand 		req_key_usage = TEE_USAGE_MAC;
180b0104773SPascal Brand 		break;
181b0104773SPascal Brand 
182b0104773SPascal Brand 	default:
183b0104773SPascal Brand 		return TEE_ERROR_NOT_SUPPORTED;
184b0104773SPascal Brand 	}
185b0104773SPascal Brand 
186b0104773SPascal Brand 	op = TEE_Malloc(sizeof(*op), 0);
187b0104773SPascal Brand 	if (op == NULL)
188b0104773SPascal Brand 		return TEE_ERROR_OUT_OF_MEMORY;
189b0104773SPascal Brand 
190b0104773SPascal Brand 	op->info.algorithm = algorithm;
191b0104773SPascal Brand 	op->info.operationClass = TEE_ALG_GET_CLASS(algorithm);
192b0104773SPascal Brand 	op->info.mode = mode;
193b0104773SPascal Brand 	op->info.maxKeySize = maxKeySize;
194b0104773SPascal Brand 	op->info.requiredKeyUsage = req_key_usage;
195b0104773SPascal Brand 	op->info.handleState = handle_state;
196b0104773SPascal Brand 
197b0104773SPascal Brand 	if (block_size > 1) {
198b0104773SPascal Brand 		size_t buffer_size = block_size;
199b0104773SPascal Brand 
200b0104773SPascal Brand 		if (buffer_two_blocks)
201b0104773SPascal Brand 			buffer_size *= 2;
202b0104773SPascal Brand 
203b0104773SPascal Brand 		op->buffer =
204b0104773SPascal Brand 		    TEE_Malloc(buffer_size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
205b0104773SPascal Brand 		if (op->buffer == NULL) {
206b0104773SPascal Brand 			res = TEE_ERROR_OUT_OF_MEMORY;
207b0104773SPascal Brand 			goto out;
208b0104773SPascal Brand 		}
209b0104773SPascal Brand 	}
210b0104773SPascal Brand 	op->block_size = block_size;
211b0104773SPascal Brand 	op->buffer_two_blocks = buffer_two_blocks;
212b0104773SPascal Brand 
213b0104773SPascal Brand 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
214b0104773SPascal Brand 		uint32_t mks = maxKeySize;
215b0104773SPascal Brand 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
216b0104773SPascal Brand 						       with_private_key);
217b0104773SPascal Brand 
218b0104773SPascal Brand 		/*
219b0104773SPascal Brand 		 * If two keys are expected the max key size is the sum of
220b0104773SPascal Brand 		 * the size of both keys.
221b0104773SPascal Brand 		 */
222b0104773SPascal Brand 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
223b0104773SPascal Brand 			mks /= 2;
224b0104773SPascal Brand 
225b0104773SPascal Brand 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
226b0104773SPascal Brand 		if (res != TEE_SUCCESS)
227b0104773SPascal Brand 			goto out;
228b0104773SPascal Brand 
229b0104773SPascal Brand 		if ((op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
230b0104773SPascal Brand 		    0) {
231b0104773SPascal Brand 			res =
232b0104773SPascal Brand 			    TEE_AllocateTransientObject(key_type, mks,
233b0104773SPascal Brand 							&op->key2);
234b0104773SPascal Brand 			if (res != TEE_SUCCESS)
235b0104773SPascal Brand 				goto out;
236b0104773SPascal Brand 		}
237b0104773SPascal Brand 	}
238b0104773SPascal Brand 
239b0104773SPascal Brand 	res = utee_cryp_state_alloc(algorithm, mode, (uint32_t) op->key1,
240b0104773SPascal Brand 				    (uint32_t) op->key2, &op->state);
241b0104773SPascal Brand 	if (res != TEE_SUCCESS)
242b0104773SPascal Brand 		goto out;
243b0104773SPascal Brand 
244b0104773SPascal Brand 	/* For multi-stage operation do an "init". */
245b0104773SPascal Brand 	TEE_ResetOperation(op);
246b0104773SPascal Brand 	*operation = op;
247b0104773SPascal Brand 
248b0104773SPascal Brand out:
249b0104773SPascal Brand 	if (res != TEE_SUCCESS) {
250b0104773SPascal Brand 		TEE_FreeTransientObject(op->key1);
251b0104773SPascal Brand 		TEE_FreeTransientObject(op->key2);
252b0104773SPascal Brand 		TEE_FreeOperation(op);
253b0104773SPascal Brand 	}
254b0104773SPascal Brand 
255b0104773SPascal Brand 	return res;
256b0104773SPascal Brand }
257b0104773SPascal Brand 
258b0104773SPascal Brand void TEE_FreeOperation(TEE_OperationHandle operation)
259b0104773SPascal Brand {
260b0104773SPascal Brand 	if (operation != TEE_HANDLE_NULL) {
261b0104773SPascal Brand 		/*
262b0104773SPascal Brand 		 * Note that keys should not be freed here, since they are
263b0104773SPascal Brand 		 * claimed by the operation they will be freed by
264b0104773SPascal Brand 		 * utee_cryp_state_free().
265b0104773SPascal Brand 		 */
266b0104773SPascal Brand 		utee_cryp_state_free(operation->state);
267b0104773SPascal Brand 		TEE_Free(operation->buffer);
268b0104773SPascal Brand 		TEE_Free(operation);
269b0104773SPascal Brand 	}
270b0104773SPascal Brand }
271b0104773SPascal Brand 
272b0104773SPascal Brand void TEE_GetOperationInfo(TEE_OperationHandle operation,
273b0104773SPascal Brand 			  TEE_OperationInfo *operationInfo)
274b0104773SPascal Brand {
275b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
276b0104773SPascal Brand 		TEE_Panic(0);
277b0104773SPascal Brand 
278b0104773SPascal Brand 	if (operationInfo == NULL)
279b0104773SPascal Brand 		TEE_Panic(0);
280b0104773SPascal Brand 
281b0104773SPascal Brand 	*operationInfo = operation->info;
282b0104773SPascal Brand }
283b0104773SPascal Brand 
284b0104773SPascal Brand void TEE_ResetOperation(TEE_OperationHandle operation)
285b0104773SPascal Brand {
286b0104773SPascal Brand 	TEE_Result res;
287b0104773SPascal Brand 
288b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
289b0104773SPascal Brand 		TEE_Panic(0);
290b0104773SPascal Brand 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
291b0104773SPascal Brand 		res = utee_hash_init(operation->state, NULL, 0);
292b0104773SPascal Brand 		if (res != TEE_SUCCESS)
293b0104773SPascal Brand 			TEE_Panic(res);
294b0104773SPascal Brand 	}
295b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
296b0104773SPascal Brand }
297b0104773SPascal Brand 
298b0104773SPascal Brand TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
299b0104773SPascal Brand 			       TEE_ObjectHandle key)
300b0104773SPascal Brand {
301b0104773SPascal Brand 	uint32_t key_size = 0;
302b0104773SPascal Brand 
303b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
304b0104773SPascal Brand 		TEE_Panic(0);
305b0104773SPascal Brand 
306b0104773SPascal Brand 	/* No key for digests */
307b0104773SPascal Brand 	if (operation->info.operationClass == TEE_OPERATION_DIGEST)
308b0104773SPascal Brand 		TEE_Panic(0);
309b0104773SPascal Brand 
310b0104773SPascal Brand 	/* Two keys expected */
311b0104773SPascal Brand 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
312b0104773SPascal Brand 	    0)
313b0104773SPascal Brand 		TEE_Panic(0);
314b0104773SPascal Brand 
315b0104773SPascal Brand 	if (key != TEE_HANDLE_NULL) {
316b0104773SPascal Brand 		TEE_ObjectInfo key_info;
317b0104773SPascal Brand 
318b0104773SPascal Brand 		TEE_GetObjectInfo(key, &key_info);
319b0104773SPascal Brand 		/* Supplied key has to meet required usage */
320b0104773SPascal Brand 		if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
321b0104773SPascal Brand 		    operation->info.requiredKeyUsage) {
322b0104773SPascal Brand 			TEE_Panic(0);
323b0104773SPascal Brand 		}
324b0104773SPascal Brand 
325b0104773SPascal Brand 		if (operation->info.maxKeySize < key_info.objectSize)
326b0104773SPascal Brand 			TEE_Panic(0);
327b0104773SPascal Brand 
328b0104773SPascal Brand 		key_size = key_info.objectSize;
329b0104773SPascal Brand 	}
330b0104773SPascal Brand 
331b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
332b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
333b0104773SPascal Brand 
334b0104773SPascal Brand 	if (key != TEE_HANDLE_NULL) {
335b0104773SPascal Brand 		TEE_CopyObjectAttributes(operation->key1, key);
336b0104773SPascal Brand 		operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
337b0104773SPascal Brand 	}
338b0104773SPascal Brand 
339b0104773SPascal Brand 	operation->info.keySize = key_size;
340b0104773SPascal Brand 
341b0104773SPascal Brand 	return TEE_SUCCESS;
342b0104773SPascal Brand }
343b0104773SPascal Brand 
344b0104773SPascal Brand TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
345b0104773SPascal Brand 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
346b0104773SPascal Brand {
347b0104773SPascal Brand 	uint32_t key_size = 0;
348b0104773SPascal Brand 
349b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
350b0104773SPascal Brand 		TEE_Panic(0);
351b0104773SPascal Brand 
352b0104773SPascal Brand 	/* Two keys not expected */
353b0104773SPascal Brand 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
354b0104773SPascal Brand 	    0)
355b0104773SPascal Brand 		TEE_Panic(0);
356b0104773SPascal Brand 
357b0104773SPascal Brand 	/* Either both keys are NULL or both are not NULL */
358b0104773SPascal Brand 	if ((key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) &&
359b0104773SPascal Brand 	    key1 != key2)
360b0104773SPascal Brand 		TEE_Panic(0);
361b0104773SPascal Brand 
362b0104773SPascal Brand 	if (key1 != TEE_HANDLE_NULL) {
363b0104773SPascal Brand 		TEE_ObjectInfo key_info1;
364b0104773SPascal Brand 		TEE_ObjectInfo key_info2;
365b0104773SPascal Brand 
366b0104773SPascal Brand 		TEE_GetObjectInfo(key1, &key_info1);
367b0104773SPascal Brand 		/* Supplied key has to meet required usage */
368b0104773SPascal Brand 		if ((key_info1.objectUsage & operation->info.
369b0104773SPascal Brand 		     requiredKeyUsage) != operation->info.requiredKeyUsage) {
370b0104773SPascal Brand 			TEE_Panic(0);
371b0104773SPascal Brand 		}
372b0104773SPascal Brand 
373b0104773SPascal Brand 		TEE_GetObjectInfo(key2, &key_info2);
374b0104773SPascal Brand 		/* Supplied key has to meet required usage */
375b0104773SPascal Brand 		if ((key_info2.objectUsage & operation->info.
376b0104773SPascal Brand 		     requiredKeyUsage) != operation->info.requiredKeyUsage) {
377b0104773SPascal Brand 			TEE_Panic(0);
378b0104773SPascal Brand 		}
379b0104773SPascal Brand 
380b0104773SPascal Brand 		/*
381b0104773SPascal Brand 		 * AES-XTS (the only multi key algorithm supported, requires the
382b0104773SPascal Brand 		 * keys to be of equal size.
383b0104773SPascal Brand 		 */
384b0104773SPascal Brand 		if (operation->info.algorithm == TEE_ALG_AES_XTS &&
385b0104773SPascal Brand 		    key_info1.objectSize != key_info2.objectSize)
386b0104773SPascal Brand 			TEE_Panic(0);
387b0104773SPascal Brand 
388b0104773SPascal Brand 		if (operation->info.maxKeySize < key_info1.objectSize)
389b0104773SPascal Brand 			TEE_Panic(0);
390b0104773SPascal Brand 
391b0104773SPascal Brand 		/*
392b0104773SPascal Brand 		 * Odd that only the size of one key should be reported while
393b0104773SPascal Brand 		 * size of two key are used when allocating the operation.
394b0104773SPascal Brand 		 */
395b0104773SPascal Brand 		key_size = key_info1.objectSize;
396b0104773SPascal Brand 	}
397b0104773SPascal Brand 
398b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key1);
399b0104773SPascal Brand 	TEE_ResetTransientObject(operation->key2);
400b0104773SPascal Brand 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
401b0104773SPascal Brand 
402b0104773SPascal Brand 	if (key1 != TEE_HANDLE_NULL) {
403b0104773SPascal Brand 		TEE_CopyObjectAttributes(operation->key1, key1);
404b0104773SPascal Brand 		TEE_CopyObjectAttributes(operation->key2, key2);
405b0104773SPascal Brand 		operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
406b0104773SPascal Brand 	}
407b0104773SPascal Brand 
408b0104773SPascal Brand 	operation->info.keySize = key_size;
409b0104773SPascal Brand 
410b0104773SPascal Brand 	return TEE_SUCCESS;
411b0104773SPascal Brand }
412b0104773SPascal Brand 
413b0104773SPascal Brand void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
414b0104773SPascal Brand {
415b0104773SPascal Brand 	TEE_Result res;
416b0104773SPascal Brand 
417b0104773SPascal Brand 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
418b0104773SPascal Brand 		TEE_Panic(0);
419b0104773SPascal Brand 	if (dst_op->info.algorithm != src_op->info.algorithm)
420b0104773SPascal Brand 		TEE_Panic(0);
421b0104773SPascal Brand 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
422b0104773SPascal Brand 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
423b0104773SPascal Brand 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
424b0104773SPascal Brand 
425b0104773SPascal Brand 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
426b0104773SPascal Brand 			key1 = src_op->key1;
427b0104773SPascal Brand 			key2 = src_op->key2;
428b0104773SPascal Brand 		}
429b0104773SPascal Brand 
430b0104773SPascal Brand 		if ((src_op->info.handleState &
431b0104773SPascal Brand 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
432b0104773SPascal Brand 			TEE_SetOperationKey(dst_op, key1);
433b0104773SPascal Brand 		} else {
434b0104773SPascal Brand 			TEE_SetOperationKey2(dst_op, key1, key2);
435b0104773SPascal Brand 		}
436b0104773SPascal Brand 	}
437b0104773SPascal Brand 	dst_op->info.handleState = src_op->info.handleState;
438b0104773SPascal Brand 	dst_op->info.keySize = src_op->info.keySize;
439b0104773SPascal Brand 
440b0104773SPascal Brand 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
441b0104773SPascal Brand 	    dst_op->block_size != src_op->block_size)
442b0104773SPascal Brand 		TEE_Panic(0);
443b0104773SPascal Brand 
444b0104773SPascal Brand 	if (dst_op->buffer != NULL) {
445b0104773SPascal Brand 		if (src_op->buffer == NULL)
446b0104773SPascal Brand 			TEE_Panic(0);
447b0104773SPascal Brand 
448b0104773SPascal Brand 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
449b0104773SPascal Brand 		dst_op->buffer_offs = src_op->buffer_offs;
450b0104773SPascal Brand 	} else if (src_op->buffer != NULL) {
451b0104773SPascal Brand 		TEE_Panic(0);
452b0104773SPascal Brand 	}
453b0104773SPascal Brand 
454b0104773SPascal Brand 	res = utee_cryp_state_copy(dst_op->state, src_op->state);
455b0104773SPascal Brand 	if (res != TEE_SUCCESS)
456b0104773SPascal Brand 		TEE_Panic(res);
457b0104773SPascal Brand }
458b0104773SPascal Brand 
459b0104773SPascal Brand /* Cryptographic Operations API - Message Digest Functions */
460b0104773SPascal Brand 
461b0104773SPascal Brand void TEE_DigestUpdate(TEE_OperationHandle operation,
462b0104773SPascal Brand 		      void *chunk, size_t chunkSize)
463b0104773SPascal Brand {
46473d6c3baSJoakim Bech 	TEE_Result res = TEE_ERROR_GENERIC;
465b0104773SPascal Brand 
46673d6c3baSJoakim Bech 	if (operation == TEE_HANDLE_NULL ||
46773d6c3baSJoakim Bech 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
468b0104773SPascal Brand 		TEE_Panic(0);
46973d6c3baSJoakim Bech 
470b0104773SPascal Brand 	res = utee_hash_update(operation->state, chunk, chunkSize);
471b0104773SPascal Brand 	if (res != TEE_SUCCESS)
472b0104773SPascal Brand 		TEE_Panic(res);
473b0104773SPascal Brand }
474b0104773SPascal Brand 
475b0104773SPascal Brand TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
476b0104773SPascal Brand 			     size_t chunkLen, void *hash, size_t *hashLen)
477b0104773SPascal Brand {
47873d6c3baSJoakim Bech 	if ((operation == TEE_HANDLE_NULL) || (!chunk && chunkLen) ||
47973d6c3baSJoakim Bech 	    !hash || !hashLen ||
48073d6c3baSJoakim Bech 	    (operation->info.operationClass != TEE_OPERATION_DIGEST))
481b0104773SPascal Brand 		TEE_Panic(0);
48273d6c3baSJoakim Bech 
483b0104773SPascal Brand 	return utee_hash_final(operation->state, chunk, chunkLen, hash,
484b0104773SPascal Brand 			       hashLen);
485b0104773SPascal Brand }
486b0104773SPascal Brand 
487b0104773SPascal Brand /* Cryptographic Operations API - Symmetric Cipher Functions */
488b0104773SPascal Brand 
489b0104773SPascal Brand void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, size_t IVLen)
490b0104773SPascal Brand {
491b0104773SPascal Brand 	TEE_Result res;
492b0104773SPascal Brand 
493b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
494b0104773SPascal Brand 		TEE_Panic(0);
495b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
496b0104773SPascal Brand 		TEE_Panic(0);
497b0104773SPascal Brand 	res = utee_cipher_init(operation->state, IV, IVLen);
498b0104773SPascal Brand 	if (res != TEE_SUCCESS)
499b0104773SPascal Brand 		TEE_Panic(res);
500b0104773SPascal Brand 	operation->buffer_offs = 0;
501b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
502b0104773SPascal Brand }
503b0104773SPascal Brand 
504b0104773SPascal Brand static TEE_Result tee_buffer_update(
505b0104773SPascal Brand 		TEE_OperationHandle op,
506b0104773SPascal Brand 		TEE_Result(*update_func) (uint32_t state, const void *src,
507b0104773SPascal Brand 					  size_t slen, void *dst, size_t *dlen),
508b0104773SPascal Brand 		const void *src_data, size_t src_len,
509b0104773SPascal Brand 		void *dest_data, size_t *dest_len)
510b0104773SPascal Brand {
511b0104773SPascal Brand 	TEE_Result res;
512b0104773SPascal Brand 	const uint8_t *src = src_data;
513b0104773SPascal Brand 	size_t slen = src_len;
514b0104773SPascal Brand 	uint8_t *dst = dest_data;
515b0104773SPascal Brand 	size_t dlen = *dest_len;
516b0104773SPascal Brand 	size_t acc_dlen = 0;
517b0104773SPascal Brand 	size_t tmp_dlen;
518b0104773SPascal Brand 	size_t l;
519b0104773SPascal Brand 	size_t buffer_size;
520b0104773SPascal Brand 
521b0104773SPascal Brand 	if (op->buffer_two_blocks)
522b0104773SPascal Brand 		buffer_size = op->block_size * 2;
523b0104773SPascal Brand 	else
524b0104773SPascal Brand 		buffer_size = op->block_size;
525b0104773SPascal Brand 
526b0104773SPascal Brand 	if (op->buffer_offs > 0) {
527b0104773SPascal Brand 		/* Fill up complete block */
528b0104773SPascal Brand 		if (op->buffer_offs < op->block_size)
529b0104773SPascal Brand 			l = MIN(slen, op->block_size - op->buffer_offs);
530b0104773SPascal Brand 		else
531b0104773SPascal Brand 			l = MIN(slen, buffer_size - op->buffer_offs);
532b0104773SPascal Brand 		memcpy(op->buffer + op->buffer_offs, src, l);
533b0104773SPascal Brand 		op->buffer_offs += l;
534b0104773SPascal Brand 		src += l;
535b0104773SPascal Brand 		slen -= l;
536b0104773SPascal Brand 		if ((op->buffer_offs % op->block_size) != 0)
537b0104773SPascal Brand 			goto out;	/* Nothing left to do */
538b0104773SPascal Brand 	}
539b0104773SPascal Brand 
540b0104773SPascal Brand 	/* If we can feed from buffer */
541b0104773SPascal Brand 	if (op->buffer_offs > 0 && (op->buffer_offs + slen) > buffer_size) {
5422ff3fdbbSPascal Brand 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
543b0104773SPascal Brand 				op->block_size);
544b0104773SPascal Brand 		l = MIN(op->buffer_offs, l);
545b0104773SPascal Brand 		tmp_dlen = dlen;
546b0104773SPascal Brand 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
547b0104773SPascal Brand 		if (res != TEE_SUCCESS)
548b0104773SPascal Brand 			TEE_Panic(res);
549b0104773SPascal Brand 		dst += tmp_dlen;
550b0104773SPascal Brand 		dlen -= tmp_dlen;
551b0104773SPascal Brand 		acc_dlen += tmp_dlen;
552b0104773SPascal Brand 		op->buffer_offs -= l;
553b0104773SPascal Brand 		if (op->buffer_offs > 0) {
554b0104773SPascal Brand 			/*
555b0104773SPascal Brand 			 * Slen is small enough to be contained in rest buffer.
556b0104773SPascal Brand 			 */
557b0104773SPascal Brand 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
558b0104773SPascal Brand 			memcpy(op->buffer + op->buffer_offs, src, slen);
559b0104773SPascal Brand 			op->buffer_offs += slen;
560b0104773SPascal Brand 			goto out;	/* Nothing left to do */
561b0104773SPascal Brand 		}
562b0104773SPascal Brand 	}
563b0104773SPascal Brand 
564b0104773SPascal Brand 	if (slen > buffer_size) {
565b0104773SPascal Brand 		/* Buffer is empty, feed as much as possible from src */
566b0104773SPascal Brand 		if (TEE_ALIGNMENT_IS_OK(src, uint32_t)) {
5672ff3fdbbSPascal Brand 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
568b0104773SPascal Brand 
569b0104773SPascal Brand 			tmp_dlen = dlen;
570b0104773SPascal Brand 			res = update_func(op->state, src, l, dst, &tmp_dlen);
571b0104773SPascal Brand 			if (res != TEE_SUCCESS)
572b0104773SPascal Brand 				TEE_Panic(res);
573b0104773SPascal Brand 			src += l;
574b0104773SPascal Brand 			slen -= l;
575b0104773SPascal Brand 			dst += tmp_dlen;
576b0104773SPascal Brand 			dlen -= tmp_dlen;
577b0104773SPascal Brand 			acc_dlen += tmp_dlen;
578b0104773SPascal Brand 		} else {
579b0104773SPascal Brand 			/*
580b0104773SPascal Brand 			 * Supplied data isn't well aligned, we're forced to
581b0104773SPascal Brand 			 * feed through the buffer.
582b0104773SPascal Brand 			 */
583b0104773SPascal Brand 			while (slen >= op->block_size) {
584b0104773SPascal Brand 				memcpy(op->buffer, src, op->block_size);
585b0104773SPascal Brand 
586b0104773SPascal Brand 				tmp_dlen = dlen;
587b0104773SPascal Brand 				res =
588b0104773SPascal Brand 				    update_func(op->state, op->buffer,
589b0104773SPascal Brand 						op->block_size, dst, &tmp_dlen);
590b0104773SPascal Brand 				if (res != TEE_SUCCESS)
591b0104773SPascal Brand 					TEE_Panic(res);
592b0104773SPascal Brand 				src += op->block_size;
593b0104773SPascal Brand 				slen -= op->block_size;
594b0104773SPascal Brand 				dst += tmp_dlen;
595b0104773SPascal Brand 				dlen -= tmp_dlen;
596b0104773SPascal Brand 				acc_dlen += tmp_dlen;
597b0104773SPascal Brand 			}
598b0104773SPascal Brand 		}
599b0104773SPascal Brand 	}
600b0104773SPascal Brand 
601b0104773SPascal Brand 	/* Slen is small enough to be contained in buffer. */
602b0104773SPascal Brand 	memcpy(op->buffer + op->buffer_offs, src, slen);
603b0104773SPascal Brand 	op->buffer_offs += slen;
604b0104773SPascal Brand 
605b0104773SPascal Brand out:
606b0104773SPascal Brand 	*dest_len = acc_dlen;
607b0104773SPascal Brand 	return TEE_SUCCESS;
608b0104773SPascal Brand }
609b0104773SPascal Brand 
610b0104773SPascal Brand TEE_Result TEE_CipherUpdate(TEE_OperationHandle op, const void *srcData,
611b0104773SPascal Brand 			    size_t srcLen, void *destData, size_t *destLen)
612b0104773SPascal Brand {
613b0104773SPascal Brand 	size_t req_dlen;
614b0104773SPascal Brand 
615b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
616b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
617b0104773SPascal Brand 		TEE_Panic(0);
618b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_CIPHER)
619b0104773SPascal Brand 		TEE_Panic(0);
620b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
621b0104773SPascal Brand 		TEE_Panic(0);
622b0104773SPascal Brand 
623b0104773SPascal Brand 	/* Calculate required dlen */
624b0104773SPascal Brand 	req_dlen = ((op->buffer_offs + srcLen) / op->block_size) *
625b0104773SPascal Brand 	    op->block_size;
626b0104773SPascal Brand 	if (op->buffer_two_blocks) {
627b0104773SPascal Brand 		if (req_dlen > op->block_size * 2)
628b0104773SPascal Brand 			req_dlen -= op->block_size * 2;
629b0104773SPascal Brand 		else
630b0104773SPascal Brand 			req_dlen = 0;
631b0104773SPascal Brand 	}
632b0104773SPascal Brand 	/*
633b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
634b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
635b0104773SPascal Brand 	 * can't restore sync with this API.
636b0104773SPascal Brand 	 */
637b0104773SPascal Brand 	if (*destLen < req_dlen) {
638b0104773SPascal Brand 		*destLen = req_dlen;
639b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
640b0104773SPascal Brand 	}
641b0104773SPascal Brand 
642b0104773SPascal Brand 	tee_buffer_update(op, utee_cipher_update, srcData, srcLen, destData,
643b0104773SPascal Brand 			  destLen);
644b0104773SPascal Brand 
645b0104773SPascal Brand 	return TEE_SUCCESS;
646b0104773SPascal Brand }
647b0104773SPascal Brand 
648b0104773SPascal Brand TEE_Result TEE_CipherDoFinal(TEE_OperationHandle op,
649b0104773SPascal Brand 			     const void *srcData, size_t srcLen, void *destData,
650b0104773SPascal Brand 			     size_t *destLen)
651b0104773SPascal Brand {
652b0104773SPascal Brand 	TEE_Result res;
653b0104773SPascal Brand 	uint8_t *dst = destData;
654b0104773SPascal Brand 	size_t acc_dlen = 0;
655b0104773SPascal Brand 	size_t tmp_dlen;
656b0104773SPascal Brand 	size_t req_dlen;
657b0104773SPascal Brand 
658b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
659b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
660b0104773SPascal Brand 		TEE_Panic(0);
661b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_CIPHER)
662b0104773SPascal Brand 		TEE_Panic(0);
663b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
664b0104773SPascal Brand 		TEE_Panic(0);
665b0104773SPascal Brand 
666b0104773SPascal Brand 	/*
667b0104773SPascal Brand 	 * Check that the final block doesn't require padding for those
668b0104773SPascal Brand 	 * algorithms that requires client to supply padding.
669b0104773SPascal Brand 	 */
670b0104773SPascal Brand 	if (op->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
671b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
672b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
673b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
674b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
675b0104773SPascal Brand 	    op->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) {
676b0104773SPascal Brand 		if (((op->buffer_offs + srcLen) % op->block_size) != 0)
677b0104773SPascal Brand 			return TEE_ERROR_BAD_PARAMETERS;
678b0104773SPascal Brand 	}
679b0104773SPascal Brand 
680b0104773SPascal Brand 	/*
681b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
682b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
683b0104773SPascal Brand 	 * can't restore sync with this API.
684b0104773SPascal Brand 	 */
685b0104773SPascal Brand 	req_dlen = op->buffer_offs + srcLen;
686b0104773SPascal Brand 	if (*destLen < req_dlen) {
687b0104773SPascal Brand 		*destLen = req_dlen;
688b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
689b0104773SPascal Brand 	}
690b0104773SPascal Brand 
691b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
692b0104773SPascal Brand 	tee_buffer_update(op, utee_cipher_update, srcData, srcLen, dst,
693b0104773SPascal Brand 			  &tmp_dlen);
694b0104773SPascal Brand 	dst += tmp_dlen;
695b0104773SPascal Brand 	acc_dlen += tmp_dlen;
696b0104773SPascal Brand 
697b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
698b0104773SPascal Brand 	res = utee_cipher_final(op->state, op->buffer, op->buffer_offs,
699b0104773SPascal Brand 				dst, &tmp_dlen);
700b0104773SPascal Brand 	if (res != TEE_SUCCESS)
701b0104773SPascal Brand 		TEE_Panic(res);
702b0104773SPascal Brand 	acc_dlen += tmp_dlen;
703b0104773SPascal Brand 
704b0104773SPascal Brand 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
705b0104773SPascal Brand 	*destLen = acc_dlen;
706b0104773SPascal Brand 	return TEE_SUCCESS;
707b0104773SPascal Brand }
708b0104773SPascal Brand 
709b0104773SPascal Brand /* Cryptographic Operations API - MAC Functions */
710b0104773SPascal Brand 
711b0104773SPascal Brand void TEE_MACInit(TEE_OperationHandle operation, const void *IV, size_t IVLen)
712b0104773SPascal Brand {
713b0104773SPascal Brand 	TEE_Result res;
714b0104773SPascal Brand 
715b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL)
716b0104773SPascal Brand 		TEE_Panic(0);
717b0104773SPascal Brand 	if (IV == NULL && IVLen != 0)
718b0104773SPascal Brand 		TEE_Panic(0);
719b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_MAC)
720b0104773SPascal Brand 		TEE_Panic(0);
721b0104773SPascal Brand 	res = utee_hash_init(operation->state, IV, IVLen);
722b0104773SPascal Brand 	if (res != TEE_SUCCESS)
723b0104773SPascal Brand 		TEE_Panic(res);
724b0104773SPascal Brand 	operation->buffer_offs = 0;
725b0104773SPascal Brand 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
726b0104773SPascal Brand }
727b0104773SPascal Brand 
728b0104773SPascal Brand void TEE_MACUpdate(TEE_OperationHandle op, const void *chunk, size_t chunkSize)
729b0104773SPascal Brand {
730b0104773SPascal Brand 	TEE_Result res;
731b0104773SPascal Brand 
732b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
733b0104773SPascal Brand 		TEE_Panic(0);
734b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_MAC)
735b0104773SPascal Brand 		TEE_Panic(0);
736b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
737b0104773SPascal Brand 		TEE_Panic(0);
738b0104773SPascal Brand 
739b0104773SPascal Brand 	res = utee_hash_update(op->state, chunk, chunkSize);
740b0104773SPascal Brand 	if (res != TEE_SUCCESS)
741b0104773SPascal Brand 		TEE_Panic(res);
742b0104773SPascal Brand }
743b0104773SPascal Brand 
744b0104773SPascal Brand TEE_Result TEE_MACComputeFinal(TEE_OperationHandle op,
745b0104773SPascal Brand 			       const void *message, size_t messageLen,
746b0104773SPascal Brand 			       void *mac, size_t *macLen)
747b0104773SPascal Brand {
748b0104773SPascal Brand 	TEE_Result res;
749b0104773SPascal Brand 
750b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (message == NULL && messageLen != 0) ||
751b0104773SPascal Brand 	    mac == NULL || macLen == NULL)
752b0104773SPascal Brand 		TEE_Panic(0);
753b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_MAC)
754b0104773SPascal Brand 		TEE_Panic(0);
755b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
756b0104773SPascal Brand 		TEE_Panic(0);
757b0104773SPascal Brand 
758b0104773SPascal Brand 	res = utee_hash_final(op->state, message, messageLen, mac, macLen);
759b0104773SPascal Brand 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
760b0104773SPascal Brand 	return res;
761b0104773SPascal Brand }
762b0104773SPascal Brand 
763b0104773SPascal Brand TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
764b0104773SPascal Brand 			       const void *message, size_t messageLen,
765b0104773SPascal Brand 			       const void *mac, size_t macLen)
766b0104773SPascal Brand {
767b0104773SPascal Brand 	TEE_Result res;
768b0104773SPascal Brand 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
769b0104773SPascal Brand 	size_t computed_mac_size = TEE_MAX_HASH_SIZE;
770b0104773SPascal Brand 
771b0104773SPascal Brand 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
772b0104773SPascal Brand 				  &computed_mac_size);
773b0104773SPascal Brand 	if (res != TEE_SUCCESS)
774b0104773SPascal Brand 		return res;
775b0104773SPascal Brand 	if (computed_mac_size != macLen)
776b0104773SPascal Brand 		return TEE_ERROR_MAC_INVALID;
777*b796ebf3SJerome Forissier 	if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0)
778b0104773SPascal Brand 		return TEE_ERROR_MAC_INVALID;
779b0104773SPascal Brand 	return TEE_SUCCESS;
780b0104773SPascal Brand }
781b0104773SPascal Brand 
782b0104773SPascal Brand /* Cryptographic Operations API - Authenticated Encryption Functions */
783b0104773SPascal Brand 
784b0104773SPascal Brand TEE_Result TEE_AEInit(TEE_OperationHandle op, const void *nonce,
785b0104773SPascal Brand 		      size_t nonceLen, uint32_t tagLen, uint32_t AADLen,
786b0104773SPascal Brand 		      uint32_t payloadLen)
787b0104773SPascal Brand {
788b0104773SPascal Brand 	TEE_Result res;
789b0104773SPascal Brand 
790b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || nonce == NULL)
791b0104773SPascal Brand 		TEE_Panic(0);
792b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
793b0104773SPascal Brand 		TEE_Panic(0);
794b0104773SPascal Brand 
795b0104773SPascal Brand 	/*
796b0104773SPascal Brand 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
797b0104773SPascal Brand 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
798b0104773SPascal Brand 	 * according to the same principle so we have to check here instead to
799b0104773SPascal Brand 	 * be GP compliant.
800b0104773SPascal Brand 	 */
801b0104773SPascal Brand 	if (op->info.algorithm == TEE_ALG_AES_GCM) {
802b0104773SPascal Brand 		/*
803b0104773SPascal Brand 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
804b0104773SPascal Brand 		 */
805b0104773SPascal Brand 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0))
806b0104773SPascal Brand 			return TEE_ERROR_NOT_SUPPORTED;
807b0104773SPascal Brand 	}
808b0104773SPascal Brand 
809b0104773SPascal Brand 	res = utee_authenc_init(op->state, nonce, nonceLen, tagLen / 8, AADLen,
810b0104773SPascal Brand 				payloadLen);
811b0104773SPascal Brand 	if (res != TEE_SUCCESS) {
812b0104773SPascal Brand 		if (res != TEE_ERROR_NOT_SUPPORTED)
813b0104773SPascal Brand 			TEE_Panic(res);
814b0104773SPascal Brand 		return res;
815b0104773SPascal Brand 	}
816b0104773SPascal Brand 	op->ae_tag_len = tagLen / 8;
817b0104773SPascal Brand 
818b0104773SPascal Brand 	op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
819b0104773SPascal Brand 	return TEE_SUCCESS;
820b0104773SPascal Brand }
821b0104773SPascal Brand 
822b0104773SPascal Brand void TEE_AEUpdateAAD(TEE_OperationHandle op, const void *AADdata,
823b0104773SPascal Brand 		     size_t AADdataLen)
824b0104773SPascal Brand {
825b0104773SPascal Brand 	TEE_Result res;
826b0104773SPascal Brand 
827b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (AADdata == NULL && AADdataLen != 0))
828b0104773SPascal Brand 		TEE_Panic(0);
829b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
830b0104773SPascal Brand 		TEE_Panic(0);
831b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
832b0104773SPascal Brand 		TEE_Panic(0);
833b0104773SPascal Brand 
834b0104773SPascal Brand 	res = utee_authenc_update_aad(op->state, AADdata, AADdataLen);
835b0104773SPascal Brand 	if (res != TEE_SUCCESS)
836b0104773SPascal Brand 		TEE_Panic(res);
837b0104773SPascal Brand }
838b0104773SPascal Brand 
839b0104773SPascal Brand TEE_Result TEE_AEUpdate(TEE_OperationHandle op, const void *srcData,
840b0104773SPascal Brand 			size_t srcLen, void *destData, size_t *destLen)
841b0104773SPascal Brand {
842b0104773SPascal Brand 	size_t req_dlen;
843b0104773SPascal Brand 
844b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
845b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
846b0104773SPascal Brand 		TEE_Panic(0);
847b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
848b0104773SPascal Brand 		TEE_Panic(0);
849b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
850b0104773SPascal Brand 		TEE_Panic(0);
851b0104773SPascal Brand 
852b0104773SPascal Brand 	/*
853b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
854b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
855b0104773SPascal Brand 	 * can't restore sync with this API.
856b0104773SPascal Brand 	 */
8572ff3fdbbSPascal Brand 	req_dlen = ROUNDDOWN(op->buffer_offs + srcLen, op->block_size);
858b0104773SPascal Brand 	if (*destLen < req_dlen) {
859b0104773SPascal Brand 		*destLen = req_dlen;
860b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
861b0104773SPascal Brand 	}
862b0104773SPascal Brand 
863b0104773SPascal Brand 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
864b0104773SPascal Brand 			  destData, destLen);
865b0104773SPascal Brand 
866b0104773SPascal Brand 	return TEE_SUCCESS;
867b0104773SPascal Brand }
868b0104773SPascal Brand 
869b0104773SPascal Brand TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle op,
870b0104773SPascal Brand 			      const void *srcData, size_t srcLen,
871b0104773SPascal Brand 			      void *destData, size_t *destLen, void *tag,
872b0104773SPascal Brand 			      size_t *tagLen)
873b0104773SPascal Brand {
874b0104773SPascal Brand 	TEE_Result res;
875b0104773SPascal Brand 	uint8_t *dst = destData;
876b0104773SPascal Brand 	size_t acc_dlen = 0;
877b0104773SPascal Brand 	size_t tmp_dlen;
878b0104773SPascal Brand 	size_t req_dlen;
879b0104773SPascal Brand 
880b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
881b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0) ||
882b0104773SPascal Brand 	    tag == NULL || tagLen == NULL)
883b0104773SPascal Brand 		TEE_Panic(0);
884b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
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 	/*
890b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
891b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
892b0104773SPascal Brand 	 * can't restore sync with this API.
893b0104773SPascal Brand 	 */
894b0104773SPascal Brand 	req_dlen = op->buffer_offs + srcLen;
895b0104773SPascal Brand 	if (*destLen < req_dlen) {
896b0104773SPascal Brand 		*destLen = req_dlen;
897b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
898b0104773SPascal Brand 	}
899b0104773SPascal Brand 
900b0104773SPascal Brand 	/*
901b0104773SPascal Brand 	 * Need to check this before update_payload since sync would be lost if
902b0104773SPascal Brand 	 * we return short buffer after that.
903b0104773SPascal Brand 	 */
904b0104773SPascal Brand 	if (*tagLen < op->ae_tag_len) {
905b0104773SPascal Brand 		*tagLen = op->ae_tag_len;
906b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
907b0104773SPascal Brand 	}
908b0104773SPascal Brand 
909b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
910b0104773SPascal Brand 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
911b0104773SPascal Brand 			  dst, &tmp_dlen);
912b0104773SPascal Brand 	dst += tmp_dlen;
913b0104773SPascal Brand 	acc_dlen += tmp_dlen;
914b0104773SPascal Brand 
915b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
916b0104773SPascal Brand 	res =
917b0104773SPascal Brand 	    utee_authenc_enc_final(op->state, op->buffer, op->buffer_offs, dst,
918b0104773SPascal Brand 				   &tmp_dlen, tag, tagLen);
919b0104773SPascal Brand 	if (res != TEE_SUCCESS)
920b0104773SPascal Brand 		TEE_Panic(res);
921b0104773SPascal Brand 	acc_dlen += tmp_dlen;
922b0104773SPascal Brand 
923b0104773SPascal Brand 	*destLen = acc_dlen;
924b0104773SPascal Brand 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
925b0104773SPascal Brand 
926b0104773SPascal Brand 	return res;
927b0104773SPascal Brand }
928b0104773SPascal Brand 
929b0104773SPascal Brand TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle op,
930b0104773SPascal Brand 			      const void *srcData, size_t srcLen,
931b0104773SPascal Brand 			      void *destData, size_t *destLen, const void *tag,
932b0104773SPascal Brand 			      size_t tagLen)
933b0104773SPascal Brand {
934b0104773SPascal Brand 	TEE_Result res;
935b0104773SPascal Brand 	uint8_t *dst = destData;
936b0104773SPascal Brand 	size_t acc_dlen = 0;
937b0104773SPascal Brand 	size_t tmp_dlen;
938b0104773SPascal Brand 	size_t req_dlen;
939b0104773SPascal Brand 
940b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
941b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0) ||
942b0104773SPascal Brand 	    (tag == NULL && tagLen != 0))
943b0104773SPascal Brand 		TEE_Panic(0);
944b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_AE)
945b0104773SPascal Brand 		TEE_Panic(0);
946b0104773SPascal Brand 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
947b0104773SPascal Brand 		TEE_Panic(0);
948b0104773SPascal Brand 
949b0104773SPascal Brand 	/*
950b0104773SPascal Brand 	 * Check that required destLen is big enough before starting to feed
951b0104773SPascal Brand 	 * data to the algorithm. Errors during feeding of data are fatal as we
952b0104773SPascal Brand 	 * can't restore sync with this API.
953b0104773SPascal Brand 	 */
954b0104773SPascal Brand 	req_dlen = op->buffer_offs + srcLen;
955b0104773SPascal Brand 	if (*destLen < req_dlen) {
956b0104773SPascal Brand 		*destLen = req_dlen;
957b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
958b0104773SPascal Brand 	}
959b0104773SPascal Brand 
960b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
961b0104773SPascal Brand 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
962b0104773SPascal Brand 			  dst, &tmp_dlen);
963b0104773SPascal Brand 	dst += tmp_dlen;
964b0104773SPascal Brand 	acc_dlen += tmp_dlen;
965b0104773SPascal Brand 
966b0104773SPascal Brand 	tmp_dlen = *destLen - acc_dlen;
967b0104773SPascal Brand 	res =
968b0104773SPascal Brand 	    utee_authenc_dec_final(op->state, op->buffer, op->buffer_offs, dst,
969b0104773SPascal Brand 				   &tmp_dlen, tag, tagLen);
970b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_MAC_INVALID)
971b0104773SPascal Brand 		TEE_Panic(res);
972b0104773SPascal Brand 	/* Supplied tagLen should match what we initiated with */
973b0104773SPascal Brand 	if (tagLen != op->ae_tag_len)
974b0104773SPascal Brand 		res = TEE_ERROR_MAC_INVALID;
975b0104773SPascal Brand 
976b0104773SPascal Brand 	acc_dlen += tmp_dlen;
977b0104773SPascal Brand 
978b0104773SPascal Brand 	*destLen = acc_dlen;
979b0104773SPascal Brand 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
980b0104773SPascal Brand 
981b0104773SPascal Brand 	return res;
982b0104773SPascal Brand }
983b0104773SPascal Brand 
984b0104773SPascal Brand /* Cryptographic Operations API - Asymmetric Functions */
985b0104773SPascal Brand 
986b0104773SPascal Brand TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle op,
987b0104773SPascal Brand 				 const TEE_Attribute *params,
988b0104773SPascal Brand 				 uint32_t paramCount, const void *srcData,
989b0104773SPascal Brand 				 size_t srcLen, void *destData,
990b0104773SPascal Brand 				 size_t *destLen)
991b0104773SPascal Brand {
992b0104773SPascal Brand 	TEE_Result res;
993b0104773SPascal Brand 
994b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
995b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
996b0104773SPascal Brand 		TEE_Panic(0);
997b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
998b0104773SPascal Brand 		TEE_Panic(0);
999b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1000b0104773SPascal Brand 		TEE_Panic(0);
1001b0104773SPascal Brand 	if (op->info.mode != TEE_MODE_ENCRYPT)
1002b0104773SPascal Brand 		TEE_Panic(0);
1003b0104773SPascal Brand 
1004b0104773SPascal Brand 	res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen,
1005b0104773SPascal Brand 				 destData, destLen);
10068844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
10078844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
10088844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1009b0104773SPascal Brand 		TEE_Panic(res);
1010b0104773SPascal Brand 	return res;
1011b0104773SPascal Brand }
1012b0104773SPascal Brand 
1013b0104773SPascal Brand TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle op,
1014b0104773SPascal Brand 				 const TEE_Attribute *params,
1015b0104773SPascal Brand 				 uint32_t paramCount, const void *srcData,
1016b0104773SPascal Brand 				 size_t srcLen, void *destData,
1017b0104773SPascal Brand 				 size_t *destLen)
1018b0104773SPascal Brand {
1019b0104773SPascal Brand 	TEE_Result res;
1020b0104773SPascal Brand 
1021b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1022b0104773SPascal Brand 	    destLen == NULL || (destData == NULL && *destLen != 0))
1023b0104773SPascal Brand 		TEE_Panic(0);
1024b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1025b0104773SPascal Brand 		TEE_Panic(0);
1026b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1027b0104773SPascal Brand 		TEE_Panic(0);
1028b0104773SPascal Brand 	if (op->info.mode != TEE_MODE_DECRYPT)
1029b0104773SPascal Brand 		TEE_Panic(0);
1030b0104773SPascal Brand 
1031b0104773SPascal Brand 	res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen,
1032b0104773SPascal Brand 				 destData, destLen);
10338844ebfcSPascal Brand 	if (res != TEE_SUCCESS &&
10348844ebfcSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER &&
10358844ebfcSPascal Brand 	    res != TEE_ERROR_BAD_PARAMETERS)
1036b0104773SPascal Brand 		TEE_Panic(res);
1037b0104773SPascal Brand 	return res;
1038b0104773SPascal Brand }
1039b0104773SPascal Brand 
1040b0104773SPascal Brand TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle op,
1041b0104773SPascal Brand 				    const TEE_Attribute *params,
1042b0104773SPascal Brand 				    uint32_t paramCount, const void *digest,
1043b0104773SPascal Brand 				    size_t digestLen, void *signature,
1044b0104773SPascal Brand 				    size_t *signatureLen)
1045b0104773SPascal Brand {
1046b0104773SPascal Brand 	TEE_Result res;
1047b0104773SPascal Brand 
1048b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) ||
1049b0104773SPascal Brand 	    signature == NULL || signatureLen == NULL)
1050b0104773SPascal Brand 		TEE_Panic(0);
1051b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1052b0104773SPascal Brand 		TEE_Panic(0);
1053b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1054b0104773SPascal Brand 		TEE_Panic(0);
1055b0104773SPascal Brand 	if (op->info.mode != TEE_MODE_SIGN)
1056b0104773SPascal Brand 		TEE_Panic(0);
1057b0104773SPascal Brand 
1058b0104773SPascal Brand 	res =
1059b0104773SPascal Brand 	    utee_asymm_operate(op->state, params, paramCount, digest, digestLen,
1060b0104773SPascal Brand 			       signature, signatureLen);
1061b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1062b0104773SPascal Brand 		TEE_Panic(res);
1063b0104773SPascal Brand 	return res;
1064b0104773SPascal Brand }
1065b0104773SPascal Brand 
1066b0104773SPascal Brand TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle op,
1067b0104773SPascal Brand 				      const TEE_Attribute *params,
1068b0104773SPascal Brand 				      uint32_t paramCount, const void *digest,
1069b0104773SPascal Brand 				      size_t digestLen, const void *signature,
1070b0104773SPascal Brand 				      size_t signatureLen)
1071b0104773SPascal Brand {
1072b0104773SPascal Brand 	TEE_Result res;
1073b0104773SPascal Brand 
1074b0104773SPascal Brand 	if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) ||
1075b0104773SPascal Brand 	    (signature == NULL && signatureLen != 0))
1076b0104773SPascal Brand 		TEE_Panic(0);
1077b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1078b0104773SPascal Brand 		TEE_Panic(0);
1079b0104773SPascal Brand 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1080b0104773SPascal Brand 		TEE_Panic(0);
1081b0104773SPascal Brand 	if (op->info.mode != TEE_MODE_VERIFY)
1082b0104773SPascal Brand 		TEE_Panic(0);
1083b0104773SPascal Brand 
1084b0104773SPascal Brand 	res =
1085b0104773SPascal Brand 	    utee_asymm_verify(op->state, params, paramCount, digest, digestLen,
1086b0104773SPascal Brand 			      signature, signatureLen);
1087b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1088b0104773SPascal Brand 		TEE_Panic(res);
1089b0104773SPascal Brand 	return res;
1090b0104773SPascal Brand }
1091b0104773SPascal Brand 
1092b0104773SPascal Brand /* Cryptographic Operations API - Key Derivation Functions */
1093b0104773SPascal Brand 
1094b0104773SPascal Brand void TEE_DeriveKey(TEE_OperationHandle operation,
1095b0104773SPascal Brand 		   const TEE_Attribute *params, uint32_t paramCount,
1096b0104773SPascal Brand 		   TEE_ObjectHandle derivedKey)
1097b0104773SPascal Brand {
1098b0104773SPascal Brand 	TEE_Result res;
1099b0104773SPascal Brand 	TEE_ObjectInfo key_info;
1100b0104773SPascal Brand 
1101b0104773SPascal Brand 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1102b0104773SPascal Brand 		TEE_Panic(0);
1103b0104773SPascal Brand 	if (paramCount != 0 && params == NULL)
1104b0104773SPascal Brand 		TEE_Panic(0);
1105b0104773SPascal Brand 
1106b0104773SPascal Brand 	if (operation->info.algorithm != TEE_ALG_DH_DERIVE_SHARED_SECRET)
1107b0104773SPascal Brand 		TEE_Panic(0);
1108b0104773SPascal Brand 
1109b0104773SPascal Brand 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1110b0104773SPascal Brand 		TEE_Panic(0);
1111b0104773SPascal Brand 	if (operation->info.mode != TEE_MODE_DERIVE)
1112b0104773SPascal Brand 		TEE_Panic(0);
1113b0104773SPascal Brand 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1114b0104773SPascal Brand 		TEE_Panic(0);
1115b0104773SPascal Brand 
1116b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t) derivedKey, &key_info);
1117b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1118b0104773SPascal Brand 		TEE_Panic(0);
1119b0104773SPascal Brand 
1120b0104773SPascal Brand 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1121b0104773SPascal Brand 		TEE_Panic(0);
1122b0104773SPascal Brand 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1123b0104773SPascal Brand 		TEE_Panic(0);
1124b0104773SPascal Brand 
1125b0104773SPascal Brand 	if ((operation->info.algorithm == TEE_ALG_DH_DERIVE_SHARED_SECRET) &&
1126b0104773SPascal Brand 	    (paramCount != 1 ||
1127b0104773SPascal Brand 	     params[0].attributeID != TEE_ATTR_DH_PUBLIC_VALUE))
1128b0104773SPascal Brand 		TEE_Panic(0);
1129b0104773SPascal Brand 
1130b0104773SPascal Brand 	res = utee_cryp_derive_key(operation->state, params, paramCount,
1131b0104773SPascal Brand 				   (uint32_t) derivedKey);
1132b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1133b0104773SPascal Brand 		TEE_Panic(res);
1134b0104773SPascal Brand }
1135b0104773SPascal Brand 
1136b0104773SPascal Brand /* Cryptographic Operations API - Random Number Generation Functions */
1137b0104773SPascal Brand 
1138b0104773SPascal Brand void TEE_GenerateRandom(void *randomBuffer, size_t randomBufferLen)
1139b0104773SPascal Brand {
1140b0104773SPascal Brand 	TEE_Result res;
1141b0104773SPascal Brand 
1142b0104773SPascal Brand 	res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1143b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1144b0104773SPascal Brand 		TEE_Panic(res);
1145b0104773SPascal Brand }
1146