| 1 |
// Package test provides helper methods for testing. It should never be |
| 2 |
// included in non-test libraries/binaries. |
| 3 |
package test |
| 4 |
|
| 5 |
import ( |
| 6 |
"crypto/rand" |
| 7 |
"crypto/rsa" |
| 8 |
"io" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/google/go-tpm/legacy/tpm2" |
| 12 |
"github.com/google/go-tpm/tpmutil" |
| 13 |
) |
| 14 |
|
| 15 |
// LoadRandomExternalKey loads a randomly generated external key into the |
| 16 |
// TPM simulator and returns its' handle. If any errors occur, calls Fatal() |
| 17 |
// on the passed testing.TB. |
| 18 |
func LoadRandomExternalKey(tb testing.TB, rw io.ReadWriter) tpmutil.Handle { |
| 19 |
tb.Helper() |
| 20 |
pk, err := rsa.GenerateKey(rand.Reader, 2048) |
| 21 |
if err != nil { |
| 22 |
tb.Fatal(err) |
| 23 |
} |
| 24 |
public := tpm2.Public{ |
| 25 |
Type: tpm2.AlgRSA, |
| 26 |
NameAlg: tpm2.AlgSHA1, |
| 27 |
Attributes: tpm2.FlagSign | tpm2.FlagUserWithAuth, |
| 28 |
RSAParameters: &tpm2.RSAParams{ |
| 29 |
Sign: &tpm2.SigScheme{ |
| 30 |
Alg: tpm2.AlgRSASSA, |
| 31 |
Hash: tpm2.AlgSHA1, |
| 32 |
}, |
| 33 |
KeyBits: 2048, |
| 34 |
ExponentRaw: uint32(pk.PublicKey.E), |
| 35 |
ModulusRaw: pk.PublicKey.N.Bytes(), |
| 36 |
}, |
| 37 |
} |
| 38 |
private := tpm2.Private{ |
| 39 |
Type: tpm2.AlgRSA, |
| 40 |
Sensitive: pk.Primes[0].Bytes(), |
| 41 |
} |
| 42 |
handle, _, err := tpm2.LoadExternal(rw, public, private, tpm2.HandleNull) |
| 43 |
if err != nil { |
| 44 |
tb.Error(err) |
| 45 |
} |
| 46 |
return handle |
| 47 |
} |