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