Skip to content

Commit

Permalink
Implement and tested go-tpm direct function TPM2_GetRandom (google#277)
Browse files Browse the repository at this point in the history
* Implement and tested go-tpm direct function TPM2_GetRandom

* Resolved all nits from PR

Co-authored-by: Matthew Tsai <matthewtsai@google.com>
  • Loading branch information
matt-tsai and Matthew Tsai committed Jun 21, 2022
1 parent d50cbcd commit 7cdbba3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
23 changes: 23 additions & 0 deletions direct/tpm2/get_random_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
28 changes: 28 additions & 0 deletions direct/tpm2/tpm2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7cdbba3

Please sign in to comment.