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