| 1 |
//go:build cgo |
| 2 |
// +build cgo |
| 3 |
|
| 4 |
package internal |
| 5 |
|
| 6 |
// // Directories containing .h files in the simulator source |
| 7 |
// #cgo CFLAGS: -I ../ms-tpm-20-ref/Samples/Google |
| 8 |
// #cgo CFLAGS: -I ../ms-tpm-20-ref/TPMCmd/tpm/include |
| 9 |
// #cgo CFLAGS: -I ../ms-tpm-20-ref/TPMCmd/tpm/include/prototypes |
| 10 |
// // Allows simulator.c to import files without repeating the source repo path. |
| 11 |
// #cgo CFLAGS: -I ../ms-tpm-20-ref/Samples/Google |
| 12 |
// #cgo CFLAGS: -I ../ms-tpm-20-ref/TPMCmd/tpm/src |
| 13 |
// // Store NVDATA in memory, and we don't care about updates to failedTries. |
| 14 |
// #cgo CFLAGS: -DVTPM=NO -DSIMULATION=NO -DUSE_DA_USED=NO |
| 15 |
// // Flags from ../ms-tpm-20-ref/TPMCmd/configure.ac |
| 16 |
// #cgo CFLAGS: -std=gnu11 -Wall -Wformat-security -fPIC |
| 17 |
// // Windows has linking errors when using stack protectors |
| 18 |
// #cgo !windows CFLAGS: -fstack-protector-all |
| 19 |
// // Silence known warnings from the reference code and CGO code. |
| 20 |
// #cgo CFLAGS: -Wno-missing-braces -Wno-empty-body -Wno-unused-variable -Wno-uninitialized |
| 21 |
// // Silence openssl deprecation warnings for ms-tpm-20-ref |
| 22 |
// #cgo CFLAGS: -Wno-deprecated-declarations |
| 23 |
// // Link against the system OpenSSL |
| 24 |
// #cgo CFLAGS: -DDEBUG=YES |
| 25 |
// #cgo CFLAGS: -DSIMULATION=NO |
| 26 |
// #cgo CFLAGS: -DCOMPILER_CHECKS=DEBUG |
| 27 |
// #cgo CFLAGS: -DRUNTIME_SIZE_CHECKS=DEBUG |
| 28 |
// #cgo CFLAGS: -DUSE_DA_USED=NO |
| 29 |
// #cgo CFLAGS: -DCERTIFYX509_DEBUG=NO |
| 30 |
// #cgo CFLAGS: -DECC_NIST_P224=YES |
| 31 |
// #cgo CFLAGS: -DECC_NIST_P521=YES |
| 32 |
// #cgo CFLAGS: -DALG_SHA512=ALG_YES |
| 33 |
// #cgo CFLAGS: -DMAX_CONTEXT_SIZE=1360 |
| 34 |
// // Flags to find OpenSSL installation on macOS (default Homebrew location) |
| 35 |
// #cgo darwin,amd64 CFLAGS: -I/usr/local/opt/openssl/include |
| 36 |
// #cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/openssl/lib |
| 37 |
// #cgo darwin,arm64 CFLAGS: -I/opt/homebrew/opt/openssl/include |
| 38 |
// #cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/openssl/lib |
| 39 |
// // Flags to find OpenSSL installation on Windows (default install location) |
| 40 |
// #cgo windows CFLAGS: -I"C:/Program Files/OpenSSL-Win64/include" |
| 41 |
// #cgo windows LDFLAGS: -L"C:/Program Files/OpenSSL-Win64/lib" |
| 42 |
// // Link against OpenSSL |
| 43 |
// #cgo LDFLAGS: -lcrypto |
| 44 |
// |
| 45 |
// #include <stdlib.h> |
| 46 |
// #include "Platform.h" |
| 47 |
// #include "Tpm.h" |
| 48 |
// |
| 49 |
// void sync_seeds() { |
| 50 |
// NV_SYNC_PERSISTENT(EPSeed); |
| 51 |
// NV_SYNC_PERSISTENT(SPSeed); |
| 52 |
// NV_SYNC_PERSISTENT(PPSeed); |
| 53 |
// } |
| 54 |
import "C" |
| 55 |
import ( |
| 56 |
"errors" |
| 57 |
"io" |
| 58 |
"unsafe" |
| 59 |
) |
| 60 |
|
| 61 |
// SetSeeds uses the output of r to reset the 3 TPM simulator seeds. |
| 62 |
func SetSeeds(r io.Reader) { |
| 63 |
// The first two bytes of the seed encode the size (so we don't overwrite) |
| 64 |
r.Read(C.gp.EPSeed[2:]) |
| 65 |
r.Read(C.gp.SPSeed[2:]) |
| 66 |
r.Read(C.gp.PPSeed[2:]) |
| 67 |
} |
| 68 |
|
| 69 |
// Reset simulates toggling the power the TPM. If forceManufacture is true, |
| 70 |
// the reset will be a manufacturer reset. |
| 71 |
func Reset(forceManufacture bool) { |
| 72 |
C._plat__Reset(C.bool(forceManufacture)) |
| 73 |
} |
| 74 |
|
| 75 |
// RunCommand passes cmd to the simulator and returns the simulator's response. |
| 76 |
func RunCommand(cmd []byte) ([]byte, error) { |
| 77 |
responseSize := C.uint32_t(C.MAX_RESPONSE_SIZE) |
| 78 |
// _plat__RunCommand takes the response buffer as a uint8_t** instead of as |
| 79 |
// a uint8_t*. As Cgo bans go pointers to go pointers, we must allocate the |
| 80 |
// response buffer with malloc(). |
| 81 |
response := C.malloc(C.size_t(responseSize)) |
| 82 |
defer C.free(response) |
| 83 |
// Make a copy of the response pointer, so we can be sure _plat__RunCommand |
| 84 |
// doesn't modify the pointer (it _is_ expected to modify the buffer). |
| 85 |
responsePtr := (*C.uint8_t)(response) |
| 86 |
|
| 87 |
C._plat__RunCommand(C.uint32_t(len(cmd)), (*C.uint8_t)(&cmd[0]), |
| 88 |
&responseSize, &responsePtr) |
| 89 |
// As long as NO_FAIL_TRACE is not defined, debug error information is |
| 90 |
// written to certain global variables on internal failure. |
| 91 |
if C.g_inFailureMode == C.TRUE { |
| 92 |
return nil, errors.New("unknown internal failure") |
| 93 |
} |
| 94 |
if response != unsafe.Pointer(responsePtr) { |
| 95 |
panic("Response pointer shouldn't be modified on success") |
| 96 |
} |
| 97 |
return C.GoBytes(response, C.int(responseSize)), nil |
| 98 |
} |