1########################################### 2Protected Storage Service Integration Guide 3########################################### 4 5************ 6Introduction 7************ 8TF-M Protected Storage (PS) service implements PSA Protected Storage APIs. 9 10The service is usually backed by hardware isolation of the flash 11access domain and, in the current version, relies on hardware to 12isolate the flash area from non-secure access. In absence of hardware 13isolation, the secrecy and integrity of data is still maintained. 14 15The PS service implements an AES-GCM based AEAD encryption policy, as a 16reference, to protect data integrity and authenticity. 17 18The PS reuses the non-hierarchical filesystem provided by the TF-M 19Internal Trusted Storage service to store encrypted, authenticated 20objects. 21 22The design addresses the following high level requirements as well: 23 24- **Confidentiality** - Resistance to unauthorised accesses through 25 hardware/software attacks. 26 27- **Access Authentication** - Mechanism to establish requester's identity (a 28 non-secure entity, secure entity, or a remote server). 29 30- **Integrity** - Resistant to tampering by either the normal users of a 31 product, package, or system or others with physical access to it. If the 32 content of the protected storage is changed maliciously, the service is able 33 to detect it. 34 35- **Reliability** - Resistant to power failure scenarios and incomplete write 36 cycles. 37 38- **Configurability** - High level configurability to scale up/down memory 39 footprint to cater for a variety of devices with varying security 40 requirements. 41 42- **Performance** - Optimized to be used for resource constrained devices with 43 very small silicon footprint, the PPA (power, performance, area) should be 44 optimal. 45 46****************************** 47Current PS Service Limitations 48****************************** 49- **Asset size limitation** - An asset is stored in a contiguous space in a 50 block/sector. Hence, the maximum asset size can be up-to the size of the 51 data block/sector. Detailed information about the maximum asset size can be 52 found in the section `Maximum asset size` below. 53 54- **Fragmentation** - The current design does not support fragmentation, as an 55 asset is stored in a contiguous space in a block. 56 Each block can potentially store multiple assets. 57 A delete operation implicitly moves all the assets towards the top of the 58 block to avoid fragmentation within block. However, this may also result in 59 unutilized space at the end of each block. 60 61- **Non-hierarchical storage model** - The current design uses a 62 non-hierarchical storage model, as a filesystem, where all the assets are 63 managed by a linearly indexed list of metadata. This model locates the 64 metadata in blocks which are always stored in the same flash location. That 65 increases the number of writes in a specific flash location as every change in 66 the storage area requires a metadata update. 67 68- **PSA internal trusted storage API** - In the current design, the service does 69 not use the PSA Internal Trusted Storage API to write the rollback protection 70 values stored in the internal storage. 71 72- **Protection against physical storage medium failure** - Complete handling of 73 inherent failures of storage mediums (e.g. bad blocks in a NAND based device) 74 is not supported by the current design. 75 76- **Key diversification** - In a more robust design, each asset would be 77 encrypted through a different key. 78 79- **Lifecycle management** - Currently, it does not support any subscription 80 based keys and certificates required in a secure lifecycle management. Hence, 81 an asset's validity time-stamp can not be invalidated based on the system 82 time. 83 84- **Provisioning vs user/device data** - In the current design, all assets are 85 treated in the same manner. In an alternative design, it may be required to 86 create separate partitions for provisioning content and user/device generated 87 content. This is to allow safe update of provisioning data during firmware 88 updates without the need to wipe out the user/device generated data. 89 90************** 91Code Structure 92************** 93Protected storage service code is located in 94``secure_fw/partitions/protected_storage/`` and is divided as follows: 95 96 - Core files 97 - Cryptographic interfaces 98 - Non-volatile (NV) counters interfaces 99 100The PSA PS interfaces for PS service are located in ``interface/include/psa`` 101 102PSA Protected Storage Interfaces 103================================ 104 105The PS service exposes the following mandatory PSA PS interfaces, version 1.0: 106 107.. code-block:: c 108 109 psa_status_t psa_ps_set(psa_storage_uid_t uid, size_t data_length, const void *p_data, psa_storage_create_flags_t create_flags); 110 psa_status_t psa_ps_get(psa_storage_uid_t uid, size_t data_offset, size_t data_size, void *p_data, size_t *p_data_length); 111 psa_status_t psa_ps_get_info(psa_storage_uid_t uid, struct psa_storage_info_t *p_info); 112 psa_status_t psa_ps_remove(psa_storage_uid_t uid); 113 uint32_t psa_ps_get_support(void); 114 115For the moment, it does not support the extended version of those APIs. 116 117These PSA PS interfaces and PS TF-M types are defined and documented in 118``interface/include/psa/protected_storage.h``, 119``interface/include/psa/storage_common.h`` and 120``interface/include/tfm_ps_defs.h`` 121 122Core Files 123========== 124- ``tfm_ps_req_mngr.c`` - Contains the PS request manager implementation which 125 handles all requests which arrive to the service. This layer extracts the 126 arguments from the input and output vectors, and it calls the protected 127 storage layer with the provided parameters. 128 129- ``tfm_protected_storage.c`` - Contains the TF-M protected storage API 130 implementations which are the entry points to the PS service. 131 132- ``ps_object_system.c`` - Contains the object system implementation to manage 133 all objects in PS area. 134 135- ``ps_object_table.c`` - Contains the object system table implementation which 136 complements the object system to manage all object in the PS area. 137 The object table has an entry for each object stored in the object system 138 and keeps track of its version and owner. 139 140- ``ps_encrypted_object.c`` - Contains an implementation to manipulate 141 encrypted objects in the PS object system. 142 143- ``ps_utils.c`` - Contains common and basic functionalities used across the 144 PS service code. 145 146- ``ps_filesystem_interface.c`` - Contains the interface to directly use ITS 147 implementation of services. 148 149Flash Filesystem and Flash Interfaces 150===================================== 151 152The non-hierarchical filesystem and flash interfaces reside under 153Internal Trusted Storage service directory. The way PS service uses 154them depends on the config option ``TFM_PARTITION_INTERNAL_TRUSTED_STORAGE``. 155 156When TF-M Internal trusted storage service is active (i.e. 157``TFM_PARTITION_INTERNAL_TRUSTED_STORAGE`` = ON), all file system and storage 158interfaces are built as part of ITS service library. Thus, the PS service stores 159encrypted, authenticated objects by making service calls to the ITS service. 160 161When TF-M Internal trusted storage service is disabled (i.e. 162``TFM_PARTITION_INTERNAL_TRUSTED_STORAGE`` = OFF), all file system and storage 163interfaces are built as part of PS service library. Thus, the PS service stores 164encrypted, authenticated objects by making standard function calls to the file 165system within its own partition code. 166 167The ITS filesystem and flash interfaces and their implementation can be found in 168``secure_fw/partitions/internal_trusted_storage/flash_fs`` and 169``secure_fw/partitions/internal_trusted_storage/flash`` respectively. More 170information about the filesystem and flash interfaces can be found in the 171:doc:`ITS integration guide 172</integration_guide/services/tfm_its_integration_guide>`. 173 174The ITS service implementation in 175``secure_fw/partitions/internal_trusted_storage/tfm_internal_trusted_storage.c``, 176constructs a filesystem configuration for Protected Storage based on 177target-specific definitions from the Protected Storage HAL. Please see the 178`Protected Storage Service HAL` section for details of these. 179 180Cryptographic Interface 181======================= 182- ``crypto/ps_crypto_interface.h`` - Abstracts the cryptographic operations for 183 the protected storage service. 184 185- ``crypto/ps_crypto_interface.c`` - Implements the PS service cryptographic 186 operations with calls to the TF-M Crypto service. 187 188Non-volatile (NV) Counters Interface 189==================================== 190The current PS service provides rollback protection based on NV 191counters. 192PS defines and implements the following NV counters functionalities: 193 194- ``nv_counters/ps_nv_counters.h`` - Abstracts PS non-volatile 195 counters operations. This API detaches the use of NV counters from the TF-M NV 196 counters implementation, provided by the platform, and provides a mechanism to 197 compile in a different API implementation for test purposes. A PS test suite 198 **may** provide its own custom implementation to be able to test different PS 199 service use cases. 200 201- ``nv_counters/ps_nv_counters.c`` - Implements the PS NV counters interfaces 202 based on TF-M NV counters implementation provided by the platform. 203 204**************************** 205PS Service Integration Guide 206**************************** 207This section describes mandatory (i.e. **must** implement) or optional 208(i.e. **may** implement) interfaces which the system integrator have to take 209in to account in order to integrate the protected storage service in a new 210platform. 211 212Maximum Asset Size 213================== 214An asset is stored in a contiguous space in a block/sector. The maximum 215size of an asset can be up-to the size of the data block/sector minus the object 216header size (``PS_OBJECT_HEADER_SIZE``) which is defined in 217``ps_object_defs.h``. The ``PS_OBJECT_HEADER_SIZE`` changes based on the 218``PS_ENCRYPTION`` flag status. 219 220Protected Storage Service HAL 221============================= 222The PS service requires the platform to implement the PS HAL, defined in 223``platform/include/tfm_hal_ps.h``. 224 225The following C definitions in the HAL are mandatory, and must be defined by the 226platform in a header named ``flash_layout.h``: 227 228- ``TFM_HAL_PS_FLASH_DRIVER`` - Defines the identifier of the CMSIS Flash 229 ARM_DRIVER_FLASH object to use for PS. It must have been allocated by the 230 platform and will be declared extern in the HAL header. 231 232- ``TFM_HAL_PS_PROGRAM_UNIT`` - Defines the size of the PS flash device's 233 physical program unit (the smallest unit of data that can be individually 234 programmed to flash). It must be equal to 235 ``TFM_HAL_PS_FLASH_DRIVER.GetInfo()->program_unit``, but made available at 236 compile time so that filesystem structures can be statically sized. Valid 237 values are powers of two between 1 and the flash sector size, inclusive. 238 239The following C definitions in the HAL may optionally be defined by the platform 240in the ``flash_layout.h`` header: 241 242- ``TFM_HAL_PS_FLASH_AREA_ADDR`` - Defines the base address of the dedicated 243 flash area for PS. 244 245- ``TFM_HAL_PS_FLASH_AREA_SIZE`` - Defines the size of the dedicated flash area 246 for PS in bytes. 247 248- ``TFM_HAL_PS_SECTORS_PER_BLOCK`` - Defines the number of contiguous physical 249 flash erase sectors that form a logical erase block in the filesystem. The 250 typical value is ``1``, but it may be increased so that the maximum required 251 asset size will fit in one logical block. 252 253If any of the above definitions are not provided by the platform, then the 254``tfm_hal_ps_fs_info()`` HAL API must be implemented instead. This function is 255documented in ``tfm_hal_ps.h``. 256 257The sectors reserved to be used for Protected Storage **must** be contiguous 258sectors starting at ``TFM_HAL_PS_FLASH_AREA_ADDR``. 259 260The design requires either 2 blocks, or any number of blocks greater than or 261equal to 4. Total number of blocks can not be 0, 1 or 3. This is a design choice 262limitation to provide power failure safe update operations. 263 264Protected Storage Service Optional Platform Definitions 265======================================================= 266The following optional platform definitions may be defined in 267``flash_layout.h``: 268 269- ``PS_RAM_FS_SIZE`` - Defines the size of the RAM FS buffer when using the 270 RAM FS emulated flash implementation. The buffer must be at least as large as 271 the area earmarked for the filesystem by the HAL. 272- ``PS_FLASH_NAND_BUF_SIZE`` - Defines the size of the write buffer when using 273 the NAND flash implementation. The buffer must be at least as large as a 274 logical filesystem block. 275 276More information about the ``flash_layout.h`` content, not ITS related, is 277available in :ref:`platform_ext_folder` along with other 278platform information. 279 280TF-M NV Counter Interface 281========================= 282To have a platform independent way to access the NV counters, TF-M defines a 283platform NV counter interface. For API specification, please check: 284``platform/include/tfm_plat_nv_counters.h`` 285 286The system integrators **may** implement this interface based on the target 287capabilities and set the ``PS_ROLLBACK_PROTECTION`` flag to compile in 288the rollback protection code. 289 290 .. Note:: 291 If this flag is enabled, the lifecycle of the PS service depends on the 292 shorter write endurance of the assets storage device and the NV counters 293 storage device. 294 295Secret Platform Unique Key 296========================== 297The encryption policy relies on a secret hardware unique key (HUK) per device. 298It is system integrator's responsibility to provide an implementation which 299**must** be a non-mutable target implementation. 300For API specification, please check: 301``platform/include/tfm_plat_crypto_keys.h`` 302 303A stub implementation is provided in 304``platform/ext/common/template/crypto_keys.c`` 305 306Non-Secure Identity Manager 307=========================== 308TF-M core tracks the current client IDs running in the secure or non-secure 309processing environment. It provides a dedicated API to retrieve the client ID 310which performs the service request. 311 312:doc:`Non-secure Client Extension Integration Guide </integration_guide/non-secure_client_extension_integration_guide>` 313provides further details on how client identification works. 314 315PS service uses that TF-M core API to retrieve the client ID and associate it 316as the owner of an asset. Only the owner can read, write or delete that asset 317based on the creation flags. 318 319The :doc:`integration guide </integration_guide/index>` 320provides further details of non-secure implementation requirements for TF-M. 321 322Cryptographic Interface 323======================= 324The reference encryption policy is built on AES-GCM, and it **may** be replaced 325by a vendor specific implementation. 326 327The PS service abstracts all the cryptographic requirements and specifies the 328required cryptographic interface in 329``secure_fw/partitions/protected_storage/crypto/ps_crypto_interface.h`` 330 331The PS service cryptographic operations are implemented in 332``secure_fw/partitions/protected_storage/crypto/ps_crypto_interface.c``, using 333calls to the TF-M Crypto service. 334 335PS Service Build Definitions 336============================ 337The PS service uses a set of C definitions to compile in/out certain features, 338as well as to configure certain service parameters. When using the TF-M build 339system, these definitions are controlled by build flags of the same name. The 340``config/config_base.cmake`` file sets the default values of those flags, but 341they can be overwritten based on platform capabilities by setting them in 342``platform/ext/target/<TARGET_NAME>/config.cmake``. The list of PS service build 343definitions is: 344 345- ``PS_ENCRYPTION``- this flag allows to enable/disable encryption 346 option to encrypt the protected storage data. 347 348- ``PS_CRYPTO_AEAD_ALG`` - this flag indicates the AEAD algorithm to use for 349 authenticated encryption in Protected Storage. 350 351 .. Note:: 352 For GCM/CCM it is essential that IV doesn't get repeated. If this flag is 353 set to ``PSA_ALG_GCM`` or ``PSA_ALG_CCM``, ``PS_ROLLBACK_PROTECTION`` must 354 be enabled to protect against IV rollback. 355 356- ``PS_CREATE_FLASH_LAYOUT``- this flag indicates that it is required 357 to create a PS flash layout. If this flag is set, PS service will 358 generate an empty and valid PS flash layout to store assets. It will 359 erase all data located in the assigned PS memory area before generating 360 the PS layout. This flag is required to be set if the PS memory area 361 is located in a non-persistent memory. This flag can be set if the PS 362 memory area is located in a persistent memory without a valid PS flash 363 layout in it. That is the case when it is the first time in the device 364 life that the PS service is executed. 365- ``PS_VALIDATE_METADATA_FROM_FLASH``- this flag allows to 366 enable/disable the validation mechanism to check the metadata store in flash 367 every time the flash data is read from flash. This validation is required 368 if the flash is not hardware protected against malicious writes. In case 369 the flash is protected against malicious writes (i.e embedded flash, etc), 370 this validation can be disabled in order to reduce the validation overhead. 371- ``PS_ROLLBACK_PROTECTION``- this flag allows to enable/disable 372 rollback protection in protected storage service. This flag takes effect only 373 if the target has non-volatile counters and ``PS_ENCRYPTION`` flag is on. 374- ``PS_AES_KEY_USAGE_LIMIT`` - setting this to a value other than zero limits 375 the number of AES blocks that will be encrypted/decrypted using any one key. 376 When the limit is reached for a given object, a new key will be derived and 377 the data will be encrypted with the new key before being stored. 378 Note that setting this limit too low may reduce the maximum asset size 379 because PS will reject objects that are too large to be encrypted and 380 decrypted without hitting this limit. 381- ``PS_RAM_FS``- setting this flag to ``ON`` enables the use of RAM instead of 382 the persistent storage device to store the FS in the Protected Storage 383 service. This flag is ``OFF`` by default. The PS regression tests write/erase 384 storage multiple time, so enabling this flag can increase the life of flash 385 memory when testing. 386 If this flag is set to ``ON``, PS_RAM_FS_SIZE must also be provided. This 387 specifies the size of the block of RAM to be used to simulate the flash. 388 389 .. Note:: 390 If this flag is disabled when running the regression tests, then it is 391 recommended that the persistent storage area is erased before running the 392 tests to ensure that all tests can run to completion. The type of persistent 393 storage area is platform specific (eFlash, MRAM, etc.) and it is described 394 in corresponding flash_layout.h 395 396- ``PS_MAX_ASSET_SIZE`` - Defines the maximum asset size to be stored in the 397 PS area. This size is used to define the temporary buffers used by PS to 398 read/write the asset content from/to flash. The memory used by the temporary 399 buffers is allocated statically as PS does not use dynamic memory allocation. 400- ``PS_NUM_ASSETS`` - Defines the maximum number of assets to be stored in the 401 PS area. This number is used to dimension statically the object table size in 402 RAM (fast access) and flash (persistent storage). The memory used by the 403 object table is allocated statically as PS does not use dynamic memory 404 allocation. 405- ``PS_TEST_NV_COUNTERS``- this flag enables the virtual implementation of the 406 PS NV counters interface in ``test/secure_fw/suites/ps/secure/nv_counters`` of 407 the ``tf-m-tests`` repo, which emulates NV counters in 408 RAM, and disables the hardware implementation of NV counters provided by 409 the secure service. This flag is enabled by default, but has no effect when 410 the secure regression test is disabled. This flag can be 411 overridden to ``OFF`` when building the regression tests. In this case, 412 the PS rollback protection test suite will not be built, as it relies 413 on extra functionality provided by the virtual NV counters to simulate 414 different rollback scenarios. The remainder of the PS test suites will 415 run using the hardware NV counters. Please note that running the tests in 416 this configuration will quickly increase the hardware NV counter values, 417 which cannot be decreased again. 418 Overriding this flag from its default value of ``OFF`` when not 419 building the regression tests is not currently supported. 420- ``PS_STACK_SIZE``- Defines the stack size of the Protected Storage Secure 421 Partition. This value mainly depends on the build type(debug, release and 422 minisizerel) and compiler. 423 424-------------- 425 426*Copyright (c) 2018-2024, Arm Limited. All rights reserved.* 427*Copyright (c) 2020, Cypress Semiconductor Corporation. All rights reserved.* 428