xref: /optee_os/lib/libutee/tee_api_objects.c (revision be96c837733613b0f5299b587e5f32e0c5cbf342)
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 #define TEE_USAGE_DEFAULT   0xffffffff
35b0104773SPascal Brand 
36b0104773SPascal Brand #define TEE_ATTR_BIT_VALUE                  (1 << 29)
37b0104773SPascal Brand #define TEE_ATTR_BIT_PROTECTED              (1 << 28)
38b0104773SPascal Brand 
39e86f1266SJens Wiklander void __utee_from_attr(struct utee_attribute *ua, const TEE_Attribute *attrs,
40e86f1266SJens Wiklander 			uint32_t attr_count)
41e86f1266SJens Wiklander {
42e86f1266SJens Wiklander 	size_t n;
43e86f1266SJens Wiklander 
44e86f1266SJens Wiklander 	for (n = 0; n < attr_count; n++) {
45e86f1266SJens Wiklander 		ua[n].attribute_id = attrs[n].attributeID;
46e86f1266SJens Wiklander 		if (attrs[n].attributeID & TEE_ATTR_BIT_VALUE) {
47e86f1266SJens Wiklander 			ua[n].a = attrs[n].content.value.a;
48e86f1266SJens Wiklander 			ua[n].b = attrs[n].content.value.b;
49e86f1266SJens Wiklander 		} else {
50e86f1266SJens Wiklander 			ua[n].a = (uintptr_t)attrs[n].content.ref.buffer;
51e86f1266SJens Wiklander 			ua[n].b = attrs[n].content.ref.length;
52e86f1266SJens Wiklander 		}
53e86f1266SJens Wiklander 	}
54e86f1266SJens Wiklander }
55e86f1266SJens Wiklander 
56b0104773SPascal Brand /* Data and Key Storage API  - Generic Object Functions */
577583c59eSCedric Chaumont /*
587583c59eSCedric Chaumont  * Use of this function is deprecated
597583c59eSCedric Chaumont  * new code SHOULD use the TEE_GetObjectInfo1 function instead
607583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
617583c59eSCedric Chaumont  * this specification
627583c59eSCedric Chaumont  */
63b0104773SPascal Brand void TEE_GetObjectInfo(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo)
64b0104773SPascal Brand {
65b0104773SPascal Brand 	TEE_Result res;
66b0104773SPascal Brand 
67e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, objectInfo);
687583c59eSCedric Chaumont 
69b0104773SPascal Brand 	if (res != TEE_SUCCESS)
70b0104773SPascal Brand 		TEE_Panic(res);
717583c59eSCedric Chaumont 
727583c59eSCedric Chaumont 	if (objectInfo->objectType == TEE_TYPE_CORRUPTED_OBJECT) {
737583c59eSCedric Chaumont 		objectInfo->keySize = 0;
747583c59eSCedric Chaumont 		objectInfo->maxKeySize = 0;
757583c59eSCedric Chaumont 		objectInfo->objectUsage = 0;
767583c59eSCedric Chaumont 		objectInfo->dataSize = 0;
777583c59eSCedric Chaumont 		objectInfo->dataPosition = 0;
787583c59eSCedric Chaumont 		objectInfo->handleFlags = 0;
797583c59eSCedric Chaumont 	}
80b0104773SPascal Brand }
81b0104773SPascal Brand 
827583c59eSCedric Chaumont TEE_Result TEE_GetObjectInfo1(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo)
837583c59eSCedric Chaumont {
847583c59eSCedric Chaumont 	TEE_Result res;
857583c59eSCedric Chaumont 
86e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, objectInfo);
877583c59eSCedric Chaumont 
88a2e9a830SCedric Chaumont 	if (res != TEE_SUCCESS &&
89a2e9a830SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
90a2e9a830SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
917583c59eSCedric Chaumont 		TEE_Panic(res);
927583c59eSCedric Chaumont 
937583c59eSCedric Chaumont 	return res;
947583c59eSCedric Chaumont }
957583c59eSCedric Chaumont 
967583c59eSCedric Chaumont /*
977583c59eSCedric Chaumont  * Use of this function is deprecated
987583c59eSCedric Chaumont  * new code SHOULD use the TEE_RestrictObjectUsage1 function instead
997583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
1007583c59eSCedric Chaumont  * this specification
1017583c59eSCedric Chaumont  */
102b0104773SPascal Brand void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage)
103b0104773SPascal Brand {
104b0104773SPascal Brand 	TEE_Result res;
1057583c59eSCedric Chaumont 	TEE_ObjectInfo objectInfo;
1067583c59eSCedric Chaumont 
107e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &objectInfo);
1087583c59eSCedric Chaumont 	if (objectInfo.objectType == TEE_TYPE_CORRUPTED_OBJECT)
1097583c59eSCedric Chaumont 		return;
1107583c59eSCedric Chaumont 
1117583c59eSCedric Chaumont 	res = TEE_RestrictObjectUsage1(object, objectUsage);
112b0104773SPascal Brand 
113b0104773SPascal Brand 	if (res != TEE_SUCCESS)
114b0104773SPascal Brand 		TEE_Panic(0);
115b0104773SPascal Brand }
116b0104773SPascal Brand 
1177583c59eSCedric Chaumont TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage)
1187583c59eSCedric Chaumont {
1197583c59eSCedric Chaumont 	TEE_Result res;
1207583c59eSCedric Chaumont 
121e86f1266SJens Wiklander 	res = utee_cryp_obj_restrict_usage((unsigned long)object, objectUsage);
1227583c59eSCedric Chaumont 
123a2e9a830SCedric Chaumont 	if (res != TEE_SUCCESS &&
124a2e9a830SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
125a2e9a830SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
126a2e9a830SCedric Chaumont 		TEE_Panic(res);
1277583c59eSCedric Chaumont 
1287583c59eSCedric Chaumont 	return res;
1297583c59eSCedric Chaumont }
1307583c59eSCedric Chaumont 
131b0104773SPascal Brand TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object,
132b0104773SPascal Brand 					uint32_t attributeID, void *buffer,
13379a3c601SCedric Chaumont 					uint32_t *size)
134b0104773SPascal Brand {
135b0104773SPascal Brand 	TEE_Result res;
136b0104773SPascal Brand 	TEE_ObjectInfo info;
137e86f1266SJens Wiklander 	uint64_t sz;
138b0104773SPascal Brand 
139e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
140b0104773SPascal Brand 	if (res != TEE_SUCCESS)
141a2e9a830SCedric Chaumont 		goto exit;
142b0104773SPascal Brand 
143b0104773SPascal Brand 	/* This function only supports reference attributes */
144a2e9a830SCedric Chaumont 	if ((attributeID & TEE_ATTR_BIT_VALUE)) {
145a2e9a830SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
146a2e9a830SCedric Chaumont 		goto exit;
147a2e9a830SCedric Chaumont 	}
148b0104773SPascal Brand 
149e86f1266SJens Wiklander 	sz = *size;
150e86f1266SJens Wiklander 	res = utee_cryp_obj_get_attr((unsigned long)object, attributeID,
151e86f1266SJens Wiklander 				     buffer, &sz);
152e86f1266SJens Wiklander 	*size = sz;
153b0104773SPascal Brand 
154a2e9a830SCedric Chaumont exit:
1550ed6a6caSCedric Chaumont 	if (res != TEE_SUCCESS &&
1560ed6a6caSCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
1570ed6a6caSCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER &&
1580ed6a6caSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
1590ed6a6caSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
160b0104773SPascal Brand 		TEE_Panic(0);
161b0104773SPascal Brand 
162b0104773SPascal Brand 	return res;
163b0104773SPascal Brand }
164b0104773SPascal Brand 
165b0104773SPascal Brand TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object,
166b0104773SPascal Brand 				       uint32_t attributeID, uint32_t *a,
167b0104773SPascal Brand 				       uint32_t *b)
168b0104773SPascal Brand {
169b0104773SPascal Brand 	TEE_Result res;
170b0104773SPascal Brand 	TEE_ObjectInfo info;
171b0104773SPascal Brand 	uint32_t buf[2];
172e86f1266SJens Wiklander 	uint64_t size = sizeof(buf);
173b0104773SPascal Brand 
174e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
175b0104773SPascal Brand 	if (res != TEE_SUCCESS)
176a2e9a830SCedric Chaumont 		goto exit;
177b0104773SPascal Brand 
178b0104773SPascal Brand 	/* This function only supports value attributes */
179a2e9a830SCedric Chaumont 	if (!(attributeID & TEE_ATTR_BIT_VALUE)) {
180a2e9a830SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
181a2e9a830SCedric Chaumont 		goto exit;
182a2e9a830SCedric Chaumont 	}
183b0104773SPascal Brand 
184e86f1266SJens Wiklander 	res = utee_cryp_obj_get_attr((unsigned long)object, attributeID, buf,
185e86f1266SJens Wiklander 				     &size);
186b0104773SPascal Brand 
187a2e9a830SCedric Chaumont exit:
1880ed6a6caSCedric Chaumont 	if (res != TEE_SUCCESS &&
1890ed6a6caSCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
1900ed6a6caSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
1910ed6a6caSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
192b0104773SPascal Brand 		TEE_Panic(0);
193b0104773SPascal Brand 
194b0104773SPascal Brand 	if (size != sizeof(buf))
195b0104773SPascal Brand 		TEE_Panic(0);
196b0104773SPascal Brand 
197*be96c837SJerome Forissier 	if (res == TEE_SUCCESS) {
198b0104773SPascal Brand 		*a = buf[0];
199b0104773SPascal Brand 		*b = buf[1];
200*be96c837SJerome Forissier 	}
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 (!objectID) {
4139b520646SCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
4149b520646SCedric Chaumont 		goto out;
4159b520646SCedric Chaumont 	}
4169b520646SCedric Chaumont 
4179b520646SCedric Chaumont 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) {
4189b520646SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
4199b520646SCedric Chaumont 		goto out;
4209b520646SCedric Chaumont 	}
4219b520646SCedric Chaumont 
4229b520646SCedric Chaumont 	if (!object) {
4239b520646SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
4249b520646SCedric Chaumont 		goto out;
4259b520646SCedric Chaumont 	}
4269b520646SCedric Chaumont 
4279b520646SCedric Chaumont 	res = utee_storage_obj_open(storageID, objectID, objectIDLen, flags,
428e86f1266SJens Wiklander 				     &obj);
429e86f1266SJens Wiklander 	if (res == TEE_SUCCESS)
430e86f1266SJens Wiklander 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
4319b520646SCedric Chaumont 
4329b520646SCedric Chaumont out:
4339b520646SCedric Chaumont 	if (res != TEE_SUCCESS &&
4349b520646SCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
4359b520646SCedric Chaumont 	    res != TEE_ERROR_ACCESS_CONFLICT &&
4369b520646SCedric Chaumont 	    res != TEE_ERROR_OUT_OF_MEMORY &&
4379b520646SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
4389b520646SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
439b0104773SPascal Brand 		TEE_Panic(0);
440b0104773SPascal Brand 
4419b520646SCedric Chaumont 	return res;
442b0104773SPascal Brand }
443b0104773SPascal Brand 
444b0104773SPascal Brand TEE_Result TEE_CreatePersistentObject(uint32_t storageID, void *objectID,
44579a3c601SCedric Chaumont 				      uint32_t objectIDLen, uint32_t flags,
446b0104773SPascal Brand 				      TEE_ObjectHandle attributes,
447b0104773SPascal Brand 				      const void *initialData,
44879a3c601SCedric Chaumont 				      uint32_t initialDataLen,
449b0104773SPascal Brand 				      TEE_ObjectHandle *object)
450b0104773SPascal Brand {
45184431ae3SCedric Chaumont 	TEE_Result res;
452e86f1266SJens Wiklander 	uint32_t obj;
453b0104773SPascal Brand 
454aeb0d927SCedric Chaumont 	if (!objectID) {
45584431ae3SCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
45684431ae3SCedric Chaumont 		goto err;
45784431ae3SCedric Chaumont 	}
458b0104773SPascal Brand 
45984431ae3SCedric Chaumont 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) {
46084431ae3SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
46184431ae3SCedric Chaumont 		goto err;
46284431ae3SCedric Chaumont 	}
463b0104773SPascal Brand 
46484431ae3SCedric Chaumont 	res = utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
465e86f1266SJens Wiklander 				      (unsigned long)attributes, initialData,
466e86f1266SJens Wiklander 				      initialDataLen, &obj);
467e86f1266SJens Wiklander 	if (res == TEE_SUCCESS) {
4681c96fa7fSPascal Brand 		if (object)
469e86f1266SJens Wiklander 			*object = (TEE_ObjectHandle)(uintptr_t)obj;
4701c96fa7fSPascal Brand 		else
4711c96fa7fSPascal Brand 			res = utee_cryp_obj_close(obj);
4721c96fa7fSPascal Brand 		if (res == TEE_SUCCESS)
47384431ae3SCedric Chaumont 			goto out;
474e86f1266SJens Wiklander 	}
47584431ae3SCedric Chaumont err:
4761c96fa7fSPascal Brand 	if (object)
4771c96fa7fSPascal Brand 		*object = TEE_HANDLE_NULL;
47884431ae3SCedric Chaumont 	if (res == TEE_ERROR_ITEM_NOT_FOUND ||
47984431ae3SCedric Chaumont 	    res == TEE_ERROR_ACCESS_CONFLICT ||
48084431ae3SCedric Chaumont 	    res == TEE_ERROR_OUT_OF_MEMORY ||
48184431ae3SCedric Chaumont 	    res == TEE_ERROR_STORAGE_NO_SPACE ||
48284431ae3SCedric Chaumont 	    res == TEE_ERROR_CORRUPT_OBJECT ||
48384431ae3SCedric Chaumont 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE)
48484431ae3SCedric Chaumont 		return res;
48584431ae3SCedric Chaumont 	TEE_Panic(0);
48684431ae3SCedric Chaumont out:
48784431ae3SCedric Chaumont 	return TEE_SUCCESS;
488b0104773SPascal Brand }
489b0104773SPascal Brand 
4907583c59eSCedric Chaumont /*
4917583c59eSCedric Chaumont  * Use of this function is deprecated
4927583c59eSCedric Chaumont  * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead
4937583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
4947583c59eSCedric Chaumont  * this specification
4957583c59eSCedric Chaumont  */
496b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
497b0104773SPascal Brand {
498b0104773SPascal Brand 	TEE_Result res;
499b0104773SPascal Brand 
500b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
501b0104773SPascal Brand 		return;
502b0104773SPascal Brand 
5037583c59eSCedric Chaumont 	res = TEE_CloseAndDeletePersistentObject1(object);
504b0104773SPascal Brand 
505b0104773SPascal Brand 	if (res != TEE_SUCCESS)
506b0104773SPascal Brand 		TEE_Panic(0);
507b0104773SPascal Brand }
508b0104773SPascal Brand 
5097583c59eSCedric Chaumont TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object)
5107583c59eSCedric Chaumont {
5117583c59eSCedric Chaumont 	TEE_Result res;
5127583c59eSCedric Chaumont 
5137583c59eSCedric Chaumont 	if (object == TEE_HANDLE_NULL)
5147583c59eSCedric Chaumont 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
5157583c59eSCedric Chaumont 
516e86f1266SJens Wiklander 	res = utee_storage_obj_del((unsigned long)object);
5177583c59eSCedric Chaumont 
5187583c59eSCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
5197583c59eSCedric Chaumont 		TEE_Panic(0);
5207583c59eSCedric Chaumont 
5217583c59eSCedric Chaumont 	return res;
5227583c59eSCedric Chaumont }
5237583c59eSCedric Chaumont 
5247583c59eSCedric Chaumont 
525b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
526b0104773SPascal Brand 				      const void *newObjectID,
52779a3c601SCedric Chaumont 				      uint32_t newObjectIDLen)
528b0104773SPascal Brand {
529b0104773SPascal Brand 	TEE_Result res;
530b0104773SPascal Brand 
531a76bf53fSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
532a76bf53fSCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
533a76bf53fSCedric Chaumont 		goto out;
534a76bf53fSCedric Chaumont 	}
535b0104773SPascal Brand 
536a76bf53fSCedric Chaumont 	if (!newObjectID) {
537a76bf53fSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
538a76bf53fSCedric Chaumont 		goto out;
539a76bf53fSCedric Chaumont 	}
540b0104773SPascal Brand 
541a76bf53fSCedric Chaumont 	if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN) {
542a76bf53fSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
543a76bf53fSCedric Chaumont 		goto out;
544a76bf53fSCedric Chaumont 	}
545b0104773SPascal Brand 
546e86f1266SJens Wiklander 	res = utee_storage_obj_rename((unsigned long)object, newObjectID,
547e86f1266SJens Wiklander 				      newObjectIDLen);
548b0104773SPascal Brand 
549a76bf53fSCedric Chaumont out:
550a76bf53fSCedric Chaumont 	if (res != TEE_SUCCESS &&
551a76bf53fSCedric Chaumont 	    res != TEE_ERROR_ACCESS_CONFLICT &&
552a76bf53fSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
553a76bf53fSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
554b0104773SPascal Brand 		TEE_Panic(0);
555b0104773SPascal Brand 
556b0104773SPascal Brand 	return res;
557b0104773SPascal Brand }
558b0104773SPascal Brand 
559b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
560b0104773SPascal Brand 						  objectEnumerator)
561b0104773SPascal Brand {
562b0104773SPascal Brand 	TEE_Result res;
563e86f1266SJens Wiklander 	uint32_t oe;
564b0104773SPascal Brand 
56515cd3c30SCedric Chaumont 	if (!objectEnumerator)
566b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
567b0104773SPascal Brand 
568e86f1266SJens Wiklander 	res = utee_storage_alloc_enum(&oe);
569b0104773SPascal Brand 
570b0104773SPascal Brand 	if (res != TEE_SUCCESS)
571e86f1266SJens Wiklander 		oe = TEE_HANDLE_NULL;
572e86f1266SJens Wiklander 
573e86f1266SJens Wiklander 	*objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe;
574b0104773SPascal Brand 
57515cd3c30SCedric Chaumont 	if (res != TEE_SUCCESS &&
57615cd3c30SCedric Chaumont 	    res != TEE_ERROR_ACCESS_CONFLICT)
57715cd3c30SCedric Chaumont 		TEE_Panic(0);
57815cd3c30SCedric Chaumont 
579b0104773SPascal Brand 	return res;
580b0104773SPascal Brand }
581b0104773SPascal Brand 
582b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
583b0104773SPascal Brand {
584b0104773SPascal Brand 	TEE_Result res;
585b0104773SPascal Brand 
586b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
587b0104773SPascal Brand 		return;
588b0104773SPascal Brand 
589e86f1266SJens Wiklander 	res = utee_storage_free_enum((unsigned long)objectEnumerator);
590b0104773SPascal Brand 
591b0104773SPascal Brand 	if (res != TEE_SUCCESS)
592b0104773SPascal Brand 		TEE_Panic(0);
593b0104773SPascal Brand }
594b0104773SPascal Brand 
595b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
596b0104773SPascal Brand {
597b0104773SPascal Brand 	TEE_Result res;
598b0104773SPascal Brand 
599b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
600b0104773SPascal Brand 		return;
601b0104773SPascal Brand 
602e86f1266SJens Wiklander 	res = utee_storage_reset_enum((unsigned long)objectEnumerator);
603b0104773SPascal Brand 
604b0104773SPascal Brand 	if (res != TEE_SUCCESS)
605b0104773SPascal Brand 		TEE_Panic(0);
606b0104773SPascal Brand }
607b0104773SPascal Brand 
608b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
609b0104773SPascal Brand 					       objectEnumerator,
610b0104773SPascal Brand 					       uint32_t storageID)
611b0104773SPascal Brand {
612b0104773SPascal Brand 	TEE_Result res;
613b0104773SPascal Brand 
614e86f1266SJens Wiklander 	res = utee_storage_start_enum((unsigned long)objectEnumerator,
615e86f1266SJens Wiklander 				      storageID);
616b0104773SPascal Brand 
61715cd3c30SCedric Chaumont 	if (res != TEE_SUCCESS &&
61815cd3c30SCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
61915cd3c30SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
62015cd3c30SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
621b0104773SPascal Brand 		TEE_Panic(0);
622b0104773SPascal Brand 
623b0104773SPascal Brand 	return res;
624b0104773SPascal Brand }
625b0104773SPascal Brand 
626b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
627b0104773SPascal Brand 				       TEE_ObjectInfo *objectInfo,
62879a3c601SCedric Chaumont 				       void *objectID, uint32_t *objectIDLen)
629b0104773SPascal Brand {
630b0104773SPascal Brand 	TEE_Result res;
631e86f1266SJens Wiklander 	uint64_t len;
6322342799fSPascal Brand 	TEE_ObjectInfo local_info;
6332342799fSPascal Brand 	TEE_ObjectInfo *pt_info;
634b0104773SPascal Brand 
63515cd3c30SCedric Chaumont 	if (!objectID) {
63615cd3c30SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
63715cd3c30SCedric Chaumont 		goto out;
63815cd3c30SCedric Chaumont 	}
63915cd3c30SCedric Chaumont 
64015cd3c30SCedric Chaumont 	if (!objectIDLen) {
64115cd3c30SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
64215cd3c30SCedric Chaumont 		goto out;
64315cd3c30SCedric Chaumont 	}
64415cd3c30SCedric Chaumont 
6452342799fSPascal Brand 	if (objectInfo)
6462342799fSPascal Brand 		pt_info = objectInfo;
6472342799fSPascal Brand 	else
6482342799fSPascal Brand 		pt_info = &local_info;
649e86f1266SJens Wiklander 	len = *objectIDLen;
650e86f1266SJens Wiklander 	res = utee_storage_next_enum((unsigned long)objectEnumerator,
6512342799fSPascal Brand 				     pt_info, objectID, &len);
652e86f1266SJens Wiklander 	*objectIDLen = len;
653b0104773SPascal Brand 
65415cd3c30SCedric Chaumont out:
65515cd3c30SCedric Chaumont 	if (res != TEE_SUCCESS &&
65615cd3c30SCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
65715cd3c30SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
65815cd3c30SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
659b0104773SPascal Brand 		TEE_Panic(0);
660b0104773SPascal Brand 
661b0104773SPascal Brand 	return res;
662b0104773SPascal Brand }
663b0104773SPascal Brand 
664b0104773SPascal Brand /* Data and Key Storage API  - Data Stream Access Functions */
665b0104773SPascal Brand 
666b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
66779a3c601SCedric Chaumont 			      uint32_t size, uint32_t *count)
668b0104773SPascal Brand {
669b0104773SPascal Brand 	TEE_Result res;
670e86f1266SJens Wiklander 	uint64_t cnt64;
671b0104773SPascal Brand 
672ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
673ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
674ae1289baSCedric Chaumont 		goto out;
675ae1289baSCedric Chaumont 	}
676b0104773SPascal Brand 
677e86f1266SJens Wiklander 	cnt64 = *count;
678e86f1266SJens Wiklander 	res = utee_storage_obj_read((unsigned long)object, buffer, size,
679e86f1266SJens Wiklander 				    &cnt64);
680e86f1266SJens Wiklander 	*count = cnt64;
681b0104773SPascal Brand 
682ae1289baSCedric Chaumont out:
683ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
684ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
685ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
686b0104773SPascal Brand 		TEE_Panic(0);
687b0104773SPascal Brand 
688b0104773SPascal Brand 	return res;
689b0104773SPascal Brand }
690b0104773SPascal Brand 
691b0104773SPascal Brand TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, void *buffer,
69279a3c601SCedric Chaumont 			       uint32_t size)
693b0104773SPascal Brand {
694b0104773SPascal Brand 	TEE_Result res;
695b0104773SPascal Brand 
696ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
697ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
698ae1289baSCedric Chaumont 		goto out;
699ae1289baSCedric Chaumont 	}
700ae1289baSCedric Chaumont 
701ae1289baSCedric Chaumont 	if (size > TEE_DATA_MAX_POSITION) {
702ae1289baSCedric Chaumont 		res = TEE_ERROR_OVERFLOW;
703ae1289baSCedric Chaumont 		goto out;
704ae1289baSCedric Chaumont 	}
705b0104773SPascal Brand 
706e86f1266SJens Wiklander 	res = utee_storage_obj_write((unsigned long)object, buffer, size);
707b0104773SPascal Brand 
708ae1289baSCedric Chaumont out:
709ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
710ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
711ae1289baSCedric Chaumont 	    res != TEE_ERROR_OVERFLOW &&
712ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
713ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
714b0104773SPascal Brand 		TEE_Panic(0);
715b0104773SPascal Brand 
716b0104773SPascal Brand 	return res;
717b0104773SPascal Brand }
718b0104773SPascal Brand 
719b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
720b0104773SPascal Brand {
721b0104773SPascal Brand 	TEE_Result res;
722b0104773SPascal Brand 
723ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
724ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
725ae1289baSCedric Chaumont 		goto out;
726ae1289baSCedric Chaumont 	}
727b0104773SPascal Brand 
728e86f1266SJens Wiklander 	res = utee_storage_obj_trunc((unsigned long)object, size);
729b0104773SPascal Brand 
730ae1289baSCedric Chaumont out:
731ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
732ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
733ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
734ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
735b0104773SPascal Brand 		TEE_Panic(0);
736b0104773SPascal Brand 
737b0104773SPascal Brand 	return res;
738b0104773SPascal Brand }
739b0104773SPascal Brand 
740b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
741b0104773SPascal Brand 			      TEE_Whence whence)
742b0104773SPascal Brand {
743b0104773SPascal Brand 	TEE_Result res;
744b0104773SPascal Brand 	TEE_ObjectInfo info;
745b0104773SPascal Brand 
746ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
747ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
748ae1289baSCedric Chaumont 		goto out;
749ae1289baSCedric Chaumont 	}
750b0104773SPascal Brand 
751e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
752b0104773SPascal Brand 	if (res != TEE_SUCCESS)
753ae1289baSCedric Chaumont 		goto out;
754b0104773SPascal Brand 
755b0104773SPascal Brand 	switch (whence) {
756b0104773SPascal Brand 	case TEE_DATA_SEEK_SET:
757ae1289baSCedric Chaumont 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) {
758ae1289baSCedric Chaumont 			res = TEE_ERROR_OVERFLOW;
759ae1289baSCedric Chaumont 			goto out;
760ae1289baSCedric Chaumont 		}
761b0104773SPascal Brand 		break;
762b0104773SPascal Brand 	case TEE_DATA_SEEK_CUR:
763b0104773SPascal Brand 		if (offset > 0 &&
764b0104773SPascal Brand 		    ((uint32_t)offset + info.dataPosition >
765b0104773SPascal Brand 		     TEE_DATA_MAX_POSITION ||
766b0104773SPascal Brand 		     (uint32_t)offset + info.dataPosition <
767ae1289baSCedric Chaumont 		     info.dataPosition)) {
768ae1289baSCedric Chaumont 			res = TEE_ERROR_OVERFLOW;
769ae1289baSCedric Chaumont 			goto out;
770ae1289baSCedric Chaumont 		}
771b0104773SPascal Brand 		break;
772b0104773SPascal Brand 	case TEE_DATA_SEEK_END:
773b0104773SPascal Brand 		if (offset > 0 &&
774b0104773SPascal Brand 		    ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION ||
775ae1289baSCedric Chaumont 		     (uint32_t)offset + info.dataSize < info.dataSize)) {
776ae1289baSCedric Chaumont 			res = TEE_ERROR_OVERFLOW;
777ae1289baSCedric Chaumont 			goto out;
778ae1289baSCedric Chaumont 		}
779b0104773SPascal Brand 		break;
780b0104773SPascal Brand 	default:
781ae1289baSCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
782ae1289baSCedric Chaumont 		goto out;
783b0104773SPascal Brand 	}
784b0104773SPascal Brand 
785e86f1266SJens Wiklander 	res = utee_storage_obj_seek((unsigned long)object, offset, whence);
786b0104773SPascal Brand 
787ae1289baSCedric Chaumont out:
788ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
789ae1289baSCedric Chaumont 	    res != TEE_ERROR_OVERFLOW &&
790ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
791ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
792b0104773SPascal Brand 		TEE_Panic(0);
793b0104773SPascal Brand 
794b0104773SPascal Brand 	return res;
795b0104773SPascal Brand }
796