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 14e86f1266SJens Wiklander void __utee_from_attr(struct utee_attribute *ua, const TEE_Attribute *attrs, 15e86f1266SJens Wiklander uint32_t attr_count) 16e86f1266SJens Wiklander { 17e86f1266SJens Wiklander size_t n; 18e86f1266SJens Wiklander 19e86f1266SJens Wiklander for (n = 0; n < attr_count; n++) { 20e86f1266SJens Wiklander ua[n].attribute_id = attrs[n].attributeID; 21*b9416909SJens Wiklander if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE) { 22e86f1266SJens Wiklander ua[n].a = attrs[n].content.value.a; 23e86f1266SJens Wiklander ua[n].b = attrs[n].content.value.b; 24e86f1266SJens Wiklander } else { 25e86f1266SJens Wiklander ua[n].a = (uintptr_t)attrs[n].content.ref.buffer; 26e86f1266SJens Wiklander ua[n].b = attrs[n].content.ref.length; 27e86f1266SJens Wiklander } 28e86f1266SJens Wiklander } 29e86f1266SJens Wiklander } 30e86f1266SJens Wiklander 31b0104773SPascal Brand /* Data and Key Storage API - Generic Object Functions */ 327583c59eSCedric Chaumont /* 337583c59eSCedric Chaumont * Use of this function is deprecated 347583c59eSCedric Chaumont * new code SHOULD use the TEE_GetObjectInfo1 function instead 357583c59eSCedric Chaumont * These functions will be removed at some future major revision of 367583c59eSCedric Chaumont * this specification 377583c59eSCedric Chaumont */ 38b0104773SPascal Brand void TEE_GetObjectInfo(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo) 39b0104773SPascal Brand { 40b0104773SPascal Brand TEE_Result res; 41b0104773SPascal Brand 422c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, objectInfo); 437583c59eSCedric Chaumont 44b0104773SPascal Brand if (res != TEE_SUCCESS) 45b0104773SPascal Brand TEE_Panic(res); 467583c59eSCedric Chaumont 477583c59eSCedric Chaumont if (objectInfo->objectType == TEE_TYPE_CORRUPTED_OBJECT) { 487583c59eSCedric Chaumont objectInfo->keySize = 0; 497583c59eSCedric Chaumont objectInfo->maxKeySize = 0; 507583c59eSCedric Chaumont objectInfo->objectUsage = 0; 517583c59eSCedric Chaumont objectInfo->dataSize = 0; 527583c59eSCedric Chaumont objectInfo->dataPosition = 0; 537583c59eSCedric Chaumont objectInfo->handleFlags = 0; 547583c59eSCedric Chaumont } 55b0104773SPascal Brand } 56b0104773SPascal Brand 577583c59eSCedric Chaumont TEE_Result TEE_GetObjectInfo1(TEE_ObjectHandle object, TEE_ObjectInfo *objectInfo) 587583c59eSCedric Chaumont { 597583c59eSCedric Chaumont TEE_Result res; 607583c59eSCedric Chaumont 612c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, objectInfo); 627583c59eSCedric Chaumont 63a2e9a830SCedric Chaumont if (res != TEE_SUCCESS && 64a2e9a830SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 65a2e9a830SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 667583c59eSCedric Chaumont TEE_Panic(res); 677583c59eSCedric Chaumont 687583c59eSCedric Chaumont return res; 697583c59eSCedric Chaumont } 707583c59eSCedric Chaumont 717583c59eSCedric Chaumont /* 727583c59eSCedric Chaumont * Use of this function is deprecated 737583c59eSCedric Chaumont * new code SHOULD use the TEE_RestrictObjectUsage1 function instead 747583c59eSCedric Chaumont * These functions will be removed at some future major revision of 757583c59eSCedric Chaumont * this specification 767583c59eSCedric Chaumont */ 77b0104773SPascal Brand void TEE_RestrictObjectUsage(TEE_ObjectHandle object, uint32_t objectUsage) 78b0104773SPascal Brand { 79b0104773SPascal Brand TEE_Result res; 807583c59eSCedric Chaumont TEE_ObjectInfo objectInfo; 817583c59eSCedric Chaumont 822c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &objectInfo); 837583c59eSCedric Chaumont if (objectInfo.objectType == TEE_TYPE_CORRUPTED_OBJECT) 847583c59eSCedric Chaumont return; 857583c59eSCedric Chaumont 867583c59eSCedric Chaumont res = TEE_RestrictObjectUsage1(object, objectUsage); 87b0104773SPascal Brand 88b0104773SPascal Brand if (res != TEE_SUCCESS) 89b36311adSJerome Forissier TEE_Panic(res); 90b0104773SPascal Brand } 91b0104773SPascal Brand 927583c59eSCedric Chaumont TEE_Result TEE_RestrictObjectUsage1(TEE_ObjectHandle object, uint32_t objectUsage) 937583c59eSCedric Chaumont { 947583c59eSCedric Chaumont TEE_Result res; 957583c59eSCedric Chaumont 962c028fdeSJerome Forissier res = _utee_cryp_obj_restrict_usage((unsigned long)object, 972c028fdeSJerome Forissier objectUsage); 987583c59eSCedric Chaumont 99a2e9a830SCedric Chaumont if (res != TEE_SUCCESS && 100a2e9a830SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 101a2e9a830SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 102a2e9a830SCedric Chaumont TEE_Panic(res); 1037583c59eSCedric Chaumont 1047583c59eSCedric Chaumont return res; 1057583c59eSCedric Chaumont } 1067583c59eSCedric Chaumont 107b0104773SPascal Brand TEE_Result TEE_GetObjectBufferAttribute(TEE_ObjectHandle object, 108b0104773SPascal Brand uint32_t attributeID, void *buffer, 10979a3c601SCedric Chaumont uint32_t *size) 110b0104773SPascal Brand { 111b0104773SPascal Brand TEE_Result res; 112b0104773SPascal Brand TEE_ObjectInfo info; 113e86f1266SJens Wiklander uint64_t sz; 114b0104773SPascal Brand 1156915bbbbSJens Wiklander __utee_check_inout_annotation(size, sizeof(*size)); 1166915bbbbSJens Wiklander 1172c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 118b0104773SPascal Brand if (res != TEE_SUCCESS) 119a2e9a830SCedric Chaumont goto exit; 120b0104773SPascal Brand 121b0104773SPascal Brand /* This function only supports reference attributes */ 122*b9416909SJens Wiklander if ((attributeID & TEE_ATTR_FLAG_VALUE)) { 123a2e9a830SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 124a2e9a830SCedric Chaumont goto exit; 125a2e9a830SCedric Chaumont } 126b0104773SPascal Brand 127e86f1266SJens Wiklander sz = *size; 1282c028fdeSJerome Forissier res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID, 129e86f1266SJens Wiklander buffer, &sz); 130e86f1266SJens Wiklander *size = sz; 131b0104773SPascal Brand 132a2e9a830SCedric Chaumont exit: 1330ed6a6caSCedric Chaumont if (res != TEE_SUCCESS && 1340ed6a6caSCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 1350ed6a6caSCedric Chaumont res != TEE_ERROR_SHORT_BUFFER && 1360ed6a6caSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 1370ed6a6caSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 138b36311adSJerome Forissier TEE_Panic(res); 139b0104773SPascal Brand 140b0104773SPascal Brand return res; 141b0104773SPascal Brand } 142b0104773SPascal Brand 143b0104773SPascal Brand TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object, 144b0104773SPascal Brand uint32_t attributeID, uint32_t *a, 145b0104773SPascal Brand uint32_t *b) 146b0104773SPascal Brand { 147b0104773SPascal Brand TEE_Result res; 148b0104773SPascal Brand TEE_ObjectInfo info; 149b0104773SPascal Brand uint32_t buf[2]; 150e86f1266SJens Wiklander uint64_t size = sizeof(buf); 151b0104773SPascal Brand 1526915bbbbSJens Wiklander if (a) 1536915bbbbSJens Wiklander __utee_check_out_annotation(a, sizeof(*a)); 1546915bbbbSJens Wiklander if (b) 1556915bbbbSJens Wiklander __utee_check_out_annotation(b, sizeof(*b)); 1566915bbbbSJens Wiklander 1572c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 158b0104773SPascal Brand if (res != TEE_SUCCESS) 159a2e9a830SCedric Chaumont goto exit; 160b0104773SPascal Brand 161b0104773SPascal Brand /* This function only supports value attributes */ 162*b9416909SJens Wiklander if (!(attributeID & TEE_ATTR_FLAG_VALUE)) { 163a2e9a830SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 164a2e9a830SCedric Chaumont goto exit; 165a2e9a830SCedric Chaumont } 166b0104773SPascal Brand 1672c028fdeSJerome Forissier res = _utee_cryp_obj_get_attr((unsigned long)object, attributeID, buf, 168e86f1266SJens Wiklander &size); 169b0104773SPascal Brand 170a2e9a830SCedric Chaumont exit: 1710ed6a6caSCedric Chaumont if (res != TEE_SUCCESS && 1720ed6a6caSCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 1730ed6a6caSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 1740ed6a6caSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 175b36311adSJerome Forissier TEE_Panic(res); 176b0104773SPascal Brand 177b0104773SPascal Brand if (size != sizeof(buf)) 178b0104773SPascal Brand TEE_Panic(0); 179b0104773SPascal Brand 180be96c837SJerome Forissier if (res == TEE_SUCCESS) { 181b9d8f134SJerome Forissier if (a) 182b0104773SPascal Brand *a = buf[0]; 183b9d8f134SJerome Forissier if (b) 184b0104773SPascal Brand *b = buf[1]; 185be96c837SJerome Forissier } 186b0104773SPascal Brand 187b0104773SPascal Brand return res; 188b0104773SPascal Brand } 189b0104773SPascal Brand 190b0104773SPascal Brand void TEE_CloseObject(TEE_ObjectHandle object) 191b0104773SPascal Brand { 192b0104773SPascal Brand TEE_Result res; 193b0104773SPascal Brand 194b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 195b0104773SPascal Brand return; 196b0104773SPascal Brand 1972c028fdeSJerome Forissier res = _utee_cryp_obj_close((unsigned long)object); 198b0104773SPascal Brand if (res != TEE_SUCCESS) 199b36311adSJerome Forissier TEE_Panic(res); 200b0104773SPascal Brand } 201b0104773SPascal Brand 202b0104773SPascal Brand /* Data and Key Storage API - Transient Object Functions */ 203b0104773SPascal Brand 204b0104773SPascal Brand TEE_Result TEE_AllocateTransientObject(TEE_ObjectType objectType, 20579a3c601SCedric Chaumont uint32_t maxKeySize, 206b0104773SPascal Brand TEE_ObjectHandle *object) 207b0104773SPascal Brand { 208b0104773SPascal Brand TEE_Result res; 209b0104773SPascal Brand uint32_t obj; 210b0104773SPascal Brand 2116915bbbbSJens Wiklander __utee_check_out_annotation(object, sizeof(*object)); 2126915bbbbSJens Wiklander 2132c028fdeSJerome Forissier res = _utee_cryp_obj_alloc(objectType, maxKeySize, &obj); 214aeb0d927SCedric Chaumont 215aeb0d927SCedric Chaumont if (res != TEE_SUCCESS && 216aeb0d927SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 217aeb0d927SCedric Chaumont res != TEE_ERROR_NOT_SUPPORTED) 218b36311adSJerome Forissier TEE_Panic(res); 219aeb0d927SCedric Chaumont 220b0104773SPascal Brand if (res == TEE_SUCCESS) 221e86f1266SJens Wiklander *object = (TEE_ObjectHandle)(uintptr_t)obj; 2220ed6a6caSCedric Chaumont 223b0104773SPascal Brand return res; 224b0104773SPascal Brand } 225b0104773SPascal Brand 226b0104773SPascal Brand void TEE_FreeTransientObject(TEE_ObjectHandle object) 227b0104773SPascal Brand { 228b0104773SPascal Brand TEE_Result res; 229b0104773SPascal Brand TEE_ObjectInfo info; 230b0104773SPascal Brand 231b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 232b0104773SPascal Brand return; 233b0104773SPascal Brand 2342c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 235b0104773SPascal Brand if (res != TEE_SUCCESS) 236b36311adSJerome Forissier TEE_Panic(res); 237b0104773SPascal Brand 238b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 239b0104773SPascal Brand TEE_Panic(0); 240b0104773SPascal Brand 2412c028fdeSJerome Forissier res = _utee_cryp_obj_close((unsigned long)object); 242b0104773SPascal Brand if (res != TEE_SUCCESS) 243b36311adSJerome Forissier TEE_Panic(res); 244b0104773SPascal Brand } 245b0104773SPascal Brand 246b0104773SPascal Brand void TEE_ResetTransientObject(TEE_ObjectHandle object) 247b0104773SPascal Brand { 248b0104773SPascal Brand TEE_Result res; 249b0104773SPascal Brand TEE_ObjectInfo info; 250b0104773SPascal Brand 251b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 252b0104773SPascal Brand return; 253b0104773SPascal Brand 2542c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 255b0104773SPascal Brand if (res != TEE_SUCCESS) 256b36311adSJerome Forissier TEE_Panic(res); 257b0104773SPascal Brand 258b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 259b0104773SPascal Brand TEE_Panic(0); 260b0104773SPascal Brand 2612c028fdeSJerome Forissier res = _utee_cryp_obj_reset((unsigned long)object); 262b0104773SPascal Brand if (res != TEE_SUCCESS) 263b36311adSJerome Forissier TEE_Panic(res); 264b0104773SPascal Brand } 265b0104773SPascal Brand 266b0104773SPascal Brand TEE_Result TEE_PopulateTransientObject(TEE_ObjectHandle object, 2678f07fe6fSJerome Forissier const TEE_Attribute *attrs, 268b0104773SPascal Brand uint32_t attrCount) 269b0104773SPascal Brand { 270b0104773SPascal Brand TEE_Result res; 271b0104773SPascal Brand TEE_ObjectInfo info; 272e86f1266SJens Wiklander struct utee_attribute ua[attrCount]; 273b0104773SPascal Brand 2746915bbbbSJens Wiklander __utee_check_attr_in_annotation(attrs, attrCount); 2756915bbbbSJens Wiklander 2762c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 277b0104773SPascal Brand if (res != TEE_SUCCESS) 278b36311adSJerome Forissier TEE_Panic(res); 279b0104773SPascal Brand 280b0104773SPascal Brand /* Must be a transient object */ 281b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) 282b0104773SPascal Brand TEE_Panic(0); 283b0104773SPascal Brand 284b0104773SPascal Brand /* Must not be initialized already */ 285b0104773SPascal Brand if ((info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0) 286b0104773SPascal Brand TEE_Panic(0); 287b0104773SPascal Brand 288e86f1266SJens Wiklander __utee_from_attr(ua, attrs, attrCount); 2892c028fdeSJerome Forissier res = _utee_cryp_obj_populate((unsigned long)object, ua, attrCount); 290b0104773SPascal Brand if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 291b0104773SPascal Brand TEE_Panic(res); 292b0104773SPascal Brand return res; 293b0104773SPascal Brand } 294b0104773SPascal Brand 295b0104773SPascal Brand void TEE_InitRefAttribute(TEE_Attribute *attr, uint32_t attributeID, 2968f07fe6fSJerome Forissier const void *buffer, uint32_t length) 297b0104773SPascal Brand { 2986915bbbbSJens Wiklander __utee_check_out_annotation(attr, sizeof(*attr)); 2996915bbbbSJens Wiklander 300*b9416909SJens Wiklander if ((attributeID & TEE_ATTR_FLAG_VALUE) != 0) 301b0104773SPascal Brand TEE_Panic(0); 302b0104773SPascal Brand attr->attributeID = attributeID; 3038f07fe6fSJerome Forissier attr->content.ref.buffer = (void *)buffer; 304b0104773SPascal Brand attr->content.ref.length = length; 305b0104773SPascal Brand } 306b0104773SPascal Brand 307b0104773SPascal Brand void TEE_InitValueAttribute(TEE_Attribute *attr, uint32_t attributeID, 308b0104773SPascal Brand uint32_t a, uint32_t b) 309b0104773SPascal Brand { 3106915bbbbSJens Wiklander __utee_check_out_annotation(attr, sizeof(*attr)); 3116915bbbbSJens Wiklander 312*b9416909SJens Wiklander if ((attributeID & TEE_ATTR_FLAG_VALUE) == 0) 313b0104773SPascal Brand TEE_Panic(0); 314b0104773SPascal Brand attr->attributeID = attributeID; 315b0104773SPascal Brand attr->content.value.a = a; 316b0104773SPascal Brand attr->content.value.b = b; 317b0104773SPascal Brand } 318b0104773SPascal Brand 3197583c59eSCedric Chaumont /* 3207583c59eSCedric Chaumont * Use of this function is deprecated 3217583c59eSCedric Chaumont * new code SHOULD use the TEE_CopyObjectAttributes1 function instead 3227583c59eSCedric Chaumont * These functions will be removed at some future major revision of 3237583c59eSCedric Chaumont * this specification 3247583c59eSCedric Chaumont */ 325b0104773SPascal Brand void TEE_CopyObjectAttributes(TEE_ObjectHandle destObject, 326b0104773SPascal Brand TEE_ObjectHandle srcObject) 327b0104773SPascal Brand { 328b0104773SPascal Brand TEE_Result res; 3297583c59eSCedric Chaumont TEE_ObjectInfo src_info; 3307583c59eSCedric Chaumont 3312c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info); 3327583c59eSCedric Chaumont if (src_info.objectType == TEE_TYPE_CORRUPTED_OBJECT) 3337583c59eSCedric Chaumont return; 3347583c59eSCedric Chaumont 3357583c59eSCedric Chaumont res = TEE_CopyObjectAttributes1(destObject, srcObject); 3367583c59eSCedric Chaumont if (res != TEE_SUCCESS) 337b36311adSJerome Forissier TEE_Panic(res); 3387583c59eSCedric Chaumont } 3397583c59eSCedric Chaumont 3407583c59eSCedric Chaumont TEE_Result TEE_CopyObjectAttributes1(TEE_ObjectHandle destObject, 3417583c59eSCedric Chaumont TEE_ObjectHandle srcObject) 3427583c59eSCedric Chaumont { 3437583c59eSCedric Chaumont TEE_Result res; 344b0104773SPascal Brand TEE_ObjectInfo dst_info; 345b0104773SPascal Brand TEE_ObjectInfo src_info; 346b0104773SPascal Brand 3472c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)destObject, &dst_info); 348b0104773SPascal Brand if (res != TEE_SUCCESS) 349a2e9a830SCedric Chaumont goto exit; 350b0104773SPascal Brand 3512c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)srcObject, &src_info); 352b0104773SPascal Brand if (res != TEE_SUCCESS) 353a2e9a830SCedric Chaumont goto exit; 354b0104773SPascal Brand 355a2e9a830SCedric Chaumont if (!(src_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 356b0104773SPascal Brand TEE_Panic(0); 357a2e9a830SCedric Chaumont 358a2e9a830SCedric Chaumont if ((dst_info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) 359b0104773SPascal Brand TEE_Panic(0); 360a2e9a830SCedric Chaumont 361a2e9a830SCedric Chaumont if ((dst_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) 362b0104773SPascal Brand TEE_Panic(0); 363b0104773SPascal Brand 3642c028fdeSJerome Forissier res = _utee_cryp_obj_copy((unsigned long)destObject, 365e86f1266SJens Wiklander (unsigned long)srcObject); 3667583c59eSCedric Chaumont 367a2e9a830SCedric Chaumont exit: 368a2e9a830SCedric Chaumont if (res != TEE_SUCCESS && 369a2e9a830SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 370a2e9a830SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 371a2e9a830SCedric Chaumont TEE_Panic(res); 3727583c59eSCedric Chaumont 3737583c59eSCedric Chaumont return res; 374b0104773SPascal Brand } 375b0104773SPascal Brand 376b0104773SPascal Brand TEE_Result TEE_GenerateKey(TEE_ObjectHandle object, uint32_t keySize, 3778f07fe6fSJerome Forissier const TEE_Attribute *params, uint32_t paramCount) 378b0104773SPascal Brand { 379b0104773SPascal Brand TEE_Result res; 380e86f1266SJens Wiklander struct utee_attribute ua[paramCount]; 381b0104773SPascal Brand 3826915bbbbSJens Wiklander __utee_check_attr_in_annotation(params, paramCount); 3836915bbbbSJens Wiklander 384e86f1266SJens Wiklander __utee_from_attr(ua, params, paramCount); 3852c028fdeSJerome Forissier res = _utee_cryp_obj_generate_key((unsigned long)object, keySize, 386e86f1266SJens Wiklander ua, paramCount); 387b0104773SPascal Brand 388aeb0d927SCedric Chaumont if (res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS) 389b36311adSJerome Forissier TEE_Panic(res); 390b0104773SPascal Brand 391b0104773SPascal Brand return res; 392b0104773SPascal Brand } 393b0104773SPascal Brand 394b0104773SPascal Brand /* Data and Key Storage API - Persistent Object Functions */ 395b0104773SPascal Brand 3968f07fe6fSJerome Forissier TEE_Result TEE_OpenPersistentObject(uint32_t storageID, const void *objectID, 39779a3c601SCedric Chaumont uint32_t objectIDLen, uint32_t flags, 398b0104773SPascal Brand TEE_ObjectHandle *object) 399b0104773SPascal Brand { 4009b520646SCedric Chaumont TEE_Result res; 401e86f1266SJens Wiklander uint32_t obj; 402b0104773SPascal Brand 4039b520646SCedric Chaumont if (!objectID) { 4049b520646SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 405172d637bSCedric Auger goto exit; 4069b520646SCedric Chaumont } 4079b520646SCedric Chaumont 4082c028fdeSJerome Forissier res = _utee_storage_obj_open(storageID, objectID, objectIDLen, flags, 409e86f1266SJens Wiklander &obj); 410e86f1266SJens Wiklander if (res == TEE_SUCCESS) 411e86f1266SJens Wiklander *object = (TEE_ObjectHandle)(uintptr_t)obj; 4129b520646SCedric Chaumont 413172d637bSCedric Auger exit: 4149b520646SCedric Chaumont if (res != TEE_SUCCESS && 4159b520646SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 4169b520646SCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT && 4179b520646SCedric Chaumont res != TEE_ERROR_OUT_OF_MEMORY && 4189b520646SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 4199b520646SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 420b36311adSJerome Forissier TEE_Panic(res); 421b0104773SPascal Brand 422172d637bSCedric Auger if (res != TEE_SUCCESS) 42369a3d6beSDaniel Glöckner *object = TEE_HANDLE_NULL; 42469a3d6beSDaniel Glöckner 4259b520646SCedric Chaumont return res; 426b0104773SPascal Brand } 427b0104773SPascal Brand 4288f07fe6fSJerome Forissier TEE_Result TEE_CreatePersistentObject(uint32_t storageID, const void *objectID, 42979a3c601SCedric Chaumont uint32_t objectIDLen, uint32_t flags, 430b0104773SPascal Brand TEE_ObjectHandle attributes, 431b0104773SPascal Brand const void *initialData, 43279a3c601SCedric Chaumont uint32_t initialDataLen, 433b0104773SPascal Brand TEE_ObjectHandle *object) 434b0104773SPascal Brand { 43584431ae3SCedric Chaumont TEE_Result res; 436e86f1266SJens Wiklander uint32_t obj; 437b0104773SPascal Brand 438aeb0d927SCedric Chaumont if (!objectID) { 43984431ae3SCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 440172d637bSCedric Auger goto exit; 44184431ae3SCedric Chaumont } 442b0104773SPascal Brand 4436915bbbbSJens Wiklander __utee_check_out_annotation(object, sizeof(*object)); 444b0104773SPascal Brand 4452c028fdeSJerome Forissier res = _utee_storage_obj_create(storageID, objectID, objectIDLen, flags, 446e86f1266SJens Wiklander (unsigned long)attributes, initialData, 447e86f1266SJens Wiklander initialDataLen, &obj); 448172d637bSCedric Auger 4491c96fa7fSPascal Brand if (res == TEE_SUCCESS) 450172d637bSCedric Auger *object = (TEE_ObjectHandle)(uintptr_t)obj; 451172d637bSCedric Auger 452172d637bSCedric Auger exit: 453172d637bSCedric Auger if (res != TEE_SUCCESS && 454172d637bSCedric Auger res != TEE_ERROR_ITEM_NOT_FOUND && 455172d637bSCedric Auger res != TEE_ERROR_ACCESS_CONFLICT && 456172d637bSCedric Auger res != TEE_ERROR_OUT_OF_MEMORY && 457172d637bSCedric Auger res != TEE_ERROR_STORAGE_NO_SPACE && 458172d637bSCedric Auger res != TEE_ERROR_CORRUPT_OBJECT && 459172d637bSCedric Auger res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 460b36311adSJerome Forissier TEE_Panic(res); 461172d637bSCedric Auger 462172d637bSCedric Auger if (res != TEE_SUCCESS) 463172d637bSCedric Auger *object = TEE_HANDLE_NULL; 464172d637bSCedric Auger 465172d637bSCedric Auger return res; 466b0104773SPascal Brand } 467b0104773SPascal Brand 4687583c59eSCedric Chaumont /* 4697583c59eSCedric Chaumont * Use of this function is deprecated 4707583c59eSCedric Chaumont * new code SHOULD use the TEE_CloseAndDeletePersistentObject1 function instead 4717583c59eSCedric Chaumont * These functions will be removed at some future major revision of 4727583c59eSCedric Chaumont * this specification 4737583c59eSCedric Chaumont */ 474b0104773SPascal Brand void TEE_CloseAndDeletePersistentObject(TEE_ObjectHandle object) 475b0104773SPascal Brand { 476b0104773SPascal Brand TEE_Result res; 477b0104773SPascal Brand 478b0104773SPascal Brand if (object == TEE_HANDLE_NULL) 479b0104773SPascal Brand return; 480b0104773SPascal Brand 4817583c59eSCedric Chaumont res = TEE_CloseAndDeletePersistentObject1(object); 482b0104773SPascal Brand 483b0104773SPascal Brand if (res != TEE_SUCCESS) 484b0104773SPascal Brand TEE_Panic(0); 485b0104773SPascal Brand } 486b0104773SPascal Brand 4877583c59eSCedric Chaumont TEE_Result TEE_CloseAndDeletePersistentObject1(TEE_ObjectHandle object) 4887583c59eSCedric Chaumont { 4897583c59eSCedric Chaumont TEE_Result res; 4907583c59eSCedric Chaumont 4917583c59eSCedric Chaumont if (object == TEE_HANDLE_NULL) 49246cfd17cSJens Wiklander return TEE_SUCCESS; 4937583c59eSCedric Chaumont 4942c028fdeSJerome Forissier res = _utee_storage_obj_del((unsigned long)object); 4957583c59eSCedric Chaumont 4967583c59eSCedric Chaumont if (res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 497b36311adSJerome Forissier TEE_Panic(res); 4987583c59eSCedric Chaumont 4997583c59eSCedric Chaumont return res; 5007583c59eSCedric Chaumont } 5017583c59eSCedric Chaumont 5027583c59eSCedric Chaumont 503b0104773SPascal Brand TEE_Result TEE_RenamePersistentObject(TEE_ObjectHandle object, 504b0104773SPascal Brand const void *newObjectID, 50579a3c601SCedric Chaumont uint32_t newObjectIDLen) 506b0104773SPascal Brand { 507b0104773SPascal Brand TEE_Result res; 508b0104773SPascal Brand 509a76bf53fSCedric Chaumont if (object == TEE_HANDLE_NULL) { 510a76bf53fSCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 511a76bf53fSCedric Chaumont goto out; 512a76bf53fSCedric Chaumont } 513b0104773SPascal Brand 5142c028fdeSJerome Forissier res = _utee_storage_obj_rename((unsigned long)object, newObjectID, 515e86f1266SJens Wiklander newObjectIDLen); 516b0104773SPascal Brand 517a76bf53fSCedric Chaumont out: 518a76bf53fSCedric Chaumont if (res != TEE_SUCCESS && 519a76bf53fSCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT && 520a76bf53fSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 521a76bf53fSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 522b36311adSJerome Forissier TEE_Panic(res); 523b0104773SPascal Brand 524b0104773SPascal Brand return res; 525b0104773SPascal Brand } 526b0104773SPascal Brand 527b0104773SPascal Brand TEE_Result TEE_AllocatePersistentObjectEnumerator(TEE_ObjectEnumHandle * 528b0104773SPascal Brand objectEnumerator) 529b0104773SPascal Brand { 530b0104773SPascal Brand TEE_Result res; 531e86f1266SJens Wiklander uint32_t oe; 532b0104773SPascal Brand 5336915bbbbSJens Wiklander __utee_check_out_annotation(objectEnumerator, 5346915bbbbSJens Wiklander sizeof(*objectEnumerator)); 535b0104773SPascal Brand 5362c028fdeSJerome Forissier res = _utee_storage_alloc_enum(&oe); 537b0104773SPascal Brand 538b0104773SPascal Brand if (res != TEE_SUCCESS) 539e86f1266SJens Wiklander oe = TEE_HANDLE_NULL; 540e86f1266SJens Wiklander 541e86f1266SJens Wiklander *objectEnumerator = (TEE_ObjectEnumHandle)(uintptr_t)oe; 542b0104773SPascal Brand 54315cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 54415cd3c30SCedric Chaumont res != TEE_ERROR_ACCESS_CONFLICT) 545b36311adSJerome Forissier TEE_Panic(res); 54615cd3c30SCedric Chaumont 547b0104773SPascal Brand return res; 548b0104773SPascal Brand } 549b0104773SPascal Brand 550b0104773SPascal Brand void TEE_FreePersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 551b0104773SPascal Brand { 552b0104773SPascal Brand TEE_Result res; 553b0104773SPascal Brand 554b0104773SPascal Brand if (objectEnumerator == TEE_HANDLE_NULL) 555b0104773SPascal Brand return; 556b0104773SPascal Brand 5572c028fdeSJerome Forissier res = _utee_storage_free_enum((unsigned long)objectEnumerator); 558b0104773SPascal Brand 559b0104773SPascal Brand if (res != TEE_SUCCESS) 560b36311adSJerome Forissier TEE_Panic(res); 561b0104773SPascal Brand } 562b0104773SPascal Brand 563b0104773SPascal Brand void TEE_ResetPersistentObjectEnumerator(TEE_ObjectEnumHandle objectEnumerator) 564b0104773SPascal Brand { 565b0104773SPascal Brand TEE_Result res; 566b0104773SPascal Brand 567b0104773SPascal Brand if (objectEnumerator == TEE_HANDLE_NULL) 568b0104773SPascal Brand return; 569b0104773SPascal Brand 5702c028fdeSJerome Forissier res = _utee_storage_reset_enum((unsigned long)objectEnumerator); 571b0104773SPascal Brand 572b0104773SPascal Brand if (res != TEE_SUCCESS) 573b36311adSJerome Forissier TEE_Panic(res); 574b0104773SPascal Brand } 575b0104773SPascal Brand 576b0104773SPascal Brand TEE_Result TEE_StartPersistentObjectEnumerator(TEE_ObjectEnumHandle 577b0104773SPascal Brand objectEnumerator, 578b0104773SPascal Brand uint32_t storageID) 579b0104773SPascal Brand { 580b0104773SPascal Brand TEE_Result res; 581b0104773SPascal Brand 5822c028fdeSJerome Forissier res = _utee_storage_start_enum((unsigned long)objectEnumerator, 583e86f1266SJens Wiklander storageID); 584b0104773SPascal Brand 58515cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 58615cd3c30SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 58715cd3c30SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 58815cd3c30SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 589b36311adSJerome Forissier TEE_Panic(res); 590b0104773SPascal Brand 591b0104773SPascal Brand return res; 592b0104773SPascal Brand } 593b0104773SPascal Brand 594b0104773SPascal Brand TEE_Result TEE_GetNextPersistentObject(TEE_ObjectEnumHandle objectEnumerator, 595b0104773SPascal Brand TEE_ObjectInfo *objectInfo, 59679a3c601SCedric Chaumont void *objectID, uint32_t *objectIDLen) 597b0104773SPascal Brand { 598b0104773SPascal Brand TEE_Result res; 599e86f1266SJens Wiklander uint64_t len; 6002342799fSPascal Brand TEE_ObjectInfo local_info; 6012342799fSPascal Brand TEE_ObjectInfo *pt_info; 602b0104773SPascal Brand 6036915bbbbSJens Wiklander if (objectInfo) 6046915bbbbSJens Wiklander __utee_check_out_annotation(objectInfo, sizeof(*objectInfo)); 6056915bbbbSJens Wiklander __utee_check_out_annotation(objectIDLen, sizeof(*objectIDLen)); 60615cd3c30SCedric Chaumont 6076915bbbbSJens Wiklander if (!objectID) { 60815cd3c30SCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 60915cd3c30SCedric Chaumont goto out; 61015cd3c30SCedric Chaumont } 61115cd3c30SCedric Chaumont 6122342799fSPascal Brand if (objectInfo) 6132342799fSPascal Brand pt_info = objectInfo; 6142342799fSPascal Brand else 6152342799fSPascal Brand pt_info = &local_info; 616e86f1266SJens Wiklander len = *objectIDLen; 6172c028fdeSJerome Forissier res = _utee_storage_next_enum((unsigned long)objectEnumerator, 6182342799fSPascal Brand pt_info, objectID, &len); 619e86f1266SJens Wiklander *objectIDLen = len; 620b0104773SPascal Brand 62115cd3c30SCedric Chaumont out: 62215cd3c30SCedric Chaumont if (res != TEE_SUCCESS && 62315cd3c30SCedric Chaumont res != TEE_ERROR_ITEM_NOT_FOUND && 62415cd3c30SCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 62515cd3c30SCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 626b36311adSJerome Forissier TEE_Panic(res); 627b0104773SPascal Brand 628b0104773SPascal Brand return res; 629b0104773SPascal Brand } 630b0104773SPascal Brand 631b0104773SPascal Brand /* Data and Key Storage API - Data Stream Access Functions */ 632b0104773SPascal Brand 633b0104773SPascal Brand TEE_Result TEE_ReadObjectData(TEE_ObjectHandle object, void *buffer, 63479a3c601SCedric Chaumont uint32_t size, uint32_t *count) 635b0104773SPascal Brand { 636b0104773SPascal Brand TEE_Result res; 637e86f1266SJens Wiklander uint64_t cnt64; 638b0104773SPascal Brand 639ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 640ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 641ae1289baSCedric Chaumont goto out; 642ae1289baSCedric Chaumont } 6436915bbbbSJens Wiklander __utee_check_out_annotation(count, sizeof(*count)); 644b0104773SPascal Brand 645e86f1266SJens Wiklander cnt64 = *count; 6462c028fdeSJerome Forissier res = _utee_storage_obj_read((unsigned long)object, buffer, size, 647e86f1266SJens Wiklander &cnt64); 648e86f1266SJens Wiklander *count = cnt64; 649b0104773SPascal Brand 650ae1289baSCedric Chaumont out: 651ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 652ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 653ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 654b36311adSJerome Forissier TEE_Panic(res); 655b0104773SPascal Brand 656b0104773SPascal Brand return res; 657b0104773SPascal Brand } 658b0104773SPascal Brand 6598f07fe6fSJerome Forissier TEE_Result TEE_WriteObjectData(TEE_ObjectHandle object, const void *buffer, 66079a3c601SCedric Chaumont uint32_t size) 661b0104773SPascal Brand { 662b0104773SPascal Brand TEE_Result res; 663b0104773SPascal Brand 664ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 665ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 666ae1289baSCedric Chaumont goto out; 667ae1289baSCedric Chaumont } 668ae1289baSCedric Chaumont 669ae1289baSCedric Chaumont if (size > TEE_DATA_MAX_POSITION) { 670ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 671ae1289baSCedric Chaumont goto out; 672ae1289baSCedric Chaumont } 673b0104773SPascal Brand 6742c028fdeSJerome Forissier res = _utee_storage_obj_write((unsigned long)object, buffer, size); 675b0104773SPascal Brand 676ae1289baSCedric Chaumont out: 677ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 678ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NO_SPACE && 679ae1289baSCedric Chaumont res != TEE_ERROR_OVERFLOW && 680ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 681ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 682b36311adSJerome Forissier TEE_Panic(res); 683b0104773SPascal Brand 684b0104773SPascal Brand return res; 685b0104773SPascal Brand } 686b0104773SPascal Brand 687b0104773SPascal Brand TEE_Result TEE_TruncateObjectData(TEE_ObjectHandle object, uint32_t size) 688b0104773SPascal Brand { 689b0104773SPascal Brand TEE_Result res; 690b0104773SPascal Brand 691ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 692ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 693ae1289baSCedric Chaumont goto out; 694ae1289baSCedric Chaumont } 695b0104773SPascal Brand 6962c028fdeSJerome Forissier res = _utee_storage_obj_trunc((unsigned long)object, size); 697b0104773SPascal Brand 698ae1289baSCedric Chaumont out: 699ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 700ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NO_SPACE && 701ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 702ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 703b36311adSJerome Forissier TEE_Panic(res); 704b0104773SPascal Brand 705b0104773SPascal Brand return res; 706b0104773SPascal Brand } 707b0104773SPascal Brand 708b0104773SPascal Brand TEE_Result TEE_SeekObjectData(TEE_ObjectHandle object, int32_t offset, 709b0104773SPascal Brand TEE_Whence whence) 710b0104773SPascal Brand { 711b0104773SPascal Brand TEE_Result res; 712b0104773SPascal Brand TEE_ObjectInfo info; 713b0104773SPascal Brand 714ae1289baSCedric Chaumont if (object == TEE_HANDLE_NULL) { 715ae1289baSCedric Chaumont res = TEE_ERROR_BAD_PARAMETERS; 716ae1289baSCedric Chaumont goto out; 717ae1289baSCedric Chaumont } 718b0104773SPascal Brand 7192c028fdeSJerome Forissier res = _utee_cryp_obj_get_info((unsigned long)object, &info); 720b0104773SPascal Brand if (res != TEE_SUCCESS) 721ae1289baSCedric Chaumont goto out; 722b0104773SPascal Brand 723b0104773SPascal Brand switch (whence) { 724b0104773SPascal Brand case TEE_DATA_SEEK_SET: 725ae1289baSCedric Chaumont if (offset > 0 && (uint32_t)offset > TEE_DATA_MAX_POSITION) { 726ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 727ae1289baSCedric Chaumont goto out; 728ae1289baSCedric Chaumont } 729b0104773SPascal Brand break; 730b0104773SPascal Brand case TEE_DATA_SEEK_CUR: 731b0104773SPascal Brand if (offset > 0 && 732b0104773SPascal Brand ((uint32_t)offset + info.dataPosition > 733b0104773SPascal Brand TEE_DATA_MAX_POSITION || 734b0104773SPascal Brand (uint32_t)offset + info.dataPosition < 735ae1289baSCedric Chaumont info.dataPosition)) { 736ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 737ae1289baSCedric Chaumont goto out; 738ae1289baSCedric Chaumont } 739b0104773SPascal Brand break; 740b0104773SPascal Brand case TEE_DATA_SEEK_END: 741b0104773SPascal Brand if (offset > 0 && 742b0104773SPascal Brand ((uint32_t)offset + info.dataSize > TEE_DATA_MAX_POSITION || 743ae1289baSCedric Chaumont (uint32_t)offset + info.dataSize < info.dataSize)) { 744ae1289baSCedric Chaumont res = TEE_ERROR_OVERFLOW; 745ae1289baSCedric Chaumont goto out; 746ae1289baSCedric Chaumont } 747b0104773SPascal Brand break; 748b0104773SPascal Brand default: 749ae1289baSCedric Chaumont res = TEE_ERROR_ITEM_NOT_FOUND; 750ae1289baSCedric Chaumont goto out; 751b0104773SPascal Brand } 752b0104773SPascal Brand 7532c028fdeSJerome Forissier res = _utee_storage_obj_seek((unsigned long)object, offset, whence); 754b0104773SPascal Brand 755ae1289baSCedric Chaumont out: 756ae1289baSCedric Chaumont if (res != TEE_SUCCESS && 757ae1289baSCedric Chaumont res != TEE_ERROR_OVERFLOW && 758ae1289baSCedric Chaumont res != TEE_ERROR_CORRUPT_OBJECT && 759ae1289baSCedric Chaumont res != TEE_ERROR_STORAGE_NOT_AVAILABLE) 760b36311adSJerome Forissier TEE_Panic(res); 761b0104773SPascal Brand 762b0104773SPascal Brand return res; 763b0104773SPascal Brand } 764