xref: /optee_os/lib/libutee/tee_api_objects.c (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include <tee_api.h>
32 #include <utee_syscalls.h>
33 #include "tee_api_private.h"
34 
35 #define TEE_USAGE_DEFAULT   0xffffffff
36 
37 #define TEE_ATTR_BIT_VALUE                  (1 << 29)
38 #define TEE_ATTR_BIT_PROTECTED              (1 << 28)
39 
40 void __utee_from_attr(struct utee_attribute *ua, const TEE_Attribute *attrs,
41 			uint32_t attr_count)
42 {
43 	size_t n;
44 
45 	for (n = 0; n < attr_count; n++) {
46 		ua[n].attribute_id = attrs[n].attributeID;
47 		if (attrs[n].attributeID & TEE_ATTR_BIT_VALUE) {
48 			ua[n].a = attrs[n].content.value.a;
49 			ua[n].b = attrs[n].content.value.b;
50 		} else {
51 			ua[n].a = (uintptr_t)attrs[n].content.ref.buffer;
52 			ua[n].b = attrs[n].content.ref.length;
53 		}
54 	}
55 }
56 
57 /* Data and Key Storage API  - Generic Object Functions */
58 /*
59  * Use of this function is deprecated
60  * new code SHOULD use the TEE_GetObjectInfo1 function instead
61  * These functions will be removed at some future major revision of
62  * this specification
63  */
64 void TEE_GetObjectInfo(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo)
65 {
66 	TEE_Result res;
67 
68 	res = utee_cryp_obj_get_info((unsigned long)object, objectInfo);
69 
70 	if (res != TEE_SUCCESS)
71 		TEE_Panic(res);
72 
73 	if (objectInfo->objectType == TEE_TYPE_CORRUPTED_OBJECT) {
74 		objectInfo->keySize = 0;
75 		objectInfo->maxKeySize = 0;
76 		objectInfo->objectUsage = 0;
77 		objectInfo->dataSize = 0;
78 		objectInfo->dataPosition = 0;
79 		objectInfo->handleFlags = 0;
80 	}
81 }
82 
83 TEE_Result TEE_GetObjectInfo1(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo)
84 {
85 	TEE_Result res;
86 
87 	res = utee_cryp_obj_get_info((unsigned long)object, objectInfo);
88 
89 	if (res != TEE_SUCCESS &&
90 	    res != TEE_ERROR_CORRUPT_OBJECT &&
91 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
92 		TEE_Panic(res);
93 
94 	return res;
95 }
96 
97 /*
98  * Use of this function is deprecated
99  * new code SHOULD use the TEE_RestrictObjectUsage1 function instead
100  * These functions will be removed at some future major revision of
101  * this specification
102  */
103 void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage)
104 {
105 	TEE_Result res;
106 	TEE_ObjectInfo objectInfo;
107 
108 	res = utee_cryp_obj_get_info((unsigned long)object, &objectInfo);
109 	if (objectInfo.objectType == TEE_TYPE_CORRUPTED_OBJECT)
110 		return;
111 
112 	res = TEE_RestrictObjectUsage1(object, objectUsage);
113 
114 	if (res != TEE_SUCCESS)
115 		TEE_Panic(res);
116 }
117 
118 TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage)
119 {
120 	TEE_Result res;
121 
122 	res = utee_cryp_obj_restrict_usage((unsigned long)object, objectUsage);
123 
124 	if (res != TEE_SUCCESS &&
125 	    res != TEE_ERROR_CORRUPT_OBJECT &&
126 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
127 		TEE_Panic(res);
128 
129 	return res;
130 }
131 
132 TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object,
133 					uint32_t attributeID, void *buffer,
134 					uint32_t *size)
135 {
136 	TEE_Result res;
137 	TEE_ObjectInfo info;
138 	uint64_t sz;
139 
140 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
141 	if (res != TEE_SUCCESS)
142 		goto exit;
143 
144 	/* This function only supports reference attributes */
145 	if ((attributeID & TEE_ATTR_BIT_VALUE)) {
146 		res = TEE_ERROR_BAD_PARAMETERS;
147 		goto exit;
148 	}
149 
150 	sz = *size;
151 	res = utee_cryp_obj_get_attr((unsigned long)object, attributeID,
152 				     buffer, &sz);
153 	*size = sz;
154 
155 exit:
156 	if (res != TEE_SUCCESS &&
157 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
158 	    res != TEE_ERROR_SHORT_BUFFER &&
159 	    res != TEE_ERROR_CORRUPT_OBJECT &&
160 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
161 		TEE_Panic(res);
162 
163 	return res;
164 }
165 
166 TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object,
167 				       uint32_t attributeID, uint32_t *a,
168 				       uint32_t *b)
169 {
170 	TEE_Result res;
171 	TEE_ObjectInfo info;
172 	uint32_t buf[2];
173 	uint64_t size = sizeof(buf);
174 
175 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
176 	if (res != TEE_SUCCESS)
177 		goto exit;
178 
179 	/* This function only supports value attributes */
180 	if (!(attributeID & TEE_ATTR_BIT_VALUE)) {
181 		res = TEE_ERROR_BAD_PARAMETERS;
182 		goto exit;
183 	}
184 
185 	res = utee_cryp_obj_get_attr((unsigned long)object, attributeID, buf,
186 				     &size);
187 
188 exit:
189 	if (res != TEE_SUCCESS &&
190 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
191 	    res != TEE_ERROR_CORRUPT_OBJECT &&
192 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
193 		TEE_Panic(res);
194 
195 	if (size != sizeof(buf))
196 		TEE_Panic(0);
197 
198 	if (res == TEE_SUCCESS) {
199 		if (a)
200 			*a = buf[0];
201 		if (b)
202 			*b = buf[1];
203 	}
204 
205 	return res;
206 }
207 
208 void TEE_CloseObject(TEE_ObjectHandle object)
209 {
210 	TEE_Result res;
211 
212 	if (object == TEE_HANDLE_NULL)
213 		return;
214 
215 	res = utee_cryp_obj_close((unsigned long)object);
216 	if (res != TEE_SUCCESS)
217 		TEE_Panic(res);
218 }
219 
220 /* Data and Key Storage API  - Transient Object Functions */
221 
222 TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType,
223 				       uint32_t maxKeySize,
224 				       TEE_ObjectHandle *object)
225 {
226 	TEE_Result res;
227 	uint32_t obj;
228 
229 	res = utee_cryp_obj_alloc(objectType, maxKeySize, &obj);
230 
231 	if (res != TEE_SUCCESS &&
232 	    res != TEE_ERROR_OUT_OF_MEMORY &&
233 	    res != TEE_ERROR_NOT_SUPPORTED)
234 		TEE_Panic(res);
235 
236 	if (res == TEE_SUCCESS)
237 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
238 
239 	return res;
240 }
241 
242 void TEE_FreeTransientObject(TEE_ObjectHandle object)
243 {
244 	TEE_Result res;
245 	TEE_ObjectInfo info;
246 
247 	if (object == TEE_HANDLE_NULL)
248 		return;
249 
250 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
251 	if (res != TEE_SUCCESS)
252 		TEE_Panic(res);
253 
254 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
255 		TEE_Panic(0);
256 
257 	res = utee_cryp_obj_close((unsigned long)object);
258 	if (res != TEE_SUCCESS)
259 		TEE_Panic(res);
260 }
261 
262 void TEE_ResetTransientObject(TEE_ObjectHandle object)
263 {
264 	TEE_Result res;
265 	TEE_ObjectInfo info;
266 
267 	if (object == TEE_HANDLE_NULL)
268 		return;
269 
270 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
271 	if (res != TEE_SUCCESS)
272 		TEE_Panic(res);
273 
274 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
275 		TEE_Panic(0);
276 
277 	res = utee_cryp_obj_reset((unsigned long)object);
278 	if (res != TEE_SUCCESS)
279 		TEE_Panic(res);
280 }
281 
282 TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object,
283 				       const TEE_Attribute *attrs,
284 				       uint32_t attrCount)
285 {
286 	TEE_Result res;
287 	TEE_ObjectInfo info;
288 	struct utee_attribute ua[attrCount];
289 
290 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
291 	if (res != TEE_SUCCESS)
292 		TEE_Panic(res);
293 
294 	/* Must be a transient object */
295 	if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
296 		TEE_Panic(0);
297 
298 	/* Must not be initialized already */
299 	if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
300 		TEE_Panic(0);
301 
302 	__utee_from_attr(ua, attrs, attrCount);
303 	res = utee_cryp_obj_populate((unsigned long)object, ua, attrCount);
304 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
305 		TEE_Panic(res);
306 	return res;
307 }
308 
309 void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID,
310 			  const void *buffer, uint32_t length)
311 {
312 	if (attr == NULL)
313 		TEE_Panic(0);
314 	if ((attributeID & TEE_ATTR_BIT_VALUE) != 0)
315 		TEE_Panic(0);
316 	attr->attributeID = attributeID;
317 	attr->content.ref.buffer = (void *)buffer;
318 	attr->content.ref.length = length;
319 }
320 
321 void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID,
322 			    uint32_t a, uint32_t b)
323 {
324 	if (attr == NULL)
325 		TEE_Panic(0);
326 	if ((attributeID & TEE_ATTR_BIT_VALUE) == 0)
327 		TEE_Panic(0);
328 	attr->attributeID = attributeID;
329 	attr->content.value.a = a;
330 	attr->content.value.b = b;
331 }
332 
333 /*
334  * Use of this function is deprecated
335  * new code SHOULD use the TEE_CopyObjectAttributes1 function instead
336  * These functions will be removed at some future major revision of
337  * this specification
338  */
339 void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject,
340 			      TEE_ObjectHandle srcObject)
341 {
342 	TEE_Result res;
343 	TEE_ObjectInfo src_info;
344 
345 	res = utee_cryp_obj_get_info((unsigned long)srcObject, &src_info);
346 	if (src_info.objectType == TEE_TYPE_CORRUPTED_OBJECT)
347 		return;
348 
349 	res = TEE_CopyObjectAttributes1(destObject, srcObject);
350 	if (res != TEE_SUCCESS)
351 		TEE_Panic(res);
352 }
353 
354 TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject,
355 			      TEE_ObjectHandle srcObject)
356 {
357 	TEE_Result res;
358 	TEE_ObjectInfo dst_info;
359 	TEE_ObjectInfo src_info;
360 
361 	res = utee_cryp_obj_get_info((unsigned long)destObject, &dst_info);
362 	if (res != TEE_SUCCESS)
363 		goto exit;
364 
365 	res = utee_cryp_obj_get_info((unsigned long)srcObject, &src_info);
366 	if (res != TEE_SUCCESS)
367 		goto exit;
368 
369 	if (!(src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED))
370 		TEE_Panic(0);
371 
372 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT))
373 		TEE_Panic(0);
374 
375 	if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED))
376 		TEE_Panic(0);
377 
378 	res = utee_cryp_obj_copy((unsigned long)destObject,
379 				 (unsigned long)srcObject);
380 
381 exit:
382 	if (res != TEE_SUCCESS &&
383 	    res != TEE_ERROR_CORRUPT_OBJECT &&
384 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
385 		TEE_Panic(res);
386 
387 	return res;
388 }
389 
390 TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
391 			   const TEE_Attribute *params, uint32_t paramCount)
392 {
393 	TEE_Result res;
394 	struct utee_attribute ua[paramCount];
395 
396 	__utee_from_attr(ua, params, paramCount);
397 	res = utee_cryp_obj_generate_key((unsigned long)object, keySize,
398 					 ua, paramCount);
399 
400 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
401 		TEE_Panic(res);
402 
403 	return res;
404 }
405 
406 /* Data and Key Storage API  - Persistent Object Functions */
407 
408 TEE_Result TEE_OpenPersistentObject(uint32_t storageID, const void *objectID,
409 				    uint32_t objectIDLen, uint32_t flags,
410 				    TEE_ObjectHandle *object)
411 {
412 	TEE_Result res;
413 	uint32_t obj;
414 
415 	if (!objectID) {
416 		res = TEE_ERROR_ITEM_NOT_FOUND;
417 		goto out;
418 	}
419 
420 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) {
421 		res = TEE_ERROR_BAD_PARAMETERS;
422 		goto out;
423 	}
424 
425 	if (!object) {
426 		res = TEE_ERROR_BAD_PARAMETERS;
427 		goto out;
428 	}
429 
430 	res = utee_storage_obj_open(storageID, objectID, objectIDLen, flags,
431 				     &obj);
432 	if (res == TEE_SUCCESS)
433 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
434 
435 out:
436 	if (res != TEE_SUCCESS &&
437 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
438 	    res != TEE_ERROR_ACCESS_CONFLICT &&
439 	    res != TEE_ERROR_OUT_OF_MEMORY &&
440 	    res != TEE_ERROR_CORRUPT_OBJECT &&
441 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
442 		TEE_Panic(res);
443 
444 	return res;
445 }
446 
447 TEE_Result TEE_CreatePersistentObject(uint32_t storageID, const void *objectID,
448 				      uint32_t objectIDLen, uint32_t flags,
449 				      TEE_ObjectHandle attributes,
450 				      const void *initialData,
451 				      uint32_t initialDataLen,
452 				      TEE_ObjectHandle *object)
453 {
454 	TEE_Result res;
455 	uint32_t obj;
456 
457 	if (!objectID) {
458 		res = TEE_ERROR_ITEM_NOT_FOUND;
459 		goto err;
460 	}
461 
462 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) {
463 		res = TEE_ERROR_BAD_PARAMETERS;
464 		goto err;
465 	}
466 
467 	res = utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
468 				      (unsigned long)attributes, initialData,
469 				      initialDataLen, &obj);
470 	if (res == TEE_SUCCESS) {
471 		if (object)
472 			*object = (TEE_ObjectHandle)(uintptr_t)obj;
473 		else
474 			res = utee_cryp_obj_close(obj);
475 		if (res == TEE_SUCCESS)
476 			goto out;
477 	}
478 err:
479 	if (object)
480 		*object = TEE_HANDLE_NULL;
481 	if (res == TEE_ERROR_ITEM_NOT_FOUND ||
482 	    res == TEE_ERROR_ACCESS_CONFLICT ||
483 	    res == TEE_ERROR_OUT_OF_MEMORY ||
484 	    res == TEE_ERROR_STORAGE_NO_SPACE ||
485 	    res == TEE_ERROR_CORRUPT_OBJECT ||
486 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE)
487 		return res;
488 	TEE_Panic(res);
489 out:
490 	return TEE_SUCCESS;
491 }
492 
493 /*
494  * Use of this function is deprecated
495  * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead
496  * These functions will be removed at some future major revision of
497  * this specification
498  */
499 void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
500 {
501 	TEE_Result res;
502 
503 	if (object == TEE_HANDLE_NULL)
504 		return;
505 
506 	res = TEE_CloseAndDeletePersistentObject1(object);
507 
508 	if (res != TEE_SUCCESS)
509 		TEE_Panic(0);
510 }
511 
512 TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object)
513 {
514 	TEE_Result res;
515 
516 	if (object == TEE_HANDLE_NULL)
517 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
518 
519 	res = utee_storage_obj_del((unsigned long)object);
520 
521 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
522 		TEE_Panic(res);
523 
524 	return res;
525 }
526 
527 
528 TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
529 				      const void *newObjectID,
530 				      uint32_t newObjectIDLen)
531 {
532 	TEE_Result res;
533 
534 	if (object == TEE_HANDLE_NULL) {
535 		res = TEE_ERROR_ITEM_NOT_FOUND;
536 		goto out;
537 	}
538 
539 	if (!newObjectID) {
540 		res = TEE_ERROR_BAD_PARAMETERS;
541 		goto out;
542 	}
543 
544 	if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN) {
545 		res = TEE_ERROR_BAD_PARAMETERS;
546 		goto out;
547 	}
548 
549 	res = utee_storage_obj_rename((unsigned long)object, newObjectID,
550 				      newObjectIDLen);
551 
552 out:
553 	if (res != TEE_SUCCESS &&
554 	    res != TEE_ERROR_ACCESS_CONFLICT &&
555 	    res != TEE_ERROR_CORRUPT_OBJECT &&
556 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
557 		TEE_Panic(res);
558 
559 	return res;
560 }
561 
562 TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
563 						  objectEnumerator)
564 {
565 	TEE_Result res;
566 	uint32_t oe;
567 
568 	if (!objectEnumerator)
569 		return TEE_ERROR_BAD_PARAMETERS;
570 
571 	res = utee_storage_alloc_enum(&oe);
572 
573 	if (res != TEE_SUCCESS)
574 		oe = TEE_HANDLE_NULL;
575 
576 	*objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe;
577 
578 	if (res != TEE_SUCCESS &&
579 	    res != TEE_ERROR_ACCESS_CONFLICT)
580 		TEE_Panic(res);
581 
582 	return res;
583 }
584 
585 void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
586 {
587 	TEE_Result res;
588 
589 	if (objectEnumerator == TEE_HANDLE_NULL)
590 		return;
591 
592 	res = utee_storage_free_enum((unsigned long)objectEnumerator);
593 
594 	if (res != TEE_SUCCESS)
595 		TEE_Panic(res);
596 }
597 
598 void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
599 {
600 	TEE_Result res;
601 
602 	if (objectEnumerator == TEE_HANDLE_NULL)
603 		return;
604 
605 	res = utee_storage_reset_enum((unsigned long)objectEnumerator);
606 
607 	if (res != TEE_SUCCESS)
608 		TEE_Panic(res);
609 }
610 
611 TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
612 					       objectEnumerator,
613 					       uint32_t storageID)
614 {
615 	TEE_Result res;
616 
617 	res = utee_storage_start_enum((unsigned long)objectEnumerator,
618 				      storageID);
619 
620 	if (res != TEE_SUCCESS &&
621 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
622 	    res != TEE_ERROR_CORRUPT_OBJECT &&
623 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
624 		TEE_Panic(res);
625 
626 	return res;
627 }
628 
629 TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
630 				       TEE_ObjectInfo *objectInfo,
631 				       void *objectID, uint32_t *objectIDLen)
632 {
633 	TEE_Result res;
634 	uint64_t len;
635 	TEE_ObjectInfo local_info;
636 	TEE_ObjectInfo *pt_info;
637 
638 	if (!objectID) {
639 		res = TEE_ERROR_BAD_PARAMETERS;
640 		goto out;
641 	}
642 
643 	if (!objectIDLen) {
644 		res = TEE_ERROR_BAD_PARAMETERS;
645 		goto out;
646 	}
647 
648 	if (objectInfo)
649 		pt_info = objectInfo;
650 	else
651 		pt_info = &local_info;
652 	len = *objectIDLen;
653 	res = utee_storage_next_enum((unsigned long)objectEnumerator,
654 				     pt_info, objectID, &len);
655 	*objectIDLen = len;
656 
657 out:
658 	if (res != TEE_SUCCESS &&
659 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
660 	    res != TEE_ERROR_CORRUPT_OBJECT &&
661 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
662 		TEE_Panic(res);
663 
664 	return res;
665 }
666 
667 /* Data and Key Storage API  - Data Stream Access Functions */
668 
669 TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
670 			      uint32_t size, uint32_t *count)
671 {
672 	TEE_Result res;
673 	uint64_t cnt64;
674 
675 	if (object == TEE_HANDLE_NULL) {
676 		res = TEE_ERROR_BAD_PARAMETERS;
677 		goto out;
678 	}
679 
680 	cnt64 = *count;
681 	res = utee_storage_obj_read((unsigned long)object, buffer, size,
682 				    &cnt64);
683 	*count = cnt64;
684 
685 out:
686 	if (res != TEE_SUCCESS &&
687 	    res != TEE_ERROR_CORRUPT_OBJECT &&
688 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
689 		TEE_Panic(res);
690 
691 	return res;
692 }
693 
694 TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, const void *buffer,
695 			       uint32_t size)
696 {
697 	TEE_Result res;
698 
699 	if (object == TEE_HANDLE_NULL) {
700 		res = TEE_ERROR_BAD_PARAMETERS;
701 		goto out;
702 	}
703 
704 	if (size > TEE_DATA_MAX_POSITION) {
705 		res = TEE_ERROR_OVERFLOW;
706 		goto out;
707 	}
708 
709 	res = utee_storage_obj_write((unsigned long)object, buffer, size);
710 
711 out:
712 	if (res != TEE_SUCCESS &&
713 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
714 	    res != TEE_ERROR_OVERFLOW &&
715 	    res != TEE_ERROR_CORRUPT_OBJECT &&
716 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
717 		TEE_Panic(res);
718 
719 	return res;
720 }
721 
722 TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
723 {
724 	TEE_Result res;
725 
726 	if (object == TEE_HANDLE_NULL) {
727 		res = TEE_ERROR_BAD_PARAMETERS;
728 		goto out;
729 	}
730 
731 	res = utee_storage_obj_trunc((unsigned long)object, size);
732 
733 out:
734 	if (res != TEE_SUCCESS &&
735 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
736 	    res != TEE_ERROR_CORRUPT_OBJECT &&
737 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
738 		TEE_Panic(res);
739 
740 	return res;
741 }
742 
743 TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
744 			      TEE_Whence whence)
745 {
746 	TEE_Result res;
747 	TEE_ObjectInfo info;
748 
749 	if (object == TEE_HANDLE_NULL) {
750 		res = TEE_ERROR_BAD_PARAMETERS;
751 		goto out;
752 	}
753 
754 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
755 	if (res != TEE_SUCCESS)
756 		goto out;
757 
758 	switch (whence) {
759 	case TEE_DATA_SEEK_SET:
760 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) {
761 			res = TEE_ERROR_OVERFLOW;
762 			goto out;
763 		}
764 		break;
765 	case TEE_DATA_SEEK_CUR:
766 		if (offset > 0 &&
767 		    ((uint32_t)offset + info.dataPosition >
768 		     TEE_DATA_MAX_POSITION ||
769 		     (uint32_t)offset + info.dataPosition <
770 		     info.dataPosition)) {
771 			res = TEE_ERROR_OVERFLOW;
772 			goto out;
773 		}
774 		break;
775 	case TEE_DATA_SEEK_END:
776 		if (offset > 0 &&
777 		    ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION ||
778 		     (uint32_t)offset + info.dataSize < info.dataSize)) {
779 			res = TEE_ERROR_OVERFLOW;
780 			goto out;
781 		}
782 		break;
783 	default:
784 		res = TEE_ERROR_ITEM_NOT_FOUND;
785 		goto out;
786 	}
787 
788 	res = utee_storage_obj_seek((unsigned long)object, offset, whence);
789 
790 out:
791 	if (res != TEE_SUCCESS &&
792 	    res != TEE_ERROR_OVERFLOW &&
793 	    res != TEE_ERROR_CORRUPT_OBJECT &&
794 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
795 		TEE_Panic(res);
796 
797 	return res;
798 }
799