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 452c028fdeSJerome 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 642c028fdeSJerome 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 852c028fdeSJerome 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 992c028fdeSJerome Forissier res = _utee_cryp_obj_restrict_usage((unsigned long)object, 1002c028fdeSJerome 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 1186915bbbbSJens Wiklander __utee_check_inout_annotation(size, sizeof(*size)); 1196915bbbbSJens Wiklander 1202c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 121b0104773SPascal Brand if (res != TEE_SUCCESS) 122a2e9a830SCedric Chaumont goto exit; 123b0104773SPascal Brand 124b0104773SPascal Brand /* This function only supports reference attributes */ 125a2e9a830SCedric Chaumont if ((attributeID & TEE_ATTR_BIT_VALUE)) { 126a2e9a830SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 127a2e9a830SCedric Chaumont goto exit; 128a2e9a830SCedric Chaumont } 129b0104773SPascal Brand 130e86f1266SJens Wiklander sz = *size; 1312c028fdeSJerome Forissier res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID, 132e86f1266SJens Wiklander buffer, &sz); 133e86f1266SJens Wiklander *size = sz; 134b0104773SPascal Brand 135a2e9a830SCedric Chaumont exit: 1360ed6a6caSCedric Chaumont if (res != TEE_SUCCESS && 1370ed6a6caSCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 1380ed6a6caSCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1390ed6a6caSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 1400ed6a6caSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 141b36311adSJerome Forissier TEE_Panic(res); 142b0104773SPascal Brand 143b0104773SPascal Brand return res; 144b0104773SPascal Brand } 145b0104773SPascal Brand 146b0104773SPascal Brand TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object, 147b0104773SPascal Brand uint32_t attributeID, uint32_t *a, 148b0104773SPascal Brand uint32_t *b) 149b0104773SPascal Brand { 150b0104773SPascal Brand TEE_Result res; 151b0104773SPascal Brand TEE_ObjectInfo info; 152b0104773SPascal Brand uint32_t buf[2]; 153e86f1266SJens Wiklander uint64_t size = sizeof(buf); 154b0104773SPascal Brand 1556915bbbbSJens Wiklander if (a) 1566915bbbbSJens Wiklander __utee_check_out_annotation(a, sizeof(*a)); 1576915bbbbSJens Wiklander if (b) 1586915bbbbSJens Wiklander __utee_check_out_annotation(b, sizeof(*b)); 1596915bbbbSJens Wiklander 1602c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 161b0104773SPascal Brand if (res != TEE_SUCCESS) 162a2e9a830SCedric Chaumont goto exit; 163b0104773SPascal Brand 164b0104773SPascal Brand /* This function only supports value attributes */ 165a2e9a830SCedric Chaumont if (!(attributeID & TEE_ATTR_BIT_VALUE)) { 166a2e9a830SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 167a2e9a830SCedric Chaumont goto exit; 168a2e9a830SCedric Chaumont } 169b0104773SPascal Brand 1702c028fdeSJerome Forissier res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID, buf, 171e86f1266SJens Wiklander &size); 172b0104773SPascal Brand 173a2e9a830SCedric Chaumont exit: 1740ed6a6caSCedric Chaumont if (res != TEE_SUCCESS && 1750ed6a6caSCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 1760ed6a6caSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 1770ed6a6caSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 178b36311adSJerome Forissier TEE_Panic(res); 179b0104773SPascal Brand 180b0104773SPascal Brand if (size != sizeof(buf)) 181b0104773SPascal Brand TEE_Panic(0); 182b0104773SPascal Brand 183be96c837SJerome Forissier if (res == TEE_SUCCESS) { 184b9d8f134SJerome Forissier if (a) 185b0104773SPascal Brand *a = buf[0]; 186b9d8f134SJerome Forissier if (b) 187b0104773SPascal Brand *b = buf[1]; 188be96c837SJerome Forissier } 189b0104773SPascal Brand 190b0104773SPascal Brand return res; 191b0104773SPascal Brand } 192b0104773SPascal Brand 193b0104773SPascal Brand void TEE_CloseObject(TEE_ObjectHandle object) 194b0104773SPascal Brand { 195b0104773SPascal Brand TEE_Result res; 196b0104773SPascal Brand 197b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 198b0104773SPascal Brand return; 199b0104773SPascal Brand 2002c028fdeSJerome Forissier res = _utee_cryp_obj_close((unsigned long)object); 201b0104773SPascal Brand if (res != TEE_SUCCESS) 202b36311adSJerome Forissier TEE_Panic(res); 203b0104773SPascal Brand } 204b0104773SPascal Brand 205b0104773SPascal Brand /* Data and Key Storage API - Transient Object Functions */ 206b0104773SPascal Brand 207b0104773SPascal Brand TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType, 20879a3c601SCedric Chaumont uint32_t maxKeySize, 209b0104773SPascal Brand TEE_ObjectHandle *object) 210b0104773SPascal Brand { 211b0104773SPascal Brand TEE_Result res; 212b0104773SPascal Brand uint32_t obj; 213b0104773SPascal Brand 2146915bbbbSJens Wiklander __utee_check_out_annotation(object, sizeof(*object)); 2156915bbbbSJens Wiklander 2162c028fdeSJerome Forissier res = _utee_cryp_obj_alloc(objectType, maxKeySize, &obj); 217aeb0d927SCedric Chaumont 218aeb0d927SCedric Chaumont if (res != TEE_SUCCESS && 219aeb0d927SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 220aeb0d927SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 221b36311adSJerome Forissier TEE_Panic(res); 222aeb0d927SCedric Chaumont 223b0104773SPascal Brand if (res == TEE_SUCCESS) 224e86f1266SJens Wiklander *object = (TEE_ObjectHandle)(uintptr_t)obj; 2250ed6a6caSCedric Chaumont 226b0104773SPascal Brand return res; 227b0104773SPascal Brand } 228b0104773SPascal Brand 229b0104773SPascal Brand void TEE_FreeTransientObject(TEE_ObjectHandle object) 230b0104773SPascal Brand { 231b0104773SPascal Brand TEE_Result res; 232b0104773SPascal Brand TEE_ObjectInfo info; 233b0104773SPascal Brand 234b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 235b0104773SPascal Brand return; 236b0104773SPascal Brand 2372c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 238b0104773SPascal Brand if (res != TEE_SUCCESS) 239b36311adSJerome Forissier TEE_Panic(res); 240b0104773SPascal Brand 241b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 242b0104773SPascal Brand TEE_Panic(0); 243b0104773SPascal Brand 2442c028fdeSJerome Forissier res = _utee_cryp_obj_close((unsigned long)object); 245b0104773SPascal Brand if (res != TEE_SUCCESS) 246b36311adSJerome Forissier TEE_Panic(res); 247b0104773SPascal Brand } 248b0104773SPascal Brand 249b0104773SPascal Brand void TEE_ResetTransientObject(TEE_ObjectHandle object) 250b0104773SPascal Brand { 251b0104773SPascal Brand TEE_Result res; 252b0104773SPascal Brand TEE_ObjectInfo info; 253b0104773SPascal Brand 254b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 255b0104773SPascal Brand return; 256b0104773SPascal Brand 2572c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 258b0104773SPascal Brand if (res != TEE_SUCCESS) 259b36311adSJerome Forissier TEE_Panic(res); 260b0104773SPascal Brand 261b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 262b0104773SPascal Brand TEE_Panic(0); 263b0104773SPascal Brand 2642c028fdeSJerome Forissier res = _utee_cryp_obj_reset((unsigned long)object); 265b0104773SPascal Brand if (res != TEE_SUCCESS) 266b36311adSJerome Forissier TEE_Panic(res); 267b0104773SPascal Brand } 268b0104773SPascal Brand 269b0104773SPascal Brand TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object, 2708f07fe6fSJerome Forissier const TEE_Attribute *attrs, 271b0104773SPascal Brand uint32_t attrCount) 272b0104773SPascal Brand { 273b0104773SPascal Brand TEE_Result res; 274b0104773SPascal Brand TEE_ObjectInfo info; 275e86f1266SJens Wiklander struct utee_attribute ua[attrCount]; 276b0104773SPascal Brand 2776915bbbbSJens Wiklander __utee_check_attr_in_annotation(attrs, attrCount); 2786915bbbbSJens Wiklander 2792c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 280b0104773SPascal Brand if (res != TEE_SUCCESS) 281b36311adSJerome Forissier TEE_Panic(res); 282b0104773SPascal Brand 283b0104773SPascal Brand /* Must be a transient object */ 284b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 285b0104773SPascal Brand TEE_Panic(0); 286b0104773SPascal Brand 287b0104773SPascal Brand /* Must not be initialized already */ 288b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 289b0104773SPascal Brand TEE_Panic(0); 290b0104773SPascal Brand 291e86f1266SJens Wiklander __utee_from_attr(ua, attrs, attrCount); 2922c028fdeSJerome Forissier res = _utee_cryp_obj_populate((unsigned long)object, ua, attrCount); 293b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 294b0104773SPascal Brand TEE_Panic(res); 295b0104773SPascal Brand return res; 296b0104773SPascal Brand } 297b0104773SPascal Brand 298b0104773SPascal Brand void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID, 2998f07fe6fSJerome Forissier const void *buffer, uint32_t length) 300b0104773SPascal Brand { 3016915bbbbSJens Wiklander __utee_check_out_annotation(attr, sizeof(*attr)); 3026915bbbbSJens Wiklander 303b0104773SPascal Brand if ((attributeID & TEE_ATTR_BIT_VALUE) != 0) 304b0104773SPascal Brand TEE_Panic(0); 305b0104773SPascal Brand attr->attributeID = attributeID; 3068f07fe6fSJerome Forissier attr->content.ref.buffer = (void *)buffer; 307b0104773SPascal Brand attr->content.ref.length = length; 308b0104773SPascal Brand } 309b0104773SPascal Brand 310b0104773SPascal Brand void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID, 311b0104773SPascal Brand uint32_t a, uint32_t b) 312b0104773SPascal Brand { 3136915bbbbSJens Wiklander __utee_check_out_annotation(attr, sizeof(*attr)); 3146915bbbbSJens Wiklander 315b0104773SPascal Brand if ((attributeID & TEE_ATTR_BIT_VALUE) == 0) 316b0104773SPascal Brand TEE_Panic(0); 317b0104773SPascal Brand attr->attributeID = attributeID; 318b0104773SPascal Brand attr->content.value.a = a; 319b0104773SPascal Brand attr->content.value.b = b; 320b0104773SPascal Brand } 321b0104773SPascal Brand 3227583c59eSCedric Chaumont /* 3237583c59eSCedric Chaumont * Use of this function is deprecated 3247583c59eSCedric Chaumont * new code SHOULD use the TEE_CopyObjectAttributes1 function instead 3257583c59eSCedric Chaumont * These functions will be removed at some future major revision of 3267583c59eSCedric Chaumont * this specification 3277583c59eSCedric Chaumont */ 328b0104773SPascal Brand void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject, 329b0104773SPascal Brand TEE_ObjectHandle srcObject) 330b0104773SPascal Brand { 331b0104773SPascal Brand TEE_Result res; 3327583c59eSCedric Chaumont TEE_ObjectInfo src_info; 3337583c59eSCedric Chaumont 3342c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info); 3357583c59eSCedric Chaumont if (src_info.objectType == TEE_TYPE_CORRUPTED_OBJECT) 3367583c59eSCedric Chaumont return; 3377583c59eSCedric Chaumont 3387583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(destObject, srcObject); 3397583c59eSCedric Chaumont if (res != TEE_SUCCESS) 340b36311adSJerome Forissier TEE_Panic(res); 3417583c59eSCedric Chaumont } 3427583c59eSCedric Chaumont 3437583c59eSCedric Chaumont TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject, 3447583c59eSCedric Chaumont TEE_ObjectHandle srcObject) 3457583c59eSCedric Chaumont { 3467583c59eSCedric Chaumont TEE_Result res; 347b0104773SPascal Brand TEE_ObjectInfo dst_info; 348b0104773SPascal Brand TEE_ObjectInfo src_info; 349b0104773SPascal Brand 3502c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)destObject, &dst_info); 351b0104773SPascal Brand if (res != TEE_SUCCESS) 352a2e9a830SCedric Chaumont goto exit; 353b0104773SPascal Brand 3542c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info); 355b0104773SPascal Brand if (res != TEE_SUCCESS) 356a2e9a830SCedric Chaumont goto exit; 357b0104773SPascal Brand 358a2e9a830SCedric Chaumont if (!(src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 359b0104773SPascal Brand TEE_Panic(0); 360a2e9a830SCedric Chaumont 361a2e9a830SCedric Chaumont if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) 362b0104773SPascal Brand TEE_Panic(0); 363a2e9a830SCedric Chaumont 364a2e9a830SCedric Chaumont if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 365b0104773SPascal Brand TEE_Panic(0); 366b0104773SPascal Brand 3672c028fdeSJerome Forissier res = _utee_cryp_obj_copy((unsigned long)destObject, 368e86f1266SJens Wiklander (unsigned long)srcObject); 3697583c59eSCedric Chaumont 370a2e9a830SCedric Chaumont exit: 371a2e9a830SCedric Chaumont if (res != TEE_SUCCESS && 372a2e9a830SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 373a2e9a830SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 374a2e9a830SCedric Chaumont TEE_Panic(res); 3757583c59eSCedric Chaumont 3767583c59eSCedric Chaumont return res; 377b0104773SPascal Brand } 378b0104773SPascal Brand 379b0104773SPascal Brand TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize, 3808f07fe6fSJerome Forissier const TEE_Attribute *params, uint32_t paramCount) 381b0104773SPascal Brand { 382b0104773SPascal Brand TEE_Result res; 383e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 384b0104773SPascal Brand 3856915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 3866915bbbbSJens Wiklander 387e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 3882c028fdeSJerome Forissier res = _utee_cryp_obj_generate_key((unsigned long)object, keySize, 389e86f1266SJens Wiklander ua, paramCount); 390b0104773SPascal Brand 391aeb0d927SCedric Chaumont if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 392b36311adSJerome Forissier TEE_Panic(res); 393b0104773SPascal Brand 394b0104773SPascal Brand return res; 395b0104773SPascal Brand } 396b0104773SPascal Brand 397b0104773SPascal Brand /* Data and Key Storage API - Persistent Object Functions */ 398b0104773SPascal Brand 3998f07fe6fSJerome Forissier TEE_Result TEE_OpenPersistentObject(uint32_t storageID, const void *objectID, 40079a3c601SCedric Chaumont uint32_t objectIDLen, uint32_t flags, 401b0104773SPascal Brand TEE_ObjectHandle *object) 402b0104773SPascal Brand { 4039b520646SCedric Chaumont TEE_Result res; 404e86f1266SJens Wiklander uint32_t obj; 405b0104773SPascal Brand 4069b520646SCedric Chaumont if (!objectID) { 4079b520646SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 408172d637bSCedric Auger goto exit; 4099b520646SCedric Chaumont } 4109b520646SCedric Chaumont 4112c028fdeSJerome Forissier res = _utee_storage_obj_open(storageID, objectID, objectIDLen, flags, 412e86f1266SJens Wiklander &obj); 413e86f1266SJens Wiklander if (res == TEE_SUCCESS) 414e86f1266SJens Wiklander *object = (TEE_ObjectHandle)(uintptr_t)obj; 4159b520646SCedric Chaumont 416172d637bSCedric Auger exit: 4179b520646SCedric Chaumont if (res != TEE_SUCCESS && 4189b520646SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 4199b520646SCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT && 4209b520646SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 4219b520646SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 4229b520646SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 423b36311adSJerome Forissier TEE_Panic(res); 424b0104773SPascal Brand 425172d637bSCedric Auger if (res != TEE_SUCCESS) 42669a3d6beSDaniel Glöckner *object = TEE_HANDLE_NULL; 42769a3d6beSDaniel Glöckner 4289b520646SCedric Chaumont return res; 429b0104773SPascal Brand } 430b0104773SPascal Brand 4318f07fe6fSJerome Forissier TEE_Result TEE_CreatePersistentObject(uint32_t storageID, const void *objectID, 43279a3c601SCedric Chaumont uint32_t objectIDLen, uint32_t flags, 433b0104773SPascal Brand TEE_ObjectHandle attributes, 434b0104773SPascal Brand const void *initialData, 43579a3c601SCedric Chaumont uint32_t initialDataLen, 436b0104773SPascal Brand TEE_ObjectHandle *object) 437b0104773SPascal Brand { 43884431ae3SCedric Chaumont TEE_Result res; 439e86f1266SJens Wiklander uint32_t obj; 440b0104773SPascal Brand 441aeb0d927SCedric Chaumont if (!objectID) { 44284431ae3SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 443172d637bSCedric Auger goto exit; 44484431ae3SCedric Chaumont } 445b0104773SPascal Brand 4466915bbbbSJens Wiklander __utee_check_out_annotation(object, sizeof(*object)); 447b0104773SPascal Brand 4482c028fdeSJerome Forissier res = _utee_storage_obj_create(storageID, objectID, objectIDLen, flags, 449e86f1266SJens Wiklander (unsigned long)attributes, initialData, 450e86f1266SJens Wiklander initialDataLen, &obj); 451172d637bSCedric Auger 4521c96fa7fSPascal Brand if (res == TEE_SUCCESS) 453172d637bSCedric Auger *object = (TEE_ObjectHandle)(uintptr_t)obj; 454172d637bSCedric Auger 455172d637bSCedric Auger exit: 456172d637bSCedric Auger if (res != TEE_SUCCESS && 457172d637bSCedric Auger res != TEE_ERROR_ITEM_NOT_FOUND && 458172d637bSCedric Auger res != TEE_ERROR_ACCESS_CONFLICT && 459172d637bSCedric Auger res != TEE_ERROR_OUT_OF_MEMORY && 460172d637bSCedric Auger res != TEE_ERROR_STORAGE_NO_SPACE && 461172d637bSCedric Auger res != TEE_ERROR_CORRUPT_OBJECT && 462172d637bSCedric Auger res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 463b36311adSJerome Forissier TEE_Panic(res); 464172d637bSCedric Auger 465172d637bSCedric Auger if (res != TEE_SUCCESS) 466172d637bSCedric Auger *object = TEE_HANDLE_NULL; 467172d637bSCedric Auger 468172d637bSCedric Auger return res; 469b0104773SPascal Brand } 470b0104773SPascal Brand 4717583c59eSCedric Chaumont /* 4727583c59eSCedric Chaumont * Use of this function is deprecated 4737583c59eSCedric Chaumont * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead 4747583c59eSCedric Chaumont * These functions will be removed at some future major revision of 4757583c59eSCedric Chaumont * this specification 4767583c59eSCedric Chaumont */ 477b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object) 478b0104773SPascal Brand { 479b0104773SPascal Brand TEE_Result res; 480b0104773SPascal Brand 481b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 482b0104773SPascal Brand return; 483b0104773SPascal Brand 4847583c59eSCedric Chaumont res = TEE_CloseAndDeletePersistentObject1(object); 485b0104773SPascal Brand 486b0104773SPascal Brand if (res != TEE_SUCCESS) 487b0104773SPascal Brand TEE_Panic(0); 488b0104773SPascal Brand } 489b0104773SPascal Brand 4907583c59eSCedric Chaumont TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object) 4917583c59eSCedric Chaumont { 4927583c59eSCedric Chaumont TEE_Result res; 4937583c59eSCedric Chaumont 4947583c59eSCedric Chaumont if (object == TEE_HANDLE_NULL) 495*46cfd17cSJens Wiklander return TEE_SUCCESS; 4967583c59eSCedric Chaumont 4972c028fdeSJerome Forissier res = _utee_storage_obj_del((unsigned long)object); 4987583c59eSCedric Chaumont 4997583c59eSCedric Chaumont if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 500b36311adSJerome Forissier TEE_Panic(res); 5017583c59eSCedric Chaumont 5027583c59eSCedric Chaumont return res; 5037583c59eSCedric Chaumont } 5047583c59eSCedric Chaumont 5057583c59eSCedric Chaumont 506b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object, 507b0104773SPascal Brand const void *newObjectID, 50879a3c601SCedric Chaumont uint32_t newObjectIDLen) 509b0104773SPascal Brand { 510b0104773SPascal Brand TEE_Result res; 511b0104773SPascal Brand 512a76bf53fSCedric Chaumont if (object == TEE_HANDLE_NULL) { 513a76bf53fSCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 514a76bf53fSCedric Chaumont goto out; 515a76bf53fSCedric Chaumont } 516b0104773SPascal Brand 5172c028fdeSJerome Forissier res = _utee_storage_obj_rename((unsigned long)object, newObjectID, 518e86f1266SJens Wiklander newObjectIDLen); 519b0104773SPascal Brand 520a76bf53fSCedric Chaumont out: 521a76bf53fSCedric Chaumont if (res != TEE_SUCCESS && 522a76bf53fSCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT && 523a76bf53fSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 524a76bf53fSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 525b36311adSJerome Forissier TEE_Panic(res); 526b0104773SPascal Brand 527b0104773SPascal Brand return res; 528b0104773SPascal Brand } 529b0104773SPascal Brand 530b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle * 531b0104773SPascal Brand objectEnumerator) 532b0104773SPascal Brand { 533b0104773SPascal Brand TEE_Result res; 534e86f1266SJens Wiklander uint32_t oe; 535b0104773SPascal Brand 5366915bbbbSJens Wiklander __utee_check_out_annotation(objectEnumerator, 5376915bbbbSJens Wiklander sizeof(*objectEnumerator)); 538b0104773SPascal Brand 5392c028fdeSJerome Forissier res = _utee_storage_alloc_enum(&oe); 540b0104773SPascal Brand 541b0104773SPascal Brand if (res != TEE_SUCCESS) 542e86f1266SJens Wiklander oe = TEE_HANDLE_NULL; 543e86f1266SJens Wiklander 544e86f1266SJens Wiklander *objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe; 545b0104773SPascal Brand 54615cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 54715cd3c30SCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT) 548b36311adSJerome Forissier TEE_Panic(res); 54915cd3c30SCedric Chaumont 550b0104773SPascal Brand return res; 551b0104773SPascal Brand } 552b0104773SPascal Brand 553b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 554b0104773SPascal Brand { 555b0104773SPascal Brand TEE_Result res; 556b0104773SPascal Brand 557b0104773SPascal Brand if (objectEnumerator == TEE_HANDLE_NULL) 558b0104773SPascal Brand return; 559b0104773SPascal Brand 5602c028fdeSJerome Forissier res = _utee_storage_free_enum((unsigned long)objectEnumerator); 561b0104773SPascal Brand 562b0104773SPascal Brand if (res != TEE_SUCCESS) 563b36311adSJerome Forissier TEE_Panic(res); 564b0104773SPascal Brand } 565b0104773SPascal Brand 566b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 567b0104773SPascal Brand { 568b0104773SPascal Brand TEE_Result res; 569b0104773SPascal Brand 570b0104773SPascal Brand if (objectEnumerator == TEE_HANDLE_NULL) 571b0104773SPascal Brand return; 572b0104773SPascal Brand 5732c028fdeSJerome Forissier res = _utee_storage_reset_enum((unsigned long)objectEnumerator); 574b0104773SPascal Brand 575b0104773SPascal Brand if (res != TEE_SUCCESS) 576b36311adSJerome Forissier TEE_Panic(res); 577b0104773SPascal Brand } 578b0104773SPascal Brand 579b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle 580b0104773SPascal Brand objectEnumerator, 581b0104773SPascal Brand uint32_t storageID) 582b0104773SPascal Brand { 583b0104773SPascal Brand TEE_Result res; 584b0104773SPascal Brand 5852c028fdeSJerome Forissier res = _utee_storage_start_enum((unsigned long)objectEnumerator, 586e86f1266SJens Wiklander storageID); 587b0104773SPascal Brand 58815cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 58915cd3c30SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 59015cd3c30SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 59115cd3c30SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 592b36311adSJerome Forissier TEE_Panic(res); 593b0104773SPascal Brand 594b0104773SPascal Brand return res; 595b0104773SPascal Brand } 596b0104773SPascal Brand 597b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator, 598b0104773SPascal Brand TEE_ObjectInfo *objectInfo, 59979a3c601SCedric Chaumont void *objectID, uint32_t *objectIDLen) 600b0104773SPascal Brand { 601b0104773SPascal Brand TEE_Result res; 602e86f1266SJens Wiklander uint64_t len; 6032342799fSPascal Brand TEE_ObjectInfo local_info; 6042342799fSPascal Brand TEE_ObjectInfo *pt_info; 605b0104773SPascal Brand 6066915bbbbSJens Wiklander if (objectInfo) 6076915bbbbSJens Wiklander __utee_check_out_annotation(objectInfo, sizeof(*objectInfo)); 6086915bbbbSJens Wiklander __utee_check_out_annotation(objectIDLen, sizeof(*objectIDLen)); 60915cd3c30SCedric Chaumont 6106915bbbbSJens Wiklander if (!objectID) { 61115cd3c30SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 61215cd3c30SCedric Chaumont goto out; 61315cd3c30SCedric Chaumont } 61415cd3c30SCedric Chaumont 6152342799fSPascal Brand if (objectInfo) 6162342799fSPascal Brand pt_info = objectInfo; 6172342799fSPascal Brand else 6182342799fSPascal Brand pt_info = &local_info; 619e86f1266SJens Wiklander len = *objectIDLen; 6202c028fdeSJerome Forissier res = _utee_storage_next_enum((unsigned long)objectEnumerator, 6212342799fSPascal Brand pt_info, objectID, &len); 622e86f1266SJens Wiklander *objectIDLen = len; 623b0104773SPascal Brand 62415cd3c30SCedric Chaumont out: 62515cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 62615cd3c30SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 62715cd3c30SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 62815cd3c30SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 629b36311adSJerome Forissier TEE_Panic(res); 630b0104773SPascal Brand 631b0104773SPascal Brand return res; 632b0104773SPascal Brand } 633b0104773SPascal Brand 634b0104773SPascal Brand /* Data and Key Storage API - Data Stream Access Functions */ 635b0104773SPascal Brand 636b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer, 63779a3c601SCedric Chaumont uint32_t size, uint32_t *count) 638b0104773SPascal Brand { 639b0104773SPascal Brand TEE_Result res; 640e86f1266SJens Wiklander uint64_t cnt64; 641b0104773SPascal Brand 642ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 643ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 644ae1289baSCedric Chaumont goto out; 645ae1289baSCedric Chaumont } 6466915bbbbSJens Wiklander __utee_check_out_annotation(count, sizeof(*count)); 647b0104773SPascal Brand 648e86f1266SJens Wiklander cnt64 = *count; 6492c028fdeSJerome Forissier res = _utee_storage_obj_read((unsigned long)object, buffer, size, 650e86f1266SJens Wiklander &cnt64); 651e86f1266SJens Wiklander *count = cnt64; 652b0104773SPascal Brand 653ae1289baSCedric Chaumont out: 654ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 655ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 656ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 657b36311adSJerome Forissier TEE_Panic(res); 658b0104773SPascal Brand 659b0104773SPascal Brand return res; 660b0104773SPascal Brand } 661b0104773SPascal Brand 6628f07fe6fSJerome Forissier TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, const void *buffer, 66379a3c601SCedric Chaumont uint32_t size) 664b0104773SPascal Brand { 665b0104773SPascal Brand TEE_Result res; 666b0104773SPascal Brand 667ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 668ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 669ae1289baSCedric Chaumont goto out; 670ae1289baSCedric Chaumont } 671ae1289baSCedric Chaumont 672ae1289baSCedric Chaumont if (size > TEE_DATA_MAX_POSITION) { 673ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 674ae1289baSCedric Chaumont goto out; 675ae1289baSCedric Chaumont } 676b0104773SPascal Brand 6772c028fdeSJerome Forissier res = _utee_storage_obj_write((unsigned long)object, buffer, size); 678b0104773SPascal Brand 679ae1289baSCedric Chaumont out: 680ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 681ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NO_SPACE && 682ae1289baSCedric Chaumont res != TEE_ERROR_OVERFLOW && 683ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 684ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 685b36311adSJerome Forissier TEE_Panic(res); 686b0104773SPascal Brand 687b0104773SPascal Brand return res; 688b0104773SPascal Brand } 689b0104773SPascal Brand 690b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size) 691b0104773SPascal Brand { 692b0104773SPascal Brand TEE_Result res; 693b0104773SPascal Brand 694ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 695ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 696ae1289baSCedric Chaumont goto out; 697ae1289baSCedric Chaumont } 698b0104773SPascal Brand 6992c028fdeSJerome Forissier res = _utee_storage_obj_trunc((unsigned long)object, size); 700b0104773SPascal Brand 701ae1289baSCedric Chaumont out: 702ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 703ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NO_SPACE && 704ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 705ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 706b36311adSJerome Forissier TEE_Panic(res); 707b0104773SPascal Brand 708b0104773SPascal Brand return res; 709b0104773SPascal Brand } 710b0104773SPascal Brand 711b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset, 712b0104773SPascal Brand TEE_Whence whence) 713b0104773SPascal Brand { 714b0104773SPascal Brand TEE_Result res; 715b0104773SPascal Brand TEE_ObjectInfo info; 716b0104773SPascal Brand 717ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 718ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 719ae1289baSCedric Chaumont goto out; 720ae1289baSCedric Chaumont } 721b0104773SPascal Brand 7222c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 723b0104773SPascal Brand if (res != TEE_SUCCESS) 724ae1289baSCedric Chaumont goto out; 725b0104773SPascal Brand 726b0104773SPascal Brand switch (whence) { 727b0104773SPascal Brand case TEE_DATA_SEEK_SET: 728ae1289baSCedric Chaumont if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) { 729ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 730ae1289baSCedric Chaumont goto out; 731ae1289baSCedric Chaumont } 732b0104773SPascal Brand break; 733b0104773SPascal Brand case TEE_DATA_SEEK_CUR: 734b0104773SPascal Brand if (offset > 0 && 735b0104773SPascal Brand ((uint32_t)offset + info.dataPosition > 736b0104773SPascal Brand TEE_DATA_MAX_POSITION || 737b0104773SPascal Brand (uint32_t)offset + info.dataPosition < 738ae1289baSCedric Chaumont info.dataPosition)) { 739ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 740ae1289baSCedric Chaumont goto out; 741ae1289baSCedric Chaumont } 742b0104773SPascal Brand break; 743b0104773SPascal Brand case TEE_DATA_SEEK_END: 744b0104773SPascal Brand if (offset > 0 && 745b0104773SPascal Brand ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION || 746ae1289baSCedric Chaumont (uint32_t)offset + info.dataSize < info.dataSize)) { 747ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 748ae1289baSCedric Chaumont goto out; 749ae1289baSCedric Chaumont } 750b0104773SPascal Brand break; 751b0104773SPascal Brand default: 752ae1289baSCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 753ae1289baSCedric Chaumont goto out; 754b0104773SPascal Brand } 755b0104773SPascal Brand 7562c028fdeSJerome Forissier res = _utee_storage_obj_seek((unsigned long)object, offset, whence); 757b0104773SPascal Brand 758ae1289baSCedric Chaumont out: 759ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 760ae1289baSCedric Chaumont res != TEE_ERROR_OVERFLOW && 761ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 762ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 763b36311adSJerome Forissier TEE_Panic(res); 764b0104773SPascal Brand 765b0104773SPascal Brand return res; 766b0104773SPascal Brand } 767