blob: 2d4fed56c2b23bd421f8bedfd63a8b6d9e496760 [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
87Mbed Crypto 0.2.0
88-----------------
89
90**Warning:** the information in this section is provisional and may change before Mbed Crypto is released for Mbed OS 5.12. At the time of writing, we don't even know whether this version will be called 0.2.0.
91
92To be released for Mbed OS 5.12.
93
Gilles Peskine11eca712019-02-20 15:44:22 +010094Supported integrations:
Gilles Peskine0b020022019-02-12 14:25:57 +010095
96* [PSA platform](#file-namespace-on-a-psa-platform-for-0.2.0)
97* [library using PSA ITS](#file-namespace-on-its-as-a-library-for-0.2.0)
98* [library using C stdio](#file-namespace-on-stdio-for-0.2.0)
99
100Supported features:
101
102* [Persistent transparent keys](#key-file-format-for-0.2.0) designated by a [key identifier and owner](#key-names-for-0.2.0).
103* [Nonvolatile random seed](#nonvolatile-random-seed-file-format-for-0.2.0) on ITS only.
104
105Backward compatibility commitments: TBD
106
107### Key names for 0.2.0
108
Gilles Peskine11eca712019-02-20 15:44:22 +0100109Information about each key is stored in a dedicated file designated by a _key file identifier_ (`psa_key_file_id_t`). The key file identifier is constructed from the 32-bit key identifier (`psa_key_id_t`) and, if applicable, an identifier of the owner of the key. In integrations where there is no concept of key owner (in particular, in library integrations), the key file identifier is exactly the key identifier. When the library is integrated into a service, the service determines the semantics of the owner identifier.
110
111The way in which the file name is constructed from the key file identifier depends on the storage backend. The content of the file is described [below](#key-file-format-for-0.2.0).
Gilles Peskine0b020022019-02-12 14:25:57 +0100112
Gilles Peskineb5a132f2019-02-12 16:47:20 +0100113The 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 +0100114
115* Library integration: the key file name is just the key identifer. This is a 32-bit value.
Gilles Peskine11eca712019-02-20 15:44:22 +0100116* PSA service integration: the key file identifier is `(uint32_t)owner_uid << 32 | key_id` where `key_id` is the key identifier specified by the application 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 +0100117
118### Key file format for 0.2.0
119
Gilles Peskineb5a132f2019-02-12 16:47:20 +0100120The 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 +0100121
122### Nonvolatile random seed file format for 0.2.0
123
124[Identical to 0.1.0](#nonvolatile-random-seed-file-format-for-0.1.0).
125
126### File namespace on a PSA platform for 0.2.0
127
128Assumption: 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.
129
Gilles Peskine11eca712019-02-20 15:44:22 +0100130Assumption: the owner identifier is a nonzero value of type `int32_t`.
131
132* 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 +0100133* File 0xffffff52 (`PSA_CRYPTO_ITS_RANDOM_SEED_UID`): [nonvolatile random seed](#nonvolatile-random-seed-file-format-for-0.1.0).
Gilles Peskine11eca712019-02-20 15:44:22 +0100134* Files 0x100000000 through 0xffffffffffff: [content](#key-file-format-for-0.2.0) of the [key whose identifier is the file identifier](#key-names-for-0.2.0). The upper 32 bits determine the owner.
Gilles Peskine0b020022019-02-12 14:25:57 +0100135
136### File namespace on ITS as a library for 0.2.0
137
Gilles Peskinef02fbf42019-02-13 15:43:35 +0100138Assumption: 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 +0100139
Gilles Peskine11eca712019-02-20 15:44:22 +0100140This is a library integration, so there is no owner. The key file identifier is identical to the key identifier.
141
Gilles Peskine0b020022019-02-12 14:25:57 +0100142* File 0: unused.
143* Files 1 through 0xfffeffff: [content](#key-file-format-for-0.2.0) of the [key whose identifier is the file identifier](#key-names-for-0.2.0).
144* File 0xffffff52 (`PSA_CRYPTO_ITS_RANDOM_SEED_UID`): [nonvolatile random seed](#nonvolatile-random-seed-file-format-for-0.2.0).
145* Files 0xffff0000 through 0xffffff51, 0xffffff53 through 0xffffffff, 0x100000000 through 0xffffffffffffffff: unused.
146
147### File namespace on stdio for 0.2.0
148
Gilles Peskine11eca712019-02-20 15:44:22 +0100149This is a library integration, so there is no owner. The key file identifier is identical to the key identifier.
150
Gilles Peskine0b020022019-02-12 14:25:57 +0100151[Identical to 0.1.0](#file-namespace-on-stdio-for-0.1.0).
152
153### Upgrade from 0.1.0 to 0.2.0.
154
155* Delete files 1 through 0xfffeffff, which contain keys in a format that is no longer supported.
156
157### Suggested changes to make before 0.2.0
158
159The 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).
160
161It 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.