1 /* 2 * Copyright (c) 2014, STMicroelectronics International N.V. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #include <stdlib.h> 28 #include <string.h> 29 30 #include <tee_api.h> 31 #include <utee_syscalls.h> 32 #include "tee_api_private.h" 33 34 #define TEE_USAGE_DEFAULT 0xffffffff 35 36 #define TEE_ATTR_BIT_VALUE (1 << 29) 37 #define TEE_ATTR_BIT_PROTECTED (1 << 28) 38 39 void __utee_from_attr(struct utee_attribute *ua, const TEE_Attribute *attrs, 40 uint32_t attr_count) 41 { 42 size_t n; 43 44 for (n = 0; n < attr_count; n++) { 45 ua[n].attribute_id = attrs[n].attributeID; 46 if (attrs[n].attributeID & TEE_ATTR_BIT_VALUE) { 47 ua[n].a = attrs[n].content.value.a; 48 ua[n].b = attrs[n].content.value.b; 49 } else { 50 ua[n].a = (uintptr_t)attrs[n].content.ref.buffer; 51 ua[n].b = attrs[n].content.ref.length; 52 } 53 } 54 } 55 56 /* Data and Key Storage API - Generic Object Functions */ 57 /* 58 * Use of this function is deprecated 59 * new code SHOULD use the TEE_GetObjectInfo1 function instead 60 * These functions will be removed at some future major revision of 61 * this specification 62 */ 63 void TEE_GetObjectInfo(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo) 64 { 65 TEE_Result res; 66 67 res = utee_cryp_obj_get_info((unsigned long)object, objectInfo); 68 69 if (res != TEE_SUCCESS) 70 TEE_Panic(res); 71 72 if (objectInfo->objectType == TEE_TYPE_CORRUPTED_OBJECT) { 73 objectInfo->keySize = 0; 74 objectInfo->maxKeySize = 0; 75 objectInfo->objectUsage = 0; 76 objectInfo->dataSize = 0; 77 objectInfo->dataPosition = 0; 78 objectInfo->handleFlags = 0; 79 } 80 } 81 82 TEE_Result TEE_GetObjectInfo1(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo) 83 { 84 TEE_Result res; 85 86 res = utee_cryp_obj_get_info((unsigned long)object, objectInfo); 87 88 if (res != TEE_SUCCESS && 89 res != TEE_ERROR_CORRUPT_OBJECT && 90 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 91 TEE_Panic(res); 92 93 return res; 94 } 95 96 /* 97 * Use of this function is deprecated 98 * new code SHOULD use the TEE_RestrictObjectUsage1 function instead 99 * These functions will be removed at some future major revision of 100 * this specification 101 */ 102 void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage) 103 { 104 TEE_Result res; 105 TEE_ObjectInfo objectInfo; 106 107 res = utee_cryp_obj_get_info((unsigned long)object, &objectInfo); 108 if (objectInfo.objectType == TEE_TYPE_CORRUPTED_OBJECT) 109 return; 110 111 res = TEE_RestrictObjectUsage1(object, objectUsage); 112 113 if (res != TEE_SUCCESS) 114 TEE_Panic(res); 115 } 116 117 TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage) 118 { 119 TEE_Result res; 120 121 res = utee_cryp_obj_restrict_usage((unsigned long)object, objectUsage); 122 123 if (res != TEE_SUCCESS && 124 res != TEE_ERROR_CORRUPT_OBJECT && 125 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 126 TEE_Panic(res); 127 128 return res; 129 } 130 131 TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object, 132 uint32_t attributeID, void *buffer, 133 uint32_t *size) 134 { 135 TEE_Result res; 136 TEE_ObjectInfo info; 137 uint64_t sz; 138 139 res = utee_cryp_obj_get_info((unsigned long)object, &info); 140 if (res != TEE_SUCCESS) 141 goto exit; 142 143 /* This function only supports reference attributes */ 144 if ((attributeID & TEE_ATTR_BIT_VALUE)) { 145 res = TEE_ERROR_BAD_PARAMETERS; 146 goto exit; 147 } 148 149 sz = *size; 150 res = utee_cryp_obj_get_attr((unsigned long)object, attributeID, 151 buffer, &sz); 152 *size = sz; 153 154 exit: 155 if (res != TEE_SUCCESS && 156 res != TEE_ERROR_ITEM_NOT_FOUND && 157 res != TEE_ERROR_SHORT_BUFFER && 158 res != TEE_ERROR_CORRUPT_OBJECT && 159 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 160 TEE_Panic(res); 161 162 return res; 163 } 164 165 TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object, 166 uint32_t attributeID, uint32_t *a, 167 uint32_t *b) 168 { 169 TEE_Result res; 170 TEE_ObjectInfo info; 171 uint32_t buf[2]; 172 uint64_t size = sizeof(buf); 173 174 res = utee_cryp_obj_get_info((unsigned long)object, &info); 175 if (res != TEE_SUCCESS) 176 goto exit; 177 178 /* This function only supports value attributes */ 179 if (!(attributeID & TEE_ATTR_BIT_VALUE)) { 180 res = TEE_ERROR_BAD_PARAMETERS; 181 goto exit; 182 } 183 184 res = utee_cryp_obj_get_attr((unsigned long)object, attributeID, buf, 185 &size); 186 187 exit: 188 if (res != TEE_SUCCESS && 189 res != TEE_ERROR_ITEM_NOT_FOUND && 190 res != TEE_ERROR_CORRUPT_OBJECT && 191 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 192 TEE_Panic(res); 193 194 if (size != sizeof(buf)) 195 TEE_Panic(0); 196 197 if (res == TEE_SUCCESS) { 198 if (a) 199 *a = buf[0]; 200 if (b) 201 *b = buf[1]; 202 } 203 204 return res; 205 } 206 207 void TEE_CloseObject(TEE_ObjectHandle object) 208 { 209 TEE_Result res; 210 211 if (object == TEE_HANDLE_NULL) 212 return; 213 214 res = utee_cryp_obj_close((unsigned long)object); 215 if (res != TEE_SUCCESS) 216 TEE_Panic(res); 217 } 218 219 /* Data and Key Storage API - Transient Object Functions */ 220 221 TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType, 222 uint32_t maxKeySize, 223 TEE_ObjectHandle *object) 224 { 225 TEE_Result res; 226 uint32_t obj; 227 228 res = utee_cryp_obj_alloc(objectType, maxKeySize, &obj); 229 230 if (res != TEE_SUCCESS && 231 res != TEE_ERROR_OUT_OF_MEMORY && 232 res != TEE_ERROR_NOT_SUPPORTED) 233 TEE_Panic(res); 234 235 if (res == TEE_SUCCESS) 236 *object = (TEE_ObjectHandle)(uintptr_t)obj; 237 238 return res; 239 } 240 241 void TEE_FreeTransientObject(TEE_ObjectHandle object) 242 { 243 TEE_Result res; 244 TEE_ObjectInfo info; 245 246 if (object == TEE_HANDLE_NULL) 247 return; 248 249 res = utee_cryp_obj_get_info((unsigned long)object, &info); 250 if (res != TEE_SUCCESS) 251 TEE_Panic(res); 252 253 if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 254 TEE_Panic(0); 255 256 res = utee_cryp_obj_close((unsigned long)object); 257 if (res != TEE_SUCCESS) 258 TEE_Panic(res); 259 } 260 261 void TEE_ResetTransientObject(TEE_ObjectHandle object) 262 { 263 TEE_Result res; 264 TEE_ObjectInfo info; 265 266 if (object == TEE_HANDLE_NULL) 267 return; 268 269 res = utee_cryp_obj_get_info((unsigned long)object, &info); 270 if (res != TEE_SUCCESS) 271 TEE_Panic(res); 272 273 if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 274 TEE_Panic(0); 275 276 res = utee_cryp_obj_reset((unsigned long)object); 277 if (res != TEE_SUCCESS) 278 TEE_Panic(res); 279 } 280 281 TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object, 282 const TEE_Attribute *attrs, 283 uint32_t attrCount) 284 { 285 TEE_Result res; 286 TEE_ObjectInfo info; 287 struct utee_attribute ua[attrCount]; 288 289 res = utee_cryp_obj_get_info((unsigned long)object, &info); 290 if (res != TEE_SUCCESS) 291 TEE_Panic(res); 292 293 /* Must be a transient object */ 294 if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 295 TEE_Panic(0); 296 297 /* Must not be initialized already */ 298 if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 299 TEE_Panic(0); 300 301 __utee_from_attr(ua, attrs, attrCount); 302 res = utee_cryp_obj_populate((unsigned long)object, ua, attrCount); 303 if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 304 TEE_Panic(res); 305 return res; 306 } 307 308 void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID, 309 const void *buffer, uint32_t length) 310 { 311 if (attr == NULL) 312 TEE_Panic(0); 313 if ((attributeID & TEE_ATTR_BIT_VALUE) != 0) 314 TEE_Panic(0); 315 attr->attributeID = attributeID; 316 attr->content.ref.buffer = (void *)buffer; 317 attr->content.ref.length = length; 318 } 319 320 void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID, 321 uint32_t a, uint32_t b) 322 { 323 if (attr == NULL) 324 TEE_Panic(0); 325 if ((attributeID & TEE_ATTR_BIT_VALUE) == 0) 326 TEE_Panic(0); 327 attr->attributeID = attributeID; 328 attr->content.value.a = a; 329 attr->content.value.b = b; 330 } 331 332 /* 333 * Use of this function is deprecated 334 * new code SHOULD use the TEE_CopyObjectAttributes1 function instead 335 * These functions will be removed at some future major revision of 336 * this specification 337 */ 338 void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject, 339 TEE_ObjectHandle srcObject) 340 { 341 TEE_Result res; 342 TEE_ObjectInfo src_info; 343 344 res = utee_cryp_obj_get_info((unsigned long)srcObject, &src_info); 345 if (src_info.objectType == TEE_TYPE_CORRUPTED_OBJECT) 346 return; 347 348 res = TEE_CopyObjectAttributes1(destObject, srcObject); 349 if (res != TEE_SUCCESS) 350 TEE_Panic(res); 351 } 352 353 TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject, 354 TEE_ObjectHandle srcObject) 355 { 356 TEE_Result res; 357 TEE_ObjectInfo dst_info; 358 TEE_ObjectInfo src_info; 359 360 res = utee_cryp_obj_get_info((unsigned long)destObject, &dst_info); 361 if (res != TEE_SUCCESS) 362 goto exit; 363 364 res = utee_cryp_obj_get_info((unsigned long)srcObject, &src_info); 365 if (res != TEE_SUCCESS) 366 goto exit; 367 368 if (!(src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 369 TEE_Panic(0); 370 371 if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) 372 TEE_Panic(0); 373 374 if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 375 TEE_Panic(0); 376 377 res = utee_cryp_obj_copy((unsigned long)destObject, 378 (unsigned long)srcObject); 379 380 exit: 381 if (res != TEE_SUCCESS && 382 res != TEE_ERROR_CORRUPT_OBJECT && 383 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 384 TEE_Panic(res); 385 386 return res; 387 } 388 389 TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize, 390 const TEE_Attribute *params, uint32_t paramCount) 391 { 392 TEE_Result res; 393 struct utee_attribute ua[paramCount]; 394 395 __utee_from_attr(ua, params, paramCount); 396 res = utee_cryp_obj_generate_key((unsigned long)object, keySize, 397 ua, paramCount); 398 399 if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 400 TEE_Panic(res); 401 402 return res; 403 } 404 405 /* Data and Key Storage API - Persistent Object Functions */ 406 407 TEE_Result TEE_OpenPersistentObject(uint32_t storageID, const void *objectID, 408 uint32_t objectIDLen, uint32_t flags, 409 TEE_ObjectHandle *object) 410 { 411 TEE_Result res; 412 uint32_t obj; 413 414 if (!objectID) { 415 res = TEE_ERROR_ITEM_NOT_FOUND; 416 goto out; 417 } 418 419 if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) { 420 res = TEE_ERROR_BAD_PARAMETERS; 421 goto out; 422 } 423 424 if (!object) { 425 res = TEE_ERROR_BAD_PARAMETERS; 426 goto out; 427 } 428 429 res = utee_storage_obj_open(storageID, objectID, objectIDLen, flags, 430 &obj); 431 if (res == TEE_SUCCESS) 432 *object = (TEE_ObjectHandle)(uintptr_t)obj; 433 434 out: 435 if (res != TEE_SUCCESS && 436 res != TEE_ERROR_ITEM_NOT_FOUND && 437 res != TEE_ERROR_ACCESS_CONFLICT && 438 res != TEE_ERROR_OUT_OF_MEMORY && 439 res != TEE_ERROR_CORRUPT_OBJECT && 440 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 441 TEE_Panic(res); 442 443 return res; 444 } 445 446 TEE_Result TEE_CreatePersistentObject(uint32_t storageID, const void *objectID, 447 uint32_t objectIDLen, uint32_t flags, 448 TEE_ObjectHandle attributes, 449 const void *initialData, 450 uint32_t initialDataLen, 451 TEE_ObjectHandle *object) 452 { 453 TEE_Result res; 454 uint32_t obj; 455 456 if (!objectID) { 457 res = TEE_ERROR_ITEM_NOT_FOUND; 458 goto err; 459 } 460 461 if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) { 462 res = TEE_ERROR_BAD_PARAMETERS; 463 goto err; 464 } 465 466 res = utee_storage_obj_create(storageID, objectID, objectIDLen, flags, 467 (unsigned long)attributes, initialData, 468 initialDataLen, &obj); 469 if (res == TEE_SUCCESS) { 470 if (object) 471 *object = (TEE_ObjectHandle)(uintptr_t)obj; 472 else 473 res = utee_cryp_obj_close(obj); 474 if (res == TEE_SUCCESS) 475 goto out; 476 } 477 err: 478 if (object) 479 *object = TEE_HANDLE_NULL; 480 if (res == TEE_ERROR_ITEM_NOT_FOUND || 481 res == TEE_ERROR_ACCESS_CONFLICT || 482 res == TEE_ERROR_OUT_OF_MEMORY || 483 res == TEE_ERROR_STORAGE_NO_SPACE || 484 res == TEE_ERROR_CORRUPT_OBJECT || 485 res == TEE_ERROR_STORAGE_NOT_AVAILABLE) 486 return res; 487 TEE_Panic(res); 488 out: 489 return TEE_SUCCESS; 490 } 491 492 /* 493 * Use of this function is deprecated 494 * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead 495 * These functions will be removed at some future major revision of 496 * this specification 497 */ 498 void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object) 499 { 500 TEE_Result res; 501 502 if (object == TEE_HANDLE_NULL) 503 return; 504 505 res = TEE_CloseAndDeletePersistentObject1(object); 506 507 if (res != TEE_SUCCESS) 508 TEE_Panic(0); 509 } 510 511 TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object) 512 { 513 TEE_Result res; 514 515 if (object == TEE_HANDLE_NULL) 516 return TEE_ERROR_STORAGE_NOT_AVAILABLE; 517 518 res = utee_storage_obj_del((unsigned long)object); 519 520 if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 521 TEE_Panic(res); 522 523 return res; 524 } 525 526 527 TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object, 528 const void *newObjectID, 529 uint32_t newObjectIDLen) 530 { 531 TEE_Result res; 532 533 if (object == TEE_HANDLE_NULL) { 534 res = TEE_ERROR_ITEM_NOT_FOUND; 535 goto out; 536 } 537 538 if (!newObjectID) { 539 res = TEE_ERROR_BAD_PARAMETERS; 540 goto out; 541 } 542 543 if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN) { 544 res = TEE_ERROR_BAD_PARAMETERS; 545 goto out; 546 } 547 548 res = utee_storage_obj_rename((unsigned long)object, newObjectID, 549 newObjectIDLen); 550 551 out: 552 if (res != TEE_SUCCESS && 553 res != TEE_ERROR_ACCESS_CONFLICT && 554 res != TEE_ERROR_CORRUPT_OBJECT && 555 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 556 TEE_Panic(res); 557 558 return res; 559 } 560 561 TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle * 562 objectEnumerator) 563 { 564 TEE_Result res; 565 uint32_t oe; 566 567 if (!objectEnumerator) 568 return TEE_ERROR_BAD_PARAMETERS; 569 570 res = utee_storage_alloc_enum(&oe); 571 572 if (res != TEE_SUCCESS) 573 oe = TEE_HANDLE_NULL; 574 575 *objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe; 576 577 if (res != TEE_SUCCESS && 578 res != TEE_ERROR_ACCESS_CONFLICT) 579 TEE_Panic(res); 580 581 return res; 582 } 583 584 void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 585 { 586 TEE_Result res; 587 588 if (objectEnumerator == TEE_HANDLE_NULL) 589 return; 590 591 res = utee_storage_free_enum((unsigned long)objectEnumerator); 592 593 if (res != TEE_SUCCESS) 594 TEE_Panic(res); 595 } 596 597 void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 598 { 599 TEE_Result res; 600 601 if (objectEnumerator == TEE_HANDLE_NULL) 602 return; 603 604 res = utee_storage_reset_enum((unsigned long)objectEnumerator); 605 606 if (res != TEE_SUCCESS) 607 TEE_Panic(res); 608 } 609 610 TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle 611 objectEnumerator, 612 uint32_t storageID) 613 { 614 TEE_Result res; 615 616 res = utee_storage_start_enum((unsigned long)objectEnumerator, 617 storageID); 618 619 if (res != TEE_SUCCESS && 620 res != TEE_ERROR_ITEM_NOT_FOUND && 621 res != TEE_ERROR_CORRUPT_OBJECT && 622 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 623 TEE_Panic(res); 624 625 return res; 626 } 627 628 TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator, 629 TEE_ObjectInfo *objectInfo, 630 void *objectID, uint32_t *objectIDLen) 631 { 632 TEE_Result res; 633 uint64_t len; 634 TEE_ObjectInfo local_info; 635 TEE_ObjectInfo *pt_info; 636 637 if (!objectID) { 638 res = TEE_ERROR_BAD_PARAMETERS; 639 goto out; 640 } 641 642 if (!objectIDLen) { 643 res = TEE_ERROR_BAD_PARAMETERS; 644 goto out; 645 } 646 647 if (objectInfo) 648 pt_info = objectInfo; 649 else 650 pt_info = &local_info; 651 len = *objectIDLen; 652 res = utee_storage_next_enum((unsigned long)objectEnumerator, 653 pt_info, objectID, &len); 654 *objectIDLen = len; 655 656 out: 657 if (res != TEE_SUCCESS && 658 res != TEE_ERROR_ITEM_NOT_FOUND && 659 res != TEE_ERROR_CORRUPT_OBJECT && 660 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 661 TEE_Panic(res); 662 663 return res; 664 } 665 666 /* Data and Key Storage API - Data Stream Access Functions */ 667 668 TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer, 669 uint32_t size, uint32_t *count) 670 { 671 TEE_Result res; 672 uint64_t cnt64; 673 674 if (object == TEE_HANDLE_NULL) { 675 res = TEE_ERROR_BAD_PARAMETERS; 676 goto out; 677 } 678 679 cnt64 = *count; 680 res = utee_storage_obj_read((unsigned long)object, buffer, size, 681 &cnt64); 682 *count = cnt64; 683 684 out: 685 if (res != TEE_SUCCESS && 686 res != TEE_ERROR_CORRUPT_OBJECT && 687 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 688 TEE_Panic(res); 689 690 return res; 691 } 692 693 TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, const void *buffer, 694 uint32_t size) 695 { 696 TEE_Result res; 697 698 if (object == TEE_HANDLE_NULL) { 699 res = TEE_ERROR_BAD_PARAMETERS; 700 goto out; 701 } 702 703 if (size > TEE_DATA_MAX_POSITION) { 704 res = TEE_ERROR_OVERFLOW; 705 goto out; 706 } 707 708 res = utee_storage_obj_write((unsigned long)object, buffer, size); 709 710 out: 711 if (res != TEE_SUCCESS && 712 res != TEE_ERROR_STORAGE_NO_SPACE && 713 res != TEE_ERROR_OVERFLOW && 714 res != TEE_ERROR_CORRUPT_OBJECT && 715 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 716 TEE_Panic(res); 717 718 return res; 719 } 720 721 TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size) 722 { 723 TEE_Result res; 724 725 if (object == TEE_HANDLE_NULL) { 726 res = TEE_ERROR_BAD_PARAMETERS; 727 goto out; 728 } 729 730 res = utee_storage_obj_trunc((unsigned long)object, size); 731 732 out: 733 if (res != TEE_SUCCESS && 734 res != TEE_ERROR_STORAGE_NO_SPACE && 735 res != TEE_ERROR_CORRUPT_OBJECT && 736 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 737 TEE_Panic(res); 738 739 return res; 740 } 741 742 TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset, 743 TEE_Whence whence) 744 { 745 TEE_Result res; 746 TEE_ObjectInfo info; 747 748 if (object == TEE_HANDLE_NULL) { 749 res = TEE_ERROR_BAD_PARAMETERS; 750 goto out; 751 } 752 753 res = utee_cryp_obj_get_info((unsigned long)object, &info); 754 if (res != TEE_SUCCESS) 755 goto out; 756 757 switch (whence) { 758 case TEE_DATA_SEEK_SET: 759 if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) { 760 res = TEE_ERROR_OVERFLOW; 761 goto out; 762 } 763 break; 764 case TEE_DATA_SEEK_CUR: 765 if (offset > 0 && 766 ((uint32_t)offset + info.dataPosition > 767 TEE_DATA_MAX_POSITION || 768 (uint32_t)offset + info.dataPosition < 769 info.dataPosition)) { 770 res = TEE_ERROR_OVERFLOW; 771 goto out; 772 } 773 break; 774 case TEE_DATA_SEEK_END: 775 if (offset > 0 && 776 ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION || 777 (uint32_t)offset + info.dataSize < info.dataSize)) { 778 res = TEE_ERROR_OVERFLOW; 779 goto out; 780 } 781 break; 782 default: 783 res = TEE_ERROR_ITEM_NOT_FOUND; 784 goto out; 785 } 786 787 res = utee_storage_obj_seek((unsigned long)object, offset, whence); 788 789 out: 790 if (res != TEE_SUCCESS && 791 res != TEE_ERROR_OVERFLOW && 792 res != TEE_ERROR_CORRUPT_OBJECT && 793 res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 794 TEE_Panic(res); 795 796 return res; 797 } 798