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 727583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) { 737583c59eSCedric Chaumont res = utee_storage_obj_del(object); 747583c59eSCedric Chaumont if (res != TEE_SUCCESS) 757583c59eSCedric Chaumont TEE_Panic(0); 767583c59eSCedric Chaumont return TEE_ERROR_CORRUPT_OBJECT; 777583c59eSCedric Chaumont } 787583c59eSCedric Chaumont 797583c59eSCedric Chaumont if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 807583c59eSCedric Chaumont TEE_Panic(res); 817583c59eSCedric Chaumont 827583c59eSCedric Chaumont return res; 837583c59eSCedric Chaumont } 847583c59eSCedric Chaumont 857583c59eSCedric Chaumont /* 867583c59eSCedric Chaumont * Use of this function is deprecated 877583c59eSCedric Chaumont * new code SHOULD use the TEE_RestrictObjectUsage1 function instead 887583c59eSCedric Chaumont * These functions will be removed at some future major revision of 897583c59eSCedric Chaumont * this specification 907583c59eSCedric Chaumont */ 91b0104773SPascal Brand void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage) 92b0104773SPascal Brand { 93b0104773SPascal Brand TEE_Result res; 947583c59eSCedric Chaumont TEE_ObjectInfo objectInfo; 957583c59eSCedric Chaumont 967583c59eSCedric Chaumont res = utee_cryp_obj_get_info((uint32_t)object, &objectInfo); 977583c59eSCedric Chaumont if (objectInfo.objectType == TEE_TYPE_CORRUPTED_OBJECT) 987583c59eSCedric Chaumont return; 997583c59eSCedric Chaumont 1007583c59eSCedric Chaumont res = TEE_RestrictObjectUsage1(object, objectUsage); 101b0104773SPascal Brand 102b0104773SPascal Brand if (res != TEE_SUCCESS) 103b0104773SPascal Brand TEE_Panic(0); 104b0104773SPascal Brand } 105b0104773SPascal Brand 1067583c59eSCedric Chaumont TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage) 1077583c59eSCedric Chaumont { 1087583c59eSCedric Chaumont TEE_Result res; 1097583c59eSCedric Chaumont 1107583c59eSCedric Chaumont res = utee_cryp_obj_restrict_usage((uint32_t)object, objectUsage); 1117583c59eSCedric Chaumont 1127583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) { 1137583c59eSCedric Chaumont res = utee_storage_obj_del(object); 1147583c59eSCedric Chaumont if (res != TEE_SUCCESS) 1157583c59eSCedric Chaumont TEE_Panic(0); 1167583c59eSCedric Chaumont return TEE_ERROR_CORRUPT_OBJECT; 1177583c59eSCedric Chaumont } 1187583c59eSCedric Chaumont 1197583c59eSCedric Chaumont if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 1207583c59eSCedric Chaumont TEE_Panic(0); 1217583c59eSCedric Chaumont 1227583c59eSCedric Chaumont return res; 1237583c59eSCedric Chaumont } 1247583c59eSCedric Chaumont 125b0104773SPascal Brand TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object, 126b0104773SPascal Brand uint32_t attributeID, void *buffer, 12779a3c601SCedric Chaumont uint32_t *size) 128b0104773SPascal Brand { 129b0104773SPascal Brand TEE_Result res; 130b0104773SPascal Brand TEE_ObjectInfo info; 131b0104773SPascal Brand 132b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 133b0104773SPascal Brand if (res != TEE_SUCCESS) 134b0104773SPascal Brand TEE_Panic(0); 135b0104773SPascal Brand 136b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 137b0104773SPascal Brand TEE_Panic(0); 138b0104773SPascal Brand 139b0104773SPascal Brand /* This function only supports reference attributes */ 140b0104773SPascal Brand if ((attributeID & TEE_ATTR_BIT_VALUE) != 0) 141b0104773SPascal Brand TEE_Panic(0); 142b0104773SPascal Brand 1430ed6a6caSCedric Chaumont res = utee_cryp_obj_get_attr((uint32_t)object, 1440ed6a6caSCedric Chaumont attributeID, buffer, size); 145b0104773SPascal Brand 1460ed6a6caSCedric Chaumont if (res != TEE_SUCCESS && 1470ed6a6caSCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 1480ed6a6caSCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1490ed6a6caSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 1500ed6a6caSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 151b0104773SPascal Brand TEE_Panic(0); 152b0104773SPascal Brand 153b0104773SPascal Brand return res; 154b0104773SPascal Brand } 155b0104773SPascal Brand 156b0104773SPascal Brand TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object, 157b0104773SPascal Brand uint32_t attributeID, uint32_t *a, 158b0104773SPascal Brand uint32_t *b) 159b0104773SPascal Brand { 160b0104773SPascal Brand TEE_Result res; 161b0104773SPascal Brand TEE_ObjectInfo info; 162b0104773SPascal Brand uint32_t buf[2]; 1637f74c64aSPascal Brand uint32_t size = sizeof(buf); 164b0104773SPascal Brand 165b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 166b0104773SPascal Brand if (res != TEE_SUCCESS) 167b0104773SPascal Brand TEE_Panic(0); 168b0104773SPascal Brand 169b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 170b0104773SPascal Brand TEE_Panic(0); 171b0104773SPascal Brand 172b0104773SPascal Brand /* This function only supports value attributes */ 173b0104773SPascal Brand if ((attributeID & TEE_ATTR_BIT_VALUE) == 0) 174b0104773SPascal Brand TEE_Panic(0); 175b0104773SPascal Brand 1760ed6a6caSCedric Chaumont res = utee_cryp_obj_get_attr((uint32_t)object, 1770ed6a6caSCedric Chaumont attributeID, buf, &size); 178b0104773SPascal Brand 1790ed6a6caSCedric Chaumont if (res != TEE_SUCCESS && 1800ed6a6caSCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 1810ed6a6caSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 1820ed6a6caSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 183b0104773SPascal Brand TEE_Panic(0); 184b0104773SPascal Brand 185b0104773SPascal Brand if (size != sizeof(buf)) 186b0104773SPascal Brand TEE_Panic(0); 187b0104773SPascal Brand 188b0104773SPascal Brand *a = buf[0]; 189b0104773SPascal Brand *b = buf[1]; 190b0104773SPascal Brand 191b0104773SPascal Brand return res; 192b0104773SPascal Brand } 193b0104773SPascal Brand 194b0104773SPascal Brand void TEE_CloseObject(TEE_ObjectHandle object) 195b0104773SPascal Brand { 196b0104773SPascal Brand TEE_Result res; 197b0104773SPascal Brand 198b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 199b0104773SPascal Brand return; 200b0104773SPascal Brand 201b0104773SPascal Brand res = utee_cryp_obj_close((uint32_t)object); 202b0104773SPascal Brand if (res != TEE_SUCCESS) 203b0104773SPascal Brand TEE_Panic(0); 204b0104773SPascal Brand } 205b0104773SPascal Brand 206b0104773SPascal Brand /* Data and Key Storage API - Transient Object Functions */ 207b0104773SPascal Brand 208b0104773SPascal Brand TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType, 20979a3c601SCedric Chaumont uint32_t maxKeySize, 210b0104773SPascal Brand TEE_ObjectHandle *object) 211b0104773SPascal Brand { 212b0104773SPascal Brand TEE_Result res; 213b0104773SPascal Brand uint32_t obj; 214b0104773SPascal Brand 21579a3c601SCedric Chaumont res = utee_cryp_obj_alloc(objectType, maxKeySize, &obj); 216aeb0d927SCedric Chaumont 217aeb0d927SCedric Chaumont if (res != TEE_SUCCESS && 218aeb0d927SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 219aeb0d927SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 220aeb0d927SCedric Chaumont TEE_Panic(0); 221aeb0d927SCedric Chaumont 222b0104773SPascal Brand if (res == TEE_SUCCESS) 223b0104773SPascal Brand *object = (TEE_ObjectHandle) obj; 2240ed6a6caSCedric Chaumont 225b0104773SPascal Brand return res; 226b0104773SPascal Brand } 227b0104773SPascal Brand 228b0104773SPascal Brand void TEE_FreeTransientObject(TEE_ObjectHandle object) 229b0104773SPascal Brand { 230b0104773SPascal Brand TEE_Result res; 231b0104773SPascal Brand TEE_ObjectInfo info; 232b0104773SPascal Brand 233b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 234b0104773SPascal Brand return; 235b0104773SPascal Brand 236b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 237b0104773SPascal Brand if (res != TEE_SUCCESS) 238b0104773SPascal Brand TEE_Panic(0); 239b0104773SPascal Brand 240b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 241b0104773SPascal Brand TEE_Panic(0); 242b0104773SPascal Brand 243b0104773SPascal Brand res = utee_cryp_obj_close((uint32_t)object); 244b0104773SPascal Brand if (res != TEE_SUCCESS) 245b0104773SPascal Brand TEE_Panic(0); 246b0104773SPascal Brand } 247b0104773SPascal Brand 248b0104773SPascal Brand void TEE_ResetTransientObject(TEE_ObjectHandle object) 249b0104773SPascal Brand { 250b0104773SPascal Brand TEE_Result res; 251b0104773SPascal Brand TEE_ObjectInfo info; 252b0104773SPascal Brand 253b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 254b0104773SPascal Brand return; 255b0104773SPascal Brand 256b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 257b0104773SPascal Brand if (res != TEE_SUCCESS) 258b0104773SPascal Brand TEE_Panic(0); 259b0104773SPascal Brand 260b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 261b0104773SPascal Brand TEE_Panic(0); 262b0104773SPascal Brand 263b0104773SPascal Brand res = utee_cryp_obj_reset((uint32_t)object); 264b0104773SPascal Brand if (res != TEE_SUCCESS) 265b0104773SPascal Brand TEE_Panic(0); 266b0104773SPascal Brand } 267b0104773SPascal Brand 268b0104773SPascal Brand TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object, 269b0104773SPascal Brand TEE_Attribute *attrs, 270b0104773SPascal Brand uint32_t attrCount) 271b0104773SPascal Brand { 272b0104773SPascal Brand TEE_Result res; 273b0104773SPascal Brand TEE_ObjectInfo info; 274b0104773SPascal Brand 275b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 276b0104773SPascal Brand if (res != TEE_SUCCESS) 277b0104773SPascal Brand TEE_Panic(0); 278b0104773SPascal Brand 279b0104773SPascal Brand /* Must be a transient object */ 280b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 281b0104773SPascal Brand TEE_Panic(0); 282b0104773SPascal Brand 283b0104773SPascal Brand /* Must not be initialized already */ 284b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 285b0104773SPascal Brand TEE_Panic(0); 286b0104773SPascal Brand 287b0104773SPascal Brand res = utee_cryp_obj_populate((uint32_t)object, attrs, attrCount); 288b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 289b0104773SPascal Brand TEE_Panic(res); 290b0104773SPascal Brand return res; 291b0104773SPascal Brand } 292b0104773SPascal Brand 293b0104773SPascal Brand void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID, 29479a3c601SCedric Chaumont void *buffer, uint32_t length) 295b0104773SPascal Brand { 296b0104773SPascal Brand if (attr == NULL) 297b0104773SPascal Brand TEE_Panic(0); 298b0104773SPascal Brand if ((attributeID & TEE_ATTR_BIT_VALUE) != 0) 299b0104773SPascal Brand TEE_Panic(0); 300b0104773SPascal Brand attr->attributeID = attributeID; 301b0104773SPascal Brand attr->content.ref.buffer = buffer; 302b0104773SPascal Brand attr->content.ref.length = length; 303b0104773SPascal Brand } 304b0104773SPascal Brand 305b0104773SPascal Brand void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID, 306b0104773SPascal Brand uint32_t a, uint32_t b) 307b0104773SPascal Brand { 308b0104773SPascal Brand if (attr == NULL) 309b0104773SPascal Brand TEE_Panic(0); 310b0104773SPascal Brand if ((attributeID & TEE_ATTR_BIT_VALUE) == 0) 311b0104773SPascal Brand TEE_Panic(0); 312b0104773SPascal Brand attr->attributeID = attributeID; 313b0104773SPascal Brand attr->content.value.a = a; 314b0104773SPascal Brand attr->content.value.b = b; 315b0104773SPascal Brand } 316b0104773SPascal Brand 3177583c59eSCedric Chaumont /* 3187583c59eSCedric Chaumont * Use of this function is deprecated 3197583c59eSCedric Chaumont * new code SHOULD use the TEE_CopyObjectAttributes1 function instead 3207583c59eSCedric Chaumont * These functions will be removed at some future major revision of 3217583c59eSCedric Chaumont * this specification 3227583c59eSCedric Chaumont */ 323b0104773SPascal Brand void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject, 324b0104773SPascal Brand TEE_ObjectHandle srcObject) 325b0104773SPascal Brand { 326b0104773SPascal Brand TEE_Result res; 3277583c59eSCedric Chaumont TEE_ObjectInfo src_info; 3287583c59eSCedric Chaumont 3297583c59eSCedric Chaumont res = utee_cryp_obj_get_info((uint32_t)srcObject, &src_info); 3307583c59eSCedric Chaumont if (src_info.objectType == TEE_TYPE_CORRUPTED_OBJECT) 3317583c59eSCedric Chaumont return; 3327583c59eSCedric Chaumont 3337583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(destObject, srcObject); 3347583c59eSCedric Chaumont if (res != TEE_SUCCESS) 3357583c59eSCedric Chaumont TEE_Panic(0); 3367583c59eSCedric Chaumont } 3377583c59eSCedric Chaumont 3387583c59eSCedric Chaumont TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject, 3397583c59eSCedric Chaumont TEE_ObjectHandle srcObject) 3407583c59eSCedric Chaumont { 3417583c59eSCedric Chaumont TEE_Result res; 342b0104773SPascal Brand TEE_ObjectInfo dst_info; 343b0104773SPascal Brand TEE_ObjectInfo src_info; 344b0104773SPascal Brand 345b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)destObject, &dst_info); 346b0104773SPascal Brand if (res != TEE_SUCCESS) 3477583c59eSCedric Chaumont goto err; 348b0104773SPascal Brand 349b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)srcObject, &src_info); 350b0104773SPascal Brand if (res != TEE_SUCCESS) 3517583c59eSCedric Chaumont goto err; 352b0104773SPascal Brand 353b0104773SPascal Brand if ((src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) 354b0104773SPascal Brand TEE_Panic(0); 355b0104773SPascal Brand if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 356b0104773SPascal Brand TEE_Panic(0); 357b0104773SPascal Brand if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 358b0104773SPascal Brand TEE_Panic(0); 359b0104773SPascal Brand 360b0104773SPascal Brand res = utee_cryp_obj_copy((uint32_t)destObject, (uint32_t)srcObject); 361b0104773SPascal Brand if (res != TEE_SUCCESS) 362b0104773SPascal Brand TEE_Panic(0); 3637583c59eSCedric Chaumont 3647583c59eSCedric Chaumont goto out; 3657583c59eSCedric Chaumont 3667583c59eSCedric Chaumont err: 3677583c59eSCedric Chaumont if (res == TEE_ERROR_CORRUPT_OBJECT) { 3687583c59eSCedric Chaumont res = utee_storage_obj_del(srcObject); 3697583c59eSCedric Chaumont if (res != TEE_SUCCESS) 3707583c59eSCedric Chaumont TEE_Panic(0); 3717583c59eSCedric Chaumont return TEE_ERROR_CORRUPT_OBJECT; 3727583c59eSCedric Chaumont } 3737583c59eSCedric Chaumont if (res == TEE_ERROR_STORAGE_NOT_AVAILABLE) 3747583c59eSCedric Chaumont return res; 3757583c59eSCedric Chaumont TEE_Panic(0); 3767583c59eSCedric Chaumont out: 3777583c59eSCedric Chaumont return TEE_SUCCESS; 378b0104773SPascal Brand } 379b0104773SPascal Brand 380b0104773SPascal Brand TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize, 381b0104773SPascal Brand TEE_Attribute *params, uint32_t paramCount) 382b0104773SPascal Brand { 383b0104773SPascal Brand TEE_Result res; 384b0104773SPascal Brand 385b0104773SPascal Brand res = utee_cryp_obj_generate_key((uint32_t)object, keySize, 386b0104773SPascal Brand params, paramCount); 387b0104773SPascal Brand 388aeb0d927SCedric Chaumont if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 389b0104773SPascal Brand TEE_Panic(0); 390b0104773SPascal Brand 391b0104773SPascal Brand return res; 392b0104773SPascal Brand } 393b0104773SPascal Brand 394b0104773SPascal Brand /* Data and Key Storage API - Persistent Object Functions */ 395b0104773SPascal Brand 396b0104773SPascal Brand TEE_Result TEE_OpenPersistentObject(uint32_t storageID, void *objectID, 39779a3c601SCedric Chaumont uint32_t objectIDLen, uint32_t flags, 398b0104773SPascal Brand TEE_ObjectHandle *object) 399b0104773SPascal Brand { 400*9b520646SCedric Chaumont TEE_Result res; 401b0104773SPascal Brand 402*9b520646SCedric Chaumont if (storageID != TEE_STORAGE_PRIVATE) { 403*9b520646SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 404*9b520646SCedric Chaumont goto out; 405*9b520646SCedric Chaumont } 406b0104773SPascal Brand 407*9b520646SCedric Chaumont if (!objectID) { 408*9b520646SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 409*9b520646SCedric Chaumont goto out; 410*9b520646SCedric Chaumont } 411*9b520646SCedric Chaumont 412*9b520646SCedric Chaumont if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) { 413*9b520646SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 414*9b520646SCedric Chaumont goto out; 415*9b520646SCedric Chaumont } 416*9b520646SCedric Chaumont 417*9b520646SCedric Chaumont if (!object) { 418*9b520646SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 419*9b520646SCedric Chaumont goto out; 420*9b520646SCedric Chaumont } 421*9b520646SCedric Chaumont 422*9b520646SCedric Chaumont res = utee_storage_obj_open(storageID, objectID, objectIDLen, flags, 423*9b520646SCedric Chaumont object); 424*9b520646SCedric Chaumont 425*9b520646SCedric Chaumont out: 426*9b520646SCedric Chaumont if (res != TEE_SUCCESS && 427*9b520646SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 428*9b520646SCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT && 429*9b520646SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 430*9b520646SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 431*9b520646SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 432b0104773SPascal Brand TEE_Panic(0); 433b0104773SPascal Brand 434*9b520646SCedric Chaumont return res; 435b0104773SPascal Brand } 436b0104773SPascal Brand 437b0104773SPascal Brand TEE_Result TEE_CreatePersistentObject(uint32_t storageID, void *objectID, 43879a3c601SCedric Chaumont uint32_t objectIDLen, uint32_t flags, 439b0104773SPascal Brand TEE_ObjectHandle attributes, 440b0104773SPascal Brand const void *initialData, 44179a3c601SCedric Chaumont uint32_t initialDataLen, 442b0104773SPascal Brand TEE_ObjectHandle *object) 443b0104773SPascal Brand { 44484431ae3SCedric Chaumont TEE_Result res; 445b0104773SPascal Brand 44684431ae3SCedric Chaumont if (storageID != TEE_STORAGE_PRIVATE) { 44784431ae3SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 44884431ae3SCedric Chaumont goto err; 44984431ae3SCedric Chaumont } 450b0104773SPascal Brand 451aeb0d927SCedric Chaumont if (!objectID) { 45284431ae3SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 45384431ae3SCedric Chaumont goto err; 45484431ae3SCedric Chaumont } 455b0104773SPascal Brand 45684431ae3SCedric Chaumont if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) { 45784431ae3SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 45884431ae3SCedric Chaumont goto err; 45984431ae3SCedric Chaumont } 460b0104773SPascal Brand 461aeb0d927SCedric Chaumont if (!object) { 46284431ae3SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 46384431ae3SCedric Chaumont goto err; 46484431ae3SCedric Chaumont } 46584431ae3SCedric Chaumont 46684431ae3SCedric Chaumont res = utee_storage_obj_create(storageID, objectID, objectIDLen, flags, 467b0104773SPascal Brand attributes, initialData, initialDataLen, 468b0104773SPascal Brand object); 46984431ae3SCedric Chaumont if (res == TEE_SUCCESS) 47084431ae3SCedric Chaumont goto out; 47184431ae3SCedric Chaumont err: 47284431ae3SCedric Chaumont if (res == TEE_ERROR_ITEM_NOT_FOUND || 47384431ae3SCedric Chaumont res == TEE_ERROR_ACCESS_CONFLICT || 47484431ae3SCedric Chaumont res == TEE_ERROR_OUT_OF_MEMORY || 47584431ae3SCedric Chaumont res == TEE_ERROR_STORAGE_NO_SPACE || 47684431ae3SCedric Chaumont res == TEE_ERROR_CORRUPT_OBJECT || 47784431ae3SCedric Chaumont res == TEE_ERROR_STORAGE_NOT_AVAILABLE) 47884431ae3SCedric Chaumont return res; 47984431ae3SCedric Chaumont TEE_Panic(0); 48084431ae3SCedric Chaumont out: 48184431ae3SCedric Chaumont return TEE_SUCCESS; 482b0104773SPascal Brand } 483b0104773SPascal Brand 4847583c59eSCedric Chaumont /* 4857583c59eSCedric Chaumont * Use of this function is deprecated 4867583c59eSCedric Chaumont * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead 4877583c59eSCedric Chaumont * These functions will be removed at some future major revision of 4887583c59eSCedric Chaumont * this specification 4897583c59eSCedric Chaumont */ 490b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object) 491b0104773SPascal Brand { 492b0104773SPascal Brand TEE_Result res; 493b0104773SPascal Brand 494b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 495b0104773SPascal Brand return; 496b0104773SPascal Brand 4977583c59eSCedric Chaumont res = TEE_CloseAndDeletePersistentObject1(object); 498b0104773SPascal Brand 499b0104773SPascal Brand if (res != TEE_SUCCESS) 500b0104773SPascal Brand TEE_Panic(0); 501b0104773SPascal Brand } 502b0104773SPascal Brand 5037583c59eSCedric Chaumont TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object) 5047583c59eSCedric Chaumont { 5057583c59eSCedric Chaumont TEE_Result res; 5067583c59eSCedric Chaumont 5077583c59eSCedric Chaumont if (object == TEE_HANDLE_NULL) 5087583c59eSCedric Chaumont return TEE_ERROR_STORAGE_NOT_AVAILABLE; 5097583c59eSCedric Chaumont 5107583c59eSCedric Chaumont res = utee_storage_obj_del(object); 5117583c59eSCedric Chaumont 5127583c59eSCedric Chaumont if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 5137583c59eSCedric Chaumont TEE_Panic(0); 5147583c59eSCedric Chaumont 5157583c59eSCedric Chaumont return res; 5167583c59eSCedric Chaumont } 5177583c59eSCedric Chaumont 5187583c59eSCedric Chaumont 519b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object, 520b0104773SPascal Brand const void *newObjectID, 52179a3c601SCedric Chaumont uint32_t newObjectIDLen) 522b0104773SPascal Brand { 523b0104773SPascal Brand TEE_Result res; 524b0104773SPascal Brand 525b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 526b0104773SPascal Brand return TEE_ERROR_ITEM_NOT_FOUND; 527b0104773SPascal Brand 528b0104773SPascal Brand if (newObjectID == NULL) 529b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 530b0104773SPascal Brand 531b0104773SPascal Brand if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN) 532b0104773SPascal Brand TEE_Panic(0); 533b0104773SPascal Brand 534b0104773SPascal Brand res = utee_storage_obj_rename(object, newObjectID, newObjectIDLen); 535b0104773SPascal Brand 536b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_ACCESS_CONFLICT) 537b0104773SPascal Brand TEE_Panic(0); 538b0104773SPascal Brand 539b0104773SPascal Brand return res; 540b0104773SPascal Brand } 541b0104773SPascal Brand 542b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle * 543b0104773SPascal Brand objectEnumerator) 544b0104773SPascal Brand { 545b0104773SPascal Brand TEE_Result res; 546b0104773SPascal Brand 547b0104773SPascal Brand if (objectEnumerator == NULL) 548b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 549b0104773SPascal Brand 550b0104773SPascal Brand res = utee_storage_alloc_enum(objectEnumerator); 551b0104773SPascal Brand 552b0104773SPascal Brand if (res != TEE_SUCCESS) 553b0104773SPascal Brand *objectEnumerator = TEE_HANDLE_NULL; 554b0104773SPascal Brand 555b0104773SPascal Brand return res; 556b0104773SPascal Brand } 557b0104773SPascal Brand 558b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 559b0104773SPascal Brand { 560b0104773SPascal Brand TEE_Result res; 561b0104773SPascal Brand 562b0104773SPascal Brand if (objectEnumerator == TEE_HANDLE_NULL) 563b0104773SPascal Brand return; 564b0104773SPascal Brand 565b0104773SPascal Brand res = utee_storage_free_enum(objectEnumerator); 566b0104773SPascal Brand 567b0104773SPascal Brand if (res != TEE_SUCCESS) 568b0104773SPascal Brand TEE_Panic(0); 569b0104773SPascal Brand } 570b0104773SPascal Brand 571b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 572b0104773SPascal Brand { 573b0104773SPascal Brand TEE_Result res; 574b0104773SPascal Brand 575b0104773SPascal Brand if (objectEnumerator == TEE_HANDLE_NULL) 576b0104773SPascal Brand return; 577b0104773SPascal Brand 578b0104773SPascal Brand res = utee_storage_reset_enum(objectEnumerator); 579b0104773SPascal Brand 580b0104773SPascal Brand if (res != TEE_SUCCESS) 581b0104773SPascal Brand TEE_Panic(0); 582b0104773SPascal Brand } 583b0104773SPascal Brand 584b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle 585b0104773SPascal Brand objectEnumerator, 586b0104773SPascal Brand uint32_t storageID) 587b0104773SPascal Brand { 588b0104773SPascal Brand TEE_Result res; 589b0104773SPascal Brand 590b0104773SPascal Brand if (storageID != TEE_STORAGE_PRIVATE) 591b0104773SPascal Brand return TEE_ERROR_ITEM_NOT_FOUND; 592b0104773SPascal Brand 593b0104773SPascal Brand res = utee_storage_start_enum(objectEnumerator, storageID); 594b0104773SPascal Brand 595b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND) 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 607ae1289baSCedric Chaumont res = utee_storage_next_enum(objectEnumerator, objectInfo, objectID, 608b0104773SPascal Brand objectIDLen); 609b0104773SPascal Brand 610b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND) 611b0104773SPascal Brand TEE_Panic(0); 612b0104773SPascal Brand 613b0104773SPascal Brand return res; 614b0104773SPascal Brand } 615b0104773SPascal Brand 616b0104773SPascal Brand /* Data and Key Storage API - Data Stream Access Functions */ 617b0104773SPascal Brand 618b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer, 61979a3c601SCedric Chaumont uint32_t size, uint32_t *count) 620b0104773SPascal Brand { 621b0104773SPascal Brand TEE_Result res; 622b0104773SPascal Brand 623ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 624ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 625ae1289baSCedric Chaumont goto out; 626ae1289baSCedric Chaumont } 627b0104773SPascal Brand 628b0104773SPascal Brand res = utee_storage_obj_read(object, buffer, size, count); 629b0104773SPascal Brand 630ae1289baSCedric Chaumont out: 631ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 632ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 633ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 634b0104773SPascal Brand TEE_Panic(0); 635b0104773SPascal Brand 636b0104773SPascal Brand return res; 637b0104773SPascal Brand } 638b0104773SPascal Brand 639b0104773SPascal Brand TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, void *buffer, 64079a3c601SCedric Chaumont uint32_t size) 641b0104773SPascal Brand { 642b0104773SPascal Brand TEE_Result res; 643b0104773SPascal Brand 644ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 645ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 646ae1289baSCedric Chaumont goto out; 647ae1289baSCedric Chaumont } 648ae1289baSCedric Chaumont 649ae1289baSCedric Chaumont if (size > TEE_DATA_MAX_POSITION) { 650ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 651ae1289baSCedric Chaumont goto out; 652ae1289baSCedric Chaumont } 653b0104773SPascal Brand 654b0104773SPascal Brand res = utee_storage_obj_write(object, buffer, size); 655b0104773SPascal Brand 656ae1289baSCedric Chaumont out: 657ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 658ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NO_SPACE && 659ae1289baSCedric Chaumont res != TEE_ERROR_OVERFLOW && 660ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 661ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 662b0104773SPascal Brand TEE_Panic(0); 663b0104773SPascal Brand 664b0104773SPascal Brand return res; 665b0104773SPascal Brand } 666b0104773SPascal Brand 667b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size) 668b0104773SPascal Brand { 669b0104773SPascal Brand TEE_Result res; 670b0104773SPascal Brand 671ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 672ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 673ae1289baSCedric Chaumont goto out; 674ae1289baSCedric Chaumont } 675b0104773SPascal Brand 676b0104773SPascal Brand res = utee_storage_obj_trunc(object, size); 677b0104773SPascal Brand 678ae1289baSCedric Chaumont out: 679ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 680ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NO_SPACE && 681ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 682ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 683b0104773SPascal Brand TEE_Panic(0); 684b0104773SPascal Brand 685b0104773SPascal Brand return res; 686b0104773SPascal Brand } 687b0104773SPascal Brand 688b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset, 689b0104773SPascal Brand TEE_Whence whence) 690b0104773SPascal Brand { 691b0104773SPascal Brand TEE_Result res; 692b0104773SPascal Brand TEE_ObjectInfo info; 693b0104773SPascal Brand 694ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 695ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 696ae1289baSCedric Chaumont goto out; 697ae1289baSCedric Chaumont } 698b0104773SPascal Brand 699b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 700b0104773SPascal Brand if (res != TEE_SUCCESS) 701ae1289baSCedric Chaumont goto out; 702b0104773SPascal Brand 703b0104773SPascal Brand switch (whence) { 704b0104773SPascal Brand case TEE_DATA_SEEK_SET: 705ae1289baSCedric Chaumont if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) { 706ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 707ae1289baSCedric Chaumont goto out; 708ae1289baSCedric Chaumont } 709b0104773SPascal Brand break; 710b0104773SPascal Brand case TEE_DATA_SEEK_CUR: 711b0104773SPascal Brand if (offset > 0 && 712b0104773SPascal Brand ((uint32_t)offset + info.dataPosition > 713b0104773SPascal Brand TEE_DATA_MAX_POSITION || 714b0104773SPascal Brand (uint32_t)offset + info.dataPosition < 715ae1289baSCedric Chaumont info.dataPosition)) { 716ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 717ae1289baSCedric Chaumont goto out; 718ae1289baSCedric Chaumont } 719b0104773SPascal Brand break; 720b0104773SPascal Brand case TEE_DATA_SEEK_END: 721b0104773SPascal Brand if (offset > 0 && 722b0104773SPascal Brand ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION || 723ae1289baSCedric Chaumont (uint32_t)offset + info.dataSize < info.dataSize)) { 724ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 725ae1289baSCedric Chaumont goto out; 726ae1289baSCedric Chaumont } 727b0104773SPascal Brand break; 728b0104773SPascal Brand default: 729ae1289baSCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 730ae1289baSCedric Chaumont goto out; 731b0104773SPascal Brand } 732b0104773SPascal Brand 733b0104773SPascal Brand res = utee_storage_obj_seek(object, offset, whence); 734b0104773SPascal Brand 735ae1289baSCedric Chaumont out: 736ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 737ae1289baSCedric Chaumont res != TEE_ERROR_OVERFLOW && 738ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 739ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 740b0104773SPascal Brand TEE_Panic(0); 741b0104773SPascal Brand 742b0104773SPascal Brand return res; 743b0104773SPascal Brand } 744