Skip to content

Commit

Permalink
tpm2: Implement TPM2_TestParms (#352)
Browse files Browse the repository at this point in the history
Signed-off-by: Morten Linderud <morten@linderud.pw>
  • Loading branch information
Foxboron authored Apr 11, 2024
1 parent cfdeb6e commit 1fb8444
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tpm2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ const (
TPMCCPolicyPhysicalPresence TPMCC = 0x00000187
TPMCCPolicyDuplicationSelect TPMCC = 0x00000188
TPMCCPolicyGetDigest TPMCC = 0x00000189
TPMCCTestParams TPMCC = 0x0000018A
TPMCCTestParms TPMCC = 0x0000018A
TPMCCCommit TPMCC = 0x0000018B
TPMCCPolicyPassword TPMCC = 0x0000018C
TPMCCZGen2Phase TPMCC = 0x0000018D
Expand Down
10 changes: 10 additions & 0 deletions tpm2/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -2796,6 +2796,16 @@ func (u *TPMUPublicParms) ECCDetail() (*TPMSECCParms, error) {
return nil, fmt.Errorf("did not contain eccDetail (selector value was %v)", u.selector)
}

// TPMTPublicParms represents a TPMT_PUBLIC_PARMS.
// See definition in Part 2: Structures, section 12.2.3.8.
type TPMTPublicParms struct {
marshalByReflection
// algorithm to be tested
Type TPMIAlgPublic
// algorithm details
Parameters TPMUPublicParms `gotpm:"tag=Type"`
}

// TPMTPublic represents a TPMT_PUBLIC.
// See definition in Part 2: Structures, section 12.2.4.
type TPMTPublic struct {
Expand Down
110 changes: 110 additions & 0 deletions tpm2/test/test_parms_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package tpm2test

import (
"errors"
"testing"

. "github.com/google/go-tpm/tpm2"
"github.com/google/go-tpm/tpm2/transport/simulator"
)

func TestTestParms(t *testing.T) {
thetpm, err := simulator.OpenSimulator()
if err != nil {
t.Fatalf("could not connect to TPM simulator: %v", err)
}
defer thetpm.Close()

for _, tt := range []struct {
name string
parms TPMTPublicParms
wantErr error
}{
{
"p256",
TPMTPublicParms{
Type: TPMAlgECC,
Parameters: NewTPMUPublicParms(
TPMAlgECC,
&TPMSECCParms{
CurveID: TPMECCNistP256,
},
),
},
nil,
},
{
"p364",
TPMTPublicParms{
Type: TPMAlgECC,
Parameters: NewTPMUPublicParms(
TPMAlgECC,
&TPMSECCParms{
CurveID: TPMECCNistP384,
},
),
},
nil,
},
{
"p521",
TPMTPublicParms{
Type: TPMAlgECC,
Parameters: NewTPMUPublicParms(
TPMAlgECC,
&TPMSECCParms{
CurveID: TPMECCNistP521,
},
),
},
nil,
},
{
"rsa2048",
TPMTPublicParms{
Type: TPMAlgRSA,
Parameters: NewTPMUPublicParms(
TPMAlgRSA,
&TPMSRSAParms{
KeyBits: 2048,
},
),
},
nil,
},
{
"rsa3072 - unsupported",
TPMTPublicParms{
Type: TPMAlgRSA,
Parameters: NewTPMUPublicParms(
TPMAlgRSA,
&TPMSRSAParms{
KeyBits: 3072,
},
),
},
TPMRCValue,
},
{
"rsa4096 - unsupported",
TPMTPublicParms{
Type: TPMAlgRSA,
Parameters: NewTPMUPublicParms(
TPMAlgRSA,
&TPMSRSAParms{
KeyBits: 4096,
},
),
},
TPMRCValue,
},
} {
t.Run(tt.name, func(t *testing.T) {
grc := TestParms{Parameters: tt.parms}
_, err := grc.Execute(thetpm)
if !errors.Is(err, tt.wantErr) {
t.Fatalf("TestParms failed failed. Expecting err %v got %v", tt.wantErr, err)
}
})
}
}
22 changes: 22 additions & 0 deletions tpm2/tpm2.go
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,28 @@ type GetCapabilityResponse struct {
CapabilityData TPMSCapabilityData
}

// TestParms is the input to TPM2_TestParms.
// See definition in Part 3, Commands, section 30.3
type TestParms struct {
// Algorithms parameters to be validates
Parameters TPMTPublicParms
}

// Command implements the Command interface.
func (TestParms) Command() TPMCC { return TPMCCTestParms }

// Execute executes the command and returns the response.
func (cmd TestParms) Execute(t transport.TPM, s ...Session) (*TestParmsResponse, error) {
var rsp TestParmsResponse
if err := execute[TestParmsResponse](t, cmd, &rsp, s...); err != nil {
return nil, err
}
return &rsp, nil
}

// TestParmsResponse is the response from TPM2_TestParms.
type TestParmsResponse struct{}

// NVDefineSpace is the input to TPM2_NV_DefineSpace.
// See definition in Part 3, Commands, section 31.3.
type NVDefineSpace struct {
Expand Down

0 comments on commit 1fb8444

Please sign in to comment.