From ed6b647662965304ec48b5f4f4b30eb7020008e5 Mon Sep 17 00:00:00 2001 From: Jordan Hand Date: Fri, 8 Dec 2023 17:05:20 -0800 Subject: [PATCH] Load X,Y public key into TPM with correct padding big.Int.Bytes() will trim any leading zeros. But when passed to the TPM, the X,Y coordinates of an ECC point have to be properly left-padded to the ECC int size for that curve. Ensure the integer is padded properly. --- verification/tpm.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/verification/tpm.go b/verification/tpm.go index 838ddc77..46ec66f2 100644 --- a/verification/tpm.go +++ b/verification/tpm.go @@ -197,6 +197,11 @@ func loadPubKey(t *testing.T, pubKey any, tpm io.ReadWriteCloser, alg tpm2.Algor // Create a tpm2.Public structure from the parsed ECDSA public key switch pubKey := pubKey.(type) { case *ecdsa.PublicKey: + byteSize := pubKey.Params().BitSize / 8 + x := make([]byte, byteSize) + y := make([]byte, byteSize) + x = pubKey.X.FillBytes(x) + y = pubKey.Y.FillBytes(y) tpmPublic = tpm2.Public{ Type: tpm2.AlgECC, // ECDSA key type NameAlg: alg, @@ -208,8 +213,8 @@ func loadPubKey(t *testing.T, pubKey any, tpm io.ReadWriteCloser, alg tpm2.Algor }, CurveID: ec, Point: tpm2.ECPoint{ - XRaw: new(big.Int).SetBytes(pubKey.X.Bytes()).Bytes(), - YRaw: new(big.Int).SetBytes(pubKey.Y.Bytes()).Bytes(), + XRaw: x, + YRaw: y, }, }, }