Repository navigation
crypto/mbedtls: route PSA key management through cryptodev - #4133
Conversation
|
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. |
794256c to
eb75a78
Compare
3a351d1
eb75a78 to
3a351d1
Compare
|
Thanks for this. I read both commits against nuttx master ( 1. Needs a rebase; drop the The PR doesn't apply on master: CMakeLists.txt, Kconfig, Makefile, Returning 2.
For example, The driver can't work out the size itself because 3. Failed key creation leaks key IDs and leaves key material in the store
Suggestion:
4.
The transparent path checks these; with an SE driver that job falls to the driver. Please limit import to what the driver actually supports (AES 16/24/32 bytes, SECP_R1 256 key pair as a 32-byte scalar) and return 5. ECC/RSA generate and public export don't work with the in-tree software backend
The testing section only covers AES. Which backend were the ECC/RSA paths written for? If it's an out-of-tree hardware driver, I'd drop ECC/RSA from this PR, or add tests for them. 6. Error mapping These backend errors currently become
7. Slot numbers get truncated
8. If Smaller things
|
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. The import of a persistent key follows the CRK_IMPORT_KEY with a CRK_SAVE_KEY: the key store may keep an imported key in a RAM cache only, and without the save the key material would be gone once the cache is, while the PSA metadata in the internal trusted storage kept pointing at the slot. Enabled with MBEDTLS_PSA_CRYPTODEV_ALT, which depends on MBEDTLS_PSA_CRYPTO_SE_C and selects the ALT layer. Testing ------- sim:nsh with CONFIG_CRYPTO_MBEDTLS, MBEDTLS_PSA_CRYPTO_SE_C and MBEDTLS_PSA_CRYPTODEV_ALT enabled, the cryptodev software key management backend on /dev/nvs, and PSA_ITS_STORAGE_PREFIX="/tmp/": the psakey test drives the registration, the import/export round trip, the AES-256 generate, the destroy lifecycle, a persistent key and one AES-CBC round from the PSA API, all green, and the apps build is warning-free. 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>
Restrict the driver to AES keys (the in-tree software backend has no asymmetric generator that fits the PSA single-slot model and no CRK_EXPORT_PUBLIC_KEY), and fix the review findings: - Validate the key attributes in p_allocate and p_validate_slot_number before any id is reserved, so a key that cannot be created does not burn a slot. The same check in p_import rejects a bit count that does not match the material. - Send a best-effort CRK_DELETE_KEY on the import error path, so a failed import does not leave the id reserved (the PSA core does not call p_destroy on failed creation, mbed-crypto#217). - Map -EEXIST to PSA_ERROR_ALREADY_EXISTS, -ENOBUFS to PSA_ERROR_BUFFER_TOO_SMALL and -EFBIG to NOT_SUPPORTED. - Reject slot numbers above UINT32_MAX instead of silently truncating them. - The registration uses static const driver tables: no heap, no leak when a second registration returns ALREADY_EXISTS. - p_export reports the length from the kernel's crp_nbits writeback instead of echoing the caller's buffer size. - p_export_public returns NOT_SUPPORTED explicitly. The dev_alt CIOCKEY wrapper is no longer conditional on CONFIG_MBEDTLS_PSA_CRYPTODEV_ALT: it is already unconditional on master (PR#4134), and ecdsa_alt/ecdh_alt use it too. Signed-off-by: makejian <makejian@xiaomi.com>
3a351d1 to
6d4fb92
Compare
|
All points addressed; the series is rebased onto current master (the dev_alt CIOCKEY wrapper hunks are dropped since #4134 made it unconditional) and tested against Mbed TLS 3.6.5.
Smaller items: the Kconfig help texts, the The |
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. The full crypto regression - 12 tests including the PKCS12 cmocka suite - pass unchanged.A
psakeytest exercises the path at runtime: driver registration (including the double-registration error), volatile and persistent import/export round trips, AES-256 generation, destroy semantics, and the exported key material used as a working AES key against the RFC 3602 vectors. Run on sim against the software key store:Environment: the software key store on
/dev/nvs(local test wiring, as for the keymanagement test) and the PSA metadata files under thePSA_ITS_STORAGE_PREFIXdirectory.