blob: e8f20b2b67533ad7b8469889c77b4125f212fb5f [file] [log] [blame] [view]
Gilles Peskine7ee4cc32023-11-28 15:49:57 +01001Bridges between legacy and PSA crypto APIs
2==========================================
3
4## Introduction
5
6### Goal of this document
7
8This document explores the needs of applications that use both Mbed TLS legacy crypto interfaces and PSA crypto interfaces. Based on [requirements](#requirements), we [analyze gaps](#gap-analysis) and [API design](#api-design).
9
10This is a design document. The target audience is library maintainers. See the companion document [“Transitioning to the PSA API”](../../psa-transition.md) for a user focus on the same topic.
11
12### Keywords
13
14* [TODO] A part of the analysis that isn't finished.
15* [QUESTION] A specific aspect of the design where there are several plausible decisions.
16* [ACTION] A finalized part of the design that will need to be carried out.
17
18### Context
19
20Mbed TLS 3.x supports two cryptographic APIs:
21
22* The legacy API `mbedtls_xxx` is inherited from PolarSSL.
23* The PSA API `psa_xxx` was introduced in Mbed TLS 2.17.
24
25Mbed TLS is gradually shifting from the legacy API to the PSA API. Mbed TLS 4.0 will be the first version where the PSA API is considered the main API, and large parts of the legacy API will be removed.
26
27In Mbed TLS 4.0, the cryptography will be provided by a separate project [TF-PSA-Crypto](https://github.com/Mbed-TLS/TF-PSA-Crypto). For simplicity, in this document, we just refer to the whole as “Mbed TLS”.
28
29### Document history
30
31This document was originally written when preparing Mbed TLS 3.6. Mbed TLS 3.6 includes both PSA and legacy APIs covering largely overlapping ground. Many legacy APIs will be removed in Mbed TLS 4.0.
32
33## Requirements
34
35### Why mix APIs?
36
37There is functionality that is tied to one API and is not directly available in the other API:
38
39* Only PSA fully supports PSA accelerators and secure element integration.
40* Only PSA supports isolating cryptographic material in a secure service.
41* The legacy API has features that are not present (yet) in PSA, notably parsing and formatting asymmetric keys.
42
43The legacy API can partially leverage PSA features via `MBEDTLS_USE_PSA_CRYPTO`, but this has limited scope.
44
45In addition, many applications cannot be migrated in a single go. For large projects, it is impractical to rewrite a significant part of the code all at once. (For example, Mbed TLS itself will have taken more than 6 years to transition.) Projects that use one or more library in addition to Mbed TLS must follow the evolution of these libraries, each of which might have its own pace.
46
47### Where mixing happens
48
49Mbed TLS can be, and normally is, built with support for both APIs. Therefore no special effort is necessary to allow an application to use both APIs.
50
51Special effort is necessary to use both APIs as part of the implementation of the same feature. From an informal analysis of typical application requirements, we identify four parts of the use of cryptography which can be provided by different APIs:
52
53* Metadata manipulation: parsing and producing encrypted or signed files, finding mutually supported algorithms in a network protocol negotiation, etc.
54* Key management: parsing, generating, deriving and formatting cryptographic keys.
55* Data manipulation other than keys. In practice, most data formats within the scope of the legacy crypto APIs are trivial (ciphertexts, hashes, MACs, shared secrets). The one exception is ECDSA signatures.
56* Cryptographic operations: hash, sign, encrypt, etc.
57
58From this, we deduce the following requirements:
59
60* Convert between PSA and legacy metadata.
61* Creating a key with the legacy API and consuming it in the PSA API.
62* Creating a key with the PSA API and consuming it in the legacy API.
63* Manipulating data formats, other than keys, where the PSA API is lacking.
64
65### Scope limitations
66
67The goal of this document is to bridge the legacy API and the PSA API. The goal is not to provide a PSA way to do everything that is currently possible with the legacy API. The PSA API is less flexible in some regards, and extending it is out of scope in the present study.
68
69With respect to the legacy API, we do not consider functionality of low-level modules for individual algorithms. Our focus is on applications that use high-level legacy crypto modules (md, cipher, pk) and need to combine that with uses of the PSA APIs.
70
71## Gap analysis
72
73Based on “[Where mixing happens](#where-mixing-happens)”, we focus the gap analysis on two topics: metadata and keys. This chapter explores the gaps in each family of cryptographic mechanisms.
74
75### Generic metadata gaps
76
77#### Need for error code conversion
78
79[QUESTION] Do we need public functions to convert between `MBEDTLS_ERR_xxx` error codes and `PSA_ERROR_xxx` error codes? We have such functions for internal use.
80
81### Hash gap analysis
82
83Hashes do not involve keys, and involves no nontrivial data format. Therefore the only gap is with metadata, namely specifying a hash algorithm.
84
85Hashes are often used as building blocks for other mechanisms (HMAC, signatures, key derivation, etc.). Therefore metadata about hashes is relevant not only when calculating hashes, but also when performing many other cryptographic operations.
86
87Gap: functions to convert between `psa_algorithm_t` hash algorithms and `mbedtls_md_type_t`. Such functions exist in Mbed TLS 3.5 (`mbedtls_md_psa_alg_from_type`, `mbedtls_md_type_from_psa_alg`) but they are declared only in private headers.
88
89### MAC gap analysis
90
91[TODO]
92
93### Cipher and AEAD gap analysis
94
95[TODO]
96
97### Key derivation gap analysis
98
99[TODO]
100
101### Random generation gap analysis
102
103[TODO]
104
105### Asymmetric cryptography gap analysis
106
107[TODO]
108
109## New APIs
110
111This section presents new APIs to implement based on the [gap analysis](#gap-analysis).
112
113### Hash APIs
114
115Based on the [gap analysis](#hash-gap-analysis):
116
117[ACTION] Move `mbedtls_md_psa_alg_from_type` and `mbedtls_md_type_from_psa_alg` from `library/md_psa.h` to `include/mbedtls/md.h`.
118
119### MAC APIs
120
121[TODO]
122
123### Cipher and AEAD APIs
124
125[TODO]
126
127### Key derivation APIs
128
129[TODO]
130
131### Random generation APIs
132
133[TODO]
134
135### Asymmetric cryptography APIs
136
137[TODO]