blob: e170504b2bb9d8d1251577536bedd350eb0ab2ee [file] [log] [blame]
Galanakis, Minos41f85972019-09-30 15:56:40 +01001======================================
2Internal Trusted Storage (ITS) Service
3======================================
Jamie Fox69257652019-07-03 17:53:13 +01004
5:Author: Jamie Fox
6:Organization: Arm Limited
7:Contact: Jamie Fox <jamie.fox@arm.com>
Jamie Fox69257652019-07-03 17:53:13 +01008
Anton Komlevb3f64662023-01-28 11:53:05 +00009.. toctree::
10 :maxdepth: 1
11
12 Block-aligned flash <tfm_its_512_flash.rst>
13
Jamie Fox69257652019-07-03 17:53:13 +010014PSA Internal Trusted Storage
15============================
16PSA Internal Trusted Storage (ITS) is a PSA RoT Service for storing the most
17security-critical device data (e.g. cryptographic keys) in internal storage,
18which is trusted to provide data confidentiality and authenticity. This
19contrasts with PSA Protected Storage, which is an Application RoT service that
20allows larger data sets to be stored securely in external flash, with the option
21for encryption, authentication and rollback protection to protect the
22data-at-rest.
23
24Current TF-M Secure Storage
25===========================
26Currently, the TF-M Secure Storage service implements PSA Protected Storage
27version 1.0-beta2. There is not yet an implementation of PSA Internal Trusted
28Storage in TF-M.
29
30New TF-M service
31================
32The proposal is to implement the *PSA Internal Trusted Storage API* with the
33*TF-M Internal Trusted Storage service*. It can be abbreviated to *TF-M ITS
34service* in general and to ``its`` in code. This name has the advantage of
35making clear the correspondence between the service and the API it implements.
36
37If this name is adopted, then it may make sense to rename the *Secure Storage
38service* to the *Protected Storage service* in the future to match. Then "secure
39storage" could refer to the two services as a collective.
40
41The TF-M ITS service will implement PSA ITS version 1.0. It will be provided by
42a separate partition to Protected Storage, for a couple of reasons:
43
44- To permit isolation between the services.
45
46 - ITS is a PSA RoT Service, while Protected Storage is an Application RoT
47 Service.
48
49- To avoid circular dependencies.
50
51 - The PSA Firmware Framework does not permit circular dependencies between
52 partitions, which would occur if Protected Storage and ITS were provided by
53 the same partition. Protected Storage depends on Crypto, which in turn
54 depends on ITS.
55
56The existing SST filesystem will be reused to provide the backend of the
57service, with the flash layer modified to direct storage to internal flash,
58rather than external.
59
60Compared to Protected Storage, encryption, authentication and rollback
61protection are not required, so the SST encrypted object layer and the crypto
62and NV counter interfaces are not required. The rollback protection feature of
63the object table is also not required.
64
65Code structure
66==============
67The code structure of the service will be as follows:
68
Kevin Peng1a2f3652020-07-30 15:17:05 +080069TF-M repo:
70
Jamie Fox69257652019-07-03 17:53:13 +010071``interface/``
72
73- ``include/psa/internal_trusted_storage.h`` - PSA ITS API
74- ``src/tfm_its_api.c`` - PSA ITS API implementation for NSPE
75
76``secure_fw/ns_callable/tfm_veneers.c`` - ITS veneers (auto-generated from
77manifest)
78
Ken Liu738a4b02020-06-04 14:52:38 +080079``secure_fw/partitions/internal_trusted_storage/``
Jamie Fox69257652019-07-03 17:53:13 +010080
81- ``tfm_internal_trusted_storage.yaml`` - Partition manifest
82- ``tfm_its_secure_api.c`` - PSA ITS API implementation for SPE
83- ``tfm_its_req_mngr.c`` - Uniform secure functions and IPC request handlers
84- ``tfm_internal_trusted_storage.h`` - TF-M ITS API (with client_id parameter)
85- ``tfm_internal_trusted_storage.c`` - TF-M ITS implementation, using the
86 flash_fs as a backend
87- ``flash_fs/`` - Filesystem
88- ``flash/`` - Flash interface
89
Kevin Peng1a2f3652020-07-30 15:17:05 +080090tf-m-tests repo:
91
Jianliang Shen53f7ecd2022-02-22 10:57:51 +080092``test/secure_fw/suites/its/``
Jamie Fox69257652019-07-03 17:53:13 +010093
94- ``non_secure/psa_its_ns_interface_testsuite.c`` - Non-secure interface tests
95- ``secure/psa_its_s_interface_testsuite.c`` - Secure interface tests
96
97TF-M ITS implementation
98-----------------------
99The following APIs will be exposed by ``tfm_internal_trusted_storage.h``::
100
101 psa_status_t tfm_its_init(void);
102
103 psa_status_t tfm_its_set(int32_t client_id,
104 psa_storage_uid_t uid,
105 size_t data_length,
106 const void *p_data,
107 psa_storage_create_flags_t create_flags);
108
109 psa_status_t tfm_its_get(int32_t client_id,
110 psa_storage_uid_t uid,
111 size_t data_offset,
112 size_t data_size,
113 void *p_data,
114 size_t *p_data_length);
115
116 psa_status_t tfm_its_get_info(int32_t client_id,
117 psa_storage_uid_t uid,
118 struct psa_storage_info_t *p_info);
119
120 psa_status_t tfm_its_remove(int32_t client_id,
121 psa_storage_uid_t uid);
122
123That is, the TF-M ITS APIs will have the same prototypes as the PSA ITS APIs,
124but with the addition of a ``client_id`` parameter, which will be passed from
125the ITS request manager. A ``tfm_its_init`` function will also be present, which
126will be called at initialisation time and not exposed through a veneer or SID.
127
128The implementation in ``tfm_internal_trusted_storage.c`` must validate the
129parameters (excepting memory references, which are validated by the SPM),
130translate the UID and client ID into a file ID and then make appropriate calls
131to the filesystem layer. It must also take care ensure that any PSA Storage
132flags associated with the UID are honoured.
133
134Filesystem
135----------
136The ITS filesystem will be copied and modified from the SST filesystem. The
137modifications required will be to rename symbols from ``sst`` to ``its`` and to
138update the implementation to be aligned with the latest version of the PSA
139Storage spec (which consists mainly of moving to the ``psa_status_t`` error type
140and using common error codes from ``psa/error.h``).
141
142The filesystem will also be modified to align the size of each file stored to
143the alignment requirement exposed by the flash interface, by adding appropriate
144padding.
145
146The filesystem code will be de-duplicated again once the ITS service is
147implemented (see below).
148
149Flash layer
150-----------
151The flash layer will be copied from SST, and modified to direct writes to the
152internal flash device. It too needs to be updated to use ``psa_status_t`` error
153types.
154
155Platform layer
156--------------
157The TF-M platform layer must be be updated to distinguish between the external
158flash device used for Protected Storage and internal flash device used for ITS.
159A flash region for the relevant storage service needs to be allocated in each.
160
161On test platforms these may just be two distinct regions of the same flash
162device, but in general they will separate devices with their own drivers.
163
164Detailed design considerations
165==============================
166
167Mapping UID onto file ID
168------------------------
169The ITS APIs identify assets with 64-bit UIDs, to which the ITS service must
170append the 32-bit client ID of the calling partition for access control. The
171existing filesystem uses 32-bit file IDs to identify files, so some mapping
172would be required to convert between the identifiers.
173
174SST uses the object table to do the mapping from client ID, UID pairs to file
175IDs, which means making an extra filesystem read/write for each get/set
176operation. This mapping has minimal overhead for SST though, because object
177table lookups are already required for rollback protection.
178
179For ITS, no rollback protection feature is required, so there are two options:
180
181- Keep a simplified version of the SST object table that just maps from
182 (client ID, UID) to file ID
183
184- Modify the filesystem to take (at least) 96-bit file IDs, in the form of a
185 fixed-length char buffer.
186
187The advantage of the former is that it would require no extra modification to
188the existing filesystem code, and the existing SST object table could be cut
189down for ITS. However, it would mean that every ITS request would invoke twice
190the number of filesystem operations, increasing latency and flash wear. The code
191size of the ITS partition would be increased, as would RAM usage as the table
192would need to be read into RAM.
193
194The latter option would make the filesystem slightly more complex: the size of a
195metadata entry would be increased by 64-bits and the 96-bit fids would need to
196be copied and compared with ``memcpy`` and ``memcmp`` calls. On the other hand,
197mapping onto file IDs would incur only the cost of copying the UID and client ID
198values into the file ID buffer.
199
200A third, even more general, solution would be to use arbitrary-length
201null-terminated strings as the file IDs. This is the standard solution in
202full-featured filesystems, but we do not currently require this level of
203complexity in secure storage.
204
205With this in mind, the proposed option is the second.
206
207Storing create flags
208--------------------
209The ITS APIs provide a 32-bit ``create_flags`` parameter, which contains bit
210flags that determine the properties of the stored data. Only one flag is
211currently defined for ITS: ``PSA_STORAGE_FLAG_WRITE_ONCE``, which prevents a UID
212from being modified or deleted after it is set for the first time.
213
214There are two places that these flags could be stored: in the file data or as
215part of the file metadata.
216
217For the first option, the ITS implementation would need to copy to the flags
218into the buffer containing the data, and adjust the size accordingly, for each
219set operation, and the reverse for each get. Every get_info operation would need
220to read some of the file data, rather than just the metadata, implying a second
221flash read. A potential downside is that many of the cryptographic assets stored
222in ITS will be aligned to power-of-two sizes; adding an extra 32-bits would
223misalign the size, which may reduce flash performance or necessitate adding
224padding to align to the flash page size.
225
226To implement the second option, a 32-bit ``flag`` field would be added to the
227filesystem's metadata structure, whose interpretation is defined by the user.
228This field would clearly be catered towards the PSA Storage APIs, even if
229nominally generic, and alternative filesystems may not have any such field.
230However, it is a more intuitive solution and would simplify both flash alignment
231and get_info operations.
232
233Overall, it seems more beneficial to store the flags in the metadata, so this is
234the proposed solution.
235
236Code sharing between Protected Storage and ITS
237----------------------------------------------
238To de-duplicate the filesystem code used by both Protected Storage and ITS, it
239is proposed that Protected Storage calls ITS APIs as its backend filesystem.
240
241Protected Storage essentially becomes an encryption, authentication and rollback
242protection layer on top of ITS. It makes IPC requests or secure function calls
243to the ITS service to do filesystem operations on its behalf.
244
245This has a couple of advantages:
246
247- It shrinks Protected Storage's stack size, because the filesystem and flash
248 layer stack is only in ITS.
249
250- It automatically solves the problem of ensuring mutual exclusion in the
251 filesystem and flash layers when Protected Storage and ITS are called
252 concurrently. The second request to ITS will just be made to wait by the SPM.
253
254The disadvantage of this approach is that it will increase the latency of
255Protected Storage requests, due to the extra overhead associated with making a
256second IPC request or secure function call. It also limits Protected Storage to
257using only the ITS APIs, unless extra veneers are added solely for Protected
258Storage to use. This, for example, prevents Protected Storage from doing partial
259writes to file without reading and re-writing the whole file.
260
261ITS will need to be modified to direct calls from Protected Storage to a
262different flash device. It can use the client ID to detect when the caller is
263Protected Storage, and pass down the identity of the flash device to use to the
264flash layer, which then calls the appropriate driver.
265
266An open question is what to do if Protected Storage itself wants to store
267something in internal storage in the future (e.g. rollback counters, hash
268tree/table or top hash). A couple of possible solutions would be:
269
270- Divide up the UIDs, so certain UIDs from Protected Storage refer to assets in
271 internal storage, and others to ones in external storage.
272
Summer Qin2db78c82022-10-10 17:17:44 +0800273- Use the ``type`` field of ``psa_call`` in IPC model to distinguish between
274 internal and external storage requests.
Jamie Fox69257652019-07-03 17:53:13 +0100275
276The other option for code sharing would be for Protected Storage and ITS to
277directly share filesystem code, which would be placed in a shared code region.
278With this approach, mutual exclusion to the flash device would need to be
279implemented separately, as would some way of isolating static memory belonging
280to each partition but not the code. Because of these complications, this option
281has not been considered further at this time.
282
283--------------
284
Jianliang Shen53f7ecd2022-02-22 10:57:51 +0800285*Copyright (c) 2019-2022, Arm Limited. All rights reserved.*