xref: /optee_os/lib/libutee/tee_api_objects.c (revision 9b520646177e9a32bde160e09a65abaafe572dea)
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 		return TEE_ERROR_ITEM_NOT_FOUND;
527 
528 	if (newObjectID == NULL)
529 		return TEE_ERROR_BAD_PARAMETERS;
530 
531 	if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN)
532 		TEE_Panic(0);
533 
534 	res = utee_storage_obj_rename(object, newObjectID, newObjectIDLen);
535 
536 	if (res != TEE_SUCCESS && res != TEE_ERROR_ACCESS_CONFLICT)
537 		TEE_Panic(0);
538 
539 	return res;
540 }
541 
542 TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
543 						  objectEnumerator)
544 {
545 	TEE_Result res;
546 
547 	if (objectEnumerator == NULL)
548 		return TEE_ERROR_BAD_PARAMETERS;
549 
550 	res = utee_storage_alloc_enum(objectEnumerator);
551 
552 	if (res != TEE_SUCCESS)
553 		*objectEnumerator = TEE_HANDLE_NULL;
554 
555 	return res;
556 }
557 
558 void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
559 {
560 	TEE_Result res;
561 
562 	if (objectEnumerator == TEE_HANDLE_NULL)
563 		return;
564 
565 	res = utee_storage_free_enum(objectEnumerator);
566 
567 	if (res != TEE_SUCCESS)
568 		TEE_Panic(0);
569 }
570 
571 void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
572 {
573 	TEE_Result res;
574 
575 	if (objectEnumerator == TEE_HANDLE_NULL)
576 		return;
577 
578 	res = utee_storage_reset_enum(objectEnumerator);
579 
580 	if (res != TEE_SUCCESS)
581 		TEE_Panic(0);
582 }
583 
584 TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
585 					       objectEnumerator,
586 					       uint32_t storageID)
587 {
588 	TEE_Result res;
589 
590 	if (storageID != TEE_STORAGE_PRIVATE)
591 		return TEE_ERROR_ITEM_NOT_FOUND;
592 
593 	res = utee_storage_start_enum(objectEnumerator, storageID);
594 
595 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND)
596 		TEE_Panic(0);
597 
598 	return res;
599 }
600 
601 TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
602 				       TEE_ObjectInfo *objectInfo,
603 				       void *objectID, uint32_t *objectIDLen)
604 {
605 	TEE_Result res;
606 
607 	res = utee_storage_next_enum(objectEnumerator, objectInfo, objectID,
608 				     objectIDLen);
609 
610 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND)
611 		TEE_Panic(0);
612 
613 	return res;
614 }
615 
616 /* Data and Key Storage API  - Data Stream Access Functions */
617 
618 TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
619 			      uint32_t size, uint32_t *count)
620 {
621 	TEE_Result res;
622 
623 	if (object == TEE_HANDLE_NULL) {
624 		res = TEE_ERROR_BAD_PARAMETERS;
625 		goto out;
626 	}
627 
628 	res = utee_storage_obj_read(object, buffer, size, count);
629 
630 out:
631 	if (res != TEE_SUCCESS &&
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_WriteObjectData(TEE_ObjectHandle object, void *buffer,
640 			       uint32_t size)
641 {
642 	TEE_Result res;
643 
644 	if (object == TEE_HANDLE_NULL) {
645 		res = TEE_ERROR_BAD_PARAMETERS;
646 		goto out;
647 	}
648 
649 	if (size > TEE_DATA_MAX_POSITION) {
650 		res = TEE_ERROR_OVERFLOW;
651 		goto out;
652 	}
653 
654 	res = utee_storage_obj_write(object, buffer, size);
655 
656 out:
657 	if (res != TEE_SUCCESS &&
658 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
659 	    res != TEE_ERROR_OVERFLOW &&
660 	    res != TEE_ERROR_CORRUPT_OBJECT &&
661 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
662 		TEE_Panic(0);
663 
664 	return res;
665 }
666 
667 TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
668 {
669 	TEE_Result res;
670 
671 	if (object == TEE_HANDLE_NULL) {
672 		res = TEE_ERROR_BAD_PARAMETERS;
673 		goto out;
674 	}
675 
676 	res = utee_storage_obj_trunc(object, size);
677 
678 out:
679 	if (res != TEE_SUCCESS &&
680 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
681 	    res != TEE_ERROR_CORRUPT_OBJECT &&
682 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
683 		TEE_Panic(0);
684 
685 	return res;
686 }
687 
688 TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
689 			      TEE_Whence whence)
690 {
691 	TEE_Result res;
692 	TEE_ObjectInfo info;
693 
694 	if (object == TEE_HANDLE_NULL) {
695 		res = TEE_ERROR_BAD_PARAMETERS;
696 		goto out;
697 	}
698 
699 	res = utee_cryp_obj_get_info((uint32_t)object, &info);
700 	if (res != TEE_SUCCESS)
701 		goto out;
702 
703 	switch (whence) {
704 	case TEE_DATA_SEEK_SET:
705 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) {
706 			res = TEE_ERROR_OVERFLOW;
707 			goto out;
708 		}
709 		break;
710 	case TEE_DATA_SEEK_CUR:
711 		if (offset > 0 &&
712 		    ((uint32_t)offset + info.dataPosition >
713 		     TEE_DATA_MAX_POSITION ||
714 		     (uint32_t)offset + info.dataPosition <
715 		     info.dataPosition)) {
716 			res = TEE_ERROR_OVERFLOW;
717 			goto out;
718 		}
719 		break;
720 	case TEE_DATA_SEEK_END:
721 		if (offset > 0 &&
722 		    ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION ||
723 		     (uint32_t)offset + info.dataSize < info.dataSize)) {
724 			res = TEE_ERROR_OVERFLOW;
725 			goto out;
726 		}
727 		break;
728 	default:
729 		res = TEE_ERROR_ITEM_NOT_FOUND;
730 		goto out;
731 	}
732 
733 	res = utee_storage_obj_seek(object, offset, whence);
734 
735 out:
736 	if (res != TEE_SUCCESS &&
737 	    res != TEE_ERROR_OVERFLOW &&
738 	    res != TEE_ERROR_CORRUPT_OBJECT &&
739 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
740 		TEE_Panic(0);
741 
742 	return res;
743 }
744