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 { 4009b520646SCedric Chaumont TEE_Result res; 401b0104773SPascal Brand 4029b520646SCedric Chaumont if (storageID != TEE_STORAGE_PRIVATE) { 4039b520646SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 4049b520646SCedric Chaumont goto out; 4059b520646SCedric Chaumont } 406b0104773SPascal Brand 4079b520646SCedric Chaumont if (!objectID) { 4089b520646SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 4099b520646SCedric Chaumont goto out; 4109b520646SCedric Chaumont } 4119b520646SCedric Chaumont 4129b520646SCedric Chaumont if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) { 4139b520646SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 4149b520646SCedric Chaumont goto out; 4159b520646SCedric Chaumont } 4169b520646SCedric Chaumont 4179b520646SCedric Chaumont if (!object) { 4189b520646SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 4199b520646SCedric Chaumont goto out; 4209b520646SCedric Chaumont } 4219b520646SCedric Chaumont 4229b520646SCedric Chaumont res = utee_storage_obj_open(storageID, objectID, objectIDLen, flags, 4239b520646SCedric Chaumont object); 4249b520646SCedric Chaumont 4259b520646SCedric Chaumont out: 4269b520646SCedric Chaumont if (res != TEE_SUCCESS && 4279b520646SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 4289b520646SCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT && 4299b520646SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 4309b520646SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 4319b520646SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 432b0104773SPascal Brand TEE_Panic(0); 433b0104773SPascal Brand 4349b520646SCedric 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 525a76bf53fSCedric Chaumont if (object == TEE_HANDLE_NULL) { 526a76bf53fSCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 527a76bf53fSCedric Chaumont goto out; 528a76bf53fSCedric Chaumont } 529b0104773SPascal Brand 530a76bf53fSCedric Chaumont if (!newObjectID) { 531a76bf53fSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 532a76bf53fSCedric Chaumont goto out; 533a76bf53fSCedric Chaumont } 534b0104773SPascal Brand 535a76bf53fSCedric Chaumont if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN) { 536a76bf53fSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 537a76bf53fSCedric Chaumont goto out; 538a76bf53fSCedric Chaumont } 539b0104773SPascal Brand 540b0104773SPascal Brand res = utee_storage_obj_rename(object, newObjectID, newObjectIDLen); 541b0104773SPascal Brand 542a76bf53fSCedric Chaumont out: 543a76bf53fSCedric Chaumont if (res != TEE_SUCCESS && 544a76bf53fSCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT && 545a76bf53fSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 546a76bf53fSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 547b0104773SPascal Brand TEE_Panic(0); 548b0104773SPascal Brand 549b0104773SPascal Brand return res; 550b0104773SPascal Brand } 551b0104773SPascal Brand 552b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle * 553b0104773SPascal Brand objectEnumerator) 554b0104773SPascal Brand { 555b0104773SPascal Brand TEE_Result res; 556b0104773SPascal Brand 557*15cd3c30SCedric Chaumont if (!objectEnumerator) 558b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 559b0104773SPascal Brand 560b0104773SPascal Brand res = utee_storage_alloc_enum(objectEnumerator); 561b0104773SPascal Brand 562b0104773SPascal Brand if (res != TEE_SUCCESS) 563b0104773SPascal Brand *objectEnumerator = TEE_HANDLE_NULL; 564b0104773SPascal Brand 565*15cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 566*15cd3c30SCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT) 567*15cd3c30SCedric Chaumont TEE_Panic(0); 568*15cd3c30SCedric Chaumont 569b0104773SPascal Brand return res; 570b0104773SPascal Brand } 571b0104773SPascal Brand 572b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 573b0104773SPascal Brand { 574b0104773SPascal Brand TEE_Result res; 575b0104773SPascal Brand 576b0104773SPascal Brand if (objectEnumerator == TEE_HANDLE_NULL) 577b0104773SPascal Brand return; 578b0104773SPascal Brand 579b0104773SPascal Brand res = utee_storage_free_enum(objectEnumerator); 580b0104773SPascal Brand 581b0104773SPascal Brand if (res != TEE_SUCCESS) 582b0104773SPascal Brand TEE_Panic(0); 583b0104773SPascal Brand } 584b0104773SPascal Brand 585b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 586b0104773SPascal Brand { 587b0104773SPascal Brand TEE_Result res; 588b0104773SPascal Brand 589b0104773SPascal Brand if (objectEnumerator == TEE_HANDLE_NULL) 590b0104773SPascal Brand return; 591b0104773SPascal Brand 592b0104773SPascal Brand res = utee_storage_reset_enum(objectEnumerator); 593b0104773SPascal Brand 594b0104773SPascal Brand if (res != TEE_SUCCESS) 595b0104773SPascal Brand TEE_Panic(0); 596b0104773SPascal Brand } 597b0104773SPascal Brand 598b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle 599b0104773SPascal Brand objectEnumerator, 600b0104773SPascal Brand uint32_t storageID) 601b0104773SPascal Brand { 602b0104773SPascal Brand TEE_Result res; 603b0104773SPascal Brand 604b0104773SPascal Brand if (storageID != TEE_STORAGE_PRIVATE) 605b0104773SPascal Brand return TEE_ERROR_ITEM_NOT_FOUND; 606b0104773SPascal Brand 607b0104773SPascal Brand res = utee_storage_start_enum(objectEnumerator, storageID); 608b0104773SPascal Brand 609*15cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 610*15cd3c30SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 611*15cd3c30SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 612*15cd3c30SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 613b0104773SPascal Brand TEE_Panic(0); 614b0104773SPascal Brand 615b0104773SPascal Brand return res; 616b0104773SPascal Brand } 617b0104773SPascal Brand 618b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator, 619b0104773SPascal Brand TEE_ObjectInfo *objectInfo, 62079a3c601SCedric Chaumont void *objectID, uint32_t *objectIDLen) 621b0104773SPascal Brand { 622b0104773SPascal Brand TEE_Result res; 623b0104773SPascal Brand 624*15cd3c30SCedric Chaumont if (!objectID) { 625*15cd3c30SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 626*15cd3c30SCedric Chaumont goto out; 627*15cd3c30SCedric Chaumont } 628*15cd3c30SCedric Chaumont 629*15cd3c30SCedric Chaumont if (!objectIDLen) { 630*15cd3c30SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 631*15cd3c30SCedric Chaumont goto out; 632*15cd3c30SCedric Chaumont } 633*15cd3c30SCedric Chaumont 634ae1289baSCedric Chaumont res = utee_storage_next_enum(objectEnumerator, objectInfo, objectID, 635b0104773SPascal Brand objectIDLen); 636b0104773SPascal Brand 637*15cd3c30SCedric Chaumont out: 638*15cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 639*15cd3c30SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 640*15cd3c30SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 641*15cd3c30SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 642b0104773SPascal Brand TEE_Panic(0); 643b0104773SPascal Brand 644b0104773SPascal Brand return res; 645b0104773SPascal Brand } 646b0104773SPascal Brand 647b0104773SPascal Brand /* Data and Key Storage API - Data Stream Access Functions */ 648b0104773SPascal Brand 649b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer, 65079a3c601SCedric Chaumont uint32_t size, uint32_t *count) 651b0104773SPascal Brand { 652b0104773SPascal Brand TEE_Result res; 653b0104773SPascal Brand 654ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 655ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 656ae1289baSCedric Chaumont goto out; 657ae1289baSCedric Chaumont } 658b0104773SPascal Brand 659b0104773SPascal Brand res = utee_storage_obj_read(object, buffer, size, count); 660b0104773SPascal Brand 661ae1289baSCedric Chaumont out: 662ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 663ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 664ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 665b0104773SPascal Brand TEE_Panic(0); 666b0104773SPascal Brand 667b0104773SPascal Brand return res; 668b0104773SPascal Brand } 669b0104773SPascal Brand 670b0104773SPascal Brand TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, void *buffer, 67179a3c601SCedric Chaumont uint32_t size) 672b0104773SPascal Brand { 673b0104773SPascal Brand TEE_Result res; 674b0104773SPascal Brand 675ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 676ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 677ae1289baSCedric Chaumont goto out; 678ae1289baSCedric Chaumont } 679ae1289baSCedric Chaumont 680ae1289baSCedric Chaumont if (size > TEE_DATA_MAX_POSITION) { 681ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 682ae1289baSCedric Chaumont goto out; 683ae1289baSCedric Chaumont } 684b0104773SPascal Brand 685b0104773SPascal Brand res = utee_storage_obj_write(object, buffer, size); 686b0104773SPascal Brand 687ae1289baSCedric Chaumont out: 688ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 689ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NO_SPACE && 690ae1289baSCedric Chaumont res != TEE_ERROR_OVERFLOW && 691ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 692ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 693b0104773SPascal Brand TEE_Panic(0); 694b0104773SPascal Brand 695b0104773SPascal Brand return res; 696b0104773SPascal Brand } 697b0104773SPascal Brand 698b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size) 699b0104773SPascal Brand { 700b0104773SPascal Brand TEE_Result res; 701b0104773SPascal Brand 702ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 703ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 704ae1289baSCedric Chaumont goto out; 705ae1289baSCedric Chaumont } 706b0104773SPascal Brand 707b0104773SPascal Brand res = utee_storage_obj_trunc(object, size); 708b0104773SPascal Brand 709ae1289baSCedric Chaumont out: 710ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 711ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NO_SPACE && 712ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 713ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 714b0104773SPascal Brand TEE_Panic(0); 715b0104773SPascal Brand 716b0104773SPascal Brand return res; 717b0104773SPascal Brand } 718b0104773SPascal Brand 719b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset, 720b0104773SPascal Brand TEE_Whence whence) 721b0104773SPascal Brand { 722b0104773SPascal Brand TEE_Result res; 723b0104773SPascal Brand TEE_ObjectInfo info; 724b0104773SPascal Brand 725ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 726ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 727ae1289baSCedric Chaumont goto out; 728ae1289baSCedric Chaumont } 729b0104773SPascal Brand 730b0104773SPascal Brand res = utee_cryp_obj_get_info((uint32_t)object, &info); 731b0104773SPascal Brand if (res != TEE_SUCCESS) 732ae1289baSCedric Chaumont goto out; 733b0104773SPascal Brand 734b0104773SPascal Brand switch (whence) { 735b0104773SPascal Brand case TEE_DATA_SEEK_SET: 736ae1289baSCedric Chaumont if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) { 737ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 738ae1289baSCedric Chaumont goto out; 739ae1289baSCedric Chaumont } 740b0104773SPascal Brand break; 741b0104773SPascal Brand case TEE_DATA_SEEK_CUR: 742b0104773SPascal Brand if (offset > 0 && 743b0104773SPascal Brand ((uint32_t)offset + info.dataPosition > 744b0104773SPascal Brand TEE_DATA_MAX_POSITION || 745b0104773SPascal Brand (uint32_t)offset + info.dataPosition < 746ae1289baSCedric Chaumont info.dataPosition)) { 747ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 748ae1289baSCedric Chaumont goto out; 749ae1289baSCedric Chaumont } 750b0104773SPascal Brand break; 751b0104773SPascal Brand case TEE_DATA_SEEK_END: 752b0104773SPascal Brand if (offset > 0 && 753b0104773SPascal Brand ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION || 754ae1289baSCedric Chaumont (uint32_t)offset + info.dataSize < info.dataSize)) { 755ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 756ae1289baSCedric Chaumont goto out; 757ae1289baSCedric Chaumont } 758b0104773SPascal Brand break; 759b0104773SPascal Brand default: 760ae1289baSCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 761ae1289baSCedric Chaumont goto out; 762b0104773SPascal Brand } 763b0104773SPascal Brand 764b0104773SPascal Brand res = utee_storage_obj_seek(object, offset, whence); 765b0104773SPascal Brand 766ae1289baSCedric Chaumont out: 767ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 768ae1289baSCedric Chaumont res != TEE_ERROR_OVERFLOW && 769ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 770ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 771b0104773SPascal Brand TEE_Panic(0); 772b0104773SPascal Brand 773b0104773SPascal Brand return res; 774b0104773SPascal Brand } 775