xref: /optee_os/lib/libutee/tee_api_objects.c (revision 91c842489f0995fe21336f9fca53b94dcb43d6ee)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 #include <stdlib.h>
6 #include <string.h>
7 
8 #include <tee_api.h>
9 #include <utee_syscalls.h>
10 #include "tee_api_private.h"
11 
12 #define TEE_USAGE_DEFAULT   0xffffffff
13 
14 void __utee_from_attr(struct utee_attribute *ua, const TEE_Attribute *attrs,
15 			uint32_t attr_count)
16 {
17 	size_t n;
18 
19 	for (n = 0; n < attr_count; n++) {
20 		ua[n].attribute_id = attrs[n].attributeID;
21 		if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE) {
22 			ua[n].a = attrs[n].content.value.a;
23 			ua[n].b = attrs[n].content.value.b;
24 		} else {
25 			ua[n].a = (uintptr_t)attrs[n].content.ref.buffer;
26 			ua[n].b = attrs[n].content.ref.length;
27 		}
28 	}
29 }
30 
31 void __utee_from_gp11_attr(struct utee_attribute *ua,
32 			   const __GP11_TEE_Attribute *attrs,
33 			   uint32_t attr_count)
34 {
35 	size_t n = 0;
36 
37 	for (n = 0; n < attr_count; n++) {
38 		ua[n].attribute_id = attrs[n].attributeID;
39 		if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE) {
40 			ua[n].a = attrs[n].content.value.a;
41 			ua[n].b = attrs[n].content.value.b;
42 		} else {
43 			ua[n].a = (uintptr_t)attrs[n].content.ref.buffer;
44 			ua[n].b = attrs[n].content.ref.length;
45 		}
46 	}
47 }
48 
49 /* Data and Key Storage API  - Generic Object Functions */
50 /*
51  * Use of this function is deprecated
52  * new code SHOULD use the TEE_GetObjectInfo1 function instead
53  * These functions will be removed at some future major revision of
54  * this specification
55  */
56 void TEE_GetObjectInfo(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo)
57 {
58 	struct utee_object_info info = { };
59 	TEE_Result res = TEE_SUCCESS;
60 
61 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
62 
63 	if (res != TEE_SUCCESS)
64 		TEE_Panic(res);
65 
66 	if (info.obj_type == TEE_TYPE_CORRUPTED_OBJECT) {
67 		objectInfo->objectSize = 0;
68 		objectInfo->maxObjectSize = 0;
69 		objectInfo->objectUsage = 0;
70 		objectInfo->dataSize = 0;
71 		objectInfo->dataPosition = 0;
72 		objectInfo->handleFlags = 0;
73 	} else {
74 		objectInfo->objectType = info.obj_type;
75 		objectInfo->objectSize = info.obj_size;
76 		objectInfo->maxObjectSize = info.max_obj_size;
77 		objectInfo->objectUsage = info.obj_usage;
78 		objectInfo->dataSize = info.data_size;
79 		objectInfo->dataPosition = info.data_pos;
80 		objectInfo->handleFlags = info.handle_flags;
81 	}
82 }
83 
84 void __GP11_TEE_GetObjectInfo(TEE_ObjectHandle object,
85 			      __GP11_TEE_ObjectInfo *objectInfo)
86 {
87 	struct utee_object_info info = { };
88 	TEE_Result res = TEE_SUCCESS;
89 
90 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
91 
92 	if (res != TEE_SUCCESS)
93 		TEE_Panic(res);
94 
95 	if (info.obj_type == TEE_TYPE_CORRUPTED_OBJECT) {
96 		objectInfo->keySize = 0;
97 		objectInfo->maxKeySize = 0;
98 		objectInfo->objectUsage = 0;
99 		objectInfo->dataSize = 0;
100 		objectInfo->dataPosition = 0;
101 		objectInfo->handleFlags = 0;
102 	} else {
103 		objectInfo->objectType = info.obj_type;
104 		objectInfo->keySize = info.obj_size;
105 		objectInfo->maxKeySize = info.max_obj_size;
106 		objectInfo->objectUsage = info.obj_usage;
107 		objectInfo->dataSize = info.data_size;
108 		objectInfo->dataPosition = info.data_pos;
109 		objectInfo->handleFlags = info.handle_flags;
110 	}
111 }
112 
113 TEE_Result TEE_GetObjectInfo1(TEE_ObjectHandle object,
114 			      TEE_ObjectInfo *objectInfo)
115 {
116 	struct utee_object_info info = { };
117 	TEE_Result res = TEE_SUCCESS;
118 
119 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
120 
121 	if (res != TEE_SUCCESS &&
122 	    res != TEE_ERROR_CORRUPT_OBJECT &&
123 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
124 		TEE_Panic(res);
125 
126 	objectInfo->objectType = info.obj_type;
127 	objectInfo->objectSize = info.obj_size;
128 	objectInfo->maxObjectSize = info.max_obj_size;
129 	objectInfo->objectUsage = info.obj_usage;
130 	objectInfo->dataSize = info.data_size;
131 	objectInfo->dataPosition = info.data_pos;
132 	objectInfo->handleFlags = info.handle_flags;
133 
134 	return res;
135 }
136 
137 TEE_Result __GP11_TEE_GetObjectInfo1(TEE_ObjectHandle object,
138 				     __GP11_TEE_ObjectInfo *objectInfo)
139 {
140 	struct utee_object_info info = { };
141 	TEE_Result res = TEE_SUCCESS;
142 
143 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
144 
145 	if (res != TEE_SUCCESS &&
146 	    res != TEE_ERROR_CORRUPT_OBJECT &&
147 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
148 		TEE_Panic(res);
149 
150 	objectInfo->objectType = info.obj_type;
151 	objectInfo->keySize = info.obj_size;
152 	objectInfo->maxKeySize = info.max_obj_size;
153 	objectInfo->objectUsage = info.obj_usage;
154 	objectInfo->dataSize = info.data_size;
155 	objectInfo->dataPosition = info.data_pos;
156 	objectInfo->handleFlags = info.handle_flags;
157 
158 	return res;
159 }
160 
161 /*
162  * Use of this function is deprecated
163  * new code SHOULD use the TEE_RestrictObjectUsage1 function instead
164  * These functions will be removed at some future major revision of
165  * this specification
166  */
167 void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage)
168 {
169 	struct utee_object_info info = { };
170 	TEE_Result res = TEE_SUCCESS;
171 
172 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
173 	if (info.obj_type == TEE_TYPE_CORRUPTED_OBJECT)
174 		return;
175 
176 	res = TEE_RestrictObjectUsage1(object, objectUsage);
177 
178 	if (res != TEE_SUCCESS)
179 		TEE_Panic(res);
180 }
181 
182 TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage)
183 {
184 	TEE_Result res;
185 
186 	res = _utee_cryp_obj_restrict_usage((unsigned long)object,
187 					    objectUsage);
188 
189 	if (res != TEE_SUCCESS &&
190 	    res != TEE_ERROR_CORRUPT_OBJECT &&
191 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
192 		TEE_Panic(res);
193 
194 	return res;
195 }
196 
197 TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object,
198 					uint32_t attributeID, void *buffer,
199 					size_t *size)
200 {
201 	struct utee_object_info info = { };
202 	TEE_Result res = TEE_SUCCESS;
203 	uint64_t sz = 0;
204 
205 	__utee_check_inout_annotation(size, sizeof(*size));
206 
207 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
208 	if (res != TEE_SUCCESS)
209 		goto exit;
210 
211 	/* This function only supports reference attributes */
212 	if ((attributeID & TEE_ATTR_FLAG_VALUE)) {
213 		res = TEE_ERROR_BAD_PARAMETERS;
214 		goto exit;
215 	}
216 
217 	sz = *size;
218 	res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID,
219 				      buffer, &sz);
220 	*size = sz;
221 
222 exit:
223 	if (res != TEE_SUCCESS &&
224 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
225 	    res != TEE_ERROR_SHORT_BUFFER &&
226 	    res != TEE_ERROR_CORRUPT_OBJECT &&
227 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
228 		TEE_Panic(res);
229 
230 	return res;
231 }
232 
233 TEE_Result __GP11_TEE_GetObjectBufferAttribute(TEE_ObjectHandle object,
234 					       uint32_t attributeID,
235 					       void *buffer, uint32_t *size)
236 {
237 	TEE_Result res = TEE_SUCCESS;
238 	size_t l = 0;
239 
240 	__utee_check_inout_annotation(size, sizeof(*size));
241 	l = *size;
242 	res = TEE_GetObjectBufferAttribute(object, attributeID, buffer, &l);
243 	*size = l;
244 	return res;
245 }
246 
247 TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object,
248 				       uint32_t attributeID, uint32_t *a,
249 				       uint32_t *b)
250 {
251 	struct utee_object_info info = { };
252 	TEE_Result res = TEE_SUCCESS;
253 	uint32_t buf[2];
254 	uint64_t size = sizeof(buf);
255 
256 	if (a)
257 		__utee_check_out_annotation(a, sizeof(*a));
258 	if (b)
259 		__utee_check_out_annotation(b, sizeof(*b));
260 
261 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
262 	if (res != TEE_SUCCESS)
263 		goto exit;
264 
265 	/* This function only supports value attributes */
266 	if (!(attributeID & TEE_ATTR_FLAG_VALUE)) {
267 		res = TEE_ERROR_BAD_PARAMETERS;
268 		goto exit;
269 	}
270 
271 	res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID, buf,
272 				      &size);
273 
274 exit:
275 	if (res != TEE_SUCCESS &&
276 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
277 	    res != TEE_ERROR_CORRUPT_OBJECT &&
278 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
279 		TEE_Panic(res);
280 
281 	if (size != sizeof(buf))
282 		TEE_Panic(0);
283 
284 	if (res == TEE_SUCCESS) {
285 		if (a)
286 			*a = buf[0];
287 		if (b)
288 			*b = buf[1];
289 	}
290 
291 	return res;
292 }
293 
294 void TEE_CloseObject(TEE_ObjectHandle object)
295 {
296 	TEE_Result res;
297 
298 	if (object == TEE_HANDLE_NULL)
299 		return;
300 
301 	res = _utee_cryp_obj_close((unsigned long)object);
302 	if (res != TEE_SUCCESS)
303 		TEE_Panic(res);
304 }
305 
306 /* Data and Key Storage API  - Transient Object Functions */
307 
308 TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType,
309 				       uint32_t maxKeySize,
310 				       TEE_ObjectHandle *object)
311 {
312 	TEE_Result res;
313 	uint32_t obj;
314 
315 	__utee_check_out_annotation(object, sizeof(*object));
316 
317 	res = _utee_cryp_obj_alloc(objectType, maxKeySize, &obj);
318 
319 	if (res != TEE_SUCCESS &&
320 	    res != TEE_ERROR_OUT_OF_MEMORY &&
321 	    res != TEE_ERROR_NOT_SUPPORTED)
322 		TEE_Panic(res);
323 
324 	if (res == TEE_SUCCESS)
325 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
326 
327 	return res;
328 }
329 
330 void TEE_FreeTransientObject(TEE_ObjectHandle object)
331 {
332 	struct utee_object_info info = { };
333 	TEE_Result res = TEE_SUCCESS;
334 
335 	if (object == TEE_HANDLE_NULL)
336 		return;
337 
338 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
339 	if (res != TEE_SUCCESS)
340 		TEE_Panic(res);
341 
342 	if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
343 		TEE_Panic(0);
344 
345 	res = _utee_cryp_obj_close((unsigned long)object);
346 	if (res != TEE_SUCCESS)
347 		TEE_Panic(res);
348 }
349 
350 void TEE_ResetTransientObject(TEE_ObjectHandle object)
351 {
352 	struct utee_object_info info = { };
353 	TEE_Result res = TEE_SUCCESS;
354 
355 	if (object == TEE_HANDLE_NULL)
356 		return;
357 
358 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
359 	if (res != TEE_SUCCESS)
360 		TEE_Panic(res);
361 
362 	if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
363 		TEE_Panic(0);
364 
365 	res = _utee_cryp_obj_reset((unsigned long)object);
366 	if (res != TEE_SUCCESS)
367 		TEE_Panic(res);
368 }
369 
370 TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object,
371 				       const TEE_Attribute *attrs,
372 				       uint32_t attrCount)
373 {
374 	struct utee_attribute ua[attrCount];
375 	struct utee_object_info info = { };
376 	TEE_Result res = TEE_SUCCESS;
377 
378 	__utee_check_attr_in_annotation(attrs, attrCount);
379 
380 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
381 	if (res != TEE_SUCCESS)
382 		TEE_Panic(res);
383 
384 	/* Must be a transient object */
385 	if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
386 		TEE_Panic(0);
387 
388 	/* Must not be initialized already */
389 	if ((info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
390 		TEE_Panic(0);
391 
392 	__utee_from_attr(ua, attrs, attrCount);
393 	res = _utee_cryp_obj_populate((unsigned long)object, ua, attrCount);
394 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
395 		TEE_Panic(res);
396 	return res;
397 }
398 
399 TEE_Result __GP11_TEE_PopulateTransientObject(TEE_ObjectHandle object,
400 					      const __GP11_TEE_Attribute *attrs,
401 					      uint32_t attrCount)
402 {
403 	struct utee_attribute ua[attrCount];
404 	struct utee_object_info info = { };
405 	TEE_Result res = TEE_SUCCESS;
406 
407 	__utee_check_gp11_attr_in_annotation(attrs, attrCount);
408 
409 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
410 	if (res != TEE_SUCCESS)
411 		TEE_Panic(res);
412 
413 	/* Must be a transient object */
414 	if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
415 		TEE_Panic(0);
416 
417 	/* Must not be initialized already */
418 	if ((info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
419 		TEE_Panic(0);
420 
421 	__utee_from_gp11_attr(ua, attrs, attrCount);
422 	res = _utee_cryp_obj_populate((unsigned long)object, ua, attrCount);
423 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
424 		TEE_Panic(res);
425 	return res;
426 }
427 
428 void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID,
429 			  const void *buffer, uint32_t length)
430 {
431 	__utee_check_out_annotation(attr, sizeof(*attr));
432 
433 	if ((attributeID & TEE_ATTR_FLAG_VALUE) != 0)
434 		TEE_Panic(0);
435 	attr->attributeID = attributeID;
436 	attr->content.ref.buffer = (void *)buffer;
437 	attr->content.ref.length = length;
438 }
439 
440 void __GP11_TEE_InitRefAttribute(__GP11_TEE_Attribute *attr,
441 				 uint32_t attributeID,
442 				 const void *buffer, uint32_t length)
443 {
444 	__utee_check_out_annotation(attr, sizeof(*attr));
445 
446 	if ((attributeID & TEE_ATTR_FLAG_VALUE) != 0)
447 		TEE_Panic(0);
448 	attr->attributeID = attributeID;
449 	attr->content.ref.buffer = (void *)buffer;
450 	attr->content.ref.length = length;
451 }
452 
453 void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID,
454 			    uint32_t a, uint32_t b)
455 {
456 	__utee_check_out_annotation(attr, sizeof(*attr));
457 
458 	if ((attributeID & TEE_ATTR_FLAG_VALUE) == 0)
459 		TEE_Panic(0);
460 	attr->attributeID = attributeID;
461 	attr->content.value.a = a;
462 	attr->content.value.b = b;
463 }
464 
465 void __GP11_TEE_InitValueAttribute(__GP11_TEE_Attribute *attr,
466 				   uint32_t attributeID,
467 				   uint32_t a, uint32_t b)
468 {
469 	__utee_check_out_annotation(attr, sizeof(*attr));
470 
471 	if ((attributeID & TEE_ATTR_FLAG_VALUE) == 0)
472 		TEE_Panic(0);
473 	attr->attributeID = attributeID;
474 	attr->content.value.a = a;
475 	attr->content.value.b = b;
476 }
477 
478 /*
479  * Use of this function is deprecated
480  * new code SHOULD use the TEE_CopyObjectAttributes1 function instead
481  * These functions will be removed at some future major revision of
482  * this specification
483  */
484 void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject,
485 			      TEE_ObjectHandle srcObject)
486 {
487 	struct utee_object_info src_info = { };
488 	TEE_Result res = TEE_SUCCESS;
489 
490 	res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info);
491 	if (src_info.obj_type == TEE_TYPE_CORRUPTED_OBJECT)
492 		return;
493 
494 	res = TEE_CopyObjectAttributes1(destObject, srcObject);
495 	if (res != TEE_SUCCESS)
496 		TEE_Panic(res);
497 }
498 
499 TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject,
500 			      TEE_ObjectHandle srcObject)
501 {
502 	struct utee_object_info dst_info = { };
503 	struct utee_object_info src_info = { };
504 	TEE_Result res = TEE_SUCCESS;
505 
506 	res = _utee_cryp_obj_get_info((unsigned long)destObject, &dst_info);
507 	if (res != TEE_SUCCESS)
508 		goto exit;
509 
510 	res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info);
511 	if (res != TEE_SUCCESS)
512 		goto exit;
513 
514 	if (!(src_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED))
515 		TEE_Panic(0);
516 
517 	if ((dst_info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT))
518 		TEE_Panic(0);
519 
520 	if ((dst_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED))
521 		TEE_Panic(0);
522 
523 	res = _utee_cryp_obj_copy((unsigned long)destObject,
524 				  (unsigned long)srcObject);
525 
526 exit:
527 	if (res != TEE_SUCCESS &&
528 	    res != TEE_ERROR_CORRUPT_OBJECT &&
529 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
530 		TEE_Panic(res);
531 
532 	return res;
533 }
534 
535 TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
536 			   const TEE_Attribute *params, uint32_t paramCount)
537 {
538 	TEE_Result res;
539 	struct utee_attribute ua[paramCount];
540 
541 	__utee_check_attr_in_annotation(params, paramCount);
542 
543 	__utee_from_attr(ua, params, paramCount);
544 	res = _utee_cryp_obj_generate_key((unsigned long)object, keySize,
545 					  ua, paramCount);
546 
547 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
548 		TEE_Panic(res);
549 
550 	return res;
551 }
552 
553 TEE_Result __GP11_TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
554 				  const __GP11_TEE_Attribute *params,
555 				  uint32_t paramCount)
556 {
557 	TEE_Result res = TEE_SUCCESS;
558 	struct utee_attribute ua[paramCount];
559 
560 	__utee_check_gp11_attr_in_annotation(params, paramCount);
561 
562 	__utee_from_gp11_attr(ua, params, paramCount);
563 	res = _utee_cryp_obj_generate_key((unsigned long)object, keySize,
564 					  ua, paramCount);
565 
566 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
567 		TEE_Panic(res);
568 
569 	return res;
570 }
571 
572 /* Data and Key Storage API  - Persistent Object Functions */
573 
574 TEE_Result TEE_OpenPersistentObject(uint32_t storageID, const void *objectID,
575 				    uint32_t objectIDLen, uint32_t flags,
576 				    TEE_ObjectHandle *object)
577 {
578 	TEE_Result res;
579 	uint32_t obj;
580 
581 	__utee_check_out_annotation(object, sizeof(*object));
582 
583 	res = _utee_storage_obj_open(storageID, objectID, objectIDLen, flags,
584 				     &obj);
585 	if (res == TEE_SUCCESS)
586 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
587 
588 	if (res != TEE_SUCCESS &&
589 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
590 	    res != TEE_ERROR_ACCESS_CONFLICT &&
591 	    res != TEE_ERROR_OUT_OF_MEMORY &&
592 	    res != TEE_ERROR_CORRUPT_OBJECT &&
593 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
594 		TEE_Panic(res);
595 
596 	if (res != TEE_SUCCESS)
597 		*object = TEE_HANDLE_NULL;
598 
599 	return res;
600 }
601 
602 TEE_Result TEE_CreatePersistentObject(uint32_t storageID, const void *objectID,
603 				      uint32_t objectIDLen, uint32_t flags,
604 				      TEE_ObjectHandle attributes,
605 				      const void *initialData,
606 				      uint32_t initialDataLen,
607 				      TEE_ObjectHandle *object)
608 {
609 	TEE_Result res;
610 	uint32_t obj;
611 
612 	__utee_check_out_annotation(object, sizeof(*object));
613 
614 	res = _utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
615 				       (unsigned long)attributes, initialData,
616 				       initialDataLen, &obj);
617 
618 	if (res == TEE_SUCCESS)
619 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
620 
621 	if (res != TEE_SUCCESS &&
622 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
623 	    res != TEE_ERROR_ACCESS_CONFLICT &&
624 	    res != TEE_ERROR_OUT_OF_MEMORY &&
625 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
626 	    res != TEE_ERROR_CORRUPT_OBJECT &&
627 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
628 		TEE_Panic(res);
629 
630 	if (res != TEE_SUCCESS)
631 		*object = TEE_HANDLE_NULL;
632 
633 	return res;
634 }
635 
636 /*
637  * Use of this function is deprecated
638  * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead
639  * These functions will be removed at some future major revision of
640  * this specification
641  */
642 void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
643 {
644 	TEE_Result res;
645 
646 	if (object == TEE_HANDLE_NULL)
647 		return;
648 
649 	res = TEE_CloseAndDeletePersistentObject1(object);
650 
651 	if (res != TEE_SUCCESS)
652 		TEE_Panic(0);
653 }
654 
655 TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object)
656 {
657 	TEE_Result res;
658 
659 	if (object == TEE_HANDLE_NULL)
660 		return TEE_SUCCESS;
661 
662 	res = _utee_storage_obj_del((unsigned long)object);
663 
664 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
665 		TEE_Panic(res);
666 
667 	return res;
668 }
669 
670 
671 TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
672 				      const void *newObjectID,
673 				      uint32_t newObjectIDLen)
674 {
675 	TEE_Result res;
676 
677 	if (object == TEE_HANDLE_NULL) {
678 		res = TEE_ERROR_ITEM_NOT_FOUND;
679 		goto out;
680 	}
681 
682 	res = _utee_storage_obj_rename((unsigned long)object, newObjectID,
683 				       newObjectIDLen);
684 
685 out:
686 	if (res != TEE_SUCCESS &&
687 	    res != TEE_ERROR_ACCESS_CONFLICT &&
688 	    res != TEE_ERROR_CORRUPT_OBJECT &&
689 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
690 		TEE_Panic(res);
691 
692 	return res;
693 }
694 
695 TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
696 						  objectEnumerator)
697 {
698 	TEE_Result res;
699 	uint32_t oe;
700 
701 	__utee_check_out_annotation(objectEnumerator,
702 				    sizeof(*objectEnumerator));
703 
704 	res = _utee_storage_alloc_enum(&oe);
705 
706 	if (res != TEE_SUCCESS)
707 		oe = TEE_HANDLE_NULL;
708 
709 	*objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe;
710 
711 	if (res != TEE_SUCCESS &&
712 	    res != TEE_ERROR_ACCESS_CONFLICT)
713 		TEE_Panic(res);
714 
715 	return res;
716 }
717 
718 void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
719 {
720 	TEE_Result res;
721 
722 	if (objectEnumerator == TEE_HANDLE_NULL)
723 		return;
724 
725 	res = _utee_storage_free_enum((unsigned long)objectEnumerator);
726 
727 	if (res != TEE_SUCCESS)
728 		TEE_Panic(res);
729 }
730 
731 void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
732 {
733 	TEE_Result res;
734 
735 	if (objectEnumerator == TEE_HANDLE_NULL)
736 		return;
737 
738 	res = _utee_storage_reset_enum((unsigned long)objectEnumerator);
739 
740 	if (res != TEE_SUCCESS)
741 		TEE_Panic(res);
742 }
743 
744 TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
745 					       objectEnumerator,
746 					       uint32_t storageID)
747 {
748 	TEE_Result res;
749 
750 	res = _utee_storage_start_enum((unsigned long)objectEnumerator,
751 				       storageID);
752 
753 	if (res != TEE_SUCCESS &&
754 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
755 	    res != TEE_ERROR_CORRUPT_OBJECT &&
756 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
757 		TEE_Panic(res);
758 
759 	return res;
760 }
761 
762 TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
763 				       TEE_ObjectInfo *objectInfo,
764 				       void *objectID, uint32_t *objectIDLen)
765 {
766 	struct utee_object_info info = { };
767 	TEE_Result res = TEE_SUCCESS;
768 	uint64_t len = 0;
769 
770 	if (objectInfo)
771 		__utee_check_out_annotation(objectInfo, sizeof(*objectInfo));
772 	__utee_check_out_annotation(objectIDLen, sizeof(*objectIDLen));
773 
774 	if (!objectID) {
775 		res = TEE_ERROR_BAD_PARAMETERS;
776 		goto out;
777 	}
778 
779 	len = *objectIDLen;
780 	res = _utee_storage_next_enum((unsigned long)objectEnumerator,
781 				      &info, objectID, &len);
782 	if (objectInfo) {
783 		objectInfo->objectType = info.obj_type;
784 		objectInfo->objectSize = info.obj_size;
785 		objectInfo->maxObjectSize = info.max_obj_size;
786 		objectInfo->objectUsage = info.obj_usage;
787 		objectInfo->dataSize = info.data_size;
788 		objectInfo->dataPosition = info.data_pos;
789 		objectInfo->handleFlags = info.handle_flags;
790 	}
791 	*objectIDLen = len;
792 
793 out:
794 	if (res != TEE_SUCCESS &&
795 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
796 	    res != TEE_ERROR_CORRUPT_OBJECT &&
797 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
798 		TEE_Panic(res);
799 
800 	return res;
801 }
802 
803 TEE_Result
804 __GP11_TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
805 				   __GP11_TEE_ObjectInfo *objectInfo,
806 				   void *objectID, uint32_t *objectIDLen)
807 {
808 	struct utee_object_info info = { };
809 	TEE_Result res = TEE_SUCCESS;
810 	uint64_t len = 0;
811 
812 	if (objectInfo)
813 		__utee_check_out_annotation(objectInfo, sizeof(*objectInfo));
814 	__utee_check_out_annotation(objectIDLen, sizeof(*objectIDLen));
815 
816 	if (!objectID) {
817 		res = TEE_ERROR_BAD_PARAMETERS;
818 		goto out;
819 	}
820 
821 	len = *objectIDLen;
822 	res = _utee_storage_next_enum((unsigned long)objectEnumerator,
823 				      &info, objectID, &len);
824 	if (objectInfo) {
825 		objectInfo->objectType = info.obj_type;
826 		objectInfo->keySize = info.obj_size;
827 		objectInfo->maxKeySize = info.max_obj_size;
828 		objectInfo->objectUsage = info.obj_usage;
829 		objectInfo->dataSize = info.data_size;
830 		objectInfo->dataPosition = info.data_pos;
831 		objectInfo->handleFlags = info.handle_flags;
832 	}
833 	*objectIDLen = len;
834 
835 out:
836 	if (res != TEE_SUCCESS &&
837 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
838 	    res != TEE_ERROR_CORRUPT_OBJECT &&
839 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
840 		TEE_Panic(res);
841 
842 	return res;
843 }
844 
845 /* Data and Key Storage API  - Data Stream Access Functions */
846 
847 TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
848 			      uint32_t size, uint32_t *count)
849 {
850 	TEE_Result res;
851 	uint64_t cnt64;
852 
853 	if (object == TEE_HANDLE_NULL) {
854 		res = TEE_ERROR_BAD_PARAMETERS;
855 		goto out;
856 	}
857 	__utee_check_out_annotation(count, sizeof(*count));
858 
859 	cnt64 = *count;
860 	res = _utee_storage_obj_read((unsigned long)object, buffer, size,
861 				     &cnt64);
862 	*count = cnt64;
863 
864 out:
865 	if (res != TEE_SUCCESS &&
866 	    res != TEE_ERROR_CORRUPT_OBJECT &&
867 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
868 		TEE_Panic(res);
869 
870 	return res;
871 }
872 
873 TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, const void *buffer,
874 			       uint32_t size)
875 {
876 	TEE_Result res;
877 
878 	if (object == TEE_HANDLE_NULL) {
879 		res = TEE_ERROR_BAD_PARAMETERS;
880 		goto out;
881 	}
882 
883 	if (size > TEE_DATA_MAX_POSITION) {
884 		res = TEE_ERROR_OVERFLOW;
885 		goto out;
886 	}
887 
888 	res = _utee_storage_obj_write((unsigned long)object, buffer, size);
889 
890 out:
891 	if (res != TEE_SUCCESS &&
892 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
893 	    res != TEE_ERROR_OVERFLOW &&
894 	    res != TEE_ERROR_CORRUPT_OBJECT &&
895 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
896 		TEE_Panic(res);
897 
898 	return res;
899 }
900 
901 TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
902 {
903 	TEE_Result res;
904 
905 	if (object == TEE_HANDLE_NULL) {
906 		res = TEE_ERROR_BAD_PARAMETERS;
907 		goto out;
908 	}
909 
910 	res = _utee_storage_obj_trunc((unsigned long)object, size);
911 
912 out:
913 	if (res != TEE_SUCCESS &&
914 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
915 	    res != TEE_ERROR_CORRUPT_OBJECT &&
916 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
917 		TEE_Panic(res);
918 
919 	return res;
920 }
921 
922 TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
923 			      TEE_Whence whence)
924 {
925 	struct utee_object_info info = { };
926 	TEE_Result res = TEE_SUCCESS;
927 
928 	if (object == TEE_HANDLE_NULL) {
929 		res = TEE_ERROR_BAD_PARAMETERS;
930 		goto out;
931 	}
932 
933 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
934 	if (res != TEE_SUCCESS)
935 		goto out;
936 
937 	switch (whence) {
938 	case TEE_DATA_SEEK_SET:
939 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) {
940 			res = TEE_ERROR_OVERFLOW;
941 			goto out;
942 		}
943 		break;
944 	case TEE_DATA_SEEK_CUR:
945 		if (offset > 0 &&
946 		    ((uint32_t)offset + info.data_pos > TEE_DATA_MAX_POSITION ||
947 		     (uint32_t)offset + info.data_pos < info.data_pos)) {
948 			res = TEE_ERROR_OVERFLOW;
949 			goto out;
950 		}
951 		break;
952 	case TEE_DATA_SEEK_END:
953 		if (offset > 0 &&
954 		    ((uint32_t)offset + info.data_size >
955 		     TEE_DATA_MAX_POSITION ||
956 		     (uint32_t)offset + info.data_size < info.data_size)) {
957 			res = TEE_ERROR_OVERFLOW;
958 			goto out;
959 		}
960 		break;
961 	default:
962 		res = TEE_ERROR_ITEM_NOT_FOUND;
963 		goto out;
964 	}
965 
966 	res = _utee_storage_obj_seek((unsigned long)object, offset, whence);
967 
968 out:
969 	if (res != TEE_SUCCESS &&
970 	    res != TEE_ERROR_OVERFLOW &&
971 	    res != TEE_ERROR_CORRUPT_OBJECT &&
972 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
973 		TEE_Panic(res);
974 
975 	return res;
976 }
977