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