xref: /optee_os/lib/libutee/tee_api_objects.c (revision 7583c59eb7d76afc7c36830b3b7034d44d43bb9e)
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 */
41*7583c59eSCedric Chaumont /*
42*7583c59eSCedric Chaumont  * Use of this function is deprecated
43*7583c59eSCedric Chaumont  * new code SHOULD use the TEE_GetObjectInfo1 function instead
44*7583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
45*7583c59eSCedric Chaumont  * this specification
46*7583c59eSCedric Chaumont  */
47b0104773SPascal Brand void TEE_GetObjectInfo(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo)
48b0104773SPascal Brand {
49b0104773SPascal Brand 	TEE_Result res;
50b0104773SPascal Brand 
51b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, objectInfo);
52*7583c59eSCedric Chaumont 
53b0104773SPascal Brand 	if (res != TEE_SUCCESS)
54b0104773SPascal Brand 		TEE_Panic(res);
55*7583c59eSCedric Chaumont 
56*7583c59eSCedric Chaumont 	if (objectInfo->objectType == TEE_TYPE_CORRUPTED_OBJECT) {
57*7583c59eSCedric Chaumont 		objectInfo->keySize = 0;
58*7583c59eSCedric Chaumont 		objectInfo->maxKeySize = 0;
59*7583c59eSCedric Chaumont 		objectInfo->objectUsage = 0;
60*7583c59eSCedric Chaumont 		objectInfo->dataSize = 0;
61*7583c59eSCedric Chaumont 		objectInfo->dataPosition = 0;
62*7583c59eSCedric Chaumont 		objectInfo->handleFlags = 0;
63*7583c59eSCedric Chaumont 	}
64b0104773SPascal Brand }
65b0104773SPascal Brand 
66*7583c59eSCedric Chaumont TEE_Result TEE_GetObjectInfo1(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo)
67*7583c59eSCedric Chaumont {
68*7583c59eSCedric Chaumont 	TEE_Result res;
69*7583c59eSCedric Chaumont 
70*7583c59eSCedric Chaumont 	res = utee_cryp_obj_get_info((uint32_t)object, objectInfo);
71*7583c59eSCedric Chaumont 
72*7583c59eSCedric Chaumont 	if (res == TEE_ERROR_CORRUPT_OBJECT) {
73*7583c59eSCedric Chaumont 		res = utee_storage_obj_del(object);
74*7583c59eSCedric Chaumont 		if (res != TEE_SUCCESS)
75*7583c59eSCedric Chaumont 			TEE_Panic(0);
76*7583c59eSCedric Chaumont 		return TEE_ERROR_CORRUPT_OBJECT;
77*7583c59eSCedric Chaumont 	}
78*7583c59eSCedric Chaumont 
79*7583c59eSCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
80*7583c59eSCedric Chaumont 		TEE_Panic(res);
81*7583c59eSCedric Chaumont 
82*7583c59eSCedric Chaumont 	return res;
83*7583c59eSCedric Chaumont }
84*7583c59eSCedric Chaumont 
85*7583c59eSCedric Chaumont /*
86*7583c59eSCedric Chaumont  * Use of this function is deprecated
87*7583c59eSCedric Chaumont  * new code SHOULD use the TEE_RestrictObjectUsage1 function instead
88*7583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
89*7583c59eSCedric Chaumont  * this specification
90*7583c59eSCedric Chaumont  */
91b0104773SPascal Brand void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage)
92b0104773SPascal Brand {
93b0104773SPascal Brand 	TEE_Result res;
94*7583c59eSCedric Chaumont 	TEE_ObjectInfo objectInfo;
95*7583c59eSCedric Chaumont 
96*7583c59eSCedric Chaumont 	res = utee_cryp_obj_get_info((uint32_t)object, &objectInfo);
97*7583c59eSCedric Chaumont 	if (objectInfo.objectType == TEE_TYPE_CORRUPTED_OBJECT)
98*7583c59eSCedric Chaumont 		return;
99*7583c59eSCedric Chaumont 
100*7583c59eSCedric Chaumont 	res = TEE_RestrictObjectUsage1(object, objectUsage);
101b0104773SPascal Brand 
102b0104773SPascal Brand 	if (res != TEE_SUCCESS)
103b0104773SPascal Brand 		TEE_Panic(0);
104b0104773SPascal Brand }
105b0104773SPascal Brand 
106*7583c59eSCedric Chaumont TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage)
107*7583c59eSCedric Chaumont {
108*7583c59eSCedric Chaumont 	TEE_Result res;
109*7583c59eSCedric Chaumont 
110*7583c59eSCedric Chaumont 	res = utee_cryp_obj_restrict_usage((uint32_t)object, objectUsage);
111*7583c59eSCedric Chaumont 
112*7583c59eSCedric Chaumont 	if (res == TEE_ERROR_CORRUPT_OBJECT) {
113*7583c59eSCedric Chaumont 		res = utee_storage_obj_del(object);
114*7583c59eSCedric Chaumont 		if (res != TEE_SUCCESS)
115*7583c59eSCedric Chaumont 			TEE_Panic(0);
116*7583c59eSCedric Chaumont 		return TEE_ERROR_CORRUPT_OBJECT;
117*7583c59eSCedric Chaumont 	}
118*7583c59eSCedric Chaumont 
119*7583c59eSCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
120*7583c59eSCedric Chaumont 		TEE_Panic(0);
121*7583c59eSCedric Chaumont 
122*7583c59eSCedric Chaumont 	return res;
123*7583c59eSCedric Chaumont }
124*7583c59eSCedric Chaumont 
125b0104773SPascal Brand TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object,
126b0104773SPascal Brand 					uint32_t attributeID, void *buffer,
12779a3c601SCedric Chaumont 					uint32_t *size)
128b0104773SPascal Brand {
129b0104773SPascal Brand 	TEE_Result res;
130b0104773SPascal Brand 	TEE_ObjectInfo info;
131b0104773SPascal Brand 
132b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
133b0104773SPascal Brand 	if (res != TEE_SUCCESS)
134b0104773SPascal Brand 		TEE_Panic(0);
135b0104773SPascal Brand 
136b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
137b0104773SPascal Brand 		TEE_Panic(0);
138b0104773SPascal Brand 
139b0104773SPascal Brand 	/* This function only supports reference attributes */
140b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) != 0)
141b0104773SPascal Brand 		TEE_Panic(0);
142b0104773SPascal Brand 
143b0104773SPascal Brand 	res =
144b0104773SPascal Brand 	    utee_cryp_obj_get_attr((uint32_t)object, attributeID, buffer,
145b0104773SPascal Brand 				   size);
146b0104773SPascal Brand 
147b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND &&
148b0104773SPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER)
149b0104773SPascal Brand 		TEE_Panic(0);
150b0104773SPascal Brand 
151b0104773SPascal Brand 	return res;
152b0104773SPascal Brand }
153b0104773SPascal Brand 
154b0104773SPascal Brand TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object,
155b0104773SPascal Brand 				       uint32_t attributeID, uint32_t *a,
156b0104773SPascal Brand 				       uint32_t *b)
157b0104773SPascal Brand {
158b0104773SPascal Brand 	TEE_Result res;
159b0104773SPascal Brand 	TEE_ObjectInfo info;
160b0104773SPascal Brand 	uint32_t buf[2];
1617f74c64aSPascal Brand 	uint32_t size = sizeof(buf);
162b0104773SPascal Brand 
163b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
164b0104773SPascal Brand 	if (res != TEE_SUCCESS)
165b0104773SPascal Brand 		TEE_Panic(0);
166b0104773SPascal Brand 
167b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
168b0104773SPascal Brand 		TEE_Panic(0);
169b0104773SPascal Brand 
170b0104773SPascal Brand 	/* This function only supports value attributes */
171b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) == 0)
172b0104773SPascal Brand 		TEE_Panic(0);
173b0104773SPascal Brand 
174b0104773SPascal Brand 	res =
175b0104773SPascal Brand 	    utee_cryp_obj_get_attr((uint32_t)object, attributeID, buf, &size);
176b0104773SPascal Brand 
177b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND &&
178b0104773SPascal Brand 	    res != TEE_ERROR_ACCESS_DENIED)
179b0104773SPascal Brand 		TEE_Panic(0);
180b0104773SPascal Brand 
181b0104773SPascal Brand 	if (size != sizeof(buf))
182b0104773SPascal Brand 		TEE_Panic(0);
183b0104773SPascal Brand 
184b0104773SPascal Brand 	*a = buf[0];
185b0104773SPascal Brand 	*b = buf[1];
186b0104773SPascal Brand 
187b0104773SPascal Brand 	return res;
188b0104773SPascal Brand }
189b0104773SPascal Brand 
190b0104773SPascal Brand void TEE_CloseObject(TEE_ObjectHandle object)
191b0104773SPascal Brand {
192b0104773SPascal Brand 	TEE_Result res;
193b0104773SPascal Brand 
194b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
195b0104773SPascal Brand 		return;
196b0104773SPascal Brand 
197b0104773SPascal Brand 	res = utee_cryp_obj_close((uint32_t)object);
198b0104773SPascal Brand 	if (res != TEE_SUCCESS)
199b0104773SPascal Brand 		TEE_Panic(0);
200b0104773SPascal Brand }
201b0104773SPascal Brand 
202b0104773SPascal Brand /* Data and Key Storage API  - Transient Object Functions */
203b0104773SPascal Brand 
204b0104773SPascal Brand TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType,
20579a3c601SCedric Chaumont 				       uint32_t maxKeySize,
206b0104773SPascal Brand 				       TEE_ObjectHandle *object)
207b0104773SPascal Brand {
208b0104773SPascal Brand 	TEE_Result res;
209b0104773SPascal Brand 	uint32_t obj;
210b0104773SPascal Brand 
21179a3c601SCedric Chaumont 	res = utee_cryp_obj_alloc(objectType, maxKeySize, &obj);
212b0104773SPascal Brand 	if (res == TEE_SUCCESS)
213b0104773SPascal Brand 		*object = (TEE_ObjectHandle) obj;
214b0104773SPascal Brand 	return res;
215b0104773SPascal Brand }
216b0104773SPascal Brand 
217b0104773SPascal Brand void TEE_FreeTransientObject(TEE_ObjectHandle object)
218b0104773SPascal Brand {
219b0104773SPascal Brand 	TEE_Result res;
220b0104773SPascal Brand 	TEE_ObjectInfo info;
221b0104773SPascal Brand 
222b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
223b0104773SPascal Brand 		return;
224b0104773SPascal Brand 
225b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
226b0104773SPascal Brand 	if (res != TEE_SUCCESS)
227b0104773SPascal Brand 		TEE_Panic(0);
228b0104773SPascal Brand 
229b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
230b0104773SPascal Brand 		TEE_Panic(0);
231b0104773SPascal Brand 
232b0104773SPascal Brand 	res = utee_cryp_obj_close((uint32_t)object);
233b0104773SPascal Brand 	if (res != TEE_SUCCESS)
234b0104773SPascal Brand 		TEE_Panic(0);
235b0104773SPascal Brand }
236b0104773SPascal Brand 
237b0104773SPascal Brand void TEE_ResetTransientObject(TEE_ObjectHandle object)
238b0104773SPascal Brand {
239b0104773SPascal Brand 	TEE_Result res;
240b0104773SPascal Brand 	TEE_ObjectInfo info;
241b0104773SPascal Brand 
242b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
243b0104773SPascal Brand 		return;
244b0104773SPascal Brand 
245b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
246b0104773SPascal Brand 	if (res != TEE_SUCCESS)
247b0104773SPascal Brand 		TEE_Panic(0);
248b0104773SPascal Brand 
249b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
250b0104773SPascal Brand 		TEE_Panic(0);
251b0104773SPascal Brand 
252b0104773SPascal Brand 	res = utee_cryp_obj_reset((uint32_t)object);
253b0104773SPascal Brand 	if (res != TEE_SUCCESS)
254b0104773SPascal Brand 		TEE_Panic(0);
255b0104773SPascal Brand }
256b0104773SPascal Brand 
257b0104773SPascal Brand TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object,
258b0104773SPascal Brand 				       TEE_Attribute *attrs,
259b0104773SPascal Brand 				       uint32_t attrCount)
260b0104773SPascal Brand {
261b0104773SPascal Brand 	TEE_Result res;
262b0104773SPascal Brand 	TEE_ObjectInfo info;
263b0104773SPascal Brand 
264b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
265b0104773SPascal Brand 	if (res != TEE_SUCCESS)
266b0104773SPascal Brand 		TEE_Panic(0);
267b0104773SPascal Brand 
268b0104773SPascal Brand 	/* Must be a transient object */
269b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
270b0104773SPascal Brand 		TEE_Panic(0);
271b0104773SPascal Brand 
272b0104773SPascal Brand 	/* Must not be initialized already */
273b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
274b0104773SPascal Brand 		TEE_Panic(0);
275b0104773SPascal Brand 
276b0104773SPascal Brand 	res = utee_cryp_obj_populate((uint32_t)object, attrs, attrCount);
277b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
278b0104773SPascal Brand 		TEE_Panic(res);
279b0104773SPascal Brand 	return res;
280b0104773SPascal Brand }
281b0104773SPascal Brand 
282b0104773SPascal Brand void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID,
28379a3c601SCedric Chaumont 			  void *buffer, uint32_t length)
284b0104773SPascal Brand {
285b0104773SPascal Brand 	if (attr == NULL)
286b0104773SPascal Brand 		TEE_Panic(0);
287b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) != 0)
288b0104773SPascal Brand 		TEE_Panic(0);
289b0104773SPascal Brand 	attr->attributeID = attributeID;
290b0104773SPascal Brand 	attr->content.ref.buffer = buffer;
291b0104773SPascal Brand 	attr->content.ref.length = length;
292b0104773SPascal Brand }
293b0104773SPascal Brand 
294b0104773SPascal Brand void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID,
295b0104773SPascal Brand 			    uint32_t a, uint32_t b)
296b0104773SPascal Brand {
297b0104773SPascal Brand 	if (attr == NULL)
298b0104773SPascal Brand 		TEE_Panic(0);
299b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) == 0)
300b0104773SPascal Brand 		TEE_Panic(0);
301b0104773SPascal Brand 	attr->attributeID = attributeID;
302b0104773SPascal Brand 	attr->content.value.a = a;
303b0104773SPascal Brand 	attr->content.value.b = b;
304b0104773SPascal Brand }
305b0104773SPascal Brand 
306*7583c59eSCedric Chaumont /*
307*7583c59eSCedric Chaumont  * Use of this function is deprecated
308*7583c59eSCedric Chaumont  * new code SHOULD use the TEE_CopyObjectAttributes1 function instead
309*7583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
310*7583c59eSCedric Chaumont  * this specification
311*7583c59eSCedric Chaumont  */
312b0104773SPascal Brand void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject,
313b0104773SPascal Brand 			      TEE_ObjectHandle srcObject)
314b0104773SPascal Brand {
315b0104773SPascal Brand 	TEE_Result res;
316*7583c59eSCedric Chaumont 	TEE_ObjectInfo src_info;
317*7583c59eSCedric Chaumont 
318*7583c59eSCedric Chaumont 	res = utee_cryp_obj_get_info((uint32_t)srcObject, &src_info);
319*7583c59eSCedric Chaumont 	if (src_info.objectType == TEE_TYPE_CORRUPTED_OBJECT)
320*7583c59eSCedric Chaumont 		return;
321*7583c59eSCedric Chaumont 
322*7583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(destObject, srcObject);
323*7583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
324*7583c59eSCedric Chaumont 		TEE_Panic(0);
325*7583c59eSCedric Chaumont }
326*7583c59eSCedric Chaumont 
327*7583c59eSCedric Chaumont TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject,
328*7583c59eSCedric Chaumont 			      TEE_ObjectHandle srcObject)
329*7583c59eSCedric Chaumont {
330*7583c59eSCedric Chaumont 	TEE_Result res;
331b0104773SPascal Brand 	TEE_ObjectInfo dst_info;
332b0104773SPascal Brand 	TEE_ObjectInfo src_info;
333b0104773SPascal Brand 
334b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)destObject, &dst_info);
335b0104773SPascal Brand 	if (res != TEE_SUCCESS)
336*7583c59eSCedric Chaumont 		goto err;
337b0104773SPascal Brand 
338b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)srcObject, &src_info);
339b0104773SPascal Brand 	if (res != TEE_SUCCESS)
340*7583c59eSCedric Chaumont 		goto err;
341b0104773SPascal Brand 
342b0104773SPascal Brand 	if ((src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
343b0104773SPascal Brand 		TEE_Panic(0);
344b0104773SPascal Brand 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
345b0104773SPascal Brand 		TEE_Panic(0);
346b0104773SPascal Brand 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
347b0104773SPascal Brand 		TEE_Panic(0);
348b0104773SPascal Brand 
349b0104773SPascal Brand 	res = utee_cryp_obj_copy((uint32_t)destObject, (uint32_t)srcObject);
350b0104773SPascal Brand 	if (res != TEE_SUCCESS)
351b0104773SPascal Brand 		TEE_Panic(0);
352*7583c59eSCedric Chaumont 
353*7583c59eSCedric Chaumont 	goto out;
354*7583c59eSCedric Chaumont 
355*7583c59eSCedric Chaumont err:
356*7583c59eSCedric Chaumont 	if (res == TEE_ERROR_CORRUPT_OBJECT) {
357*7583c59eSCedric Chaumont 		res = utee_storage_obj_del(srcObject);
358*7583c59eSCedric Chaumont 		if (res != TEE_SUCCESS)
359*7583c59eSCedric Chaumont 			TEE_Panic(0);
360*7583c59eSCedric Chaumont 		return TEE_ERROR_CORRUPT_OBJECT;
361*7583c59eSCedric Chaumont 	}
362*7583c59eSCedric Chaumont 	if (res == TEE_ERROR_STORAGE_NOT_AVAILABLE)
363*7583c59eSCedric Chaumont 		return res;
364*7583c59eSCedric Chaumont 	TEE_Panic(0);
365*7583c59eSCedric Chaumont out:
366*7583c59eSCedric Chaumont 	return TEE_SUCCESS;
367b0104773SPascal Brand }
368b0104773SPascal Brand 
369b0104773SPascal Brand TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
370b0104773SPascal Brand 			   TEE_Attribute *params, uint32_t paramCount)
371b0104773SPascal Brand {
372b0104773SPascal Brand 	TEE_Result res;
373b0104773SPascal Brand 
374b0104773SPascal Brand 	res = utee_cryp_obj_generate_key((uint32_t)object, keySize,
375b0104773SPascal Brand 					 params, paramCount);
376b0104773SPascal Brand 
377b0104773SPascal Brand 	if (res != TEE_SUCCESS)
378b0104773SPascal Brand 		TEE_Panic(0);
379b0104773SPascal Brand 
380b0104773SPascal Brand 	return res;
381b0104773SPascal Brand }
382b0104773SPascal Brand 
383b0104773SPascal Brand /* Data and Key Storage API  - Persistent Object Functions */
384b0104773SPascal Brand 
385b0104773SPascal Brand TEE_Result TEE_OpenPersistentObject(uint32_t storageID, void *objectID,
38679a3c601SCedric Chaumont 				    uint32_t objectIDLen, uint32_t flags,
387b0104773SPascal Brand 				    TEE_ObjectHandle *object)
388b0104773SPascal Brand {
389b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
390b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
391b0104773SPascal Brand 
392b0104773SPascal Brand 	if (objectID == NULL)
393b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
394b0104773SPascal Brand 
395b0104773SPascal Brand 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN)
396b0104773SPascal Brand 		TEE_Panic(0);
397b0104773SPascal Brand 
398b0104773SPascal Brand 	if (object == NULL)
399b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
400b0104773SPascal Brand 
401b0104773SPascal Brand 	return utee_storage_obj_open(storageID, objectID, objectIDLen, flags,
402b0104773SPascal Brand 				     object);
403b0104773SPascal Brand }
404b0104773SPascal Brand 
405b0104773SPascal Brand TEE_Result TEE_CreatePersistentObject(uint32_t storageID, void *objectID,
40679a3c601SCedric Chaumont 				      uint32_t objectIDLen, uint32_t flags,
407b0104773SPascal Brand 				      TEE_ObjectHandle attributes,
408b0104773SPascal Brand 				      const void *initialData,
40979a3c601SCedric Chaumont 				      uint32_t initialDataLen,
410b0104773SPascal Brand 				      TEE_ObjectHandle *object)
411b0104773SPascal Brand {
412b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
413b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
414b0104773SPascal Brand 
415b0104773SPascal Brand 	if (objectID == NULL)
416b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
417b0104773SPascal Brand 
418b0104773SPascal Brand 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN)
419b0104773SPascal Brand 		TEE_Panic(0);
420b0104773SPascal Brand 
421b0104773SPascal Brand 	if (object == NULL)
422b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
423b0104773SPascal Brand 
424b0104773SPascal Brand 	return utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
425b0104773SPascal Brand 				       attributes, initialData, initialDataLen,
426b0104773SPascal Brand 				       object);
427b0104773SPascal Brand }
428b0104773SPascal Brand 
429*7583c59eSCedric Chaumont /*
430*7583c59eSCedric Chaumont  * Use of this function is deprecated
431*7583c59eSCedric Chaumont  * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead
432*7583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
433*7583c59eSCedric Chaumont  * this specification
434*7583c59eSCedric Chaumont  */
435b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
436b0104773SPascal Brand {
437b0104773SPascal Brand 	TEE_Result res;
438b0104773SPascal Brand 
439b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
440b0104773SPascal Brand 		return;
441b0104773SPascal Brand 
442*7583c59eSCedric Chaumont 	res = TEE_CloseAndDeletePersistentObject1(object);
443b0104773SPascal Brand 
444b0104773SPascal Brand 	if (res != TEE_SUCCESS)
445b0104773SPascal Brand 		TEE_Panic(0);
446b0104773SPascal Brand }
447b0104773SPascal Brand 
448*7583c59eSCedric Chaumont TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object)
449*7583c59eSCedric Chaumont {
450*7583c59eSCedric Chaumont 	TEE_Result res;
451*7583c59eSCedric Chaumont 
452*7583c59eSCedric Chaumont 	if (object == TEE_HANDLE_NULL)
453*7583c59eSCedric Chaumont 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
454*7583c59eSCedric Chaumont 
455*7583c59eSCedric Chaumont 	res = utee_storage_obj_del(object);
456*7583c59eSCedric Chaumont 
457*7583c59eSCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
458*7583c59eSCedric Chaumont 		TEE_Panic(0);
459*7583c59eSCedric Chaumont 
460*7583c59eSCedric Chaumont 	return res;
461*7583c59eSCedric Chaumont }
462*7583c59eSCedric Chaumont 
463*7583c59eSCedric Chaumont 
464b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
465b0104773SPascal Brand 				      const void *newObjectID,
46679a3c601SCedric Chaumont 				      uint32_t newObjectIDLen)
467b0104773SPascal Brand {
468b0104773SPascal Brand 	TEE_Result res;
469b0104773SPascal Brand 
470b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
471b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
472b0104773SPascal Brand 
473b0104773SPascal Brand 	if (newObjectID == NULL)
474b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
475b0104773SPascal Brand 
476b0104773SPascal Brand 	if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN)
477b0104773SPascal Brand 		TEE_Panic(0);
478b0104773SPascal Brand 
479b0104773SPascal Brand 	res = utee_storage_obj_rename(object, newObjectID, newObjectIDLen);
480b0104773SPascal Brand 
481b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ACCESS_CONFLICT)
482b0104773SPascal Brand 		TEE_Panic(0);
483b0104773SPascal Brand 
484b0104773SPascal Brand 	return res;
485b0104773SPascal Brand }
486b0104773SPascal Brand 
487b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
488b0104773SPascal Brand 						  objectEnumerator)
489b0104773SPascal Brand {
490b0104773SPascal Brand 	TEE_Result res;
491b0104773SPascal Brand 
492b0104773SPascal Brand 	if (objectEnumerator == NULL)
493b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
494b0104773SPascal Brand 
495b0104773SPascal Brand 	res = utee_storage_alloc_enum(objectEnumerator);
496b0104773SPascal Brand 
497b0104773SPascal Brand 	if (res != TEE_SUCCESS)
498b0104773SPascal Brand 		*objectEnumerator = TEE_HANDLE_NULL;
499b0104773SPascal Brand 
500b0104773SPascal Brand 	return res;
501b0104773SPascal Brand }
502b0104773SPascal Brand 
503b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
504b0104773SPascal Brand {
505b0104773SPascal Brand 	TEE_Result res;
506b0104773SPascal Brand 
507b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
508b0104773SPascal Brand 		return;
509b0104773SPascal Brand 
510b0104773SPascal Brand 	res = utee_storage_free_enum(objectEnumerator);
511b0104773SPascal Brand 
512b0104773SPascal Brand 	if (res != TEE_SUCCESS)
513b0104773SPascal Brand 		TEE_Panic(0);
514b0104773SPascal Brand }
515b0104773SPascal Brand 
516b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
517b0104773SPascal Brand {
518b0104773SPascal Brand 	TEE_Result res;
519b0104773SPascal Brand 
520b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
521b0104773SPascal Brand 		return;
522b0104773SPascal Brand 
523b0104773SPascal Brand 	res = utee_storage_reset_enum(objectEnumerator);
524b0104773SPascal Brand 
525b0104773SPascal Brand 	if (res != TEE_SUCCESS)
526b0104773SPascal Brand 		TEE_Panic(0);
527b0104773SPascal Brand }
528b0104773SPascal Brand 
529b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
530b0104773SPascal Brand 					       objectEnumerator,
531b0104773SPascal Brand 					       uint32_t storageID)
532b0104773SPascal Brand {
533b0104773SPascal Brand 	TEE_Result res;
534b0104773SPascal Brand 
535b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
536b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
537b0104773SPascal Brand 
538b0104773SPascal Brand 	res = utee_storage_start_enum(objectEnumerator, storageID);
539b0104773SPascal Brand 
540b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND)
541b0104773SPascal Brand 		TEE_Panic(0);
542b0104773SPascal Brand 
543b0104773SPascal Brand 	return res;
544b0104773SPascal Brand }
545b0104773SPascal Brand 
546b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
547b0104773SPascal Brand 				       TEE_ObjectInfo *objectInfo,
54879a3c601SCedric Chaumont 				       void *objectID, uint32_t *objectIDLen)
549b0104773SPascal Brand {
550b0104773SPascal Brand 	TEE_Result res;
551b0104773SPascal Brand 
552b0104773SPascal Brand 	res =
553b0104773SPascal Brand 	    utee_storage_next_enum(objectEnumerator, objectInfo, objectID,
554b0104773SPascal Brand 				   objectIDLen);
555b0104773SPascal Brand 
556b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND)
557b0104773SPascal Brand 		TEE_Panic(0);
558b0104773SPascal Brand 
559b0104773SPascal Brand 	return res;
560b0104773SPascal Brand }
561b0104773SPascal Brand 
562b0104773SPascal Brand /* Data and Key Storage API  - Data Stream Access Functions */
563b0104773SPascal Brand 
564b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
56579a3c601SCedric Chaumont 			      uint32_t size, uint32_t *count)
566b0104773SPascal Brand {
567b0104773SPascal Brand 	TEE_Result res;
568b0104773SPascal Brand 
569b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
570b0104773SPascal Brand 		TEE_Panic(0);
571b0104773SPascal Brand 
572b0104773SPascal Brand 	res = utee_storage_obj_read(object, buffer, size, count);
573b0104773SPascal Brand 
574b0104773SPascal Brand 	if (res != TEE_SUCCESS)
575b0104773SPascal Brand 		TEE_Panic(0);
576b0104773SPascal Brand 
577b0104773SPascal Brand 	return res;
578b0104773SPascal Brand }
579b0104773SPascal Brand 
580b0104773SPascal Brand TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, void *buffer,
58179a3c601SCedric Chaumont 			       uint32_t size)
582b0104773SPascal Brand {
583b0104773SPascal Brand 	TEE_Result res;
584b0104773SPascal Brand 
585b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
586b0104773SPascal Brand 		TEE_Panic(0);
587b0104773SPascal Brand 
588b0104773SPascal Brand 	res = utee_storage_obj_write(object, buffer, size);
589b0104773SPascal Brand 
590b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NO_SPACE)
591b0104773SPascal Brand 		TEE_Panic(0);
592b0104773SPascal Brand 
593b0104773SPascal Brand 	return res;
594b0104773SPascal Brand }
595b0104773SPascal Brand 
596b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
597b0104773SPascal Brand {
598b0104773SPascal Brand 	TEE_Result res;
599b0104773SPascal Brand 
600b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
601b0104773SPascal Brand 		TEE_Panic(0);
602b0104773SPascal Brand 
603b0104773SPascal Brand 	res = utee_storage_obj_trunc(object, size);
604b0104773SPascal Brand 
605b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NO_SPACE)
606b0104773SPascal Brand 		TEE_Panic(0);
607b0104773SPascal Brand 
608b0104773SPascal Brand 	return res;
609b0104773SPascal Brand }
610b0104773SPascal Brand 
611b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
612b0104773SPascal Brand 			      TEE_Whence whence)
613b0104773SPascal Brand {
614b0104773SPascal Brand 	TEE_Result res;
615b0104773SPascal Brand 	TEE_ObjectInfo info;
616b0104773SPascal Brand 
617b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
618b0104773SPascal Brand 		TEE_Panic(0);
619b0104773SPascal Brand 
620b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
621b0104773SPascal Brand 	if (res != TEE_SUCCESS)
622b0104773SPascal Brand 		TEE_Panic(0);
623b0104773SPascal Brand 
624b0104773SPascal Brand 	switch (whence) {
625b0104773SPascal Brand 	case TEE_DATA_SEEK_SET:
626b0104773SPascal Brand 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION)
627b0104773SPascal Brand 			return TEE_ERROR_OVERFLOW;
628b0104773SPascal Brand 		break;
629b0104773SPascal Brand 	case TEE_DATA_SEEK_CUR:
630b0104773SPascal Brand 		if (offset > 0 &&
631b0104773SPascal Brand 		    ((uint32_t)offset + info.dataPosition >
632b0104773SPascal Brand 		     TEE_DATA_MAX_POSITION ||
633b0104773SPascal Brand 		     (uint32_t)offset + info.dataPosition <
634b0104773SPascal Brand 		     info.dataPosition))
635b0104773SPascal Brand 			return TEE_ERROR_OVERFLOW;
636b0104773SPascal Brand 		break;
637b0104773SPascal Brand 	case TEE_DATA_SEEK_END:
638b0104773SPascal Brand 		if (offset > 0 &&
639b0104773SPascal Brand 		    ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION ||
640b0104773SPascal Brand 		     (uint32_t)offset + info.dataSize < info.dataSize))
641b0104773SPascal Brand 			return TEE_ERROR_OVERFLOW;
642b0104773SPascal Brand 		break;
643b0104773SPascal Brand 	default:
644b0104773SPascal Brand 		TEE_Panic(0);
645b0104773SPascal Brand 	}
646b0104773SPascal Brand 
647b0104773SPascal Brand 	res = utee_storage_obj_seek(object, offset, whence);
648b0104773SPascal Brand 
649b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_OVERFLOW)
650b0104773SPascal Brand 		TEE_Panic(0);
651b0104773SPascal Brand 
652b0104773SPascal Brand 	return res;
653b0104773SPascal Brand }
654