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