11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause 2b0104773SPascal Brand /* 3b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 4b0104773SPascal Brand */ 5b0104773SPascal Brand #include <stdlib.h> 6b0104773SPascal Brand #include <string.h> 7b0104773SPascal Brand 8b0104773SPascal Brand #include <tee_api.h> 9b0104773SPascal Brand #include <utee_syscalls.h> 10e86f1266SJens Wiklander #include "tee_api_private.h" 11b0104773SPascal Brand 12b0104773SPascal Brand #define TEE_USAGE_DEFAULT 0xffffffff 13b0104773SPascal Brand 14b0104773SPascal Brand #define TEE_ATTR_BIT_VALUE (1 << 29) 15b0104773SPascal Brand #define TEE_ATTR_BIT_PROTECTED (1 << 28) 16b0104773SPascal Brand 17e86f1266SJens Wiklander void __utee_from_attr(struct utee_attribute *ua, const TEE_Attribute *attrs, 18e86f1266SJens Wiklander uint32_t attr_count) 19e86f1266SJens Wiklander { 20e86f1266SJens Wiklander size_t n; 21e86f1266SJens Wiklander 22e86f1266SJens Wiklander for (n = 0; n < attr_count; n++) { 23e86f1266SJens Wiklander ua[n].attribute_id = attrs[n].attributeID; 24e86f1266SJens Wiklander if (attrs[n].attributeID & TEE_ATTR_BIT_VALUE) { 25e86f1266SJens Wiklander ua[n].a = attrs[n].content.value.a; 26e86f1266SJens Wiklander ua[n].b = attrs[n].content.value.b; 27e86f1266SJens Wiklander } else { 28e86f1266SJens Wiklander ua[n].a = (uintptr_t)attrs[n].content.ref.buffer; 29e86f1266SJens Wiklander ua[n].b = attrs[n].content.ref.length; 30e86f1266SJens Wiklander } 31e86f1266SJens Wiklander } 32e86f1266SJens Wiklander } 33e86f1266SJens Wiklander 34b0104773SPascal Brand /* Data and Key Storage API - Generic Object Functions */ 357583c59eSCedric Chaumont /* 367583c59eSCedric Chaumont * Use of this function is deprecated 377583c59eSCedric Chaumont * new code SHOULD use the TEE_GetObjectInfo1 function instead 387583c59eSCedric Chaumont * These functions will be removed at some future major revision of 397583c59eSCedric Chaumont * this specification 407583c59eSCedric Chaumont */ 41b0104773SPascal Brand void TEE_GetObjectInfo(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo) 42b0104773SPascal Brand { 43b0104773SPascal Brand TEE_Result res; 44b0104773SPascal Brand 45*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, objectInfo); 467583c59eSCedric Chaumont 47b0104773SPascal Brand if (res != TEE_SUCCESS) 48b0104773SPascal Brand TEE_Panic(res); 497583c59eSCedric Chaumont 507583c59eSCedric Chaumont if (objectInfo->objectType == TEE_TYPE_CORRUPTED_OBJECT) { 517583c59eSCedric Chaumont objectInfo->keySize = 0; 527583c59eSCedric Chaumont objectInfo->maxKeySize = 0; 537583c59eSCedric Chaumont objectInfo->objectUsage = 0; 547583c59eSCedric Chaumont objectInfo->dataSize = 0; 557583c59eSCedric Chaumont objectInfo->dataPosition = 0; 567583c59eSCedric Chaumont objectInfo->handleFlags = 0; 577583c59eSCedric Chaumont } 58b0104773SPascal Brand } 59b0104773SPascal Brand 607583c59eSCedric Chaumont TEE_Result TEE_GetObjectInfo1(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo) 617583c59eSCedric Chaumont { 627583c59eSCedric Chaumont TEE_Result res; 637583c59eSCedric Chaumont 64*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, objectInfo); 657583c59eSCedric Chaumont 66a2e9a830SCedric Chaumont if (res != TEE_SUCCESS && 67a2e9a830SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 68a2e9a830SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 697583c59eSCedric Chaumont TEE_Panic(res); 707583c59eSCedric Chaumont 717583c59eSCedric Chaumont return res; 727583c59eSCedric Chaumont } 737583c59eSCedric Chaumont 747583c59eSCedric Chaumont /* 757583c59eSCedric Chaumont * Use of this function is deprecated 767583c59eSCedric Chaumont * new code SHOULD use the TEE_RestrictObjectUsage1 function instead 777583c59eSCedric Chaumont * These functions will be removed at some future major revision of 787583c59eSCedric Chaumont * this specification 797583c59eSCedric Chaumont */ 80b0104773SPascal Brand void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage) 81b0104773SPascal Brand { 82b0104773SPascal Brand TEE_Result res; 837583c59eSCedric Chaumont TEE_ObjectInfo objectInfo; 847583c59eSCedric Chaumont 85*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &objectInfo); 867583c59eSCedric Chaumont if (objectInfo.objectType == TEE_TYPE_CORRUPTED_OBJECT) 877583c59eSCedric Chaumont return; 887583c59eSCedric Chaumont 897583c59eSCedric Chaumont res = TEE_RestrictObjectUsage1(object, objectUsage); 90b0104773SPascal Brand 91b0104773SPascal Brand if (res != TEE_SUCCESS) 92b36311adSJerome Forissier TEE_Panic(res); 93b0104773SPascal Brand } 94b0104773SPascal Brand 957583c59eSCedric Chaumont TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage) 967583c59eSCedric Chaumont { 977583c59eSCedric Chaumont TEE_Result res; 987583c59eSCedric Chaumont 99*2c028fdeSJerome Forissier res = _utee_cryp_obj_restrict_usage((unsigned long)object, 100*2c028fdeSJerome Forissier objectUsage); 1017583c59eSCedric Chaumont 102a2e9a830SCedric Chaumont if (res != TEE_SUCCESS && 103a2e9a830SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 104a2e9a830SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 105a2e9a830SCedric Chaumont TEE_Panic(res); 1067583c59eSCedric Chaumont 1077583c59eSCedric Chaumont return res; 1087583c59eSCedric Chaumont } 1097583c59eSCedric Chaumont 110b0104773SPascal Brand TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object, 111b0104773SPascal Brand uint32_t attributeID, void *buffer, 11279a3c601SCedric Chaumont uint32_t *size) 113b0104773SPascal Brand { 114b0104773SPascal Brand TEE_Result res; 115b0104773SPascal Brand TEE_ObjectInfo info; 116e86f1266SJens Wiklander uint64_t sz; 117b0104773SPascal Brand 118*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 119b0104773SPascal Brand if (res != TEE_SUCCESS) 120a2e9a830SCedric Chaumont goto exit; 121b0104773SPascal Brand 122b0104773SPascal Brand /* This function only supports reference attributes */ 123a2e9a830SCedric Chaumont if ((attributeID & TEE_ATTR_BIT_VALUE)) { 124a2e9a830SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 125a2e9a830SCedric Chaumont goto exit; 126a2e9a830SCedric Chaumont } 127b0104773SPascal Brand 128e86f1266SJens Wiklander sz = *size; 129*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID, 130e86f1266SJens Wiklander buffer, &sz); 131e86f1266SJens Wiklander *size = sz; 132b0104773SPascal Brand 133a2e9a830SCedric Chaumont exit: 1340ed6a6caSCedric Chaumont if (res != TEE_SUCCESS && 1350ed6a6caSCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 1360ed6a6caSCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1370ed6a6caSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 1380ed6a6caSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 139b36311adSJerome Forissier TEE_Panic(res); 140b0104773SPascal Brand 141b0104773SPascal Brand return res; 142b0104773SPascal Brand } 143b0104773SPascal Brand 144b0104773SPascal Brand TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object, 145b0104773SPascal Brand uint32_t attributeID, uint32_t *a, 146b0104773SPascal Brand uint32_t *b) 147b0104773SPascal Brand { 148b0104773SPascal Brand TEE_Result res; 149b0104773SPascal Brand TEE_ObjectInfo info; 150b0104773SPascal Brand uint32_t buf[2]; 151e86f1266SJens Wiklander uint64_t size = sizeof(buf); 152b0104773SPascal Brand 153*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 154b0104773SPascal Brand if (res != TEE_SUCCESS) 155a2e9a830SCedric Chaumont goto exit; 156b0104773SPascal Brand 157b0104773SPascal Brand /* This function only supports value attributes */ 158a2e9a830SCedric Chaumont if (!(attributeID & TEE_ATTR_BIT_VALUE)) { 159a2e9a830SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 160a2e9a830SCedric Chaumont goto exit; 161a2e9a830SCedric Chaumont } 162b0104773SPascal Brand 163*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID, buf, 164e86f1266SJens Wiklander &size); 165b0104773SPascal Brand 166a2e9a830SCedric Chaumont exit: 1670ed6a6caSCedric Chaumont if (res != TEE_SUCCESS && 1680ed6a6caSCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 1690ed6a6caSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 1700ed6a6caSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 171b36311adSJerome Forissier TEE_Panic(res); 172b0104773SPascal Brand 173b0104773SPascal Brand if (size != sizeof(buf)) 174b0104773SPascal Brand TEE_Panic(0); 175b0104773SPascal Brand 176be96c837SJerome Forissier if (res == TEE_SUCCESS) { 177b9d8f134SJerome Forissier if (a) 178b0104773SPascal Brand *a = buf[0]; 179b9d8f134SJerome Forissier if (b) 180b0104773SPascal Brand *b = buf[1]; 181be96c837SJerome Forissier } 182b0104773SPascal Brand 183b0104773SPascal Brand return res; 184b0104773SPascal Brand } 185b0104773SPascal Brand 186b0104773SPascal Brand void TEE_CloseObject(TEE_ObjectHandle object) 187b0104773SPascal Brand { 188b0104773SPascal Brand TEE_Result res; 189b0104773SPascal Brand 190b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 191b0104773SPascal Brand return; 192b0104773SPascal Brand 193*2c028fdeSJerome Forissier res = _utee_cryp_obj_close((unsigned long)object); 194b0104773SPascal Brand if (res != TEE_SUCCESS) 195b36311adSJerome Forissier TEE_Panic(res); 196b0104773SPascal Brand } 197b0104773SPascal Brand 198b0104773SPascal Brand /* Data and Key Storage API - Transient Object Functions */ 199b0104773SPascal Brand 200b0104773SPascal Brand TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType, 20179a3c601SCedric Chaumont uint32_t maxKeySize, 202b0104773SPascal Brand TEE_ObjectHandle *object) 203b0104773SPascal Brand { 204b0104773SPascal Brand TEE_Result res; 205b0104773SPascal Brand uint32_t obj; 206b0104773SPascal Brand 207*2c028fdeSJerome Forissier res = _utee_cryp_obj_alloc(objectType, maxKeySize, &obj); 208aeb0d927SCedric Chaumont 209aeb0d927SCedric Chaumont if (res != TEE_SUCCESS && 210aeb0d927SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 211aeb0d927SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 212b36311adSJerome Forissier TEE_Panic(res); 213aeb0d927SCedric Chaumont 214b0104773SPascal Brand if (res == TEE_SUCCESS) 215e86f1266SJens Wiklander *object = (TEE_ObjectHandle)(uintptr_t)obj; 2160ed6a6caSCedric Chaumont 217b0104773SPascal Brand return res; 218b0104773SPascal Brand } 219b0104773SPascal Brand 220b0104773SPascal Brand void TEE_FreeTransientObject(TEE_ObjectHandle object) 221b0104773SPascal Brand { 222b0104773SPascal Brand TEE_Result res; 223b0104773SPascal Brand TEE_ObjectInfo info; 224b0104773SPascal Brand 225b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 226b0104773SPascal Brand return; 227b0104773SPascal Brand 228*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 229b0104773SPascal Brand if (res != TEE_SUCCESS) 230b36311adSJerome Forissier TEE_Panic(res); 231b0104773SPascal Brand 232b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 233b0104773SPascal Brand TEE_Panic(0); 234b0104773SPascal Brand 235*2c028fdeSJerome Forissier res = _utee_cryp_obj_close((unsigned long)object); 236b0104773SPascal Brand if (res != TEE_SUCCESS) 237b36311adSJerome Forissier TEE_Panic(res); 238b0104773SPascal Brand } 239b0104773SPascal Brand 240b0104773SPascal Brand void TEE_ResetTransientObject(TEE_ObjectHandle object) 241b0104773SPascal Brand { 242b0104773SPascal Brand TEE_Result res; 243b0104773SPascal Brand TEE_ObjectInfo info; 244b0104773SPascal Brand 245b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 246b0104773SPascal Brand return; 247b0104773SPascal Brand 248*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 249b0104773SPascal Brand if (res != TEE_SUCCESS) 250b36311adSJerome Forissier TEE_Panic(res); 251b0104773SPascal Brand 252b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 253b0104773SPascal Brand TEE_Panic(0); 254b0104773SPascal Brand 255*2c028fdeSJerome Forissier res = _utee_cryp_obj_reset((unsigned long)object); 256b0104773SPascal Brand if (res != TEE_SUCCESS) 257b36311adSJerome Forissier TEE_Panic(res); 258b0104773SPascal Brand } 259b0104773SPascal Brand 260b0104773SPascal Brand TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object, 2618f07fe6fSJerome Forissier const TEE_Attribute *attrs, 262b0104773SPascal Brand uint32_t attrCount) 263b0104773SPascal Brand { 264b0104773SPascal Brand TEE_Result res; 265b0104773SPascal Brand TEE_ObjectInfo info; 266e86f1266SJens Wiklander struct utee_attribute ua[attrCount]; 267b0104773SPascal Brand 268*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 269b0104773SPascal Brand if (res != TEE_SUCCESS) 270b36311adSJerome Forissier TEE_Panic(res); 271b0104773SPascal Brand 272b0104773SPascal Brand /* Must be a transient object */ 273b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 274b0104773SPascal Brand TEE_Panic(0); 275b0104773SPascal Brand 276b0104773SPascal Brand /* Must not be initialized already */ 277b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 278b0104773SPascal Brand TEE_Panic(0); 279b0104773SPascal Brand 280e86f1266SJens Wiklander __utee_from_attr(ua, attrs, attrCount); 281*2c028fdeSJerome Forissier res = _utee_cryp_obj_populate((unsigned long)object, ua, attrCount); 282b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 283b0104773SPascal Brand TEE_Panic(res); 284b0104773SPascal Brand return res; 285b0104773SPascal Brand } 286b0104773SPascal Brand 287b0104773SPascal Brand void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID, 2888f07fe6fSJerome Forissier const void *buffer, uint32_t length) 289b0104773SPascal Brand { 290b0104773SPascal Brand if (attr == NULL) 291b0104773SPascal Brand TEE_Panic(0); 292b0104773SPascal Brand if ((attributeID & TEE_ATTR_BIT_VALUE) != 0) 293b0104773SPascal Brand TEE_Panic(0); 294b0104773SPascal Brand attr->attributeID = attributeID; 2958f07fe6fSJerome Forissier attr->content.ref.buffer = (void *)buffer; 296b0104773SPascal Brand attr->content.ref.length = length; 297b0104773SPascal Brand } 298b0104773SPascal Brand 299b0104773SPascal Brand void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID, 300b0104773SPascal Brand uint32_t a, uint32_t b) 301b0104773SPascal Brand { 302b0104773SPascal Brand if (attr == NULL) 303b0104773SPascal Brand TEE_Panic(0); 304b0104773SPascal Brand if ((attributeID & TEE_ATTR_BIT_VALUE) == 0) 305b0104773SPascal Brand TEE_Panic(0); 306b0104773SPascal Brand attr->attributeID = attributeID; 307b0104773SPascal Brand attr->content.value.a = a; 308b0104773SPascal Brand attr->content.value.b = b; 309b0104773SPascal Brand } 310b0104773SPascal Brand 3117583c59eSCedric Chaumont /* 3127583c59eSCedric Chaumont * Use of this function is deprecated 3137583c59eSCedric Chaumont * new code SHOULD use the TEE_CopyObjectAttributes1 function instead 3147583c59eSCedric Chaumont * These functions will be removed at some future major revision of 3157583c59eSCedric Chaumont * this specification 3167583c59eSCedric Chaumont */ 317b0104773SPascal Brand void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject, 318b0104773SPascal Brand TEE_ObjectHandle srcObject) 319b0104773SPascal Brand { 320b0104773SPascal Brand TEE_Result res; 3217583c59eSCedric Chaumont TEE_ObjectInfo src_info; 3227583c59eSCedric Chaumont 323*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info); 3247583c59eSCedric Chaumont if (src_info.objectType == TEE_TYPE_CORRUPTED_OBJECT) 3257583c59eSCedric Chaumont return; 3267583c59eSCedric Chaumont 3277583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(destObject, srcObject); 3287583c59eSCedric Chaumont if (res != TEE_SUCCESS) 329b36311adSJerome Forissier TEE_Panic(res); 3307583c59eSCedric Chaumont } 3317583c59eSCedric Chaumont 3327583c59eSCedric Chaumont TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject, 3337583c59eSCedric Chaumont TEE_ObjectHandle srcObject) 3347583c59eSCedric Chaumont { 3357583c59eSCedric Chaumont TEE_Result res; 336b0104773SPascal Brand TEE_ObjectInfo dst_info; 337b0104773SPascal Brand TEE_ObjectInfo src_info; 338b0104773SPascal Brand 339*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)destObject, &dst_info); 340b0104773SPascal Brand if (res != TEE_SUCCESS) 341a2e9a830SCedric Chaumont goto exit; 342b0104773SPascal Brand 343*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info); 344b0104773SPascal Brand if (res != TEE_SUCCESS) 345a2e9a830SCedric Chaumont goto exit; 346b0104773SPascal Brand 347a2e9a830SCedric Chaumont if (!(src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 348b0104773SPascal Brand TEE_Panic(0); 349a2e9a830SCedric Chaumont 350a2e9a830SCedric Chaumont if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) 351b0104773SPascal Brand TEE_Panic(0); 352a2e9a830SCedric Chaumont 353a2e9a830SCedric Chaumont if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 354b0104773SPascal Brand TEE_Panic(0); 355b0104773SPascal Brand 356*2c028fdeSJerome Forissier res = _utee_cryp_obj_copy((unsigned long)destObject, 357e86f1266SJens Wiklander (unsigned long)srcObject); 3587583c59eSCedric Chaumont 359a2e9a830SCedric Chaumont exit: 360a2e9a830SCedric Chaumont if (res != TEE_SUCCESS && 361a2e9a830SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 362a2e9a830SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 363a2e9a830SCedric Chaumont TEE_Panic(res); 3647583c59eSCedric Chaumont 3657583c59eSCedric Chaumont return res; 366b0104773SPascal Brand } 367b0104773SPascal Brand 368b0104773SPascal Brand TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize, 3698f07fe6fSJerome Forissier const TEE_Attribute *params, uint32_t paramCount) 370b0104773SPascal Brand { 371b0104773SPascal Brand TEE_Result res; 372e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 373b0104773SPascal Brand 374e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 375*2c028fdeSJerome Forissier res = _utee_cryp_obj_generate_key((unsigned long)object, keySize, 376e86f1266SJens Wiklander ua, paramCount); 377b0104773SPascal Brand 378aeb0d927SCedric Chaumont if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 379b36311adSJerome Forissier TEE_Panic(res); 380b0104773SPascal Brand 381b0104773SPascal Brand return res; 382b0104773SPascal Brand } 383b0104773SPascal Brand 384b0104773SPascal Brand /* Data and Key Storage API - Persistent Object Functions */ 385b0104773SPascal Brand 3868f07fe6fSJerome Forissier TEE_Result TEE_OpenPersistentObject(uint32_t storageID, const void *objectID, 38779a3c601SCedric Chaumont uint32_t objectIDLen, uint32_t flags, 388b0104773SPascal Brand TEE_ObjectHandle *object) 389b0104773SPascal Brand { 3909b520646SCedric Chaumont TEE_Result res; 391e86f1266SJens Wiklander uint32_t obj; 392b0104773SPascal Brand 3939b520646SCedric Chaumont if (!objectID) { 3949b520646SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 395172d637bSCedric Auger goto exit; 3969b520646SCedric Chaumont } 3979b520646SCedric Chaumont 3989b520646SCedric Chaumont if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) { 3999b520646SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 400172d637bSCedric Auger goto exit; 4019b520646SCedric Chaumont } 4029b520646SCedric Chaumont 403*2c028fdeSJerome Forissier res = _utee_storage_obj_open(storageID, objectID, objectIDLen, flags, 404e86f1266SJens Wiklander &obj); 405e86f1266SJens Wiklander if (res == TEE_SUCCESS) 406e86f1266SJens Wiklander *object = (TEE_ObjectHandle)(uintptr_t)obj; 4079b520646SCedric Chaumont 408172d637bSCedric Auger exit: 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) 415b36311adSJerome Forissier TEE_Panic(res); 416b0104773SPascal Brand 417172d637bSCedric Auger if (res != TEE_SUCCESS) 41869a3d6beSDaniel Glöckner *object = TEE_HANDLE_NULL; 41969a3d6beSDaniel Glöckner 4209b520646SCedric Chaumont return res; 421b0104773SPascal Brand } 422b0104773SPascal Brand 4238f07fe6fSJerome Forissier TEE_Result TEE_CreatePersistentObject(uint32_t storageID, const void *objectID, 42479a3c601SCedric Chaumont uint32_t objectIDLen, uint32_t flags, 425b0104773SPascal Brand TEE_ObjectHandle attributes, 426b0104773SPascal Brand const void *initialData, 42779a3c601SCedric Chaumont uint32_t initialDataLen, 428b0104773SPascal Brand TEE_ObjectHandle *object) 429b0104773SPascal Brand { 43084431ae3SCedric Chaumont TEE_Result res; 431e86f1266SJens Wiklander uint32_t obj; 432b0104773SPascal Brand 433aeb0d927SCedric Chaumont if (!objectID) { 43484431ae3SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 435172d637bSCedric Auger goto exit; 43684431ae3SCedric Chaumont } 437b0104773SPascal Brand 43884431ae3SCedric Chaumont if (objectIDLen > TEE_OBJECT_ID_MAX_LEN) { 43984431ae3SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 440172d637bSCedric Auger goto exit; 44184431ae3SCedric Chaumont } 442b0104773SPascal Brand 443*2c028fdeSJerome Forissier res = _utee_storage_obj_create(storageID, objectID, objectIDLen, flags, 444e86f1266SJens Wiklander (unsigned long)attributes, initialData, 445e86f1266SJens Wiklander initialDataLen, &obj); 446172d637bSCedric Auger 4471c96fa7fSPascal Brand if (res == TEE_SUCCESS) 448172d637bSCedric Auger *object = (TEE_ObjectHandle)(uintptr_t)obj; 449172d637bSCedric Auger 450172d637bSCedric Auger exit: 451172d637bSCedric Auger if (res != TEE_SUCCESS && 452172d637bSCedric Auger res != TEE_ERROR_ITEM_NOT_FOUND && 453172d637bSCedric Auger res != TEE_ERROR_ACCESS_CONFLICT && 454172d637bSCedric Auger res != TEE_ERROR_OUT_OF_MEMORY && 455172d637bSCedric Auger res != TEE_ERROR_STORAGE_NO_SPACE && 456172d637bSCedric Auger res != TEE_ERROR_CORRUPT_OBJECT && 457172d637bSCedric Auger res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 458b36311adSJerome Forissier TEE_Panic(res); 459172d637bSCedric Auger 460172d637bSCedric Auger if (res != TEE_SUCCESS) 461172d637bSCedric Auger *object = TEE_HANDLE_NULL; 462172d637bSCedric Auger 463172d637bSCedric Auger return res; 464b0104773SPascal Brand } 465b0104773SPascal Brand 4667583c59eSCedric Chaumont /* 4677583c59eSCedric Chaumont * Use of this function is deprecated 4687583c59eSCedric Chaumont * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead 4697583c59eSCedric Chaumont * These functions will be removed at some future major revision of 4707583c59eSCedric Chaumont * this specification 4717583c59eSCedric Chaumont */ 472b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object) 473b0104773SPascal Brand { 474b0104773SPascal Brand TEE_Result res; 475b0104773SPascal Brand 476b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 477b0104773SPascal Brand return; 478b0104773SPascal Brand 4797583c59eSCedric Chaumont res = TEE_CloseAndDeletePersistentObject1(object); 480b0104773SPascal Brand 481b0104773SPascal Brand if (res != TEE_SUCCESS) 482b0104773SPascal Brand TEE_Panic(0); 483b0104773SPascal Brand } 484b0104773SPascal Brand 4857583c59eSCedric Chaumont TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object) 4867583c59eSCedric Chaumont { 4877583c59eSCedric Chaumont TEE_Result res; 4887583c59eSCedric Chaumont 4897583c59eSCedric Chaumont if (object == TEE_HANDLE_NULL) 4907583c59eSCedric Chaumont return TEE_ERROR_STORAGE_NOT_AVAILABLE; 4917583c59eSCedric Chaumont 492*2c028fdeSJerome Forissier res = _utee_storage_obj_del((unsigned long)object); 4937583c59eSCedric Chaumont 4947583c59eSCedric Chaumont if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 495b36311adSJerome Forissier TEE_Panic(res); 4967583c59eSCedric Chaumont 4977583c59eSCedric Chaumont return res; 4987583c59eSCedric Chaumont } 4997583c59eSCedric Chaumont 5007583c59eSCedric Chaumont 501b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object, 502b0104773SPascal Brand const void *newObjectID, 50379a3c601SCedric Chaumont uint32_t newObjectIDLen) 504b0104773SPascal Brand { 505b0104773SPascal Brand TEE_Result res; 506b0104773SPascal Brand 507a76bf53fSCedric Chaumont if (object == TEE_HANDLE_NULL) { 508a76bf53fSCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 509a76bf53fSCedric Chaumont goto out; 510a76bf53fSCedric Chaumont } 511b0104773SPascal Brand 512a76bf53fSCedric Chaumont if (!newObjectID) { 513a76bf53fSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 514a76bf53fSCedric Chaumont goto out; 515a76bf53fSCedric Chaumont } 516b0104773SPascal Brand 517a76bf53fSCedric Chaumont if (newObjectIDLen > TEE_OBJECT_ID_MAX_LEN) { 518a76bf53fSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 519a76bf53fSCedric Chaumont goto out; 520a76bf53fSCedric Chaumont } 521b0104773SPascal Brand 522*2c028fdeSJerome Forissier res = _utee_storage_obj_rename((unsigned long)object, newObjectID, 523e86f1266SJens Wiklander 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) 530b36311adSJerome Forissier TEE_Panic(res); 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; 539e86f1266SJens Wiklander uint32_t oe; 540b0104773SPascal Brand 54115cd3c30SCedric Chaumont if (!objectEnumerator) 542b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 543b0104773SPascal Brand 544*2c028fdeSJerome Forissier res = _utee_storage_alloc_enum(&oe); 545b0104773SPascal Brand 546b0104773SPascal Brand if (res != TEE_SUCCESS) 547e86f1266SJens Wiklander oe = TEE_HANDLE_NULL; 548e86f1266SJens Wiklander 549e86f1266SJens Wiklander *objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe; 550b0104773SPascal Brand 55115cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 55215cd3c30SCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT) 553b36311adSJerome Forissier TEE_Panic(res); 55415cd3c30SCedric Chaumont 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 565*2c028fdeSJerome Forissier res = _utee_storage_free_enum((unsigned long)objectEnumerator); 566b0104773SPascal Brand 567b0104773SPascal Brand if (res != TEE_SUCCESS) 568b36311adSJerome Forissier TEE_Panic(res); 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 578*2c028fdeSJerome Forissier res = _utee_storage_reset_enum((unsigned long)objectEnumerator); 579b0104773SPascal Brand 580b0104773SPascal Brand if (res != TEE_SUCCESS) 581b36311adSJerome Forissier TEE_Panic(res); 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 590*2c028fdeSJerome Forissier res = _utee_storage_start_enum((unsigned long)objectEnumerator, 591e86f1266SJens Wiklander storageID); 592b0104773SPascal Brand 59315cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 59415cd3c30SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 59515cd3c30SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 59615cd3c30SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 597b36311adSJerome Forissier TEE_Panic(res); 598b0104773SPascal Brand 599b0104773SPascal Brand return res; 600b0104773SPascal Brand } 601b0104773SPascal Brand 602b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator, 603b0104773SPascal Brand TEE_ObjectInfo *objectInfo, 60479a3c601SCedric Chaumont void *objectID, uint32_t *objectIDLen) 605b0104773SPascal Brand { 606b0104773SPascal Brand TEE_Result res; 607e86f1266SJens Wiklander uint64_t len; 6082342799fSPascal Brand TEE_ObjectInfo local_info; 6092342799fSPascal Brand TEE_ObjectInfo *pt_info; 610b0104773SPascal Brand 61115cd3c30SCedric Chaumont if (!objectID) { 61215cd3c30SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 61315cd3c30SCedric Chaumont goto out; 61415cd3c30SCedric Chaumont } 61515cd3c30SCedric Chaumont 61615cd3c30SCedric Chaumont if (!objectIDLen) { 61715cd3c30SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 61815cd3c30SCedric Chaumont goto out; 61915cd3c30SCedric Chaumont } 62015cd3c30SCedric Chaumont 6212342799fSPascal Brand if (objectInfo) 6222342799fSPascal Brand pt_info = objectInfo; 6232342799fSPascal Brand else 6242342799fSPascal Brand pt_info = &local_info; 625e86f1266SJens Wiklander len = *objectIDLen; 626*2c028fdeSJerome Forissier res = _utee_storage_next_enum((unsigned long)objectEnumerator, 6272342799fSPascal Brand pt_info, objectID, &len); 628e86f1266SJens Wiklander *objectIDLen = len; 629b0104773SPascal Brand 63015cd3c30SCedric Chaumont out: 63115cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 63215cd3c30SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 63315cd3c30SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 63415cd3c30SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 635b36311adSJerome Forissier TEE_Panic(res); 636b0104773SPascal Brand 637b0104773SPascal Brand return res; 638b0104773SPascal Brand } 639b0104773SPascal Brand 640b0104773SPascal Brand /* Data and Key Storage API - Data Stream Access Functions */ 641b0104773SPascal Brand 642b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer, 64379a3c601SCedric Chaumont uint32_t size, uint32_t *count) 644b0104773SPascal Brand { 645b0104773SPascal Brand TEE_Result res; 646e86f1266SJens Wiklander uint64_t cnt64; 647b0104773SPascal Brand 648ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 649ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 650ae1289baSCedric Chaumont goto out; 651ae1289baSCedric Chaumont } 652b0104773SPascal Brand 653e86f1266SJens Wiklander cnt64 = *count; 654*2c028fdeSJerome Forissier res = _utee_storage_obj_read((unsigned long)object, buffer, size, 655e86f1266SJens Wiklander &cnt64); 656e86f1266SJens Wiklander *count = cnt64; 657b0104773SPascal Brand 658ae1289baSCedric Chaumont out: 659ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 660ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 661ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 662b36311adSJerome Forissier TEE_Panic(res); 663b0104773SPascal Brand 664b0104773SPascal Brand return res; 665b0104773SPascal Brand } 666b0104773SPascal Brand 6678f07fe6fSJerome Forissier TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, const void *buffer, 66879a3c601SCedric Chaumont uint32_t size) 669b0104773SPascal Brand { 670b0104773SPascal Brand TEE_Result res; 671b0104773SPascal Brand 672ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 673ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 674ae1289baSCedric Chaumont goto out; 675ae1289baSCedric Chaumont } 676ae1289baSCedric Chaumont 677ae1289baSCedric Chaumont if (size > TEE_DATA_MAX_POSITION) { 678ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 679ae1289baSCedric Chaumont goto out; 680ae1289baSCedric Chaumont } 681b0104773SPascal Brand 682*2c028fdeSJerome Forissier res = _utee_storage_obj_write((unsigned long)object, buffer, size); 683b0104773SPascal Brand 684ae1289baSCedric Chaumont out: 685ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 686ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NO_SPACE && 687ae1289baSCedric Chaumont res != TEE_ERROR_OVERFLOW && 688ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 689ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 690b36311adSJerome Forissier TEE_Panic(res); 691b0104773SPascal Brand 692b0104773SPascal Brand return res; 693b0104773SPascal Brand } 694b0104773SPascal Brand 695b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size) 696b0104773SPascal Brand { 697b0104773SPascal Brand TEE_Result res; 698b0104773SPascal Brand 699ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 700ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 701ae1289baSCedric Chaumont goto out; 702ae1289baSCedric Chaumont } 703b0104773SPascal Brand 704*2c028fdeSJerome Forissier res = _utee_storage_obj_trunc((unsigned long)object, size); 705b0104773SPascal Brand 706ae1289baSCedric Chaumont out: 707ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 708ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NO_SPACE && 709ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 710ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 711b36311adSJerome Forissier TEE_Panic(res); 712b0104773SPascal Brand 713b0104773SPascal Brand return res; 714b0104773SPascal Brand } 715b0104773SPascal Brand 716b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset, 717b0104773SPascal Brand TEE_Whence whence) 718b0104773SPascal Brand { 719b0104773SPascal Brand TEE_Result res; 720b0104773SPascal Brand TEE_ObjectInfo info; 721b0104773SPascal Brand 722ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 723ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 724ae1289baSCedric Chaumont goto out; 725ae1289baSCedric Chaumont } 726b0104773SPascal Brand 727*2c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 728b0104773SPascal Brand if (res != TEE_SUCCESS) 729ae1289baSCedric Chaumont goto out; 730b0104773SPascal Brand 731b0104773SPascal Brand switch (whence) { 732b0104773SPascal Brand case TEE_DATA_SEEK_SET: 733ae1289baSCedric Chaumont if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) { 734ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 735ae1289baSCedric Chaumont goto out; 736ae1289baSCedric Chaumont } 737b0104773SPascal Brand break; 738b0104773SPascal Brand case TEE_DATA_SEEK_CUR: 739b0104773SPascal Brand if (offset > 0 && 740b0104773SPascal Brand ((uint32_t)offset + info.dataPosition > 741b0104773SPascal Brand TEE_DATA_MAX_POSITION || 742b0104773SPascal Brand (uint32_t)offset + info.dataPosition < 743ae1289baSCedric Chaumont info.dataPosition)) { 744ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 745ae1289baSCedric Chaumont goto out; 746ae1289baSCedric Chaumont } 747b0104773SPascal Brand break; 748b0104773SPascal Brand case TEE_DATA_SEEK_END: 749b0104773SPascal Brand if (offset > 0 && 750b0104773SPascal Brand ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION || 751ae1289baSCedric Chaumont (uint32_t)offset + info.dataSize < info.dataSize)) { 752ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 753ae1289baSCedric Chaumont goto out; 754ae1289baSCedric Chaumont } 755b0104773SPascal Brand break; 756b0104773SPascal Brand default: 757ae1289baSCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 758ae1289baSCedric Chaumont goto out; 759b0104773SPascal Brand } 760b0104773SPascal Brand 761*2c028fdeSJerome Forissier res = _utee_storage_obj_seek((unsigned long)object, offset, whence); 762b0104773SPascal Brand 763ae1289baSCedric Chaumont out: 764ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 765ae1289baSCedric Chaumont res != TEE_ERROR_OVERFLOW && 766ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 767ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 768b36311adSJerome Forissier TEE_Panic(res); 769b0104773SPascal Brand 770b0104773SPascal Brand return res; 771b0104773SPascal Brand } 772