Код: Выделить всё
tpm2_createprimary -C e -g sha256 -G ecc -c primary.ctx -a "fixedtpm|fixedparent|sensitivedataorigin|userwithauth|sign"
tpm2_evictcontrol -C o -c primary.ctx 0x81000000 # make the key persistent
echo "data to sign" > data.in.raw # create raw data
sha256sum data.in.raw | awk '{ print "000000 " $1 }' | xxd -r -c 32 > data.in.digest # create digest
tpm2_sign -Q -c 0x81000000 -g sha256 -d -f plain -o data.out.signed data.in.digest
Код: Выделить всё
import tpm2_pytss
# Initialize TPM
tpm = tpm2_pytss.ESAPI(tcti=None)
tpm.startup(tpm2_pytss.TPM2_SU.CLEAR)
# Handle for the persistent key
handle = tpm.tr_from_tpmpublic(0x81000000)
# Inspect public key data
public_data, _, _ = tpm.read_public(handle)
public_area = public_data.publicArea
ecc_params = public_area.parameters.eccDetail
print(f"Key Type: {public_area.type}")
print(f"Name Algorithm: {public_area.nameAlg}")
print(f"Attributes: {public_area.objectAttributes}")
print(f"ECC Curve: {ecc_params.curveID}")
print(f"ECC Scheme: {ecc_params.scheme.scheme}")
# Data to be signed
data = b"data to sign"
# Generate digest and validation ticket
digest, validation = tpm.hash(
data,
hash_alg=tpm2_pytss.TPM2_ALG.SHA256,
hierarchy=tpm2_pytss.ESYS_TR.NULL
)
# Set up ECDSA signing scheme
signing_scheme = tpm2_pytss.TPMT_SIG_SCHEME(
scheme=tpm2_pytss.TPM2_ALG.ECDSA,
details=tpm2_pytss.TPMU_SIG_SCHEME() # Empty scheme for ECDSA
)
# Attempt to sign the digest using TPM
try:
signature = tpm.sign(
handle, # Key handle in TPM
digest, # Generated digest
in_scheme=signing_scheme, # ECDSA signing scheme
validation=validation # Validation ticket
)
print(f"Generated Signature: {signature}")
except tpm2_pytss.TSS2_Exception as e:
print(f"Error while signing: {e}")
Код: Выделить всё
Key Type: ecc
Name Algorithm: sha256
Attributes: fixedtpm|fixedparent|sensitivedataorigin|userwithauth|sign
ECC Curve: nist_p256
ECC Scheme: null
WARNING:esys:src/tss2-esys/api/Esys_Sign.c:314:Esys_Sign_Finish() Received TPM Error
ERROR:esys:src/tss2-esys/api/Esys_Sign.c:108:Esys_Sign() Esys Finish ErrorCode (0x000002c3)
Error while signing: tpm:parameter(2):hash algorithm not supported or not appropriate
Код: Выделить всё
I verified the key attributes and scheme using tpm.read_public(). The scheme shows as null, which might be the issue.
I've used the terminal commands to confirm the key works correctly for signing.
Adjusted the signing scheme in Python to match the expected ECDSA parameters.
Код: Выделить всё
How can I correctly specify the signing scheme for the TPM2_ALG.ECDSA key in tpm2-pytss?
Is there an issue with how I am setting up the digest or validation ticket?
Should the key's scheme (null) be explicitly set during creation to support ECDSA?
Подробнее здесь: https://stackoverflow.com/questions/792 ... en-signing