xref: /optee_os/lib/libutee/tee_api_objects.c (revision 8f07fe6f754c642bcc33224df2772359b36f7d2b)
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)
114b36311adSJerome Forissier 		TEE_Panic(res);
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)
160b36311adSJerome Forissier 		TEE_Panic(res);
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)
192b36311adSJerome Forissier 		TEE_Panic(res);
193b0104773SPascal Brand 
194b0104773SPascal Brand 	if (size != sizeof(buf))
195b0104773SPascal Brand 		TEE_Panic(0);
196b0104773SPascal Brand 
197be96c837SJerome Forissier 	if (res == TEE_SUCCESS) {
198b9d8f134SJerome Forissier 		if (a)
199b0104773SPascal Brand 			*a = buf[0];
200b9d8f134SJerome Forissier 		if (b)
201b0104773SPascal Brand 			*b = buf[1];
202be96c837SJerome Forissier 	}
203b0104773SPascal Brand 
204b0104773SPascal Brand 	return res;
205b0104773SPascal Brand }
206b0104773SPascal Brand 
207b0104773SPascal Brand void TEE_CloseObject(TEE_ObjectHandle object)
208b0104773SPascal Brand {
209b0104773SPascal Brand 	TEE_Result res;
210b0104773SPascal Brand 
211b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
212b0104773SPascal Brand 		return;
213b0104773SPascal Brand 
214e86f1266SJens Wiklander 	res = utee_cryp_obj_close((unsigned long)object);
215b0104773SPascal Brand 	if (res != TEE_SUCCESS)
216b36311adSJerome Forissier 		TEE_Panic(res);
217b0104773SPascal Brand }
218b0104773SPascal Brand 
219b0104773SPascal Brand /* Data and Key Storage API  - Transient Object Functions */
220b0104773SPascal Brand 
221b0104773SPascal Brand TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType,
22279a3c601SCedric Chaumont 				       uint32_t maxKeySize,
223b0104773SPascal Brand 				       TEE_ObjectHandle *object)
224b0104773SPascal Brand {
225b0104773SPascal Brand 	TEE_Result res;
226b0104773SPascal Brand 	uint32_t obj;
227b0104773SPascal Brand 
22879a3c601SCedric Chaumont 	res = utee_cryp_obj_alloc(objectType, maxKeySize, &obj);
229aeb0d927SCedric Chaumont 
230aeb0d927SCedric Chaumont 	if (res != TEE_SUCCESS &&
231aeb0d927SCedric Chaumont 	    res != TEE_ERROR_OUT_OF_MEMORY &&
232aeb0d927SCedric Chaumont 	    res != TEE_ERROR_NOT_SUPPORTED)
233b36311adSJerome Forissier 		TEE_Panic(res);
234aeb0d927SCedric Chaumont 
235b0104773SPascal Brand 	if (res == TEE_SUCCESS)
236e86f1266SJens Wiklander 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
2370ed6a6caSCedric Chaumont 
238b0104773SPascal Brand 	return res;
239b0104773SPascal Brand }
240b0104773SPascal Brand 
241b0104773SPascal Brand void TEE_FreeTransientObject(TEE_ObjectHandle object)
242b0104773SPascal Brand {
243b0104773SPascal Brand 	TEE_Result res;
244b0104773SPascal Brand 	TEE_ObjectInfo info;
245b0104773SPascal Brand 
246b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
247b0104773SPascal Brand 		return;
248b0104773SPascal Brand 
249e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
250b0104773SPascal Brand 	if (res != TEE_SUCCESS)
251b36311adSJerome Forissier 		TEE_Panic(res);
252b0104773SPascal Brand 
253b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
254b0104773SPascal Brand 		TEE_Panic(0);
255b0104773SPascal Brand 
256e86f1266SJens Wiklander 	res = utee_cryp_obj_close((unsigned long)object);
257b0104773SPascal Brand 	if (res != TEE_SUCCESS)
258b36311adSJerome Forissier 		TEE_Panic(res);
259b0104773SPascal Brand }
260b0104773SPascal Brand 
261b0104773SPascal Brand void TEE_ResetTransientObject(TEE_ObjectHandle object)
262b0104773SPascal Brand {
263b0104773SPascal Brand 	TEE_Result res;
264b0104773SPascal Brand 	TEE_ObjectInfo info;
265b0104773SPascal Brand 
266b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
267b0104773SPascal Brand 		return;
268b0104773SPascal Brand 
269e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
270b0104773SPascal Brand 	if (res != TEE_SUCCESS)
271b36311adSJerome Forissier 		TEE_Panic(res);
272b0104773SPascal Brand 
273b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
274b0104773SPascal Brand 		TEE_Panic(0);
275b0104773SPascal Brand 
276e86f1266SJens Wiklander 	res = utee_cryp_obj_reset((unsigned long)object);
277b0104773SPascal Brand 	if (res != TEE_SUCCESS)
278b36311adSJerome Forissier 		TEE_Panic(res);
279b0104773SPascal Brand }
280b0104773SPascal Brand 
281b0104773SPascal Brand TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object,
282*8f07fe6fSJerome Forissier 				       const TEE_Attribute *attrs,
283b0104773SPascal Brand 				       uint32_t attrCount)
284b0104773SPascal Brand {
285b0104773SPascal Brand 	TEE_Result res;
286b0104773SPascal Brand 	TEE_ObjectInfo info;
287e86f1266SJens Wiklander 	struct utee_attribute ua[attrCount];
288b0104773SPascal Brand 
289e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
290b0104773SPascal Brand 	if (res != TEE_SUCCESS)
291b36311adSJerome Forissier 		TEE_Panic(res);
292b0104773SPascal Brand 
293b0104773SPascal Brand 	/* Must be a transient object */
294b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
295b0104773SPascal Brand 		TEE_Panic(0);
296b0104773SPascal Brand 
297b0104773SPascal Brand 	/* Must not be initialized already */
298b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
299b0104773SPascal Brand 		TEE_Panic(0);
300b0104773SPascal Brand 
301e86f1266SJens Wiklander 	__utee_from_attr(ua, attrs, attrCount);
302e86f1266SJens Wiklander 	res = utee_cryp_obj_populate((unsigned long)object, ua, attrCount);
303b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
304b0104773SPascal Brand 		TEE_Panic(res);
305b0104773SPascal Brand 	return res;
306b0104773SPascal Brand }
307b0104773SPascal Brand 
308b0104773SPascal Brand void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID,
309*8f07fe6fSJerome Forissier 			  const void *buffer, uint32_t length)
310b0104773SPascal Brand {
311b0104773SPascal Brand 	if (attr == NULL)
312b0104773SPascal Brand 		TEE_Panic(0);
313b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) != 0)
314b0104773SPascal Brand 		TEE_Panic(0);
315b0104773SPascal Brand 	attr->attributeID = attributeID;
316*8f07fe6fSJerome Forissier 	attr->content.ref.buffer = (void *)buffer;
317b0104773SPascal Brand 	attr->content.ref.length = length;
318b0104773SPascal Brand }
319b0104773SPascal Brand 
320b0104773SPascal Brand void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID,
321b0104773SPascal Brand 			    uint32_t a, uint32_t b)
322b0104773SPascal Brand {
323b0104773SPascal Brand 	if (attr == NULL)
324b0104773SPascal Brand 		TEE_Panic(0);
325b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) == 0)
326b0104773SPascal Brand 		TEE_Panic(0);
327b0104773SPascal Brand 	attr->attributeID = attributeID;
328b0104773SPascal Brand 	attr->content.value.a = a;
329b0104773SPascal Brand 	attr->content.value.b = b;
330b0104773SPascal Brand }
331b0104773SPascal Brand 
3327583c59eSCedric Chaumont /*
3337583c59eSCedric Chaumont  * Use of this function is deprecated
3347583c59eSCedric Chaumont  * new code SHOULD use the TEE_CopyObjectAttributes1 function instead
3357583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
3367583c59eSCedric Chaumont  * this specification
3377583c59eSCedric Chaumont  */
338b0104773SPascal Brand void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject,
339b0104773SPascal Brand 			      TEE_ObjectHandle srcObject)
340b0104773SPascal Brand {
341b0104773SPascal Brand 	TEE_Result res;
3427583c59eSCedric Chaumont 	TEE_ObjectInfo src_info;
3437583c59eSCedric Chaumont 
344e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)srcObject, &src_info);
3457583c59eSCedric Chaumont 	if (src_info.objectType == TEE_TYPE_CORRUPTED_OBJECT)
3467583c59eSCedric Chaumont 		return;
3477583c59eSCedric Chaumont 
3487583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(destObject, srcObject);
3497583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
350b36311adSJerome Forissier 		TEE_Panic(res);
3517583c59eSCedric Chaumont }
3527583c59eSCedric Chaumont 
3537583c59eSCedric Chaumont TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject,
3547583c59eSCedric Chaumont 			      TEE_ObjectHandle srcObject)
3557583c59eSCedric Chaumont {
3567583c59eSCedric Chaumont 	TEE_Result res;
357b0104773SPascal Brand 	TEE_ObjectInfo dst_info;
358b0104773SPascal Brand 	TEE_ObjectInfo src_info;
359b0104773SPascal Brand 
360e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)destObject, &dst_info);
361b0104773SPascal Brand 	if (res != TEE_SUCCESS)
362a2e9a830SCedric Chaumont 		goto exit;
363b0104773SPascal Brand 
364e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)srcObject, &src_info);
365b0104773SPascal Brand 	if (res != TEE_SUCCESS)
366a2e9a830SCedric Chaumont 		goto exit;
367b0104773SPascal Brand 
368a2e9a830SCedric Chaumont 	if (!(src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED))
369b0104773SPascal Brand 		TEE_Panic(0);
370a2e9a830SCedric Chaumont 
371a2e9a830SCedric Chaumont 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT))
372b0104773SPascal Brand 		TEE_Panic(0);
373a2e9a830SCedric Chaumont 
374a2e9a830SCedric Chaumont 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED))
375b0104773SPascal Brand 		TEE_Panic(0);
376b0104773SPascal Brand 
377e86f1266SJens Wiklander 	res = utee_cryp_obj_copy((unsigned long)destObject,
378e86f1266SJens Wiklander 				 (unsigned long)srcObject);
3797583c59eSCedric Chaumont 
380a2e9a830SCedric Chaumont exit:
381a2e9a830SCedric Chaumont 	if (res != TEE_SUCCESS &&
382a2e9a830SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
383a2e9a830SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
384a2e9a830SCedric Chaumont 		TEE_Panic(res);
3857583c59eSCedric Chaumont 
3867583c59eSCedric Chaumont 	return res;
387b0104773SPascal Brand }
388b0104773SPascal Brand 
389b0104773SPascal Brand TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
390*8f07fe6fSJerome Forissier 			   const TEE_Attribute *params, uint32_t paramCount)
391b0104773SPascal Brand {
392b0104773SPascal Brand 	TEE_Result res;
393e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
394b0104773SPascal Brand 
395e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
396e86f1266SJens Wiklander 	res = utee_cryp_obj_generate_key((unsigned long)object, keySize,
397e86f1266SJens Wiklander 					 ua, paramCount);
398b0104773SPascal Brand 
399aeb0d927SCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
400b36311adSJerome Forissier 		TEE_Panic(res);
401b0104773SPascal Brand 
402b0104773SPascal Brand 	return res;
403b0104773SPascal Brand }
404b0104773SPascal Brand 
405b0104773SPascal Brand /* Data and Key Storage API  - Persistent Object Functions */
406b0104773SPascal Brand 
407*8f07fe6fSJerome Forissier TEE_Result TEE_OpenPersistentObject(uint32_t storageID, const void *objectID,
40879a3c601SCedric Chaumont 				    uint32_t objectIDLen, uint32_t flags,
409b0104773SPascal Brand 				    TEE_ObjectHandle *object)
410b0104773SPascal Brand {
4119b520646SCedric Chaumont 	TEE_Result res;
412e86f1266SJens Wiklander 	uint32_t obj;
413b0104773SPascal Brand 
4149b520646SCedric Chaumont 	if (!objectID) {
4159b520646SCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
4169b520646SCedric Chaumont 		goto out;
4179b520646SCedric Chaumont 	}
4189b520646SCedric Chaumont 
4199b520646SCedric Chaumont 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) {
4209b520646SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
4219b520646SCedric Chaumont 		goto out;
4229b520646SCedric Chaumont 	}
4239b520646SCedric Chaumont 
4249b520646SCedric Chaumont 	if (!object) {
4259b520646SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
4269b520646SCedric Chaumont 		goto out;
4279b520646SCedric Chaumont 	}
4289b520646SCedric Chaumont 
4299b520646SCedric Chaumont 	res = utee_storage_obj_open(storageID, objectID, objectIDLen, flags,
430e86f1266SJens Wiklander 				     &obj);
431e86f1266SJens Wiklander 	if (res == TEE_SUCCESS)
432e86f1266SJens Wiklander 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
4339b520646SCedric Chaumont 
4349b520646SCedric Chaumont out:
4359b520646SCedric Chaumont 	if (res != TEE_SUCCESS &&
4369b520646SCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
4379b520646SCedric Chaumont 	    res != TEE_ERROR_ACCESS_CONFLICT &&
4389b520646SCedric Chaumont 	    res != TEE_ERROR_OUT_OF_MEMORY &&
4399b520646SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
4409b520646SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
441b36311adSJerome Forissier 		TEE_Panic(res);
442b0104773SPascal Brand 
4439b520646SCedric Chaumont 	return res;
444b0104773SPascal Brand }
445b0104773SPascal Brand 
446*8f07fe6fSJerome Forissier TEE_Result TEE_CreatePersistentObject(uint32_t storageID, const void *objectID,
44779a3c601SCedric Chaumont 				      uint32_t objectIDLen, uint32_t flags,
448b0104773SPascal Brand 				      TEE_ObjectHandle attributes,
449b0104773SPascal Brand 				      const void *initialData,
45079a3c601SCedric Chaumont 				      uint32_t initialDataLen,
451b0104773SPascal Brand 				      TEE_ObjectHandle *object)
452b0104773SPascal Brand {
45384431ae3SCedric Chaumont 	TEE_Result res;
454e86f1266SJens Wiklander 	uint32_t obj;
455b0104773SPascal Brand 
456aeb0d927SCedric Chaumont 	if (!objectID) {
45784431ae3SCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
45884431ae3SCedric Chaumont 		goto err;
45984431ae3SCedric Chaumont 	}
460b0104773SPascal Brand 
46184431ae3SCedric Chaumont 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) {
46284431ae3SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
46384431ae3SCedric Chaumont 		goto err;
46484431ae3SCedric Chaumont 	}
465b0104773SPascal Brand 
46684431ae3SCedric Chaumont 	res = utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
467e86f1266SJens Wiklander 				      (unsigned long)attributes, initialData,
468e86f1266SJens Wiklander 				      initialDataLen, &obj);
469e86f1266SJens Wiklander 	if (res == TEE_SUCCESS) {
4701c96fa7fSPascal Brand 		if (object)
471e86f1266SJens Wiklander 			*object = (TEE_ObjectHandle)(uintptr_t)obj;
4721c96fa7fSPascal Brand 		else
4731c96fa7fSPascal Brand 			res = utee_cryp_obj_close(obj);
4741c96fa7fSPascal Brand 		if (res == TEE_SUCCESS)
47584431ae3SCedric Chaumont 			goto out;
476e86f1266SJens Wiklander 	}
47784431ae3SCedric Chaumont err:
4781c96fa7fSPascal Brand 	if (object)
4791c96fa7fSPascal Brand 		*object = TEE_HANDLE_NULL;
48084431ae3SCedric Chaumont 	if (res == TEE_ERROR_ITEM_NOT_FOUND ||
48184431ae3SCedric Chaumont 	    res == TEE_ERROR_ACCESS_CONFLICT ||
48284431ae3SCedric Chaumont 	    res == TEE_ERROR_OUT_OF_MEMORY ||
48384431ae3SCedric Chaumont 	    res == TEE_ERROR_STORAGE_NO_SPACE ||
48484431ae3SCedric Chaumont 	    res == TEE_ERROR_CORRUPT_OBJECT ||
48584431ae3SCedric Chaumont 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE)
48684431ae3SCedric Chaumont 		return res;
487b36311adSJerome Forissier 	TEE_Panic(res);
48884431ae3SCedric Chaumont out:
48984431ae3SCedric Chaumont 	return TEE_SUCCESS;
490b0104773SPascal Brand }
491b0104773SPascal Brand 
4927583c59eSCedric Chaumont /*
4937583c59eSCedric Chaumont  * Use of this function is deprecated
4947583c59eSCedric Chaumont  * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead
4957583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
4967583c59eSCedric Chaumont  * this specification
4977583c59eSCedric Chaumont  */
498b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
499b0104773SPascal Brand {
500b0104773SPascal Brand 	TEE_Result res;
501b0104773SPascal Brand 
502b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
503b0104773SPascal Brand 		return;
504b0104773SPascal Brand 
5057583c59eSCedric Chaumont 	res = TEE_CloseAndDeletePersistentObject1(object);
506b0104773SPascal Brand 
507b0104773SPascal Brand 	if (res != TEE_SUCCESS)
508b0104773SPascal Brand 		TEE_Panic(0);
509b0104773SPascal Brand }
510b0104773SPascal Brand 
5117583c59eSCedric Chaumont TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object)
5127583c59eSCedric Chaumont {
5137583c59eSCedric Chaumont 	TEE_Result res;
5147583c59eSCedric Chaumont 
5157583c59eSCedric Chaumont 	if (object == TEE_HANDLE_NULL)
5167583c59eSCedric Chaumont 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
5177583c59eSCedric Chaumont 
518e86f1266SJens Wiklander 	res = utee_storage_obj_del((unsigned long)object);
5197583c59eSCedric Chaumont 
5207583c59eSCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
521b36311adSJerome Forissier 		TEE_Panic(res);
5227583c59eSCedric Chaumont 
5237583c59eSCedric Chaumont 	return res;
5247583c59eSCedric Chaumont }
5257583c59eSCedric Chaumont 
5267583c59eSCedric Chaumont 
527b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
528b0104773SPascal Brand 				      const void *newObjectID,
52979a3c601SCedric Chaumont 				      uint32_t newObjectIDLen)
530b0104773SPascal Brand {
531b0104773SPascal Brand 	TEE_Result res;
532b0104773SPascal Brand 
533a76bf53fSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
534a76bf53fSCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
535a76bf53fSCedric Chaumont 		goto out;
536a76bf53fSCedric Chaumont 	}
537b0104773SPascal Brand 
538a76bf53fSCedric Chaumont 	if (!newObjectID) {
539a76bf53fSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
540a76bf53fSCedric Chaumont 		goto out;
541a76bf53fSCedric Chaumont 	}
542b0104773SPascal Brand 
543a76bf53fSCedric Chaumont 	if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN) {
544a76bf53fSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
545a76bf53fSCedric Chaumont 		goto out;
546a76bf53fSCedric Chaumont 	}
547b0104773SPascal Brand 
548e86f1266SJens Wiklander 	res = utee_storage_obj_rename((unsigned long)object, newObjectID,
549e86f1266SJens Wiklander 				      newObjectIDLen);
550b0104773SPascal Brand 
551a76bf53fSCedric Chaumont out:
552a76bf53fSCedric Chaumont 	if (res != TEE_SUCCESS &&
553a76bf53fSCedric Chaumont 	    res != TEE_ERROR_ACCESS_CONFLICT &&
554a76bf53fSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
555a76bf53fSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
556b36311adSJerome Forissier 		TEE_Panic(res);
557b0104773SPascal Brand 
558b0104773SPascal Brand 	return res;
559b0104773SPascal Brand }
560b0104773SPascal Brand 
561b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
562b0104773SPascal Brand 						  objectEnumerator)
563b0104773SPascal Brand {
564b0104773SPascal Brand 	TEE_Result res;
565e86f1266SJens Wiklander 	uint32_t oe;
566b0104773SPascal Brand 
56715cd3c30SCedric Chaumont 	if (!objectEnumerator)
568b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
569b0104773SPascal Brand 
570e86f1266SJens Wiklander 	res = utee_storage_alloc_enum(&oe);
571b0104773SPascal Brand 
572b0104773SPascal Brand 	if (res != TEE_SUCCESS)
573e86f1266SJens Wiklander 		oe = TEE_HANDLE_NULL;
574e86f1266SJens Wiklander 
575e86f1266SJens Wiklander 	*objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe;
576b0104773SPascal Brand 
57715cd3c30SCedric Chaumont 	if (res != TEE_SUCCESS &&
57815cd3c30SCedric Chaumont 	    res != TEE_ERROR_ACCESS_CONFLICT)
579b36311adSJerome Forissier 		TEE_Panic(res);
58015cd3c30SCedric Chaumont 
581b0104773SPascal Brand 	return res;
582b0104773SPascal Brand }
583b0104773SPascal Brand 
584b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
585b0104773SPascal Brand {
586b0104773SPascal Brand 	TEE_Result res;
587b0104773SPascal Brand 
588b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
589b0104773SPascal Brand 		return;
590b0104773SPascal Brand 
591e86f1266SJens Wiklander 	res = utee_storage_free_enum((unsigned long)objectEnumerator);
592b0104773SPascal Brand 
593b0104773SPascal Brand 	if (res != TEE_SUCCESS)
594b36311adSJerome Forissier 		TEE_Panic(res);
595b0104773SPascal Brand }
596b0104773SPascal Brand 
597b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
598b0104773SPascal Brand {
599b0104773SPascal Brand 	TEE_Result res;
600b0104773SPascal Brand 
601b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
602b0104773SPascal Brand 		return;
603b0104773SPascal Brand 
604e86f1266SJens Wiklander 	res = utee_storage_reset_enum((unsigned long)objectEnumerator);
605b0104773SPascal Brand 
606b0104773SPascal Brand 	if (res != TEE_SUCCESS)
607b36311adSJerome Forissier 		TEE_Panic(res);
608b0104773SPascal Brand }
609b0104773SPascal Brand 
610b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
611b0104773SPascal Brand 					       objectEnumerator,
612b0104773SPascal Brand 					       uint32_t storageID)
613b0104773SPascal Brand {
614b0104773SPascal Brand 	TEE_Result res;
615b0104773SPascal Brand 
616e86f1266SJens Wiklander 	res = utee_storage_start_enum((unsigned long)objectEnumerator,
617e86f1266SJens Wiklander 				      storageID);
618b0104773SPascal Brand 
61915cd3c30SCedric Chaumont 	if (res != TEE_SUCCESS &&
62015cd3c30SCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
62115cd3c30SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
62215cd3c30SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
623b36311adSJerome Forissier 		TEE_Panic(res);
624b0104773SPascal Brand 
625b0104773SPascal Brand 	return res;
626b0104773SPascal Brand }
627b0104773SPascal Brand 
628b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
629b0104773SPascal Brand 				       TEE_ObjectInfo *objectInfo,
63079a3c601SCedric Chaumont 				       void *objectID, uint32_t *objectIDLen)
631b0104773SPascal Brand {
632b0104773SPascal Brand 	TEE_Result res;
633e86f1266SJens Wiklander 	uint64_t len;
6342342799fSPascal Brand 	TEE_ObjectInfo local_info;
6352342799fSPascal Brand 	TEE_ObjectInfo *pt_info;
636b0104773SPascal Brand 
63715cd3c30SCedric Chaumont 	if (!objectID) {
63815cd3c30SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
63915cd3c30SCedric Chaumont 		goto out;
64015cd3c30SCedric Chaumont 	}
64115cd3c30SCedric Chaumont 
64215cd3c30SCedric Chaumont 	if (!objectIDLen) {
64315cd3c30SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
64415cd3c30SCedric Chaumont 		goto out;
64515cd3c30SCedric Chaumont 	}
64615cd3c30SCedric Chaumont 
6472342799fSPascal Brand 	if (objectInfo)
6482342799fSPascal Brand 		pt_info = objectInfo;
6492342799fSPascal Brand 	else
6502342799fSPascal Brand 		pt_info = &local_info;
651e86f1266SJens Wiklander 	len = *objectIDLen;
652e86f1266SJens Wiklander 	res = utee_storage_next_enum((unsigned long)objectEnumerator,
6532342799fSPascal Brand 				     pt_info, objectID, &len);
654e86f1266SJens Wiklander 	*objectIDLen = len;
655b0104773SPascal Brand 
65615cd3c30SCedric Chaumont out:
65715cd3c30SCedric Chaumont 	if (res != TEE_SUCCESS &&
65815cd3c30SCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
65915cd3c30SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
66015cd3c30SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
661b36311adSJerome Forissier 		TEE_Panic(res);
662b0104773SPascal Brand 
663b0104773SPascal Brand 	return res;
664b0104773SPascal Brand }
665b0104773SPascal Brand 
666b0104773SPascal Brand /* Data and Key Storage API  - Data Stream Access Functions */
667b0104773SPascal Brand 
668b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
66979a3c601SCedric Chaumont 			      uint32_t size, uint32_t *count)
670b0104773SPascal Brand {
671b0104773SPascal Brand 	TEE_Result res;
672e86f1266SJens Wiklander 	uint64_t cnt64;
673b0104773SPascal Brand 
674ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
675ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
676ae1289baSCedric Chaumont 		goto out;
677ae1289baSCedric Chaumont 	}
678b0104773SPascal Brand 
679e86f1266SJens Wiklander 	cnt64 = *count;
680e86f1266SJens Wiklander 	res = utee_storage_obj_read((unsigned long)object, buffer, size,
681e86f1266SJens Wiklander 				    &cnt64);
682e86f1266SJens Wiklander 	*count = cnt64;
683b0104773SPascal Brand 
684ae1289baSCedric Chaumont out:
685ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
686ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
687ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
688b36311adSJerome Forissier 		TEE_Panic(res);
689b0104773SPascal Brand 
690b0104773SPascal Brand 	return res;
691b0104773SPascal Brand }
692b0104773SPascal Brand 
693*8f07fe6fSJerome Forissier TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, const void *buffer,
69479a3c601SCedric Chaumont 			       uint32_t size)
695b0104773SPascal Brand {
696b0104773SPascal Brand 	TEE_Result res;
697b0104773SPascal Brand 
698ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
699ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
700ae1289baSCedric Chaumont 		goto out;
701ae1289baSCedric Chaumont 	}
702ae1289baSCedric Chaumont 
703ae1289baSCedric Chaumont 	if (size > TEE_DATA_MAX_POSITION) {
704ae1289baSCedric Chaumont 		res = TEE_ERROR_OVERFLOW;
705ae1289baSCedric Chaumont 		goto out;
706ae1289baSCedric Chaumont 	}
707b0104773SPascal Brand 
708e86f1266SJens Wiklander 	res = utee_storage_obj_write((unsigned long)object, buffer, size);
709b0104773SPascal Brand 
710ae1289baSCedric Chaumont out:
711ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
712ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
713ae1289baSCedric Chaumont 	    res != TEE_ERROR_OVERFLOW &&
714ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
715ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
716b36311adSJerome Forissier 		TEE_Panic(res);
717b0104773SPascal Brand 
718b0104773SPascal Brand 	return res;
719b0104773SPascal Brand }
720b0104773SPascal Brand 
721b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
722b0104773SPascal Brand {
723b0104773SPascal Brand 	TEE_Result res;
724b0104773SPascal Brand 
725ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
726ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
727ae1289baSCedric Chaumont 		goto out;
728ae1289baSCedric Chaumont 	}
729b0104773SPascal Brand 
730e86f1266SJens Wiklander 	res = utee_storage_obj_trunc((unsigned long)object, size);
731b0104773SPascal Brand 
732ae1289baSCedric Chaumont out:
733ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
734ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
735ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
736ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
737b36311adSJerome Forissier 		TEE_Panic(res);
738b0104773SPascal Brand 
739b0104773SPascal Brand 	return res;
740b0104773SPascal Brand }
741b0104773SPascal Brand 
742b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
743b0104773SPascal Brand 			      TEE_Whence whence)
744b0104773SPascal Brand {
745b0104773SPascal Brand 	TEE_Result res;
746b0104773SPascal Brand 	TEE_ObjectInfo info;
747b0104773SPascal Brand 
748ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
749ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
750ae1289baSCedric Chaumont 		goto out;
751ae1289baSCedric Chaumont 	}
752b0104773SPascal Brand 
753e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
754b0104773SPascal Brand 	if (res != TEE_SUCCESS)
755ae1289baSCedric Chaumont 		goto out;
756b0104773SPascal Brand 
757b0104773SPascal Brand 	switch (whence) {
758b0104773SPascal Brand 	case TEE_DATA_SEEK_SET:
759ae1289baSCedric Chaumont 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) {
760ae1289baSCedric Chaumont 			res = TEE_ERROR_OVERFLOW;
761ae1289baSCedric Chaumont 			goto out;
762ae1289baSCedric Chaumont 		}
763b0104773SPascal Brand 		break;
764b0104773SPascal Brand 	case TEE_DATA_SEEK_CUR:
765b0104773SPascal Brand 		if (offset > 0 &&
766b0104773SPascal Brand 		    ((uint32_t)offset + info.dataPosition >
767b0104773SPascal Brand 		     TEE_DATA_MAX_POSITION ||
768b0104773SPascal Brand 		     (uint32_t)offset + info.dataPosition <
769ae1289baSCedric Chaumont 		     info.dataPosition)) {
770ae1289baSCedric Chaumont 			res = TEE_ERROR_OVERFLOW;
771ae1289baSCedric Chaumont 			goto out;
772ae1289baSCedric Chaumont 		}
773b0104773SPascal Brand 		break;
774b0104773SPascal Brand 	case TEE_DATA_SEEK_END:
775b0104773SPascal Brand 		if (offset > 0 &&
776b0104773SPascal Brand 		    ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION ||
777ae1289baSCedric Chaumont 		     (uint32_t)offset + info.dataSize < info.dataSize)) {
778ae1289baSCedric Chaumont 			res = TEE_ERROR_OVERFLOW;
779ae1289baSCedric Chaumont 			goto out;
780ae1289baSCedric Chaumont 		}
781b0104773SPascal Brand 		break;
782b0104773SPascal Brand 	default:
783ae1289baSCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
784ae1289baSCedric Chaumont 		goto out;
785b0104773SPascal Brand 	}
786b0104773SPascal Brand 
787e86f1266SJens Wiklander 	res = utee_storage_obj_seek((unsigned long)object, offset, whence);
788b0104773SPascal Brand 
789ae1289baSCedric Chaumont out:
790ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
791ae1289baSCedric Chaumont 	    res != TEE_ERROR_OVERFLOW &&
792ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
793ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
794b36311adSJerome Forissier 		TEE_Panic(res);
795b0104773SPascal Brand 
796b0104773SPascal Brand 	return res;
797b0104773SPascal Brand }
798