xref: /aosp_15_r20/external/mbedtls/docs/use-psa-crypto.md (revision 62c56f9862f102b96d72393aff6076c951fb8148)
1*62c56f98SSadaf EbrahimiThis document describes the compile-time configuration option
2*62c56f98SSadaf Ebrahimi`MBEDTLS_USE_PSA_CRYPTO` from a user's perspective.
3*62c56f98SSadaf Ebrahimi
4*62c56f98SSadaf EbrahimiThis option:
5*62c56f98SSadaf Ebrahimi- makes the X.509 and TLS libraries use PSA for cryptographic operations as
6*62c56f98SSadaf Ebrahimi  much as possible, see "Internal changes" below;
7*62c56f98SSadaf Ebrahimi- enables new APIs for using keys handled by PSA Crypto, such as
8*62c56f98SSadaf Ebrahimi  `mbedtls_pk_setup_opaque()` and `mbedtls_ssl_conf_psk_opaque()`, see
9*62c56f98SSadaf Ebrahimi"New APIs / API extensions" below.
10*62c56f98SSadaf Ebrahimi
11*62c56f98SSadaf EbrahimiGeneral considerations
12*62c56f98SSadaf Ebrahimi----------------------
13*62c56f98SSadaf Ebrahimi
14*62c56f98SSadaf Ebrahimi**Application code:** when this option is enabled, you need to call
15*62c56f98SSadaf Ebrahimi`psa_crypto_init()` before calling any function from the SSL/TLS, X.509 or PK
16*62c56f98SSadaf Ebrahimimodules, except for the various mbedtls_xxx_init() functions which can be called
17*62c56f98SSadaf Ebrahimiat any time.
18*62c56f98SSadaf Ebrahimi
19*62c56f98SSadaf Ebrahimi**Why enable this option:** to fully take advantage of PSA drivers in PK,
20*62c56f98SSadaf EbrahimiX.509 and TLS. For example, enabling this option is what allows use of drivers
21*62c56f98SSadaf Ebrahimifor ECDSA, ECDH and EC J-PAKE in those modules. However, note that even with
22*62c56f98SSadaf Ebrahimithis option disabled, some code in PK, X.509, TLS or the crypto library might
23*62c56f98SSadaf Ebrahimistill use PSA drivers, if it can determine it's safe to do so; currently
24*62c56f98SSadaf Ebrahimithat's the case for hashes.
25*62c56f98SSadaf Ebrahimi
26*62c56f98SSadaf Ebrahimi**Relationship with other options:** This option depends on
27*62c56f98SSadaf Ebrahimi`MBEDTLS_PSA_CRYPTO_C`. These two options differ in the following way:
28*62c56f98SSadaf Ebrahimi- `MBEDTLS_PSA_CRYPTO_C` enables the implementation of the PSA Crypto API.
29*62c56f98SSadaf Ebrahimi  When it is enabled, `psa_xxx()` APIs are available and you must call
30*62c56f98SSadaf Ebrahimi`psa_crypto_init()` before you call any other `psa_xxx()` function. Other
31*62c56f98SSadaf Ebrahimimodules in the library (non-PSA crypto APIs, X.509, TLS) may or may not use
32*62c56f98SSadaf EbrahimiPSA Crypto but you're not required to call `psa_crypto_init()` before calling
33*62c56f98SSadaf Ebrahiminon-PSA functions, unless explicitly documented (TLS 1.3).
34*62c56f98SSadaf Ebrahimi- `MBEDTLS_USE_PSA_CRYPTO` means that X.509 and TLS will use PSA Crypto as
35*62c56f98SSadaf Ebrahimi  much as possible (that is, everywhere except for features that are not
36*62c56f98SSadaf Ebrahimisupported by PSA Crypto, see "Internal Changes" below for a complete list of
37*62c56f98SSadaf Ebrahimiexceptions). When it is enabled, you need to call `psa_crypto_init()` before
38*62c56f98SSadaf Ebrahimicalling any function from PK, X.509 or TLS; however it doesn't change anything
39*62c56f98SSadaf Ebrahimifor the rest of the library.
40*62c56f98SSadaf Ebrahimi
41*62c56f98SSadaf Ebrahimi**Scope:** `MBEDTLS_USE_PSA_CRYPTO` has no effect on modules other than PK,
42*62c56f98SSadaf EbrahimiX.509 and TLS. It also has no effect on most of the TLS 1.3 code, which always
43*62c56f98SSadaf Ebrahimiuses PSA crypto. The parts of the TLS 1.3 code that will use PSA Crypto or not
44*62c56f98SSadaf Ebrahimidepending on this option being set or not are:
45*62c56f98SSadaf Ebrahimi- record protection;
46*62c56f98SSadaf Ebrahimi- running handshake hash;
47*62c56f98SSadaf Ebrahimi- asymmetric signature verification & generation;
48*62c56f98SSadaf Ebrahimi- X.509 certificate chain verification.
49*62c56f98SSadaf EbrahimiYou need to enable `MBEDTLS_USE_PSA_CRYPTO` if you want TLS 1.3 to use PSA
50*62c56f98SSadaf Ebrahimieverywhere.
51*62c56f98SSadaf Ebrahimi
52*62c56f98SSadaf Ebrahimi**Historical note:** This option was introduced at a time when PSA Crypto was
53*62c56f98SSadaf Ebrahimistill beta and not ready for production, so we made its use in X.509 and TLS
54*62c56f98SSadaf Ebrahimiopt-in: by default, these modules would keep using the stable,
55*62c56f98SSadaf Ebrahimiproduction-ready legacy (pre-PSA) crypto APIs. So, the scope of was X.509 and
56*62c56f98SSadaf EbrahimiTLS, as well as some of PK for technical reasons. Nowadays PSA Crypto is no
57*62c56f98SSadaf Ebrahimilonger beta, and production quality, so there's no longer any reason to make
58*62c56f98SSadaf Ebrahimiits use in other modules opt-in. However, PSA Crypto functions require that
59*62c56f98SSadaf Ebrahimi`psa_crypto_init()` has been called before their use, and for backwards
60*62c56f98SSadaf Ebrahimicompatibility reasons we can't impose this requirement on non-PSA functions
61*62c56f98SSadaf Ebrahimithat didn't have such a requirement before. So, nowadays the main meaning of
62*62c56f98SSadaf Ebrahimi`MBEDTLS_USE_PSA_CRYPTO` is that the user promises to call `psa_crypto_init()`
63*62c56f98SSadaf Ebrahimibefore calling any PK, X.509 or TLS functions. For the same compatibility
64*62c56f98SSadaf Ebrahimireasons, we can't extend its scope. However, new modules in the library, such
65*62c56f98SSadaf Ebrahimias TLS 1.3, can be introduced with a requirement to call `psa_crypto_init()`.
66*62c56f98SSadaf Ebrahimi
67*62c56f98SSadaf EbrahimiNew APIs / API extensions
68*62c56f98SSadaf Ebrahimi-------------------------
69*62c56f98SSadaf Ebrahimi
70*62c56f98SSadaf Ebrahimi### PSA-held (opaque) keys in the PK layer
71*62c56f98SSadaf Ebrahimi
72*62c56f98SSadaf Ebrahimi**New API function:** `mbedtls_pk_setup_opaque()` - can be used to
73*62c56f98SSadaf Ebrahimiwrap a PSA key pair into a PK context. The key can be used for private-key
74*62c56f98SSadaf Ebrahimioperations and its public part can be exported.
75*62c56f98SSadaf Ebrahimi
76*62c56f98SSadaf Ebrahimi**Benefits:** isolation of long-term secrets, use of PSA Crypto drivers.
77*62c56f98SSadaf Ebrahimi
78*62c56f98SSadaf Ebrahimi**Limitations:** can only wrap a key pair, can only use it for private key
79*62c56f98SSadaf Ebrahimioperations. (That is, signature generation, and for RSA decryption too.)
80*62c56f98SSadaf EbrahimiNote: for ECDSA, currently this uses randomized ECDSA while Mbed TLS uses
81*62c56f98SSadaf Ebrahimideterministic ECDSA by default. The following operations are not supported
82*62c56f98SSadaf Ebrahimiwith a context set this way, while they would be available with a normal
83*62c56f98SSadaf Ebrahimicontext: `mbedtls_pk_check_pair()`, `mbedtls_pk_debug()`, all public key
84*62c56f98SSadaf Ebrahimioperations.
85*62c56f98SSadaf Ebrahimi
86*62c56f98SSadaf Ebrahimi**Use in X.509 and TLS:** opt-in. The application needs to construct the PK context
87*62c56f98SSadaf Ebrahimiusing the new API in order to get the benefits; it can then pass the
88*62c56f98SSadaf Ebrahimiresulting context to the following existing APIs:
89*62c56f98SSadaf Ebrahimi
90*62c56f98SSadaf Ebrahimi- `mbedtls_ssl_conf_own_cert()` or `mbedtls_ssl_set_hs_own_cert()` to use the
91*62c56f98SSadaf Ebrahimi  key together with a certificate for certificate-based key exchanges;
92*62c56f98SSadaf Ebrahimi- `mbedtls_x509write_csr_set_key()` to generate a CSR (certificate signature
93*62c56f98SSadaf Ebrahimi  request);
94*62c56f98SSadaf Ebrahimi- `mbedtls_x509write_crt_set_issuer_key()` to generate a certificate.
95*62c56f98SSadaf Ebrahimi
96*62c56f98SSadaf Ebrahimi### PSA-held (opaque) keys for TLS pre-shared keys (PSK)
97*62c56f98SSadaf Ebrahimi
98*62c56f98SSadaf Ebrahimi**New API functions:** `mbedtls_ssl_conf_psk_opaque()` and
99*62c56f98SSadaf Ebrahimi`mbedtls_ssl_set_hs_psk_opaque()`. Call one of these from an application to
100*62c56f98SSadaf Ebrahimiregister a PSA key for use with a PSK key exchange.
101*62c56f98SSadaf Ebrahimi
102*62c56f98SSadaf Ebrahimi**Benefits:** isolation of long-term secrets.
103*62c56f98SSadaf Ebrahimi
104*62c56f98SSadaf Ebrahimi**Limitations:** none.
105*62c56f98SSadaf Ebrahimi
106*62c56f98SSadaf Ebrahimi**Use in TLS:** opt-in. The application needs to register the key using one of
107*62c56f98SSadaf Ebrahimithe new APIs to get the benefits.
108*62c56f98SSadaf Ebrahimi
109*62c56f98SSadaf Ebrahimi### PSA-held (opaque) keys for TLS 1.2 EC J-PAKE key exchange
110*62c56f98SSadaf Ebrahimi
111*62c56f98SSadaf Ebrahimi**New API function:** `mbedtls_ssl_set_hs_ecjpake_password_opaque()`.
112*62c56f98SSadaf EbrahimiCall this function from an application to register a PSA key for use with the
113*62c56f98SSadaf EbrahimiTLS 1.2 EC J-PAKE key exchange.
114*62c56f98SSadaf Ebrahimi
115*62c56f98SSadaf Ebrahimi**Benefits:** isolation of long-term secrets.
116*62c56f98SSadaf Ebrahimi
117*62c56f98SSadaf Ebrahimi**Limitations:** none.
118*62c56f98SSadaf Ebrahimi
119*62c56f98SSadaf Ebrahimi**Use in TLS:** opt-in. The application needs to register the key using one of
120*62c56f98SSadaf Ebrahimithe new APIs to get the benefits.
121*62c56f98SSadaf Ebrahimi
122*62c56f98SSadaf Ebrahimi### PSA-based operations in the Cipher layer
123*62c56f98SSadaf Ebrahimi
124*62c56f98SSadaf EbrahimiThere is a new API function `mbedtls_cipher_setup_psa()` to set up a context
125*62c56f98SSadaf Ebrahimithat will call PSA to store the key and perform the operations.
126*62c56f98SSadaf Ebrahimi
127*62c56f98SSadaf EbrahimiThis function only worked for a small number of ciphers. It is now deprecated
128*62c56f98SSadaf Ebrahimiand it is recommended to use `psa_cipher_xxx()` or `psa_aead_xxx()` functions
129*62c56f98SSadaf Ebrahimidirectly instead.
130*62c56f98SSadaf Ebrahimi
131*62c56f98SSadaf Ebrahimi**Warning:** This function will be removed in a future version of Mbed TLS. If
132*62c56f98SSadaf Ebrahimiyou are using it and would like us to keep it, please let us know about your
133*62c56f98SSadaf Ebrahimiuse case.
134*62c56f98SSadaf Ebrahimi
135*62c56f98SSadaf EbrahimiInternal changes
136*62c56f98SSadaf Ebrahimi----------------
137*62c56f98SSadaf Ebrahimi
138*62c56f98SSadaf EbrahimiAll of these internal changes are active as soon as `MBEDTLS_USE_PSA_CRYPTO`
139*62c56f98SSadaf Ebrahimiis enabled, no change required on the application side.
140*62c56f98SSadaf Ebrahimi
141*62c56f98SSadaf Ebrahimi### TLS: most crypto operations based on PSA
142*62c56f98SSadaf Ebrahimi
143*62c56f98SSadaf EbrahimiCurrent exceptions:
144*62c56f98SSadaf Ebrahimi
145*62c56f98SSadaf Ebrahimi- Finite-field (non-EC) Diffie-Hellman (used in key exchanges: DHE-RSA,
146*62c56f98SSadaf Ebrahimi  DHE-PSK).
147*62c56f98SSadaf Ebrahimi- Restartable operations when `MBEDTLS_ECP_RESTARTABLE` is also enabled (see
148*62c56f98SSadaf Ebrahimi  the documentation of that option).
149*62c56f98SSadaf Ebrahimi
150*62c56f98SSadaf EbrahimiOther than the above exceptions, all crypto operations are based on PSA when
151*62c56f98SSadaf Ebrahimi`MBEDTLS_USE_PSA_CRYPTO` is enabled.
152*62c56f98SSadaf Ebrahimi
153*62c56f98SSadaf Ebrahimi### X.509: most crypto operations based on PSA
154*62c56f98SSadaf Ebrahimi
155*62c56f98SSadaf EbrahimiCurrent exceptions:
156*62c56f98SSadaf Ebrahimi
157*62c56f98SSadaf Ebrahimi- Restartable operations when `MBEDTLS_ECP_RESTARTABLE` is also enabled (see
158*62c56f98SSadaf Ebrahimi  the documentation of that option).
159*62c56f98SSadaf Ebrahimi
160*62c56f98SSadaf EbrahimiOther than the above exception, all crypto operations are based on PSA when
161*62c56f98SSadaf Ebrahimi`MBEDTLS_USE_PSA_CRYPTO` is enabled.
162*62c56f98SSadaf Ebrahimi
163*62c56f98SSadaf Ebrahimi### PK layer: most crypto operations based on PSA
164*62c56f98SSadaf Ebrahimi
165*62c56f98SSadaf EbrahimiCurrent exceptions:
166*62c56f98SSadaf Ebrahimi
167*62c56f98SSadaf Ebrahimi- Verification of RSA-PSS signatures with an MGF hash that's different from
168*62c56f98SSadaf Ebrahimi  the message hash.
169*62c56f98SSadaf Ebrahimi- Restartable operations when `MBEDTLS_ECP_RESTARTABLE` is also enabled (see
170*62c56f98SSadaf Ebrahimi  the documentation of that option).
171*62c56f98SSadaf Ebrahimi
172*62c56f98SSadaf EbrahimiOther than the above exceptions, all crypto operations are based on PSA when
173*62c56f98SSadaf Ebrahimi`MBEDTLS_USE_PSA_CRYPTO` is enabled.
174*62c56f98SSadaf Ebrahimi
175