xref: /optee_os/lib/libutee/tee_api_objects.c (revision e86f1266afb6871795921b1eeb2bcbe5b1928cb5)
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>
32*e86f1266SJens 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 
41*e86f1266SJens Wiklander void __utee_from_attr(struct utee_attribute *ua, const TEE_Attribute *attrs,
42*e86f1266SJens Wiklander 			uint32_t attr_count)
43*e86f1266SJens Wiklander {
44*e86f1266SJens Wiklander 	size_t n;
45*e86f1266SJens Wiklander 
46*e86f1266SJens Wiklander 	for (n = 0; n < attr_count; n++) {
47*e86f1266SJens Wiklander 		ua[n].attribute_id = attrs[n].attributeID;
48*e86f1266SJens Wiklander 		if (attrs[n].attributeID & TEE_ATTR_BIT_VALUE) {
49*e86f1266SJens Wiklander 			ua[n].a = attrs[n].content.value.a;
50*e86f1266SJens Wiklander 			ua[n].b = attrs[n].content.value.b;
51*e86f1266SJens Wiklander 		} else {
52*e86f1266SJens Wiklander 			ua[n].a = (uintptr_t)attrs[n].content.ref.buffer;
53*e86f1266SJens Wiklander 			ua[n].b = attrs[n].content.ref.length;
54*e86f1266SJens Wiklander 		}
55*e86f1266SJens Wiklander 	}
56*e86f1266SJens Wiklander }
57*e86f1266SJens 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 
69*e86f1266SJens 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 
88*e86f1266SJens 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 
109*e86f1266SJens 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 
123*e86f1266SJens 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;
139*e86f1266SJens Wiklander 	uint64_t sz;
140b0104773SPascal Brand 
141*e86f1266SJens 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 
151*e86f1266SJens Wiklander 	sz = *size;
152*e86f1266SJens Wiklander 	res = utee_cryp_obj_get_attr((unsigned long)object, attributeID,
153*e86f1266SJens Wiklander 				     buffer, &sz);
154*e86f1266SJens 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];
174*e86f1266SJens Wiklander 	uint64_t size = sizeof(buf);
175b0104773SPascal Brand 
176*e86f1266SJens 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 
186*e86f1266SJens Wiklander 	res = utee_cryp_obj_get_attr((unsigned long)object, attributeID, buf,
187*e86f1266SJens 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 
212*e86f1266SJens 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)
234*e86f1266SJens 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 
247*e86f1266SJens 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 
254*e86f1266SJens 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 
267*e86f1266SJens 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 
274*e86f1266SJens 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;
285*e86f1266SJens Wiklander 	struct utee_attribute ua[attrCount];
286b0104773SPascal Brand 
287*e86f1266SJens 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 
299*e86f1266SJens Wiklander 	__utee_from_attr(ua, attrs, attrCount);
300*e86f1266SJens 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 
342*e86f1266SJens 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 
358*e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)destObject, &dst_info);
359b0104773SPascal Brand 	if (res != TEE_SUCCESS)
360a2e9a830SCedric Chaumont 		goto exit;
361b0104773SPascal Brand 
362*e86f1266SJens 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 
375*e86f1266SJens Wiklander 	res = utee_cryp_obj_copy((unsigned long)destObject,
376*e86f1266SJens 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;
391*e86f1266SJens Wiklander 	struct utee_attribute ua[paramCount];
392b0104773SPascal Brand 
393*e86f1266SJens Wiklander 	__utee_from_attr(ua, params, paramCount);
394*e86f1266SJens Wiklander 	res = utee_cryp_obj_generate_key((unsigned long)object, keySize,
395*e86f1266SJens 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;
410*e86f1266SJens 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,
433*e86f1266SJens Wiklander 				     &obj);
434*e86f1266SJens Wiklander 	if (res == TEE_SUCCESS)
435*e86f1266SJens 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;
457*e86f1266SJens 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 
474aeb0d927SCedric Chaumont 	if (!object) {
47584431ae3SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
47684431ae3SCedric Chaumont 		goto err;
47784431ae3SCedric Chaumont 	}
47884431ae3SCedric Chaumont 
47984431ae3SCedric Chaumont 	res = utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
480*e86f1266SJens Wiklander 				      (unsigned long)attributes, initialData,
481*e86f1266SJens Wiklander 				      initialDataLen, &obj);
482*e86f1266SJens Wiklander 	if (res == TEE_SUCCESS) {
483*e86f1266SJens Wiklander 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
48484431ae3SCedric Chaumont 		goto out;
485*e86f1266SJens Wiklander 	}
48684431ae3SCedric Chaumont err:
48784431ae3SCedric Chaumont 	if (res == TEE_ERROR_ITEM_NOT_FOUND ||
48884431ae3SCedric Chaumont 	    res == TEE_ERROR_ACCESS_CONFLICT ||
48984431ae3SCedric Chaumont 	    res == TEE_ERROR_OUT_OF_MEMORY ||
49084431ae3SCedric Chaumont 	    res == TEE_ERROR_STORAGE_NO_SPACE ||
49184431ae3SCedric Chaumont 	    res == TEE_ERROR_CORRUPT_OBJECT ||
49284431ae3SCedric Chaumont 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE)
49384431ae3SCedric Chaumont 		return res;
49484431ae3SCedric Chaumont 	TEE_Panic(0);
49584431ae3SCedric Chaumont out:
49684431ae3SCedric Chaumont 	return TEE_SUCCESS;
497b0104773SPascal Brand }
498b0104773SPascal Brand 
4997583c59eSCedric Chaumont /*
5007583c59eSCedric Chaumont  * Use of this function is deprecated
5017583c59eSCedric Chaumont  * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead
5027583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
5037583c59eSCedric Chaumont  * this specification
5047583c59eSCedric Chaumont  */
505b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
506b0104773SPascal Brand {
507b0104773SPascal Brand 	TEE_Result res;
508b0104773SPascal Brand 
509b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
510b0104773SPascal Brand 		return;
511b0104773SPascal Brand 
5127583c59eSCedric Chaumont 	res = TEE_CloseAndDeletePersistentObject1(object);
513b0104773SPascal Brand 
514b0104773SPascal Brand 	if (res != TEE_SUCCESS)
515b0104773SPascal Brand 		TEE_Panic(0);
516b0104773SPascal Brand }
517b0104773SPascal Brand 
5187583c59eSCedric Chaumont TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object)
5197583c59eSCedric Chaumont {
5207583c59eSCedric Chaumont 	TEE_Result res;
5217583c59eSCedric Chaumont 
5227583c59eSCedric Chaumont 	if (object == TEE_HANDLE_NULL)
5237583c59eSCedric Chaumont 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
5247583c59eSCedric Chaumont 
525*e86f1266SJens Wiklander 	res = utee_storage_obj_del((unsigned long)object);
5267583c59eSCedric Chaumont 
5277583c59eSCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
5287583c59eSCedric Chaumont 		TEE_Panic(0);
5297583c59eSCedric Chaumont 
5307583c59eSCedric Chaumont 	return res;
5317583c59eSCedric Chaumont }
5327583c59eSCedric Chaumont 
5337583c59eSCedric Chaumont 
534b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
535b0104773SPascal Brand 				      const void *newObjectID,
53679a3c601SCedric Chaumont 				      uint32_t newObjectIDLen)
537b0104773SPascal Brand {
538b0104773SPascal Brand 	TEE_Result res;
539b0104773SPascal Brand 
540a76bf53fSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
541a76bf53fSCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
542a76bf53fSCedric Chaumont 		goto out;
543a76bf53fSCedric Chaumont 	}
544b0104773SPascal Brand 
545a76bf53fSCedric Chaumont 	if (!newObjectID) {
546a76bf53fSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
547a76bf53fSCedric Chaumont 		goto out;
548a76bf53fSCedric Chaumont 	}
549b0104773SPascal Brand 
550a76bf53fSCedric Chaumont 	if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN) {
551a76bf53fSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
552a76bf53fSCedric Chaumont 		goto out;
553a76bf53fSCedric Chaumont 	}
554b0104773SPascal Brand 
555*e86f1266SJens Wiklander 	res = utee_storage_obj_rename((unsigned long)object, newObjectID,
556*e86f1266SJens Wiklander 				      newObjectIDLen);
557b0104773SPascal Brand 
558a76bf53fSCedric Chaumont out:
559a76bf53fSCedric Chaumont 	if (res != TEE_SUCCESS &&
560a76bf53fSCedric Chaumont 	    res != TEE_ERROR_ACCESS_CONFLICT &&
561a76bf53fSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
562a76bf53fSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
563b0104773SPascal Brand 		TEE_Panic(0);
564b0104773SPascal Brand 
565b0104773SPascal Brand 	return res;
566b0104773SPascal Brand }
567b0104773SPascal Brand 
568b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
569b0104773SPascal Brand 						  objectEnumerator)
570b0104773SPascal Brand {
571b0104773SPascal Brand 	TEE_Result res;
572*e86f1266SJens Wiklander 	uint32_t oe;
573b0104773SPascal Brand 
57415cd3c30SCedric Chaumont 	if (!objectEnumerator)
575b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
576b0104773SPascal Brand 
577*e86f1266SJens Wiklander 	res = utee_storage_alloc_enum(&oe);
578b0104773SPascal Brand 
579b0104773SPascal Brand 	if (res != TEE_SUCCESS)
580*e86f1266SJens Wiklander 		oe = TEE_HANDLE_NULL;
581*e86f1266SJens Wiklander 
582*e86f1266SJens Wiklander 	*objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe;
583b0104773SPascal Brand 
58415cd3c30SCedric Chaumont 	if (res != TEE_SUCCESS &&
58515cd3c30SCedric Chaumont 	    res != TEE_ERROR_ACCESS_CONFLICT)
58615cd3c30SCedric Chaumont 		TEE_Panic(0);
58715cd3c30SCedric Chaumont 
588b0104773SPascal Brand 	return res;
589b0104773SPascal Brand }
590b0104773SPascal Brand 
591b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
592b0104773SPascal Brand {
593b0104773SPascal Brand 	TEE_Result res;
594b0104773SPascal Brand 
595b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
596b0104773SPascal Brand 		return;
597b0104773SPascal Brand 
598*e86f1266SJens Wiklander 	res = utee_storage_free_enum((unsigned long)objectEnumerator);
599b0104773SPascal Brand 
600b0104773SPascal Brand 	if (res != TEE_SUCCESS)
601b0104773SPascal Brand 		TEE_Panic(0);
602b0104773SPascal Brand }
603b0104773SPascal Brand 
604b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
605b0104773SPascal Brand {
606b0104773SPascal Brand 	TEE_Result res;
607b0104773SPascal Brand 
608b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
609b0104773SPascal Brand 		return;
610b0104773SPascal Brand 
611*e86f1266SJens Wiklander 	res = utee_storage_reset_enum((unsigned long)objectEnumerator);
612b0104773SPascal Brand 
613b0104773SPascal Brand 	if (res != TEE_SUCCESS)
614b0104773SPascal Brand 		TEE_Panic(0);
615b0104773SPascal Brand }
616b0104773SPascal Brand 
617b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
618b0104773SPascal Brand 					       objectEnumerator,
619b0104773SPascal Brand 					       uint32_t storageID)
620b0104773SPascal Brand {
621b0104773SPascal Brand 	TEE_Result res;
622b0104773SPascal Brand 
623b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
624b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
625b0104773SPascal Brand 
626*e86f1266SJens Wiklander 	res = utee_storage_start_enum((unsigned long)objectEnumerator,
627*e86f1266SJens Wiklander 				      storageID);
628b0104773SPascal Brand 
62915cd3c30SCedric Chaumont 	if (res != TEE_SUCCESS &&
63015cd3c30SCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
63115cd3c30SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
63215cd3c30SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
633b0104773SPascal Brand 		TEE_Panic(0);
634b0104773SPascal Brand 
635b0104773SPascal Brand 	return res;
636b0104773SPascal Brand }
637b0104773SPascal Brand 
638b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
639b0104773SPascal Brand 				       TEE_ObjectInfo *objectInfo,
64079a3c601SCedric Chaumont 				       void *objectID, uint32_t *objectIDLen)
641b0104773SPascal Brand {
642b0104773SPascal Brand 	TEE_Result res;
643*e86f1266SJens Wiklander 	uint64_t len;
644b0104773SPascal Brand 
64515cd3c30SCedric Chaumont 	if (!objectID) {
64615cd3c30SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
64715cd3c30SCedric Chaumont 		goto out;
64815cd3c30SCedric Chaumont 	}
64915cd3c30SCedric Chaumont 
65015cd3c30SCedric Chaumont 	if (!objectIDLen) {
65115cd3c30SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
65215cd3c30SCedric Chaumont 		goto out;
65315cd3c30SCedric Chaumont 	}
65415cd3c30SCedric Chaumont 
655*e86f1266SJens Wiklander 	len = *objectIDLen;
656*e86f1266SJens Wiklander 	res = utee_storage_next_enum((unsigned long)objectEnumerator,
657*e86f1266SJens Wiklander 				     objectInfo, objectID, &len);
658*e86f1266SJens Wiklander 	*objectIDLen = len;
659b0104773SPascal Brand 
66015cd3c30SCedric Chaumont out:
66115cd3c30SCedric Chaumont 	if (res != TEE_SUCCESS &&
66215cd3c30SCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
66315cd3c30SCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
66415cd3c30SCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
665b0104773SPascal Brand 		TEE_Panic(0);
666b0104773SPascal Brand 
667b0104773SPascal Brand 	return res;
668b0104773SPascal Brand }
669b0104773SPascal Brand 
670b0104773SPascal Brand /* Data and Key Storage API  - Data Stream Access Functions */
671b0104773SPascal Brand 
672b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
67379a3c601SCedric Chaumont 			      uint32_t size, uint32_t *count)
674b0104773SPascal Brand {
675b0104773SPascal Brand 	TEE_Result res;
676*e86f1266SJens Wiklander 	uint64_t cnt64;
677b0104773SPascal Brand 
678ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
679ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
680ae1289baSCedric Chaumont 		goto out;
681ae1289baSCedric Chaumont 	}
682b0104773SPascal Brand 
683*e86f1266SJens Wiklander 	cnt64 = *count;
684*e86f1266SJens Wiklander 	res = utee_storage_obj_read((unsigned long)object, buffer, size,
685*e86f1266SJens Wiklander 				    &cnt64);
686*e86f1266SJens Wiklander 	*count = cnt64;
687b0104773SPascal Brand 
688ae1289baSCedric Chaumont out:
689ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
690ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
691ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
692b0104773SPascal Brand 		TEE_Panic(0);
693b0104773SPascal Brand 
694b0104773SPascal Brand 	return res;
695b0104773SPascal Brand }
696b0104773SPascal Brand 
697b0104773SPascal Brand TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, void *buffer,
69879a3c601SCedric Chaumont 			       uint32_t size)
699b0104773SPascal Brand {
700b0104773SPascal Brand 	TEE_Result res;
701b0104773SPascal Brand 
702ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
703ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
704ae1289baSCedric Chaumont 		goto out;
705ae1289baSCedric Chaumont 	}
706ae1289baSCedric Chaumont 
707ae1289baSCedric Chaumont 	if (size > TEE_DATA_MAX_POSITION) {
708ae1289baSCedric Chaumont 		res = TEE_ERROR_OVERFLOW;
709ae1289baSCedric Chaumont 		goto out;
710ae1289baSCedric Chaumont 	}
711b0104773SPascal Brand 
712*e86f1266SJens Wiklander 	res = utee_storage_obj_write((unsigned long)object, buffer, size);
713b0104773SPascal Brand 
714ae1289baSCedric Chaumont out:
715ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
716ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
717ae1289baSCedric Chaumont 	    res != TEE_ERROR_OVERFLOW &&
718ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
719ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
720b0104773SPascal Brand 		TEE_Panic(0);
721b0104773SPascal Brand 
722b0104773SPascal Brand 	return res;
723b0104773SPascal Brand }
724b0104773SPascal Brand 
725b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
726b0104773SPascal Brand {
727b0104773SPascal Brand 	TEE_Result res;
728b0104773SPascal Brand 
729ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
730ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
731ae1289baSCedric Chaumont 		goto out;
732ae1289baSCedric Chaumont 	}
733b0104773SPascal Brand 
734*e86f1266SJens Wiklander 	res = utee_storage_obj_trunc((unsigned long)object, size);
735b0104773SPascal Brand 
736ae1289baSCedric Chaumont out:
737ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
738ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
739ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
740ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
741b0104773SPascal Brand 		TEE_Panic(0);
742b0104773SPascal Brand 
743b0104773SPascal Brand 	return res;
744b0104773SPascal Brand }
745b0104773SPascal Brand 
746b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
747b0104773SPascal Brand 			      TEE_Whence whence)
748b0104773SPascal Brand {
749b0104773SPascal Brand 	TEE_Result res;
750b0104773SPascal Brand 	TEE_ObjectInfo info;
751b0104773SPascal Brand 
752ae1289baSCedric Chaumont 	if (object == TEE_HANDLE_NULL) {
753ae1289baSCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
754ae1289baSCedric Chaumont 		goto out;
755ae1289baSCedric Chaumont 	}
756b0104773SPascal Brand 
757*e86f1266SJens Wiklander 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
758b0104773SPascal Brand 	if (res != TEE_SUCCESS)
759ae1289baSCedric Chaumont 		goto out;
760b0104773SPascal Brand 
761b0104773SPascal Brand 	switch (whence) {
762b0104773SPascal Brand 	case TEE_DATA_SEEK_SET:
763ae1289baSCedric Chaumont 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) {
764ae1289baSCedric Chaumont 			res = TEE_ERROR_OVERFLOW;
765ae1289baSCedric Chaumont 			goto out;
766ae1289baSCedric Chaumont 		}
767b0104773SPascal Brand 		break;
768b0104773SPascal Brand 	case TEE_DATA_SEEK_CUR:
769b0104773SPascal Brand 		if (offset > 0 &&
770b0104773SPascal Brand 		    ((uint32_t)offset + info.dataPosition >
771b0104773SPascal Brand 		     TEE_DATA_MAX_POSITION ||
772b0104773SPascal Brand 		     (uint32_t)offset + info.dataPosition <
773ae1289baSCedric Chaumont 		     info.dataPosition)) {
774ae1289baSCedric Chaumont 			res = TEE_ERROR_OVERFLOW;
775ae1289baSCedric Chaumont 			goto out;
776ae1289baSCedric Chaumont 		}
777b0104773SPascal Brand 		break;
778b0104773SPascal Brand 	case TEE_DATA_SEEK_END:
779b0104773SPascal Brand 		if (offset > 0 &&
780b0104773SPascal Brand 		    ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION ||
781ae1289baSCedric Chaumont 		     (uint32_t)offset + info.dataSize < info.dataSize)) {
782ae1289baSCedric Chaumont 			res = TEE_ERROR_OVERFLOW;
783ae1289baSCedric Chaumont 			goto out;
784ae1289baSCedric Chaumont 		}
785b0104773SPascal Brand 		break;
786b0104773SPascal Brand 	default:
787ae1289baSCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
788ae1289baSCedric Chaumont 		goto out;
789b0104773SPascal Brand 	}
790b0104773SPascal Brand 
791*e86f1266SJens Wiklander 	res = utee_storage_obj_seek((unsigned long)object, offset, whence);
792b0104773SPascal Brand 
793ae1289baSCedric Chaumont out:
794ae1289baSCedric Chaumont 	if (res != TEE_SUCCESS &&
795ae1289baSCedric Chaumont 	    res != TEE_ERROR_OVERFLOW &&
796ae1289baSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
797ae1289baSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
798b0104773SPascal Brand 		TEE_Panic(0);
799b0104773SPascal Brand 
800b0104773SPascal Brand 	return res;
801b0104773SPascal Brand }
802