blob: c47dfef7bdafe6208ae780887012293630f288d1 [file] [log] [blame] [view]
Gilles Peskine0b020022019-02-12 14:25:57 +01001Mbed Crypto storage specification
2=================================
3
4This document specifies how Mbed Crypto uses storage.
5
6Mbed Crypto may be upgraded on an existing device with the storage preserved. Therefore:
7
81. Any change may break existing installations and may require an upgrade path.
91. This document retains historical information about all past released versions. Do not remove information from this document unless it has always been incorrect or it is about a version that you are sure was never released.
10
11Mbed Crypto 0.1.0
12-----------------
13
14Tags: mbedcrypto-0.1.0b, mbedcrypto-0.1.0b2
15
16Released in November 2018. <br>
17Integrated in Mbed OS 5.11.
18
19Supported backends:
20
21* [PSA ITS](#file-namespace-on-its-for-0.1.0)
22* [C stdio](#file-namespace-on-stdio-for-0.1.0)
23
24Supported features:
25
26* [Persistent transparent keys](#key-file-format-for-0.1.0) designated by a [slot number](#key-names-for-0.1.0).
27* [Nonvolatile random seed](#nonvolatile-random-seed-file-format-for-0.1.0) on ITS only.
28
29This is a beta release, and we do not promise backward compatibility, with one exception:
30
31> On Mbed OS, if a device has a nonvolatile random seed file produced with Mbed OS 5.11.x and is upgraded to a later version of Mbed OS, the nonvolatile random seed file is preserved or upgraded.
32
33We do not make any promises regarding key storage, or regarding the nonvolatile random seed file on other platforms.
34
35### Key names for 0.1.0
36
37Information about each key is stored in a dedicated file whose name is constructed from the key identifier. The way in which the file name is constructed depends on the storage backend. The content of the file is described [below](#key-file-format-for-0.1.0).
38
Gilles Peskineb5a132f2019-02-12 16:47:20 +010039The valid values for a key identifier are the range from 1 to 0xfffeffff. This limitation on the range is not documented in user-facing documentation: according to the user-facing documentation, arbitrary 32-bit values are valid.
Gilles Peskine0b020022019-02-12 14:25:57 +010040
41The code uses the following constant in an internal header (note that despite the name, this value is actually one plus the maximum permitted value):
42
43 #define PSA_MAX_PERSISTENT_KEY_IDENTIFIER 0xffff0000
44
45There is a shared namespace for all callers.
46
47### Key file format for 0.1.0
48
49All integers are encoded in little-endian order in 8-bit bytes.
50
51The layout of a key file is:
52
53* magic (8 bytes): `"PSA\0KEY\0"`
54* version (4 bytes): 0
55* type (4 bytes): `psa_key_type_t` value
56* policy usage flags (4 bytes): `psa_key_usage_t` value
57* policy usage algorithm (4 bytes): `psa_algorithm_t` value
58* key material length (4 bytes)
59* key material: output of `psa_export_key`
60* Any trailing data is rejected on load.
61
62### Nonvolatile random seed file format for 0.1.0
63
64The nonvolatile random seed file contains a seed for the random generator. If present, it is rewritten at each boot as part of the random generator initialization.
65
66The file format is just the seed as a byte string with no metadata or encoding of any kind.
67
68### File namespace on ITS for 0.1.0
69
70Assumption: ITS provides a 32-bit file identifier namespace. The Crypto service can use arbitrary file identifiers and no other part of the system accesses the same file identifier namespace.
71
72* File 0: unused.
73* Files 1 through 0xfffeffff: [content](#key-file-format-for-0.1.0) of the [key whose identifier is the file identifier](#key-names-for-0.1.0).
74* File 0xffffff52 (`PSA_CRYPTO_ITS_RANDOM_SEED_UID`): [nonvolatile random seed](#nonvolatile-random-seed-file-format-for-0.1.0).
75* Files 0xffff0000 through 0xffffff51, 0xffffff53 through 0xffffffff: unused.
76
77### File namespace on stdio for 0.1.0
78
79Assumption: C stdio, allowing names containing lowercase letters, digits and underscores, of length up to 23.
80
81An undocumented build-time configuration value `CRYPTO_STORAGE_FILE_LOCATION` allows storing the key files in a directory other than the current directory. This value is simply prepended to the file name (so it must end with a directory separator to put the keys in a different directory).
82
83* `CRYPTO_STORAGE_FILE_LOCATION "psa_key_slot_0"`: used as a temporary file. Must be writable. May be overwritten or deleted if present.
84* `sprintf(CRYPTO_STORAGE_FILE_LOCATION "psa_key_slot_%lu", key_id)` [content](#key-file-format-for-0.1.0) of the [key whose identifier](#key-names-for-0.1.0) is `key_id`.
85* Other files: unused.
86
Gilles Peskine640273a2019-05-20 17:16:43 +020087Mbed Crypto 1.0.0
Gilles Peskine0b020022019-02-12 14:25:57 +010088-----------------
89
Gilles Peskine640273a2019-05-20 17:16:43 +020090Tags: mbedcrypto-1.0.0d4, mbedcrypto-1.0.0
Gilles Peskine0b020022019-02-12 14:25:57 +010091
Gilles Peskine640273a2019-05-20 17:16:43 +020092Released in February 2019. <br>
93Integrated in Mbed OS 5.12.
Gilles Peskine0b020022019-02-12 14:25:57 +010094
Gilles Peskine11eca712019-02-20 15:44:22 +010095Supported integrations:
Gilles Peskine0b020022019-02-12 14:25:57 +010096
Gilles Peskine640273a2019-05-20 17:16:43 +020097* [PSA platform](#file-namespace-on-a-psa-platform-for-1.0.0)
98* [library using PSA ITS](#file-namespace-on-its-as-a-library-for-1.0.0)
99* [library using C stdio](#file-namespace-on-stdio-for-1.0.0)
Gilles Peskine0b020022019-02-12 14:25:57 +0100100
101Supported features:
102
Gilles Peskine640273a2019-05-20 17:16:43 +0200103* [Persistent transparent keys](#key-file-format-for-1.0.0) designated by a [key identifier and owner](#key-names-for-1.0.0).
104* [Nonvolatile random seed](#nonvolatile-random-seed-file-format-for-1.0.0) on ITS only.
Gilles Peskine0b020022019-02-12 14:25:57 +0100105
106Backward compatibility commitments: TBD
107
Gilles Peskine640273a2019-05-20 17:16:43 +0200108### Key names for 1.0.0
Gilles Peskine0b020022019-02-12 14:25:57 +0100109
Ronald Cron71016a92020-08-28 19:01:50 +0200110Information about each key is stored in a dedicated file designated by the key identifier. In integrations where there is no concept of key owner (in particular, in library integrations), the key identifier is exactly the key identifier as defined in the PSA Cryptography API specification (`psa_key_id_t`). In integrations where there is a concept of key owner (integration into a service for example), the key identifier is made of an owner identifier (its semantics and type are integration specific) and of the key identifier (`psa_key_id_t`) from the key owner point of view.
Gilles Peskine11eca712019-02-20 15:44:22 +0100111
Ronald Cron71016a92020-08-28 19:01:50 +0200112The way in which the file name is constructed from the key identifier depends on the storage backend. The content of the file is described [below](#key-file-format-for-1.0.0).
Gilles Peskine0b020022019-02-12 14:25:57 +0100113
Ronald Cron71016a92020-08-28 19:01:50 +0200114* Library integration: the key file name is just the key identifier as defined in the PSA crypto specification. This is a 32-bit value.
Gilles Peskine5d20c732022-06-30 12:15:35 +0200115* PSA service integration: the key file name is `(uint64_t)owner_uid << 32 | key_id` where `key_id` is the key identifier from the owner point of view and `owner_uid` (of type `int32_t`) is the calling partition identifier provided to the server by the partition manager. This is a 64-bit value.
Gilles Peskine0b020022019-02-12 14:25:57 +0100116
Gilles Peskine640273a2019-05-20 17:16:43 +0200117### Key file format for 1.0.0
Gilles Peskine0b020022019-02-12 14:25:57 +0100118
Gilles Peskineb5a132f2019-02-12 16:47:20 +0100119The layout is identical to [0.1.0](#key-file-format-for-0.1.0) so far. However note that the encoding of key types, algorithms and key material has changed, therefore the storage format is not compatible (despite using the same value in the version field so far).
Gilles Peskine0b020022019-02-12 14:25:57 +0100120
Gilles Peskine640273a2019-05-20 17:16:43 +0200121### Nonvolatile random seed file format for 1.0.0
Gilles Peskine0b020022019-02-12 14:25:57 +0100122
Gilles Peskinec64eab32022-06-30 12:15:53 +0200123The nonvolatile random seed file contains a seed for the random generator. If present, it is rewritten at each boot as part of the random generator initialization.
124
125The file format is just the seed as a byte string with no metadata or encoding of any kind.
126
127This is unchanged since [the feature was introduced in Mbed Crypto 0.1.0](#nonvolatile-random-seed-file-format-for-0.1.0).
Gilles Peskine0b020022019-02-12 14:25:57 +0100128
Gilles Peskine640273a2019-05-20 17:16:43 +0200129### File namespace on a PSA platform for 1.0.0
Gilles Peskine0b020022019-02-12 14:25:57 +0100130
131Assumption: ITS provides a 64-bit file identifier namespace. The Crypto service can use arbitrary file identifiers and no other part of the system accesses the same file identifier namespace.
132
Gilles Peskine11eca712019-02-20 15:44:22 +0100133Assumption: the owner identifier is a nonzero value of type `int32_t`.
134
135* Files 0 through 0xffffff51, 0xffffff53 through 0xffffffff: unused, reserved for internal use of the crypto library or crypto service.
Gilles Peskine0b020022019-02-12 14:25:57 +0100136* File 0xffffff52 (`PSA_CRYPTO_ITS_RANDOM_SEED_UID`): [nonvolatile random seed](#nonvolatile-random-seed-file-format-for-0.1.0).
Gilles Peskine640273a2019-05-20 17:16:43 +0200137* Files 0x100000000 through 0xffffffffffff: [content](#key-file-format-for-1.0.0) of the [key whose identifier is the file identifier](#key-names-for-1.0.0). The upper 32 bits determine the owner.
Gilles Peskine0b020022019-02-12 14:25:57 +0100138
Gilles Peskine640273a2019-05-20 17:16:43 +0200139### File namespace on ITS as a library for 1.0.0
Gilles Peskine0b020022019-02-12 14:25:57 +0100140
Gilles Peskinef02fbf42019-02-13 15:43:35 +0100141Assumption: ITS provides a 64-bit file identifier namespace. The entity using the crypto library can use arbitrary file identifiers and no other part of the system accesses the same file identifier namespace.
Gilles Peskine0b020022019-02-12 14:25:57 +0100142
Gilles Peskine11eca712019-02-20 15:44:22 +0100143This is a library integration, so there is no owner. The key file identifier is identical to the key identifier.
144
Gilles Peskine0b020022019-02-12 14:25:57 +0100145* File 0: unused.
Gilles Peskine640273a2019-05-20 17:16:43 +0200146* Files 1 through 0xfffeffff: [content](#key-file-format-for-1.0.0) of the [key whose identifier is the file identifier](#key-names-for-1.0.0).
147* File 0xffffff52 (`PSA_CRYPTO_ITS_RANDOM_SEED_UID`): [nonvolatile random seed](#nonvolatile-random-seed-file-format-for-1.0.0).
Gilles Peskine0b020022019-02-12 14:25:57 +0100148* Files 0xffff0000 through 0xffffff51, 0xffffff53 through 0xffffffff, 0x100000000 through 0xffffffffffffffff: unused.
149
Gilles Peskine640273a2019-05-20 17:16:43 +0200150### File namespace on stdio for 1.0.0
Gilles Peskine0b020022019-02-12 14:25:57 +0100151
Gilles Peskine11eca712019-02-20 15:44:22 +0100152This is a library integration, so there is no owner. The key file identifier is identical to the key identifier.
153
Gilles Peskine0b020022019-02-12 14:25:57 +0100154[Identical to 0.1.0](#file-namespace-on-stdio-for-0.1.0).
155
Gilles Peskine640273a2019-05-20 17:16:43 +0200156### Upgrade from 0.1.0 to 1.0.0.
Gilles Peskine0b020022019-02-12 14:25:57 +0100157
158* Delete files 1 through 0xfffeffff, which contain keys in a format that is no longer supported.
159
Gilles Peskine640273a2019-05-20 17:16:43 +0200160### Suggested changes to make before 1.0.0
Gilles Peskine0b020022019-02-12 14:25:57 +0100161
162The library integration and the PSA platform integration use different sets of file names. This is annoyingly non-uniform. For example, if we want to store non-key files, we have room in different ranges (0 through 0xffffffff on a PSA platform, 0xffff0000 through 0xffffffffffffffff in a library integration).
163
164It would simplify things to always have a 32-bit owner, with a nonzero value, and thus reserve the range 0–0xffffffff for internal library use.
Gilles Peskine131aa312019-05-20 17:17:17 +0200165
Gilles Peskine2c8f9092019-07-10 17:18:13 +0200166Mbed Crypto 1.1.0
Gilles Peskine131aa312019-05-20 17:17:17 +0200167-----------------
168
Gilles Peskine2c8f9092019-07-10 17:18:13 +0200169Tags: mbedcrypto-1.1.0
Gilles Peskine131aa312019-05-20 17:17:17 +0200170
Gilles Peskine2c8f9092019-07-10 17:18:13 +0200171Released in early June 2019. <br>
Gilles Peskine131aa312019-05-20 17:17:17 +0200172Integrated in Mbed OS 5.13.
173
174Identical to [1.0.0](#mbed-crypto-1.0.0) except for some changes in the key file format.
175
Gilles Peskine2c8f9092019-07-10 17:18:13 +0200176### Key file format for 1.1.0
Gilles Peskine131aa312019-05-20 17:17:17 +0200177
178The key file format is identical to [1.0.0](#key-file-format-for-1.0.0), except for the following changes:
179
Gilles Peskine2c8f9092019-07-10 17:18:13 +0200180* A new policy field, marked as [NEW:1.1.0] below.
Gilles Peskine131aa312019-05-20 17:17:17 +0200181* The encoding of key types, algorithms and key material has changed, therefore the storage format is not compatible (despite using the same value in the version field so far).
182
183A self-contained description of the file layout follows.
184
185All integers are encoded in little-endian order in 8-bit bytes.
186
187The layout of a key file is:
188
189* magic (8 bytes): `"PSA\0KEY\0"`
190* version (4 bytes): 0
191* type (4 bytes): `psa_key_type_t` value
192* policy usage flags (4 bytes): `psa_key_usage_t` value
193* policy usage algorithm (4 bytes): `psa_algorithm_t` value
Gilles Peskine2c8f9092019-07-10 17:18:13 +0200194* policy enrollment algorithm (4 bytes): `psa_algorithm_t` value [NEW:1.1.0]
Gilles Peskine131aa312019-05-20 17:17:17 +0200195* key material length (4 bytes)
196* key material: output of `psa_export_key`
197* Any trailing data is rejected on load.
Gilles Peskine831ac722019-07-23 19:29:35 +0200198
199Mbed Crypto TBD
200---------------
201
202Tags: TBD
203
204Released in TBD 2019. <br>
205Integrated in Mbed OS TBD.
206
207### Changes introduced in TBD
208
209* The layout of a key file now has a lifetime field before the type field.
210* Key files can store references to keys in a secure element. In such key files, the key material contains the slot number.
211
212### File namespace on a PSA platform on TBD
213
214Assumption: ITS provides a 64-bit file identifier namespace. The Crypto service can use arbitrary file identifiers and no other part of the system accesses the same file identifier namespace.
215
216Assumption: the owner identifier is a nonzero value of type `int32_t`.
217
218* Files 0 through 0xfffeffff: unused.
219* Files 0xffff0000 through 0xffffffff: reserved for internal use of the crypto library or crypto service. See [non-key files](#non-key-files-on-tbd).
220* Files 0x100000000 through 0xffffffffffff: [content](#key-file-format-for-1.0.0) of the [key whose identifier is the file identifier](#key-names-for-1.0.0). The upper 32 bits determine the owner.
221
222### File namespace on ITS as a library on TBD
223
224Assumption: ITS provides a 64-bit file identifier namespace. The entity using the crypto library can use arbitrary file identifiers and no other part of the system accesses the same file identifier namespace.
225
226This is a library integration, so there is no owner. The key file identifier is identical to the key identifier.
227
228* File 0: unused.
229* Files 1 through 0xfffeffff: [content](#key-file-format-for-1.0.0) of the [key whose identifier is the file identifier](#key-names-for-1.0.0).
230* Files 0xffff0000 through 0xffffffff: reserved for internal use of the crypto library or crypto service. See [non-key files](#non-key-files-on-tbd).
231* Files 0x100000000 through 0xffffffffffffffff: unused.
232
233### Non-key files on TBD
234
235File identifiers in the range 0xffff0000 through 0xffffffff are reserved for internal use in Mbed Crypto.
236
237* Files 0xfffffe02 through 0xfffffeff (`PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + lifetime`): secure element driver storage. The content of the file is the secure element driver's persistent data.
238* File 0xffffff52 (`PSA_CRYPTO_ITS_RANDOM_SEED_UID`): [nonvolatile random seed](#nonvolatile-random-seed-file-format-for-1.0.0).
239* File 0xffffff54 (`PSA_CRYPTO_ITS_TRANSACTION_UID`): [transaction file](#transaction-file-format-for-tbd).
240* Other files are unused and reserved for future use.
241
242### Key file format for TBD
243
244All integers are encoded in little-endian order in 8-bit bytes except where otherwise indicated.
245
246The layout of a key file is:
247
248* magic (8 bytes): `"PSA\0KEY\0"`.
249* version (4 bytes): 0.
250* lifetime (4 bytes): `psa_key_lifetime_t` value.
Torstein Nessed9246552020-10-21 09:27:37 +0200251* type (4 bytes): `psa_key_type_t` value.
Gilles Peskine831ac722019-07-23 19:29:35 +0200252* policy usage flags (4 bytes): `psa_key_usage_t` value.
253* policy usage algorithm (4 bytes): `psa_algorithm_t` value.
254* policy enrollment algorithm (4 bytes): `psa_algorithm_t` value.
255* key material length (4 bytes).
256* key material:
257 * For a transparent key: output of `psa_export_key`.
Torstein Nesse162a1102020-10-07 10:50:15 +0200258 * For an opaque key (unified driver interface): driver-specific opaque key blob.
Gilles Peskine831ac722019-07-23 19:29:35 +0200259 * For an opaque key (key in a secure element): slot number (8 bytes), in platform endianness.
260* Any trailing data is rejected on load.
261
262### Transaction file format for TBD
263
264The transaction file contains data about an ongoing action that cannot be completed atomically. It exists only if there is an ongoing transaction.
265
266All integers are encoded in platform endianness.
267
268All currently existing transactions concern a key in a secure element.
269
270The layout of a transaction file is:
271
272* type (2 bytes): the [transaction type](#transaction-types-on-tbd).
273* unused (2 bytes)
274* lifetime (4 bytes): `psa_key_lifetime_t` value that corresponds to a key in a secure element.
275* slot number (8 bytes): `psa_key_slot_number_t` value. This is the unique designation of the key for the secure element driver.
276* key identifier (4 bytes in a library integration, 8 bytes on a PSA platform): the internal representation of the key identifier. On a PSA platform, this encodes the key owner in the same way as [in file identifiers for key files](#file-namespace-on-a-psa-platform-on-tbd)).
277
278#### Transaction types on TBD
279
280* 0x0001: key creation. The following locations may or may not contain data about the key that is being created:
281 * The slot in the secure element designated by the slot number.
282 * The file containing the key metadata designated by the key identifier.
283 * The driver persistent data.
284* 0x0002: key destruction. The following locations may or may not still contain data about the key that is being destroyed:
285 * The slot in the secure element designated by the slot number.
286 * The file containing the key metadata designated by the key identifier.
287 * The driver persistent data.
Torstein Nessed9246552020-10-21 09:27:37 +0200288
289Mbed Crypto TBD
290---------------
291
292Tags: TBD
293
294Released in TBD 2020. <br>
295Integrated in Mbed OS TBD.
296
297### Changes introduced in TBD
298
299* The type field has been split into a type and a bits field of 2 bytes each.
300
301### Key file format for TBD
302
303All integers are encoded in little-endian order in 8-bit bytes except where otherwise indicated.
304
305The layout of a key file is:
306
307* magic (8 bytes): `"PSA\0KEY\0"`.
308* version (4 bytes): 0.
309* lifetime (4 bytes): `psa_key_lifetime_t` value.
310* type (2 bytes): `psa_key_type_t` value.
311* bits (2 bytes): `psa_key_bits_t` value.
312* policy usage flags (4 bytes): `psa_key_usage_t` value.
313* policy usage algorithm (4 bytes): `psa_algorithm_t` value.
314* policy enrollment algorithm (4 bytes): `psa_algorithm_t` value.
315* key material length (4 bytes).
316* key material:
317 * For a transparent key: output of `psa_export_key`.
318 * For an opaque key (unified driver interface): driver-specific opaque key blob.
319 * For an opaque key (key in a secure element): slot number (8 bytes), in platform endianness.
320* Any trailing data is rejected on load.
Gilles Peskine141a7702022-06-30 09:16:53 +0200321
322Mbed TLS 2.25.0
323---------------
324
325Tags: `mbedtls-2.25.0`, `mbedtls-2.26.0`, `mbedtls-2.27.0`, `mbedtls-2.28.0` (continued in early 3.x releases)
326
327First released in December 2020.
328
329Note: this is the first version that is officially supported. The version number is still 0.
330
Gilles Peskine49857282022-06-30 12:16:32 +0200331Backward compatibility commitments: we promise backward compatibility for stored keys when Mbed TLS is upgraded from x to y if x >= 2.25 and y < 4. See [`BRANCHES.md`](../../BRANCHES.md) for more details.
Gilles Peskine141a7702022-06-30 09:16:53 +0200332
333Supported integrations:
334
335* [PSA platform](#file-namespace-on-a-psa-platform-on-mbed-tls-2.25.0)
336* [library using PSA ITS](#file-namespace-on-its-as-a-library-on-mbed-tls-2.25.0)
337* [library using C stdio](#file-namespace-on-stdio-for-mbed-tls-2.25.0)
338
339Supported features:
340
341* [Persistent keys](#key-file-format-for-mbed-tls-2.25.0) designated by a [key identifier and owner](#key-names-for-mbed-tls-2.25.0). Keys can be:
342 * Transparent, stored in the export format.
343 * Opaque, using the unified driver interface with statically registered drivers (`MBEDTLS_PSA_CRYPTO_DRIVERS`). The driver determines the content of the opaque key blob.
344 * Opaque, using the deprecated secure element interface with dynamically registered drivers (`MBEDTLS_PSA_CRYPTO_SE_C`). The driver picks a slot number which is stored in the place of the key material.
345* [Nonvolatile random seed](#nonvolatile-random-seed-file-format-for-mbed-tls-2.25.0) on ITS only.
346
347### Changes introduced in Mbed TLS 2.25.0
348
349* The numerical encodings of `psa_key_type_t`, `psa_key_usage_t` and `psa_algorithm_t` have changed.
350
351### File namespace on a PSA platform on Mbed TLS 2.25.0
352
353Assumption: ITS provides a 64-bit file identifier namespace. The Crypto service can use arbitrary file identifiers and no other part of the system accesses the same file identifier namespace.
354
355Assumption: the owner identifier is a nonzero value of type `int32_t`.
356
357* Files 0 through 0xfffeffff: unused.
358* Files 0xffff0000 through 0xffffffff: reserved for internal use of the crypto library or crypto service. See [non-key files](#non-key-files-on-mbed-tls-2.25.0).
359* Files 0x100000000 through 0xffffffffffff: [content](#key-file-format-for-mbed-tls-2.25.0) of the [key whose identifier is the file identifier](#key-names-for-mbed-tls-2.25.0). The upper 32 bits determine the owner.
360
361### File namespace on ITS as a library on Mbed TLS 2.25.0
362
363Assumption: ITS provides a 64-bit file identifier namespace. The entity using the crypto library can use arbitrary file identifiers and no other part of the system accesses the same file identifier namespace.
364
365This is a library integration, so there is no owner. The key file identifier is identical to the key identifier.
366
367* File 0: unused.
368* Files 1 through 0xfffeffff: [content](#key-file-format-for-mbed-tls-2.25.0) of the [key whose identifier is the file identifier](#key-names-for-mbed-tls-2.25.0).
369* Files 0xffff0000 through 0xffffffff: reserved for internal use of the crypto library or crypto service. See [non-key files](#non-key-files-on-mbed-tls-2.25.0).
370* Files 0x100000000 through 0xffffffffffffffff: unused.
371
372### File namespace on stdio for Mbed TLS 2.25.0
373
374Assumption: C stdio, allowing names containing lowercase letters, digits and underscores, of length up to 23.
375
376An undocumented build-time configuration value `PSA_ITS_STORAGE_PREFIX` allows storing the key files in a directory other than the current directory. This value is simply prepended to the file name (so it must end with a directory separator to put the keys in a different directory).
377
378* `PSA_ITS_STORAGE_PREFIX "tempfile.psa_its"`: used as a temporary file. Must be writable. May be overwritten or deleted if present.
Gilles Peskine49857282022-06-30 12:16:32 +0200379* `sprintf(PSA_ITS_STORAGE_PREFIX "%016llx.psa_its", key_id)`: a key or non-key file. The `key_id` in the name is the 64-bit file identifier, which is the [key identifier](#key-names-for-mbed-tls-2.25.0) for a key file or some reserved identifier for a [non-key file](#non-key-files-on-mbed-tls-2.25.0). The contents of the file are:
Gilles Peskine141a7702022-06-30 09:16:53 +0200380 * Magic header (8 bytes): `"PSA\0ITS\0"`
381 * File contents.
382
383### Key names for Mbed TLS 2.25.0
384
385Information about each key is stored in a dedicated file designated by the key identifier. In integrations where there is no concept of key owner (in particular, in library integrations), the key identifier is exactly the key identifier as defined in the PSA Cryptography API specification (`psa_key_id_t`). In integrations where there is a concept of key owner (integration into a service for example), the key identifier is made of an owner identifier (its semantics and type are integration specific) and of the key identifier (`psa_key_id_t`) from the key owner point of view.
386
387The way in which the file name is constructed from the key identifier depends on the storage backend. The content of the file is described [below](#key-file-format-for-mbed-tls-2.25.0).
388
389* Library integration: the key file name is just the key identifier as defined in the PSA crypto specification. This is a 32-bit value which must be in the range 0x00000001..0x3fffffff (`PSA_KEY_ID_USER_MIN`..`PSA_KEY_ID_USER_MAX`).
Gilles Peskine5d20c732022-06-30 12:15:35 +0200390* PSA service integration: the key file name is `(uint64_t)owner_uid << 32 | key_id` where `key_id` is the key identifier from the owner point of view and `owner_uid` (of type `int32_t`) is the calling partition identifier provided to the server by the partition manager. This is a 64-bit value.
Gilles Peskine141a7702022-06-30 09:16:53 +0200391
392### Key file format for Mbed TLS 2.25.0
393
394All integers are encoded in little-endian order in 8-bit bytes except where otherwise indicated.
395
396The layout of a key file is:
397
398* magic (8 bytes): `"PSA\0KEY\0"`.
399* version (4 bytes): 0.
400* lifetime (4 bytes): `psa_key_lifetime_t` value.
401* type (2 bytes): `psa_key_type_t` value.
402* bits (2 bytes): `psa_key_bits_t` value.
403* policy usage flags (4 bytes): `psa_key_usage_t` value.
404* policy usage algorithm (4 bytes): `psa_algorithm_t` value.
405* policy enrollment algorithm (4 bytes): `psa_algorithm_t` value.
406* key material length (4 bytes).
407* key material:
408 * For a transparent key: output of `psa_export_key`.
409 * For an opaque key (unified driver interface): driver-specific opaque key blob.
410 * For an opaque key (key in a dynamic secure element): slot number (8 bytes), in platform endianness.
411* Any trailing data is rejected on load.
412
413### Non-key files on Mbed TLS 2.25.0
414
415File identifiers that are outside the range of persistent key identifiers are reserved for internal use by the library. The only identifiers currently in use have the owner id (top 32 bits) set to 0.
416
417* Files 0xfffffe02 through 0xfffffeff (`PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + lifetime`): dynamic secure element driver storage. The content of the file is the secure element driver's persistent data.
418* File 0xffffff52 (`PSA_CRYPTO_ITS_RANDOM_SEED_UID`): [nonvolatile random seed](#nonvolatile-random-seed-file-format-for-mbed-tls-2.25.0).
419* File 0xffffff54 (`PSA_CRYPTO_ITS_TRANSACTION_UID`): [transaction file](#transaction-file-format-for-mbed-tls-2.25.0).
420* Other files are unused and reserved for future use.
421
422### Nonvolatile random seed file format for Mbed TLS 2.25.0
423
424[Identical to Mbed Crypto 0.1.0](#nonvolatile-random-seed-file-format-for-0.1.0).
425
426### Transaction file format for Mbed TLS 2.25.0
427
428The transaction file contains data about an ongoing action that cannot be completed atomically. It exists only if there is an ongoing transaction.
429
430All integers are encoded in platform endianness.
431
432All currently existing transactions concern a key in a dynamic secure element.
433
434The layout of a transaction file is:
435
436* type (2 bytes): the [transaction type](#transaction-types-on-mbed-tls-2.25.0).
437* unused (2 bytes)
438* lifetime (4 bytes): `psa_key_lifetime_t` value that corresponds to a key in a secure element.
439* slot number (8 bytes): `psa_key_slot_number_t` value. This is the unique designation of the key for the secure element driver.
440* key identifier (4 bytes in a library integration, 8 bytes on a PSA platform): the internal representation of the key identifier. On a PSA platform, this encodes the key owner in the same way as [in file identifiers for key files](#file-namespace-on-a-psa-platform-on-mbed-tls-2.25.0)).
441
442#### Transaction types on Mbed TLS 2.25.0
443
444* 0x0001: key creation. The following locations may or may not contain data about the key that is being created:
445 * The slot in the secure element designated by the slot number.
446 * The file containing the key metadata designated by the key identifier.
447 * The driver persistent data.
448* 0x0002: key destruction. The following locations may or may not still contain data about the key that is being destroyed:
449 * The slot in the secure element designated by the slot number.
450 * The file containing the key metadata designated by the key identifier.
451 * The driver persistent data.