xref: /optee_os/lib/libutee/tee_api_objects.c (revision 0ed6a6cae32ff852c94cd8480b7012f678be67cf)
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 */
417583c59eSCedric Chaumont /*
427583c59eSCedric Chaumont  * Use of this function is deprecated
437583c59eSCedric Chaumont  * new code SHOULD use the TEE_GetObjectInfo1 function instead
447583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
457583c59eSCedric Chaumont  * this specification
467583c59eSCedric 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);
527583c59eSCedric Chaumont 
53b0104773SPascal Brand 	if (res != TEE_SUCCESS)
54b0104773SPascal Brand 		TEE_Panic(res);
557583c59eSCedric Chaumont 
567583c59eSCedric Chaumont 	if (objectInfo->objectType == TEE_TYPE_CORRUPTED_OBJECT) {
577583c59eSCedric Chaumont 		objectInfo->keySize = 0;
587583c59eSCedric Chaumont 		objectInfo->maxKeySize = 0;
597583c59eSCedric Chaumont 		objectInfo->objectUsage = 0;
607583c59eSCedric Chaumont 		objectInfo->dataSize = 0;
617583c59eSCedric Chaumont 		objectInfo->dataPosition = 0;
627583c59eSCedric Chaumont 		objectInfo->handleFlags = 0;
637583c59eSCedric Chaumont 	}
64b0104773SPascal Brand }
65b0104773SPascal Brand 
667583c59eSCedric Chaumont TEE_Result TEE_GetObjectInfo1(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo)
677583c59eSCedric Chaumont {
687583c59eSCedric Chaumont 	TEE_Result res;
697583c59eSCedric Chaumont 
707583c59eSCedric Chaumont 	res = utee_cryp_obj_get_info((uint32_t)object, objectInfo);
717583c59eSCedric Chaumont 
727583c59eSCedric Chaumont 	if (res == TEE_ERROR_CORRUPT_OBJECT) {
737583c59eSCedric Chaumont 		res = utee_storage_obj_del(object);
747583c59eSCedric Chaumont 		if (res != TEE_SUCCESS)
757583c59eSCedric Chaumont 			TEE_Panic(0);
767583c59eSCedric Chaumont 		return TEE_ERROR_CORRUPT_OBJECT;
777583c59eSCedric Chaumont 	}
787583c59eSCedric Chaumont 
797583c59eSCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
807583c59eSCedric Chaumont 		TEE_Panic(res);
817583c59eSCedric Chaumont 
827583c59eSCedric Chaumont 	return res;
837583c59eSCedric Chaumont }
847583c59eSCedric Chaumont 
857583c59eSCedric Chaumont /*
867583c59eSCedric Chaumont  * Use of this function is deprecated
877583c59eSCedric Chaumont  * new code SHOULD use the TEE_RestrictObjectUsage1 function instead
887583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
897583c59eSCedric Chaumont  * this specification
907583c59eSCedric Chaumont  */
91b0104773SPascal Brand void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage)
92b0104773SPascal Brand {
93b0104773SPascal Brand 	TEE_Result res;
947583c59eSCedric Chaumont 	TEE_ObjectInfo objectInfo;
957583c59eSCedric Chaumont 
967583c59eSCedric Chaumont 	res = utee_cryp_obj_get_info((uint32_t)object, &objectInfo);
977583c59eSCedric Chaumont 	if (objectInfo.objectType == TEE_TYPE_CORRUPTED_OBJECT)
987583c59eSCedric Chaumont 		return;
997583c59eSCedric Chaumont 
1007583c59eSCedric Chaumont 	res = TEE_RestrictObjectUsage1(object, objectUsage);
101b0104773SPascal Brand 
102b0104773SPascal Brand 	if (res != TEE_SUCCESS)
103b0104773SPascal Brand 		TEE_Panic(0);
104b0104773SPascal Brand }
105b0104773SPascal Brand 
1067583c59eSCedric Chaumont TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage)
1077583c59eSCedric Chaumont {
1087583c59eSCedric Chaumont 	TEE_Result res;
1097583c59eSCedric Chaumont 
1107583c59eSCedric Chaumont 	res = utee_cryp_obj_restrict_usage((uint32_t)object, objectUsage);
1117583c59eSCedric Chaumont 
1127583c59eSCedric Chaumont 	if (res == TEE_ERROR_CORRUPT_OBJECT) {
1137583c59eSCedric Chaumont 		res = utee_storage_obj_del(object);
1147583c59eSCedric Chaumont 		if (res != TEE_SUCCESS)
1157583c59eSCedric Chaumont 			TEE_Panic(0);
1167583c59eSCedric Chaumont 		return TEE_ERROR_CORRUPT_OBJECT;
1177583c59eSCedric Chaumont 	}
1187583c59eSCedric Chaumont 
1197583c59eSCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
1207583c59eSCedric Chaumont 		TEE_Panic(0);
1217583c59eSCedric Chaumont 
1227583c59eSCedric Chaumont 	return res;
1237583c59eSCedric Chaumont }
1247583c59eSCedric 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 
143*0ed6a6caSCedric Chaumont 	res = utee_cryp_obj_get_attr((uint32_t)object,
144*0ed6a6caSCedric Chaumont 				     attributeID, buffer, size);
145b0104773SPascal Brand 
146*0ed6a6caSCedric Chaumont 	if (res != TEE_SUCCESS &&
147*0ed6a6caSCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
148*0ed6a6caSCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER &&
149*0ed6a6caSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
150*0ed6a6caSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
151b0104773SPascal Brand 		TEE_Panic(0);
152b0104773SPascal Brand 
153b0104773SPascal Brand 	return res;
154b0104773SPascal Brand }
155b0104773SPascal Brand 
156b0104773SPascal Brand TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object,
157b0104773SPascal Brand 				       uint32_t attributeID, uint32_t *a,
158b0104773SPascal Brand 				       uint32_t *b)
159b0104773SPascal Brand {
160b0104773SPascal Brand 	TEE_Result res;
161b0104773SPascal Brand 	TEE_ObjectInfo info;
162b0104773SPascal Brand 	uint32_t buf[2];
1637f74c64aSPascal Brand 	uint32_t size = sizeof(buf);
164b0104773SPascal Brand 
165b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
166b0104773SPascal Brand 	if (res != TEE_SUCCESS)
167b0104773SPascal Brand 		TEE_Panic(0);
168b0104773SPascal Brand 
169b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
170b0104773SPascal Brand 		TEE_Panic(0);
171b0104773SPascal Brand 
172b0104773SPascal Brand 	/* This function only supports value attributes */
173b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) == 0)
174b0104773SPascal Brand 		TEE_Panic(0);
175b0104773SPascal Brand 
176*0ed6a6caSCedric Chaumont 	res = utee_cryp_obj_get_attr((uint32_t)object,
177*0ed6a6caSCedric Chaumont 				     attributeID, buf, &size);
178b0104773SPascal Brand 
179*0ed6a6caSCedric Chaumont 	if (res != TEE_SUCCESS &&
180*0ed6a6caSCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
181*0ed6a6caSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
182*0ed6a6caSCedric Chaumont 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
183b0104773SPascal Brand 		TEE_Panic(0);
184b0104773SPascal Brand 
185b0104773SPascal Brand 	if (size != sizeof(buf))
186b0104773SPascal Brand 		TEE_Panic(0);
187b0104773SPascal Brand 
188b0104773SPascal Brand 	*a = buf[0];
189b0104773SPascal Brand 	*b = buf[1];
190b0104773SPascal Brand 
191b0104773SPascal Brand 	return res;
192b0104773SPascal Brand }
193b0104773SPascal Brand 
194b0104773SPascal Brand void TEE_CloseObject(TEE_ObjectHandle object)
195b0104773SPascal Brand {
196b0104773SPascal Brand 	TEE_Result res;
197b0104773SPascal Brand 
198b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
199b0104773SPascal Brand 		return;
200b0104773SPascal Brand 
201b0104773SPascal Brand 	res = utee_cryp_obj_close((uint32_t)object);
202b0104773SPascal Brand 	if (res != TEE_SUCCESS)
203b0104773SPascal Brand 		TEE_Panic(0);
204b0104773SPascal Brand }
205b0104773SPascal Brand 
206b0104773SPascal Brand /* Data and Key Storage API  - Transient Object Functions */
207b0104773SPascal Brand 
208b0104773SPascal Brand TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType,
20979a3c601SCedric Chaumont 				       uint32_t maxKeySize,
210b0104773SPascal Brand 				       TEE_ObjectHandle *object)
211b0104773SPascal Brand {
212b0104773SPascal Brand 	TEE_Result res;
213b0104773SPascal Brand 	uint32_t obj;
214b0104773SPascal Brand 
21579a3c601SCedric Chaumont 	res = utee_cryp_obj_alloc(objectType, maxKeySize, &obj);
216b0104773SPascal Brand 	if (res == TEE_SUCCESS)
217b0104773SPascal Brand 		*object = (TEE_ObjectHandle) obj;
218*0ed6a6caSCedric Chaumont 
219b0104773SPascal Brand 	return res;
220b0104773SPascal Brand }
221b0104773SPascal Brand 
222b0104773SPascal Brand void TEE_FreeTransientObject(TEE_ObjectHandle object)
223b0104773SPascal Brand {
224b0104773SPascal Brand 	TEE_Result res;
225b0104773SPascal Brand 	TEE_ObjectInfo info;
226b0104773SPascal Brand 
227b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
228b0104773SPascal Brand 		return;
229b0104773SPascal Brand 
230b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
231b0104773SPascal Brand 	if (res != TEE_SUCCESS)
232b0104773SPascal Brand 		TEE_Panic(0);
233b0104773SPascal Brand 
234b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
235b0104773SPascal Brand 		TEE_Panic(0);
236b0104773SPascal Brand 
237b0104773SPascal Brand 	res = utee_cryp_obj_close((uint32_t)object);
238b0104773SPascal Brand 	if (res != TEE_SUCCESS)
239b0104773SPascal Brand 		TEE_Panic(0);
240b0104773SPascal Brand }
241b0104773SPascal Brand 
242b0104773SPascal Brand void TEE_ResetTransientObject(TEE_ObjectHandle object)
243b0104773SPascal Brand {
244b0104773SPascal Brand 	TEE_Result res;
245b0104773SPascal Brand 	TEE_ObjectInfo info;
246b0104773SPascal Brand 
247b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
248b0104773SPascal Brand 		return;
249b0104773SPascal Brand 
250b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
251b0104773SPascal Brand 	if (res != TEE_SUCCESS)
252b0104773SPascal Brand 		TEE_Panic(0);
253b0104773SPascal Brand 
254b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
255b0104773SPascal Brand 		TEE_Panic(0);
256b0104773SPascal Brand 
257b0104773SPascal Brand 	res = utee_cryp_obj_reset((uint32_t)object);
258b0104773SPascal Brand 	if (res != TEE_SUCCESS)
259b0104773SPascal Brand 		TEE_Panic(0);
260b0104773SPascal Brand }
261b0104773SPascal Brand 
262b0104773SPascal Brand TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object,
263b0104773SPascal Brand 				       TEE_Attribute *attrs,
264b0104773SPascal Brand 				       uint32_t attrCount)
265b0104773SPascal Brand {
266b0104773SPascal Brand 	TEE_Result res;
267b0104773SPascal Brand 	TEE_ObjectInfo info;
268b0104773SPascal Brand 
269b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
270b0104773SPascal Brand 	if (res != TEE_SUCCESS)
271b0104773SPascal Brand 		TEE_Panic(0);
272b0104773SPascal Brand 
273b0104773SPascal Brand 	/* Must be a transient object */
274b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
275b0104773SPascal Brand 		TEE_Panic(0);
276b0104773SPascal Brand 
277b0104773SPascal Brand 	/* Must not be initialized already */
278b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
279b0104773SPascal Brand 		TEE_Panic(0);
280b0104773SPascal Brand 
281b0104773SPascal Brand 	res = utee_cryp_obj_populate((uint32_t)object, attrs, attrCount);
282b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
283b0104773SPascal Brand 		TEE_Panic(res);
284b0104773SPascal Brand 	return res;
285b0104773SPascal Brand }
286b0104773SPascal Brand 
287b0104773SPascal Brand void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID,
28879a3c601SCedric Chaumont 			  void *buffer, uint32_t length)
289b0104773SPascal Brand {
290b0104773SPascal Brand 	if (attr == NULL)
291b0104773SPascal Brand 		TEE_Panic(0);
292b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) != 0)
293b0104773SPascal Brand 		TEE_Panic(0);
294b0104773SPascal Brand 	attr->attributeID = attributeID;
295b0104773SPascal Brand 	attr->content.ref.buffer = buffer;
296b0104773SPascal Brand 	attr->content.ref.length = length;
297b0104773SPascal Brand }
298b0104773SPascal Brand 
299b0104773SPascal Brand void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID,
300b0104773SPascal Brand 			    uint32_t a, uint32_t b)
301b0104773SPascal Brand {
302b0104773SPascal Brand 	if (attr == NULL)
303b0104773SPascal Brand 		TEE_Panic(0);
304b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) == 0)
305b0104773SPascal Brand 		TEE_Panic(0);
306b0104773SPascal Brand 	attr->attributeID = attributeID;
307b0104773SPascal Brand 	attr->content.value.a = a;
308b0104773SPascal Brand 	attr->content.value.b = b;
309b0104773SPascal Brand }
310b0104773SPascal Brand 
3117583c59eSCedric Chaumont /*
3127583c59eSCedric Chaumont  * Use of this function is deprecated
3137583c59eSCedric Chaumont  * new code SHOULD use the TEE_CopyObjectAttributes1 function instead
3147583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
3157583c59eSCedric Chaumont  * this specification
3167583c59eSCedric Chaumont  */
317b0104773SPascal Brand void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject,
318b0104773SPascal Brand 			      TEE_ObjectHandle srcObject)
319b0104773SPascal Brand {
320b0104773SPascal Brand 	TEE_Result res;
3217583c59eSCedric Chaumont 	TEE_ObjectInfo src_info;
3227583c59eSCedric Chaumont 
3237583c59eSCedric Chaumont 	res = utee_cryp_obj_get_info((uint32_t)srcObject, &src_info);
3247583c59eSCedric Chaumont 	if (src_info.objectType == TEE_TYPE_CORRUPTED_OBJECT)
3257583c59eSCedric Chaumont 		return;
3267583c59eSCedric Chaumont 
3277583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(destObject, srcObject);
3287583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
3297583c59eSCedric Chaumont 		TEE_Panic(0);
3307583c59eSCedric Chaumont }
3317583c59eSCedric Chaumont 
3327583c59eSCedric Chaumont TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject,
3337583c59eSCedric Chaumont 			      TEE_ObjectHandle srcObject)
3347583c59eSCedric Chaumont {
3357583c59eSCedric Chaumont 	TEE_Result res;
336b0104773SPascal Brand 	TEE_ObjectInfo dst_info;
337b0104773SPascal Brand 	TEE_ObjectInfo src_info;
338b0104773SPascal Brand 
339b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)destObject, &dst_info);
340b0104773SPascal Brand 	if (res != TEE_SUCCESS)
3417583c59eSCedric Chaumont 		goto err;
342b0104773SPascal Brand 
343b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)srcObject, &src_info);
344b0104773SPascal Brand 	if (res != TEE_SUCCESS)
3457583c59eSCedric Chaumont 		goto err;
346b0104773SPascal Brand 
347b0104773SPascal Brand 	if ((src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
348b0104773SPascal Brand 		TEE_Panic(0);
349b0104773SPascal Brand 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
350b0104773SPascal Brand 		TEE_Panic(0);
351b0104773SPascal Brand 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
352b0104773SPascal Brand 		TEE_Panic(0);
353b0104773SPascal Brand 
354b0104773SPascal Brand 	res = utee_cryp_obj_copy((uint32_t)destObject, (uint32_t)srcObject);
355b0104773SPascal Brand 	if (res != TEE_SUCCESS)
356b0104773SPascal Brand 		TEE_Panic(0);
3577583c59eSCedric Chaumont 
3587583c59eSCedric Chaumont 	goto out;
3597583c59eSCedric Chaumont 
3607583c59eSCedric Chaumont err:
3617583c59eSCedric Chaumont 	if (res == TEE_ERROR_CORRUPT_OBJECT) {
3627583c59eSCedric Chaumont 		res = utee_storage_obj_del(srcObject);
3637583c59eSCedric Chaumont 		if (res != TEE_SUCCESS)
3647583c59eSCedric Chaumont 			TEE_Panic(0);
3657583c59eSCedric Chaumont 		return TEE_ERROR_CORRUPT_OBJECT;
3667583c59eSCedric Chaumont 	}
3677583c59eSCedric Chaumont 	if (res == TEE_ERROR_STORAGE_NOT_AVAILABLE)
3687583c59eSCedric Chaumont 		return res;
3697583c59eSCedric Chaumont 	TEE_Panic(0);
3707583c59eSCedric Chaumont out:
3717583c59eSCedric Chaumont 	return TEE_SUCCESS;
372b0104773SPascal Brand }
373b0104773SPascal Brand 
374b0104773SPascal Brand TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
375b0104773SPascal Brand 			   TEE_Attribute *params, uint32_t paramCount)
376b0104773SPascal Brand {
377b0104773SPascal Brand 	TEE_Result res;
378b0104773SPascal Brand 
379b0104773SPascal Brand 	res = utee_cryp_obj_generate_key((uint32_t)object, keySize,
380b0104773SPascal Brand 					 params, paramCount);
381b0104773SPascal Brand 
382b0104773SPascal Brand 	if (res != TEE_SUCCESS)
383b0104773SPascal Brand 		TEE_Panic(0);
384b0104773SPascal Brand 
385b0104773SPascal Brand 	return res;
386b0104773SPascal Brand }
387b0104773SPascal Brand 
388b0104773SPascal Brand /* Data and Key Storage API  - Persistent Object Functions */
389b0104773SPascal Brand 
390b0104773SPascal Brand TEE_Result TEE_OpenPersistentObject(uint32_t storageID, void *objectID,
39179a3c601SCedric Chaumont 				    uint32_t objectIDLen, uint32_t flags,
392b0104773SPascal Brand 				    TEE_ObjectHandle *object)
393b0104773SPascal Brand {
394b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
395b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
396b0104773SPascal Brand 
397b0104773SPascal Brand 	if (objectID == NULL)
398b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
399b0104773SPascal Brand 
400b0104773SPascal Brand 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN)
401b0104773SPascal Brand 		TEE_Panic(0);
402b0104773SPascal Brand 
403b0104773SPascal Brand 	if (object == NULL)
404b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
405b0104773SPascal Brand 
406b0104773SPascal Brand 	return utee_storage_obj_open(storageID, objectID, objectIDLen, flags,
407b0104773SPascal Brand 				     object);
408b0104773SPascal Brand }
409b0104773SPascal Brand 
410b0104773SPascal Brand TEE_Result TEE_CreatePersistentObject(uint32_t storageID, void *objectID,
41179a3c601SCedric Chaumont 				      uint32_t objectIDLen, uint32_t flags,
412b0104773SPascal Brand 				      TEE_ObjectHandle attributes,
413b0104773SPascal Brand 				      const void *initialData,
41479a3c601SCedric Chaumont 				      uint32_t initialDataLen,
415b0104773SPascal Brand 				      TEE_ObjectHandle *object)
416b0104773SPascal Brand {
417b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
418b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
419b0104773SPascal Brand 
420b0104773SPascal Brand 	if (objectID == NULL)
421b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
422b0104773SPascal Brand 
423b0104773SPascal Brand 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN)
424b0104773SPascal Brand 		TEE_Panic(0);
425b0104773SPascal Brand 
426b0104773SPascal Brand 	if (object == NULL)
427b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
428b0104773SPascal Brand 
429b0104773SPascal Brand 	return utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
430b0104773SPascal Brand 				       attributes, initialData, initialDataLen,
431b0104773SPascal Brand 				       object);
432b0104773SPascal Brand }
433b0104773SPascal Brand 
4347583c59eSCedric Chaumont /*
4357583c59eSCedric Chaumont  * Use of this function is deprecated
4367583c59eSCedric Chaumont  * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead
4377583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
4387583c59eSCedric Chaumont  * this specification
4397583c59eSCedric Chaumont  */
440b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
441b0104773SPascal Brand {
442b0104773SPascal Brand 	TEE_Result res;
443b0104773SPascal Brand 
444b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
445b0104773SPascal Brand 		return;
446b0104773SPascal Brand 
4477583c59eSCedric Chaumont 	res = TEE_CloseAndDeletePersistentObject1(object);
448b0104773SPascal Brand 
449b0104773SPascal Brand 	if (res != TEE_SUCCESS)
450b0104773SPascal Brand 		TEE_Panic(0);
451b0104773SPascal Brand }
452b0104773SPascal Brand 
4537583c59eSCedric Chaumont TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object)
4547583c59eSCedric Chaumont {
4557583c59eSCedric Chaumont 	TEE_Result res;
4567583c59eSCedric Chaumont 
4577583c59eSCedric Chaumont 	if (object == TEE_HANDLE_NULL)
4587583c59eSCedric Chaumont 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
4597583c59eSCedric Chaumont 
4607583c59eSCedric Chaumont 	res = utee_storage_obj_del(object);
4617583c59eSCedric Chaumont 
4627583c59eSCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
4637583c59eSCedric Chaumont 		TEE_Panic(0);
4647583c59eSCedric Chaumont 
4657583c59eSCedric Chaumont 	return res;
4667583c59eSCedric Chaumont }
4677583c59eSCedric Chaumont 
4687583c59eSCedric Chaumont 
469b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
470b0104773SPascal Brand 				      const void *newObjectID,
47179a3c601SCedric Chaumont 				      uint32_t newObjectIDLen)
472b0104773SPascal Brand {
473b0104773SPascal Brand 	TEE_Result res;
474b0104773SPascal Brand 
475b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
476b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
477b0104773SPascal Brand 
478b0104773SPascal Brand 	if (newObjectID == NULL)
479b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
480b0104773SPascal Brand 
481b0104773SPascal Brand 	if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN)
482b0104773SPascal Brand 		TEE_Panic(0);
483b0104773SPascal Brand 
484b0104773SPascal Brand 	res = utee_storage_obj_rename(object, newObjectID, newObjectIDLen);
485b0104773SPascal Brand 
486b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ACCESS_CONFLICT)
487b0104773SPascal Brand 		TEE_Panic(0);
488b0104773SPascal Brand 
489b0104773SPascal Brand 	return res;
490b0104773SPascal Brand }
491b0104773SPascal Brand 
492b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
493b0104773SPascal Brand 						  objectEnumerator)
494b0104773SPascal Brand {
495b0104773SPascal Brand 	TEE_Result res;
496b0104773SPascal Brand 
497b0104773SPascal Brand 	if (objectEnumerator == NULL)
498b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
499b0104773SPascal Brand 
500b0104773SPascal Brand 	res = utee_storage_alloc_enum(objectEnumerator);
501b0104773SPascal Brand 
502b0104773SPascal Brand 	if (res != TEE_SUCCESS)
503b0104773SPascal Brand 		*objectEnumerator = TEE_HANDLE_NULL;
504b0104773SPascal Brand 
505b0104773SPascal Brand 	return res;
506b0104773SPascal Brand }
507b0104773SPascal Brand 
508b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
509b0104773SPascal Brand {
510b0104773SPascal Brand 	TEE_Result res;
511b0104773SPascal Brand 
512b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
513b0104773SPascal Brand 		return;
514b0104773SPascal Brand 
515b0104773SPascal Brand 	res = utee_storage_free_enum(objectEnumerator);
516b0104773SPascal Brand 
517b0104773SPascal Brand 	if (res != TEE_SUCCESS)
518b0104773SPascal Brand 		TEE_Panic(0);
519b0104773SPascal Brand }
520b0104773SPascal Brand 
521b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
522b0104773SPascal Brand {
523b0104773SPascal Brand 	TEE_Result res;
524b0104773SPascal Brand 
525b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
526b0104773SPascal Brand 		return;
527b0104773SPascal Brand 
528b0104773SPascal Brand 	res = utee_storage_reset_enum(objectEnumerator);
529b0104773SPascal Brand 
530b0104773SPascal Brand 	if (res != TEE_SUCCESS)
531b0104773SPascal Brand 		TEE_Panic(0);
532b0104773SPascal Brand }
533b0104773SPascal Brand 
534b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
535b0104773SPascal Brand 					       objectEnumerator,
536b0104773SPascal Brand 					       uint32_t storageID)
537b0104773SPascal Brand {
538b0104773SPascal Brand 	TEE_Result res;
539b0104773SPascal Brand 
540b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
541b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
542b0104773SPascal Brand 
543b0104773SPascal Brand 	res = utee_storage_start_enum(objectEnumerator, storageID);
544b0104773SPascal Brand 
545b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND)
546b0104773SPascal Brand 		TEE_Panic(0);
547b0104773SPascal Brand 
548b0104773SPascal Brand 	return res;
549b0104773SPascal Brand }
550b0104773SPascal Brand 
551b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
552b0104773SPascal Brand 				       TEE_ObjectInfo *objectInfo,
55379a3c601SCedric Chaumont 				       void *objectID, uint32_t *objectIDLen)
554b0104773SPascal Brand {
555b0104773SPascal Brand 	TEE_Result res;
556b0104773SPascal Brand 
557b0104773SPascal Brand 	res =
558b0104773SPascal Brand 	    utee_storage_next_enum(objectEnumerator, objectInfo, objectID,
559b0104773SPascal Brand 				   objectIDLen);
560b0104773SPascal Brand 
561b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND)
562b0104773SPascal Brand 		TEE_Panic(0);
563b0104773SPascal Brand 
564b0104773SPascal Brand 	return res;
565b0104773SPascal Brand }
566b0104773SPascal Brand 
567b0104773SPascal Brand /* Data and Key Storage API  - Data Stream Access Functions */
568b0104773SPascal Brand 
569b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
57079a3c601SCedric Chaumont 			      uint32_t size, uint32_t *count)
571b0104773SPascal Brand {
572b0104773SPascal Brand 	TEE_Result res;
573b0104773SPascal Brand 
574b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
575b0104773SPascal Brand 		TEE_Panic(0);
576b0104773SPascal Brand 
577b0104773SPascal Brand 	res = utee_storage_obj_read(object, buffer, size, count);
578b0104773SPascal Brand 
579b0104773SPascal Brand 	if (res != TEE_SUCCESS)
580b0104773SPascal Brand 		TEE_Panic(0);
581b0104773SPascal Brand 
582b0104773SPascal Brand 	return res;
583b0104773SPascal Brand }
584b0104773SPascal Brand 
585b0104773SPascal Brand TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, void *buffer,
58679a3c601SCedric Chaumont 			       uint32_t size)
587b0104773SPascal Brand {
588b0104773SPascal Brand 	TEE_Result res;
589b0104773SPascal Brand 
590b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
591b0104773SPascal Brand 		TEE_Panic(0);
592b0104773SPascal Brand 
593b0104773SPascal Brand 	res = utee_storage_obj_write(object, buffer, size);
594b0104773SPascal Brand 
595b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NO_SPACE)
596b0104773SPascal Brand 		TEE_Panic(0);
597b0104773SPascal Brand 
598b0104773SPascal Brand 	return res;
599b0104773SPascal Brand }
600b0104773SPascal Brand 
601b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
602b0104773SPascal Brand {
603b0104773SPascal Brand 	TEE_Result res;
604b0104773SPascal Brand 
605b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
606b0104773SPascal Brand 		TEE_Panic(0);
607b0104773SPascal Brand 
608b0104773SPascal Brand 	res = utee_storage_obj_trunc(object, size);
609b0104773SPascal Brand 
610b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NO_SPACE)
611b0104773SPascal Brand 		TEE_Panic(0);
612b0104773SPascal Brand 
613b0104773SPascal Brand 	return res;
614b0104773SPascal Brand }
615b0104773SPascal Brand 
616b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
617b0104773SPascal Brand 			      TEE_Whence whence)
618b0104773SPascal Brand {
619b0104773SPascal Brand 	TEE_Result res;
620b0104773SPascal Brand 	TEE_ObjectInfo info;
621b0104773SPascal Brand 
622b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
623b0104773SPascal Brand 		TEE_Panic(0);
624b0104773SPascal Brand 
625b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
626b0104773SPascal Brand 	if (res != TEE_SUCCESS)
627b0104773SPascal Brand 		TEE_Panic(0);
628b0104773SPascal Brand 
629b0104773SPascal Brand 	switch (whence) {
630b0104773SPascal Brand 	case TEE_DATA_SEEK_SET:
631b0104773SPascal Brand 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION)
632b0104773SPascal Brand 			return TEE_ERROR_OVERFLOW;
633b0104773SPascal Brand 		break;
634b0104773SPascal Brand 	case TEE_DATA_SEEK_CUR:
635b0104773SPascal Brand 		if (offset > 0 &&
636b0104773SPascal Brand 		    ((uint32_t)offset + info.dataPosition >
637b0104773SPascal Brand 		     TEE_DATA_MAX_POSITION ||
638b0104773SPascal Brand 		     (uint32_t)offset + info.dataPosition <
639b0104773SPascal Brand 		     info.dataPosition))
640b0104773SPascal Brand 			return TEE_ERROR_OVERFLOW;
641b0104773SPascal Brand 		break;
642b0104773SPascal Brand 	case TEE_DATA_SEEK_END:
643b0104773SPascal Brand 		if (offset > 0 &&
644b0104773SPascal Brand 		    ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION ||
645b0104773SPascal Brand 		     (uint32_t)offset + info.dataSize < info.dataSize))
646b0104773SPascal Brand 			return TEE_ERROR_OVERFLOW;
647b0104773SPascal Brand 		break;
648b0104773SPascal Brand 	default:
649b0104773SPascal Brand 		TEE_Panic(0);
650b0104773SPascal Brand 	}
651b0104773SPascal Brand 
652b0104773SPascal Brand 	res = utee_storage_obj_seek(object, offset, whence);
653b0104773SPascal Brand 
654b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_OVERFLOW)
655b0104773SPascal Brand 		TEE_Panic(0);
656b0104773SPascal Brand 
657b0104773SPascal Brand 	return res;
658b0104773SPascal Brand }
659