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