Skip to content

Commit

Permalink
Fixed test, nits and added additional comments on maxListLength
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Tsai committed Jun 29, 2022
1 parent f3b49a4 commit f4c8bf9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
99 changes: 49 additions & 50 deletions direct/tpm2/combined_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,112 +7,111 @@ import (
"github.com/google/go-tpm/direct/structures/tpm"
"github.com/google/go-tpm/direct/structures/tpm2b"
"github.com/google/go-tpm/direct/structures/tpma"
"github.com/google/go-tpm/direct/structures/tpml"
"github.com/google/go-tpm/direct/structures/tpms"
"github.com/google/go-tpm/direct/structures/tpmt"
"github.com/google/go-tpm/direct/structures/tpmu"
"github.com/google/go-tpm/direct/transport"
"github.com/google/go-tpm/direct/transport/simulator"
)

func CombinedContextTest(t *testing.T) {
func ReadPublicName(t *testing.T, handle tpm.Handle, thetpm transport.TPM) tpm2b.Name {
readPublic := ReadPublic{
ObjectHandle: handle,
}

rspRP, err := readPublic.Execute(thetpm)
if err != nil {
t.Fatalf("Failed to read public: %v", err)
}

return rspRP.Name
}

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

PCR7, err := CreatePCRSelection([]int{7})
if err != nil {
t.Fatalf("Failed to create PCRSelection")
}

createPrimary := CreatePrimary{
PrimaryHandle: tpm.RHOwner,

InPublic: tpm2b.Public{
PublicArea: tpmt.Public{
Type: tpm.AlgECC,
Type: tpm.AlgRSA,
NameAlg: tpm.AlgSHA256,
ObjectAttributes: tpma.Object{
SignEncrypt: true,
FixedTPM: true,
FixedParent: true,
SensitiveDataOrigin: true,
UserWithAuth: true,
SignEncrypt: true,
},
Parameters: tpmu.PublicParms{
ECCDetail: &tpms.ECCParms{
Scheme: tpmt.ECCScheme{
Scheme: tpm.AlgECDSA,
RSADetail: &tpms.RSAParms{
Scheme: tpmt.RSAScheme{
Scheme: tpm.AlgRSASSA,
Details: tpmu.AsymScheme{
ECDSA: &tpms.SigSchemeECDSA{
RSASSA: &tpms.SigSchemeRSASSA{
HashAlg: tpm.AlgSHA256,
},
},
},
CurveID: tpm.ECCNistP256,
KeyBits: 2048,
},
},
},
},
}

rspCP, err := createPrimary.Execute(thetpm)
if err != nil {
t.Fatalf("CreatePrimary failed: %v", err)
}

flushContextCP := FlushContext{FlushHandle: rspCP.ObjectHandle}
defer flushContextCP.Execute(thetpm)

cl := CreateLoaded{
ParentHandle: rspCP.ObjectHandle,
InPublic: tpm2b.Template{
Template: tpmt.Public{
Type: tpm.AlgKeyedHash,
NameAlg: tpm.AlgSHA256,
ObjectAttributes: tpma.Object{
SensitiveDataOrigin: true,
UserWithAuth: true,
Decrypt: true,
Restricted: true,
},
Parameters: tpmu.PublicParms{
KeyedHashDetail: &tpms.KeyedHashParms{
Scheme: tpmt.KeyedHashScheme{
Scheme: tpm.AlgXOR,
Details: tpmu.SchemeKeyedHash{
XOR: &tpms.SchemeXOR{
HashAlg: tpm.AlgSHA256,
KDF: tpm.AlgKDF1SP800108,
},
},
},
},
CreationPCR: tpml.PCRSelection{
PCRSelections: []tpms.PCRSelection{
{
Hash: tpm.AlgSHA1,
PCRSelect: PCR7,
},
},
},
}

rspCrL, err := cl.Execute(thetpm)
rspCP, err := createPrimary.Execute(thetpm)
if err != nil {
t.Fatalf("could not create derivation parent: %v:", err)
t.Fatalf("could not create key: %v", err)
}

flushContextObject := FlushContext{FlushHandle: rspCP.ObjectHandle}
defer flushContextObject.Execute(thetpm)

contextSave := ContextSave{
SaveHandle: rspCrL.ObjectHandle,
SaveHandle: rspCP.ObjectHandle,
}

rspCS, err := contextSave.Execute(thetpm)
if err != nil {
t.Fatalf("ContextSave failed: %v", err)
}

flushContextCL := FlushContext{FlushHandle: rspCrL.ObjectHandle}
flushContextCL.Execute(thetpm)

contextLoad := ContextLoad{
Context: rspCS.Context,
}

rspCoL, err := contextLoad.Execute(thetpm)
rspCL, err := contextLoad.Execute(thetpm)
if err != nil {
t.Fatalf("ContextLoad failed: %v", err)
}

if !cmp.Equal(rspCoL.LoadedHandle, rspCrL.ObjectHandle) {
flushContextLoaded := FlushContext{FlushHandle: rspCL.LoadedHandle}
defer flushContextLoaded.Execute(thetpm)

rspCLName := ReadPublicName(t, rspCL.LoadedHandle, thetpm)
rspCPName := ReadPublicName(t, rspCP.ObjectHandle, thetpm)

if !cmp.Equal(rspCLName, rspCPName) {
t.Error("Mismatch between public returned from ContextLoad & CreateLoaded")
}
}
4 changes: 3 additions & 1 deletion direct/tpm2/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
const (
// Chosen based on MAX_DIGEST_BUFFER, the length of the longest
// reasonable list returned by the reference implementation.
maxListLength uint32 = 1024
// The maxListLength must be greater than MAX_CONTEXT_SIZE = 1344,
// in order to allow for the unmarshalling of Context.
maxListLength uint32 = 4096
)

// execute sends the provided command and returns the TPM's response.
Expand Down

0 comments on commit f4c8bf9

Please sign in to comment.