xref: /optee_os/lib/libutee/tee_api_objects.c (revision 1c96fa7f6c18d783f767482713f9c2c924e77cf8)
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>
29b0104773SPascal Brand 
30b0104773SPascal Brand #include <tee_api.h>
31b0104773SPascal Brand #include <utee_syscalls.h>
32e86f1266SJens Wiklander #include "tee_api_private.h"
33b0104773SPascal Brand 
34b0104773SPascal Brand #include <assert.h>
35b0104773SPascal Brand 
36b0104773SPascal Brand #define TEE_USAGE_DEFAULT   0xffffffff
37b0104773SPascal Brand 
38b0104773SPascal Brand #define TEE_ATTR_BIT_VALUE                  (1 << 29)
39b0104773SPascal Brand #define TEE_ATTR_BIT_PROTECTED              (1 << 28)
40b0104773SPascal Brand 
41e86f1266SJens Wiklander void __utee_from_attr(struct utee_attribute *ua, const TEE_Attribute *attrs,
42e86f1266SJens Wiklander 			uint32_t attr_count)
43e86f1266SJens Wiklander {
44e86f1266SJens Wiklander 	size_t n;
45e86f1266SJens Wiklander 
46e86f1266SJens Wiklander 	for (n = 0; n < attr_count; n++) {
47e86f1266SJens Wiklander 		ua[n].attribute_id = attrs[n].attributeID;
48e86f1266SJens Wiklander 		if (attrs[n].attributeID & TEE_ATTR_BIT_VALUE) {
49e86f1266SJens Wiklander 			ua[n].a = attrs[n].content.value.a;
50e86f1266SJens Wiklander 			ua[n].b = attrs[n].content.value.b;
51e86f1266SJens Wiklander 		} else {
52e86f1266SJens Wiklander 			ua[n].a = (uintptr_t)attrs[n].content.ref.buffer;
53e86f1266SJens Wiklander 			ua[n].b = attrs[n].content.ref.length;
54e86f1266SJens Wiklander 		}
55e86f1266SJens Wiklander 	}
56e86f1266SJens Wiklander }
57e86f1266SJens Wiklander 
58b0104773SPascal Brand /* Data and Key Storage API  - Generic Object Functions */
597583c59eSCedric Chaumont /*
607583c59eSCedric Chaumont  * Use of this function is deprecated
617583c59eSCedric Chaumont  * new code SHOULD use the TEE_GetObjectInfo1 function instead
627583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
637583c59eSCedric Chaumont  * this specification
647583c59eSCedric Chaumont  */
65b0104773SPascal Brand void TEE_GetObjectInfo(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo)
66b0104773SPascal Brand {
67b0104773SPascal Brand 	TEE_Result res;
68b0104773SPascal Brand 
69e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, objectInfo);
707583c59eSCedric Chaumont 
71b0104773SPascal Brand 	if (res != TEE_SUCCESS)
72b0104773SPascal Brand 		TEE_Panic(res);
737583c59eSCedric Chaumont 
747583c59eSCedric Chaumont 	if (objectInfo->objectType == TEE_TYPE_CORRUPTED_OBJECT) {
757583c59eSCedric Chaumont 		objectInfo->keySize = 0;
767583c59eSCedric Chaumont 		objectInfo->maxKeySize = 0;
777583c59eSCedric Chaumont 		objectInfo->objectUsage = 0;
787583c59eSCedric Chaumont 		objectInfo->dataSize = 0;
797583c59eSCedric Chaumont 		objectInfo->dataPosition = 0;
807583c59eSCedric Chaumont 		objectInfo->handleFlags = 0;
817583c59eSCedric Chaumont 	}
82b0104773SPascal Brand }
83b0104773SPascal Brand 
847583c59eSCedric Chaumont TEE_Result TEE_GetObjectInfo1(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo)
857583c59eSCedric Chaumont {
867583c59eSCedric Chaumont 	TEE_Result res;
877583c59eSCedric Chaumont 
88e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, objectInfo);
897583c59eSCedric Chaumont 
90a2e9a830SCedric Chaumont 	if (res != TEE_SUCCESS &&
91a2e9a830SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
92a2e9a830SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
937583c59eSCedric Chaumont 		TEE_Panic(res);
947583c59eSCedric Chaumont 
957583c59eSCedric Chaumont 	return res;
967583c59eSCedric Chaumont }
977583c59eSCedric Chaumont 
987583c59eSCedric Chaumont /*
997583c59eSCedric Chaumont  * Use of this function is deprecated
1007583c59eSCedric Chaumont  * new code SHOULD use the TEE_RestrictObjectUsage1 function instead
1017583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
1027583c59eSCedric Chaumont  * this specification
1037583c59eSCedric Chaumont  */
104b0104773SPascal Brand void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage)
105b0104773SPascal Brand {
106b0104773SPascal Brand 	TEE_Result res;
1077583c59eSCedric Chaumont 	TEE_ObjectInfo objectInfo;
1087583c59eSCedric Chaumont 
109e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &objectInfo);
1107583c59eSCedric Chaumont 	if (objectInfo.objectType == TEE_TYPE_CORRUPTED_OBJECT)
1117583c59eSCedric Chaumont 		return;
1127583c59eSCedric Chaumont 
1137583c59eSCedric Chaumont 	res = TEE_RestrictObjectUsage1(object, objectUsage);
114b0104773SPascal Brand 
115b0104773SPascal Brand 	if (res != TEE_SUCCESS)
116b0104773SPascal Brand 		TEE_Panic(0);
117b0104773SPascal Brand }
118b0104773SPascal Brand 
1197583c59eSCedric Chaumont TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage)
1207583c59eSCedric Chaumont {
1217583c59eSCedric Chaumont 	TEE_Result res;
1227583c59eSCedric Chaumont 
123e86f1266SJens Wiklander 	res = utee_cryp_obj_restrict_usage((unsigned long)object, objectUsage);
1247583c59eSCedric Chaumont 
125a2e9a830SCedric Chaumont 	if (res != TEE_SUCCESS &&
126a2e9a830SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
127a2e9a830SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
128a2e9a830SCedric Chaumont 		TEE_Panic(res);
1297583c59eSCedric Chaumont 
1307583c59eSCedric Chaumont 	return res;
1317583c59eSCedric Chaumont }
1327583c59eSCedric Chaumont 
133b0104773SPascal Brand TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object,
134b0104773SPascal Brand 					uint32_t attributeID, void *buffer,
13579a3c601SCedric Chaumont 					uint32_t *size)
136b0104773SPascal Brand {
137b0104773SPascal Brand 	TEE_Result res;
138b0104773SPascal Brand 	TEE_ObjectInfo info;
139e86f1266SJens Wiklander 	uint64_t sz;
140b0104773SPascal Brand 
141e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
142b0104773SPascal Brand 	if (res != TEE_SUCCESS)
143a2e9a830SCedric Chaumont 		goto exit;
144b0104773SPascal Brand 
145b0104773SPascal Brand 	/* This function only supports reference attributes */
146a2e9a830SCedric Chaumont 	if ((attributeID & TEE_ATTR_BIT_VALUE)) {
147a2e9a830SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
148a2e9a830SCedric Chaumont 		goto exit;
149a2e9a830SCedric Chaumont 	}
150b0104773SPascal Brand 
151e86f1266SJens Wiklander 	sz = *size;
152e86f1266SJens Wiklander 	res = utee_cryp_obj_get_attr((unsigned long)object, attributeID,
153e86f1266SJens Wiklander 				     buffer, &sz);
154e86f1266SJens Wiklander 	*size = sz;
155b0104773SPascal Brand 
156a2e9a830SCedric Chaumont exit:
1570ed6a6caSCedric Chaumont 	if (res != TEE_SUCCESS &&
1580ed6a6caSCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
1590ed6a6caSCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER &&
1600ed6a6caSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
1610ed6a6caSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
162b0104773SPascal Brand 		TEE_Panic(0);
163b0104773SPascal Brand 
164b0104773SPascal Brand 	return res;
165b0104773SPascal Brand }
166b0104773SPascal Brand 
167b0104773SPascal Brand TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object,
168b0104773SPascal Brand 				       uint32_t attributeID, uint32_t *a,
169b0104773SPascal Brand 				       uint32_t *b)
170b0104773SPascal Brand {
171b0104773SPascal Brand 	TEE_Result res;
172b0104773SPascal Brand 	TEE_ObjectInfo info;
173b0104773SPascal Brand 	uint32_t buf[2];
174e86f1266SJens Wiklander 	uint64_t size = sizeof(buf);
175b0104773SPascal Brand 
176e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
177b0104773SPascal Brand 	if (res != TEE_SUCCESS)
178a2e9a830SCedric Chaumont 		goto exit;
179b0104773SPascal Brand 
180b0104773SPascal Brand 	/* This function only supports value attributes */
181a2e9a830SCedric Chaumont 	if (!(attributeID & TEE_ATTR_BIT_VALUE)) {
182a2e9a830SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
183a2e9a830SCedric Chaumont 		goto exit;
184a2e9a830SCedric Chaumont 	}
185b0104773SPascal Brand 
186e86f1266SJens Wiklander 	res = utee_cryp_obj_get_attr((unsigned long)object, attributeID, buf,
187e86f1266SJens Wiklander 				     &size);
188b0104773SPascal Brand 
189a2e9a830SCedric Chaumont exit:
1900ed6a6caSCedric Chaumont 	if (res != TEE_SUCCESS &&
1910ed6a6caSCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
1920ed6a6caSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
1930ed6a6caSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
194b0104773SPascal Brand 		TEE_Panic(0);
195b0104773SPascal Brand 
196b0104773SPascal Brand 	if (size != sizeof(buf))
197b0104773SPascal Brand 		TEE_Panic(0);
198b0104773SPascal Brand 
199b0104773SPascal Brand 	*a = buf[0];
200b0104773SPascal Brand 	*b = buf[1];
201b0104773SPascal Brand 
202b0104773SPascal Brand 	return res;
203b0104773SPascal Brand }
204b0104773SPascal Brand 
205b0104773SPascal Brand void TEE_CloseObject(TEE_ObjectHandle object)
206b0104773SPascal Brand {
207b0104773SPascal Brand 	TEE_Result res;
208b0104773SPascal Brand 
209b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
210b0104773SPascal Brand 		return;
211b0104773SPascal Brand 
212e86f1266SJens Wiklander 	res = utee_cryp_obj_close((unsigned long)object);
213b0104773SPascal Brand 	if (res != TEE_SUCCESS)
214b0104773SPascal Brand 		TEE_Panic(0);
215b0104773SPascal Brand }
216b0104773SPascal Brand 
217b0104773SPascal Brand /* Data and Key Storage API  - Transient Object Functions */
218b0104773SPascal Brand 
219b0104773SPascal Brand TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType,
22079a3c601SCedric Chaumont 				       uint32_t maxKeySize,
221b0104773SPascal Brand 				       TEE_ObjectHandle *object)
222b0104773SPascal Brand {
223b0104773SPascal Brand 	TEE_Result res;
224b0104773SPascal Brand 	uint32_t obj;
225b0104773SPascal Brand 
22679a3c601SCedric Chaumont 	res = utee_cryp_obj_alloc(objectType, maxKeySize, &obj);
227aeb0d927SCedric Chaumont 
228aeb0d927SCedric Chaumont 	if (res != TEE_SUCCESS &&
229aeb0d927SCedric Chaumont 	    res != TEE_ERROR_OUT_OF_MEMORY &&
230aeb0d927SCedric Chaumont 	    res != TEE_ERROR_NOT_SUPPORTED)
231aeb0d927SCedric Chaumont 		TEE_Panic(0);
232aeb0d927SCedric Chaumont 
233b0104773SPascal Brand 	if (res == TEE_SUCCESS)
234e86f1266SJens Wiklander 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
2350ed6a6caSCedric Chaumont 
236b0104773SPascal Brand 	return res;
237b0104773SPascal Brand }
238b0104773SPascal Brand 
239b0104773SPascal Brand void TEE_FreeTransientObject(TEE_ObjectHandle object)
240b0104773SPascal Brand {
241b0104773SPascal Brand 	TEE_Result res;
242b0104773SPascal Brand 	TEE_ObjectInfo info;
243b0104773SPascal Brand 
244b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
245b0104773SPascal Brand 		return;
246b0104773SPascal Brand 
247e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
248b0104773SPascal Brand 	if (res != TEE_SUCCESS)
249b0104773SPascal Brand 		TEE_Panic(0);
250b0104773SPascal Brand 
251b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
252b0104773SPascal Brand 		TEE_Panic(0);
253b0104773SPascal Brand 
254e86f1266SJens Wiklander 	res = utee_cryp_obj_close((unsigned long)object);
255b0104773SPascal Brand 	if (res != TEE_SUCCESS)
256b0104773SPascal Brand 		TEE_Panic(0);
257b0104773SPascal Brand }
258b0104773SPascal Brand 
259b0104773SPascal Brand void TEE_ResetTransientObject(TEE_ObjectHandle object)
260b0104773SPascal Brand {
261b0104773SPascal Brand 	TEE_Result res;
262b0104773SPascal Brand 	TEE_ObjectInfo info;
263b0104773SPascal Brand 
264b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
265b0104773SPascal Brand 		return;
266b0104773SPascal Brand 
267e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
268b0104773SPascal Brand 	if (res != TEE_SUCCESS)
269b0104773SPascal Brand 		TEE_Panic(0);
270b0104773SPascal Brand 
271b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
272b0104773SPascal Brand 		TEE_Panic(0);
273b0104773SPascal Brand 
274e86f1266SJens Wiklander 	res = utee_cryp_obj_reset((unsigned long)object);
275b0104773SPascal Brand 	if (res != TEE_SUCCESS)
276b0104773SPascal Brand 		TEE_Panic(0);
277b0104773SPascal Brand }
278b0104773SPascal Brand 
279b0104773SPascal Brand TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object,
280b0104773SPascal Brand 				       TEE_Attribute *attrs,
281b0104773SPascal Brand 				       uint32_t attrCount)
282b0104773SPascal Brand {
283b0104773SPascal Brand 	TEE_Result res;
284b0104773SPascal Brand 	TEE_ObjectInfo info;
285e86f1266SJens Wiklander 	struct utee_attribute ua[attrCount];
286b0104773SPascal Brand 
287e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
288b0104773SPascal Brand 	if (res != TEE_SUCCESS)
289b0104773SPascal Brand 		TEE_Panic(0);
290b0104773SPascal Brand 
291b0104773SPascal Brand 	/* Must be a transient object */
292b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
293b0104773SPascal Brand 		TEE_Panic(0);
294b0104773SPascal Brand 
295b0104773SPascal Brand 	/* Must not be initialized already */
296b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
297b0104773SPascal Brand 		TEE_Panic(0);
298b0104773SPascal Brand 
299e86f1266SJens Wiklander 	__utee_from_attr(ua, attrs, attrCount);
300e86f1266SJens Wiklander 	res = utee_cryp_obj_populate((unsigned long)object, ua, attrCount);
301b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
302b0104773SPascal Brand 		TEE_Panic(res);
303b0104773SPascal Brand 	return res;
304b0104773SPascal Brand }
305b0104773SPascal Brand 
306b0104773SPascal Brand void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID,
30779a3c601SCedric Chaumont 			  void *buffer, uint32_t length)
308b0104773SPascal Brand {
309b0104773SPascal Brand 	if (attr == NULL)
310b0104773SPascal Brand 		TEE_Panic(0);
311b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) != 0)
312b0104773SPascal Brand 		TEE_Panic(0);
313b0104773SPascal Brand 	attr->attributeID = attributeID;
314b0104773SPascal Brand 	attr->content.ref.buffer = buffer;
315b0104773SPascal Brand 	attr->content.ref.length = length;
316b0104773SPascal Brand }
317b0104773SPascal Brand 
318b0104773SPascal Brand void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID,
319b0104773SPascal Brand 			    uint32_t a, uint32_t b)
320b0104773SPascal Brand {
321b0104773SPascal Brand 	if (attr == NULL)
322b0104773SPascal Brand 		TEE_Panic(0);
323b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) == 0)
324b0104773SPascal Brand 		TEE_Panic(0);
325b0104773SPascal Brand 	attr->attributeID = attributeID;
326b0104773SPascal Brand 	attr->content.value.a = a;
327b0104773SPascal Brand 	attr->content.value.b = b;
328b0104773SPascal Brand }
329b0104773SPascal Brand 
3307583c59eSCedric Chaumont /*
3317583c59eSCedric Chaumont  * Use of this function is deprecated
3327583c59eSCedric Chaumont  * new code SHOULD use the TEE_CopyObjectAttributes1 function instead
3337583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
3347583c59eSCedric Chaumont  * this specification
3357583c59eSCedric Chaumont  */
336b0104773SPascal Brand void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject,
337b0104773SPascal Brand 			      TEE_ObjectHandle srcObject)
338b0104773SPascal Brand {
339b0104773SPascal Brand 	TEE_Result res;
3407583c59eSCedric Chaumont 	TEE_ObjectInfo src_info;
3417583c59eSCedric Chaumont 
342e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)srcObject, &src_info);
3437583c59eSCedric Chaumont 	if (src_info.objectType == TEE_TYPE_CORRUPTED_OBJECT)
3447583c59eSCedric Chaumont 		return;
3457583c59eSCedric Chaumont 
3467583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(destObject, srcObject);
3477583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
3487583c59eSCedric Chaumont 		TEE_Panic(0);
3497583c59eSCedric Chaumont }
3507583c59eSCedric Chaumont 
3517583c59eSCedric Chaumont TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject,
3527583c59eSCedric Chaumont 			      TEE_ObjectHandle srcObject)
3537583c59eSCedric Chaumont {
3547583c59eSCedric Chaumont 	TEE_Result res;
355b0104773SPascal Brand 	TEE_ObjectInfo dst_info;
356b0104773SPascal Brand 	TEE_ObjectInfo src_info;
357b0104773SPascal Brand 
358e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)destObject, &dst_info);
359b0104773SPascal Brand 	if (res != TEE_SUCCESS)
360a2e9a830SCedric Chaumont 		goto exit;
361b0104773SPascal Brand 
362e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)srcObject, &src_info);
363b0104773SPascal Brand 	if (res != TEE_SUCCESS)
364a2e9a830SCedric Chaumont 		goto exit;
365b0104773SPascal Brand 
366a2e9a830SCedric Chaumont 	if (!(src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED))
367b0104773SPascal Brand 		TEE_Panic(0);
368a2e9a830SCedric Chaumont 
369a2e9a830SCedric Chaumont 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT))
370b0104773SPascal Brand 		TEE_Panic(0);
371a2e9a830SCedric Chaumont 
372a2e9a830SCedric Chaumont 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED))
373b0104773SPascal Brand 		TEE_Panic(0);
374b0104773SPascal Brand 
375e86f1266SJens Wiklander 	res = utee_cryp_obj_copy((unsigned long)destObject,
376e86f1266SJens Wiklander 				 (unsigned long)srcObject);
3777583c59eSCedric Chaumont 
378a2e9a830SCedric Chaumont exit:
379a2e9a830SCedric Chaumont 	if (res != TEE_SUCCESS &&
380a2e9a830SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
381a2e9a830SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
382a2e9a830SCedric Chaumont 		TEE_Panic(res);
3837583c59eSCedric Chaumont 
3847583c59eSCedric Chaumont 	return res;
385b0104773SPascal Brand }
386b0104773SPascal Brand 
387b0104773SPascal Brand TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
388b0104773SPascal Brand 			   TEE_Attribute *params, uint32_t paramCount)
389b0104773SPascal Brand {
390b0104773SPascal Brand 	TEE_Result res;
391e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
392b0104773SPascal Brand 
393e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
394e86f1266SJens Wiklander 	res = utee_cryp_obj_generate_key((unsigned long)object, keySize,
395e86f1266SJens Wiklander 					 ua, paramCount);
396b0104773SPascal Brand 
397aeb0d927SCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
398b0104773SPascal Brand 		TEE_Panic(0);
399b0104773SPascal Brand 
400b0104773SPascal Brand 	return res;
401b0104773SPascal Brand }
402b0104773SPascal Brand 
403b0104773SPascal Brand /* Data and Key Storage API  - Persistent Object Functions */
404b0104773SPascal Brand 
405b0104773SPascal Brand TEE_Result TEE_OpenPersistentObject(uint32_t storageID, void *objectID,
40679a3c601SCedric Chaumont 				    uint32_t objectIDLen, uint32_t flags,
407b0104773SPascal Brand 				    TEE_ObjectHandle *object)
408b0104773SPascal Brand {
4099b520646SCedric Chaumont 	TEE_Result res;
410e86f1266SJens Wiklander 	uint32_t obj;
411b0104773SPascal Brand 
4129b520646SCedric Chaumont 	if (storageID != TEE_STORAGE_PRIVATE) {
4139b520646SCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
4149b520646SCedric Chaumont 		goto out;
4159b520646SCedric Chaumont 	}
416b0104773SPascal Brand 
4179b520646SCedric Chaumont 	if (!objectID) {
4189b520646SCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
4199b520646SCedric Chaumont 		goto out;
4209b520646SCedric Chaumont 	}
4219b520646SCedric Chaumont 
4229b520646SCedric Chaumont 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) {
4239b520646SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
4249b520646SCedric Chaumont 		goto out;
4259b520646SCedric Chaumont 	}
4269b520646SCedric Chaumont 
4279b520646SCedric Chaumont 	if (!object) {
4289b520646SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
4299b520646SCedric Chaumont 		goto out;
4309b520646SCedric Chaumont 	}
4319b520646SCedric Chaumont 
4329b520646SCedric Chaumont 	res = utee_storage_obj_open(storageID, objectID, objectIDLen, flags,
433e86f1266SJens Wiklander 				     &obj);
434e86f1266SJens Wiklander 	if (res == TEE_SUCCESS)
435e86f1266SJens Wiklander 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
4369b520646SCedric Chaumont 
4379b520646SCedric Chaumont out:
4389b520646SCedric Chaumont 	if (res != TEE_SUCCESS &&
4399b520646SCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
4409b520646SCedric Chaumont 	    res != TEE_ERROR_ACCESS_CONFLICT &&
4419b520646SCedric Chaumont 	    res != TEE_ERROR_OUT_OF_MEMORY &&
4429b520646SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
4439b520646SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
444b0104773SPascal Brand 		TEE_Panic(0);
445b0104773SPascal Brand 
4469b520646SCedric Chaumont 	return res;
447b0104773SPascal Brand }
448b0104773SPascal Brand 
449b0104773SPascal Brand TEE_Result TEE_CreatePersistentObject(uint32_t storageID, void *objectID,
45079a3c601SCedric Chaumont 				      uint32_t objectIDLen, uint32_t flags,
451b0104773SPascal Brand 				      TEE_ObjectHandle attributes,
452b0104773SPascal Brand 				      const void *initialData,
45379a3c601SCedric Chaumont 				      uint32_t initialDataLen,
454b0104773SPascal Brand 				      TEE_ObjectHandle *object)
455b0104773SPascal Brand {
45684431ae3SCedric Chaumont 	TEE_Result res;
457e86f1266SJens Wiklander 	uint32_t obj;
458b0104773SPascal Brand 
45984431ae3SCedric Chaumont 	if (storageID != TEE_STORAGE_PRIVATE) {
46084431ae3SCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
46184431ae3SCedric Chaumont 		goto err;
46284431ae3SCedric Chaumont 	}
463b0104773SPascal Brand 
464aeb0d927SCedric Chaumont 	if (!objectID) {
46584431ae3SCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
46684431ae3SCedric Chaumont 		goto err;
46784431ae3SCedric Chaumont 	}
468b0104773SPascal Brand 
46984431ae3SCedric Chaumont 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) {
47084431ae3SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
47184431ae3SCedric Chaumont 		goto err;
47284431ae3SCedric Chaumont 	}
473b0104773SPascal Brand 
47484431ae3SCedric Chaumont 	res = utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
475e86f1266SJens Wiklander 				      (unsigned long)attributes, initialData,
476e86f1266SJens Wiklander 				      initialDataLen, &obj);
477e86f1266SJens Wiklander 	if (res == TEE_SUCCESS) {
478*1c96fa7fSPascal Brand 		if (object)
479e86f1266SJens Wiklander 			*object = (TEE_ObjectHandle)(uintptr_t)obj;
480*1c96fa7fSPascal Brand 		else
481*1c96fa7fSPascal Brand 			res = utee_cryp_obj_close(obj);
482*1c96fa7fSPascal Brand 		if (res == TEE_SUCCESS)
48384431ae3SCedric Chaumont 			goto out;
484e86f1266SJens Wiklander 	}
48584431ae3SCedric Chaumont err:
486*1c96fa7fSPascal Brand 	if (object)
487*1c96fa7fSPascal Brand 		*object = TEE_HANDLE_NULL;
48884431ae3SCedric Chaumont 	if (res == TEE_ERROR_ITEM_NOT_FOUND ||
48984431ae3SCedric Chaumont 	    res == TEE_ERROR_ACCESS_CONFLICT ||
49084431ae3SCedric Chaumont 	    res == TEE_ERROR_OUT_OF_MEMORY ||
49184431ae3SCedric Chaumont 	    res == TEE_ERROR_STORAGE_NO_SPACE ||
49284431ae3SCedric Chaumont 	    res == TEE_ERROR_CORRUPT_OBJECT ||
49384431ae3SCedric Chaumont 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE)
49484431ae3SCedric Chaumont 		return res;
49584431ae3SCedric Chaumont 	TEE_Panic(0);
49684431ae3SCedric Chaumont out:
49784431ae3SCedric Chaumont 	return TEE_SUCCESS;
498b0104773SPascal Brand }
499b0104773SPascal Brand 
5007583c59eSCedric Chaumont /*
5017583c59eSCedric Chaumont  * Use of this function is deprecated
5027583c59eSCedric Chaumont  * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead
5037583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
5047583c59eSCedric Chaumont  * this specification
5057583c59eSCedric Chaumont  */
506b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
507b0104773SPascal Brand {
508b0104773SPascal Brand 	TEE_Result res;
509b0104773SPascal Brand 
510b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
511b0104773SPascal Brand 		return;
512b0104773SPascal Brand 
5137583c59eSCedric Chaumont 	res = TEE_CloseAndDeletePersistentObject1(object);
514b0104773SPascal Brand 
515b0104773SPascal Brand 	if (res != TEE_SUCCESS)
516b0104773SPascal Brand 		TEE_Panic(0);
517b0104773SPascal Brand }
518b0104773SPascal Brand 
5197583c59eSCedric Chaumont TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object)
5207583c59eSCedric Chaumont {
5217583c59eSCedric Chaumont 	TEE_Result res;
5227583c59eSCedric Chaumont 
5237583c59eSCedric Chaumont 	if (object == TEE_HANDLE_NULL)
5247583c59eSCedric Chaumont 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
5257583c59eSCedric Chaumont 
526e86f1266SJens Wiklander 	res = utee_storage_obj_del((unsigned long)object);
5277583c59eSCedric Chaumont 
5287583c59eSCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
5297583c59eSCedric Chaumont 		TEE_Panic(0);
5307583c59eSCedric Chaumont 
5317583c59eSCedric Chaumont 	return res;
5327583c59eSCedric Chaumont }
5337583c59eSCedric Chaumont 
5347583c59eSCedric Chaumont 
535b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
536b0104773SPascal Brand 				      const void *newObjectID,
53779a3c601SCedric Chaumont 				      uint32_t newObjectIDLen)
538b0104773SPascal Brand {
539b0104773SPascal Brand 	TEE_Result res;
540b0104773SPascal Brand 
541a76bf53fSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
542a76bf53fSCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
543a76bf53fSCedric Chaumont 		goto out;
544a76bf53fSCedric Chaumont 	}
545b0104773SPascal Brand 
546a76bf53fSCedric Chaumont 	if (!newObjectID) {
547a76bf53fSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
548a76bf53fSCedric Chaumont 		goto out;
549a76bf53fSCedric Chaumont 	}
550b0104773SPascal Brand 
551a76bf53fSCedric Chaumont 	if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN) {
552a76bf53fSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
553a76bf53fSCedric Chaumont 		goto out;
554a76bf53fSCedric Chaumont 	}
555b0104773SPascal Brand 
556e86f1266SJens Wiklander 	res = utee_storage_obj_rename((unsigned long)object, newObjectID,
557e86f1266SJens Wiklander 				      newObjectIDLen);
558b0104773SPascal Brand 
559a76bf53fSCedric Chaumont out:
560a76bf53fSCedric Chaumont 	if (res != TEE_SUCCESS &&
561a76bf53fSCedric Chaumont 	    res != TEE_ERROR_ACCESS_CONFLICT &&
562a76bf53fSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
563a76bf53fSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
564b0104773SPascal Brand 		TEE_Panic(0);
565b0104773SPascal Brand 
566b0104773SPascal Brand 	return res;
567b0104773SPascal Brand }
568b0104773SPascal Brand 
569b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
570b0104773SPascal Brand 						  objectEnumerator)
571b0104773SPascal Brand {
572b0104773SPascal Brand 	TEE_Result res;
573e86f1266SJens Wiklander 	uint32_t oe;
574b0104773SPascal Brand 
57515cd3c30SCedric Chaumont 	if (!objectEnumerator)
576b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
577b0104773SPascal Brand 
578e86f1266SJens Wiklander 	res = utee_storage_alloc_enum(&oe);
579b0104773SPascal Brand 
580b0104773SPascal Brand 	if (res != TEE_SUCCESS)
581e86f1266SJens Wiklander 		oe = TEE_HANDLE_NULL;
582e86f1266SJens Wiklander 
583e86f1266SJens Wiklander 	*objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe;
584b0104773SPascal Brand 
58515cd3c30SCedric Chaumont 	if (res != TEE_SUCCESS &&
58615cd3c30SCedric Chaumont 	    res != TEE_ERROR_ACCESS_CONFLICT)
58715cd3c30SCedric Chaumont 		TEE_Panic(0);
58815cd3c30SCedric Chaumont 
589b0104773SPascal Brand 	return res;
590b0104773SPascal Brand }
591b0104773SPascal Brand 
592b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
593b0104773SPascal Brand {
594b0104773SPascal Brand 	TEE_Result res;
595b0104773SPascal Brand 
596b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
597b0104773SPascal Brand 		return;
598b0104773SPascal Brand 
599e86f1266SJens Wiklander 	res = utee_storage_free_enum((unsigned long)objectEnumerator);
600b0104773SPascal Brand 
601b0104773SPascal Brand 	if (res != TEE_SUCCESS)
602b0104773SPascal Brand 		TEE_Panic(0);
603b0104773SPascal Brand }
604b0104773SPascal Brand 
605b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
606b0104773SPascal Brand {
607b0104773SPascal Brand 	TEE_Result res;
608b0104773SPascal Brand 
609b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
610b0104773SPascal Brand 		return;
611b0104773SPascal Brand 
612e86f1266SJens Wiklander 	res = utee_storage_reset_enum((unsigned long)objectEnumerator);
613b0104773SPascal Brand 
614b0104773SPascal Brand 	if (res != TEE_SUCCESS)
615b0104773SPascal Brand 		TEE_Panic(0);
616b0104773SPascal Brand }
617b0104773SPascal Brand 
618b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
619b0104773SPascal Brand 					       objectEnumerator,
620b0104773SPascal Brand 					       uint32_t storageID)
621b0104773SPascal Brand {
622b0104773SPascal Brand 	TEE_Result res;
623b0104773SPascal Brand 
624b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
625b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
626b0104773SPascal Brand 
627e86f1266SJens Wiklander 	res = utee_storage_start_enum((unsigned long)objectEnumerator,
628e86f1266SJens Wiklander 				      storageID);
629b0104773SPascal Brand 
63015cd3c30SCedric Chaumont 	if (res != TEE_SUCCESS &&
63115cd3c30SCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
63215cd3c30SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
63315cd3c30SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
634b0104773SPascal Brand 		TEE_Panic(0);
635b0104773SPascal Brand 
636b0104773SPascal Brand 	return res;
637b0104773SPascal Brand }
638b0104773SPascal Brand 
639b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
640b0104773SPascal Brand 				       TEE_ObjectInfo *objectInfo,
64179a3c601SCedric Chaumont 				       void *objectID, uint32_t *objectIDLen)
642b0104773SPascal Brand {
643b0104773SPascal Brand 	TEE_Result res;
644e86f1266SJens Wiklander 	uint64_t len;
645b0104773SPascal Brand 
64615cd3c30SCedric Chaumont 	if (!objectID) {
64715cd3c30SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
64815cd3c30SCedric Chaumont 		goto out;
64915cd3c30SCedric Chaumont 	}
65015cd3c30SCedric Chaumont 
65115cd3c30SCedric Chaumont 	if (!objectIDLen) {
65215cd3c30SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
65315cd3c30SCedric Chaumont 		goto out;
65415cd3c30SCedric Chaumont 	}
65515cd3c30SCedric Chaumont 
656e86f1266SJens Wiklander 	len = *objectIDLen;
657e86f1266SJens Wiklander 	res = utee_storage_next_enum((unsigned long)objectEnumerator,
658e86f1266SJens Wiklander 				     objectInfo, objectID, &len);
659e86f1266SJens Wiklander 	*objectIDLen = len;
660b0104773SPascal Brand 
66115cd3c30SCedric Chaumont out:
66215cd3c30SCedric Chaumont 	if (res != TEE_SUCCESS &&
66315cd3c30SCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
66415cd3c30SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
66515cd3c30SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
666b0104773SPascal Brand 		TEE_Panic(0);
667b0104773SPascal Brand 
668b0104773SPascal Brand 	return res;
669b0104773SPascal Brand }
670b0104773SPascal Brand 
671b0104773SPascal Brand /* Data and Key Storage API  - Data Stream Access Functions */
672b0104773SPascal Brand 
673b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
67479a3c601SCedric Chaumont 			      uint32_t size, uint32_t *count)
675b0104773SPascal Brand {
676b0104773SPascal Brand 	TEE_Result res;
677e86f1266SJens Wiklander 	uint64_t cnt64;
678b0104773SPascal Brand 
679ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
680ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
681ae1289baSCedric Chaumont 		goto out;
682ae1289baSCedric Chaumont 	}
683b0104773SPascal Brand 
684e86f1266SJens Wiklander 	cnt64 = *count;
685e86f1266SJens Wiklander 	res = utee_storage_obj_read((unsigned long)object, buffer, size,
686e86f1266SJens Wiklander 				    &cnt64);
687e86f1266SJens Wiklander 	*count = cnt64;
688b0104773SPascal Brand 
689ae1289baSCedric Chaumont out:
690ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
691ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
692ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
693b0104773SPascal Brand 		TEE_Panic(0);
694b0104773SPascal Brand 
695b0104773SPascal Brand 	return res;
696b0104773SPascal Brand }
697b0104773SPascal Brand 
698b0104773SPascal Brand TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, void *buffer,
69979a3c601SCedric Chaumont 			       uint32_t size)
700b0104773SPascal Brand {
701b0104773SPascal Brand 	TEE_Result res;
702b0104773SPascal Brand 
703ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
704ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
705ae1289baSCedric Chaumont 		goto out;
706ae1289baSCedric Chaumont 	}
707ae1289baSCedric Chaumont 
708ae1289baSCedric Chaumont 	if (size > TEE_DATA_MAX_POSITION) {
709ae1289baSCedric Chaumont 		res = TEE_ERROR_OVERFLOW;
710ae1289baSCedric Chaumont 		goto out;
711ae1289baSCedric Chaumont 	}
712b0104773SPascal Brand 
713e86f1266SJens Wiklander 	res = utee_storage_obj_write((unsigned long)object, buffer, size);
714b0104773SPascal Brand 
715ae1289baSCedric Chaumont out:
716ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
717ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
718ae1289baSCedric Chaumont 	    res != TEE_ERROR_OVERFLOW &&
719ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
720ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
721b0104773SPascal Brand 		TEE_Panic(0);
722b0104773SPascal Brand 
723b0104773SPascal Brand 	return res;
724b0104773SPascal Brand }
725b0104773SPascal Brand 
726b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
727b0104773SPascal Brand {
728b0104773SPascal Brand 	TEE_Result res;
729b0104773SPascal Brand 
730ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
731ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
732ae1289baSCedric Chaumont 		goto out;
733ae1289baSCedric Chaumont 	}
734b0104773SPascal Brand 
735e86f1266SJens Wiklander 	res = utee_storage_obj_trunc((unsigned long)object, size);
736b0104773SPascal Brand 
737ae1289baSCedric Chaumont out:
738ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
739ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
740ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
741ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
742b0104773SPascal Brand 		TEE_Panic(0);
743b0104773SPascal Brand 
744b0104773SPascal Brand 	return res;
745b0104773SPascal Brand }
746b0104773SPascal Brand 
747b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
748b0104773SPascal Brand 			      TEE_Whence whence)
749b0104773SPascal Brand {
750b0104773SPascal Brand 	TEE_Result res;
751b0104773SPascal Brand 	TEE_ObjectInfo info;
752b0104773SPascal Brand 
753ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
754ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
755ae1289baSCedric Chaumont 		goto out;
756ae1289baSCedric Chaumont 	}
757b0104773SPascal Brand 
758e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
759b0104773SPascal Brand 	if (res != TEE_SUCCESS)
760ae1289baSCedric Chaumont 		goto out;
761b0104773SPascal Brand 
762b0104773SPascal Brand 	switch (whence) {
763b0104773SPascal Brand 	case TEE_DATA_SEEK_SET:
764ae1289baSCedric Chaumont 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) {
765ae1289baSCedric Chaumont 			res = TEE_ERROR_OVERFLOW;
766ae1289baSCedric Chaumont 			goto out;
767ae1289baSCedric Chaumont 		}
768b0104773SPascal Brand 		break;
769b0104773SPascal Brand 	case TEE_DATA_SEEK_CUR:
770b0104773SPascal Brand 		if (offset > 0 &&
771b0104773SPascal Brand 		    ((uint32_t)offset + info.dataPosition >
772b0104773SPascal Brand 		     TEE_DATA_MAX_POSITION ||
773b0104773SPascal Brand 		     (uint32_t)offset + info.dataPosition <
774ae1289baSCedric Chaumont 		     info.dataPosition)) {
775ae1289baSCedric Chaumont 			res = TEE_ERROR_OVERFLOW;
776ae1289baSCedric Chaumont 			goto out;
777ae1289baSCedric Chaumont 		}
778b0104773SPascal Brand 		break;
779b0104773SPascal Brand 	case TEE_DATA_SEEK_END:
780b0104773SPascal Brand 		if (offset > 0 &&
781b0104773SPascal Brand 		    ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION ||
782ae1289baSCedric Chaumont 		     (uint32_t)offset + info.dataSize < info.dataSize)) {
783ae1289baSCedric Chaumont 			res = TEE_ERROR_OVERFLOW;
784ae1289baSCedric Chaumont 			goto out;
785ae1289baSCedric Chaumont 		}
786b0104773SPascal Brand 		break;
787b0104773SPascal Brand 	default:
788ae1289baSCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
789ae1289baSCedric Chaumont 		goto out;
790b0104773SPascal Brand 	}
791b0104773SPascal Brand 
792e86f1266SJens Wiklander 	res = utee_storage_obj_seek((unsigned long)object, offset, whence);
793b0104773SPascal Brand 
794ae1289baSCedric Chaumont out:
795ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
796ae1289baSCedric Chaumont 	    res != TEE_ERROR_OVERFLOW &&
797ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
798ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
799b0104773SPascal Brand 		TEE_Panic(0);
800b0104773SPascal Brand 
801b0104773SPascal Brand 	return res;
802b0104773SPascal Brand }
803