Skip to content

testing/crypto: add async crypto test - #4093

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
ThePassionate:crypto-async-test
Nov 21, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
ThePassionate:crypto-async-test

Conversation

@ThePassionate

@ThePassionate ThePassionate commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add an async test for the asynchronous cryptodev path of apache/nuttx#22062 (COP_FLAG_CBIMM submit + CIOCCRYPTRET fetch): single-block and multi-block AES-CBC, SHA-256 one-shot and multi-update, AES-CMAC and AES-GCM/GMAC with the tag fetched asynchronously, cancellation of a queued request (EAGAIN on fetch, ENOENT for an unknown id), and three concurrent pthread workers with private descriptors.

Depends-on: apache/nuttx#22062.

Impact

  • Testing only. New TESTING_CRYPTO_ASYNC option (default n).

Testing

On sim:sim/crypto with that kernel PR: all nine cases listed above pass (async test ok). Expected values cross-checked against python cryptography on the host.

tools/checkpatch.sh (via the paired nuttx tree) passes.

Exercise the asynchronous path of /dev/crypto: submit CIOCCRYPT with
COP_FLAG_CBIMM, block on poll(2) for completion and retrieve the result
through CIOCCRYPTRET.  Every result is checked against a known answer
rather than only a self round trip, so a backend that is wrong in both
directions still fails.  The vectors are RFC 3602 (AES-CBC, one block
and four blocks), FIPS 180-2 (SHA-256, one shot and four 64-byte
updates), NIST SP 800-38B (AES-CMAC, update plus asynchronous final) and
NIST SP 800-38D test case 4 (AES-GCM and GMAC, the tag comes from the
asynchronous final request).

Also cover cancellation: cancelling a submitted reqid drops its result
whether the operation was still in flight or already queued, a later
retrieval reports EAGAIN, and an unknown reqid reports ENOENT.  A
concurrent section runs three submitters with private descriptors,
each doing ten asynchronous rounds against a known answer.

Signed-off-by: makejian <makejian@xiaomi.com>
@cederom

cederom commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Thanks @ThePassionate, my remarks:

  • please remember to provide test logs for reviewers to be able to repeat local build and runtime!

  • If you use AI you must provide Assisted-by: tag in the git commit messages and PR descriptions.

  • A failure after submitting leaves the request outstanding, and that can break later tests. In async_cbc, async_sha256, async_cmac and async_aead, if the request is submitted and then poll() times out or CIOCCRYPTRET fails, the code goes to err: and frees the session without cancelling the reqid. Two problems follow on a real hardware backend:

    • The kernel request still points at cse->key (crde->crd_key = cse->key, cryptodev.c:588), so CIOCFSESSION frees a key the hardware may still be reading. It also still points at the caller’s stack iv[] and src. Only the output is copied into a kernel buffer.
    • In cryptof_poll, POLLIN means “the completion queue isn’t empty”. A late completion left over from a failed case makes the next case’s poll() return at once. Its CIOCCRYPTRET for the new reqid then returns EAGAIN, so one failure turns into a run of false failures.

    Fix: after a successful submit, send COP_FLAG_CANCEL for the reqid on every error path. Optionally, loop poll → RET while it returns EAGAIN, so the test doesn’t assume its own result is the one that woke it.

  • On sim, the “cancel while in flight” path never runs. With a software backend, cryptodev_op() finishes the request inline through crypto_invoke() → callback. So by the time async_cancel cancels, the result is already on crp_ret, and only the “already completed” branch runs. The in-flight branch (setting CRYPTO_F_CANCEL, the callback discarding the result, cryptodev_cancel_dispatch) is never reached, even though it’s the trickiest part of #22062. The commit message says both cases are covered. Either reword it (“on a software backend only the completed-result path is exercised”) or add a case that is guaranteed to still be in flight. That needs a slow or hardware backend, or a test hook.

  • check_concurrent() mishandles a failed pthread_create (lines 847–853):
    If the second or third create fails, the threads already started are never joined. They keep writing to wa_* on the stack after the function returns.
    pthread_create returns its error code and doesn’t set errno, so warn() prints the wrong error.
    Fix: create the threads one at a time, join whatever started, and use warnx("pthread_create: %s", strerror(ret)).

  • Minor remarks:

    • Reused reqid: async_cancel uses 0x7001, which the GMAC case at line 933 also uses. That’s harmless when GMAC succeeds. But if GMAC times out, the cancel test cancels GMAC’s stale request and passes for the wrong reason. Use a distinct ID, e.g. 0x8001.
    • Kconfig dependencies: TESTING_CRYPTO_ASYNC should at least depends on !DISABLE_PTHREAD. Ideally it would also depend on the software-crypto/AES-GCM/CMAC support it needs. Existing entries don’t declare such dependencies either, so this is optional.
    • Thread stacks: the workers run on the default pthread stack (CONFIG_PTHREAD_STACK_DEFAULT, not TESTING_CRYPTO_STACKSIZE). worker_sha keeps 256+32 bytes locally and then calls warn()/printf. That’s fine on sim but could be tight on small boards; consider pthread_attr_setstacksize().
    • Headers: bool and uint32_t are used without <stdbool.h>/<stdint.h>. They work only because other headers pull them in.
    • Comment wording: the async_ctx_s comment says “cloned descriptor”, but the code uses CRIOGET, which creates a new descriptor, not CIOCCLONE. The comment at line 937 says “cancel a completed request”, while the commit message says “in flight or queued”. Make them agree with point 2.
    • String initialiser: g_aes_plain[16] = "Single block msg" drops the terminating NUL. That’s valid C, but GCC 15’s -Wunterminated-string-initialization (in -Wextra) will warn. aescbc.c does the same, so this only matters if the CI toolchain moves to GCC 15 with -Werror.
    • Early exit message: if async_open fails, main returns without printing “async test failed”, so log-scraping CI sees no result line.
    • Program name: async is a very generic NSH command name. clone sets a precedent, but something like cryptoasync would be less likely to collide.
    • Kernel side (crypto: add an asynchronous path for symmetric cryptodev operations nuttx#22062): CIOCFSESSION doesn’t check whether the session still has requests in flight, and those requests point at the session’s keys. The test should avoid triggering that, but the kernel PR should probably refuse with EBUSY or take a reference.

@cederom

cederom commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

This is a test response from Claude:

Thanks, this is a useful test. I checked all the known-answer vectors independently (Python cryptography + hashlib): AES-CBC 1 and 4 blocks, SHA-256 "abc" and 256×0xaa, CMAC, GCM ciphertext/tag and GMAC tag all match.

Three things I'd like fixed before merge, then some nits.

1. Error paths after an async submit don't cancel the reqid

async.c L356 (same at L435, L514, L621)

If async_wait_ready() or CIOCCRYPTRET fails, the request is still outstanding when we jump to err: and free the session. With a hardware backend:

  • the in-flight cryptop still has crd_key = cse->key, which CIOCFSESSION frees, and crp_buf/crp_iv point at the caller's buffer and the function's stack iv[] (only the output is copied into the kernel shadow buffer);
  • cryptof_poll is level-triggered on crp_ret being non-empty, so when this result arrives late the next test's poll() returns immediately and its CIOCCRYPTRET gets EAGAIN. One failure then shows up as several.

Suggest a small helper that sends COP_FLAG_CANCEL for reqid, called on every error path after a successful submit (all four helpers). Optionally, loop poll → CIOCCRYPTRET while it returns EAGAIN, so the test doesn't assume the wakeup is for its own reqid.

2. "Cancel while in flight" is never exercised on sim

async.c L655

On software backends cryptodev_op() finishes the request inline through crypto_invoke() → cryptodevop_cb(), so by the time we cancel, the result is always already on crp_ret. The in-flight branch (CRYPTO_F_CANCEL set, the callback discarding the result, cryptodev_cancel_dispatch()) is never reached, even though it's the trickiest part of apache/nuttx#22062. Please either say so in the comment and the commit message ("on a software backend only the completed-result path is exercised"), or add a case that is guaranteed to be in flight when cancelled.

3. check_concurrent() leaks threads on pthread_create failure

async.c L847

If the 2nd or 3rd pthread_create() fails, the threads already started are never joined and keep writing to wa_* after the function returns. Also, pthread_create() returns the error code and doesn't set errno, so warn() prints a stale error. Suggest creating the threads one by one, joining whatever started, and reporting with warnx("pthread_create: %s", strerror(ret)).

Minor: the workers run on CONFIG_PTHREAD_STACK_DEFAULT, not TESTING_CRYPTO_STACKSIZE. worker_sha keeps ~290 bytes locally plus warn()/printf, which may be tight on small boards. pthread_attr_setstacksize() would make it deterministic.

Nits

  • async.c L700: reqid 0x7001 is also the reqid of the GMAC case (L933). If GMAC times out, this cancel would match its pending request and pass for the wrong reason. Use a distinct id, e.g. 0x8001.
  • async.c L27: bool (L549) and uint32_t are used, but <stdbool.h>/<stdint.h> aren't included. They only work because other headers pull them in.
  • async.c L69: this is a new descriptor from CRIOGET, not a CIOCCLONE clone. "private descriptor" would avoid confusion with clone.c.
  • async.c L96: a 16-char literal into [16] drops the NUL. That's valid C (and aescbc.c does the same), but GCC 15 warns via -Wunterminated-string-initialization under -Wextra.
  • async.c L885: on async_open() failure we return without printing async test failed, so log-scraping CI sees no result line.
  • async.c L937: the comment says "cancel a completed request", the commit message says "in flight or already queued". Please make them agree (see 2).
  • Kconfig L163: should this depends on !DISABLE_PTHREAD? It also needs AES-GCM/CMAC/SHA-256 in the cryptodev backend (e.g. CRYPTO_CRYPTODEV_SOFTWARE_CRYPTO). Other entries don't declare backend dependencies, so that part is optional.
  • async is a very generic NSH command name. cryptoasync would be less likely to collide.

Related, for apache/nuttx#22062

CIOCFSESSION doesn't check for in-flight requests that still point at the session's keys. Probably worth returning EBUSY or taking a reference there.

@xiaoxiang781216
xiaoxiang781216 merged commit 5d3ecff into apache:master Nov 21, 2026
41 checks passed
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.

4 participants