blob: 39ae0f9d4f46aa5957434d99162e053bf6520be9 [file] [log] [blame] [view]
Gilles Peskineb51f96a2020-08-31 14:03:05 +02001Conditional inclusion of cryptographic mechanism through the PSA API in Mbed TLS
2================================================================================
3
4This document is a proposed interface for deciding at build time which cryptographic mechanisms to include in the PSA Cryptography interface.
5
6This is currently a proposal for Mbed TLS. It is not currently on track for standardization in PSA.
7
Gilles Peskined8c27cc2020-11-16 21:44:23 +01008Time-stamp: "2020/11/16 20:44:13 GMT"
Gilles Peskineb51f96a2020-08-31 14:03:05 +02009
10## Introduction
11
12### Purpose of this specification
13
14The [PSA Cryptography API specification](https://armmbed.github.io/mbed-crypto/psa/#application-programming-interface) specifies the interface between a PSA Cryptography implementation and an application. The interface defines a number of categories of cryptographic algorithms (hashes, MAC, signatures, etc.). In each category, a typical implementation offers many algorithms (e.g. for signatures: RSA-PKCS#1v1.5, RSA-PSS, ECDSA). When building the implementation for a specific use case, it is often desirable to include only a subset of the available cryptographic mechanisms, primarily in order to reduce the code footprint of the compiled system.
15
16The present document proposes a way for an application using the PSA cryptography interface to declare which mechanisms it requires.
17
Gilles Peskine34b07e72020-09-07 10:28:38 +020018### Conditional inclusion of legacy cryptography modules
Gilles Peskineb51f96a2020-08-31 14:03:05 +020019
Gilles Peskine6cf4ab82020-09-07 09:43:16 +020020Mbed TLS offers a way to select which cryptographic mechanisms are included in a build through its configuration file (`config.h`). This mechanism is based on two main sets of symbols: `MBEDTLS_xxx_C` controls the availability of the mechanism to the application, and `MBEDTLS_xxx_ALT` controls the availability of an alternative implementation, so the software implementation is only included if `MBEDTLS_xxx_C` is defined but not `MBEDTLS_xxx_ALT`.
Gilles Peskineb51f96a2020-08-31 14:03:05 +020021
Gilles Peskine34b07e72020-09-07 10:28:38 +020022### PSA evolution
23
24In the PSA cryptography interface, the **core** (built-in implementations of cryptographic mechanisms) can be augmented with drivers. **Transparent drivers** replace the built-in implementation of a cryptographic mechanism (or, with **fallback**, the built-in implementation is tried if the driver only has partial support for the mechanism). **Opaque drivers** implement cryptographic mechanisms on keys which are stored in a separate domain such as a secure element, for which the core only does key management and dispatch using wrapped key blobs or key identifiers.
25
26The current model is difficult to adapt to the PSA interface for several reasons. The `MBEDTLS_xxx_ALT` symbols are somewhat inconsistent, and in particular do not work well for asymmetric cryptography. For example, many parts of the ECC code have no `MBEDTLS_xxx_ALT` symbol, so a platform with ECC acceleration that can perform all ECDSA and ECDH operations in the accelerator would still embark the `bignum` module and large parts of the `ecp_curves`, `ecp` and `ecdsa` modules. Also the availability of a transparent driver for a mechanism does not translate directly to `MBEDTLS_xxx` symbols.
Gilles Peskineb51f96a2020-08-31 14:03:05 +020027
28### Requirements
29
30[Req.interface] The application can declare which cryptographic mechanisms it needs.
31
32[Req.inclusion] If the application does not require a mechanism, a suitably configured Mbed TLS build must not include it. The granularity of mechanisms must work for typical use cases and has [acceptable limitations](#acceptable-limitations).
33
34[Req.drivers] If a PSA driver is available in the build, a suitably configured Mbed TLS build must not include the corresponding software code (unless a software fallback is needed).
35
36[Req.c] The configuration mechanism consists of C preprocessor definitions, and the build does not require tools other than a C compiler. This is necessary to allow building an application and Mbed TLS in development environments that do not allow third-party tools.
37
38[Req.adaptability] The implementation of the mechanism must be adaptable with future evolution of the PSA cryptography specifications and Mbed TLS. Therefore the interface must remain sufficiently simple and abstract.
39
40### Acceptable limitations
41
42[Limitation.matrix] If a mechanism is defined by a combination of algorithms and key types, for example a block cipher mode (CBC, CTR, CFB, …) and a block permutation (AES, CAMELLIA, ARIA, …), there is no requirement to include only specific combinations.
43
44[Limitation.direction] For mechanisms that have multiple directions (for example encrypt/decrypt, sign/verify), there is no requirement to include only one direction.
45
46[Limitation.size] There is no requirement to include only support for certain key sizes.
47
Gilles Peskine54a0ad22020-08-31 15:41:29 +020048[Limitation.multipart] Where there are multiple ways to perform an operation, for example single-part and multi-part, there is no mechanism to select only one or a subset of the possible ways.
49
Gilles Peskineb51f96a2020-08-31 14:03:05 +020050## Interface
51
52### PSA Crypto configuration file
53
Gilles Peskine9e069072020-11-16 10:26:01 +010054The PSA Crypto configuration file `psa/crypto_config.h` defines a series of symbols of the form `PSA_WANT_xxx` where `xxx` describes the feature that the symbol enables. The symbols are documented in the section [“PSA Crypto configuration symbols”](#psa-crypto-configuration-symbols) below.
Gilles Peskineb51f96a2020-08-31 14:03:05 +020055
Gilles Peskine48584b52020-10-13 19:07:02 +020056The symbol `MBEDTLS_PSA_CRYPTO_CONFIG` in `mbedtls/config.h` determines whether `psa/crypto_config.h` is used.
Gilles Peskineb51f96a2020-08-31 14:03:05 +020057
58* If `MBEDTLS_PSA_CRYPTO_CONFIG` is unset, which is the default at least in Mbed TLS 2.x versions, things are as they are today: the PSA subsystem includes generic code unconditionally, and includes support for specific mechanisms conditionally based on the existing `MBEDTLS_xxx_` symbols.
Gilles Peskine9e069072020-11-16 10:26:01 +010059* If `MBEDTLS_PSA_CRYPTO_CONFIG` is set, the necessary software implementations of cryptographic algorithms are included based on both the content of the PSA Crypto configuration file and the Mbed TLS configuration file. For example, the code in `aes.c` is enabled if either `mbedtls/config.h` contains `MBEDTLS_AES_C` or `psa/crypto_config.h` contains `PSA_WANT_KEY_TYPE_AES`.
Gilles Peskineb51f96a2020-08-31 14:03:05 +020060
61### PSA Crypto configuration symbols
62
63#### Configuration symbol syntax
64
Gilles Peskine9e069072020-11-16 10:26:01 +010065A PSA Crypto configuration symbol is a C preprocessor symbol whose name starts with `PSA_WANT_`.
Gilles Peskineb51f96a2020-08-31 14:03:05 +020066
67* If the symbol is not defined, the corresponding feature is not included.
68* If the symbol is defined to a preprocessor expression with the value `1`, the corresponding feature is included.
69* If the symbol is defined with a different value, the behavior is currently undefined and reserved for future use.
70
Gilles Peskine993c2492020-11-13 22:45:13 +010071#### Configuration symbol usage
72
Gilles Peskined8c27cc2020-11-16 21:44:23 +010073The presence of a symbol `PSA_WANT_xxx` in the Mbed TLS configuration determines whether a feature is available through the PSA API. These symbols should be used in any place that requires conditional compilation based on the availability of a cryptographic mechanism through the PSA API, including:
Gilles Peskine993c2492020-11-13 22:45:13 +010074
75* In Mbed TLS test code.
76* In Mbed TLS library code using `MBEDTLS_USE_PSA_CRYPTO`, for example in TLS to determine which cipher suites to enable.
Gilles Peskine1b1f3fb2020-11-16 12:02:01 +010077* In application code that provides additional features based on cryptographic capabilities, for example additional key parsing and formatting functions, or cipher suite availability for network protocols.
Gilles Peskine993c2492020-11-13 22:45:13 +010078
Gilles Peskineb51f96a2020-08-31 14:03:05 +020079#### Configuration symbol semantics
80
Gilles Peskine9e069072020-11-16 10:26:01 +010081If a feature is not requested for inclusion in the PSA Crypto configuration file, it may still be included in the build, either because the feature has been requested in some other way, or because the library does not support the exclusion of this feature. Mbed TLS should make a best effort to support the exclusion of all features, but in some cases this may be judged too much effort for too little benefit.
Gilles Peskineb51f96a2020-08-31 14:03:05 +020082
83#### Configuration symbols for key types
84
85For each constant or constructor macro of the form `PSA_KEY_TYPE_xxx`, the symbol **`PSA_WANT_KEY_TYPE_xxx`** indicates that support for this key type is desired.
86
87For asymmetric cryptography, `PSA_WANT_KEY_TYPE_xxx_KEY_PAIR` determines whether private-key operations are desired, and `PSA_WANT_KEY_TYPE_xxx_PUBLIC_KEY` determines whether public-key operations are desired. `PSA_WANT_KEY_TYPE_xxx_KEY_PAIR` implicitly enables `PSA_WANT_KEY_TYPE_xxx_PUBLIC_KEY`: there is no way to only include private-key operations (which typically saves little code).
88
89#### Configuration symbols for curves
90
91For elliptic curve key types, only the specified curves are included. To include a curve, include a symbol of the form **`PSA_WANT_ECC_family_size`**. For example: `PSA_WANT_ECC_SECP_R1_256` for secp256r1, `PSA_WANT_ECC_MONTGOMERY_CURVE25519`. It is an error to require an ECC key type but no curve, and Mbed TLS will reject this at compile time.
92
93#### Configuration symbols for algorithms
94
95For each constant or constructor macro of the form `PSA_ALG_xxx`, the symbol **`PSA_WANT_ALG_xxx`** indicates that support for this algorithm is desired.
96
97For parametrized algorithms, the `PSA_WANT_ALG_xxx` symbol indicates whether the base mechanism is supported. Parameters must themselves be included through their own `PSA_WANT_ALG_xxx` symbols. It is an error to include a base mechanism without at least one possible parameter, and Mbed TLS will reject this at compile time. For example, `PSA_WANT_ALG_ECDSA` requires the inclusion of randomized ECDSA for all hash algorithms whose corresponding symbol `PSA_WANT_ALG_xxx` is enabled.
98
99## Implementation
100
Gilles Peskine2e4062c2020-08-31 15:41:54 +0200101### Additional non-public symbols
102
103#### Accounting for transparent drivers
104
105In addition to the [configuration symbols](#psa-crypto-configuration-symbols), we need two parallel or mostly parallel sets of symbols:
106
107* **`MBEDTLS_PSA_ACCEL_xxx`** indicates whether a fully-featured, fallback-free transparent driver is available.
108* **`MBEDTLS_PSA_BUILTIN_xxx`** indicates whether the software implementation is needed.
109
110`MBEDTLS_PSA_ACCEL_xxx` is one of the outputs of the transpilation of a driver description, alongside the glue code for calling the drivers.
111
112`MBEDTLS_PSA_BUILTIN_xxx` is enabled when `PSA_WANT_xxx` is enabled and `MBEDTLS_PSA_ACCEL_xxx` is disabled.
113
114These symbols are not part of the public interface of Mbed TLS towards applications or to drivers, regardless of whether the symbols are actually visible.
115
116### Architecture of symbol definitions
117
Gilles Peskinebb483f62020-11-16 14:35:12 +0100118#### New-style definition of configuration symbols
Gilles Peskine2e4062c2020-08-31 15:41:54 +0200119
Gilles Peskine9e069072020-11-16 10:26:01 +0100120When `MBEDTLS_PSA_CRYPTO_CONFIG` is set, the header file `mbedtls/config.h` needs to define all the `MBEDTLS_xxx_C` configuration symbols, including the ones deduced from the PSA Crypto configuration. It does this by including the new header file **`mbedtls/config_psa.h`**, which defines the `MBEDTLS_PSA_BUILTIN_xxx` symbols and deduces the corresponding `MBEDTLS_xxx_C` (and other) symbols.
Gilles Peskine95434382020-09-21 20:08:51 +0200121
Gilles Peskinebb483f62020-11-16 14:35:12 +0100122`mbedtls/config_psa.h` includes `psa/crypto_config.h`, the user-editable file that defines application requirements.
Gilles Peskine95434382020-09-21 20:08:51 +0200123
Gilles Peskined8c27cc2020-11-16 21:44:23 +0100124#### Old-style definition of configuration symbols
Gilles Peskine95434382020-09-21 20:08:51 +0200125
Gilles Peskinebb483f62020-11-16 14:35:12 +0100126When `MBEDTLS_PSA_CRYPTO_CONFIG` is not set, the configuration of Mbed TLS works as before, and the inclusion of non-PSA code only depends on `MBEDTLS_xxx` symbols defined (or not) in `mbedtls/config.h`. Furthermore, a new header file **`mbedtls/config_psa.h`** deduces PSA configuration symbols (`PSA_WANT_xxx`, `MBEDTLS_PSA_BUILTIN_xxx`) from classic configuration symbols (`MBEDTLS_xxx`).
Gilles Peskine95434382020-09-21 20:08:51 +0200127
Gilles Peskinebb483f62020-11-16 14:35:12 +0100128The `PSA_WANT_xxx` definitions in `mbedtls/config_psa.h` are needed not only to build the PSA parts of the library, but also to build code that uses these parts. This includes structure definitions in `psa/crypto_struct.h`, size calculations in `psa/crypto_sizes.h`, and application code that's specific to a given cryptographic mechanism. In Mbed TLS itself, code under `MBEDTLS_USE_PSA_CRYPTO` and conditional compilation guards in tests and sample programs need `PSA_WANT_xxx`.
129
130Since some existing applications use a handwritten `mbedtls/config.h` or an edited copy of `mbedtls/config.h` from an earlier version of Mbed TLS, `mbedtls/config_psa.h` must be included via an already existing header that is not `mbedtls/config.h`, so it is included via `psa/crypto.h` (for example from `psa/crypto_platform.h`).
131
132#### Summary of definitions of configuration symbols
133
Gilles Peskined8c27cc2020-11-16 21:44:23 +0100134Whether `MBEDTLS_PSA_CRYPTO_CONFIG` is set or not, `mbedtls/config_psa.h` includes `mbedtls/crypto_drivers.h`, a header file generated by the transpilation of the driver descriptions. It defines `MBEDTLS_PSA_ACCEL_xxx` symbols according to the availability of transparent drivers without fallback.
Gilles Peskinebb483f62020-11-16 14:35:12 +0100135
136The following table summarizes where symbols are defined depending on the configuration mode.
Gilles Peskine95434382020-09-21 20:08:51 +0200137
138* (U) indicates a symbol that is defined by the user (application).
139* (D) indicates a symbol that is deduced from other symbols by code that ships with Mbed TLS.
140* (G) indicates a symbol that is generated from driver descriptions.
141
142------------------------------------------------------------------------------------------------
143Symbols With `MBEDTLS_PSA_CRYPTO_CONFIG` Without `MBEDTLS_PSA_CRYPTO_CONFIG`
144------------------------- -------------------------------- -----------------------------------
145`MBEDTLS_xxx_C` `mbedtls/config.h` (U) or `mbedtls/config.h` (U)
146 `mbedtls/config_psa.h` (D)
Gilles Peskine55687642020-10-12 23:23:52 +0200147`PSA_WANT_xxx` `psa/crypto_config.h` (U) `mbedtls/config_psa.h (D)
Gilles Peskine95434382020-09-21 20:08:51 +0200148`MBEDTLS_PSA_BUILTIN_xxx` `mbedtls/config_psa.h` (D) `mbedtls/config_psa.h` (D)
Gilles Peskine7b5e6b92020-11-04 18:30:01 +0100149`MBEDTLS_PSA_ACCEL_xxx` `mbedtls/crypto_drivers.h` (G) N/A
Gilles Peskine95434382020-09-21 20:08:51 +0200150------------------------------------------------------------------------------------------------
Gilles Peskine2e4062c2020-08-31 15:41:54 +0200151
152#### Visibility of internal symbols
153
154Ideally, the `MBEDTLS_PSA_ACCEL_xxx` and `MBEDTLS_PSA_BUILTIN_xxx` symbols should not be visible to application code or driver code, since they are not part of the public interface of the library. However these symbols are needed to deduce whether to include library modules (for example `MBEDTLS_AES_C` has to be enabled if `MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES` is enabled), which makes it difficult to keep them private.
155
156#### Compile-time checks
157
158The header file **`library/psa_check_config.h`** applies sanity checks to the configuration, throwing `#error` if something is wrong.
159
160A mechanism similar to `mbedtls/check_config.h` detects errors such as enabling ECDSA but no curve.
161
162Since configuration symbols must be undefined or 1, any other value should trigger an `#error`.
163
164#### Automatic generation of preprocessor symbol manipulations
165
166A lot of the preprocessor symbol manipulation is systematic calculations that analyze the configuration. `mbedtls/config_psa.h` and `library/psa_check_config.h` should be generated automatically, in the same manner as `version_features.c`.
167
Gilles Peskine9e069072020-11-16 10:26:01 +0100168### Structure of PSA Crypto library code
Gilles Peskine2e4062c2020-08-31 15:41:54 +0200169
170#### Conditional inclusion of library entry points
171
172An entry point can be eliminated entirely if no algorithm requires it.
173
174#### Conditional inclusion of mechanism-specific code
175
176Code that is specific to certain key types or to certain algorithms must be guarded by the applicable symbols: `PSA_WANT_xxx` for code that is independent of the application, and `MBEDTLS_PSA_BUILTIN_xxx` for code that calls an Mbed TLS software implementation.
177
178## PSA standardization
179
180### JSON configuration mechanism
181
182At the time of writing, the preferred configuration mechanism for a PSA service is in JSON syntax. The translation from JSON to build instructions is not specified by PSA.
183
184For PSA Crypto, the preferred configuration mechanism would be similar to capability specifications of transparent drivers. The same JSON properties that are used to mean “this driver can perform that mechanism” in a driver description would be used to mean “the application wants to perform that mechanism” in the application configuration.
185
186### From JSON to C
187
188The JSON capability language allows a more fine-grained selection than the C mechanism proposed here. For example, it allows requesting only single-part mechanisms, only certain key sizes, or only certain combinations of algorithms and key types.
189
190The JSON capability language can be translated approximately to the boolean symbol mechanism proposed here. The approximation considers a feature to be enabled if any part of it is enabled. For example, if there is a capability for AES-CTR and one for CAMELLIA-GCM, the translation to boolean symbols will also include AES-GCM and CAMELLIA-CTR. If there is a capability for AES-128, the translation will also include AES-192 and AES-256.
191
192The boolean symbol mechanism proposed here can be translated to a list of JSON capabilities: for each included algorithm, include a capability with that algorithm, the key types that apply to that algorithm, no size restriction, and all the entry points that apply to that algorithm.
193
Gilles Peskineb51f96a2020-08-31 14:03:05 +0200194## Open questions
195
196### Open questions about the interface
197
198#### Naming of symbols
199
200The names of [elliptic curve symbols](#configuration-symbols-for-curves) are a bit weird: `SECP_R1_256` instead of `SECP256R1`. Should we make them more classical, but less systematic?
201
Gilles Peskine2e4062c2020-08-31 15:41:54 +0200202#### Impossible combinations
203
204What does it mean to have `PSA_WANT_ALG_ECDSA` enabled but with only Curve25519? Is it a mandatory error?
205
Gilles Peskineb51f96a2020-08-31 14:03:05 +0200206#### Diffie-Hellman
207
208Way to request only specific groups? Not a priority: constrained devices don't do FFDH. Specify it as may change in future versions.
209
210#### Coexistence with the current Mbed TLS configuration
211
212The two mechanisms have very different designs. Is there serious potential for confusion? Do we understand how the combinations work?
213
214### Open questions about the design
215
216#### Algorithms without a key type or vice versa
217
218Is it realistic to mandate a compile-time error if a key type is required, but no matching algorithm, or vice versa? Is it always the right thing, for example if there is an opaque driver that manipulates this key type?
219
220#### Opaque-only mechanisms
221
222If a mechanism should only be supported in an opaque driver, what does the core need to know about it? Do we have all the information we need?
223
224This is especially relevant to suppress a mechanism completely if there is no matching algorithm. For example, if there is no transparent implementation of RSA or ECDSA, `psa_sign_hash` and `psa_verify_hash` may still be needed if there is an opaque signature driver.
225
226### Open questions about the implementation
227
228#### Testability
229
230Is this proposal decently testable? There are a lot of combinations. What combinations should we test?
Gilles Peskinedbd2e3c2020-08-31 15:41:10 +0200231
232<!--
233Local Variables:
234time-stamp-line-limit: 40
235time-stamp-start: "Time-stamp: *\""
236time-stamp-end: "\""
237time-stamp-format: "%04Y/%02m/%02d %02H:%02M:%02S %Z"
238time-stamp-time-zone: "GMT"
239End:
240-->