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