| 1 |
package tpm2test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/sha1" |
| 6 |
"crypto/sha256" |
| 7 |
"crypto/sha512" |
| 8 |
"fmt" |
| 9 |
"testing" |
| 10 |
|
| 11 |
. "github.com/google/go-tpm/tpm2" |
| 12 |
"github.com/google/go-tpm/tpm2/transport/simulator" |
| 13 |
) |
| 14 |
|
| 15 |
func TestPCRs(t *testing.T) { |
| 16 |
for i, tc := range []struct { |
| 17 |
pcrs []uint |
| 18 |
wantSelect []byte |
| 19 |
}{ |
| 20 |
{ |
| 21 |
pcrs: nil, |
| 22 |
wantSelect: []byte{0x00, 0x00, 0x00}, |
| 23 |
}, |
| 24 |
{ |
| 25 |
pcrs: []uint{0}, |
| 26 |
wantSelect: []byte{0x01, 0x00, 0x00}, |
| 27 |
}, |
| 28 |
{ |
| 29 |
pcrs: []uint{0, 1, 2}, |
| 30 |
wantSelect: []byte{0x07, 0x00, 0x00}, |
| 31 |
}, |
| 32 |
{ |
| 33 |
pcrs: []uint{0, 7}, |
| 34 |
wantSelect: []byte{0x81, 0x00, 0x00}, |
| 35 |
}, |
| 36 |
{ |
| 37 |
pcrs: []uint{8}, |
| 38 |
wantSelect: []byte{0x00, 0x01, 0x00}, |
| 39 |
}, |
| 40 |
{ |
| 41 |
pcrs: []uint{1, 8, 9}, |
| 42 |
wantSelect: []byte{0x02, 0x03, 0x00}, |
| 43 |
}, |
| 44 |
{ |
| 45 |
pcrs: []uint{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}, |
| 46 |
wantSelect: []byte{0xff, 0xff, 0xff}, |
| 47 |
}, |
| 48 |
{ |
| 49 |
pcrs: []uint{255}, |
| 50 |
wantSelect: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 51 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, |
| 52 |
}, |
| 53 |
} { |
| 54 |
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { |
| 55 |
selection := PCClientCompatible.PCRs(tc.pcrs...) |
| 56 |
if !bytes.Equal(selection, tc.wantSelect) { |
| 57 |
t.Errorf("PCRs() = 0x%x, want 0x%x", selection, tc.wantSelect) |
| 58 |
} |
| 59 |
}) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
var extendstpm2 = map[TPMAlgID][]struct { |
| 64 |
digest []byte |
| 65 |
}{ |
| 66 |
TPMAlgSHA1: { |
| 67 |
{bytes.Repeat([]byte{0x00}, sha1.Size)}, |
| 68 |
{bytes.Repeat([]byte{0x01}, sha1.Size)}, |
| 69 |
{bytes.Repeat([]byte{0x02}, sha1.Size)}}, |
| 70 |
TPMAlgSHA256: { |
| 71 |
{bytes.Repeat([]byte{0x00}, sha256.Size)}, |
| 72 |
{bytes.Repeat([]byte{0x01}, sha256.Size)}, |
| 73 |
{bytes.Repeat([]byte{0x02}, sha256.Size)}}, |
| 74 |
TPMAlgSHA384: { |
| 75 |
{bytes.Repeat([]byte{0x00}, sha512.Size384)}, |
| 76 |
{bytes.Repeat([]byte{0x01}, sha512.Size384)}, |
| 77 |
{bytes.Repeat([]byte{0x02}, sha512.Size384)}}, |
| 78 |
} |
| 79 |
|
| 80 |
func allZero(s []byte) bool { |
| 81 |
for _, v := range s { |
| 82 |
if v != 0 { |
| 83 |
return false |
| 84 |
} |
| 85 |
} |
| 86 |
return true |
| 87 |
} |
| 88 |
|
| 89 |
func TestPCRReset(t *testing.T) { |
| 90 |
thetpm, err := simulator.OpenSimulator() |
| 91 |
if err != nil { |
| 92 |
t.Fatalf("could not connect to TPM simulator: %v", err) |
| 93 |
} |
| 94 |
defer thetpm.Close() |
| 95 |
|
| 96 |
DebugPCR := uint(16) |
| 97 |
|
| 98 |
cases := []struct { |
| 99 |
name string |
| 100 |
hashalg TPMAlgID |
| 101 |
}{ |
| 102 |
{"SHA1", TPMAlgSHA1}, |
| 103 |
{"SHA256", TPMAlgSHA256}, |
| 104 |
{"SHA384", TPMAlgSHA384}, |
| 105 |
} |
| 106 |
|
| 107 |
for _, c := range cases { |
| 108 |
t.Run(c.name, func(t *testing.T) { |
| 109 |
authHandle := AuthHandle{ |
| 110 |
Handle: TPMHandle(DebugPCR), |
| 111 |
Auth: PasswordAuth(nil), |
| 112 |
} |
| 113 |
|
| 114 |
pcrRead := PCRRead{ |
| 115 |
PCRSelectionIn: TPMLPCRSelection{ |
| 116 |
PCRSelections: []TPMSPCRSelection{ |
| 117 |
{ |
| 118 |
Hash: c.hashalg, |
| 119 |
PCRSelect: PCClientCompatible.PCRs(DebugPCR), |
| 120 |
}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
} |
| 124 |
|
| 125 |
// Extending PCR 16 |
| 126 |
for _, d := range extendstpm2[c.hashalg] { |
| 127 |
pcrExtend := PCRExtend{ |
| 128 |
PCRHandle: authHandle, |
| 129 |
Digests: TPMLDigestValues{ |
| 130 |
Digests: []TPMTHA{ |
| 131 |
{ |
| 132 |
HashAlg: c.hashalg, |
| 133 |
Digest: d.digest, |
| 134 |
}, |
| 135 |
}, |
| 136 |
}, |
| 137 |
} |
| 138 |
if _, err := pcrExtend.Execute(thetpm); err != nil { |
| 139 |
t.Fatalf("failed to extend pcr for test %v", err) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
pcrReadRsp, err := pcrRead.Execute(thetpm) |
| 144 |
if err != nil { |
| 145 |
t.Fatalf("failed to read PCRs") |
| 146 |
} |
| 147 |
postExtendPCR16 := pcrReadRsp.PCRValues.Digests[0].Buffer |
| 148 |
if allZero(postExtendPCR16) { |
| 149 |
t.Errorf("postExtendPCR16 not expected to be all Zero: %v", postExtendPCR16) |
| 150 |
} |
| 151 |
|
| 152 |
// Resetting PCR 16 |
| 153 |
pcrReset := PCRReset{ |
| 154 |
PCRHandle: authHandle, |
| 155 |
} |
| 156 |
if _, err := pcrReset.Execute(thetpm); err != nil { |
| 157 |
t.Fatalf("pcrReset failed: %v", err) |
| 158 |
} |
| 159 |
if pcrReadRsp, err = pcrRead.Execute(thetpm); err != nil { |
| 160 |
t.Fatalf("failed to read PCRs") |
| 161 |
} |
| 162 |
postResetPCR16 := pcrReadRsp.PCRValues.Digests[0].Buffer |
| 163 |
|
| 164 |
if !allZero(postResetPCR16) { |
| 165 |
t.Errorf("postResetPCR16 expected to be all Zero: %v", postExtendPCR16) |
| 166 |
} |
| 167 |
}) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func TestPCREvent(t *testing.T) { |
| 172 |
thetpm, err := simulator.OpenSimulator() |
| 173 |
if err != nil { |
| 174 |
t.Fatalf("could not connect to TPM simulator: %v", err) |
| 175 |
} |
| 176 |
defer thetpm.Close() |
| 177 |
|
| 178 |
cases := []struct { |
| 179 |
name string |
| 180 |
hashalg TPMAlgID |
| 181 |
}{ |
| 182 |
{"SHA1", TPMAlgSHA1}, |
| 183 |
{"SHA256", TPMAlgSHA256}, |
| 184 |
{"SHA384", TPMAlgSHA384}, |
| 185 |
} |
| 186 |
|
| 187 |
// Extend every SRTM PCR with TPM2_PCR_Event |
| 188 |
for _, c := range cases { |
| 189 |
t.Run(c.name, func(t *testing.T) { |
| 190 |
for i := 0; i < 17; i++ { |
| 191 |
t.Run(fmt.Sprintf("PCR%02d", i), func(t *testing.T) { |
| 192 |
pcrRead := PCRRead{ |
| 193 |
PCRSelectionIn: TPMLPCRSelection{ |
| 194 |
PCRSelections: []TPMSPCRSelection{ |
| 195 |
{ |
| 196 |
Hash: c.hashalg, |
| 197 |
PCRSelect: PCClientCompatible.PCRs(20), |
| 198 |
}, |
| 199 |
}, |
| 200 |
}, |
| 201 |
} |
| 202 |
|
| 203 |
pcrEvent := PCREvent{ |
| 204 |
PCRHandle: TPMHandle(i), |
| 205 |
EventData: TPM2BEvent{Buffer: []byte("hello")}, |
| 206 |
} |
| 207 |
if _, err := pcrEvent.Execute(thetpm); err != nil { |
| 208 |
t.Fatalf("failed to extend pcr for test %v", err) |
| 209 |
} |
| 210 |
|
| 211 |
pcrReadRsp, err := pcrRead.Execute(thetpm) |
| 212 |
if err != nil { |
| 213 |
t.Fatalf("failed to read PCRs") |
| 214 |
} |
| 215 |
postExtendPCR16 := pcrReadRsp.PCRValues.Digests[0].Buffer |
| 216 |
if allZero(postExtendPCR16) { |
| 217 |
t.Errorf("postExtendPCR16 not expected to be all Zero: %v", postExtendPCR16) |
| 218 |
} |
| 219 |
}) |
| 220 |
} |
| 221 |
}) |
| 222 |
} |
| 223 |
} |