google / go-tpm

Apache License 2.0
560 stars 162 forks source link

GetRandom #264

Closed 538Michael closed 3 years ago

538Michael commented 3 years ago

How to use tpm2.GetRandom? What's this ReadWriter the method asks?

chrisfenner commented 3 years ago

Hi, @538Michael!

The io.ReadWriter is the generic transport interface for talking to the TPM. (This makes it easier to dependency-inject a different way of talking to TPM into the library, which concerns itself more with what to say than with how to get the bits there.)

The test is a good place to see how this is used: https://github.com/google/go-tpm/blob/90fb6247068f5197c3bd29e4085cf50124b2bece/tpm2/test/tpm2_test.go#L118-L125

The test uses helpers that chain up to one of the following methods:

On Windows, OpenTPM returns the TPM interface: https://github.com/google/go-tpm/blob/90fb6247068f5197c3bd29e4085cf50124b2bece/tpm2/open_windows.go#L26

On Linux, another implementation of OpenTPM returns it: https://github.com/google/go-tpm/blob/90fb6247068f5197c3bd29e4085cf50124b2bece/tpm2/open_other.go#L34