Skip to content

Create EC keys with TPM capabilities matching object attributes - #210

Merged
dgarske merged 2 commits into
wolfSSL:masterfrom
LinuxJedi:tpm-ecdh-derive-fix
Aug 7, 2026
Merged

Create EC keys with TPM capabilities matching object attributes#210
dgarske merged 2 commits into
wolfSSL:masterfrom
LinuxJedi:tpm-ecdh-derive-fix

Conversation

@LinuxJedi

Copy link
Copy Markdown
Member

EC keys generated on a TPM slot were created with a sign-only ECDSA template through the crypto callback, which the TPM will not accept for ECDH. The software copy of a TPM key holds only the public point, so it does not carry the private scalar the agreement needs either. A recent wolfSSL change to shared secret validation surfaced this in CI.

Create generated EC keys directly on the TPM with capabilities matching the object attributes: CKA_SIGN maps to the TPM sign attribute and CKA_DERIVE to the decrypt attribute that TPM2_ECDH_ZGen requires, with the null scheme when both are set. The crypto callback key generation is no longer used as it only creates signing keys. Curves the TPM does not support still fall back to software generation.

Compute ECDH for TPM-resident keys with TPM2_ECDH_ZGen. Keys created without the decrypt attribute, from a store written before derive support, return an error as the TPM will not perform ECDH with them.

Also clear crypto callback key references when freeing an object so the callback cannot act on freed memory, and have the ECDH tests check the derived secret so an incomplete agreement is caught.

EC keys generated on a TPM slot were created with a sign-only ECDSA
template through the crypto callback, which the TPM will not accept for
ECDH. The software copy of a TPM key holds only the public point, so it
does not carry the private scalar the agreement needs either. A recent
wolfSSL change to shared secret validation surfaced this in CI.

Create generated EC keys directly on the TPM with capabilities matching
the object attributes: CKA_SIGN maps to the TPM sign attribute and
CKA_DERIVE to the decrypt attribute that TPM2_ECDH_ZGen requires, with
the null scheme when both are set. The crypto callback key generation is
no longer used as it only creates signing keys. Curves the TPM does not
support still fall back to software generation.

Compute ECDH for TPM-resident keys with TPM2_ECDH_ZGen. Keys created
without the decrypt attribute, from a store written before derive
support, return an error as the TPM will not perform ECDH with them.

Also clear crypto callback key references when freeing an object so the
callback cannot act on freed memory, and have the ECDH tests check the
derived secret so an incomplete agreement is caught.
Copilot AI lite review requested due to automatic review settings August 4, 2026 16:33

Copilot AI left a comment

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.

Pull request overview

This PR fixes TPM-backed EC key generation and ECDH behavior so keys created on a TPM slot have TPM capabilities that match PKCS#11 object attributes (sign vs derive), and ECDH for TPM-resident keys is performed inside the TPM to avoid deriving with missing private material.

Changes:

  • Generate supported EC keys directly on the TPM with sign/decrypt attributes derived from CKA_SIGN/CKA_DERIVE, falling back to software for unsupported curves.
  • Compute ECDH for TPM-resident keys via TPM2_ECDH_ZGen, and error out for older sign-only TPM keys that lack the decrypt attribute.
  • Harden cleanup and tests: clear TPM crypto-callback key references on object free, and add an ECDH test check to catch “derived secret is all zeros” failures.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/pkcs11test.c Adds a guard in the ECDH test to fail if the derived secret is all zeros (catches incomplete/invalid derive behavior).
src/internal.c Implements TPM EC keypair generation with attribute-matched capabilities, TPM-based ECDH using TPM2_ECDH_ZGen, and clears TPM callback key references during object free.

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

Comment thread src/internal.c
@dgarske
dgarske self-requested a review August 4, 2026 16:47
The TPM ECC scheme is a TPMT_SIG_SCHEME, whose union members all share
the leading hash algorithm and which has no ECDH member. Set the scheme
hash through the common member the marshalling reads, as the scheme here
may be ECDSA, ECDH or NULL.

test_ecc_curve was excluded from TPM builds because it derives with a
key generated for derive only, which the TPM would not perform ECDH
with. That path now works, so build the test for TPM as well. It is the
only coverage of an ECDH scheme key generated on the TPM.
@dgarske
dgarske merged commit 79249f7 into wolfSSL:master Aug 7, 2026
82 checks passed
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