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