1b0104773SPascal Brand /* 2b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 3b0104773SPascal Brand * All rights reserved. 4b0104773SPascal Brand * 5b0104773SPascal Brand * Redistribution and use in source and binary forms, with or without 6b0104773SPascal Brand * modification, are permitted provided that the following conditions are met: 7b0104773SPascal Brand * 8b0104773SPascal Brand * 1. Redistributions of source code must retain the above copyright notice, 9b0104773SPascal Brand * this list of conditions and the following disclaimer. 10b0104773SPascal Brand * 11b0104773SPascal Brand * 2. Redistributions in binary form must reproduce the above copyright notice, 12b0104773SPascal Brand * this list of conditions and the following disclaimer in the documentation 13b0104773SPascal Brand * and/or other materials provided with the distribution. 14b0104773SPascal Brand * 15b0104773SPascal Brand * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16b0104773SPascal Brand * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17b0104773SPascal Brand * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18b0104773SPascal Brand * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19b0104773SPascal Brand * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20b0104773SPascal Brand * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21b0104773SPascal Brand * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22b0104773SPascal Brand * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23b0104773SPascal Brand * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24b0104773SPascal Brand * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25b0104773SPascal Brand * POSSIBILITY OF SUCH DAMAGE. 26b0104773SPascal Brand */ 27b0104773SPascal Brand #include <stdlib.h> 28b0104773SPascal Brand #include <string.h> 29b0104773SPascal Brand 30b0104773SPascal Brand #include <tee_api.h> 31b0104773SPascal Brand #include <utee_syscalls.h> 32b0104773SPascal Brand 33b0104773SPascal Brand #include <assert.h> 34b0104773SPascal Brand 35b0104773SPascal Brand #define TEE_USAGE_DEFAULT 0xffffffff 36b0104773SPascal Brand 37b0104773SPascal Brand #define TEE_ATTR_BIT_VALUE (1 << 29) 38b0104773SPascal Brand #define TEE_ATTR_BIT_PROTECTED (1 << 28) 39b0104773SPascal Brand 40b0104773SPascal Brand /* Data and Key Storage API - Generic Object Functions */ 417583c59eSCedric Chaumont /* 427583c59eSCedric Chaumont * Use of this function is deprecated 437583c59eSCedric Chaumont * new code SHOULD use the TEE_GetObjectInfo1 function instead 447583c59eSCedric Chaumont * These functions will be removed at some future major revision of 457583c59eSCedric Chaumont * this specification 467583c59eSCedric Chaumont */ 47b0104773SPascal Brand void TEE_GetObjectInfo(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo) 48b0104773SPascal Brand { 49b0104773SPascal Brand TEE_Result res; 50b0104773SPascal Brand 51b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, objectInfo); 527583c59eSCedric Chaumont 53b0104773SPascal Brand if (res != TEE_SUCCESS) 54b0104773SPascal Brand TEE_Panic(res); 557583c59eSCedric Chaumont 567583c59eSCedric Chaumont if (objectInfo->objectType == TEE_TYPE_CORRUPTED_OBJECT) { 577583c59eSCedric Chaumont objectInfo->keySize = 0; 587583c59eSCedric Chaumont objectInfo->maxKeySize = 0; 597583c59eSCedric Chaumont objectInfo->objectUsage = 0; 607583c59eSCedric Chaumont objectInfo->dataSize = 0; 617583c59eSCedric Chaumont objectInfo->dataPosition = 0; 627583c59eSCedric Chaumont objectInfo->handleFlags = 0; 637583c59eSCedric Chaumont } 64b0104773SPascal Brand } 65b0104773SPascal Brand 667583c59eSCedric Chaumont TEE_Result TEE_GetObjectInfo1(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo) 677583c59eSCedric Chaumont { 687583c59eSCedric Chaumont TEE_Result res; 697583c59eSCedric Chaumont 707583c59eSCedric Chaumont res = utee_cryp_obj_get_info((uint32_t)object, objectInfo); 717583c59eSCedric Chaumont 72*a2e9a830SCedric Chaumont if (res != TEE_SUCCESS && 73*a2e9a830SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 74*a2e9a830SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 757583c59eSCedric Chaumont TEE_Panic(res); 767583c59eSCedric Chaumont 777583c59eSCedric Chaumont return res; 787583c59eSCedric Chaumont } 797583c59eSCedric Chaumont 807583c59eSCedric Chaumont /* 817583c59eSCedric Chaumont * Use of this function is deprecated 827583c59eSCedric Chaumont * new code SHOULD use the TEE_RestrictObjectUsage1 function instead 837583c59eSCedric Chaumont * These functions will be removed at some future major revision of 847583c59eSCedric Chaumont * this specification 857583c59eSCedric Chaumont */ 86b0104773SPascal Brand void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage) 87b0104773SPascal Brand { 88b0104773SPascal Brand TEE_Result res; 897583c59eSCedric Chaumont TEE_ObjectInfo objectInfo; 907583c59eSCedric Chaumont 917583c59eSCedric Chaumont res = utee_cryp_obj_get_info((uint32_t)object, &objectInfo); 927583c59eSCedric Chaumont if (objectInfo.objectType == TEE_TYPE_CORRUPTED_OBJECT) 937583c59eSCedric Chaumont return; 947583c59eSCedric Chaumont 957583c59eSCedric Chaumont res = TEE_RestrictObjectUsage1(object, objectUsage); 96b0104773SPascal Brand 97b0104773SPascal Brand if (res != TEE_SUCCESS) 98b0104773SPascal Brand TEE_Panic(0); 99b0104773SPascal Brand } 100b0104773SPascal Brand 1017583c59eSCedric Chaumont TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage) 1027583c59eSCedric Chaumont { 1037583c59eSCedric Chaumont TEE_Result res; 1047583c59eSCedric Chaumont 1057583c59eSCedric Chaumont res = utee_cryp_obj_restrict_usage((uint32_t)object, objectUsage); 1067583c59eSCedric Chaumont 107*a2e9a830SCedric Chaumont if (res != TEE_SUCCESS && 108*a2e9a830SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 109*a2e9a830SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 110*a2e9a830SCedric Chaumont TEE_Panic(res); 1117583c59eSCedric Chaumont 1127583c59eSCedric Chaumont return res; 1137583c59eSCedric Chaumont } 1147583c59eSCedric Chaumont 115b0104773SPascal Brand TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object, 116b0104773SPascal Brand uint32_t attributeID, void *buffer, 11779a3c601SCedric Chaumont uint32_t *size) 118b0104773SPascal Brand { 119b0104773SPascal Brand TEE_Result res; 120b0104773SPascal Brand TEE_ObjectInfo info; 121b0104773SPascal Brand 122b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 123b0104773SPascal Brand if (res != TEE_SUCCESS) 124*a2e9a830SCedric Chaumont goto exit; 125b0104773SPascal Brand 126b0104773SPascal Brand /* This function only supports reference attributes */ 127*a2e9a830SCedric Chaumont if ((attributeID & TEE_ATTR_BIT_VALUE)) { 128*a2e9a830SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 129*a2e9a830SCedric Chaumont goto exit; 130*a2e9a830SCedric Chaumont } 131b0104773SPascal Brand 1320ed6a6caSCedric Chaumont res = utee_cryp_obj_get_attr((uint32_t)object, 1330ed6a6caSCedric Chaumont attributeID, buffer, size); 134b0104773SPascal Brand 135*a2e9a830SCedric Chaumont exit: 1360ed6a6caSCedric Chaumont if (res != TEE_SUCCESS && 1370ed6a6caSCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 1380ed6a6caSCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1390ed6a6caSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 1400ed6a6caSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 141b0104773SPascal Brand TEE_Panic(0); 142b0104773SPascal Brand 143b0104773SPascal Brand return res; 144b0104773SPascal Brand } 145b0104773SPascal Brand 146b0104773SPascal Brand TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object, 147b0104773SPascal Brand uint32_t attributeID, uint32_t *a, 148b0104773SPascal Brand uint32_t *b) 149b0104773SPascal Brand { 150b0104773SPascal Brand TEE_Result res; 151b0104773SPascal Brand TEE_ObjectInfo info; 152b0104773SPascal Brand uint32_t buf[2]; 1537f74c64aSPascal Brand uint32_t size = sizeof(buf); 154b0104773SPascal Brand 155b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 156b0104773SPascal Brand if (res != TEE_SUCCESS) 157*a2e9a830SCedric Chaumont goto exit; 158b0104773SPascal Brand 159b0104773SPascal Brand /* This function only supports value attributes */ 160*a2e9a830SCedric Chaumont if (!(attributeID & TEE_ATTR_BIT_VALUE)) { 161*a2e9a830SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 162*a2e9a830SCedric Chaumont goto exit; 163*a2e9a830SCedric Chaumont } 164b0104773SPascal Brand 1650ed6a6caSCedric Chaumont res = utee_cryp_obj_get_attr((uint32_t)object, 1660ed6a6caSCedric Chaumont attributeID, buf, &size); 167b0104773SPascal Brand 168*a2e9a830SCedric Chaumont exit: 1690ed6a6caSCedric Chaumont if (res != TEE_SUCCESS && 1700ed6a6caSCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 1710ed6a6caSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 1720ed6a6caSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 173b0104773SPascal Brand TEE_Panic(0); 174b0104773SPascal Brand 175b0104773SPascal Brand if (size != sizeof(buf)) 176b0104773SPascal Brand TEE_Panic(0); 177b0104773SPascal Brand 178b0104773SPascal Brand *a = buf[0]; 179b0104773SPascal Brand *b = buf[1]; 180b0104773SPascal Brand 181b0104773SPascal Brand return res; 182b0104773SPascal Brand } 183b0104773SPascal Brand 184b0104773SPascal Brand void TEE_CloseObject(TEE_ObjectHandle object) 185b0104773SPascal Brand { 186b0104773SPascal Brand TEE_Result res; 187b0104773SPascal Brand 188b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 189b0104773SPascal Brand return; 190b0104773SPascal Brand 191b0104773SPascal Brand res = utee_cryp_obj_close((uint32_t)object); 192b0104773SPascal Brand if (res != TEE_SUCCESS) 193b0104773SPascal Brand TEE_Panic(0); 194b0104773SPascal Brand } 195b0104773SPascal Brand 196b0104773SPascal Brand /* Data and Key Storage API - Transient Object Functions */ 197b0104773SPascal Brand 198b0104773SPascal Brand TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType, 19979a3c601SCedric Chaumont uint32_t maxKeySize, 200b0104773SPascal Brand TEE_ObjectHandle *object) 201b0104773SPascal Brand { 202b0104773SPascal Brand TEE_Result res; 203b0104773SPascal Brand uint32_t obj; 204b0104773SPascal Brand 20579a3c601SCedric Chaumont res = utee_cryp_obj_alloc(objectType, maxKeySize, &obj); 206aeb0d927SCedric Chaumont 207aeb0d927SCedric Chaumont if (res != TEE_SUCCESS && 208aeb0d927SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 209aeb0d927SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 210aeb0d927SCedric Chaumont TEE_Panic(0); 211aeb0d927SCedric Chaumont 212b0104773SPascal Brand if (res == TEE_SUCCESS) 213b0104773SPascal Brand *object = (TEE_ObjectHandle) obj; 2140ed6a6caSCedric Chaumont 215b0104773SPascal Brand return res; 216b0104773SPascal Brand } 217b0104773SPascal Brand 218b0104773SPascal Brand void TEE_FreeTransientObject(TEE_ObjectHandle object) 219b0104773SPascal Brand { 220b0104773SPascal Brand TEE_Result res; 221b0104773SPascal Brand TEE_ObjectInfo info; 222b0104773SPascal Brand 223b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 224b0104773SPascal Brand return; 225b0104773SPascal Brand 226b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 227b0104773SPascal Brand if (res != TEE_SUCCESS) 228b0104773SPascal Brand TEE_Panic(0); 229b0104773SPascal Brand 230b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 231b0104773SPascal Brand TEE_Panic(0); 232b0104773SPascal Brand 233b0104773SPascal Brand res = utee_cryp_obj_close((uint32_t)object); 234b0104773SPascal Brand if (res != TEE_SUCCESS) 235b0104773SPascal Brand TEE_Panic(0); 236b0104773SPascal Brand } 237b0104773SPascal Brand 238b0104773SPascal Brand void TEE_ResetTransientObject(TEE_ObjectHandle object) 239b0104773SPascal Brand { 240b0104773SPascal Brand TEE_Result res; 241b0104773SPascal Brand TEE_ObjectInfo info; 242b0104773SPascal Brand 243b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 244b0104773SPascal Brand return; 245b0104773SPascal Brand 246b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 247b0104773SPascal Brand if (res != TEE_SUCCESS) 248b0104773SPascal Brand TEE_Panic(0); 249b0104773SPascal Brand 250b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 251b0104773SPascal Brand TEE_Panic(0); 252b0104773SPascal Brand 253b0104773SPascal Brand res = utee_cryp_obj_reset((uint32_t)object); 254b0104773SPascal Brand if (res != TEE_SUCCESS) 255b0104773SPascal Brand TEE_Panic(0); 256b0104773SPascal Brand } 257b0104773SPascal Brand 258b0104773SPascal Brand TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object, 259b0104773SPascal Brand TEE_Attribute *attrs, 260b0104773SPascal Brand uint32_t attrCount) 261b0104773SPascal Brand { 262b0104773SPascal Brand TEE_Result res; 263b0104773SPascal Brand TEE_ObjectInfo info; 264b0104773SPascal Brand 265b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 266b0104773SPascal Brand if (res != TEE_SUCCESS) 267b0104773SPascal Brand TEE_Panic(0); 268b0104773SPascal Brand 269b0104773SPascal Brand /* Must be a transient object */ 270b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 271b0104773SPascal Brand TEE_Panic(0); 272b0104773SPascal Brand 273b0104773SPascal Brand /* Must not be initialized already */ 274b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 275b0104773SPascal Brand TEE_Panic(0); 276b0104773SPascal Brand 277b0104773SPascal Brand res = utee_cryp_obj_populate((uint32_t)object, attrs, attrCount); 278b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 279b0104773SPascal Brand TEE_Panic(res); 280b0104773SPascal Brand return res; 281b0104773SPascal Brand } 282b0104773SPascal Brand 283b0104773SPascal Brand void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID, 28479a3c601SCedric Chaumont void *buffer, uint32_t length) 285b0104773SPascal Brand { 286b0104773SPascal Brand if (attr == NULL) 287b0104773SPascal Brand TEE_Panic(0); 288b0104773SPascal Brand if ((attributeID & TEE_ATTR_BIT_VALUE) != 0) 289b0104773SPascal Brand TEE_Panic(0); 290b0104773SPascal Brand attr->attributeID = attributeID; 291b0104773SPascal Brand attr->content.ref.buffer = buffer; 292b0104773SPascal Brand attr->content.ref.length = length; 293b0104773SPascal Brand } 294b0104773SPascal Brand 295b0104773SPascal Brand void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID, 296b0104773SPascal Brand uint32_t a, uint32_t b) 297b0104773SPascal Brand { 298b0104773SPascal Brand if (attr == NULL) 299b0104773SPascal Brand TEE_Panic(0); 300b0104773SPascal Brand if ((attributeID & TEE_ATTR_BIT_VALUE) == 0) 301b0104773SPascal Brand TEE_Panic(0); 302b0104773SPascal Brand attr->attributeID = attributeID; 303b0104773SPascal Brand attr->content.value.a = a; 304b0104773SPascal Brand attr->content.value.b = b; 305b0104773SPascal Brand } 306b0104773SPascal Brand 3077583c59eSCedric Chaumont /* 3087583c59eSCedric Chaumont * Use of this function is deprecated 3097583c59eSCedric Chaumont * new code SHOULD use the TEE_CopyObjectAttributes1 function instead 3107583c59eSCedric Chaumont * These functions will be removed at some future major revision of 3117583c59eSCedric Chaumont * this specification 3127583c59eSCedric Chaumont */ 313b0104773SPascal Brand void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject, 314b0104773SPascal Brand TEE_ObjectHandle srcObject) 315b0104773SPascal Brand { 316b0104773SPascal Brand TEE_Result res; 3177583c59eSCedric Chaumont TEE_ObjectInfo src_info; 3187583c59eSCedric Chaumont 3197583c59eSCedric Chaumont res = utee_cryp_obj_get_info((uint32_t)srcObject, &src_info); 3207583c59eSCedric Chaumont if (src_info.objectType == TEE_TYPE_CORRUPTED_OBJECT) 3217583c59eSCedric Chaumont return; 3227583c59eSCedric Chaumont 3237583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(destObject, srcObject); 3247583c59eSCedric Chaumont if (res != TEE_SUCCESS) 3257583c59eSCedric Chaumont TEE_Panic(0); 3267583c59eSCedric Chaumont } 3277583c59eSCedric Chaumont 3287583c59eSCedric Chaumont TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject, 3297583c59eSCedric Chaumont TEE_ObjectHandle srcObject) 3307583c59eSCedric Chaumont { 3317583c59eSCedric Chaumont TEE_Result res; 332b0104773SPascal Brand TEE_ObjectInfo dst_info; 333b0104773SPascal Brand TEE_ObjectInfo src_info; 334b0104773SPascal Brand 335b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)destObject, &dst_info); 336b0104773SPascal Brand if (res != TEE_SUCCESS) 337*a2e9a830SCedric Chaumont goto exit; 338b0104773SPascal Brand 339b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)srcObject, &src_info); 340b0104773SPascal Brand if (res != TEE_SUCCESS) 341*a2e9a830SCedric Chaumont goto exit; 342b0104773SPascal Brand 343*a2e9a830SCedric Chaumont if (!(src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 344b0104773SPascal Brand TEE_Panic(0); 345*a2e9a830SCedric Chaumont 346*a2e9a830SCedric Chaumont if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) 347b0104773SPascal Brand TEE_Panic(0); 348*a2e9a830SCedric Chaumont 349*a2e9a830SCedric Chaumont if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 350b0104773SPascal Brand TEE_Panic(0); 351b0104773SPascal Brand 352b0104773SPascal Brand res = utee_cryp_obj_copy((uint32_t)destObject, (uint32_t)srcObject); 3537583c59eSCedric Chaumont 354*a2e9a830SCedric Chaumont exit: 355*a2e9a830SCedric Chaumont if (res != TEE_SUCCESS && 356*a2e9a830SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 357*a2e9a830SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 358*a2e9a830SCedric Chaumont TEE_Panic(res); 3597583c59eSCedric Chaumont 3607583c59eSCedric Chaumont return res; 361b0104773SPascal Brand } 362b0104773SPascal Brand 363b0104773SPascal Brand TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize, 364b0104773SPascal Brand TEE_Attribute *params, uint32_t paramCount) 365b0104773SPascal Brand { 366b0104773SPascal Brand TEE_Result res; 367b0104773SPascal Brand 368b0104773SPascal Brand res = utee_cryp_obj_generate_key((uint32_t)object, keySize, 369b0104773SPascal Brand params, paramCount); 370b0104773SPascal Brand 371aeb0d927SCedric Chaumont if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 372b0104773SPascal Brand TEE_Panic(0); 373b0104773SPascal Brand 374b0104773SPascal Brand return res; 375b0104773SPascal Brand } 376b0104773SPascal Brand 377b0104773SPascal Brand /* Data and Key Storage API - Persistent Object Functions */ 378b0104773SPascal Brand 379b0104773SPascal Brand TEE_Result TEE_OpenPersistentObject(uint32_t storageID, void *objectID, 38079a3c601SCedric Chaumont uint32_t objectIDLen, uint32_t flags, 381b0104773SPascal Brand TEE_ObjectHandle *object) 382b0104773SPascal Brand { 3839b520646SCedric Chaumont TEE_Result res; 384b0104773SPascal Brand 3859b520646SCedric Chaumont if (storageID != TEE_STORAGE_PRIVATE) { 3869b520646SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 3879b520646SCedric Chaumont goto out; 3889b520646SCedric Chaumont } 389b0104773SPascal Brand 3909b520646SCedric Chaumont if (!objectID) { 3919b520646SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 3929b520646SCedric Chaumont goto out; 3939b520646SCedric Chaumont } 3949b520646SCedric Chaumont 3959b520646SCedric Chaumont if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) { 3969b520646SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 3979b520646SCedric Chaumont goto out; 3989b520646SCedric Chaumont } 3999b520646SCedric Chaumont 4009b520646SCedric Chaumont if (!object) { 4019b520646SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 4029b520646SCedric Chaumont goto out; 4039b520646SCedric Chaumont } 4049b520646SCedric Chaumont 4059b520646SCedric Chaumont res = utee_storage_obj_open(storageID, objectID, objectIDLen, flags, 4069b520646SCedric Chaumont object); 4079b520646SCedric Chaumont 4089b520646SCedric Chaumont out: 4099b520646SCedric Chaumont if (res != TEE_SUCCESS && 4109b520646SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 4119b520646SCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT && 4129b520646SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 4139b520646SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 4149b520646SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 415b0104773SPascal Brand TEE_Panic(0); 416b0104773SPascal Brand 4179b520646SCedric Chaumont return res; 418b0104773SPascal Brand } 419b0104773SPascal Brand 420b0104773SPascal Brand TEE_Result TEE_CreatePersistentObject(uint32_t storageID, void *objectID, 42179a3c601SCedric Chaumont uint32_t objectIDLen, uint32_t flags, 422b0104773SPascal Brand TEE_ObjectHandle attributes, 423b0104773SPascal Brand const void *initialData, 42479a3c601SCedric Chaumont uint32_t initialDataLen, 425b0104773SPascal Brand TEE_ObjectHandle *object) 426b0104773SPascal Brand { 42784431ae3SCedric Chaumont TEE_Result res; 428b0104773SPascal Brand 42984431ae3SCedric Chaumont if (storageID != TEE_STORAGE_PRIVATE) { 43084431ae3SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 43184431ae3SCedric Chaumont goto err; 43284431ae3SCedric Chaumont } 433b0104773SPascal Brand 434aeb0d927SCedric Chaumont if (!objectID) { 43584431ae3SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 43684431ae3SCedric Chaumont goto err; 43784431ae3SCedric Chaumont } 438b0104773SPascal Brand 43984431ae3SCedric Chaumont if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) { 44084431ae3SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 44184431ae3SCedric Chaumont goto err; 44284431ae3SCedric Chaumont } 443b0104773SPascal Brand 444aeb0d927SCedric Chaumont if (!object) { 44584431ae3SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 44684431ae3SCedric Chaumont goto err; 44784431ae3SCedric Chaumont } 44884431ae3SCedric Chaumont 44984431ae3SCedric Chaumont res = utee_storage_obj_create(storageID, objectID, objectIDLen, flags, 450b0104773SPascal Brand attributes, initialData, initialDataLen, 451b0104773SPascal Brand object); 45284431ae3SCedric Chaumont if (res == TEE_SUCCESS) 45384431ae3SCedric Chaumont goto out; 45484431ae3SCedric Chaumont err: 45584431ae3SCedric Chaumont if (res == TEE_ERROR_ITEM_NOT_FOUND || 45684431ae3SCedric Chaumont res == TEE_ERROR_ACCESS_CONFLICT || 45784431ae3SCedric Chaumont res == TEE_ERROR_OUT_OF_MEMORY || 45884431ae3SCedric Chaumont res == TEE_ERROR_STORAGE_NO_SPACE || 45984431ae3SCedric Chaumont res == TEE_ERROR_CORRUPT_OBJECT || 46084431ae3SCedric Chaumont res == TEE_ERROR_STORAGE_NOT_AVAILABLE) 46184431ae3SCedric Chaumont return res; 46284431ae3SCedric Chaumont TEE_Panic(0); 46384431ae3SCedric Chaumont out: 46484431ae3SCedric Chaumont return TEE_SUCCESS; 465b0104773SPascal Brand } 466b0104773SPascal Brand 4677583c59eSCedric Chaumont /* 4687583c59eSCedric Chaumont * Use of this function is deprecated 4697583c59eSCedric Chaumont * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead 4707583c59eSCedric Chaumont * These functions will be removed at some future major revision of 4717583c59eSCedric Chaumont * this specification 4727583c59eSCedric Chaumont */ 473b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object) 474b0104773SPascal Brand { 475b0104773SPascal Brand TEE_Result res; 476b0104773SPascal Brand 477b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 478b0104773SPascal Brand return; 479b0104773SPascal Brand 4807583c59eSCedric Chaumont res = TEE_CloseAndDeletePersistentObject1(object); 481b0104773SPascal Brand 482b0104773SPascal Brand if (res != TEE_SUCCESS) 483b0104773SPascal Brand TEE_Panic(0); 484b0104773SPascal Brand } 485b0104773SPascal Brand 4867583c59eSCedric Chaumont TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object) 4877583c59eSCedric Chaumont { 4887583c59eSCedric Chaumont TEE_Result res; 4897583c59eSCedric Chaumont 4907583c59eSCedric Chaumont if (object == TEE_HANDLE_NULL) 4917583c59eSCedric Chaumont return TEE_ERROR_STORAGE_NOT_AVAILABLE; 4927583c59eSCedric Chaumont 4937583c59eSCedric Chaumont res = utee_storage_obj_del(object); 4947583c59eSCedric Chaumont 4957583c59eSCedric Chaumont if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 4967583c59eSCedric Chaumont TEE_Panic(0); 4977583c59eSCedric Chaumont 4987583c59eSCedric Chaumont return res; 4997583c59eSCedric Chaumont } 5007583c59eSCedric Chaumont 5017583c59eSCedric Chaumont 502b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object, 503b0104773SPascal Brand const void *newObjectID, 50479a3c601SCedric Chaumont uint32_t newObjectIDLen) 505b0104773SPascal Brand { 506b0104773SPascal Brand TEE_Result res; 507b0104773SPascal Brand 508a76bf53fSCedric Chaumont if (object == TEE_HANDLE_NULL) { 509a76bf53fSCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 510a76bf53fSCedric Chaumont goto out; 511a76bf53fSCedric Chaumont } 512b0104773SPascal Brand 513a76bf53fSCedric Chaumont if (!newObjectID) { 514a76bf53fSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 515a76bf53fSCedric Chaumont goto out; 516a76bf53fSCedric Chaumont } 517b0104773SPascal Brand 518a76bf53fSCedric Chaumont if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN) { 519a76bf53fSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 520a76bf53fSCedric Chaumont goto out; 521a76bf53fSCedric Chaumont } 522b0104773SPascal Brand 523b0104773SPascal Brand res = utee_storage_obj_rename(object, newObjectID, newObjectIDLen); 524b0104773SPascal Brand 525a76bf53fSCedric Chaumont out: 526a76bf53fSCedric Chaumont if (res != TEE_SUCCESS && 527a76bf53fSCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT && 528a76bf53fSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 529a76bf53fSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 530b0104773SPascal Brand TEE_Panic(0); 531b0104773SPascal Brand 532b0104773SPascal Brand return res; 533b0104773SPascal Brand } 534b0104773SPascal Brand 535b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle * 536b0104773SPascal Brand objectEnumerator) 537b0104773SPascal Brand { 538b0104773SPascal Brand TEE_Result res; 539b0104773SPascal Brand 54015cd3c30SCedric Chaumont if (!objectEnumerator) 541b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 542b0104773SPascal Brand 543b0104773SPascal Brand res = utee_storage_alloc_enum(objectEnumerator); 544b0104773SPascal Brand 545b0104773SPascal Brand if (res != TEE_SUCCESS) 546b0104773SPascal Brand *objectEnumerator = TEE_HANDLE_NULL; 547b0104773SPascal Brand 54815cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 54915cd3c30SCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT) 55015cd3c30SCedric Chaumont TEE_Panic(0); 55115cd3c30SCedric Chaumont 552b0104773SPascal Brand return res; 553b0104773SPascal Brand } 554b0104773SPascal Brand 555b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 556b0104773SPascal Brand { 557b0104773SPascal Brand TEE_Result res; 558b0104773SPascal Brand 559b0104773SPascal Brand if (objectEnumerator == TEE_HANDLE_NULL) 560b0104773SPascal Brand return; 561b0104773SPascal Brand 562b0104773SPascal Brand res = utee_storage_free_enum(objectEnumerator); 563b0104773SPascal Brand 564b0104773SPascal Brand if (res != TEE_SUCCESS) 565b0104773SPascal Brand TEE_Panic(0); 566b0104773SPascal Brand } 567b0104773SPascal Brand 568b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 569b0104773SPascal Brand { 570b0104773SPascal Brand TEE_Result res; 571b0104773SPascal Brand 572b0104773SPascal Brand if (objectEnumerator == TEE_HANDLE_NULL) 573b0104773SPascal Brand return; 574b0104773SPascal Brand 575b0104773SPascal Brand res = utee_storage_reset_enum(objectEnumerator); 576b0104773SPascal Brand 577b0104773SPascal Brand if (res != TEE_SUCCESS) 578b0104773SPascal Brand TEE_Panic(0); 579b0104773SPascal Brand } 580b0104773SPascal Brand 581b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle 582b0104773SPascal Brand objectEnumerator, 583b0104773SPascal Brand uint32_t storageID) 584b0104773SPascal Brand { 585b0104773SPascal Brand TEE_Result res; 586b0104773SPascal Brand 587b0104773SPascal Brand if (storageID != TEE_STORAGE_PRIVATE) 588b0104773SPascal Brand return TEE_ERROR_ITEM_NOT_FOUND; 589b0104773SPascal Brand 590b0104773SPascal Brand res = utee_storage_start_enum(objectEnumerator, storageID); 591b0104773SPascal Brand 59215cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 59315cd3c30SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 59415cd3c30SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 59515cd3c30SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 596b0104773SPascal Brand TEE_Panic(0); 597b0104773SPascal Brand 598b0104773SPascal Brand return res; 599b0104773SPascal Brand } 600b0104773SPascal Brand 601b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator, 602b0104773SPascal Brand TEE_ObjectInfo *objectInfo, 60379a3c601SCedric Chaumont void *objectID, uint32_t *objectIDLen) 604b0104773SPascal Brand { 605b0104773SPascal Brand TEE_Result res; 606b0104773SPascal Brand 60715cd3c30SCedric Chaumont if (!objectID) { 60815cd3c30SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 60915cd3c30SCedric Chaumont goto out; 61015cd3c30SCedric Chaumont } 61115cd3c30SCedric Chaumont 61215cd3c30SCedric Chaumont if (!objectIDLen) { 61315cd3c30SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 61415cd3c30SCedric Chaumont goto out; 61515cd3c30SCedric Chaumont } 61615cd3c30SCedric Chaumont 617ae1289baSCedric Chaumont res = utee_storage_next_enum(objectEnumerator, objectInfo, objectID, 618b0104773SPascal Brand objectIDLen); 619b0104773SPascal Brand 62015cd3c30SCedric Chaumont out: 62115cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 62215cd3c30SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 62315cd3c30SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 62415cd3c30SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 625b0104773SPascal Brand TEE_Panic(0); 626b0104773SPascal Brand 627b0104773SPascal Brand return res; 628b0104773SPascal Brand } 629b0104773SPascal Brand 630b0104773SPascal Brand /* Data and Key Storage API - Data Stream Access Functions */ 631b0104773SPascal Brand 632b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer, 63379a3c601SCedric Chaumont uint32_t size, uint32_t *count) 634b0104773SPascal Brand { 635b0104773SPascal Brand TEE_Result res; 636b0104773SPascal Brand 637ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 638ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 639ae1289baSCedric Chaumont goto out; 640ae1289baSCedric Chaumont } 641b0104773SPascal Brand 642b0104773SPascal Brand res = utee_storage_obj_read(object, buffer, size, count); 643b0104773SPascal Brand 644ae1289baSCedric Chaumont out: 645ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 646ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 647ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 648b0104773SPascal Brand TEE_Panic(0); 649b0104773SPascal Brand 650b0104773SPascal Brand return res; 651b0104773SPascal Brand } 652b0104773SPascal Brand 653b0104773SPascal Brand TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, void *buffer, 65479a3c601SCedric Chaumont uint32_t size) 655b0104773SPascal Brand { 656b0104773SPascal Brand TEE_Result res; 657b0104773SPascal Brand 658ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 659ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 660ae1289baSCedric Chaumont goto out; 661ae1289baSCedric Chaumont } 662ae1289baSCedric Chaumont 663ae1289baSCedric Chaumont if (size > TEE_DATA_MAX_POSITION) { 664ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 665ae1289baSCedric Chaumont goto out; 666ae1289baSCedric Chaumont } 667b0104773SPascal Brand 668b0104773SPascal Brand res = utee_storage_obj_write(object, buffer, size); 669b0104773SPascal Brand 670ae1289baSCedric Chaumont out: 671ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 672ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NO_SPACE && 673ae1289baSCedric Chaumont res != TEE_ERROR_OVERFLOW && 674ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 675ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 676b0104773SPascal Brand TEE_Panic(0); 677b0104773SPascal Brand 678b0104773SPascal Brand return res; 679b0104773SPascal Brand } 680b0104773SPascal Brand 681b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size) 682b0104773SPascal Brand { 683b0104773SPascal Brand TEE_Result res; 684b0104773SPascal Brand 685ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 686ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 687ae1289baSCedric Chaumont goto out; 688ae1289baSCedric Chaumont } 689b0104773SPascal Brand 690b0104773SPascal Brand res = utee_storage_obj_trunc(object, size); 691b0104773SPascal Brand 692ae1289baSCedric Chaumont out: 693ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 694ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NO_SPACE && 695ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 696ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 697b0104773SPascal Brand TEE_Panic(0); 698b0104773SPascal Brand 699b0104773SPascal Brand return res; 700b0104773SPascal Brand } 701b0104773SPascal Brand 702b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset, 703b0104773SPascal Brand TEE_Whence whence) 704b0104773SPascal Brand { 705b0104773SPascal Brand TEE_Result res; 706b0104773SPascal Brand TEE_ObjectInfo info; 707b0104773SPascal Brand 708ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 709ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 710ae1289baSCedric Chaumont goto out; 711ae1289baSCedric Chaumont } 712b0104773SPascal Brand 713b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 714b0104773SPascal Brand if (res != TEE_SUCCESS) 715ae1289baSCedric Chaumont goto out; 716b0104773SPascal Brand 717b0104773SPascal Brand switch (whence) { 718b0104773SPascal Brand case TEE_DATA_SEEK_SET: 719ae1289baSCedric Chaumont if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) { 720ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 721ae1289baSCedric Chaumont goto out; 722ae1289baSCedric Chaumont } 723b0104773SPascal Brand break; 724b0104773SPascal Brand case TEE_DATA_SEEK_CUR: 725b0104773SPascal Brand if (offset > 0 && 726b0104773SPascal Brand ((uint32_t)offset + info.dataPosition > 727b0104773SPascal Brand TEE_DATA_MAX_POSITION || 728b0104773SPascal Brand (uint32_t)offset + info.dataPosition < 729ae1289baSCedric Chaumont info.dataPosition)) { 730ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 731ae1289baSCedric Chaumont goto out; 732ae1289baSCedric Chaumont } 733b0104773SPascal Brand break; 734b0104773SPascal Brand case TEE_DATA_SEEK_END: 735b0104773SPascal Brand if (offset > 0 && 736b0104773SPascal Brand ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION || 737ae1289baSCedric Chaumont (uint32_t)offset + info.dataSize < info.dataSize)) { 738ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 739ae1289baSCedric Chaumont goto out; 740ae1289baSCedric Chaumont } 741b0104773SPascal Brand break; 742b0104773SPascal Brand default: 743ae1289baSCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 744ae1289baSCedric Chaumont goto out; 745b0104773SPascal Brand } 746b0104773SPascal Brand 747b0104773SPascal Brand res = utee_storage_obj_seek(object, offset, whence); 748b0104773SPascal Brand 749ae1289baSCedric Chaumont out: 750ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 751ae1289baSCedric Chaumont res != TEE_ERROR_OVERFLOW && 752ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 753ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 754b0104773SPascal Brand TEE_Panic(0); 755b0104773SPascal Brand 756b0104773SPascal Brand return res; 757b0104773SPascal Brand } 758