Repository navigation
crypto/mbedtls: route PSA key management through cryptodev - #4133
ThePassionate wants to merge 2 commits into
Conversation
Add two more Mbed TLS options to the Kconfig so they can be selected from NuttX: - MBEDTLS_PSA_CRYPTO_SE_C enables the dynamic secure element support of the PSA cryptography API, the framework that secure element drivers register against. - PSA_ITS_STORAGE_PREFIX moves the PSA key files out of the current working directory, which is where the ITS file backend keeps them otherwise. Both options map one to one onto the upstream Mbed TLS options of the same names. Testing ------- Built sim:nsh with CONFIG_CRYPTO_MBEDTLS and the PSA options enabled: the Mbed TLS library, including library/psa_crypto_se.c that the new option gates, compiles with 0 warnings; the crypto driver tests and the PKCS12 wrapper test stay green. Signed-off-by: makejian <makejian@xiaomi.com>
Register a PSA secure element driver that forwards the PSA key management operations (allocate, validate slot, import, generate, export, export public, destroy) to the NuttX cryptodev key management interface, so keys whose lifetime locates them at PSA_KEY_LOCATION_LOCAL_CRYPTODEV are held by the cryptodev key store instead of the PSA local storage. An application brings the driver in by calling psa_alt_register_se_driver() before psa_crypto_init(). The cryptodev ALT layer grows the CIOCKEY access: cryptodev_context_t gets a struct crypt_kop, guarded by the new option so the hash and MAC contexts that embed it stay small in builds that do not use the secure element path. The secure element interface of Mbed TLS 3.x marks the driver tables as private, so the fields are assigned through MBEDTLS_PRIVATE(). The AES key generation passes the key length (16/24/32 bytes) as the second input parameter the kernel expects. Enabled with MBEDTLS_PSA_CRYPTODEV_ALT, which depends on MBEDTLS_PSA_CRYPTO_SE_C and selects the ALT layer. Testing ------- Built sim:nsh with CONFIG_CRYPTO_MBEDTLS, MBEDTLS_PSA_CRYPTO_SE_C and MBEDTLS_PSA_CRYPTODEV_ALT enabled: 0 warnings; the driver is not exercised at runtime because sim has no secure element behind the cryptodev. The crypto driver tests (aescbc, aesctr, aesxts, des3cbc, chacha20, chachapoly, hmac, hash, aesgcm, aescmac, pbkdf2, poly1305) and the PKCS12 wrapper test (2/2) stay green. Signed-off-by: makejian <makejian@xiaomi.com>
|
The Linux (arm-imx) failure is not from this PR. The failing config is imx93-qsb/rpmsg, which enables none of the symbols this series touches (no CRYPTO_MBEDTLS, no PSA options); building it locally with and without this series produces a byte-identical .data. The overflow is a master-side regression: apache/nuttx@1325a419756 ("sched: split tcb->flags by writer", landed 04:06 UTC today) grows that config's .data by 92 bytes with the CI toolchain - 168 bytes with a local gcc - and tips the Every apps PR that builds after 04:06 UTC will hit the same failure until the flags-split cost is trimmed or the board's memory map is adjusted. Happy to help with either, but it is independent of this series. |
Summary
Adds a PSA secure element driver that routes PSA key management through the NuttX cryptodev interface, so applications using the PSA Crypto API can keep their keys in the cryptodev key store (the software swkey backend today, a hardware secure element driver when one registers) instead of plaintext files.
MBEDTLS_PSA_CRYPTO_SE_CandPSA_ITS_STORAGE_PREFIXKconfig entries with their config.h mappings.PSA_KEY_LOCATION_LOCAL_CRYPTODEVwhose key management callbacks (allocate, validate, import, generate, export, export_public, destroy) translate to theCIOCKEYioctls the cryptodev core already exposes (CRK_ALLOCATE_KEY,CRK_IMPORT_KEY, ...). PSA metadata lives in ITS files; the key material lives in the cryptodev key store.Compared to the Vela original this fixes three contract errors found while porting: the AES generation now passes the key length the kernel interface requires (Vela passed one parameter where cryptodev validates two, so the call always failed with EINVAL), the export callback sets the output length the PSA core contract requires, and kernel errnos are mapped to PSA status values instead of leaking into the PSA status space.
The PSA external RNG routing (
mbedtls_psa_external_get_random) is already upstream (9a1a8d3). The SE driver interface this builds on is marked deprecated by Mbed TLS 3.6 in favor of the PSA driver interface, so the port deliberately stays minimal.Impact
Both new options default n.
MBEDTLS_PSA_CRYPTODEV_ALTlives in theCRYPTO_CRYPTODEVblock and selectsMBEDTLS_ALT.Testing
Built from a fresh checkout state (distclean + refetch) with sim:nsh +
CONFIG_CRYPTO_MBEDTLS+CONFIG_OPENSSL_MBEDTLS_WRAPPER+MBEDTLS_PSA_CRYPTO_SE_C+MBEDTLS_PSA_CRYPTODEV_ALT: 0 warnings, 0 errors, with the new translation units present in libapps.a (the sim ELF gc-sections them out, as nothing calls the registration API there). The full crypto regression - 12 tests including the PKCS12 cmocka suite - is byte-identical to the plain master baseline.