diff --git a/direct/tpm2/get_random_test.go b/direct/tpm2/get_random_test.go new file mode 100644 index 00000000..8951c340 --- /dev/null +++ b/direct/tpm2/get_random_test.go @@ -0,0 +1,23 @@ +package tpm2 + +import ( + "testing" + + "github.com/google/go-tpm/direct/transport/simulator" +) + +func TestGetRandom(t *testing.T) { + thetpm, err := simulator.OpenSimulator() + if err != nil { + t.Fatalf("could not connect to TPM simulator: %v", err) + } + defer thetpm.Close() + + grc := GetRandom{ + BytesRequested: 16, + } + + if _, err := grc.Execute(thetpm); err != nil { + t.Fatalf("GetRandom failed: %v", err) + } +} diff --git a/direct/tpm2/tpm2.go b/direct/tpm2/tpm2.go index 6060d17c..2487a88d 100644 --- a/direct/tpm2/tpm2.go +++ b/direct/tpm2/tpm2.go @@ -387,6 +387,34 @@ type CreateLoadedResponse struct { // Response implements the Response interface. func (*CreateLoadedResponse) Response() tpm.CC { return tpm.CCCreateLoaded } +// GetRandom is the input to TPM2_GetRandom. +// See definition in Part 3, Commands, section 16.1 +type GetRandom struct { + // number of octets to return + BytesRequested uint16 +} + +// Command implements the Command interface. +func (*GetRandom) Command() tpm.CC { return tpm.CCGetRandom } + +// Execute executes the command and returns the response. +func (cmd *GetRandom) Execute(t transport.TPM, s ...Session) (*GetRandomResponse, error) { + var rsp GetRandomResponse + if err := execute(t, cmd, &rsp, s...); err != nil { + return nil, err + } + return &rsp, nil +} + +// GetRandomReponse is the reponse from TPM2_GetRandom. +type GetRandomResponse struct { + // the random octets + RandomBytes tpm2b.Digest +} + +// Reponse implements the Response interface. +func (*GetRandomResponse) Response() tpm.CC { return tpm.CCGetRandom } + // Quote is the input to TPM2_Quote. // See definition in Part 3, Commands, section 18.4 type Quote struct {