Skip to content

cache-only shared secrets#372

Open
bigbrett wants to merge 4 commits into
wolfSSL:mainfrom
bigbrett:cached-shared-secret
Open

cache-only shared secrets#372
bigbrett wants to merge 4 commits into
wolfSSL:mainfrom
bigbrett:cached-shared-secret

Conversation

@bigbrett
Copy link
Copy Markdown
Contributor

@bigbrett bigbrett commented May 18, 2026

This PR adds cache-mode shared-secret support so ECDH and X25519 outputs can remain server-resident instead of always being returned to the client.

Changes:

  • Extends ECDH and Curve25519 request/response messages with cache flags, output key IDs, and labels.
  • Adds client/server handling for cached shared-secret outputs using raw key-cache import.
  • Adds crypto tests covering cached ECDH/X25519 secrets, caller-supplied IDs, NONEXPORTABLE behavior, and guard cases.

Copilot AI review requested due to automatic review settings May 18, 2026 20:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds cache-mode shared-secret support so ECDH and X25519 outputs can remain server-resident instead of always being returned to the client.

Changes:

  • Extends ECDH and Curve25519 request/response messages with cache flags, output key IDs, and labels.
  • Adds client/server handling for cached shared-secret outputs using raw key-cache import.
  • Adds crypto tests covering cached ECDH/X25519 secrets, caller-supplied IDs, NONEXPORTABLE behavior, and guard cases.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
wolfhsm/wh_server_crypto.h Declares raw key-cache import helper for shared-secret/KDF outputs.
wolfhsm/wh_message_crypto.h Extends ECDH and Curve25519 message structs for cache-mode semantics.
wolfhsm/wh_client_crypto.h Adds public cache-mode shared-secret APIs and async Curve25519 shared-secret APIs.
src/wh_server_crypto.c Implements cache-mode ECDH/X25519 server handling and raw cache import helper.
src/wh_message_crypto.c Translates new message fields.
src/wh_client_crypto.c Refactors shared-secret client paths and adds cache-key request/response wrappers.
test/wh_test_crypto.c Adds ECDH and X25519 cache-mode shared-secret coverage.
test/wh_test_check_struct_padding.c Includes Curve25519 message structs in padding checks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #372

Scan targets checked: wolfhsm-core-bugs, wolfhsm-crypto-bugs, wolfhsm-src

No new issues found in the changed files. ✅

@bigbrett bigbrett marked this pull request as ready for review May 19, 2026 05:47
@bigbrett bigbrett requested a review from rizlik May 19, 2026 05:48
Comment thread src/wh_server_crypto.c
}
/* Scrub the secret from the response buffer regardless of import
* success/failure. */
memset(res_out, 0, res_len);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you consider getting a cache slot with wh_Server_KeystoreGetCacheSlotChecked before the wc_ecc_shared_secret operation to avoid a copy and a memset?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I thought about this but it means that the crypto would have to occur inside the critical section, otherwise there could be a race on getting a cache slot for global keys. For now I think its best to follow the pattern of the other handlers that work similarly. Would rather pay for a small copy inside the critical section than pay for a public key crypto operation

Comment thread src/wh_server_crypto.c Outdated
out_key_id = wh_KeyId_TranslateFromClient(
WH_KEYTYPE_CRYPTO, ctx->comm->client_id, req.keyId);
if (WH_KEYID_ISERASED(out_key_id)) {
ret = wh_Server_KeystoreGetUniqueId(ctx, &out_key_id);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is probably racey under THREAD_SAFE

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

bigbrett added 4 commits May 29, 2026 10:00
Extend EccSharedSecret and Curve25519SharedSecret APIs with CacheKey
variants (sync + async request/response) that store the derived secret
in a server key cache slot instead of returning it to the client. Adds
flags/keyId/label fields to the ECDH and Curve25519 messages, a shared
wh_Server_KeyCacheImportRaw helper, and crypto tests covering both
paths.
When the blocking CacheKey API auto-imports local input keys, the server
allocates their keyIds from WH_KEYID_IDMAX downward (0xFF, 0xFE, ...).
If the caller picks an output keyId that collides with one of those
auto-imported inputs, the EVICTPUB/EVICTPRV cleanup runs after the
import and deletes the cache slot we just wrote the secret to, while
still returning success and the keyId.

Clear the matching evict flag after a successful import so cleanup
leaves the cached secret alone
@bigbrett bigbrett force-pushed the cached-shared-secret branch from 241948c to 362f093 Compare May 29, 2026 16:13
@bigbrett bigbrett requested a review from rizlik May 29, 2026 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants