xref: /optee_os/lib/libutee/tee_api_objects.c (revision 4f4374c8555bb3308d5d646b41df5e53c52ee7e5)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 #include <stdlib.h>
6 #include <string.h>
7 
8 #include <tee_api.h>
9 #include <utee_syscalls.h>
10 #include "tee_api_private.h"
11 
12 #define TEE_USAGE_DEFAULT   0xffffffff
13 
14 void __utee_from_attr(struct utee_attribute *ua, const TEE_Attribute *attrs,
15 			uint32_t attr_count)
16 {
17 	size_t n;
18 
19 	for (n = 0; n < attr_count; n++) {
20 		ua[n].attribute_id = attrs[n].attributeID;
21 		if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE) {
22 			ua[n].a = attrs[n].content.value.a;
23 			ua[n].b = attrs[n].content.value.b;
24 		} else {
25 			ua[n].a = (uintptr_t)attrs[n].content.ref.buffer;
26 			ua[n].b = attrs[n].content.ref.length;
27 		}
28 	}
29 }
30 
31 void __utee_from_gp11_attr(struct utee_attribute *ua,
32 			   const __GP11_TEE_Attribute *attrs,
33 			   uint32_t attr_count)
34 {
35 	size_t n = 0;
36 
37 	for (n = 0; n < attr_count; n++) {
38 		ua[n].attribute_id = attrs[n].attributeID;
39 		if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE) {
40 			ua[n].a = attrs[n].content.value.a;
41 			ua[n].b = attrs[n].content.value.b;
42 		} else {
43 			ua[n].a = (uintptr_t)attrs[n].content.ref.buffer;
44 			ua[n].b = attrs[n].content.ref.length;
45 		}
46 	}
47 }
48 
49 /* Data and Key Storage API  - Generic Object Functions */
50 /*
51  * Use of this function is deprecated
52  * new code SHOULD use the TEE_GetObjectInfo1 function instead
53  * These functions will be removed at some future major revision of
54  * this specification
55  */
56 void TEE_GetObjectInfo(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo)
57 {
58 	struct utee_object_info info = { };
59 	TEE_Result res = TEE_SUCCESS;
60 
61 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
62 
63 	if (res != TEE_SUCCESS)
64 		TEE_Panic(res);
65 
66 	if (info.obj_type == TEE_TYPE_CORRUPTED_OBJECT) {
67 		objectInfo->objectSize = 0;
68 		objectInfo->maxObjectSize = 0;
69 		objectInfo->objectUsage = 0;
70 		objectInfo->dataSize = 0;
71 		objectInfo->dataPosition = 0;
72 		objectInfo->handleFlags = 0;
73 	} else {
74 		objectInfo->objectType = info.obj_type;
75 		objectInfo->objectSize = info.obj_size;
76 		objectInfo->maxObjectSize = info.max_obj_size;
77 		objectInfo->objectUsage = info.obj_usage;
78 		objectInfo->dataSize = info.data_size;
79 		objectInfo->dataPosition = info.data_pos;
80 		objectInfo->handleFlags = info.handle_flags;
81 	}
82 }
83 
84 void __GP11_TEE_GetObjectInfo(TEE_ObjectHandle object,
85 			      __GP11_TEE_ObjectInfo *objectInfo)
86 {
87 	struct utee_object_info info = { };
88 	TEE_Result res = TEE_SUCCESS;
89 
90 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
91 
92 	if (res != TEE_SUCCESS)
93 		TEE_Panic(res);
94 
95 	if (info.obj_type == TEE_TYPE_CORRUPTED_OBJECT) {
96 		objectInfo->keySize = 0;
97 		objectInfo->maxKeySize = 0;
98 		objectInfo->objectUsage = 0;
99 		objectInfo->dataSize = 0;
100 		objectInfo->dataPosition = 0;
101 		objectInfo->handleFlags = 0;
102 	} else {
103 		objectInfo->objectType = info.obj_type;
104 		objectInfo->keySize = info.obj_size;
105 		objectInfo->maxKeySize = info.max_obj_size;
106 		objectInfo->objectUsage = info.obj_usage;
107 		objectInfo->dataSize = info.data_size;
108 		objectInfo->dataPosition = info.data_pos;
109 		objectInfo->handleFlags = info.handle_flags;
110 	}
111 }
112 
113 TEE_Result TEE_GetObjectInfo1(TEE_ObjectHandle object,
114 			      TEE_ObjectInfo *objectInfo)
115 {
116 	struct utee_object_info info = { };
117 	TEE_Result res = TEE_SUCCESS;
118 
119 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
120 
121 	if (res != TEE_SUCCESS &&
122 	    res != TEE_ERROR_CORRUPT_OBJECT &&
123 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
124 		TEE_Panic(res);
125 
126 	objectInfo->objectType = info.obj_type;
127 	objectInfo->objectSize = info.obj_size;
128 	objectInfo->maxObjectSize = info.max_obj_size;
129 	objectInfo->objectUsage = info.obj_usage;
130 	objectInfo->dataSize = info.data_size;
131 	objectInfo->dataPosition = info.data_pos;
132 	objectInfo->handleFlags = info.handle_flags;
133 
134 	return res;
135 }
136 
137 TEE_Result __GP11_TEE_GetObjectInfo1(TEE_ObjectHandle object,
138 				     __GP11_TEE_ObjectInfo *objectInfo)
139 {
140 	struct utee_object_info info = { };
141 	TEE_Result res = TEE_SUCCESS;
142 
143 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
144 
145 	if (res != TEE_SUCCESS &&
146 	    res != TEE_ERROR_CORRUPT_OBJECT &&
147 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
148 		TEE_Panic(res);
149 
150 	objectInfo->objectType = info.obj_type;
151 	objectInfo->keySize = info.obj_size;
152 	objectInfo->maxKeySize = info.max_obj_size;
153 	objectInfo->objectUsage = info.obj_usage;
154 	objectInfo->dataSize = info.data_size;
155 	objectInfo->dataPosition = info.data_pos;
156 	objectInfo->handleFlags = info.handle_flags;
157 
158 	return res;
159 }
160 
161 /*
162  * Use of this function is deprecated
163  * new code SHOULD use the TEE_RestrictObjectUsage1 function instead
164  * These functions will be removed at some future major revision of
165  * this specification
166  */
167 void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage)
168 {
169 	struct utee_object_info info = { };
170 	TEE_Result res = TEE_SUCCESS;
171 
172 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
173 	if (info.obj_type == TEE_TYPE_CORRUPTED_OBJECT)
174 		return;
175 
176 	res = TEE_RestrictObjectUsage1(object, objectUsage);
177 
178 	if (res != TEE_SUCCESS)
179 		TEE_Panic(res);
180 }
181 
182 TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage)
183 {
184 	TEE_Result res;
185 
186 	res = _utee_cryp_obj_restrict_usage((unsigned long)object,
187 					    objectUsage);
188 
189 	if (res != TEE_SUCCESS &&
190 	    res != TEE_ERROR_CORRUPT_OBJECT &&
191 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
192 		TEE_Panic(res);
193 
194 	return res;
195 }
196 
197 TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object,
198 					uint32_t attributeID, void *buffer,
199 					uint32_t *size)
200 {
201 	struct utee_object_info info = { };
202 	TEE_Result res = TEE_SUCCESS;
203 	uint64_t sz = 0;
204 
205 	__utee_check_inout_annotation(size, sizeof(*size));
206 
207 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
208 	if (res != TEE_SUCCESS)
209 		goto exit;
210 
211 	/* This function only supports reference attributes */
212 	if ((attributeID & TEE_ATTR_FLAG_VALUE)) {
213 		res = TEE_ERROR_BAD_PARAMETERS;
214 		goto exit;
215 	}
216 
217 	sz = *size;
218 	res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID,
219 				      buffer, &sz);
220 	*size = sz;
221 
222 exit:
223 	if (res != TEE_SUCCESS &&
224 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
225 	    res != TEE_ERROR_SHORT_BUFFER &&
226 	    res != TEE_ERROR_CORRUPT_OBJECT &&
227 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
228 		TEE_Panic(res);
229 
230 	return res;
231 }
232 
233 TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object,
234 				       uint32_t attributeID, uint32_t *a,
235 				       uint32_t *b)
236 {
237 	struct utee_object_info info = { };
238 	TEE_Result res = TEE_SUCCESS;
239 	uint32_t buf[2];
240 	uint64_t size = sizeof(buf);
241 
242 	if (a)
243 		__utee_check_out_annotation(a, sizeof(*a));
244 	if (b)
245 		__utee_check_out_annotation(b, sizeof(*b));
246 
247 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
248 	if (res != TEE_SUCCESS)
249 		goto exit;
250 
251 	/* This function only supports value attributes */
252 	if (!(attributeID & TEE_ATTR_FLAG_VALUE)) {
253 		res = TEE_ERROR_BAD_PARAMETERS;
254 		goto exit;
255 	}
256 
257 	res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID, buf,
258 				      &size);
259 
260 exit:
261 	if (res != TEE_SUCCESS &&
262 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
263 	    res != TEE_ERROR_CORRUPT_OBJECT &&
264 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
265 		TEE_Panic(res);
266 
267 	if (size != sizeof(buf))
268 		TEE_Panic(0);
269 
270 	if (res == TEE_SUCCESS) {
271 		if (a)
272 			*a = buf[0];
273 		if (b)
274 			*b = buf[1];
275 	}
276 
277 	return res;
278 }
279 
280 void TEE_CloseObject(TEE_ObjectHandle object)
281 {
282 	TEE_Result res;
283 
284 	if (object == TEE_HANDLE_NULL)
285 		return;
286 
287 	res = _utee_cryp_obj_close((unsigned long)object);
288 	if (res != TEE_SUCCESS)
289 		TEE_Panic(res);
290 }
291 
292 /* Data and Key Storage API  - Transient Object Functions */
293 
294 TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType,
295 				       uint32_t maxKeySize,
296 				       TEE_ObjectHandle *object)
297 {
298 	TEE_Result res;
299 	uint32_t obj;
300 
301 	__utee_check_out_annotation(object, sizeof(*object));
302 
303 	res = _utee_cryp_obj_alloc(objectType, maxKeySize, &obj);
304 
305 	if (res != TEE_SUCCESS &&
306 	    res != TEE_ERROR_OUT_OF_MEMORY &&
307 	    res != TEE_ERROR_NOT_SUPPORTED)
308 		TEE_Panic(res);
309 
310 	if (res == TEE_SUCCESS)
311 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
312 
313 	return res;
314 }
315 
316 void TEE_FreeTransientObject(TEE_ObjectHandle object)
317 {
318 	struct utee_object_info info = { };
319 	TEE_Result res = TEE_SUCCESS;
320 
321 	if (object == TEE_HANDLE_NULL)
322 		return;
323 
324 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
325 	if (res != TEE_SUCCESS)
326 		TEE_Panic(res);
327 
328 	if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
329 		TEE_Panic(0);
330 
331 	res = _utee_cryp_obj_close((unsigned long)object);
332 	if (res != TEE_SUCCESS)
333 		TEE_Panic(res);
334 }
335 
336 void TEE_ResetTransientObject(TEE_ObjectHandle object)
337 {
338 	struct utee_object_info info = { };
339 	TEE_Result res = TEE_SUCCESS;
340 
341 	if (object == TEE_HANDLE_NULL)
342 		return;
343 
344 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
345 	if (res != TEE_SUCCESS)
346 		TEE_Panic(res);
347 
348 	if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
349 		TEE_Panic(0);
350 
351 	res = _utee_cryp_obj_reset((unsigned long)object);
352 	if (res != TEE_SUCCESS)
353 		TEE_Panic(res);
354 }
355 
356 TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object,
357 				       const TEE_Attribute *attrs,
358 				       uint32_t attrCount)
359 {
360 	struct utee_attribute ua[attrCount];
361 	struct utee_object_info info = { };
362 	TEE_Result res = TEE_SUCCESS;
363 
364 	__utee_check_attr_in_annotation(attrs, attrCount);
365 
366 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
367 	if (res != TEE_SUCCESS)
368 		TEE_Panic(res);
369 
370 	/* Must be a transient object */
371 	if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
372 		TEE_Panic(0);
373 
374 	/* Must not be initialized already */
375 	if ((info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
376 		TEE_Panic(0);
377 
378 	__utee_from_attr(ua, attrs, attrCount);
379 	res = _utee_cryp_obj_populate((unsigned long)object, ua, attrCount);
380 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
381 		TEE_Panic(res);
382 	return res;
383 }
384 
385 TEE_Result __GP11_TEE_PopulateTransientObject(TEE_ObjectHandle object,
386 					      const __GP11_TEE_Attribute *attrs,
387 					      uint32_t attrCount)
388 {
389 	struct utee_attribute ua[attrCount];
390 	struct utee_object_info info = { };
391 	TEE_Result res = TEE_SUCCESS;
392 
393 	__utee_check_gp11_attr_in_annotation(attrs, attrCount);
394 
395 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
396 	if (res != TEE_SUCCESS)
397 		TEE_Panic(res);
398 
399 	/* Must be a transient object */
400 	if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
401 		TEE_Panic(0);
402 
403 	/* Must not be initialized already */
404 	if ((info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
405 		TEE_Panic(0);
406 
407 	__utee_from_gp11_attr(ua, attrs, attrCount);
408 	res = _utee_cryp_obj_populate((unsigned long)object, ua, attrCount);
409 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
410 		TEE_Panic(res);
411 	return res;
412 }
413 
414 void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID,
415 			  const void *buffer, uint32_t length)
416 {
417 	__utee_check_out_annotation(attr, sizeof(*attr));
418 
419 	if ((attributeID & TEE_ATTR_FLAG_VALUE) != 0)
420 		TEE_Panic(0);
421 	attr->attributeID = attributeID;
422 	attr->content.ref.buffer = (void *)buffer;
423 	attr->content.ref.length = length;
424 }
425 
426 void __GP11_TEE_InitRefAttribute(__GP11_TEE_Attribute *attr,
427 				 uint32_t attributeID,
428 				 const void *buffer, uint32_t length)
429 {
430 	__utee_check_out_annotation(attr, sizeof(*attr));
431 
432 	if ((attributeID & TEE_ATTR_FLAG_VALUE) != 0)
433 		TEE_Panic(0);
434 	attr->attributeID = attributeID;
435 	attr->content.ref.buffer = (void *)buffer;
436 	attr->content.ref.length = length;
437 }
438 
439 void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID,
440 			    uint32_t a, uint32_t b)
441 {
442 	__utee_check_out_annotation(attr, sizeof(*attr));
443 
444 	if ((attributeID & TEE_ATTR_FLAG_VALUE) == 0)
445 		TEE_Panic(0);
446 	attr->attributeID = attributeID;
447 	attr->content.value.a = a;
448 	attr->content.value.b = b;
449 }
450 
451 void __GP11_TEE_InitValueAttribute(__GP11_TEE_Attribute *attr,
452 				   uint32_t attributeID,
453 				   uint32_t a, uint32_t b)
454 {
455 	__utee_check_out_annotation(attr, sizeof(*attr));
456 
457 	if ((attributeID & TEE_ATTR_FLAG_VALUE) == 0)
458 		TEE_Panic(0);
459 	attr->attributeID = attributeID;
460 	attr->content.value.a = a;
461 	attr->content.value.b = b;
462 }
463 
464 /*
465  * Use of this function is deprecated
466  * new code SHOULD use the TEE_CopyObjectAttributes1 function instead
467  * These functions will be removed at some future major revision of
468  * this specification
469  */
470 void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject,
471 			      TEE_ObjectHandle srcObject)
472 {
473 	struct utee_object_info src_info = { };
474 	TEE_Result res = TEE_SUCCESS;
475 
476 	res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info);
477 	if (src_info.obj_type == TEE_TYPE_CORRUPTED_OBJECT)
478 		return;
479 
480 	res = TEE_CopyObjectAttributes1(destObject, srcObject);
481 	if (res != TEE_SUCCESS)
482 		TEE_Panic(res);
483 }
484 
485 TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject,
486 			      TEE_ObjectHandle srcObject)
487 {
488 	struct utee_object_info dst_info = { };
489 	struct utee_object_info src_info = { };
490 	TEE_Result res = TEE_SUCCESS;
491 
492 	res = _utee_cryp_obj_get_info((unsigned long)destObject, &dst_info);
493 	if (res != TEE_SUCCESS)
494 		goto exit;
495 
496 	res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info);
497 	if (res != TEE_SUCCESS)
498 		goto exit;
499 
500 	if (!(src_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED))
501 		TEE_Panic(0);
502 
503 	if ((dst_info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT))
504 		TEE_Panic(0);
505 
506 	if ((dst_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED))
507 		TEE_Panic(0);
508 
509 	res = _utee_cryp_obj_copy((unsigned long)destObject,
510 				  (unsigned long)srcObject);
511 
512 exit:
513 	if (res != TEE_SUCCESS &&
514 	    res != TEE_ERROR_CORRUPT_OBJECT &&
515 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
516 		TEE_Panic(res);
517 
518 	return res;
519 }
520 
521 TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
522 			   const TEE_Attribute *params, uint32_t paramCount)
523 {
524 	TEE_Result res;
525 	struct utee_attribute ua[paramCount];
526 
527 	__utee_check_attr_in_annotation(params, paramCount);
528 
529 	__utee_from_attr(ua, params, paramCount);
530 	res = _utee_cryp_obj_generate_key((unsigned long)object, keySize,
531 					  ua, paramCount);
532 
533 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
534 		TEE_Panic(res);
535 
536 	return res;
537 }
538 
539 TEE_Result __GP11_TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
540 				  const __GP11_TEE_Attribute *params,
541 				  uint32_t paramCount)
542 {
543 	TEE_Result res = TEE_SUCCESS;
544 	struct utee_attribute ua[paramCount];
545 
546 	__utee_check_gp11_attr_in_annotation(params, paramCount);
547 
548 	__utee_from_gp11_attr(ua, params, paramCount);
549 	res = _utee_cryp_obj_generate_key((unsigned long)object, keySize,
550 					  ua, paramCount);
551 
552 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
553 		TEE_Panic(res);
554 
555 	return res;
556 }
557 
558 /* Data and Key Storage API  - Persistent Object Functions */
559 
560 TEE_Result TEE_OpenPersistentObject(uint32_t storageID, const void *objectID,
561 				    uint32_t objectIDLen, uint32_t flags,
562 				    TEE_ObjectHandle *object)
563 {
564 	TEE_Result res;
565 	uint32_t obj;
566 
567 	__utee_check_out_annotation(object, sizeof(*object));
568 
569 	res = _utee_storage_obj_open(storageID, objectID, objectIDLen, flags,
570 				     &obj);
571 	if (res == TEE_SUCCESS)
572 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
573 
574 	if (res != TEE_SUCCESS &&
575 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
576 	    res != TEE_ERROR_ACCESS_CONFLICT &&
577 	    res != TEE_ERROR_OUT_OF_MEMORY &&
578 	    res != TEE_ERROR_CORRUPT_OBJECT &&
579 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
580 		TEE_Panic(res);
581 
582 	if (res != TEE_SUCCESS)
583 		*object = TEE_HANDLE_NULL;
584 
585 	return res;
586 }
587 
588 TEE_Result TEE_CreatePersistentObject(uint32_t storageID, const void *objectID,
589 				      uint32_t objectIDLen, uint32_t flags,
590 				      TEE_ObjectHandle attributes,
591 				      const void *initialData,
592 				      uint32_t initialDataLen,
593 				      TEE_ObjectHandle *object)
594 {
595 	TEE_Result res;
596 	uint32_t obj;
597 
598 	__utee_check_out_annotation(object, sizeof(*object));
599 
600 	res = _utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
601 				       (unsigned long)attributes, initialData,
602 				       initialDataLen, &obj);
603 
604 	if (res == TEE_SUCCESS)
605 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
606 
607 	if (res != TEE_SUCCESS &&
608 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
609 	    res != TEE_ERROR_ACCESS_CONFLICT &&
610 	    res != TEE_ERROR_OUT_OF_MEMORY &&
611 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
612 	    res != TEE_ERROR_CORRUPT_OBJECT &&
613 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
614 		TEE_Panic(res);
615 
616 	if (res != TEE_SUCCESS)
617 		*object = TEE_HANDLE_NULL;
618 
619 	return res;
620 }
621 
622 /*
623  * Use of this function is deprecated
624  * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead
625  * These functions will be removed at some future major revision of
626  * this specification
627  */
628 void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
629 {
630 	TEE_Result res;
631 
632 	if (object == TEE_HANDLE_NULL)
633 		return;
634 
635 	res = TEE_CloseAndDeletePersistentObject1(object);
636 
637 	if (res != TEE_SUCCESS)
638 		TEE_Panic(0);
639 }
640 
641 TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object)
642 {
643 	TEE_Result res;
644 
645 	if (object == TEE_HANDLE_NULL)
646 		return TEE_SUCCESS;
647 
648 	res = _utee_storage_obj_del((unsigned long)object);
649 
650 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
651 		TEE_Panic(res);
652 
653 	return res;
654 }
655 
656 
657 TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
658 				      const void *newObjectID,
659 				      uint32_t newObjectIDLen)
660 {
661 	TEE_Result res;
662 
663 	if (object == TEE_HANDLE_NULL) {
664 		res = TEE_ERROR_ITEM_NOT_FOUND;
665 		goto out;
666 	}
667 
668 	res = _utee_storage_obj_rename((unsigned long)object, newObjectID,
669 				       newObjectIDLen);
670 
671 out:
672 	if (res != TEE_SUCCESS &&
673 	    res != TEE_ERROR_ACCESS_CONFLICT &&
674 	    res != TEE_ERROR_CORRUPT_OBJECT &&
675 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
676 		TEE_Panic(res);
677 
678 	return res;
679 }
680 
681 TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
682 						  objectEnumerator)
683 {
684 	TEE_Result res;
685 	uint32_t oe;
686 
687 	__utee_check_out_annotation(objectEnumerator,
688 				    sizeof(*objectEnumerator));
689 
690 	res = _utee_storage_alloc_enum(&oe);
691 
692 	if (res != TEE_SUCCESS)
693 		oe = TEE_HANDLE_NULL;
694 
695 	*objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe;
696 
697 	if (res != TEE_SUCCESS &&
698 	    res != TEE_ERROR_ACCESS_CONFLICT)
699 		TEE_Panic(res);
700 
701 	return res;
702 }
703 
704 void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
705 {
706 	TEE_Result res;
707 
708 	if (objectEnumerator == TEE_HANDLE_NULL)
709 		return;
710 
711 	res = _utee_storage_free_enum((unsigned long)objectEnumerator);
712 
713 	if (res != TEE_SUCCESS)
714 		TEE_Panic(res);
715 }
716 
717 void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
718 {
719 	TEE_Result res;
720 
721 	if (objectEnumerator == TEE_HANDLE_NULL)
722 		return;
723 
724 	res = _utee_storage_reset_enum((unsigned long)objectEnumerator);
725 
726 	if (res != TEE_SUCCESS)
727 		TEE_Panic(res);
728 }
729 
730 TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
731 					       objectEnumerator,
732 					       uint32_t storageID)
733 {
734 	TEE_Result res;
735 
736 	res = _utee_storage_start_enum((unsigned long)objectEnumerator,
737 				       storageID);
738 
739 	if (res != TEE_SUCCESS &&
740 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
741 	    res != TEE_ERROR_CORRUPT_OBJECT &&
742 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
743 		TEE_Panic(res);
744 
745 	return res;
746 }
747 
748 TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
749 				       TEE_ObjectInfo *objectInfo,
750 				       void *objectID, uint32_t *objectIDLen)
751 {
752 	struct utee_object_info info = { };
753 	TEE_Result res = TEE_SUCCESS;
754 	uint64_t len = 0;
755 
756 	if (objectInfo)
757 		__utee_check_out_annotation(objectInfo, sizeof(*objectInfo));
758 	__utee_check_out_annotation(objectIDLen, sizeof(*objectIDLen));
759 
760 	if (!objectID) {
761 		res = TEE_ERROR_BAD_PARAMETERS;
762 		goto out;
763 	}
764 
765 	len = *objectIDLen;
766 	res = _utee_storage_next_enum((unsigned long)objectEnumerator,
767 				      &info, objectID, &len);
768 	if (objectInfo) {
769 		objectInfo->objectType = info.obj_type;
770 		objectInfo->objectSize = info.obj_size;
771 		objectInfo->maxObjectSize = info.max_obj_size;
772 		objectInfo->objectUsage = info.obj_usage;
773 		objectInfo->dataSize = info.data_size;
774 		objectInfo->dataPosition = info.data_pos;
775 		objectInfo->handleFlags = info.handle_flags;
776 	}
777 	*objectIDLen = len;
778 
779 out:
780 	if (res != TEE_SUCCESS &&
781 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
782 	    res != TEE_ERROR_CORRUPT_OBJECT &&
783 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
784 		TEE_Panic(res);
785 
786 	return res;
787 }
788 
789 TEE_Result
790 __GP11_TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
791 				   __GP11_TEE_ObjectInfo *objectInfo,
792 				   void *objectID, uint32_t *objectIDLen)
793 {
794 	struct utee_object_info info = { };
795 	TEE_Result res = TEE_SUCCESS;
796 	uint64_t len = 0;
797 
798 	if (objectInfo)
799 		__utee_check_out_annotation(objectInfo, sizeof(*objectInfo));
800 	__utee_check_out_annotation(objectIDLen, sizeof(*objectIDLen));
801 
802 	if (!objectID) {
803 		res = TEE_ERROR_BAD_PARAMETERS;
804 		goto out;
805 	}
806 
807 	len = *objectIDLen;
808 	res = _utee_storage_next_enum((unsigned long)objectEnumerator,
809 				      &info, objectID, &len);
810 	if (objectInfo) {
811 		objectInfo->objectType = info.obj_type;
812 		objectInfo->keySize = info.obj_size;
813 		objectInfo->maxKeySize = info.max_obj_size;
814 		objectInfo->objectUsage = info.obj_usage;
815 		objectInfo->dataSize = info.data_size;
816 		objectInfo->dataPosition = info.data_pos;
817 		objectInfo->handleFlags = info.handle_flags;
818 	}
819 	*objectIDLen = len;
820 
821 out:
822 	if (res != TEE_SUCCESS &&
823 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
824 	    res != TEE_ERROR_CORRUPT_OBJECT &&
825 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
826 		TEE_Panic(res);
827 
828 	return res;
829 }
830 
831 /* Data and Key Storage API  - Data Stream Access Functions */
832 
833 TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
834 			      uint32_t size, uint32_t *count)
835 {
836 	TEE_Result res;
837 	uint64_t cnt64;
838 
839 	if (object == TEE_HANDLE_NULL) {
840 		res = TEE_ERROR_BAD_PARAMETERS;
841 		goto out;
842 	}
843 	__utee_check_out_annotation(count, sizeof(*count));
844 
845 	cnt64 = *count;
846 	res = _utee_storage_obj_read((unsigned long)object, buffer, size,
847 				     &cnt64);
848 	*count = cnt64;
849 
850 out:
851 	if (res != TEE_SUCCESS &&
852 	    res != TEE_ERROR_CORRUPT_OBJECT &&
853 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
854 		TEE_Panic(res);
855 
856 	return res;
857 }
858 
859 TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, const void *buffer,
860 			       uint32_t size)
861 {
862 	TEE_Result res;
863 
864 	if (object == TEE_HANDLE_NULL) {
865 		res = TEE_ERROR_BAD_PARAMETERS;
866 		goto out;
867 	}
868 
869 	if (size > TEE_DATA_MAX_POSITION) {
870 		res = TEE_ERROR_OVERFLOW;
871 		goto out;
872 	}
873 
874 	res = _utee_storage_obj_write((unsigned long)object, buffer, size);
875 
876 out:
877 	if (res != TEE_SUCCESS &&
878 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
879 	    res != TEE_ERROR_OVERFLOW &&
880 	    res != TEE_ERROR_CORRUPT_OBJECT &&
881 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
882 		TEE_Panic(res);
883 
884 	return res;
885 }
886 
887 TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
888 {
889 	TEE_Result res;
890 
891 	if (object == TEE_HANDLE_NULL) {
892 		res = TEE_ERROR_BAD_PARAMETERS;
893 		goto out;
894 	}
895 
896 	res = _utee_storage_obj_trunc((unsigned long)object, size);
897 
898 out:
899 	if (res != TEE_SUCCESS &&
900 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
901 	    res != TEE_ERROR_CORRUPT_OBJECT &&
902 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
903 		TEE_Panic(res);
904 
905 	return res;
906 }
907 
908 TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
909 			      TEE_Whence whence)
910 {
911 	struct utee_object_info info = { };
912 	TEE_Result res = TEE_SUCCESS;
913 
914 	if (object == TEE_HANDLE_NULL) {
915 		res = TEE_ERROR_BAD_PARAMETERS;
916 		goto out;
917 	}
918 
919 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
920 	if (res != TEE_SUCCESS)
921 		goto out;
922 
923 	switch (whence) {
924 	case TEE_DATA_SEEK_SET:
925 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) {
926 			res = TEE_ERROR_OVERFLOW;
927 			goto out;
928 		}
929 		break;
930 	case TEE_DATA_SEEK_CUR:
931 		if (offset > 0 &&
932 		    ((uint32_t)offset + info.data_pos > TEE_DATA_MAX_POSITION ||
933 		     (uint32_t)offset + info.data_pos < info.data_pos)) {
934 			res = TEE_ERROR_OVERFLOW;
935 			goto out;
936 		}
937 		break;
938 	case TEE_DATA_SEEK_END:
939 		if (offset > 0 &&
940 		    ((uint32_t)offset + info.data_size >
941 		     TEE_DATA_MAX_POSITION ||
942 		     (uint32_t)offset + info.data_size < info.data_size)) {
943 			res = TEE_ERROR_OVERFLOW;
944 			goto out;
945 		}
946 		break;
947 	default:
948 		res = TEE_ERROR_ITEM_NOT_FOUND;
949 		goto out;
950 	}
951 
952 	res = _utee_storage_obj_seek((unsigned long)object, offset, whence);
953 
954 out:
955 	if (res != TEE_SUCCESS &&
956 	    res != TEE_ERROR_OVERFLOW &&
957 	    res != TEE_ERROR_CORRUPT_OBJECT &&
958 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
959 		TEE_Panic(res);
960 
961 	return res;
962 }
963