2022-04-29 07:21:29 -04:00
|
|
|
//go:build !disable_tpm_simulator
|
|
|
|
|
2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
// TPM2 simulator used for unit tests.
|
2022-04-22 05:06:55 -04:00
|
|
|
package simulator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/google/go-tpm-tools/simulator"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OpenSimulatedTPM returns a simulated TPM device.
|
|
|
|
func OpenSimulatedTPM() (io.ReadWriteCloser, error) {
|
|
|
|
return simulator.Get()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSimulatedTPMOpenFunc returns a TPMOpenFunc that opens a simulated TPM.
|
|
|
|
func NewSimulatedTPMOpenFunc() (func() (io.ReadWriteCloser, error), io.Closer) {
|
|
|
|
tpm, err := OpenSimulatedTPM()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return func() (io.ReadWriteCloser, error) {
|
|
|
|
return &simulatedTPM{tpm}, nil
|
|
|
|
}, tpm
|
|
|
|
}
|
|
|
|
|
|
|
|
type simulatedTPM struct {
|
|
|
|
openSimulatedTPM io.ReadWriteCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *simulatedTPM) Read(p []byte) (int, error) {
|
|
|
|
return t.openSimulatedTPM.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *simulatedTPM) Write(p []byte) (int, error) {
|
|
|
|
return t.openSimulatedTPM.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *simulatedTPM) Close() error {
|
|
|
|
// never close the underlying simulated TPM to allow calling the TPMOpenFunc again
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-12 11:26:54 -05:00
|
|
|
|
|
|
|
func (*simulatedTPM) EventLog() ([]byte, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|