Skip to content

crypto/mbedtls: extend the ALT layer with asym, GCM, PKCS5 and key-id sessions - #4134

Open
ThePassionate wants to merge 7 commits into
apache:masterfrom
ThePassionate:crypto-mbedtls-alt3
Open

ThePassionate wants to merge 7 commits into
apache:masterfrom
ThePassionate:crypto-mbedtls-alt3

Conversation

@ThePassionate

Copy link
Copy Markdown
Contributor

Summary

Extends the Mbed TLS ALT layer so the API surface the merged kernel capabilities support is reachable from mbedtls: asymmetric operations, GCM with any IV, PBKDF2, sessions keyed by key id, and the CIOCKEY path - plus two fixes the extension uncovered.

  • fix private member access in cmac_alt: CONFIG_MBEDTLS_CMAC_ALT never compiled on master (20 errors reaching into private Mbed TLS 3.x members); rewritten to MBEDTLS_PRIVATE(). This is the only place the series touches existing files' private access.
  • add CIOCKEY wrapper to dev_alt: a cryptodev_cryptk() helper and crypt_kop context member, mirroring cryptodev_crypt().
  • reset len/src and set olen in the alt finish paths: fixes a latent bug - every hash/MAC alt finish left the context length and source pointer set, so a second mbedtls_md_hmac_update/finish processed the same message twice (a 16-byte CMAC input returned CMAC(M,M)). The ALT path has never worked correctly on master; no defconfig enables it, so nothing caught it.
  • add ecdsa and ecdh alt implementations: ECDSA sign/verify and ECDH over cryptodev, little-endian integers matching the kernel side.
  • add gcm alt implementation: any IV length (not just 96-bit), streaming update_ad/update/finish, and session release on finish.
  • add PKCS#5 PBKDF2 alt implementation: routes PBKDF2 to the kernel with the per-request iteration count; carries a 0004 patch that guards the fetched pkcs5.c with the ALT symbols (the fetched-tree convention of 0001-0003).
  • support AES sessions keyed by key id: the _keyid setkey variants open sessions whose key is a cryptodev key id (the key-id mechanism of crypto: support referencing keys by id in cipher, mac and asymmetric operations nuttx#22061); plain setkey clears SOP_F_KEYID so contexts cannot leak the mode.

Impact

All new options default n. Kernel-side prerequisites are already merged (CIOCKEY ops, SOP_F_KEYID, ECC little-endian, any-IV GCM, PBKDF2 iteration forwarding); this series needs no nuttx changes.

Testing

Built from a fresh checkout state (distclean + refetch + patches reapplied) with all twelve ALT options enabled: 0 warnings, 0 errors, and each commit builds standalone. Functional verification through the mbedtls API against reference vectors: CMAC over RFC 4493 and SP 800-38B AES-256 (including multi-part update/finish), GCM with 96-bit/8-byte/60-byte IVs and streaming vs python cryptography, PBKDF2 over RFC 6070 and SHA-256, key-id sessions byte-identical to direct-key sessions, ECDSA/ECDH round trips and tamper rejection. The standard crypto regression (12 tests) and the PKCS12 cmocka suite (2/2) pass unchanged - the latter exercises the wrapper-to-ALT hash path end to end.

Out of scope but found while testing: the kernel's aad-only GCM finalize (empty payload) computes a wrong tag through cryptosoft - reproducible with direct CIOCCRYPT without any ALT involvement; that needs its own fix on the nuttx side.

CONFIG_MBEDTLS_CMAC_ALT has never compiled on master: cmac_alt.c
reaches into mbedtls_cipher_context_t fields (cipher_info, cmac_ctx)
that mbedtls 3.x wraps in MBEDTLS_PRIVATE(), so every reference fails
with "has no member named ..." (20 errors).  The option came in with
AES-256-CMAC support but no defconfig enables it, so CI never caught
this.

Access the fields through MBEDTLS_PRIVATE() and keep using the public
mbedtls_cipher_info_get_key_bitlen() helper, so the file compiles
against vanilla mbedtls 3.6 without MBEDTLS_ALLOW_PRIVATE_ACCESS.
While touching the file, drop an "ilen < 0" comparison that is always
false for a size_t.

Signed-off-by: makejian <makejian@xiaomi.com>
The asymmetric alt modules (ECDSA/ECDH) talk to /dev/crypto through
cryptodev_context_t, but the context only carries the symmetric
crypt_op.  Add a crypt_kop and a cryptodev_cryptk() helper so those
modules can submit CIOCKEY requests the same way they submit
CIOCCRYPT.

The kernel reports key-op hardware results through the ioctl return
value, so map failures to -errno exactly like cryptodev_crypt().
A signature verification failure thus comes back as -EBADMSG.

Signed-off-by: makejian <makejian@xiaomi.com>
The hash and MAC finish calls leave crypt.len and crypt.src set from
the last update, so the final cryptodev request re-processes the
message a second time: mbedtls_md5_finish() hashes the data twice and
mbedtls_cipher_cmac() computes CMAC(M, M) for any non-empty message.
RFC 4493 vectors for the 16/40/64-byte messages confirm the wrong
tags.

Clear len/src in every finish path (md5, sha1, sha256, sha512,
poly1305, ripemd160, cmac) so the finalize request carries no payload,
and set crypt.olen to the digest size.  olen is how many MAC bytes
the kernel copies back on the asynchronous completion path
(CIOCCRYPTRET); leaving it at 0 would make that copy vanish.

Verified on sim:crypto + software cryptodev with the alt layer
enabled: mbedtls_cipher_cmac() now returns the RFC 4493 values for
all four vector lengths (0/16/40/64 bytes), the SP 800-38B AES-256
vector, and the split update/finish flow.

Signed-off-by: makejian <makejian@xiaomi.com>
Route the SECP-256R1 ECDSA and ECDH entry points to /dev/crypto key
operations (CIOCKEY).  ecdsa_alt.c provides sign, verify and genkey
through CRK_ECDSA_SECP256R1_{SIGN,VERIFY,GENKEY}, ecdh_alt.c provides
the ephemeral key generation and shared secret computation through
CRK_ECDH_{GEN_PUBLIC,COMPUTE_SHARED}.  Both map the endianness of the
scalars: cryptodev ECC integers are little-endian while the SHA-256
hash input is big-endian, and r/s values wider than the curve are
rejected before they are submitted.  Curves other than SECP256R1
return FEATURE_UNAVAILABLE and fall back nowhere (the caller decides).

The kernel side (ECC little-endian ABI and the ECDH key ops) is in
place since the crypto asym capabilities series; the alt layer needs
the CIOCKEY wrapper from the earlier dev_alt commit.

Verified on sim:crypto + software cryptodev with the alts enabled:
an ECDSA sign/verify roundtrip with a fixed private key, a tampered
signature correctly rejected as VERIFY_FAILED, a hardware genkey ->
sign -> verify roundtrip, and ECDH where both sides compute the same
shared secret.

Signed-off-by: makejian <makejian@xiaomi.com>
Route the mbed TLS GCM entry points to the cryptodev AEAD session
(CRYPTO_AES_GCM_16 + the AES-{128,192,256} GMAC MAC).  starts() opens
the session and records the IV, update_ad()/update() stream AAD and
payload with COP_FLAG_UPDATE_AAD/COP_FLAG_UPDATE, and finish() emits
the tag and releases the session.  crypt_and_tag()/auth_decrypt() are
built from those primitives, with a constant-time tag comparison
before returning plaintext.

The IV length is not fixed to 96 bits: the driver takes it through
crypt.ivlen, so the GCM-specified [1, 2^39-256] range is supported up
to GCM_IV_MAX_SIZE.  The AAD and payload limits of SP 800-38D 5.2.1.1
are checked before the counters can wrap.

Verified on sim:crypto + software cryptodev with the alt enabled:
encryption vectors with 96-bit, 8-byte and 60-byte IVs all match the
reference implementation (python cryptography), the streaming
starts/update_ad/update/finish flow produces the same ciphertext and
tag as the one-shot call, auth_decrypt round-trips, and a tampered
tag is rejected with MBEDTLS_ERR_GCM_AUTH_FAILED.

Signed-off-by: makejian <makejian@xiaomi.com>
Route mbedtls_pkcs5_pbkdf2_hmac[_ext] to /dev/crypto: the salt is
submitted as the request payload, the password is the HMAC session
key, the iteration count goes through crypt.iterations and the
derived key length through crypt.olen.  Only PBKDF2-HMAC-SHA1 and
PBKDF2-HMAC-SHA256 are supported by the engine; every other digest
returns FEATURE_UNAVAILABLE so the caller can fall back to software.

mbed TLS 3.6 has no MBEDTLS_PKCS5_ALT guard in library/pkcs5.c, so
the tree carries a small patch (0004) wrapping the software PBKDF2
path in the usual "#if !defined(MBEDTLS_xxx_ALT)" convention, next to
the existing 0001-0003 carry-in patches.

Verified on sim:crypto + software cryptodev with the alt enabled:
the RFC 6070 HMAC-SHA1 vectors (1/2/4096 iterations) and two
HMAC-SHA256 vectors match through mbedtls_pkcs5_pbkdf2_hmac_ext().

Signed-off-by: makejian <makejian@xiaomi.com>
Add mbedtls_aes_set{128,192,256}key_{enc,dec}_keyid(): like the plain
setkey functions, but the buffer the caller hands over carries a
32-bit key id (padded to the full key size) and the session is
created with SOP_F_KEYID, so the crypto backend loads the key from
its key store instead of taking the material inline.  The key must
already live in a store the backend can reach, e.g. one populated
through CIOCKEY CRK_ALLOCATE_KEY/CRK_IMPORT_KEY/CRK_SAVE_KEY.

The regular setkey path clears SOP_F_KEYID, so a context that was
once keyed by id can be reused with plain key material.

Verified on sim:crypto + software cryptodev with the swkey key store
(/dev/nvs): a key imported and saved through CIOCKEY, then used via
mbedtls_aes_set128key_enc_keyid() + mbedtls_aes_crypt_cbc(), produces
the same ciphertext as the direct cryptodev reference call with the
real key material.

Signed-off-by: makejian <makejian@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant