xref: /optee_os/lib/libutee/tee_api_objects.c (revision f22e1655f23819a7b080fe0963e51ae8a7ce529f)
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 					size_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 __GP11_TEE_GetObjectBufferAttribute(TEE_ObjectHandle object,
234 					       uint32_t attributeID,
235 					       void *buffer, uint32_t *size)
236 {
237 	TEE_Result res = TEE_SUCCESS;
238 	size_t l = 0;
239 
240 	__utee_check_inout_annotation(size, sizeof(*size));
241 	l = *size;
242 	res = TEE_GetObjectBufferAttribute(object, attributeID, buffer, &l);
243 	*size = l;
244 	return res;
245 }
246 
247 TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object,
248 				       uint32_t attributeID, uint32_t *a,
249 				       uint32_t *b)
250 {
251 	struct utee_object_info info = { };
252 	TEE_Result res = TEE_SUCCESS;
253 	uint32_t buf[2];
254 	uint64_t size = sizeof(buf);
255 
256 	if (a)
257 		__utee_check_out_annotation(a, sizeof(*a));
258 	if (b)
259 		__utee_check_out_annotation(b, sizeof(*b));
260 
261 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
262 	if (res != TEE_SUCCESS)
263 		goto exit;
264 
265 	/* This function only supports value attributes */
266 	if (!(attributeID & TEE_ATTR_FLAG_VALUE)) {
267 		res = TEE_ERROR_BAD_PARAMETERS;
268 		goto exit;
269 	}
270 
271 	res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID, buf,
272 				      &size);
273 
274 exit:
275 	if (res != TEE_SUCCESS &&
276 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
277 	    res != TEE_ERROR_CORRUPT_OBJECT &&
278 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
279 		TEE_Panic(res);
280 
281 	if (size != sizeof(buf))
282 		TEE_Panic(0);
283 
284 	if (res == TEE_SUCCESS) {
285 		if (a)
286 			*a = buf[0];
287 		if (b)
288 			*b = buf[1];
289 	}
290 
291 	return res;
292 }
293 
294 void TEE_CloseObject(TEE_ObjectHandle object)
295 {
296 	TEE_Result res;
297 
298 	if (object == TEE_HANDLE_NULL)
299 		return;
300 
301 	res = _utee_cryp_obj_close((unsigned long)object);
302 	if (res != TEE_SUCCESS)
303 		TEE_Panic(res);
304 }
305 
306 /* Data and Key Storage API  - Transient Object Functions */
307 
308 TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType,
309 				       uint32_t maxObjectSize,
310 				       TEE_ObjectHandle *object)
311 {
312 	if (objectType == TEE_TYPE_DATA)
313 		return TEE_ERROR_NOT_SUPPORTED;
314 
315 	return __GP11_TEE_AllocateTransientObject(objectType, maxObjectSize,
316 						  object);
317 }
318 
319 TEE_Result __GP11_TEE_AllocateTransientObject(TEE_ObjectType objectType,
320 					      uint32_t maxKeySize,
321 					      TEE_ObjectHandle *object)
322 {
323 	TEE_Result res;
324 	uint32_t obj;
325 
326 	__utee_check_out_annotation(object, sizeof(*object));
327 
328 	res = _utee_cryp_obj_alloc(objectType, maxKeySize, &obj);
329 
330 	if (res != TEE_SUCCESS &&
331 	    res != TEE_ERROR_OUT_OF_MEMORY &&
332 	    res != TEE_ERROR_NOT_SUPPORTED)
333 		TEE_Panic(res);
334 
335 	if (res == TEE_SUCCESS)
336 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
337 
338 	return res;
339 }
340 
341 void TEE_FreeTransientObject(TEE_ObjectHandle object)
342 {
343 	struct utee_object_info info = { };
344 	TEE_Result res = TEE_SUCCESS;
345 
346 	if (object == TEE_HANDLE_NULL)
347 		return;
348 
349 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
350 	if (res != TEE_SUCCESS)
351 		TEE_Panic(res);
352 
353 	if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
354 		TEE_Panic(0);
355 
356 	res = _utee_cryp_obj_close((unsigned long)object);
357 	if (res != TEE_SUCCESS)
358 		TEE_Panic(res);
359 }
360 
361 void TEE_ResetTransientObject(TEE_ObjectHandle object)
362 {
363 	struct utee_object_info info = { };
364 	TEE_Result res = TEE_SUCCESS;
365 
366 	if (object == TEE_HANDLE_NULL)
367 		return;
368 
369 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
370 	if (res != TEE_SUCCESS)
371 		TEE_Panic(res);
372 
373 	if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
374 		TEE_Panic(0);
375 
376 	res = _utee_cryp_obj_reset((unsigned long)object);
377 	if (res != TEE_SUCCESS)
378 		TEE_Panic(res);
379 }
380 
381 TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object,
382 				       const TEE_Attribute *attrs,
383 				       uint32_t attrCount)
384 {
385 	struct utee_attribute ua[attrCount];
386 	struct utee_object_info info = { };
387 	TEE_Result res = TEE_SUCCESS;
388 
389 	__utee_check_attr_in_annotation(attrs, attrCount);
390 
391 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
392 	if (res != TEE_SUCCESS)
393 		TEE_Panic(res);
394 
395 	/* Must be a transient object */
396 	if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
397 		TEE_Panic(0);
398 
399 	/* Must not be initialized already */
400 	if ((info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
401 		TEE_Panic(0);
402 
403 	__utee_from_attr(ua, attrs, attrCount);
404 	res = _utee_cryp_obj_populate((unsigned long)object, ua, attrCount);
405 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
406 		TEE_Panic(res);
407 	return res;
408 }
409 
410 TEE_Result __GP11_TEE_PopulateTransientObject(TEE_ObjectHandle object,
411 					      const __GP11_TEE_Attribute *attrs,
412 					      uint32_t attrCount)
413 {
414 	struct utee_attribute ua[attrCount];
415 	struct utee_object_info info = { };
416 	TEE_Result res = TEE_SUCCESS;
417 
418 	__utee_check_gp11_attr_in_annotation(attrs, attrCount);
419 
420 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
421 	if (res != TEE_SUCCESS)
422 		TEE_Panic(res);
423 
424 	/* Must be a transient object */
425 	if ((info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
426 		TEE_Panic(0);
427 
428 	/* Must not be initialized already */
429 	if ((info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
430 		TEE_Panic(0);
431 
432 	__utee_from_gp11_attr(ua, attrs, attrCount);
433 	res = _utee_cryp_obj_populate((unsigned long)object, ua, attrCount);
434 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
435 		TEE_Panic(res);
436 	return res;
437 }
438 
439 void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID,
440 			  const void *buffer, size_t length)
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.ref.buffer = (void *)buffer;
448 	attr->content.ref.length = length;
449 }
450 
451 void __GP11_TEE_InitRefAttribute(__GP11_TEE_Attribute *attr,
452 				 uint32_t attributeID,
453 				 const void *buffer, uint32_t length)
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.ref.buffer = (void *)buffer;
461 	attr->content.ref.length = length;
462 }
463 
464 void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID,
465 			    uint32_t a, uint32_t b)
466 {
467 	__utee_check_out_annotation(attr, sizeof(*attr));
468 
469 	if ((attributeID & TEE_ATTR_FLAG_VALUE) == 0)
470 		TEE_Panic(0);
471 	attr->attributeID = attributeID;
472 	attr->content.value.a = a;
473 	attr->content.value.b = b;
474 }
475 
476 void __GP11_TEE_InitValueAttribute(__GP11_TEE_Attribute *attr,
477 				   uint32_t attributeID,
478 				   uint32_t a, uint32_t b)
479 {
480 	__utee_check_out_annotation(attr, sizeof(*attr));
481 
482 	if ((attributeID & TEE_ATTR_FLAG_VALUE) == 0)
483 		TEE_Panic(0);
484 	attr->attributeID = attributeID;
485 	attr->content.value.a = a;
486 	attr->content.value.b = b;
487 }
488 
489 /*
490  * Use of this function is deprecated
491  * new code SHOULD use the TEE_CopyObjectAttributes1 function instead
492  * These functions will be removed at some future major revision of
493  * this specification
494  */
495 void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject,
496 			      TEE_ObjectHandle srcObject)
497 {
498 	struct utee_object_info src_info = { };
499 	TEE_Result res = TEE_SUCCESS;
500 
501 	res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info);
502 	if (src_info.obj_type == TEE_TYPE_CORRUPTED_OBJECT)
503 		return;
504 
505 	res = TEE_CopyObjectAttributes1(destObject, srcObject);
506 	if (res != TEE_SUCCESS)
507 		TEE_Panic(res);
508 }
509 
510 TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject,
511 			      TEE_ObjectHandle srcObject)
512 {
513 	struct utee_object_info dst_info = { };
514 	struct utee_object_info src_info = { };
515 	TEE_Result res = TEE_SUCCESS;
516 
517 	res = _utee_cryp_obj_get_info((unsigned long)destObject, &dst_info);
518 	if (res != TEE_SUCCESS)
519 		goto exit;
520 
521 	res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info);
522 	if (res != TEE_SUCCESS)
523 		goto exit;
524 
525 	if (!(src_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED))
526 		TEE_Panic(0);
527 
528 	if ((dst_info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT))
529 		TEE_Panic(0);
530 
531 	if ((dst_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED))
532 		TEE_Panic(0);
533 
534 	res = _utee_cryp_obj_copy((unsigned long)destObject,
535 				  (unsigned long)srcObject);
536 
537 exit:
538 	if (res != TEE_SUCCESS &&
539 	    res != TEE_ERROR_CORRUPT_OBJECT &&
540 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
541 		TEE_Panic(res);
542 
543 	return res;
544 }
545 
546 TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
547 			   const TEE_Attribute *params, uint32_t paramCount)
548 {
549 	TEE_Result res;
550 	struct utee_attribute ua[paramCount];
551 
552 	__utee_check_attr_in_annotation(params, paramCount);
553 
554 	__utee_from_attr(ua, params, paramCount);
555 	res = _utee_cryp_obj_generate_key((unsigned long)object, keySize,
556 					  ua, paramCount);
557 
558 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
559 		TEE_Panic(res);
560 
561 	return res;
562 }
563 
564 TEE_Result __GP11_TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize,
565 				  const __GP11_TEE_Attribute *params,
566 				  uint32_t paramCount)
567 {
568 	TEE_Result res = TEE_SUCCESS;
569 	struct utee_attribute ua[paramCount];
570 
571 	__utee_check_gp11_attr_in_annotation(params, paramCount);
572 
573 	__utee_from_gp11_attr(ua, params, paramCount);
574 	res = _utee_cryp_obj_generate_key((unsigned long)object, keySize,
575 					  ua, paramCount);
576 
577 	if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS)
578 		TEE_Panic(res);
579 
580 	return res;
581 }
582 
583 /* Data and Key Storage API  - Persistent Object Functions */
584 
585 TEE_Result TEE_OpenPersistentObject(uint32_t storageID, const void *objectID,
586 				    size_t objectIDLen, uint32_t flags,
587 				    TEE_ObjectHandle *object)
588 {
589 	TEE_Result res;
590 	uint32_t obj;
591 
592 	__utee_check_out_annotation(object, sizeof(*object));
593 
594 	res = _utee_storage_obj_open(storageID, objectID, objectIDLen, flags,
595 				     &obj);
596 	if (res == TEE_SUCCESS)
597 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
598 
599 	if (res != TEE_SUCCESS &&
600 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
601 	    res != TEE_ERROR_ACCESS_CONFLICT &&
602 	    res != TEE_ERROR_OUT_OF_MEMORY &&
603 	    res != TEE_ERROR_CORRUPT_OBJECT &&
604 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
605 		TEE_Panic(res);
606 
607 	if (res != TEE_SUCCESS)
608 		*object = TEE_HANDLE_NULL;
609 
610 	return res;
611 }
612 
613 TEE_Result __GP11_TEE_OpenPersistentObject(uint32_t storageID,
614 					   const void *objectID,
615 					   uint32_t objectIDLen, uint32_t flags,
616 					   TEE_ObjectHandle *object)
617 {
618 	return TEE_OpenPersistentObject(storageID, objectID, objectIDLen,
619 					flags, object);
620 }
621 
622 TEE_Result TEE_CreatePersistentObject(uint32_t storageID, const void *objectID,
623 				      size_t objectIDLen, uint32_t flags,
624 				      TEE_ObjectHandle attributes,
625 				      const void *initialData,
626 				      size_t initialDataLen,
627 				      TEE_ObjectHandle *object)
628 {
629 	TEE_Result res = TEE_SUCCESS;
630 	uint32_t *obj_ptr = NULL;
631 	uint32_t obj = 0;
632 
633 	if (object) {
634 		__utee_check_out_annotation(object, sizeof(*object));
635 		obj_ptr = &obj;
636 	}
637 
638 	res = _utee_storage_obj_create(storageID, objectID, objectIDLen, flags,
639 				       (unsigned long)attributes, initialData,
640 				       initialDataLen, obj_ptr);
641 
642 	if (res == TEE_SUCCESS && object)
643 		*object = (TEE_ObjectHandle)(uintptr_t)obj;
644 
645 	if (res != TEE_SUCCESS &&
646 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
647 	    res != TEE_ERROR_ACCESS_CONFLICT &&
648 	    res != TEE_ERROR_OUT_OF_MEMORY &&
649 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
650 	    res != TEE_ERROR_CORRUPT_OBJECT &&
651 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
652 		TEE_Panic(res);
653 
654 	if (res != TEE_SUCCESS && object)
655 		*object = TEE_HANDLE_NULL;
656 
657 	return res;
658 }
659 
660 TEE_Result __GP11_TEE_CreatePersistentObject(uint32_t storageID,
661 					     const void *objectID,
662 					     uint32_t objectIDLen,
663 					     uint32_t flags,
664 					     TEE_ObjectHandle attributes,
665 					     const void *initialData,
666 					     uint32_t initialDataLen,
667 					     TEE_ObjectHandle *object)
668 {
669 	__utee_check_out_annotation(object, sizeof(*object));
670 
671 	return TEE_CreatePersistentObject(storageID, objectID, objectIDLen,
672 					  flags, attributes, initialData,
673 					  initialDataLen, object);
674 }
675 
676 /*
677  * Use of this function is deprecated
678  * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead
679  * These functions will be removed at some future major revision of
680  * this specification
681  */
682 void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object)
683 {
684 	TEE_Result res;
685 
686 	if (object == TEE_HANDLE_NULL)
687 		return;
688 
689 	res = TEE_CloseAndDeletePersistentObject1(object);
690 
691 	if (res != TEE_SUCCESS)
692 		TEE_Panic(0);
693 }
694 
695 TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object)
696 {
697 	TEE_Result res;
698 
699 	if (object == TEE_HANDLE_NULL)
700 		return TEE_SUCCESS;
701 
702 	res = _utee_storage_obj_del((unsigned long)object);
703 
704 	if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
705 		TEE_Panic(res);
706 
707 	return res;
708 }
709 
710 
711 TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object,
712 				      const void *newObjectID,
713 				      uint32_t newObjectIDLen)
714 {
715 	TEE_Result res;
716 
717 	if (object == TEE_HANDLE_NULL) {
718 		res = TEE_ERROR_ITEM_NOT_FOUND;
719 		goto out;
720 	}
721 
722 	res = _utee_storage_obj_rename((unsigned long)object, newObjectID,
723 				       newObjectIDLen);
724 
725 out:
726 	if (res != TEE_SUCCESS &&
727 	    res != TEE_ERROR_ACCESS_CONFLICT &&
728 	    res != TEE_ERROR_CORRUPT_OBJECT &&
729 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
730 		TEE_Panic(res);
731 
732 	return res;
733 }
734 
735 TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle *
736 						  objectEnumerator)
737 {
738 	TEE_Result res;
739 	uint32_t oe;
740 
741 	__utee_check_out_annotation(objectEnumerator,
742 				    sizeof(*objectEnumerator));
743 
744 	res = _utee_storage_alloc_enum(&oe);
745 
746 	if (res != TEE_SUCCESS)
747 		oe = TEE_HANDLE_NULL;
748 
749 	*objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe;
750 
751 	if (res != TEE_SUCCESS &&
752 	    res != TEE_ERROR_ACCESS_CONFLICT)
753 		TEE_Panic(res);
754 
755 	return res;
756 }
757 
758 void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
759 {
760 	TEE_Result res;
761 
762 	if (objectEnumerator == TEE_HANDLE_NULL)
763 		return;
764 
765 	res = _utee_storage_free_enum((unsigned long)objectEnumerator);
766 
767 	if (res != TEE_SUCCESS)
768 		TEE_Panic(res);
769 }
770 
771 void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator)
772 {
773 	TEE_Result res;
774 
775 	if (objectEnumerator == TEE_HANDLE_NULL)
776 		return;
777 
778 	res = _utee_storage_reset_enum((unsigned long)objectEnumerator);
779 
780 	if (res != TEE_SUCCESS)
781 		TEE_Panic(res);
782 }
783 
784 TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle
785 					       objectEnumerator,
786 					       uint32_t storageID)
787 {
788 	TEE_Result res;
789 
790 	res = _utee_storage_start_enum((unsigned long)objectEnumerator,
791 				       storageID);
792 
793 	if (res != TEE_SUCCESS &&
794 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
795 	    res != TEE_ERROR_CORRUPT_OBJECT &&
796 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
797 		TEE_Panic(res);
798 
799 	return res;
800 }
801 
802 TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
803 				       TEE_ObjectInfo *objectInfo,
804 				       void *objectID, uint32_t *objectIDLen)
805 {
806 	struct utee_object_info info = { };
807 	TEE_Result res = TEE_SUCCESS;
808 	uint64_t len = 0;
809 
810 	if (objectInfo)
811 		__utee_check_out_annotation(objectInfo, sizeof(*objectInfo));
812 	__utee_check_out_annotation(objectIDLen, sizeof(*objectIDLen));
813 
814 	if (!objectID) {
815 		res = TEE_ERROR_BAD_PARAMETERS;
816 		goto out;
817 	}
818 
819 	len = *objectIDLen;
820 	res = _utee_storage_next_enum((unsigned long)objectEnumerator,
821 				      &info, objectID, &len);
822 	if (objectInfo) {
823 		objectInfo->objectType = info.obj_type;
824 		objectInfo->objectSize = info.obj_size;
825 		objectInfo->maxObjectSize = info.max_obj_size;
826 		objectInfo->objectUsage = info.obj_usage;
827 		objectInfo->dataSize = info.data_size;
828 		objectInfo->dataPosition = info.data_pos;
829 		objectInfo->handleFlags = info.handle_flags;
830 	}
831 	*objectIDLen = len;
832 
833 out:
834 	if (res != TEE_SUCCESS &&
835 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
836 	    res != TEE_ERROR_CORRUPT_OBJECT &&
837 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
838 		TEE_Panic(res);
839 
840 	return res;
841 }
842 
843 TEE_Result
844 __GP11_TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator,
845 				   __GP11_TEE_ObjectInfo *objectInfo,
846 				   void *objectID, uint32_t *objectIDLen)
847 {
848 	struct utee_object_info info = { };
849 	TEE_Result res = TEE_SUCCESS;
850 	uint64_t len = 0;
851 
852 	if (objectInfo)
853 		__utee_check_out_annotation(objectInfo, sizeof(*objectInfo));
854 	__utee_check_out_annotation(objectIDLen, sizeof(*objectIDLen));
855 
856 	if (!objectID) {
857 		res = TEE_ERROR_BAD_PARAMETERS;
858 		goto out;
859 	}
860 
861 	len = *objectIDLen;
862 	res = _utee_storage_next_enum((unsigned long)objectEnumerator,
863 				      &info, objectID, &len);
864 	if (objectInfo) {
865 		objectInfo->objectType = info.obj_type;
866 		objectInfo->keySize = info.obj_size;
867 		objectInfo->maxKeySize = info.max_obj_size;
868 		objectInfo->objectUsage = info.obj_usage;
869 		objectInfo->dataSize = info.data_size;
870 		objectInfo->dataPosition = info.data_pos;
871 		objectInfo->handleFlags = info.handle_flags;
872 	}
873 	*objectIDLen = len;
874 
875 out:
876 	if (res != TEE_SUCCESS &&
877 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
878 	    res != TEE_ERROR_CORRUPT_OBJECT &&
879 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
880 		TEE_Panic(res);
881 
882 	return res;
883 }
884 
885 /* Data and Key Storage API  - Data Stream Access Functions */
886 
887 TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer,
888 			      uint32_t size, uint32_t *count)
889 {
890 	TEE_Result res;
891 	uint64_t cnt64;
892 
893 	if (object == TEE_HANDLE_NULL) {
894 		res = TEE_ERROR_BAD_PARAMETERS;
895 		goto out;
896 	}
897 	__utee_check_out_annotation(count, sizeof(*count));
898 
899 	cnt64 = *count;
900 	res = _utee_storage_obj_read((unsigned long)object, buffer, size,
901 				     &cnt64);
902 	*count = cnt64;
903 
904 out:
905 	if (res != TEE_SUCCESS &&
906 	    res != TEE_ERROR_CORRUPT_OBJECT &&
907 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
908 		TEE_Panic(res);
909 
910 	return res;
911 }
912 
913 TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, const void *buffer,
914 			       uint32_t size)
915 {
916 	TEE_Result res;
917 
918 	if (object == TEE_HANDLE_NULL) {
919 		res = TEE_ERROR_BAD_PARAMETERS;
920 		goto out;
921 	}
922 
923 	if (size > TEE_DATA_MAX_POSITION) {
924 		res = TEE_ERROR_OVERFLOW;
925 		goto out;
926 	}
927 
928 	res = _utee_storage_obj_write((unsigned long)object, buffer, size);
929 
930 out:
931 	if (res != TEE_SUCCESS &&
932 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
933 	    res != TEE_ERROR_OVERFLOW &&
934 	    res != TEE_ERROR_CORRUPT_OBJECT &&
935 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
936 		TEE_Panic(res);
937 
938 	return res;
939 }
940 
941 TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size)
942 {
943 	TEE_Result res;
944 
945 	if (object == TEE_HANDLE_NULL) {
946 		res = TEE_ERROR_BAD_PARAMETERS;
947 		goto out;
948 	}
949 
950 	res = _utee_storage_obj_trunc((unsigned long)object, size);
951 
952 out:
953 	if (res != TEE_SUCCESS &&
954 	    res != TEE_ERROR_STORAGE_NO_SPACE &&
955 	    res != TEE_ERROR_CORRUPT_OBJECT &&
956 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
957 		TEE_Panic(res);
958 
959 	return res;
960 }
961 
962 TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset,
963 			      TEE_Whence whence)
964 {
965 	struct utee_object_info info = { };
966 	TEE_Result res = TEE_SUCCESS;
967 
968 	if (object == TEE_HANDLE_NULL) {
969 		res = TEE_ERROR_BAD_PARAMETERS;
970 		goto out;
971 	}
972 
973 	res = _utee_cryp_obj_get_info((unsigned long)object, &info);
974 	if (res != TEE_SUCCESS)
975 		goto out;
976 
977 	switch (whence) {
978 	case TEE_DATA_SEEK_SET:
979 		if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) {
980 			res = TEE_ERROR_OVERFLOW;
981 			goto out;
982 		}
983 		break;
984 	case TEE_DATA_SEEK_CUR:
985 		if (offset > 0 &&
986 		    ((uint32_t)offset + info.data_pos > TEE_DATA_MAX_POSITION ||
987 		     (uint32_t)offset + info.data_pos < info.data_pos)) {
988 			res = TEE_ERROR_OVERFLOW;
989 			goto out;
990 		}
991 		break;
992 	case TEE_DATA_SEEK_END:
993 		if (offset > 0 &&
994 		    ((uint32_t)offset + info.data_size >
995 		     TEE_DATA_MAX_POSITION ||
996 		     (uint32_t)offset + info.data_size < info.data_size)) {
997 			res = TEE_ERROR_OVERFLOW;
998 			goto out;
999 		}
1000 		break;
1001 	default:
1002 		res = TEE_ERROR_ITEM_NOT_FOUND;
1003 		goto out;
1004 	}
1005 
1006 	res = _utee_storage_obj_seek((unsigned long)object, offset, whence);
1007 
1008 out:
1009 	if (res != TEE_SUCCESS &&
1010 	    res != TEE_ERROR_OVERFLOW &&
1011 	    res != TEE_ERROR_CORRUPT_OBJECT &&
1012 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
1013 		TEE_Panic(res);
1014 
1015 	return res;
1016 }
1017