xref: /optee_os/lib/libutee/tee_api_objects.c (revision aeb0d927fc84a8e385d4fc5365aeeb1cb10c2a1e)
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 
1430ed6a6caSCedric Chaumont 	res = utee_cryp_obj_get_attr((uint32_t)object,
1440ed6a6caSCedric Chaumont 				     attributeID, buffer, size);
145b0104773SPascal Brand 
1460ed6a6caSCedric Chaumont 	if (res != TEE_SUCCESS &&
1470ed6a6caSCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
1480ed6a6caSCedric Chaumont 	    res != TEE_ERROR_SHORT_BUFFER &&
1490ed6a6caSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
1500ed6a6caSCedric 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 
1760ed6a6caSCedric Chaumont 	res = utee_cryp_obj_get_attr((uint32_t)object,
1770ed6a6caSCedric Chaumont 				     attributeID, buf, &size);
178b0104773SPascal Brand 
1790ed6a6caSCedric Chaumont 	if (res != TEE_SUCCESS &&
1800ed6a6caSCedric Chaumont 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
1810ed6a6caSCedric Chaumont 	    res != TEE_ERROR_CORRUPT_OBJECT &&
1820ed6a6caSCedric 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);
216*aeb0d927SCedric Chaumont 
217*aeb0d927SCedric Chaumont 	if (res != TEE_SUCCESS &&
218*aeb0d927SCedric Chaumont 	    res != TEE_ERROR_OUT_OF_MEMORY &&
219*aeb0d927SCedric Chaumont 	    res != TEE_ERROR_NOT_SUPPORTED)
220*aeb0d927SCedric Chaumont 		TEE_Panic(0);
221*aeb0d927SCedric Chaumont 
222b0104773SPascal Brand 	if (res == TEE_SUCCESS)
223b0104773SPascal Brand 		*object = (TEE_ObjectHandle) obj;
2240ed6a6caSCedric Chaumont 
225b0104773SPascal Brand 	return res;
226b0104773SPascal Brand }
227b0104773SPascal Brand 
228b0104773SPascal Brand void TEE_FreeTransientObject(TEE_ObjectHandle object)
229b0104773SPascal Brand {
230b0104773SPascal Brand 	TEE_Result res;
231b0104773SPascal Brand 	TEE_ObjectInfo info;
232b0104773SPascal Brand 
233b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
234b0104773SPascal Brand 		return;
235b0104773SPascal Brand 
236b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
237b0104773SPascal Brand 	if (res != TEE_SUCCESS)
238b0104773SPascal Brand 		TEE_Panic(0);
239b0104773SPascal Brand 
240b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
241b0104773SPascal Brand 		TEE_Panic(0);
242b0104773SPascal Brand 
243b0104773SPascal Brand 	res = utee_cryp_obj_close((uint32_t)object);
244b0104773SPascal Brand 	if (res != TEE_SUCCESS)
245b0104773SPascal Brand 		TEE_Panic(0);
246b0104773SPascal Brand }
247b0104773SPascal Brand 
248b0104773SPascal Brand void TEE_ResetTransientObject(TEE_ObjectHandle object)
249b0104773SPascal Brand {
250b0104773SPascal Brand 	TEE_Result res;
251b0104773SPascal Brand 	TEE_ObjectInfo info;
252b0104773SPascal Brand 
253b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
254b0104773SPascal Brand 		return;
255b0104773SPascal Brand 
256b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
257b0104773SPascal Brand 	if (res != TEE_SUCCESS)
258b0104773SPascal Brand 		TEE_Panic(0);
259b0104773SPascal Brand 
260b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
261b0104773SPascal Brand 		TEE_Panic(0);
262b0104773SPascal Brand 
263b0104773SPascal Brand 	res = utee_cryp_obj_reset((uint32_t)object);
264b0104773SPascal Brand 	if (res != TEE_SUCCESS)
265b0104773SPascal Brand 		TEE_Panic(0);
266b0104773SPascal Brand }
267b0104773SPascal Brand 
268b0104773SPascal Brand TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object,
269b0104773SPascal Brand 				       TEE_Attribute *attrs,
270b0104773SPascal Brand 				       uint32_t attrCount)
271b0104773SPascal Brand {
272b0104773SPascal Brand 	TEE_Result res;
273b0104773SPascal Brand 	TEE_ObjectInfo info;
274b0104773SPascal Brand 
275b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
276b0104773SPascal Brand 	if (res != TEE_SUCCESS)
277b0104773SPascal Brand 		TEE_Panic(0);
278b0104773SPascal Brand 
279b0104773SPascal Brand 	/* Must be a transient object */
280b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
281b0104773SPascal Brand 		TEE_Panic(0);
282b0104773SPascal Brand 
283b0104773SPascal Brand 	/* Must not be initialized already */
284b0104773SPascal Brand 	if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
285b0104773SPascal Brand 		TEE_Panic(0);
286b0104773SPascal Brand 
287b0104773SPascal Brand 	res = utee_cryp_obj_populate((uint32_t)object, attrs, attrCount);
288b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
289b0104773SPascal Brand 		TEE_Panic(res);
290b0104773SPascal Brand 	return res;
291b0104773SPascal Brand }
292b0104773SPascal Brand 
293b0104773SPascal Brand void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID,
29479a3c601SCedric Chaumont 			  void *buffer, uint32_t length)
295b0104773SPascal Brand {
296b0104773SPascal Brand 	if (attr == NULL)
297b0104773SPascal Brand 		TEE_Panic(0);
298b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) != 0)
299b0104773SPascal Brand 		TEE_Panic(0);
300b0104773SPascal Brand 	attr->attributeID = attributeID;
301b0104773SPascal Brand 	attr->content.ref.buffer = buffer;
302b0104773SPascal Brand 	attr->content.ref.length = length;
303b0104773SPascal Brand }
304b0104773SPascal Brand 
305b0104773SPascal Brand void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID,
306b0104773SPascal Brand 			    uint32_t a, uint32_t b)
307b0104773SPascal Brand {
308b0104773SPascal Brand 	if (attr == NULL)
309b0104773SPascal Brand 		TEE_Panic(0);
310b0104773SPascal Brand 	if ((attributeID & TEE_ATTR_BIT_VALUE) == 0)
311b0104773SPascal Brand 		TEE_Panic(0);
312b0104773SPascal Brand 	attr->attributeID = attributeID;
313b0104773SPascal Brand 	attr->content.value.a = a;
314b0104773SPascal Brand 	attr->content.value.b = b;
315b0104773SPascal Brand }
316b0104773SPascal Brand 
3177583c59eSCedric Chaumont /*
3187583c59eSCedric Chaumont  * Use of this function is deprecated
3197583c59eSCedric Chaumont  * new code SHOULD use the TEE_CopyObjectAttributes1 function instead
3207583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
3217583c59eSCedric Chaumont  * this specification
3227583c59eSCedric Chaumont  */
323b0104773SPascal Brand void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject,
324b0104773SPascal Brand 			      TEE_ObjectHandle srcObject)
325b0104773SPascal Brand {
326b0104773SPascal Brand 	TEE_Result res;
3277583c59eSCedric Chaumont 	TEE_ObjectInfo src_info;
3287583c59eSCedric Chaumont 
3297583c59eSCedric Chaumont 	res = utee_cryp_obj_get_info((uint32_t)srcObject, &src_info);
3307583c59eSCedric Chaumont 	if (src_info.objectType == TEE_TYPE_CORRUPTED_OBJECT)
3317583c59eSCedric Chaumont 		return;
3327583c59eSCedric Chaumont 
3337583c59eSCedric Chaumont 	res = TEE_CopyObjectAttributes1(destObject, srcObject);
3347583c59eSCedric Chaumont 	if (res != TEE_SUCCESS)
3357583c59eSCedric Chaumont 		TEE_Panic(0);
3367583c59eSCedric Chaumont }
3377583c59eSCedric Chaumont 
3387583c59eSCedric Chaumont TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject,
3397583c59eSCedric Chaumont 			      TEE_ObjectHandle srcObject)
3407583c59eSCedric Chaumont {
3417583c59eSCedric Chaumont 	TEE_Result res;
342b0104773SPascal Brand 	TEE_ObjectInfo dst_info;
343b0104773SPascal Brand 	TEE_ObjectInfo src_info;
344b0104773SPascal Brand 
345b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)destObject, &dst_info);
346b0104773SPascal Brand 	if (res != TEE_SUCCESS)
3477583c59eSCedric Chaumont 		goto err;
348b0104773SPascal Brand 
349b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)srcObject, &src_info);
350b0104773SPascal Brand 	if (res != TEE_SUCCESS)
3517583c59eSCedric Chaumont 		goto err;
352b0104773SPascal Brand 
353b0104773SPascal Brand 	if ((src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
354b0104773SPascal Brand 		TEE_Panic(0);
355b0104773SPascal Brand 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
356b0104773SPascal Brand 		TEE_Panic(0);
357b0104773SPascal Brand 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
358b0104773SPascal Brand 		TEE_Panic(0);
359b0104773SPascal Brand 
360b0104773SPascal Brand 	res = utee_cryp_obj_copy((uint32_t)destObject, (uint32_t)srcObject);
361b0104773SPascal Brand 	if (res != TEE_SUCCESS)
362b0104773SPascal Brand 		TEE_Panic(0);
3637583c59eSCedric Chaumont 
3647583c59eSCedric Chaumont 	goto out;
3657583c59eSCedric Chaumont 
3667583c59eSCedric Chaumont err:
3677583c59eSCedric Chaumont 	if (res == TEE_ERROR_CORRUPT_OBJECT) {
3687583c59eSCedric Chaumont 		res = utee_storage_obj_del(srcObject);
3697583c59eSCedric Chaumont 		if (res != TEE_SUCCESS)
3707583c59eSCedric Chaumont 			TEE_Panic(0);
3717583c59eSCedric Chaumont 		return TEE_ERROR_CORRUPT_OBJECT;
3727583c59eSCedric Chaumont 	}
3737583c59eSCedric Chaumont 	if (res == TEE_ERROR_STORAGE_NOT_AVAILABLE)
3747583c59eSCedric Chaumont 		return res;
3757583c59eSCedric Chaumont 	TEE_Panic(0);
3767583c59eSCedric Chaumont out:
3777583c59eSCedric Chaumont 	return TEE_SUCCESS;
378b0104773SPascal Brand }
379b0104773SPascal Brand 
380b0104773SPascal Brand TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
381b0104773SPascal Brand 			   TEE_Attribute *params, uint32_t paramCount)
382b0104773SPascal Brand {
383b0104773SPascal Brand 	TEE_Result res;
384b0104773SPascal Brand 
385b0104773SPascal Brand 	res = utee_cryp_obj_generate_key((uint32_t)object, keySize,
386b0104773SPascal Brand 					 params, paramCount);
387b0104773SPascal Brand 
388*aeb0d927SCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
389b0104773SPascal Brand 		TEE_Panic(0);
390b0104773SPascal Brand 
391b0104773SPascal Brand 	return res;
392b0104773SPascal Brand }
393b0104773SPascal Brand 
394b0104773SPascal Brand /* Data and Key Storage API  - Persistent Object Functions */
395b0104773SPascal Brand 
396b0104773SPascal Brand TEE_Result TEE_OpenPersistentObject(uint32_t storageID, void *objectID,
39779a3c601SCedric Chaumont 				    uint32_t objectIDLen, uint32_t flags,
398b0104773SPascal Brand 				    TEE_ObjectHandle *object)
399b0104773SPascal Brand {
400b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
401b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
402b0104773SPascal Brand 
403b0104773SPascal Brand 	if (objectID == NULL)
404b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
405b0104773SPascal Brand 
406b0104773SPascal Brand 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN)
407b0104773SPascal Brand 		TEE_Panic(0);
408b0104773SPascal Brand 
409b0104773SPascal Brand 	if (object == NULL)
410b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
411b0104773SPascal Brand 
412b0104773SPascal Brand 	return utee_storage_obj_open(storageID, objectID, objectIDLen, flags,
413b0104773SPascal Brand 				     object);
414b0104773SPascal Brand }
415b0104773SPascal Brand 
416b0104773SPascal Brand TEE_Result TEE_CreatePersistentObject(uint32_t storageID, void *objectID,
41779a3c601SCedric Chaumont 				      uint32_t objectIDLen, uint32_t flags,
418b0104773SPascal Brand 				      TEE_ObjectHandle attributes,
419b0104773SPascal Brand 				      const void *initialData,
42079a3c601SCedric Chaumont 				      uint32_t initialDataLen,
421b0104773SPascal Brand 				      TEE_ObjectHandle *object)
422b0104773SPascal Brand {
42384431ae3SCedric Chaumont 	TEE_Result res;
424b0104773SPascal Brand 
42584431ae3SCedric Chaumont 	if (storageID != TEE_STORAGE_PRIVATE) {
42684431ae3SCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
42784431ae3SCedric Chaumont 		goto err;
42884431ae3SCedric Chaumont 	}
429b0104773SPascal Brand 
430*aeb0d927SCedric Chaumont 	if (!objectID) {
43184431ae3SCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
43284431ae3SCedric Chaumont 		goto err;
43384431ae3SCedric Chaumont 	}
434b0104773SPascal Brand 
43584431ae3SCedric Chaumont 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) {
43684431ae3SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
43784431ae3SCedric Chaumont 		goto err;
43884431ae3SCedric Chaumont 	}
439b0104773SPascal Brand 
440*aeb0d927SCedric Chaumont 	if (!object) {
44184431ae3SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
44284431ae3SCedric Chaumont 		goto err;
44384431ae3SCedric Chaumont 	}
44484431ae3SCedric Chaumont 
44584431ae3SCedric Chaumont 	res = utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
446b0104773SPascal Brand 				       attributes, initialData, initialDataLen,
447b0104773SPascal Brand 				       object);
44884431ae3SCedric Chaumont 	if (res == TEE_SUCCESS)
44984431ae3SCedric Chaumont 		goto out;
45084431ae3SCedric Chaumont err:
45184431ae3SCedric Chaumont 	if (res == TEE_ERROR_ITEM_NOT_FOUND ||
45284431ae3SCedric Chaumont 	    res == TEE_ERROR_ACCESS_CONFLICT ||
45384431ae3SCedric Chaumont 	    res == TEE_ERROR_OUT_OF_MEMORY ||
45484431ae3SCedric Chaumont 	    res == TEE_ERROR_STORAGE_NO_SPACE ||
45584431ae3SCedric Chaumont 	    res == TEE_ERROR_CORRUPT_OBJECT ||
45684431ae3SCedric Chaumont 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE)
45784431ae3SCedric Chaumont 		return res;
45884431ae3SCedric Chaumont 	TEE_Panic(0);
45984431ae3SCedric Chaumont out:
46084431ae3SCedric Chaumont 	return TEE_SUCCESS;
461b0104773SPascal Brand }
462b0104773SPascal Brand 
4637583c59eSCedric Chaumont /*
4647583c59eSCedric Chaumont  * Use of this function is deprecated
4657583c59eSCedric Chaumont  * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead
4667583c59eSCedric Chaumont  * These functions will be removed at some future major revision of
4677583c59eSCedric Chaumont  * this specification
4687583c59eSCedric Chaumont  */
469b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
470b0104773SPascal Brand {
471b0104773SPascal Brand 	TEE_Result res;
472b0104773SPascal Brand 
473b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
474b0104773SPascal Brand 		return;
475b0104773SPascal Brand 
4767583c59eSCedric Chaumont 	res = TEE_CloseAndDeletePersistentObject1(object);
477b0104773SPascal Brand 
478b0104773SPascal Brand 	if (res != TEE_SUCCESS)
479b0104773SPascal Brand 		TEE_Panic(0);
480b0104773SPascal Brand }
481b0104773SPascal Brand 
4827583c59eSCedric Chaumont TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object)
4837583c59eSCedric Chaumont {
4847583c59eSCedric Chaumont 	TEE_Result res;
4857583c59eSCedric Chaumont 
4867583c59eSCedric Chaumont 	if (object == TEE_HANDLE_NULL)
4877583c59eSCedric Chaumont 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
4887583c59eSCedric Chaumont 
4897583c59eSCedric Chaumont 	res = utee_storage_obj_del(object);
4907583c59eSCedric Chaumont 
4917583c59eSCedric Chaumont 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
4927583c59eSCedric Chaumont 		TEE_Panic(0);
4937583c59eSCedric Chaumont 
4947583c59eSCedric Chaumont 	return res;
4957583c59eSCedric Chaumont }
4967583c59eSCedric Chaumont 
4977583c59eSCedric Chaumont 
498b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
499b0104773SPascal Brand 				      const void *newObjectID,
50079a3c601SCedric Chaumont 				      uint32_t newObjectIDLen)
501b0104773SPascal Brand {
502b0104773SPascal Brand 	TEE_Result res;
503b0104773SPascal Brand 
504b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
505b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
506b0104773SPascal Brand 
507b0104773SPascal Brand 	if (newObjectID == NULL)
508b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
509b0104773SPascal Brand 
510b0104773SPascal Brand 	if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN)
511b0104773SPascal Brand 		TEE_Panic(0);
512b0104773SPascal Brand 
513b0104773SPascal Brand 	res = utee_storage_obj_rename(object, newObjectID, newObjectIDLen);
514b0104773SPascal Brand 
515b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ACCESS_CONFLICT)
516b0104773SPascal Brand 		TEE_Panic(0);
517b0104773SPascal Brand 
518b0104773SPascal Brand 	return res;
519b0104773SPascal Brand }
520b0104773SPascal Brand 
521b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
522b0104773SPascal Brand 						  objectEnumerator)
523b0104773SPascal Brand {
524b0104773SPascal Brand 	TEE_Result res;
525b0104773SPascal Brand 
526b0104773SPascal Brand 	if (objectEnumerator == NULL)
527b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
528b0104773SPascal Brand 
529b0104773SPascal Brand 	res = utee_storage_alloc_enum(objectEnumerator);
530b0104773SPascal Brand 
531b0104773SPascal Brand 	if (res != TEE_SUCCESS)
532b0104773SPascal Brand 		*objectEnumerator = TEE_HANDLE_NULL;
533b0104773SPascal Brand 
534b0104773SPascal Brand 	return res;
535b0104773SPascal Brand }
536b0104773SPascal Brand 
537b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
538b0104773SPascal Brand {
539b0104773SPascal Brand 	TEE_Result res;
540b0104773SPascal Brand 
541b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
542b0104773SPascal Brand 		return;
543b0104773SPascal Brand 
544b0104773SPascal Brand 	res = utee_storage_free_enum(objectEnumerator);
545b0104773SPascal Brand 
546b0104773SPascal Brand 	if (res != TEE_SUCCESS)
547b0104773SPascal Brand 		TEE_Panic(0);
548b0104773SPascal Brand }
549b0104773SPascal Brand 
550b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
551b0104773SPascal Brand {
552b0104773SPascal Brand 	TEE_Result res;
553b0104773SPascal Brand 
554b0104773SPascal Brand 	if (objectEnumerator == TEE_HANDLE_NULL)
555b0104773SPascal Brand 		return;
556b0104773SPascal Brand 
557b0104773SPascal Brand 	res = utee_storage_reset_enum(objectEnumerator);
558b0104773SPascal Brand 
559b0104773SPascal Brand 	if (res != TEE_SUCCESS)
560b0104773SPascal Brand 		TEE_Panic(0);
561b0104773SPascal Brand }
562b0104773SPascal Brand 
563b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
564b0104773SPascal Brand 					       objectEnumerator,
565b0104773SPascal Brand 					       uint32_t storageID)
566b0104773SPascal Brand {
567b0104773SPascal Brand 	TEE_Result res;
568b0104773SPascal Brand 
569b0104773SPascal Brand 	if (storageID != TEE_STORAGE_PRIVATE)
570b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
571b0104773SPascal Brand 
572b0104773SPascal Brand 	res = utee_storage_start_enum(objectEnumerator, storageID);
573b0104773SPascal Brand 
574b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND)
575b0104773SPascal Brand 		TEE_Panic(0);
576b0104773SPascal Brand 
577b0104773SPascal Brand 	return res;
578b0104773SPascal Brand }
579b0104773SPascal Brand 
580b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
581b0104773SPascal Brand 				       TEE_ObjectInfo *objectInfo,
58279a3c601SCedric Chaumont 				       void *objectID, uint32_t *objectIDLen)
583b0104773SPascal Brand {
584b0104773SPascal Brand 	TEE_Result res;
585b0104773SPascal Brand 
586b0104773SPascal Brand 	res =
587b0104773SPascal Brand 	    utee_storage_next_enum(objectEnumerator, objectInfo, objectID,
588b0104773SPascal Brand 				   objectIDLen);
589b0104773SPascal Brand 
590b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND)
591b0104773SPascal Brand 		TEE_Panic(0);
592b0104773SPascal Brand 
593b0104773SPascal Brand 	return res;
594b0104773SPascal Brand }
595b0104773SPascal Brand 
596b0104773SPascal Brand /* Data and Key Storage API  - Data Stream Access Functions */
597b0104773SPascal Brand 
598b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
59979a3c601SCedric Chaumont 			      uint32_t size, uint32_t *count)
600b0104773SPascal Brand {
601b0104773SPascal Brand 	TEE_Result res;
602b0104773SPascal Brand 
603b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
604b0104773SPascal Brand 		TEE_Panic(0);
605b0104773SPascal Brand 
606b0104773SPascal Brand 	res = utee_storage_obj_read(object, buffer, size, count);
607b0104773SPascal Brand 
608b0104773SPascal Brand 	if (res != TEE_SUCCESS)
609b0104773SPascal Brand 		TEE_Panic(0);
610b0104773SPascal Brand 
611b0104773SPascal Brand 	return res;
612b0104773SPascal Brand }
613b0104773SPascal Brand 
614b0104773SPascal Brand TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, void *buffer,
61579a3c601SCedric Chaumont 			       uint32_t size)
616b0104773SPascal Brand {
617b0104773SPascal Brand 	TEE_Result res;
618b0104773SPascal Brand 
619b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
620b0104773SPascal Brand 		TEE_Panic(0);
621b0104773SPascal Brand 
622b0104773SPascal Brand 	res = utee_storage_obj_write(object, buffer, size);
623b0104773SPascal Brand 
624b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NO_SPACE)
625b0104773SPascal Brand 		TEE_Panic(0);
626b0104773SPascal Brand 
627b0104773SPascal Brand 	return res;
628b0104773SPascal Brand }
629b0104773SPascal Brand 
630b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
631b0104773SPascal Brand {
632b0104773SPascal Brand 	TEE_Result res;
633b0104773SPascal Brand 
634b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
635b0104773SPascal Brand 		TEE_Panic(0);
636b0104773SPascal Brand 
637b0104773SPascal Brand 	res = utee_storage_obj_trunc(object, size);
638b0104773SPascal Brand 
639b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NO_SPACE)
640b0104773SPascal Brand 		TEE_Panic(0);
641b0104773SPascal Brand 
642b0104773SPascal Brand 	return res;
643b0104773SPascal Brand }
644b0104773SPascal Brand 
645b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
646b0104773SPascal Brand 			      TEE_Whence whence)
647b0104773SPascal Brand {
648b0104773SPascal Brand 	TEE_Result res;
649b0104773SPascal Brand 	TEE_ObjectInfo info;
650b0104773SPascal Brand 
651b0104773SPascal Brand 	if (object == TEE_HANDLE_NULL)
652b0104773SPascal Brand 		TEE_Panic(0);
653b0104773SPascal Brand 
654b0104773SPascal Brand 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
655b0104773SPascal Brand 	if (res != TEE_SUCCESS)
656b0104773SPascal Brand 		TEE_Panic(0);
657b0104773SPascal Brand 
658b0104773SPascal Brand 	switch (whence) {
659b0104773SPascal Brand 	case TEE_DATA_SEEK_SET:
660b0104773SPascal Brand 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION)
661b0104773SPascal Brand 			return TEE_ERROR_OVERFLOW;
662b0104773SPascal Brand 		break;
663b0104773SPascal Brand 	case TEE_DATA_SEEK_CUR:
664b0104773SPascal Brand 		if (offset > 0 &&
665b0104773SPascal Brand 		    ((uint32_t)offset + info.dataPosition >
666b0104773SPascal Brand 		     TEE_DATA_MAX_POSITION ||
667b0104773SPascal Brand 		     (uint32_t)offset + info.dataPosition <
668b0104773SPascal Brand 		     info.dataPosition))
669b0104773SPascal Brand 			return TEE_ERROR_OVERFLOW;
670b0104773SPascal Brand 		break;
671b0104773SPascal Brand 	case TEE_DATA_SEEK_END:
672b0104773SPascal Brand 		if (offset > 0 &&
673b0104773SPascal Brand 		    ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION ||
674b0104773SPascal Brand 		     (uint32_t)offset + info.dataSize < info.dataSize))
675b0104773SPascal Brand 			return TEE_ERROR_OVERFLOW;
676b0104773SPascal Brand 		break;
677b0104773SPascal Brand 	default:
678b0104773SPascal Brand 		TEE_Panic(0);
679b0104773SPascal Brand 	}
680b0104773SPascal Brand 
681b0104773SPascal Brand 	res = utee_storage_obj_seek(object, offset, whence);
682b0104773SPascal Brand 
683b0104773SPascal Brand 	if (res != TEE_SUCCESS && res != TEE_ERROR_OVERFLOW)
684b0104773SPascal Brand 		TEE_Panic(0);
685b0104773SPascal Brand 
686b0104773SPascal Brand 	return res;
687b0104773SPascal Brand }
688