xref: /optee_os/lib/libutee/tee_api_objects.c (revision d10c4c4b2d7661c542400f174b7cd4499349385c)
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 (!objectID) {
413 		res = TEE_ERROR_ITEM_NOT_FOUND;
414 		goto out;
415 	}
416 
417 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) {
418 		res = TEE_ERROR_BAD_PARAMETERS;
419 		goto out;
420 	}
421 
422 	if (!object) {
423 		res = TEE_ERROR_BAD_PARAMETERS;
424 		goto out;
425 	}
426 
427 	res = utee_storage_obj_open(storageID, objectID, objectIDLen, flags,
428 				     &obj);
429 	if (res == TEE_SUCCESS)
430 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
431 
432 out:
433 	if (res != TEE_SUCCESS &&
434 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
435 	    res != TEE_ERROR_ACCESS_CONFLICT &&
436 	    res != TEE_ERROR_OUT_OF_MEMORY &&
437 	    res != TEE_ERROR_CORRUPT_OBJECT &&
438 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
439 		TEE_Panic(0);
440 
441 	return res;
442 }
443 
444 TEE_Result TEE_CreatePersistentObject(uint32_t storageID, void *objectID,
445 				      uint32_t objectIDLen, uint32_t flags,
446 				      TEE_ObjectHandle attributes,
447 				      const void *initialData,
448 				      uint32_t initialDataLen,
449 				      TEE_ObjectHandle *object)
450 {
451 	TEE_Result res;
452 	uint32_t obj;
453 
454 	if (!objectID) {
455 		res = TEE_ERROR_ITEM_NOT_FOUND;
456 		goto err;
457 	}
458 
459 	if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) {
460 		res = TEE_ERROR_BAD_PARAMETERS;
461 		goto err;
462 	}
463 
464 	res = utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
465 				      (unsigned long)attributes, initialData,
466 				      initialDataLen, &obj);
467 	if (res == TEE_SUCCESS) {
468 		if (object)
469 			*object = (TEE_ObjectHandle)(uintptr_t)obj;
470 		else
471 			res = utee_cryp_obj_close(obj);
472 		if (res == TEE_SUCCESS)
473 			goto out;
474 	}
475 err:
476 	if (object)
477 		*object = TEE_HANDLE_NULL;
478 	if (res == TEE_ERROR_ITEM_NOT_FOUND ||
479 	    res == TEE_ERROR_ACCESS_CONFLICT ||
480 	    res == TEE_ERROR_OUT_OF_MEMORY ||
481 	    res == TEE_ERROR_STORAGE_NO_SPACE ||
482 	    res == TEE_ERROR_CORRUPT_OBJECT ||
483 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE)
484 		return res;
485 	TEE_Panic(0);
486 out:
487 	return TEE_SUCCESS;
488 }
489 
490 /*
491  * Use of this function is deprecated
492  * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead
493  * These functions will be removed at some future major revision of
494  * this specification
495  */
496 void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
497 {
498 	TEE_Result res;
499 
500 	if (object == TEE_HANDLE_NULL)
501 		return;
502 
503 	res = TEE_CloseAndDeletePersistentObject1(object);
504 
505 	if (res != TEE_SUCCESS)
506 		TEE_Panic(0);
507 }
508 
509 TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object)
510 {
511 	TEE_Result res;
512 
513 	if (object == TEE_HANDLE_NULL)
514 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
515 
516 	res = utee_storage_obj_del((unsigned long)object);
517 
518 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
519 		TEE_Panic(0);
520 
521 	return res;
522 }
523 
524 
525 TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
526 				      const void *newObjectID,
527 				      uint32_t newObjectIDLen)
528 {
529 	TEE_Result res;
530 
531 	if (object == TEE_HANDLE_NULL) {
532 		res = TEE_ERROR_ITEM_NOT_FOUND;
533 		goto out;
534 	}
535 
536 	if (!newObjectID) {
537 		res = TEE_ERROR_BAD_PARAMETERS;
538 		goto out;
539 	}
540 
541 	if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN) {
542 		res = TEE_ERROR_BAD_PARAMETERS;
543 		goto out;
544 	}
545 
546 	res = utee_storage_obj_rename((unsigned long)object, newObjectID,
547 				      newObjectIDLen);
548 
549 out:
550 	if (res != TEE_SUCCESS &&
551 	    res != TEE_ERROR_ACCESS_CONFLICT &&
552 	    res != TEE_ERROR_CORRUPT_OBJECT &&
553 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
554 		TEE_Panic(0);
555 
556 	return res;
557 }
558 
559 TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
560 						  objectEnumerator)
561 {
562 	TEE_Result res;
563 	uint32_t oe;
564 
565 	if (!objectEnumerator)
566 		return TEE_ERROR_BAD_PARAMETERS;
567 
568 	res = utee_storage_alloc_enum(&oe);
569 
570 	if (res != TEE_SUCCESS)
571 		oe = TEE_HANDLE_NULL;
572 
573 	*objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe;
574 
575 	if (res != TEE_SUCCESS &&
576 	    res != TEE_ERROR_ACCESS_CONFLICT)
577 		TEE_Panic(0);
578 
579 	return res;
580 }
581 
582 void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
583 {
584 	TEE_Result res;
585 
586 	if (objectEnumerator == TEE_HANDLE_NULL)
587 		return;
588 
589 	res = utee_storage_free_enum((unsigned long)objectEnumerator);
590 
591 	if (res != TEE_SUCCESS)
592 		TEE_Panic(0);
593 }
594 
595 void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
596 {
597 	TEE_Result res;
598 
599 	if (objectEnumerator == TEE_HANDLE_NULL)
600 		return;
601 
602 	res = utee_storage_reset_enum((unsigned long)objectEnumerator);
603 
604 	if (res != TEE_SUCCESS)
605 		TEE_Panic(0);
606 }
607 
608 TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
609 					       objectEnumerator,
610 					       uint32_t storageID)
611 {
612 	TEE_Result res;
613 
614 	res = utee_storage_start_enum((unsigned long)objectEnumerator,
615 				      storageID);
616 
617 	if (res != TEE_SUCCESS &&
618 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
619 	    res != TEE_ERROR_CORRUPT_OBJECT &&
620 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
621 		TEE_Panic(0);
622 
623 	return res;
624 }
625 
626 TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
627 				       TEE_ObjectInfo *objectInfo,
628 				       void *objectID, uint32_t *objectIDLen)
629 {
630 	TEE_Result res;
631 	uint64_t len;
632 	TEE_ObjectInfo local_info;
633 	TEE_ObjectInfo *pt_info;
634 
635 	if (!objectID) {
636 		res = TEE_ERROR_BAD_PARAMETERS;
637 		goto out;
638 	}
639 
640 	if (!objectIDLen) {
641 		res = TEE_ERROR_BAD_PARAMETERS;
642 		goto out;
643 	}
644 
645 	if (objectInfo)
646 		pt_info = objectInfo;
647 	else
648 		pt_info = &local_info;
649 	len = *objectIDLen;
650 	res = utee_storage_next_enum((unsigned long)objectEnumerator,
651 				     pt_info, objectID, &len);
652 	*objectIDLen = len;
653 
654 out:
655 	if (res != TEE_SUCCESS &&
656 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
657 	    res != TEE_ERROR_CORRUPT_OBJECT &&
658 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
659 		TEE_Panic(0);
660 
661 	return res;
662 }
663 
664 /* Data and Key Storage API  - Data Stream Access Functions */
665 
666 TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
667 			      uint32_t size, uint32_t *count)
668 {
669 	TEE_Result res;
670 	uint64_t cnt64;
671 
672 	if (object == TEE_HANDLE_NULL) {
673 		res = TEE_ERROR_BAD_PARAMETERS;
674 		goto out;
675 	}
676 
677 	cnt64 = *count;
678 	res = utee_storage_obj_read((unsigned long)object, buffer, size,
679 				    &cnt64);
680 	*count = cnt64;
681 
682 out:
683 	if (res != TEE_SUCCESS &&
684 	    res != TEE_ERROR_CORRUPT_OBJECT &&
685 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
686 		TEE_Panic(0);
687 
688 	return res;
689 }
690 
691 TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, void *buffer,
692 			       uint32_t size)
693 {
694 	TEE_Result res;
695 
696 	if (object == TEE_HANDLE_NULL) {
697 		res = TEE_ERROR_BAD_PARAMETERS;
698 		goto out;
699 	}
700 
701 	if (size > TEE_DATA_MAX_POSITION) {
702 		res = TEE_ERROR_OVERFLOW;
703 		goto out;
704 	}
705 
706 	res = utee_storage_obj_write((unsigned long)object, buffer, size);
707 
708 out:
709 	if (res != TEE_SUCCESS &&
710 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
711 	    res != TEE_ERROR_OVERFLOW &&
712 	    res != TEE_ERROR_CORRUPT_OBJECT &&
713 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
714 		TEE_Panic(0);
715 
716 	return res;
717 }
718 
719 TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
720 {
721 	TEE_Result res;
722 
723 	if (object == TEE_HANDLE_NULL) {
724 		res = TEE_ERROR_BAD_PARAMETERS;
725 		goto out;
726 	}
727 
728 	res = utee_storage_obj_trunc((unsigned long)object, size);
729 
730 out:
731 	if (res != TEE_SUCCESS &&
732 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
733 	    res != TEE_ERROR_CORRUPT_OBJECT &&
734 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
735 		TEE_Panic(0);
736 
737 	return res;
738 }
739 
740 TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
741 			      TEE_Whence whence)
742 {
743 	TEE_Result res;
744 	TEE_ObjectInfo info;
745 
746 	if (object == TEE_HANDLE_NULL) {
747 		res = TEE_ERROR_BAD_PARAMETERS;
748 		goto out;
749 	}
750 
751 	res = utee_cryp_obj_get_info((unsigned long)object, &info);
752 	if (res != TEE_SUCCESS)
753 		goto out;
754 
755 	switch (whence) {
756 	case TEE_DATA_SEEK_SET:
757 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) {
758 			res = TEE_ERROR_OVERFLOW;
759 			goto out;
760 		}
761 		break;
762 	case TEE_DATA_SEEK_CUR:
763 		if (offset > 0 &&
764 		    ((uint32_t)offset + info.dataPosition >
765 		     TEE_DATA_MAX_POSITION ||
766 		     (uint32_t)offset + info.dataPosition <
767 		     info.dataPosition)) {
768 			res = TEE_ERROR_OVERFLOW;
769 			goto out;
770 		}
771 		break;
772 	case TEE_DATA_SEEK_END:
773 		if (offset > 0 &&
774 		    ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION ||
775 		     (uint32_t)offset + info.dataSize < info.dataSize)) {
776 			res = TEE_ERROR_OVERFLOW;
777 			goto out;
778 		}
779 		break;
780 	default:
781 		res = TEE_ERROR_ITEM_NOT_FOUND;
782 		goto out;
783 	}
784 
785 	res = utee_storage_obj_seek((unsigned long)object, offset, whence);
786 
787 out:
788 	if (res != TEE_SUCCESS &&
789 	    res != TEE_ERROR_OVERFLOW &&
790 	    res != TEE_ERROR_CORRUPT_OBJECT &&
791 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
792 		TEE_Panic(0);
793 
794 	return res;
795 }
796