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