| 1 |
package tpm2test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
|
| 7 |
. "github.com/google/go-tpm/tpm2" |
| 8 |
"github.com/google/go-tpm/tpm2/transport/simulator" |
| 9 |
) |
| 10 |
|
| 11 |
func TestHandleName(t *testing.T) { |
| 12 |
want := []byte{0x40, 0x00, 0x00, 0x0B} |
| 13 |
name := HandleName(TPMRHEndorsement) |
| 14 |
if !bytes.Equal(want, name.Buffer) { |
| 15 |
t.Errorf("Incorrect name for RH_ENDORSEMENT (want %x got %x)", want, name.Buffer) |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
func TestObjectName(t *testing.T) { |
| 20 |
thetpm, err := simulator.OpenSimulator() |
| 21 |
if err != nil { |
| 22 |
t.Fatalf("could not connect to TPM simulator: %v", err) |
| 23 |
} |
| 24 |
defer thetpm.Close() |
| 25 |
|
| 26 |
createPrimary := CreatePrimary{ |
| 27 |
PrimaryHandle: TPMRHEndorsement, |
| 28 |
InPublic: New2B(ECCEKTemplate), |
| 29 |
} |
| 30 |
rsp, err := createPrimary.Execute(thetpm) |
| 31 |
if err != nil { |
| 32 |
t.Fatalf("could not call TPM2_CreatePrimary: %v", err) |
| 33 |
} |
| 34 |
flush := FlushContext{FlushHandle: rsp.ObjectHandle} |
| 35 |
defer flush.Execute(thetpm) |
| 36 |
public := rsp.OutPublic |
| 37 |
|
| 38 |
want := rsp.Name |
| 39 |
pub, err := public.Contents() |
| 40 |
if err != nil { |
| 41 |
t.Fatalf("%v", err) |
| 42 |
} |
| 43 |
name, err := ObjectName(pub) |
| 44 |
if err != nil { |
| 45 |
t.Fatalf("error from ObjectName: %v", err) |
| 46 |
} |
| 47 |
if !bytes.Equal(want.Buffer, name.Buffer) { |
| 48 |
t.Errorf("Incorrect name for ECC EK (want %x got %x)", want.Buffer, name.Buffer) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func TestNVName(t *testing.T) { |
| 53 |
thetpm, err := simulator.OpenSimulator() |
| 54 |
if err != nil { |
| 55 |
t.Fatalf("could not connect to TPM simulator: %v", err) |
| 56 |
} |
| 57 |
defer thetpm.Close() |
| 58 |
|
| 59 |
public := New2B( |
| 60 |
TPMSNVPublic{ |
| 61 |
NVIndex: TPMHandle(0x0180000F), |
| 62 |
NameAlg: TPMAlgSHA256, |
| 63 |
Attributes: TPMANV{ |
| 64 |
OwnerWrite: true, |
| 65 |
OwnerRead: true, |
| 66 |
NT: TPMNTOrdinary, |
| 67 |
}, |
| 68 |
DataSize: 4, |
| 69 |
}) |
| 70 |
|
| 71 |
defineSpace := NVDefineSpace{ |
| 72 |
AuthHandle: TPMRHOwner, |
| 73 |
PublicInfo: public, |
| 74 |
} |
| 75 |
if _, err := defineSpace.Execute(thetpm); err != nil { |
| 76 |
t.Fatalf("could not call TPM2_DefineSpace: %v", err) |
| 77 |
} |
| 78 |
|
| 79 |
pub, err := public.Contents() |
| 80 |
if err != nil { |
| 81 |
t.Fatalf("%v", err) |
| 82 |
} |
| 83 |
readPublic := NVReadPublic{ |
| 84 |
NVIndex: pub.NVIndex, |
| 85 |
} |
| 86 |
rsp, err := readPublic.Execute(thetpm) |
| 87 |
if err != nil { |
| 88 |
t.Fatalf("could not call TPM2_ReadPublic: %v", err) |
| 89 |
} |
| 90 |
|
| 91 |
want := rsp.NVName |
| 92 |
name, err := NVName(pub) |
| 93 |
if err != nil { |
| 94 |
t.Fatalf("error from NVIndexName: %v", err) |
| 95 |
} |
| 96 |
if !bytes.Equal(want.Buffer, name.Buffer) { |
| 97 |
t.Errorf("Incorrect name for NV index (want %x got %x)", want.Buffer, name.Buffer) |
| 98 |
} |
| 99 |
} |