xref: /optee_os/lib/libutee/tee_api_objects.c (revision 79a3c601fa657bb596483d8c215d659a6e9d0021)
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>
32b0104773SPascal Brand 
33b0104773SPascal Brand #include <assert.h>
34b0104773SPascal Brand 
35b0104773SPascal Brand #define TEE_USAGE_DEFAULT   0xffffffff
36b0104773SPascal Brand 
37b0104773SPascal Brand #define TEE_ATTR_BIT_VALUE                  (1 << 29)
38b0104773SPascal Brand #define TEE_ATTR_BIT_PROTECTED              (1 << 28)
39b0104773SPascal Brand 
40b0104773SPascal Brand /* Data and Key Storage API  - Generic Object Functions */
41b0104773SPascal Brand void TEE_GetObjectInfo(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo)
42b0104773SPascal Brand {
43b0104773SPascal Brand 	TEE_Result res;
44b0104773SPascal Brand 
45b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, objectInfo);
46b0104773SPascal Brand 	if (res != TEE_SUCCESS)
47b0104773SPascal Brand 		TEE_Panic(res);
48b0104773SPascal Brand }
49b0104773SPascal Brand 
50b0104773SPascal Brand void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage)
51b0104773SPascal Brand {
52b0104773SPascal Brand 	TEE_Result res;
53b0104773SPascal Brand 	res = utee_cryp_obj_restrict_usage((uint32_t)object, objectUsage);
54b0104773SPascal Brand 
55b0104773SPascal Brand 	if (res != TEE_SUCCESS)
56b0104773SPascal Brand 		TEE_Panic(0);
57b0104773SPascal Brand }
58b0104773SPascal Brand 
59b0104773SPascal Brand TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object,
60b0104773SPascal Brand 					uint32_t attributeID, void *buffer,
61*79a3c601SCedric Chaumont 					uint32_t *size)
62b0104773SPascal Brand {
63b0104773SPascal Brand 	TEE_Result res;
64b0104773SPascal Brand 	TEE_ObjectInfo info;
65b0104773SPascal Brand 
66b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
67b0104773SPascal Brand 	if (res != TEE_SUCCESS)
68b0104773SPascal Brand 		TEE_Panic(0);
69b0104773SPascal Brand 
70b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
71b0104773SPascal Brand 		TEE_Panic(0);
72b0104773SPascal Brand 
73b0104773SPascal Brand 	/* This function only supports reference attributes */
74b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) != 0)
75b0104773SPascal Brand 		TEE_Panic(0);
76b0104773SPascal Brand 
77b0104773SPascal Brand 	res =
78b0104773SPascal Brand 	    utee_cryp_obj_get_attr((uint32_t)object, attributeID, buffer,
79b0104773SPascal Brand 				   size);
80b0104773SPascal Brand 
81b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND &&
82b0104773SPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER)
83b0104773SPascal Brand 		TEE_Panic(0);
84b0104773SPascal Brand 
85b0104773SPascal Brand 	return res;
86b0104773SPascal Brand }
87b0104773SPascal Brand 
88b0104773SPascal Brand TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object,
89b0104773SPascal Brand 				       uint32_t attributeID, uint32_t *a,
90b0104773SPascal Brand 				       uint32_t *b)
91b0104773SPascal Brand {
92b0104773SPascal Brand 	TEE_Result res;
93b0104773SPascal Brand 	TEE_ObjectInfo info;
94b0104773SPascal Brand 	uint32_t buf[2];
95b0104773SPascal Brand 	size_t size = sizeof(buf);
96b0104773SPascal Brand 
97b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
98b0104773SPascal Brand 	if (res != TEE_SUCCESS)
99b0104773SPascal Brand 		TEE_Panic(0);
100b0104773SPascal Brand 
101b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
102b0104773SPascal Brand 		TEE_Panic(0);
103b0104773SPascal Brand 
104b0104773SPascal Brand 	/* This function only supports value attributes */
105b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) == 0)
106b0104773SPascal Brand 		TEE_Panic(0);
107b0104773SPascal Brand 
108b0104773SPascal Brand 	res =
109b0104773SPascal Brand 	    utee_cryp_obj_get_attr((uint32_t)object, attributeID, buf, &size);
110b0104773SPascal Brand 
111b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND &&
112b0104773SPascal Brand 	    res != TEE_ERROR_ACCESS_DENIED)
113b0104773SPascal Brand 		TEE_Panic(0);
114b0104773SPascal Brand 
115b0104773SPascal Brand 	if (size != sizeof(buf))
116b0104773SPascal Brand 		TEE_Panic(0);
117b0104773SPascal Brand 
118b0104773SPascal Brand 	*a = buf[0];
119b0104773SPascal Brand 	*b = buf[1];
120b0104773SPascal Brand 
121b0104773SPascal Brand 	return res;
122b0104773SPascal Brand }
123b0104773SPascal Brand 
124b0104773SPascal Brand void TEE_CloseObject(TEE_ObjectHandle object)
125b0104773SPascal Brand {
126b0104773SPascal Brand 	TEE_Result res;
127b0104773SPascal Brand 
128b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
129b0104773SPascal Brand 		return;
130b0104773SPascal Brand 
131b0104773SPascal Brand 	res = utee_cryp_obj_close((uint32_t)object);
132b0104773SPascal Brand 	if (res != TEE_SUCCESS)
133b0104773SPascal Brand 		TEE_Panic(0);
134b0104773SPascal Brand }
135b0104773SPascal Brand 
136b0104773SPascal Brand /* Data and Key Storage API  - Transient Object Functions */
137b0104773SPascal Brand 
138b0104773SPascal Brand TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType,
139*79a3c601SCedric Chaumont 				       uint32_t maxKeySize,
140b0104773SPascal Brand 				       TEE_ObjectHandle *object)
141b0104773SPascal Brand {
142b0104773SPascal Brand 	TEE_Result res;
143b0104773SPascal Brand 	uint32_t obj;
144b0104773SPascal Brand 
145*79a3c601SCedric Chaumont 	res = utee_cryp_obj_alloc(objectType, maxKeySize, &obj);
146b0104773SPascal Brand 	if (res == TEE_SUCCESS)
147b0104773SPascal Brand 		*object = (TEE_ObjectHandle) obj;
148b0104773SPascal Brand 	return res;
149b0104773SPascal Brand }
150b0104773SPascal Brand 
151b0104773SPascal Brand void TEE_FreeTransientObject(TEE_ObjectHandle object)
152b0104773SPascal Brand {
153b0104773SPascal Brand 	TEE_Result res;
154b0104773SPascal Brand 	TEE_ObjectInfo info;
155b0104773SPascal Brand 
156b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
157b0104773SPascal Brand 		return;
158b0104773SPascal Brand 
159b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
160b0104773SPascal Brand 	if (res != TEE_SUCCESS)
161b0104773SPascal Brand 		TEE_Panic(0);
162b0104773SPascal Brand 
163b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
164b0104773SPascal Brand 		TEE_Panic(0);
165b0104773SPascal Brand 
166b0104773SPascal Brand 	res = utee_cryp_obj_close((uint32_t)object);
167b0104773SPascal Brand 	if (res != TEE_SUCCESS)
168b0104773SPascal Brand 		TEE_Panic(0);
169b0104773SPascal Brand }
170b0104773SPascal Brand 
171b0104773SPascal Brand void TEE_ResetTransientObject(TEE_ObjectHandle object)
172b0104773SPascal Brand {
173b0104773SPascal Brand 	TEE_Result res;
174b0104773SPascal Brand 	TEE_ObjectInfo info;
175b0104773SPascal Brand 
176b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
177b0104773SPascal Brand 		return;
178b0104773SPascal Brand 
179b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
180b0104773SPascal Brand 	if (res != TEE_SUCCESS)
181b0104773SPascal Brand 		TEE_Panic(0);
182b0104773SPascal Brand 
183b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
184b0104773SPascal Brand 		TEE_Panic(0);
185b0104773SPascal Brand 
186b0104773SPascal Brand 	res = utee_cryp_obj_reset((uint32_t)object);
187b0104773SPascal Brand 	if (res != TEE_SUCCESS)
188b0104773SPascal Brand 		TEE_Panic(0);
189b0104773SPascal Brand }
190b0104773SPascal Brand 
191b0104773SPascal Brand TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object,
192b0104773SPascal Brand 				       TEE_Attribute *attrs,
193b0104773SPascal Brand 				       uint32_t attrCount)
194b0104773SPascal Brand {
195b0104773SPascal Brand 	TEE_Result res;
196b0104773SPascal Brand 	TEE_ObjectInfo info;
197b0104773SPascal Brand 
198b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
199b0104773SPascal Brand 	if (res != TEE_SUCCESS)
200b0104773SPascal Brand 		TEE_Panic(0);
201b0104773SPascal Brand 
202b0104773SPascal Brand 	/* Must be a transient object */
203b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
204b0104773SPascal Brand 		TEE_Panic(0);
205b0104773SPascal Brand 
206b0104773SPascal Brand 	/* Must not be initialized already */
207b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
208b0104773SPascal Brand 		TEE_Panic(0);
209b0104773SPascal Brand 
210b0104773SPascal Brand 	res = utee_cryp_obj_populate((uint32_t)object, attrs, attrCount);
211b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
212b0104773SPascal Brand 		TEE_Panic(res);
213b0104773SPascal Brand 	return res;
214b0104773SPascal Brand }
215b0104773SPascal Brand 
216b0104773SPascal Brand void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID,
217*79a3c601SCedric Chaumont 			  void *buffer, uint32_t length)
218b0104773SPascal Brand {
219b0104773SPascal Brand 	if (attr == NULL)
220b0104773SPascal Brand 		TEE_Panic(0);
221b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) != 0)
222b0104773SPascal Brand 		TEE_Panic(0);
223b0104773SPascal Brand 	attr->attributeID = attributeID;
224b0104773SPascal Brand 	attr->content.ref.buffer = buffer;
225b0104773SPascal Brand 	attr->content.ref.length = length;
226b0104773SPascal Brand }
227b0104773SPascal Brand 
228b0104773SPascal Brand void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID,
229b0104773SPascal Brand 			    uint32_t a, uint32_t b)
230b0104773SPascal Brand {
231b0104773SPascal Brand 	if (attr == NULL)
232b0104773SPascal Brand 		TEE_Panic(0);
233b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) == 0)
234b0104773SPascal Brand 		TEE_Panic(0);
235b0104773SPascal Brand 	attr->attributeID = attributeID;
236b0104773SPascal Brand 	attr->content.value.a = a;
237b0104773SPascal Brand 	attr->content.value.b = b;
238b0104773SPascal Brand }
239b0104773SPascal Brand 
240b0104773SPascal Brand void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject,
241b0104773SPascal Brand 			      TEE_ObjectHandle srcObject)
242b0104773SPascal Brand {
243b0104773SPascal Brand 	TEE_Result res;
244b0104773SPascal Brand 	TEE_ObjectInfo dst_info;
245b0104773SPascal Brand 	TEE_ObjectInfo src_info;
246b0104773SPascal Brand 
247b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)destObject, &dst_info);
248b0104773SPascal Brand 	if (res != TEE_SUCCESS)
249b0104773SPascal Brand 		TEE_Panic(0);
250b0104773SPascal Brand 
251b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)srcObject, &src_info);
252b0104773SPascal Brand 	if (res != TEE_SUCCESS)
253b0104773SPascal Brand 		TEE_Panic(0);
254b0104773SPascal Brand 
255b0104773SPascal Brand 	if ((src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
256b0104773SPascal Brand 		TEE_Panic(0);
257b0104773SPascal Brand 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
258b0104773SPascal Brand 		TEE_Panic(0);
259b0104773SPascal Brand 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
260b0104773SPascal Brand 		TEE_Panic(0);
261b0104773SPascal Brand 
262b0104773SPascal Brand 	res = utee_cryp_obj_copy((uint32_t)destObject, (uint32_t)srcObject);
263b0104773SPascal Brand 	if (res != TEE_SUCCESS)
264b0104773SPascal Brand 		TEE_Panic(0);
265b0104773SPascal Brand }
266b0104773SPascal Brand 
267b0104773SPascal Brand TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
268b0104773SPascal Brand 			   TEE_Attribute *params, uint32_t paramCount)
269b0104773SPascal Brand {
270b0104773SPascal Brand 	TEE_Result res;
271b0104773SPascal Brand 
272b0104773SPascal Brand 	res = utee_cryp_obj_generate_key((uint32_t)object, keySize,
273b0104773SPascal Brand 					 params, paramCount);
274b0104773SPascal Brand 
275b0104773SPascal Brand 	if (res != TEE_SUCCESS)
276b0104773SPascal Brand 		TEE_Panic(0);
277b0104773SPascal Brand 
278b0104773SPascal Brand 	return res;
279b0104773SPascal Brand }
280b0104773SPascal Brand 
281b0104773SPascal Brand /* Data and Key Storage API  - Persistent Object Functions */
282b0104773SPascal Brand 
283b0104773SPascal Brand TEE_Result TEE_OpenPersistentObject(uint32_t storageID, void *objectID,
284*79a3c601SCedric Chaumont 				    uint32_t objectIDLen, uint32_t flags,
285b0104773SPascal Brand 				    TEE_ObjectHandle *object)
286b0104773SPascal Brand {
287b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
288b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
289b0104773SPascal Brand 
290b0104773SPascal Brand 	if (objectID == NULL)
291b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
292b0104773SPascal Brand 
293b0104773SPascal Brand 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN)
294b0104773SPascal Brand 		TEE_Panic(0);
295b0104773SPascal Brand 
296b0104773SPascal Brand 	if (object == NULL)
297b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
298b0104773SPascal Brand 
299b0104773SPascal Brand 	return utee_storage_obj_open(storageID, objectID, objectIDLen, flags,
300b0104773SPascal Brand 				     object);
301b0104773SPascal Brand }
302b0104773SPascal Brand 
303b0104773SPascal Brand TEE_Result TEE_CreatePersistentObject(uint32_t storageID, void *objectID,
304*79a3c601SCedric Chaumont 				      uint32_t objectIDLen, uint32_t flags,
305b0104773SPascal Brand 				      TEE_ObjectHandle attributes,
306b0104773SPascal Brand 				      const void *initialData,
307*79a3c601SCedric Chaumont 				      uint32_t initialDataLen,
308b0104773SPascal Brand 				      TEE_ObjectHandle *object)
309b0104773SPascal Brand {
310b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
311b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
312b0104773SPascal Brand 
313b0104773SPascal Brand 	if (objectID == NULL)
314b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
315b0104773SPascal Brand 
316b0104773SPascal Brand 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN)
317b0104773SPascal Brand 		TEE_Panic(0);
318b0104773SPascal Brand 
319b0104773SPascal Brand 	if (object == NULL)
320b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
321b0104773SPascal Brand 
322b0104773SPascal Brand 	return utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
323b0104773SPascal Brand 				       attributes, initialData, initialDataLen,
324b0104773SPascal Brand 				       object);
325b0104773SPascal Brand }
326b0104773SPascal Brand 
327b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
328b0104773SPascal Brand {
329b0104773SPascal Brand 	TEE_Result res;
330b0104773SPascal Brand 
331b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
332b0104773SPascal Brand 		return;
333b0104773SPascal Brand 
334b0104773SPascal Brand 	res = utee_storage_obj_del(object);
335b0104773SPascal Brand 
336b0104773SPascal Brand 	if (res != TEE_SUCCESS)
337b0104773SPascal Brand 		TEE_Panic(0);
338b0104773SPascal Brand }
339b0104773SPascal Brand 
340b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
341b0104773SPascal Brand 				      const void *newObjectID,
342*79a3c601SCedric Chaumont 				      uint32_t newObjectIDLen)
343b0104773SPascal Brand {
344b0104773SPascal Brand 	TEE_Result res;
345b0104773SPascal Brand 
346b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
347b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
348b0104773SPascal Brand 
349b0104773SPascal Brand 	if (newObjectID == NULL)
350b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
351b0104773SPascal Brand 
352b0104773SPascal Brand 	if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN)
353b0104773SPascal Brand 		TEE_Panic(0);
354b0104773SPascal Brand 
355b0104773SPascal Brand 	res = utee_storage_obj_rename(object, newObjectID, newObjectIDLen);
356b0104773SPascal Brand 
357b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ACCESS_CONFLICT)
358b0104773SPascal Brand 		TEE_Panic(0);
359b0104773SPascal Brand 
360b0104773SPascal Brand 	return res;
361b0104773SPascal Brand }
362b0104773SPascal Brand 
363b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
364b0104773SPascal Brand 						  objectEnumerator)
365b0104773SPascal Brand {
366b0104773SPascal Brand 	TEE_Result res;
367b0104773SPascal Brand 
368b0104773SPascal Brand 	if (objectEnumerator == NULL)
369b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
370b0104773SPascal Brand 
371b0104773SPascal Brand 	res = utee_storage_alloc_enum(objectEnumerator);
372b0104773SPascal Brand 
373b0104773SPascal Brand 	if (res != TEE_SUCCESS)
374b0104773SPascal Brand 		*objectEnumerator = TEE_HANDLE_NULL;
375b0104773SPascal Brand 
376b0104773SPascal Brand 	return res;
377b0104773SPascal Brand }
378b0104773SPascal Brand 
379b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
380b0104773SPascal Brand {
381b0104773SPascal Brand 	TEE_Result res;
382b0104773SPascal Brand 
383b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
384b0104773SPascal Brand 		return;
385b0104773SPascal Brand 
386b0104773SPascal Brand 	res = utee_storage_free_enum(objectEnumerator);
387b0104773SPascal Brand 
388b0104773SPascal Brand 	if (res != TEE_SUCCESS)
389b0104773SPascal Brand 		TEE_Panic(0);
390b0104773SPascal Brand }
391b0104773SPascal Brand 
392b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
393b0104773SPascal Brand {
394b0104773SPascal Brand 	TEE_Result res;
395b0104773SPascal Brand 
396b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
397b0104773SPascal Brand 		return;
398b0104773SPascal Brand 
399b0104773SPascal Brand 	res = utee_storage_reset_enum(objectEnumerator);
400b0104773SPascal Brand 
401b0104773SPascal Brand 	if (res != TEE_SUCCESS)
402b0104773SPascal Brand 		TEE_Panic(0);
403b0104773SPascal Brand }
404b0104773SPascal Brand 
405b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
406b0104773SPascal Brand 					       objectEnumerator,
407b0104773SPascal Brand 					       uint32_t storageID)
408b0104773SPascal Brand {
409b0104773SPascal Brand 	TEE_Result res;
410b0104773SPascal Brand 
411b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
412b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
413b0104773SPascal Brand 
414b0104773SPascal Brand 	res = utee_storage_start_enum(objectEnumerator, storageID);
415b0104773SPascal Brand 
416b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND)
417b0104773SPascal Brand 		TEE_Panic(0);
418b0104773SPascal Brand 
419b0104773SPascal Brand 	return res;
420b0104773SPascal Brand }
421b0104773SPascal Brand 
422b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
423b0104773SPascal Brand 				       TEE_ObjectInfo *objectInfo,
424*79a3c601SCedric Chaumont 				       void *objectID, uint32_t *objectIDLen)
425b0104773SPascal Brand {
426b0104773SPascal Brand 	TEE_Result res;
427b0104773SPascal Brand 
428b0104773SPascal Brand 	res =
429b0104773SPascal Brand 	    utee_storage_next_enum(objectEnumerator, objectInfo, objectID,
430b0104773SPascal Brand 				   objectIDLen);
431b0104773SPascal Brand 
432b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND)
433b0104773SPascal Brand 		TEE_Panic(0);
434b0104773SPascal Brand 
435b0104773SPascal Brand 	return res;
436b0104773SPascal Brand }
437b0104773SPascal Brand 
438b0104773SPascal Brand /* Data and Key Storage API  - Data Stream Access Functions */
439b0104773SPascal Brand 
440b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
441*79a3c601SCedric Chaumont 			      uint32_t size, uint32_t *count)
442b0104773SPascal Brand {
443b0104773SPascal Brand 	TEE_Result res;
444b0104773SPascal Brand 
445b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
446b0104773SPascal Brand 		TEE_Panic(0);
447b0104773SPascal Brand 
448b0104773SPascal Brand 	res = utee_storage_obj_read(object, buffer, size, count);
449b0104773SPascal Brand 
450b0104773SPascal Brand 	if (res != TEE_SUCCESS)
451b0104773SPascal Brand 		TEE_Panic(0);
452b0104773SPascal Brand 
453b0104773SPascal Brand 	return res;
454b0104773SPascal Brand }
455b0104773SPascal Brand 
456b0104773SPascal Brand TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, void *buffer,
457*79a3c601SCedric Chaumont 			       uint32_t size)
458b0104773SPascal Brand {
459b0104773SPascal Brand 	TEE_Result res;
460b0104773SPascal Brand 
461b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
462b0104773SPascal Brand 		TEE_Panic(0);
463b0104773SPascal Brand 
464b0104773SPascal Brand 	res = utee_storage_obj_write(object, buffer, size);
465b0104773SPascal Brand 
466b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NO_SPACE)
467b0104773SPascal Brand 		TEE_Panic(0);
468b0104773SPascal Brand 
469b0104773SPascal Brand 	return res;
470b0104773SPascal Brand }
471b0104773SPascal Brand 
472b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
473b0104773SPascal Brand {
474b0104773SPascal Brand 	TEE_Result res;
475b0104773SPascal Brand 
476b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
477b0104773SPascal Brand 		TEE_Panic(0);
478b0104773SPascal Brand 
479b0104773SPascal Brand 	res = utee_storage_obj_trunc(object, size);
480b0104773SPascal Brand 
481b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NO_SPACE)
482b0104773SPascal Brand 		TEE_Panic(0);
483b0104773SPascal Brand 
484b0104773SPascal Brand 	return res;
485b0104773SPascal Brand }
486b0104773SPascal Brand 
487b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
488b0104773SPascal Brand 			      TEE_Whence whence)
489b0104773SPascal Brand {
490b0104773SPascal Brand 	TEE_Result res;
491b0104773SPascal Brand 	TEE_ObjectInfo info;
492b0104773SPascal Brand 
493b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
494b0104773SPascal Brand 		TEE_Panic(0);
495b0104773SPascal Brand 
496b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
497b0104773SPascal Brand 	if (res != TEE_SUCCESS)
498b0104773SPascal Brand 		TEE_Panic(0);
499b0104773SPascal Brand 
500b0104773SPascal Brand 	switch (whence) {
501b0104773SPascal Brand 	case TEE_DATA_SEEK_SET:
502b0104773SPascal Brand 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION)
503b0104773SPascal Brand 			return TEE_ERROR_OVERFLOW;
504b0104773SPascal Brand 		break;
505b0104773SPascal Brand 	case TEE_DATA_SEEK_CUR:
506b0104773SPascal Brand 		if (offset > 0 &&
507b0104773SPascal Brand 		    ((uint32_t)offset + info.dataPosition >
508b0104773SPascal Brand 		     TEE_DATA_MAX_POSITION ||
509b0104773SPascal Brand 		     (uint32_t)offset + info.dataPosition <
510b0104773SPascal Brand 		     info.dataPosition))
511b0104773SPascal Brand 			return TEE_ERROR_OVERFLOW;
512b0104773SPascal Brand 		break;
513b0104773SPascal Brand 	case TEE_DATA_SEEK_END:
514b0104773SPascal Brand 		if (offset > 0 &&
515b0104773SPascal Brand 		    ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION ||
516b0104773SPascal Brand 		     (uint32_t)offset + info.dataSize < info.dataSize))
517b0104773SPascal Brand 			return TEE_ERROR_OVERFLOW;
518b0104773SPascal Brand 		break;
519b0104773SPascal Brand 	default:
520b0104773SPascal Brand 		TEE_Panic(0);
521b0104773SPascal Brand 	}
522b0104773SPascal Brand 
523b0104773SPascal Brand 	res = utee_storage_obj_seek(object, offset, whence);
524b0104773SPascal Brand 
525b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_OVERFLOW)
526b0104773SPascal Brand 		TEE_Panic(0);
527b0104773SPascal Brand 
528b0104773SPascal Brand 	return res;
529b0104773SPascal Brand }
530