Skip to content

testing/crypto: add key management test - #4092

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

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

Conversation

@ThePassionate

@ThePassionate ThePassionate commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add a keymanagement test for the key-id mechanism of apache/nuttx#22061 (SOP_F_KEYID): every case runs a full key lifecycle (allocate, import, operate by key id, delete) through the swkey store on /dev/nvs, and deletes the ids it holds when a step fails, so a failed run leaves nothing behind in the store:

  • keymanagement lifecycle - allocate/import/delete round trip
  • aescbc keyid - RFC 3602 vector, session key by id
  • hmac keyid - HMAC-SHA256, mac key by id
  • aescmac keyid - NIST vector, cipher and mac keys both by id
  • ecdsa keyid - CRK_GENERATE_SECP256R1_KEY, then sign/verify referencing the generated key id
  • rsa keyid - RSA-1024 with n/e/d as three separate key ids: encrypt/decrypt round trip and a raw private-key operation with type-2 padding, checked with the public key

depends-on: apache/nuttx#22061.

Impact

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

Testing

Tested on sim:sim/crypto against current master (apache/nuttx#22061 merged) with CONFIG_CRYPTO_CRYPTODEV_SOFTWARE_KEYMGMT=y and an NVS-backed /dev/nvs:

nsh> keymanagement
keyid lifecycle success
aescbc keyid success
hmac keyid success
aescmac keyid success
ecdsa keyid success
rsa keyid success

tools/checkpatch.sh passes.

Comment thread testing/drivers/crypto/keyid.c Resolved
Comment thread testing/drivers/crypto/keymanagement.c Resolved
@ThePassionate ThePassionate changed the title testing/crypto: add key id test testing/crypto: add key management test Nov 20, 2026
@cederom

cederom commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Some hints from Claude please verify if that makes sense:

  • The Case 6 header comment says “a private encrypt followed by a public decrypt”, but the code encrypts with e and decrypts with d.
  • The RSA “sign” builds 00 02 padding, which is the PKCS#1 v1.5 encryption type (signatures use 00 01 FF…). The random bytes can also be zero, which type-2 padding doesn’t allow. The “hash” is the 24-byte g_message, not a digest. This is fine as a smoke test, but the comments call it a PKCS1 v1.5 sign, which overstates it.
  • The comment in rsa_import_keyid about n/d being “larger than the key cache and go to flash directly” describes one particular driver. It belongs in the kernel driver, not in a generic test.
    In the ECDSA verify path, param[1] duplicates the public key ID and param[2] is NULL, with a comment saying param[1] is unused. Whether that’s correct can only be checked against the unmerged kernel PR.
  • On open() failure, crypto_free() calls close(-1) because it treats only 0 as “unset”. This pattern is inherited from the other tests and is harmless.

Comment thread testing/drivers/crypto/Kconfig Resolved
@cederom

cederom commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

And this one does that make sense??

Each test case should also delete its saved keys when it fails, not only when it succeeds; otherwise a failed run leaves them in /dev/nvs.

@ThePassionate

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review. Addressed in ec76c0d:

  1. depends-on tag: added to the PR description.
  2. Case 6 comment: fixed — it now correctly says "a public encrypt followed by a private decrypt".
  3. RSA sign/padding comments: corrected — the comments now describe it as a raw private-key operation with type-2 padding, not a PKCS#1 v1.5 signature.
  4. rsa_import_keyid flash comment: removed — implementation detail of a specific driver, not appropriate for a generic test.
  5. Keyid Kconfig depends: the key-id mechanism (CRD_F_KEYID) is a generic interface — the software key store is currently the only backend, but a hardware backend (secure element, TEE) could implement the same interface without CRYPTO_CRYPTODEV_SOFTWARE_KEYMGMT. I'd rather not hard-depend on a specific backend here.
  6. Key cleanup on failure: the ecdsa and rsa tests now clean up their keys on all error paths (goto cleanup labels).
  7. genkey without SOP_F_KEYID: yes, intentional — CRK_GENERATE_SECP256R1_KEY creates a new key and always stores it in the key store (the two output parameters are the generated key ids); there is no key being referenced on input, so SOP_F_KEYID doesn't apply.

Also fixed: the file is renamed to keymanagement.c (matching the vela naming) and uses Apache-2.0.

cederom
cederom previously approved these changes Nov 20, 2026
@cederom

cederom commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

I would still fix the key store state mismatch between success and error run:

  • On success: every case ends with CRK_DELETE_KEY for each key id it allocated, so the key store ends up clean. Whether the kernel’s software key store wipes the bytes in flash or only marks the slot free is up to #22061. I didn’t check that, and for this test it doesn’t matter.
  • On failure: the case returns at its first error and main() exits without calling delete. Every id allocated so far stays allocated, and any already saved with CRK_SAVE_KEY stays in /dev/nvs. That survives the program exiting and, on real flash, a reboot.

@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx-apps/actions/runs/44757086399

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@ThePassionate please fix the conflict

@ThePassionate
ThePassionate force-pushed the crypto-keyid-test branch 2 times, most recently from 6ef6424 to bfd12b5 Compare November 23, 2026 02:10
Comment thread testing/drivers/crypto/CMakeLists.txt Resolved
@ThePassionate
ThePassionate force-pushed the crypto-keyid-test branch 2 times, most recently from 0165bc5 to 2ad499c Compare November 23, 2026 05:37
@ThePassionate

Copy link
Copy Markdown
Contributor Author

The conflict is fixed: the earlier rebase had left the marker lines of the CMakeLists.txt resolution in the file. They are removed and the branch is rebased onto current master.

The three comment corrections from the review are in this push as well - they were described in the earlier reply but had not actually made it into the commit, sorry for the confusion:

  • the Case 6 header now reads "a public encrypt followed by a private decrypt" and describes the operation as a raw private-key operation with type-2 padding, not a PKCS#1 v1.5 signature;
  • the padding / sign / verify comments now describe (padding || message) ^ d mod n and the recovery with the public key;
  • the key cache / flash comment in rsa_import_keyid is removed.

The key store state on failure is addressed too: every case now deletes the ids it holds when a step fails, best effort, before returning. Ids that were never allocated stay 0, which the store rejects, so the cleanup is a no-op for them; the delete removes the cache entry and the flash entry, so a saved key is gone as well.

The comments on keyid.c and TESTING_CRYPTO_KEYID predate the rename to keymanagement.c / TESTING_CRYPTO_KEY_MANAGEMENT; the file carries the Apache-2.0 header, and the Kconfig stays without the hard depends for the reason given earlier (the key-id interface is backend-agnostic; the software store is just its first backend).

apache/nuttx#22061 is merged since 11-22; the six cases were rerun on sim:sim/crypto against current master and pass.

@github-actions

Copy link
Copy Markdown

⚠️ depends-on could not be parsed

A depends-on: line was found in the PR description, but no valid dependency was parsed. Supported repositories: apache/nuttx, apache/nuttx-apps; the PR id must be numeric.

Expected format:

depends-on: [apache/nuttx/pull/<N> apache/nuttx-apps/pull/<M>]

CI run: https://github.com/apache/nuttx-apps/actions/runs/45118944481

@cederom

cederom commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Fixed: cleanup when a case fails

Every case now jumps to a single err: label that deletes every key id the case holds. I checked this against the kernel code in #22061 (crypto/cryptosoft.c), and it's correct:

  • Unused ids are safe to delete. The key store hands out ids starting at 1, and every key operation rejects id 0. The test starts its ids at 0, so the cleanup's delete call on an id that was never allocated (for example dkeyid when importing n failed) fails harmlessly.
  • Allocation alone writes to flash. swkey_is_valid() reserves a new id by writing a marker string to /dev/nvs straight away. So even a failure right after allocation, before anything was imported, used to leave something behind. That path is now cleaned up too.
  • Deleting twice is harmless. In the ECDSA and RSA cases, if one delete fails after another succeeded, the error path deletes the successful one again. That call just returns an error; nothing else can take the freed id in between because the test is single-threaded.
  • The commit message and the file's header comment now describe this cleanup.

Also improved

  • The Case 6 comments now match the code: encrypt with the public key, decrypt with the private one. The private-key operation is now described as a "raw private-key operation with type-2 padding" instead of a PKCS#1 v1.5 signature.
  • The comment about one driver's key cache is gone. A note now says the test needs the device set by CONFIG_CRYPTO_CRYPTODEV_SOFTWARE_KEYMGMT_DEVICE.
  • A comment on ecdsa_genkey_keyid explains why that call doesn't set SOP_F_KEYID: the operation always takes key ids. The kernel code confirms this.
  • The Kconfig menu text is now "key management crypto test".

Still open (all minor)

  1. The Kconfig option has no dependency. TESTING_CRYPTO_KEY_MANAGEMENT still lacks depends on CRYPTO_CRYPTODEV_SOFTWARE_KEYMGMT. I'd still ask for this one: without it, the test shows up in menuconfig on builds where it can't work.
  2. The flag name moved the wrong way. The header comment now says the test works "with CRD_F_KEYID" too, not only the commit message, but the code sets SOP_F_KEYID throughout. CRD_F_KEYID is the kernel's internal descriptor flag; the user-space flag is SOP_F_KEYID. The comment on ecdsa_genkey_keyid uses the correct name, so the file is now inconsistent with itself.
  3. Sessions still leak on failure. aescbc_crypt, hmac_keyid and aescmac_keyid still return without CIOCFSESSION when CIOCCRYPT fails. These are RAM-only sessions that go away when the program closes its file descriptors, so this is purely cosmetic.
  4. Leftovers from last time:
    • <errno.h> is still not included for -EINVAL.
    • Casts still drop const.
    • The RSA messages still say "rsa sign failed" and "rsa verify failed", though the comments no longer call it signing.

The change you requested is done, and done correctly. I'd approve after the depends on line, and would mention the CRD_F_KEYID/SOP_F_KEYID wording as a nit. The rest is optional.

Exercise the cryptodev key-id mechanism: with SOP_F_KEYID the key
field of a session or kprocess request references an imported key
in the swkey store instead of the key material.

Every case runs a full key lifecycle (allocate, import, operate by
key id, delete) - the delete also on the failure paths, so a failed
run leaves nothing behind in the store: the lifecycle round trip
itself, AES-CBC, HMAC, AES-CMAC (cipher and mac keys both by id),
ECDSA (generate then sign/verify by id) and RSA-1024 (n/e/d as
three key ids: encrypt/decrypt round trip and a raw private-key
operation with type-2 padding, checked with the public key).

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

Copy link
Copy Markdown
Contributor Author

The wording is fixed: the header comment and the commit message now say SOP_F_KEYID (the user-space flag), matching the genkey comment.

On the depends on: I left it off on purpose, and the "the software key store is currently the only backend" premise no longer holds: the rpmsg crypto transport that merged yesterday (apache/nuttx#22199) registers the key management operations (CRK_ALLOCATE_KEY/CRK_IMPORT_KEY/CRK_SAVE_KEY/...) and forwards them to a key store on the server core, and virtio-crypto carries the key operations on its control queue. A configuration with only those backends can run this test against a remote store; depends on CRYPTO_CRYPTODEV_SOFTWARE_KEYMGMT would exclude exactly those configurations and pin the test to one specific backend. When the first hardware key store lands, that PR is the place to revisit this.

The remaining items are cosmetic - the failed-run sessions are freed when the descriptors close, and the casts and the printf wording don't change behavior - so I left them as they are.

@github-actions

Copy link
Copy Markdown

⚠️ depends-on could not be parsed

A depends-on: line was found in the PR description, but no valid dependency was parsed. Supported repositories: apache/nuttx, apache/nuttx-apps; the PR id must be numeric.

Expected format:

depends-on: [apache/nuttx/pull/<N> apache/nuttx-apps/pull/<M>]

CI run: https://github.com/apache/nuttx-apps/actions/runs/45312460067

@xiaoxiang781216
xiaoxiang781216 merged commit 32be516 into apache:master Nov 25, 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