1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
package tpm2test
import (
"bytes"
"encoding/binary"
"testing"
. "github.com/google/go-tpm/tpm2"
"github.com/google/go-tpm/tpm2/transport/simulator"
)
func TestNVAuthWrite(t *testing.T) {
thetpm, err := simulator.OpenSimulator()
if err != nil {
t.Fatalf("could not connect to TPM simulator: %v", err)
}
defer thetpm.Close()
def := NVDefineSpace{
AuthHandle: TPMRHOwner,
Auth: TPM2BAuth{
Buffer: []byte("p@ssw0rd"),
},
PublicInfo: New2B(
TPMSNVPublic{
NVIndex: TPMHandle(0x0180000F),
NameAlg: TPMAlgSHA256,
Attributes: TPMANV{
OwnerWrite: true,
OwnerRead: true,
AuthWrite: true,
AuthRead: true,
NT: TPMNTOrdinary,
NoDA: true,
},
DataSize: 4,
}),
}
if _, err := def.Execute(thetpm); err != nil {
t.Fatalf("Calling TPM2_NV_DefineSpace: %v", err)
}
pub, err := def.PublicInfo.Contents()
if err != nil {
t.Fatalf("%v", err)
}
nvName, err := NVName(pub)
if err != nil {
t.Fatalf("Calculating name of NV index: %v", err)
}
prewrite := NVWrite{
AuthHandle: AuthHandle{
Handle: pub.NVIndex,
Name: *nvName,
Auth: PasswordAuth([]byte("p@ssw0rd")),
},
NVIndex: NamedHandle{
Handle: pub.NVIndex,
Name: *nvName,
},
Data: TPM2BMaxNVBuffer{
Buffer: []byte{0x01, 0x02, 0x03, 0x04},
},
Offset: 0,
}
if _, err := prewrite.Execute(thetpm); err != nil {
t.Errorf("Calling TPM2_NV_Write: %v", err)
}
read := NVReadPublic{
NVIndex: pub.NVIndex,
}
readRsp, err := read.Execute(thetpm)
if err != nil {
t.Fatalf("Calling TPM2_NV_ReadPublic: %v", err)
}
t.Logf("Name: %x", readRsp.NVName.Buffer)
write := NVWrite{
AuthHandle: AuthHandle{
Handle: TPMRHOwner,
Auth: HMAC(TPMAlgSHA256, 16, Auth([]byte{})),
},
NVIndex: NamedHandle{
Handle: pub.NVIndex,
Name: readRsp.NVName,
},
Data: TPM2BMaxNVBuffer{
Buffer: []byte{0x01, 0x02, 0x03, 0x04},
},
Offset: 0,
}
if _, err := write.Execute(thetpm); err != nil {
t.Errorf("Calling TPM2_NV_Write: %v", err)
}
}
func TestNVAuthIncrement(t *testing.T) {
thetpm, err := simulator.OpenSimulator()
if err != nil {
t.Fatalf("could not connect to TPM simulator: %v", err)
}
defer thetpm.Close()
// Define the counter space
def := NVDefineSpace{
AuthHandle: TPMRHOwner,
Auth: TPM2BAuth{
Buffer: []byte("p@ssw0rd"),
},
PublicInfo: New2B(
TPMSNVPublic{
NVIndex: TPMHandle(0x0180000F),
NameAlg: TPMAlgSHA256,
Attributes: TPMANV{
OwnerWrite: true,
OwnerRead: true,
AuthWrite: true,
AuthRead: true,
NT: TPMNTCounter,
NoDA: true,
},
DataSize: 8,
}),
}
if _, err := def.Execute(thetpm); err != nil {
t.Fatalf("Calling TPM2_NV_DefineSpace: %v", err)
}
pub, err := def.PublicInfo.Contents()
if err != nil {
t.Fatalf("%v", err)
}
// Calculate the Name of the index as of its creation
// (i.e., without NV_WRITTEN set).
nvName, err := NVName(pub)
if err != nil {
t.Fatalf("Calculating name of NV index: %v", err)
}
incr := NVIncrement{
AuthHandle: AuthHandle{
Handle: TPMRHOwner,
Auth: HMAC(TPMAlgSHA256, 16, Auth([]byte{})),
},
NVIndex: NamedHandle{
Handle: pub.NVIndex,
Name: *nvName,
},
}
if _, err := incr.Execute(thetpm); err != nil {
t.Errorf("Calling TPM2_NV_Increment: %v", err)
}
// The NV index's Name has changed. Ask the TPM for it.
readPub := NVReadPublic{
NVIndex: pub.NVIndex,
}
readPubRsp, err := readPub.Execute(thetpm)
if err != nil {
t.Fatalf("Calling TPM2_NV_ReadPublic: %v", err)
}
incr.NVIndex = NamedHandle{
Handle: pub.NVIndex,
Name: readPubRsp.NVName,
}
read := NVRead{
AuthHandle: AuthHandle{
Handle: TPMRHOwner,
Auth: HMAC(TPMAlgSHA256, 16, Auth([]byte{})),
},
NVIndex: NamedHandle{
Handle: pub.NVIndex,
Name: readPubRsp.NVName,
},
Size: 8,
}
readRsp, err := read.Execute(thetpm)
if err != nil {
t.Fatalf("Calling TPM2_NV_Read: %v", err)
}
if _, err := incr.Execute(thetpm); err != nil {
t.Errorf("Calling TPM2_NV_Increment: %v", err)
}
var val1 uint64
err = binary.Read(bytes.NewReader(readRsp.Data.Buffer), binary.BigEndian, &val1)
if err != nil {
t.Fatalf("Parsing counter: %v", err)
}
readRsp, err = read.Execute(thetpm)
if err != nil {
t.Fatalf("Calling TPM2_NV_Read: %v", err)
}
var val2 uint64
err = binary.Read(bytes.NewReader(readRsp.Data.Buffer), binary.BigEndian, &val2)
if err != nil {
t.Fatalf("Parsing counter: %v", err)
}
if val2 != (val1 + 1) {
t.Errorf("want %v got %v", val1+1, val2)
}
}
|