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