From f1c03aaa4e58492c76aa7c896d8e7996cff6a4df Mon Sep 17 00:00:00 2001 From: Kai Michaelis Date: Wed, 29 Mar 2023 03:17:07 -0700 Subject: [PATCH] add TPM2_SelfTest and TPM2_GetTestResult --- tpm2/constants.go | 2 ++ tpm2/tpm2.go | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/tpm2/constants.go b/tpm2/constants.go index 2b0de544..18b56ce1 100644 --- a/tpm2/constants.go +++ b/tpm2/constants.go @@ -456,6 +456,7 @@ const ( CmdPCREvent tpmutil.Command = 0x0000013C CmdPCRReset tpmutil.Command = 0x0000013D CmdSequenceComplete tpmutil.Command = 0x0000013E + CmdSelfTest tpmutil.Command = 0x00000143 CmdStartup tpmutil.Command = 0x00000144 CmdShutdown tpmutil.Command = 0x00000145 CmdActivateCredential tpmutil.Command = 0x00000147 @@ -489,6 +490,7 @@ const ( CmdStartAuthSession tpmutil.Command = 0x00000176 CmdGetCapability tpmutil.Command = 0x0000017A CmdGetRandom tpmutil.Command = 0x0000017B + CmdGetTestResult tpmutil.Command = 0x0000017C CmdHash tpmutil.Command = 0x0000017D CmdPCRRead tpmutil.Command = 0x0000017E CmdPolicyPCR tpmutil.Command = 0x0000017F diff --git a/tpm2/tpm2.go b/tpm2/tpm2.go index 0f2d32c7..8ecfe5d7 100644 --- a/tpm2/tpm2.go +++ b/tpm2/tpm2.go @@ -24,6 +24,32 @@ import ( "github.com/google/go-tpm/tpmutil" ) +func SelfTest(rw io.ReadWriter, fullTest bool) error { + var yesno uint8 + if fullTest { + yesno = 1 + } + _, err := runCommand(rw, TagNoSessions, CmdSelfTest, yesno) + if err != nil { + return err + } + + return nil +} + +func GetTestResult(rw io.ReadWriter) ([]byte, error) { + resp, err := runCommand(rw, TagNoSessions, CmdGetTestResult) + if err != nil { + return nil, err + } + + var testResult tpmutil.U16Bytes + if _, err := tpmutil.Unpack(resp, &testResult); err != nil { + return nil, err + } + return testResult, nil +} + // GetRandom gets random bytes from the TPM. func GetRandom(rw io.ReadWriter, size uint16) ([]byte, error) { resp, err := runCommand(rw, TagNoSessions, CmdGetRandom, size)