Create EC keys with TPM capabilities matching object attributes - #210
Merged
Conversation
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.
Contributor
There was a problem hiding this comment.
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.
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
approved these changes
Aug 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.